state_sim_template.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #ifndef STATE_SIM_TEMPLATE_H
34 #define STATE_SIM_TEMPLATE_H
35 
38 #include <boost/numeric/odeint.hpp>
39 
40 namespace tuw
41 {
47 template <std::size_t StateSize, std::size_t StateNmSize>
49 template <std::size_t StateSize, std::size_t StateNmSize>
50 using StateSimTemplateSPtr = std::shared_ptr<StateSimTemplate<StateSize, StateNmSize>>;
51 template <std::size_t StateSize, std::size_t StateNmSize>
52 using StateSimTemplateConstSPtr = std::shared_ptr<StateSimTemplate<StateSize, StateNmSize> const>;
53 template <std::size_t StateSize, std::size_t StateNmSize>
54 using StateSimTemplateUPtr = std::unique_ptr<StateSimTemplate<StateSize, StateNmSize>>;
55 template <std::size_t StateSize, std::size_t StateNmSize>
56 using StateSimTemplateConstUPtr = std::unique_ptr<StateSimTemplate<StateSize, StateNmSize> const>;
57 template <std::size_t StateSize, std::size_t StateNmSize>
58 class StateSimTemplate : public StateSim
59 {
60  // special class member functions
61 public:
62  StateSimTemplate() = default;
63 
64 public:
65  virtual ~StateSimTemplate() = default;
66 
67 public:
69  {
70  for (size_t i = 0; i < valueSize(); ++i)
71  {
72  value(i) = _o.value(i);
73  state0_.value(i) = _o.state0_.value(i);
74  }
75  } //= default;
76 public:
78  {
80  for (size_t i = 0; i < valueSize(); ++i)
81  {
82  value(i) = _o.value(i);
83  state0_.value(i) = _o.state0_.value(i);
84  }
85  return *this;
86  } //= default;
87 public:
88  StateSimTemplate(StateSimTemplate&&) = delete; // default;
89 public:
90  StateSimTemplate& operator=(StateSimTemplate&&) = delete; // default;
91 
92  // implemented virtual functions
93 public:
94  virtual StateSPtr cloneState() const override
95  {
96  StateSPtr retState = std::shared_ptr<StateArray<StateSize>>(new StateArray<StateSize>);
97  const std::size_t sNmS = stateNm_.valueSize();
98  for (std::size_t i = 0; i < sNmS; i++)
99  {
100  retState->value(i) = stateNm_.value(i);
101  }
102  for (std::size_t i = 0; i < stateCf_.valueSize(); i++)
103  {
104  retState->value(i + sNmS) = stateCf_.value(i);
105  }
106  return retState;
107  }
108 
109 public:
110  void toState0() override
111  {
112  const std::size_t sNmS = stateNm_.valueSize();
113  for (std::size_t i = 0; i < sNmS; i++)
114  {
115  stateNm_.value(i) = state0_.value(i);
116  }
117  // for( std::size_t i = 0; i < stateCf_.valueSize(); i++ ){ stateCf_.value(i) = state0_.value(i+sNmS); }
119  rk = std::shared_ptr<boost::numeric::odeint::runge_kutta4<std::array<double, StateNmSize>>>(new boost::numeric::odeint::runge_kutta4<std::array<double, StateNmSize>>);
120  arcOld = 0;
121  }
122 
123 public:
124  void setDiscrType(const RungeKutta::DiscretizationType& _discrType) override
125  {
126  discrFunc_ = RungeKutta::getDiscrFunc<StateNmSize>(_discrType);
127  }
128 
129 public:
130  void setState(StateSPtr& _otherState) override
131  {
133  const std::size_t sNmS = stateNm_.valueSize();
134  for (std::size_t i = 0; i < sNmS; i++)
135  {
136  stateNm_.value(i) = _otherState->value(i);
137  }
138  for (std::size_t i = 0; i < stateCf_.valueSize(); i++)
139  {
140  stateCf_.value(i) = _otherState->value(i + sNmS);
141  }
142  }
143 
144 public:
145  State& state0() override
146  {
147  return state0_;
148  }
149 
150 public:
151  State& stateNm() override
152  {
153  return stateNm_;
154  }
155 
156 public:
157  State& stateCf() override
158  {
159  return stateCf_;
160  }
161 
162 public:
163  size_t valueSize() const override
164  {
165  return StateSize;
166  }
167 
168 public:
169  double& value(const std::size_t& _i) override
170  {
171  if (_i < StateNmSize)
172  {
173  return stateNm_.value(_i);
174  }
175  else
176  {
177  return stateCf_.value(_i - StateNmSize);
178  }
179  };
180 
181 public:
182  const double& value(const std::size_t& _i) const override
183  {
184  if (_i < StateNmSize)
185  {
186  return stateNm_.value(_i);
187  }
188  else
189  {
190  return stateCf_.value(_i - StateNmSize);
191  }
192  };
193 
194 public:
195  void advance(double _arc) override
196  {
197  discrFunc_(*this, _arc);
198  }
199 
200 public:
201  void advanceODEInt(double _arc)
202  {
203  // setStateCf (arcOld, ParamFuncs::EvalArcGuarantee::NONE);
204  rk->do_step(
205  [this](const std::array<double, StateNmSize>& _x, std::array<double, StateNmSize>& _dxdt, const double _t)
206  {
208  stateNm_.valuesArray() = _x;
209  stateNmDot();
210  _dxdt = stateNmDotCache_.valuesArray();
211  },
212  stateNm_.valuesArray(), arcOld, _arc - arcOld);
213  arcOld = _arc;
215  }
216 
217 public:
218  void sys1(const std::array<double, StateNmSize>& _x, std::array<double, StateNmSize>& _dxdt, const double _t)
219  {
221  stateNmDot();
222  _dxdt = stateNm_.valuesArray();
223  }
224 
225  double arcOld;
226 
227 protected:
228  std::shared_ptr<boost::numeric::odeint::runge_kutta4<std::array<double, StateNmSize>>> rk;
229 
231 protected:
234 protected:
237 protected:
240 protected:
241  StateArray<StateSize - StateNmSize> stateCf_;
243 private:
245 
246 private:
247  using StateSim::setStateCf;
248 };
249 }
250 
251 #endif // STATE_SIM_TEMPLATE_H
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
StateArray< StateSize-StateNmSize > stateCf_
State array storing the closed-form-computed value.
StateSimTemplate()=default
std::shared_ptr< State > StateSPtr
Definition: state.h:129
previous evaluation arc <= this evaluation arc
this evaluation arc is at the arc parametrization begin
StateArray< StateNmSize > stateNm_
State array storing the numerical-computed value.
std::shared_ptr< StateSimTemplate< StateSize, StateNmSize >> StateSimTemplateSPtr
void advanceODEInt(double _arc)
std::unique_ptr< StateSimTemplate< StateSize, StateNmSize >> StateSimTemplateUPtr
const double & value(const std::size_t &_i) const override
Const access state variable based on index _i.
void advance(double _arc) override
Performs one simulation step to parametrized arc length _arc.
std::shared_ptr< StateSimTemplate< StateSize, StateNmSize > const > StateSimTemplateConstSPtr
virtual void setStateCfNmStep(const double &_arc, const ParamFuncs::EvalArcGuarantee &_evalArcGuarantee=ParamFuncs::EvalArcGuarantee::AFTER_LAST)
Definition: state_sim.h:150
StateArray< StateSize > state0_
State array storing the initial value.
virtual size_t valueSize() const override
Size of the state variables.
DiscretizationType
Several discretization modes.
Templated partial implementation of StateSim.
std::unique_ptr< StateSimTemplate< StateSize, StateNmSize > const > StateSimTemplateConstUPtr
virtual double & value(const std::size_t &_i) override
Access state variable based on index _i.
Definition: state_array.hpp:98
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
Definition: state.h:135
State & stateCf() override
Reference to the actual closed-form state.
void toState0() override
Resets entire state to state0. It also sets the control structure evaluation point at most at the new...
virtual State & stateNmDot()=0
Computes numerical continuous arc state transition (return) based on internal closed-form state (stat...
void sys1(const std::array< double, StateNmSize > &_x, std::array< double, StateNmSize > &_dxdt, const double _t)
void setState(StateSPtr &_otherState) override
Sets the state variables to the values of _otherState. It also sets the control structure evaluation ...
std::array< double, N > & valuesArray()
Reference to the state variables array.
void setDiscrType(const RungeKutta::DiscretizationType &_discrType) override
Set discretization type used in the simulation.
RungeKutta::DiscretizationFuncPtr discrFunc_
Pointer to the active discretization-method function.
size_t valueSize() const override
Size of the state variables.
virtual void setStateCf(const double &_arc, const ParamFuncs::EvalArcGuarantee &_evalArcGuarantee=ParamFuncs::EvalArcGuarantee::AFTER_LAST)=0
Sets closed-form state at arc _arc.
StateSimTemplate & operator=(const StateSimTemplate &_o)
Interface for a state simulator structure that performs numerical integration of not-closed-form stat...
Definition: state_sim.h:58
State & state0() override
Reference to the initial state.
double & value(const std::size_t &_i) override
Access state variable based on index _i.
State & stateNm() override
Reference to the actual numerical-computed state.
StateArray< StateNmSize > stateNmDotCache_
State array caching the evaluation of the last call of the value transition function.
std::shared_ptr< boost::numeric::odeint::runge_kutta4< std::array< double, StateNmSize > > > rk
void(*)(StateSim &, const double &) DiscretizationFuncPtr
StateSimTemplate(const StateSimTemplate &_o)
virtual ~StateSimTemplate()=default


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:22