]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/LinearLeastSquares.py
Salome HOME
Updating copyright date information (1)
[modules/adao.git] / src / daComposant / daAlgorithms / LinearLeastSquares.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2018 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
25 import numpy
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "LINEARLEASTSQUARES")
31         self.defineRequiredParameter(
32             name     = "StoreInternalVariables",
33             default  = False,
34             typecast = bool,
35             message  = "Stockage des variables internes ou intermédiaires du calcul",
36             )
37         self.defineRequiredParameter(
38             name     = "StoreSupplementaryCalculations",
39             default  = [],
40             typecast = tuple,
41             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
42             listval  = ["OMA", "CurrentState", "CostFunctionJ", "CostFunctionJb", "CostFunctionJo", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
43             )
44         self.requireInputArguments(
45             mandatory= ("Y", "HO", "R"),
46             )
47
48     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
49         self._pre_run(Parameters, Xb, Y, R, B, Q)
50         #
51         Hm = HO["Tangent"].asMatrix(None)
52         Hm = Hm.reshape(Y.size,-1) # ADAO & check shape
53         Ha = HO["Adjoint"].asMatrix(None)
54         Ha = Ha.reshape(-1,Y.size) # ADAO & check shape
55         #
56         RI = R.getI()
57         #
58         # Calcul de la matrice de gain et de l'analyse
59         # --------------------------------------------
60         K = (Ha * RI * Hm).I * Ha * RI
61         Xa =  K * Y
62         self.StoredVariables["Analysis"].store( Xa.A1 )
63         #
64         # Calcul de la fonction coût
65         # --------------------------
66         if self._parameters["StoreInternalVariables"] or \
67            "CostFunctionJ"                 in self._parameters["StoreSupplementaryCalculations"] or \
68            "OMA"                           in self._parameters["StoreSupplementaryCalculations"] or \
69            "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
70             HXa = Hm * Xa
71             oma = Y - HXa
72         if self._parameters["StoreInternalVariables"] or \
73            "CostFunctionJ"                 in self._parameters["StoreSupplementaryCalculations"]:
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 self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
84             self.StoredVariables["CurrentState"].store( numpy.ravel(Xa) )
85         if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
86             self.StoredVariables["OMA"].store( numpy.ravel(oma) )
87         if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
88             self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(HXa) )
89         if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
90             self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
91         #
92         self._post_run(HO)
93         return 0
94
95 # ==============================================================================
96 if __name__ == "__main__":
97     print('\n AUTODIAGNOSTIC \n')