Sandals  v0.0.0
A C++ library for ODEs/DAEs integration
Loading...
Searching...
No Matches
Implicit.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_IMPLICIT_SYSTEM_HH
14#define SANDALS_IMPLICIT_SYSTEM_HH
15
16#include <Sandals.hh>
17
18namespace Sandals
19{
20
21 /*\
22 | ___ _ _ _ _
23 | |_ _|_ __ ___ _ __ | (_) ___(_) |_
24 | | || '_ ` _ \| '_ \| | |/ __| | __|
25 | | || | | | | | |_) | | | (__| | |_
26 | |___|_| |_| |_| .__/|_|_|\___|_|\__|
27 | |_|
28 \*/
29
40 template <typename Real, Integer N, Integer M = 0>
42 {
43 public:
45
46 using Type = enum class Type : Integer {IMPLICIT = 0, EXPLICIT = 1, SEMIEXPLICIT = 1};
47 using Pointer = std::shared_ptr<Implicit<Real, N, M>>;
48 using VectorF = Eigen::Vector<Real, N>;
49 using MatrixJF = Eigen::Matrix<Real, N, N>;
50 using VectorH = Eigen::Vector<Real, M>;
51 using MatrixJH = Eigen::Matrix<Real, M, N>;
52
53 private:
54 Type m_type{Type::IMPLICIT};
55 std::string m_name;
56
57 protected:
63 Implicit(Type t_type, std::string t_name) : m_type(t_type), m_name(t_name) {}
64
65 public:
69 Implicit() : m_type(Type::IMPLICIT), m_name("(missing name)") {}
70
75 Implicit(std::string t_name) : m_type(Type::IMPLICIT), m_name(t_name) {}
76
80 virtual ~Implicit() {}
81
86 Type type() const {return this->m_type;}
87
92 bool is_implicit() const {return this->m_type == Type::IMPLICIT;}
93
98 bool is_explicit() const {return this->m_type == Type::EXPLICIT;}
99
104 bool is_semiexplicit() const {return this->m_type == Type::SEMIEXPLICIT;}
105
110 std::string & name() {return this->m_name;}
111
116 std::string const & name() const {return this->m_name;}
117
122 Integer equations_number() const {return N;}
123
128 Integer invariants_number() const {return M;}
129
137 virtual VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const = 0;
138
153 virtual MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real t) const = 0;
154
170 virtual MatrixJF JF_x_dot(VectorF const &x, VectorF const &x_dot, Real t) const = 0;
171
178 virtual VectorH h(VectorF const &x, Real t) const = 0;
179
193 virtual MatrixJH Jh_x(VectorF const &x, Real t) const = 0;
194
202 virtual bool in_domain(VectorF const &x, Real t) const = 0;
203
212 VectorF F_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
213 {
214 return -this->F(x, -x_dot, -t);
215 }
216
227 MatrixJF JF_x_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
228 {
229 return -this->JF_x(x, -x_dot, -t);
230 }
231
242 MatrixJF JF_x_dot_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
243 {
244 return this->JF_x_dot(x, -x_dot, -t);
245 }
246
247 }; // class Implicit
248
249 /*\
250 | ___ _ _ _ _ __ __
251 | |_ _|_ __ ___ _ __ | (_) ___(_) |\ \ / / __ __ _ _ __ _ __ ___ _ __
252 | | || '_ ` _ \| '_ \| | |/ __| | _ \ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__|
253 | | || | | | | | |_) | | | (__| | |_ \ V V /| | | (_| | |_) | |_) | __/ |
254 | |___|_| |_| |_| .__/|_|_|\___|_|\__| \_/\_/ |_| \__,_| .__/| .__/ \___|_|
255 | |_| |_| |_|
256 \*/
257
268 template <typename Real, Integer N, Integer M = 0>
269 class ImplicitWrapper : public Implicit<Real, N, M>
270 {
271 public:
273
274 using Pointer = std::shared_ptr<ImplicitWrapper<Real, N, M>>;
275 using VectorF = typename Implicit<Real, N, M>::VectorF;
276 using MatrixJF = typename Implicit<Real, N, M>::MatrixJF;
277 using VectorH = typename Implicit<Real, N, M>::VectorH;
278 using MatrixJH = typename Implicit<Real, N, M>::MatrixJH;
279 using FunctionF = std::function<VectorF(VectorF const &, VectorF const &, Real)>;
280 using FunctionJF = std::function<MatrixJF(VectorF const &, VectorF const &, Real)>;
281 using FunctionH = std::function<VectorH(VectorF const &, Real)>;
282 using FunctionJH = std::function<MatrixJH(VectorF const &, Real)>;
283 using FunctionID = std::function<bool(VectorF const &, Real)>;
284
285 inline static const FunctionH DefaultH = [](VectorF const &, Real) {return VectorH::Zero();};
286 inline static const FunctionJH DefaultJH = [](VectorF const &, Real) {return MatrixJH::Zero();};
287 inline static const FunctionID DefaultID = [](VectorF const &, Real) {return true;};
288
289 private:
290 FunctionF m_F{nullptr};
293 FunctionH m_h{nullptr};
296
297 public:
308 FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID) : Implicit<Real, N, M>(), m_F(t_F), m_JF_x(t_JF_x),
309 m_JF_x_dot(t_JF_x_dot), m_h(t_h), m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
310 {}
311
322 ImplicitWrapper(std::string t_name, FunctionF t_F, FunctionJF t_JF_x, FunctionJF t_JF_x_dot,
323 FunctionH t_h = DefaultH, FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID)
324 : Implicit<Real, N, M>(t_name), m_F(t_F), m_JF_x(t_JF_x), m_JF_x_dot(t_JF_x_dot), m_h(t_h),
325 m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
326 {}
327
332
337 FunctionF & F() {return this->m_F;}
338
343 FunctionJF & JF_x() {return this->m_JF_x;}
344
349 FunctionJF & JF_x_dot() {return this->m_JF_x_dot;}
350
355 FunctionH & h() {return this->m_h;}
356
361 FunctionJH & Jh_x() {return this->m_Jh_x;}
362
367 FunctionID & in_domain() {return this->m_in_domain;}
368
376 VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
377 {
378 return this->m_F(x, x_dot, t);
379 }
380
395 MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real t) const override
396 {
397 return this->m_JF_x(x, x_dot, t);
398 }
399
415 MatrixJF JF_x_dot(VectorF const &x, VectorF const &x_dot, Real t) const override
416 {
417 return this->m_JF_x_dot(x, x_dot, t);
418 }
419
426 VectorH h(VectorF const &x, Real t) const override
427 {
428 return this->m_h(x, t);
429 }
430
444 MatrixJH Jh_x(VectorF const &x, Real t) const override
445 {
446 return this->m_Jh_x(x, t);
447 }
448
456 bool in_domain(VectorF const &x, Real t) const override
457 {
458 return this->m_in_domain(x, t);
459 }
460
461 }; // class ImplicitWrapper
462
463} // namespace Sandals
464
465#endif // SANDALS_IMPLICIT_SYSTEM_HH
#define SANDALS_BASIC_CONSTANTS(Real)
Definition Sandals.hh:70
virtual ~Implicit()
Definition Implicit.hh:80
std::string const & name() const
Definition Implicit.hh:116
bool is_semiexplicit() const
Definition Implicit.hh:104
Eigen::Matrix< Real, N, N > MatrixJF
Definition Implicit.hh:49
virtual MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real t) const =0
virtual MatrixJH Jh_x(VectorF const &x, Real t) const =0
Implicit(Type t_type, std::string t_name)
Definition Implicit.hh:63
Eigen::Vector< Real, N > VectorF
Definition Implicit.hh:48
bool is_implicit() const
Definition Implicit.hh:92
Eigen::Vector< Real, M > VectorH
Definition Implicit.hh:50
Implicit()
Definition Implicit.hh:69
virtual bool in_domain(VectorF const &x, Real t) const =0
std::string & name()
Definition Implicit.hh:110
enum class Type :Integer {IMPLICIT=0, EXPLICIT=1, SEMIEXPLICIT=1} Type
Definition Implicit.hh:46
bool is_explicit() const
Definition Implicit.hh:98
Type m_type
Definition Implicit.hh:54
virtual VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const =0
Integer equations_number() const
Definition Implicit.hh:122
std::string m_name
Definition Implicit.hh:55
Type type() const
Definition Implicit.hh:86
MatrixJF JF_x_dot_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
Definition Implicit.hh:242
Integer invariants_number() const
Definition Implicit.hh:128
std::shared_ptr< Implicit< Real, N, M > > Pointer
Definition Implicit.hh:47
Eigen::Matrix< Real, M, N > MatrixJH
Definition Implicit.hh:51
VectorF F_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
Definition Implicit.hh:212
MatrixJF JF_x_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
Definition Implicit.hh:227
virtual VectorH h(VectorF const &x, Real t) const =0
virtual MatrixJF JF_x_dot(VectorF const &x, VectorF const &x_dot, Real t) const =0
Implicit(std::string t_name)
Definition Implicit.hh:75
FunctionID m_in_domain
Definition Implicit.hh:295
std::function< bool(VectorF const &, Real)> FunctionID
Definition Implicit.hh:283
FunctionJF & JF_x_dot()
Definition Implicit.hh:349
std::function< VectorH(VectorF const &, Real)> FunctionH
Definition Implicit.hh:281
FunctionF & F()
Definition Implicit.hh:337
std::function< VectorF(VectorF const &, VectorF const &, Real)> FunctionF
Definition Implicit.hh:279
FunctionJF m_JF_x
Definition Implicit.hh:291
typename Implicit< Real, N, M >::VectorH VectorH
Definition Implicit.hh:277
MatrixJH Jh_x(VectorF const &x, Real t) const override
Definition Implicit.hh:444
ImplicitWrapper(FunctionF t_F, FunctionJF t_JF_x, FunctionJF t_JF_x_dot, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition Implicit.hh:307
typename Implicit< Real, N, M >::VectorF VectorF
Definition Implicit.hh:275
FunctionID & in_domain()
Definition Implicit.hh:367
~ImplicitWrapper()
Definition Implicit.hh:331
bool in_domain(VectorF const &x, Real t) const override
Definition Implicit.hh:456
FunctionJF m_JF_x_dot
Definition Implicit.hh:292
FunctionJH & Jh_x()
Definition Implicit.hh:361
FunctionJH m_Jh_x
Definition Implicit.hh:294
std::function< MatrixJF(VectorF const &, VectorF const &, Real)> FunctionJF
Definition Implicit.hh:280
ImplicitWrapper(std::string t_name, FunctionF t_F, FunctionJF t_JF_x, FunctionJF t_JF_x_dot, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition Implicit.hh:322
typename Implicit< Real, N, M >::MatrixJF MatrixJF
Definition Implicit.hh:276
static const FunctionID DefaultID
Definition Implicit.hh:287
FunctionF m_F
Definition Implicit.hh:290
FunctionJF & JF_x()
Definition Implicit.hh:343
FunctionH m_h
Definition Implicit.hh:293
static const FunctionH DefaultH
Definition Implicit.hh:285
std::shared_ptr< ImplicitWrapper< Real, N, M > > Pointer
Definition Implicit.hh:274
MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition Implicit.hh:395
std::function< MatrixJH(VectorF const &, Real)> FunctionJH
Definition Implicit.hh:282
VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition Implicit.hh:376
static const FunctionJH DefaultJH
Definition Implicit.hh:286
FunctionH & h()
Definition Implicit.hh:355
VectorH h(VectorF const &x, Real t) const override
Definition Implicit.hh:426
MatrixJF JF_x_dot(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition Implicit.hh:415
typename Implicit< Real, N, M >::MatrixJH MatrixJH
Definition Implicit.hh:278
The namespace for the Sandals library.
Definition Sandals.hh:89
SANDALS_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Sandals.hh:97