MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
NewtonRaphsonSolver Class Reference

#include <nr.h>

Inheritance diagram for NewtonRaphsonSolver:
Collaboration diagram for NewtonRaphsonSolver:

Public Member Functions

 NewtonRaphsonSolver (const bool bTNR, const bool bKJ, const integer IterBfAss, const NonlinearSolverOptions &options)
 
 ~NewtonRaphsonSolver (void)
 
void Solve (const NonlinearProblem *pNLP, Solver *pS, const integer iMaxIter, const doublereal &Tol, integer &iIterCnt, doublereal &dErr, const doublereal &SolTol, doublereal &dSolErr)
 
- Public Member Functions inherited from NonlinearSolver
 NonlinearSolver (const NonlinearSolverOptions &options)
 
virtual void SetTest (NonlinearSolverTest *pr, NonlinearSolverTest *ps)
 
virtual ~NonlinearSolver (void)
 
virtual bool MakeResTest (Solver *pS, const NonlinearProblem *pNLP, const VectorHandler &Vec, const doublereal &dTol, doublereal &dTest, doublereal &dTestDiff)
 
virtual integer TotalAssembledJacobian (void)
 
virtual NonlinearSolverTestpGetResTest (void)
 
virtual NonlinearSolverTestpGetSolTest (void)
 
- Public Member Functions inherited from SolverDiagnostics
 SolverDiagnostics (unsigned OF=OUTPUT_DEFAULT, DriveCaller *pOM=0)
 
virtual ~SolverDiagnostics (void)
 
void SetNoOutput (void)
 
void SetOutputMeter (DriveCaller *pOM)
 
void SetOutputDriveHandler (const DriveHandler *pDH)
 
void SetOutputFlags (unsigned OF)
 
void AddOutputFlags (unsigned OF)
 
void DelOutputFlags (unsigned OF)
 
MatrixHandler::Norm_t GetCondMatNorm (void) const
 
bool outputMeter (void) const
 
bool outputIters (void) const
 
bool outputRes (void) const
 
bool outputSol (void) const
 
bool outputJac (void) const
 
bool outputStep (void) const
 
bool outputBailout (void) const
 
bool outputCounter (void) const
 
bool outputMatrixConditionNumber (void) const
 
bool outputSolverConditionNumber (void) const
 
bool outputSolverConditionStat (void) const
 
bool outputCPUTime (void) const
 
bool outputMsg (void) const
 

Private Attributes

VectorHandlerpRes
 
VectorHandlerpSol
 
bool bTrueNewtonRaphson
 
integer IterationBeforeAssembly
 
bool bKeepJac
 
integer iPerformedIterations
 
const NonlinearProblempPrevNLP
 

Additional Inherited Members

- Public Types inherited from NonlinearSolver
enum  Type {
  UNKNOWN = -1, NEWTONRAPHSON, MATRIXFREE, LINESEARCH,
  DEFAULT = NEWTONRAPHSON, LASTSOLVERTYPE
}
 
- Protected Types inherited from NonlinearSolver
enum  CPUTimeType { CPU_RESIDUAL, CPU_JACOBIAN, CPU_LINEAR_SOLVER, CPU_LAST_TYPE }
 
- Protected Types inherited from SolverDiagnostics
enum  {
  OUTPUT_NONE = 0x0000, OUTPUT_ITERS = 0x0001, OUTPUT_RES = 0x0002, OUTPUT_SOL = 0x0004,
  OUTPUT_JAC = 0x0008, OUTPUT_BAILOUT = 0x0010, OUTPUT_MSG = 0x0020, OUTPUT_COUNTER = 0x0040,
  OUTPUT_MAT_COND_NUM_1 = 0x0080, OUTPUT_MAT_COND_NUM_INF = 0x0100, OUTPUT_SOLVER_COND_NUM = 0x0200, OUTPUT_SOLVER_COND_STAT = 0x400,
  OUTPUT_CPU_TIME = 0x800, OUTPUT_MAT_COND_NUM = OUTPUT_MAT_COND_NUM_1 | OUTPUT_MAT_COND_NUM_INF, OUTPUT_DEFAULT = OUTPUT_MSG, OUTPUT_STEP = (OUTPUT_ITERS | OUTPUT_RES | OUTPUT_SOL | OUTPUT_JAC | OUTPUT_MAT_COND_NUM | OUTPUT_SOLVER_COND_NUM),
  OUTPUT_MASK = 0x07FF
}
 
