Salome HOME
Minor documentation and code review corrections (40)
[modules/adao.git] / src / daComposant / daAlgorithms / LocalSensitivityTest.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2023 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, numpy
24 from daCore import BasicObjects
25
26 # ==============================================================================
27 class ElementaryAlgorithm(BasicObjects.Algorithm):
28     def __init__(self):
29         BasicObjects.Algorithm.__init__(self, "LOCALSENSITIVITYTEST")
30         self.defineRequiredParameter(
31             name     = "SetDebug",
32             default  = False,
33             typecast = bool,
34             message  = "Activation du mode debug lors de l'exécution",
35             )
36         self.defineRequiredParameter(
37             name     = "StoreSupplementaryCalculations",
38             default  = ["JacobianMatrixAtCurrentState",],
39             typecast = tuple,
40             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
41             listval  = [
42                 "CurrentState",
43                 "JacobianMatrixAtCurrentState",
44                 "SimulatedObservationAtCurrentState",
45                 ]
46             )
47         self.requireInputArguments(
48             mandatory= ("Xb", "Y", "HO"),
49             )
50         self.setAttributes(tags=(
51             "Checking",
52             ))
53
54     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
55         self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
56         #
57         if self._parameters["SetDebug"]:
58             CUR_LEVEL = logging.getLogger().getEffectiveLevel()
59             logging.getLogger().setLevel(logging.DEBUG)
60             print("===> Beginning of evaluation, activating debug\n")
61             print("     %s\n"%("-"*75,))
62         #
63         # ----------
64         Ht = HO["Tangent"].asMatrix( Xb )
65         Ht = Ht.reshape(Y.size,Xb.size) # ADAO & check shape
66         # ----------
67         #
68         if self._parameters["SetDebug"]:
69             print("\n     %s\n"%("-"*75,))
70             print("===> End evaluation, deactivating debug if necessary\n")
71             logging.getLogger().setLevel(CUR_LEVEL)
72         #
73         if self._toStore("CurrentState"):
74             self.StoredVariables["CurrentState"].store( Xb )
75         if self._toStore("JacobianMatrixAtCurrentState"):
76             self.StoredVariables["JacobianMatrixAtCurrentState"].store( Ht )
77         if self._toStore("SimulatedObservationAtCurrentState"):
78             if HO["AppliedInX"] is not None and "HXb" in HO["AppliedInX"]:
79                 HXb = HO["AppliedInX"]["HXb"]
80             else:
81                 HXb = Ht @ Xb
82             HXb = numpy.ravel( HXb ).reshape((-1,1))
83             if Y.size != HXb.size:
84                 raise ValueError("The size %i of observations Yobs and %i of observed calculation F(X) are different, they have to be identical."%(Y.size,HXb.size))
85             if max(Y.shape) != max(HXb.shape):
86                 raise ValueError("The shapes %s of observations Yobs and %s of observed calculation F(X) are different, they have to be identical."%(Y.shape,HXb.shape))
87             self.StoredVariables["SimulatedObservationAtCurrentState"].store( HXb )
88         #
89         self._post_run(HO)
90         return 0
91
92 # ==============================================================================
93 if __name__ == "__main__":
94     print('\n AUTODIAGNOSTIC\n')