File: main.cc

package info (click to toggle)
madlib 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,912 kB
  • sloc: cpp: 39,850; sh: 10,088; makefile: 476
file content (1357 lines) | stat: -rw-r--r-- 43,842 bytes parent folder | download | duplicates (3)
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
// -------------------------------------------------------------------
// 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
// -------------------------------------------------------------------

// the template parameter file for this executable
#include "Parameters.h"
// the parameter file for the current test case
#ifdef _HAVE_PARSER_
 #include "moveItParse.h"
#else
 #include "MyParams.h"
#endif

#include "MAdLib.h"

#include <unistd.h>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <set>

#ifdef PARALLEL
 #include "mpi.h"
#endif

using std::cout;
using std::cerr;
using std::endl;
using std::stringstream;
using std::ofstream;
using std::vector;
using std::set;

using namespace MAd;

// ----------------------------------------------------------------------
#ifdef PARALLEL
class EmptyExchanger : public MDB_DataExchanger
{
public:
  EmptyExchanger(int _tag): MDB_DataExchanger(_tag) {}
  ~EmptyExchanger() {}

  void * sendData (pEntity pe,    // in
		   int iProcDest, // in
		   int &_size ) {
    _size = 0;
    return NULL;
  }
  void receiveData (pEntity pe,      //in
	            int iProcSender, //in
		    void *buf ) {}
  void deleteExternalData( pEntity pe) const {}
};
#endif


// ----------------------------------------------------------------------
double CPUTime() {
  
#ifdef PARALLEL
  return MPI_Wtime();
#else
  struct timeval tp;
  struct timezone tz;

  gettimeofday(&tp,&tz);

  return ((double) tp.tv_sec +
          (double) ((double) .000001 * (double) tp.tv_usec));
#endif
}

// ----------------------------------------------------------------------
#if defined(__linux) || defined(linux)
void process_mem_usage(double& vm_usage, double& resident_set)
{
  using std::ios_base;
  using std::ifstream;
  using std::string;
  
  vm_usage     = 0.0;
  resident_set = 0.0;
  
  // 'file' stat seems to give the most reliable results
  //
  ifstream stat_stream("/proc/self/stat",ios_base::in);
  
  // dummy vars for leading entries in stat that we don't care about
  //
  string pid, comm, state, ppid, pgrp, session, tty_nr;
  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
  string utime, stime, cutime, cstime, priority, nice;
  string O, itrealvalue, starttime;
  
  // the two fields we want
  //
  unsigned long vsize;
  long rss;
  
  stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
              >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
              >> utime >> stime >> cutime >> cstime >> priority >> nice
              >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
  
  long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
  vm_usage     = vsize / 1024.0 / 1024;
  resident_set = rss * page_size_kb / 1024;
}
#endif

