Sandals  v0.0.0
A C++ library for ODEs/DAEs integration
Loading...
Searching...
No Matches
Linear.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#pragma once
11
12#ifndef SANDALS_LINEAR_SYSTEM_HH
13#define SANDALS_LINEAR_SYSTEM_HH
14
15#include <Sandals.hh>
17
18namespace Sandals {
19
20 /*\
21 | _ _
22 | | | (_)_ __ ___ __ _ _ __
23 | | | | | '_ \ / _ \/ _` | '__|
24 | | |___| | | | | __/ (_| | |
25 | |_____|_|_| |_|\___|\__,_|_|
26 |
27 \*/
28
39 template <typename Real, Integer N, Integer M = 0>
40 class Linear : public Explicit<Real, N, M>
41 {
42 public:
44 const Real SQRT_EPSILON{std::sqrt(EPSILON)}; \
45
46 using Pointer = std::shared_ptr<SemiExplicit<Real, N, M>>;
53
54 private:
55 Eigen::FullPivLU<MatrixE> m_lu;
56
57 public:
61 Linear() : Explicit<Real, N, M>(Type::LINEAR, "(missing name)") {}
62
67 Linear(std::string t_name) : Explicit<Real, N, M>(Type::LINEAR, t_name) {}
68
82 VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
83 {
84 return this->E(t)*x_dot - this->A(t)*x - this->b(t);
85 }
86
102 MatrixJF JF_x(VectorF const &/*x*/, VectorF const &/*x_dot*/, Real t) const override {return -this->A(t);}
103
119 MatrixJF JF_x_dot(VectorF const &/*x*/, VectorF const &/*x_dot*/, Real t) const override {return this->E(t);}
120
132 VectorF f(VectorF const &x, Real t) const override
133 {
134 this->m_lu.compute(this->E(t));
135 SANDALS_ASSERT(this->m_lu.rank() == N, "Sandals:Linear::f(...): singular mass matrix E(t) detected.");
136 return this->m_lu.solve(this->A(t)*x + this->b(t));
137 }
138
152 MatrixJF Jf_x(VectorF const &/*x*/, Real t) const override
153 {
154 this->m_lu.compute(this->E(t));
155 SANDALS_ASSERT(this->m_lu.rank() == N, "Sandals:Linear::Jf_x(...): singular mass matrix E(t) detected.");
156 return this->m_lu.solve(this->A(t));
157 }
158
164 virtual MatrixE E(Real t) const = 0;
165
171 virtual MatrixA A(Real t) const = 0;
172
178 virtual VectorB b(Real t) const = 0;
179
180 }; // class Linear
181
182 /*\
183 | _ _ __ __
184 | | | (_)_ __ ___ __ _ _ _\ \ / / __ __ _ _ __ _ __ ___ _ __
185 | | | | | '_ \ / _ \/ _` | '__\ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__|
186 | | |___| | | | | __/ (_| | | \ V V /| | | (_| | |_) | |_) | __/ |
187 | |_____|_|_| |_|\___|\__,_|_| \_/\_/ |_| \__,_| .__/| .__/ \___|_|
188 | |_| |_|
189 \*/
190
201 template <typename Real, Integer N, Integer M = 0>
202 class LinearWrapper : public Linear<Real, N, M>
203 {
204 public:
206
207 using Pointer = std::shared_ptr<LinearWrapper<Real, N, M>>;
208 using VectorF = typename Linear<Real, N, M>::VectorF;
209 using MatrixJF = typename Linear<Real, N, M>::MatrixJF;
210 using MatrixE = typename Linear<Real, N, M>::MatrixJF;
211 using MatrixA = typename Linear<Real, N, M>::MatrixJF;
212 using VectorB = typename Linear<Real, N, M>::VectorF;
213 using VectorH = typename Explicit<Real, N, M>::VectorH;
214 using MatrixJH = typename Explicit<Real, N, M>::MatrixJH;
215 using FunctionE = std::function<MatrixE(Real)>;
216 using FunctionA = std::function<MatrixA(Real)>;
217 using FunctionB = std::function<VectorB(Real)>;
218 using FunctionH = std::function<VectorH(VectorF const &, Real)>;
219 using FunctionJH = std::function<MatrixJH(VectorF const &, Real)>;
220 using FunctionID = std::function<bool(VectorF const &, Real)>;
221
222 inline const static FunctionH DefaultH = [](VectorF const &, Real) {return VectorH::Zero();};
223 inline const static FunctionJH DefaultJH = [](VectorF const &, Real) {return MatrixJH::Zero();};
224 inline const static FunctionID DefaultID = [](VectorF const &, Real) {return true;};
225
226 private:
227 FunctionE m_E{nullptr};
228 FunctionA m_A{nullptr};
229 FunctionB m_b{nullptr};
230 FunctionH m_h{nullptr};
233
234 public:
245 t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID) : Linear<Real, N, M>("(missing name)"),
246 m_E(t_E), m_A(t_A), m_b(t_b), m_h(t_h), m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
247 {}
248
259 LinearWrapper(std::string t_name, FunctionE t_E, FunctionA t_A, FunctionB t_b, FunctionH t_h = DefaultH,
260 FunctionJH t_Jh_x = DefaultJH, FunctionID t_in_domain = DefaultID) : Linear<Real, N, M>(t_name),
261 m_E(t_E), m_A(t_A), m_b(t_b), m_h(t_h), m_Jh_x(t_Jh_x), m_in_domain(t_in_domain)
262 {}
263
268
273 FunctionE & E() {return this->m_E;}
274
279 FunctionA & A() {return this->m_A;}
280
285 FunctionB & b() {return this->m_b;}
286
291 FunctionH & h() {return this->m_h;}
292
297 FunctionJH & Jh_x() {return this->m_Jh_x;}
298
303 FunctionID & in_domain() {return this->m_in_domain;}
304
310 MatrixE E(Real t) const override
311 {
312 return this->m_E(t);
313 }
314
320 MatrixA A(Real t) const override
321 {
322 return this->m_A(t);
323 }
324
330 VectorB b(Real t) const override
331 {
332 return this->m_b(t);
333 }
334
341 VectorH h(VectorF const &x, Real t) const override
342 {
343 return this->m_h(x, t);
344 }
345
359 MatrixJH Jh_x(VectorF const &x, Real t) const override
360 {
361 return this->m_Jh_x(x, t);
362 }
363
371 bool in_domain(VectorF const &x, Real t) const override
372 {
373 return this->m_in_domain(x, t);
374 }
375
376 }; // class LinearWrapper
377
378} // namespace Sandals
379
380#endif // SANDALS_LINEAR_SYSTEM_HH
#define SANDALS_BASIC_CONSTANTS(Real)
Definition Sandals.hh:70
#define SANDALS_ASSERT(COND, MSG)
Definition Sandals.hh:44
Class container for the system of explicit ODEs.
Definition Explicit.hh:42
Explicit(Type t_type, std::string t_name)
Definition Explicit.hh:57
typename Implicit< Real, N, M >::Type Type
Definition Explicit.hh:49
typename Implicit< Real, N, M >::MatrixJF MatrixJF
Definition Explicit.hh:48
typename Implicit< Real, N, M >::VectorF VectorF
Definition Explicit.hh:47
typename Explicit< Real, N, M >::Type Type
Definition Linear.hh:52
Eigen::FullPivLU< MatrixE > m_lu
Definition Linear.hh:55
virtual VectorB b(Real t) const =0
typename Explicit< Real, N, M >::VectorF VectorF
Definition Linear.hh:47
typename Explicit< Real, N, M >::MatrixJF MatrixA
Definition Linear.hh:50
MatrixJF JF_x_dot(VectorF const &, VectorF const &, Real t) const override
Definition Linear.hh:119
VectorF f(VectorF const &x, Real t) const override
Definition Linear.hh:132
typename Explicit< Real, N, M >::MatrixJF MatrixE
Definition Linear.hh:49
virtual MatrixE E(Real t) const =0
VectorF F(VectorF const &x, VectorF const &x_dot, Real t) const override
Definition Linear.hh:82
Linear()
Definition Linear.hh:61
typename Explicit< Real, N, M >::MatrixJF MatrixJF
Definition Linear.hh:48
typename Explicit< Real, N, M >::VectorF VectorB
Definition Linear.hh:51
std::shared_ptr< SemiExplicit< Real, N, M > > Pointer
Definition Linear.hh:46
const Real SQRT_EPSILON
Definition Linear.hh:44
MatrixJF Jf_x(VectorF const &, Real t) const override
Definition Linear.hh:152
virtual MatrixA A(Real t) const =0
MatrixJF JF_x(VectorF const &, VectorF const &, Real t) const override
Definition Linear.hh:102
Linear(std::string t_name)
Definition Linear.hh:67
std::function< MatrixJH(VectorF const &, Real)> FunctionJH
Definition Linear.hh:219
LinearWrapper(FunctionE t_E, FunctionA t_A, FunctionB t_b, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition Linear.hh:244
std::function< VectorB(Real)> FunctionB
Definition Linear.hh:217
bool in_domain(VectorF const &x, Real t) const override
Definition Linear.hh:371
FunctionH & h()
Definition Linear.hh:291
typename Explicit< Real, N, M >::VectorH VectorH
Definition Linear.hh:213
~LinearWrapper()
Definition Linear.hh:267
static const FunctionH DefaultH
Definition Linear.hh:222
std::function< bool(VectorF const &, Real)> FunctionID
Definition Linear.hh:220
FunctionH m_h
Definition Linear.hh:230
FunctionB m_b
Definition Linear.hh:229
FunctionID & in_domain()
Definition Linear.hh:303
static const FunctionJH DefaultJH
Definition Linear.hh:223
LinearWrapper(std::string t_name, FunctionE t_E, FunctionA t_A, FunctionB t_b, FunctionH t_h=DefaultH, FunctionJH t_Jh_x=DefaultJH, FunctionID t_in_domain=DefaultID)
Definition Linear.hh:259
FunctionB & b()
Definition Linear.hh:285
std::function< MatrixA(Real)> FunctionA
Definition Linear.hh:216
FunctionE & E()
Definition Linear.hh:273
FunctionE m_E
Definition Linear.hh:227
std::function< VectorH(VectorF const &, Real)> FunctionH
Definition Linear.hh:218
typename Explicit< Real, N, M >::MatrixJH MatrixJH
Definition Linear.hh:214
FunctionA m_A
Definition Linear.hh:228
FunctionID m_in_domain
Definition Linear.hh:232
static const FunctionID DefaultID
Definition Linear.hh:224
typename Linear< Real, N, M >::VectorF VectorF
Definition Linear.hh:208
typename Linear< Real, N, M >::MatrixJF MatrixJF
Definition Linear.hh:209
MatrixJH Jh_x(VectorF const &x, Real t) const override
Definition Linear.hh:359
FunctionJH & Jh_x()
Definition Linear.hh:297
typename Linear< Real, N, M >::MatrixJF MatrixA
Definition Linear.hh:211
std::function< MatrixE(Real)> FunctionE
Definition Linear.hh:215
std::shared_ptr< LinearWrapper< Real, N, M > > Pointer
Definition Linear.hh:207
MatrixE E(Real t) const override
Definition Linear.hh:310
VectorB b(Real t) const override
Definition Linear.hh:330
FunctionA & A()
Definition Linear.hh:279
VectorH h(VectorF const &x, Real t) const override
Definition Linear.hh:341
typename Linear< Real, N, M >::MatrixJF MatrixE
Definition Linear.hh:210
MatrixA A(Real t) const override
Definition Linear.hh:320
typename Linear< Real, N, M >::VectorF VectorB
Definition Linear.hh:212
FunctionJH m_Jh_x
Definition Linear.hh:231
The namespace for the Sandals library.
Definition Sandals.hh:89