1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # Check that numpy arrays can be used to define coordinates, connectivities and field values
27 from libMEDMEM_Swig import *
29 class medNumPyTest(unittest.TestCase):
33 from numpy import array, arange, ndarray
35 # numpy is not available, it is not an error
36 print "numpy unavailable"
40 myMeshing.setName("meshing")
44 coordinates = [0.0, 0.0, 0.0 ,
64 myMeshing.setCoordinates(spaceDimension,numberOfNodes, array( coordinates ),
65 "CARTESIAN",MED_FULL_INTERLACE)
66 coord = myMeshing.getCoordinates(MED_FULL_INTERLACE)
67 self.assertTrue( isinstance( coord, ndarray ))
69 units = ["cm ", "cm ", "cm "]
70 names = ["X ", "Y ", "Z "]
71 myMeshing.setCoordinatesNames(names)
72 myMeshing.setCoordinatesUnits(units)
74 # definition of connectivities
80 numberOfElements = [12]
82 myMeshing.setNumberOfTypes(numberOfTypes,entity)
83 myMeshing.setTypes(types,entity)
84 myMeshing.setNumberOfElements(numberOfElements,entity)
86 connectivityTetra = [1,2,3,6,
99 myMeshing.setConnectivity(entity,types[0], array( connectivityTetra ))
100 conn = myMeshing.getConnectivity(MED_NODAL,entity,MED_ALL_ELEMENTS)
101 self.assertTrue( isinstance( conn, ndarray ))
104 myGroup.setName("SomeCells")
105 myGroup.setMesh(myMeshing)
106 myGroup.setEntity(MED_CELL)
107 myGroup.setNumberOfGeometricType(1)
109 myGroup.setGeometricType(types)
110 myNumberOfElements = [4,1,2]
111 myGroup.setNumberOfElements(myNumberOfElements)
114 values = [2,7,8,12,13,15,16]
115 myGroup.setNumber( array( index ), array( values ))
117 myMeshing.addGroup(myGroup)
119 # saving the generated mesh in MED
122 tmpDir = os.getenv("TMP")
124 tmpDir = os.getenv("TMPDIR", "/tmp")
126 filename = os.path.join(tmpDir,"medNumPy_test.med")
127 myMeshing.write(MED_DRIVER,filename)
129 # we build now 2 fields
131 supportOnNodes = SUPPORT(myMeshing,"On_All_Nodes",MED_NODE)
132 numberOfNodes = supportOnNodes.getNumberOfElements(MED_ALL_ELEMENTS)
134 fieldDoubleScalarOnNodes = FIELDDOUBLE(supportOnNodes,1)
135 fieldDoubleScalarOnNodes.setName("fieldScalarDoubleNode")
136 fieldDoubleScalarOnNodes.setIterationNumber(-1)
137 fieldDoubleScalarOnNodes.setOrderNumber(-1)
138 fieldDoubleScalarOnNodes.setTime(0.0)
140 fieldDoubleScalarOnNodes.setComponentName(1,"Vx")
141 fieldDoubleScalarOnNodes.setComponentDescription(1,"comp1")
142 fieldDoubleScalarOnNodes.setMEDComponentUnit(1,"unit1")
144 nodeValues = arange( numberOfNodes, dtype=float )
145 fieldDoubleScalarOnNodes.setValue( nodeValues )
147 resValue = fieldDoubleScalarOnNodes.getValue()
148 self.assertTrue( isinstance( resValue, ndarray ))
150 intArray = arange( numberOfNodes, dtype=int )
151 self.assertRaises( TypeError, fieldDoubleScalarOnNodes.setValue, intArray )
153 fieldDoubleScalarOnNodes.write(MED_DRIVER,filename)
156 supportOnCells = SUPPORT(myMeshing,"On_All_Cells",MED_CELL)
157 numberOfCells = supportOnCells.getNumberOfElements(MED_ALL_ELEMENTS)
159 fieldIntScalarOnCells = FIELDINT(supportOnCells,1)
160 fieldIntScalarOnCells.setName("fieldScalarIntCell")
161 fieldIntScalarOnCells.setIterationNumber(-1)
162 fieldIntScalarOnCells.setOrderNumber(-1)
163 fieldIntScalarOnCells.setTime(0.0)
165 fieldIntScalarOnCells.setComponentName(1,"Vx")
166 fieldIntScalarOnCells.setComponentDescription(1,"comp1")
167 fieldIntScalarOnCells.setMEDComponentUnit(1,"unit1")
169 cellValues = arange(numberOfCells)
170 fieldIntScalarOnCells.setValue( cellValues )
172 resValue = fieldIntScalarOnCells.getValue()
173 self.assertTrue( isinstance( resValue, ndarray ))
174 self.assertTrue( len(resValue) == numberOfCells)
176 dblArray = arange( numberOfNodes, dtype=float )
177 self.assertRaises( TypeError, fieldIntScalarOnCells.setValue, dblArray )
179 fieldIntScalarOnCells.write(MED_DRIVER,filename)
181 os.remove( filename )