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:
45
46 using Pointer = std::shared_ptr<Explicit<Real, N, M>>;
47 using VectorF = typename Implicit<Real, N, M>::VectorF;
48 using MatrixJF = typename Implicit<Real, N, M>::MatrixJF;
49 using Type = typename Implicit<Real, N, M>::Type;
50
51 protected:
57 Explicit(Type t_type, std::string t_name) : Implicit<Real, N, M>(t_type, t_name) {}
58
59 public:
63 Explicit() : Implicit<Real, N, M>(Type::EXPLICIT, "(missing name)") {}
64
69 Explicit(std::string t_name) : Implicit<Real, N, M>(Type::EXPLICIT, t_name) {}
70
84 VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
85 {
86 return x_dot - this->f(x, t);
87 }
88
105 MatrixJF JF_x(VectorF const &x, VectorF const &/*x_dot*/, Real t) const override
106 {
107 return -this->Jf_x(x, t);
108 }
109
125 MatrixJF JF_x_dot(VectorF const &/*x*/, VectorF const &/*x_dot*/, Real /*t*/) const override
126 {
127 return MatrixJF::Identity();
128 }
129
136 virtual VectorF f(VectorF const &x, Real t) const = 0;
137
151 virtual MatrixJF Jf_x(VectorF const &x, Real t) const = 0;
152
160 VectorF f_reverse(VectorF const &x, Real t) const
161 {
162 return -this->f(x, -t);
163 }
164
172 MatrixJF Jf_x_reverse(VectorF const &x, Real t) const
173 {
174 return -this->Jf_x(x, -t);
175 }
176
185 VectorF F_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
186 {
187 return -x_dot - this->f(x, -t);
188 }
189
200 MatrixJF JF_x_reverse(VectorF const &x, VectorF const &/*x_dot*/, Real t) const
201 {
202 return -this->Jf_x(x, -t);
203 }
204
215 MatrixJF JF_x_dot_reverse(VectorF const &/*x*/, VectorF const &/*x_dot*/, Real /*t*/) const
216 {
217 return -MatrixJF::Identity();
218 }
219
220 }; // class Explicit
221
222 /*\
223 | _____ _ _ _ _ __ __
224 | | ____|_ ___ __ | (_) ___(_) |\ \ / / __ __ _ _ __ _ __ ___ _ __
225 | | _| \ \/ / '_ \| | |/ __| | __\ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__|
226 | | |___ > <| |_) | | | (__| | |_ \ V V /| | | (_| | |_) | |_) | __/ |
227 | |_____/_/\_\ .__/|_|_|\___|_|\__| \_/\_/ |_| \__,_| .__/| .__/ \___|_|
228 | |_| |_| |_|
229 \*/
230
241 template <typename Real, Integer N, Integer M = 0>
242 class ExplicitWrapper : public Explicit<Real, N, M>
243 {
244 public:
246
247 using Pointer = std::shared_ptr<ExplicitWrapper<Real, N, M>>;
248 using VectorF = typename Explicit<Real, N, M>::VectorF;
249 using MatrixJF = typename Explicit<Real, N, M>::MatrixJF;
250 using VectorH = typename Explicit<Real, N, M>::VectorH;
251 using MatrixJH = typename Explicit<Real, N, M>::MatrixJH;
252 using FunctionF = std::function<VectorF(VectorF const &, Real)>;
253 using FunctionJF = std::function<MatrixJF(VectorF const &, Real)>;
254 using FunctionH = std::function<VectorH(VectorF const &, Real)>;
255 using FunctionJH = std::function<MatrixJH(VectorF const &, Real)>;
256 using FunctionID = std::function<bool(VectorF const &, Real)>;
257
258 inline const static FunctionH DefaultH = [](VectorF const &, Real) {return VectorH::Zero();};
259 inline const static FunctionJH DefaultJH = [](VectorF const &, Real) {return MatrixJH::Zero();};
260 inline const static FunctionID DefaultID = [](VectorF const &, Real) {return true;};
261
262 private:
263 FunctionF m_f{nullptr};
265 FunctionH m_h{nullptr};
268
269 public:
279 FunctionID t_in_domain = DefaultID) : Implicit<Real, N, M>(), m_f(t_f), m_Jf_x(t_Jf_x), m_h(t_h),
280 m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
281 {}
282
292 ExplicitWrapper(std::string t_name, FunctionF t_f, FunctionJF t_Jf_x, FunctionH t_h = DefaultH,
293 FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID) : Implicit<Real, N, M>(t_name),
294 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)
295 {}
296
301
306 FunctionF & f() {return this->m_f;}
307
312 FunctionJF & Jf_x() {return this->m_Jf_x;}
313
318 FunctionH & h() {return this->m_h;}
319
324 FunctionJH & Jh_x() {return this->m_Jh_x;}
325
330 FunctionID & in_domain() {return this->m_in_domain;}
331
338 VectorF f(VectorF const &x, Real t) const override
339 {
340 return this->m_f(x, t);
341 }
342
356 MatrixJF Jf_x(VectorF const &x, Real t) const override
357 {
358 return this->m_Jf_x(x, t);
359 }
360
367 VectorH h(VectorF const &x, Real t) const override
368 {
369 return this->m_h(x, t);
370 }
371
385 MatrixJH Jh_x(VectorF const &x, Real t) const override
386 {
387 return this->m_Jh_x(x, t);
388 }
389
397 bool in_domain(VectorF const &x, Real t) const override
398 {
399 return this->m_in_domain(x, t);
400 }
401
402 }; // class ExplicitWrapper
403
404} // namespace Sandals
405
406#endif // SANDALS_EXPLICIT_SYSTEM_HH
#define SANDALS_BASIC_CONSTANTS(Real)
Definition Sandals.hh:70
VectorF f_reverse(VectorF const &x, Real t) const
Definition Explicit.hh:160
MatrixJF JF_x_reverse(VectorF const &x, VectorF const &, Real t) const
Definition Explicit.hh:200
std::shared_ptr< Explicit< Real, N, M > > Pointer
Definition Explicit.hh:46
Explicit(std::string t_name)
Definition Explicit.hh:69
Explicit(Type t_type, std::string t_name)
Definition Explicit.hh:57
MatrixJF JF_x_dot_reverse(VectorF const &, VectorF const &, Real) const
Definition Explicit.hh:215
MatrixJF JF_x(VectorF const &x, VectorF const &, Real t) const override
Definition Explicit.hh:105
MatrixJF JF_x_dot(VectorF const &, VectorF const &, Real) const override
Definition Explicit.hh:125
virtual MatrixJF Jf_x(VectorF const &x, Real t) const =0
virtual VectorF f(VectorF const &x, Real t) const =0
MatrixJF Jf_x_reverse(VectorF const &x, Real t) const
Definition Explicit.hh:172
typename Implicit< Real, N, M >::Type Type
Definition Explicit.hh:49
Explicit()
Definition Explicit.hh:63
typename Implicit< Real, N, M >::MatrixJF MatrixJF
Definition Explicit.hh:48
VectorF F_reverse(VectorF const &x, VectorF const &x_dot, Real t) const
Definition Explicit.hh:185
VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition Explicit.hh:84
typename Implicit< Real, N, M >::VectorF VectorF
Definition Explicit.hh:47
static const FunctionH DefaultH
Definition Explicit.hh:258
typename Explicit< Real, N, M >::MatrixJF MatrixJF
Definition Explicit.hh:249
std::function< MatrixJH(VectorF const &, Real)> FunctionJH
Definition Explicit.hh:255
bool in_domain(VectorF const &x, Real t) const override
Definition Explicit.hh:397
static const FunctionID DefaultID
Definition Explicit.hh:260
FunctionH & h()
Definition Explicit.hh:318
std::shared_ptr< ExplicitWrapper< Real, N, M > > Pointer
Definition Explicit.hh:247
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:292
typename Explicit< Real, N, M >::MatrixJH MatrixJH
Definition Explicit.hh:251
FunctionJF & Jf_x()
Definition Explicit.hh:312
FunctionF & f()
Definition Explicit.hh:306
typename Explicit< Real, N, M >::VectorH VectorH
Definition Explicit.hh:250
FunctionID m_in_domain
Definition Explicit.hh:267
MatrixJF Jf_x(VectorF const &x, Real t) const override
Definition Explicit.hh:356
FunctionJF m_Jf_x
Definition Explicit.hh:264
typename Explicit< Real, N, M >::VectorF VectorF
Definition Explicit.hh:248
FunctionF m_f
Definition Explicit.hh:263
FunctionJH & Jh_x()
Definition Explicit.hh:324
FunctionH m_h
Definition Explicit.hh:265
std::function< VectorF(VectorF const &, Real)> FunctionF
Definition Explicit.hh:252
std::function< MatrixJF(VectorF const &, Real)> FunctionJF
Definition Explicit.hh:253
std::function< VectorH(VectorF const &, Real)> FunctionH
Definition Explicit.hh:254
FunctionJH m_Jh_x
Definition Explicit.hh:266
VectorF f(VectorF const &x, Real t) const override
Definition Explicit.hh:338
static const FunctionJH DefaultJH
Definition Explicit.hh:259
MatrixJH Jh_x(VectorF const &x, Real t) const override
Definition Explicit.hh:385
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:278
FunctionID & in_domain()
Definition Explicit.hh:330
~ExplicitWrapper()
Definition Explicit.hh:300
std::function< bool(VectorF const &, Real)> FunctionID
Definition Explicit.hh:256
VectorH h(VectorF const &x, Real t) const override
Definition Explicit.hh:367
Class container for the system of implicit ODEs/DAEs.
Definition Implicit.hh:42
Implicit(Type t_type, std::string t_name)
Definition Implicit.hh:63
The namespace for the Sandals library.
Definition Sandals.hh:89