Back to index

salome-med  6.5.0
SauvLoaderTest.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, os
00023 from MEDLoaderDataForTest import MEDLoaderDataForTest
00024 
00025 class SauvLoaderTest(unittest.TestCase):
00026 
00027     def testSauv2Med(self):
00028         # get a file containing all types of readable piles
00029         self.assertTrue( os.getenv("MED_ROOT_DIR") )
00030         sauvFile = os.path.join( os.getenv("MED_ROOT_DIR"), "share","salome",
00031                                  "resources","med","allPillesTest.sauv")
00032         self.assertTrue( os.access( sauvFile, os.F_OK))
00033 
00034         # read SAUV and write MED
00035         medFile = "SauvLoaderTest.med"
00036         sr=SauvReader.New(sauvFile);
00037         d2=sr.loadInMEDFileDS();
00038         d2.write(medFile,0);
00039 
00040         # check 
00041         self.assertEqual(1,d2.getNumberOfMeshes())
00042         self.assertEqual(8+97,d2.getNumberOfFields())
00043         mm = d2.getMeshes()
00044         m = mm.getMeshAtPos(0)
00045         self.assertEqual(17,len(m.getGroupsNames()))
00046 
00047         os.remove( medFile )
00048         pass
00049 
00050     def testMed2Sauv(self):
00051         # read pointe.med
00052         self.assertTrue( os.getenv("MED_ROOT_DIR") )
00053         medFile = os.path.join( os.getenv("MED_ROOT_DIR"), "share","salome",
00054                                 "resources","med","pointe.med")
00055         self.assertTrue( os.access( medFile, os.F_OK))
00056         pointeMed = MEDFileData.New( medFile )
00057 
00058         # add 3 faces to pointeMed
00059         pointeMedMesh = pointeMed.getMeshes().getMeshAtPos(0)
00060         pointeM1D = MEDCouplingUMesh.New()
00061         pointeM1D.setCoords( pointeMedMesh.getCoords() )
00062         pointeM1D.setMeshDimension( 2 )
00063         pointeM1D.allocateCells( 3 )
00064         pointeM1D.insertNextCell( NORM_TRI3, 3, [0,1,2])
00065         pointeM1D.insertNextCell( NORM_TRI3, 3, [0,1,3])
00066         pointeM1D.insertNextCell( NORM_QUAD4, 4, [10,11,12,13])
00067         pointeM1D.finishInsertingCells()
00068         pointeMedMesh.setMeshAtLevel( -1, pointeM1D )
00069         pointeMed.getMeshes().setMeshAtPos( 0, pointeMedMesh )
00070 
00071         # add a field on 2 faces to pointeMed
00072         ff1=MEDFileFieldMultiTS.New()
00073         f1=MEDCouplingFieldDouble.New(ON_GAUSS_NE,ONE_TIME)
00074         #f1.setMesh( pointeM1D )
00075         f1.setName("Field on 2 faces")
00076         d=DataArrayDouble.New()
00077         d.alloc(3+4,2)
00078         d.setInfoOnComponent(0,"sigX [MPa]")
00079         d.setInfoOnComponent(1,"sigY [GPa]")
00080         d.setValues([311,312,321,322,331,332,411,412,421,422,431,432,441,442],3+4,2)
00081         f1.setArray(d)
00082         da=DataArrayInt.New()
00083         da.setValues([0,2],2,1)
00084         da.setName("sup2")
00085         ff1.appendFieldProfile(f1,pointeMedMesh,-1,da)
00086         pointeMed.getFields().pushField( ff1 )
00087 
00088         #remove fieldnodeint
00089         pointeFields = pointeMed.getFields()
00090         for i in range( pointeFields.getNumberOfFields() ):
00091             if pointeFields.getFieldAtPos(i).getName() == "fieldnodeint":
00092                 pointeFields.destroyFieldAtPos( i )
00093                 break
00094 
00095         # write pointeMed to SAUV
00096         sauvFile = "SauvLoaderTest.sauv"
00097         sw=SauvWriter.New();
00098         sw.setMEDFileDS(pointeMed);
00099         sw.write(sauvFile);
00100 
00101         # read SAUV and check
00102         sr=SauvReader.New(sauvFile);
00103         d2=sr.loadInMEDFileDS();
00104         self.assertEqual(1,d2.getNumberOfMeshes())
00105         self.assertEqual(4,d2.getNumberOfFields())
00106         m = d2.getMeshes().getMeshAtPos(0)
00107         self.assertEqual("maa1",m.getName())
00108         self.assertEqual(5,len(m.getGroupsNames()))
00109         self.assertEqual(3,m.getMeshDimension())
00110         groups = m.getGroupsNames()
00111         self.assertTrue( "groupe1" in groups )
00112         self.assertTrue( "groupe2" in groups )
00113         self.assertTrue( "groupe3" in groups )
00114         self.assertTrue( "groupe4" in groups )
00115         self.assertTrue( "groupe5" in groups )
00116         self.assertEqual(16,m.getSizeAtLevel(0))
00117         um0 = m.getGenMeshAtLevel(0)
00118         self.assertEqual(12, um0.getNumberOfCellsWithType( NORM_TETRA4 ))
00119         self.assertEqual(2, um0.getNumberOfCellsWithType( NORM_PYRA5 ))
00120         self.assertEqual(2, um0.getNumberOfCellsWithType( NORM_HEXA8 ))
00121         um1 = m.getGenMeshAtLevel(-1)
00122         self.assertEqual(2, um1.getNumberOfCellsWithType( NORM_TRI3 ))
00123         pointeUM0 = pointeMedMesh.getGenMeshAtLevel(0)
00124         self.assertTrue(m.getCoords().isEqualWithoutConsideringStr(pointeMedMesh.getCoords(),1e-12))
00125         self.assertEqual( um0.getMeasureField(0).accumulate(0),
00126                           pointeUM0.getMeasureField(0).accumulate(0),1e-12)
00127         # check fields
00128         # fieldnodedouble
00129         fieldnodedoubleTS1 = pointeMed.getFields().getFieldWithName("fieldnodedouble")
00130         fieldnodedoubleTS2 = d2.getFields().getFieldWithName("fieldnodedouble")
00131         self.assertEqual( fieldnodedoubleTS1.getInfo(), fieldnodedoubleTS2.getInfo())
00132         self.assertEqual( fieldnodedoubleTS1.getNumberOfTS(), fieldnodedoubleTS2.getNumberOfTS())
00133         io1 = fieldnodedoubleTS1.getIterations()
00134         io2 = fieldnodedoubleTS2.getIterations()
00135         for i in range(fieldnodedoubleTS1.getNumberOfTS() ):
00136             fnd1 = fieldnodedoubleTS1.getFieldOnMeshAtLevel(ON_NODES, io1[i][0],io1[i][1],pointeUM0)
00137             fnd2 = fieldnodedoubleTS2.getFieldOnMeshAtLevel(ON_NODES, io2[i][0],io2[i][1],um0)
00138             self.assertTrue( fnd1.getArray().isEqual( fnd2.getArray(), 1e-12 ))
00139         # fieldcelldoublevector
00140         fieldnodedoubleTS1 = pointeMed.getFields().getFieldWithName("fieldcelldoublevector")
00141         fieldnodedoubleTS2 = d2.getFields().getFieldWithName("fieldcelldoublevector")
00142         self.assertEqual( fieldnodedoubleTS1.getInfo(), fieldnodedoubleTS2.getInfo())
00143         self.assertEqual( fieldnodedoubleTS1.getNumberOfTS(), fieldnodedoubleTS2.getNumberOfTS())
00144         io1 = fieldnodedoubleTS1.getIterations()
00145         io2 = fieldnodedoubleTS2.getIterations()
00146         for i in range(fieldnodedoubleTS1.getNumberOfTS() ):
00147             fnd1 = fieldnodedoubleTS1.getFieldOnMeshAtLevel(ON_CELLS, io1[i][0],io1[i][1],pointeUM0)
00148             fnd2 = fieldnodedoubleTS2.getFieldOnMeshAtLevel(ON_CELLS, io2[i][0],io2[i][1],um0)
00149             self.assertAlmostEqual( fnd1.accumulate(0), fnd2.accumulate(0) )
00150             self.assertAlmostEqual( fnd1.accumulate(1), fnd2.accumulate(1) )
00151             self.assertAlmostEqual( fnd1.accumulate(2), fnd2.accumulate(2) )
00152         # Field on 2 faces
00153         fieldOnFaces = d2.getFields().getFieldWithName(f1.getName())
00154         io1 = fieldOnFaces.getIterations()
00155         fof = fieldOnFaces.getFieldOnMeshAtLevel(f1.getTypeOfField(),io1[i][0],io1[i][1],um1)
00156         self.assertTrue( d.isEqual( fof.getArray(), 1e-12 ))
00157 
00158         os.remove( sauvFile )
00159         pass
00160     pass
00161 
00162 unittest.main()