2 Copyright (C) 2008-2017 EDF R&D
4 This file is part of SALOME ADAO module.
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
22 Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
26 ================================================================================
27 **[DocU]** Advanced usage of the ADAO module
28 ================================================================================
30 This section presents advanced methods to use the ADAO module, how to get more
31 information during calculation, or how to use it without the graphical user
32 interface (GUI). It requires to know how to find files or commands included
33 inside the whole SALOME installation. All the names to be replaced by user are
34 indicated by the syntax ``<...>``.
36 Converting and executing an ADAO command file (JDC) using a Shell script
37 ------------------------------------------------------------------------
39 It is possible to convert and execute an ADAO command file (JDC, or ".comm/.py"
40 files pair, which resides in ``<ADAO JDC file directory>``) automatically by
41 using a template Shell script containing all the required steps. If the SALOME
42 main launching command , named ``salome``, is not available in a classical
43 terminal, the user has to know where are the main SALOME launching files, and in
44 particular the ``salome`` one. The directory in which this script resides is
45 symbolically named ``<SALOME main installation dir>`` and has to be replaced by
46 the good one in the Shell file template.
48 When an ADAO command file is build by the ADAO graphical editor and saved, if it
49 is named for example "AdaoStudy1.comm", then a companion file named
50 "AdaoStudy1.py" is automatically created in the same directory. It is named
51 ``<ADAO Python file>`` in the template, and it is converted to YACS as an
52 ``<ADAO YACS xml scheme>`` as a ".xml" file named "AdaoStudy1.xml". After that,
53 this last one can be executed in console mode using the standard YACS console
54 command (see YACS documentation for more information).
56 In all launching command Shell files examples, we choose to start and stop the
57 SALOME application server in the same script. It is not mandatory, but it is
58 useful to avoid stalling SALOME sessions.
60 The simplest example consist in only launching the given YACS sheme, which was
61 previously generated by the user in the graphical interface. In this case, after
62 having replaced the strings between ``<...>`` symbols, one needs only to save
63 the following Shell script::
66 USERDIR="<ADAO JDC file directory>"
67 SALOMEDIR="<SALOME main installation directory>"
68 $SALOMEDIR/salome start -k -t
69 $SALOMEDIR/salome shell -- "driver $USERDIR/<ADAO YACS xml scheme>"
70 $SALOMEDIR/salome shell killSalome.py
72 It is then required to change it to be in executable mode.
74 A more complete example consist in launching execution of a YACS scheme given by
75 the user, having previously verified its availability. For that, replacing the
76 text ``<SALOME main installation directory>``, one needs only to save the
77 following Shell script::
82 echo -e "\nUsage: $0 <ADAO YACS xml scheme>\n"
87 if (test ! -e $USERFILE)
89 echo -e "\nError : the XML file named $USERFILE does not exist.\n"
92 SALOMEDIR="<SALOME main installation directory>"
93 $SALOMEDIR/salome start -k -t
94 $SALOMEDIR/salome shell -- "driver $USERFILE"
95 $SALOMEDIR/salome shell killSalome.py
98 An another example consist in adding the conversion of the ADAO command file
99 (JDC, or ".comm/.py" files pair) in an associated YACS scheme (".xml" file). At
100 the end of the script, one choose also to remove the ``<ADAO YACS xml scheme>``
101 because it is a generated file. For that, after having carefully replaced the
102 text ``<SALOME main installation directory>``, one needs only to save the
103 following Shell script::
108 echo -e "\nUsage: $0 <ADAO .comm/.py case>\n"
112 F=`basename -s .comm $1`
113 F=`basename -s .py $F`
116 if (test ! -e $USERFILE.py)
118 echo -e "\nError : the PY file named $USERFILE.py does not exist.\n"
121 SALOMEDIR="<SALOME main installation directory>"
122 $SALOMEDIR/salome start -k -t
123 $SALOMEDIR/salome shell -- "python $SALOMEDIR/bin/salome/AdaoYacsSchemaCreator.py $USERFILE.py $USERFILE.xml"
124 $SALOMEDIR/salome shell -- "driver $USERFILE.xml"
125 $SALOMEDIR/salome shell killSalome.py
129 In all cases, the standard output and errors come in the launching terminal.
131 Running an ADAO calculation scheme in YACS using the text user mode (YACS TUI)
132 ------------------------------------------------------------------------------
134 This section describes how to execute in TUI (Text User Interface) YACS mode a
135 YACS calculation scheme, obtained in the graphical interface by using the ADAO
136 "Export to YACS" function. It uses the standard YACS TUI mode, which is briefly
137 recalled here (see YACS documentation for more information) through a simple
138 example. As described in documentation, a XML scheme can be loaded in a Python.
139 We give here a whole sequence of command lines to test the validity of the
140 scheme before executing it, adding some initial supplementary ones to explicitly
141 load the types catalog to avoid weird difficulties::
146 SALOMERuntime.RuntimeSALOME_setRuntime()
148 r = pilot.getRuntime()
149 xmlLoader = loader.YACSLoader()
150 xmlLoader.registerProcCataLoader()
152 catalogAd = r.loadCatalog("proc", "<ADAO YACS xml scheme>")
155 r.addCatalog(catalogAd)
158 p = xmlLoader.load("<ADAO YACS xml scheme>")
160 print "IO exception:",ex
162 logger = p.getLogger("parser")
163 if not logger.isEmpty():
164 print "The imported file has errors :"
165 print logger.getStr()
168 print "The schema is not valid and can not be executed"
169 print p.getErrorReport()
171 info=pilot.LinkInfo(pilot.LinkInfo.ALL_DONT_STOP)
172 p.checkConsistency(info)
173 if info.areWarningsOrErrors():
174 print "The schema is not consistent and can not be executed"
175 print info.getGlobalRepr()
177 e = pilot.ExecutorSwig()
179 if p.getEffectiveState() != pilot.DONE:
180 print p.getErrorReport()
182 This method allows for example to edit the YACS XML scheme in TUI, or to gather
183 results for further use.
185 .. _section_advanced_R:
187 Running an ADAO calculation in R environment using the TUI ADAO interface
188 -------------------------------------------------------------------------
191 .. index:: single: rPython
193 To extend the analysis and treatment capacities, it is possible to use ADAO
194 calculations in **R** environment (see [R]_ for more details). It is available
195 in SALOME by launching the R interpreter in the shell "``salome shell``".
196 Moreover, the package "*rPython*" has to be available, it can be installed by
197 the user if required by the following R command::
199 #-*-coding:iso-8859-1-*-
201 # IMPORTANT: to be run in R interpreter
202 # -------------------------------------
203 install.packages("rPython")
205 One will refer to the [GilBellosta15]_ documentation for more information on
208 The ADAO calculations defined in text interface (API/TUI, see
209 :ref:`section_tui`) can be interpreted from the R environment, using some data
210 and information from R. The approach is illustrated in the example
211 :ref:`subsection_tui_example`, suggested in the API/TUI interface description.
212 In the R interpreter, one can run the following commands, directly coming from
215 #-*-coding:iso-8859-1-*-
217 # IMPORTANT: to be run in R interpreter
218 # -------------------------------------
221 from numpy import array
223 case = adaoBuilder.New()
224 case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
225 case.set( 'Background', Vector=[0, 1, 2] )
226 case.set( 'BackgroundError', ScalarSparseMatrix=1.0 )
227 case.set( 'Observation', Vector=array([0.5, 1.5, 2.5]) )
228 case.set( 'ObservationError', DiagonalSparseMatrix='1 1 1' )
229 case.set( 'ObservationOperator', Matrix='1 0 0;0 2 0;0 0 3' )
230 case.set( 'Observer', Variable='Analysis', Template='ValuePrinter' )
236 Analysis [ 0.25000264 0.79999797 0.94999939]
238 In writing the ADAO calculations run from R, one must take close attention to
239 the good use of single and double quotes, that should not collide between the
242 The data can come from the R environment and should be stored in properly
243 assigned variables to be used later in Python for ADAO. One will refer to the
244 [GilBellosta15]_ documentation for the implementation work. We can transform the
245 above example to use data from R to feed the three variables of background,
246 observation and observation operator. We get in the end the optimal state also
247 in a R variable. The other lines are identical. The example thus becomes::
249 #-*-coding:iso-8859-1-*-
251 # IMPORTANT: to be run in R interpreter
252 # -------------------------------------
257 yo <- c(0.5, 1.5, 2.5)
258 h <- '1 0 0;0 2 0;0 0 3'
263 python.assign( "xb", xb )
264 python.assign( "yo", yo )
265 python.assign( "h", h )
267 from numpy import array
269 case = adaoBuilder.New()
270 case.set( 'AlgorithmParameters', Algorithm='3DVAR' )
271 case.set( 'Background', Vector=xb )
272 case.set( 'BackgroundError', ScalarSparseMatrix=1.0 )
273 case.set( 'Observation', Vector=array(yo) )
274 case.set( 'ObservationError', DiagonalSparseMatrix='1 1 1' )
275 case.set( 'ObservationOperator', Matrix=str(h) )
276 case.set( 'Observer', Variable='Analysis', Template='ValuePrinter' )
278 xa = list(case.get('Analysis')[-1])
283 xa <- python.get("xa")
285 One notices the explicit ``str`` and ``list`` type conversions to ensure that
286 the data are transmitted as known standard types from "*rPython*" package.
287 Moreover, it is the data that can be transferred between the two languages, not
288 functions or methods. It is therefore necessary to prepare generically in Python
289 the functions to execute required by ADAO, and to forward them correctly the
292 The most comprehensive cases, proposed in :ref:`subsection_tui_advanced`, can be
293 executed in the same way, and they give the same result as in the standard
296 .. _section_advanced_observer:
298 Getting information on special variables during the ADAO calculation in YACS
299 -----------------------------------------------------------------------------
301 .. index:: single: Observer
302 .. index:: single: Observer Template
304 Some special internal optimization variables, used during calculations, can be
305 monitored during the ADAO calculation. These variables can be printed,
306 plotted, saved, etc. This can be done using "*observer*", that are scripts,
307 each associated with one variable.
309 Some templates are available when editing the ADAO case in graphical editor.
310 These simple scripts can be customized by the user, either at the embedded
311 edition stage, or at the edition stage before execution, to improve the tuning
312 of the ADAO calculation.
314 To implement these "*observer*" efficiently, one can look to the
315 :ref:`ref_observers_requirements`.
317 Getting more information when running a calculation
318 ---------------------------------------------------
320 .. index:: single: Logging
322 When running a calculation, useful data and messages are logged. There are two
323 ways to obtain theses information.
325 The first one, and the preferred way, is to use the built-in variable "*Debug*"
326 available in every ADAO case. It is available through the edition GUI of the
327 module. Setting it to "*1*" will send messages in the log window of the YACS
330 The second one consist in using the "*logging*" native module of Python (see the
331 Python documentation http://docs.python.org/library/logging.html for more
332 information on this module). Everywhere in the YACS scheme, mainly through the
333 scripts entries, the user can set the logging level in accordance to the needs
334 of detailed information. The different logging levels are: "*DEBUG*", "*INFO*",
335 "*WARNING*", "*ERROR*", "*CRITICAL*". All the information flagged with a
336 certain level will be printed for whatever activated level above this particular
337 one (included). The easiest way is to change the log level by using the
338 following Python lines::
341 logging.getLogger().setLevel(logging.DEBUG)
343 The standard logging module default level is "*WARNING*", the default level in
344 the ADAO module is "*INFO*".
346 It is also recommended to include some logging or debug mechanisms in the
347 simulation code, and use them in conjunction with the two previous methods. But
348 be careful not to store too big variables because it cost time, whatever logging
349 level is chosen (that is, even if these variables are not printed).
351 .. _subsection_ref_parallel_df:
353 Accelerating numerical derivatives calculations by using a parallel mode
354 ------------------------------------------------------------------------
356 .. index:: single: EnableMultiProcessing
357 .. index:: single: NumberOfProcesses
359 When setting an operator, as described in
360 :ref:`section_ref_operator_requirements`, the user can choose a functional form
361 "*ScriptWithOneFunction*". This form explicitly leads to approximate the tangent
362 and adjoint operators by a finite differences calculation. It requires several
363 calls to the direct operator (user defined function), at least as many times as
364 the dimension of the state vector. This are these calls that can potentially be
365 executed in parallel.
367 Under some conditions, it is then possible to accelerate the numerical
368 derivatives calculations by using a parallel mode for the finite differences
369 approximation. When setting up an ADAO case, it is done by adding the optional
370 keyword "*EnableMultiProcessing*", set to "1", for the "*SCRIPTWITHONEFUNCTION*"
371 command in the operator definition. The parallel mode will only use local
372 resources (both multi-cores or multi-processors) of the computer on which SALOME
373 is running, requiring as many resources as available. If necessary, one can
374 reduce the available ressources by limiting the possible number of parallel
375 processes using the keyword "*NumberOfProcesses*", set to desired maximum (or to
376 "0" for automatic control, which is the default value). By default, this
377 parallel mode is disabled ("*EnableMultiProcessing=0*").
379 The main conditions to perform parallel calculations come from the user defined
380 function, that represents the direct operator. This function has at least to be
381 "thread safe" to be executed in Python parallel environment (notions out of
382 scope of this paragraph). It is not obvious to give general rules, so it's
383 recommended, for the user who enable this internal parallelism, to carefully
384 verify his function and the obtained results.
386 From a user point of view, some conditions, that have to be met to set up
387 parallel calculations for tangent and the adjoint operators approximations, are
390 #. The dimension of the state vector is more than 2 or 3.
391 #. Unitary calculation of user defined direct function "last for long time", that is, more than few minutes.
392 #. The user defined direct function does not already use parallelism (or parallel execution is disabled in the user calculation).
393 #. The user defined direct function avoids read/write access to common resources, mainly stored data, output files or memory capacities.
394 #. The "*observer*" added by the user avoid read/write access to common resources, such as files or memory.
396 If these conditions are satisfied, the user can choose to enable the internal
397 parallelism for the numerical derivative calculations. Despite the simplicity of
398 activating, by setting one variable only, the user is urged to verify the
399 results of its calculations. One must at least doing them one time with
400 parallelism enabled, and an another time with parallelism disabled, to compare
401 the results. If it does fail somewhere, you have to know that this parallel
402 scheme is working for complex codes, like *Code_Aster* in *SalomeMeca*
403 [SalomeMeca]_ for example. So, if it does not work in your case, check your
404 operator function before and during enabling parallelism...
408 in case of doubt, it is recommended NOT TO ACTIVATE this parallelism.
410 It is also recalled that one have to choose the type "*multi*" for the default
411 container in order to launch the scheme, to allow a really parallel execution.
413 Switching from a version of ADAO to a newer one
414 -----------------------------------------------
416 .. index:: single: Version
418 The ADAO module and its ".comm" case files are identified by versions, with
419 "Major", "Minor" and "Revision" characteristics. A particular version is
420 numbered as "Major.Minor.Revision", with strong link with the numbering of the
423 Each version "Major.Minor.Revision" of the ADAO module can read ADAO case files
424 of the previous minor version "Major.Minor-1.*". In general, it can also read
425 ADAO case files of all the previous minor versions for one major branch, but it
426 is not guaranteed for all the commands or keywords. In general also, an ADAO
427 case file for one version can not be read by a previous minor or major version
430 Switching from 8.x to 8.y with x < y
431 ++++++++++++++++++++++++++++++++++++
433 There is no known incompatibility for the ADAO case files. The upgrade procedure
434 is to read the old ADAO case file with the new SALOME/ADAO module, and save it
437 To make future developments easier, it is strongly recommended to ensure that
438 your user scripts files use a Python 2 and a Python 3 compatible syntax. In
439 particular, it is recommended to use the functional syntax for "*print*" and not
440 the "*command*" syntax, for example:
444 print( "x = %s %s"%(str(x),str(unit)) )
450 print( "x = {0} {1}".format(str(x),str(unit)) )
458 Switching from 7.8 to 8.1
459 +++++++++++++++++++++++++
461 There is no known incompatibility for the ADAO case files. The upgrade procedure
462 is to read the old ADAO case file with the new SALOME/ADAO module, and save it
465 Switching from 7.x to 7.y with x < y
466 ++++++++++++++++++++++++++++++++++++
468 There is no known incompatibility for the ADAO case files. The upgrade procedure
469 is to read the old ADAO case file with the new SALOME/ADAO module, and save it
472 Switching from 6.6 to 7.2
473 +++++++++++++++++++++++++
475 There is no known incompatibility for the ADAO case files. The upgrade procedure
476 is to read the old ADAO case file with the new SALOME/ADAO module, and save it
479 There is one incompatibility introduced for the post-processing or observer
480 script files. The old syntax to call a result object, such as the "*Analysis*"
481 one (in a script provided through the "*UserPostAnalysis*" keyword), was for
484 Analysis = ADD.get("Analysis").valueserie(-1)
485 Analysis = ADD.get("Analysis").valueserie()
487 The new syntax is entirely similar to the (classical) one of a list or tuple
490 Analysis = ADD.get("Analysis")[-1]
491 Analysis = ADD.get("Analysis")[:]
493 The post-processing scripts has to be modified.
495 Switching from 6.x to 6.y with x < y
496 ++++++++++++++++++++++++++++++++++++
498 There is no known incompatibility for the ADAO case file. The upgrade procedure
499 is to read the old ADAO case file with the new SALOME/ADAO module, and save it
502 There is one incompatibility introduced for the operator script files, in the
503 naming of operators used to for the observation operator. The new mandatory
504 names are "*DirectOperator*", "*TangentOperator*" and "*AdjointOperator*", as
505 described in the last subsection of the chapter :ref:`section_reference`. The
506 operator script files has to be modified.