1 ####################################################################################################
2 # CALCULATOR_TEST_STUDY_WITHOUTIHM.py
4 # Test the calculator component: using Med Client classes with writing in a med file
5 # results from the calculator component
7 ####################################################################################################
8 from libMEDClient import *
16 #CCRTfrom libSALOME_Swig import *
17 #CCRTsg = SALOMEGUI_Swig()
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
44 def changeBlankToUnderScore(stringWithBlank):
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
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
64 elif (listLength > 1) :
65 print "there are more than one instance of ",objNameInStudy," in the Study under the component ",compNameInStudy
69 print "getMedObjectFromStudy",objNameInStudy," cannot be found in the Study"
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)
77 print objNameInStudy," has been found in the Study but with the wrong type"
80 def getMeshObjectFromStudy(meshName):
81 objNameInStudy = "/Med/MEDMESH/"+meshName
82 #CCRTmySO = salome.myStudy.FindObjectByPath(objNameInStudy)
83 mySO = myStudy.FindObjectByPath(objNameInStudy)
85 print "getMeshObjectFromStudy",objNameInStudy," cannot be found in the Study"
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)
93 print objNameInStudy," has been found in the Study but with the wrong type"
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)
102 print "getSupportObjectFromStudy",objNameInStudy," cannot be found in the Study"
103 print "/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy,":",myStudy.GetObjectNames("/Med/MEDMESH/MEDSUPPORTS_OF_"+meshNameStudy)
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)
111 print objNameInStudy," has been found in the Study but with the wrong type"
114 def getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName):
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)
121 print "getFieldObjectFromStudy",objNameInStudy," cannot be found in the Study"
122 print "/Med/MEDFIELD/"+fieldName,":",myStudy.GetObjectNames("/Med/MEDFIELD/"+fieldName)
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)
131 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
134 print objNameInStudy," has been found in the Study but with the wrong type"
138 fileName = "pointe.med"
140 #CCRTmedComp=salome.lcc.FindOrLoadComponent("FactoryServer", "MED")
141 medComp=lcc.FindOrLoadComponent("FactoryServer", "MED")
145 filePath=os.environ["MED_ROOT_DIR"]
146 filePath=filePath+"/share/salome/resources/med/"
148 filePathName = filePath + fileName
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)
155 print "getting the MED object from the study"
156 medObj = getMedObjectFromStudy(fileName)
158 nbOfMeshes = medObj.getNumberOfMeshes()
159 meshNames = medObj.getMeshNames()
161 print "in this med file there is(are) ",nbOfMeshes," mesh(es):"
163 meshName = meshNames[0]
165 meshObj = medObj.getMeshByName(meshName)
167 fieldName = "fieldcelldoublevector"
170 entitySupport = "MED_MAILLE"
171 supportName = "SupportOnAll_"+entitySupport
173 fieldTypedObj,typeField = getFieldObjectFromStudy(dt,it,fieldName,supportName,meshName)
176 fieldTypedLocalCopy = FIELDDOUBLEClient(fieldTypedObj)
177 elif (typeField == 0):
178 fieldTypedLocalCopy = FIELDINTClient(fieldTypedObj)
180 print "Problem with the type of the field"
182 ############## Load Calculator Component ###################
183 # Calculator Component must be in the Container of MED
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")
192 #calculator.printField(fieldTypedObj)
196 ############## Test Calculator Component ###################
199 print "Appel cloneField : fieldTypedObj -> f1,f2,f3,f4"
200 fieldTypedObj.Register()
201 (f1,f2,f3,f4)=calculator.cloneField(fieldTypedObj) # fieldTypedObj is consumed
204 ##calculator.printField(f1)
205 print "Add fields f2+f3"
206 f_add=calculator.add(f2, f3)
208 ##calculator.printField( f_add ) # f_add is consumed
211 print "Apply linear function"
212 f_lin=calculator.applyLin(f4,2.0,1.0)
214 ##calculator.printField( f_lin ) # f_lin is consumed
216 print "Appel Norme Max "
218 norme=calculator.normMax(f_lin) # f_lin is consumed
219 print " -> norme = ",norme
224 ############ Creation of a MED file with fields created by Caculator #################
228 meshDistant = f_add.getSupport().getMesh()
230 meshLocalCopy = MESHClient(meshDistant)
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 :"
244 for isd in range(spaceDimension):
245 coordNames.append(meshLocalCopy.getCoordinateName(isd))
246 coordUnits.append(meshLocalCopy.getCoordinateUnit(isd))
248 print " names:", coordNames
249 print " units", coordUnits
251 coordinates = meshLocalCopy.getCoordinates(MED_FULL_INTERLACE)
252 for k in range(numberOfNodes):
254 print " ---- ", coordinates[k*spaceDimension:(kp1*spaceDimension)]
256 print " The Cell Nodal Connectivity of the Cells:"
257 nbTypesCell = meshLocalCopy.getNumberOfTypes(MED_CELL)
260 print " The Mesh has",nbTypesCell,"Type(s) of Cell"
261 types = meshLocalCopy.getTypes(MED_CELL)
262 for k in range(nbTypesCell):
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]
275 ## TEST METHODS ABOUT POLY ELEMENTS ##
277 nbTypesCellWithPoly = meshLocalCopy.getNumberOfTypesWithPoly(MED_CELL)
278 if (nbTypesCell == nbTypesCellWithPoly):
280 print " No Poly Cells in the mesh"
285 print " The Cell Nodal Connectivity of the Poly Cells:"
287 print " The Mesh has",nbTypesCellWithPoly-nbTypesCell,"Type(s) of Poly Cell"
288 types = meshLocalCopy.getTypesWithPoly(MED_CELL)
289 for k in range(nbTypesCellWithPoly):
291 if type == MED_POLYGON:
292 nbElemType = meshLocalCopy.getNumberOfPolygons()
293 elif type == MED_POLYHEDRA:
294 nbElemType = meshLocalCopy.getNumberOfPolyhedron()
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 ]
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 ]
323 f_addLocal = FIELDDOUBLEClient(f_add)
325 f_addLocal.setName(f_addLocal.getName()+"add")
327 f_linLocal = FIELDDOUBLEClient(f_lin)
329 f_linLocal.setName(f_linLocal.getName()+"lin")
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 )
337 # writting the mesh and the fields
338 medFileVersion = getMedFileVersionForWriting()
340 if (medFileVersion == V22):
341 setMedFileVersionForWriting(V21)
343 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed21File, meshLocalCopy.getName(), MED_REMP)
344 meshLocalCopy.write(idMed)
346 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed21File, f_addLocal.getName())
347 f_addLocal.write(idMed)
349 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed21File, f_linLocal.getName())
350 f_linLocal.write(idMed)
352 medFileVersion = getMedFileVersionForWriting()
354 if (medFileVersion == V21):
355 setMedFileVersionForWriting(V22)
357 idMed = meshLocalCopy.addDriver(MED_DRIVER, Outmed22File, meshLocalCopy.getName(), MED_REMP)
358 meshLocalCopy.write(idMed)
360 idMed = f_addLocal.addDriver(MED_DRIVER, Outmed22File, f_addLocal.getName())
361 f_addLocal.write(idMed)
363 idMed = f_linLocal.addDriver(MED_DRIVER, Outmed22File, f_linLocal.getName())
364 f_linLocal.write(idMed)
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')
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')
396 #myStudyManager.Close(myStudy)
401 ############ Output MED file with fields created by Caculator #################
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
410 print "Reading the .med file ",Outmed21File," and pushing corba objects in the SALOME study"
412 medComp.readStructFileWithFieldType(Outmed21File,studyname)
414 Outf_add = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectoradd',-1,-1)
415 Outf_lin = medComp.readFieldInFile(Outmed21File,studyname,'fieldcelldoublevectorlin',-1,-1)
417 myStudy.GetObjectNames('/Med')
419 myStudy.GetObjectNames('/Med/MED_OBJECT_FROM_FILE_OutCalculatorpointe21_V3.2.0b1.med')
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')
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')
441 #myStudyManager.Save(myStudy,1)
442 #myStudy.DumpStudy("/tmp","medClient_withoutIHM_add_lin",1)
444 myStudyManager.SaveAs(studyname+'.hdf',myStudy,0)
447 print "END of the Pyhton script ..... Ctrl D to exit"