Sandals  v0.0.0
A C++ library for ODEs/DAEs integration
Loading...
Searching...
No Matches
Explicit.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Sandals project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * e-mail: davide.stocco@unitn.it e-mail: enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef SANDALS_EXPLICIT_SYSTEM_HH
14#define SANDALS_EXPLICIT_SYSTEM_HH
15
16#include <Sandals.hh>
18
19namespace Sandals {
20
21 /*\
22 | _____ _ _ _ _
23 | | ____|_ ___ __ | (_) ___(_) |_
24 | | _| \ \/ / '_ \| | |/ __| | __|
25 | | |___ > <| |_) | | | (__| | |_
26 | |_____/_/\_\ .__/|_|_|\___|_|\__|
27 | |_|
28 \*/
29
40 template <typename Real, Integer N, Integer M = 0>
41 class Explicit : public Implicit<Real, N, M>
42 {
43 public:
44 using Pointer = std::shared_ptr<Explicit<Real, N, M>>;
48
49 protected:
55 Explicit(Type t_type, std::string t_name) : Implicit<Real, N, M>(t_type, t_name) {}
56
57 public:
61 Explicit() : Implicit<Real, N, M>(Type::EXPLICIT, "(missing name)") {}
62
67 Explicit(std::string t_name) : Implicit<Real, N, M>(Type::EXPLICIT, t_name) {}
68
82 VectorF F(VectorF const & x, VectorF const & x_dot, Real const t) const override
83 {
84 return x_dot - this->f(x, t);
85 }
86
103 MatrixJF JF_x(VectorF const & x, VectorF const &/*x_dot*/, Real const t) const override
104 {
105 return -this->Jf_x(x, t);
106 }
107
123 MatrixJF JF_x_dot(VectorF const &/*x*/, VectorF const &/*x_dot*/, Real /*t*/) const override
124 {
125 return MatrixJF::Identity();
126 }
127
134 virtual VectorF f(VectorF const & x, Real const t) const = 0;
135
149 virtual MatrixJF Jf_x(VectorF const & x, Real const t) const = 0;
150
158 VectorF f_reverse(VectorF const & x, Real const t) const
159 {
160 return -this->f(x, -t);
161 }
162
170 MatrixJF Jf_x_reverse(VectorF const & x, Real const t) const
171 {
172 return -this->Jf_x(x, -t);
173 }
174
183 VectorF F_reverse(VectorF const & x, VectorF const & x_dot, Real const t) const
184 {
185 return -x_dot - this->f(x, -t);
186 }
187
198 MatrixJF JF_x_reverse(VectorF const & x, VectorF const &/*x_dot*/, Real const t) const
199 {
200 return -this->Jf_x(x, -t);
201 }
202
213 MatrixJF JF_x_dot_reverse(VectorF const &/*x*/, VectorF const &/*x_dot*/, Real /*t*/) const
214 {
215 return -MatrixJF::Identity();
216 }
217
218 }; // class Explicit
219
220 /*\
221 | _____ _ _ _ _ __ __
222 | | ____|_ ___ __ | (_) ___(_) |\ \ / / __ __ _ _ __ _ __ ___ _ __
223 | | _| \ \/ / '_ \| | |/ __| | __\ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__|
224 | | |___ > <| |_) | | | (__| | |_ \ V V /| | | (_| | |_) | |_) | __/ |
225 | |_____/_/\_\ .__/|_|_|\___|_|\__| \_/\_/ |_| \__,_| .__/| .__/ \___|_|
226 | |_| |_| |_|
227 \*/
228
239 template <typename Real, Integer N, Integer M = 0>
240 class ExplicitWrapper : public Explicit<Real, N, M>
241 {
242 public:
243 using Pointer = std::shared_ptr<ExplicitWrapper<Real, N, M>>;
244 using typename Explicit<Real, N, M>::VectorF;
245 using typename Explicit<Real, N, M>::MatrixJF;
246 using typename Explicit<Real, N, M>::VectorH;
247 using typename Explicit<Real, N, M>::MatrixJH;
248 using FunctionF = std::function<VectorF(VectorF const &, Real const)>;
249 using FunctionJF = std::function<MatrixJF(VectorF const &, Real const)>;
250 using FunctionH = std::function<VectorH(VectorF const &, Real const)>;
251 using FunctionJH = std::function<MatrixJH(VectorF const &, Real const)>;
252 using FunctionID = std::function<bool(VectorF const &, Real const)>;
253
254 inline const static FunctionH DefaultH = [](VectorF const &, Real const) {return VectorH::Zero();};
255 inline const static FunctionJH DefaultJH = [](VectorF const &, Real const) {return MatrixJH::Zero();};
256 inline const static FunctionID DefaultID = [](VectorF const &, Real const) {return true;};
257
258 private:
259 FunctionF m_f{nullptr};
261 FunctionH m_h{nullptr};
264
265 public:
275 FunctionID t_in_domain = DefaultID) : Explicit<Real, N, M>(), m_f(t_f), m_Jf_x(t_Jf_x), m_h(t_h),
276 m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
277 {}
278
288 ExplicitWrapper(std::string t_name, FunctionF t_f, FunctionJF t_Jf_x, FunctionH t_h = DefaultH,
289 FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID) : Explicit<Real, N, M>(t_name),
290 m_f(t_f), m_Jf_x(t_Jf_x), m_h(t_h), m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
291 {}
292
297
302 FunctionF & f() {return this->m_f;}
303
308 FunctionJF & Jf_x() {return this->m_Jf_x;}
309
314 FunctionH & h() {return this->m_h;}
315
320 FunctionJH & Jh_x() {return this->m_Jh_x;}
321
326 FunctionID & in_domain() {return this->m_in_domain;}
327
334 VectorF f(VectorF const & x, Real const t) const override
335 {
336 return this->m_f(x, t);
337 }
338
352 MatrixJF Jf_x(VectorF const & x, Real const t) const override
353 {
354 return this->m_Jf_x(x, t);
355 }
356
363 VectorH h(VectorF const & x, Real const t) const override
364 {
365 return this->m_h(x, t);
366 }
367
381 MatrixJH Jh_x(VectorF const & x, Real const t) const override
382 {
383 return this->m_Jh_x(x, t);
384 }
385
393 bool in_domain(VectorF const & x, Real const t) const override
394 {
395 return this->m_in_domain(x, t);
396 }
397
398 }; // class ExplicitWrapper
399
400} // namespace Sandals
401
402#endif // SANDALS_EXPLICIT_SYSTEM_HH
MatrixJF Jf_x_reverse(VectorF const &x, Real const t) const
Definition Explicit.hh:170
VectorF F(VectorF const &x, VectorF const &x_dot, Real const t) const override
Definition Explicit.hh:82
std::shared_ptr< Explicit< Real, N, M > > Pointer
Definition Explicit.hh:44
Explicit(std::string t_name)
Definition Explicit.hh:67
Explicit(Type t_type, std::string t_name)
Definition Explicit.hh:55
MatrixJF JF_x_dot_reverse(VectorF const &, VectorF const &, Real) const
Definition Explicit.hh:213
MatrixJF JF_x(VectorF const &x, VectorF const &, Real const t) const override
Definition Explicit.hh:103
MatrixJF JF_x_dot(VectorF const &, VectorF const &, Real) const override
Definition Explicit.hh:123
virtual VectorF f(VectorF const &x, Real const t) const =0
typename Implicit< Real, N, M >::Type Type
Definition Explicit.hh:47
MatrixJF JF_x_reverse(VectorF const &x, VectorF const &, Real const t) const
Definition Explicit.hh:198
Explicit()
Definition Explicit.hh:61
virtual MatrixJF Jf_x(VectorF const &x, Real const t) const =0
typename Implicit< Real, N, M >::MatrixJF MatrixJF
Definition Explicit.hh:46
VectorF f_reverse(VectorF const &x, Real const t) const
Definition Explicit.hh:158
VectorF F_reverse(VectorF const &x, VectorF const &x_dot, Real const t) const
Definition Explicit.hh:183
typename Implicit< Real, N, M >::VectorF VectorF
Definition Explicit.hh:45
static const FunctionH DefaultH
Definition Explicit.hh:254
std::function< MatrixJF(VectorF const &, Real const)> FunctionJF
Definition Explicit.hh:249
static const FunctionID DefaultID
Definition Explicit.hh:256
std::function< VectorH(VectorF const &, Real const)> FunctionH
Definition Explicit.hh:250
FunctionH & h()
Definition Explicit.hh:314
std::shared_ptr< ExplicitWrapper< Real, N, M > > Pointer
Definition Explicit.hh:243
ExplicitWrapper(std::string t_name, FunctionF t_f, FunctionJF t_Jf_x, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition Explicit.hh:288
FunctionJF & Jf_x()
Definition Explicit.hh:308
VectorF f(VectorF const &x, Real const t) const override
Definition Explicit.hh:334
FunctionF & f()
Definition Explicit.hh:302
MatrixJF Jf_x(VectorF const &x, Real const t) const override
Definition Explicit.hh:352
FunctionID m_in_domain
Definition Explicit.hh:263
FunctionJF m_Jf_x
Definition Explicit.hh:260
std::function< bool(VectorF const &, Real const)> FunctionID
Definition Explicit.hh:252
bool in_domain(VectorF const &x, Real const t) const override
Definition Explicit.hh:393
VectorH h(VectorF const &x, Real const t) const override
Definition Explicit.hh:363
FunctionF m_f
Definition Explicit.hh:259
FunctionJH & Jh_x()
Definition Explicit.hh:320
FunctionH m_h
Definition Explicit.hh:261
FunctionJH m_Jh_x
Definition Explicit.hh:262
MatrixJH Jh_x(VectorF const &x, Real const t) const override
Definition Explicit.hh:381
static const FunctionJH DefaultJH
Definition Explicit.hh:255
std::function< MatrixJH(VectorF const &, Real const)> FunctionJH
Definition Explicit.hh:251
ExplicitWrapper(FunctionF t_f, FunctionJF t_Jf_x, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition Explicit.hh:274
FunctionID & in_domain()
Definition Explicit.hh:326
std::function< VectorF(VectorF const &, Real const)> FunctionF
Definition Explicit.hh:248
~ExplicitWrapper()
Definition Explicit.hh:296
Eigen::Matrix< Real, N, N > MatrixJF
Definition Implicit.hh:47
Implicit(Type t_type, std::string t_name)
Definition Implicit.hh:61
Eigen::Vector< Real, N > VectorF
Definition Implicit.hh:46
Eigen::Vector< Real, M > VectorH
Definition Implicit.hh:48
enum class Type :Integer {IMPLICIT=0, EXPLICIT=1, SEMIEXPLICIT=1} Type
Definition Implicit.hh:44
Eigen::Matrix< Real, M, N > MatrixJH
Definition Implicit.hh:49
The namespace for the Sandals library.
Definition Sandals.hh:89