1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2013 EDF R&D
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 from daCore import BasicObjects, PlatformInfo
25 m = PlatformInfo.SystemUsage()
29 # ==============================================================================
30 class ElementaryAlgorithm(BasicObjects.Algorithm):
32 BasicObjects.Algorithm.__init__(self, "ENSEMBLEBLUE")
33 self.defineRequiredParameter(
35 typecast = numpy.random.seed,
36 message = "Graine fixée pour le générateur aléatoire",
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")))
43 # Paramètres de pilotage
44 # ----------------------
45 self.setParameters(Parameters)
47 # Précalcul des inversions de B et R
48 # ----------------------------------
51 elif self._parameters["B_scalar"] is not None:
52 BI = 1.0 / self._parameters["B_scalar"]
53 B = self._parameters["B_scalar"]
55 raise ValueError("Background error covariance matrix has to be properly defined!")
59 elif self._parameters["R_scalar"] is not None:
60 RI = 1.0 / self._parameters["R_scalar"]
62 raise ValueError("Observation error covariance matrix has to be properly defined!")
64 # Nombre d'ensemble pour l'ébauche
65 # --------------------------------
66 nb_ens = Xb.stepnumber()
68 # Construction de l'ensemble des observations, par génération a partir
69 # de la diagonale de R
70 # --------------------------------------------------------------------
71 DiagonaleR = numpy.diag(R)
72 EnsembleY = numpy.zeros([Y.size,nb_ens])
73 for npar in range(DiagonaleR.size) :
74 bruit = numpy.random.normal(0,DiagonaleR[npar],nb_ens)
75 EnsembleY[npar,:] = Y[npar] + bruit
76 EnsembleY = numpy.matrix(EnsembleY)
78 # Initialisation des opérateurs d'observation et de la matrice gain
79 # -----------------------------------------------------------------
80 Hm = HO["Tangent"].asMatrix(None)
81 Ha = HO["Adjoint"].asMatrix(None)
83 # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
84 # ------------------------------------------------------------------------
85 if Y.size <= Xb[0].size:
86 if self._parameters["R_scalar"] is not None:
87 R = self._parameters["R_scalar"] * numpy.eye(Y.size, dtype=numpy.float)
88 K = B * Ha * (Hm * B * Ha + R).I
90 K = (Ha * RI * Hm + BI).I * Ha * RI
92 # Calcul du BLUE pour chaque membre de l'ensemble
93 # -----------------------------------------------
94 for iens in range(nb_ens):
95 d = EnsembleY[:,iens] - Hm * Xb[iens]
98 self.StoredVariables["CurrentState"].store( Xa.A1 )
99 self.StoredVariables["Innovation"].store( d.A1 )
101 # Fabrication de l'analyse
102 # ------------------------
103 Members = self.StoredVariables["CurrentState"][-nb_ens:]
104 Xa = numpy.matrix( Members ).mean(axis=0)
105 self.StoredVariables["Analysis"].store( Xa.A1 )
107 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
108 logging.debug("%s Terminé"%self._name)
111 # ==============================================================================
112 if __name__ == "__main__":
113 print '\n AUTODIAGNOSTIC \n'