Salome HOME
Minot improvement of debug or error informations
[modules/adao.git] / src / daComposant / daAlgorithms / EnsembleBlue.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2013 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, PlatformInfo
25 m = PlatformInfo.SystemUsage()
26
27 import numpy
28
29 # ==============================================================================
30 class ElementaryAlgorithm(BasicObjects.Algorithm):
31     def __init__(self):
32         BasicObjects.Algorithm.__init__(self, "ENSEMBLEBLUE")
33         self.defineRequiredParameter(
34             name     = "SetSeed",
35             typecast = numpy.random.seed,
36             message  = "Graine fixée pour le générateur aléatoire",
37             )
38
39     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
40         logging.debug("%s Lancement"%self._name)
41         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
42         #
43         # Paramètres de pilotage
44         # ----------------------
45         self.setParameters(Parameters)
46         #
47         # Précalcul des inversions de B et R
48         # ----------------------------------
49         BI = B.getI()
50         RI = R.getI()
51         #
52         # Nombre d'ensemble pour l'ébauche
53         # --------------------------------
54         nb_ens = Xb.stepnumber()
55         #
56         # Construction de l'ensemble des observations, par génération a partir
57         # de la diagonale de R
58         # --------------------------------------------------------------------
59         DiagonaleR = R.diag(Y.size)
60         EnsembleY = numpy.zeros([Y.size,nb_ens])
61         for npar in range(DiagonaleR.size):
62             bruit = numpy.random.normal(0,DiagonaleR[npar],nb_ens)
63             EnsembleY[npar,:] = Y[npar] + bruit
64         EnsembleY = numpy.matrix(EnsembleY)
65         #
66         # Initialisation des opérateurs d'observation et de la matrice gain
67         # -----------------------------------------------------------------
68         Hm = HO["Tangent"].asMatrix(None)
69         Hm = Hm.reshape(Y.size,Xb[0].size) # ADAO & check shape
70         Ha = HO["Adjoint"].asMatrix(None)
71         Ha = Ha.reshape(Xb[0].size,Y.size) # ADAO & check shape
72         #
73         # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
74         # ------------------------------------------------------------------------
75         if Y.size <= Xb[0].size:
76             K  = B * Ha * (R + Hm * B * Ha).I
77         else:
78             K = (BI + Ha * RI * Hm).I * Ha * RI
79         #
80         # Calcul du BLUE pour chaque membre de l'ensemble
81         # -----------------------------------------------
82         for iens in range(nb_ens):
83             d  = EnsembleY[:,iens] - Hm * Xb[iens]
84             Xa = Xb[iens] + K*d
85             
86             self.StoredVariables["CurrentState"].store( Xa.A1 )
87             self.StoredVariables["Innovation"].store( d.A1 )
88         #
89         # Fabrication de l'analyse
90         # ------------------------
91         Members = self.StoredVariables["CurrentState"][-nb_ens:]
92         Xa = numpy.matrix( Members ).mean(axis=0)
93         self.StoredVariables["Analysis"].store( Xa.A1 )
94         #
95         logging.debug("%s Nombre d'évaluation(s) de l'opérateur d'observation direct/tangent/adjoint : %i/%i/%i"%(self._name, HO["Direct"].nbcalls()[0],HO["Tangent"].nbcalls()[0],HO["Adjoint"].nbcalls()[0]))
96         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
97         logging.debug("%s Terminé"%self._name)
98         return 0
99
100 # ==============================================================================
101 if __name__ == "__main__":
102     print '\n AUTODIAGNOSTIC \n'