]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/LinearLeastSquares.py
Salome HOME
Minor improvement of debug informations
[modules/adao.git] / src / daComposant / daAlgorithms / LinearLeastSquares.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, "LINEARLEASTSQUARES")
32         self.defineRequiredParameter(
33             name     = "StoreInternalVariables",
34             default  = False,
35             typecast = bool,
36             message  = "Stockage des variables internes ou intermédiaires du calcul",
37             )
38         self.defineRequiredParameter(
39             name     = "StoreSupplementaryCalculations",
40             default  = [],
41             typecast = tuple,
42             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
43             listval  = ["OMA"]
44             )
45
46     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
47         logging.debug("%s Lancement"%self._name)
48         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
49         #
50         # Paramètres de pilotage
51         # ----------------------
52         self.setParameters(Parameters)
53         #
54         # Opérateur d'observation
55         # -----------------------
56         Hm = HO["Tangent"].asMatrix(None)
57         Hm = Hm.reshape(Y.size,-1) # ADAO & check shape
58         Ha = HO["Adjoint"].asMatrix(None)
59         Ha = Ha.reshape(-1,Y.size) # ADAO & check shape
60         #
61         RI = R.getI()
62         #
63         # Calcul de la matrice de gain et de l'analyse
64         # --------------------------------------------
65         K = (Ha * RI * Hm).I * Ha * RI
66         Xa =  K * Y
67         self.StoredVariables["Analysis"].store( Xa.A1 )
68         #
69         # Calcul de la fonction coût
70         # --------------------------
71         if self._parameters["StoreInternalVariables"] or "OMA" in self._parameters["StoreSupplementaryCalculations"]:
72             oma = Y - Hm * Xa
73         if self._parameters["StoreInternalVariables"]:
74             Jb  = 0.
75             Jo  = 0.5 * oma.T * RI * oma
76             J   = float( Jb ) + float( Jo )
77             self.StoredVariables["CostFunctionJb"].store( Jb )
78             self.StoredVariables["CostFunctionJo"].store( Jo )
79             self.StoredVariables["CostFunctionJ" ].store( J )
80         #
81         # Calculs et/ou stockages supplémentaires
82         # ---------------------------------------
83         if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
84             self.StoredVariables["OMA"].store( numpy.ravel(oma) )
85         #
86         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)))
87         logging.debug("%s Nombre d'appels au cache d'opérateur d'observation direct/tangent/adjoint..: %i/%i/%i"%(self._name, HO["Direct"].nbcalls(3),HO["Tangent"].nbcalls(3),HO["Adjoint"].nbcalls(3)))
88         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
89         logging.debug("%s Terminé"%self._name)
90         #
91         return 0
92
93 # ==============================================================================
94 if __name__ == "__main__":
95     print '\n AUTODIAGNOSTIC \n'