Salome HOME
Remove useless dependency to py2cpp.
[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::string val(this->pyStr());
98   if( !val.empty() )
99     {
100       std::ostringstream oss;
101       oss << "\"" << this->getKey() << "\" : " << val;
102       return oss.str();
103     }
104   else
105     return std::string();
106 }
107
108 void GenericKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor)
109 {
110   visitor->visit(this);
111 }
112
113 std::string DoubleKeyVal::pyStr() const
114 {
115   std::ostringstream oss;
116   oss << std::scientific << _val;
117   return oss.str();
118 }
119
120 std::string BoolKeyVal::pyStr() const
121 {
122   return _val?"True":"False";
123 }
124
125 std::string StringKeyVal::pyStr() const
126 {
127   std::ostringstream oss;
128   oss << "\"" << _val << "\"";
129   return oss.str();
130 }
131
132 std::string NoneKeyVal::pyStr() const
133 {
134   return "None";
135 }
136
137 std::string ListStringsKeyVal::pyStr() const
138 {
139   std::ostringstream oss;
140   oss << "[ ";
141   std::size_t len(_val.size());
142   for(std::size_t i=0;i<len;++i)
143     {
144       oss << "\"" << _val[i] << "\"";
145       if(i!=len-1)
146         oss << ", ";
147     }
148   oss << " ]";
149   return oss.str();
150 }
151
152 StoreSupplKeyVal::StoreSupplKeyVal():ListStringsKeyVal(KEY)
153 {
154   _val.insert(_val.end(),DFTL,DFTL+sizeof(DFTL)/sizeof(char *));
155 }
156
157 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::generateDftParameters() const
158 {
159   switch(_enum)
160     {
161     case EnumAlgo::ThreeDVar:
162     case EnumAlgo::LinearLeastSquares:
163     case EnumAlgo::NonLinearLeastSquares:
164       {
165         return templateForOthers();
166       }
167     case EnumAlgo::Blue:
168       {
169         return templateForBlue();
170       }
171     default:
172       throw AdaoExchangeLayerException("EnumAlgoKeyVal::generateDftParameters : Unrecognized Algo !");
173     }
174 }
175
176 std::string EnumAlgoKeyVal::getRepresentation() const
177 {
178   switch(_enum)
179     {
180     case EnumAlgo::ThreeDVar:
181       return "3DVAR";
182     case EnumAlgo::NonLinearLeastSquares:
183       return "NonLinearLeastSquares";
184     case EnumAlgo::LinearLeastSquares:
185       return "LinearLeastSquares";
186     case EnumAlgo::Blue:
187       return "Blue";
188     default:
189       throw AdaoExchangeLayerException("EnumAlgoKeyVal::getRepresentation : Unrecognized Algo !");
190     }
191 }
192
193 std::string EnumAlgoKeyVal::pyStr() const
194 {
195   std::ostringstream oss;
196   oss << "\"" << this->getRepresentation() << "\"";
197   return oss.str();
198 }
199
200 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::templateForBlue() const
201 {
202   std::shared_ptr<DictKeyVal> ret(std::make_shared<ParametersOfAlgorithmParameters>());
203   std::shared_ptr<StoreSupplKeyVal> v(std::make_shared<StoreSupplKeyVal>());
204   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v));
205   return ret;
206 }
207
208 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::templateForOthers() const
209 {
210   std::shared_ptr<DictKeyVal> ret(std::make_shared<ParametersOfAlgorithmParameters>());
211   std::shared_ptr<Bounds> v0(std::make_shared<Bounds>());
212   std::shared_ptr<MaximumNumberOfSteps> v1(std::make_shared<MaximumNumberOfSteps>());
213   std::shared_ptr<CostDecrementTolerance> v2(std::make_shared<CostDecrementTolerance>());
214   std::shared_ptr<StoreSupplKeyVal> v3(std::make_shared<StoreSupplKeyVal>());
215   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v0));
216   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v1));
217   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v2));
218   ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v3));
219   return ret;
220 }
221
222 std::string DictKeyVal::pyStr() const
223 {
224   std::vector<std::string> vect;
225   std::size_t len(_pairs.size());
226   for(std::size_t i=0;i<len;++i)
227     {
228       const auto& elt(_pairs[i]);
229       std::string cont(elt->pyStrKeyVal());
230       if( ! cont.empty() )
231         vect.push_back(cont);
232     }
233   len = vect.size();
234   std::ostringstream oss;
235   oss << "{ ";
236   for(std::size_t i=0;i<len;++i)
237     {
238       oss << vect[i];
239       if(i!=len-1)
240         oss << ", ";
241     }
242   oss << " }";
243   return oss.str();
244 }
245
246 void DictKeyVal::visitPython(MainModel *godFather, PythonLeafVisitor *visitor)
247 {
248   for(auto elt : _pairs)
249     {
250       elt->visitPython(godFather, visitor);
251     }
252 }
253
254 void DictKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor)
255 {
256   visitor->enterSubDir(this);
257   for(auto elt : _pairs)
258     {
259       elt->visitAll(godFather, visitor);
260     }
261   visitor->exitSubDir(this);
262 }
263
264 std::string PyObjKeyVal::pyStr() const
265 {
266   std::ostringstream oss;
267   oss << _var_name;
268   return oss.str();
269 }
270
271 void PyObjKeyVal::visitPython(MainModel *godFather, PythonLeafVisitor *visitor)
272 {
273   visitor->visit(godFather,this);
274 }
275
276 std::string UnsignedIntKeyVal::pyStr() const
277 {
278   std::ostringstream oss;
279   oss << _val;
280   return oss.str();
281 }
282
283 void InputFunctionAsMulti::setVal(bool val)
284 {
285   if(!val)
286     throw AdaoExchangeLayerException("InputFunctionAsMulti : value has to remain to true !");
287 }
288
289 ObservationOperatorParameters::ObservationOperatorParameters():DictKeyVal(KEY)
290 {
291   std::shared_ptr<DifferentialIncrement> v0(std::make_shared<DifferentialIncrement>());
292   std::shared_ptr<CenteredFiniteDifference> v1(std::make_shared<CenteredFiniteDifference>());
293   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DifferentialIncrement>(v0));
294   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,CenteredFiniteDifference>(v1));
295 }
296
297 AlgorithmParameters::AlgorithmParameters():DictKeyVal(KEY)
298 {
299   std::shared_ptr<EnumAlgoKeyVal> v0(std::make_shared<EnumAlgoKeyVal>());
300   std::shared_ptr<DictKeyVal> v1(v0->generateDftParameters());
301   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,EnumAlgoKeyVal>(v0));
302   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DictKeyVal>(v1));
303 }
304
305 Background::Background():DictKeyVal(KEY)
306 {
307   std::shared_ptr<VectorBackground> v0(std::make_shared<VectorBackground>());
308   std::shared_ptr<StoreBackground> v1(std::make_shared<StoreBackground>());
309   v1->setVal(true);
310   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VectorBackground>(v0));
311   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StoreBackground>(v1));
312 }
313
314 GenericError::GenericError(const std::string& key, double dftValForScalarSparseMatrix):DictKeyVal(key)
315 {
316   std::shared_ptr<MatrixBackgroundError> v0(std::make_shared<MatrixBackgroundError>());
317   std::shared_ptr<ScalarSparseMatrixError> v1(std::make_shared<ScalarSparseMatrixError>(dftValForScalarSparseMatrix));
318   std::shared_ptr<DiagonalSparseMatrixError> v2(std::make_shared<DiagonalSparseMatrixError>());
319   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,MatrixBackgroundError>(v0));
320   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,ScalarSparseMatrixError>(v1));
321   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DiagonalSparseMatrixError>(v2));
322 }
323
324 Observation::Observation():DictKeyVal(KEY)
325 {
326   std::shared_ptr<VectorBackground> v0(std::make_shared<VectorBackground>());
327   std::shared_ptr<StoreBackground> v1(std::make_shared<StoreBackground>());
328   v1->setVal(false);
329   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VectorBackground>(v0));
330   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StoreBackground>(v1));
331 }
332
333 ObservationOperator::ObservationOperator():DictKeyVal(KEY)
334 {
335   std::shared_ptr<OneFunction> v0(std::make_shared<OneFunction>());
336   std::shared_ptr<MatrixBackgroundError> v1(std::make_shared<MatrixBackgroundError>());
337   std::shared_ptr<ObservationOperatorParameters> v2(std::make_shared<ObservationOperatorParameters>());
338   std::shared_ptr<InputFunctionAsMulti> v3(std::make_shared<InputFunctionAsMulti>());
339   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,OneFunction>(v0));
340   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,MatrixBackgroundError>(v1));
341   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,ObservationOperatorParameters>(v2));
342   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,InputFunctionAsMulti>(v3));
343 }
344
345 ObserverEntry::ObserverEntry():DictKeyVal(KEY)
346 {
347   std::shared_ptr<VariableKV> v0(std::make_shared<VariableKV>());
348   std::shared_ptr<TemplateKV> v1(std::make_shared<TemplateKV>());
349   std::shared_ptr<StringObserver> v2(std::make_shared<StringObserver>());
350   std::shared_ptr<InfoObserver> v3(std::make_shared<InfoObserver>());
351   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VariableKV>(v0));
352   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,TemplateKV>(v1));
353   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StringObserver>(v2));
354   _pairs.push_back(std::static_pointer_cast<GenericKeyVal,InfoObserver>(v3));
355 }
356
357 MainModel::MainModel():_algo(std::make_shared<AlgorithmParameters>()),
358     _bg(std::make_shared<Background>()),
359     _bg_err(std::make_shared<BackgroundError>()),
360     _obs(std::make_shared<Observation>()),
361     _obs_err(std::make_shared<ObservationError>()),
362     _observ_op(std::make_shared<ObservationOperator>()),
363     _observ_entry(std::make_shared<ObserverEntry>())
364 {
365 }
366
367 std::string MainModel::pyStr() const
368 {
369   std::ostringstream oss;
370   oss << "from adao import adaoBuilder" << std::endl;
371   oss << "case = adaoBuilder.New()" << std::endl;
372   oss << _algo->getParamForSet(*_algo) << std::endl;
373   oss << _bg->getParamForSet(*_bg) << std::endl;
374   oss << _bg_err->getParamForSet(*_bg_err) << std::endl;
375   oss << _obs->getParamForSet(*_obs) << std::endl;
376   oss << _obs_err->getParamForSet(*_obs_err) << std::endl;
377   oss << _observ_op->getParamForSet(*_observ_op) << std::endl;
378   oss << _observ_entry->getParamForSet(*_observ_entry) << std::endl;
379   return oss.str();
380 }
381
382 std::vector< std::shared_ptr<GenericKeyVal> > MainModel::toVect() const
383 {
384   return {
385         std::static_pointer_cast<GenericKeyVal,AlgorithmParameters>(_algo),
386         std::static_pointer_cast<GenericKeyVal,Background>(_bg),
387         std::static_pointer_cast<GenericKeyVal,BackgroundError>(_bg_err),
388         std::static_pointer_cast<GenericKeyVal,Observation>(_obs),
389         std::static_pointer_cast<GenericKeyVal,ObservationError>(_obs_err),
390         std::static_pointer_cast<GenericKeyVal,ObservationOperator>(_observ_op),
391         std::static_pointer_cast<GenericKeyVal,ObserverEntry>(_observ_entry)
392   };
393 }
394
395 class MyAllVisitor : public RecursiveVisitor
396 {
397 public:
398   MyAllVisitor(GenericKeyVal *elt):_elt_to_find(elt) { }
399   void visit(GenericKeyVal *elt) override
400       {
401       if(_found) return ;
402       if(elt != _elt_to_find) return ;
403       _path.push_back(elt->getKey());
404       _found=true;
405       }
406   void enterSubDir(DictKeyVal *subdir) override
407       {
408       if(_found) return ;
409       _path.push_back(subdir->getKey());
410       }
411   void exitSubDir(DictKeyVal *subdir) override
412       {
413       if(_found) return ;
414       _path.pop_back();
415       }
416   std::string getPath() const
417   {
418     std::ostringstream oss;
419     std::size_t len(_path.size()),ii(0);
420     for(auto elt : _path)
421       {
422         oss << elt;
423         if(ii != len-1)
424           oss << "/";
425         ii++;
426       }
427     return oss.str();
428   }
429 private:
430   GenericKeyVal *_elt_to_find = nullptr;
431   std::vector<std::string> _path;
432   bool _found = false;
433 };
434
435 std::string MainModel::findPathOf(GenericKeyVal *elt)
436 {
437   MyAllVisitor vis(elt);
438   this->visitAll(&vis);
439   return vis.getPath();
440 }
441
442 void MainModel::visitPythonLeaves(PythonLeafVisitor *visitor)
443 {
444   std::vector< std::shared_ptr<GenericKeyVal> > sons(toVect());
445   for(auto elt : sons)
446     {
447       elt->visitPython(this, visitor);
448     }
449 }
450
451 void MainModel::visitAll(RecursiveVisitor *visitor)
452 {
453   std::vector< std::shared_ptr<GenericKeyVal> > sons(toVect());
454   for(auto elt : sons)
455     {
456       elt->visitAll(this, visitor);
457     }
458 }