File: AnalyticalSField.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 (384 lines) | stat: -rw-r--r-- 11,458 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// -------------------------------------------------------------------
// 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 "AnalyticalSField.h"
#include "MAdTimeManager.h"
#include "IsoMeshSize.h"
#include "AnisoMeshSize.h"
#include "MathUtils.h"
#include "MAdMessage.h"
#include "MAdStringFieldEvaluator.h"
#include "MAdDefines.h"

#include <math.h>
#include <stdio.h>
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

namespace MAd {

  // -------------------------------------------------------------------
  // Constructors / destructors
  // -------------------------------------------------------------------
  AnalyticalSField::AnalyticalSField():
    SizeFieldBase(), sFct(NULL), isotropic(true), evaluator(NULL)
  {
    setSize("1.");
  }

  // -------------------------------------------------------------------
  AnalyticalSField::AnalyticalSField(std::string h):
    SizeFieldBase(), sFct(NULL), isotropic(true), evaluator(NULL)
  {
    setSize(h);
  }

  // -------------------------------------------------------------------
  AnalyticalSField::AnalyticalSField(std::vector<std::string> _h,
                                     std::vector<std::string> _e0,
                                     std::vector<std::string> _e1,
                                     std::vector<std::string> _e2):
    SizeFieldBase(), sFct(NULL), isotropic(false), evaluator(NULL)
  {
    setSize(_h,_e0,_e1,_e2);
  }

  // -------------------------------------------------------------------
  AnalyticalSField::AnalyticalSField(sizeFunction f): 
    SizeFieldBase(), sFct(f), isotropic(false),
    evaluator(NULL)
  {}

  // -------------------------------------------------------------------
  AnalyticalSField::~AnalyticalSField()
  {
    if (evaluator) delete evaluator;
  }

  // -------------------------------------------------------------------
  void AnalyticalSField::describe() const {
  
    cout << "\nDescribing analytical size field: \n\n";
  
    cout << "  Orientation:   \t";
    if (isotropic) cout << "Isotropic\n\n";
    else           cout << "Anisotropic\n\n";

    cout << "  Representation:\t";
    if (sFct) cout << "Size Function\n\n";
    else {
      if (isotropic) {
        cout << "String:\n";
        cout << "   Size:  " << h0 << "\n\n";
      }
      else {
        cout << "Strings:\n";

        cout << "   - Sizes:\n";
        cout << "     * " << h0 << "\n";
        cout << "     * " << h1 << "\n";
        cout << "     * " << h2 << "\n";

        cout << "   - Vectors:\n";
        cout << "     * " << e0[0] <<"\t" << e0[1] <<"\t" << e0[2] <<"\n";
        cout << "     * " << e1[0] <<"\t" << e1[1] <<"\t" << e1[2] <<"\n";
        cout << "     * " << e2[0] <<"\t" << e2[1] <<"\t" << e2[2] <<"\n";

        cout << "\n";
      }
    }

  }

  // -------------------------------------------------------------------
  // Size imposition
  // -------------------------------------------------------------------
  void AnalyticalSField::setSize(const std::string h)
  {
    if (sFct) throw;

    isotropic = true;

    h0 = h;
    h1 = h;
    h2 = h;
    e0.resize(3);
    e1.resize(3);
    e2.resize(3);
    e0[0] = "1."; e0[1] = "0."; e0[2] = "0.";
    e1[0] = "0."; e1[1] = "1."; e1[2] = "0.";
    e2[0] = "0."; e2[1] = "0."; e2[2] = "1.";

    if(evaluator) delete evaluator;
    evaluator = new MAdStringFieldEvaluator(1,h.c_str());
  }

  // -------------------------------------------------------------------
  void AnalyticalSField::setSize(std::vector<std::string> _h,
                                 std::vector<std::string> _e0,
                                 std::vector<std::string> _e1,
                                 std::vector<std::string> _e2)
  {
    if (sFct) throw;

    isotropic = false;

    h0 = _h[0];
    h1 = _h[1];
    h2 = _h[2];
    e0 = _e0;
    e1 = _e1;
    e2 = _e2;

    if(evaluator) delete evaluator;
    evaluator = new MAdStringFieldEvaluator(12,
                                            h0.c_str(), h1.c_str(), h2.c_str(), 
                                            e0[0].c_str(), e0[1].c_str(), e0[2].c_str(),
                                            e1[0].c_str(), e1[1].c_str(), e1[2].c_str(),
                                            e2[0].c_str(), e2[1].c_str(), e2[2].c_str());
  }

  // -------------------------------------------------------------------
  void AnalyticalSField::setSize(sizeFunction f)
  {
    if (evaluator) throw;
    sFct = f;
  }

  // -------------------------------------------------------------------
  // Evaluate a local size
  // -------------------------------------------------------------------
  pMSize AnalyticalSField::eval(const double xyz[3]) const
  {
    double time = MAdTimeManagerSgl::instance().getTime();

    if(sFct)  return (*sFct)(xyz,time);

    else {
      if (isotropic) {
        double h;
        evaluator->eval(xyz,time,&h);
        return new IsoMeshSize(h);
      }
      else {
        double vals[12];
        evaluator->eval(xyz,time,vals);
        double h[3]        = {vals[0], vals[1], vals[2]};
        double e[3][3]     = { {vals[3], vals[4], vals[5]},
                               {vals[6], vals[7], vals[8]},
                               {vals[9], vals[10], vals[11]} };
        return new AnisoMeshSize(e,h);
      }
    }
    return NULL;
  }

