MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
nonlin.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/nonlin.h,v 1.55 2017/02/08 11:28:37 morandini Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32  /*
33  *
34  * Copyright (C) 2003-2017
35  * Giuseppe Quaranta <quaranta@aero.polimi.it>
36  *
37  * classi che implementano la risoluzione del sistema nonlineare
38  */
39 
40 #ifndef NONLIN_H
41 #define NONLIN_H
42 
43 #include "solverdiagnostics.h"
44 #include "external.h"
45 #include "nonlinpb.h"
46 #include "solman.h"
47 
48 #include <cfloat>
49 #include <vector>
50 
51 /*
52  * Directory tree rationale:
53  *
54  * everything is based on NonlinearSolver; direct methods,
55  * e.g. NewtonRaphsonSolver, inherit directly from NonlinearSolver
56  * matrix-free methods are based on MatrixFreeSolver, which
57  * inherits from NonlinearSolver, and may require Preconditioner.
58  * The available matrix-free methods are BiCGStab and Gmres,
59  * which requires UpHessMatrix.
60  *
61  * - nonlin.h NonlinearSolver
62  * |--- nr.h NewtonRaphsonSolver
63  * |- precond.h Preconditioner
64  * | |- precond_.h FullJacobianPr
65  * |- mfree.h MatrixFreeSolver
66  * |--- bicg.h BiCGStab
67  * |--- gmres.h Gmres, UpHessMatrix
68  */
69 
70 /* Needed for callback declaration; defined in <mbdyn/base/solver.h> */
71 class Solver;
72 class InverseSolver;
73 
75 public:
76  enum Type {
80 
82  };
83 
84  virtual ~NonlinearSolverTest(void);
85 
86  /* loops over the vector Vec */
87  virtual doublereal MakeTest(Solver *pS, const integer& Size,
88  const VectorHandler& Vec, bool bResidual = false,
89  doublereal dScaleAlgEqu = 1.,
90  doublereal* pTestDiff=0);
91 
92  /* tests a single value, and returns the measure accordingly */
93  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
94  const integer& iIndex, doublereal dCoef) const = 0;
95 
96  /* merges results of multiple tests */
97  virtual void TestMerge(doublereal& dResCurr,
98  const doublereal& dResNew) const = 0;
99 
100  /* post-processes the test */
101  virtual doublereal TestPost(const doublereal& dRes) const;
102 
103  /* scales a single value */
104  virtual const doublereal& dScaleCoef(const integer& iIndex) const;
105 };
106 
108 public:
109  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
110  const integer& iIndex, doublereal dCoef) const;
111  virtual void TestMerge(doublereal& dResCurr,
112  const doublereal& dResNew) const;
113  virtual doublereal MakeTest(Solver *pS, integer Size,
114  const VectorHandler& Vec, bool bResidual = false,
115  doublereal* pTestDiff=0);
116 };
117 
119 public:
120  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
121  const integer& iIndex, doublereal dCoef) const;
122  virtual void TestMerge(doublereal& dResCurr,
123  const doublereal& dResNew) const;
124  virtual doublereal TestPost(const doublereal& dRes) const;
125 };
126 
128 public:
129  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
130  const integer& iIndex, doublereal dCoef) const;
131  virtual void TestMerge(doublereal& dResCurr,
132  const doublereal& dResNew) const;
133 };
134 
136 protected:
138 
139 public:
140  NonlinearSolverTestScale(const VectorHandler* pScl = 0);
141  virtual ~NonlinearSolverTestScale(void);
142  virtual void SetScale(const VectorHandler* pScl);
143  virtual const doublereal& dScaleCoef(const integer& iIndex) const;
144 };
145 
147  virtual public NonlinearSolverTestNorm {
148 public:
149  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
150  const integer& iIndex, doublereal dCoef) const;
151  virtual void TestMerge(doublereal& dResCurr,
152  const doublereal& dResNew) const;
153  virtual const doublereal& dScaleCoef(const integer& iIndex) const;
154 };
155 
157  virtual public NonlinearSolverTestMinMax {
158 public:
159  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
160  const integer& iIndex, doublereal dCoef) const;
161  virtual void TestMerge(doublereal& dResCurr,
162  const doublereal& dResNew) const;
163  virtual const doublereal& dScaleCoef(const integer& iIndex) const;
164 };
165 
167 protected:
171 
172  bool bIsValid(const integer& iIndex) const;
173 
174 public:
175  NonlinearSolverTestRange(NonlinearSolverTest *pTest, integer iFirstIndex = -1, integer iLastIndex = -1);
176  virtual ~NonlinearSolverTestRange(void);
177 
178 #if 0
179  virtual doublereal MakeTest(Solver *pS, const integer& Size,
180  const VectorHandler& Vec, bool bResidual = false);
181 #endif
182 
183  virtual void TestOne(doublereal& dRes, const VectorHandler& Vec,
184  const integer& iIndex, doublereal dCoef) const;
185 
186  virtual void TestMerge(doublereal& dResCurr, const doublereal& dResNew) const;
187 
188  virtual doublereal TestPost(const doublereal& dRes) const;
189 
190  virtual const doublereal& dScaleCoef(const integer& iIndex) const;
191 
192  void SetRange(integer iFirstIndex, integer iLastIndex);
193 };
194 
196 {
198 
199  enum ScaleFlags {
202  } eScaleFlags;
203 
205 
209 };
210 
212 {
213 public:
215  public:
217  };
218  class NoConvergence : public MBDynErrBase {
219  public:
221  };
223  public:
225  };
227  public:
229  };
231  public:
233  };
234  class ErrGeneric : public MBDynErrBase {
235  public:
237  };
238 
239  enum Type {
240  UNKNOWN = -1,
241 
246 
248  };
249 
250 protected:
253 
254 #ifdef USE_MPI
255  bool bParallel;
256 #endif /* USE_MPI */
259 #ifdef USE_EXTERNAL
260  External::ExtMessage ExtStepType;
261 #endif /* USE_EXTERNAL */
262 
263  virtual bool MakeSolTest(Solver* pS,
264  const VectorHandler& Vec,
265  const doublereal& dTol,
266  doublereal& dTest);
267 
268  enum CPUTimeType {
273  };
274 
275  doublereal dGetCondMax()const { return dMaxCond; }
276  doublereal dGetCondMin()const { return dMinCond; }
278  inline doublereal dGetTimeCPU(CPUTimeType eType) const;
279 
280  inline void AddCond(doublereal dCond);
281  inline void AddTimeCPU(doublereal dTime, CPUTimeType eType);
282 
283 private:
289 
290 public:
292 
293  virtual void SetTest(NonlinearSolverTest *pr, NonlinearSolverTest *ps);
294 
295  virtual ~NonlinearSolver(void);
296 
297  virtual bool MakeResTest(Solver* pS,
298  const NonlinearProblem *pNLP,
299  const VectorHandler& Vec,
300  const doublereal& dTol,
301  doublereal& dTest,
302  doublereal& dTestDiff);
303 
304  virtual void Solve(const NonlinearProblem *pNLP,
305  Solver *pS,
306  const integer iMaxIter,
307  const doublereal& Tol,
308  integer& iIterCnt,
309  doublereal& dErr,
310  const doublereal& SolTol,
311  doublereal& dSolErr) = 0;
312 
313  virtual integer TotalAssembledJacobian(void);
314 
316  return pResTest;
317  }
318 
320  return pSolTest;
321  }
322 #ifdef USE_EXTERNAL
323  void SetExternal(const External::ExtMessage Ty);
324 
325 protected:
326  void SendExternal(void);
327 #endif /* USE_EXTERNAL */
328 };
329 
330 inline void
332 {
333  iNumCond++;
334  dSumCond += dCond;
335 
336  if (dCond > dMaxCond) {
337  dMaxCond = dCond;
338  }
339 
340  if (dCond < dMinCond) {
341  dMinCond = dCond;
342  }
343 }
344 
345 inline doublereal
347 {
348  ASSERT(eType >= 0);
349  ASSERT(eType < CPU_LAST_TYPE);
350 
351  return dTimeCPU[eType];
352 }
353 
354 inline void
356 {
357  ASSERT(eType >= 0);
358  ASSERT(eType < CPU_LAST_TYPE);
359 
360  dTimeCPU[eType] += dTime;
361 }
362 
363 #endif /* NONLIN_H */
364 
doublereal dTimeCPU[CPU_LAST_TYPE]
Definition: nonlin.h:288
void AddCond(doublereal dCond)
Definition: nonlin.h:331
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const
Definition: nonlin.cc:303
doublereal dMaxCond
Definition: nonlin.h:285
NonlinearSolverTestRange(NonlinearSolverTest *pTest, integer iFirstIndex=-1, integer iLastIndex=-1)
Definition: nonlin.cc:353
TimeStepLimitExceeded(MBDYN_EXCEPT_ARGS_DECL)
Definition: nonlin.h:224
doublereal dGetCondMax() const
Definition: nonlin.h:275
enum NonlinearSolverOptions::ScaleFlags eScaleFlags
virtual void SetScale(const VectorHandler *pScl)
Definition: nonlin.cc:278
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const
Definition: nonlin.cc:320
integer TotJac
Definition: nonlin.h:252
virtual ~NonlinearSolverTest(void)
Definition: nonlin.cc:57
NoConvergence(MBDYN_EXCEPT_ARGS_DECL)
Definition: nonlin.h:220
#define MBDYN_EXCEPT_ARGS_PASSTHRU
Definition: except.h:55
doublereal dScaleAlgebraic
Definition: nonlin.h:204
virtual doublereal MakeTest(Solver *pS, const integer &Size, const VectorHandler &Vec, bool bResidual=false, doublereal dScaleAlgEqu=1., doublereal *pTestDiff=0)
Definition: nonlin.cc:63
virtual NonlinearSolverTest * pGetSolTest(void)
Definition: nonlin.h:319
virtual bool MakeSolTest(Solver *pS, const VectorHandler &Vec, const doublereal &dTol, doublereal &dTest)
Definition: nonlin.cc:497
virtual bool MakeResTest(Solver *pS, const NonlinearProblem *pNLP, const VectorHandler &Vec, const doublereal &dTol, doublereal &dTest, doublereal &dTestDiff)
Definition: nonlin.cc:474
doublereal dMinCond
Definition: nonlin.h:286
NonlinearSolverTest * m_pTest
Definition: nonlin.h:170
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const
Definition: nonlin.cc:240
#define MBDYN_EXCEPT_ARGS_DECL
Definition: except.h:43
doublereal dGetCondAvg() const
Definition: nonlin.h:277
integer Size
Definition: nonlin.h:251
virtual doublereal MakeTest(Solver *pS, integer Size, const VectorHandler &Vec, bool bResidual=false, doublereal *pTestDiff=0)
Definition: nonlin.cc:177
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const
Definition: nonlin.cc:375
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const
Definition: nonlin.cc:384
doublereal dGetCondMin() const
Definition: nonlin.h:276
doublereal dGetTimeCPU(CPUTimeType eType) const
Definition: nonlin.h:346
virtual ~NonlinearSolverTestRange(void)
Definition: nonlin.cc:359
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const =0
bool bIsValid(const integer &iIndex) const
Definition: nonlin.cc:346
ConvergenceOnSolution(MBDYN_EXCEPT_ARGS_DECL)
Definition: nonlin.h:232
virtual integer TotalAssembledJacobian(void)
Definition: nonlin.cc:468
virtual ~NonlinearSolver(void)
Definition: nonlin.cc:461
void SetRange(integer iFirstIndex, integer iLastIndex)
Definition: nonlin.cc:406
virtual const doublereal & dScaleCoef(const integer &iIndex) const
Definition: nonlin.cc:338
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const
Definition: nonlin.cc:198
virtual const doublereal & dScaleCoef(const integer &iIndex) const
Definition: nonlin.cc:310
NonlinearSolverTest * pSolTest
Definition: nonlin.h:258
ErrGeneric(MBDYN_EXCEPT_ARGS_DECL)
Definition: nonlin.h:236
void AddTimeCPU(doublereal dTime, CPUTimeType eType)
Definition: nonlin.h:355
virtual void SetTest(NonlinearSolverTest *pr, NonlinearSolverTest *ps)
Definition: nonlin.cc:455
virtual doublereal TestPost(const doublereal &dRes) const
Definition: nonlin.cc:225
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const
Definition: nonlin.cc:191
NonlinearSolver(const NonlinearSolverOptions &options)
Definition: nonlin.cc:434
virtual ~NonlinearSolverTestScale(void)
Definition: nonlin.cc:272
#define ASSERT(expression)
Definition: colamd.c:977
virtual const doublereal & dScaleCoef(const integer &iIndex) const
Definition: nonlin.cc:396
MaxResidualExceeded(MBDYN_EXCEPT_ARGS_DECL)
Definition: nonlin.h:228
NonlinearSolverOptions(bool bHonorJacRequest=false, enum ScaleFlags eScaleFlags=SCALE_ALGEBRAIC_EQUATIONS_NO, doublereal dScaleAlgebraic=1.)
Definition: nonlin.cc:422
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const
Definition: nonlin.cc:294
ErrSimulationDiverged(MBDYN_EXCEPT_ARGS_DECL)
Definition: nonlin.h:216
struct option options[]
Definition: ann_in.c:46
Definition: solver.h:78
virtual const doublereal & dScaleCoef(const integer &iIndex) const
Definition: nonlin.cc:167
integer iNumCond
Definition: nonlin.h:284
doublereal dSumCond
Definition: nonlin.h:287
virtual doublereal TestPost(const doublereal &dRes) const
Definition: nonlin.cc:390
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const =0
NonlinearSolverTestScale(const VectorHandler *pScl=0)
Definition: nonlin.cc:266
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const
Definition: nonlin.cc:331
virtual void Solve(const NonlinearProblem *pNLP, Solver *pS, const integer iMaxIter, const doublereal &Tol, integer &iIterCnt, doublereal &dErr, const doublereal &SolTol, doublereal &dSolErr)=0
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51
virtual NonlinearSolverTest * pGetResTest(void)
Definition: nonlin.h:315
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const
Definition: nonlin.cc:218
NonlinearSolverTest * pResTest
Definition: nonlin.h:257
virtual const doublereal & dScaleCoef(const integer &iIndex) const
Definition: nonlin.cc:284
virtual void TestOne(doublereal &dRes, const VectorHandler &Vec, const integer &iIndex, doublereal dCoef) const
Definition: nonlin.cc:209
const VectorHandler * pScale
Definition: nonlin.h:137
virtual doublereal TestPost(const doublereal &dRes) const
Definition: nonlin.cc:155
virtual void TestMerge(doublereal &dResCurr, const doublereal &dResNew) const
Definition: nonlin.cc:251