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)
22 from MEDLoader import *
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.WriteUMeshDep("Pyfile1.med",mesh,False);
32 mesh_rw=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.WriteUMeshDep("Pyfile2.med",mesh,False);
40 mesh_rw=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.WriteUMeshDep("Pyfile3.med",mesh,False);
48 mesh_rw=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.WriteUMeshDep("Pyfile4.med",mesh,False);
56 mesh_rw=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.WriteUMeshDep("Pyfile5.med",mesh,False);
64 mesh_rw=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.WriteFieldDep("Pyfile6.med",f1,False);
71 f2=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.WriteFieldDep("Pyfile7.med",f1,False);
76 f2=MEDLoader.ReadFieldNode("Pyfile7.med",f1.getMesh().getName(),0,f1.getName(),2,3);
77 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
80 def testFieldRW2(self):
81 fileName="Pyfile8.med";
84 f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
85 MEDLoader.WriteFieldDep(fileName,f1,False);
87 f1.getArray().setIJ(0,0,VAL1);
88 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
89 f1.setTime(10.14,18,19);
90 f1.getArray().setIJ(0,0,VAL2);
91 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
92 #retrieving time steps...
93 f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),8,9);
95 f1.getArray().setIJ(0,0,VAL1);
96 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
97 f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),0,1);
98 f3=MEDLoaderDataForTest.buildVecFieldOnCells_1();
99 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
100 f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),18,19);
101 f1.setTime(10.14,18,19);
102 f1.getArray().setIJ(0,0,VAL2);
103 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
105 f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
106 fileName2="Pyfile9.med";
107 MEDLoader.WriteFieldDep(fileName2,f1,False);
108 f1.setTime(110.,108,109);
109 tmp=f1.getArray().getPointer();
110 f1.getArray().setIJ(0,3,VAL1);
111 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2,f1);
112 f1.setTime(210.,208,209);
113 f1.getArray().setIJ(0,3,VAL2);
114 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2,f1);
115 f2=MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),108,109);
116 f1.setTime(110.,108,109);
117 f1.getArray().setIJ(0,3,VAL1);
118 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
119 f2=MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),2,3);
120 f3=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
121 self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
122 f2=MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),208,209);
123 f1.setTime(210.,208,209);
124 f1.getArray().setIJ(0,3,VAL2);
125 self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
129 # Multi field in a same file, but this field has several
131 def testFieldRW3(self):
132 fileName="Pyfile11.med";
134 VAL2=-1111111111111.;
137 f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
138 f1.getMesh().setName(name3);
141 tmp=f1.getArray().getPointer();
142 f1.getArray().setIJ(0,0,VAL1);
143 MEDLoader.WriteFieldDep(fileName,f1,False);
144 f1.setTime(10.14,18,19);
145 f1.getArray().setIJ(0,0,VAL2);
146 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
147 f1.getMesh().setName(name3);
148 f1.setTime(10.55,28,29);
149 f1.getArray().setIJ(0,0,3*VAL1);
150 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
151 f1.setTime(10.66,38,39);
152 f1.getArray().setIJ(0,0,3*VAL2);
153 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
154 f1.setTime(10.77,48,49);
155 f1.getArray().setIJ(0,0,4*VAL2);
156 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
158 f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
160 f1.getMesh().setName(name3);
161 f1.setTime(110.,8,9);
162 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
163 f1.setTime(110.,108,109);
164 tmp=f1.getArray().getPointer();
165 f1.getArray().setIJ(0,3,VAL1);
166 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
167 f1.setTime(210.,208,209);
168 f1.getArray().setIJ(0,3,VAL2);
169 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
171 it1=MEDLoader.GetCellFieldIterations(fileName,name3,name1);
172 self.assertEqual(5,len(it1));
173 self.assertEqual(8,it1[0][0]); self.assertEqual(9,it1[0][1]);
174 self.assertEqual(18,it1[1][0]); self.assertEqual(19,it1[1][1]);
175 self.assertEqual(28,it1[2][0]); self.assertEqual(29,it1[2][1]);
176 self.assertEqual(38,it1[3][0]); self.assertEqual(39,it1[3][1]);
177 self.assertEqual(48,it1[4][0]); self.assertEqual(49,it1[4][1]);
178 it3=MEDLoader.GetNodeFieldIterations(fileName,name3,name1);
179 self.assertEqual(3,len(it3));
180 self.assertEqual(8,it3[0][0]); self.assertEqual(9,it3[0][1]);
181 self.assertEqual(108,it3[1][0]); self.assertEqual(109,it3[1][1]);
182 self.assertEqual(208,it3[2][0]); self.assertEqual(209,it3[2][1]);
185 f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,8,9);
186 self.assertAlmostEqual(VAL1,f1.getArray().getIJ(0,0),13);
187 f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,18,19);
188 self.assertAlmostEqual(VAL2,f1.getArray().getIJ(0,0),13);
189 f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,28,29);
190 self.assertAlmostEqual(3*VAL1,f1.getArray().getIJ(0,0),13);
191 f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,38,39);
192 self.assertAlmostEqual(3*VAL2,f1.getArray().getIJ(0,0),13);
193 f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,48,49);
194 self.assertAlmostEqual(4*VAL2,f1.getArray().getIJ(0,0),13);
196 f1=MEDLoader.ReadFieldNode(fileName,name3,0,name1,8,9);
197 self.assertAlmostEqual(71.,f1.getArray().getIJ(0,3),13);
198 f1=MEDLoader.ReadFieldNode(fileName,name3,0,name1,108,109);
199 self.assertAlmostEqual(VAL1,f1.getArray().getIJ(0,3),13);
200 f1=MEDLoader.ReadFieldNode(fileName,name3,0,name1,208,209);
201 self.assertAlmostEqual(VAL2,f1.getArray().getIJ(0,3),13);
204 def testMultiMeshRW1(self):
205 fileName="Pyfile10.med";
206 mesh1=MEDLoaderDataForTest.build3DMesh_1();
208 mesh2=mesh1.buildPartOfMySelf(part1,True);
209 mesh2.setName("mesh2");
211 mesh3=mesh1.buildPartOfMySelf(part2,True);
212 mesh3.setName("mesh3");
213 mesh4=MEDCouplingUMesh.New();
214 mesh4.setName("mesh4");
215 mesh4.setMeshDimension(3);
216 mesh4.allocateCells(1);
218 mesh4.insertNextCell(NORM_TETRA4,4,conn[0:4])
219 mesh4.finishInsertingCells();
220 mesh4.setCoords(mesh1.getCoords());
221 meshes=[mesh1,mesh2,mesh3,mesh4]
223 MEDLoader.WriteUMeshesPartitionDep(fileName,mnane,meshes,False);
225 mesh5=MEDLoader.ReadUMeshFromFile(fileName,mnane);
226 mesh1.setName(mnane);
227 part3=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
228 mesh6=mesh5.buildPartOfMySelf(part3,True);
229 mesh6.setName(mnane);
230 self.assertTrue(mesh6.isEqual(mesh1,1e-12));
231 grps=MEDLoader.GetMeshGroupsNames(fileName,mnane);
232 self.assertEqual(4,len(grps));
236 grps.index("3DMesh_1");
239 mesh2_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
240 self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
242 mesh3_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
243 self.assertTrue(mesh3_2.isEqual(mesh3,1e-12));
245 mesh4_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
246 self.assertTrue(mesh4_2.isEqual(mesh4,1e-12));
248 mesh1_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
249 mesh1.setName("3DMesh_1");
250 self.assertTrue(mesh1_2.isEqual(mesh1,1e-12));
252 vec=["Family_-5","Family_-3"];
253 mesh2_2=MEDLoader.ReadUMeshFromFamilies(fileName,mnane,0,vec);
254 mesh2_2.setName("mesh2");
255 self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
258 def testMesh3DSurfShuffleRW(self):
259 fileName="Pyfile15.med";
260 mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
261 renumber1=[2,5,1,0,3,4]
262 mesh.renumberCells(renumber1,False);
263 mesh.checkCoherency();
264 MEDLoader.WriteUMeshDep(fileName,mesh,False);
265 mesh_rw=MEDLoader.ReadUMeshFromFile(fileName,mesh.getName(),0);
266 self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
269 def testMultiFieldShuffleRW1(self):
270 fileName="Pyfile17.med";
271 m=MEDLoaderDataForTest.build3DMesh_2();
272 self.assertEqual(20,m.getNumberOfCells());
273 self.assertEqual(45,m.getNumberOfNodes());
275 m.convertToPolyTypes(polys);
276 renum=[1,3,2,8,9,12,13,16,19,0,4,7,5,15,14,17,10,18,6,11]
277 m.renumberCells(renum,False);
278 m.orientCorrectlyPolyhedrons();
280 MEDLoader.WriteUMeshDep(fileName,m,False);
281 f1Tmp=m.getMeasureField(False);
282 f1=f1Tmp.buildNewTimeReprFromThis(ONE_TIME,False);
284 f_1=f1.cloneWithMesh(True);
285 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
287 f1.setTime(0.01,3,4);
288 f_2=f1.cloneWithMesh(True);
289 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
290 f1.applyFunc("2*x/3");
291 f1.setTime(0.02,5,6);
292 f_3=f1.cloneWithMesh(True);
293 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
295 its=[(1,2),(3,4),(5,6)];
296 fs=MEDLoader.ReadFieldsOnSameMesh(ON_CELLS,fileName,f_1.getMesh().getName(),0,f_1.getName(),its);
297 self.assertEqual(3,len(fs));
298 self.assertTrue(fs[0].isEqual(f_1,1e-12,1e-12));
299 self.assertTrue(fs[1].isEqual(f_2,1e-12,1e-12));
300 self.assertTrue(fs[2].isEqual(f_3,1e-12,1e-12));
303 def testWriteUMeshesRW1(self):
304 fileName="Pyfile18.med";
305 m3d=MEDLoaderDataForTest.build3DMesh_2();
308 nodes=m3d.findNodesOnPlane(pt,vec,1e-12);
309 m2d=m3d.buildFacePartOfMySelfNode(nodes,True);
311 m2d.renumberCells(renumber,False);
312 m2d.setName("ExampleOfMultiDimW");
314 MEDLoader.WriteUMeshes(fileName,meshes,False);
315 m3d_bis=MEDLoader.ReadUMeshFromFile(fileName,m2d.getName(),0);
316 self.assertTrue(not m3d_bis.isEqual(m3d,1e-12));
317 m3d_bis.setName(m3d.getName());
318 self.assertTrue(m3d_bis.isEqual(m3d,1e-12));
319 m2d_bis=MEDLoader.ReadUMeshFromFile(fileName,m2d.getName(),-1);#-1 for faces
320 self.assertTrue(m2d_bis.isEqual(m2d,1e-12));
321 # Creation of a field on faces.
322 f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME);
323 f1.setName("FieldOnFacesShuffle");
325 array=DataArrayDouble.New();
326 arr1=[71.,171.,10.,110.,20.,120.,30.,130.,40.,140.]
327 array.setValues(arr1,m2d.getNumberOfCells(),2);
328 array.setInfoOnComponent(0,"plkj [mm]");
329 array.setInfoOnComponent(1,"pqqqss [mm]");
331 tmp=array.setValues(arr1,m2d.getNumberOfCells(),2);
332 f1.setTime(3.14,2,7);
334 MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
335 f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),-1,f1.getName(),2,7);
336 self.assertTrue(f2.isEqual(f1,1e-12,1e-12));