3 # Copyright (C) 2008-2024 EDF R&D
5 # This file is part of SALOME ADAO module
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 # Author: Andre Ribes, andre.ribes@edf.fr, EDF R&D
33 from daYacsSchemaCreator.infos_daComposant import *
35 def _Internal_Add_dir_script_ports(node, sf, ed, br, t_type):
36 # On conserve le pointeur de "node" et "t_type"
37 __scriptfile = str( sf )
38 __exist_dir = bool( ed )
39 __base_dir = str( br )
40 __full_name = os.path.join(__base_dir, os.path.basename(__scriptfile))
41 if os.path.exists(__full_name):
42 node.getInputPort("script").edInitPy(__full_name)
44 node.getInputPort("script").edInitPy(__scriptfile)
46 node.edAddInputPort("studydir", t_type)
47 node.getInputPort("studydir").edInitPy(__base_dir)
49 def create_yacs_proc(study_config):
51 logging.debug("[create_yacs_proc]")
54 SALOMERuntime.RuntimeSALOME_setRuntime()
55 l = loader.YACSLoader()
56 l.registerProcCataLoader()
57 runtime = pilot.getRuntime()
59 catalogAd = runtime.loadCatalog("proc", os.environ["ADAO_ENGINE_ROOT_DIR"] + "/share/resources/adao/ADAOSchemaCatalog.xml")
60 runtime.addCatalog(catalogAd)
62 raise ValueError("Exception in loading ADAO YACS Schema catalog")
64 # Starting creating proc
65 proc = runtime.createProc("proc")
66 proc.setTypeCode("pyobj", runtime.getTypeCode("pyobj"))
67 proc.setTypeCode("SALOME_TYPES/ParametricInput", catalogAd._typeMap["SALOME_TYPES/ParametricInput"])
68 proc.setTypeCode("SALOME_TYPES/ParametricOutput", catalogAd._typeMap["SALOME_TYPES/ParametricOutput"])
69 t_pyobj = proc.getTypeCode("pyobj")
70 t_string = proc.getTypeCode("string")
71 t_bool = proc.getTypeCode("bool")
72 t_param_input = proc.getTypeCode("SALOME_TYPES/ParametricInput")
73 t_param_output = proc.getTypeCode("SALOME_TYPES/ParametricOutput")
74 if "Repertory" in list(study_config.keys()):
75 base_repertory = study_config["Repertory"]
80 if "ExecuteInContainer" in list(study_config.keys()):
81 if study_config["ExecuteInContainer"] == "Mono":
82 ExecuteInContainer = True
83 ExecuteInModeMulti = False
84 elif study_config["ExecuteInContainer"] == "Multi":
85 ExecuteInContainer = True
86 ExecuteInModeMulti = True
88 ExecuteInContainer = False
89 ExecuteInModeMulti = False
91 ExecuteInContainer = False
92 ExecuteInModeMulti = False
93 if ExecuteInContainer and bool(UseSeparateContainer):
94 mycontainer = proc.createContainer("AdaoContainer")
95 if ExecuteInModeMulti:
96 # type : multi pour creer un nouveau container pour chaque noeud
97 # type : mono pour réutiliser le même container (defaut)
98 mycontainer.setProperty("type","multi")
100 # Create ADAO case bloc
101 ADAO_Case = runtime.createBloc("ADAO_Case_Bloc")
102 proc.edAddChild(ADAO_Case)
104 # Step 0: create assimilation study object
105 factory_CAS_node = catalogAd.getNodeFromNodeMap("CreateAssimilationStudy")
106 CAS_node = factory_CAS_node.cloneNode("CreateAssimilationStudy")
107 CAS_node.getInputPort("Name").edInitPy(study_config["Name"])
108 CAS_node.getInputPort("Algorithm").edInitPy(study_config["Algorithm"])
109 if "Debug" in study_config and study_config["Debug"] == "1":
110 CAS_node.getInputPort("Debug").edInitPy(True)
112 CAS_node.getInputPort("Debug").edInitPy(False)
114 # Ajout des Variables
115 InputVariablesNames = []
116 InputVariablesSizes = []
117 for var in study_config["InputVariables"]["Order"]:
118 InputVariablesNames.append(var)
119 InputVariablesSizes.append(int(study_config["InputVariables"][var]))
120 CAS_node.getInputPort("InputVariablesNames").edInitPy(InputVariablesNames)
121 CAS_node.getInputPort("InputVariablesSizes").edInitPy(InputVariablesSizes)
122 OutputVariablesNames = []
123 OutputVariablesSizes = []
124 for var in study_config["OutputVariables"]["Order"]:
125 OutputVariablesNames.append(var)
126 OutputVariablesSizes.append(int(study_config["OutputVariables"][var]))
127 CAS_node.getInputPort("OutputVariablesNames").edInitPy(OutputVariablesNames)
128 CAS_node.getInputPort("OutputVariablesSizes").edInitPy(OutputVariablesSizes)
130 ADAO_Case.edAddChild(CAS_node)
131 if ExecuteInContainer and bool(UseSeparateContainer):
132 CAS_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
133 CAS_node.setContainer(mycontainer)
135 # Add an observer init node if an user defines some
136 factory_init_observers_node = catalogAd.getNodeFromNodeMap("SetObserversNode")
137 init_observers_node = factory_init_observers_node.cloneNode("SetObservers")
138 if "Observers" in list(study_config.keys()):
139 node_script = init_observers_node.getScript()
140 node_script += "has_observers = True\n"
141 node_script += "observers = " + str(study_config["Observers"]) + "\n"
142 init_observers_node.setScript(node_script)
143 ADAO_Case.edAddChild(init_observers_node)
144 ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("has_observers"), CAS_node.getInputPort("has_observers"))
145 ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("observers"), CAS_node.getInputPort("observers"))
147 node_script = init_observers_node.getScript()
148 node_script += "has_observers = False\n"
149 node_script += "observers = \"\"\n"
150 init_observers_node.setScript(node_script)
151 ADAO_Case.edAddChild(init_observers_node)
152 ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("has_observers"), CAS_node.getInputPort("has_observers"))
153 ADAO_Case.edAddDFLink(init_observers_node.getOutputPort("observers"), CAS_node.getInputPort("observers"))
154 if ExecuteInContainer and bool(UseSeparateContainer):
155 init_observers_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
156 init_observers_node.setContainer(mycontainer)
158 # Step 0.5: Find if there is a user init node
160 init_config["Target"] = []
161 if "UserDataInit" in list(study_config.keys()):
162 init_config = study_config["UserDataInit"]
163 factory_init_node = catalogAd.getNodeFromNodeMap("UserDataInitFromScript")
164 init_node = factory_init_node.cloneNode("UserDataInit")
165 _Internal_Add_dir_script_ports( init_node, init_config["Data"], repertory, base_repertory, t_string)
166 init_node_script = init_node.getScript()
167 init_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
168 init_node_script += "init_data = user_script_module.init_data\n"
169 init_node.setScript(init_node_script)
170 ADAO_Case.edAddChild(init_node)
171 if ExecuteInContainer and bool(UseSeparateContainer):
172 init_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
173 init_node.setContainer(mycontainer)
175 # Step 1: get input data from user configuration
177 st_keys = sorted(list(study_config.keys()))
179 ad_keys = sorted(AssimData)
181 data_config = study_config[key]
183 key_type = key + "Type"
184 key_stored = key + "Stored"
186 if data_config["Type"] == "Dict" and data_config["From"] == "Script":
188 factory_back_node = catalogAd.getNodeFromNodeMap("CreateDictFromScript")
189 back_node = factory_back_node.cloneNode("Get" + key)
190 _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
191 back_node.edAddOutputPort(key, t_pyobj)
192 ADAO_Case.edAddChild(back_node)
193 if ExecuteInContainer and bool(UseSeparateContainer):
194 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
195 back_node.setContainer(mycontainer)
196 # Set content of the node
197 back_node_script = back_node.getScript()
198 if key in init_config["Target"]:
199 # Connect node with InitUserData
200 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
201 back_node.edAddInputPort("init_data", t_pyobj)
202 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
203 back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
204 if key == "AlgorithmParameters":
205 back_node_script += "if hasattr(user_script_module,'" + key + "'):\n "
206 back_node_script += key + " = user_script_module." + key + "\n"
208 back_node_script += "elif hasattr(user_script_module,'" + skey + "'):\n "
209 back_node_script += key + " = user_script_module." + skey + "\n"
211 back_node_script += key + " = user_script_module." + key + "\n"
212 back_node.setScript(back_node_script)
213 # Connect node with CreateAssimilationStudy
214 CAS_node.edAddInputPort(key, t_pyobj)
215 ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
217 if data_config["Type"] == "Dict" and data_config["From"] == "String":
219 factory_back_node = catalogAd.getNodeFromNodeMap("CreateDictFromString")
220 back_node = factory_back_node.cloneNode("Get" + key)
221 back_node.getInputPort("dict_in_string").edInitPy(data_config["Data"])
222 back_node.edAddOutputPort(key, t_pyobj)
223 ADAO_Case.edAddChild(back_node)
224 if ExecuteInContainer and bool(UseSeparateContainer):
225 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
226 back_node.setContainer(mycontainer)
227 # Set content of the node
228 back_node_script = back_node.getScript()
229 if key in init_config["Target"]:
230 # Connect node with InitUserData
231 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
232 back_node.edAddInputPort("init_data", t_pyobj)
233 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
234 back_node_script += key + " = dict(dico)\n"
235 back_node_script += "logging.debug(\"Dict is %ss\"%s%s)"%("%","%",key)
236 back_node.setScript(back_node_script)
237 # Connect node with CreateAssimilationStudy
238 CAS_node.edAddInputPort(key, t_pyobj)
239 ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
241 if data_config["Type"] == "Vector" and data_config["From"] == "String":
243 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorFromString")
244 back_node = factory_back_node.cloneNode("Get" + key)
245 back_node.getInputPort("vector_in_string").edInitPy(data_config["Data"])
246 ADAO_Case.edAddChild(back_node)
247 if ExecuteInContainer and bool(UseSeparateContainer):
248 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
249 back_node.setContainer(mycontainer)
250 # Set content of the node
251 back_node_script = back_node.getScript()
252 if "Stored" in data_config:
253 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
255 back_node_script += "stored = 0\n"
256 if key in init_config["Target"]:
257 # Connect node with InitUserData
258 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
259 back_node.edAddInputPort("init_data", t_pyobj)
260 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
261 back_node.setScript(back_node_script)
262 # Connect node with CreateAssimilationStudy
263 CAS_node.edAddInputPort(key, t_pyobj)
264 CAS_node.edAddInputPort(key_type, t_string)
265 CAS_node.edAddInputPort(key_stored, t_bool)
266 ADAO_Case.edAddDFLink(back_node.getOutputPort("vector"), CAS_node.getInputPort(key))
267 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
268 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
270 if data_config["Type"] == "Vector" and data_config["From"] == "Script":
272 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorFromScript")
273 back_node = factory_back_node.cloneNode("Get" + key)
274 _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
275 back_node.edAddOutputPort(key, t_pyobj)
276 ADAO_Case.edAddChild(back_node)
277 if ExecuteInContainer and bool(UseSeparateContainer):
278 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
279 back_node.setContainer(mycontainer)
280 # Set content of the node
281 back_node_script = back_node.getScript()
282 if key in init_config["Target"]:
283 # Connect node with InitUserData
284 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
285 back_node.edAddInputPort("init_data", t_pyobj)
286 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
287 back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
288 back_node_script += key + " = user_script_module." + key + "\n"
289 if "Stored" in data_config:
290 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
292 back_node_script += "stored = 0\n"
293 back_node.setScript(back_node_script)
294 # Connect node with CreateAssimilationStudy
295 CAS_node.edAddInputPort(key, t_pyobj)
296 CAS_node.edAddInputPort(key_type, t_string)
297 CAS_node.edAddInputPort(key_stored, t_bool)
298 ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
299 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
300 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
302 if data_config["Type"] == "Vector" and data_config["From"] == "DataFile":
304 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorFromDataFile")
305 back_node = factory_back_node.cloneNode("Get" + key)
306 back_node.getInputPort("script").edInitPy(data_config["Data"])
307 back_node.getInputPort("columns").edInitPy((key,)) # On impose le concept, et le schéma YACS est ammendable
308 if "ColMajor" in data_config:
309 colmajor = bool(data_config["ColMajor"])
312 back_node.getInputPort("colmajor").edInitPy(colmajor) # On impose le concept, et le schéma YACS est ammendable
313 ADAO_Case.edAddChild(back_node)
314 if ExecuteInContainer and bool(UseSeparateContainer):
315 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
316 back_node.setContainer(mycontainer)
317 # Set content of the node
318 back_node_script = back_node.getScript()
319 if "Stored" in data_config:
320 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
322 back_node_script += "stored = 0\n"
323 if key in init_config["Target"]:
324 # Connect node with InitUserData
325 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
326 back_node.edAddInputPort("init_data", t_pyobj)
327 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
328 back_node.setScript(back_node_script)
329 # Connect node with CreateAssimilationStudy
330 CAS_node.edAddInputPort(key, t_pyobj)
331 CAS_node.edAddInputPort(key_type, t_string)
332 CAS_node.edAddInputPort(key_stored, t_bool)
333 ADAO_Case.edAddDFLink(back_node.getOutputPort("vector"), CAS_node.getInputPort(key))
334 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
335 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
337 if data_config["Type"] == "VectorSerie" and data_config["From"] == "String":
339 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorSerieFromString")
340 back_node = factory_back_node.cloneNode("Get" + key)
341 back_node.getInputPort("vector_in_string").edInitPy(data_config["Data"])
342 ADAO_Case.edAddChild(back_node)
343 if ExecuteInContainer and bool(UseSeparateContainer):
344 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
345 back_node.setContainer(mycontainer)
346 # Set content of the node
347 back_node_script = back_node.getScript()
348 if "Stored" in data_config:
349 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
351 back_node_script += "stored = 0\n"
352 if key in init_config["Target"]:
353 # Connect node with InitUserData
354 back_node_script += "__builtins__[\"init_data\"] = init_data\n" + back_node_script
355 back_node.edAddInputPort("init_data", t_pyobj)
356 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
357 back_node.setScript(back_node_script)
358 # Connect node with CreateAssimilationStudy
359 CAS_node.edAddInputPort(key, t_pyobj)
360 CAS_node.edAddInputPort(key_type, t_string)
361 CAS_node.edAddInputPort(key_stored, t_bool)
362 ADAO_Case.edAddDFLink(back_node.getOutputPort("vector"), CAS_node.getInputPort(key))
363 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
364 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
366 if data_config["Type"] == "VectorSerie" and data_config["From"] == "Script":
368 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorSerieFromScript")
369 back_node = factory_back_node.cloneNode("Get" + key)
370 _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
371 back_node.edAddOutputPort(key, t_pyobj)
372 ADAO_Case.edAddChild(back_node)
373 if ExecuteInContainer and bool(UseSeparateContainer):
374 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
375 back_node.setContainer(mycontainer)
376 # Set content of the node
377 back_node_script = back_node.getScript()
378 if key in init_config["Target"]:
379 # Connect node with InitUserData
380 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
381 back_node.edAddInputPort("init_data", t_pyobj)
382 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
383 back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
384 back_node_script += key + " = user_script_module." + key + "\n"
385 if "Stored" in data_config:
386 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
388 back_node_script += "stored = 0\n"
389 back_node.setScript(back_node_script)
390 # Connect node with CreateAssimilationStudy
391 CAS_node.edAddInputPort(key, t_pyobj)
392 CAS_node.edAddInputPort(key_type, t_string)
393 CAS_node.edAddInputPort(key_stored, t_bool)
394 ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
395 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
396 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
398 if data_config["Type"] == "VectorSerie" and data_config["From"] == "DataFile":
400 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpyVectorSerieFromDataFile")
401 back_node = factory_back_node.cloneNode("Get" + key)
402 back_node.getInputPort("script").edInitPy(data_config["Data"])
403 back_node.getInputPort("columns").edInitPy(()) # On impose aucun nom et le schéma YACS est ammendable
404 if "ColMajor" in data_config:
405 colmajor = bool(data_config["ColMajor"])
408 back_node.getInputPort("colmajor").edInitPy(colmajor) # On impose le concept, et le schéma YACS est ammendable
409 ADAO_Case.edAddChild(back_node)
410 if ExecuteInContainer and bool(UseSeparateContainer):
411 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
412 back_node.setContainer(mycontainer)
413 # Set content of the node
414 back_node_script = back_node.getScript()
415 if "Stored" in data_config:
416 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
418 back_node_script += "stored = 0\n"
419 if key in init_config["Target"]:
420 # Connect node with InitUserData
421 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
422 back_node.edAddInputPort("init_data", t_pyobj)
423 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
424 back_node.setScript(back_node_script)
425 # Connect node with CreateAssimilationStudy
426 CAS_node.edAddInputPort(key, t_pyobj)
427 CAS_node.edAddInputPort(key_type, t_string)
428 CAS_node.edAddInputPort(key_stored, t_bool)
429 ADAO_Case.edAddDFLink(back_node.getOutputPort("vector"), CAS_node.getInputPort(key))
430 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
431 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
433 if data_config["Type"] in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix") and data_config["From"] == "String":
435 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpy%sFromString"%(data_config["Type"],))
436 back_node = factory_back_node.cloneNode("Get" + key)
437 back_node.getInputPort("matrix_in_string").edInitPy(data_config["Data"])
438 ADAO_Case.edAddChild(back_node)
439 if ExecuteInContainer and bool(UseSeparateContainer):
440 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
441 back_node.setContainer(mycontainer)
442 # Set content of the node
443 back_node_script = back_node.getScript()
444 if "Stored" in data_config:
445 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
447 back_node_script += "stored = 0\n"
448 if key in init_config["Target"]:
449 # Connect node with InitUserData
450 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
451 back_node.edAddInputPort("init_data", t_pyobj)
452 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
453 back_node.setScript(back_node_script)
454 # Connect node with CreateAssimilationStudy
455 CAS_node.edAddInputPort(key, t_pyobj)
456 CAS_node.edAddInputPort(key_type, t_string)
457 CAS_node.edAddInputPort(key_stored, t_bool)
458 ADAO_Case.edAddDFLink(back_node.getOutputPort("matrix"), CAS_node.getInputPort(key))
459 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
460 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
462 if data_config["Type"] in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix") and data_config["From"] == "Script":
464 factory_back_node = catalogAd.getNodeFromNodeMap("CreateNumpy%sFromScript"%(data_config["Type"],))
465 back_node = factory_back_node.cloneNode("Get" + key)
466 _Internal_Add_dir_script_ports( back_node, data_config["Data"], repertory, base_repertory, t_string)
467 back_node.edAddOutputPort(key, t_pyobj)
468 ADAO_Case.edAddChild(back_node)
469 if ExecuteInContainer and bool(UseSeparateContainer):
470 back_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
471 back_node.setContainer(mycontainer)
472 # Set content of the node
473 back_node_script = back_node.getScript()
474 if "Stored" in data_config:
475 back_node_script += "stored = " + str(data_config["Stored"]) + "\n"
477 back_node_script += "stored = 0\n"
478 if key in init_config["Target"]:
479 # Connect node with InitUserData
480 back_node_script += "__builtins__[\"init_data\"] = init_data\n"
481 back_node.edAddInputPort("init_data", t_pyobj)
482 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), back_node.getInputPort("init_data"))
483 back_node_script += "# Import script and get data\n__import__(module_name)\nuser_script_module = sys.modules[module_name]\n\n"
484 back_node_script += key + " = user_script_module." + key + "\n"
485 back_node.setScript(back_node_script)
486 # Connect node with CreateAssimilationStudy
487 CAS_node.edAddInputPort(key, t_pyobj)
488 CAS_node.edAddInputPort(key_type, t_string)
489 CAS_node.edAddInputPort(key_stored, t_bool)
490 ADAO_Case.edAddDFLink(back_node.getOutputPort(key), CAS_node.getInputPort(key))
491 ADAO_Case.edAddDFLink(back_node.getOutputPort("type"), CAS_node.getInputPort(key_type))
492 ADAO_Case.edAddDFLink(back_node.getOutputPort("stored"), CAS_node.getInputPort(key_stored))
494 if data_config["Type"] == "Function" and (key == "ObservationOperator" or key == "EvolutionModel"):
495 TheData = data_config["Data"]
496 for FunctionName in TheData["Function"]:
497 port_name = key + FunctionName
498 CAS_node.edAddInputPort(port_name, t_string)
499 if os.path.exists(os.path.join(base_repertory, os.path.basename(TheData["Script"][FunctionName]))):
500 CAS_node.getInputPort(port_name).edInitPy(os.path.join(base_repertory, os.path.basename(TheData["Script"][FunctionName])))
502 CAS_node.getInputPort(port_name).edInitPy(TheData["Script"][FunctionName])
504 CAS_node.edAddInputPort("studydir", t_string)
505 CAS_node.getInputPort("studydir").edInitPy(base_repertory)
508 # Step 3: create compute bloc
509 compute_bloc = runtime.createBloc("compute_bloc")
510 ADAO_Case.edAddChild(compute_bloc)
511 ADAO_Case.edAddCFLink(CAS_node, compute_bloc)
512 # We use an optimizer loop
513 name = "Execute" + study_config["Algorithm"]
514 if sys.version_info.major < 3:
515 algLib = "daYacsIntegration.py"
518 factoryName = "AssimilationAlgorithm_asynch"
519 #~ optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, "")
520 optimizer_node = runtime.createOptimizerLoop(name, algLib, factoryName, True)
521 compute_bloc.edAddChild(optimizer_node)
522 ADAO_Case.edAddDFLink(CAS_node.getOutputPort("Study"), optimizer_node.edGetAlgoInitPort())
524 # Check if we have a python script for OptimizerLoopNode
525 data_config = study_config["ObservationOperator"]
526 opt_script_nodeOO = None
527 if data_config["Type"] == "Function" and (data_config["From"] == "ScriptWithSwitch" or data_config["From"] == "FunctionDict"):
529 TheData = data_config["Data"]
531 for FunctionName in TheData["Function"]:
532 # We currently support only one file
533 script_filename = TheData["Script"][FunctionName]
535 # We create a new pyscript node
536 opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
537 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
538 script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
540 script_str= open(script_filename, 'r')
542 raise ValueError("Exception in opening function script file: " + script_filename)
543 node_script = "#-*- coding: utf-8 -*-\n"
544 node_script += "import sys, os \n"
545 node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
546 node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
547 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
548 node_script += " sys.path.insert(0,filepath)\n"
549 node_script += script_str.read()
550 opt_script_nodeOO.setScript(node_script)
551 opt_script_nodeOO.edAddInputPort("computation", t_param_input)
552 opt_script_nodeOO.edAddOutputPort("result", t_param_output)
554 elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithFunctions":
556 ScriptWithFunctions = data_config["Data"]
558 for FunctionName in ScriptWithFunctions["Function"]:
559 # We currently support only one file
560 script_filename = ScriptWithFunctions["Script"][FunctionName]
563 # We create a new pyscript node
564 opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
565 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
566 script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
568 script_str= open(script_filename, 'r')
570 raise ValueError("Exception in opening function script file: " + script_filename)
571 node_script = "#-*- coding: utf-8 -*-\n"
572 node_script += "import sys, os, numpy, logging, pickle, codecs\n"
573 node_script += "def loads( data ):\n"
574 node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n"
575 node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
576 node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
577 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
578 node_script += " sys.path.insert(0,filepath)\n"
579 node_script += """# ==============================================\n"""
580 node_script += script_str.read()
581 node_script += """# ==============================================\n"""
582 node_script += """__method = None\n"""
583 node_script += """for param in computation["specificParameters"]:\n"""
584 node_script += """ if param["name"] == "method": __method = loads(param["value"])\n"""
585 node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
586 node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
587 node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
588 node_script += """#\n"""
589 node_script += """#\n"""
590 node_script += """__data = []\n"""
591 node_script += """if __method == "Direct":\n"""
592 node_script += """ try:\n"""
593 node_script += """ DirectOperator\n"""
594 node_script += """ except NameError:\n"""
595 node_script += """ raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
596 node_script += """ logging.debug("ComputationFunctionNode: Direct computation")\n"""
597 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
598 node_script += """ __data = DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
599 node_script += """#\n"""
600 node_script += """if __method == "Tangent":\n"""
601 node_script += """ try:\n"""
602 node_script += """ TangentOperator\n"""
603 node_script += """ except NameError:\n"""
604 node_script += """ raise ValueError("ComputationFunctionNode: TangentOperator not found in the imported user script file")\n"""
605 node_script += """ logging.debug("ComputationFunctionNode: Tangent computation")\n"""
606 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
607 node_script += """ __dXcurrent = computation["inputValues"][0][0][1]\n"""
608 node_script += """ __data = TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
609 node_script += """#\n"""
610 node_script += """if __method == "Adjoint":\n"""
611 node_script += """ try:\n"""
612 node_script += """ AdjointOperator\n"""
613 node_script += """ except NameError:\n"""
614 node_script += """ raise ValueError("ComputationFunctionNode: AdjointOperator not found in the imported user script file")\n"""
615 node_script += """ logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
616 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
617 node_script += """ __Ycurrent = computation["inputValues"][0][0][1]\n"""
618 node_script += """ __data = AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
619 node_script += """#\n"""
620 node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
621 node_script += """__it = 1.*numpy.ravel(__data)\n"""
622 node_script += """outputValues = [[[[]]]]\n"""
623 node_script += """outputValues[0][0][0] = list(__it)\n"""
624 node_script += """#\n"""
625 node_script += """result = {}\n"""
626 node_script += """result["outputValues"] = outputValues\n"""
627 node_script += """result["specificOutputInfos"] = []\n"""
628 node_script += """result["returnCode"] = 0\n"""
629 node_script += """result["errorMessage"] = ""\n"""
630 node_script += """# ==============================================\n"""
632 opt_script_nodeOO.setScript(node_script)
633 opt_script_nodeOO.edAddInputPort("computation", t_param_input)
634 opt_script_nodeOO.edAddOutputPort("result", t_param_output)
636 elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction":
638 ScriptWithOneFunction = data_config["Data"]
640 for FunctionName in ScriptWithOneFunction["Function"]:
641 # We currently support only one file
642 script_filename = ScriptWithOneFunction["Script"][FunctionName]
645 # We create a new pyscript node
646 opt_script_nodeOO = runtime.createScriptNode("", "FunctionNodeOO")
647 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
648 script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
650 script_str= open(script_filename, 'r')
652 raise ValueError("Exception in opening function script file: " + script_filename)
653 node_script = "#-*- coding: utf-8 -*-\n"
654 node_script += "import sys, os, numpy, logging, pickle, codecs\n"
655 node_script += "def loads( data ):\n"
656 node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n"
657 node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
658 node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
659 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
660 node_script += " sys.path.insert(0,filepath)\n"
661 node_script += """# ==============================================\n"""
662 node_script += script_str.read()
663 node_script += """# ==============================================\n"""
664 node_script += """__method = None\n"""
665 node_script += """for param in computation["specificParameters"]:\n"""
666 node_script += """ if param["name"] == "method": __method = loads(param["value"])\n"""
667 node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
668 node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
669 node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
670 node_script += """#\n"""
671 node_script += """try:\n"""
672 node_script += """ DirectOperator\n"""
673 node_script += """except NameError:\n"""
674 node_script += """ raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
675 if sys.version_info.major < 3:
676 node_script += """from daCore import NumericObjects\n"""
678 node_script += """from adao.daCore import NumericObjects\n"""
679 node_script += """FDA = NumericObjects.FDApproximation(\n"""
680 node_script += """ Function = DirectOperator,\n"""
681 node_script += """ increment = %s,\n"""%str(ScriptWithOneFunction['DifferentialIncrement'])
682 node_script += """ centeredDF = %s,\n"""%str(ScriptWithOneFunction['CenteredFiniteDifference'])
683 if 'EnableMultiProcessing' in list(ScriptWithOneFunction.keys()):
684 node_script += """ mpEnabled = %s,\n"""%str(ScriptWithOneFunction['EnableMultiProcessing'])
685 if 'NumberOfProcesses' in list(ScriptWithOneFunction.keys()):
686 node_script += """ mpWorkers = %s,\n"""%str(ScriptWithOneFunction['NumberOfProcesses'])
687 node_script += """ )\n"""
688 node_script += """#\n"""
689 node_script += """__data = []\n"""
690 node_script += """if __method == "Direct":\n"""
691 node_script += """ logging.debug("ComputationFunctionNode: Direct computation")\n"""
692 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
693 node_script += """ __data = FDA.DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
694 node_script += """#\n"""
695 node_script += """if __method == "Tangent":\n"""
696 node_script += """ logging.debug("ComputationFunctionNode: Tangent computation")\n"""
697 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
698 node_script += """ __dXcurrent = computation["inputValues"][0][0][1]\n"""
699 node_script += """ __data = FDA.TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
700 node_script += """#\n"""
701 node_script += """if __method == "Adjoint":\n"""
702 node_script += """ logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
703 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
704 node_script += """ __Ycurrent = computation["inputValues"][0][0][1]\n"""
705 node_script += """ __data = FDA.AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
706 node_script += """#\n"""
707 node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
708 node_script += """__it = 1.*numpy.ravel(__data)\n"""
709 node_script += """outputValues = [[[[]]]]\n"""
710 node_script += """outputValues[0][0][0] = list(__it)\n"""
711 node_script += """#\n"""
712 node_script += """result = {}\n"""
713 node_script += """result["outputValues"] = outputValues\n"""
714 node_script += """result["specificOutputInfos"] = []\n"""
715 node_script += """result["returnCode"] = 0\n"""
716 node_script += """result["errorMessage"] = ""\n"""
717 node_script += """# ==============================================\n"""
719 opt_script_nodeOO.setScript(node_script)
720 opt_script_nodeOO.edAddInputPort("computation", t_param_input)
721 opt_script_nodeOO.edAddOutputPort("result", t_param_output)
724 factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
725 opt_script_nodeOO = factory_opt_script_node.cloneNode("FakeFunctionNode")
727 if ExecuteInContainer and bool(UseSeparateContainer):
728 opt_script_nodeOO.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
729 opt_script_nodeOO.setContainer(mycontainer)
731 # Check if we have a python script for OptimizerLoopNode
732 if "EvolutionModel" in list(study_config.keys()):
733 data_config = study_config["EvolutionModel"]
734 opt_script_nodeEM = None
735 if data_config["Type"] == "Function" and (data_config["From"] == "ScriptWithSwitch" or data_config["From"] == "FunctionDict"):
737 TheData = data_config["Data"]
739 for FunctionName in TheData["Function"]:
740 # We currently support only one file
741 script_filename = TheData["Script"][FunctionName]
743 # We create a new pyscript node
744 opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
745 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
746 script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
748 script_str= open(script_filename, 'r')
750 raise ValueError("Exception in opening function script file: " + script_filename)
751 node_script = "#-*- coding: utf-8 -*-\n"
752 node_script += "import sys, os \n"
753 node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
754 node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
755 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
756 node_script += " sys.path.insert(0,filepath)\n"
757 node_script += script_str.read()
758 opt_script_nodeEM.setScript(node_script)
759 opt_script_nodeEM.edAddInputPort("computation", t_param_input)
760 opt_script_nodeEM.edAddOutputPort("result", t_param_output)
762 elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithFunctions":
764 ScriptWithFunctions = data_config["Data"]
766 for FunctionName in ScriptWithFunctions["Function"]:
767 # We currently support only one file
768 script_filename = ScriptWithFunctions["Script"][FunctionName]
770 # We create a new pyscript node
771 opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
772 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
773 script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
775 script_str= open(script_filename, 'r')
777 raise ValueError("Exception in opening function script file: " + script_filename)
778 node_script = "#-*- coding: utf-8 -*-\n"
779 node_script += "import sys, os, numpy, logging, pickle, codecs\n"
780 node_script += "def loads( data ):\n"
781 node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n"
782 node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
783 node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
784 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
785 node_script += " sys.path.insert(0,filepath)\n"
786 node_script += script_str.read()
787 node_script += """# ==============================================\n"""
788 node_script += """__method = None\n"""
789 node_script += """for param in computation["specificParameters"]:\n"""
790 node_script += """ if param["name"] == "method": __method = loads(param["value"])\n"""
791 node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
792 node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
793 node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
794 node_script += """#\n"""
795 node_script += """#\n"""
796 node_script += """__data = []\n"""
797 node_script += """if __method == "Direct":\n"""
798 node_script += """ try:\n"""
799 node_script += """ DirectOperator\n"""
800 node_script += """ except NameError:\n"""
801 node_script += """ raise ValueError("ComputationFunctionNode: mandatory DirectOperator not found in the imported user script file")\n"""
802 node_script += """ logging.debug("ComputationFunctionNode: Direct computation")\n"""
803 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
804 node_script += """ if len(computation["inputValues"][0][0]) == 2:\n"""
805 node_script += """ __Ucurrent = computation["inputValues"][0][0][1]\n"""
806 node_script += """ __data = DirectOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ucurrent ).T))\n"""
807 node_script += """ else:\n"""
808 node_script += """ __data = DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
809 node_script += """#\n"""
810 node_script += """if __method == "Tangent":\n"""
811 node_script += """ try:\n"""
812 node_script += """ TangentOperator\n"""
813 node_script += """ except NameError:\n"""
814 node_script += """ raise ValueError("ComputationFunctionNode: mandatory TangentOperator not found in the imported user script file")\n"""
815 node_script += """ logging.debug("ComputationFunctionNode: Tangent computation")\n"""
816 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
817 node_script += """ __dXcurrent = computation["inputValues"][0][0][1]\n"""
818 node_script += """ __data = TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
819 node_script += """#\n"""
820 node_script += """if __method == "Adjoint":\n"""
821 node_script += """ try:\n"""
822 node_script += """ AdjointOperator\n"""
823 node_script += """ except NameError:\n"""
824 node_script += """ raise ValueError("ComputationFunctionNode: mandatory AdjointOperator not found in the imported user script file")\n"""
825 node_script += """ logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
826 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
827 node_script += """ __Ycurrent = computation["inputValues"][0][0][1]\n"""
828 node_script += """ __data = AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
829 node_script += """#\n"""
830 node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
831 node_script += """__it = 1.*numpy.ravel(__data)\n"""
832 node_script += """outputValues = [[[[]]]]\n"""
833 node_script += """outputValues[0][0][0] = list(__it)\n"""
834 node_script += """#\n"""
835 node_script += """result = {}\n"""
836 node_script += """result["outputValues"] = outputValues\n"""
837 node_script += """result["specificOutputInfos"] = []\n"""
838 node_script += """result["returnCode"] = 0\n"""
839 node_script += """result["errorMessage"] = ""\n"""
840 node_script += """# ==============================================\n"""
842 opt_script_nodeEM.setScript(node_script)
843 opt_script_nodeEM.edAddInputPort("computation", t_param_input)
844 opt_script_nodeEM.edAddOutputPort("result", t_param_output)
846 elif data_config["Type"] == "Function" and data_config["From"] == "ScriptWithOneFunction":
848 ScriptWithOneFunction = data_config["Data"]
850 for FunctionName in ScriptWithOneFunction["Function"]:
851 # We currently support only one file
852 script_filename = ScriptWithOneFunction["Script"][FunctionName]
854 # We create a new pyscript node
855 opt_script_nodeEM = runtime.createScriptNode("", "FunctionNodeEM")
856 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(script_filename))):
857 script_filename = os.path.join(base_repertory, os.path.basename(script_filename))
859 script_str= open(script_filename, 'r')
861 raise ValueError("Exception in opening function script file: " + script_filename)
862 node_script = "#-*- coding: utf-8 -*-\n"
863 node_script += "import sys, os, numpy, logging, pickle, codecs\n"
864 node_script += "def loads( data ):\n"
865 node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n"
866 node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n"
867 node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n"
868 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
869 node_script += " sys.path.insert(0,filepath)\n"
870 node_script += script_str.read()
871 node_script += """# ==============================================\n"""
872 node_script += """__method = None\n"""
873 node_script += """for param in computation["specificParameters"]:\n"""
874 node_script += """ if param["name"] == "method": __method = loads(param["value"])\n"""
875 node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n"""
876 node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n"""
877 node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n"""
878 node_script += """#\n"""
879 node_script += """try:\n"""
880 node_script += """ DirectOperator\n"""
881 node_script += """except NameError:\n"""
882 node_script += """ raise ValueError("ComputationFunctionNode: DirectOperator not found in the imported user script file")\n"""
883 if sys.version_info.major < 3:
884 node_script += """from daCore import NumericObjects\n"""
886 node_script += """from adao.daCore import NumericObjects\n"""
887 node_script += """FDA = NumericObjects.FDApproximation(\n"""
888 node_script += """ Function = DirectOperator,\n"""
889 node_script += """ increment = %s,\n"""%str(ScriptWithOneFunction['DifferentialIncrement'])
890 node_script += """ centeredDF = %s,\n"""%str(ScriptWithOneFunction['CenteredFiniteDifference'])
891 if 'EnableMultiProcessing' in list(ScriptWithOneFunction.keys()):
892 node_script += """ mpEnabled = %s,\n"""%str(ScriptWithOneFunction['EnableMultiProcessing'])
893 if 'NumberOfProcesses' in list(ScriptWithOneFunction.keys()):
894 node_script += """ mpWorkers = %s,\n"""%str(ScriptWithOneFunction['NumberOfProcesses'])
895 node_script += """ )\n"""
896 node_script += """#\n"""
897 node_script += """__data = []\n"""
898 node_script += """if __method == "Direct":\n"""
899 node_script += """ logging.debug("ComputationFunctionNode: Direct computation")\n"""
900 node_script += """ if len(computation["inputValues"][0][0]) == 2:\n"""
901 node_script += """ raise ValueError("ComputationFunctionNode: you have to build explicitly the controled evolution model and its tangent and adjoint operators, instead of using approximate derivative.")"""
902 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
903 node_script += """ __data = FDA.DirectOperator(numpy.matrix( __Xcurrent ).T)\n"""
904 node_script += """#\n"""
905 node_script += """if __method == "Tangent":\n"""
906 node_script += """ logging.debug("ComputationFunctionNode: Tangent computation")\n"""
907 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
908 node_script += """ __dXcurrent = computation["inputValues"][0][0][1]\n"""
909 node_script += """ __data = FDA.TangentOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __dXcurrent ).T))\n"""
910 node_script += """#\n"""
911 node_script += """if __method == "Adjoint":\n"""
912 node_script += """ logging.debug("ComputationFunctionNode: Adjoint computation")\n"""
913 node_script += """ __Xcurrent = computation["inputValues"][0][0][0]\n"""
914 node_script += """ __Ycurrent = computation["inputValues"][0][0][1]\n"""
915 node_script += """ __data = FDA.AdjointOperator((numpy.matrix( __Xcurrent ).T, numpy.matrix( __Ycurrent ).T))\n"""
916 node_script += """#\n"""
917 node_script += """logging.debug("ComputationFunctionNode: Formatting the output")\n"""
918 node_script += """__it = 1.*numpy.ravel(__data)\n"""
919 node_script += """outputValues = [[[[]]]]\n"""
920 node_script += """outputValues[0][0][0] = list(__it)\n"""
921 node_script += """#\n"""
922 node_script += """result = {}\n"""
923 node_script += """result["outputValues"] = outputValues\n"""
924 node_script += """result["specificOutputInfos"] = []\n"""
925 node_script += """result["returnCode"] = 0\n"""
926 node_script += """result["errorMessage"] = ""\n"""
927 node_script += """# ==============================================\n"""
929 opt_script_nodeEM.setScript(node_script)
930 opt_script_nodeEM.edAddInputPort("computation", t_param_input)
931 opt_script_nodeEM.edAddOutputPort("result", t_param_output)
934 factory_opt_script_node = catalogAd.getNodeFromNodeMap("FakeOptimizerLoopNode")
935 opt_script_nodeEM = factory_opt_script_node.cloneNode("FakeFunctionNode")
937 if ExecuteInContainer and bool(UseSeparateContainer):
938 opt_script_nodeEM.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
939 opt_script_nodeEM.setContainer(mycontainer)
941 # Add computation bloc
942 if "Observers" in list(study_config.keys()):
943 execution_bloc = runtime.createBloc("Execution Bloc")
944 optimizer_node.edSetNode(execution_bloc)
946 # Add a node that permits to configure the switch
947 factory_read_for_switch_node = catalogAd.getNodeFromNodeMap("ReadForSwitchNode")
948 read_for_switch_node = factory_read_for_switch_node.cloneNode("ReadForSwitch")
949 if ExecuteInContainer and bool(UseSeparateContainer):
950 read_for_switch_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
951 read_for_switch_node.setContainer(mycontainer)
952 execution_bloc.edAddChild(read_for_switch_node)
953 ADAO_Case.edAddDFLink(optimizer_node.edGetSamplePort(), read_for_switch_node.getInputPort("data"))
956 switch_node = runtime.createSwitch("Execution Switch")
957 execution_bloc.edAddChild(switch_node)
959 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("switch_value"), switch_node.edGetConditionPort())
961 # First case: computation bloc
962 computation_blocOO = runtime.createBloc("computation_blocOO")
963 computation_blocOO.edAddChild(opt_script_nodeOO)
964 switch_node.edSetNode(1, computation_blocOO)
966 # We connect with the script
967 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeOO.getInputPort("computation"))
968 ADAO_Case.edAddDFLink(opt_script_nodeOO.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
970 # Second case: evolution bloc
971 if "EvolutionModel" in list(study_config.keys()):
972 computation_blocEM = runtime.createBloc("computation_blocEM")
973 computation_blocEM.edAddChild(opt_script_nodeEM)
974 switch_node.edSetNode(2, computation_blocEM)
976 # We connect with the script
977 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeEM.getInputPort("computation"))
978 ADAO_Case.edAddDFLink(opt_script_nodeEM.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
980 # For each observer add a new bloc in the switch
981 observer_config = study_config["Observers"]
982 for observer_name in observer_config:
983 observer_cfg = observer_config[observer_name]
984 observer_bloc = runtime.createBloc("Observer %s" % observer_name)
985 switch_node.edSetNode(observer_cfg["number"], observer_bloc)
987 factory_extract_data_node = catalogAd.getNodeFromNodeMap("ExtractDataNode")
988 extract_data_node = factory_extract_data_node.cloneNode("ExtractData")
989 if ExecuteInContainer and bool(UseSeparateContainer):
990 extract_data_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
991 extract_data_node.setContainer(mycontainer)
992 observer_bloc.edAddChild(extract_data_node)
993 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), extract_data_node.getInputPort("data"))
995 observation_node = None
996 if observer_cfg["nodetype"] == "String":
997 factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeString")
998 observation_node = factory_observation_node.cloneNode("Observation")
999 node_script = observation_node.getScript()
1000 node_script += observer_cfg["String"]
1001 observation_node.setScript(node_script)
1003 factory_observation_node = catalogAd.getNodeFromNodeMap("ObservationNodeFile")
1004 observation_node = factory_observation_node.cloneNode("Observation")
1005 _Internal_Add_dir_script_ports( observation_node, observer_cfg["Script"], repertory, base_repertory, t_string)
1006 if ExecuteInContainer and bool(UseSeparateContainer):
1007 observation_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
1008 observation_node.setContainer(mycontainer)
1009 observer_bloc.edAddChild(observation_node)
1010 ADAO_Case.edAddDFLink(extract_data_node.getOutputPort("var"), observation_node.getInputPort("var"))
1011 ADAO_Case.edAddDFLink(extract_data_node.getOutputPort("info"), observation_node.getInputPort("info"))
1013 factory_end_observation_node = catalogAd.getNodeFromNodeMap("EndObservationNode")
1014 end_observation_node = factory_end_observation_node.cloneNode("EndObservation")
1015 if ExecuteInContainer and bool(UseSeparateContainer):
1016 end_observation_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
1017 end_observation_node.setContainer(mycontainer)
1018 observer_bloc.edAddChild(end_observation_node)
1019 ADAO_Case.edAddCFLink(observation_node, end_observation_node)
1020 ADAO_Case.edAddDFLink(end_observation_node.getOutputPort("output"), optimizer_node.edGetPortForOutPool())
1022 elif "EvolutionModel" in list(study_config.keys()):
1023 execution_bloc = runtime.createBloc("Execution Bloc")
1024 optimizer_node.edSetNode(execution_bloc)
1026 # Add a node that permits to configure the switch
1027 factory_read_for_switch_node = catalogAd.getNodeFromNodeMap("ReadForSwitchNode")
1028 read_for_switch_node = factory_read_for_switch_node.cloneNode("ReadForSwitch")
1029 if ExecuteInContainer and bool(UseSeparateContainer):
1030 read_for_switch_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
1031 read_for_switch_node.setContainer(mycontainer)
1032 execution_bloc.edAddChild(read_for_switch_node)
1033 ADAO_Case.edAddDFLink(optimizer_node.edGetSamplePort(), read_for_switch_node.getInputPort("data"))
1036 switch_node = runtime.createSwitch("Execution Switch")
1037 execution_bloc.edAddChild(switch_node)
1039 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("switch_value"), switch_node.edGetConditionPort())
1041 # First case: computation bloc
1042 computation_blocOO = runtime.createBloc("computation_blocOO")
1043 computation_blocOO.edAddChild(opt_script_nodeOO)
1044 switch_node.edSetNode(1, computation_blocOO)
1046 # We connect with the script
1047 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeOO.getInputPort("computation"))
1048 ADAO_Case.edAddDFLink(opt_script_nodeOO.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
1050 # Second case: evolution bloc
1051 computation_blocEM = runtime.createBloc("computation_blocEM")
1052 computation_blocEM.edAddChild(opt_script_nodeEM)
1053 switch_node.edSetNode(2, computation_blocEM)
1055 # We connect with the script
1056 ADAO_Case.edAddDFLink(read_for_switch_node.getOutputPort("data"), opt_script_nodeEM.getInputPort("computation"))
1057 ADAO_Case.edAddDFLink(opt_script_nodeEM.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
1060 computation_blocOO = runtime.createBloc("computation_blocOO")
1061 optimizer_node.edSetNode(computation_blocOO)
1062 computation_blocOO.edAddChild(opt_script_nodeOO)
1064 # We connect Optimizer with the script
1065 ADAO_Case.edAddDFLink(optimizer_node.edGetSamplePort(), opt_script_nodeOO.getInputPort("computation"))
1066 ADAO_Case.edAddDFLink(opt_script_nodeOO.getOutputPort("result"), optimizer_node.edGetPortForOutPool())
1068 # Connect node with InitUserData
1069 if "ObservationOperator" in init_config["Target"]:
1070 opt_node_script = opt_script_nodeOO.getScript()
1071 opt_node_script = "__builtins__[\"init_data\"] = init_data\n" + opt_node_script
1072 opt_script_nodeOO.setScript(opt_node_script)
1073 opt_script_nodeOO.edAddInputPort("init_data", t_pyobj)
1074 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), opt_script_nodeOO.getInputPort("init_data"))
1076 # Step 4: create post-processing from user configuration
1077 if "UserPostAnalysis" in list(study_config.keys()):
1078 analysis_config = study_config["UserPostAnalysis"]
1079 if analysis_config["From"] == "String":
1080 factory_analysis_node = catalogAd.getNodeFromNodeMap("SimpleUserAnalysis")
1081 analysis_node = factory_analysis_node.cloneNode("UsePostAnalysis")
1082 if ExecuteInContainer and bool(UseSeparateContainer):
1083 analysis_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
1084 analysis_node.setContainer(mycontainer)
1085 default_script = analysis_node.getScript()
1086 final_script = default_script + analysis_config["Data"]
1087 analysis_node.setScript(final_script)
1088 ADAO_Case.edAddChild(analysis_node)
1089 ADAO_Case.edAddCFLink(compute_bloc, analysis_node)
1090 if AlgoType[study_config["Algorithm"]] == "Optim":
1091 ADAO_Case.edAddDFLink(optimizer_node.edGetAlgoResultPort(), analysis_node.getInputPort("Study"))
1093 ADAO_Case.edAddDFLink(execute_node.getOutputPort("Study"), analysis_node.getInputPort("Study"))
1095 # Connect node with InitUserData
1096 if "UserPostAnalysis" in init_config["Target"]:
1097 node_script = analysis_node.getScript()
1098 node_script = "__builtins__[\"init_data\"] = init_data\n" + node_script
1099 analysis_node.setScript(node_script)
1100 analysis_node.edAddInputPort("init_data", t_pyobj)
1101 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), analysis_node.getInputPort("init_data"))
1103 elif analysis_config["From"] == "Script":
1104 factory_analysis_node = catalogAd.getNodeFromNodeMap("SimpleUserAnalysis")
1105 analysis_node = factory_analysis_node.cloneNode("UserPostAnalysis")
1106 if ExecuteInContainer and bool(UseSeparateContainer):
1107 analysis_node.setExecutionMode(SALOMERuntime.PythonNode.REMOTE_STR)
1108 analysis_node.setContainer(mycontainer)
1109 default_script = analysis_node.getScript()
1110 analysis_file_name = analysis_config["Data"]
1111 if repertory and os.path.exists(os.path.join(base_repertory, os.path.basename(analysis_file_name))):
1112 analysis_file_name = os.path.join(base_repertory, os.path.basename(analysis_file_name))
1114 analysis_file = open(analysis_file_name, 'r')
1116 raise ValueError("Exception in opening analysis file: " + str(analysis_config["Data"]))
1117 node_script = "#-*- coding: utf-8 -*-\n"
1118 node_script += "import sys, os \n"
1119 node_script += "filepath = \"" + os.path.dirname(analysis_file_name) + "\"\n"
1120 node_script += "filename = \"" + os.path.basename(analysis_file_name) + "\"\n"
1121 node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n"
1122 node_script += " sys.path.insert(0,filepath)\n"
1123 node_script += default_script
1124 node_script += analysis_file.read()
1125 analysis_node.setScript(node_script)
1126 ADAO_Case.edAddChild(analysis_node)
1127 ADAO_Case.edAddCFLink(compute_bloc, analysis_node)
1128 if AlgoType[study_config["Algorithm"]] == "Optim":
1129 ADAO_Case.edAddDFLink(optimizer_node.edGetAlgoResultPort(), analysis_node.getInputPort("Study"))
1131 ADAO_Case.edAddDFLink(execute_node.getOutputPort("Study"), analysis_node.getInputPort("Study"))
1132 # Connect node with InitUserData
1133 if "UserPostAnalysis" in init_config["Target"]:
1134 node_script = analysis_node.getScript()
1135 node_script = "__builtins__[\"init_data\"] = init_data\n" + node_script
1136 analysis_node.setScript(node_script)
1137 analysis_node.edAddInputPort("init_data", t_pyobj)
1138 ADAO_Case.edAddDFLink(init_node.getOutputPort("init_data"), analysis_node.getInputPort("init_data"))
1144 def write_yacs_proc(proc, yacs_schema_filename):
1146 proc.saveSchema(yacs_schema_filename)