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