- Protected Types inherited from NonlinearSolverOptions
enum  ScaleFlags { SCALE_ALGEBRAIC_EQUATIONS_NO = 0, SCALE_ALGEBRAIC_EQUATIONS_YES = 1 }
 
- Protected Member Functions inherited from NonlinearSolver
virtual bool MakeSolTest (Solver *pS, const VectorHandler &Vec, const doublereal &dTol, doublereal &dTest)
 
doublereal dGetCondMax () const
 
doublereal dGetCondMin () const
 
doublereal dGetCondAvg () const
 
doublereal dGetTimeCPU (CPUTimeType eType) const
 
void AddCond (doublereal dCond)
 
void AddTimeCPU (doublereal dTime, CPUTimeType eType)
 
- Protected Member Functions inherited from NonlinearSolverOptions
 NonlinearSolverOptions (bool bHonorJacRequest=false, enum ScaleFlags eScaleFlags=SCALE_ALGEBRAIC_EQUATIONS_NO, doublereal dScaleAlgebraic=1.)
 
- Protected Attributes inherited from NonlinearSolver
integer Size
 
integer TotJac
 
NonlinearSolverTestpResTest
 
NonlinearSolverTestpSolTest
 
- Protected Attributes inherited from SolverDiagnostics
unsigned OutputFlags
 
DriveCallerpOutputMeter
 
- Protected Attributes inherited from NonlinearSolverOptions
bool bHonorJacRequest
 
enum
NonlinearSolverOptions::ScaleFlags 
eScaleFlags
 
doublereal dScaleAlgebraic
 

Detailed Description

Definition at line 46 of file nr.h.

Constructor & Destructor Documentation

NewtonRaphsonSolver::NewtonRaphsonSolver ( const bool  bTNR,
const bool  bKJ,
const integer  IterBfAss,
const NonlinearSolverOptions options 
)

Definition at line 58 of file nr.cc.

References NO_OP.

62 : NonlinearSolver(options), pRes(NULL),
63 pSol(NULL),
64 bTrueNewtonRaphson(bTNR),
65 IterationBeforeAssembly(IterBfAss),
66 bKeepJac(bKJ),
68 pPrevNLP(0)
69 {
70  NO_OP;
71 }
#define NO_OP
Definition: myassert.h:74
bool bKeepJac
Definition: nr.h:52
VectorHandler * pRes
Definition: nr.h:48
integer iPerformedIterations
Definition: nr.h:53
const NonlinearProblem * pPrevNLP
Definition: nr.h:54
integer IterationBeforeAssembly
Definition: nr.h:51
NonlinearSolver(const NonlinearSolverOptions &options)
Definition: nonlin.cc:434
bool bTrueNewtonRaphson
Definition: nr.h:50
VectorHandler * pSol
Definition: nr.h:49
NewtonRaphsonSolver::~NewtonRaphsonSolver ( void  )

Definition at line 73 of file nr.cc.

References NO_OP.

74 {
75  NO_OP;
76 }
#define NO_OP
Definition: myassert.h:74

Member Function Documentation

void NewtonRaphsonSolver::Solve ( const NonlinearProblem pNLP,
Solver pS,
const integer  iMaxIter,
const doublereal Tol,
integer iIterCnt,
doublereal dErr,
const doublereal SolTol,
doublereal dSolErr 
)
virtual

Implements NonlinearSolver.

Definition at line 79 of file nr.cc.

