2 Copyright (C) 2008-2015 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: ValueGnuPlotter (Observer)
86 Template **ValueGnuPlotter** :
87 ..............................
89 Graphically plot with Gnuplot the current value of the variable.
94 v=numpy.array(var[-1], ndmin=1)
98 gp('set style data lines')
101 gp = Gnuplot.Gnuplot(persist=1)
102 gp('set style data lines')
103 gp('set title "%s (Figure %i)"'%(info,ifig))
104 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
106 .. index:: single: ValueMean (Observer)
108 Template **ValueMean** :
109 ........................
111 Print on standard output the mean of the current value of the variable.
116 print info, numpy.nanmean(var[-1])
118 .. index:: single: ValuePrinter (Observer)
120 Template **ValuePrinter** :
121 ...........................
123 Print on standard output the current value of the variable.
129 .. index:: single: ValuePrinterAndGnuPlotter (Observer)
131 Template **ValuePrinterAndGnuPlotter** :
132 ........................................
134 Print on standard output and, in the same time, graphically plot with Gnuplot the current value of the variable.
139 import numpy, Gnuplot
140 v=numpy.array(var[-1], ndmin=1)
144 gp('set style data lines')
147 gp = Gnuplot.Gnuplot(persist=1)
148 gp('set style data lines')
149 gp('set title "%s (Figure %i)"'%(info,ifig))
150 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
152 .. index:: single: ValuePrinterAndSaver (Observer)
154 Template **ValuePrinterAndSaver** :
155 ...................................
157 Print on standard output and, in the same time, save in a file the current value of the variable.
162 v=numpy.array(var[-1], ndmin=1)
169 f='/tmp/value_%s_%05i.txt'%(info,istep)
171 print 'Value saved in "%s"'%f
174 .. index:: single: ValuePrinterSaverAndGnuPlotter (Observer)
176 Template **ValuePrinterSaverAndGnuPlotter** :
177 .............................................
179 Print on standard output and, in the same, time save in a file and graphically plot the current value of the variable.
185 v=numpy.array(var[-1], ndmin=1)
191 f='/tmp/value_%s_%05i.txt'%(info,istep)
193 print 'Value saved in "%s"'%f
199 gp('set style data lines')
202 gp = Gnuplot.Gnuplot(persist=1)
203 gp('set style data lines')
204 gp('set title "%s (Figure %i)"'%(info,ifig))
205 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
207 .. index:: single: ValueRMS (Observer)
209 Template **ValueRMS** :
210 .......................
212 Print on standard output the root mean square (RMS), or quadratic mean, of the current value of the variable.
217 v = numpy.matrix( numpy.ravel( var[-1] ) )
218 print info, float( numpy.sqrt((1./v.size)*(v*v.T)) )
220 .. index:: single: ValueSaver (Observer)
222 Template **ValueSaver** :
223 .........................
225 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.
230 v=numpy.array(var[-1], ndmin=1)
236 f='/tmp/value_%s_%05i.txt'%(info,istep)
238 print 'Value saved in "%s"'%f
241 .. index:: single: ValueSerieGnuPlotter (Observer)
243 Template **ValueSerieGnuPlotter** :
244 ...................................
246 Graphically plot with Gnuplot the value serie of the variable.
250 import numpy, Gnuplot
251 v=numpy.array(var[:], ndmin=1)
255 gp('set style data lines')
258 gp = Gnuplot.Gnuplot(persist=1)
259 gp('set style data lines')
260 gp('set title "%s (Figure %i)"'%(info,ifig))
261 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
263 .. index:: single: ValueSeriePrinter (Observer)
265 Template **ValueSeriePrinter** :
266 ................................
268 Print on standard output the value serie of the variable.
274 .. index:: single: ValueSeriePrinterAndGnuPlotter (Observer)
276 Template **ValueSeriePrinterAndGnuPlotter** :
277 .............................................
279 Print on standard output and, in the same time, graphically plot with Gnuplot the value serie of the variable.
284 import numpy, Gnuplot
285 v=numpy.array(var[:], ndmin=1)
289 gp('set style data lines')
292 gp = Gnuplot.Gnuplot(persist=1)
293 gp('set style data lines')
294 gp('set title "%s (Figure %i)"'%(info,ifig))
295 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
297 .. index:: single: ValueSeriePrinterAndSaver (Observer)
299 Template **ValueSeriePrinterAndSaver** :
300 ........................................
302 Print on standard output and, in the same time, save in a file the value serie of the variable.
307 v=numpy.array(var[:], ndmin=1)
314 f='/tmp/value_%s_%05i.txt'%(info,istep)
316 print 'Value saved in "%s"'%f
319 .. index:: single: ValueSeriePrinterSaverAndGnuPlotter (Observer)
321 Template **ValueSeriePrinterSaverAndGnuPlotter** :
322 ..................................................
324 Print on standard output and, in the same, time save in a file and graphically plot the value serie of the variable.
330 v=numpy.array(var[:], ndmin=1)
336 f='/tmp/value_%s_%05i.txt'%(info,istep)
338 print 'Value saved in "%s"'%f
344 gp('set style data lines')
347 gp = Gnuplot.Gnuplot(persist=1)
348 gp('set style data lines')
349 gp('set title "%s (Figure %i)"'%(info,ifig))
350 gp.plot( Gnuplot.Data( v, with_='lines lw 2' ) )
352 .. index:: single: ValueSerieSaver (Observer)
354 Template **ValueSerieSaver** :
355 ..............................
357 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.
362 v=numpy.array(var[:], ndmin=1)
368 f='/tmp/value_%s_%05i.txt'%(info,istep)
370 print 'Value saved in "%s"'%f
373 .. index:: single: ValueStandardError (Observer)
375 Template **ValueStandardError** :
376 .................................
378 Print on standard output the standard error of the current value of the variable.
383 print info, numpy.nanstd(var[-1])
385 .. index:: single: ValueVariance (Observer)
387 Template **ValueVariance** :
388 ............................
390 Print on standard output the variance of the current value of the variable.
395 print info, numpy.nanvar(var[-1])