Back to index

salome-med  6.5.0
medNumPy_test.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 # Check that numpy arrays can be used to define coordinates, connectivities and field values
00025 
00026 import unittest
00027 from libMEDMEM_Swig import *
00028 
00029 class medNumPyTest(unittest.TestCase):
00030     def test0(self):
00031 
00032         try:
00033             from numpy import array, arange, ndarray
00034         except:
00035             # numpy is not available, it is not an error
00036             print "numpy unavailable"
00037             return
00038 
00039         myMeshing = MESHING()
00040         myMeshing.setName("meshing")
00041 
00042         spaceDimension = 3
00043         numberOfNodes = 19
00044         coordinates = [0.0, 0.0, 0.0  ,
00045                        0.0, 0.0, 1.0  ,
00046                        2.0, 0.0, 1.0  ,
00047                        0.0, 2.0, 1.0  ,
00048                        -2.0, 0.0, 1.0 ,
00049                        0.0, -2.0, 1.0 ,
00050                        1.0, 1.0, 2.0  ,
00051                        -1.0, 1.0, 2.0 ,
00052                        -1.0, -1.0, 2.0,
00053                        1.0, -1.0, 2.0 ,
00054                        1.0, 1.0, 3.0  ,
00055                        -1.0, 1.0, 3.0 ,
00056                        -1.0, -1.0, 3.0,
00057                        1.0, -1.0, 3.0 ,
00058                        1.0, 1.0, 4.0  ,
00059                        -1.0, 1.0, 4.0 ,
00060                        -1.0, -1.0, 4.0,
00061                        1.0, -1.0, 4.0 ,
00062                        0.0, 0.0, 5.0  ]
00063 
00064         myMeshing.setCoordinates(spaceDimension,numberOfNodes, array( coordinates ),
00065                                  "CARTESIAN",MED_FULL_INTERLACE)
00066         coord = myMeshing.getCoordinates(MED_FULL_INTERLACE)
00067         self.assertTrue( isinstance( coord, ndarray ))
00068         
00069         units = ["cm      ", "cm      ", "cm      "]
00070         names = ["X       ", "Y       ", "Z       "]
00071         myMeshing.setCoordinatesNames(names)
00072         myMeshing.setCoordinatesUnits(units)
00073 
00074         # definition of connectivities
00075 
00076         numberOfTypes = 1
00077         entity = MED_CELL
00078 
00079         types = [MED_TETRA4]
00080         numberOfElements = [12]
00081 
00082         myMeshing.setNumberOfTypes(numberOfTypes,entity)
00083         myMeshing.setTypes(types,entity)
00084         myMeshing.setNumberOfElements(numberOfElements,entity)
00085 
00086         connectivityTetra =  [1,2,3,6,
00087                               1,2,4,3 ,
00088                               1,2,5,4 ,
00089                               1,2,6,5 ,
00090                               2,7,4,3 ,
00091                               2,8,5,4 ,
00092                               2,9,6,5 ,
00093                               2,10,3,6,
00094                               2,7,3,10,
00095                               2,8,4,7 ,
00096                               2,9,5,8 ,
00097                               2,10,6,9]
00098 
00099         myMeshing.setConnectivity(entity,types[0], array( connectivityTetra ))
00100         conn = myMeshing.getConnectivity(MED_NODAL,entity,MED_ALL_ELEMENTS)
00101         self.assertTrue( isinstance( conn, ndarray ))
00102 
00103         myGroup = GROUP()
00104         myGroup.setName("SomeCells")
00105         myGroup.setMesh(myMeshing)
00106         myGroup.setEntity(MED_CELL)
00107         myGroup.setNumberOfGeometricType(1)
00108 
00109         myGroup.setGeometricType(types)
00110         myNumberOfElements = [4,1,2]
00111         myGroup.setNumberOfElements(myNumberOfElements)
00112 
00113         index = [1,5,6,8]
00114         values = [2,7,8,12,13,15,16]
00115         myGroup.setNumber( array( index ), array( values ))
00116 
00117         myMeshing.addGroup(myGroup)
00118 
00119         # saving the generated mesh in MED
00120 
00121         import os
00122         tmpDir  = os.getenv("TMP")
00123         if not tmpDir:
00124             tmpDir = os.getenv("TMPDIR", "/tmp")
00125 
00126         filename = os.path.join(tmpDir,"medNumPy_test.med")
00127         myMeshing.write(MED_DRIVER,filename)
00128 
00129         # we build now 2 fields
00130 
00131         supportOnNodes = SUPPORT(myMeshing,"On_All_Nodes",MED_NODE)
00132         numberOfNodes = supportOnNodes.getNumberOfElements(MED_ALL_ELEMENTS)
00133 
00134         fieldDoubleScalarOnNodes = FIELDDOUBLE(supportOnNodes,1)
00135         fieldDoubleScalarOnNodes.setName("fieldScalarDoubleNode")
00136         fieldDoubleScalarOnNodes.setIterationNumber(-1)
00137         fieldDoubleScalarOnNodes.setOrderNumber(-1)
00138         fieldDoubleScalarOnNodes.setTime(0.0)
00139 
00140         fieldDoubleScalarOnNodes.setComponentName(1,"Vx")
00141         fieldDoubleScalarOnNodes.setComponentDescription(1,"comp1")
00142         fieldDoubleScalarOnNodes.setMEDComponentUnit(1,"unit1")
00143 
00144         nodeValues = arange( numberOfNodes, dtype=float )
00145         fieldDoubleScalarOnNodes.setValue( nodeValues )
00146 
00147         resValue = fieldDoubleScalarOnNodes.getValue()
00148         self.assertTrue( isinstance( resValue, ndarray ))
00149 
00150         intArray = arange( numberOfNodes, dtype=int )
00151         self.assertRaises( TypeError, fieldDoubleScalarOnNodes.setValue, intArray )
00152 
00153         fieldDoubleScalarOnNodes.write(MED_DRIVER,filename)
00154 
00155 
00156         supportOnCells = SUPPORT(myMeshing,"On_All_Cells",MED_CELL)
00157         numberOfCells = supportOnCells.getNumberOfElements(MED_ALL_ELEMENTS)
00158 
00159         fieldIntScalarOnCells = FIELDINT(supportOnCells,1)
00160         fieldIntScalarOnCells.setName("fieldScalarIntCell")
00161         fieldIntScalarOnCells.setIterationNumber(-1)
00162         fieldIntScalarOnCells.setOrderNumber(-1)
00163         fieldIntScalarOnCells.setTime(0.0)
00164 
00165         fieldIntScalarOnCells.setComponentName(1,"Vx")
00166         fieldIntScalarOnCells.setComponentDescription(1,"comp1")
00167         fieldIntScalarOnCells.setMEDComponentUnit(1,"unit1")
00168 
00169         cellValues = arange(numberOfCells)
00170         fieldIntScalarOnCells.setValue( cellValues )
00171 
00172         resValue = fieldIntScalarOnCells.getValue()
00173         self.assertTrue( isinstance( resValue, ndarray ))
00174         self.assertTrue( len(resValue) == numberOfCells)
00175 
00176         dblArray = arange( numberOfNodes, dtype=float )
00177         self.assertRaises( TypeError, fieldIntScalarOnCells.setValue, dblArray )
00178 
00179         fieldIntScalarOnCells.write(MED_DRIVER,filename)
00180 
00181         os.remove( filename )
00182 
00183     def setUp(self):
00184         pass
00185 
00186 unittest.main()