Salome HOME
correct a small bug appearing when using the gcc 3.2.1.
[modules/med.git] / src / MEDMEM / test_MEDMEM_Meshing.cxx
1 #include "MEDMEM_Meshing.hxx"
2 #include "MEDMEM_Group.hxx"
3 #include "MEDMEM_Field.hxx"
4
5 using namespace std;
6 using namespace MEDMEM;
7
8 int main (int argc, char ** argv) {
9
10   if (argc <2) {
11     cerr << "Usage : " << argv[0] 
12          << " filenameRoot" << endl;
13     cerr << "        where filenameRoot is a root filename, the program will produce" << endl;
14     cerr << "        2 files filenameRoot.med and filenameRoot.vtk" << endl << endl;
15     exit(-1);
16   }
17
18   // filename to save the generated MESH
19   string filenameRoot = argv[1] ;
20
21   string filenameMed = filenameRoot+".med";
22   string filenameVtk = filenameRoot+".vtk";
23
24   MESHING myMeshing ;
25   myMeshing.setName("meshing") ;
26
27   // define coordinates
28
29   int SpaceDimension = 3 ;
30   int NumberOfNodes = 19 ;
31   double Coordinates[57] = {
32     0.0, 0.0, 0.0, 
33     0.0, 0.0, 1.0, 
34     2.0, 0.0, 1.0, 
35     0.0, 2.0, 1.0, 
36     -2.0, 0.0, 1.0, 
37     0.0, -2.0, 1.0, 
38     1.0, 1.0, 2.0, 
39     -1.0, 1.0, 2.0, 
40     -1.0, -1.0, 2.0, 
41     1.0, -1.0, 2.0, 
42     1.0, 1.0, 3.0, 
43     -1.0, 1.0, 3.0, 
44     -1.0, -1.0, 3.0, 
45     1.0, -1.0, 3.0, 
46     1.0, 1.0, 4.0, 
47     -1.0, 1.0, 4.0, 
48     -1.0, -1.0, 4.0, 
49     1.0, -1.0, 4.0,
50     0.0, 0.0, 5.0
51   };
52
53   myMeshing.setCoordinates(SpaceDimension,NumberOfNodes,Coordinates,"CARTESIAN",MED_FULL_INTERLACE);
54
55   string Names[3] = { "X","Y","Z" } ;
56   myMeshing.setCoordinatesNames(Names);
57
58   string Units[3] = { "cm","cm","cm" } ;
59   myMeshing.setCoordinatesUnits(Units) ;
60
61   // define conectivities
62
63   // cell part
64   
65   const int NumberOfTypes = 3 ;
66   medGeometryElement Types[NumberOfTypes] = {MED_TETRA4,MED_PYRA5,MED_HEXA8} ;
67   const int NumberOfElements[NumberOfTypes] = {12,2,2} ;
68
69   myMeshing.setNumberOfTypes(NumberOfTypes,MED_CELL);
70   myMeshing.setTypes(Types,MED_CELL);
71   myMeshing.setNumberOfElements(NumberOfElements,MED_CELL);
72
73   const int sizeTetra = 12*4 ;
74   int ConnectivityTetra[sizeTetra]=
75   {
76     1,2,3,6,
77     1,2,4,3,
78     1,2,5,4,
79     1,2,6,5,
80     2,7,4,3,
81     2,8,5,4,
82     2,9,6,5,
83     2,10,3,6,
84     2,7,3,10,
85     2,8,4,7,
86     2,9,5,8,
87     2,10,6,9
88   };
89   
90   myMeshing.setConnectivity(ConnectivityTetra,MED_CELL,MED_TETRA4);
91
92   int ConnectivityPyra[2*5]=
93   {
94     7,8,9,10,2,
95     15,18,17,16,19
96   };
97
98   myMeshing.setConnectivity(ConnectivityPyra,MED_CELL,MED_PYRA5);
99
100   int ConnectivityHexa[2*8]=
101   {
102     11,12,13,14,7,8,9,10,
103     15,16,17,18,11,12,13,14
104   };
105
106   myMeshing.setConnectivity(ConnectivityHexa,MED_CELL,MED_HEXA8);
107
108   // face part
109
110   const int NumberOfFacesTypes = 2 ;
111   medGeometryElement FacesTypes[NumberOfFacesTypes] = {MED_TRIA3,MED_QUAD4} ;
112   const int NumberOfFacesElements[NumberOfFacesTypes] = {4,4} ;
113
114   myMeshing.setNumberOfTypes(NumberOfFacesTypes,MED_FACE);
115   myMeshing.setTypes(FacesTypes,MED_FACE);
116   myMeshing.setNumberOfElements(NumberOfFacesElements,MED_FACE);
117
118   const int sizeTria = 3*4 ;
119   int ConnectivityTria[sizeTria]=
120   {
121     1,4,3,
122     1,5,4,
123     1,6,5,
124     1,3,6
125   };
126   
127   myMeshing.setConnectivity(ConnectivityTria,MED_FACE,MED_TRIA3);
128
129   int ConnectivityQua[4*4]=
130   {
131     7,8,9,10,
132     11,12,13,14,
133     11,7,8,12,
134     12,8,9,13
135   };
136
137   myMeshing.setConnectivity(ConnectivityQua,MED_FACE,MED_QUAD4);
138
139   // edge part
140
141   // not yet implemented : if set, results are unpredictable.
142
143   // Some groups :
144
145   // Node :
146   {
147     GROUP myGroup ;
148     myGroup.setName("SomeNodes");
149     myGroup.setMesh(&myMeshing);
150     myGroup.setEntity(MED_NODE);
151     myGroup.setNumberOfGeometricType(1);
152     medGeometryElement myTypes[1] = {MED_NONE};
153     myGroup.setGeometricType(myTypes);
154     const int myNumberOfElements[1] = {4} ;
155     myGroup.setNumberOfElements(myNumberOfElements);
156     const int index[1+1] = {1,5} ;
157     const int value[4]= { 1,4,5,7} ;
158     myGroup.setNumber(index,value);
159     
160     myMeshing.addGroup(myGroup);
161   }
162   {
163     GROUP myGroup ;
164     myGroup.setName("OtherNodes");
165     myGroup.setMesh(&myMeshing);
166     myGroup.setEntity(MED_NODE);
167     myGroup.setNumberOfGeometricType(1);
168     medGeometryElement myTypes[1] = {MED_NONE};
169     myGroup.setGeometricType(myTypes);
170     const int myNumberOfElements[1] = {3} ;
171     myGroup.setNumberOfElements(myNumberOfElements);
172     const int index[1+1] = {1,4} ;
173     const int value[3]= { 2,3,6} ;
174     myGroup.setNumber(index,value);
175     
176     myMeshing.addGroup(myGroup);
177   }
178
179   // Cell :
180   {
181     GROUP myGroup ;
182     myGroup.setName("SomeCells");
183     myGroup.setMesh(&myMeshing);
184     myGroup.setEntity(MED_CELL);
185     myGroup.setNumberOfGeometricType(3);
186     medGeometryElement myTypes[3] = {MED_TETRA4,MED_PYRA5,MED_HEXA8};
187     myGroup.setGeometricType(myTypes);
188     const int myNumberOfElements[3] = {4,1,2} ;
189     myGroup.setNumberOfElements(myNumberOfElements);
190     const int index[3+1] = {1,5,6,8} ;
191     const int value[4+1+2]=
192     {
193       2,7,8,12,
194       13,
195       15,16
196     };
197     myGroup.setNumber(index,value);
198     
199     myMeshing.addGroup(myGroup);
200   }
201   {
202     GROUP myGroup ;
203     myGroup.setName("OtherCells");
204     myGroup.setMesh(&myMeshing);
205     myGroup.setEntity(MED_CELL);
206     myGroup.setNumberOfGeometricType(2);
207     medGeometryElement myTypes[] = {MED_TETRA4,MED_PYRA5};
208     myGroup.setGeometricType(myTypes);
209     const int myNumberOfElements[] = {4,1} ;
210     myGroup.setNumberOfElements(myNumberOfElements);
211     const int index[2+1] = {1,5,6} ;
212     const int value[4+1]=
213     {
214       3,4,5,9,
215       14
216     };
217     myGroup.setNumber(index,value);
218     
219     myMeshing.addGroup(myGroup);
220   }
221
222   // Face :
223   {
224     GROUP myGroup ;
225     myGroup.setName("SomeFaces");
226     myGroup.setMesh(&myMeshing);
227     myGroup.setEntity(MED_FACE);
228     myGroup.setNumberOfGeometricType(2);
229     medGeometryElement myTypes[2] = {MED_TRIA3,MED_QUAD4};
230     myGroup.setGeometricType(myTypes);
231     const int myNumberOfElements[2] = {2,3} ;
232     myGroup.setNumberOfElements(myNumberOfElements);
233     const int index[2+1] = {1,3,6} ;
234     const int value[2+3]=
235     {
236       2,4,
237       5,6,8
238     } ;
239     myGroup.setNumber(index,value);
240     
241     myMeshing.addGroup(myGroup);
242   }
243   {
244     GROUP myGroup ;
245     myGroup.setName("OtherFaces");
246     myGroup.setMesh(&myMeshing);
247     myGroup.setEntity(MED_FACE);
248     myGroup.setNumberOfGeometricType(1);
249     medGeometryElement myTypes[1] = {MED_TRIA3};
250     myGroup.setGeometricType(myTypes);
251     const int myNumberOfElements[1] = {2} ;
252     myGroup.setNumberOfElements(myNumberOfElements);
253     const int index[1+1] = {1,3} ;
254     const int value[2]=
255     {
256       1,3
257     } ;
258     myGroup.setNumber(index,value);
259     
260     myMeshing.addGroup(myGroup);
261   }
262
263   // all rigtht, we save it !
264
265   int idMed = myMeshing.addDriver(MED_DRIVER,filenameMed,myMeshing.getName());
266   myMeshing.write(idMed) ;
267
268   int idVtk = myMeshing.addDriver(VTK_DRIVER,filenameVtk,myMeshing.getName());
269   myMeshing.write(idVtk) ;
270
271   // we build now 8 fields : 4 fields double (integer) :
272   //                         2 fields on nodes (cells) :
273   //                         1 scalar (vector)
274
275   SUPPORT * supportOnNodes = new SUPPORT(&myMeshing,"On_All_Nodes",MED_NODE);
276   int numberOfNodes = supportOnNodes->getNumberOfElements(MED_ALL_ELEMENTS);
277
278   SUPPORT * supportOnCells = new SUPPORT(&myMeshing,"On_All_Cells",MED_CELL);
279   int numberOfCells = supportOnCells->getNumberOfElements(MED_ALL_ELEMENTS);
280
281   FIELD<double> * fieldDoubleScalarOnNodes = new FIELD<double>(supportOnNodes,1);
282   fieldDoubleScalarOnNodes->setName("fieldScalarDoubleNode");
283   fieldDoubleScalarOnNodes->setIterationNumber(-1);
284   fieldDoubleScalarOnNodes->setOrderNumber(-1);
285   fieldDoubleScalarOnNodes->setTime(0.0);
286
287   fieldDoubleScalarOnNodes->setComponentName(1,"Vx");
288   fieldDoubleScalarOnNodes->setComponentDescription(1,"comp1");
289   fieldDoubleScalarOnNodes->setMEDComponentUnit(1,"unit1");
290
291   fieldDoubleScalarOnNodes->setValueType(MED_REEL64);
292
293   FIELD<double> * fieldDoubleVectorOnNodes = new FIELD<double>(supportOnNodes,SpaceDimension);
294   fieldDoubleVectorOnNodes->setName("fieldVectorDoubleNode");
295   fieldDoubleVectorOnNodes->setIterationNumber(-1);
296   fieldDoubleVectorOnNodes->setOrderNumber(-1);
297   fieldDoubleVectorOnNodes->setTime(0.0);
298
299   fieldDoubleVectorOnNodes->setComponentName(1,"Vx");
300   fieldDoubleVectorOnNodes->setComponentDescription(1,"comp1");
301   fieldDoubleVectorOnNodes->setMEDComponentUnit(1,"unit1");
302   fieldDoubleVectorOnNodes->setComponentName(2,"Vy");
303   fieldDoubleVectorOnNodes->setComponentDescription(2,"comp2");
304   fieldDoubleVectorOnNodes->setMEDComponentUnit(2,"unit2");
305   fieldDoubleVectorOnNodes->setComponentName(3,"Vz");
306   fieldDoubleVectorOnNodes->setComponentDescription(3,"comp3");
307   fieldDoubleVectorOnNodes->setMEDComponentUnit(3,"unit3");
308
309   fieldDoubleVectorOnNodes->setValueType(MED_REEL64);
310
311   FIELD<double> * fieldDoubleScalarOnCells = new FIELD<double>(supportOnCells,1);
312   fieldDoubleScalarOnCells->setName("fieldScalarDoubleCell");
313   fieldDoubleScalarOnCells->setIterationNumber(-1);
314   fieldDoubleScalarOnCells->setOrderNumber(-1);
315   fieldDoubleScalarOnCells->setTime(0.0);
316
317   fieldDoubleScalarOnCells->setComponentName(1,"Vx");
318   fieldDoubleScalarOnCells->setComponentDescription(1,"comp1");
319   fieldDoubleScalarOnCells->setMEDComponentUnit(1,"unit1");
320
321   fieldDoubleScalarOnCells->setValueType(MED_REEL64);
322
323   FIELD<double> * fieldDoubleVectorOnCells = new FIELD<double>(supportOnCells,SpaceDimension);
324   fieldDoubleVectorOnCells->setName("fieldVectorrDoubleCell");
325   fieldDoubleVectorOnCells->setIterationNumber(-1);
326   fieldDoubleVectorOnCells->setOrderNumber(-1);
327   fieldDoubleVectorOnCells->setTime(0.0);
328
329   fieldDoubleVectorOnCells->setComponentName(1,"Vx");
330   fieldDoubleVectorOnCells->setComponentDescription(1,"comp1");
331   fieldDoubleVectorOnCells->setMEDComponentUnit(1,"unit1");
332   fieldDoubleVectorOnCells->setComponentName(2,"Vy");
333   fieldDoubleVectorOnCells->setComponentDescription(2,"comp2");
334   fieldDoubleVectorOnCells->setMEDComponentUnit(2,"unit2");
335   fieldDoubleVectorOnCells->setComponentName(3,"Vz");
336   fieldDoubleVectorOnCells->setComponentDescription(3,"comp3");
337   fieldDoubleVectorOnCells->setMEDComponentUnit(3,"unit3");
338
339   fieldDoubleVectorOnCells->setValueType(MED_REEL64);
340
341   FIELD<int> * fieldIntScalarOnNodes = new FIELD<int>(supportOnNodes,1);
342   fieldIntScalarOnNodes->setName("fieldScalarIntNode");
343   fieldIntScalarOnNodes->setIterationNumber(-1);
344   fieldIntScalarOnNodes->setOrderNumber(-1);
345   fieldIntScalarOnNodes->setTime(0.0);
346
347   fieldIntScalarOnNodes->setComponentName(1,"Vx");
348   fieldIntScalarOnNodes->setComponentDescription(1,"comp1");
349   fieldIntScalarOnNodes->setMEDComponentUnit(1,"unit1");
350
351   fieldIntScalarOnNodes->setValueType(MED_INT32);
352
353   FIELD<int> * fieldIntVectorOnNodes = new FIELD<int>(supportOnNodes,SpaceDimension);
354   fieldIntVectorOnNodes->setName("fieldVectorIntNode");
355   fieldIntVectorOnNodes->setIterationNumber(-1);
356   fieldIntVectorOnNodes->setOrderNumber(-1);
357   fieldIntVectorOnNodes->setTime(0.0);
358
359   fieldIntVectorOnNodes->setComponentName(1,"Vx");
360   fieldIntVectorOnNodes->setComponentDescription(1,"comp1");
361   fieldIntVectorOnNodes->setMEDComponentUnit(1,"unit1");
362   fieldIntVectorOnNodes->setComponentName(2,"Vy");
363   fieldIntVectorOnNodes->setComponentDescription(2,"comp2");
364   fieldIntVectorOnNodes->setMEDComponentUnit(2,"unit2");
365   fieldIntVectorOnNodes->setComponentName(3,"Vz");
366   fieldIntVectorOnNodes->setComponentDescription(3,"comp3");
367   fieldIntVectorOnNodes->setMEDComponentUnit(3,"unit3");
368
369   fieldIntVectorOnNodes->setValueType(MED_INT32);
370
371   FIELD<int> * fieldIntScalarOnCells = new FIELD<int>(supportOnCells,1);
372   fieldIntScalarOnCells->setName("fieldScalarIntCell");
373   fieldIntScalarOnCells->setIterationNumber(-1);
374   fieldIntScalarOnCells->setOrderNumber(-1);
375   fieldIntScalarOnCells->setTime(0.0);
376
377   fieldIntScalarOnCells->setComponentName(1,"Vx");
378   fieldIntScalarOnCells->setComponentDescription(1,"comp1");
379   fieldIntScalarOnCells->setMEDComponentUnit(1,"unit1");
380
381   fieldIntScalarOnCells->setValueType(MED_INT32);
382
383   FIELD<int> * fieldIntVectorOnCells = new FIELD<int>(supportOnCells,SpaceDimension);
384   fieldIntVectorOnCells->setName("fieldVectorrIntCell");
385   fieldIntVectorOnCells->setIterationNumber(-1);
386   fieldIntVectorOnCells->setOrderNumber(-1);
387   fieldIntVectorOnCells->setTime(0.0);
388
389   fieldIntVectorOnCells->setComponentName(1,"Vx");
390   fieldIntVectorOnCells->setComponentDescription(1,"comp1");
391   fieldIntVectorOnCells->setMEDComponentUnit(1,"unit1");
392   fieldIntVectorOnCells->setComponentName(2,"Vy");
393   fieldIntVectorOnCells->setComponentDescription(2,"comp2");
394   fieldIntVectorOnCells->setMEDComponentUnit(2,"unit2");
395   fieldIntVectorOnCells->setComponentName(3,"Vz");
396   fieldIntVectorOnCells->setComponentDescription(3,"comp3");
397   fieldIntVectorOnCells->setMEDComponentUnit(3,"unit3");
398
399   fieldIntVectorOnCells->setValueType(MED_INT32);
400
401   for (int i = 0; i<numberOfNodes; i++)
402     {
403       double valueDbl1, valueDbl2, valueDbl3;
404       int valueInt1, valueInt2, valueInt3;
405       valueInt1 = i+1;
406       valueInt2 = i+2;
407       valueInt3 = i+3;
408       valueDbl1 = valueInt1*0.1;
409       valueDbl2 = valueInt2*0.1;
410       valueDbl3 = valueInt3*0.1;
411       fieldDoubleScalarOnNodes->setValueIJ(i+1,1,valueDbl1);
412
413       fieldIntScalarOnNodes->setValueIJ(i+1,1,valueInt1);
414
415       fieldDoubleVectorOnNodes->setValueIJ(i+1,1,valueDbl1);
416       fieldDoubleVectorOnNodes->setValueIJ(i+1,2,valueDbl2);
417       fieldDoubleVectorOnNodes->setValueIJ(i+1,3,valueDbl3);
418
419       fieldIntVectorOnNodes->setValueIJ(i+1,1,valueInt1);
420       fieldIntVectorOnNodes->setValueIJ(i+1,2,valueInt2);
421       fieldIntVectorOnNodes->setValueIJ(i+1,3,valueInt3);
422     }
423
424   for (int i = 0; i<numberOfCells; i++)
425     {
426       double valueDbl1, valueDbl2, valueDbl3;
427       int valueInt1, valueInt2, valueInt3;
428       valueInt1 = i+1;
429       valueInt2 = i+2;
430       valueInt3 = i+3;
431       valueDbl1 = valueInt1*0.1;
432       valueDbl2 = valueInt2*0.1;
433       valueDbl3 = valueInt3*0.1;
434       fieldDoubleScalarOnCells->setValueIJ(i+1,1,valueDbl1);
435
436       fieldIntScalarOnCells->setValueIJ(i+1,1,valueInt1);
437
438       fieldDoubleVectorOnCells->setValueIJ(i+1,1,valueDbl1);
439       fieldDoubleVectorOnCells->setValueIJ(i+1,2,valueDbl2);
440       fieldDoubleVectorOnCells->setValueIJ(i+1,3,valueDbl3);
441
442       fieldIntVectorOnCells->setValueIJ(i+1,1,valueInt1);
443       fieldIntVectorOnCells->setValueIJ(i+1,2,valueInt2);
444       fieldIntVectorOnCells->setValueIJ(i+1,3,valueInt3);
445     }
446
447   idMed = fieldDoubleScalarOnNodes->addDriver(MED_DRIVER,filenameMed,fieldDoubleScalarOnNodes->getName());
448   fieldDoubleScalarOnNodes->write(idMed) ;
449
450   idMed = fieldIntScalarOnNodes->addDriver(MED_DRIVER,filenameMed,fieldIntScalarOnNodes->getName());
451   fieldIntScalarOnNodes->write(idMed) ;
452
453   idMed = fieldDoubleVectorOnNodes->addDriver(MED_DRIVER,filenameMed,fieldDoubleVectorOnNodes->getName());
454   fieldDoubleVectorOnNodes->write(idMed) ;
455
456   idMed = fieldIntVectorOnNodes->addDriver(MED_DRIVER,filenameMed,fieldIntVectorOnNodes->getName());
457   fieldIntVectorOnNodes->write(idMed) ;
458
459   idMed = fieldDoubleScalarOnCells->addDriver(MED_DRIVER,filenameMed,fieldDoubleScalarOnCells->getName());
460   fieldDoubleScalarOnCells->write(idMed) ;
461
462   idMed = fieldIntScalarOnCells->addDriver(MED_DRIVER,filenameMed,fieldIntScalarOnCells->getName());
463   fieldIntScalarOnCells->write(idMed) ;
464
465   idMed = fieldDoubleVectorOnCells->addDriver(MED_DRIVER,filenameMed,fieldDoubleVectorOnCells->getName());
466   fieldDoubleVectorOnCells->write(idMed) ;
467
468   idMed = fieldIntVectorOnCells->addDriver(MED_DRIVER,filenameMed,fieldIntVectorOnCells->getName());
469   fieldIntVectorOnCells->write(idMed) ;
470
471   idVtk = fieldDoubleScalarOnNodes->addDriver(VTK_DRIVER,filenameVtk,fieldDoubleScalarOnNodes->getName());
472   fieldDoubleScalarOnNodes->writeAppend(idVtk) ;
473
474   idVtk = fieldIntScalarOnNodes->addDriver(VTK_DRIVER,filenameVtk,fieldIntScalarOnNodes->getName());
475   fieldIntScalarOnNodes->writeAppend(idVtk) ;
476
477   idVtk = fieldDoubleVectorOnNodes->addDriver(VTK_DRIVER,filenameVtk,fieldDoubleVectorOnNodes->getName());
478   fieldDoubleVectorOnNodes->writeAppend(idVtk) ;
479
480   idVtk = fieldIntVectorOnNodes->addDriver(VTK_DRIVER,filenameVtk,fieldIntVectorOnNodes->getName());
481   fieldIntVectorOnNodes->writeAppend(idVtk) ;
482
483   idVtk = fieldDoubleScalarOnCells->addDriver(VTK_DRIVER,filenameVtk,fieldDoubleScalarOnCells->getName());
484   fieldDoubleScalarOnCells->writeAppend(idVtk) ;
485
486   idVtk = fieldIntScalarOnCells->addDriver(VTK_DRIVER,filenameVtk,fieldIntScalarOnCells->getName());
487   fieldIntScalarOnCells->writeAppend(idVtk) ;
488
489   idVtk = fieldDoubleVectorOnCells->addDriver(VTK_DRIVER,filenameVtk,fieldDoubleVectorOnCells->getName());
490   fieldDoubleVectorOnCells->writeAppend(idVtk) ;
491
492   idVtk = fieldIntVectorOnCells->addDriver(VTK_DRIVER,filenameVtk,fieldIntVectorOnCells->getName());
493   fieldIntVectorOnCells->writeAppend(idVtk) ;
494
495   delete fieldDoubleScalarOnNodes;
496   delete fieldIntScalarOnNodes;
497   delete fieldDoubleVectorOnNodes;
498   delete fieldIntVectorOnNodes;
499   delete fieldDoubleScalarOnCells;
500   delete fieldIntScalarOnCells;
501   delete fieldDoubleVectorOnCells;
502   delete fieldIntVectorOnCells;
503
504   delete supportOnNodes;
505   delete supportOnCells;
506 }