Back to index

salome-med  6.5.0
MEDLoaderTest2.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 from MEDLoader import *
00022 import unittest
00023 from math import pi,e,sqrt
00024 from MEDLoaderDataForTest import MEDLoaderDataForTest
00025 
00026 class MEDLoaderTest(unittest.TestCase):
00027     def testMesh1DRW(self):
00028         mesh=MEDLoaderDataForTest.build1DMesh_1();
00029         mesh.checkCoherency();
00030         MEDLoader.WriteUMeshDep("Pyfile1.med",mesh,False);
00031         mesh_rw=MEDLoader.ReadUMeshFromFile("Pyfile1.med",mesh.getName(),0);
00032         self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
00033         pass
00034 
00035     def testMesh2DCurveRW(self):
00036         mesh=MEDLoaderDataForTest.build2DCurveMesh_1();
00037         mesh.checkCoherency();
00038         MEDLoader.WriteUMeshDep("Pyfile2.med",mesh,False);
00039         mesh_rw=MEDLoader.ReadUMeshFromFile("Pyfile2.med",mesh.getName(),0);
00040         self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
00041         pass
00042 
00043     def testMesh2DRW(self):
00044         mesh=MEDLoaderDataForTest.build2DMesh_1();
00045         mesh.checkCoherency();
00046         MEDLoader.WriteUMeshDep("Pyfile3.med",mesh,False);
00047         mesh_rw=MEDLoader.ReadUMeshFromFile("Pyfile3.med",mesh.getName(),0);
00048         self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
00049         pass
00050 
00051     def testMesh3DSurfRW(self):
00052         mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
00053         mesh.checkCoherency();
00054         MEDLoader.WriteUMeshDep("Pyfile4.med",mesh,False);
00055         mesh_rw=MEDLoader.ReadUMeshFromFile("Pyfile4.med",mesh.getName(),0);
00056         self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
00057         pass
00058 
00059     def testMesh3DRW(self):
00060         mesh=MEDLoaderDataForTest.build3DMesh_1();
00061         mesh.checkCoherency();
00062         MEDLoader.WriteUMeshDep("Pyfile5.med",mesh,False);
00063         mesh_rw=MEDLoader.ReadUMeshFromFile("Pyfile5.med",mesh.getName(),0);
00064         self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
00065         pass
00066 
00067     def testFieldRW1(self):
00068         f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
00069         MEDLoader.WriteFieldDep("Pyfile6.med",f1,False);
00070         f2=MEDLoader.ReadFieldCell("Pyfile6.med",f1.getMesh().getName(),0,f1.getName(),0,1);
00071         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
00072         #
00073         f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
00074         MEDLoader.WriteFieldDep("Pyfile7.med",f1,False);
00075         f2=MEDLoader.ReadFieldNode("Pyfile7.med",f1.getMesh().getName(),0,f1.getName(),2,3);
00076         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
00077         pass
00078 
00079     def testFieldRW2(self):
00080         fileName="Pyfile8.med";
00081         VAL1=12345.67890314;
00082         VAL2=-1111111111111.;
00083         f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
00084         MEDLoader.WriteFieldDep(fileName,f1,False);
00085         f1.setTime(10.,8,9);
00086         f1.getArray().setIJ(0,0,VAL1);
00087         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00088         f1.setTime(10.14,18,19);
00089         f1.getArray().setIJ(0,0,VAL2);
00090         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00091         #retrieving time steps...
00092         f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),8,9);
00093         f1.setTime(10.,8,9);
00094         f1.getArray().setIJ(0,0,VAL1);
00095         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
00096         f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),0,1);
00097         f3=MEDLoaderDataForTest.buildVecFieldOnCells_1();
00098         self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
00099         f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),0,f1.getName(),18,19);
00100         f1.setTime(10.14,18,19);
00101         f1.getArray().setIJ(0,0,VAL2);
00102         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
00103         #ON NODES
00104         f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
00105         fileName2="Pyfile9.med";
00106         MEDLoader.WriteFieldDep(fileName2,f1,False);
00107         f1.setTime(110.,108,109);
00108         tmp=f1.getArray().getPointer();
00109         f1.getArray().setIJ(0,3,VAL1);
00110         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2,f1);
00111         f1.setTime(210.,208,209);
00112         f1.getArray().setIJ(0,3,VAL2);
00113         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName2,f1);
00114         f2=MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),108,109);
00115         f1.setTime(110.,108,109);
00116         f1.getArray().setIJ(0,3,VAL1);
00117         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
00118         f2=MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),2,3);
00119         f3=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
00120         self.assertTrue(f3.isEqual(f2,1e-12,1e-12));
00121         f2=MEDLoader.ReadFieldNode(fileName2,f1.getMesh().getName(),0,f1.getName(),208,209);
00122         f1.setTime(210.,208,209);
00123         f1.getArray().setIJ(0,3,VAL2);
00124         self.assertTrue(f1.isEqual(f2,1e-12,1e-12));
00125         pass
00126 
00127     #
00128     # Multi field in a same file, but this field has several
00129     #
00130     def testFieldRW3(self):
00131         fileName="Pyfile11.med";
00132         VAL1=12345.67890314;
00133         VAL2=-1111111111111.;
00134         name1="AField";
00135         name3="AMesh1";
00136         f1=MEDLoaderDataForTest.buildVecFieldOnCells_1();
00137         f1.getMesh().setName(name3);
00138         f1.setName(name1);
00139         f1.setTime(10.,8,9);
00140         tmp=f1.getArray().getPointer();
00141         f1.getArray().setIJ(0,0,VAL1);
00142         MEDLoader.WriteFieldDep(fileName,f1,False);
00143         f1.setTime(10.14,18,19);
00144         f1.getArray().setIJ(0,0,VAL2);
00145         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00146         f1.getMesh().setName(name3);
00147         f1.setTime(10.55,28,29);
00148         f1.getArray().setIJ(0,0,3*VAL1);
00149         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00150         f1.setTime(10.66,38,39);
00151         f1.getArray().setIJ(0,0,3*VAL2);
00152         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00153         f1.setTime(10.77,48,49);
00154         f1.getArray().setIJ(0,0,4*VAL2);
00155         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00156         #ON NODES
00157         f1=MEDLoaderDataForTest.buildVecFieldOnNodes_1();
00158         f1.setName(name1);
00159         f1.getMesh().setName(name3);
00160         f1.setTime(110.,8,9);
00161         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00162         f1.setTime(110.,108,109);
00163         tmp=f1.getArray().getPointer();
00164         f1.getArray().setIJ(0,3,VAL1);
00165         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00166         f1.setTime(210.,208,209);
00167         f1.getArray().setIJ(0,3,VAL2);
00168         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00169         #
00170         it1=MEDLoader.GetCellFieldIterations(fileName,name3,name1);
00171         self.assertEqual(5,len(it1));
00172         self.assertEqual(8,it1[0][0]); self.assertEqual(9,it1[0][1]);
00173         self.assertEqual(18,it1[1][0]); self.assertEqual(19,it1[1][1]);
00174         self.assertEqual(28,it1[2][0]); self.assertEqual(29,it1[2][1]);
00175         self.assertEqual(38,it1[3][0]); self.assertEqual(39,it1[3][1]);
00176         self.assertEqual(48,it1[4][0]); self.assertEqual(49,it1[4][1]);
00177         it3=MEDLoader.GetNodeFieldIterations(fileName,name3,name1);
00178         self.assertEqual(3,len(it3));
00179         self.assertEqual(8,it3[0][0]); self.assertEqual(9,it3[0][1]);
00180         self.assertEqual(108,it3[1][0]); self.assertEqual(109,it3[1][1]);
00181         self.assertEqual(208,it3[2][0]); self.assertEqual(209,it3[2][1]);
00182         #
00183         #
00184         f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,8,9);
00185         self.assertAlmostEqual(VAL1,f1.getArray().getIJ(0,0),13);
00186         f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,18,19);
00187         self.assertAlmostEqual(VAL2,f1.getArray().getIJ(0,0),13);
00188         f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,28,29);
00189         self.assertAlmostEqual(3*VAL1,f1.getArray().getIJ(0,0),13);
00190         f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,38,39);
00191         self.assertAlmostEqual(3*VAL2,f1.getArray().getIJ(0,0),13);
00192         f1=MEDLoader.ReadFieldCell(fileName,name3,0,name1,48,49);
00193         self.assertAlmostEqual(4*VAL2,f1.getArray().getIJ(0,0),13);
00194         #
00195         f1=MEDLoader.ReadFieldNode(fileName,name3,0,name1,8,9);
00196         self.assertAlmostEqual(71.,f1.getArray().getIJ(0,3),13);
00197         f1=MEDLoader.ReadFieldNode(fileName,name3,0,name1,108,109);
00198         self.assertAlmostEqual(VAL1,f1.getArray().getIJ(0,3),13);
00199         f1=MEDLoader.ReadFieldNode(fileName,name3,0,name1,208,209);
00200         self.assertAlmostEqual(VAL2,f1.getArray().getIJ(0,3),13);
00201         pass
00202 
00203     def testMultiMeshRW1(self):
00204         fileName="Pyfile10.med";
00205         mesh1=MEDLoaderDataForTest.build3DMesh_1();
00206         part1=[1,2,4,13,15]
00207         mesh2=mesh1.buildPartOfMySelf(part1,True);
00208         mesh2.setName("mesh2");
00209         part2=[3,4,13,14]
00210         mesh3=mesh1.buildPartOfMySelf(part2,True);
00211         mesh3.setName("mesh3");
00212         mesh4=MEDCouplingUMesh.New();
00213         mesh4.setName("mesh4");
00214         mesh4.setMeshDimension(3);
00215         mesh4.allocateCells(1);
00216         conn=[0,11,1,3]
00217         mesh4.insertNextCell(NORM_TETRA4,4,conn[0:4])
00218         mesh4.finishInsertingCells();
00219         mesh4.setCoords(mesh1.getCoords());
00220         meshes=[mesh1,mesh2,mesh3,mesh4]
00221         mnane="3DToto";
00222         MEDLoader.WriteUMeshesPartitionDep(fileName,mnane,meshes,False);
00223         #
00224         mesh5=MEDLoader.ReadUMeshFromFile(fileName,mnane);
00225         mesh1.setName(mnane);
00226         part3=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
00227         mesh6=mesh5.buildPartOfMySelf(part3,True);
00228         mesh6.setName(mnane);
00229         self.assertTrue(mesh6.isEqual(mesh1,1e-12));
00230         grps=MEDLoader.GetMeshGroupsNames(fileName,mnane);
00231         self.assertEqual(4,len(grps));
00232         grps.index("mesh2");
00233         grps.index("mesh3");
00234         grps.index("mesh4");
00235         grps.index("3DMesh_1");
00236         #
00237         vec=["mesh2"];
00238         mesh2_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
00239         self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
00240         vec=["mesh3"];
00241         mesh3_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
00242         self.assertTrue(mesh3_2.isEqual(mesh3,1e-12));
00243         vec=["mesh4"];
00244         mesh4_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
00245         self.assertTrue(mesh4_2.isEqual(mesh4,1e-12));
00246         vec=["3DMesh_1"];
00247         mesh1_2=MEDLoader.ReadUMeshFromGroups(fileName,mnane,0,vec);
00248         mesh1.setName("3DMesh_1");
00249         self.assertTrue(mesh1_2.isEqual(mesh1,1e-12));
00250         #
00251         vec=["Family_4","Family_2"];
00252         mesh2_2=MEDLoader.ReadUMeshFromFamilies(fileName,mnane,0,vec);
00253         mesh2_2.setName("mesh2");
00254         self.assertTrue(mesh2_2.isEqual(mesh2,1e-12));
00255         pass
00256 
00257     def testMesh3DSurfShuffleRW(self):
00258         fileName="Pyfile15.med";
00259         mesh=MEDLoaderDataForTest.build3DSurfMesh_1();
00260         renumber1=[2,5,1,0,3,4]
00261         mesh.renumberCells(renumber1,False);
00262         mesh.checkCoherency();
00263         MEDLoader.WriteUMeshDep(fileName,mesh,False);
00264         mesh_rw=MEDLoader.ReadUMeshFromFile(fileName,mesh.getName(),0);
00265         self.assertTrue(mesh.isEqual(mesh_rw,1e-12));
00266         pass
00267 
00268     def testMultiFieldShuffleRW1(self):
00269         fileName="Pyfile17.med";
00270         m=MEDLoaderDataForTest.build3DMesh_2();
00271         self.assertEqual(20,m.getNumberOfCells());
00272         self.assertEqual(45,m.getNumberOfNodes());
00273         polys=[1,4,6]
00274         m.convertToPolyTypes(polys);
00275         renum=[1,3,2,8,9,12,13,16,19,0,4,7,5,15,14,17,10,18,6,11]
00276         m.renumberCells(renum,False);
00277         m.orientCorrectlyPolyhedrons();
00278         # Writing
00279         MEDLoader.WriteUMeshDep(fileName,m,False);
00280         f1Tmp=m.getMeasureField(False);
00281         f1=f1Tmp.buildNewTimeReprFromThis(ONE_TIME,False);
00282         f1.setTime(0.,1,2);
00283         f_1=f1.cloneWithMesh(True);
00284         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00285         f1.applyFunc("2*x");
00286         f1.setTime(0.01,3,4);
00287         f_2=f1.cloneWithMesh(True);
00288         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00289         f1.applyFunc("2*x/3");
00290         f1.setTime(0.02,5,6);
00291         f_3=f1.cloneWithMesh(True);
00292         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00293         # Reading
00294         its=[(1,2),(3,4),(5,6)];
00295         fs=MEDLoader.ReadFieldsOnSameMesh(ON_CELLS,fileName,f_1.getMesh().getName(),0,f_1.getName(),its);
00296         self.assertEqual(3,len(fs));
00297         self.assertTrue(fs[0].isEqual(f_1,1e-12,1e-12));
00298         self.assertTrue(fs[1].isEqual(f_2,1e-12,1e-12));
00299         self.assertTrue(fs[2].isEqual(f_3,1e-12,1e-12));
00300         pass
00301 
00302     def testWriteUMeshesRW1(self):
00303         fileName="Pyfile18.med";
00304         m3d=MEDLoaderDataForTest.build3DMesh_2();
00305         pt=[0.,0.,-0.3]
00306         vec=[0.,0.,1.]
00307         nodes=m3d.findNodesOnPlane(pt,vec,1e-12);
00308         m2d=m3d.buildFacePartOfMySelfNode(nodes,True);
00309         renumber=[1,2,0,4,3]
00310         m2d.renumberCells(renumber,False);
00311         m2d.setName("ExampleOfMultiDimW");
00312         meshes=[m2d,m3d]
00313         MEDLoader.WriteUMeshes(fileName,meshes,False);
00314         m3d_bis=MEDLoader.ReadUMeshFromFile(fileName,m2d.getName(),0);
00315         self.assertTrue(not m3d_bis.isEqual(m3d,1e-12));
00316         m3d_bis.setName(m3d.getName());
00317         self.assertTrue(m3d_bis.isEqual(m3d,1e-12));
00318         m2d_bis=MEDLoader.ReadUMeshFromFile(fileName,m2d.getName(),-1);#-1 for faces
00319         self.assertTrue(m2d_bis.isEqual(m2d,1e-12));
00320         # Creation of a field on faces.
00321         f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME);
00322         f1.setName("FieldOnFacesShuffle");
00323         f1.setMesh(m2d);
00324         array=DataArrayDouble.New();
00325         arr1=[71.,171.,10.,110.,20.,120.,30.,130.,40.,140.]
00326         array.setValues(arr1,m2d.getNumberOfCells(),2);
00327         array.setInfoOnComponent(0,"plkj [mm]");
00328         array.setInfoOnComponent(1,"pqqqss [mm]");
00329         f1.setArray(array);
00330         tmp=array.setValues(arr1,m2d.getNumberOfCells(),2);
00331         f1.setTime(3.14,2,7);
00332         f1.checkCoherency();
00333         MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fileName,f1);
00334         f2=MEDLoader.ReadFieldCell(fileName,f1.getMesh().getName(),-1,f1.getName(),2,7);
00335         self.assertTrue(f2.isEqual(f1,1e-12,1e-12));
00336         pass
00337     pass
00338 
00339 unittest.main()