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