References NonlinearSolver::AddCond(), NonlinearSolver::AddTimeCPU(), ASSERT, SolutionManager::bGetConditionNumber(), NonlinearSolverOptions::bHonorJacRequest, bKeepJac, bTrueNewtonRaphson, Solver::CheckTimeStepLimit(), MatrixHandler::ConditionNumber(), NonlinearSolver::CPU_JACOBIAN, NonlinearSolver::CPU_LINEAR_SOLVER, NonlinearSolver::CPU_RESIDUAL, NonlinearSolver::dGetCondAvg(), NonlinearSolver::dGetCondMax(), NonlinearSolver::dGetCondMin(), NonlinearSolver::dGetTimeCPU(), SolverDiagnostics::GetCondMatNorm(), VectorHandler::iGetSize(), iPerformedIterations, IterationBeforeAssembly, NonlinearProblem::Jacobian(), NonlinearSolver::MakeResTest(), NonlinearSolver::MakeSolTest(), SolutionManager::MatrInitialize(), SolutionManager::MatrReset(), mbdyn_clock_time(), MBDYN_EXCEPT_ARGS, mbdyn_stop_at_end_of_iteration(), SolverDiagnostics::outputBailout(), SolverDiagnostics::outputCPUTime(), SolverDiagnostics::outputIters(), SolverDiagnostics::outputJac(), SolverDiagnostics::outputMatrixConditionNumber(), SolverDiagnostics::outputRes(), SolverDiagnostics::outputSol(), SolverDiagnostics::outputSolverConditionNumber(), SolverDiagnostics::outputSolverConditionStat(), Solver::pGetSolutionManager(), SolutionManager::pMatHdl(), pPrevNLP, pRes, SolutionManager::pResHdl(), Solver::PrintResidual(), Solver::PrintSolution(), pSol, SolutionManager::pSolHdl(), VectorHandler::Reset(), NonlinearProblem::Residual(), NonlinearSolver::Size, SolutionManager::Solve(), NonlinearSolver::TotJac, and NonlinearProblem::Update().