  // -------------------------------------------------------------------
  pMSize AnalyticalSField::getSize(const pVertex pv) const
  {
    double xyz[3];
    V_coord(pv,xyz);
    return eval(xyz);
  }

  // -------------------------------------------------------------------
  pMSize AnalyticalSField::getSizeOnEntity(const pEntity, 
                                           const double xyz[3]) const
  {
    return eval(xyz);
  }

  // -------------------------------------------------------------------
  // Length squared computation
  // -------------------------------------------------------------------

  // -------------------------------------------------------------------
  double AnalyticalSField::SF_VV_lengthSq(const pVertex pV0, 
                                          const pVertex pV1) const
  {
    double xyz[2][3];
    V_coord(pV0,xyz[0]);
    V_coord(pV1,xyz[1]);

    pMSize pS[2];
    pS[0] = getSize(pV0);
    pS[1] = getSize(pV1);

    double lSq = SF_XYZ_lengthSq(xyz[0],xyz[1],pS[0],pS[1]);
  
    if ( pS[0] ) delete pS[0];
    if ( pS[1] ) delete pS[1];

    return lSq;
  }

  // -------------------------------------------------------------------
  double AnalyticalSField::SF_XYZ_lengthSq(const double xyz0[3], 
                                           const double xyz1[3], 
                                           const pMSize pS0, 
                                           const pMSize pS1) const
  {
    if( pS0 )
      {
        double e[3];
        diffVec(xyz0,xyz1,e);
        double lenSq0 = pS0->normSq(e);
        if ( pS1 )
          {
            double lenSq1 = pS1->normSq(e);
            return sqrt(lenSq0*lenSq1);
          }
        else return lenSq0;
      }
    else {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,"No size defined");
    }
    return 0.;
  }

  // -------------------------------------------------------------------
  // Area squared computation
  // -------------------------------------------------------------------

  double AnalyticalSField::SF_F_areaSq(const pFace face) const
  {
    double area = 0.;

    double xyz[3][3];
    F_coordP1(face,xyz);
  
    void * temp = 0;
    pPList fVerts = F_vertices(face,1);
    while( pVertex pV = (pVertex)PList_next(fVerts,&temp) )
      {
        pMSize pS = getSize(pV);
        area += SF_XYZ_areaSq(xyz,pS,0);
        if (pS) delete pS;
      }
    PList_delete(fVerts);
  
    area /= F_numVertices(face);

    return area;
  }

  // -------------------------------------------------------------------
  double AnalyticalSField::SF_XYZ_areaSq(const double fxyz[3][3], 
                                         const pMSize pS, 
                                         const double norDir[3]) const
  {
    if( !pS ) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,"No size defined");
    }

    // get the two first edges
    double e01[3],e02[3];
    diffVec(fxyz[1],fxyz[0],e01);
    diffVec(fxyz[2],fxyz[0],e02);
    
    double nor[3];
    crossProd(e01,e02,nor);

    double l1SqInv = 1. / pS->lengthSqInDir(e01);
    double l2SqInv = 1. / pS->lengthSqInDir(e02);

    if( norDir && dotProd(norDir,nor) < MAdTOL ) return 0.;

    double areaSq = 0.25 * dotProd(nor,nor) * l1SqInv * l2SqInv;
    if( areaSq < MAdTOL ) return 0.;

    return areaSq;
  }

  // -------------------------------------------------------------------
  // Volume computation
  // -------------------------------------------------------------------

  double AnalyticalSField::SF_R_volume(const pRegion region) const
  {
    double vol = 0.;

    double xyz[4][3];
    R_coordP1(region,xyz);

    pPList rVerts = R_vertices(region);
    void * temp = 0;
    while( pVertex pV = (pVertex)PList_next(rVerts,&temp) )
      {
        pMSize pS = getSize(pV);
        vol += SF_XYZ_volume(xyz,pS);
        if (pS) delete pS;
      }
    PList_delete(rVerts);

    vol /= R_numVertices(region);

    return vol;
  }

  // -------------------------------------------------------------------
  double AnalyticalSField::SF_XYZ_volume(const double xyz[4][3], 
                                         const pMSize pS) const
  {
    if( !pS ) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,"No size defined");
    }

    double physVol = R_XYZ_volume(xyz);
  
    return ( physVol / (pS->size(0)*pS->size(1)*pS->size(2)) );
  }

  // -------------------------------------------------------------------
  // Center of edge computation
  // -------------------------------------------------------------------

  // -------------------------------------------------------------------
  double AnalyticalSField::SF_E_center(const pEdge edge, double center[3], 
                                       double * reducSq, pMSize * cSize) const
  {
    return SF_VV_center(E_vertex(edge,0),E_vertex(edge,1),center,reducSq,cSize);
  }

  // -------------------------------------------------------------------
  double AnalyticalSField::SF_VV_center(const pVertex v0, const pVertex v1,
                                        double center[3], double * reducSq, 
                                        pMSize * cSize) const
  {
    double xyz[2][3];
    V_coord(v0,xyz[0]);
    V_coord(v1,xyz[1]);

    pMSize pS[2];
    pS[0] = getSize(v0);
    pS[1] = getSize(v1);

    double cParam = SF_XYZ_center(xyz,pS,center,reducSq,cSize);

    if ( pS[0] ) delete pS[0];
    if ( pS[1] ) delete pS[1];

    return cParam;
  }

  // -------------------------------------------------------------------
  void AnalyticalSField::scale(double fact)
  {
    printf("Not implemented (AnalyticalSField::scale)\n");
    throw;  
  }

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

}