1 // Copyright (C) 2019 EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author: Anthony Geay, anthony.geay@edf.fr, EDF R&D
21 #include "AdaoModelKeyVal.hxx"
22 #include "AdaoExchangeLayerException.hxx"
26 using namespace AdaoModel;
28 const char CostDecrementTolerance::KEY[]="CostDecrementTolerance";
30 const char StringObserver::KEY[]="String";
32 const char InfoObserver::KEY[]="Info";
34 const char StoreSupplKeyVal::KEY[]="StoreSupplementaryCalculations";
36 const char *StoreSupplKeyVal::DFTL[]={"CostFunctionJAtCurrentOptimum","CostFunctionJoAtCurrentOptimum","CurrentOptimum","SimulatedObservationAtCurrentOptimum","SimulatedObservationAtOptimum"};
38 const char EnumAlgoKeyVal::KEY[]="Algorithm";
40 const char ParametersOfAlgorithmParameters::KEY[]="Parameters";
42 const char Bounds::KEY[]="Bounds";
44 const char MaximumNumberOfSteps::KEY[]="MaximumNumberOfSteps";
46 const char VectorBackground::KEY[]="Vector";
48 const char StoreBackground::KEY[]="Stored";
50 const char MatrixBackgroundError::KEY[]="Matrix";
52 const char ScalarSparseMatrixError::KEY[]="ScalarSparseMatrix";
54 const char DiagonalSparseMatrixError::KEY[]="DiagonalSparseMatrix";
56 const char OneFunction::KEY[]="OneFunction";
58 const char DifferentialIncrement::KEY[]="DifferentialIncrement";
60 const char ObservationOperatorParameters::KEY[]="Parameters";
62 const char CenteredFiniteDifference::KEY[]="CenteredFiniteDifference";
64 const char InputFunctionAsMulti::KEY[]="InputFunctionAsMulti";
66 const char VariableKV::KEY[]="Variable";
68 const char TemplateKV::KEY[]="Template";
70 const char AlgorithmParameters::KEY[]="AlgorithmParameters";
72 const char Background::KEY[]="Background";
74 const char BackgroundError::KEY[]="BackgroundError";
76 const double BackgroundError::BACKGROUND_SCALAR_SPARSE_DFT = 1.e10;
78 const char ObservationError::KEY[]="ObservationError";
80 const char ObservationOperator::KEY[]="ObservationOperator";
82 const char Observation::KEY[]="Observation";
84 const double ObservationError::BACKGROUND_SCALAR_SPARSE_DFT = 1.;
86 const char ObserverEntry::KEY[]="Observer";
88 std::string TopEntry::getParamForSet(const GenericKeyVal& entry) const
90 std::ostringstream oss;
91 oss << "case.set(\'" << entry.getKey() << "\' , **" << entry.pyStr() << ")";
95 std::string GenericKeyVal::pyStrKeyVal() const
97 std::string val(this->pyStr());
100 std::ostringstream oss;
101 oss << "\"" << this->getKey() << "\" : " << val;
105 return std::string();
108 void GenericKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor)
110 visitor->visit(this);
113 std::string DoubleKeyVal::pyStr() const
115 std::ostringstream oss;
116 oss << std::scientific << _val;
120 std::string BoolKeyVal::pyStr() const
122 return _val?"True":"False";
125 std::string StringKeyVal::pyStr() const
127 std::ostringstream oss;
128 oss << "\"" << _val << "\"";
132 std::string NoneKeyVal::pyStr() const
137 std::string ListStringsKeyVal::pyStr() const
139 std::ostringstream oss;
141 std::size_t len(_val.size());
142 for(std::size_t i=0;i<len;++i)
144 oss << "\"" << _val[i] << "\"";
152 StoreSupplKeyVal::StoreSupplKeyVal():ListStringsKeyVal(KEY)
154 _val.insert(_val.end(),DFTL,DFTL+sizeof(DFTL)/sizeof(char *));
157 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::generateDftParameters() const
161 case EnumAlgo::ThreeDVar:
162 case EnumAlgo::NonLinearLeastSquares:
164 return templateForOthers();
168 return templateForBlue();
171 throw AdaoExchangeLayerException("EnumAlgoKeyVal::generateDftParameters : Unrecognized Algo !");
175 std::string EnumAlgoKeyVal::getRepresentation() const
179 case EnumAlgo::ThreeDVar:
181 case EnumAlgo::NonLinearLeastSquares:
182 return "NonLinearLeastSquares";
186 throw AdaoExchangeLayerException("EnumAlgoKeyVal::getRepresentation : Unrecognized Algo !");
190 std::string EnumAlgoKeyVal::pyStr() const
192 std::ostringstream oss;
193 oss << "\"" << this->getRepresentation() << "\"";
197 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::templateForBlue() const
199 std::shared_ptr<DictKeyVal> ret(std::make_shared<ParametersOfAlgorithmParameters>());
200 std::shared_ptr<StoreSupplKeyVal> v(std::make_shared<StoreSupplKeyVal>());
201 ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v));
205 std::shared_ptr<DictKeyVal> EnumAlgoKeyVal::templateForOthers() const
207 std::shared_ptr<DictKeyVal> ret(std::make_shared<ParametersOfAlgorithmParameters>());
208 std::shared_ptr<Bounds> v0(std::make_shared<Bounds>());
209 std::shared_ptr<MaximumNumberOfSteps> v1(std::make_shared<MaximumNumberOfSteps>());
210 std::shared_ptr<CostDecrementTolerance> v2(std::make_shared<CostDecrementTolerance>());
211 std::shared_ptr<StoreSupplKeyVal> v3(std::make_shared<StoreSupplKeyVal>());
212 ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v0));
213 ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v1));
214 ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v2));
215 ret->pushBack(std::static_pointer_cast<GenericKeyVal>(v3));
219 std::string DictKeyVal::pyStr() const
221 std::vector<std::string> vect;
222 std::size_t len(_pairs.size());
223 for(std::size_t i=0;i<len;++i)
225 const auto& elt(_pairs[i]);
226 std::string cont(elt->pyStrKeyVal());
228 vect.push_back(cont);
231 std::ostringstream oss;
233 for(std::size_t i=0;i<len;++i)
243 void DictKeyVal::visitPython(MainModel *godFather, PythonLeafVisitor *visitor)
245 for(auto elt : _pairs)
247 elt->visitPython(godFather, visitor);
251 void DictKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor)
253 visitor->enterSubDir(this);
254 for(auto elt : _pairs)
256 elt->visitAll(godFather, visitor);
258 visitor->exitSubDir(this);
261 std::string PyObjKeyVal::pyStr() const
263 std::ostringstream oss;
268 void PyObjKeyVal::visitPython(MainModel *godFather, PythonLeafVisitor *visitor)
270 visitor->visit(godFather,this);
273 std::string UnsignedIntKeyVal::pyStr() const
275 std::ostringstream oss;
280 void InputFunctionAsMulti::setVal(bool val)
283 throw AdaoExchangeLayerException("InputFunctionAsMulti : value has to remain to true !");
286 ObservationOperatorParameters::ObservationOperatorParameters():DictKeyVal(KEY)
288 std::shared_ptr<DifferentialIncrement> v0(std::make_shared<DifferentialIncrement>());
289 std::shared_ptr<CenteredFiniteDifference> v1(std::make_shared<CenteredFiniteDifference>());
290 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DifferentialIncrement>(v0));
291 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,CenteredFiniteDifference>(v1));
294 AlgorithmParameters::AlgorithmParameters():DictKeyVal(KEY)
296 std::shared_ptr<EnumAlgoKeyVal> v0(std::make_shared<EnumAlgoKeyVal>());
297 std::shared_ptr<DictKeyVal> v1(v0->generateDftParameters());
298 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,EnumAlgoKeyVal>(v0));
299 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DictKeyVal>(v1));
302 Background::Background():DictKeyVal(KEY)
304 std::shared_ptr<VectorBackground> v0(std::make_shared<VectorBackground>());
305 std::shared_ptr<StoreBackground> v1(std::make_shared<StoreBackground>());
307 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VectorBackground>(v0));
308 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StoreBackground>(v1));
311 GenericError::GenericError(const std::string& key, double dftValForScalarSparseMatrix):DictKeyVal(key)
313 std::shared_ptr<MatrixBackgroundError> v0(std::make_shared<MatrixBackgroundError>());
314 std::shared_ptr<ScalarSparseMatrixError> v1(std::make_shared<ScalarSparseMatrixError>(dftValForScalarSparseMatrix));
315 std::shared_ptr<DiagonalSparseMatrixError> v2(std::make_shared<DiagonalSparseMatrixError>());
316 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,MatrixBackgroundError>(v0));
317 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,ScalarSparseMatrixError>(v1));
318 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,DiagonalSparseMatrixError>(v2));
321 Observation::Observation():DictKeyVal(KEY)
323 std::shared_ptr<VectorBackground> v0(std::make_shared<VectorBackground>());
324 std::shared_ptr<StoreBackground> v1(std::make_shared<StoreBackground>());
326 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VectorBackground>(v0));
327 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StoreBackground>(v1));
330 ObservationOperator::ObservationOperator():DictKeyVal(KEY)
332 std::shared_ptr<OneFunction> v0(std::make_shared<OneFunction>());
333 std::shared_ptr<ObservationOperatorParameters> v1(std::make_shared<ObservationOperatorParameters>());
334 std::shared_ptr<InputFunctionAsMulti> v2(std::make_shared<InputFunctionAsMulti>());
335 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,OneFunction>(v0));
336 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,ObservationOperatorParameters>(v1));
337 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,InputFunctionAsMulti>(v2));
340 ObserverEntry::ObserverEntry():DictKeyVal(KEY)
342 std::shared_ptr<VariableKV> v0(std::make_shared<VariableKV>());
343 std::shared_ptr<TemplateKV> v1(std::make_shared<TemplateKV>());
344 std::shared_ptr<StringObserver> v2(std::make_shared<StringObserver>());
345 std::shared_ptr<InfoObserver> v3(std::make_shared<InfoObserver>());
346 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,VariableKV>(v0));
347 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,TemplateKV>(v1));
348 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,StringObserver>(v2));
349 _pairs.push_back(std::static_pointer_cast<GenericKeyVal,InfoObserver>(v3));
352 MainModel::MainModel():_algo(std::make_shared<AlgorithmParameters>()),
353 _bg(std::make_shared<Background>()),
354 _bg_err(std::make_shared<BackgroundError>()),
355 _obs(std::make_shared<Observation>()),
356 _obs_err(std::make_shared<ObservationError>()),
357 _observ_op(std::make_shared<ObservationOperator>()),
358 _observ_entry(std::make_shared<ObserverEntry>())
362 std::string MainModel::pyStr() const
364 std::ostringstream oss;
365 oss << "from adao import adaoBuilder" << std::endl;
366 oss << "case = adaoBuilder.New()" << std::endl;
367 oss << _algo->getParamForSet(*_algo) << std::endl;
368 oss << _bg->getParamForSet(*_bg) << std::endl;
369 oss << _bg_err->getParamForSet(*_bg_err) << std::endl;
370 oss << _obs->getParamForSet(*_obs) << std::endl;
371 oss << _obs_err->getParamForSet(*_obs_err) << std::endl;
372 oss << _observ_op->getParamForSet(*_observ_op) << std::endl;
373 oss << _observ_entry->getParamForSet(*_observ_entry) << std::endl;
377 std::vector< std::shared_ptr<GenericKeyVal> > MainModel::toVect() const
380 std::static_pointer_cast<GenericKeyVal,AlgorithmParameters>(_algo),
381 std::static_pointer_cast<GenericKeyVal,Background>(_bg),
382 std::static_pointer_cast<GenericKeyVal,BackgroundError>(_bg_err),
383 std::static_pointer_cast<GenericKeyVal,Observation>(_obs),
384 std::static_pointer_cast<GenericKeyVal,ObservationError>(_obs_err),
385 std::static_pointer_cast<GenericKeyVal,ObservationOperator>(_observ_op),
386 std::static_pointer_cast<GenericKeyVal,ObserverEntry>(_observ_entry)
390 class MyAllVisitor : public RecursiveVisitor
393 MyAllVisitor(GenericKeyVal *elt):_elt_to_find(elt) { }
394 void visit(GenericKeyVal *elt) override
397 if(elt != _elt_to_find) return ;
398 _path.push_back(elt->getKey());
401 void enterSubDir(DictKeyVal *subdir) override
404 _path.push_back(subdir->getKey());
406 void exitSubDir(DictKeyVal *subdir) override
411 std::string getPath() const
413 std::ostringstream oss;
414 std::size_t len(_path.size()),ii(0);
415 for(auto elt : _path)
425 GenericKeyVal *_elt_to_find = nullptr;
426 std::vector<std::string> _path;
430 std::string MainModel::findPathOf(GenericKeyVal *elt)
432 MyAllVisitor vis(elt);
433 this->visitAll(&vis);
434 return vis.getPath();
437 void MainModel::visitPythonLeaves(PythonLeafVisitor *visitor)
439 std::vector< std::shared_ptr<GenericKeyVal> > sons(toVect());
442 elt->visitPython(this, visitor);
446 void MainModel::visitAll(RecursiveVisitor *visitor)
448 std::vector< std::shared_ptr<GenericKeyVal> > sons(toVect());
451 elt->visitAll(this, visitor);