Salome HOME
8d08b2a34cc82ce3baa1e6e208171d63ccf82d78
[modules/med.git] / src / MedCorba_Swig / batchmode_medcorba_test.py
1 import batchmode_salome
2
3 import SALOME_MED
4
5 from libMedCorba_Swig import *
6
7 from random import *
8
9 #==============================================================================
10
11 def AnalyzeField(field):
12     name = field.getName()
13     desc = field.getDescription()
14     nbComp = field.getNumberOfComponents()
15     itNum = field.getIterationNumber()
16     ordNum = field.getOrderNumber()
17
18     print "Analysis of the field ",name," with the description ",desc
19     print "iteration number ",itNum," order Number ",ordNum
20     print "It has ",nbComp," component(s)"
21
22     fieldValue = field.getValue(SALOME_MED.MED_FULL_INTERLACE)
23     fieldSupport = field.getSupport()
24     fieldMesh = fieldSupport.getMesh()
25     fieldEntity = fieldSupport.getEntity()
26     bool = fieldSupport.isOnAllElements()
27
28     if bool:
29         print "The support of this field is on all entities ",fieldEntity," of the mesh ",fieldMesh.getName()
30         if fieldEntity == SALOME_MED.MED_NODE:
31             nbValByComp = fieldMesh.getNumberOfNodes()
32         else:
33             nbValByComp = fieldMesh.getNumberOfElements(fieldEntity,SALOME_MED.MED_ALL_ELEMENTS)
34         print "and its dimension (number of values by component of the field) is ",nbValByComp
35     else:
36         print "The support of this field is partially on entities ",fieldEntity," of the mesh ",fieldMesh.getName()
37         nbValByComp = fieldSupport.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
38         print "and its dimension (number of values by component of the field) is ",nbValByComp
39
40     for i in range(nbComp):
41         compName = field.getComponentName(i+1)
42         compUnit = field.getComponentUnit(i+1)
43         print "The ",(i+1),"-th  component ",compName," with the unit ",compUnit
44
45     for i in range(nbValByComp):
46         print "  * ",fieldValue[i*nbComp:(i+1)*nbComp]
47
48 #==============================================================================
49
50 def getMeshObjectFromStudy(number):
51     mySO = batchmode_salome.myStudy.FindObject("MEDMESH")
52     mysub = mySO.FindSubObject(number)[1]
53     if mysub:
54         Builder = batchmode_salome.myStudy.NewBuilder()
55         anAttr = Builder.FindOrCreateAttribute(mysub, "AttributeIOR")
56         obj = batchmode_salome.orb.string_to_object(anAttr.Value())
57         myObj = obj._narrow(SALOME_MED.MESH)
58         return myObj
59     else:
60         print "ERROR: No Mesh Object stored in this Study"
61         return None
62
63 #==============================================================================
64
65 def getMedObjectFromStudy():
66     mySO = batchmode_salome.myStudy.FindObject("Objet MED")
67     Builder = batchmode_salome.myStudy.NewBuilder()
68     anAttr = Builder.FindOrCreateAttribute(mySO, "AttributeIOR")
69     obj = batchmode_salome.orb.string_to_object(anAttr.Value())
70     myObj = obj._narrow(SALOME_MED.MED)
71     return myObj
72
73 #==============================================================================
74 #
75 # Since Corba pointeurs will be generated in this Python script
76 # a POA has to be registred to activate those Corba pointeurs
77 # and make them available by any clients (in general). In our
78 # case, it is to make those pointeurs avail able to itself
79 #==============================================================================
80
81 print "Activation of a POA to make available any Corba pointeurs"
82 poa = batchmode_salome.orb.resolve_initial_references("RootPOA")
83 poaManager = poa._get_the_POAManager()
84 poaManager.activate()
85
86 #==============================================================================
87
88 studyCurrent = batchmode_salome.myStudyName
89 studyCurrentId = batchmode_salome.myStudyId
90
91 print "We are working in the study ",studyCurrent," with the ID ",studyCurrentId
92 print ""
93
94 #medFile = "carre_en_quad4_seg2.med"
95 medFile = "cube_hexa8_quad4.med"
96
97 print "Loading of the Med Component"
98 print ""
99
100 med_comp = batchmode_salome.lcc.FindOrLoadComponent("FactoryServer", "Med")
101
102 med_comp.readStructFile(medFile,studyCurrent)
103
104 med_obj = getMedObjectFromStudy()
105
106 nbMeshes = med_obj.getNumberOfMeshes()
107
108 nbFields = med_obj.getNumberOfFields()
109
110 print ""
111 print "The med file ",medFile," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
112 print ""
113
114 meshcorba = getMeshObjectFromStudy(1)
115
116 name = meshcorba.getName()
117
118 nbNodes = meshcorba.getNumberOfNodes()
119
120 spaceDim = meshcorba.getSpaceDimension()
121
122 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes," Nodes"
123 print ""
124
125 for entity in [SALOME_MED.MED_NODE,SALOME_MED.MED_CELL,SALOME_MED.MED_FACE,SALOME_MED.MED_EDGE]:
126     nbFam = meshcorba.getNumberOfFamilies(entity)
127     if (entity == SALOME_MED.MED_NODE) & (nbFam > 0):
128         print "This mesh has",nbFam,"Node Family(ies)"
129     elif (entity == SALOME_MED.MED_CELL) & (nbFam > 0):
130         print "This mesh has",nbFam,"Cell Family(ies)"
131     elif (entity == SALOME_MED.MED_FACE) & (nbFam > 0):
132         print "This mesh has",nbFam,"Face Family(ies)"
133     elif (entity == SALOME_MED.MED_EDGE) & (nbFam > 0):
134         print "This mesh has",nbFam,"Edge Family(ies)"
135
136     if nbFam > 0:
137         for j in range(nbFam):
138             print ""
139             familycorba = meshcorba.getFamily(entity,j+1)
140             familyName = familycorba.getName()
141             familyDescription = familycorba.getDescription()
142             familyEntity = familycorba.getEntity()
143             familyBool = familycorba.isOnAllElements()
144             print "  -Name:",familyName
145             print "  -Description:",familyDescription
146             print "  -Entity:",familyEntity
147             familyIdentifier = familycorba.getIdentifier()
148             nbOfAtt = familycorba.getNumberOfAttributes()
149             print "  -Identifier:",familyIdentifier
150             print "  -Number Of Attributes:",nbOfAtt
151             attributesids = familycorba.getAttributesIdentifiers()
152             attributesvals = familycorba.getAttributesValues()
153             for k in range(nbOfAtt):
154                 print "    * Attributes:",attributesids[k],":",attributesvals[k],",",familycorba.getAttributeDescription(k+1)
155             print "  -Entities list:"
156             if (familyBool):
157                 print "  -Is on all entities"
158             else:
159                 types = familycorba.getTypes()
160                 nbOfTypes = len(types)
161                 print "  -Number Of Types:",nbOfTypes
162                 for k in range(nbOfTypes):
163                     type = types[k]
164                     nbOfElmtsOfType = familycorba.getNumberOfElements(type)
165                     number = familycorba.getNumber(type)
166                     print "    * Type",type
167                     print "    * Number",number[0:nbOfElmtsOfType]
168                 print ""
169                 lengthValue = familycorba.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS)
170                 nbOfComp = 1
171                 print "Generate a Local scalar double field"
172                 fieldScalDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
173                 for k in range(lengthValue):
174                     valueI = []
175                     for kcomp in range(nbOfComp):
176                         valueI.append(random())
177                     fieldScalDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
178                     valueIverif = fieldScalDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
179                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
180                 print "Generate a Corba scalar double field"
181                 fieldScalDblCorba = createCorbaFieldDouble(familycorba,fieldScalDblLoc)
182                 AnalyzeField(fieldScalDblCorba)
183                 print ""
184                 print "Generate a Local scalar integer field"
185                 fieldScalIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
186                 for k in range(lengthValue):
187                     valueI = []
188                     for kcomp in range(nbOfComp):
189                         valueI.append(randint(0,100))
190                     fieldScalIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
191                     valueIverif = fieldScalIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
192                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
193                 print "Generate a Corba scalar integer field"
194                 fieldScalIntCorba = createCorbaFieldInt(familycorba,fieldScalIntLoc)
195                 AnalyzeField(fieldScalIntCorba)
196                 print ""
197                 nbOfComp = spaceDim
198                 print "Generate a Local vector double field"
199                 fieldVectDblLoc = createLocalFieldDouble(nbOfComp,lengthValue)
200                 for k in range(lengthValue):
201                     valueI = []
202                     for kcomp in range(nbOfComp):
203                         valueI.append(random())
204                     fieldVectDblLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
205                     valueIverif = fieldVectDblLoc.getValueI(MED_FULL_INTERLACE,k+1)
206                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
207                 print "Generate a Corba vector double field"
208                 fieldVectDblCorba = createCorbaFieldDouble(familycorba,fieldVectDblLoc)
209                 AnalyzeField(fieldVectDblCorba)
210                 print ""
211                 print "Generate a Local vector integer field"
212                 fieldVectIntLoc = createLocalFieldInt(nbOfComp,lengthValue)
213                 for k in range(lengthValue):
214                     valueI = []
215                     for kcomp in range(nbOfComp):
216                         valueI.append(randint(0,100))
217                     fieldVectIntLoc.setValueI(MED_FULL_INTERLACE,k+1,valueI)
218                     valueIverif = fieldVectIntLoc.getValueI(MED_FULL_INTERLACE,k+1)
219                     print "     Set/Get Entry *",(k+1)," ",valueI[:nbOfComp],"  /  ",valueIverif[:nbOfComp]
220                 print "Generate a Corba vector integer field"
221                 fieldVectIntCorba = createCorbaFieldInt(familycorba,fieldVectIntLoc)
222                 AnalyzeField(fieldVectIntCorba)
223                 print ""
224 print "Fin du script Python ...."
225
226