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