1 # Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #%dataflow_calculator_example.py%
23 #==============================================================================
24 # File : dataflow_calculator_example.py
25 # Created : 30 sept 2002
26 # Author : Laurent DADA
28 #==============================================================================
29 #==============================================================================
30 # Example of dataflow with CalculatorComponent services (Add, Mult and Const)
31 # and MED data (mesh and field).
32 #==============================================================================
33 #==============================================================================
35 import batchmode_salome
37 from batchmode_SuperV import *
38 #==============================================================================
39 datadir = os.getenv("DATA_DIR")
41 datadir=datadir+ "/MedFiles/"
42 input_file = datadir+'pointe.med'
43 print 'dataflow_calculator_example.py',input_file
44 str= os.getenv("HOME")
47 output_file = str + 'test_dataflow_calculator.med'
49 export_xmlfile = str + "/my_dataflow_calculator_example.xml"
51 #==============================================================================
52 # Load mesh and field in MED component, register into current study and get
53 # a field under the following name : fieldnodedouble
54 #==============================================================================
55 from batchmode_MED import *
57 #==============================================================================
58 # Define a function for printing on Salome TUI the" until_index" first values
59 # for each component of a field (MED data)
60 #==============================================================================
62 def PrintField(aField,until_index):
65 print "PrintField() : aField is None "
67 name = aField.getName()
68 desc = aField.getDescription()
69 nb_comp = aField.getNumberOfComponents()
70 values = aField.getValue(SALOME_MED.MED_FULL_INTERLACE)
71 support = aField.getSupport()
72 if (support.isOnAllElements()):
73 nb_node = support.getMesh().getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
75 nb_node = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS);
77 if until_index > nb_node:
80 max_index = until_index
81 time = aField.getTime()
82 iter = aField.getIterationNumber()
83 print "------------------------------------------------"
84 print "Field %s"%(name)
85 print " Description : %s"%(desc)
86 print " Number of components : %d"%(nb_comp)
87 print " Number of nodes : %d"%(nb_node)
88 print " Iteration number : %d"%(iter)
89 print " Time : %f"%(time)
91 while icomp < nb_comp :
93 namec = aField.getComponentName(icomp)
94 unit = aField.getComponentUnit(icomp)
95 print " Component : %s"%(namec)
96 print " Unit : %s"%(type)
97 print " first %d values :"%(until_index)
99 while index < max_index :
101 print " component %d index %d :%f"%(icomp,index,values[(index-1)+(icomp-1)*nb_comp])
102 print "------------------------------------------------"
104 #----------------------------------------------------------------------
106 med_comp.readStructFileWithFieldType(input_file, studyCurrent)
108 med_obj = getMedObjectFromStudy()
110 nbMeshes = med_obj.getNumberOfMeshes()
112 nbFields = med_obj.getNumberOfFields()
115 print "The med file ",input_file," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
118 mesh = getMeshObjectFromStudy(1)
120 name = mesh.getName()
122 nbNodes = mesh.getNumberOfNodes()
124 spaceDim = mesh.getSpaceDimension()
126 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes,"Nodes"
129 fieldcelldouble = getFieldIntObjectFromStudy(1,1)
130 if (fieldcelldouble == None):
131 fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
132 print "The following field is a float (double) one"
134 print "The following field is an integer one"
136 AnalyzeField(fieldcelldouble)
138 fieldnodedouble = getFieldIntObjectFromStudy(2,1)
139 if (fieldnodedouble == None):
140 fieldnodedouble = getFieldDoubleObjectFromStudy(2,1)
141 print "The following field is a float (double) one"
143 print "The following field is an integer one"
145 AnalyzeField(fieldnodedouble)
146 #----------------------------------------------------------------------
148 #==============================================================================
149 # Building the Dataflow
150 #==============================================================================
152 myGraph = Graph("CalculatorDataflow")
155 # -------------------
157 scal_field_const = myGraph.Node("Calculator","Calculator","Constant")
158 scal_field_mult = myGraph.Node("Calculator","Calculator","Mul")
159 scal_field_add = myGraph.Node("Calculator","Calculator","Add")
161 write_initial = myGraph.Node("Calculator","Calculator","writeMEDfile")
162 write_result = myGraph.Node("Calculator","Calculator","writeMEDfile")
164 link1 = myGraph.Link( scal_field_const.Port("return") , scal_field_add.Port("field1") )
165 link2 = myGraph.Link( scal_field_mult.Port("return") , scal_field_add.Port("field2") )
166 link3 = myGraph.Link( scal_field_add.Port("return") , write_result.Port("field1") )
169 # machines ressources
170 # -------------------
172 #myGraph.SetContainer('FactoryServer')
173 scal_field_const.SetContainer('FactoryServer')
174 scal_field_mult.SetContainer('FactoryServer')
175 scal_field_add.SetContainer('FactoryServer')
176 write_initial.SetContainer('FactoryServer')
177 write_result.SetContainer('FactoryServer')
179 # validation and exporting (xml format)
180 # ---------------------------------
181 print myGraph.Export(export_xmlfile)
183 print myGraph.IsValid()
185 #==============================================================================
187 #==============================================================================
190 # ----------------------------
192 scal_const_in2 = scal_field_const.Input( "x1", 10. )
193 scal_mult_in2 = scal_field_mult.Input( "x1", -1. )
194 result_write = write_result.Input( "filename", output_file)
195 initial_write_in2 = write_initial.Input( "filename", output_file)
197 # exporting with constant inputs (xml format)
198 # -------------------------------------------
200 print myGraph.Export(export_xmlfile)
203 # ----------------------------
205 print "Print fieldnodedouble"
206 PrintField(fieldnodedouble,20)
208 scal_const_in1 = scal_field_const.Input( "field1", fieldnodedouble )
209 scal_mult_in1 = scal_field_mult.Input( "field1", fieldnodedouble )
210 initial_write_in1 = write_initial.Input( "field1", fieldnodedouble)
212 print myGraph.IsExecutable()
214 #==============================================================================
215 # Running the Dataflow (asynchronous)
216 #==============================================================================
220 print myGraph.DoneW()
222 new_field = scal_field_add.Port("return").ToAny().value()
223 print "Print new_field"
224 PrintField(new_field,20)