Salome HOME
cc2dff77d08ece53fc0a537c3cf2637902dee617
[samples/calculator.git] / src / CALCULATOR / CALCULATOR_TEST_STUDY_WITHOUTIHM.py
1 ####################################################################################################
2 # CALCULATOR_TEST_STUDY_WITHOUTIHM.py
3 #
4 # Test the calculator component: using Med Client classes with writing in a med file
5 # results from the calculator component
6 #
7 ####################################################################################################
8 from libMEDClient import *
9
10 import string
11
12 import salome
13
14 import SALOME_MED
15
16 #CCRTfrom libSALOME_Swig import *
17 #CCRTsg = SALOMEGUI_Swig()
18
19 #CCRT :
20 import SALOMEDS
21
22 import os
23 host = os.getenv( 'HOST' )
24 orb, lcc, naming_service, contmgr = salome.salome_kernel.salome_kernel_init()
25 obj = naming_service.Resolve('myStudyManager')
26 myStudyManager = obj._narrow(SALOMEDS.StudyManager)
27 print "studyManager found"
28 myStudy = myStudyManager.NewStudy('medClient_withoutIHM_test')
29 studynameId = myStudy._get_StudyId()
30 studyname = myStudy._get_Name()
31 print "We are working in the study ",studyname," with the ID ",studynameId
32 #endCCRT
33
34 def print_ord(i):
35     if i == 0:
36         return 'first'
37     elif i == 1:
38         return 'second'
39     elif i == 2:
40         return 'third'
41     else:
42         return `(i+1)`+'th'
43
44 def changeBlankToUnderScore(stringWithBlank):
45     blank = ' '
46     underscore = '_'
47     decompString = string.split(stringWithBlank,blank)
48     length = len(decompString)
49     stringWithUnderScore = decompString[0]
50     for i in range(1,length):
51         stringWithUnderScore += underscore
52         stringWithUnderScore += decompString[i]
53     return stringWithUnderScore
54
55 def getMedObjectFromStudy(file):
56     objNameInStudy = "MED_OBJECT_FROM_FILE_"+file
57     compNameInStudy= "MED"
58     #CCRTlistOfSO = salome.myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
59     listOfSO = myStudy.FindObjectByName(objNameInStudy,compNameInStudy)
60     listLength = len(listOfSO)
61     if (listLength == 0) :
62         print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study under the component ",compNameInStudy
63         return None
64     elif (listLength > 1) :
65         print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
66         return None
67     mySO = listOfSO[0]
68     if (mySO == None) :
69         print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study"
70         return mySO
71     else:
72         anAttr = mySO.FindAttribute("AttributeIOR")[1]
73         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
74         obj = orb.string_to_object(anAttr.Value())
75         myObj = obj._narrow(SALOME_MED.MED)
76         if (myObj == None) :
77             print objNameInStudy," has been found in the Study but with the wrong type"
78         return myObj
79
80 def getMeshObjectFromStudy(meshName):
81     objNameInStudy = "/Med/MEDMESH/"+meshName
82     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
83     mySO = myStudy.FindObjectByPath(objNameInStudy)
84     if (mySO == None) :
85         print "getMeshObjectFromStudy",objNameInStudy," cannot be found in the Study"
86         return mySO
87     else:
88         anAttr = mySO.FindAttribute("AttributeIOR")[1]
89         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
90         obj = orb.string_to_object(anAttr.Value())
91         myObj = obj._narrow(SALOME_MED.MESH)
92         if (myObj == None) :
93             print objNameInStudy," has been found in the Study but with the wrong type"
94         return myObj
95
96 def getSupportObjectFromStudy(meshName,supportName):
97     meshNameStudy = changeBlankToUnderScore(meshName)
98     objNameInStudy = "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy+"/"+supportName
99     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
100     mySO = myStudy.FindObjectByPath(objNameInStudy)
101     if (mySO == None) :
102         print "getSupportObjectFromStudy",objNameInStudy," cannot be found in the Study"
103         print "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy,":",myStudy.GetObjectNames("/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy)
104         return mySO
105     else:
106         anAttr = mySO.FindAttribute("AttributeIOR")[1]
107         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
108         obj = orb.string_to_object(anAttr.Value())
109         myObj = obj._narrow(SALOME_MED.SUPPORT)
110         if (myObj == None) :
111             print objNameInStudy," has been found in the Study but with the wrong type"
112         return myObj
113
114 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
115     type = -1
116     meshNameStudy = changeBlankToUnderScore(meshName)
117     objNameInStudy = "/Med/MEDFIELD/"+fieldName+"/("+str(dt)+","+str(it)+")_ON_"+supportName+"_OF_"+meshNameStudy
118     #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
119     mySO = myStudy.FindObjectByPath(objNameInStudy)
120     if (mySO == None) :
121         print "getFieldObjectFromStudy",objNameInStudy," cannot be found in the Study"
122         print "/Med/MEDFIELD/"+fieldName,":",myStudy.GetObjectNames("/Med/MEDFIELD/"+fieldName)
123         return -1,-1
124     else:
125         anAttr = mySO.FindAttribute("AttributeIOR")[1]
126         #CCRTobj = salome.orb.string_to_object(anAttr.Value())
127         obj = orb.string_to_object(anAttr.Value())
128         myObj = obj._narrow(SALOME_MED.FIELDINT)
129         type = 0
130         if (myObj == None):
131             myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
132             type = 1
133             if (myObj == None) :
134                 print objNameInStudy," has been found in the Study but with the wrong type"
135         return myObj,type
136
137
138 fileName = "pointe.med"
139
140 #CCRTmedComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
141 medComp=lcc.FindOrLoadComponent("FactoryServer", "MED")
142
143 import os
144
145 filePath=os.environ["MED_ROOT_DIR"]
146 filePath=filePath+"/share/salome/resources/med/"
147
148 filePathName = filePath + fileName
149
150 print "Reading the .med file ",filePathName," and pushing corba objects in the SALOME study"
151 #CCRTmedComp.readStructFileWithFieldType(filePathName,salome.myStudyName)
152 medComp.readStructFileWithFieldType(filePathName,studyname)
153 #CCRTsg.updateObjBrowser(1)
154
155 print "getting the MED object from the study"
156 medObj = getMedObjectFromStudy(fileName)
157
158 nbOfMeshes = medObj.getNumberOfMeshes()
159 meshNames = medObj.getMeshNames()
160
161 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
162
163 meshName = meshNames[0]
164
165 meshObj = medObj.getMeshByName(meshName)
166
167 fieldName = "fieldcelldoublevector"
168 dt = -1
169 it = -1
170 entitySupport = "MED_MAILLE"
171 supportName = "SupportOnAll_"+entitySupport
172
173 fieldTypedObj,typeField = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
174
175 if(typeField == 1):
176     fieldTypedLocalCopy = FIELDDOUBLEClient(fieldTypedObj)
177 elif (typeField == 0):
178     fieldTypedLocalCopy = FIELDINTClient(fieldTypedObj)
179 else:
180     print "Problem with the type of the field"
181
182 ##############  Load Calculator Component ###################
183 # Calculator Component must be in the Container of MED
184 #
185 print "Load Calculator Component "
186 # we need to import CALCULATOR_ORB to get a typed object (to perform narrowing)
187 import CALCULATOR_ORB
188 #CCRTcalculator = salome.lcc.FindOrLoadComponent("FactoryServer", "CALCULATOR")
189 calculator = lcc.FindOrLoadComponent("FactoryServer", "CALCULATOR")
190 #
191
192 #calculator.printField(fieldTypedObj)
193
194 #
195 #
196 ##############  Test Calculator Component ###################
197 #
198 #
199 print "Appel cloneField : fieldTypedObj -> f1,f2,f3,f4"
200 fieldTypedObj.Register()
201 (f1,f2,f3,f4)=calculator.cloneField(fieldTypedObj)  # fieldTypedObj is consumed
202 #
203 ##f1.Register()
204 ##calculator.printField(f1)
205 print "Add fields f2+f3"
206 f_add=calculator.add(f2, f3)
207 ##f_add.Register()
208 ##calculator.printField( f_add ) # f_add is consumed
209
210 #
211 print "Apply linear function"
212 f_lin=calculator.applyLin(f4,2.0,1.0)
213 ##f_lin.Register()
214 ##calculator.printField( f_lin ) # f_lin is consumed
215 #
216 print "Appel Norme Max "
217 f_lin.Register()
218 norme=calculator.normMax(f_lin) # f_lin is consumed
219 print " -> norme = ",norme
220 #
221
222 #
223 #
224 ############  Creation of a MED file with fields created by Caculator  #################
225 #                   via Client classes
226 #
227
228 meshDistant = f_add.getSupport().getMesh()
229
230 meshLocalCopy = MESHClient(meshDistant)
231
232
233 print "      getting information from the local copy of the distant mesh"
234 name = meshLocalCopy.getName()
235 spaceDimension = meshLocalCopy.getSpaceDimension()
236 meshDimension = meshLocalCopy.getMeshDimension()
237 numberOfNodes = meshLocalCopy.getNumberOfNodes()
238 print "          Name = ", name, " space Dim = ", spaceDimension, " mesh Dim = ", meshDimension, " Nb of Nodes = ", numberOfNodes
239 coordSyst = meshLocalCopy.getCoordinatesSystem()
240 print "          The coordinates system is",coordSyst
241 print "          The Coordinates :"
242 coordNames = []
243 coordUnits = []
244 for isd in range(spaceDimension):
245     coordNames.append(meshLocalCopy.getCoordinateName(isd))
246     coordUnits.append(meshLocalCopy.getCoordinateUnit(isd))
247
248 print "          names:", coordNames
249 print "          units", coordUnits
250 print "          values:"
251 coordinates = meshLocalCopy.getCoordinates(MED_FULL_INTERLACE)
252 for k in range(numberOfNodes):
253     kp1 = k+1
254     print "         ---- ", coordinates[k*spaceDimension:(kp1*spaceDimension)]
255 print ""
256 print "          The Cell Nodal Connectivity of the Cells:"
257 nbTypesCell = meshLocalCopy.getNumberOfTypes(MED_CELL)
258 print ""
259 if (nbTypesCell>0):
260     print "      The Mesh has",nbTypesCell,"Type(s) of Cell"
261     types = meshLocalCopy.getTypes(MED_CELL)
262     for k in range(nbTypesCell):
263         type = types[k]
264         nbElemType = meshLocalCopy.getNumberOfElements(MED_CELL,type)
265         print "     For the type:",type,"there is(are)",nbElemType,"elemnt(s)"
266         connectivity = meshLocalCopy.getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,type)
267         nbNodesPerCell = type%100
268         for j in range(nbElemType):
269             print "       Element",(j+1)," ",connectivity[j*nbNodesPerCell:(j+1)*nbNodesPerCell]
270             pass
271         pass
272     pass
273
274 ##
275 ## TEST METHODS ABOUT POLY ELEMENTS ##
276 ##
277 nbTypesCellWithPoly = meshLocalCopy.getNumberOfTypesWithPoly(MED_CELL)
278 if (nbTypesCell == nbTypesCellWithPoly):
279     print ""
280     print "          No Poly Cells in the mesh"
281     print ""
282     pass
283 else:
284     print ""
285     print "          The Cell Nodal Connectivity of the Poly Cells:"
286     print ""
287     print "      The Mesh has",nbTypesCellWithPoly-nbTypesCell,"Type(s) of Poly Cell"
288     types = meshLocalCopy.getTypesWithPoly(MED_CELL)
289     for k in range(nbTypesCellWithPoly):
290         type = types[k]
291         if type == MED_POLYGON:
292             nbElemType = meshLocalCopy.getNumberOfPolygons()
293         elif type == MED_POLYHEDRA:
294             nbElemType = meshLocalCopy.getNumberOfPolyhedron()
295         else:
296             continue
297         print ""
298         print "     For the type:",type,"there is(are)",nbElemType,"elemnt(s)"
299         if type == MED_POLYGON:
300             connectivity = meshLocalCopy.getPolygonsConnectivity(MED_NODAL,MED_CELL)
301             index = meshLocalCopy.getPolygonsConnectivityIndex(MED_NODAL,MED_CELL)
302             for j in range(nbElemType):
303                 print "       Polygon",(j+1)," ",connectivity[ index[j]-1 : index[j+1]-1 ]
304                 pass
305             pass
306         else:
307             connectivity = meshLocalCopy.getPolyhedronConnectivity(MED_NODAL)
308             fIndex = meshLocalCopy.getPolyhedronFacesIndex()
309             index = meshLocalCopy.getPolyhedronIndex(MED_NODAL)
310             for j in range(nbElemType):
311                 print     "       Polyhedra",(j+1)
312                 iF1, iF2 = index[ j ]-1, index[ j+1 ]-1
313                 for f in range( iF2 - iF1 ):
314                     iN1, iN2 = fIndex[ iF1+f ]-1, fIndex[ iF1+f+1 ]-1
315                     print "         Face",f+1," ",connectivity[ iN1 : iN2 ]
316                     pass
317                 pass
318             pass
319         pass
320     pass
321 pass
322
323 f_addLocal = FIELDDOUBLEClient(f_add)
324
325 f_addLocal.setName(f_addLocal.getName()+"add")
326
327 f_linLocal = FIELDDOUBLEClient(f_lin)
328
329 f_linLocal.setName(f_linLocal.getName()+"lin")
330
331 #Warning : OutputMedFiles are removed here after =================================
332 Outmed21File="OutCalculatorpointe21_V3.2.0b1.med"
333 os.system( 'rm -fr ' + Outmed21File )
334 Outmed22File="OutCalculatorpointe22_V3.2.0b1.med"
335 os.system( 'rm -fr ' + Outmed22File )
336
337 # writting the mesh and the fields
338 medFileVersion = getMedFileVersionForWriting()
339
340 if (medFileVersion == V22):
341     setMedFileVersionForWriting(V21)
342
343 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed21File, meshLocalCopy.getName(), MED_REMP)
344 meshLocalCopy.write(idMed)
345
346 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed21File, f_addLocal.getName())
347 f_addLocal.write(idMed)
348
349 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed21File, f_linLocal.getName())
350 f_linLocal.write(idMed)
351
352 medFileVersion = getMedFileVersionForWriting()
353
354 if (medFileVersion == V21):
355     setMedFileVersionForWriting(V22)
356
357 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed22File, meshLocalCopy.getName(), MED_REMP)
358 meshLocalCopy.write(idMed)
359
360 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed22File, f_addLocal.getName())
361 f_addLocal.write(idMed)
362
363 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed22File, f_linLocal.getName())
364 f_linLocal.write(idMed)
365
366 myStudy.GetObjectNames('/Med')
367 myStudy.GetObjectNames('/Med/MED_OBJECT_FROM_FILE_pointe.med')
368 myStudy.GetObjectNames('/Med/MEDMESH')
369 myStudy.GetObjectNames('/Med/MEDMESH/maa1')
370 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1')
371 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_1')
372 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_2')
373 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_3')
374 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe1')
375 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_1')
376 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_2')
377 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_3')
378 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_NOEUD_4')
379 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe2')
380 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe3')
381 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe4')
382 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe5')
383 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_MAILLE')
384 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_NOEUD')
385
386 myStudy.GetObjectNames('/Med/MEDFIELD')
387 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevector')
388 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevector/(-1,-1)_ON_SupportOnAll_MED_MAILLE_OF_maa1')
389 myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble')
390 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble/(-1,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
391 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble/1,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
392 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodedouble/(2,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
393 myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodeint')
394 #myStudy.GetObjectNames('/Med/MEDFIELD/fieldnodeint/(-1,-1)_ON_SupportOnAll_MED_NOEUD_OF_maa1')
395
396 #myStudyManager.Close(myStudy)
397 #myStudy.Close()
398
399 #
400 #
401 ############   Output MED file with fields created by Caculator  #################
402 #                   via Client classes
403 #
404
405 myStudy = myStudyManager.NewStudy('medClient_withoutIHM_add_lin')
406 studynameId = myStudy._get_StudyId()
407 studyname = myStudy._get_Name()
408 print "We are working in the study ",studyname," with the ID ",studynameId
409
410 print "Reading the .med file ",Outmed21File," and pushing corba objects in the SALOME study"
411
412 medComp.readStructFileWithFieldType(Outmed21File,studyname)
413
414 Outf_add  = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectoradd',-1,-1)
415 Outf_lin  = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectorlin',-1,-1)
416
417 myStudy.GetObjectNames('/Med')
418
419 myStudy.GetObjectNames('/Med/MED_OBJECT_FROM_FILE_OutCalculatorpointe21_V3.2.0b1.med')
420
421 myStudy.GetObjectNames('/Med/MEDMESH')
422 myStudy.GetObjectNames('/Med/MEDMESH/maa1')
423 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1')
424 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_1')
425 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_2')
426 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/FAMILLE_ELEMENT_3')
427 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe1')
428 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe2')
429 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe3')
430 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe4')
431 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/groupe5')
432 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_MAILLE')
433 myStudy.GetObjectNames('/Med/MEDMESH/MEDSUPPORTS_OF_maa1/SupportOnAll_MED_NOEUD')
434
435 myStudy.GetObjectNames('/Med/MEDFIELD')
436 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectoradd')
437 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectoradd/(-1,-1)_ON_SupportOnAll_MED_MAILLE_OF_maa1')
438 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectorlin')
439 myStudy.GetObjectNames('/Med/MEDFIELD/fieldcelldoublevectorlin/(-1,-1)_ON_SupportOnAll_MED_MAILLE_OF_maa1')
440
441 #myStudyManager.Save(myStudy,1)
442 #myStudy.DumpStudy("/tmp","medClient_withoutIHM_add_lin",1)
443
444 myStudyManager.SaveAs(studyname+'.hdf',myStudy,0)
445
446 print ""
447 print "END of the Pyhton script ..... Ctrl D to exit"