]> SALOME platform Git repositories - samples/pycalculator.git/blob - src/PYCALCULATOR/PYCALCULATOR.py
Salome HOME
06c530d35ce2c83f6817be8df03357253bffee74
[samples/pycalculator.git] / src / PYCALCULATOR / PYCALCULATOR.py
1 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 # Instruct Python to load dynamic libraries using global resolution of symbols
24 # This is necessary to ensure that different modules will have the same definition
25 # of dynamic types and C++ RTTI will work between them
26 #
27 import DLFCN, sys
28 sys.setdlopenflags(DLFCN.RTLD_NOW | DLFCN.RTLD_GLOBAL)
29
30 import PYCALCULATOR_ORB__POA
31 import SALOME_ComponentPy
32
33 import SALOME_MED
34 from libMEDMEM_Swig import *
35 from libMedCorba_Swig import *
36 from libMEDClient import *
37
38 class PYCALCULATOR (PYCALCULATOR_ORB__POA.PYCALCULATOR_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i ):
39
40     def __init__(self, orb, poa, contID, containerName, instanceName,
41                  interfaceName):
42         print "Begin of PYCALCULATOR::__init__"
43         notif = 0
44         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa, 
45                contID, containerName, instanceName, interfaceName, notif)
46         self._naming_service=SALOME_ComponentPy.SALOME_NamingServicePy_i(self._orb)
47         print "End of PYCALCULATOR::__init__" 
48
49     def Add(self, field1, field2):
50         print "Begin of PYCALCULATOR::Add"
51         print "pointeur on first argument : ",field1
52         print "            second         : ",field2
53
54         bool = 1
55         nbOfComp1 = field1.getNumberOfComponents()
56         name1 = field1.getName()
57         description1 = field1.getDescription()
58
59         nbOfComp2 = field2.getNumberOfComponents()
60         name2 = field2.getName()
61         description2 = field2.getDescription()
62
63         if (nbOfComp1 != nbOfComp2): bool = 0
64
65         compName = []
66         compUnit = []
67         for k in range(nbOfComp1):
68             kp1 = k+1
69             compName.append(field1.getComponentName(kp1))
70             compUnit.append(field1.getComponentUnit(kp1))
71             if (bool):
72                 if ((compName[k] != field2.getComponentName(kp1)) or (compUnit[k] != field2.getComponentUnit(kp1))):
73                     bool = 0
74
75         support1 = field1.getSupport()
76         entity1 = support1.getEntity()
77         mesh1 = support1.getMesh()
78
79         support2 = field2.getSupport()
80         entity2 = support2.getEntity()
81         mesh2 = support2.getMesh()
82
83         if (support1.isOnAllElements()):
84             lengthValue1 = mesh1.getNumberOfElements(entity1,SALOME_MED.MED_ALL_ELEMENTS)
85             number1 = []
86             for k in range(lengthValue1):
87                 number1.append(k)
88         else:
89             lengthValue1 = support1.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
90             number1 = support1.getNumber(SALOME_MED.MED_ALL_ELEMENTS)
91
92         if (support2.isOnAllElements()):
93             lengthValue2 = mesh2.getNumberOfElements(entity2,SALOME_MED.MED_ALL_ELEMENTS)
94             number2 = []
95             for k in range(lengthValue2):
96                 number2.append(k)
97         else:
98             lengthValue2 = support2.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
99             number2 = support2.getNumber(SALOME_MED.MED_ALL_ELEMENTS)
100
101         # comparision of each support: due to the fact that they are CORBA
102         # pointers, the comparision will be done directly throught the numbers
103
104         if (bool):
105             if ((lengthValue1 != lengthValue2) or (entity1 != entity2)):
106                 bool = 0
107
108         if (bool):
109             for k in range(lengthValue1):
110                 if (number1[k] != number2[k]):
111                     bool = 0
112
113         value1 = field1.getValue(SALOME_MED.MED_FULL_INTERLACE)
114
115         value2 = []
116         if (bool):
117             value2 = field2.getValue(SALOME_MED.MED_FULL_INTERLACE)
118
119         valueOut = []
120         if (bool):
121             print "CALCULATORPY::Add : The fields have the same support, they then can be added"
122             for k in range(lengthValue1*nbOfComp1):
123                 valueOut.append((value1[k]+value2[k]))
124         else:
125             print "CALCULATORPY::Add : WARNING !! For some reason the fields have not the same support"
126             for k in range(lengthValue1*nbOfComp1):
127                 valueOut.append(value1[k])
128
129         supportOutLocal = SUPPORTClient( support1 )
130         supportOutCorba = createCorbaSupport( supportOutLocal )
131
132         print "CALCULATORPY::Add : Creation of the local field, nbOfComp = ",nbOfComp1, " length = ",lengthValue1
133
134         fieldOutLocal = createLocalFieldDouble(nbOfComp1,lengthValue1)
135         fieldOutLocal.setValue(valueOut)
136         fieldOutLocal.setName("-new_Add_Field-")
137         fieldOutLocal.setDescription(description1)
138         fieldOutLocal.setSupport( supportOutLocal )
139
140         for k in range(nbOfComp1):
141             kp1 = k + 1
142             fieldOutLocal.setComponentName(kp1,compName[k])
143             fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
144
145         print "CALCULATORPY::Add : Creation of the CORBA field"
146
147         fieldOutCorba = createCorbaFieldDouble(supportOutCorba,fieldOutLocal)
148
149         print "End of CALCULATORPY::Add"
150         return fieldOutCorba
151
152     def Mul(self, field1, x1):
153         print "Begin of CALCULATORPY::Mul"
154         print "pointeur on first argument : ",field1
155         print "            second         : ",x1
156
157         nbOfComp = field1.getNumberOfComponents()
158         name = field1.getName()
159         description = field1.getDescription()
160         support = field1.getSupport()
161         print ".... get Names and Unit"
162         compName = []
163         compUnit = []
164         for k in range(nbOfComp):
165             kp1 = k+1
166             compName.append(field1.getComponentName(kp1))
167             compUnit.append(field1.getComponentUnit(kp1))
168
169
170         print ".... get mesh"
171         mesh = support.getMesh()
172
173         if (support.isOnAllElements()):
174             lengthValue = mesh.getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
175         else:
176             lengthValue = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
177
178         value1 = field1.getValue(SALOME_MED.MED_FULL_INTERLACE)
179         valueOut = []
180         for k in range(lengthValue*nbOfComp):
181             valueOut.append(x1*value1[k])
182
183         supportOutLocal = SUPPORTClient( support )
184         supportOutCorba = createCorbaSupport( supportOutLocal )
185
186         print "CALCULATORPY::Mul : Creation of the local field, nbOfComp = ",nbOfComp, " length = ",lengthValue
187
188         fieldOutLocal = createLocalFieldDouble(nbOfComp,lengthValue)
189         fieldOutLocal.setValue(valueOut)
190         fieldOutLocal.setName("-new_Mul_Field-")
191         fieldOutLocal.setDescription(description)
192         fieldOutLocal.setSupport( supportOutLocal )
193
194         for k in range(nbOfComp):
195             kp1 = k + 1
196             fieldOutLocal.setComponentName(kp1,compName[k])
197             fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
198
199         print "CALCULATORPY::Mul : Creation of the CORBA field"
200
201         fieldOutCorba = createCorbaFieldDouble(supportOutCorba,fieldOutLocal)
202
203         print "End of CALCULATORPY::Mul"
204         return fieldOutCorba
205
206     def Constant(self, field1, x1):
207         print "Begin of CALCULATORPY::Constant"
208         print "pointeur on first argument : ",field1
209         print "            second         : ",x1
210
211         nbOfComp = field1.getNumberOfComponents()
212         name = field1.getName()
213         description = field1.getDescription()
214         support = field1.getSupport()
215         compName = []
216         compUnit = []
217         for k in range(nbOfComp):
218             kp1 = k+1
219             compName.append(field1.getComponentName(kp1))
220             compUnit.append(field1.getComponentUnit(kp1))
221
222         mesh = support.getMesh()
223
224         if (support.isOnAllElements()):
225             lengthValue = mesh.getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
226         else:
227             lengthValue = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
228
229         valueOut = []
230         for k in range(lengthValue*nbOfComp):
231             valueOut.append(x1)
232
233         print "CALCULATORPY::Constant : Creation of the local field, nbOfComp = ",nbOfComp, " length = ",lengthValue
234
235         supportOutLocal = SUPPORTClient( support )
236         supportOutCorba = createCorbaSupport( supportOutLocal )
237
238         fieldOutLocal = createLocalFieldDouble(nbOfComp,lengthValue)
239         fieldOutLocal.setValue(valueOut)
240         fieldOutLocal.setName("-new_Const_Field-")
241         fieldOutLocal.setDescription(description)
242         fieldOutLocal.setSupport( supportOutLocal )
243
244         for k in range(nbOfComp):
245             kp1 = k + 1
246             fieldOutLocal.setComponentName(kp1,compName[k])
247             fieldOutLocal.setMEDComponentUnit(kp1,compUnit[k])
248
249         print "CALCULATORPY::Constant : Creation of the CORBA field"
250
251         fieldOutCorba = createCorbaFieldDouble(supportOutCorba,fieldOutLocal)
252
253         print "End of CALCULATORPY::Constant"
254         return fieldOutCorba
255
256     def writeMEDfile(self, field1, fileName):
257         print "Begin of CALCULATORPY::writeMEDfile"
258         print "CALCULATORPY::writeMEDfile : pointeur on first argument : ",field1
259         print "                          second         : ",fileName
260         print "CALCULATORPY::writeMEDfile : NOT YET IMPLEMENTED"
261         print "End of CALCULATORPY::writeMEDfile"