NeoPZ
pznonlinanalysis.cpp
Go to the documentation of this file.
1 
6 #include "pznonlinanalysis.h"
7 #include "pzcmesh.h"
8 #include "pzcompel.h"
9 #include "pzintel.h"
10 #include "pzfmatrix.h"
11 #include "pzsolve.h"
12 #include "TPZMaterial.h"
13 #include "pzelmat.h"
14 #include "pzvec.h"
15 #include "pzmanvector.h"
16 #include "checkconv.h"
17 #include "pzstrmatrix.h"
18 
19 #include "pzlog.h"
20 
21 #include <stdio.h>
22 #include <fstream>
23 
24 #ifdef LOG4CXX
25 static LoggerPtr logger(Logger::getLogger("pz.nonlinearanalysis"));
26 #endif
27 
28 using namespace std;
29 
31  if(Mesh()) Mesh()->Solution().Zero();
32  fSolution.Zero();
33 }
34 
35 TPZNonLinearAnalysis::TPZNonLinearAnalysis(TPZCompMesh *mesh,std::ostream &out) : TPZAnalysis(mesh,true,out) {
36  if(Mesh()) Mesh()->Solution().Zero();
37  fSolution.Zero();
38 }
39 
41 }
42 
43 
44 //#define DEBUGLINESEARCH
45 #ifdef PZDEBUGLINESEARCH
46 ofstream alphafile("c:\\Temp\\tmp\\alpha.txt");
47 #endif
49  REAL error = 2.*tol+1.;
50  REAL A = 0.1, B = 2., L = 0, M = 0.;
51  TPZFMatrix<STATE> ak, bk, lambdak, muk, Interval;
52  REAL NormResLambda = 0., NormResMu = 0.;
53  //ak = Wn + 0.1 * DeltaW
54  ak = DeltaW;
55  ak *= A;
56  ak += Wn;
57  //bk = Wn + 2. DeltaW
58  bk = DeltaW;
59  bk *= B;
60  bk += Wn;
61  //Interval = (bk-ak)
62  Interval = bk; Interval -= ak;
63  int iter = 0;
64  int KeptVal = -1; //0 means I have residual(labmda); 1 means I have residual(mu); -1 means I have nothing
65  while(error > tol && iter < niter){
66  iter++;
67 
68  if (KeptVal != 0){
69  L = 0.382*(B-A)+A;
70  //lambdak = ak + 0.382*(bk-ak)
71  lambdak = Interval; lambdak *= 0.382; lambdak += ak;
72  //computing residual
73  this->LoadSolution(lambdak);
74  LOGPZ_DEBUG(logger,"After LoadSolution")
75  // LogWellSolution(*this->Mesh(), 6);
76  this->AssembleResidual();
77  LOGPZ_DEBUG(logger,"After AssembleResidual")
78  // LogWellSolution(*this->Mesh(), 6);
79  NormResLambda = Norm(fRhs);
80  }
81 
82  if (KeptVal != 1){
83  //muk = ak + 0.618*(bk-ak)
84  M = 0.618*(B-A)+A;
85  muk = Interval; muk *= 0.618; muk += ak;
86  this->LoadSolution(muk);
87  this->AssembleResidual();
88  NormResMu = Norm(fRhs);
89  }
90 
91  if (NormResLambda > NormResMu){
92  A = L;
93  L = M;
94  ak = lambdak;
95  lambdak = muk;
96  NormResLambda = NormResMu;
97  KeptVal = 0;
98  }
99  else{
100  B = M;
101  M = L;
102  bk = muk;
103  muk = lambdak;
104  NormResMu = NormResLambda;
105  KeptVal = 1;
106  }
107  //error = Norm(bk-ak)
108  Interval = bk; Interval -= ak; error = Norm(Interval);
109 
110  //alpha shall be alpha <= 1
111  if(A > 1. && B > 1.) break;
112 
113  }//while
114 
115  double ALPHA = 0.5*(A + B);
116  NextW = ak;
117  NextW += bk;
118  NextW *= 0.5;
119 
120 
121 #ifdef PZDEBUGLINESEARCH
122  //debug: valor do alpha
123  TPZFMatrix<REAL> alpha;
124  alpha = NextW;
125  alpha -= Wn;
126  REAL sum = 0.;
127  int ncontrib = 0;
128  for(int i = 0; i < alpha.Rows(); i++){
129  if (DeltaW(i,0)){
130  alpha(i,0) = alpha(i,0)/DeltaW(i,0);
131  sum += alpha(i,0);
132  ncontrib++;
133  }
134  }
135  //REAL MeanAlpha = sum/ncontrib;
136  alphafile << /*MeanAlpha << "\t" <<*/ "ALPHA = " << ALPHA << "\n";
137  alphafile.flush();
138 #endif
139 
140  if(ALPHA > 1.){ //alpha shall be alpha <= 1
141  NextW = Wn;
142  NextW += DeltaW;
143 #ifdef PZDEBUGLINESEARCH
144  alphafile << "ALPHA LIMIT APPLIED. Alpha = 1.\n";
145 #endif
146  return 1.;
147  }
148 
149  return ALPHA;
150 
151 }//void
152 
153 void TPZNonLinearAnalysis::IterativeProcess(std::ostream &out,REAL tol,int numiter, bool linesearch, bool checkconv) {
154 
155  int iter = 0;
156  REAL error = 1.e10;
157  int numeq = fCompMesh->NEquations();
158 
159  TPZFMatrix<STATE> prevsol(fSolution);
160  if(prevsol.Rows() != numeq) prevsol.Redim(numeq,1);
161 
162  if(checkconv){
163  TPZVec<REAL> coefs(1,1.);
164  TPZFMatrix<STATE> range(numeq,1,1.);
165  CheckConvergence(*this,fSolution,range,coefs);
166  }
167 
168  while(error > tol && iter < numiter) {
169 
170 // fSolution.Redim(0,0);
171  Assemble();
172  Solve();
173  if (linesearch){
174  TPZFMatrix<STATE> nextSol;
175  REAL LineSearchTol = 1e-3 * Norm(fSolution);
176  const int niter = 10;
177  this->LineSearch(prevsol, fSolution, nextSol, LineSearchTol, niter);
178  fSolution = nextSol;
179  }
180  else{
181  fSolution += prevsol;
182  }
183 
184  prevsol -= fSolution;
185  REAL normDeltaSol = Norm(prevsol);
186  prevsol = fSolution;
187  this->LoadSolution(fSolution);
188  this->AssembleResidual();
189  double NormResLambda = Norm(fRhs);
190  double norm = NormResLambda;
191  out << "Iteracao n : " << (iter+1) << " : normas |Delta(Un)| e |Delta(rhs)| : " << normDeltaSol << " / " << NormResLambda << endl;
192 
193  if(norm < tol) {
194  out << "\nTolerancia atingida na iteracao : " << (iter+1) << endl;
195  out << "\n\nNorma da solucao |Delta(Un)| : " << norm << endl << endl;
196 
197  } else
198  if( (norm - error) > 1.e-9 ) {
199  out << "\nDivergent Method\n";
200  }
201  error = norm;
202  iter++;
203  out.flush();
204  }
205 }
206 
209  int i,cap = val.NElements() ;
210  deriv.Zero();
211  for(i=0;i<cap;i++) val[i] = 0.;
212 }
214  return Norm(fSolution);
215 }
216 
218 
219  int neq = fCompMesh->NEquations();
220  tangent.Redim(neq,neq);
221  TPZFMatrix<STATE> rhs(neq,1);
222  fStructMatrix->Assemble(tangent,rhs,NULL);
223 }
224 
226  return 1;
227 }
228 
230  int neq = fCompMesh->NEquations();
231  TPZFMatrix<STATE> tangent(neq,neq);
232  residual.Redim(neq,1);
233  fStructMatrix->Assemble(tangent,residual,NULL);
234  residual *= -1.;
235 }
236 
238  fSolution = state;
239  LoadSolution();
240 }
241 
244 }
245 
247  this->LoadSolution(state);
248 }
void CheckConvergence(TConv &obj, TPZFMatrix< STATE > &state, TPZFMatrix< STATE > &range, TPZVec< REAL > &coefs)
Implements a general procedure to check whether the class TConv implements a consistente tangent matr...
Definition: checkconv.h:23
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
virtual void Solve()
Invert the stiffness matrix.
Definition: pzanalysis.cpp:352
TPZFMatrix< STATE > fSolution
Solution vector.
Definition: pzanalysis.h:50
int64_t NEquations()
This computes the number of equations associated with non-restrained nodes.
Definition: pzcmesh.cpp:721
REAL SolutionNorm()
Computes the L2 norm of the solution.
virtual void ComputeTangent(TPZFMatrix< STATE > &tangent, TPZVec< REAL > &coefs, int icase)
Contains declaration of TPZCompEl class which defines the interface of a computational element...
Templated vector implementation.
REAL LineSearch(const TPZFMatrix< STATE > &Wn, TPZFMatrix< STATE > DeltaW, TPZFMatrix< STATE > &NextW, REAL tol, int niter)
Implements a golden section line search.
TPZCompMesh * fCompMesh
Computational mesh.
Definition: pzanalysis.h:44
virtual void Assemble()
Assemble the stiffness matrix and load vector.
Definition: pzanalysis.cpp:304
TPZCompMesh * Mesh() const
Returns the pointer to the computational mesh.
Definition: pzanalysis.h:180
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
Contains the TPZStructMatrixOR class which responsible for a interface among Matrix and Finite Elemen...
void LoadState(TPZFMatrix< STATE > &state)
Load solution with state as solution. But fSolution is not modified.
void error(char *string)
Definition: testShape.cc:7
Contains declaration of TPZElementMatrix struct which associates an element matrix with the coeficien...
int Zero() override
Makes Zero all the elements.
Definition: pzfmatrix.h:651
static const double tol
Definition: pzgeoprism.cpp:23
Implements the sequence of actions to perform a finite element analysis. Analysis.
Definition: pzanalysis.h:32
virtual void LoadSolution()
Load the solution into the computable grid.
Definition: pzanalysis.cpp:441
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
Free store vector implementation.
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
virtual void LoadSolution()
Load the solution into the computable grid.
Contains declaration of TPZCompMesh class which is a repository for computational elements...
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
Definition: pzfmatrix.h:616
virtual void IterativeProcess(std::ostream &out, REAL tol, int numiter, bool linesearch=false, bool checkconv=false)
It process a Newton&#39;s method to solve the non-linear problem.
TPZAutoPointer< TPZStructMatrix > fStructMatrix
Structural matrix.
Definition: pzanalysis.h:68
virtual void AssembleResidual()
Assemble the load vector.
Definition: pzanalysis.cpp:288
int NumCases()
Actually return 1.
Implements computational mesh. Computational Mesh.
Definition: pzcmesh.h:47
Contains TPZSolver class which defines a abstract class of solvers which will be used by matrix class...
Contains declaration of TPZInterpolatedElement class which implements computational element of the in...
virtual void Residual(TPZFMatrix< STATE > &residual, int icase)
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
void NullForce(TPZVec< REAL > &, TPZVec< STATE > &val, TPZFMatrix< STATE > &deriv)
Zeroes entries of val vector and deriv matrix.
TPZFMatrix< STATE > fRhs
Load vector.
Definition: pzanalysis.h:48
Contains the implementation of the CheckConvergence function.
TPZNonLinearAnalysis()
Default constructor.
TPZFMatrix< STATE > & Solution()
Access the solution vector.
Definition: pzcmesh.h:219
virtual ~TPZNonLinearAnalysis()
Default destructor.
Contains TPZNonLinearAnalysis class which implements the non linear analysis.