87 {
88  ASSERT(pS != NULL);
90 
91  iIterCnt = 0;
92  if ((!bKeepJac) || (pNLP != pPrevNLP)) {
94  }
95  pPrevNLP = pNLP;
96  dSolErr = 0.;
97 
98  doublereal dOldErr;
99  doublereal dErrFactor = 1.;
100  doublereal dErrDiff = 0.;
101  bool bJacBuilt = false;
102  doublereal dStartTimeCPU, dEndTimeCPU, dJacobianCPU, dResidualCPU = 0., dLinSolveCPU;
103 
104  while (true) {
105  if (outputCPUTime()) {
106  dStartTimeCPU = mbdyn_clock_time();
107  }
108 
109  pRes = pSM->pResHdl();
110  pSol = pSM->pSolHdl();
111  Size = pRes->iGetSize();
112 
113 #ifdef USE_EXTERNAL
114  SendExternal();
115 #endif /* USE_EXTERNAL */
116 
117  pRes->Reset();
118  bool forceJacobian(false);
119  try {
120  pNLP->Residual(pRes);
121  }
123  if (bHonorJacRequest) {
124  forceJacobian = true;
125  }
126  }
127 
128  /* FIXME: if Tol == 0., no convergence on residual
129  * is required, so we could simply don't compute
130  * the test; I'm leaving it in place so it appears
131  * in the output (maybe we could conditionally disable
132  * it?) */
133 
134  bool bTest = MakeResTest(pS, pNLP, *pRes, Tol, dErr, dErrDiff);
135 
136  if (outputCPUTime()) {
137  dEndTimeCPU = mbdyn_clock_time();
138  dResidualCPU += dEndTimeCPU - dStartTimeCPU;
139  AddTimeCPU(dResidualCPU, CPU_RESIDUAL);
140  }
141 
142  if (outputRes()) {
143  pS->PrintResidual(*pRes, iIterCnt);
144  }
145 
146  if (iIterCnt > 0) {
147  dErrFactor *= dErr/dOldErr;
148  }
149  dOldErr = dErr;
150 
151 #ifdef USE_MPI
152  if (!bParallel || MBDynComm.Get_rank() == 0)
153 #endif /* USE_MPI */
154  {
156  if (outputIters()) {
157  silent_cout("\tIteration(" << iIterCnt << ") " << dErr);
158 
159  if (bJacBuilt) {
160  silent_cout(" J");
161  }
162  }
163 
165  silent_cout(" cond=");
166  doublereal dCond;
167  if (pSM->bGetConditionNumber(dCond)) {
168  silent_cout(dCond);
170  AddCond(dCond);
171  silent_cout(" " << dGetCondMin() << " " << dGetCondMax() << " " << dGetCondAvg());
172  }
173  } else {
174  silent_cout("NA");
175  }
176  }
177 
178  if (outputCPUTime()) {
179  silent_cout(" CPU:" << dResidualCPU << "/" << dGetTimeCPU(CPU_RESIDUAL)
180  << "+" << dJacobianCPU << "/" << dGetTimeCPU(CPU_JACOBIAN)
181  << "+" << dLinSolveCPU << "/" << dGetTimeCPU(CPU_LINEAR_SOLVER));
182  }
183 
184  silent_cout(std::endl);
185  }
186  }
187 
188  pS->CheckTimeStepLimit(dErr, dErrDiff);
189 
190  if (bTest) {
191  return;
192  }
193 
194  if (iIterCnt >= std::abs(iMaxIter)) {
195  if (iMaxIter < 0 && dErrFactor < 1.) {
196  return;
197  }
198  if (outputBailout()) {
199  pS->PrintResidual(*pRes, iIterCnt);
200  }
201  throw NoConvergence(MBDYN_EXCEPT_ARGS);
202  }
203 
204  iIterCnt++;
205  bJacBuilt = false;
206 
207  if (outputCPUTime()) {
208  dStartTimeCPU = mbdyn_clock_time();
209  }
210 
213  || forceJacobian)
214  {
215  pSM->MatrReset();
216 rebuild_matrix:;
217  try {
218  pNLP->Jacobian(pSM->pMatHdl());
220  silent_cout("NewtonRaphsonSolver: "
221  "rebuilding matrix..."
222  << std::endl);
223 
224  /* need to rebuild the matrix... */
225  pSM->MatrInitialize();
226  /* FIXME: could loop forever! */
227  goto rebuild_matrix;
228 
229  } catch (...) {
230  throw;
231  }
232 
233  TotJac++;
234  bJacBuilt = true;
235  }
236 
237  if (outputCPUTime()) {
238  dEndTimeCPU = mbdyn_clock_time();
239  dJacobianCPU = dEndTimeCPU - dStartTimeCPU;
240  AddTimeCPU(dJacobianCPU, CPU_JACOBIAN);
241  }
242 
244 
245 #ifdef USE_MPI
246  if (!bParallel || MBDynComm.Get_rank() == 0)
247 #endif /* USE_MPI */
248  {
249  if (outputJac()) {
250  if (bJacBuilt) {
251  silent_cout("Jacobian:" << std::endl
252  << *(pSM->pMatHdl()));
253  } else {
254  silent_cout("Jacobian: unchanged" << std::endl);
255  }
256  }
257 
258  if (bJacBuilt && outputMatrixConditionNumber()) {
259  silent_cout("cond=" << pSM->pMatHdl()->ConditionNumber(GetCondMatNorm()) << std::endl);
260  }
261  }
262 
263  if (outputCPUTime()) {
264  dStartTimeCPU = mbdyn_clock_time();
265  }
266 
267  pSM->Solve();
268 
269  if (outputCPUTime()) {
270  dEndTimeCPU = mbdyn_clock_time();
271  dLinSolveCPU = dEndTimeCPU - dStartTimeCPU;
272  AddTimeCPU(dLinSolveCPU, CPU_LINEAR_SOLVER);
273  }
274 
275  if (outputSol()) {
276  pS->PrintSolution(*pSol, iIterCnt);
277  }
278 
279  if (outputCPUTime()) {
280  dStartTimeCPU = mbdyn_clock_time();
281  }
282 
283  pNLP->Update(pSol);
284 
285  bTest = MakeSolTest(pS, *pSol, SolTol, dSolErr);
286 
287  if (outputCPUTime()) {
288  dEndTimeCPU = mbdyn_clock_time();
289  dResidualCPU = dEndTimeCPU - dStartTimeCPU;
290  }
291 
292  if (outputIters()) {
293 #ifdef USE_MPI
294  if (!bParallel || MBDynComm.Get_rank() == 0)
295 #endif /* USE_MPI */
296  {
297  silent_cout("\t\tSolErr "
298  << dSolErr << std::endl);
299  }
300  }
301 
302  if (bTest) {
303  throw ConvergenceOnSolution(MBDYN_EXCEPT_ARGS);
304  }
305 
306  // allow to bail out in case of multiple CTRL^C
309  }
310  }
311 }
virtual void Reset(void)=0
virtual VectorHandler * pResHdl(void) const =0
bool outputMatrixConditionNumber(void) const
virtual SolutionManager * pGetSolutionManager(void) const
Definition: solver.h:398
void AddCond(doublereal dCond)
Definition: nonlin.h:331
virtual void Jacobian(MatrixHandler *pJac) const =0
doublereal dGetCondMax() const
Definition: nonlin.h:275
bool outputIters(void) const
integer TotJac
Definition: nonlin.h:252
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
bool outputBailout(void) const
MatrixHandler::Norm_t GetCondMatNorm(void) const
virtual bool MakeSolTest(Solver *pS, const VectorHandler &Vec, const doublereal &dTol, doublereal &dTest)
Definition: nonlin.cc:497
virtual void PrintResidual(const VectorHandler &Res, integer iIterCnt) const
Definition: solver.cc:5194
virtual bool MakeResTest(Solver *pS, const NonlinearProblem *pNLP, const VectorHandler &Vec, const doublereal &dTol, doublereal &dTest, doublereal &dTestDiff)
Definition: nonlin.cc:474
double mbdyn_clock_time()
Definition: clock_time.cc:51
virtual void CheckTimeStepLimit(doublereal dErr, doublereal dErrDiff) const
Definition: solver.cc:5205
doublereal dGetCondAvg() const
Definition: nonlin.h:277
bool outputRes(void) const
integer Size
Definition: nonlin.h:251
virtual void Residual(VectorHandler *pRes) const =0
bool outputJac(void) const
doublereal dGetCondMin() const
Definition: nonlin.h:276
virtual integer iGetSize(void) const =0
doublereal dGetTimeCPU(CPUTimeType eType) const
Definition: nonlin.h:346
bool outputSolverConditionNumber(void) const
virtual doublereal ConditionNumber(enum Norm_t eNorm=NORM_1) const
Definition: mh.cc:451
bool bKeepJac
Definition: nr.h:52
int mbdyn_stop_at_end_of_iteration(void)
Definition: solver.cc:172
bool outputSol(void) const
VectorHandler * pRes
Definition: nr.h:48
virtual MatrixHandler * pMatHdl(void) const =0
integer iPerformedIterations
Definition: nr.h:53
const NonlinearProblem * pPrevNLP
Definition: nr.h:54
virtual void MatrReset(void)=0
void AddTimeCPU(doublereal dTime, CPUTimeType eType)
Definition: nonlin.h:355
integer IterationBeforeAssembly
Definition: nr.h:51
virtual void Solve(void)=0
#define ASSERT(expression)
Definition: colamd.c:977
virtual void MatrInitialize(void)
Definition: solman.cc:72
bool bTrueNewtonRaphson
Definition: nr.h:50
bool bGetConditionNumber(doublereal &dCond) const
Definition: solman.cc:107
virtual void PrintSolution(const VectorHandler &Sol, integer iIterCnt) const
Definition: solver.cc:5200
bool outputCPUTime(void) const
VectorHandler * pSol
Definition: nr.h:49
virtual VectorHandler * pSolHdl(void) const =0
virtual void Update(const VectorHandler *pSol) const =0
double doublereal
Definition: colamd.c:52
bool outputSolverConditionStat(void) const

Here is the call graph for this function:

Member Data Documentation

bool NewtonRaphsonSolver::bKeepJac
private

Definition at line 52 of file nr.h.

Referenced by Solve().

bool NewtonRaphsonSolver::bTrueNewtonRaphson
private

Definition at line 50 of file nr.h.

Referenced by Solve().

integer NewtonRaphsonSolver::iPerformedIterations
private

Definition at line 53 of file nr.h.

Referenced by Solve().

integer NewtonRaphsonSolver::IterationBeforeAssembly
private

Definition at line 51 of file nr.h.

Referenced by Solve().

const NonlinearProblem* NewtonRaphsonSolver::pPrevNLP
private

Definition at line 54 of file nr.h.

Referenced by Solve().

VectorHandler* NewtonRaphsonSolver::pRes
private

Definition at line 48 of file nr.h.

Referenced by Solve().

VectorHandler* NewtonRaphsonSolver::pSol
private

Definition at line 49 of file nr.h.

Referenced by Solve().


The documentation for this class was generated from the following files: