Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/datafiles.git] / Superv / Python / dataflow_calculator_example.py
1 #  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 #%dataflow_calculator_example.py%
23 #==============================================================================
24 #  File      : dataflow_calculator_example.py
25 #  Created   : 30 sept 2002
26 #  Author    : Laurent DADA
27 #  Project   : SALOME
28 #==============================================================================
29 #==============================================================================
30 #  Example of dataflow with CalculatorComponent services (Add, Mult and Const)
31 #  and MED data (mesh and field).
32 #==============================================================================
33 #==============================================================================
34 #
35 import batchmode_salome
36 import SALOME_MED
37 from batchmode_SuperV import *
38 #==============================================================================
39 datadir = os.getenv("DATA_DIR")
40 if len(datadir) != 0:
41         datadir=datadir+ "/MedFiles/"
42 input_file  = datadir+'pointe.med'
43 print 'dataflow_calculator_example.py',input_file
44 str= os.getenv("HOME")
45 if str == None:
46         str = "/tmp"
47 output_file = str + 'test_dataflow_calculator.med'
48
49 export_xmlfile = str + "/my_dataflow_calculator_example.xml"
50
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 *
56
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 #==============================================================================
61
62 def PrintField(aField,until_index):
63     # check parameter
64     if aField is None:
65         print "PrintField() : aField is None "
66         return
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)
74     else:
75         nb_node = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS);
76
77     if until_index > nb_node:
78         max_index = nb_node
79     else:
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)
90     icomp = 0
91     while icomp < nb_comp :
92         icomp = icomp + 1
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)
98         index = 0
99         while index < max_index :
100             index = index + 1
101             print "                    component %d index %d :%f"%(icomp,index,values[(index-1)+(icomp-1)*nb_comp])
102     print "------------------------------------------------"
103
104 #----------------------------------------------------------------------
105
106 med_comp.readStructFileWithFieldType(input_file, studyCurrent)
107
108 med_obj = getMedObjectFromStudy()
109
110 nbMeshes = med_obj.getNumberOfMeshes()
111
112 nbFields = med_obj.getNumberOfFields()
113
114 print ""
115 print "The med file ",input_file," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
116 print ""
117
118 mesh = getMeshObjectFromStudy(1)
119
120 name = mesh.getName()
121
122 nbNodes = mesh.getNumberOfNodes()
123
124 spaceDim = mesh.getSpaceDimension()
125
126 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes,"Nodes"
127 print ""
128
129 fieldcelldouble = getFieldIntObjectFromStudy(1,1)
130 if (fieldcelldouble == None):
131     fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
132     print "The following field is a float (double) one"
133 else:
134     print "The following field is an integer one"
135
136 AnalyzeField(fieldcelldouble)
137
138 fieldnodedouble = getFieldIntObjectFromStudy(2,1)
139 if (fieldnodedouble == None):
140     fieldnodedouble = getFieldDoubleObjectFromStudy(2,1)
141     print "The following field is a float (double) one"
142 else:
143     print "The following field is an integer one"
144
145 AnalyzeField(fieldnodedouble)
146 #----------------------------------------------------------------------
147
148 #==============================================================================
149 # Building the Dataflow
150 #==============================================================================
151
152 myGraph = Graph("CalculatorDataflow")
153
154 # nodes and links
155 # -------------------
156
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")
160
161 write_initial     = myGraph.Node("Calculator","Calculator","writeMEDfile")
162 write_result      = myGraph.Node("Calculator","Calculator","writeMEDfile")
163
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") )
167
168
169 # machines ressources
170 # -------------------
171
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')
178
179 # validation and exporting (xml format)
180 # ---------------------------------
181 print myGraph.Export(export_xmlfile)
182
183 print myGraph.IsValid()
184
185 #==============================================================================
186 # Dataflow Input
187 #==============================================================================
188
189 # Ports Input
190 # ----------------------------
191
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)
196
197 # exporting with constant inputs (xml format)
198 # -------------------------------------------
199
200 print myGraph.Export(export_xmlfile)
201
202 # Other ports Input
203 # ----------------------------
204
205 print "Print fieldnodedouble"
206 PrintField(fieldnodedouble,20)
207
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)
211
212 print myGraph.IsExecutable()
213
214 #==============================================================================
215 # Running the Dataflow (asynchronous)
216 #==============================================================================
217
218 print myGraph.Run()
219
220 print myGraph.DoneW()
221
222 new_field   = scal_field_add.Port("return").ToAny().value()
223 print "Print new_field"
224 PrintField(new_field,20)
225
226
227
228