33 #ifndef EDGE8_LATTICES_HPP 34 #define EDGE8_LATTICES_HPP 42 template <
typename TNumType,
class TSimType,
class... TCostFuncsType>
43 class LatticeTypeStateSimBeginEnd
44 :
public LatticeTypeBaseCRTP<LatticeTypeStateSimBeginEnd<TNumType, TSimType, TCostFuncsType...>, TNumType, TSimType,
53 _latticeArcs.resize(1);
54 _latticeArcs[0] = _sim.paramStruct->paramFuncs.funcsArcEnd();
58 void computeDArcIdPImpl(
const TSimType& _sim,
const TNumType _arc,
const size_t& _lattIdx,
auto& _dstStateGrad)
60 auto& dstStateGradCf = _dstStateGrad.stateCf();
61 auto& dstStateGradNm = _dstStateGrad.stateNm();
62 size_t optParamTBackIdx = dstStateGradCf.sub(0).data().size() - 1;
63 dstStateGradCf.mat().col(optParamTBackIdx) += _sim.stateCfDotCache().data();
64 dstStateGradNm.mat().col(optParamTBackIdx) += _sim.stateNmDotCache().data();
73 template <
typename TNumType,
class TSimType,
class... TCostFuncsType>
75 :
public LatticeTypeBaseCRTP<LatticeTypeStateSimEqDt<TNumType, TSimType, TCostFuncsType...>, TNumType, TSimType,
84 const TNumType arcParamMax = _sim.paramStruct->paramFuncs.funcsArcEnd();
88 dt = arcParamMax / (TNumType)(nrPts_);
95 if ((
int)_latticeArcs.size() != nrPts_)
97 _latticeArcs.resize(nrPts_);
99 for (
int i = 0; i < nrPts_; ++i)
101 _latticeArcs[i] = (i + 1) * dt;
103 _latticeArcs.back() = arcParamMax;
107 void computeDArcIdPImpl(
const TSimType& _sim,
const TNumType _arc,
const size_t& _lattIdx,
auto& _dstStateGrad)
110 auto& dstStateGradCf = _dstStateGrad.stateCf();
111 auto& dstStateGradNm = _dstStateGrad.stateNm();
113 const TNumType dtIdpEnd = (TNumType)(_lattIdx + 1) / (TNumType)(this->
lattice.size() );
114 size_t optParamTBackIdx = dstStateGradCf.sub(0).data().size() - 1;
116 dstStateGradCf.mat().col(optParamTBackIdx) += _sim.stateCfDotCache().data() * dtIdpEnd;
117 dstStateGradNm.mat().col(optParamTBackIdx) += _sim.stateNmDotCache().data() * dtIdpEnd;
152 template <
typename TNumType,
class TSimType,
class... TCostFuncsType>
154 :
public LatticeTypeBaseCRTP<LatticeTypeStateSimEqDs<TNumType, TSimType, TCostFuncsType...>, TNumType, TSimType,
173 auto& paramFuncs = _sim.paramStruct->paramFuncs;
177 paramFuncs.setEvalArc(paramFuncs.funcsArcEnd());
178 sEnd = paramFuncs.computeS();
179 ds_ = sEnd / (TNumType)(nrPts_);
180 paramFuncs.setEvalArc(paramFuncs.funcsArcBegin());
181 if ((
int)_latticeArcs.size() != nrPts_)
183 _latticeArcs.resize(nrPts_);
184 cosSinCache_.resize(nrPts_);
186 for (
int i = 0; i < nrPts_; ++i)
188 _latticeArcs[i] = (i + 1) * ds_;
190 _latticeArcs.back() = sEnd;
191 paramFuncs.computeS2TLattice(_latticeArcs, _latticeArcs);
196 void computeDArcIdPImpl(
const TSimType& _sim,
const TNumType _arc,
const size_t& _lattIdx,
auto& _dstStateGrad)
202 TNumType tEnd = _sim.paramStruct->paramFuncs.funcsArcEnd();
203 auto& simNonConst =
const_cast<TSimType&
>(_sim);
208 dsdp = _sim.state().stateGradCf().s().data();
209 dsdp(dsdp.rows() - 1) += _sim.stateCfDotCache().s();
214 stateDotCache.resize(_sim.stateNmDotCache().data().size() + _sim.stateCfDotCache().data().size());
216 stateDotCache.block(0, 0, _sim.stateNmDotCache().data().size(), 1) = _sim.stateNmDotCache().data();
217 stateDotCache.block(_sim.stateNmDotCache().data().size(), 0, _sim.stateCfDotCache().data().size(), 1) =
218 _sim.stateCfDotCache().data();
219 auto& v = _sim.state().stateCf().v();
222 TNumType iByNS = (TNumType)(_lattIdx + 1) /
223 (TNumType)(this->
lattice.size() ) ;
224 _dstStateGrad.mat() += (stateDotCache) * ((iByNS * dsdp - _dstStateGrad.stateCf().s().data()) / v).transpose();
232 for (
size_t i = 0; i < cosSinCache_.size(); ++i)
234 const TNumType& th = this->
lattice[i].statePtr->stateNm().theta();
235 cosSinCache_[i].cos = cos(th);
236 cosSinCache_[i].sin = sin(th);
266 Eigen::Matrix<TNumType, -1, 1> dsdp;
269 Eigen::Matrix<TNumType, -1, 1> stateDotCache;
272 template <
typename TNumType,
class TSimType,
class... TCostFuncsType>
274 :
public LatticeTypeBaseCRTP<LatticeTypeStateSimCtrlPtKnotsP<TNumType, TSimType, TCostFuncsType...>, TNumType,
275 TSimType, TCostFuncsType...>
286 _latticeArcs.resize(_sim.paramStruct->paramFuncs.funcsArcSize(1) - 2);
287 auto& paramFuncs = _sim.paramStruct->paramFuncs;
288 for (
size_t i = 0; i < _latticeArcs.size(); ++i)
290 _latticeArcs[i] = paramFuncs.funcsArc(1, i + 1);
295 _latticeArcs.clear();
300 void computeDArcIdPImpl(
const TSimType& _sim,
const TNumType _arc,
const size_t& _lattIdx,
auto& _dstStateGrad)
304 auto& dstStateGradCf = _dstStateGrad.stateCf();
305 auto& dstStateGradNm = _dstStateGrad.stateNm();
306 size_t optParamTIdx =
307 dstStateGradCf.sub(0).optParamV().data().size() + dstStateGradCf.sub(0).optParamP().data().size();
308 dstStateGradCf.mat().col(optParamTIdx + _lattIdx) += _sim.stateCfDotCache().data();
309 dstStateGradNm.mat().col(optParamTIdx + _lattIdx) += _sim.stateNmDotCache().data();
322 template <
typename TNumType,
class TSimType,
class... TCostFuncsType>
324 :
public LatticeTypeBaseCRTP<LatticeTypeStateSimCtrlPtKnotsV<TNumType, TSimType, TCostFuncsType...>, TNumType,
325 TSimType, TCostFuncsType...>
336 _latticeArcs.resize(_sim.paramStruct->paramFuncs.funcsArcSize(0) - 1);
337 auto& paramFuncs = _sim.paramStruct->paramFuncs;
338 for (
size_t i = 0; i < _latticeArcs.size(); ++i)
340 _latticeArcs[i] = paramFuncs.funcsArc(0, i + 1);
345 _latticeArcs.clear();
350 void computeDArcIdPImpl(
const TSimType& _sim,
const TNumType _arc,
const size_t& _lattIdx,
auto& _dstStateGrad)
354 auto& dstStateGradCf = _dstStateGrad.stateCf();
355 auto& dstStateGradNm = _dstStateGrad.stateNm();
356 size_t optParamTIdx = dstStateGradCf.sub(0).optParamV().data().size() +
357 dstStateGradCf.sub(0).optParamP().data().size() +
358 dstStateGradCf.sub(0).optParamTP().data().size();
359 dstStateGradCf.mat().col(optParamTIdx + _lattIdx) += _sim.stateCfDotCache().data();
360 dstStateGradNm.mat().col(optParamTIdx + _lattIdx) += _sim.stateNmDotCache().data();
374 #endif // EDGE8_LATTICES_HPP virtual void precompute(TSimType &_sim) override
static void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs)
void setDt(const TNumType &_dt)
void setNrPts(const size_t &_nrPts)
virtual void precompute(TSimType &_sim) override
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs) const
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs) const
void setDs(const TNumType &_ds)
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs)
void setNrPts(const size_t &_nrPts)
std::vector< LatticePoint< NumType, StateType > > lattice
void computeLatticeArcsImpl(TSimType &_sim, std::vector< TNumType > &_latticeArcs) const
virtual void precompute(TSimType &_sim) override
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
std::vector< CosSinCache > cosSinCache_
virtual void precompute(TSimType &_sim) override
close to previous evaluation arc
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
void computeDArcIdPImpl(const TSimType &_sim, const TNumType _arc, const size_t &_lattIdx, auto &_dstStateGrad)
LatticeTypeBaseCRTP()=default
this evaluation arc is at the arc parametrization begin
virtual void precompute(TSimType &_sim) override