Salome HOME
Fix for improvement IPAL9815 : Remove "Add Port" functionality as duplicate of "Edit...
[modules/superv.git] / examples / dataflow_calculator_example.py
1 #%dataflow_calculator_example.py%
2 #==============================================================================
3 #  File      : dataflow_calculator_example.py
4 #  Created   : 30 sept 2002
5 #  Author    : Laurent DADA
6 #  Project   : SALOME
7 #  Copyright : CEA
8 #==============================================================================
9
10 #==============================================================================
11 #  Example of dataflow with CalculatorComponent services (Add, Mult and Const)
12 #  and MED data (mesh and field).
13 #
14 #  
15 #==============================================================================
16
17 #==============================================================================
18 import batchmode_salome
19 import SALOME_MED
20 from batchmode_SuperV import *
21 #==============================================================================
22 datadir = os.getenv("SALOMEPRO_SRC")
23 if len(datadir) != 0:
24         datadir=datadir+ "data/"
25 input_file  = datadir+'pointe.med'
26 print 'dataflow_calculator_example.py',input_file
27 str= os.getenv("HOME")
28 if str == None:
29         str = "/tmp"
30 output_file = str + 'test_dataflow_calculator.med'
31
32 export_xmlfile = str + "/my_dataflow_calculator_example.xml"
33
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 *
39
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 #==============================================================================
44
45 def PrintField(aField,until_index):
46     # check parameter
47     if aField is None:
48         print "PrintField() : aField is None "
49         return
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)
57     else:
58         nb_node = support.getNumberOfElements(SALOME_MED.MED_ALL_ELEMENTS);
59
60     if until_index > nb_node:
61         max_index = nb_node
62     else:
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)
73     icomp = 0
74     while icomp < nb_comp :
75         icomp = icomp + 1
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)
81         index = 0
82         while index < max_index :
83             index = index + 1
84             print "                    component %d index %d :%f"%(icomp,index,values[(index-1)+(icomp-1)*nb_comp])
85     print "------------------------------------------------"
86
87 #----------------------------------------------------------------------
88
89 med_comp.readStructFileWithFieldType(input_file, studyCurrent)
90
91 med_obj = getMedObjectFromStudy()
92
93 nbMeshes = med_obj.getNumberOfMeshes()
94
95 nbFields = med_obj.getNumberOfFields()
96
97 print ""
98 print "The med file ",input_file," has ",nbMeshes," Meshe(s) and ",nbFields," Field(s)"
99 print ""
100
101 mesh = getMeshObjectFromStudy(1)
102
103 name = mesh.getName()
104
105 nbNodes = mesh.getNumberOfNodes()
106
107 spaceDim = mesh.getSpaceDimension()
108
109 print "The mesh from the Study is ",name,".It is a ",spaceDim,"-D mesh and it has ",nbNodes,"Nodes"
110 print ""
111
112 fieldcelldouble = getFieldIntObjectFromStudy(1,1)
113 if (fieldcelldouble == None):
114     fieldcelldouble = getFieldDoubleObjectFromStudy(1,1)
115     print "The following field is a float (double) one"
116 else:
117     print "The following field is an integer one"
118
119 AnalyzeField(fieldcelldouble)
120
121 fieldnodedouble = getFieldIntObjectFromStudy(2,1)
122 if (fieldnodedouble == None):
123     fieldnodedouble = getFieldDoubleObjectFromStudy(2,1)
124     print "The following field is a float (double) one"
125 else:
126     print "The following field is an integer one"
127
128 AnalyzeField(fieldnodedouble)
129 #----------------------------------------------------------------------
130
131 #==============================================================================
132 # Building the Dataflow
133 #==============================================================================
134
135 myGraph = Graph("CalculatorDataflow")
136
137 # nodes and links
138 # -------------------
139
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")
143
144 write_initial     = myGraph.Node("Calculator","Calculator","writeMEDfile")
145 write_result      = myGraph.Node("Calculator","Calculator","writeMEDfile")
146
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") )
150
151
152 # machines ressources
153 # -------------------
154
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')
161
162 # validation and exporting (xml format)
163 # ---------------------------------
164 print myGraph.Export(export_xmlfile)
165
166 print myGraph.IsValid()
167
168 #==============================================================================
169 # Dataflow Input
170 #==============================================================================
171
172 # Ports Input
173 # ----------------------------
174
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)
179
180 # exporting with constant inputs (xml format)
181 # -------------------------------------------
182
183 print myGraph.Export(export_xmlfile)
184
185 # Other ports Input
186 # ----------------------------
187
188 print "Print fieldnodedouble"
189 PrintField(fieldnodedouble,20)
190
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)
194
195 print myGraph.IsExecutable()
196
197 #==============================================================================
198 # Running the Dataflow (asynchronous)
199 #==============================================================================
200
201 print myGraph.Run()
202
203 print myGraph.DoneW()
204
205 new_field   = scal_field_add.Port("return").ToAny().value()
206 print "Print new_field"
207 PrintField(new_field,20)
208
209
210
211