]> SALOME platform Git repositories - modules/med.git/blob - src/MEDLoader/Swig/MEDLoaderTest.py
Salome HOME
On the road of last imps for MEDReader
[modules/med.git] / src / MEDLoader / Swig / MEDLoaderTest.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Anthony Geay (CEA/DEN)
21
22 import MEDLoader
23 import unittest
24 from math import pi,e,sqrt
25 from MEDLoaderDataForTest import MEDLoaderDataForTest
26
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));
34         pass
35
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));
42         pass
43
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));
50         pass
51
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));
58         pass
59
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));
66         pass
67
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));
73         #
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);
79         pass
80
81     def testFieldRW2(self):
82         fileName="Pyfile8.med";
83         VAL1=12345.67890314;
84         VAL2=-1111111111111.;
85         f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
86         MEDLoader.MEDLoader.WriteField(fileName,f1,True);
87         f1.setTime(10.,8,9);
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);
95         f1.setTime(10.,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);
107         #ON NODES
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));
129         pass
130
131     #
132     # Multi field in a same file, but this field has several
133     #
134     def testFieldRW3(self):
135         fileName="Pyfile11.med";
136         VAL1=12345.67890314;
137         VAL2=-1111111111111.;
138         name1="AField";
139         name3="AMesh1";
140         f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
141         f1.getMesh().setName(name3);
142         f1.setName(name1);
143         f1.setTime(10.,8,9);
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);
161         #ON NODES
162         f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
163         f1.setName(name1);
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);
174         #
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]);
187         #
188         #
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);
199         #
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);
206         pass
207
208     def testMultiMeshRW1(self):
209         fileName="Pyfile10.med";
210         mesh1=MEDLoaderDataForTest.build3DMesh_1();
211         part1=[1,2,4,13,15]
212         mesh2=mesh1.buildPartOfMySelf(part1,True);
213         mesh2.setName("mesh2");
214         part2=[3,4,13,14]
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);
221         conn=[0,11,1,3]
222         mesh4.insertNextCell(MEDLoader.NORM_TETRA4,4,conn[0:4])
223         mesh4.finishInsertingCells();
224         mesh4.setCoords(mesh1.getCoords());
225         meshes=[mesh1,mesh2,mesh3,mesh4]
226         mnane="3DToto";
227         MEDLoader.MEDLoader.WriteUMeshesPartition(fileName,mnane,meshes,True);
228         #
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));
237         grps.index("mesh2");
238         grps.index("mesh3");
239         grps.index("mesh4");
240         grps.index("3DMesh_1");
241         #
242         vec=("mesh2",);
243         mesh2_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
244         self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
245         vec=["mesh3"];
246         mesh3_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
247         self.assertTrue(mesh3_2.isEqual(mesh3,1e-12));
248         vec=["mesh4"];
249         mesh4_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
250         self.assertTrue(mesh4_2.isEqual(mesh4,1e-12));
251         vec="3DMesh_1";
252         mesh1_2=MEDLoader.MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
253         mesh1.setName("3DMesh_1");
254         self.assertTrue(mesh1_2.isEqual(mesh1,1e-12));
255         #
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));
260         #
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");
267         #
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");
272         pass
273
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);
279         part1=[1,2,4,13,15]
280         mesh2=mesh1.buildPartOfMySelf(part1,True);
281         mesh2.setName(mesh1.getName());#<- important for the test
282         #
283         nbOfCells=mesh2.getNumberOfCells();
284         self.assertEqual(5,nbOfCells);
285         f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS,MEDLoader.ONE_TIME);
286         f1.setName("VectorFieldOnCells");
287         f1.setMesh(mesh2);
288         array=MEDLoader.DataArrayDouble.New();
289         array.alloc(nbOfCells,2);
290         f1.setArray(array);
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);
294         f1.checkCoherency();
295         #
296         MEDLoader.MEDLoader.WriteField(fileName,f1,False);#<- False important for the test
297         #
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]);
301         f2.checkCoherency();
302         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
303         #
304         pass
305
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));
312         pass
313
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));
320         pass
321
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));
331         pass
332
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());
338         polys=[1,4,6]
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();
343         # Writing
344         MEDLoader.MEDLoader.WriteUMesh(fileName,m,True);
345         f1Tmp=m.getMeasureField(False);
346         f1=f1Tmp.buildNewTimeReprFromThis(MEDLoader.ONE_TIME,False);
347         f1.setTime(0.,1,2);
348         f_1=f1.cloneWithMesh(True);
349         MEDLoader.MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
350         f1.applyFunc("2*x");
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);
358         # Reading
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));
365         pass
366
367     def testWriteUMeshesRW1(self):
368         fileName="Pyfile18.med";
369         m3d=MEDLoaderDataForTest.build3DMesh_2();
370         pt=[0.,0.,-0.3]
371         vec=[0.,0.,1.]
372         nodes=m3d.findNodesOnPlane(pt,vec,1e-12);
373         m2d=m3d.buildFacePartOfMySelfNode(nodes,True);
374         renumber=[1,2,0,4,3]
375         m2d.renumberCells(renumber,False);
376         m2d.setName("ExampleOfMultiDimW");
377         meshes=[m2d,m3d]
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");
388         f1.setMesh(m2d);
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]");
394         f1.setArray(array);
395         tmp=array.setValues(arr1,m2d.getNumberOfCells(),2);
396         f1.setTime(3.14,2,7);
397         f1.checkCoherency();
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));
401         pass
402
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");
411         f1.setMesh(m);
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);
415         f1.setArray(array);
416         array.setInfoOnComponent(0,"tyty [mm]");
417         array.setInfoOnComponent(1,"uiop [MW]");
418         f1.setTime(3.14,2,7);
419         f1.checkCoherency();
420         arr2=[1,4]
421         f2=f1.buildSubPart(arr2);
422         f2.getMesh().setName(f1.getMesh().getName());
423         MEDLoader.MEDLoader.WriteField(fileName,f2,False);#<- False important for the test
424         #
425         f3=MEDLoader.MEDLoader.ReadFieldNode(fileName,f2.getMesh().getName(),0,f2.getName(),2,7);
426         f3.checkCoherency();
427         self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
428         #
429         arr3=[1,3,0,5,2,4]
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);
434         f3.checkCoherency();
435         self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
436         #
437         pass
438
439     def testFieldNodeProfilRW2(self):
440         fileName="Pyfile23.med";
441         mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
442         MEDLoader.MEDLoader.WriteUMesh(fileName,mesh,True);
443         #
444         f1=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES,MEDLoader.ONE_TIME);
445         f1.setName("FieldMix");
446         f1.setMesh(mesh);
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);
451         f1.setArray(array);
452         array.setInfoOnComponent(0,"plkj [mm]");
453         array.setInfoOnComponent(1,"pqqqss [mm]");
454         tmp=array.getPointer();
455         f1.setTime(3.17,2,7);
456         #
457         renumArr=[3,7,2,1,5,11,10,0,9,6,8,4]
458         f1.renumberNodes(renumArr);
459         f1.checkCoherency();
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));
463         #
464         pass
465
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");
471         f1.setMesh(mesh);
472         array=MEDLoader.DataArrayDouble.New();
473         f1.setArray(array);
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);
479         f1.checkCoherency();
480         #
481         f2=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_NODES,MEDLoader.ONE_TIME);
482         f2.setName("FieldMix");
483         f2.setMesh(mesh);
484         array=MEDLoader.DataArrayDouble.New();
485         f2.setArray(array);
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);
492         f2.checkCoherency();
493         #
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");
505         #
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]);
510         #
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));
515         #
516         pass
517
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);
524         f1.setMesh(mesh);
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);
538         f1.setMesh(mesh);
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");
547         pass
548
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)
554         nbOfCompo=4100
555         arr=MEDLoader.DataArrayDouble(nbOfCompo*3) ; arr.iota()
556         arr.rearrange(nbOfCompo)
557         arr.setInfoOnComponents(["c%i"%(i) for i in xrange(nbOfCompo)])
558         f.setArray(arr)
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))
563         pass
564     pass
565
566 unittest.main()