1 # Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 #%dataflow_calculator_example.py%
24 #==============================================================================
25 # File : dataflow_calculator_example.py
26 # Created : 30 sept 2002
27 # Author : Laurent DADA
29 #==============================================================================
30 #==============================================================================
31 # Example of dataflow with CalculatorComponent services (Add, Mult and Const)
32 # and MED data (mesh and field).
33 #==============================================================================
34 #==============================================================================
36 import batchmode_salome
38 from batchmode_SuperV import *
39 #==============================================================================
40 datadir = os.getenv("DATA_DIR")
42 datadir=datadir+ "/MedFiles/"
43 input_file = datadir+'pointe.med'
44 print 'dataflow_calculator_example.py',input_file
45 str= os.getenv("HOME")
48 output_file = str + 'test_dataflow_calculator.med'
50 export_xmlfile = str + "/my_dataflow_calculator_example.xml"
52 #==============================================================================
53 # Load mesh and field in MED component, register into current study and get
54 # a field under the following name : fieldnodedouble
55 #==============================================================================
56 from batchmode_MED import *
58 #==============================================================================
59 # Define a function for printing on Salome TUI the" until_index" first values
60 # for each component of a field (MED data)
61 #==============================================================================
63 def PrintField(aField,until_index):
66 print "PrintField() : aField is None "
68 name = aField.getName()
69 desc = aField.getDescription()
70 nb_comp = aField.getNumberOfComponents()
71 values = aField.getValue(SALOME_MED.MED_FULL_INTERLACE)
72 support = aField.getSupport()
73 if (support.isOnAllElements()):
74 nb_node = support.getMesh().getNumberOfElements(support.getEntity(),SALOME_MED.MED_ALL_ELEMENTS)
76 nb_node = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS);
78 if until_index > nb_node:
81 max_index = until_index
82 time = aField.getTime()
83 iter = aField.getIterationNumber()
84 print "------------------------------------------------"
85 print "Field %s"%(name)
86 print " Description : %s"%(desc)
87 print " Number of components : %d"%(nb_comp)
88 print " Number of nodes : %d"%(nb_node)
89 print " Iteration number : %d"%(iter)
90 print " Time : %f"%(time)
92 while icomp < nb_comp :
94 namec = aField.getComponentName(icomp)
95 unit = aField.getComponentUnit(icomp)
96 print " Component : %s"%(namec)
97 print " Unit : %s"%(type)
98 print " first %d values :"%(until_index)
100 while index < max_index :
102 print " component %d index %d :%f"%(icomp,index,values[(index-1)+(icomp-1)*nb_comp])
103 print "------------------------------------------------"
105 #----------------------------------------------------------------------
107 med_comp.readStructFileWithFieldType(input_file, studyCurrent)
109 med_obj = getMedObjectFromStudy()
111 nbMeshes = med_obj.getNumberOfMeshes()
113 nbFields = med_obj.getNumberOfFields()
116 print "The med file ",input_file," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
119 mesh = getMeshObjectFromStudy(1)
121 name = mesh.getName()
123 nbNodes = mesh.getNumberOfNodes()
125 spaceDim = mesh.getSpaceDimension()
127 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes,"Nodes"
130 fieldcelldouble = getFieldIntObjectFromStudy(1,1)
131 if (fieldcelldouble == None):
132 fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
133 print "The following field is a float (double) one"
135 print "The following field is an integer one"
137 AnalyzeField(fieldcelldouble)
139 fieldnodedouble = getFieldIntObjectFromStudy(2,1)
140 if (fieldnodedouble == None):
141 fieldnodedouble = getFieldDoubleObjectFromStudy(2,1)
142 print "The following field is a float (double) one"
144 print "The following field is an integer one"
146 AnalyzeField(fieldnodedouble)
147 #----------------------------------------------------------------------
149 #==============================================================================
150 # Building the Dataflow
151 #==============================================================================
153 myGraph = Graph("CalculatorDataflow")
156 # -------------------
158 scal_field_const = myGraph.Node("Calculator","Calculator","Constant")
159 scal_field_mult = myGraph.Node("Calculator","Calculator","Mul")
160 scal_field_add = myGraph.Node("Calculator","Calculator","Add")
162 write_initial = myGraph.Node("Calculator","Calculator","writeMEDfile")
163 write_result = myGraph.Node("Calculator","Calculator","writeMEDfile")
165 link1 = myGraph.Link( scal_field_const.Port("return") , scal_field_add.Port("field1") )
166 link2 = myGraph.Link( scal_field_mult.Port("return") , scal_field_add.Port("field2") )
167 link3 = myGraph.Link( scal_field_add.Port("return") , write_result.Port("field1") )
170 # machines ressources
171 # -------------------
173 #myGraph.SetContainer('FactoryServer')
174 scal_field_const.SetContainer('FactoryServer')
175 scal_field_mult.SetContainer('FactoryServer')
176 scal_field_add.SetContainer('FactoryServer')
177 write_initial.SetContainer('FactoryServer')
178 write_result.SetContainer('FactoryServer')
180 # validation and exporting (xml format)
181 # ---------------------------------
182 print myGraph.Export(export_xmlfile)
184 print myGraph.IsValid()
186 #==============================================================================
188 #==============================================================================
191 # ----------------------------
193 scal_const_in2 = scal_field_const.Input( "x1", 10. )
194 scal_mult_in2 = scal_field_mult.Input( "x1", -1. )
195 result_write = write_result.Input( "filename", output_file)
196 initial_write_in2 = write_initial.Input( "filename", output_file)
198 # exporting with constant inputs (xml format)
199 # -------------------------------------------
201 print myGraph.Export(export_xmlfile)
204 # ----------------------------
206 print "Print fieldnodedouble"
207 PrintField(fieldnodedouble,20)
209 scal_const_in1 = scal_field_const.Input( "field1", fieldnodedouble )
210 scal_mult_in1 = scal_field_mult.Input( "field1", fieldnodedouble )
211 initial_write_in1 = write_initial.Input( "field1", fieldnodedouble)
213 print myGraph.IsExecutable()
215 #==============================================================================
216 # Running the Dataflow (asynchronous)
217 #==============================================================================
221 print myGraph.DoneW()
223 new_field = scal_field_add.Port("return").ToAny().value()
224 print "Print new_field"
225 PrintField(new_field,20)