MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
solver.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/solver.h,v 1.104 2017/05/29 17:50:43 masarati 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  * Classe che gestisce la soluzione del problema:
38  *
39  * - Inizializza i dati (nodi ed elementi)
40  * - Alloca i vettori degli stati necessari
41  * - Stabililisce il tipo di integratore e quanti passi fare con esso
42  * - determina il passo temporale
43  * - gestisce le operazioni di output
44  *
45  */
46 
47 #ifndef SOLVER_H
48 #define SOLVER_H
49 
50 #include <unistd.h>
51 #include <cfloat>
52 #include <cmath>
53 #include <limits>
54 
55 class Solver;
56 #include "myassert.h"
57 #include "mynewmem.h"
58 #include "except.h"
59 #include "dataman.h"
60 #include "schurdataman.h"
61 #include "schsolman.h"
62 #include <deque>
63 #include "linsol.h"
64 #include "stepsol.h"
65 #include "nonlin.h"
66 #include "linesearch.h"
67 #include "mfree.h"
68 #include "cstring"
69 #include "precond.h"
70 #include "rtsolver.h"
71 #include "TimeStepControl.h"
72 
73 extern "C" int mbdyn_stop_at_end_of_iteration(void);
74 extern "C" int mbdyn_stop_at_end_of_time_step(void);
75 extern "C" void mbdyn_set_stop_at_end_of_iteration(void);
76 extern "C" void mbdyn_set_stop_at_end_of_time_step(void);
77 
78 class Solver : public SolverDiagnostics, protected NonlinearSolverOptions {
79 public:
80  class ErrGeneric : public MBDynErrBase {
81  public:
83  };
84  class ErrMaxIterations : public MBDynErrBase {
85  public:
87  };
89  public:
91  };
92  class EndOfSimulation : public MBDynErrBase {
93  private:
94  int EndCode;
95  public:
98  };
99 
100 protected:
101 #ifdef USE_MULTITHREAD
102  unsigned nThreads;
103 
104  void ThreadPrepare(void);
105 #endif /* USE_MULTITHREAD */
113  std::string sInputFileName;
114  std::string sOutputFileName;
117 
118 public:
119  /* Dati per esecuzione di eigenanalysis */
120 
121  struct EigenAnalysis {
122  bool bAnalysis;
123  enum {
124  EIG_NONE = 0x0U,
125 
129 
131 
133 
135 
137 
138  EIG_SOLVE = 0x100U,
139 
140  EIG_PERMUTE = 0x200U,
141  EIG_SCALE = 0x400U,
143 
144  EIG_USE_LAPACK = 0x1000U,
145  EIG_USE_ARPACK = 0x2000U,
146  EIG_USE_JDQZ = 0x4000U,
148 
150  };
151  unsigned uFlags;
152 
153  // TODO: allow to specify eigenanalysis parameters
154  // for each analysis in the list
155  std::vector<doublereal> Analyses;
156  std::vector<doublereal>::iterator currAnalysis;
157 
160  enum { EIGAN_WIDTH_COMPUTE = -1 };
162  std::string iFNameFormat;
163 
166 
167  // text output precision control
170 
171  // ARPACK specific
172  struct ARPACK {
176  ARPACK(void) : iNEV(0), iNCV(0), dTOL(0.) { NO_OP; };
177  } arpack;
178 
179  // JDQZ specific
180  struct JDQZ {
193 
194  enum Method {
195  GMRES = 1,
197  };
198 
199  JDQZ(void)
200  : eps(std::numeric_limits<doublereal>::epsilon()),
201  method(BICGSTAB), m(30), l(2), mxmv(100),
202  maxstep(1000), lock(1e-9), order(0), testspace(3)
203  { NO_OP; };
204  } jdqz;
205 
207  : bAnalysis(false),
208  uFlags(EIG_NONE),
209  dParam(1.),
211  iFNameWidth(0),
212  iFNameFormat(),
213  dUpperFreq(std::numeric_limits<doublereal>::max()),
214  dLowerFreq(-1.)
215  {
216  currAnalysis = Analyses.end();
217  };
218  };
219 
220 protected:
222 
223  void Eig(bool bNewLine = false);
224 
226 
227  /* Strutture di gestione dei dati */
231  std::deque<MyVectorHandler*> qX; /* queque vett. stati */
232  std::deque<MyVectorHandler*> qXPrime; /* queque vett. stati der. */
233  MyVectorHandler* pX; /* queque vett. stati inc. */
234  MyVectorHandler* pXPrime; /* queque vett. stati d. inc. */
235 
236  /* Dati della simulazione */
237 
242 
243 
246 
250  } eTimeStepLimit;
251 
252  /* Dati dei passi fittizi di trimmaggio iniziale */
255 
256  /* Flags vari */
257  enum AbortAfter {
263  };
265 
266  /* Parametri per il metodo */
275  };
277 
284 
289 
291  /* Type of linear solver */
293 
294  /* Parameters for convergence tests */
297  bool bScale;
299 
300  /* Parametri per solutore nonlineare */
302  bool bKeepJac;
313 
314 /* FOR PARALLEL SOLVERS */
315  bool bParallel;
318  integer iNumLocDofs; /* Dimensioni problema locale */
319  integer iNumIntDofs; /* Dimensioni interfaccia locale */
320  integer* pLocDofs; /* Lista dof locali (stile FORTRAN) */
321  integer* pIntDofs; /* Lista dei dofs di interfaccia */
324 /* end of FOR PARALLEL SOLVERS */
325 
326  /* gestore dei dati */
328  /* Dimensioni del problema; FIXME: serve ancora? */
330 
331  /* il solution manager v*/
334 
335  /* corregge i puntatori per un nuovo passo */
336  inline void Flip(void);
337 
338  /* Lettura dati */
339  void ReadData(MBDynParser& HP);
340 
341  /* Alloca Solman */
342  SolutionManager *const AllocateSolman(integer iNLD, integer iLWS = 0);
343  /* Alloca SchurSolman */
345  /* Alloca Nonlinear Solver */
347  /* Alloca tutti i solman*/
348  void SetupSolmans(integer iStates, bool bCanBeParallel = false);
349 
350  /* Workaround: call this function instead of MaxTimeStep.dGet()
351  * until all postponed drive callers have been instantiated */
353 
354 protected:
355  enum {
359  } eStatus;
360 
362  std::string outputCounterPrefix;
363  std::string outputCounterPostfix;
365 
369  bool bSolConv;
370  bool bOut;
371  long lStep;
372 
373 public:
374  /* costruttore */
376  const std::string& sInputFileName,
377  const std::string& sOutputFileName,
378  unsigned int nThreads,
379  bool bParallel = false);
380 
381  /* distruttore: esegue tutti i distruttori e libera la memoria */
382  virtual ~Solver(void);
383 
384  virtual bool Prepare(void);
385  virtual bool Start(void);
386  virtual bool Advance(void);
387 
388  /* esegue la simulazione */
389  void Run(void);
390 
391  std::ostream & Restart(std::ostream& out, DataManager::eRestart type) const;
392 
393  /* EXPERIMENTAL */
394  /* FIXME: better const'ify? */
395  virtual DataManager *pGetDataManager(void) const {
396  return pDM;
397  };
398  virtual SolutionManager *pGetSolutionManager(void) const {
399  return pSM;
400  };
401  virtual const LinSol& GetLinearSolver(void) const {
402  return CurrLinearSolver;
403  };
404  virtual NonlinearSolver *pGetNonlinearSolver(void) const {
405  return pNLS;
406  };
407  virtual StepIntegrator *pGetStepIntegrator(void) const {
408  // if (pCurrStepIntegrator == NULL)
409  // return pRegularSteps;
411 
412  return pCurrStepIntegrator;
413  };
414  virtual doublereal dGetFinalTime(void) const {
415  return dFinalTime;
416  };
417  virtual doublereal dGetInitialTimeStep(void) const {
418  return dInitialTimeStep;
419  };
420 
421  virtual clock_t GetCPUTime(void) const;
422 
423  virtual void PrintResidual(const VectorHandler& Res, integer iIterCnt) const;
424  virtual void PrintSolution(const VectorHandler& Sol, integer iIterCnt) const;
426 };
427 
428 inline void
430 {
431  /*
432  * switcha i puntatori; in questo modo non e' necessario
433  * copiare i vettori per cambiare passo
434  */
435  qX.push_front(qX.back());
436  qX.pop_back();
437  qXPrime.push_front(qXPrime.back());
438  qXPrime.pop_back();
439 
440  /* copy from pX, pXPrime to qx[0], qxPrime[0] */
441  MyVectorHandler* x = qX[0];
442  MyVectorHandler* xp = qXPrime[0];
443  for (integer i = 1; i <= iNumDofs; i++) {
444  x->PutCoef(i, pX->operator()(i));
445  xp->PutCoef(i, pXPrime->operator()(i));
446  }
447 }
448 
449 /* Solver - end */
450 
451 #endif /* SOLVER_H */
integer iUnkStates
Definition: solver.h:229
MyVectorHandler * pX
Definition: solver.h:233
integer iNumIntDofs
Definition: solver.h:319
virtual SolutionManager * pGetSolutionManager(void) const
Definition: solver.h:398
std::string outputCounterPrefix
Definition: solver.h:362
virtual bool Prepare(void)
Definition: solver.cc:399
ErrGeneric(MBDYN_EXCEPT_ARGS_DECL)
Definition: solver.h:82
struct EigenAnalysis EigAn
Definition: solver.h:221
void mbdyn_set_stop_at_end_of_iteration(void)
Definition: solver.cc:192
std::vector< doublereal > Analyses
Definition: solver.h:155
StepIntegrator * pCurrStepIntegrator
Definition: solver.h:283
bool bParallel
Definition: solver.h:315
bool bOutputCounter
Definition: solver.h:361
integer * pLocDofs
Definition: solver.h:320
bool bScale
Definition: solver.h:297
StepIntegratorType RegularType
Definition: solver.h:276
#define MBDYN_EXCEPT_ARGS_PASSTHRU
Definition: except.h:55
TimeStepFlags
Definition: solver.h:247
doublereal dParam
Definition: solver.h:158
std::vector< doublereal >::iterator currAnalysis
Definition: solver.h:156
doublereal dRefTimeStep
Definition: solver.h:240
virtual void PrintResidual(const VectorHandler &Res, integer iIterCnt) const
Definition: solver.cc:5194
StepIntegrator * pFirstRegularStep
Definition: solver.h:281
doublereal dUpperFreq
Definition: solver.h:164
doublereal dMinTimeStep
Definition: solver.h:111
DriveCaller * pRhoDummy
Definition: solver.h:287
StepIntegratorType
Definition: solver.h:267
std::string sOutputFileName
Definition: solver.h:114
integer iTotIter
Definition: solver.h:364
virtual void CheckTimeStepLimit(doublereal dErr, doublereal dErrDiff) const
Definition: solver.cc:5205
enum Solver::TimeStepFlags eTimeStepLimit
StepIntegrator::StepChange CurrStep
Definition: solver.h:112
integer iDummyStepsNumber
Definition: solver.h:253
doublereal dDummyStepsRatio
Definition: solver.h:254
StepIntegrator * pDerivativeSteps
Definition: solver.h:278
integer iMaxIterations
Definition: solver.h:116
#define MBDYN_EXCEPT_ARGS_DECL
Definition: except.h:43
doublereal dTime
Definition: solver.h:109
integer iNumPreviousVectors
Definition: solver.h:228
integer iStIter
Definition: solver.h:108
#define NO_OP
Definition: myassert.h:74
NonlinearSolver::Type NonlinearSolverType
Definition: solver.h:304
virtual const LinSol & GetLinearSolver(void) const
Definition: solver.h:401
enum Solver::@38 eStatus
integer iIterationsBeforeAssembly
Definition: solver.h:303
ErrMaxIterations(MBDYN_EXCEPT_ARGS_DECL)
Definition: solver.h:86
std::ostream & Restart(std::ostream &out, DataManager::eRestart type) const
Definition: solver.cc:1717
virtual void PutCoef(integer iRow, const doublereal &dCoef)
Definition: vh.h:261
std::deque< MyVectorHandler * > qXPrime
Definition: solver.h:232
Definition: linsol.h:39
std::string sInputFileName
Definition: solver.h:113
doublereal dLowerFreq
Definition: solver.h:165
virtual bool Start(void)
Definition: solver.cc:1202
LinSol CurrLinearSolver
Definition: solver.h:292
void SetupSolmans(integer iStates, bool bCanBeParallel=false)
Definition: solver.cc:5135
struct LineSearchParameters LineSearch
Definition: solver.h:312
AbortAfter
Definition: solver.h:257
void ReadData(MBDynParser &HP)
Definition: solver.cc:1853
virtual clock_t GetCPUTime(void) const
Definition: solver.cc:5188
MatrixFreeSolver::SolverType MFSolverType
Definition: solver.h:305
SolutionManager * pSM
Definition: solver.h:332
NonlinearSolverTest::Type SolTest
Definition: solver.h:296
void Run(void)
Definition: solver.cc:1633
virtual StepIntegrator * pGetStepIntegrator(void) const
Definition: solver.h:407
doublereal dSolTest
Definition: solver.h:368
SimulationDiverged(MBDYN_EXCEPT_ARGS_DECL)
Definition: solver.h:90
#define MBDYN_EXCEPT_ARGS_DECL_NODEF
Definition: except.h:49
DataManager * pDM
Definition: solver.h:327
EndOfSimulation(const int e, MBDYN_EXCEPT_ARGS_DECL_NODEF)
Definition: solver.h:96
struct Solver::EigenAnalysis::JDQZ jdqz
Definition: mbdyn.h:76
NonlinearSolver *const AllocateNonlinearSolver()
Definition: solver.cc:5073
SolutionManager *const AllocateSolman(integer iNLD, integer iLWS=0)
Definition: solver.cc:5001
bool bTrueNewtonRaphson
Definition: solver.h:301
StepIntegrator * pRegularSteps
Definition: solver.h:282
bool bKeepJac
Definition: solver.h:302
doublereal dInitialTimeStep
Definition: solver.h:241
doublereal dCurrTimeStep
Definition: solver.h:107
virtual NonlinearSolver * pGetNonlinearSolver(void) const
Definition: solver.h:404
RTSolverBase * pRTSolver
Definition: solver.h:225
void Flip(void)
Definition: solver.h:429
doublereal dFinalTime
Definition: solver.h:239
void mbdyn_set_stop_at_end_of_time_step(void)
Definition: solver.cc:200
StepIntegrator * pFirstDummyStep
Definition: solver.h:279
Dof * pDofs
Definition: solver.h:322
doublereal dIterertiveTau
Definition: solver.h:311
DriveCaller * pRhoRegular
Definition: solver.h:285
doublereal dIterertiveEtaMax
Definition: solver.h:310
doublereal dMaxResidual
Definition: solver.h:244
#define ASSERT(expression)
Definition: colamd.c:977
StepIntegrator * pDummySteps
Definition: solver.h:280
std::string iFNameFormat
Definition: solver.h:162
virtual bool Advance(void)
Definition: solver.cc:1376
DriveOwner MaxTimeStep
Definition: solver.h:110
Definition: solver.h:78
integer iPrecondSteps
Definition: solver.h:308
int mbdyn_stop_at_end_of_iteration(void)
Definition: solver.cc:172
doublereal dMaxResidualDiff
Definition: solver.h:245
StepIntegratorType DummyType
Definition: solver.h:276
std::deque< MyVectorHandler * > qX
Definition: solver.h:231
doublereal dTotErr
Definition: solver.h:366
SolutionManager *const AllocateSchurSolman(integer iStates)
Definition: solver.cc:5036
NonlinearSolverTest::Type ResTest
Definition: solver.h:295
integer * pIntDofs
Definition: solver.h:321
TimeStepControl * pTSC
Definition: solver.h:106
std::string outputCounterPostfix
Definition: solver.h:363
Preconditioner::PrecondType PcType
Definition: solver.h:307
MyVectorHandler * pXPrime
Definition: solver.h:234
virtual void PrintSolution(const VectorHandler &Sol, integer iIterCnt) const
Definition: solver.cc:5200
integer iNumLocDofs
Definition: solver.h:318
NonlinearSolver * pNLS
Definition: solver.h:333
int mbdyn_stop_at_end_of_time_step(void)
Definition: solver.cc:182
doublereal dInitialTime
Definition: solver.h:238
bool bOut
Definition: solver.h:370
virtual DataManager * pGetDataManager(void) const
Definition: solver.h:395
SchurDataManager * pSDM
Definition: solver.h:316
Solver(MBDynParser &HP, const std::string &sInputFileName, const std::string &sOutputFileName, unsigned int nThreads, bool bParallel=false)
Definition: solver.cc:274
doublereal dGetInitialMaxTimeStep() const
Definition: solver.cc:4990
DriveCaller * pRhoAlgebraicDummy
Definition: solver.h:288
MyVectorHandler Scale
Definition: solver.h:298
AbortAfter eAbortAfter
Definition: solver.h:264
void Eig(bool bNewLine=false)
Definition: solver.cc:4853
double doublereal
Definition: colamd.c:52
bool bSolConv
Definition: solver.h:369
integer iIterativeMaxSteps
Definition: solver.h:309
struct Solver::EigenAnalysis::ARPACK arpack
long int integer
Definition: colamd.c:51
doublereal * pdWorkSpace
Definition: solver.h:230
integer iNumDofs
Definition: solver.h:329
Definition: dofown.h:59
DriveCaller * pRhoAlgebraicRegular
Definition: solver.h:286
doublereal dTest
Definition: solver.h:367
SolutionManager * pLocalSM
Definition: solver.h:323
doublereal dDerivativesCoef
Definition: solver.h:290
virtual doublereal dGetInitialTimeStep(void) const
Definition: solver.h:417
long lStep
Definition: solver.h:371
MBDynParser & HP
Definition: solver.h:115
virtual doublereal dGetFinalTime(void) const
Definition: solver.h:414
doublereal dIterTol
Definition: solver.h:306
virtual ~Solver(void)
Definition: solver.cc:1647
LinSol CurrIntSolver
Definition: solver.h:317