1 /*!
2  * \file   mtest/src/UserDefinedPostProcessing.cxx
3  * \brief
4  * \author Thomas Helfer
5  * \date   27/02/2018
6  */
7 
8 #include "TFEL/Raise.hxx"
9 #include "MTest/Behaviour.hxx"
10 #include "MTest/Evolution.hxx"
11 #include "MTest/UserDefinedPostProcessing.hxx"
12 
13 namespace mtest {
14 
UserDefinedPostProcessing(const Behaviour & b,const EvolutionManager & e,const std::string & f,const std::vector<std::string> & ps)15   UserDefinedPostProcessing::UserDefinedPostProcessing(
16       const Behaviour& b,
17       const EvolutionManager& e,
18       const std::string& f,
19       const std::vector<std::string>& ps)
20       : evm(e), out(f) {
21     if (!this->out) {
22       tfel::raise(
23           "UserDefinedPostProcessing::UserDefinedPostProcessing: "
24           "can't open file '" +
25           f + "'");
26     }
27     auto cevs = buildExternalFunctionManagerFromConstantEvolutions(this->evm);
28     for (const auto& p : ps) {
29       auto eval = std::make_shared<tfel::math::Evaluator>(p, cevs);
30       const auto& vns = eval->getVariablesNames();
31       for (const auto& vn : vns) {
32         if (isBehaviourVariable(b, vn)) {
33           if (this->extractors.count(vn) == 0) {
34             this->extractors[vn] = buildValueExtractor(b, vn);
35           }
36         } else {
37           if (this->evm.count(vn) == 0) {
38             tfel::raise(
39                 "UserDefinedPostProcessing::UserDefinedPostProcessing: "
40                 "no variable named '" +
41                 vn + "' defined");
42           }
43         }
44       }
45       this->postprocessings.push_back(eval);
46     }
47     this->out << "# first column : time\n";
48     auto cnbr = int{2};
49     for (const auto& p : ps) {
50       this->out << "# " << cnbr << " column : " << p << '\n';
51       ++cnbr;
52     }
53   }  // end of UserDefinedPostProcessing::UserDefinedPostProcessing
54 
exe(const CurrentState & s,const real t,const real dt)55   void UserDefinedPostProcessing::exe(const CurrentState& s,
56                                       const real t,
57                                       const real dt) {
58     this->out << t + dt << " ";
59     for (const auto& p : this->postprocessings) {
60       const auto& vns = p->getVariablesNames();
61       for (const auto& vn : vns) {
62         auto pe = this->extractors.find(vn);
63         if (pe != this->extractors.end()) {
64           p->setVariableValue(vn, pe->second(s));
65         } else {
66           const auto pev = this->evm.find(vn);
67           if (pev == this->evm.end()) {
68             tfel::raise("UserDefinedPostProcessing::exe: unknown variable '" +
69                         vn + "'");
70           }
71           const auto& ev = *(pev->second);
72           p->setVariableValue(vn, ev(t + dt));
73         }
74       }
75       this->out << " " << p->getValue();
76     }
77     this->out << std::endl;
78   }  // end of UserDefinedPostProcessing::exe
79 
80   UserDefinedPostProcessing::~UserDefinedPostProcessing() = default;
81 
82 }  // end of namespace mtest
83