1 #%dataflow_calculator_example.py%
2 #==============================================================================
3 # File : dataflow_calculator_example.py
4 # Created : 30 sept 2002
5 # Author : Laurent DADA
8 #==============================================================================
10 #==============================================================================
11 # Example of dataflow with CalculatorComponent services (Add, Mult and Const)
12 # and MED data (mesh and field).
15 #==============================================================================
17 #==============================================================================
18 import batchmode_salome
20 from batchmode_SuperV import *
21 #==============================================================================
22 datadir = os.getenv("SALOMEPRO_SRC")
24 datadir=datadir+ "data/"
25 input_file = datadir+'pointe.med'
26 print 'dataflow_calculator_example.py',input_file
27 str= os.getenv("HOME")
30 output_file = str + 'test_dataflow_calculator.med'
32 export_xmlfile = str + "/my_dataflow_calculator_example.xml"
34 #==============================================================================
35 # Load mesh and field in MED component, register into current study and get
36 # a field under the following name : fieldnodedouble
37 #==============================================================================
38 from batchmode_MED import *
40 #==============================================================================
41 # Define a function for printing on Salome TUI the" until_index" first values
42 # for each component of a field (MED data)
43 #==============================================================================
45 def PrintField(aField,until_index):
48 print "PrintField() : aField is None "
50 name = aField.getName()
51 desc = aField.getDescription()
52 nb_comp = aField.getNumberOfComponents()
53 values = aField.getValue(SALOME_MED.MED_FULL_INTERLACE)
54 support = aField.getSupport()
55 if (support.isOnAllElements()):
56 nb_node = support.getMesh().getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
58 nb_node = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS);
60 if until_index > nb_node:
63 max_index = until_index
64 time = aField.getTime()
65 iter = aField.getIterationNumber()
66 print "------------------------------------------------"
67 print "Field %s"%(name)
68 print " Description : %s"%(desc)
69 print " Number of components : %d"%(nb_comp)
70 print " Number of nodes : %d"%(nb_node)
71 print " Iteration number : %d"%(iter)
72 print " Time : %f"%(time)
74 while icomp < nb_comp :
76 namec = aField.getComponentName(icomp)
77 unit = aField.getComponentUnit(icomp)
78 print " Component : %s"%(namec)
79 print " Unit : %s"%(type)
80 print " first %d values :"%(until_index)
82 while index < max_index :
84 print " component %d index %d :%f"%(icomp,index,values[(index-1)+(icomp-1)*nb_comp])
85 print "------------------------------------------------"
87 #----------------------------------------------------------------------
89 med_comp.readStructFileWithFieldType(input_file, studyCurrent)
91 med_obj = getMedObjectFromStudy()
93 nbMeshes = med_obj.getNumberOfMeshes()
95 nbFields = med_obj.getNumberOfFields()
98 print "The med file ",input_file," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
101 mesh = getMeshObjectFromStudy(1)
103 name = mesh.getName()
105 nbNodes = mesh.getNumberOfNodes()
107 spaceDim = mesh.getSpaceDimension()
109 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes,"Nodes"
112 fieldcelldouble = getFieldIntObjectFromStudy(1,1)
113 if (fieldcelldouble == None):
114 fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
115 print "The following field is a float (double) one"
117 print "The following field is an integer one"
119 AnalyzeField(fieldcelldouble)
121 fieldnodedouble = getFieldIntObjectFromStudy(2,1)
122 if (fieldnodedouble == None):
123 fieldnodedouble = getFieldDoubleObjectFromStudy(2,1)
124 print "The following field is a float (double) one"
126 print "The following field is an integer one"
128 AnalyzeField(fieldnodedouble)
129 #----------------------------------------------------------------------
131 #==============================================================================
132 # Building the Dataflow
133 #==============================================================================
135 myGraph = Graph("CalculatorDataflow")
138 # -------------------
140 scal_field_const = myGraph.Node("Calculator","Calculator","Constant")
141 scal_field_mult = myGraph.Node("Calculator","Calculator","Mul")
142 scal_field_add = myGraph.Node("Calculator","Calculator","Add")
144 write_initial = myGraph.Node("Calculator","Calculator","writeMEDfile")
145 write_result = myGraph.Node("Calculator","Calculator","writeMEDfile")
147 link1 = myGraph.Link( scal_field_const.Port("return") , scal_field_add.Port("field1") )
148 link2 = myGraph.Link( scal_field_mult.Port("return") , scal_field_add.Port("field2") )
149 link3 = myGraph.Link( scal_field_add.Port("return") , write_result.Port("field1") )
152 # machines ressources
153 # -------------------
155 #myGraph.SetContainer('FactoryServer')
156 scal_field_const.SetContainer('FactoryServer')
157 scal_field_mult.SetContainer('FactoryServer')
158 scal_field_add.SetContainer('FactoryServer')
159 write_initial.SetContainer('FactoryServer')
160 write_result.SetContainer('FactoryServer')
162 # validation and exporting (xml format)
163 # ---------------------------------
164 print myGraph.Export(export_xmlfile)
166 print myGraph.IsValid()
168 #==============================================================================
170 #==============================================================================
173 # ----------------------------
175 scal_const_in2 = scal_field_const.Input( "x1", 10. )
176 scal_mult_in2 = scal_field_mult.Input( "x1", -1. )
177 result_write = write_result.Input( "filename", output_file)
178 initial_write_in2 = write_initial.Input( "filename", output_file)
180 # exporting with constant inputs (xml format)
181 # -------------------------------------------
183 print myGraph.Export(export_xmlfile)
186 # ----------------------------
188 print "Print fieldnodedouble"
189 PrintField(fieldnodedouble,20)
191 scal_const_in1 = scal_field_const.Input( "field1", fieldnodedouble )
192 scal_mult_in1 = scal_field_mult.Input( "field1", fieldnodedouble )
193 initial_write_in1 = write_initial.Input( "field1", fieldnodedouble)
195 print myGraph.IsExecutable()
197 #==============================================================================
198 # Running the Dataflow (asynchronous)
199 #==============================================================================
203 print myGraph.DoneW()
205 new_field = scal_field_add.Port("return").ToAny().value()
206 print "Print new_field"
207 PrintField(new_field,20)