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