2 Copyright (C) 2008-2018 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 .. _ref_observers_requirements:
26 Requirements for functions describing an "*observer*"
27 -----------------------------------------------------
29 .. index:: single: Observer
30 .. index:: single: Observer Template
32 Some special variables, internal to the optimization process, used inside
33 calculation, can be monitored during an ADAO calculation. These variables can be
34 printed, plotted, saved, etc. It can be done using some "*observer*", sometimes
35 also called "callback". They are Python scripts, each one associated to a given
36 variable. They are activated for each variable modification.
38 There are 3 practical methods to provide an "*observer*" in an ADAO case. The
39 method is chosen with the "*NodeType*" keyword of each "*observer*" entry type, as
40 shown in the following figure:
42 .. eficas_observer_nodetype:
43 .. image:: images/eficas_observer_nodetype.png
47 **Choosing for an "*observer*" its entry type**
49 The "*observer*" can be given as a explicit script (entry of type "*String*"),
50 as a script in an external file (entry of type "*Script*"), or by using a
51 template or pattern (entry of type"*Template*") available by default in ADAO
52 when using the graphical editor. These templates are simple scripts that can be
53 tuned by the user, either in the integrated edtition stage of the case, or in
54 the edition stage of the schema before execution, to improve the ADAO case
55 performance in the SALOME execution supervisor.
57 General form of a script to define an *observer*
58 ++++++++++++++++++++++++++++++++++++++++++++++++
60 To use this capability, the user must have or build scripts that have on
61 standard input (that is, in the naming space) the variables ``var`` and
62 ``info``. The variable ``var`` is to be used as an object of list/tuple type,
63 that contains the variable of interest indexed by the updating step.
65 As an example, here is a very simple script (similar to the model
66 "*ValuePrinter*"), that can be used to print the value of the monitored
69 print " --->",info," Value =",var[-1]
71 Stored as a Python file or as an explicit string, these script lines can be
72 associated to each variable found in the keyword "*SELECTION*" of the
73 "*Observers*" command of the ADAO case: "*Analysis*", "*CurrentState*",
74 "*CostFunction*"... The current value of the variable will be printed at each
75 step of the optimization or data assimilation algorithm. The "*observer*" can
76 include graphical output, storage capacities, complex treatment, statistical
79 Hereinafter we give the identifier and the contents of each model available.
81 Inventory of available *observer* models ("*Template*")
82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
84 .. index:: single: ValuePrinter (Observer)
86 Template **ValuePrinter** :
87 ...........................
89 Print on standard output the current value of the variable.
93 print(str(info)+" "+str(var[-1]))
95 .. index:: single: ValueAndIndexPrinter (Observer)
97 Template **ValueAndIndexPrinter** :
98 ...................................
100 Print on standard output the current value of the variable, adding its index.
104 print(str(info)+(" index %i:"%(len(var)-1))+" "+str(var[-1]))
106 .. index:: single: ValueSeriePrinter (Observer)
108 Template **ValueSeriePrinter** :
109 ................................
111 Print on standard output the value series of the variable.
115 print(str(info)+" "+str(var[:]))
117 .. index:: single: ValueSaver (Observer)
119 Template **ValueSaver** :
120 .........................
122 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.
127 v=numpy.array(var[-1], ndmin=1)
133 f='/tmp/value_%s_%05i.txt'%(info,istep)
135 print('Value saved in "%s"'%f)
138 .. index:: single: ValueSerieSaver (Observer)
140 Template **ValueSerieSaver** :
141 ..............................
143 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.
148 v=numpy.array(var[:], ndmin=1)
154 f='/tmp/value_%s_%05i.txt'%(info,istep)
156 print('Value saved in "%s"'%f)
159 .. index:: single: ValuePrinterAndSaver (Observer)
161 Template **ValuePrinterAndSaver** :
162 ...................................
164 Print on standard output and, in the same time save in a file, the current value of the variable.
169 v=numpy.array(var[-1], ndmin=1)
170 print(str(info)+" "+str(v))
176 f='/tmp/value_%s_%05i.txt'%(info,istep)
178 print('Value saved in "%s"'%f)
181 .. index:: single: ValueIndexPrinterAndSaver (Observer)
183 Template **ValueIndexPrinterAndSaver** :
184 ........................................
186 Print on standard output and, in the same time save in a file, the current value of the variable, adding its index.
191 v=numpy.array(var[-1], ndmin=1)
192 print(str(info)+(" index %i:"%(len(var)-1))+" "+str(v))
198 f='/tmp/value_%s_%05i.txt'%(info,istep)
200 print('Value saved in "%s"'%f)
203 .. index:: single: ValueSeriePrinterAndSaver (Observer)
205 Template **ValueSeriePrinterAndSaver** :
206 ........................................
208 Print on standard output and, in the same time, save in a file the value series of the variable.
213 v=numpy.array(var[:], ndmin=1)
214 print(str(info)+" "+str(v))
220 f='/tmp/value_%s_%05i.txt'%(info,istep)
222 print('Value saved in "%s"'%f)
225 .. index:: single: ValueGnuPlotter (Observer)
227 Template **ValueGnuPlotter** :
228 ..............................
230 Graphically plot with Gnuplot the current value of the variable.
234 import numpy, Gnuplot
235 v=numpy.array(var[-1], ndmin=1)
239 gp(' set style data lines')
242 gp = Gnuplot.Gnuplot(persist=1)
243 gp(' set style data lines')
244 gp('set title "%s (Figure %i)"'%(info,ifig))
245 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
247 .. index:: single: ValueSerieGnuPlotter (Observer)
249 Template **ValueSerieGnuPlotter** :
250 ...................................
252 Graphically plot with Gnuplot the value series of the variable.
256 import numpy, Gnuplot
257 v=numpy.array(var[:], ndmin=1)
261 gp(' set style data lines')
264 gp = Gnuplot.Gnuplot(persist=1)
265 gp(' set style data lines')
266 gp('set title "%s (Figure %i)"'%(info,ifig))
267 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
269 .. index:: single: ValuePrinterAndGnuPlotter (Observer)
271 Template **ValuePrinterAndGnuPlotter** :
272 ........................................
274 Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable.
278 print(str(info)+" "+str(var[-1]))
279 import numpy, Gnuplot
280 v=numpy.array(var[-1], ndmin=1)
284 gp(' set style data lines')
287 gp = Gnuplot.Gnuplot(persist=1)
288 gp(' set style data lines')
289 gp('set title "%s (Figure %i)"'%(info,ifig))
290 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
292 .. index:: single: ValueSeriePrinterAndGnuPlotter (Observer)
294 Template **ValueSeriePrinterAndGnuPlotter** :
295 .............................................
297 Print on standard output and, in the same time, graphically plot with Gnuplot the value series of the variable.
301 print(str(info)+" "+str(var[:]))
302 import numpy, Gnuplot
303 v=numpy.array(var[:], ndmin=1)
307 gp(' set style data lines')
310 gp = Gnuplot.Gnuplot(persist=1)
311 gp(' set style data lines')
312 gp('set title "%s (Figure %i)"'%(info,ifig))
313 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
315 .. index:: single: ValuePrinterSaverAndGnuPlotter (Observer)
317 Template **ValuePrinterSaverAndGnuPlotter** :
318 .............................................
320 Print on standard output and, in the same, time save in a file and graphically plot the current value of the variable.
324 print(str(info)+" "+str(var[-1]))
326 v=numpy.array(var[-1], ndmin=1)
332 f='/tmp/value_%s_%05i.txt'%(info,istep)
334 print('Value saved in "%s"'%f)
340 gp(' set style data lines')
343 gp = Gnuplot.Gnuplot(persist=1)
344 gp(' set style data lines')
345 gp('set title "%s (Figure %i)"'%(info,ifig))
346 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
348 .. index:: single: ValueSeriePrinterSaverAndGnuPlotter (Observer)
350 Template **ValueSeriePrinterSaverAndGnuPlotter** :
351 ..................................................
353 Print on standard output and, in the same, time save in a file and graphically plot the value series of the variable.
357 print(str(info)+" "+str(var[:]))
359 v=numpy.array(var[:], ndmin=1)
365 f='/tmp/value_%s_%05i.txt'%(info,istep)
367 print('Value saved in "%s"'%f)
373 gp(' set style data lines')
376 gp = Gnuplot.Gnuplot(persist=1)
377 gp(' set style data lines')
378 gp('set title "%s (Figure %i)"'%(info,ifig))
379 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
381 .. index:: single: ValueMean (Observer)
383 Template **ValueMean** :
384 ........................
386 Print on standard output the mean of the current value of the variable.
391 print(str(info)+" "+str(numpy.nanmean(var[-1])))
393 .. index:: single: ValueStandardError (Observer)
395 Template **ValueStandardError** :
396 .................................
398 Print on standard output the standard error of the current value of the variable.
403 print(str(info)+" "+str(numpy.nanstd(var[-1])))
405 .. index:: single: ValueVariance (Observer)
407 Template **ValueVariance** :
408 ............................
410 Print on standard output the variance of the current value of the variable.
415 print(str(info)+" "+str(numpy.nanvar(var[-1])))
417 .. index:: single: ValueL2Norm (Observer)
419 Template **ValueL2Norm** :
420 ..........................
422 Print on standard output the L2 norm of the current value of the variable.
427 v = numpy.matrix( numpy.ravel( var[-1] ) )
428 print(str(info)+" "+str(float( numpy.linalg.norm(v) )))
430 .. index:: single: ValueRMS (Observer)
432 Template **ValueRMS** :
433 .......................
435 Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable.
440 v = numpy.matrix( numpy.ravel( var[-1] ) )
441 print(str(info)+" "+str(float( numpy.sqrt((1./v.size)*(v*v.T)) )))