Salome HOME
Minor correction after moving name of Logging to ExtendedLogging
[modules/adao.git] / src / daSalome / daYacsIntegration / daStudy.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2010-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 from daCore.AssimilationStudy import AssimilationStudy
24 #from daCore import Logging
25 import logging
26
27 class daError(Exception):
28   def __init__(self, value):
29     self.value = value
30   def __str__(self):
31     return repr(self.value)
32
33 class daStudy:
34
35   def __init__(self, name, algorithm, debug):
36
37     self.ADD = AssimilationStudy(name)
38     self.ADD.setControls()
39     self.algorithm = algorithm
40     self.algorithm_dict = None
41     self.Background = None
42     self.CheckingPoint = None
43     self.InputVariables = {}
44     self.OutputVariables = {}
45     self.InputVariablesOrder = []
46     self.OutputVariablesOrder = []
47     self.observers_dict = {}
48
49     self.debug = debug
50     if self.debug:
51       logging.getLogger().setLevel(logging.DEBUG)
52     else:
53       logging.getLogger().setLevel(logging.WARNING)
54
55     # Observation Management
56     self.ObservationOperatorType = {}
57     self.FunctionObservationOperator = {}
58
59     # Evolution Management
60     self.EvolutionModelType = {}
61     self.FunctionEvolutionModel = {}
62
63   #--------------------------------------
64
65   def setInputVariable(self, name, size):
66     self.InputVariables[name] = size
67     self.InputVariablesOrder.append(name)
68
69   def setOutputVariable(self, name, size):
70     self.OutputVariables[name] = size
71     self.OutputVariablesOrder.append(name)
72
73   #--------------------------------------
74
75   def setAlgorithmParameters(self, parameters):
76     self.algorithm_dict = parameters
77
78   #--------------------------------------
79
80   def initAlgorithm(self):
81     self.ADD.setAlgorithm(choice=self.algorithm)
82     if self.algorithm_dict != None:
83       logging.debug("DASTUDY AlgorithmParameters: "+str(self.algorithm_dict))
84       self.ADD.setAlgorithmParameters(asDico=self.algorithm_dict)
85
86   #--------------------------------------
87
88   def getAssimilationStudy(self):
89     return self.ADD
90
91   #--------------------------------------
92   # Methods to initialize AssimilationStudy
93
94   def setBackgroundType(self, Type):
95     if Type == "Vector":
96       self.BackgroundType = Type
97     else:
98       raise daError("[daStudy::setBackgroundType] The following type is unkown : %s. Authorized types are : Vector"%(Type,))
99
100   def setBackgroundStored(self, Stored):
101     if Stored:
102       self.BackgroundStored = True
103     else:
104       self.BackgroundStored = False
105
106   def setBackground(self, Background):
107     try:
108       self.BackgroundType
109       self.BackgroundStored
110     except AttributeError:
111       raise daError("[daStudy::setBackground] Type or Storage is not defined !")
112     self.Background = Background
113     if self.BackgroundType == "Vector":
114       self.ADD.setBackground(asVector = Background, toBeStored = self.BackgroundStored)
115
116   def getBackground(self):
117     return self.Background
118
119   #--------------------------------------
120
121   def setCheckingPointType(self, Type):
122     if Type == "Vector":
123       self.CheckingPointType = Type
124     else:
125       raise daError("[daStudy::setCheckingPointType] The following type is unkown : %s. Authorized types are : Vector"%(Type,))
126
127   def setCheckingPointStored(self, Stored):
128     if Stored:
129       self.CheckingPointStored = True
130     else:
131       self.CheckingPointStored = False
132
133   def setCheckingPoint(self, CheckingPoint):
134     try:
135       self.CheckingPointType
136       self.CheckingPointStored
137     except AttributeError:
138       raise daError("[daStudy::setCheckingPoint] Type or Storage is not defined !")
139     self.CheckingPoint = CheckingPoint
140     if self.CheckingPointType == "Vector":
141       self.ADD.setBackground(asVector = CheckingPoint, toBeStored = self.CheckingPointStored)
142
143   #--------------------------------------
144
145   def setBackgroundErrorType(self, Type):
146     if Type in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix"):
147       self.BackgroundErrorType = Type
148     else:
149       raise daError("[daStudy::setBackgroundErrorType] The following type is unkown : %s. Authorized types are : Matrix, ScalarSparseMatrix, DiagonalSparseMatrix"%(Type,))
150
151   def setBackgroundErrorStored(self, Stored):
152     if Stored:
153       self.BackgroundErrorStored = True
154     else:
155       self.BackgroundErrorStored = False
156
157   def setBackgroundError(self, BackgroundError):
158     try:
159       self.BackgroundErrorType
160       self.BackgroundErrorStored
161     except AttributeError:
162       raise daError("[daStudy::setBackgroundError] Type or Storage is not defined !")
163     if self.BackgroundErrorType == "Matrix":
164       self.ADD.setBackgroundError(asCovariance  = BackgroundError, toBeStored = self.BackgroundErrorStored)
165     if self.BackgroundErrorType == "ScalarSparseMatrix":
166       self.ADD.setBackgroundError(asEyeByScalar = BackgroundError, toBeStored = self.BackgroundErrorStored)
167     if self.BackgroundErrorType == "DiagonalSparseMatrix":
168       self.ADD.setBackgroundError(asEyeByVector = BackgroundError, toBeStored = self.BackgroundErrorStored)
169
170   #--------------------------------------
171
172   def setControlInputType(self, Type):
173     if Type in ("Vector", "VectorSerie"):
174       self.ControlInputType = Type
175     else:
176       raise daError("[daStudy::setControlInputType] The following type is unkown : %s. Authorized types are : Vector, VectorSerie"%(Type,))
177
178   def setControlInputStored(self, Stored):
179     if Stored:
180       self.ControlInputStored = True
181     else:
182       self.ControlInputStored = False
183
184   def setControlInput(self, ControlInput):
185     try:
186       self.ControlInputType
187       self.ControlInputStored
188     except AttributeError:
189       raise daError("[daStudy::setControlInput] Type or Storage is not defined !")
190     if self.ControlInputType == "Vector":
191       self.ADD.setControlInput(asVector = ControlInput, toBeStored = self.ControlInputStored)
192     if self.ControlInputType == "VectorSerie":
193       self.ADD.setControlInput(asPersistentVector = ControlInput, toBeStored = self.ControlInputStored)
194
195   #--------------------------------------
196
197   def setObservationType(self, Type):
198     if Type in ("Vector", "VectorSerie"):
199       self.ObservationType = Type
200     else:
201       raise daError("[daStudy::setObservationType] The following type is unkown : %s. Authorized types are : Vector, VectorSerie"%(Type,))
202
203   def setObservationStored(self, Stored):
204     if Stored:
205       self.ObservationStored = True
206     else:
207       self.ObservationStored = False
208
209   def setObservation(self, Observation):
210     try:
211       self.ObservationType
212       self.ObservationStored
213     except AttributeError:
214       raise daError("[daStudy::setObservation] Type or Storage is not defined !")
215     if self.ObservationType == "Vector":
216       self.ADD.setObservation(asVector = Observation, toBeStored = self.ObservationStored)
217     if self.ObservationType == "VectorSerie":
218       self.ADD.setObservation(asPersistentVector = Observation, toBeStored = self.ObservationStored)
219
220   #--------------------------------------
221
222   def setObservationErrorType(self, Type):
223     if Type in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix"):
224       self.ObservationErrorType = Type
225     else:
226       raise daError("[daStudy::setObservationErrorType] The following type is unkown : %s. Authorized types are : Matrix, ScalarSparseMatrix, DiagonalSparseMatrix"%(Type,))
227
228   def setObservationErrorStored(self, Stored):
229     if Stored:
230       self.ObservationErrorStored = True
231     else:
232       self.ObservationErrorStored = False
233
234   def setObservationError(self, ObservationError):
235     try:
236       self.ObservationErrorType
237       self.ObservationErrorStored
238     except AttributeError:
239       raise daError("[daStudy::setObservationError] Type or Storage is not defined !")
240     if self.ObservationErrorType == "Matrix":
241       self.ADD.setObservationError(asCovariance  = ObservationError, toBeStored = self.ObservationErrorStored)
242     if self.ObservationErrorType == "ScalarSparseMatrix":
243       self.ADD.setObservationError(asEyeByScalar = ObservationError, toBeStored = self.ObservationErrorStored)
244     if self.ObservationErrorType == "DiagonalSparseMatrix":
245       self.ADD.setObservationError(asEyeByVector = ObservationError, toBeStored = self.ObservationErrorStored)
246
247   #--------------------------------------
248
249   def getObservationOperatorType(self, Name):
250     rtn = None
251     try:
252       rtn = self.ObservationOperatorType[Name]
253     except:
254       pass
255     return rtn
256
257   def setObservationOperatorType(self, Name, Type):
258     if Type in ("Matrix", "Function"):
259       self.ObservationOperatorType[Name] = Type
260     else:
261       raise daError("[daStudy::setObservationOperatorType] The following type is unkown : %s. Authorized types are : Matrix, Function"%(Type,))
262
263   def setObservationOperator(self, Name, ObservationOperator):
264     try:
265       self.ObservationOperatorType[Name]
266     except AttributeError:
267       raise daError("[daStudy::setObservationOperator] Type is not defined !")
268     if self.ObservationOperatorType[Name] == "Matrix":
269       self.ADD.setObservationOperator(asMatrix = ObservationOperator)
270     elif self.ObservationOperatorType[Name] == "Function":
271       self.FunctionObservationOperator[Name] = ObservationOperator
272
273   #--------------------------------------
274
275   def setEvolutionErrorType(self, Type):
276     if Type in ("Matrix", "ScalarSparseMatrix", "DiagonalSparseMatrix"):
277       self.EvolutionErrorType = Type
278     else:
279       raise daError("[daStudy::setEvolutionErrorType] The following type is unkown : %s. Authorized types are : Matrix, ScalarSparseMatrix, DiagonalSparseMatrix"%(Type,))
280
281   def setEvolutionErrorStored(self, Stored):
282     if Stored:
283       self.EvolutionErrorStored = True
284     else:
285       self.EvolutionErrorStored = False
286
287   def setEvolutionError(self, EvolutionError):
288     try:
289       self.EvolutionErrorType
290       self.EvolutionErrorStored
291     except AttributeError:
292       raise daError("[daStudy::setEvolutionError] Type or Storage is not defined !")
293     if self.EvolutionErrorType == "Matrix":
294       self.ADD.setEvolutionError(asCovariance  = EvolutionError, toBeStored = self.EvolutionErrorStored)
295     if self.EvolutionErrorType == "ScalarSparseMatrix":
296       self.ADD.setEvolutionError(asEyeByScalar = EvolutionError, toBeStored = self.EvolutionErrorStored)
297     if self.EvolutionErrorType == "DiagonalSparseMatrix":
298       self.ADD.setEvolutionError(asEyeByVector = EvolutionError, toBeStored = self.EvolutionErrorStored)
299
300   #--------------------------------------
301
302   def getEvolutionModelType(self, Name):
303     rtn = None
304     try:
305       rtn = self.EvolutionModelType[Name]
306     except:
307       pass
308     return rtn
309
310   def setEvolutionModelType(self, Name, Type):
311     if Type in ("Matrix", "Function"):
312       self.EvolutionModelType[Name] = Type
313     else:
314       raise daError("[daStudy::setEvolutionModelType] The following type is unkown : %s. Authorized types are : Matrix, Function"%(Type,))
315
316   def setEvolutionModel(self, Name, EvolutionModel):
317     try:
318       self.EvolutionModelType[Name]
319     except AttributeError:
320       raise daError("[daStudy::setEvolutionModel] Type is not defined !")
321     if self.EvolutionModelType[Name] == "Matrix":
322       self.ADD.setEvolutionModel(asMatrix = EvolutionModel)
323     elif self.EvolutionModelType[Name] == "Function":
324       self.FunctionEvolutionModel[Name] = EvolutionModel
325
326   #--------------------------------------
327
328   def addObserver(self, name, scheduler, info, number):
329     self.observers_dict[name] = {}
330     self.observers_dict[name]["scheduler"] = scheduler
331     self.observers_dict[name]["info"] = info
332     self.observers_dict[name]["number"] = number
333
334   def getObservers(self):
335     return self.observers_dict