// ----------------------------------------------------------------------
void displayMemoryUsage(string step="")
{
  double ram, virt;

#if defined(__linux) || defined(linux)
  process_mem_usage(virt,ram);
  
#ifdef PARALLEL
    double send[2] = {virt,ram};
    double recv[2];
    MPI_Allreduce(send,recv,2,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
    virt  = recv[0];
    ram = recv[1];
#endif

  std::cout << "Memory usage at step \'"<<step<<"\': " 
            << ram  << " Mb (resident), "
            << virt << " Mb (virtual)\n";
#endif
}

// ----------------------------------------------------------------------
void writeSolution(MeshAdapter* ma, int iter, string type) 
{
  stringstream ss;
  string iterStr;  ss << iter;  ss >> iterStr;
  
  if ( !strcmp(type.c_str(),"Pos") || !strcmp(type.c_str(),"MshAndPos") ) {
    string namePos = "result" + iterStr + ".pos";
    ma->writePos(namePos,OD_MEANRATIO);
//     ma->writePos(namePos,OD_SIZEFIELD_MEAN);
  }
  
  if ( !strcmp(type.c_str(),"Msh") || !strcmp(type.c_str(),"MshAndPos") ) {
    string nameMsh = "result" + iterStr + ".msh";
    ma->writeMsh(nameMsh);
  }

// #warning "output distance"
//   string nameDist = "distance" + iterStr;
//   ma->writeDistanceToWalls(nameDist);

// #warning "output curvature"
//   string nameCurv = "curvature" + iterStr;
//   ma->writeVolumicCurvature(nameCurv);
}

// ----------------------------------------------------------------------
void setMobileObject(mobileObject* mob, pMesh mesh, ObjectDef def)
{
  // --- Name ---
  string name = def.name;
  mob->setName(name);

  // --- Geometry ---
  int type = def.geomLevel;
  set<int>::const_iterator gIter = def.geomTags.begin();
  set<int>::const_iterator gLast = def.geomTags.end();
  for (; gIter != gLast; gIter++) {
    for (int iTag = (*gIter); iTag <= (*gIter); iTag++) {
      mob->addGEntity(type,iTag);
    }
  }

  // --- Kinematics ---
  KinematicType kinType = def.kinType;
  if ( kinType == KT_DISPLACEMENT ) {
    mob->setDxKinematics(def.kinExpression);
  }
  else {
    mob->setVKinematics(PARSED, NULL, NULL, def.kinExpression);
  }

  // --- Local size fields ---
  std::list<LocalSFDef>::const_iterator sIter = def.sizes.begin();
  std::list<LocalSFDef>::const_iterator sLast = def.sizes.end();
  for (; sIter != sLast; sIter++) {
    LocalSizeField* locSField = new LocalSizeField(mesh, (*sIter).name, 
                                                   (*sIter).distToFaces);
    bool   iso = (*sIter).isotropic;
    double radius = (*sIter).radius;
    string sizeN = (*sIter).sizeN;
    string sizeT = (*sIter).sizeT;
    bool   limit = (*sIter).limit;
    double limitSizeTg = (*sIter).limitSizeTg;
    double maxCurv = (*sIter).maxCurv;
    if ( iso ) locSField->setIsoSize(radius,sizeN);
    else       locSField->setAnisoSize(radius,sizeN,sizeT);
    if ( limit ) locSField->setCurvatureLimiter(limitSizeTg, maxCurv);
    
    set<int>::const_iterator gIter = def.geomTags.begin();
    set<int>::const_iterator gLast = def.geomTags.end();
    for (; gIter != gLast; gIter++) {
      for (int iTag = (*gIter); iTag <= (*gIter); iTag++) {
        locSField->addGeometricEntity(type,iTag);
      }
    }
    locSField->updateTree();
    mob->addLocalSField(locSField);

// #warning "debug"
//     locSField->printPosAnisotropic(mesh,"locSF");
  }
}

// ----------------------------------------------------------------------
void deleteObjects(mobileObjectSet* objSet)
{
  set<mobileObject*> objs = objSet->getObjects();
  set<mobileObject*>::iterator oIter = objs.begin();
  for (; oIter != objs.end(); oIter++) {
    
    mobileObject * mob = *oIter;

    // clean size fields
    set<LocalSizeField* > localSFs = mob->getSizes();
    set<LocalSizeField* >::iterator sIter = localSFs.begin();
    for (; sIter != localSFs.end(); sIter++) {
      if (*sIter) delete (*sIter);
    }
    
    // delete object
    if (mob) delete mob;
  }
}

// ----------------------------------------------------------------------
int main(int argc, char* argv[]) 
{
  double main_t0 = CPUTime();

  MAdLibInitialize(&argc,&argv);

  displayMemoryUsage("initialized");

#ifdef _HAVE_PARSER_
  parseControlFile(argc,argv);
#endif

  MoveParameters parameters = getParameters();

  // ------------------------------------------------
  // setup the output
  // ------------------------------------------------

  string outputType = parameters.outType;
  int outputFrequency = parameters.outFrequency;
  string outputPrefix = parameters.outPrefix;

  // ------------------------------------------------
  // load the mesh
  // ------------------------------------------------

  cout << "Loading the mesh...\n";
  double cpu_mesh_0 = CPUTime();

  // --- Reading model ---
  string meshFileName = parameters.meshFileName;
  string geoFileName  = parameters.geoFileName;
  pGModel model = 0;
  GM_create(&model,"theModel");
  if ( !geoFileName.empty() ) GM_read(model, geoFileName.c_str());
  else                        GM_readFromMSH(model, meshFileName.c_str());

  // --- Reading mesh ---
  pMesh mesh = M_new(model);
  M_load(mesh,meshFileName.c_str());
//   M_writeMsh (mesh, (outputPrefix + "initMesh.msh").c_str(), 2, NULL);

  double cpu_mesh_tot = CPUTime() - cpu_mesh_0;
  cout << "Loaded the mesh in "<<cpu_mesh_tot<<" seconds\n";
  displayMemoryUsage("Mesh loaded");

  // ------------------------------------------------
  // build the mesh adapter
  // ------------------------------------------------

  cout << "Building the mesh adapter...\n";
  double cpu_ma_0 = CPUTime();
  MeshAdapter* ma = new MeshAdapter(mesh);

  ma->setOutputPrefix(outputPrefix);

  ma->setMaxIterationsNumber(parameters.maxInnerIter);

  double lowerLenghtBound = parameters.lowerLength;
  double upperLenghtBound = parameters.upperLength;
  ma->setEdgeLenSqBounds( lowerLenghtBound * lowerLenghtBound,
                          upperLenghtBound * upperLenghtBound );

  ma->setSwapMinImproveRatio (parameters.swapMinImproveRatio);
  ma->setSliverQuality       (parameters.sliverQuality);
  ma->setSliverPermissionInESplit    (parameters.splitCanMakeSlivers,
                                      parameters.makeSliverInSplitLenSqBound);
  ma->setSliverPermissionInECollapse (parameters.collapseCanMakeSlivers,
                                      parameters.makeSliverInCollapseLenSqBound);
  ma->setCollapseOnBoundary  (parameters.collapseOnBoundary,
                              parameters.clpOnBdryTolerance);
  ma->setSwapOnBoundary      (parameters.swapOnBoundary,
                              parameters.swapOnBdryTolerance);
  ma->setGeoTracking         (parameters.trackGeometry,
                              parameters.snap_cavityIsMesh,
                              parameters.snap_thickness,
                              parameters.snap_chi,
                              parameters.snap_check,
                              parameters.snap_force);
  ma->setSizeFieldSmoothing  (parameters.SFSmoothing,
                              parameters.SFSmoothGrad);
  ma->setInfiniteLength(parameters.infLength);

// #ifdef PARALLEL
//   EmptyExchanger * dExch = new EmptyExchanger(4238458);
//   ma->setDataExchanger( dExch );
// #endif

  ma->setSFUpdateFrequency(0);

  double cpu_ma_tot = CPUTime() - cpu_ma_0;
  cout << "Built the mesh adapter in "<<cpu_ma_tot<<" seconds\n";

  displayMemoryUsage("Mesh adapter built");

// #warning "debug"
//   ma->writePos("testCurvMaxVec.pos",OD_CURVATURE_MAX_VEC);
//   printf("Written!");

  // ------------------------------------------------
  // build the size fields
  // ------------------------------------------------
  cout << "Buidling the size fields...\n";
  double cpu_sf_0 = CPUTime();

  set<pSField> sizeFields;

  std::list<SizeFieldDef>::const_iterator sIter = parameters.sizes.begin();
  std::list<SizeFieldDef>::const_iterator sLast = parameters.sizes.end();
  for (; sIter != sLast; sIter++) {

    SizeFieldBase* sizeField = NULL;
  
    if ( (*sIter).type == ANALYTICALSFIELD )
      {
        OrientationType orient = (*sIter).orientation;
        if ( orient == ORT_ISOTROPIC ) {
          sizeField = new AnalyticalSField((*sIter).isoSize);
        }
        else {
          sizeField = new AnalyticalSField((*sIter).anisoSize,
                                           (*sIter).anisoDir0,
                                           (*sIter).anisoDir1,
                                           (*sIter).anisoDir2);
        }
      }
    else if ( (*sIter).type == DISCRETESFIELD )
      {
        sizeField = new PWLSField(mesh);
        string source = (*sIter).pwlSource;
        if ( !strcmp(source.c_str(),"InitialLength") ) {
          ((PWLSField*) sizeField)->setCurrentSize();
        }
        else if ( !strcmp(source.c_str(),"Curvature") ) {
          bool aniso = (*sIter).curv_aniso;
          double alpha = (*sIter).curv_alpha;
          double hMin = (*sIter).curv_hMin;
          ((PWLSField*) sizeField)->setCurvatureSize(aniso,alpha,hMin);
        }
        else throw;
      }
    else if ( (*sIter).type == BACKGROUNDSFIELD )
      {
        sizeField = new BackgroundSF();
        string mshFile = (*sIter).mshFile;
        ((BackgroundSF*) sizeField)->loadData(mshFile);
      }
    else throw;

    sizeFields.insert(sizeField);
    ma->addSizeField(sizeField);
  }

  double cpu_sf_tot = CPUTime() - cpu_sf_0;
  cout << "Built the size field in "<<cpu_sf_tot<<" seconds\n";

  displayMemoryUsage("Size fields built");

// #warning "debug"
//   ma->writePos("initSF.pos",OD_SIZEFIELD_MEAN);

  ma->printStatistics(cout);


  // ------------------------------------------------
  // setup the statistics monitoring
  // ------------------------------------------------

  string statFileName = outputPrefix + "modifications";
  FILE* statFile = fopen(statFileName.c_str(),"w");
  if (!statFile) {
    cerr << "Could not open the statistics file " << statFileName << endl;
    throw;
  }

  fprintf(statFile,"iter\t# edge splits\t# edge collaspes\t# edge swaps\t");
  fprintf(statFile,"CPU split\tCPU collapse\tCPU swap\tCPU sliver\n");
  fclose(statFile);


  // ------------------------------------------------
  // setup the quality monitoring
  // ------------------------------------------------

  string qualityFileName = outputPrefix + "quality";
  FILE* qualFile = fopen(qualityFileName.c_str(),"w");
  if (!qualFile) {
    cerr << "Could not open the quality file " << qualityFileName << endl;
    throw;
  }

  double meanShape, worstShape;
  ma->getStatistics(&meanShape,&worstShape);
  fprintf(qualFile,"iter\tmean quality\tworst quality\n");
  fprintf(qualFile,"%d\t%g\t%g\n",0,meanShape,worstShape);
  fclose(qualFile);

  // ------------------------------------------------
  // setup the cpu time monitoring
  // ------------------------------------------------

  string cpuFileName = outputPrefix + "cpu";
  FILE* cpuFile = fopen(cpuFileName.c_str(),"w");
  if (!cpuFile) {
    cerr << "Could not open the cpu report file " << cpuFileName << endl;
    throw;
  }

  fprintf(cpuFile,"iter\tnodes motion\tadaptation\toutputs  \taccumulated nm\taccum adapt\taccum out\ttotal\n");
  fclose(cpuFile);

  string produceCpuInfo = "cat /proc/cpuinfo > \"" + outputPrefix + "cpuinfo\"";
  system(produceCpuInfo.c_str());

  // ------------------------------------------------
  // setup the mesh size monitoring
  // ------------------------------------------------

  string MSFileName = outputPrefix + "meshSize";
  FILE* MSFile = fopen(MSFileName.c_str(),"w");
  if (!MSFile) {
    cerr << "Could not open the mesh size report file " << MSFileName << endl;
    throw;
  }

  fprintf(MSFile,"iter\t#nodes\t#edges\t#faces\t#regions\n");
  fprintf(MSFile,"init\t%d\t%d\t%d\t%d\n",
          M_numVertices(mesh),M_numEdges(mesh),M_numFaces(mesh),M_numRegions(mesh));
  fclose(MSFile);

  // ------------------------------------------------
  // setup debug tools
  // ------------------------------------------------

  int debugLevel = parameters.debugLevel;
  ma->setDebugLevel(debugLevel);

  // --- cross checks between two executions ---
  bool openJournal = parameters.openJournal;
  if ( openJournal ) {
    
    // Will hold a list of operations and checks in this execution
    ma->openJournal();

    // Will compare the list to another one (and abort if different)
    string refJournalName = parameters.referenceJournal;
    if ( !refJournalName.empty() ) {
      std::cout<<"Warning: comparing execution with the journal \'"
               << refJournalName << "\'\n";
      ma->setReferenceJournal(refJournalName);
    }
  }

  // --- slivers output ---
  bool sliverReports = parameters.sliverReports;
  if ( sliverReports ) {
    ma->enableSliverReports();
  }
  bool sliverOps = parameters.testSliverOperators;
  ma->testSliverOperators(sliverOps);

  displayMemoryUsage("Monitoring and debug tools set");

  // ------------------------------------------------
  // TEST MOBILE OBJECTS
  // ------------------------------------------------
  
  if ( !strcmp(parameters.task.c_str(),"MobileObject") ) {

    // initial mesh check
    if ( debugLevel >= 2 && !(ma->checkTheMesh()) )  ma->abort(__LINE__,__FILE__);

#ifndef PARALLEL
    ma->storeInitialCoordinates();

    displayMemoryUsage("Initial coordinates stored");

    // ---- build the mobile objects ----
    cout << "Building the mobile objects...\n";
    double cpu_mobj_0 = CPUTime();
    mobileObjectSet* objs = new mobileObjectSet();
    for (unsigned int iObj = 0; iObj < parameters.objects.size(); iObj++) {
      mobileObject* mob = new mobileObject(mesh);
      setMobileObject(mob,mesh,parameters.objects[iObj]);
      objs->insert(mob);
    }
    ma->registerObjects(objs);
    double cpu_mobj_tot = CPUTime() - cpu_mobj_0;
    cout << "Built the mobile objects in "<<cpu_mobj_tot<<" seconds\n";
    displayMemoryUsage("Mobile objects built");

// #warning "Size field printed to debug"
//     ma->writePos("sizeFieldInitMean.pos", OD_SIZEFIELD_MEAN);
//     ma->writePos("sizeFieldInitMin.pos",  OD_SIZEFIELD_MIN);
//     ma->writePos("sizeFieldInitMax.pos",  OD_SIZEFIELD_MAX);
//     ma->writePos("sizeFieldInit0.pos",OD_ANISO_SF_AXIS0);
//     ma->writePos("sizeFieldInit1.pos",OD_ANISO_SF_AXIS1);
//     ma->writePos("sizeFieldInit2.pos",OD_ANISO_SF_AXIS2);
// #warning "output curvature"
//     string nameCurv = "curvatureInit";
//     ma->writeVolumicCurvature(nameCurv);
// #warning "output distance"
//     string nameDist = "distanceInit";
//     ma->writeDistanceToWalls(nameDist);

    // ---- get node motion parameters ----
    string nodeMotionType = parameters.nodeMotionType;
    double elasticChi = parameters.elasticStiffnessAlteration;
    bool   elasticCavityMesh = parameters.elasticIsMeshTheCavity;
    int    elasticCavityThick = parameters.elasticCavityThickness;
#else
    string nodeMotionType = "None";
    double elasticChi;
    bool   elasticCavityMesh;
    int    elasticCavityThick;
#endif

    // ---- get time parameters ----
    double dtGlob = parameters.timeStep;
    double finalTime = parameters.finalTime;
    int maxTimeSteps = parameters.maxNbTimeSteps;



// #warning "hack propeller: to clean the CAD"
//     int cnt = 0;
//     EIter ei = M_edgeIter(mesh);
//     while ( pEdge pe = EIter_next(ei) ) {
//       if ( E_whatInType(pe) == 1 ) {
//         if ( GEN_tag(E_whatIn(pe)) == 29 || 
//              GEN_tag(E_whatIn(pe)) == 31 || 
//              GEN_tag(E_whatIn(pe)) ==  37 || 
//              GEN_tag(E_whatIn(pe)) ==  51 || 
//              GEN_tag(E_whatIn(pe)) ==  43 || 
//              GEN_tag(E_whatIn(pe)) ==  49 || 
//              GEN_tag(E_whatIn(pe)) ==  57 || 
//              GEN_tag(E_whatIn(pe)) ==  59    )
//           {
//             ma->collapseEdgeBrute(pe);
//             cnt++;
//           }
//       }
//     } 
//     EIter_delete(ei);
//     printf("Brute collapsed %d edges\n",cnt);







    // ---- the loop ----
    double cpu_t0 = CPUTime();
    double t = 0., dt_iter = 0.;
    int iter = 0;
    double cpu_move = 0., cpu_adapt = 0., cpu_output = 0.;
    while (iter <= maxTimeSteps && t < finalTime)
      {
        double cpu_t1 = CPUTime();
        if ( iter > 0 )  // a first step is performed with the adaptation before any motion occurs
          {
            // --- find time step for this iteration ---
            double t_left_tot = std::max(finalTime - t, 0.0);
            dt_iter = std::min(dtGlob, t_left_tot);
          
            // --- move nodes (dummy move and Laplace smoothing) ---
            if ( !strcmp(nodeMotionType.c_str(),"ForceBoundaries") ) {

              std::cout << "Warning: forcing boundaries is followed by Laplace smoothing\n";
              throw;

              double part;
              if (!ma->partlyMoveObjects(t, dt_iter, &part)) {
                cout <<"Could not perform a forced move\n";
                ma->abort(__LINE__,__FILE__);
              }
              dt_iter *= part;

              if ( debugLevel >= 1 && !(ma->checkTheMesh()) )  ma->abort(__LINE__,__FILE__);
              ma->LaplaceSmoothing();
              if ( debugLevel >= 1 && !(ma->checkTheMesh()) )  ma->abort(__LINE__,__FILE__);
            }

            t += dt_iter;  ma->setTime(t);
            printf("\nGlobal time step %d, t = %f, dt = %f\n\n",iter,t,dt_iter);

            // --- move nodes (elasticity analogy) ---
            if ( !strcmp(nodeMotionType.c_str(),"ElasticAnalogy") ) {
              ma->moveObjectsAndReposition(t, dt_iter, 
                                           parameters.elasticSubAdapt,
                                           parameters.elasticSubAdaptQualThr,
                                           elasticChi,
                                           elasticCavityMesh, 
                                           elasticCavityThick);
              displayMemoryUsage("Nodes repositionned");
            }
          }
        double cpu_t2 = CPUTime(); 
        cpu_move += ( cpu_t2 - cpu_t1 );

        // --- adapt the mesh ---
        if ( parameters.adapt || iter == 0 ) {
          if ( parameters.adaptAlways || iter == 0  ) {
            ma->run();
            displayMemoryUsage("After adaptation");
          }
          else {
            double worstQ, meanQ;
            ma->getStatistics(&meanQ,&worstQ);
            if ( worstQ < parameters.adaptQualityThr ) {
              ma->run();
              displayMemoryUsage("After adaptation");
            }
          }
        }
        double cpu_t3 = CPUTime();
        cpu_adapt += ( cpu_t3 - cpu_t2 );

        // --- outputs ---

        {  // full mesh
          if ( ( outputFrequency > 0 ) && 
               ( (iter%outputFrequency) == 0 ) ) {
            writeSolution(ma,iter,outputType);
          }
          displayMemoryUsage("After outputs");
        }

        {  // modifications monitor
          int nSplit, nColl, nSwap;
          double cpu_spl, cpu_coll, cpu_swap, cpu_sliv;
          ma->getModificationsInfo(&nSplit, &nColl, &nSwap,
                                   &cpu_spl, &cpu_coll, &cpu_swap, &cpu_sliv);
          FILE* sf = fopen(statFileName.c_str(),"a");
          fprintf(sf,"%d\t%d\t%d\t%d\t%e\t%e\t%e\t%e\n", iter, nSplit, nColl, nSwap,
                  cpu_spl, cpu_coll, cpu_swap, cpu_sliv);
          fclose(sf);
        }

        {  // quality monitor
          ma->getStatistics(&meanShape,&worstShape);
          FILE* qf = fopen(qualityFileName.c_str(),"a");
          fprintf(qf,"%d\t%e\t%e\n",iter,meanShape,worstShape);
          fclose(qf);
        }

        {  // cpu monitor
          double cpu_t4 = CPUTime();
          cpu_output += ( cpu_t4 - cpu_t3 );
      
          FILE* cpuF = fopen(cpuFileName.c_str(),"a");
          fprintf(cpuF,"%d\t%e\t%e\t%e\t%e\t%e\t%e\t%e\n",
                  iter, cpu_t2 - cpu_t1, cpu_t3 - cpu_t2, cpu_t4 - cpu_t3,
                  cpu_move, cpu_adapt, cpu_output, cpu_t4 - cpu_t0 );
          fclose(cpuF);
        }

        {  // mesh size monitor
          FILE* msf = fopen(MSFileName.c_str(),"a");
          fprintf(msf,"%d\t%d\t%d\t%d\t%d\n",
                  iter,M_numVertices(mesh),M_numEdges(mesh),M_numFaces(mesh),M_numRegions(mesh));
          fclose(msf);
        }

        { // slivers stats
          if ( (iter%10) == 0 ) {
            ofstream sliverOut( (outputPrefix + "slivers_tmp").c_str() );
            ma->printSliverRegionStatistics(sliverOut);
          }
        }

        { // operations stats
          if ( (iter%10) == 0 ) {
            ofstream operationsOut( (outputPrefix + "statistics_tmp").c_str() );
            ma->printStatistics(operationsOut);
          }
        }

        { // journal
          if ( (iter%10) == 0 ) {
            ofstream journalOut( (outputPrefix + "journal_tmp").c_str() );
            ma->flushJournal(journalOut);
          }
        }

        displayMemoryUsage("After monitors");
        iter++;
      }

#ifndef PARALLEL
    if (objs) {
      deleteObjects(objs);
      delete objs;
    }
    displayMemoryUsage("Objects deleted");

    ma->removeStoredCoordinates();
    displayMemoryUsage("Stored coordinates deleted");
#endif
  }

  // ------------------------------------------------
  // TEST REMOVE REGION
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"RemoveRegion") )
    {
      for (int i=0; i<1; i++) {
        int numOp = 0;
        int oriNumFaces = M_numFaces(mesh);
        int count = 0;
        FIter fi = M_faceIter(mesh);
        pFace pf;
        while ( ( pf = FIter_next(fi) ) && ( count < oriNumFaces ) ) {
          count++;
          if (F_numRegions(pf) != 1) continue;
          else {
            pRegion pr = F_region(pf,0);
            if (!pr) pr = F_region(pf,1);
            if ( ma->removeRegion(pr) ) numOp++;
          }
        }
        FIter_delete(fi);
        cout << "Num region removed: "<<numOp<<endl;
      }
    }

  // ------------------------------------------------
  // test size fields intersection or smoothing
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"SizeField") )
    {
      ma->writePos("testSF.pos",OD_SIZEFIELD_MEAN);
      ma->writePos("aniso0.pos",OD_ANISO_SF_AXIS0);
      ma->writePos("aniso1.pos",OD_ANISO_SF_AXIS1);
      ma->writePos("aniso2.pos",OD_ANISO_SF_AXIS2);

      PWLSField* testSF = new PWLSField(mesh);
      string h = "2.*x+0.1";
      AnalyticalSField* testASF = new AnalyticalSField(h);

      testSF->intersect(testASF);
//       testSF->smooth(0.5);
  
      MeshAdapter* testMA = new MeshAdapter(mesh,testSF);
  
      testMA->writePos("sizeField.pos",OD_SIZEFIELD_MEAN);
       //      testMA->writePos("sizeFieldSmoothed.pos",OD_SIZEFIELD_MEAN);

      exit(0);
    }

  // ------------------------------------------------
  // TEST REMOVE SLIVER FACES
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"RemoveSliverFaces") )
    {
      for (int i=0; i<10; i++) {
        ma->removeSlivers();
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);
      }
    }

  // ------------------------------------------------
  // TEST LAPLACE SMOOTHING
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"LaplaceSmoothing") )
    {
      for (int i=0; i<1; i++) {
        ma->LaplaceSmoothing();
        cout << "Smoothing "<<i<<" operated\n";
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);
      }
    }

  // ------------------------------------------------
  // TEST GEOMETRIC CONSTRAINTS
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"GeoConstraints") )
    {
      ma->setConstraint(2,5);
      ma->removeConstraint(2,5);
    }

  // ------------------------------------------------
  // TEST CONSTRAINTS
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"TopoConstraints") )
    {
      FIter fi = M_faceIter(mesh);
      while ( pFace pf = FIter_next(fi) ) {
        int gtag  = GEN_tag ( EN_whatIn ((pEntity)pf) );
        int gtype = GEN_type( EN_whatIn ((pEntity)pf) );
        if ( gtype == 2 && ( gtag == 5 || gtag == 22 || gtag == 14 || gtag == 27 ) ) ma->setConstraint((pEntity)pf);
      } 
      FIter_delete(fi);

      VIter vi = M_vertexIter(mesh);
      pVertex pv;
      while ( ( pv = VIter_next(vi) ) ) {
        // int gtag  = GEN_tag ( EN_whatIn ((pEntity)pv) );
        int gtype = GEN_type( EN_whatIn ((pEntity)pv) );
        if ( gtype == 0 ) ma->setConstraint((pEntity)pv);
      } 
      VIter_delete(vi);
    }

  // ------------------------------------------------
  // TEST FACE SWAP OPERATOR
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"FaceSwap") )
    {
      for (int i=0; i<5; i++) {
        int count=0, numOp=0;
        int oriNumFaces = M_numFaces(mesh);
        FIter fi = M_faceIter(mesh);
        pFace pf;
        while ( ( pf = FIter_next(fi) ) && ( count < oriNumFaces ) ) {
          int res = ma->swapFace(pf);
          if ( res ) numOp++;
          count++;
        }
        FIter_delete(fi);
        cout << "Num face swaps: "<<numOp<<endl;
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);

        int nESwaps = ma->optimiseElementShape();
        cout<< "Edge swaps applied: "<<nESwaps<<endl;
      }
    }

  // ------------------------------------------------
  // MAKE A BAD MESH
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"UglyMesh") )
    {
      ma->uglyTheMesh(0.2,10);
      ma->writeMsh("uglyMesh.msh");
    }

  // ------------------------------------------------
  // TEST REMOVE SLIVER REGIONS
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"RemoveSliverRegions") )
    {
      ma->setSliverQuality(0.1);
      for (int i=0; i<2; i++) {
        ma->removeSlivers();
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);
      }
    }
  
  // ------------------------------------------------
  // TEST DOUBLE-SPLIT-COLLAPSE OPERATOR
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"DESC") )
    {
      for (int i=0; i<20; i++) {
        int numOp = 0;
        int oriNumRgn = M_numRegions(mesh);
        int count = 0;
        RIter ri = M_regionIter(mesh);
        pRegion pr;
        while ( ( pr = RIter_next(ri) ) && ( count < oriNumRgn ) ) {
          pEdge pe1 = R_edge(pr,0);
          pEdge pe2 = R_gtOppEdg(pr, pe1);

          if ( ma->DSplitCollapseEdge(pr,pe1,pe2) )
            numOp++;
          count++;
        }
        RIter_delete(ri);
        cout << "Num double-split-collapses: "<<numOp<<endl;

        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);
        //     ma->printStatistics(std::cout);

        //     int* nbBefore = new int(0);
        //     int* nbAfter = new int(0);
        //     ma->removeSliverRegions(nbBefore,nbAfter);
        //     cout <<"Nb slivers before->after realignement:\t"<<*nbBefore<<" -> "<<*nbAfter<<endl;
      }
    }

  // ------------------------------------------------
  // TEST EDGE SWAP
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"EdgeSwap") )
    {
      if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 
      for (int i=0; i<30; i++) {
        int count = 0;
        int numOp = 0;
        int numE = M_numEdges(mesh);
        EIter ei = M_edgeIter(mesh);
        while ( pEdge pe = EIter_next(ei) ) {
          count++;
          if ( ma->swapEdge(pe) ) numOp++;
          if ( count > numE ) break;
        } 
        EIter_delete(ei);
        cout << "Num edge swaps ("<<i<<"): " << numOp << endl;
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 
        writeSolution(ma,i,outputType);
      }
    }

  // ------------------------------------------------
  // TEST GEOMATCHER
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"GeoMatcher") )
    {
      for (int i=0; i<8; i++) {
        int count = 0;
        int numOp = 0;
        int numE = M_numEdges(mesh);
        EIter ei = M_edgeIter(mesh);
        pEdge pe;
        while ( ( pe = EIter_next(ei) ) ) {
          count++;
          if ( ma->splitEdge(pe) ) {
            ma->snapVertices();
            numOp++;
          }
          if ( count > numE ) break;
        } 
        EIter_delete(ei);
        cout << "Num edge splits ("<<i<<"): " << numOp << endl;
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 
      }
    }

  // ------------------------------------------------
  // TEST EDGE SPLIT
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"EdgeSplit") )
    {
      if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 
      for (int i=0; i<5; i++) {

        ma->splitEveryEdgeOnce();

//         int count = 0;
//         int numOp = 0;
//         int numE = M_numEdges(mesh);
//         EIter ei = M_edgeIter(mesh);
//         pEdge pe;
//         while ( ( pe = EIter_next(ei) ) ) {
//           count++;
//           if ( E_whatInType(pe) <= 2 && ma->splitEdge(pe) ) { numOp++; }
//           if ( count > numE ) break;
//         } 
//         EIter_delete(ei);
//         cout << "Num edge splits ("<<i<<"): " << numOp << endl;
//         if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 

//         // --- Geometry tracking ---
//         cout << "Snapping boundary nodes" << endl;
//         if (numOp) ma->snapVertices();

        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);
        writeSolution(ma,i,outputType);
      }
    }

  // ------------------------------------------------
  // TEST EDGE COLLAPSE
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"EdgeCollapse") )
    {
      //      GM_writeGEO(model,"testgeo.geo");
      //      ma->writeMsh("MeshInit.msh");
      for (int i=0; i<10; i++) {
        double t0 = CPUTime();
        int count = 0;
        int nbEdgesInit = M_numEdges(mesh);
        int numOp = 0;
        EIter ei = M_edgeIter(mesh);
        pEdge pe;
        while ( ( pe = EIter_next(ei) ) ) {
          count++;
          if ( ma->collapseEdge(pe) ) { numOp++; }
          if ( count > nbEdgesInit ) break;
        } 
        EIter_delete(ei);
        double dt = CPUTime() - t0;
        cout << "Num edge collapses ("<<i<<"): " << numOp << " in "<<dt<<" seconds\n";
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 
//         ma->printStatistics(std::cout);
        writeSolution(ma,i,outputType);
      }
    }

  // ------------------------------------------------
  // TEST EDGE SPLIT AND COLLAPSE
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"EdgeSplitCollapse") )
    {
      for (int i=0; i<20; i++) {
        
        // --- collapse as many edges as possible ---
        int totColl = 0;
        while (1) {
          int numClp = 0;
//           while (1)
//             {
//               pEdge pe;
//               EIter ei = M_edgeIter(mesh);
//               while ( ( pe = EIter_next(ei) ) ) {
//                 if ( ma->collapseEdge(pe) ) { numClp++; break; }
//               }
//               EIter_delete(ei);
//               if ( !pe ) break;
//               if ( !checkMesh(mesh,CHECK_GEOM_COMPATIBILITY,1,std::cout) ) ma->abort(__LINE__,__FILE__); 
//             }
          EIter ei = M_edgeIter(mesh);
          pEdge pe;
          while ( ( pe = EIter_next(ei) ) ) {
            if ( ma->collapseEdge(pe) ) numClp++;
          }
          EIter_delete(ei);
          totColl += numClp;
          cout << "Num edge collapses: "<< numClp << endl;
          if ( ! ma->checkTheMesh() ) ma->abort(__LINE__,__FILE__); 
          if ( numClp == 0 ) break;
        }

        // --- perform many edge splits ---
        for (int iS=0; iS<2; iS++) {
          int numSpl = ma->splitEveryEdgeOnce();
          cout << "Num edge splits: " << numSpl << endl;
          if ( ! ma->checkTheMesh() ) ma->abort(__LINE__,__FILE__); 
        }

        // --- Geometry tracking ---
//         if (numSpl) ma->snapVertices();
//         if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 

        writeSolution(ma,i,outputType);
      }
    }

  // ------------------------------------------------
  // TEST EDGE SPLIT AND COLLAPSE 2
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"EdgeSplitCollapse2") )
    {
      for (int i=0; i<10; i++) {
        int count = 0;
        int oriNumEdges = M_numEdges(mesh);
        int numClp = 0, numSpl = 0;
        EIter ei = M_edgeIter(mesh);
        pEdge pe;
        while ( ( pe = EIter_next(ei) ) ) {
          count++;
          if ( E_whatInType(pe) <= 2 ) 
            {
              if ( (count%3) == 0 ) {
                if ( ma->splitEdge(pe) ) numSpl++;
              }
              else {
                if ( ma->collapseEdge(pe) ) numClp++;
              }
            }
          if ( count > oriNumEdges ) break;
        } 
        EIter_delete(ei);
        cout << "Num edge splits ("<<i<<"): " << numSpl << endl;
        cout << "Num edge collapses ("<<i<<"): " << numClp << endl;
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 

        // --- Geometry tracking ---
        if (numSpl) ma->snapVertices();
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__); 

        writeSolution(ma,i,outputType);
//         ma->printStatistics(std::cout);
      }
      
    }

  // ------------------------------------------------
  // TEST FACE COLLAPSE OPERATOR
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"FaceCollapse") )
    {
      for (int i=0; i<10; i++) {
        int numOp = 0;
        int oriNumFaces = M_numFaces(mesh);
        int count = 0;
        FIter fi = M_faceIter(mesh);
        pFace pf;
        while ( ( pf = FIter_next(fi) ) && ( count < oriNumFaces ) ) {
          for (int j=0; j<3; j++) {
            pEdge pe = F_edge(pf,j);
            if ( ma->collapseFace(pf,pe) ) {
              numOp++;
              break;
            }
          }
          count++;
        }
        FIter_delete(fi);
        cout << "Num face collapse: "<<numOp<<endl;
        if ( !checkMesh(mesh,CHECK_ALL,1) )  ma->abort(__LINE__,__FILE__);
//         ma->printStatistics(std::cout);
      }
    }

  // ------------------------------------------------
  // TEST OPTIMISE EDGE LENGTH
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"OptimizeLength") )
    {
      cout<<"Split collapses applied: "<<ma->optimiseEdgeLength()<<endl;
    }

  // ------------------------------------------------
  // TEST OPTIMISE SHAPE
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"OptimizeShape") )
    {
      int nSwaps = ma->optimiseElementShape();
      cout<< "Swaps applied: "<<nSwaps<<endl;
    }

  // ------------------------------------------------
  // TEST METRIC
  // ------------------------------------------------

