File: MobileObject.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 (553 lines) | stat: -rw-r--r-- 18,802 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// -------------------------------------------------------------------
// 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 "MobileObject.h"
#include "NodalDataManager.h"
#include "VertexMoveOp.h"
#include "MAdElasticityOp.h"
#include "DistanceFunction.h"

#include "MAdStringFieldEvaluator.h"

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <string.h>
using std::set;
//using std::multiset;
using std::list;
using std::pair;
using std::vector;
using std::string;
#include <stdlib.h>

namespace MAd {

  // ----------------------------------------------------------------------
  mobileObject::mobileObject(const pMesh m, string _name):
    mesh(m),name(_name),prescribeType(""),DxParsed(NULL),
    velType(NOFORMULATION),VParsed(NULL)//, Cxyz(NULL), Vxyz(NULL)
  {
#ifdef PARALLEL
    MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                "Mobile objects not supported in parallel");
#endif
  }

  // ----------------------------------------------------------------------
  mobileObject::~mobileObject()
  {
    if (DxParsed) { delete DxParsed; DxParsed = NULL; }
    if (VParsed)  { delete VParsed;  VParsed  = NULL; }
  }

  // ----------------------------------------------------------------------
  void mobileObject::setDxKinematics(vector<string> _str)
  {
    if ( strcmp(prescribeType.c_str(),"") ) {
      cerr << "Error: imposing a position on an object with another imposed movement: "<<prescribeType<<"\n";
      throw;
    }
    prescribeType = "Displacement";

    if (DxParsed) delete DxParsed;
    DxParsed = new MAdStringFieldEvaluator(_str);
  }

  // ----------------------------------------------------------------------
  void mobileObject::setVKinematics(velocityFormulation type, double V[3], 
                                    double C[3], vector<string> _str)
  {
    if ( strcmp(prescribeType.c_str(),"") ) {
      cerr << "Error: imposing a velocity on an object with another imposed movement: "<<prescribeType<<"\n";
      throw;
    }
    prescribeType = "Velocity";
      
    velType = type; 
    if ( V ) {
      for (int i=0; i<3; i++)  Vxyz[i] = V[i];
    }
    if ( C ) {
      for (int i=0; i<3; i++)  Cxyz[i] = C[i];
    }
    if (type == PARSED)  VParsed = new MAdStringFieldEvaluator(_str);
  }

  // ----------------------------------------------------------------------
  void mobileObject::addLocalSField(LocalSizeField* lsf)
  {
    sizes.insert(lsf);
  }

  // ----------------------------------------------------------------------
  void mobileObject::addGEntity(int type, int tag)
  {
    geomEntities.push_back(std::make_pair(type,tag));
    addVerticesOnGEntity(type,tag);
  }

  // ----------------------------------------------------------------------
  void mobileObject::reAddVertices()
  {
    vertices.clear();
    list<pair<int,int> >::const_iterator it = geomEntities.begin();
    list<pair<int,int> >::const_iterator itEnd = geomEntities.end();
    for (; it != itEnd; it++)  addVerticesOnGEntity(it->first, it->second);
  }

  // ----------------------------------------------------------------------
  void mobileObject::addVerticesOnGEntity(int type, int tag)
  {
    switch (type) {
    case 0:
      {
        VIter vit = M_vertexIter(mesh);
        while (pVertex pv = VIter_next(vit))
          {
            pGEntity pg = EN_whatIn((pEntity)pv);
            int pgType = EN_whatInType( (pEntity) pv );
            if(pg)
              if (GEN_tag(pg) == tag && pgType == 0)
                {
                  vertices.insert(pv);
                }
          }
        VIter_delete(vit);
        break;
      }
    case 1:
      {
        EIter eit = M_edgeIter(mesh);
        while (pEdge pe = EIter_next(eit))
          {
            pGEntity pg = EN_whatIn((pEntity)pe);
            int pgType = EN_whatInType( (pEntity) pe );
            if(pg)
              if (GEN_tag(pg) == tag && pgType == 1)
                {
                  vertices.insert(E_vertex (pe,0));
                  vertices.insert(E_vertex (pe,1));
                }
          }
        EIter_delete(eit);
      }
    case 2:
      {
        FIter fit = M_faceIter(mesh);
        while (pFace pf = FIter_next(fit))
          {
            pGEntity pg = EN_whatIn((pEntity)pf);
            int pgType = EN_whatInType( (pEntity) pf );
            if(pg)
              if (GEN_tag(pg) == tag && pgType == 2)
                {
                  vertices.insert(F_vertex (pf,0));
                  vertices.insert(F_vertex (pf,1));
                  vertices.insert(F_vertex (pf,2));
                }
          }
        FIter_delete(fit);
      }
    case 3:
      {
        RIter rit = M_regionIter(mesh);
        while (pRegion pr = RIter_next(rit))
          {
            pGEntity pg = EN_whatIn((pEntity)pr);
            int pgType = EN_whatInType( (pEntity) pr );
            if(pg)
              if (GEN_tag(pg) == tag && pgType == 3)
                {
                  vertices.insert(R_vertex (pr,0));
                  vertices.insert(R_vertex (pr,1));
                  vertices.insert(R_vertex (pr,2));
                  vertices.insert(R_vertex (pr,3));
                }
          }
        RIter_delete(rit);
      }
    }
  }

  // ----------------------------------------------------------------------
  void mobileObject::computePrescribedDisplacement (double t, double dt)
  {
    if ( !(NodalDataManagerSgl::instance().isCoordinates()) ) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Allocate initial coordinates before making any move (use AdaptInterface::storeInitialCoordinates())");
    }
  
    prescribedDisplacement.clear();
    reAddVertices();
  
    if ( !strcmp(prescribeType.c_str(),"Velocity") ) 
      {
        double V[3]; V[0]=0.; V[1]=0.; V[2]=0.; 
        if (velType == RANDOM_TRANSLATION) 
          randomVelocity(V);
        set<pVertex> :: iterator itV    = vertices.begin();
        set<pVertex> :: iterator itVEnd = vertices.end();
        for ( ; itV != itVEnd ; ++itV)
          {
            double dx[3];
            double xyz[3]; V_coord (*itV,xyz);
            if ( velType != RANDOM_TRANSLATION)
              velocityFunction(xyz,t,V);
            dx[0] = V[0] * dt;
            dx[1] = V[1] * dt;
            dx[2] = V[2] * dt;
          
            vDisplacement disp(*itV,dx);
            prescribedDisplacement.insert(disp);
          }
      }

    else if ( !strcmp(prescribeType.c_str(),"Displacement") ) 
      {
        set<pVertex> :: iterator itV    = vertices.begin();
        set<pVertex> :: iterator itVEnd = vertices.end();
        for ( ; itV != itVEnd ; ++itV)
          {
            // get initial position
            vector<double> xyz0;
            NodalDataManagerSgl::instance().getStoredCoordinates(*itV,xyz0);
          
            // get prescribed displacement (relative to and evaluated on initial position)
            double dx0[3];
            DxParsed->eval(xyz0,t,dx0);
          
            // get current position
            double xyz[3]; V_coord (*itV,xyz);

            // get new position
            double xyznew[3];
            for (int i=0; i<3; i++)  xyznew[i] = xyz0[i] + dx0[i];

            // compute the displacement
            double dx[3];
            dx[0] = xyznew[0] - xyz[0];
            dx[1] = xyznew[1] - xyz[1];
            dx[2] = xyznew[2] - xyz[2];
          
            vDisplacement disp(*itV,dx);
            prescribedDisplacement.insert(disp);
          }
      }

    else {
      cerr<< "Error: no kinematics specified for this object\n";
      throw;
    }
  }

  // ----------------------------------------------------------------------
  void mobileObject::describe (std::ostream& out) const 
  {
    out << "Object \'" << name.c_str() << "\' carries "
        << vertices.size() << " nodes with xyz velocity:\n"
        << "  *  Vx: "<<Vxyz[0]<<"\n"
        << "  *  Vy: "<<Vxyz[1]<<"\n"
        << "  *  Vz: "<<Vxyz[2]<<"\n"
        << " and analytical velocity formulation "<<velType<<"\n\n";
  }

  // ----------------------------------------------------------------------
  // generates velocities between -1 and 1
  void mobileObject::randomVelocity(double* V)
  {
    V[0] = (  (  (double)rand() / ((double)(RAND_MAX)+(double)(1))  ) - 0.5  ) * 2.;
    V[1] = (  (  (double)rand() / ((double)(RAND_MAX)+(double)(1))  ) - 0.5  ) * 2.;
    V[2] = (  (  (double)rand() / ((double)(RAND_MAX)+(double)(1))  ) - 0.5  ) * 2.;
  }

  // ----------------------------------------------------------------------
  void mobileObject::velocityFunction(double* xyz, double t, double* V)
  {
    double x = xyz[0];
    double y = xyz[1];
    double z = xyz[2];

    switch (velType) 
      {
      case NOFORMULATION:
        {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "Velocity formulation: NOFORMULATION");
        }
      case PARSED:
        {
          VParsed->eval(xyz,t,V);
          break;
        }
      case TRANSLATION:
        {
          V[0] = Vxyz[0]; V[1] = Vxyz[1]; V[2] = Vxyz[2];
          break;
        }
      case FULLRANDOM:
        {
          randomVelocity(V);
          break;
        }
      case RANDOM_TRANSLATION:
        {
          MAdMsgSgl::instance().warning(__LINE__,__FILE__,
                                        "VelocityFunction with tag RANDOM_TRANSLATION should not be used");
          randomVelocity(V);
          break;
        }
      case ISOMETRY: // expansion 
        {
          V[0] = Vxyz[0] * ( x - Cxyz[0] );
          V[1] = Vxyz[1] * ( y - Cxyz[1] );
          V[2] = Vxyz[2] * ( z - Cxyz[2] );
          break;
        }
      case ROTATION: // rotation
        { 
          double velYZ = Vxyz[0];
          double velZX = Vxyz[1];
          double velXY = Vxyz[2];
          double rYZ = sqrt ( (y - Cxyz[1])*(y - Cxyz[1]) + (z - Cxyz[2])*(z - Cxyz[2]) );
          double rZX = sqrt ( (x - Cxyz[0])*(x - Cxyz[0]) + (z - Cxyz[2])*(z - Cxyz[2]) );
          double rXY = sqrt ( (x - Cxyz[0])*(x - Cxyz[0]) + (y - Cxyz[1])*(y - Cxyz[1]) );
          double angleYZ = atan2((y - Cxyz[1]), (z - Cxyz[2]));
          double angleZX = atan2((z - Cxyz[2]), (x - Cxyz[0]));
          double angleXY = atan2((x - Cxyz[0]), (y - Cxyz[1]));
          V[0] = 0                               - velZX * rZX * sin(angleZX) + velXY * rXY * cos(angleXY) ;
          V[1] = 0 +  velYZ * rYZ * cos(angleYZ)                              - velXY * rXY * sin(angleXY) ;
          V[2] = 0 -  velYZ * rYZ * sin(angleYZ) + velZX * rZX * cos(angleZX)                            ;
          break;	
        }
      case SHEAR_YZ: // shear plane YZ  
        {
          V[0] = Vxyz[0] * ( x - Cxyz[0] );
          V[1] = Vxyz[1] * ( x - Cxyz[0] );
          V[2] = Vxyz[2] * ( x - Cxyz[0] );
          break;	
        }
      case SHEAR_ZX: // shear plane ZX  
        {
          V[0] = Vxyz[0] * ( y - Cxyz[1] );
          V[1] = Vxyz[1] * ( y - Cxyz[1] );
          V[2] = Vxyz[2] * ( y - Cxyz[1] );
          break;	
        }
      case SHEAR_XY: // shear plane XY  
        {
          V[0] = Vxyz[0] * ( z - Cxyz[2] );
          V[1] = Vxyz[1] * ( z - Cxyz[2] );
          V[2] = Vxyz[2] * ( z - Cxyz[2] );
          break;	
        }
      case BENCH2: // bench 2 
        {
          double Xmax0 = 10.;
          double X = Xmax0 + Vxyz[0] * t;
          V[0] = Vxyz[0] * x / X;
          V[1] = 0.0;
          V[2] = 0.0;
          break;
        }
      case CROSS_EXPANSION_WITH_ROTATION: // cross expantion with rotation
        {
          double cx = 0.45;
          double cy = 0.35 ;
          double r = sqrt ((x - cx)*(x - cx) + (y - cy)*(y - cy));
          double tt = atan2((x - cx), (y - cy));
          V[0] = 0 +  4*r * cos(tt) + sin(tt) * .3;
          V[1] = 0 -  4*r * sin(tt) + cos(tt) * .3;
          V[2] = 0;
          break;	
        }
      default:
        {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "Unknowm velocity formulation %d",velType);
        }
      }
  }

  // ----------------------------------------------------------------------
  // ----------------------- MOBILE OBJECTS SET ---------------------------
  // ----------------------------------------------------------------------
  mobileObjectSet::mobileObjectSet(): elasticOp(NULL)
  {
  }

  // ----------------------------------------------------------------------
  mobileObjectSet::~mobileObjectSet() 
  {
    clear();
    if (elasticOp) delete elasticOp;
  }

  // ----------------------------------------------------------------------
  void mobileObjectSet::clear()
  {
    mobSet.clear();
  }

  // ----------------------------------------------------------------------
  void mobileObjectSet::insert(mobileObject* mob)
  {
    mobSet.insert(mob);
  }

  // ----------------------------------------------------------------------
  void mobileObjectSet::describe(std::ostream& out) const
  {
    out << "\n--- Mobile objects set description ---\n\n";
    set<mobileObject*>::const_iterator groupIt = mobSet.begin(); 
    set<mobileObject*>::const_iterator groupItEnd = mobSet.end(); 
    for ( ; groupIt != groupItEnd ; ++groupIt) {
      (*groupIt)->describe(out);
    }
  }

  // ----------------------------------------------------------------------
  void mobileObjectSet::computePrescribedDisplacement (double t, double dt)
  {
    prescribedDisplacement.clear();

    set<mobileObject*>::const_iterator groupIt = mobSet.begin(); 
    set<mobileObject*>::const_iterator groupItEnd = mobSet.end(); 
    for ( ; groupIt != groupItEnd ; ++groupIt) {

      // compute the forced displacement for each object
      (*groupIt)->computePrescribedDisplacement (t,dt);

      // gather all displacements
      set<vDisplacement,vDisplacementLess> objDisp = (*groupIt)->getPrescribedDisplacement();
      set<vDisplacement,vDisplacementLess>::const_iterator itObjDx = objDisp.begin();
      for(; itObjDx != objDisp.end(); itObjDx++) {
        vDisplacement vdisp(*itObjDx);
        prescribedDisplacement.insert(vdisp);
      }
    }
  }

  // ----------------------------------------------------------------------
  int mobileObjectSet::partlyMove(vertexMoveOp& vMoveOp, double t, double dt, double * part)
  {
    computePrescribedDisplacement(t, dt);
  
    int ok = 0;
    const int maxNumReduct = 8;
    const double reductFactor = 0.5;
    *part = 1.0;
    cout<<"Attempts to move vertices: [ ";
    for (int i=0; i < maxNumReduct; i++) {
      cout <<"* ";
      if (!vMoveOp.move(prescribedDisplacement,*part))  *part *= reductFactor;
      else { ok = 1; break;}
    }
    cout << "]\n";
    return ok;
  }

  // ----------------------------------------------------------------------
  void mobileObjectSet::setupElasticRepositioning(pMesh mesh, double t, double dt, 
                                                  double qualThr,
                                                  double chi, bool meshIsCavity,
                                                  int cavityThickness)
  {
    if ( mobSet.empty() ) return;

    // setup the elastic operator
    if (elasticOp) { delete elasticOp; elasticOp = NULL; }
    elasticOp = new MAdElasticityOp(mesh);
    if ( chi >= 0. ) elasticOp->setStiffnessAlterationCoef(chi);
    elasticOp->setCavityEqualMesh(meshIsCavity,cavityThickness);
    elasticOp->setQualityThreshold(qualThr);

    // prescribe bcs
    computePrescribedDisplacement(t, dt);
    set<vDisplacement,vDisplacementLess>::const_iterator dIter = prescribedDisplacement.begin();
    set<vDisplacement,vDisplacementLess>::const_iterator dLast = prescribedDisplacement.end();
    for (; dIter != dLast; dIter++) {
      pVertex vert = (*dIter).pv;
      smallVector disp(3);
      for (int i=0; i<3; i++)  disp(i) = (*dIter).dxyz[i];
      elasticOp->addDirichlet(vert,disp);
    }

    elasticOp->buildCavity();
    elasticOp->setDirichletBC();

    // compute elastic repositioning
    elasticOp->compute();
  }

  // ----------------------------------------------------------------------
  int mobileObjectSet::reposition(double * ratio)
  {
    if ( mobSet.empty() ) return true;
    int flag = elasticOp->advance(ratio);
    if ( flag == 2 ) elasticOp->clear();
    return flag;
  }

  // ----------------------------------------------------------------------
  bool mobileObjectSet::moveAndReposition(pMesh mesh, double t, double dt,
                                          bool subAdaptation,
                                          double qualityThreshold,
                                          double chi, bool meshIsCavity,
                                          int cavityThickness,
                                          MeshAdapter * ma)
  {
    setupElasticRepositioning(mesh, t, dt, qualityThreshold, chi, 
                              meshIsCavity, cavityThickness);
    bool joker = false;
    int subIter = 0;
    double ratio = 0.;
    int achieved = -1;
    while ( achieved != 2 ) {
      achieved = reposition(&ratio);
      MAdMsgSgl::instance().info(-1,__FILE__,
                                 "Advanced repositioning, achieved: %d, ratio: %f",
                                 achieved,ratio);

      if ( achieved <= 1 ) {
        if ( !subAdaptation ) {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "Could not advance objects, ratio reached: %f",ratio);
        }
        else if ( !ma->removeSlivers() && !ma->optimiseElementShape() ) {
          if ( joker ) {
            MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                        "Could not advance objects, ratio reached: %f",
                                        ratio);
          }
          else {
            MAdMsgSgl::instance().warning(__LINE__,__FILE__,
                                          "Could not maintain quality at threshold value %f when repositioning nodes, ratio reached: %f",
                                          qualityThreshold,ratio);
            joker = true;
            elasticOp->setQualityThreshold(0.);
          }
        }
      }

      subIter++;
    }

    if ( joker )  elasticOp->setQualityThreshold(qualityThreshold);

    return true;
  }

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

}