File: AnisoMeshSize.cc

package info (click to toggle)
madlib 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,196 kB
  • sloc: cpp: 39,851; sh: 10,041; makefile: 473
file content (281 lines) | stat: -rw-r--r-- 9,288 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information. 
// You should have received a copy of these files along with MAdLib. 
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------

#include "AnisoMeshSize.h"
#include "MathUtils.h"
#include "MAdMessage.h"
#include "MAdDefines.h"
#include "MeshParametersManager.h"

#include <math.h>
#include <iostream>
using std::string;

namespace MAd {

  // -------------------------------------------------------------------
  AnisoMeshSize::AnisoMeshSize(double dirs[3][3], double _h[3]):
    MeshSizeBase()
  {
    if( _h[0] <= 0. || _h[1] <= 0. || _h[2] <= 0. ) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Negative size(s): %f %f %f",
                                  _h[0], _h[1], _h[2]);
    }

    double t1[3], t2[3], t3[3];
    double l1,l2,l3;

    // sort the sizes from lowest to highest and fill first direction (GCRemark: not useful)
    int hMin = indexOfMin(_h[0], _h[1], _h[2]);
    l1 = 1. / (_h[hMin] * _h[hMin]);
    double normInv = 1. / sqrt ( dirs[hMin][0] * dirs[hMin][0] + 
                                 dirs[hMin][1] * dirs[hMin][1] + 
                                 dirs[hMin][2] * dirs[hMin][2]   );
    for (int i=0; i<3; i++) t1[i] = dirs[hMin][i] * normInv;

    hMin++;
    if( _h[(hMin+1)%3] > _h[(hMin)%3] )
      {
        l2 = 1. / (_h[hMin%3] * _h[hMin%3]);
        l3 = 1. / (_h[(hMin+1)%3] * _h[(hMin+1)%3]);
        hMin  = hMin%3;
      }
    else
      {
        l2 = 1. / (_h[(hMin+1)%3] * _h[(hMin+1)%3]);
        l3 = 1. / (_h[hMin%3] * _h[hMin%3]);
        hMin = (hMin+1)%3;
      }

    // project dir 1 so that it is perpendicular to dir 0
    double cosa = dotProd(t1,dirs[hMin]);
    double vec[3];
    for( int i=0; i<3; i++ ) vec[i] = dirs[hMin][i] - cosa * t1[i];
    normInv = 1. / sqrt ( vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] );
    for (int i=0; i<3; i++) t2[i] = vec[i] * normInv;
    
    // find last dir as the direction orthogonal to the two previous ones
    crossProd(t1,t2,t3);

    // build the metric
    M = MAdMetric(l1,l2,l3,t1,t2,t3);
  }

  // -------------------------------------------------------------------
  AnisoMeshSize::AnisoMeshSize(double dir[3], double hDir, double hTg):
    MeshSizeBase()
  {
    if( hDir <= 0. || hTg <= 0. ) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,"Negative size(s): %f %f",
                                  hDir, hTg);
    }
    
    double e[3][3];
    double len[3];

    // see if 'dir' is the direction of the minimal length
    int iDir = 0;
    if ( hDir > hTg ) iDir = 2;

    // set the 'dir' direction and its length
    normalizeVec(dir,e[iDir]);
    len[iDir] = 1. / ( hDir * hDir );

    // set other sizes in the right order
    len[(iDir+1)%3] = 1. / ( hTg * hTg );
    len[(iDir+2)%3] = len[(iDir+1)%3];

    // find a perpendicular direction to dir
    double dummy[3];
    dummy[0] = 2. * ( e[iDir][0] + 1.3654364 );
    dummy[1] = 3. * ( e[iDir][1] + 3.1368136 );
    dummy[2] = 5. * ( e[iDir][2] + 7.3683686 );
    normalizeVec(dummy,dummy);
    double cosa = dotProd(e[iDir],dummy);
    for( int i=0; i<3; i++ ) dummy[i] -= cosa * e[iDir][i];
    normalizeVec(dummy,e[(iDir+1)%3]);
    
    // find last dir as the direction orthogonal to the two previous ones
    crossProd(e[iDir],e[(iDir+1)%3],e[(iDir+2)%3]);

    // build the metric
    M = MAdMetric(len[0],len[1],len[2],e[0],e[1],e[2]);
  }

  // -------------------------------------------------------------------
  AnisoMeshSize::AnisoMeshSize(double h):
    MeshSizeBase()
  {
    M = MAdMetric( 1./(h*h) );
  }

  // -------------------------------------------------------------------
  AnisoMeshSize::AnisoMeshSize(const AnisoMeshSize &pm):
    MeshSizeBase()
  {
    M = pm.M;
  }

  // -------------------------------------------------------------------
  MeshSizeType AnisoMeshSize::getType() const 
  {
    return ANISOTROPIC;
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::intersect(const pMSize pMS1, const pMSize pMS2)
  {
    M = intersection(pMS1->getMetric(),pMS2->getMetric());
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::interpolate(const pMSize pMS0, const pMSize pMS1, 
                                  double param)
  {
    M = interpolation(pMS0->getMetric(),pMS1->getMetric(),param);
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::interpolate(const pMSize pMS0, const pMSize pMS1, 
                                  const pMSize pMS2, double u, double v)
  {
    M = interpolation(pMS0->getMetric(), pMS1->getMetric(), 
                      pMS2->getMetric(), u, v);
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::interpolate(const pMSize pMS0, const pMSize pMS1, 
                                  const pMSize pMS2, const pMSize pMS3, 
                                  double u, double v, double w)
  {
    M = interpolation(pMS0->getMetric(), pMS1->getMetric(), 
                      pMS2->getMetric(), pMS3->getMetric(), 
                      u, v, w);
  }

  // -------------------------------------------------------------------
  double AnisoMeshSize::size(int i) const
  {
    doubleMatrix V = doubleMatrix(3,3);
    doubleVector S = doubleVector(3);
    M.eig(V,S,true);
    double s = 1. / sqrt( S(i) );
    if ( isnan(s) ) return MeshParametersManagerSgl::instance().getBigLength();
    return s;
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::sizes(double _h[3]) const
  {
    doubleMatrix V = doubleMatrix(3,3);
    doubleVector S = doubleVector(3);
    M.eig(V,S,true);
    for (int i=0; i<3; i++) {
      _h[i] = ( 1. / sqrt( S(i) ) );
      if ( isnan(_h[i]) ) _h[i] = MeshParametersManagerSgl::instance().getBigLength();
    }
  }

  // -------------------------------------------------------------------
  double AnisoMeshSize::direction(int i, double dir[3]) const 
  {
    doubleMatrix V = doubleMatrix(3,3);
    doubleVector S = doubleVector(3);
    M.eig(V,S,true); // each column of V is a direction, S gives corresponding 1/h^2
    for (int iC=0; iC<3; iC++) dir[iC] = V(iC,i);
    double s = 1. / sqrt( S(i) );
    if ( isnan(s) ) return MeshParametersManagerSgl::instance().getBigLength();
    return s;
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::scale(int dir, double factor)
  {
    MAdMsgSgl::instance().error(__LINE__,__FILE__,"Not implemented");
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::scale(double factor)
  {
    MAdMsgSgl::instance().error(__LINE__,__FILE__,"Not implemented");
  }

  // -------------------------------------------------------------------
  double AnisoMeshSize::getMeanLength() const
  {
    doubleMatrix V = doubleMatrix(3,3);
    doubleVector S = doubleVector(3);
    M.eig(V,S,false);
    double mean = 0.;
    for (int i=0; i<3; i++) mean += 1. / sqrt( S(i) );
    return ( MAdTHIRD * mean );
  }

  // -------------------------------------------------------------------
  double AnisoMeshSize::getMinLength() const
  {
    doubleMatrix V = doubleMatrix(3,3);
    doubleVector S = doubleVector(3);
    M.eig(V,S,false);
    int i = indexOfMax(S(0),S(1),S(2));
    return ( 1. / sqrt( S(i) ) );
  }

  // -------------------------------------------------------------------
  double AnisoMeshSize::getMaxLength() const
  {
    doubleMatrix V = doubleMatrix(3,3);
    doubleVector S = doubleVector(3);
    M.eig(V,S,false);
    int i = indexOfMin(S(0),S(1),S(2));
    return ( 1. / sqrt( S(i) ) );
  }

  // -------------------------------------------------------------------
  // get the square of the norm in the metric
  double AnisoMeshSize::normSq(const double vec[3]) const
  {
    return dot(vec,M,vec);
  }

  // -------------------------------------------------------------------
  // get the square of desired edge length along 'vec'
  double AnisoMeshSize::lengthSqInDir(const double vec[3]) const
  {
    double tmp[3];
    normalizeVec(vec,tmp);
    return ( 1. / dot(tmp,M,tmp) );
  }
    
  // -------------------------------------------------------------------
  // get the cosine of the angle with the direction of the minimal size
  double AnisoMeshSize::angleWithDir0(const double dir[3]) const
  {
    double tmp[3];
    normalizeVec(dir,tmp);
    double dir0[3];
    direction(0,dir0);
    normalizeVec(dir0,dir0);
    return dotProd(tmp,dir0);
  }

  // -------------------------------------------------------------------
  void AnisoMeshSize::print(string name) const
  {
    std::cout<<"Printing AnisoMeshSize \'"<<name<<"\' ("<<this<<")\n";
    M.print("Mesh size metric");
  }

  // -------------------------------------------------------------------

}