G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsStaticBase.h
Go to the documentation of this file.
1 
14 #include <typeinfo>
15 
16 #include <gsCore/gsLinearAlgebra.h>
17 #ifdef gsSpectra_ENABLED
18 #include <gsSpectra/gsSpectra.h>
19 #endif
20 #include <gsIO/gsOptionList.h>
22 
23 #pragma once
24 
25 
26 namespace gismo
27 {
28 
36 template <class T>
38 {
39 protected:
40 
41  typedef typename gsStructuralAnalysisOps<T>::Residual_t Residual_t;
42  typedef typename gsStructuralAnalysisOps<T>::ALResidual_t ALResidual_t;
43  typedef typename gsStructuralAnalysisOps<T>::Jacobian_t Jacobian_t;
44  typedef typename gsStructuralAnalysisOps<T>::dJacobian_t dJacobian_t;
45 
46 public:
47 
48  virtual ~gsStaticBase() {};
49 
51  virtual gsStatus solve() = 0;
52 
54  virtual void initialize()
55  {
56  this->reset();
57  this->getOptions();
58  }
59 
61  virtual void initOutput() {};
63  virtual void stepOutput(index_t k) {};
64 
66  virtual void defaultOptions()
67  {
68  m_options.addReal("tol","Relative Tolerance",1e-6);
69  m_options.addReal("tolF","Residual relative tolerance",-1);
70  m_options.addReal("tolU","Solution step relative tolerance",-1);
71  m_options.addInt("maxIt","Maximum number of iterations",25);
72  m_options.addInt("verbose","Verbose output",0);
73  m_options.addInt ("BifurcationMethod","Bifurcation Identification based on: 0: Determinant; 1: Eigenvalue",stabmethod::Eigenvalue);
74  m_options.addString("Solver","Sparse linear solver", "SimplicialLDLT");
75  }
76 
78  virtual void getOptions()
79  {
80  m_tolF = m_options.getReal("tolF")!=-1 ? m_options.getReal("tolF") : m_options.getReal("tol");
81  m_tolU = m_options.getReal("tolU")!=-1 ? m_options.getReal("tolU") : m_options.getReal("tol");
82  m_maxIterations = m_options.getInt("maxIt");
83  m_verbose = m_options.getInt("verbose");
84  m_stabilityMethod = m_options.getInt ("BifurcationMethod");
85  m_solver = gsSparseSolver<T>::get( m_options.askString("Solver","SimplicialLDLT") );
86  }
87 
89  virtual void setOptions(gsOptionList & options) {m_options.update(options,gsOptionList::addIfUnknown); }
90 
92  virtual gsOptionList options() const {return m_options;}
93 
95  virtual gsVector<T> solution() const {return m_U;}
96 
98  virtual gsVector<T> update() const {return m_DeltaU;}
99 
101  virtual void setDisplacement(const gsVector<T> & displacement)
102  {
103  m_start = true;
104  m_U = displacement;
105  }
106 
108  virtual void setLoad(const T L) { m_L = L;}
109 
111  virtual void setSolution(const gsVector<T> & displacement, const T L)
112  {
113  this->setDisplacement(displacement);
114  this->setLoad(L);
115  }
116  virtual void setUpdate(const gsVector<T> & update)
117  {
118  m_headstart = true;
119  m_DeltaU = update;
120  }
121 
123  virtual index_t iterations() const { return m_numIterations; }
124 
126  virtual bool converged() const { return m_status==gsStatus::Success; }
127 
129  virtual gsStatus status() const { return m_status; }
130 
132  virtual index_t numDofs() { return m_dofs; }
133 
135  virtual void reset()
136  {
137  m_U.setZero(m_dofs);
138  m_DeltaU.setZero(m_dofs);
139  m_deltaU.setZero(m_dofs);
140  m_R.setZero(m_dofs);
141  m_L = m_DeltaL = m_deltaL = 0.0;
142  m_headstart = false;
143  }
144 
146  virtual T indicator(const gsSparseMatrix<T> & jacMat, T shift = -1e-2)
147  {
148  _computeStability(jacMat, shift);
149  return m_indicator;
150  }
151 
153  virtual gsVector<T> stabilityVec(const gsSparseMatrix<T> & jacMat, T shift = -1e-2)
154  {
155  _computeStability(jacMat, shift);
156  return m_stabilityVec;
157  }
158 
159 protected:
161  virtual bool _computeStabilityDet(const gsSparseMatrix<T> & jacMat)
162  {
163  m_solver->compute(jacMat);
164  // If 1: matrix is not SPD
165  if (m_solver->info()!=gsEigen::ComputationInfo::Success)
166  {
167  gsInfo<<"Solver error with code "<<m_solver->info()<<". See Eigen documentation on ComputationInfo \n"
168  <<gsEigen::ComputationInfo::Success<<": Success"<<"\n"
169  <<gsEigen::ComputationInfo::NumericalIssue<<": NumericalIssue"<<"\n"
170  <<gsEigen::ComputationInfo::NoConvergence<<": NoConvergence"<<"\n"
171  <<gsEigen::ComputationInfo::InvalidInput<<": InvalidInput"<<"\n";
172  return false;
173  }
174 
175  if ( auto * s = dynamic_cast<typename gsSparseSolver<T>::SimplicialLDLT*>(m_solver.get()) )
176  m_stabilityVec = s->vectorD();
177  return true;
178  }
179 
181  virtual bool _computeStabilityEig(const gsSparseMatrix<T> & jacMat, T shift)
182  {
183 #ifdef gsSpectra_ENABLED
184  index_t number = std::min(static_cast<index_t>(std::floor(jacMat.cols()/5.)),10);
185  /*
186  // Without shift!
187  // This one can sometimes not converge, because spectra is better at finding large values.
188  gsSpectraSymSolver<gsSparseMatrix<T>> es(jacMat,number,5*number);
189  es.init();
190  es.compute(Spectra::SortRule::SmallestAlge,1000,1e-6,Spectra::SortRule::SmallestAlge);
191  GISMO_ASSERT(es.info()==Spectra::CompInfo::Successful,"Spectra did not converge!"); // Reason for not converging can be due to the value of ncv (last input in the class member), which is too low.
192  */
193 
194  // With shift!
195  // This one converges easier. However, a shift must be provided!
196  gsSpectraSymShiftSolver<gsSparseMatrix<T>> es(jacMat,number,5*number,shift);
197  es.init();
198  es.compute(Spectra::SortRule::LargestAlge,1000,1e-6,Spectra::SortRule::SmallestAlge);
199  if (es.info()!=Spectra::CompInfo::Successful)
200  {
201  gsWarn<<"Spectra did not converge!\n";
202  return false;
203  }
204 
205  // if (es.info()==Spectra::CompInfo::NotComputed)
206  // if (es.info()==Spectra::CompInfo::NotConverging)
207  // if (es.info()==Spectra::CompInfo::NumericalIssue)
208  // gsEigen::SelfAdjointEigenSolver< gsMatrix<T> > es(jacMat);
209  m_stabilityVec = es.eigenvalues();
210 #else
211  gsEigen::SelfAdjointEigenSolver<gsMatrix<T>> es2(jacMat);
212  m_stabilityVec = es2.eigenvalues();
213 #endif
214 
215  m_indicator = m_stabilityVec.colwise().minCoeff()[0]; // This is required since D does not necessarily have one column.
216  return true;
217  }
218 
220  virtual bool _computeStability (const gsSparseMatrix<T> & jacMat, T shift)
221  {
222  bool success = false;
223  if (m_stabilityMethod == stabmethod::Determinant)
224  success = _computeStabilityDet(jacMat);
225  else if (m_stabilityMethod == stabmethod::Eigenvalue)
226  success = _computeStabilityEig(jacMat, shift);
227  else
228  gsInfo<<"bifurcation method unknown!";
229 
230  if (success)
231  m_indicator = m_stabilityVec.colwise().minCoeff()[0]; // This is required since D does not necessarily have one column
232  return success;
233  }
234 
235 protected:
236  mutable gsOptionList m_options;
237 
238  T m_L, m_DeltaL, m_deltaL;
239  gsVector<T> m_U, m_DeltaU, m_deltaU;
240  gsVector<T> m_R;
241  T m_residual, m_residualIni, m_residualOld;
242 
243  index_t m_numIterations;
244  index_t m_maxIterations;
245  bool m_start;
246  index_t m_headstart;
247 
248  index_t m_verbose;
249 
250  index_t m_dofs;
251 
252  T m_tolF,m_tolU;
253 
254  T m_indicator;
255 
256  gsVector<T> m_stabilityVec;
257 
258  mutable typename gsSparseSolver<T>::uPtr m_solver; // Cholesky by default
259 
260  index_t m_stabilityMethod;
261 
262  struct stabmethod
263  {
264  enum type
265  {
266  Determinant = 0,
267  Eigenvalue = 1,
268  };
269  };
270 
271  gsStatus m_status;
272 
273 };
274 
275 
276 } // namespace gismo
277 
278 
279 #ifndef GISMO_BUILD_LIB
280 #include GISMO_HPP_HEADER(gsStaticBase.hpp)
281 #endif
Shifted Eigenvalue solver for real symmetric matrices.
Definition: gsSpectra.h:359
void addString(const std::string &label, const std::string &desc, const std::string &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:190
virtual gsVector< T > solution() const
Access the solution.
Definition: gsStaticBase.h:95
std::string askString(const std::string &label, const std::string &value="") const
Reads value for option label from options.
Definition: gsOptionList.cpp:106
virtual void initialize()
See gsStaticBase.
Definition: gsStaticBase.h:54
#define index_t
Definition: gsConfig.h:32
gsStatus
Definition: gsStructuralAnalysisTypes.h:20
virtual void defaultOptions()
Get default options.
Definition: gsStaticBase.h:66
const index_t & getInt(const std::string &label) const
Reads value for option label from options.
Definition: gsOptionList.cpp:37
virtual void stepOutput(index_t k)
Stepwise output.
Definition: gsStaticBase.h:63
std::function< bool(gsVector< T > const &, gsVector< T > &)> Residual_t
Residual, Fint-Fext.
Definition: gsStructuralAnalysisTypes.h:65
virtual T indicator(const gsSparseMatrix< T > &jacMat, T shift=-1e-2)
Returns the stability indicator.
Definition: gsStaticBase.h:146
virtual void reset()
Reset the stored solution.
Definition: gsStaticBase.h:135
std::function< bool(gsVector< T > const &, gsSparseMatrix< T > &) > Jacobian_t
Jacobian.
Definition: gsStructuralAnalysisTypes.h:83
virtual bool _computeStabilityDet(const gsSparseMatrix< T > &jacMat)
Computes the stability vector using the determinant of the Jacobian.
Definition: gsStaticBase.h:161
void addInt(const std::string &label, const std::string &desc, const index_t &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:201
Provides a list of labeled parameters/options that can be set and accessed easily.
#define gsWarn
Definition: gsDebug.h:50
std::function< bool(gsVector< T > const &, const T, gsVector< T > &)> ALResidual_t
Arc-Length Residual, Fint-lambda*Fext.
Definition: gsStructuralAnalysisTypes.h:67
virtual void setOptions(gsOptionList &options)
Set the options from options.
Definition: gsStaticBase.h:89
virtual bool converged() const
Returns whether the solver converged or not.
Definition: gsStaticBase.h:126
#define gsInfo
Definition: gsDebug.h:43
virtual index_t iterations() const
Returns the number of iterations.
Definition: gsStaticBase.h:123
virtual void setLoad(const T L)
Set the load.
Definition: gsStaticBase.h:108
virtual gsStatus solve()=0
Solve.
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.
Header file for using Spectra extension.
virtual index_t numDofs()
Returns the number of DoFs of the system.
Definition: gsStaticBase.h:132
virtual void setDisplacement(const gsVector< T > &displacement)
Set the displacement.
Definition: gsStaticBase.h:101
std::function< bool(gsVector< T > const &, gsVector< T > const &, gsSparseMatrix< T > &) > dJacobian_t
Jacobian with solution update as argument.
Definition: gsStructuralAnalysisTypes.h:87
virtual bool _computeStabilityEig(const gsSparseMatrix< T > &jacMat, T shift)
Computes the stability vector using the eigenvalues of the Jacobian, optionally applying a shift...
Definition: gsStaticBase.h:181
void addReal(const std::string &label, const std::string &desc, const Real &value)
Adds a option named label, with description desc and value value.
Definition: gsOptionList.cpp:211
virtual gsVector< T > update() const
Access the update.
Definition: gsStaticBase.h:98
Abstract class for solvers. The solver interface is base on 3 methods: -compute set the system matrix...
Definition: gsSparseSolver.h:66
virtual void setSolution(const gsVector< T > &displacement, const T L)
Set the displacement and the load.
Definition: gsStaticBase.h:111
This is the main header file that collects wrappers of Eigen for linear algebra.
virtual gsVector< T > stabilityVec(const gsSparseMatrix< T > &jacMat, T shift=-1e-2)
Returns the stability vector.
Definition: gsStaticBase.h:153
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
virtual gsOptionList options() const
Get options.
Definition: gsStaticBase.h:92
virtual bool _computeStability(const gsSparseMatrix< T > &jacMat, T shift)
Computes the stability of the Jacobian, optionally applying a shift (if provided) ...
Definition: gsStaticBase.h:220
virtual void initOutput()
Initialize output.
Definition: gsStaticBase.h:61
Real getReal(const std::string &label) const
Reads value for option label from options.
Definition: gsOptionList.cpp:44
virtual void getOptions()
Apply the options.
Definition: gsStaticBase.h:78
virtual gsStatus status() const
Returns the status.
Definition: gsStaticBase.h:129
Base class for static solvers.
Definition: gsStaticBase.h:37