]> SALOME platform Git repositories - tools/adao_interface.git/blob - AdaoModelKeyVal.cxx
Salome HOME
First implementation
[tools/adao_interface.git] / AdaoModelKeyVal.cxx
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 #include "AdaoModelKeyVal.hxx"
22 #include "AdaoExchangeLayerException.hxx"
23
24 #include <sstream>
25
26 using namespace AdaoModel;
27
28 const char CostDecrementTolerance::KEY[]="CostDecrementTolerance";
29
30 const char StringObserver::KEY[]="String";
31
32 const char InfoObserver::KEY[]="Info";
33
34 const char StoreSupplKeyVal::KEY[]="StoreSupplementaryCalculations";
35
36 const char *StoreSupplKeyVal::DFTL[]={"CostFunctionJAtCurrentOptimum","CostFunctionJoAtCurrentOptimum","CurrentOptimum","SimulatedObservationAtCurrentOptimum","SimulatedObservationAtOptimum"};
37
38 const char EnumAlgoKeyVal::KEY[]="Algorithm";
39
40 const char ParametersOfAlgorithmParameters::KEY[]="Parameters";
41
42 const char Bounds::KEY[]="Bounds";
43
44 const char MaximumNumberOfSteps::KEY[]="MaximumNumberOfSteps";
45
46 const char VectorBackground::KEY[]="Vector";
47
48 const char StoreBackground::KEY[]="Stored";
49
50 const char MatrixBackgroundError::KEY[]="Matrix";
51
52 const char ScalarSparseMatrixError::KEY[]="ScalarSparseMatrix";
53
54 const char DiagonalSparseMatrixError::KEY[]="DiagonalSparseMatrix";
55
56 const char OneFunction::KEY[]="OneFunction";
57
58 const char DifferentialIncrement::KEY[]="DifferentialIncrement";
59
60 const char ObservationOperatorParameters::KEY[]="Parameters";
61
62 const char CenteredFiniteDifference::KEY[]="CenteredFiniteDifference";
63
64 const char InputFunctionAsMulti::KEY[]="InputFunctionAsMulti";
65
66 const char VariableKV::KEY[]="Variable";
67
68 const char TemplateKV::KEY[]="Template";
69
70 const char AlgorithmParameters::KEY[]="AlgorithmParameters";
71
72 const char Background::KEY[]="Background";
73
74 const char BackgroundError::KEY[]="BackgroundError";
75
76 const double BackgroundError::BACKGROUND_SCALAR_SPARSE_DFT = 1.e10;
77
78 const char ObservationError::KEY[]="ObservationError";
79
80 const char ObservationOperator::KEY[]="ObservationOperator";
81
82 const char Observation::KEY[]="Observation";
83
84 const double ObservationError::BACKGROUND_SCALAR_SPARSE_DFT = 1.;
85
86 const char ObserverEntry::KEY[]="Observer";
87
88 std::string TopEntry::getParamForSet(const GenericKeyVal& entry) const
89 {
90   std::ostringstream oss;
91   oss << "case.set(\'" << entry.getKey() << "\' , **" << entry.pyStr() << ")";
92   return oss.str();
93 }
94
95 std::string GenericKeyVal::pyStrKeyVal() const
96 {
97   std::ostringstream oss;
98   oss << "\"" << this->getKey() << "\" : " << this->pyStr();
99   return oss.str();
100 }
101
102 void GenericKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor)
103 {
104   visitor->visit(this);
105 }
106
107 std::string DoubleKeyVal::pyStr() const
108 {
109   std::ostringstream oss;
110   oss << std::scientific << _val;
111   return oss.str();
112 }
113
114 std::string BoolKeyVal::pyStr() const
115 {
116   return _val?"True":"False";
117 }
118
119 std::string StringKeyVal::pyStr() const
120 {
121   std::ostringstream oss;
122   oss << "\"" << _val << "\"";
123   return oss.str();
124 }
125
126 std::string NoneKeyVal::pyStr() const
127 {
128   return "None";
129 }
130
131 std::string ListStringsKeyVal::pyStr() const
132 {
133   std::ostringstream oss;
134   oss << "[ ";
135   std::size_t len(_val.size());
136   for(std::size_t i=0;i<len;++i)
137     {
138       oss << "\"" << _val[i] << "\"";
139       if(i!=len-1)
140         oss << ", ";
141     }
142   oss << " ]";
143   return oss.str();
144 }
145
146 StoreSupplKeyVal::StoreSupplKeyVal():ListStringsKeyVal(KEY)
147 {
148   _val.insert(_val.end(),DFTL,DFTL+sizeof(DFTL)/sizeof(char *));
149 }
150
151 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::generateDftParameters() const
152 {
153   switch(_enum)
154     {
155     case EnumAlgo::ThreeDVar:
156     case EnumAlgo::NonLinearLeastSquares:
157       {
158         return templateForOthers();
159       }
160     case EnumAlgo::Blue:
161       {
162         return templateForBlue();
163       }
164     default:
165       throw AdaoExchangeLayerException("EnumAlgoKeyVal::generateDftParameters : Unrecognized Algo !");
166     }
167 }
168
169 std::string EnumAlgoKeyVal::getRepresentation() const
170 {
171   switch(_enum)
172     {
173     case EnumAlgo::ThreeDVar:
174       return "3DVAR";
175     case EnumAlgo::NonLinearLeastSquares:
176       return "NonLinearLeastSquares";
177     case EnumAlgo::Blue:
178       return "Blue";
179     default:
180       throw AdaoExchangeLayerException("EnumAlgoKeyVal::getRepresentation : Unrecognized Algo !");
181     }
182 }
183
184 std::string EnumAlgoKeyVal::pyStr() const
185 {
186   std::ostringstream oss;
187   oss << "\"" << this->getRepresentation() << "\"";
188   return oss.str();
189 }
190
191 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::templateForBlue() const
192 {
193   std::shared_ptr<DictKeyVal> ret(std::make_shared<ParametersOfAlgorithmParameters>());
194   std::shared_ptr<StoreSupplKeyVal> v(std::make_shared<StoreSupplKeyVal>());
195   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v));
196   return ret;
197 }
198
199 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::templateForOthers() const
200 {
201   std::shared_ptr<DictKeyVal> ret(std::make_shared<ParametersOfAlgorithmParameters>());
202   std::shared_ptr<Bounds> v0(std::make_shared<Bounds>());
203   std::shared_ptr<MaximumNumberOfSteps> v1(std::make_shared<MaximumNumberOfSteps>());
204   std::shared_ptr<CostDecrementTolerance> v2(std::make_shared<CostDecrementTolerance>());
205   std::shared_ptr<StoreSupplKeyVal> v3(std::make_shared<StoreSupplKeyVal>());
206   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v0));
207   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v1));
208   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v2));
209   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v3));
210   return ret;
211 }
212
213 std::string DictKeyVal::pyStr() const
214 {
215   std::ostringstream oss;
216   oss << "{ ";
217   std::size_t len(_pairs.size());
218   for(std::size_t i=0;i<len;++i)
219     {
220       const auto& elt(_pairs[i]);
221       oss << elt->pyStrKeyVal();
222       if(i!=len-1)
223         oss << ", ";
224     }
225   oss << " }";
226   return oss.str();
227 }
228
229 void DictKeyVal::visitPython(MainModel *godFather, PythonLeafVisitor *visitor)
230 {
231   for(auto elt : _pairs)
232     {
233       elt->visitPython(godFather, visitor);
234     }
235 }
236
237 void DictKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor)
238 {
239   visitor->enterSubDir(this);
240   for(auto elt : _pairs)
241     {
242       elt->visitAll(godFather, visitor);
243     }
244   visitor->exitSubDir(this);
245 }
246
247 std::string PyObjKeyVal::pyStr() const
248 {
249   std::ostringstream oss;
250   oss << _var_name;
251   return oss.str();
252 }
253
254 void PyObjKeyVal::visitPython(MainModel *godFather, PythonLeafVisitor *visitor)
255 {
256   visitor->visit(godFather,this);
257 }
258
259 std::string UnsignedIntKeyVal::pyStr() const
260 {
261   std::ostringstream oss;
262   oss << _val;
263   return oss.str();
264 }
265
266 void InputFunctionAsMulti::setVal(bool val)
267 {
268   if(!val)
269     throw AdaoExchangeLayerException("InputFunctionAsMulti : value has to remain to true !");
270 }
271
272 ObservationOperatorParameters::ObservationOperatorParameters():DictKeyVal(KEY)
273 {
274   std::shared_ptr<DifferentialIncrement> v0(std::make_shared<DifferentialIncrement>());
275   std::shared_ptr<CenteredFiniteDifference> v1(std::make_shared<CenteredFiniteDifference>());
276   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DifferentialIncrement>(v0));
277   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,CenteredFiniteDifference>(v1));
278 }
279
280 AlgorithmParameters::AlgorithmParameters():DictKeyVal(KEY)
281 {
282   std::shared_ptr<EnumAlgoKeyVal> v0(std::make_shared<EnumAlgoKeyVal>());
283   std::shared_ptr<DictKeyVal> v1(v0->generateDftParameters());
284   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,EnumAlgoKeyVal>(v0));
285   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DictKeyVal>(v1));
286 }
287
288 Background::Background():DictKeyVal(KEY)
289 {
290   std::shared_ptr<VectorBackground> v0(std::make_shared<VectorBackground>());
291   std::shared_ptr<StoreBackground> v1(std::make_shared<StoreBackground>());
292   v1->setVal(true);
293   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VectorBackground>(v0));
294   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StoreBackground>(v1));
295 }
296
297 GenericError::GenericError(const std::string& key, double dftValForScalarSparseMatrix):DictKeyVal(key)
298 {
299   std::shared_ptr<MatrixBackgroundError> v0(std::make_shared<MatrixBackgroundError>());
300   std::shared_ptr<ScalarSparseMatrixError> v1(std::make_shared<ScalarSparseMatrixError>(dftValForScalarSparseMatrix));
301   std::shared_ptr<DiagonalSparseMatrixError> v2(std::make_shared<DiagonalSparseMatrixError>());
302   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,MatrixBackgroundError>(v0));
303   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,ScalarSparseMatrixError>(v1));
304   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DiagonalSparseMatrixError>(v2));
305 }
306
307 Observation::Observation():DictKeyVal(KEY)
308 {
309   std::shared_ptr<VectorBackground> v0(std::make_shared<VectorBackground>());
310   std::shared_ptr<StoreBackground> v1(std::make_shared<StoreBackground>());
311   v1->setVal(false);
312   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VectorBackground>(v0));
313   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StoreBackground>(v1));
314 }
315
316 ObservationOperator::ObservationOperator():DictKeyVal(KEY)
317 {
318   std::shared_ptr<OneFunction> v0(std::make_shared<OneFunction>());
319   std::shared_ptr<ObservationOperatorParameters> v1(std::make_shared<ObservationOperatorParameters>());
320   std::shared_ptr<InputFunctionAsMulti> v2(std::make_shared<InputFunctionAsMulti>());
321   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,OneFunction>(v0));
322   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,ObservationOperatorParameters>(v1));
323   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,InputFunctionAsMulti>(v2));
324 }
325
326 ObserverEntry::ObserverEntry():DictKeyVal(KEY)
327 {
328   std::shared_ptr<VariableKV> v0(std::make_shared<VariableKV>());
329   std::shared_ptr<TemplateKV> v1(std::make_shared<TemplateKV>());
330   std::shared_ptr<StringObserver> v2(std::make_shared<StringObserver>());
331   std::shared_ptr<InfoObserver> v3(std::make_shared<InfoObserver>());
332   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VariableKV>(v0));
333   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,TemplateKV>(v1));
334   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StringObserver>(v2));
335   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,InfoObserver>(v3));
336 }
337
338 MainModel::MainModel():_algo(std::make_shared<AlgorithmParameters>()),
339     _bg(std::make_shared<Background>()),
340     _bg_err(std::make_shared<BackgroundError>()),
341     _obs(std::make_shared<Observation>()),
342     _obs_err(std::make_shared<ObservationError>()),
343     _observ_op(std::make_shared<ObservationOperator>()),
344     _observ_entry(std::make_shared<ObserverEntry>())
345 {
346 }
347
348 std::string MainModel::pyStr() const
349 {
350   std::ostringstream oss;
351   oss << "from adao import adaoBuilder" << std::endl;
352   oss << "case = adaoBuilder.New()" << std::endl;
353   oss << _algo->getParamForSet(*_algo) << std::endl;
354   oss << _bg->getParamForSet(*_bg) << std::endl;
355   oss << _bg_err->getParamForSet(*_bg_err) << std::endl;
356   oss << _obs->getParamForSet(*_obs) << std::endl;
357   oss << _obs_err->getParamForSet(*_obs_err) << std::endl;
358   oss << _observ_op->getParamForSet(*_observ_op) << std::endl;
359   oss << _observ_entry->getParamForSet(*_observ_entry) << std::endl;
360   return oss.str();
361 }
362
363 std::vector< std::shared_ptr<GenericKeyVal> > MainModel::toVect() const
364 {
365   return {
366         std::static_pointer_cast<GenericKeyVal,AlgorithmParameters>(_algo),
367         std::static_pointer_cast<GenericKeyVal,Background>(_bg),
368         std::static_pointer_cast<GenericKeyVal,BackgroundError>(_bg_err),
369         std::static_pointer_cast<GenericKeyVal,Observation>(_obs),
370         std::static_pointer_cast<GenericKeyVal,ObservationError>(_obs_err),
371         std::static_pointer_cast<GenericKeyVal,ObservationOperator>(_observ_op),
372         std::static_pointer_cast<GenericKeyVal,ObserverEntry>(_observ_entry)
373   };
374 }
375
376 class MyAllVisitor : public RecursiveVisitor
377 {
378 public:
379   MyAllVisitor(GenericKeyVal *elt):_elt_to_find(elt) { }
380   void visit(GenericKeyVal *elt) override
381       {
382       if(_found) return ;
383       if(elt != _elt_to_find) return ;
384       _path.push_back(elt->getKey());
385       _found=true;
386       }
387   void enterSubDir(DictKeyVal *subdir) override
388       {
389       if(_found) return ;
390       _path.push_back(subdir->getKey());
391       }
392   void exitSubDir(DictKeyVal *subdir) override
393       {
394       if(_found) return ;
395       _path.pop_back();
396       }
397   std::string getPath() const
398   {
399     std::ostringstream oss;
400     std::size_t len(_path.size()),ii(0);
401     for(auto elt : _path)
402       {
403         oss << elt;
404         if(ii != len-1)
405           oss << "/";
406         ii++;
407       }
408     return oss.str();
409   }
410 private:
411   GenericKeyVal *_elt_to_find = nullptr;
412   std::vector<std::string> _path;
413   bool _found = false;
414 };
415
416 std::string MainModel::findPathOf(GenericKeyVal *elt)
417 {
418   MyAllVisitor vis(elt);
419   this->visitAll(&vis);
420   return vis.getPath();
421 }
422
423 void MainModel::visitPythonLeaves(PythonLeafVisitor *visitor)
424 {
425   std::vector< std::shared_ptr<GenericKeyVal> > sons(toVect());
426   for(auto elt : sons)
427     {
428       elt->visitPython(this, visitor);
429     }
430 }
431
432 void MainModel::visitAll(RecursiveVisitor *visitor)
433 {
434   std::vector< std::shared_ptr<GenericKeyVal> > sons(toVect());
435   for(auto elt : sons)
436     {
437       elt->visitAll(this, visitor);
438     }
439 }