Salome HOME
Nouvelle version de la documentation
[modules/adao.git] / doc / examples.rst
1 ================================================================================
2 Examples on using the ADAO module
3 ================================================================================
4
5 This section presents some examples on using the ADAO module in SALOME.
6
7 Building a simple estimation case with explicit data definition
8 --------------------------------------------------------------------------------
9
10 This simple example is a demonstration one, and describes how to set a BLUE
11 estimation framework in order to get *weighted least square estimated state* of
12 a system from an observation of the state and from an *a priori* knowledge (or
13 background) of this state. In other words, we look for the weighted middle
14 between the observation and the background vectors. All the numerical values of
15 this example are arbitrary.
16
17 Experimental set up
18 +++++++++++++++++++
19
20 We choose to operate in a 3-dimensional space. 3D is chosen in order to restrict
21 the size of numerical object to explicitly enter by the user, but the problem is
22 not dependant of the dimension and can be set in dimension 1000... The observed
23 state is of value 1 in each direction, so:
24
25     ``Yo = [1 1 1]``
26
27 The background state, wich represent some *a priori* knowledge or a
28 regularization, is of value of 0 in each direction, which is:
29
30     ``Xb = [0 0 0]``
31
32 Data assimilation requires information on errors covariances for observation and
33 background variables. We choose here to have uncorrelated errors (that is,
34 diagonal matrices) and to have the same variance of 1 for all variables (that
35 is, identity matrices. We get:
36
37     ``B = R = [1 0 0 ; 0 1 0 ; 0 0 1]``
38
39 Last, we need an observation operator to convert the background value in the
40 space of observation value. Here, because the space dimensions are the same, we
41 can choose the identity  as the observation operator:
42
43     ``H = [1 0 0 ; 0 1 0 ; 0 0 1]``
44
45 With such choices, the Best Linear Unbiased Estimator (BLUE) will be the
46 average vector between ``Yo`` and ``Xb``, named the *analysis* and denoted by
47 ``Xa``:
48
49     ``Xa = [0.5 0.5 0.5]``
50
51 As en extension of this example, one can change the variances for ``B`` or ``R``
52 independantly, and the analysis will move to ``Yo`` or ``Xb`` in inverse
53 proportion of the variances in ``B`` and ``R``. It is also equivalent to serach
54 for the analysis thought a BLUE algorithm or a 3DVAR one.
55
56 Using the GUI to build the ADAO case
57 ++++++++++++++++++++++++++++++++++++
58
59 First, you have to activate the ADAO module by choosing the appropriate module
60 button or menu of SALOME, and you will see:
61
62   .. _adao_activate:
63   .. image:: images/adao_activate.png
64     :align: center
65   .. centered::
66     **Activating the module ADAO in SALOME**
67
68 .. |eficas_new| image:: images/eficas_new.png
69    :align: middle
70
71 Choose the "*New*" button in this window. You will directly get the EFICAS
72 interface for variables definition, along with the "*Object browser*". You can
73 then click on the "*New*" button |eficas_new| to create a new ADAO case, and you
74 will see:
75
76   .. _adao_viewer:
77   .. image:: images/adao_viewer.png
78     :align: center
79     :width: 100%
80   .. centered::
81     **The EFICAS viewer for cases definition in module ADAO**
82
83 Then fill in the variables to build the ADAO case by using the experimental set
84 up described above. All the technical information given above will be directly
85 inserted in the ADAO case definition, by using the *String* type for all the
86 variables. When the case definition is ready, save it to a "*JDC (\*.comm)*"
87 native file somewhere in your path. Remember that other files will be also
88 created near this first one, so it is better to make a specific directory for
89 your case, and to save the file inside. The name of the file will appear in the
90 "*Object browser*" window, under the "*ADAO*" menu. The final case definition
91 looks like this:
92
93   .. _adao_jdcexample01:
94   .. image:: images/adao_jdcexample01.png
95     :align: center
96     :width: 100%
97   .. centered::
98     **Definition of the experimental set up chosen for the ADAO case**
99
100 To go further, we need now to generate the YACS scheme from the ADAO case
101 definition. In order to do that, right click on the name of the file case in the
102 "*Object browser*" window, and choose the "*Export to YACS*" submenu as below:
103
104   .. _adao_exporttoyacs:
105   .. image:: images/adao_exporttoyacs.png
106     :align: center
107     :scale: 75%
108   .. centered::
109     **"*Export to YACS*" submenu to generate the YACS scheme from the ADAO case**
110
111 This command will generate the YACS scheme, activate YACS module in SALOME, and
112 open the new scheme in the YACS module GUI [#]. After reordering the nodes by
113 using the "*arrange local node*" submenu of the YACS graphical view of the
114 scheme, you get the following representation of the generated ADAO scheme:
115
116   .. _yacs_generatedscheme:
117   .. image:: images/yacs_generatedscheme.png
118     :align: center
119     :width: 100%
120   .. centered::
121     **YACS generated scheme from the ADAO case**
122
123 After that point, all the modifications, executions and post-processing of the
124 data assimilation scheme will be done in YACS. In order to check the result in a
125 simple way, we create here a new YACS node by using the "*inline script node*"
126 submenu of the YACS graphical view, and we name it "*PostProcessing*".
127
128 This script will retrieve the data assimilation analysis from the
129 "*algoResults*" output port of the computation bloc (which gives access to a
130 SALOME Python Object), and will print it on the standard output. 
131
132 To obtain this, the inline script node need to have an input port of type
133 "*pyobj*" named "*results*", that have to be linked graphically to the
134 "*algoResults*" output port of the computation bloc. Then the code to fill in
135 the script node is::
136
137     Xa = results.ADD.get("Analysis").valueserie(-1)
138
139     print
140     print "Analysis =",Xa
141     print
142
143 The augmented YACS scheme can be saved (overwriting the generated scheme if the
144 simple "*Save*" command or button is used, or with a new name). Then,
145 classically in YACS, it have to be prepared for run, and then executed. After
146 completion, the printing on standard output is available in the "*YACS Container
147 Log*", obtained throught the right click menu of the "*proc*" window in the YACS
148 scheme as shown below:
149
150   .. _yacs_containerlog:
151   .. image:: images/yacs_containerlog.png
152     :align: center
153     :width: 100%
154   .. centered::
155     **YACS menu for Container Log, and dialog window showing the log**
156
157 We verify that the result is correct by checking that the log contains the
158 following line::
159
160     Analysis = [0.5, 0.5, 0.5]
161
162 as shown in the image above.
163
164 As a simple extension of this example, one can notice that the same problem
165 solved with a 3DVAR algorithm gives the same result. This algorithm can be
166 chosen at the ADAO case building step, before entering in YACS step. The
167 ADAO 3DVAR case will look completly similar to the BLUE algoritmic case, as
168 shown by the following figure:
169
170   .. _adao_jdcexample01:
171   .. image:: images/adao_jdcexample02.png
172     :align: center
173     :width: 100%
174   .. centered::
175     **Defining an ADAO 3DVAR case looks completly similar to a BLUE case**
176
177 There is only one keyword changing, with "*ThreeDVAR*" instead of "*Blue*".
178
179 Building a simple estimation case with external data definition by functions
180 --------------------------------------------------------------------------------
181
182 It is useful to get parts or all of the data from external definition, using
183 Python script files to provide access to the data. As an example, we build here
184 an ADAO case representing the same experimental set up as in the above example
185 `Building a simple estimation case with explicit data definition`_, but using
186 data form a single one external Python script file.
187
188 First, we write the following script file, using conventional names for the
189 desired variables. Here, all the input variables are defined in the script, but
190 the user can choose to split the file in several ones, or to mix explicit data
191 definition in the ADAO GUI and implicit data definition by external files. This
192 script looks like::
193
194     #-*-coding:iso-8859-1-*-
195     import numpy
196     #
197     # Definition of the Background as a vector
198     # ----------------------------------------
199     Background = [0, 0, 0]
200     #
201     # Definition of the Observation as a vector
202     # -----------------------------------------
203     Observation = "1 1 1"
204     #
205     # Definition of the Background Error covariance as a matrix
206     # ---------------------------------------------------------
207     BackgroundError = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
208     #
209     # Definition of the Observation Error covariance as a matrix
210     # ----------------------------------------------------------
211     ObservationError = numpy.matrix("1 0 0 ; 0 1 0 ; 0 0 1")
212     #
213     # Definition of the Observation Operator as a matrix
214     # --------------------------------------------------
215     ObservationOperator = numpy.identity(3)
216
217 The names of the Python variables above are mandatory, in order to define the
218 right variables, but the Python script can be bigger and define classes,
219 functions, etc. with other names. It shows different ways to define arrays and
220 matrices, using list, string (as in Numpy or Octave), Numpy array type or Numpy
221 matrix type, and Numpy special functions. All of these syntaxes are valid.
222
223 After saving this script somewhere in your path (named here
224 "*test003_ADAO_example_scripts.py*" for the example), we use the GUI to build
225 the ADAO case. The procedure to fill in the case is similar except that, instead
226 of selecting the "*String*" option for the "*FROM*" keyword, we select the
227 "*Script*" one. This leads to a "*SCRIPT_DATA/SCRIPT_FILE*" entry in the tree,
228 allowing to choose a file as:
229
230   .. _adao_scriptentry01:
231   .. image:: images/adao_scriptentry01.png
232     :align: center
233     :width: 100%
234   .. centered::
235     **Defining an input value using an external script file**
236
237 Other steps and results are exactly the same as in the `Building a simple
238 estimation case with explicit data definition`_ previous example.
239
240 In fact, this script methodology allows to retrieve data from inline or previous
241 calculations, from static files, from database or from stream, all of them
242 outside of SALOME. It allows also to modify easily some input data, for example
243 debug purpose or for repetitive process. **But be careful, it is not a "safe"
244 process, in the sense that erroneous data, or errors in calculations, can be
245 directly injected into the YACS scheme execution.**
246
247 Adding parameters to control the data assimilation algorithm
248 --------------------------------------------------------------------------------
249
250 One can add some optional parameters to control the data assimilation algorithm
251 calculation. This is done by using the "*AlgorithmParameters*" keyword in the
252 definition of the ADAO case, which is an option of the algorithm. This keyword
253 requires a Python dictionary containing some key/value pairs.
254
255 For example, with a 3DVAR algorithm, the possible keys are "*Minimizer*",
256 "*MaximumNumberOfSteps*", "*Bounds*". Bounds can be given by a list of list of
257 pairs of lower/upper bounds for each variable, with possibly ``None`` every time
258 there is no bound. If no bounds at all are required on the control variables,
259 then one can choose the "BFGS" or "CG" minimisation algorithm for the 3DVAR
260 algorithm.
261
262 This dictionary has to be defined in an external Python script file, using the
263 mandatory variable name "*AlgorithmParameters*" for the dictionary. All the keys
264 inside the dictionary are optional, they all have default values, and can exist
265 without being used. For example::
266
267     #-*-coding:iso-8859-1-*-
268     #
269     AlgorithmParameters = {
270         "Minimizer" : "CG", # Possible choice : "LBFGSB", "TNC", "CG", "BFGS"
271         "MaximumNumberOfSteps" : 10,
272         }
273
274 Then the script can be added to the ADAO case, in a file entry describing the
275 "*AlgorithmParameters*" keyword, as follows:
276
277   .. _adao_scriptentry02:
278   .. image:: images/adao_scriptentry02.png
279     :align: center
280     :width: 100%
281   .. centered::
282     **Adding parameters to control the algorithm**
283
284 Other steps and results are exactly the same as in the `Building a simple
285 estimation case with explicit data definition`_ previous example.
286
287 .. [#] For more information on YACS, see the the *YACS User Guide* available in the main "*Help*" menu of SALOME GUI.
288