2 Copyright (C) 2008-2016 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.
95 .. index:: single: ValueSeriePrinter (Observer)
97 Template **ValueSeriePrinter** :
98 ................................
100 Print on standard output the value serie of the variable.
106 .. index:: single: ValueSaver (Observer)
108 Template **ValueSaver** :
109 .........................
111 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.
116 v=numpy.array(var[-1], ndmin=1)
122 f='/tmp/value_%s_%05i.txt'%(info,istep)
124 print 'Value saved in "%s"'%f
127 .. index:: single: ValueSerieSaver (Observer)
129 Template **ValueSerieSaver** :
130 ..............................
132 Save the value serie of the variable in a file of the '/tmp' directory named 'value...txt' from the variable name and the saving step.
137 v=numpy.array(var[:], ndmin=1)
143 f='/tmp/value_%s_%05i.txt'%(info,istep)
145 print 'Value saved in "%s"'%f
148 .. index:: single: ValuePrinterAndSaver (Observer)
150 Template **ValuePrinterAndSaver** :
151 ...................................
153 Print on standard output and, in the same time, save in a file the current value of the variable.
158 v=numpy.array(var[-1], ndmin=1)
165 f='/tmp/value_%s_%05i.txt'%(info,istep)
167 print 'Value saved in "%s"'%f
170 .. index:: single: ValueSeriePrinterAndSaver (Observer)
172 Template **ValueSeriePrinterAndSaver** :
173 ........................................
175 Print on standard output and, in the same time, save in a file the value serie of the variable.
180 v=numpy.array(var[:], ndmin=1)
187 f='/tmp/value_%s_%05i.txt'%(info,istep)
189 print 'Value saved in "%s"'%f
192 .. index:: single: ValueGnuPlotter (Observer)
194 Template **ValueGnuPlotter** :
195 ..............................
197 Graphically plot with Gnuplot the current value of the variable.
201 import numpy, Gnuplot
202 v=numpy.array(var[-1], ndmin=1)
206 gp(' set style data lines')
209 gp = Gnuplot.Gnuplot(persist=1)
210 gp(' set style data lines')
211 gp('set title "%s (Figure %i)"'%(info,ifig))
212 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
214 .. index:: single: ValueSerieGnuPlotter (Observer)
216 Template **ValueSerieGnuPlotter** :
217 ...................................
219 Graphically plot with Gnuplot the value serie of the variable.
223 import numpy, Gnuplot
224 v=numpy.array(var[:], ndmin=1)
228 gp(' set style data lines')
231 gp = Gnuplot.Gnuplot(persist=1)
232 gp(' set style data lines')
233 gp('set title "%s (Figure %i)"'%(info,ifig))
234 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
236 .. index:: single: ValuePrinterAndGnuPlotter (Observer)
238 Template **ValuePrinterAndGnuPlotter** :
239 ........................................
241 Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable.
246 import numpy, Gnuplot
247 v=numpy.array(var[-1], ndmin=1)
251 gp(' set style data lines')
254 gp = Gnuplot.Gnuplot(persist=1)
255 gp(' set style data lines')
256 gp('set title "%s (Figure %i)"'%(info,ifig))
257 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
259 .. index:: single: ValueSeriePrinterAndGnuPlotter (Observer)
261 Template **ValueSeriePrinterAndGnuPlotter** :
262 .............................................
264 Print on standard output and, in the same time, graphically plot with Gnuplot the value serie of the variable.
269 import numpy, Gnuplot
270 v=numpy.array(var[:], ndmin=1)
274 gp(' set style data lines')
277 gp = Gnuplot.Gnuplot(persist=1)
278 gp(' set style data lines')
279 gp('set title "%s (Figure %i)"'%(info,ifig))
280 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
282 .. index:: single: ValuePrinterSaverAndGnuPlotter (Observer)
284 Template **ValuePrinterSaverAndGnuPlotter** :
285 .............................................
287 Print on standard output and, in the same, time save in a file and graphically plot the current value of the variable.
293 v=numpy.array(var[-1], ndmin=1)
299 f='/tmp/value_%s_%05i.txt'%(info,istep)
301 print 'Value saved in "%s"'%f
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: ValueSeriePrinterSaverAndGnuPlotter (Observer)
317 Template **ValueSeriePrinterSaverAndGnuPlotter** :
318 ..................................................
320 Print on standard output and, in the same, time save in a file and graphically plot the value serie of the variable.
326 v=numpy.array(var[:], 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: ValueMean (Observer)
350 Template **ValueMean** :
351 ........................
353 Print on standard output the mean of the current value of the variable.
358 print info, numpy.nanmean(var[-1])
360 .. index:: single: ValueStandardError (Observer)
362 Template **ValueStandardError** :
363 .................................
365 Print on standard output the standard error of the current value of the variable.
370 print info, numpy.nanstd(var[-1])
372 .. index:: single: ValueVariance (Observer)
374 Template **ValueVariance** :
375 ............................
377 Print on standard output the variance of the current value of the variable.
382 print info, numpy.nanvar(var[-1])
384 .. index:: single: ValueL2Norm (Observer)
386 Template **ValueL2Norm** :
387 ..........................
389 Print on standard output the L2 norm of the current value of the variable.
394 v = numpy.matrix( numpy.ravel( var[-1] ) )
395 print info, float( numpy.linalg.norm(v) )
397 .. index:: single: ValueRMS (Observer)
399 Template **ValueRMS** :
400 .......................
402 Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable.
407 v = numpy.matrix( numpy.ravel( var[-1] ) )
408 print info, float( numpy.sqrt((1./v.size)*(v*v.T)) )