G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsNewtonIterator.h
Go to the documentation of this file.
1 
15 #pragma once
16 
18 
19 
20 namespace gismo
21 {
22 
30 template <class T>
32 {
33 public:
34 
38  const gsMultiPatch<T> & initialSolution)
39  : m_assembler(assembler),
40  m_curSolution(initialSolution),
41  m_numIterations(0),
42  m_maxIterations(100),
43  m_tolerance(1e-12),
44  m_converged(false)
45  {
46 
47  }
48 
49  gsNewtonIterator(gsAssembler<T> & assembler)
50  : m_assembler(assembler),
51  m_numIterations(0),
52  m_maxIterations(100),
53  m_tolerance(1e-12),
54  m_converged(false)
55  {
56 
57  }
58 
59 
60 public:
61 
64  void solve();
65 
68  void firstIteration();
69 
72  void nextIteration();
73 
74 public:
75 
77  const gsMultiPatch<T> & solution() const { return m_curSolution; }
78 
80  bool converged() const {return m_converged;}
81 
84 
86  T tolerance() const {return m_tolerance;}
87 
89  T residue() const {return m_residue;}
90 
92  void setMaxIterations(index_t nIter) {m_maxIterations = nIter;}
93 
95  void setTolerance(T tol) {m_tolerance = tol;}
96 
97 protected:
98 
99  virtual void solveLinearProblem(gsMatrix<T> &updateVector);
100 
101  virtual void solveLinearProblem(const gsMultiPatch<T> & currentSol, gsMatrix<T> &updateVector);
102 
103  virtual T getResidue() {return m_assembler.rhs().norm();}
104 protected:
105 
109 
112 
115 
117  //gsSparseSolver<>::LU m_solver;
118  //typename gsSparseSolver<T>::BiCGSTABDiagonal m_solver;
119  //typename gsSparseSolver<>::CGDiagonal m_solver;
120  gsSparseSolver<>::LU m_solver;
121 
122 protected:
123 
126 
129 
132 
133 protected:
134 
137 
140 
143 
144 };
145 
146 
147 } // namespace gismo
148 
149 
150 namespace gismo
151 {
152 
153 template <class T>
154 void gsNewtonIterator<T>::solveLinearProblem(gsMatrix<T>& updateVector)
155 {
156  // Construct the linear system
157  m_assembler.assemble();
158 
159  // gsDebugVar( m_assembler.matrix().toDense() );
160  // gsDebugVar( m_assembler.rhs().transpose() );
161 
162  // Compute the newton update
163  m_solver.compute( m_assembler.matrix() );
164  updateVector = m_solver.solve( m_assembler.rhs() );
165 
166  // gsDebugVar(updateVector);
167 }
168 
169 template <class T>
170 void gsNewtonIterator<T>::solveLinearProblem(const gsMultiPatch<T> & currentSol, gsMatrix<T>& updateVector)
171 {
172  // Construct linear system for next iteration
173  m_assembler.assemble(currentSol);
174 
175  // gsDebugVar( m_assembler.matrix().toDense() );
176  // gsDebugVar( m_assembler.rhs().transpose() );
177 
178  // Compute the newton update
179  m_solver.compute( m_assembler.matrix() );
180  updateVector = m_solver.solve( m_assembler.rhs() );
181 
182  // gsDebugVar(updateVector);
183 }
184 
185 
186 template <class T>
188 {
189  firstIteration();
190 
191  const T initResidue = m_residue;
192  const T initUpdate = m_updnorm;
193 
194  // ----- Iterations start -----
195  for (m_numIterations = 1; m_numIterations < m_maxIterations; ++m_numIterations)
196  {
197  gsDebug << "Newton iteration " << m_numIterations
198  << " residue " << math::abs(m_residue)
199  << ".\n";
200  nextIteration();
201 
202  // termination criteria
203  if ( math::abs(m_updnorm / initUpdate) < m_tolerance ||
204  math::abs(m_residue / initResidue) < m_tolerance )
205  {
206  m_converged = true;
207  break;
208  }
209  }
210 }
211 
212 
213 template <class T>
215 {
216  // ----- First iteration -----
217  m_converged = false;
218 
219  // Solve
220  solveLinearProblem(m_updateVector);
221 
222  // Construct initial solution
223  m_assembler.constructSolution(m_updateVector, m_curSolution);
224 
225  // Homogenize Dirichlet dofs (values are now copied in m_curSolution)
226  m_assembler.homogenizeFixedDofs(-1);
227 
228  // Compute initial residue
229  m_residue = getResidue();
230  m_updnorm = m_updateVector .norm();
231 
232  gsDebug<<"Iteration: "<< 0
233  <<", residue: "<< m_residue
234  <<", update norm: "<< m_updnorm
235  <<"\n";
236 }
237 
238 template <class T>
240 {
241  // Solve the linaer system of the current iteration
242  solveLinearProblem(m_curSolution, m_updateVector);
243 
244  // Update the deformed solution
245  m_assembler.updateSolution(m_updateVector, m_curSolution);
246 
247  // Compute residue
248  m_residue = getResidue();
249  m_updnorm = m_updateVector.norm();
250 
251  gsDebug<<"Iteration: "<< m_numIterations
252  <<", residue: "<< m_residue
253  <<", update norm: "<< m_updnorm
254  <<"\n";
255 }
256 
257 
258 
259 } // namespace gismo
260 
gsAssembler< T > & m_assembler
gsAssemblerBase object to generate the linear system for each iteration
Definition: gsNewtonIterator.h:108
T m_residue
Final error.
Definition: gsNewtonIterator.h:139
Provides generic assembler routines.
T m_updnorm
Norm of the current Newton update vector.
Definition: gsNewtonIterator.h:142
Performs Newton iterations to solve a nonlinear system of PDEs.
Definition: gsNewtonIterator.h:31
#define gsDebug
Definition: gsDebug.h:61
#define index_t
Definition: gsConfig.h:32
gsMatrix< T > m_updateVector
Solution of the linear system in each iteration.
Definition: gsNewtonIterator.h:114
T m_tolerance
Tolerance value to decide convergence.
Definition: gsNewtonIterator.h:131
void setTolerance(T tol)
Set the tolerance for convergence.
Definition: gsNewtonIterator.h:95
bool m_converged
Convergence result.
Definition: gsNewtonIterator.h:136
index_t m_maxIterations
Maximum number of Newton iterations allowed.
Definition: gsNewtonIterator.h:128
void nextIteration()
Solves linear system in each iteration based on last solution and computes residual.
Definition: gsNewtonIterator.h:239
index_t m_numIterations
Number of Newton iterations performed.
Definition: gsNewtonIterator.h:125
Container class for a set of geometry patches and their topology, that is, the interface connections ...
Definition: gsMultiPatch.h:33
bool converged() const
Tells whether the Newton method converged.
Definition: gsNewtonIterator.h:80
const gsMultiPatch< T > & solution() const
Returns the latest configuration.
Definition: gsNewtonIterator.h:77
T residue() const
Returns the error after solving the nonlinear system.
Definition: gsNewtonIterator.h:89
void solve()
Applies Newton method and Performs Newton iterations until convergence or maximum iterations...
Definition: gsNewtonIterator.h:187
gsSparseSolver::LU m_solver
Linear solver employed.
Definition: gsNewtonIterator.h:120
EIGEN_STRONG_INLINE abs_expr< E > abs(const E &u)
Absolute value.
Definition: gsExpressions.h:4486
gsMultiPatch< T > m_curSolution
The latest/current solution.
Definition: gsNewtonIterator.h:111
The assembler class provides generic routines for volume and boundary integrals that are used for for...
Definition: gsAssembler.h:265
void setMaxIterations(index_t nIter)
Set the maximum number of Newton iterations allowed.
Definition: gsNewtonIterator.h:92
T tolerance() const
Returns the tolerance value used.
Definition: gsNewtonIterator.h:86
gsNewtonIterator(gsAssembler< T > &assembler, const gsMultiPatch< T > &initialSolution)
Definition: gsNewtonIterator.h:37
index_t numIterations() const
Returns the number of Newton iterations performed.
Definition: gsNewtonIterator.h:83
void firstIteration()
Solves linear system obtained using linear elasticity in first step and computes residual.
Definition: gsNewtonIterator.h:214