Salome HOME
f9c8132622c29d10a344abb328cfe27b5cd7b62e
[tools/adao_interface.git] / AdaoModelKeyVal.hxx
1 // Copyright (C) 2019 EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author: Anthony Geay, anthony.geay@edf.fr, EDF R&D
20
21 #pragma once
22
23 #include "PyObjectRAII.hxx"
24
25 #include <string>
26 #include <memory>
27 #include <vector>
28
29 namespace AdaoModel
30 {
31   enum class Type
32   {
33       Double,
34       UnsignedInt,
35       Bool,
36       String,
37       PyObj,
38       EnumAlgo,
39       ListStrings,
40       None,
41       Child
42   };
43
44   enum class EnumAlgo
45   {
46       ThreeDVar,
47       Blue,
48       NonLinearLeastSquares
49   };
50
51   class GenericKeyVal;
52   class MainModel;
53   
54   class TopEntry
55   {
56   public:
57     std::string getParamForSet(const GenericKeyVal& entry) const;
58   };
59   
60   class PythonLeafVisitor;
61   class RecursiveVisitor;
62
63   class GenericKeyVal
64   {
65   public:
66     virtual bool isNeeded() const = 0;
67     virtual Type getType() const = 0;
68     virtual std::string pyStr() const = 0;
69     virtual void visitPython(MainModel *godFather, PythonLeafVisitor *visitor) { }
70     virtual void visitAll(MainModel *godFather, RecursiveVisitor *visitor);
71     virtual ~GenericKeyVal() { }
72     std::string pyStrKeyVal() const;
73     std::string getKey() const { return _key; }
74   protected:
75     GenericKeyVal(const std::string& key):_key(key) { }
76   private:
77     std::string _key;
78   };
79
80   class NeededGenericKeyVal : public GenericKeyVal
81   {
82   protected:
83     NeededGenericKeyVal(const std::string& key):GenericKeyVal(key) { }
84   public:
85     bool isNeeded() const override { return true; }
86   };
87
88   class NotNeededGenericKeyVal : public GenericKeyVal
89   {
90   public:
91     bool isNeeded() const override { return false; }
92   };
93
94   class DoubleKeyVal : public NeededGenericKeyVal
95   {
96   public:
97     DoubleKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
98     Type getType() const override { return Type::Double; }
99     void setVal(double val) { _val=val; }
100     double getVal() const { return _val; }
101     std::string pyStr() const override;
102   private:
103     double _val = 0.;
104   };
105
106   class BoolKeyVal : public NeededGenericKeyVal
107   {
108   public:
109     BoolKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
110     Type getType() const override { return Type::Bool; }
111     virtual void setVal(bool val) { _val=val; }
112     bool getVal() const { return _val; }
113     std::string pyStr() const override;
114   private:
115     bool _val = false;
116   };
117
118   class CostDecrementTolerance : public DoubleKeyVal
119   {
120   public:
121     CostDecrementTolerance():DoubleKeyVal(KEY) { setVal(1e-7); }
122   public:
123     static const char KEY[];
124   };
125   
126   class StringKeyVal : public NeededGenericKeyVal
127   {
128   public:
129     StringKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
130     Type getType() const override { return Type::String; }
131     std::string pyStr() const override;
132     void setVal(const std::string& val) { _val = val; }
133     std::string getVal() const { return _val; }
134   private:
135     std::string _val;
136   };
137
138   class NoneKeyVal : public NeededGenericKeyVal
139   {
140   public:
141     NoneKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
142     Type getType() const override { return Type::None; }
143     std::string pyStr() const override;
144   };
145
146   class StringObserver : public NoneKeyVal
147   {
148   public:
149     StringObserver():NoneKeyVal(KEY) { }
150   public:
151     static const char KEY[];
152   };
153
154   class InfoObserver : public NoneKeyVal
155   {
156   public:
157     InfoObserver():NoneKeyVal(KEY) { }
158   public:
159     static const char KEY[];
160   };
161
162   class ListStringsKeyVal : public NeededGenericKeyVal
163   {
164   protected:
165     ListStringsKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
166   public:
167     Type getType() const override { return Type::ListStrings; }
168     std::string pyStr() const override;
169   protected:
170     std::vector< std::string > _val;
171   };
172
173   class StoreSupplKeyVal : public ListStringsKeyVal
174   {
175   public:
176     StoreSupplKeyVal();
177   public:
178     static const char *DFTL[];
179     static const char KEY[];
180   };
181
182   class DictKeyVal;
183   
184   class EnumAlgoKeyVal : public NeededGenericKeyVal
185   {
186   public:
187     EnumAlgoKeyVal():NeededGenericKeyVal(KEY),_enum(EnumAlgo::ThreeDVar) { }
188     Type getType() const override { return Type::EnumAlgo; }
189     std::shared_ptr<DictKeyVal> generateDftParameters() const;
190     std::string getRepresentation() const;
191     std::string pyStr() const override;
192     void setVal(EnumAlgo newAlgo) { _enum = newAlgo; }
193     EnumAlgo getVal() const { return _enum; }
194   private:
195     std::shared_ptr<DictKeyVal> templateForBlue() const;
196     std::shared_ptr<DictKeyVal> templateForOthers() const;
197   private:
198     static const char KEY[];
199     EnumAlgo _enum;
200   };
201   
202   class DictKeyVal : public NeededGenericKeyVal
203   {
204   public:
205     DictKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
206     void pushBack(std::shared_ptr<GenericKeyVal> elt) { _pairs.push_back(elt); }
207     Type getType() const override { return Type::Child; }
208     std::string pyStr() const override;
209     void visitPython(MainModel *godFather, PythonLeafVisitor *visitor) override;
210     void visitAll(MainModel *godFather, RecursiveVisitor *visitor) override;
211   protected:
212     std::vector< std::shared_ptr<GenericKeyVal> > _pairs;
213   };
214
215   class ParametersOfAlgorithmParameters : public DictKeyVal
216   {
217   public:
218     ParametersOfAlgorithmParameters():DictKeyVal(KEY) { }
219   public:
220     static const char KEY[];
221   };
222
223   class PyObjKeyVal : public NeededGenericKeyVal
224   {
225   public:
226     PyObjKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
227     Type getType() const override { return Type::PyObj; }
228     void setVal(PyObject *obj) { _val = PyObjectRAII::FromBorrowed(obj); }
229     // borrowed ref
230     PyObject *getVal() const { return _val; }
231     std::string pyStr() const;
232     void setVarName(const std::string& vn) { _var_name = vn; }
233     void visitPython(MainModel *godFather, PythonLeafVisitor *visitor) override;
234   private:
235     PyObjectRAII _val;
236     std::string _var_name;
237   };
238
239   class Bounds : public PyObjKeyVal
240   {
241   public:
242     Bounds():PyObjKeyVal(KEY) { }
243   public:
244     static const char KEY[];
245   };
246
247   class UnsignedIntKeyVal : public NeededGenericKeyVal
248   {
249   public:
250     UnsignedIntKeyVal(const std::string& key):NeededGenericKeyVal(key) { }
251     void setVal(unsigned int val) { _val=val; }
252     unsigned int getVal() const { return _val; }
253     Type getType() const override { return Type::UnsignedInt; }
254     std::string pyStr() const;
255   private:
256     unsigned int _val = -1;
257   };
258   
259   class MaximumNumberOfSteps : public UnsignedIntKeyVal
260   {
261   public:
262     MaximumNumberOfSteps():UnsignedIntKeyVal(KEY) { setVal(100); }
263   public:
264     static const char KEY[];
265   };
266
267   class VectorBackground : public PyObjKeyVal
268   {
269   public:
270     VectorBackground():PyObjKeyVal(KEY) { }
271   public:
272     static const char KEY[];
273   };
274
275   class StoreBackground : public BoolKeyVal
276   {
277   public:
278     StoreBackground():BoolKeyVal(KEY) { }
279   public:
280     static const char KEY[];
281   };
282
283   class MatrixBackgroundError : public PyObjKeyVal
284   {
285   public:
286     MatrixBackgroundError():PyObjKeyVal(KEY) { }
287   public:
288     static const char KEY[];
289   };
290
291   class ScalarSparseMatrixError : public DoubleKeyVal
292   {
293   public:
294     ScalarSparseMatrixError(double val):DoubleKeyVal(KEY) { setVal(val); }
295   public:
296     static const char KEY[];
297   };
298
299   class DiagonalSparseMatrixError : public PyObjKeyVal
300   {
301   public:
302     DiagonalSparseMatrixError():PyObjKeyVal(KEY) { }
303   public:
304     static const char KEY[];
305   };
306
307   class OneFunction : public PyObjKeyVal
308   {
309   public:
310     OneFunction():PyObjKeyVal(KEY) { }
311   public:
312     static const char KEY[];
313   };
314
315   class DifferentialIncrement : public DoubleKeyVal
316   {
317   public:
318     DifferentialIncrement():DoubleKeyVal(KEY) { setVal(0.0001); }
319   public:
320     static const char KEY[];
321   };
322
323   class ObservationOperatorParameters : public DictKeyVal
324   {
325   public:
326     ObservationOperatorParameters();
327   public:
328     static const char KEY[];
329   };
330
331   class CenteredFiniteDifference : public BoolKeyVal
332   {
333   public:
334     CenteredFiniteDifference():BoolKeyVal(KEY) { setVal(false); }
335   public:
336     static const char KEY[];
337   };
338
339   class InputFunctionAsMulti : public BoolKeyVal
340   {
341   public:
342     InputFunctionAsMulti():BoolKeyVal(KEY) { BoolKeyVal::setVal(true); }
343     void setVal(bool val) override;
344   public:
345     static const char KEY[];
346   };
347
348   class VariableKV : public StringKeyVal
349   {
350   public:
351     VariableKV():StringKeyVal(KEY) { setVal("CurrentState"); }
352   public:
353     static const char KEY[];
354   };
355
356   class TemplateKV : public StringKeyVal
357   {
358   public:
359     TemplateKV():StringKeyVal(KEY) { setVal("CurrentState"); }
360   public:
361     static const char KEY[];
362   };
363
364   //////////////
365
366   class AlgorithmParameters : public DictKeyVal, public TopEntry
367   {
368   public:
369     AlgorithmParameters();
370   public:
371     static const char KEY[];
372   };
373
374   class Background : public DictKeyVal, public TopEntry
375   {
376   public:
377     Background();
378   public:
379     static const char KEY[];
380   };
381
382   class GenericError : public DictKeyVal, public TopEntry
383   {
384   protected:
385     GenericError(const std::string& key, double dftValForScalarSparseMatrix);
386   };
387
388   class BackgroundError : public GenericError
389   {
390   public:
391     BackgroundError():GenericError(KEY,BACKGROUND_SCALAR_SPARSE_DFT) { }
392   public:
393     static const char KEY[];
394     static const double BACKGROUND_SCALAR_SPARSE_DFT;
395   };
396
397   class ObservationError : public GenericError
398   {
399   public:
400     ObservationError():GenericError(KEY,BACKGROUND_SCALAR_SPARSE_DFT) { }
401   public:
402     static const char KEY[];
403     static const double BACKGROUND_SCALAR_SPARSE_DFT;
404   };
405
406   class ObservationOperator : public DictKeyVal, public TopEntry
407   {
408   public:
409     ObservationOperator();
410   public:
411     static const char KEY[];
412   };
413
414   class Observation : public DictKeyVal, public TopEntry
415   {
416   public:
417     Observation();
418   public:
419     static const char KEY[];
420   };
421
422   class ObserverEntry : public DictKeyVal, public TopEntry
423   {
424   public:
425     ObserverEntry();
426   public:
427     static const char KEY[];
428   };
429
430   ///////////////
431
432   class PythonLeafVisitor
433   {
434   public:
435     virtual void visit(MainModel *godFather, PyObjKeyVal *obj) = 0;
436     virtual ~PythonLeafVisitor() { }
437   };
438
439   class RecursiveVisitor
440   {
441   public:
442     virtual ~RecursiveVisitor() { }
443     virtual void visit(GenericKeyVal *obj) = 0;
444     virtual void enterSubDir(DictKeyVal *subdir) = 0;
445     virtual void exitSubDir(DictKeyVal *subdir) = 0;
446   };
447
448   class MainModel
449   {
450   public:
451     MainModel();
452     std::string findPathOf(GenericKeyVal *elt);
453     std::string pyStr() const;
454     std::vector< std::shared_ptr<GenericKeyVal> > toVect() const;
455     void visitPythonLeaves(PythonLeafVisitor *visitor);
456     void visitAll(RecursiveVisitor *visitor);
457   private:
458     std::shared_ptr<AlgorithmParameters> _algo;
459     std::shared_ptr<Background> _bg;
460     std::shared_ptr<BackgroundError> _bg_err;
461     std::shared_ptr<Observation> _obs;
462     std::shared_ptr<ObservationError> _obs_err;
463     std::shared_ptr<ObservationOperator> _observ_op;
464     std::shared_ptr<ObserverEntry> _observ_entry;
465   };
466 }