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:
44 using Type = enum class Type : Integer {IMPLICIT = 0, EXPLICIT = 1, SEMIEXPLICIT = 1};
45 using Pointer = std::shared_ptr<Implicit<Real, N, M>>;
46 using VectorF = Eigen::Vector<Real, N>;
47 using MatrixJF = Eigen::Matrix<Real, N, N>;
48 using VectorH = Eigen::Vector<Real, M>;
49 using MatrixJH = Eigen::Matrix<Real, M, N>;
50
51 private:
52 Type m_type{Type::IMPLICIT};
53 std::string m_name;
54
55 protected:
61 Implicit(Type t_type, std::string t_name) : m_type(t_type), m_name(t_name) {}
62
63 public:
67 Implicit() : m_type(Type::IMPLICIT), m_name("(missing name)") {}
68
73 Implicit(std::string t_name) : m_type(Type::IMPLICIT), m_name(t_name) {}
74
78 virtual ~Implicit() {}
79
84 Type type() const {return this->m_type;}
85
90 bool is_implicit() const {return this->m_type == Type::IMPLICIT;}
91
96 bool is_explicit() const {return this->m_type == Type::EXPLICIT;}
97
102 bool is_semiexplicit() const {return this->m_type == Type::SEMIEXPLICIT;}
103
108 std::string & name() {return this->m_name;}
109
114 std::string const & name() const {return this->m_name;}
115
120 Integer equations_number() const {return N;}
121
126 Integer invariants_number() const {return M;}
127
135 virtual VectorF F(VectorF const & x, VectorF const & x_dot, Real const t) const = 0;
136
151 virtual MatrixJF JF_x(VectorF const & x, VectorF const & x_dot, Real const t) const = 0;
152
168 virtual MatrixJF JF_x_dot(VectorF const & x, VectorF const & x_dot, Real const t) const = 0;
169
176 virtual VectorH h(VectorF const & x, Real const t) const = 0;
177
191 virtual MatrixJH Jh_x(VectorF const & x, Real const t) const = 0;
192
200 virtual bool in_domain(VectorF const & x, Real const t) const = 0;
201
210 VectorF F_reverse(VectorF const & x, VectorF const & x_dot, Real const t) const
211 {
212 return -this->F(x, -x_dot, -t);
213 }
214
225 MatrixJF JF_x_reverse(VectorF const & x, VectorF const & x_dot, Real const t) const
226 {
227 return -this->JF_x(x, -x_dot, -t);
228 }
229
240 MatrixJF JF_x_dot_reverse(VectorF const & x, VectorF const & x_dot, Real const t) const
241 {
242 return this->JF_x_dot(x, -x_dot, -t);
243 }
244
245 }; // class Implicit
246
247 /*\
248 | ___ _ _ _ _ __ __
249 | |_ _|_ __ ___ _ __ | (_) ___(_) |\ \ / / __ __ _ _ __ _ __ ___ _ __
250 | | || '_ ` _ \| '_ \| | |/ __| | _ \ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__|
251 | | || | | | | | |_) | | | (__| | |_ \ V V /| | | (_| | |_) | |_) | __/ |
252 | |___|_| |_| |_| .__/|_|_|\___|_|\__| \_/\_/ |_| \__,_| .__/| .__/ \___|_|
253 | |_| |_| |_|
254 \*/
255
266 template <typename Real, Integer N, Integer M = 0>
267 class ImplicitWrapper : public Implicit<Real, N, M>
268 {
269 public:
270 using Pointer = std::shared_ptr<ImplicitWrapper<Real, N, M>>;
271 using typename Implicit<Real, N, M>::VectorF;
272 using typename Implicit<Real, N, M>::MatrixJF;
273 using typename Implicit<Real, N, M>::VectorH;
274 using typename Implicit<Real, N, M>::MatrixJH;
275 using FunctionF = std::function<VectorF(VectorF const &, VectorF const &, Real const)>;
276 using FunctionJF = std::function<MatrixJF(VectorF const &, VectorF const &, Real const)>;
277 using FunctionH = std::function<VectorH(VectorF const &, Real const)>;
278 using FunctionJH = std::function<MatrixJH(VectorF const &, Real const)>;
279 using FunctionID = std::function<bool(VectorF const &, Real const)>;
280
281 inline static const FunctionH DefaultH = [](VectorF const &, Real const) {return VectorH::Zero();};
282 inline static const FunctionJH DefaultJH = [](VectorF const &, Real const) {return MatrixJH::Zero();};
283 inline static const FunctionID DefaultID = [](VectorF const &, Real const) {return true;};
284
285 private:
286 FunctionF m_F{nullptr};
289 FunctionH m_h{nullptr};
292
293 public:
304 FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID) : Implicit<Real, N, M>(), m_F(t_F), m_JF_x(t_JF_x),
305 m_JF_x_dot(t_JF_x_dot), m_h(t_h), m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
306 {}
307
318 ImplicitWrapper(std::string t_name, FunctionF t_F, FunctionJF t_JF_x, FunctionJF t_JF_x_dot,
319 FunctionH t_h = DefaultH, FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID)
320 : 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),
321 m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
322 {}
323
328
333 FunctionF & F() {return this->m_F;}
334
339 FunctionJF & JF_x() {return this->m_JF_x;}
340
345 FunctionJF & JF_x_dot() {return this->m_JF_x_dot;}
346
351 FunctionH & h() {return this->m_h;}
352
357 FunctionJH & Jh_x() {return this->m_Jh_x;}
358
363 FunctionID & in_domain() {return this->m_in_domain;}
364
372 VectorF F(VectorF const & x, VectorF const & x_dot, Real const t) const override
373 {
374 return this->m_F(x, x_dot, t);
375 }
376
391 MatrixJF JF_x(VectorF const & x, VectorF const & x_dot, Real const t) const override
392 {
393 return this->m_JF_x(x, x_dot, t);
394 }
395
411 MatrixJF JF_x_dot(VectorF const & x, VectorF const & x_dot, Real const t) const override
412 {
413 return this->m_JF_x_dot(x, x_dot, t);
414 }
415
422 VectorH h(VectorF const & x, Real const t) const override
423 {
424 return this->m_h(x, t);
425 }
426
440 MatrixJH Jh_x(VectorF const & x, Real const t) const override
441 {
442 return this->m_Jh_x(x, t);
443 }
444
452 bool in_domain(VectorF const & x, Real const t) const override
453 {
454 return this->m_in_domain(x, t);
455 }
456
457 }; // class ImplicitWrapper
458
459} // namespace Sandals
460
461#endif // SANDALS_IMPLICIT_SYSTEM_HH
virtual ~Implicit()
Definition Implicit.hh:78
std::string const & name() const
Definition Implicit.hh:114
bool is_semiexplicit() const
Definition Implicit.hh:102
Eigen::Matrix< Real, N, N > MatrixJF
Definition Implicit.hh:47
virtual VectorH h(VectorF const &x, Real const t) const =0
Implicit(Type t_type, std::string t_name)
Definition Implicit.hh:61
Eigen::Vector< Real, N > VectorF
Definition Implicit.hh:46
MatrixJF JF_x_reverse(VectorF const &x, VectorF const &x_dot, Real const t) const
Definition Implicit.hh:225
MatrixJF JF_x_dot_reverse(VectorF const &x, VectorF const &x_dot, Real const t) const
Definition Implicit.hh:240
virtual MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real const t) const =0
bool is_implicit() const
Definition Implicit.hh:90
Eigen::Vector< Real, M > VectorH
Definition Implicit.hh:48
Implicit()
Definition Implicit.hh:67
std::string & name()
Definition Implicit.hh:108
enum class Type :Integer {IMPLICIT=0, EXPLICIT=1, SEMIEXPLICIT=1} Type
Definition Implicit.hh:44
bool is_explicit() const
Definition Implicit.hh:96
Type m_type
Definition Implicit.hh:52
VectorF F_reverse(VectorF const &x, VectorF const &x_dot, Real const t) const
Definition Implicit.hh:210
virtual MatrixJF JF_x_dot(VectorF const &x, VectorF const &x_dot, Real const t) const =0
Integer equations_number() const
Definition Implicit.hh:120
std::string m_name
Definition Implicit.hh:53
Type type() const
Definition Implicit.hh:84
virtual VectorF F(VectorF const &x, VectorF const &x_dot, Real const t) const =0
Integer invariants_number() const
Definition Implicit.hh:126
std::shared_ptr< Implicit< Real, N, M > > Pointer
Definition Implicit.hh:45
Eigen::Matrix< Real, M, N > MatrixJH
Definition Implicit.hh:49
virtual MatrixJH Jh_x(VectorF const &x, Real const t) const =0
virtual bool in_domain(VectorF const &x, Real const t) const =0
Implicit(std::string t_name)
Definition Implicit.hh:73
VectorH h(VectorF const &x, Real const t) const override
Definition Implicit.hh:422
std::function< bool(VectorF const &, Real const)> FunctionID
Definition Implicit.hh:279
FunctionID m_in_domain
Definition Implicit.hh:291
std::function< VectorF(VectorF const &, VectorF const &, Real const)> FunctionF
Definition Implicit.hh:275
FunctionJF & JF_x_dot()
Definition Implicit.hh:345
VectorF F(VectorF const &x, VectorF const &x_dot, Real const t) const override
Definition Implicit.hh:372
FunctionF & F()
Definition Implicit.hh:333
FunctionJF m_JF_x
Definition Implicit.hh:287
std::function< MatrixJF(VectorF const &, VectorF const &, Real const)> FunctionJF
Definition Implicit.hh:276
bool in_domain(VectorF const &x, Real const t) const override
Definition Implicit.hh:452
std::function< VectorH(VectorF const &, Real const)> FunctionH
Definition Implicit.hh:277
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:303
MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real const t) const override
Definition Implicit.hh:391
FunctionID & in_domain()
Definition Implicit.hh:363
MatrixJH Jh_x(VectorF const &x, Real const t) const override
Definition Implicit.hh:440
~ImplicitWrapper()
Definition Implicit.hh:327
FunctionJF m_JF_x_dot
Definition Implicit.hh:288
FunctionJH & Jh_x()
Definition Implicit.hh:357
FunctionJH m_Jh_x
Definition Implicit.hh:290
std::function< MatrixJH(VectorF const &, Real const)> FunctionJH
Definition Implicit.hh:278
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:318
static const FunctionID DefaultID
Definition Implicit.hh:283
FunctionF m_F
Definition Implicit.hh:286
FunctionJF & JF_x()
Definition Implicit.hh:339
FunctionH m_h
Definition Implicit.hh:289
static const FunctionH DefaultH
Definition Implicit.hh:281
std::shared_ptr< ImplicitWrapper< Real, N, M > > Pointer
Definition Implicit.hh:270
MatrixJF JF_x_dot(VectorF const &x, VectorF const &x_dot, Real const t) const override
Definition Implicit.hh:411
static const FunctionJH DefaultJH
Definition Implicit.hh:282
FunctionH & h()
Definition Implicit.hh:351
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