Salome HOME
c4364fc38bb28b9a3e21ab8ef73d8483a5d8abd3
[modules/med.git] / src / MEDMEM_SWIG / medNumPy_test.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 # Check that numpy arrays can be used to define coordinates, connectivities and field values
25
26 import unittest
27 from libMEDMEM_Swig import *
28
29 class medNumPyTest(unittest.TestCase):
30     def test0(self):
31
32         try:
33             from numpy import array, arange, ndarray
34         except:
35             # numpy is not available, it is not an error
36             print "numpy unavailable"
37             return
38
39         myMeshing = MESHING()
40         myMeshing.setName("meshing")
41
42         spaceDimension = 3
43         numberOfNodes = 19
44         coordinates = [0.0, 0.0, 0.0  ,
45                        0.0, 0.0, 1.0  ,
46                        2.0, 0.0, 1.0  ,
47                        0.0, 2.0, 1.0  ,
48                        -2.0, 0.0, 1.0 ,
49                        0.0, -2.0, 1.0 ,
50                        1.0, 1.0, 2.0  ,
51                        -1.0, 1.0, 2.0 ,
52                        -1.0, -1.0, 2.0,
53                        1.0, -1.0, 2.0 ,
54                        1.0, 1.0, 3.0  ,
55                        -1.0, 1.0, 3.0 ,
56                        -1.0, -1.0, 3.0,
57                        1.0, -1.0, 3.0 ,
58                        1.0, 1.0, 4.0  ,
59                        -1.0, 1.0, 4.0 ,
60                        -1.0, -1.0, 4.0,
61                        1.0, -1.0, 4.0 ,
62                        0.0, 0.0, 5.0  ]
63
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 ))
68         
69         units = ["cm      ", "cm      ", "cm      "]
70         names = ["X       ", "Y       ", "Z       "]
71         myMeshing.setCoordinatesNames(names)
72         myMeshing.setCoordinatesUnits(units)
73
74         # definition of connectivities
75
76         numberOfTypes = 1
77         entity = MED_CELL
78
79         types = [MED_TETRA4]
80         numberOfElements = [12]
81
82         myMeshing.setNumberOfTypes(numberOfTypes,entity)
83         myMeshing.setTypes(types,entity)
84         myMeshing.setNumberOfElements(numberOfElements,entity)
85
86         connectivityTetra =  [1,2,3,6,
87                               1,2,4,3 ,
88                               1,2,5,4 ,
89                               1,2,6,5 ,
90                               2,7,4,3 ,
91                               2,8,5,4 ,
92                               2,9,6,5 ,
93                               2,10,3,6,
94                               2,7,3,10,
95                               2,8,4,7 ,
96                               2,9,5,8 ,
97                               2,10,6,9]
98
99         myMeshing.setConnectivity(entity,types[0], array( connectivityTetra ))
100         conn = myMeshing.getConnectivity(MED_NODAL,entity,MED_ALL_ELEMENTS)
101         self.assertTrue( isinstance( conn, ndarray ))
102
103         myGroup = GROUP()
104         myGroup.setName("SomeCells")
105         myGroup.setMesh(myMeshing)
106         myGroup.setEntity(MED_CELL)
107         myGroup.setNumberOfGeometricType(1)
108
109         myGroup.setGeometricType(types)
110         myNumberOfElements = [4,1,2]
111         myGroup.setNumberOfElements(myNumberOfElements)
112
113         index = [1,5,6,8]
114         values = [2,7,8,12,13,15,16]
115         myGroup.setNumber( array( index ), array( values ))
116
117         myMeshing.addGroup(myGroup)
118
119         # saving the generated mesh in MED
120
121         import os
122         tmpDir  = os.getenv("TMP")
123         if not tmpDir:
124             tmpDir = os.getenv("TMPDIR", "/tmp")
125
126         filename = os.path.join(tmpDir,"medNumPy_test.med")
127         myMeshing.write(MED_DRIVER,filename)
128
129         # we build now 2 fields
130
131         supportOnNodes = SUPPORT(myMeshing,"On_All_Nodes",MED_NODE)
132         numberOfNodes = supportOnNodes.getNumberOfElements(MED_ALL_ELEMENTS)
133
134         fieldDoubleScalarOnNodes = FIELDDOUBLE(supportOnNodes,1)
135         fieldDoubleScalarOnNodes.setName("fieldScalarDoubleNode")
136         fieldDoubleScalarOnNodes.setIterationNumber(-1)
137         fieldDoubleScalarOnNodes.setOrderNumber(-1)
138         fieldDoubleScalarOnNodes.setTime(0.0)
139
140         fieldDoubleScalarOnNodes.setComponentName(1,"Vx")
141         fieldDoubleScalarOnNodes.setComponentDescription(1,"comp1")
142         fieldDoubleScalarOnNodes.setMEDComponentUnit(1,"unit1")
143
144         nodeValues = arange( numberOfNodes, dtype=float )
145         fieldDoubleScalarOnNodes.setValue( nodeValues )
146
147         resValue = fieldDoubleScalarOnNodes.getValue()
148         self.assertTrue( isinstance( resValue, ndarray ))
149
150         intArray = arange( numberOfNodes, dtype=int )
151         self.assertRaises( TypeError, fieldDoubleScalarOnNodes.setValue, intArray )
152
153         fieldDoubleScalarOnNodes.write(MED_DRIVER,filename)
154
155
156         supportOnCells = SUPPORT(myMeshing,"On_All_Cells",MED_CELL)
157         numberOfCells = supportOnCells.getNumberOfElements(MED_ALL_ELEMENTS)
158
159         fieldIntScalarOnCells = FIELDINT(supportOnCells,1)
160         fieldIntScalarOnCells.setName("fieldScalarIntCell")
161         fieldIntScalarOnCells.setIterationNumber(-1)
162         fieldIntScalarOnCells.setOrderNumber(-1)
163         fieldIntScalarOnCells.setTime(0.0)
164
165         fieldIntScalarOnCells.setComponentName(1,"Vx")
166         fieldIntScalarOnCells.setComponentDescription(1,"comp1")
167         fieldIntScalarOnCells.setMEDComponentUnit(1,"unit1")
168
169         cellValues = arange(numberOfCells)
170         fieldIntScalarOnCells.setValue( cellValues )
171
172         resValue = fieldIntScalarOnCells.getValue()
173         self.assertTrue( isinstance( resValue, ndarray ))
174         self.assertTrue( len(resValue) == numberOfCells)
175
176         dblArray = arange( numberOfNodes, dtype=float )
177         self.assertRaises( TypeError, fieldIntScalarOnCells.setValue, dblArray )
178
179         fieldIntScalarOnCells.write(MED_DRIVER,filename)
180
181         os.remove( filename )
182
183     def setUp(self):
184         pass
185
186 unittest.main()