//   else if ( !strcmp(parameters.task.c_str(),"Metric") )
//     {
//       double h[3] = { 2., 3., 10. };
//       double e[3][3] = {
//         {sqrt(2.)/2., sqrt(2.)/2., 0.},
//         {0., 0., 1.},
//         {sqrt(2.)/2., -sqrt(2.)/2., 0.}
//       };

//       MAdMetric M = MAdMetric(h[0],h[1],h[2],e[0],e[1],e[2]);

//       doubleMatrix V = doubleMatrix(3,3);
//       doubleVector S = doubleVector(3);
//       M.eig(V,S,true);

//       printVec(h,"h");
//       printMat(e,"e");
//       V.print("V");
//       S.print("S");
//     }

  // ------------------------------------------------
  // TEST CURVATURES
  // ------------------------------------------------

  else if ( !strcmp(parameters.task.c_str(),"Curvatures") )
    {
      ma->writePos("CurvDiv.pos",OD_CURVATURE_DIV);
      ma->writePos("CurvMax.pos",OD_CURVATURE_MAX);
      ma->writePos("CurvMin.pos",OD_CURVATURE_MIN);
      ma->writePos("CurvMaxVec.pos",OD_CURVATURE_MAX_VEC);
      ma->writePos("CurvMinVec.pos",OD_CURVATURE_MIN_VEC);
      
//       FIter fi = M_faceIter(mesh);
//       pFace pf;
//       while ( ( pf = FIter_next(fi) ) )
//         {
//           if ( F_whatInType(pf) != 2 ) continue;
          
//           pGEntity pge = F_whatIn(pf);
//           {
//             double u[2] = { 0., 0. };
//             double xyz[3];
//             GF_xyz( (pGFace) pge, u[0], u[1], xyz);
//             printf("u: %f, v: %f\n",u[0],u[1]);
//             printVec(xyz,"xyz");
//           }
          
//           double u[2] = { 0.5, 0.5 };
//           double xyz[3];
//           GF_xyz( (pGFace) pge, u[0], u[1], xyz);
//           printf("u: %f, v: %f\n",u[0],u[1]);
//           printVec(xyz,"xyz");
          
//           double divCurv = GF_curvature( (pGFace)pge, u);
//           double dirMax[3], dirMin[3], maxCurv, minCurv;
//           double mCurv = GF_curvatures( (pGFace)pge, u,
//                                         dirMax, dirMin, &maxCurv, &minCurv);
          
//           printVec(dirMax,"dirMax");
//           printVec(dirMin,"dirMin");
//           printf("Max curv: %f, min curv: %f, div curv: %f\n",maxCurv,minCurv,divCurv);
          
//           double tmp[3];
//           tmp[0] = dotProd(xyz,dirMax);
//           tmp[1] = dotProd(xyz,dirMin);
//           tmp[2] = dotProd(dirMin,dirMax);
//           printVec(tmp,"sould be 0 vector");
          
//           break;
//         }
//       FIter_delete(fi);
    }

  // ------------------------------------------------
  // MISCELLANEOUS
  // ------------------------------------------------
  
  else if ( !strcmp(parameters.task.c_str(),"Miscellaneous") )
    {
      int count = 0;
      EIter ei = M_edgeIter(mesh);
      pEdge pe;
      while ( ( pe = EIter_next(ei) ) ) {
        count++;
        if ( count == 69 ) //65
          {
            if ( !(ma->collapseEdge(pe)) ) {
              printf("not collapsed\n");
              throw;
            }
            break;
          }
      } 
      EIter_delete(ei);
    }

  // ------------------------------------------------
  else if ( !strcmp(parameters.task.c_str(),"None") )
    {
    }

  else {
    std::cerr << "Error: unknown task " << parameters.task.c_str() << "\n";
  }

  // ------------------------------------------------
  // Write final result
  // ------------------------------------------------

  ma->writeMsh("result.msh");
