Salome HOME
Updating copyright date information and version
[modules/adao.git] / src / daComposant / daAlgorithms / EnsembleBlue.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2015 EDF R&D
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 #  Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import logging
24 from daCore import BasicObjects
25 import numpy
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "ENSEMBLEBLUE")
31         self.defineRequiredParameter(
32             name     = "SetSeed",
33             typecast = numpy.random.seed,
34             message  = "Graine fixée pour le générateur aléatoire",
35             )
36
37     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
38         self._pre_run()
39         #
40         # Paramètres de pilotage
41         # ----------------------
42         self.setParameters(Parameters)
43         #
44         # Précalcul des inversions de B et R
45         # ----------------------------------
46         BI = B.getI()
47         RI = R.getI()
48         #
49         # Nombre d'ensemble pour l'ébauche
50         # --------------------------------
51         nb_ens = Xb.stepnumber()
52         #
53         # Construction de l'ensemble des observations, par génération a partir
54         # de la diagonale de R
55         # --------------------------------------------------------------------
56         DiagonaleR = R.diag(Y.size)
57         EnsembleY = numpy.zeros([Y.size,nb_ens])
58         for npar in range(DiagonaleR.size):
59             bruit = numpy.random.normal(0,DiagonaleR[npar],nb_ens)
60             EnsembleY[npar,:] = Y[npar] + bruit
61         EnsembleY = numpy.matrix(EnsembleY)
62         #
63         # Initialisation des opérateurs d'observation et de la matrice gain
64         # -----------------------------------------------------------------
65         Hm = HO["Tangent"].asMatrix(None)
66         Hm = Hm.reshape(Y.size,Xb[0].size) # ADAO & check shape
67         Ha = HO["Adjoint"].asMatrix(None)
68         Ha = Ha.reshape(Xb[0].size,Y.size) # ADAO & check shape
69         #
70         # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
71         # ------------------------------------------------------------------------
72         if Y.size <= Xb[0].size:
73             K  = B * Ha * (R + Hm * B * Ha).I
74         else:
75             K = (BI + Ha * RI * Hm).I * Ha * RI
76         #
77         # Calcul du BLUE pour chaque membre de l'ensemble
78         # -----------------------------------------------
79         for iens in range(nb_ens):
80             d  = EnsembleY[:,iens] - Hm * Xb[iens]
81             Xa = Xb[iens] + K*d
82             self.StoredVariables["CurrentState"].store( Xa )
83             self.StoredVariables["Innovation"].store( d.A1 )
84         #
85         # Fabrication de l'analyse
86         # ------------------------
87         Members = self.StoredVariables["CurrentState"][-nb_ens:]
88         Xa = numpy.matrix( Members ).mean(axis=0)
89         self.StoredVariables["Analysis"].store( Xa.A1 )
90         #
91         self._post_run(HO)
92         return 0
93
94 # ==============================================================================
95 if __name__ == "__main__":
96     print '\n AUTODIAGNOSTIC \n'