source: issm/trunk-jpl/src/c/solvers/solver_newton.cpp@ 14167

Last change on this file since 14167 was 14167, checked in by Mathieu Morlighem, 12 years ago

NEW: fixed newton iteration solver (only one iteration needed)

File size: 4.2 KB
Line 
1/*!\file: solver_nonlinear.cpp
2 * \brief: core of a non-linear solution, using fixed-point method
3 */
4
5#include "../toolkits/toolkits.h"
6#include "../classes/objects/objects.h"
7#include "../io/io.h"
8#include "../EnumDefinitions/EnumDefinitions.h"
9#include "../modules/modules.h"
10#include "../solutions/solutions.h"
11#include "./solvers.h"
12
13void solver_newton(FemModel* femmodel){
14
15 /*intermediary: */
16 bool converged;
17 int count;
18 IssmDouble kmax;
19 Matrix<IssmDouble>* Kff = NULL;
20 Matrix<IssmDouble>* Kfs = NULL;
21 Matrix<IssmDouble>* Jff = NULL;
22 Vector<IssmDouble>* ug = NULL;
23 Vector<IssmDouble>* old_ug = NULL;
24 Vector<IssmDouble>* uf = NULL;
25 Vector<IssmDouble>* old_uf = NULL;
26 Vector<IssmDouble>* duf = NULL;
27 Vector<IssmDouble>* pf = NULL;
28 Vector<IssmDouble>* pJf = NULL;
29 Vector<IssmDouble>* df = NULL;
30 Vector<IssmDouble>* ys = NULL;
31
32 /*parameters:*/
33 int max_nonlinear_iterations;
34 int configuration_type;
35
36 /*Recover parameters: */
37 femmodel->parameters->FindParam(&max_nonlinear_iterations,DiagnosticMaxiterEnum);
38 femmodel->parameters->FindParam(&configuration_type,ConfigurationTypeEnum);
39 femmodel->UpdateConstraintsx();
40
41 count=1;
42 converged=false;
43
44 /*Start non-linear iteration using input velocity: */
45 GetSolutionFromInputsx(&ug,femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters);
46 Reducevectorgtofx(&uf,ug,femmodel->nodes,femmodel->parameters);
47
48 //Update once again the solution to make sure that vx and vxold are similar (for next step in transient or steadystate)
49 InputUpdateFromConstantx(femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,converged,ConvergedEnum);
50 InputUpdateFromSolutionx(femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,ug);
51
52 for(;;){
53
54 xdelete(&old_ug);old_ug=ug;
55 xdelete(&old_uf);old_uf=uf;
56
57 /*Solver forward model*/
58 if(count==1){
59 femmodel->SystemMatricesx(&Kff, &Kfs, &pf, &df, NULL);
60 CreateNodalConstraintsx(&ys,femmodel->nodes,configuration_type);
61 Reduceloadx(pf,Kfs,ys);xdelete(&Kfs);
62 Solverx(&uf,Kff,pf,old_uf,df,femmodel->parameters);xdelete(&df);
63 Mergesolutionfromftogx(&ug,uf,ys,femmodel->nodes,femmodel->parameters);xdelete(&ys);
64 InputUpdateFromSolutionx(femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,ug);xdelete(&ug);
65 xdelete(&old_ug);old_ug=ug;
66 xdelete(&old_uf);old_uf=uf;
67 }
68 uf=old_uf->Duplicate(); old_uf->Copy(uf);
69
70 /*Prepare next iteration using Newton's method*/
71 femmodel->SystemMatricesx(&Kff, &Kfs, &pf, &df, NULL);xdelete(&df);
72 CreateNodalConstraintsx(&ys,femmodel->nodes,configuration_type);
73 Reduceloadx(pf,Kfs,ys); xdelete(&Kfs);
74
75 pJf=pf->Duplicate();
76 Kff->MatMult(uf,pJf);// xdelete(&Kff);
77 pJf->Scale(-1.0); pJf->AXPY(pf,+1.0); //xdelete(&pf);
78
79 femmodel->CreateJacobianMatrixx(&Jff,kmax);
80 Solverx(&duf,Jff,pJf,NULL,NULL,femmodel->parameters); xdelete(&Jff); xdelete(&pJf);
81 uf->AXPY(duf, 1.0); xdelete(&duf);
82 Mergesolutionfromftogx(&ug,uf,ys,femmodel->nodes,femmodel->parameters);xdelete(&ys);
83 InputUpdateFromSolutionx(femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,ug);
84
85 /*Check convergence*/
86 convergence(&converged,Kff,pf,uf,old_uf,femmodel->parameters);
87 xdelete(&Kff); xdelete(&pf);
88 if(converged==true){
89 bool max_iteration_state=false;
90 int tempStep=1;
91 IssmDouble tempTime=1.0;
92 femmodel->results->AddObject(new GenericExternalResult<bool>(femmodel->results->Size()+1, MaxIterationConvergenceFlagEnum, max_iteration_state, tempStep, tempTime));
93 break;
94 }
95 if(count>=max_nonlinear_iterations){
96 _pprintLine_(" maximum number of Newton iterations (" << max_nonlinear_iterations << ") exceeded");
97 bool max_iteration_state=true;
98 int tempStep=1;
99 IssmDouble tempTime=1.0;
100 femmodel->results->AddObject(new GenericExternalResult<bool>(femmodel->results->Size()+1, MaxIterationConvergenceFlagEnum, max_iteration_state, tempStep, tempTime));
101 break;
102 }
103
104 count++;
105 }
106
107 if(VerboseConvergence()) _pprintLine_("\n total number of iterations: " << count-1);
108
109 /*clean-up*/
110 xdelete(&uf);
111 xdelete(&ug);
112 xdelete(&old_ug);
113 xdelete(&old_uf);
114}
Note: See TracBrowser for help on using the repository browser.