2 Copyright (C) 2008-2020 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
24 .. _section_ref_observers_requirements:
26 Requirements for functions describing an "*observer*"
27 -----------------------------------------------------
29 .. index:: single: Observer
30 .. index:: single: setObserver
31 .. index:: single: Observer Template
33 Some special variables, internal to the optimization process and used inside
34 calculation, can be monitored during an ADAO calculation. These variables can
35 be printed, plotted, saved, etc. by the user. This can be done using some
36 "*observer*", sometimes also called "callback". They are Python scripts, each
37 one associated to a given variable, and that are automatically activated for
38 each variable modification.
40 In the graphical interface EFICAS of ADAO, there are 3 practical methods to
41 provide an "*observer*" in an ADAO case. The method is chosen with the
42 "*NodeType*" keyword of each "*observer*" entry type, as shown in the following
45 .. eficas_observer_nodetype:
46 .. image:: images/eficas_observer_nodetype.png
50 **Choosing for an "*observer*" its entry type**
52 The "*observer*" can be given as an explicit script (entry of type "*String*"),
53 as a script in an external file (entry of type "*Script*"), or by using a
54 template or pattern (entry of type"*Template*") available by default in ADAO
55 when using the graphical editor and detailed in the following part
56 :ref:`section_ref_observers_templates`. These templates are simple scripts that
57 can be tuned by the user, either in the integrated edition stage of the case
58 with ADAO EFICAS, or in the edition stage of the schema before execution, to
59 improve the ADAO case performance in the SALOME execution supervisor YACS.
61 In the textual interface (TUI) of ADAO (see the part :ref:`section_tui`), the
62 same information can be given with the command "*setObserver*" applied to a
63 specific variable indicated in the argument "*Variable*". The other arguments
64 of this command allow to define the observer either as a template (argument
65 "*Template*") representing one of the scripts detailed in the part
66 :ref:`section_ref_observers_templates`, or as an explicit script (argument
67 "*String*"), or as a script in an external file (argument "*Script*").
69 General form for a script describing an *observer*
70 ++++++++++++++++++++++++++++++++++++++++++++++++++
72 To use this capability, the user must have or build scripts that have on
73 standard input (that is, in the naming space) the variables ``var`` and
74 ``info``. The variable ``var`` is to be used as an object of list/tuple type,
75 that contains the variable of interest indexed by the updating step.
77 As an example, here is a very simple script (similar to the model
78 "*ValuePrinter*"), that can be used to print the value of the monitored
81 print(" --->",info," Value =",var[-1])
83 Stored as a Python file or as an explicit string, these script lines can be
84 associated to each variable found in the keyword "*SELECTION*" of the
85 "*Observers*" command of the ADAO case: "*Analysis*", "*CurrentState*",
86 "*CostFunction*"... The current value of the variable will be printed at each
87 step of the optimization or data assimilation algorithm. The "*observer*" can
88 include graphical output, storage capacities, complex treatment, statistical
91 Hereinafter we give the identifier and the contents of each model available.
93 .. _section_ref_observers_templates:
95 Inventory of available *observer* models ("*Template*")
96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
98 .. index:: single: ValuePrinter (Observer)
100 Template **ValuePrinter** :
101 ...........................
103 Print on standard output the current value of the variable.
107 print(str(info)+" "+str(var[-1]))
109 .. index:: single: ValueAndIndexPrinter (Observer)
111 Template **ValueAndIndexPrinter** :
112 ...................................
114 Print on standard output the current value of the variable, adding its index.
118 print(str(info)+(" index %i:"%(len(var)-1))+" "+str(var[-1]))
120 .. index:: single: ValueSeriePrinter (Observer)
122 Template **ValueSeriePrinter** :
123 ................................
125 Print on standard output the value series of the variable.
129 print(str(info)+" "+str(var[:]))
131 .. index:: single: ValueSaver (Observer)
133 Template **ValueSaver** :
134 .........................
136 Save the current value of the variable in a file of the '/tmp' directory named 'value...txt' from the variable name and the saving step.
141 v=numpy.array(var[-1], ndmin=1)
147 f='/tmp/value_%s_%05i.txt'%(info,istep)
149 print('Value saved in "%s"'%f)
152 .. index:: single: ValueSerieSaver (Observer)
154 Template **ValueSerieSaver** :
155 ..............................
157 Save the value series of the variable in a file of the '/tmp' directory named 'value...txt' from the variable name and the saving step.
162 v=numpy.array(var[:], ndmin=1)
168 f='/tmp/value_%s_%05i.txt'%(info,istep)
170 print('Value saved in "%s"'%f)
173 .. index:: single: ValuePrinterAndSaver (Observer)
175 Template **ValuePrinterAndSaver** :
176 ...................................
178 Print on standard output and, in the same time save in a file of the '/tmp' directory, the current value of the variable.
183 v=numpy.array(var[-1], ndmin=1)
184 print(str(info)+" "+str(v))
190 f='/tmp/value_%s_%05i.txt'%(info,istep)
192 print('Value saved in "%s"'%f)
195 .. index:: single: ValueIndexPrinterAndSaver (Observer)
197 Template **ValueIndexPrinterAndSaver** :
198 ........................................
200 Print on standard output and, in the same time save in a file of the '/tmp' directory, the current value of the variable, adding its index.
205 v=numpy.array(var[-1], ndmin=1)
206 print(str(info)+(" index %i:"%(len(var)-1))+" "+str(v))
212 f='/tmp/value_%s_%05i.txt'%(info,istep)
214 print('Value saved in "%s"'%f)
217 .. index:: single: ValueSeriePrinterAndSaver (Observer)
219 Template **ValueSeriePrinterAndSaver** :
220 ........................................
222 Print on standard output and, in the same time, save in a file of the '/tmp' directory, the value series of the variable.
227 v=numpy.array(var[:], ndmin=1)
228 print(str(info)+" "+str(v))
234 f='/tmp/value_%s_%05i.txt'%(info,istep)
236 print('Value saved in "%s"'%f)
239 .. index:: single: ValueGnuPlotter (Observer)
241 Template **ValueGnuPlotter** :
242 ..............................
244 Graphically plot with Gnuplot the current value of the variable.
248 import numpy, Gnuplot
249 v=numpy.array(var[-1], ndmin=1)
253 gp(' set style data lines')
256 gp = Gnuplot.Gnuplot(persist=1)
257 gp(' set style data lines')
258 gp('set title "%s (Figure %i)"'%(info,ifig))
259 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
261 .. index:: single: ValueSerieGnuPlotter (Observer)
263 Template **ValueSerieGnuPlotter** :
264 ...................................
266 Graphically plot with Gnuplot the value series of the variable.
270 import numpy, Gnuplot
271 v=numpy.array(var[:], ndmin=1)
275 gp(' set style data lines')
278 gp = Gnuplot.Gnuplot(persist=1)
279 gp(' set style data lines')
280 gp('set title "%s (Figure %i)"'%(info,ifig))
281 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
283 .. index:: single: ValuePrinterAndGnuPlotter (Observer)
285 Template **ValuePrinterAndGnuPlotter** :
286 ........................................
288 Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable.
292 print(str(info)+" "+str(var[-1]))
293 import numpy, Gnuplot
294 v=numpy.array(var[-1], ndmin=1)
298 gp(' set style data lines')
301 gp = Gnuplot.Gnuplot(persist=1)
302 gp(' set style data lines')
303 gp('set title "%s (Figure %i)"'%(info,ifig))
304 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
306 .. index:: single: ValueSeriePrinterAndGnuPlotter (Observer)
308 Template **ValueSeriePrinterAndGnuPlotter** :
309 .............................................
311 Print on standard output and, in the same time, graphically plot with Gnuplot the value series of the variable.
315 print(str(info)+" "+str(var[:]))
316 import numpy, Gnuplot
317 v=numpy.array(var[:], ndmin=1)
321 gp(' set style data lines')
324 gp = Gnuplot.Gnuplot(persist=1)
325 gp(' set style data lines')
326 gp('set title "%s (Figure %i)"'%(info,ifig))
327 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
329 .. index:: single: ValuePrinterSaverAndGnuPlotter (Observer)
331 Template **ValuePrinterSaverAndGnuPlotter** :
332 .............................................
334 Print on standard output and, in the same, time save in a file of the '/tmp' directory and graphically plot the current value of the variable.
338 print(str(info)+" "+str(var[-1]))
340 v=numpy.array(var[-1], ndmin=1)
346 f='/tmp/value_%s_%05i.txt'%(info,istep)
348 print('Value saved in "%s"'%f)
354 gp(' set style data lines')
357 gp = Gnuplot.Gnuplot(persist=1)
358 gp(' set style data lines')
359 gp('set title "%s (Figure %i)"'%(info,ifig))
360 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
362 .. index:: single: ValueSeriePrinterSaverAndGnuPlotter (Observer)
364 Template **ValueSeriePrinterSaverAndGnuPlotter** :
365 ..................................................
367 Print on standard output and, in the same, time save in a file of the '/tmp' directory and graphically plot the value series of the variable.
371 print(str(info)+" "+str(var[:]))
373 v=numpy.array(var[:], ndmin=1)
379 f='/tmp/value_%s_%05i.txt'%(info,istep)
381 print('Value saved in "%s"'%f)
387 gp(' set style data lines')
390 gp = Gnuplot.Gnuplot(persist=1)
391 gp(' set style data lines')
392 gp('set title "%s (Figure %i)"'%(info,ifig))
393 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
395 .. index:: single: ValueMean (Observer)
397 Template **ValueMean** :
398 ........................
400 Print on standard output the mean of the current value of the variable.
405 print(str(info)+" "+str(numpy.nanmean(var[-1])))
407 .. index:: single: ValueStandardError (Observer)
409 Template **ValueStandardError** :
410 .................................
412 Print on standard output the standard error of the current value of the variable.
417 print(str(info)+" "+str(numpy.nanstd(var[-1])))
419 .. index:: single: ValueVariance (Observer)
421 Template **ValueVariance** :
422 ............................
424 Print on standard output the variance of the current value of the variable.
429 print(str(info)+" "+str(numpy.nanvar(var[-1])))
431 .. index:: single: ValueL2Norm (Observer)
433 Template **ValueL2Norm** :
434 ..........................
436 Print on standard output the L2 norm of the current value of the variable.
441 v = numpy.ravel( var[-1] )
442 print(str(info)+" "+str(float( numpy.linalg.norm(v) )))
444 .. index:: single: ValueRMS (Observer)
446 Template **ValueRMS** :
447 .......................
449 Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable.
454 v = numpy.ravel( var[-1] )
455 print(str(info)+" "+str(float( numpy.sqrt((1./v.size)*numpy.dot(v,v)) )))