G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsStaticBase.h
Go to the documentation of this file.
1
14#include <typeinfo>
15
17#ifdef gsSpectra_ENABLED
18#include <gsSpectra/gsSpectra.h>
19#endif
20#include <gsIO/gsOptionList.h>
22
23#pragma once
24
25
26namespace gismo
27{
28
36template <class T>
38{
39protected:
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
46public:
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
159protected:
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 GISMO_UNUSED(shift);
212 gsEigen::SelfAdjointEigenSolver<gsMatrix<T>> es2(jacMat);
213 m_stabilityVec = es2.eigenvalues();
214#endif
215
216 m_indicator = m_stabilityVec.colwise().minCoeff()[0]; // This is required since D does not necessarily have one column.
217 return true;
218 }
219
221 virtual bool _computeStability (const gsSparseMatrix<T> & jacMat, T shift)
222 {
223 bool success = false;
224 if (m_stabilityMethod == stabmethod::Determinant)
225 success = _computeStabilityDet(jacMat);
226 else if (m_stabilityMethod == stabmethod::Eigenvalue)
227 success = _computeStabilityEig(jacMat, shift);
228 else
229 gsInfo<<"bifurcation method unknown!";
230
231 if (success)
232 m_indicator = m_stabilityVec.colwise().minCoeff()[0]; // This is required since D does not necessarily have one column
233 return success;
234 }
235
236protected:
237 mutable gsOptionList m_options;
238
239 T m_L, m_DeltaL, m_deltaL;
240 gsVector<T> m_U, m_DeltaU, m_deltaU;
241 gsVector<T> m_R;
242 T m_residual, m_residualIni, m_residualOld;
243
244 index_t m_numIterations;
245 index_t m_maxIterations;
246 bool m_start;
247 index_t m_headstart;
248
249 index_t m_verbose;
250
251 index_t m_dofs;
252
253 T m_tolF,m_tolU;
254
255 T m_indicator;
256
257 gsVector<T> m_stabilityVec;
258
259 mutable typename gsSparseSolver<T>::uPtr m_solver; // Cholesky by default
260
261 index_t m_stabilityMethod;
262
263 struct stabmethod
264 {
265 enum type
266 {
267 Determinant = 0,
268 Eigenvalue = 1,
269 };
270 };
271
272 gsStatus m_status;
273
274};
275
276
277} // namespace gismo
278
279
280#ifndef GISMO_BUILD_LIB
281#include GISMO_HPP_HEADER(gsStaticBase.hpp)
282#endif
Class which holds a list of parameters/options, and provides easy access to them.
Definition gsOptionList.h:33
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
void update(const gsOptionList &other, updateType type=ignoreIfUnknown)
Updates the object using the data from other.
Definition gsOptionList.cpp:253
const index_t & getInt(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:37
Real getReal(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:44
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
std::string askString(const std::string &label, const std::string &value="") const
Reads value for option label from options.
Definition gsOptionList.cpp:106
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
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
Abstract class for solvers. The solver interface is base on 3 methods: -compute set the system matrix...
Definition gsSparseSolver.h:67
Shifted Eigenvalue solver for real symmetric matrices.
Definition gsSpectra.h:362
Base class for static solvers.
Definition gsStaticBase.h:38
virtual void initOutput()
Initialize output.
Definition gsStaticBase.h:61
virtual gsStatus status() const
Returns the status.
Definition gsStaticBase.h:129
virtual gsVector< T > update() const
Access the update.
Definition gsStaticBase.h:98
virtual void setDisplacement(const gsVector< T > &displacement)
Set the displacement.
Definition gsStaticBase.h:101
virtual index_t iterations() const
Returns the number of iterations.
Definition gsStaticBase.h:123
virtual bool _computeStabilityDet(const gsSparseMatrix< T > &jacMat)
Computes the stability vector using the determinant of the Jacobian.
Definition gsStaticBase.h:161
virtual bool converged() const
Returns whether the solver converged or not.
Definition gsStaticBase.h:126
virtual gsStatus solve()=0
Solve.
virtual void setLoad(const T L)
Set the load.
Definition gsStaticBase.h:108
virtual bool _computeStability(const gsSparseMatrix< T > &jacMat, T shift)
Computes the stability of the Jacobian, optionally applying a shift (if provided)
Definition gsStaticBase.h:221
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
virtual void initialize()
See gsStaticBase.
Definition gsStaticBase.h:54
virtual void reset()
Reset the stored solution.
Definition gsStaticBase.h:135
virtual void getOptions()
Apply the options.
Definition gsStaticBase.h:78
virtual T indicator(const gsSparseMatrix< T > &jacMat, T shift=-1e-2)
Returns the stability indicator.
Definition gsStaticBase.h:146
virtual void stepOutput(index_t)
Stepwise output.
Definition gsStaticBase.h:63
virtual index_t numDofs()
Returns the number of DoFs of the system.
Definition gsStaticBase.h:132
virtual gsVector< T > solution() const
Access the solution.
Definition gsStaticBase.h:95
virtual gsVector< T > stabilityVec(const gsSparseMatrix< T > &jacMat, T shift=-1e-2)
Returns the stability vector.
Definition gsStaticBase.h:153
virtual void setSolution(const gsVector< T > &displacement, const T L)
Set the displacement and the load.
Definition gsStaticBase.h:111
virtual gsOptionList options() const
Get options.
Definition gsStaticBase.h:92
virtual void setOptions(gsOptionList &options)
Set the options from options.
Definition gsStaticBase.h:89
virtual void defaultOptions()
Get default options.
Definition gsStaticBase.h:66
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition gsVector.h:37
#define index_t
Definition gsConfig.h:32
#define gsWarn
Definition gsDebug.h:50
#define GISMO_UNUSED(x)
Definition gsDebug.h:112
#define gsInfo
Definition gsDebug.h:43
This is the main header file that collects wrappers of Eigen for linear algebra.
Provides a list of labeled parameters/options that can be set and accessed easily.
Header file for using Spectra extension.
Provides a status object and typedefs.
The G+Smo namespace, containing all definitions for the library.
gsStatus
Definition gsStructuralAnalysisTypes.h:21
@ Success
Successful.
std::function< bool(gsVector< T > const &, gsVector< T > const &, gsSparseMatrix< T > &) > dJacobian_t
Jacobian with solution update as argument.
Definition gsStructuralAnalysisTypes.h:90
std::function< bool(gsVector< T > const &, const T, gsVector< T > &)> ALResidual_t
Arc-Length Residual, Fint-lambda*Fext.
Definition gsStructuralAnalysisTypes.h:70
std::function< bool(gsVector< T > const &, gsSparseMatrix< T > &) > Jacobian_t
Jacobian.
Definition gsStructuralAnalysisTypes.h:86
std::function< bool(gsVector< T > const &, gsVector< T > &)> Residual_t
Residual, Fint-Fext.
Definition gsStructuralAnalysisTypes.h:68