Sandals  v0.0.0
A C++ library for ODEs/DAEs integration
Loading...
Searching...
No Matches
SemiExplicit.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_SEMIEXPLICIT_SYSTEM_HH
14#define SANDALS_SEMIEXPLICIT_SYSTEM_HH
15
16#include <Sandals.hh>
18
19namespace Sandals {
20
21 /*\
22 | ____ _ _____ _ _ _ _
23 | / ___| ___ _ __ ___ (_) ____|_ ___ __ | (_) ___(_) |_
24 | \___ \ / _ \ '_ ` _ \| | _| \ \/ / '_ \| | |/ __| | __|
25 | ___) | __/ | | | | | | |___ > <| |_) | | | (__| | |_
26 | |____/ \___|_| |_| |_|_|_____/_/\_\ .__/|_|_|\___|_|\__|
27 | |_|
28 \*/
29
41 template <typename Real, Integer N, Integer M = 0>
42 class SemiExplicit : public Explicit<Real, N, M>
43 {
44 public:
46
47 using Pointer = std::shared_ptr<SemiExplicit<Real, N, M>>;
48 using VectorF = typename Explicit<Real, N, M>::VectorF;
49 using MatrixJF = typename Explicit<Real, N, M>::MatrixJF;
50 using MatrixA = typename Explicit<Real, N, M>::MatrixJF;
51 using TensorTA = typename std::vector<MatrixJF>;
52 using VectorB = typename Explicit<Real, N, M>::VectorF;
53 using MatrixJB = typename Explicit<Real, N, M>::MatrixJF;
54 using Type = typename Explicit<Real, N, M>::Type;
55
56 private:
57 mutable Eigen::FullPivLU<MatrixA> m_lu;
58
59 public:
63 SemiExplicit() : Explicit<Real, N, M>(Type::SEMIEXPLICIT, "(missing name)") {}
64
69 SemiExplicit(std::string t_name) : Explicit<Real, N, M>(Type::SEMIEXPLICIT, t_name) {}
70
84 VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
85 {
86 return this->A(x, t)*x_dot - this->b(x, t);
87 }
88
106 MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real t) const override
107 {
108 return -this->Jf_x(x, x_dot, t);
109 }
110
126 MatrixJF JF_x_dot(VectorF const &x, VectorF const &/*x_dot*/, Real t) const override
127 {
128 return this->A(x, t);
129 }
130
142 VectorF f(VectorF const &x, Real t) const override
143 {
144 this->m_lu.compute(this->A(x, t));
145 SANDALS_ASSERT(this->m_lu.rank() == N,
146 "Sandals:SemiExplicit::f(...): singular mass matrix A detected.");
147 return this->m_lu.solve(this->b(x, t));
148 }
149
165 MatrixJF Jf_x(VectorF const &x, VectorF const &x_dot, Real t) const
166 {
167 TensorTA TA_x{this->TA_x(x, t)};
168 MatrixJF tAp{MatrixJF::Zero()};
169 for (Integer i{0}; i < N; ++i) {tAp.col(i) = TA_x[i] * x_dot;} // TODO: Check if TA_x.size() = N
170 this->m_lu.compute(this->A(x, t));
171 SANDALS_ASSERT(this->m_lu.rank() == N,
172 "Sandals:SemiExplicit::Jf_x(...): singular mass matrix A detected.");
173 return this->m_lu.solve(this->Jb_x(x, t) - tAp);
174 }
175
190 MatrixJF Jf_x(VectorF const &x, Real t) const override {return this->Jf_x(x, this->f(x, t), t);}
191
198 virtual MatrixA A(VectorF const &x, Real t) const = 0;
199
213 virtual TensorTA TA_x(VectorF const &x, Real t) const = 0;
214
221 virtual VectorB b(VectorF const &x, Real t) const = 0;
222
236 virtual MatrixJB Jb_x(VectorF const &x, Real t) const = 0;
237
238 }; // class SemiExplicit
239
240 /*\
241 | ____ _ _____ _ _ _ _ __ __
242 | / ___| ___ _ __ ___ (_) ____|_ ___ __ | (_) ___(_) |\ \ / / __ __ _ _ __ _ __ ___ _ __
243 | \___ \ / _ \ '_ ` _ \| | _| \ \/ / '_ \| | |/ __| | __\ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__|
244 | ___) | __/ | | | | | | |___ > <| |_) | | | (__| | |_ \ V V /| | | (_| | |_) | |_) | __/ |
245 | |____/ \___|_| |_| |_|_|_____/_/\_\ .__/|_|_|\___|_|\__| \_/\_/ |_| \__,_| .__/| .__/ \___|_|
246 | |_| |_| |_|
247 \*/
248
260 template <typename Real, Integer N, Integer M = 0>
261 class SemiExplicitWrapper : public SemiExplicit<Real, N, M>
262 {
263 public:
265
266 using Pointer = std::shared_ptr<SemiExplicitWrapper<Real, N, M>>;
267 using VectorF = typename SemiExplicit<Real, N, M>::VectorF;
268 using MatrixA = typename SemiExplicit<Real, N, M>::MatrixA;
269 using TensorTA = typename SemiExplicit<Real, N, M>::TensorTA;
270 using VectorB = typename SemiExplicit<Real, N, M>::VectorB;
271 using MatrixJB = typename SemiExplicit<Real, N, M>::MatrixJB;
272 using VectorH = typename SemiExplicit<Real, N, M>::VectorH;
273 using MatrixJH = typename SemiExplicit<Real, N, M>::MatrixJH;
274 using FunctionA = std::function<MatrixA(VectorF const &, Real)>;
275 using FunctionTA = std::function<TensorTA(VectorF const &, Real)>;
276 using FunctionB = std::function<VectorB(VectorF const &, Real)>;
277 using FunctionJB = std::function<MatrixJB(VectorF const &, Real)>;
278 using FunctionH = std::function<VectorH(VectorF const &, Real)>;
279 using FunctionJH = std::function<MatrixJH(VectorF const &, Real)>;
280 using FunctionID = std::function<bool(VectorF const &, Real)>;
281
282 inline const static FunctionH DefaultH = [](VectorF const &, Real) {return VectorH::Zero();};
283 inline const static FunctionJH DefaultJH = [](VectorF const &, Real) {return MatrixJH::Zero();};
284 inline const static FunctionID DefaultID = [](VectorF const &, Real) {return true;};
285
286 private:
287 FunctionA m_A{nullptr};
289 FunctionB m_b{nullptr};
291 FunctionH m_h{nullptr};
294
295 public:
307 FunctionH t_h = DefaultH, FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID)
308 : SemiExplicit<Real, N, M>(), m_A(t_A), m_TA_x(t_TA_x), m_b(t_b), m_Jb_x(t_Jb_x), m_h(t_h),
309 m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
310 {}
311
323 SemiExplicitWrapper(std::string t_name, FunctionA t_A, FunctionTA t_TA_x, FunctionB t_b, FunctionJB t_Jb_x,
324 FunctionH t_h = DefaultH, FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID)
325 : SemiExplicit<Real, N, M>(t_name), m_A(t_A), m_TA_x(t_TA_x), m_b(t_b), m_Jb_x(t_Jb_x), m_h(t_h),
326 m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
327 {}
328
333
338 FunctionA & A() {return this->m_A;}
339
344 FunctionTA & TA_x() {return this->m_TA_x;}
345
350 FunctionB & b() {return this->m_b;}
351
356 FunctionJB & Jb_x() {return this->m_Jb_x;}
357
362 FunctionH & h() {return this->m_h;}
363
368 FunctionJH & Jh_x() {return this->m_Jh_x;}
369
374 FunctionID & in_domain() {return this->m_in_domain;}
375
382 MatrixA A(VectorF const &x, Real t) const override
383 {
384 return this->m_A(x, t);
385 }
386
400 TensorTA TA_x(VectorF const &x, Real t) const override
401 {
402 return this->m_TA_x(x, t);
403 }
404
411 VectorB b(VectorF const &x, Real t) const override
412 {
413 return this->m_b(x, t);
414 }
415
429 MatrixJB Jb_x(VectorF const &x, Real t) const override
430 {
431 return this->m_Jb_x(x, t);
432 }
433
434
441 VectorH h(VectorF const &x, Real t) const override
442 {
443 return this->m_h(x, t);
444 }
445
459 MatrixJH Jh_x(VectorF const &x, Real t) const override
460 {
461 return this->m_Jh_x(x, t);
462 }
463
471 bool in_domain(VectorF const &x, Real t) const override
472 {
473 return this->m_in_domain(x, t);
474 }
475
476 }; // class SemiExplicitWrapper
477
478} // namespace Sandals
479
480#endif // SANDALS_SEMIEXPLICIT_SYSTEM_HH
#define SANDALS_BASIC_CONSTANTS(Real)
Definition Sandals.hh:70
#define SANDALS_ASSERT(COND, MSG)
Definition Sandals.hh:44
Explicit(Type t_type, std::string t_name)
Definition Explicit.hh:57
MatrixJF Jf_x(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:190
SemiExplicit(std::string t_name)
Definition SemiExplicit.hh:69
typename Explicit< Real, N, M >::MatrixJF MatrixJB
Definition SemiExplicit.hh:53
typename Explicit< Real, N, M >::Type Type
Definition SemiExplicit.hh:54
virtual VectorB b(VectorF const &x, Real t) const =0
SemiExplicit()
Definition SemiExplicit.hh:63
typename Explicit< Real, N, M >::VectorF VectorF
Definition SemiExplicit.hh:48
virtual MatrixJB Jb_x(VectorF const &x, Real t) const =0
typename Explicit< Real, N, M >::MatrixJF MatrixJF
Definition SemiExplicit.hh:49
std::shared_ptr< SemiExplicit< Real, N, M > > Pointer
Definition SemiExplicit.hh:47
virtual TensorTA TA_x(VectorF const &x, Real t) const =0
MatrixJF Jf_x(VectorF const &x, VectorF const &x_dot, Real t) const
Definition SemiExplicit.hh:165
MatrixJF JF_x(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition SemiExplicit.hh:106
Eigen::FullPivLU< MatrixA > m_lu
Definition SemiExplicit.hh:57
MatrixJF JF_x_dot(VectorF const &x, VectorF const &, Real t) const override
Definition SemiExplicit.hh:126
virtual MatrixA A(VectorF const &x, Real t) const =0
typename Explicit< Real, N, M >::VectorF VectorB
Definition SemiExplicit.hh:52
typename Explicit< Real, N, M >::MatrixJF MatrixA
Definition SemiExplicit.hh:50
typename std::vector< MatrixJF > TensorTA
Definition SemiExplicit.hh:51
VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition SemiExplicit.hh:84
VectorF f(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:142
FunctionTA & TA_x()
Definition SemiExplicit.hh:344
MatrixJH Jh_x(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:459
typename SemiExplicit< Real, N, M >::MatrixJH MatrixJH
Definition SemiExplicit.hh:273
typename SemiExplicit< Real, N, M >::VectorB VectorB
Definition SemiExplicit.hh:270
FunctionID m_in_domain
Definition SemiExplicit.hh:293
std::shared_ptr< SemiExplicitWrapper< Real, N, M > > Pointer
Definition SemiExplicit.hh:266
std::function< VectorH(VectorF const &, Real)> FunctionH
Definition SemiExplicit.hh:278
VectorH h(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:441
FunctionJB m_Jb_x
Definition SemiExplicit.hh:290
VectorB b(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:411
FunctionJH & Jh_x()
Definition SemiExplicit.hh:368
MatrixA A(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:382
static const FunctionH DefaultH
Definition SemiExplicit.hh:282
bool in_domain(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:471
std::function< VectorB(VectorF const &, Real)> FunctionB
Definition SemiExplicit.hh:276
typename SemiExplicit< Real, N, M >::TensorTA TensorTA
Definition SemiExplicit.hh:269
TensorTA TA_x(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:400
FunctionB & b()
Definition SemiExplicit.hh:350
~SemiExplicitWrapper()
Definition SemiExplicit.hh:332
FunctionJB & Jb_x()
Definition SemiExplicit.hh:356
typename SemiExplicit< Real, N, M >::VectorH VectorH
Definition SemiExplicit.hh:272
MatrixJB Jb_x(VectorF const &x, Real t) const override
Definition SemiExplicit.hh:429
static const FunctionJH DefaultJH
Definition SemiExplicit.hh:283
FunctionB m_b
Definition SemiExplicit.hh:289
std::function< MatrixA(VectorF const &, Real)> FunctionA
Definition SemiExplicit.hh:274
std::function< MatrixJB(VectorF const &, Real)> FunctionJB
Definition SemiExplicit.hh:277
static const FunctionID DefaultID
Definition SemiExplicit.hh:284
SemiExplicitWrapper(FunctionA t_A, FunctionTA t_TA_x, FunctionB t_b, FunctionJB t_Jb_x, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition SemiExplicit.hh:306
typename SemiExplicit< Real, N, M >::VectorF VectorF
Definition SemiExplicit.hh:267
FunctionID & in_domain()
Definition SemiExplicit.hh:374
typename SemiExplicit< Real, N, M >::MatrixJB MatrixJB
Definition SemiExplicit.hh:271
std::function< MatrixJH(VectorF const &, Real)> FunctionJH
Definition SemiExplicit.hh:279
FunctionJH m_Jh_x
Definition SemiExplicit.hh:292
FunctionH m_h
Definition SemiExplicit.hh:291
std::function< bool(VectorF const &, Real)> FunctionID
Definition SemiExplicit.hh:280
typename SemiExplicit< Real, N, M >::MatrixA MatrixA
Definition SemiExplicit.hh:268
FunctionTA m_TA_x
Definition SemiExplicit.hh:288
SemiExplicitWrapper(std::string t_name, FunctionA t_A, FunctionTA t_TA_x, FunctionB t_b, FunctionJB t_Jb_x, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition SemiExplicit.hh:323
std::function< TensorTA(VectorF const &, Real)> FunctionTA
Definition SemiExplicit.hh:275
FunctionA m_A
Definition SemiExplicit.hh:287
FunctionA & A()
Definition SemiExplicit.hh:338
FunctionH & h()
Definition SemiExplicit.hh:362
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