//   ma->writePos("result.pos",OD_MEANRATIO);
  
// #warning "Size field printed to debug"
//     ma->writePos("sizeFieldFinalMean.pos", OD_SIZEFIELD_MEAN);
//     ma->writePos("sizeFieldFinalMin.pos",  OD_SIZEFIELD_MIN);
//     ma->writePos("sizeFieldFinalMax.pos",  OD_SIZEFIELD_MAX);
//     ma->writePos("sizeFieldFinal0.pos",OD_ANISO_SF_AXIS0);
//     ma->writePos("sizeFieldFinal1.pos",OD_ANISO_SF_AXIS1);
//     ma->writePos("sizeFieldFinal2.pos",OD_ANISO_SF_AXIS2);
// #warning "output curvature"
//     ma->writeVolumicCurvature("curvatureFinal");
// #warning "output distance"
//     ma->writeDistanceToWalls("distanceFinal");

  ma->printStatistics(cout);
  ofstream statOut((outputPrefix + "statistics").c_str());
  ma->printStatistics(statOut);

  ofstream sliverOut((outputPrefix + "slivers").c_str());
  ma->printSliverRegionStatistics(sliverOut);

  ofstream journalOut((outputPrefix + "journal").c_str());
  ma->flushJournal(journalOut);

  // ------------------------------------------------
  // Clean up
  // ------------------------------------------------

  if (ma) delete ma;
  set<pSField>::iterator sfIter = sizeFields.begin();
  set<pSField>::iterator sfLast = sizeFields.end();
  for (; sfIter != sfLast; sfIter++) {
    delete (*sfIter);
  }
  displayMemoryUsage("Adapter and size fields deleted");
  
  if (mesh)  M_delete(mesh);
  if (model) GM_delete(model);
  displayMemoryUsage("Mesh and model deleted");

  MAdLibFinalize();

  printf("Total execution time: %f seconds\n", CPUTime()-main_t0);
}

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