Salome HOME
Improvements of inversions in algorithms
[modules/adao.git] / src / daComposant / daAlgorithms / LinearLeastSquares.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2011  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
22 import logging
23 from daCore import BasicObjects, PlatformInfo
24 m = PlatformInfo.SystemUsage()
25
26 # ==============================================================================
27 class ElementaryAlgorithm(BasicObjects.Algorithm):
28     def __init__(self):
29         BasicObjects.Algorithm.__init__(self)
30         self._name = "LINEARLEASTSQUARES"
31
32     def run(self, Xb=None, Y=None, H=None, M=None, R=None, B=None, Q=None, Parameters=None):
33         """
34         Calcul de l'estimateur moindres carrés pondérés linéaires
35         (assimilation variationnelle sans ébauche)
36         """
37         logging.debug("%s Lancement"%self._name)
38         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("Mo")))
39         #
40         Hm = H["Direct"].asMatrix()
41         Ht = H["Adjoint"].asMatrix()
42         #
43         if R is not None:
44             RI = R.I
45         elif Parameters["R_scalar"] is not None:
46             RI = 1.0 / Parameters["R_scalar"]
47         #
48         K =  (Ht * RI * Hm ).I * Ht * RI
49         Xa =  K * Y
50         logging.debug("%s Analyse Xa = %s"%(self._name, Xa))
51         #
52         # Calcul de la fonction coût
53         # --------------------------
54         d  = Y - Hm * Xa
55         Jb  = 0.
56         Jo  = 0.5 * d.T * RI * d
57         J   = float( Jb ) + float( Jo )
58         logging.debug("%s CostFunction Jb = %s"%(self._name, Jb))
59         logging.debug("%s CostFunction Jo = %s"%(self._name, Jo))
60         logging.debug("%s CostFunction J  = %s"%(self._name, J))
61         #
62         self.StoredVariables["Analysis"].store( Xa.A1 )
63         self.StoredVariables["Innovation"].store( d.A1 )
64         self.StoredVariables["CostFunctionJb"].store( Jb )
65         self.StoredVariables["CostFunctionJo"].store( Jo )
66         self.StoredVariables["CostFunctionJ" ].store( J )
67         #
68         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("Mo")))
69         logging.debug("%s Terminé"%self._name)
70         #
71         return 0
72
73 # ==============================================================================
74 if __name__ == "__main__":
75     print '\n AUTODIAGNOSTIC \n'