Salome HOME
Minor source update for OM compatibility
[modules/adao.git] / src / daComposant / daAlgorithms / 4DVAR.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2024 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 numpy
24 from daCore import BasicObjects
25 from daAlgorithms.Atoms import std4dvar
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29
30     def __init__(self):
31         BasicObjects.Algorithm.__init__(self, "4DVAR")
32         self.defineRequiredParameter(
33             name     = "ConstrainedBy",
34             default  = "EstimateProjection",
35             typecast = str,
36             message  = "Prise en compte des contraintes",
37             listval  = ["EstimateProjection"],
38         )
39         self.defineRequiredParameter(
40             name     = "Variant",
41             default  = "4DVAR",
42             typecast = str,
43             message  = "Variant ou formulation de la méthode",
44             listval  = [
45                 "4DVAR",
46             ],
47             listadv  = [
48                 "4DVAR-Std",
49             ],
50         )
51         self.defineRequiredParameter(
52             name     = "EstimationOf",
53             default  = "State",
54             typecast = str,
55             message  = "Estimation d'état ou de paramètres",
56             listval  = ["State", "Parameters"],
57         )
58         self.defineRequiredParameter(
59             name     = "Minimizer",
60             default  = "LBFGSB",
61             typecast = str,
62             message  = "Minimiseur utilisé",
63             listval  = [
64                 "LBFGSB",
65                 "TNC",
66                 "CG",
67                 "BFGS",
68             ],
69             listadv  = [
70                 "NCG",
71             ],
72         )
73         self.defineRequiredParameter(
74             name     = "MaximumNumberOfIterations",
75             default  = 15000,
76             typecast = int,
77             message  = "Nombre maximal de pas d'optimisation",
78             minval   = -1,
79             oldname  = "MaximumNumberOfSteps",
80         )
81         self.defineRequiredParameter(
82             name     = "CostDecrementTolerance",
83             default  = 1.e-7,
84             typecast = float,
85             message  = "Diminution relative minimale du coût lors de l'arrêt",
86             minval   = 0.,
87         )
88         self.defineRequiredParameter(
89             name     = "ProjectedGradientTolerance",
90             default  = -1,
91             typecast = float,
92             message  = "Maximum des composantes du gradient projeté lors de l'arrêt",
93             minval   = -1,
94         )
95         self.defineRequiredParameter(
96             name     = "GradientNormTolerance",
97             default  = 1.e-05,
98             typecast = float,
99             message  = "Maximum des composantes du gradient lors de l'arrêt",
100             minval   = 0.,
101         )
102         self.defineRequiredParameter(
103             name     = "StoreInternalVariables",
104             default  = False,
105             typecast = bool,
106             message  = "Stockage des variables internes ou intermédiaires du calcul",
107         )
108         self.defineRequiredParameter(
109             name     = "StoreSupplementaryCalculations",
110             default  = [],
111             typecast = tuple,
112             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
113             listval  = [
114                 "Analysis",
115                 "BMA",
116                 "CostFunctionJ",
117                 "CostFunctionJAtCurrentOptimum",
118                 "CostFunctionJb",
119                 "CostFunctionJbAtCurrentOptimum",
120                 "CostFunctionJo",
121                 "CostFunctionJoAtCurrentOptimum",
122                 "CurrentIterationNumber",
123                 "CurrentOptimum",
124                 "CurrentState",
125                 "IndexOfOptimum",
126             ]
127         )
128         self.defineRequiredParameter(  # Pas de type
129             name     = "Bounds",
130             message  = "Liste des valeurs de bornes",
131         )
132         self.defineRequiredParameter(
133             name     = "InitializationPoint",
134             typecast = numpy.ravel,
135             message  = "État initial imposé (par défaut, c'est l'ébauche si None)",
136         )
137         self.requireInputArguments(
138             mandatory= ("Xb", "Y", "HO", "EM", "R", "B"),
139             optional = ("U", "CM", "Q"),
140         )
141         self.setAttributes(
142             tags=(
143                 "DataAssimilation",
144                 "NonLinear",
145                 "Variational",
146                 "Dynamic",
147             ),
148             features=(
149                 "NonLocalOptimization",
150                 "DerivativeNeeded",
151                 "ParallelDerivativesOnly",
152                 "ConvergenceOnBoth",
153             ),
154         )
155
156     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
157         self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
158         #
159         # --------------------------
160         if self._parameters["Variant"] in ["4DVAR", "4DVAR-Std"]:
161             std4dvar.std4dvar(self, Xb, Y, U, HO, EM, CM, R, B, Q)
162         #
163         # --------------------------
164         else:
165             raise ValueError("Error in Variant name: %s"%self._parameters["Variant"])
166         #
167         self._post_run(HO, EM)
168         return 0
169
170 # ==============================================================================
171 if __name__ == "__main__":
172     print("\n AUTODIAGNOSTIC\n")