Salome HOME
Improving and simplifying result variables access methods
[modules/adao.git] / src / daComposant / daAlgorithms / LinearLeastSquares.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, "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, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
42         logging.debug("%s Lancement"%self._name)
43         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
44         #
45         # Paramètres de pilotage
46         # ----------------------
47         self.setParameters(Parameters)
48         #
49         # Opérateur d'observation
50         # -----------------------
51         Hm = HO["Tangent"].asMatrix(None)
52         Ha = HO["Adjoint"].asMatrix(None)
53         #
54         if R is not None:
55             RI = R.I
56         elif self._parameters["R_scalar"] is not None:
57             RI = 1.0 / self._parameters["R_scalar"]
58         else:
59             raise ValueError("Observation error covariance matrix has to be properly defined!")
60         #
61         # Calcul de la matrice de gain et de l'analyse
62         # --------------------------------------------
63         K =  (Ha * RI * Hm ).I * Ha * RI
64         Xa =  K * Y
65         #
66         # Calcul de la fonction coût
67         # --------------------------
68         oma = Y - Hm * Xa
69         Jb  = 0.
70         Jo  = 0.5 * oma.T * RI * oma
71         J   = float( Jb ) + float( Jo )
72         self.StoredVariables["Analysis"].store( Xa.A1 )
73         self.StoredVariables["CostFunctionJb"].store( Jb )
74         self.StoredVariables["CostFunctionJo"].store( Jo )
75         self.StoredVariables["CostFunctionJ" ].store( J )
76         #
77         # Calculs et/ou stockages supplémentaires
78         # ---------------------------------------
79         if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
80             self.StoredVariables["OMA"].store( numpy.ravel(oma) )
81         #
82         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
83         logging.debug("%s Terminé"%self._name)
84         #
85         return 0
86
87 # ==============================================================================
88 if __name__ == "__main__":
89     print '\n AUTODIAGNOSTIC \n'