1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2023 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
26 # ==============================================================================
27 class ElementaryAlgorithm(BasicObjects.Algorithm):
29 BasicObjects.Algorithm.__init__(self, "ENSEMBLEBLUE")
30 self.defineRequiredParameter(
31 name = "StoreInternalVariables",
34 message = "Stockage des variables internes ou intermédiaires du calcul",
36 self.defineRequiredParameter(
37 name = "StoreSupplementaryCalculations",
40 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
46 "SimulatedObservationAtBackground",
47 "SimulatedObservationAtCurrentState",
48 "SimulatedObservationAtOptimum",
51 self.defineRequiredParameter(
53 typecast = numpy.random.seed,
54 message = "Graine fixée pour le générateur aléatoire",
56 self.requireInputArguments(
57 mandatory= ("Xb", "Y", "HO", "R", "B"),
59 self.setAttributes(tags=(
67 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
68 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
70 # Précalcul des inversions de B et R
71 # ----------------------------------
75 # Nombre d'ensemble pour l'ébauche
76 # --------------------------------
77 nb_ens = Xb.stepnumber()
79 # Construction de l'ensemble des observations, par génération a partir
80 # de la diagonale de R
81 # --------------------------------------------------------------------
82 DiagonaleR = R.diag(Y.size)
83 EnsembleY = numpy.zeros([Y.size,nb_ens])
84 for npar in range(DiagonaleR.size):
85 bruit = numpy.random.normal(0,DiagonaleR[npar],nb_ens)
86 EnsembleY[npar,:] = Y[npar] + bruit
88 # Initialisation des opérateurs d'observation et de la matrice gain
89 # -----------------------------------------------------------------
91 Hm = HO["Tangent"].asMatrix(Xbm)
92 Hm = Hm.reshape(Y.size,Xbm.size) # ADAO & check shape
93 Ha = HO["Adjoint"].asMatrix(Xbm)
94 Ha = Ha.reshape(Xbm.size,Y.size) # ADAO & check shape
96 # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
97 # ------------------------------------------------------------------------
98 if Y.size <= Xb[0].size:
99 K = B * Ha * (R + Hm * (B * Ha)).I
101 K = (BI + Ha * (RI * Hm)).I * Ha * RI
103 # Calcul du BLUE pour chaque membre de l'ensemble
104 # -----------------------------------------------
105 for iens in range(nb_ens):
107 if self._toStore("SimulatedObservationAtBackground"):
108 self.StoredVariables["SimulatedObservationAtBackground"].store( HXb )
109 Innovation = numpy.ravel(EnsembleY[:,iens]) - numpy.ravel(HXb)
110 if self._toStore("Innovation"):
111 self.StoredVariables["Innovation"].store( Innovation )
112 Xa = Xb[iens] + K @ Innovation
113 self.StoredVariables["CurrentState"].store( Xa )
114 if self._toStore("SimulatedObservationAtCurrentState"):
115 self.StoredVariables["SimulatedObservationAtCurrentState"].store( Hm @ numpy.ravel(Xa) )
117 # Fabrication de l'analyse
118 # ------------------------
119 Members = self.StoredVariables["CurrentState"][-nb_ens:]
120 Xa = numpy.array( Members ).mean(axis=0)
121 self.StoredVariables["Analysis"].store( Xa )
122 if self._toStore("CurrentOptimum"):
123 self.StoredVariables["CurrentOptimum"].store( Xa )
124 if self._toStore("SimulatedObservationAtOptimum"):
125 self.StoredVariables["SimulatedObservationAtOptimum"].store( Hm @ numpy.ravel(Xa) )
130 # ==============================================================================
131 if __name__ == "__main__":
132 print('\n AUTODIAGNOSTIC\n')