NeoPZ
pztransientanalysis.cpp
Go to the documentation of this file.
1 
6 #include "pztransientanalysis.h"
7 #include "pztransientmat.h"
8 #include "TPZSpStructMatrix.h"
9 #include "pzfstrmatrix.h"
10 #include "pzstrmatrix.h"
11 #include "pzseqsolver.h"
12 #include "checkconv.h"
13 
14 using namespace std;
15 
16 template<class TRANSIENTCLASS>
18 
19 template<class TRANSIENTCLASS>
20 TPZTransientAnalysis<TRANSIENTCLASS>::TPZTransientAnalysis(TPZCompMesh *mesh, bool IsLinear, std::ostream &out):/*TPZAnalysis*/TPZNonLinearAnalysis(mesh,out), fSavedSolutionVec(){
21  this->fTimeStep = 0.;
22  this->fCurrentIter = 0;
23  this->SetConvergence(0, 0.);
24  this->SetNewtonConvergence(0, 0.);
26  this->fIsLinearProblem = IsLinear;
27  this->SetSaveFrequency(0,0);
28  this->fSaveSolutionVecFrequency = 0;
29 }
30 
31 template<class TRANSIENTCLASS>
33  fSavedSolutionVec.clear();
34 }
35 
36 template<class TRANSIENTCLASS>
38  const int nrows = this->Mesh()->Solution().Rows();
39  const int ncols = this->Mesh()->Solution().Cols();
40  if ( (InitialSol.Rows() != nrows) || (InitialSol.Cols() != ncols) ){
41  PZError << "ERROR! " << __PRETTY_FUNCTION__ << " at line " << __LINE__ << std::endl;
42  }
43  else{
44  this->fSolution = InitialSol;
45  }
46 }
47 
48 template<class TRANSIENTCLASS>
50  TPZFMatrix<STATE> & MeshSol = this->Mesh()->Solution();
51  this->fSolution.Redim( MeshSol.Rows(), MeshSol.Cols() );
52  this->fSolution.Zero();
53 }
54 
55 template<class TRANSIENTCLASS>
56 void TPZTransientAnalysis<TRANSIENTCLASS>::RunTransient(std::ostream &out, bool FromBegining, bool linesearch){
57 
58  this->SetImplicit();
59 
60  if (FromBegining){
61  this->fCurrentIter = 0;
62  this->fSavedSolutionVec.clear();
63  }
64 
65 #ifdef _DOCHECKCONV_
66  {
67  TPZVec<REAL> coefs(1,1.);
69  TPZFMatrix<STATE> range(fCompMesh->NEquations(),1,1.);
70  this->SetLastState();
71  CheckConvergence(*this,cpSol,range,coefs);
72  this->SetCurrentState();
73  CheckConvergence(*this,cpSol,range,coefs);
74  }
75 #endif
76 
77  this->LoadSolution(fSolution);
78 
80  // this->PostProcess(this->fDXResolution);
81 
82  this->SetAllMaterialsDeltaT();
83 
84  if (this->fIsLinearProblem){
86  }
87 
88  TPZFMatrix<STATE> prevsol, laststate, lastsol;
89  for( ; this->fCurrentIter < this->fNIter;){
90 
91  this->fCurrentIter++;
93 
94  //Computing residual of last state solution
95  // this->fSolution = prevsol;
96  this->SetLastState();
97  this->Assemble();
98  laststate = this->fRhs;
99  prevsol = fSolution;
100  lastsol = fSolution;
101  //Newton's method
102  this->SetCurrentState();
103  REAL error = this->fNewtonTol * 2. + 1.;
104  int iter = 0;
105  while(error > this->fNewtonTol && iter < this->fNewtonMaxIter) {
106 
107  fSolution.Redim(0,0);
108  this->Assemble();
109  this->fRhs += laststate;
110  this->Solve();
111 
112  if (linesearch){
113  TPZFMatrix<STATE> nextSol;
114  REAL LineSearchTol = 1e-3 * Norm(fSolution);
115  const int niter = 100;
116  this->LineSearch(prevsol, fSolution, nextSol, LineSearchTol, niter);
117  fSolution = nextSol;
118  }
119  else{
120  fSolution += prevsol;
121  }
122 
123  prevsol -= fSolution;
124  REAL norm = Norm(prevsol);
125  out << "Iteracao n : " << (iter+1) << " : norma da solucao |Delta(Un)|: " << norm << std::endl;
126 
127  prevsol = fSolution;
129 
130  error = norm;
131  iter++;
132  }//Newton's iterations
133 
134  prevsol = fSolution;
135 
136  if (this->fSaveFrequency){
137  if (!(this->fCurrentIter % fSaveFrequency)){
138  this->PostProcess(this->fDXResolution);
139  }
140  }
141  this->SaveCurrentSolutionVec();
142 
143  prevsol -= lastsol;
144  REAL steadynorm = Norm(prevsol);
145  std::cout << "*********** Steady state error at iteration " << this->fCurrentIter << " = " << steadynorm << "\n\n";
146  if (!fForceAllSteps){
147  if (steadynorm < this->fSteadyTol){
148  std::cout << "Steady state solution achieved\n\n";
149  this->fNIter = fCurrentIter;
150  break;
151  }
152  }
153  std::cout.flush();
154 
155  }//time iterations
156 
157 }//method
158 
159 template<class TRANSIENTCLASS>
161  TPZCompMesh * mesh = this->Mesh();
162  std::map<int, TPZMaterial * >::iterator matit;
163  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
164  {
165  if(!matit->second) continue;
167  if (trans){
168  trans->SetImplicit();
169  }
170  }
171 }
172 
173 template<class TRANSIENTCLASS>
175  TPZCompMesh * mesh = this->Mesh();
176  std::map<int, TPZMaterial * >::iterator matit;
177  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
178  {
179  if(!matit->second) continue;
181  if (trans){
182  trans->SetExplicit();
183  }
184  }
185 }
186 
187 template<class TRANSIENTCLASS>
189  TPZCompMesh * mesh = this->Mesh();
190  std::map<int, TPZMaterial * >::iterator matit;
191  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
192  {
193  if(!matit->second) continue;
195  if (trans){
196  trans->SetLastState();
197  }
198  }
199 }
200 
201 template<class TRANSIENTCLASS>
203  TPZCompMesh * mesh = this->Mesh();
204  std::map<int, TPZMaterial * >::iterator matit;
205  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
206  {
207  if(!matit->second) continue;
209  if (trans){
210  trans->SetCurrentState();
211  }
212  }
213 }
214 
215 template<class TRANSIENTCLASS>
217  TPZCompMesh * mesh = this->Mesh();
218  std::map<int, TPZMaterial * >::iterator matit;
219  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
220  {
221  if(!matit->second) continue;
223  if (trans){
224  trans->SetMassMatrix();
225  }
226  }
227 }
228 
229 template<class TRANSIENTCLASS>
231  TPZCompMesh * mesh = this->Mesh();
232  std::map<int, TPZMaterial * >::iterator matit;
233  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
234  {
235  if(!matit->second) continue;
237  if (trans){
238  trans->SetFluxOnly();
239  }
240  }
241 }
242 
243 template<class TRANSIENTCLASS>
245  TPZCompMesh * mesh = this->Mesh();
246  std::map<int, TPZMaterial * >::iterator matit;
247  for(matit = mesh->MaterialVec().begin(); matit != mesh->MaterialVec().end(); matit++)
248  {
249  if(!matit->second) continue;
251  if (trans){
252  trans->SetTimeStep(this->TimeStep());
253  }
254  }
255 }
256 
257 template<class TRANSIENTCLASS>
259  REAL T = this->GetCurrentIter() * this->TimeStep();
260  this->fTime = T;
261  TPZAnalysis::PostProcess(resolution, dimension);
262 }//method
263 
264 template<class TRANSIENTCLASS>
266  REAL T = this->GetCurrentIter() * this->TimeStep();
267  this->gTime = T;
268  out << "\nSOLUTION #" << this->GetCurrentIter() << " AT TIME = " << T << std::endl;
269  TPZAnalysis::PostProcess(loc, out);
270  out << "\n***************************************\n" << std::endl;
271 }//method
272 
273 template<class TRANSIENTCLASS>
275  if(!fCompMesh || !fStructMatrix || !fSolver){
276  cout << "TPZTransientAnalysis::Assemble lacking definition for Assemble fCompMesh "<< (void *) fCompMesh
277  << " fStructMatrix " << (bool) fStructMatrix << " fSolver " << fSolver << " at file "
278  << __FILE__ << " line " << __LINE__ << endl;
279  return;
280  }
281 
282  int sz = fCompMesh->NEquations();
283  fRhs.Redim(sz,1);
284 
285  bool exist = false;
286  if(fSolver->Matrix()) if (fSolver->Matrix()->Rows()==sz) exist = true;
288  if (exist){
289  if (fIsLinearProblem){
290  // TPZStructMatrix::Assemble(fRhs, *Mesh());
291  fStructMatrix->Assemble(fRhs,inter);
292  }
293  else{
294  fSolver->Matrix()->Zero();
295  fStructMatrix->Assemble((TPZMatrix<STATE>&)fSolver->Matrix(),fRhs,inter);
296  }
297  }
298  else{
299  if (this->fIsLinearProblem){
300  std::cout << __PRETTY_FUNCTION__ << " @ " << __LINE__ << " Error! StrMatrix must be created using"
301  << " methodTPZTransientAnalysis::ComputeLinearTangentMatrix()"
302  << " when (this->fIsLinearProblem == true)\n";
303  }
304  TPZMatrix<STATE> *mat = fStructMatrix->CreateAssemble(fRhs,NULL);
305  fSolver->SetMatrix(mat);
306  }
308 }
309 
310 template<class TRANSIENTCLASS>
312  if (!fIsLinearProblem) return;
313  this->SetCurrentState();
314  const int sz = this->Mesh()->NEquations();
315  fRhs.Redim(sz,1);
316  TPZMatrix<STATE> *mat = fStructMatrix->CreateAssemble(fRhs,NULL);
317  fSolver->SetMatrix(mat);
318 }//method
319 
320 template<class TRANSIENTCLASS>
322  this->SetMassMatrix();
323  const int sz = this->Mesh()->NEquations();
324  fRhs.Redim(sz,1);
325  TPZMatrix<STATE> *mat = fStructMatrix->CreateAssemble(fRhs,NULL);
326  fSolver->SetMatrix(mat);
327 }//method
328 
329 template<class TRANSIENTCLASS>
331  if(!fCompMesh || !fStructMatrix || !fSolver){
332  cout << "TPZTransientAnalysis::Assemble lacking definition for Assemble fCompMesh "<< (void *) fCompMesh
333  << " fStructMatrix " << (bool) fStructMatrix << " fSolver " << fSolver << " at file "
334  << __FILE__ << " line " << __LINE__ << endl;
335  return;
336  }
337 
338  this->SetFluxOnly();
339  int sz = fCompMesh->NEquations();
340  fRhs.Redim(sz,1);
341  if(fSolver->Matrix() && fSolver->Matrix()->Rows()==sz){
342  fStructMatrix->Assemble(fRhs,NULL);
343  // TPZStructMatrix::Assemble(fRhs, *Mesh());
344  }//if
345 }//method
346 
347 template<class TRANSIENTCLASS>
348 void TPZTransientAnalysis<TRANSIENTCLASS>::RunExplicit(std::ostream &out, bool FromBegining){
349 
350  this->SetExplicit();
351 
352  if (FromBegining){
353  this->fCurrentIter = 0;
354  this->fSavedSolutionVec.clear();
355  }
357  this->PostProcess(this->fDXResolution);
358 
359  this->SetAllMaterialsDeltaT();
360 
361  TPZFMatrix<STATE> prevsol;
362  for( this->fCurrentIter++ ; this->fCurrentIter < this->fNIter; this->fCurrentIter++){
363 
364  this->ComputeMassMatrix();
365 
367 
368  this->SetFluxOnly();
369 
370  //Computing residual of last state solution
371  prevsol = fSolution;
373  this->ComputeFluxOnly();
374 
375  this->Solve();
376  //now fSolution = deltaSol
377  fSolution += prevsol;
378 
380  if (this->fSaveFrequency){
381  if (!(this->fCurrentIter % fSaveFrequency)){
382  this->PostProcess(this->fDXResolution);
383  }
384  }
385  this->SaveCurrentSolutionVec();
386 
387  prevsol -= fSolution;
388  REAL steadynorm = Norm(prevsol);
389  std::cout << "*********** Steady state error at iteration " << (this->fCurrentIter) << " = " << steadynorm << "\n\n";
390  if (!fForceAllSteps){
391  if (steadynorm < this->fSteadyTol){
392  std::cout << "Steady state solution achieved\n\n";
393  this->fNIter = fCurrentIter;
394  break;
395  }
396  }
397  std::cout.flush();
398 
399  }//time iterations
400 
401 }//method
402 
403 template<class TRANSIENTCLASS>
405  this->fSaveSolutionVecFrequency = SaveFrequency;
406 }
407 
408 template<class TRANSIENTCLASS>
409 std::list< std::pair<TPZFMatrix<STATE>, STATE> > & TPZTransientAnalysis<TRANSIENTCLASS>::GetSavedSolutions(){
410  return this->fSavedSolutionVec;
411 }
412 
413 #include <sstream>
414 template<class TRANSIENTCLASS>
416  if(!this->fSaveSolutionVecFrequency) return;
417  if(this->fCurrentIter % this->fSaveSolutionVecFrequency == 0){
418  std::pair< TPZFMatrix<STATE>, STATE > mypair;
419  mypair.first = this->Solution();
420  mypair.second = TPZTransientAnalysis::gTime;
421  this->fSavedSolutionVec.push_back(mypair);
422 
423  ofstream file("currentsol.txt");
424  stringstream mess; mess << "sol( " << TPZTransientAnalysis::gTime << " ) = ";
425  this->Solution().Print(mess.str().c_str(), file);
426 
427  }
428 }
429 
430 //instantiations
431 #ifndef STATE_COMPLEX
432 #include "pzpoisson3d.h"
434 
435 #include "pznonlinearpoisson3d.h"
437 
438 #include "pzburger.h"
439 template class TPZTransientAnalysis< TPZBurger >;
440 
441 #endif
TPZMatrixSolver< STATE > * fSolver
Type of solver to be applied.
Definition: pzanalysis.h:52
void SetSaveSolution(int SaveFrequency)
Defines to save solution vector with SaveFrequency frequency.
static double gTime
Static attribute storing the current time of simulation.
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 the TPZNonLinearPoisson3d class.
REAL fNewtonTol
Tolerance of Newton&#39;s method.
void SetFluxOnly()
Sets all materials to compute only the flux contributions - used in the explicit scheme.
virtual void Solve()
Invert the stiffness matrix.
Definition: pzanalysis.cpp:352
TPZFMatrix< STATE > fSolution
Solution vector.
Definition: pzanalysis.h:50
void SetExplicit()
Sets all materials in temporal scheme as an explicit Euler.
Contains the TPZTransientMaterial class which implements an implicit Euler time integrator.
virtual void UpdateFrom(TPZAutoPointer< TPZMatrix< TVar > > matrix) override
Updates the values of the current matrix based on the values of the matrix.
Definition: pzsolve.h:121
void SetTimeStep(REAL TimeStep)
Define time step DeltaT.
int64_t NEquations()
This computes the number of equations associated with non-restrained nodes.
Definition: pzcmesh.cpp:721
clarg::argInt dimension("-d", "Matrices dimension M x M", 1000)
std::list< std::pair< TPZFMatrix< STATE >, STATE > > fSavedSolutionVec
Attribute to store solution vectors during process. Pair of (solution vec, simulation time) ...
virtual void PostProcess(int resolution)
See base class for comments.
REAL LineSearch(const TPZFMatrix< STATE > &Wn, TPZFMatrix< STATE > DeltaW, TPZFMatrix< STATE > &NextW, REAL tol, int niter)
Implements a golden section line search.
~TPZTransientAnalysis()
Default destructor.
virtual int Zero()
Zeroes the matrix.
Definition: pzmatrix.h:296
TPZCompMesh * fCompMesh
Computational mesh.
Definition: pzanalysis.h:44
void SetLastState()
Sets all materials in LastState.
TPZCompMesh * Mesh() const
Returns the pointer to the computational mesh.
Definition: pzanalysis.h:180
virtual void RunTransient(std::ostream &out=std::cout, bool FromBegining=true, bool linesearch=true)
Executes a Newton&#39;s method for the solution of the implicit in time equation.
void ComputeLinearTangentMatrix()
Computes linear tangent matrix for linear problems.
Contains the TPZStructMatrixOR class which responsible for a interface among Matrix and Finite Elemen...
void SaveCurrentSolutionVec()
If fSaveSolutionVecFrequency != 0, save current solution vector in fSavedSolutionVec attribute...
Contains the TPZMatPoisson3d class.
virtual void Assemble()
Assemble flux vector and jacobian matrix.
void ComputeFluxOnly()
Computes the only the flux contribution for the explicit scheme.
Contains the TPZFStructMatrix class which implements Full Structural Matrices.
TPZFMatrix< STATE > & Solution()
Returns the solution matrix.
Definition: pzanalysis.h:177
void error(char *string)
Definition: testShape.cc:7
virtual void RunExplicit(std::ostream &out=std::cout, bool FromBegining=true)
Solves a explicit Euler&#39;s scheme in time.
void SetAllMaterialsDeltaT()
Sets all materials the time step.
int Zero() override
Makes Zero all the elements.
Definition: pzfmatrix.h:651
Contains the TPZSpStructMatrix class which implements sparse structural matrices. ...
int fCurrentIter
Current iteration. Variable allowing to restart the simulation.
virtual void LoadSolution()
Load the solution into the computable grid.
Definition: pzanalysis.cpp:441
void SetLastState()
Set material to compute only Integral[- un/deltaT * v, Omega].
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
int fSaveSolutionVecFrequency
Frequency which solution vector must be saved.
REAL & TimeStep()
Access to time step attribute.
Implements a very simple manner to perform transient simulations. Analysis.
void SetImplicit()
Sets all materials in temporal scheme as an implicit Euler.
virtual void SetMatrix(TPZAutoPointer< TPZMatrix< TVar > > Refmat)
Sets a matrix to the current object.
Definition: pzsolve.h:115
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
Implements an implicit Euler time integrator. The Material Classes Material.
void SetConvergence(int niter, REAL eps, bool ForceAllSteps=true)
Defines max number of steps and steady state convergence tolerance.
Contains TPZSequenceSolver class which defines sequence solvers.
virtual void LoadSolution()
Load the solution into the computable grid.
Contains the TPZBurger class which implements a linear convection equation using a burger flux...
virtual void PostProcess(int resolution)
Draw solution over mesh for all dimensions.
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
Definition: pzfmatrix.h:616
void SetCurrentState()
Sets all materials in CurrentState.
void SetMassMatrix()
Set material to compute ek = Integral[phi_i phi_j, Omega]/deltaT.
std::map< int,TPZMaterial *> & MaterialVec()
Returns a reference to the material pointers vector.
Definition: pzcmesh.h:203
Contains TPZTransientAnalysis class which implements a simple manner to perform transient simulations...
int fNIter
Number of iterations counting from fCurrentIter to fCurrentIter+fNIter.
void SetInitialSolutionAsZero()
Sets problem initial solution as zero.
std::list< std::pair< TPZFMatrix< STATE >, STATE > > & GetSavedSolutions()
Access to saved solution. Pair of (solution vec, simulation time)
int fSaveFrequency
Frequency which solution must be saved in DX file.
void SetExplicit()
Sets integral scheme as an explicit Euler.
bool fIsLinearProblem
Flag indicating whether the problem is linear or not.
int fDXResolution
Resolution of DX mesh.
TPZAutoPointer< TPZStructMatrix > fStructMatrix
Structural matrix.
Definition: pzanalysis.h:68
REAL fSteadyTol
Tolerance to consider the problem solution as steady state.
This class implements a very simple interface from PZ kernel to GUI. Module: Common.
Implements computational mesh. Computational Mesh.
Definition: pzcmesh.h:47
int GetCurrentIter()
Returns current iteration.
TPZAutoPointer< TPZMatrix< TVar > > Matrix() const
Returns a pointer to TPZMatrix<>
Definition: pzsolve.h:138
REAL fTimeStep
Simulation time step.
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
Derived class from TPZAnalysis implements non linear analysis (Newton&#39;s method). Analysis.
virtual void Print(std::ostream &out) const
Definition: pzmatrix.h:253
bool fForceAllSteps
Flag indicating whether all steps must be performed even if tolerance is achieved.
void SetNewtonConvergence(int niter, REAL eps)
Defines max number of steps and error convergence tolerance for Newton&#39;s method.
void ComputeMassMatrix()
Computes the mass matrix for the explicit scheme.
TPZFMatrix< STATE > fRhs
Load vector.
Definition: pzanalysis.h:48
void SetSaveFrequency(int SaveFrequency, int resolution)
Defines properties of DX file.
Contains the implementation of the CheckConvergence function.
void SetInitialSolution(TPZFMatrix< STATE > &InitialSol)
Sets problem initial solution.
TPZFMatrix< STATE > & Solution()
Access the solution vector.
Definition: pzcmesh.h:219
REAL fTime
Time variable which is used in dx output.
Definition: pzanalysis.h:62
void SetFluxOnly()
Set material to compute ef = Linear Form - Bilinear Form(u) = F -ku.
void SetMassMatrix()
Sets all materials to compute the mass matrix - used in the explicit scheme.
int fNewtonMaxIter
Max iteration number of Newton&#39;s method.
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15
TPZTransientAnalysis(TPZCompMesh *mesh, bool IsLinear=false, std::ostream &out=std::cout)
Constructor.
void SetCurrentState()
Set material to compute Integral[un+1/deltaT * v, Omega] + Bilinear Form = Linear Form...