]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/LinearLeastSquares.py
Salome HOME
Unified management of supplementary variables to calculate or store
[modules/adao.git] / src / daComposant / daAlgorithms / LinearLeastSquares.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2012 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, "LINEARLEASTSQUARES")
33         self.defineRequiredParameter(
34             name     = "StoreSupplementaryCalculations",
35             default  = [],
36             typecast = tuple,
37             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
38             listval  = ["OMA"]
39             )
40
41     def run(self, Xb=None, Y=None, H=None, M=None, R=None, B=None, Q=None, Parameters=None):
42         """
43         Calcul de l'estimateur moindres carrés pondérés linéaires
44         (assimilation variationnelle sans ébauche)
45         """
46         logging.debug("%s Lancement"%self._name)
47         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
48         #
49         # Paramètres de pilotage
50         # ----------------------
51         self.setParameters(Parameters)
52         #
53         # Opérateur d'observation
54         # -----------------------
55         Hm = H["Tangent"].asMatrix(None)
56         Ha = H["Adjoint"].asMatrix(None)
57         #
58         if R is not None:
59             RI = R.I
60         elif self._parameters["R_scalar"] is not None:
61             RI = 1.0 / self._parameters["R_scalar"]
62         else:
63             raise ValueError("Observation error covariance matrix has to be properly defined!")
64         #
65         # Calcul de la matrice de gain et de l'analyse
66         # --------------------------------------------
67         K =  (Ha * RI * Hm ).I * Ha * RI
68         Xa =  K * Y
69         logging.debug("%s Analyse Xa = %s"%(self._name, Xa))
70         #
71         # Calcul de la fonction coût
72         # --------------------------
73         oma = Y - Hm * Xa
74         Jb  = 0.
75         Jo  = 0.5 * oma.T * RI * oma
76         J   = float( Jb ) + float( Jo )
77         logging.debug("%s CostFunction Jb = %s"%(self._name, Jb))
78         logging.debug("%s CostFunction Jo = %s"%(self._name, Jo))
79         logging.debug("%s CostFunction J  = %s"%(self._name, J))
80         #
81         self.StoredVariables["Analysis"].store( Xa.A1 )
82         self.StoredVariables["CostFunctionJb"].store( Jb )
83         self.StoredVariables["CostFunctionJo"].store( Jo )
84         self.StoredVariables["CostFunctionJ" ].store( J )
85         #
86         # Calculs et/ou stockages supplémentaires
87         # ---------------------------------------
88         if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
89             self.StoredVariables["OMA"].store( numpy.asmatrix(oma).flatten().A1 )
90         #
91         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
92         logging.debug("%s Terminé"%self._name)
93         #
94         return 0
95
96 # ==============================================================================
97 if __name__ == "__main__":
98     print '\n AUTODIAGNOSTIC \n'