1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Anthony Geay (CEA/DEN)
24 from math import pi,e,sqrt
25 from MEDLoaderDataForTest import MEDLoaderDataForTest
27 class MEDLoaderTest(unittest.TestCase):
28 def testMesh1DRW(self):
29 mesh=MEDLoaderDataForTest.build1DMesh_1();
30 mesh.checkCoherency();
31 MEDLoader.MEDLoader.WriteUMesh("Pyfile1.med",mesh,True);
32 mesh_rw=MEDLoader.MEDLoader.ReadUMeshFromFile("Pyfile1.med",mesh.getName(),0);
33 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
36 def testMesh2DCurveRW(self):
37 mesh=MEDLoaderDataForTest.build2DCurveMesh_1();
38 mesh.checkCoherency();
39 MEDLoader.MEDLoader.WriteUMesh("Pyfile2.med",mesh,True);
40 mesh_rw=MEDLoader.MEDLoader.ReadUMeshFromFile("Pyfile2.med",mesh.getName(),0);
41 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
44 def testMesh2DRW(self):
45 mesh=MEDLoaderDataForTest.build2DMesh_1();
46 mesh.checkCoherency();
47 MEDLoader.MEDLoader.WriteUMesh("Pyfile3.med",mesh,True);
48 mesh_rw=MEDLoader.MEDLoader.ReadUMeshFromFile("Pyfile3.med",mesh.getName(),0);
49 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
52 def testMesh3DSurfRW(self):
53 mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
54 mesh.checkCoherency();
55 MEDLoader.MEDLoader.WriteUMesh("Pyfile4.med",mesh,True);
56 mesh_rw=MEDLoader.MEDLoader.ReadUMeshFromFile("Pyfile4.med",mesh.getName(),0);
57 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
60 def testMesh3DRW(self):
61 mesh=MEDLoaderDataForTest.build3DMesh_1();
62 mesh.checkCoherency();
63 MEDLoader.MEDLoader.WriteUMesh("Pyfile5.med",mesh,True);
64 mesh_rw=MEDLoader.MEDLoader.ReadUMeshFromFile("Pyfile5.med",mesh.getName(),0);
65 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
68 def testFieldRW1(self):
69 f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
70 MEDLoader.MEDLoader.WriteField("Pyfile6.med",f1,True);
71 f2=MEDLoader.MEDLoader.ReadFieldCell("Pyfile6.med",f1.getMesh().getName(),0,f1.getName(),0,1);
72 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
74 f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
75 MEDLoader.MEDLoader.WriteField("Pyfile7.med",f1,True);
76 f2=MEDLoader.MEDLoader.ReadFieldNode("Pyfile7.med",f1.getMesh().getName(),0,f1.getName(),2,3);
77 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
78 self.assertRaises(Exception,MEDLoader.MEDLoader.ReadFieldCell,"Pyfile7.med",f1.getMesh().getName(),0,f1.getName(),2,3);
81 def testFieldRW2(self):
82 fileName="Pyfile8.med";
85 f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
86 MEDLoader.MEDLoader.WriteField(fileName,f1,True);
88 f1.getArray().setIJ(0,0,VAL1);
89 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
90 f1.setTime(10.14,18,19);
91 f1.getArray().setIJ(0,0,VAL2);
92 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
93 #retrieving time steps...
94 f2=MEDLoader.MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),8,9);
96 f1.getArray().setIJ(0,0,VAL1);
97 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
98 f2=MEDLoader.MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),0,1);
99 f3=MEDLoaderDataForTest.buildVecFieldOnCells_1();
100 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
101 f2=MEDLoader.MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),18,19);
102 f1.setTime(10.14,18,19);
103 f1.getArray().setIJ(0,0,VAL2);
104 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
105 #test of throw on invalid (dt,it)
106 self.assertRaises(Exception,MEDLoader.MEDLoader.ReadFieldCell,fileName,f1.getMesh().getName(),0,f1.getName(),28,19);
108 f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
109 fileName2="Pyfile9.med";
110 MEDLoader.MEDLoader.WriteField(fileName2,f1,True);
111 f1.setTime(110.,108,109);
112 tmp=f1.getArray().getPointer();
113 f1.getArray().setIJ(0,3,VAL1);
114 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2,f1);
115 f1.setTime(210.,208,209);
116 f1.getArray().setIJ(0,3,VAL2);
117 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2,f1);
118 f2=MEDLoader.MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),108,109);
119 f1.setTime(110.,108,109);
120 f1.getArray().setIJ(0,3,VAL1);
121 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
122 f2=MEDLoader.MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),2,3);
123 f3=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
124 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
125 f2=MEDLoader.MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),208,209);
126 f1.setTime(210.,208,209);
127 f1.getArray().setIJ(0,3,VAL2);
128 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
132 # Multi field in a same file, but this field has several
134 def testFieldRW3(self):
135 fileName="Pyfile11.med";
137 VAL2=-1111111111111.;
140 f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
141 f1.getMesh().setName(name3);
144 tmp=f1.getArray().getPointer();
145 f1.getArray().setIJ(0,0,VAL1);
146 MEDLoader.MEDLoader.WriteField(fileName,f1,True);
147 f1.setTime(10.14,18,19);
148 f1.getArray().setIJ(0,0,VAL2);
149 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
150 f1.getMesh().setName(name3);
151 f1.setTime(10.55,28,29);
152 f1.getArray().setIJ(0,0,3*VAL1);
153 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
154 vec=MEDLoader.MEDLoader.GetMeshNamesOnField(fileName,name1);
155 f1.setTime(10.66,38,39);
156 f1.getArray().setIJ(0,0,3*VAL2);
157 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
158 f1.setTime(10.77,48,49);
159 f1.getArray().setIJ(0,0,4*VAL2);
160 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
162 f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
164 f1.getMesh().setName(name3);
165 f1.setTime(110.,8,9);
166 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
167 f1.setTime(110.,108,109);
168 tmp=f1.getArray().getPointer();
169 f1.getArray().setIJ(0,3,VAL1);
170 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
171 f1.setTime(210.,208,209);
172 f1.getArray().setIJ(0,3,VAL2);
173 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
175 it1=MEDLoader.MEDLoader.GetCellFieldIterations(fileName,name3,name1);
176 self.assertEqual(5,len(it1));
177 self.assertEqual(8,it1[0][0]); self.assertEqual(9,it1[0][1]);
178 self.assertEqual(18,it1[1][0]); self.assertEqual(19,it1[1][1]);
179 self.assertEqual(28,it1[2][0]); self.assertEqual(29,it1[2][1]);
180 self.assertEqual(38,it1[3][0]); self.assertEqual(39,it1[3][1]);
181 self.assertEqual(48,it1[4][0]); self.assertEqual(49,it1[4][1]);
182 it3=MEDLoader.MEDLoader.GetNodeFieldIterations(fileName,name3,name1);
183 self.assertEqual(3,len(it3));
184 self.assertEqual(8,it3[0][0]); self.assertEqual(9,it3[0][1]);
185 self.assertEqual(108,it3[1][0]); self.assertEqual(109,it3[1][1]);
186 self.assertEqual(208,it3[2][0]); self.assertEqual(209,it3[2][1]);
189 f1=MEDLoader.MEDLoader.ReadFieldCell(fileName,name3,0,name1,8,9);
190 self.assertAlmostEqual(VAL1,f1.getArray().getIJ(0,0),13);
191 f1=MEDLoader.MEDLoader.ReadFieldCell(fileName,name3,0,name1,18,19);
192 self.assertAlmostEqual(VAL2,f1.getArray().getIJ(0,0),13);
193 f1=MEDLoader.MEDLoader.ReadFieldCell(fileName,name3,0,name1,28,29);
194 self.assertAlmostEqual(3*VAL1,f1.getArray().getIJ(0,0),13);
195 f1=MEDLoader.MEDLoader.ReadFieldCell(fileName,name3,0,name1,38,39);
196 self.assertAlmostEqual(3*VAL2,f1.getArray().getIJ(0,0),13);
197 f1=MEDLoader.MEDLoader.ReadFieldCell(fileName,name3,0,name1,48,49);
198 self.assertAlmostEqual(4*VAL2,f1.getArray().getIJ(0,0),13);
200 f1=MEDLoader.MEDLoader.ReadFieldNode(fileName,name3,0,name1,8,9);
201 self.assertAlmostEqual(71.,f1.getArray().getIJ(0,3),13);
202 f1=MEDLoader.MEDLoader.ReadFieldNode(fileName,name3,0,name1,108,109);
203 self.assertAlmostEqual(VAL1,f1.getArray().getIJ(0,3),13);
204 f1=MEDLoader.MEDLoader.ReadFieldNode(fileName,name3,0,name1,208,209);
205 self.assertAlmostEqual(VAL2,f1.getArray().getIJ(0,3),13);
208 def testMultiMeshRW1(self):
209 fileName="Pyfile10.med";
210 mesh1=MEDLoaderDataForTest.build3DMesh_1();
212 mesh2=mesh1.buildPartOfMySelf(part1,True);
213 mesh2.setName("mesh2");
215 mesh3=mesh1.buildPartOfMySelf(part2,True);
216 mesh3.setName("mesh3");
217 mesh4=MEDLoader.MEDCouplingUMesh.New();
218 mesh4.setName("mesh4");
219 mesh4.setMeshDimension(3);
220 mesh4.allocateCells(1);
222 mesh4.insertNextCell(MEDLoader.NORM_TETRA4,4,conn[0:4])
223 mesh4.finishInsertingCells();
224 mesh4.setCoords(mesh1.getCoords());
225 meshes=[mesh1,mesh2,mesh3,mesh4]
227 MEDLoader.MEDLoader.WriteUMeshesPartition(fileName,mnane,meshes,True);
229 mesh5=MEDLoader.MEDLoader.ReadUMeshFromFile(fileName,mnane);
230 mesh1.setName(mnane);
231 part3=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
232 mesh6=mesh5.buildPartOfMySelf(part3,True);
233 mesh6.setName(mnane);
234 self.assertTrue(mesh6.isEqual(mesh1,1e-12));
235 grps=MEDLoader.MEDLoader.GetMeshGroupsNames(fileName,mnane);
236 self.assertEqual(4,len(grps));
240 grps.index("3DMesh_1");
243 mesh2_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
244 self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
246 mesh3_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
247 self.assertTrue(mesh3_2.isEqual(mesh3,1e-12));
249 mesh4_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
250 self.assertTrue(mesh4_2.isEqual(mesh4,1e-12));
252 mesh1_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
253 mesh1.setName("3DMesh_1");
254 self.assertTrue(mesh1_2.isEqual(mesh1,1e-12));
256 vec=["Family_-3","Family_-5"];
257 mesh2_2=MEDLoader.MEDLoader.ReadUMeshFromFamilies(fileName,mnane,0,vec);
258 mesh2_2.setName("mesh2");
259 self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
261 ret=MEDLoader.MEDLoader.GetMeshFamiliesNamesOnGroup(fileName,"3DToto","3DMesh_1");
262 self.assertEqual(4,len(ret));
263 self.assertEqual(ret[0],"Family_-2");
264 self.assertEqual(ret[1],"Family_-3");
265 self.assertEqual(ret[2],"Family_-4");
266 self.assertEqual(ret[3],"Family_-5");
268 ret1=MEDLoader.MEDLoader.GetMeshGroupsNamesOnFamily(fileName,"3DToto","Family_-3");
269 self.assertEqual(2,len(ret1));
270 self.assertEqual(ret1[0],"3DMesh_1");
271 self.assertEqual(ret1[1],"mesh2");
274 def testFieldProfilRW1(self):
275 fileName="Pyfile12.med";
276 mesh1=MEDLoaderDataForTest.build3DMesh_1();
277 da,b,newNbOfNodes=mesh1.mergeNodes(1e-12);
278 MEDLoader.MEDLoader.WriteUMesh(fileName,mesh1,True);
280 mesh2=mesh1.buildPartOfMySelf(part1,True);
281 mesh2.setName(mesh1.getName());#<- important for the test
283 nbOfCells=mesh2.getNumberOfCells();
284 self.assertEqual(5,nbOfCells);
285 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS,MEDLoader.ONE_TIME);
286 f1.setName("VectorFieldOnCells");
288 array=MEDLoader.DataArrayDouble.New();
289 array.alloc(nbOfCells,2);
291 arr1=[71.,171.,10.,110.,20.,120.,30.,130.,40.,140.]
292 array.setValues(arr1,nbOfCells,2);
293 f1.setTime(3.14,2,7);
296 MEDLoader.MEDLoader.WriteField(fileName,f1,False);#<- False important for the test
298 f2=MEDLoader.MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),2,7);
299 tt=MEDLoader.MEDLoader.GetTypesOfField(fileName,f1.getMesh().getName(),f1.getName());
300 self.assertEqual(tt,[MEDLoader.ON_CELLS]);
302 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
306 def testFieldGaussRW1(self):
307 fileName="Pyfile13.med";
308 f1=MEDLoaderDataForTest.buildVecFieldOnGauss_1();
309 MEDLoader.MEDLoader.WriteField(fileName,f1,True);
310 f2=MEDLoader.MEDLoader.ReadField(MEDLoader.ON_GAUSS_PT,fileName,f1.getMesh().getName(),0,f1.getName(),1,5);
311 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
314 def testFieldGaussNERW1(self):
315 fileName="Pyfile14.med";
316 f1=MEDLoaderDataForTest.buildVecFieldOnGaussNE_1();
317 MEDLoader.MEDLoader.WriteField(fileName,f1,True);
318 f2=MEDLoader.MEDLoader.ReadField(MEDLoader.ON_GAUSS_NE,fileName,f1.getMesh().getName(),0,f1.getName(),1,5);
319 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
322 def testMesh3DSurfShuffleRW(self):
323 fileName="Pyfile15.med";
324 mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
325 renumber1=[2,5,1,0,3,4]
326 mesh.renumberCells(renumber1,False);
327 mesh.checkCoherency();
328 MEDLoader.MEDLoader.WriteUMesh(fileName,mesh,True);
329 mesh_rw=MEDLoader.MEDLoader.ReadUMeshFromFile(fileName,mesh.getName(),0);
330 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
333 def testMultiFieldShuffleRW1(self):
334 fileName="Pyfile17.med";
335 m=MEDLoaderDataForTest.build3DMesh_2();
336 self.assertEqual(20,m.getNumberOfCells());
337 self.assertEqual(45,m.getNumberOfNodes());
339 m.convertToPolyTypes(polys);
340 renum=[1,3,2,8,9,12,13,16,19,0,4,7,5,15,14,17,10,18,6,11]
341 m.renumberCells(renum,False);
342 m.orientCorrectlyPolyhedrons();
344 MEDLoader.MEDLoader.WriteUMesh(fileName,m,True);
345 f1Tmp=m.getMeasureField(False);
346 f1=f1Tmp.buildNewTimeReprFromThis(MEDLoader.ONE_TIME,False);
348 f_1=f1.cloneWithMesh(True);
349 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
351 f1.setTime(0.01,3,4);
352 f_2=f1.cloneWithMesh(True);
353 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
354 f1.applyFunc("2*x/3");
355 f1.setTime(0.02,5,6);
356 f_3=f1.cloneWithMesh(True);
357 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
359 its=[(1,2),(3,4),(5,6)];
360 fs=MEDLoader.MEDLoader.ReadFieldsOnSameMesh(MEDLoader.ON_CELLS,fileName,f_1.getMesh().getName(),0,f_1.getName(),its);
361 self.assertEqual(3,len(fs));
362 self.assertTrue(fs[0].isEqual(f_1,1e-12,1e-12));
363 self.assertTrue(fs[1].isEqual(f_2,1e-12,1e-12));
364 self.assertTrue(fs[2].isEqual(f_3,1e-12,1e-12));
367 def testWriteUMeshesRW1(self):
368 fileName="Pyfile18.med";
369 m3d=MEDLoaderDataForTest.build3DMesh_2();
372 nodes=m3d.findNodesOnPlane(pt,vec,1e-12);
373 m2d=m3d.buildFacePartOfMySelfNode(nodes,True);
375 m2d.renumberCells(renumber,False);
376 m2d.setName("ExampleOfMultiDimW");
378 MEDLoader.MEDLoader.WriteUMeshes(fileName,meshes,True);
379 m3d_bis=MEDLoader.MEDLoader.ReadUMeshFromFile(fileName,m2d.getName(),0);
380 self.assertTrue(not m3d_bis.isEqual(m3d,1e-12));
381 m3d_bis.setName(m3d.getName());
382 self.assertTrue(m3d_bis.isEqual(m3d,1e-12));
383 m2d_bis=MEDLoader.MEDLoader.ReadUMeshFromFile(fileName,m2d.getName(),-1);#-1 for faces
384 self.assertTrue(m2d_bis.isEqual(m2d,1e-12));
385 # Creation of a field on faces.
386 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS,MEDLoader.ONE_TIME);
387 f1.setName("FieldOnFacesShuffle");
389 array=MEDLoader.DataArrayDouble.New();
390 arr1=[71.,171.,10.,110.,20.,120.,30.,130.,40.,140.]
391 array.setValues(arr1,m2d.getNumberOfCells(),2);
392 array.setInfoOnComponent(0,"plkj [mm]");
393 array.setInfoOnComponent(1,"pqqqss [mm]");
395 tmp=array.setValues(arr1,m2d.getNumberOfCells(),2);
396 f1.setTime(3.14,2,7);
398 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
399 f2=MEDLoader.MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),-1,f1.getName(),2,7);
400 self.assertTrue(f2.isEqual(f1,1e-12,1e-12));
403 def testFieldNodeProfilRW1(self):
404 fileName="Pyfile19.med";
405 fileName2="Pyfile20.med";
406 m=MEDLoaderDataForTest.build2DMesh_1();
407 nbOfNodes=m.getNumberOfNodes();
408 MEDLoader.MEDLoader.WriteUMesh(fileName,m,True);
409 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES,MEDLoader.ONE_TIME);
410 f1.setName("VFieldOnNodes");
412 array=MEDLoader.DataArrayDouble.New();
413 arr1=[1.,101.,2.,102.,3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.,12.,112.]
414 array.setValues(arr1,nbOfNodes,2);
416 array.setInfoOnComponent(0,"tyty [mm]");
417 array.setInfoOnComponent(1,"uiop [MW]");
418 f1.setTime(3.14,2,7);
421 f2=f1.buildSubPart(arr2);
422 f2.getMesh().setName(f1.getMesh().getName());
423 MEDLoader.MEDLoader.WriteField(fileName,f2,False);#<- False important for the test
425 f3=MEDLoader.MEDLoader.ReadFieldNode(fileName,f2.getMesh().getName(),0,f2.getName(),2,7);
427 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
430 f2.renumberNodes(arr3);
431 MEDLoader.MEDLoader.WriteUMesh(fileName2,m,True);
432 MEDLoader.MEDLoader.WriteField(fileName2,f2,False);#<- False important for the test
433 f3=MEDLoader.MEDLoader.ReadFieldNode(fileName2,f2.getMesh().getName(),0,f2.getName(),2,7);
435 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
439 def testFieldNodeProfilRW2(self):
440 fileName="Pyfile23.med";
441 mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
442 MEDLoader.MEDLoader.WriteUMesh(fileName,mesh,True);
444 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES,MEDLoader.ONE_TIME);
445 f1.setName("FieldMix");
447 arr2=[1071.,1171.,1010.,1110.,1020.,1120.,1030.,1130.,1040.,1140.,1050.,1150.,
448 1060.,1160.,1070.,1170.,1080.,1180.,1090.,1190.,1091.,1191.,1092.,1192.];
449 array=MEDLoader.DataArrayDouble.New();
450 array.setValues(arr2,12,2);
452 array.setInfoOnComponent(0,"plkj [mm]");
453 array.setInfoOnComponent(1,"pqqqss [mm]");
454 tmp=array.getPointer();
455 f1.setTime(3.17,2,7);
457 renumArr=[3,7,2,1,5,11,10,0,9,6,8,4]
458 f1.renumberNodes(renumArr);
460 MEDLoader.MEDLoader.WriteField(fileName,f1,False);#<- False important for the test
461 f2=MEDLoader.MEDLoader.ReadFieldNode(fileName,f1.getMesh().getName(),0,f1.getName(),2,7);
462 self.assertTrue(f2.isEqual(f1,1e-12,1e-12));
466 def testMixCellAndNodesFieldRW1(self):
467 fileName="Pyfile21.med";
468 mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
469 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS,MEDLoader.ONE_TIME);
470 f1.setName("FieldMix");
472 array=MEDLoader.DataArrayDouble.New();
474 arr1=[71.,171.,10.,110.,20.,120.,30.,130.,40.,140.,50.,150.]
475 array.setValues(arr1,6,2);
476 array.setInfoOnComponent(0,"plkj [mm]");
477 array.setInfoOnComponent(1,"pqqqss [mm]");
478 f1.setTime(3.14,2,7);
481 f2=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES,MEDLoader.ONE_TIME);
482 f2.setName("FieldMix");
484 array=MEDLoader.DataArrayDouble.New();
486 arr2=[1071.,1171.,1010.,1110.,1020.,1120.,1030.,1130.,1040.,1140.,1050.,1150.,
487 1060.,1160.,1070.,1170.,1080.,1180.,1090.,1190.,1091.,1191.,1092.,1192.]
488 array.setValues(arr2,12,2)
489 array.setInfoOnComponent(0,"plkj [mm]");
490 array.setInfoOnComponent(1,"pqqqss [mm]");
491 f2.setTime(3.14,2,7);
494 MEDLoader.MEDLoader.WriteField(fileName,f1,True);
495 ts=MEDLoader.MEDLoader.GetTypesOfField(fileName,f1.getMesh().getName(),f1.getName());
496 self.assertEqual(1,len(ts));
497 self.assertEqual(MEDLoader.ON_CELLS,ts[0]);
498 fs=MEDLoader.MEDLoader.GetAllFieldNamesOnMesh(fileName,f1.getMesh().getName());
499 self.assertEqual(1,len(fs));
500 self.assertTrue(fs[0]=="FieldMix");
501 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f2);
502 fs=MEDLoader.MEDLoader.GetAllFieldNamesOnMesh(fileName,f1.getMesh().getName());
503 self.assertEqual(1,len(fs));
504 self.assertTrue(fs[0]=="FieldMix");
506 ts=MEDLoader.MEDLoader.GetTypesOfField(fileName,f1.getMesh().getName(),f1.getName());
507 self.assertEqual(2,len(ts));
508 self.assertEqual(MEDLoader.ON_NODES,ts[0]);
509 self.assertEqual(MEDLoader.ON_CELLS,ts[1]);
511 f3=MEDLoader.MEDLoader.ReadFieldNode(fileName,f1.getMesh().getName(),0,f1.getName(),2,7);
512 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
513 f3=MEDLoader.MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),2,7);
514 self.assertTrue(f3.isEqual(f1,1e-12,1e-12));
518 def testGetAllFieldNamesRW1(self):
519 fileName="Pyfile22.med";
520 mesh=MEDLoaderDataForTest.build2DMesh_2();
521 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES,MEDLoader.ONE_TIME);
522 f1.setName("Field1");
523 f1.setTime(3.44,5,6);
525 f1.fillFromAnalytic(2,"x+y");
526 MEDLoader.MEDLoader.WriteField(fileName,f1,True);
527 f1.setTime(1002.3,7,8);
528 f1.fillFromAnalytic(2,"x+77.*y");
529 MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
530 f1.setName("Field2");
531 MEDLoader.MEDLoader.WriteField(fileName,f1,False);
532 f1.setName("Field3");
533 mesh.setName("2DMesh_2Bis");
534 MEDLoader.MEDLoader.WriteField(fileName,f1,False);
535 f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS,MEDLoader.ONE_TIME);
536 f1.setName("Field8");
537 f1.setTime(8.99,7,9);
539 f1.fillFromAnalytic(3,"3*x+y");
540 MEDLoader.MEDLoader.WriteField(fileName,f1,False);
541 fs=MEDLoader.MEDLoader.GetAllFieldNames(fileName);
542 self.assertEqual(4,len(fs));
543 self.assertTrue(fs[0]=="Field1");
544 self.assertTrue(fs[1]=="Field2");
545 self.assertTrue(fs[2]=="Field3");
546 self.assertTrue(fs[3]=="Field8");
549 def testBigNbOfCompoNonReg(self):
550 fileName="Pyfile57.med"
551 m=MEDLoader.MEDCouplingCMesh() ; m.setCoords(MEDLoader.DataArrayDouble([0,1,2,3]),MEDLoader.DataArrayDouble([0,1]),MEDLoader.DataArrayDouble([0,1]))
552 m=m.buildUnstructured() ; m.setName("TinyMesh")
553 f=MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) ; f.setMesh(m)
555 arr=MEDLoader.DataArrayDouble(nbOfCompo*3) ; arr.iota()
556 arr.rearrange(nbOfCompo)
557 arr.setInfoOnComponents(["c%i"%(i) for i in xrange(nbOfCompo)])
559 f.setName("FieldBigCompo")
560 MEDLoader.MEDLoader.WriteField(fileName,f,True)
561 f2=MEDLoader.MEDLoader.ReadFieldCell(fileName,m.getName(),0,f.getName(),-1,-1)
562 self.assertTrue(f.isEqual(f2,1e-12,1e-12))