G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsDynamicBase.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include <gsCore/gsLinearAlgebra.h>
20 #include <gsIO/gsOptionList.h>
22 
23 namespace gismo
24 {
25 
33 template <class T>
35 {
36 protected:
37 
38  typedef typename gsStructuralAnalysisOps<T>::Force_t Force_t;
39  typedef typename gsStructuralAnalysisOps<T>::TForce_t TForce_t;
40  typedef typename gsStructuralAnalysisOps<T>::Residual_t Residual_t;
41  typedef typename gsStructuralAnalysisOps<T>::TResidual_t TResidual_t;
42  typedef typename gsStructuralAnalysisOps<T>::Mass_t Mass_t;
43  typedef typename gsStructuralAnalysisOps<T>::TMass_t TMass_t;
44  typedef typename gsStructuralAnalysisOps<T>::Damping_t Damping_t;
45  typedef typename gsStructuralAnalysisOps<T>::TDamping_t TDamping_t;
46  typedef typename gsStructuralAnalysisOps<T>::Stiffness_t Stiffness_t;
47  typedef typename gsStructuralAnalysisOps<T>::Jacobian_t Jacobian_t;
48  typedef typename gsStructuralAnalysisOps<T>::TJacobian_t TJacobian_t;
49 
50 public:
51 
52  virtual ~gsDynamicBase() {};
53 
56  const Mass_t & Mass,
57  const Damping_t & Damping,
58  const Stiffness_t & Stiffness,
59  const Force_t & Force
60  )
61  :
62  m_mass(Mass),
63  m_damping(Damping),
64  m_stiffness(Stiffness),
65  m_force(Force)
66  {
67  m_Tmass = [this]( const T time, gsSparseMatrix<T> & result) -> bool {return m_mass(result);};
68  m_Tdamping = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_damping(x,result);};
69  m_Tjacobian = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_stiffness(result);};
70  m_Tforce = [this]( const T time, gsVector<T> & result) -> bool {return m_force(result);};
71  m_Tresidual = [ ](gsVector<T> const & x, const T time, gsVector<T> & result) -> bool {GISMO_ERROR("time-dependent residual not available");};
72  _init();
73  }
74 
77  const Mass_t & Mass,
78  const Damping_t & Damping,
79  const Stiffness_t & Stiffness,
80  const TForce_t & TForce
81  )
82  :
83  m_mass(Mass),
84  m_damping(Damping),
85  m_stiffness(Stiffness),
86  m_Tforce(TForce)
87  {
88  m_Tmass = [this]( const T time, gsSparseMatrix<T> & result) -> bool {return m_mass(result);};
89  m_Tdamping = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_damping(x,result);};
90  m_Tjacobian = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_stiffness(result);};
91  m_Tresidual = [ ](gsVector<T> const & x, const T time, gsVector<T> & result) -> bool {GISMO_ERROR("time-dependent residual not available");};
92  _init();
93  }
94 
97  const Mass_t & Mass,
98  const Damping_t & Damping,
99  const Jacobian_t & Jacobian,
100  const Residual_t & Residual
101  )
102  :
103  m_mass(Mass),
104  m_damping(Damping),
105  m_jacobian(Jacobian),
106  m_residual(Residual)
107  {
108  m_Tmass = [this]( const T time, gsSparseMatrix<T> & result) -> bool {return m_mass(result);};
109  m_Tdamping = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_damping(x,result);};
110  m_Tjacobian = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_jacobian(x,result);};
111  m_Tforce = [ ]( const T time, gsVector<T> & result) -> bool {GISMO_ERROR("time-dependent force not available");};
112  m_Tresidual = [this](gsVector<T> const & x, const T time, gsVector<T> & result) -> bool {return m_residual(x,result);};
113  _init();
114  }
115 
118  const Mass_t & Mass,
119  const Damping_t & Damping,
120  const Jacobian_t & Jacobian,
121  const TResidual_t & TResidual
122  )
123  :
124  m_mass(Mass),
125  m_damping(Damping),
126  m_jacobian(Jacobian),
127  m_Tresidual(TResidual)
128  {
129  m_Tmass = [this]( const T time, gsSparseMatrix<T> & result) -> bool {return m_mass(result);};
130  m_Tjacobian = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_jacobian(x,result);};
131  m_Tdamping = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_damping(x,result);};
132  _init();
133  }
134 
137  const Mass_t & Mass,
138  const Damping_t & Damping,
139  const TJacobian_t & TJacobian,
140  const TResidual_t & TResidual
141  )
142  :
143  m_mass(Mass),
144  m_damping(Damping),
145  m_Tjacobian(TJacobian),
146  m_Tresidual(TResidual)
147  {
148  m_Tmass = [this]( const T time, gsSparseMatrix<T> & result) -> bool {return m_mass(result);};
149  m_Tdamping = [this](gsVector<T> const & x, const T time, gsSparseMatrix<T> & result) -> bool {return m_damping(x,result);};
150  _init();
151  }
152 
155  const TMass_t & TMass,
156  const TDamping_t & TDamping,
157  const TJacobian_t & TJacobian,
158  const TResidual_t & TResidual
159  )
160  :
161  m_Tmass(TMass),
162  m_Tdamping(TDamping),
163  m_Tjacobian(TJacobian),
164  m_Tresidual(TResidual)
165  {
166  _init();
167  }
168 
169  gsDynamicBase() {_init();}
170 
171 protected:
172  void _init()
173  {
174  m_time = 0;
175  // initialize variables
176  m_numIterations = -1;
177  defaultOptions();
178 
179  m_status = gsStatus::NotStarted;
180  }
181 
182 // General functions
183 public:
184 
185  virtual gsStatus status() { return m_status; }
186 
187  // Returns the number of DoFs
188  // virtual index_t numDofs() {return m_forcing.size();}
189 
190  // Returns the current time step
191  virtual T getTimeStep() const {return m_options.getReal("DT"); }
192 
194  virtual gsStatus step(T dt)
195  {
196  gsStatus status = this->_step(m_time,dt,m_U,m_V,m_A);
197  m_time += dt;
198  return status;
199  }
200 
201  virtual gsStatus step()
202  {
203  return this->step(m_options.getReal("DT"));
204  }
205 
206  virtual gsStatus step(const T t, const T dt, gsVector<T> & U, gsVector<T> & V, gsVector<T> & A) const
207  {
208  return _step(t,dt,U,V,A);
209  }
210 
212  virtual void setTimeStep(T dt)
213  {
214  m_options.setReal("DT",dt);
215  }
216 
217  // Output
219  virtual bool converged() const {return m_status==gsStatus::Success;}
220 
222  virtual index_t numIterations() const { return m_numIterations;}
223 
224  virtual const gsVector<T> & displacements() const {return this->solutionU();}
225  virtual const gsVector<T> & velocities() const {return this->solutionV();}
226  virtual const gsVector<T> & accelerations() const {return this->solutionA();}
227 
228  virtual const gsVector<T> & solutionU() const {return m_U;}
229  virtual const gsVector<T> & solutionV() const {return m_V;}
230  virtual const gsVector<T> & solutionA() const {return m_A;}
231 
232  virtual const T & time() {return m_time;}
233 
234  virtual void setU(const gsVector<T> & U) {m_U = U;}
235  virtual void setV(const gsVector<T> & V) {m_V = V;}
236  virtual void setA(const gsVector<T> & A) {m_A = A;}
237 
238  virtual void setDisplacements(const gsVector<T> & U) {this->setU(U);}
239  virtual void setVelocities(const gsVector<T> & V) {this->setV(V);}
240  virtual void setAccelerations(const gsVector<T> & A) {this->setA(A);}
241 
243  virtual gsOptionList & options() {return m_options;};
244 
246  virtual void setOptions(gsOptionList options) {m_options.update(options,gsOptionList::addIfUnknown); };
247 
249  virtual const void options_into(gsOptionList options) {options = m_options;};
250 
252  virtual index_t numDofs() {return m_numDofs; }
253 // ------------------------------------------------------------------------------------------------------------
254 // ---------------------------------------Computations---------------------------------------------------------
255 // ------------------------------------------------------------------------------------------------------------
256 protected:
258  virtual void defaultOptions();
259 
261  virtual void _computeForce(const T time, gsVector<T> & F) const
262  {
263  if (!m_Tforce(time,F))
264  throw 2;
265  }
266 
268  virtual void _computeResidual(const gsVector<T> & U, const T time, gsVector<T> & R) const
269  {
270  if (!m_Tresidual(U,time,R))
271  throw 2;
272  }
273 
275  virtual void _computeMass(const T time, gsSparseMatrix<T> & M) const
276  {
277  if (!m_Tmass(time,M))
278  throw 2;
279  }
280 
282  virtual void _computeMassInverse(const gsSparseMatrix<T> & M, gsSparseMatrix<T> & Minv) const
283  {
284  if ((m_mass==nullptr) || (m_massInv.rows()==0 || m_massInv.cols()==0)) // then mass is time-dependent or the mass inverse is not stored, compute it
285  {
286  gsSparseMatrix<T> eye(M.rows(), M.cols());
287  eye.setIdentity();
288  gsSparseSolver<>::LU solver(M);
289  gsMatrix<T> MinvI = solver.solve(eye);
290  m_massInv = Minv = MinvI.sparseView();
291  }
292  else
293  Minv = m_massInv;
294  }
295 
297  virtual void _computeDamping(const gsVector<T> & U, const T time, gsSparseMatrix<T> & C) const
298  {
299  if (!m_Tdamping(U,time,C))
300  throw 2;
301  }
302 
304  virtual void _computeJacobian(const gsVector<T> & U, const T time, gsSparseMatrix<T> & K) const
305  {
306  if (!m_Tjacobian(U,time,K))
307  throw 2;
308  }
309 
310 // Purely virtual functions
311 protected:
313  virtual gsStatus _step(const T t, const T dt, gsVector<T> & U, gsVector<T> & V, gsVector<T> & A) const = 0;
314 
315 protected:
316 
317  // Number of degrees of freedom
318  index_t m_numDofs;
319 
320  Mass_t m_mass;
321  mutable gsSparseMatrix<T> m_massInv;
322  TMass_t m_Tmass;
323 
324  Damping_t m_damping;
325  TDamping_t m_Tdamping;
326 
327  Stiffness_t m_stiffness;
328 
329  Jacobian_t m_jacobian;
330  TJacobian_t m_Tjacobian;
331 
332  Force_t m_force;
333  TForce_t m_Tforce;
334 
335  Residual_t m_residual;
336  TResidual_t m_Tresidual;
337 
338  mutable typename gsSparseSolver<T>::uPtr m_solver; // Cholesky by default
339 
340 protected:
341 
342  mutable gsOptionList m_options;
343 
344 protected:
345 
346 
349 
350  // /// Maximum number of Arc Length iterations allowed
351  // index_t m_maxIterations;
352 
353  // /// Number of desired iterations
354  // index_t m_desiredIterations;
355 
356  // /// Time step
357  // T m_dt;
358 
361 
362  // /// Tolerance value to decide convergence
363  // T m_tolerance;
364 
365  // /// Tolerance value to decide convergence - Force criterion
366  // T m_toleranceF;
367 
368  // /// Tolerance value to decide convergence - Displacement criterion
369  // T m_toleranceU;
370 
371  // bool m_verbose;
372  bool m_initialized;
373 
374  // bool m_quasiNewton;
375  // index_t m_quasiNewtonInterval;
376 
377  gsStatus m_status;
378 
379 protected:
380 
381  // /// Current update
382  // gsVector<T> m_DeltaU;
383  // gsVector<T> m_DeltaV;
384  // gsVector<T> m_DeltaA;
385 
386  // /// Iterative update
387  // gsVector<T> m_deltaU;
388  // gsVector<T> m_deltaV;
389  // gsVector<T> m_deltaA;
390 
391  // Previous update
392  gsVector<T> m_DeltaUold;
393  gsVector<T> m_DeltaVold;
394  gsVector<T> m_DeltaAold;
395 
397  gsVector<T> m_U, m_Uold;
398  gsVector<T> m_V, m_Vold;
399  gsVector<T> m_A, m_Aold;
400 
402  T m_t, m_tprev;
403 };
404 
405 
406 } // namespace gismo
407 
408 #ifndef GISMO_BUILD_LIB
409 #include GISMO_HPP_HEADER(gsDynamicBase.hpp)
410 #endif
std::function< bool(gsVector< T > const &, const T, gsSparseMatrix< T > &) > TJacobian_t
Jacobian.
Definition: gsStructuralAnalysisTypes.h:85
virtual void _computeForce(const T time, gsVector< T > &F) const
Compute the residual.
Definition: gsDynamicBase.h:261
virtual index_t numDofs()
Return the number of degrees of freedom.
Definition: gsDynamicBase.h:252
virtual void _computeMassInverse(const gsSparseMatrix< T > &M, gsSparseMatrix< T > &Minv) const
Compute the mass matrix.
Definition: gsDynamicBase.h:282
virtual gsStatus _step(const T t, const T dt, gsVector< T > &U, gsVector< T > &V, gsVector< T > &A) const =0
Initialize the ALM.
std::function< bool(gsVector< T > const &, const T, gsVector< T > &)> TResidual_t
Time-dependent Residual Fint(t)-Fext(t)
Definition: gsStructuralAnalysisTypes.h:69
virtual gsStatus step(T dt)
Perform one arc-length step.
Definition: gsDynamicBase.h:194
Performs the arc length method to solve a nonlinear system of equations.
Definition: gsDynamicBase.h:34
virtual void _computeMass(const T time, gsSparseMatrix< T > &M) const
Compute the mass matrix.
Definition: gsDynamicBase.h:275
gsVector< T > m_U
Displacement vector (present, at previously converged point)
Definition: gsDynamicBase.h:397
virtual void setTimeStep(T dt)
Set time step to dt.
Definition: gsDynamicBase.h:212
#define index_t
Definition: gsConfig.h:32
gsStatus
Definition: gsStructuralAnalysisTypes.h:20
std::function< bool(gsVector< T > const &, gsVector< T > &)> Residual_t
Residual, Fint-Fext.
Definition: gsStructuralAnalysisTypes.h:65
std::function< bool(gsSparseMatrix< T > &) > Mass_t
Mass matrix.
Definition: gsStructuralAnalysisTypes.h:72
virtual bool converged() const
True if the Arc Length method converged.
Definition: gsDynamicBase.h:219
void setReal(const std::string &label, const Real &value)
Sets an existing option label to be equal to value.
Definition: gsOptionList.cpp:166
std::function< bool(gsVector< T > const &, gsSparseMatrix< T > &) > Jacobian_t
Jacobian.
Definition: gsStructuralAnalysisTypes.h:83
std::function< bool(gsVector< T > const &, gsSparseMatrix< T > &) > Damping_t
Damping matrix.
Definition: gsStructuralAnalysisTypes.h:76
virtual void defaultOptions()
Set default options.
Definition: gsDynamicBase.hpp:20
gsDynamicBase(const Mass_t &Mass, const Damping_t &Damping, const Stiffness_t &Stiffness, const TForce_t &TForce)
Constructor.
Definition: gsDynamicBase.h:76
Provides a list of labeled parameters/options that can be set and accessed easily.
virtual void _computeDamping(const gsVector< T > &U, const T time, gsSparseMatrix< T > &C) const
Compute the damping matrix.
Definition: gsDynamicBase.h:297
std::function< bool(gsVector< T > &)> Force_t
Force.
Definition: gsStructuralAnalysisTypes.h:60
std::function< bool(const T, gsSparseMatrix< T > &) > TMass_t
Time-dependent mass matrix.
Definition: gsStructuralAnalysisTypes.h:74
std::function< bool(gsSparseMatrix< T > &) > Stiffness_t
Stiffness matrix.
Definition: gsStructuralAnalysisTypes.h:81
gsDynamicBase(const Mass_t &Mass, const Damping_t &Damping, const Jacobian_t &Jacobian, const TResidual_t &TResidual)
Constructor.
Definition: gsDynamicBase.h:117
void update(const gsOptionList &other, updateType type=ignoreIfUnknown)
Updates the object using the data from other.
Definition: gsOptionList.cpp:253
Provides a status object and typedefs.
virtual void setOptions(gsOptionList options)
Set the options to options.
Definition: gsDynamicBase.h:246
gsDynamicBase(const Mass_t &Mass, const Damping_t &Damping, const Jacobian_t &Jacobian, const Residual_t &Residual)
Constructor.
Definition: gsDynamicBase.h:96
ALM has not started yet.
std::function< bool(const T, gsVector< T > &)> TForce_t
Time-dependent force.
Definition: gsStructuralAnalysisTypes.h:62
virtual void _computeJacobian(const gsVector< T > &U, const T time, gsSparseMatrix< T > &K) const
Compute the Jacobian matrix.
Definition: gsDynamicBase.h:304
T m_t
Time.
Definition: gsDynamicBase.h:402
index_t m_numIterations
Number of iterations performed.
Definition: gsDynamicBase.h:348
#define GISMO_ERROR(message)
Definition: gsDebug.h:118
This is the main header file that collects wrappers of Eigen for linear algebra.
gsDynamicBase(const Mass_t &Mass, const Damping_t &Damping, const Stiffness_t &Stiffness, const Force_t &Force)
Constructor.
Definition: gsDynamicBase.h:55
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
virtual index_t numIterations() const
Returns the number of Newton iterations performed.
Definition: gsDynamicBase.h:222
virtual gsOptionList & options()
Access the options.
Definition: gsDynamicBase.h:243
gsDynamicBase(const Mass_t &Mass, const Damping_t &Damping, const TJacobian_t &TJacobian, const TResidual_t &TResidual)
Constructor.
Definition: gsDynamicBase.h:136
std::function< bool(gsVector< T > const &, const T, gsSparseMatrix< T > &) > TDamping_t
Time-dependent Damping matrix.
Definition: gsStructuralAnalysisTypes.h:78
virtual const void options_into(gsOptionList options)
Return the options into options.
Definition: gsDynamicBase.h:249
gsDynamicBase(const TMass_t &TMass, const TDamping_t &TDamping, const TJacobian_t &TJacobian, const TResidual_t &TResidual)
Constructor.
Definition: gsDynamicBase.h:154
T m_time
Time.
Definition: gsDynamicBase.h:360
Real getReal(const std::string &label) const
Reads value for option label from options.
Definition: gsOptionList.cpp:44
virtual void _computeResidual(const gsVector< T > &U, const T time, gsVector< T > &R) const
Compute the residual.
Definition: gsDynamicBase.h:268