NeoPZ
checkconv.h
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #include <fstream>
10 #include <pzfmatrix.h>
11 
12 template <class TConv>
13 
24 
25  TPZFMatrix<STATE> incval(range);
26  int i,numrows;
27 
28  numrows = state.Rows();
29  for(i=0; i<numrows; i++) {
30  REAL randnum = (rand()&1000)/999.;
31  incval(i,0) = range(i,0)*(STATE)randnum;
32  }
33 
34  std::ofstream log("conv.log");
35 
36  int icase, j;
37 
38  int numcases = obj.NumCases();
39 
40  int ncoefs = coefs.NElements();
41 
42 
43  for(icase = 0; icase < numcases; icase++) {
44 
45  obj.LoadState(state);
46  TPZFMatrix<STATE> ReferenceResidual, Tangent, EstimateRes;
47 
48  obj.ComputeTangent(Tangent,coefs,icase);
49 
50  // Tangent.Print("tangent matrix");
51  obj.Residual(ReferenceResidual,icase);
52 
53  // ReferenceResidual.Print("reference residual");
54  EstimateRes.Redim(ReferenceResidual.Rows(),ReferenceResidual.Cols());
55  Tangent.Multiply(incval,EstimateRes);
56 
57  int interval;
58 
59  REAL difnorm[10] = {0.};
60 
61  for(interval = 1; interval < 10; interval++) {
62 
63  TPZFMatrix<STATE> actualstate(state);
64  TPZFMatrix<STATE> residual,temp;
65 
66  for(i=0; i<numrows; i++) {
67  for(j=0; j<ncoefs; j++) {
68  actualstate(i,j) += (STATE)(interval/10.)*(STATE)incval(i,0)*(STATE)coefs[j];
69  }
70  }
71 
72  obj.LoadState(actualstate);
73  obj.Residual(residual,icase);
74 
75  residual -= ReferenceResidual;
76  // residual.Print("residual-referenceresidual");
77  // resnorm[interval] = Norm(residual);
78  residual = residual - EstimateRes*(STATE(interval/10.));
79  temp = EstimateRes*(STATE(interval/10.));
80  // std::cout << "\n EstimateRes*(STATE(interval/10.)) = " <<temp << std::endl;
81  // residual.Print("residual adaptado");
82  difnorm[interval] = Norm(residual);
83  // std::cout << "\n difnorm = " << difnorm[interval]<< std::endl;
84 
85  }
86 
87  std::cout << "icase = " << icase << std::endl;
88  log << "icase = " << icase << std::endl;
89 
90  for(interval = 2; interval<10; interval++) {
91 
92  if(fabs(difnorm[interval]) < REAL(1.e-12) || fabs(difnorm[interval-1]) <REAL(1.e-12)) {
93  std::cout << "residual too small\n";
94  log << "residual too small\n";
95  break;
96  }
97  std::cout << (log10(difnorm[interval])-log10(difnorm[interval-1]))/
98 
99  (log10((float)interval)-log10(interval-1.0)) << std::endl;
100  log << (log10(difnorm[interval])-log10(difnorm[interval-1]))/
101  (log10((float)interval)-log10(interval-1.0)) << std::endl;
102  }
103 
104  }
105 
106  log.flush();
107 }
108 
109 /*
110 
111  templates for the interface a TPZCheckConvergence class must specify
113  int NumCasesCheckConv() const;
115  void TangentCheckConv(TPZFMatrix<STATE> &state, TPZFMatrix<STATE> &tangent, int icase);
117  void ResidualCheckConv(TPZFMatrix<STATE> &state, TPZFMatrix<STATE> &residual, int icase);
118  */
119 
127 template<class TConv>
128 void TPZCheckConvergence(TConv &obj, TPZFMatrix<STATE> &state, TPZFMatrix<STATE> &range, bool direction_range = false) {
129 
130  TPZFMatrix<STATE> incval(range);
131  int numrows;
132 
133  numrows = state.Rows();
134  REAL randnum = (rand()&1000)/999.;
135  for(int i=0; i<numrows; i++) {
136  incval(i,0) = range(i,0)*(STATE)randnum;
137  if(!direction_range) randnum = (rand()&1000)/999.;
138  }
139 
140  std::ofstream log("conv.log");
141 
142  int icase;
143 
144  int numcases = obj.NumCasesCheckConv();
145 
146 
147 
148  for(icase = 0; icase < numcases; icase++) {
149 
150  TPZFMatrix<STATE> ReferenceResidual, Tangent, EstimateRes;
151 
152  obj.TangentCheckConv(state, Tangent,icase);
153 
154  // Tangent.Print("tangent matrix");
155  obj.ResidualCheckConv(state, ReferenceResidual,icase);
156 
157  // ReferenceResidual.Print("reference residual");
158  EstimateRes.Redim(ReferenceResidual.Rows(),ReferenceResidual.Cols());
159  Tangent.Multiply(incval,EstimateRes);
160 
161  REAL difnorm[10] = {0.};
162 
163  for(int interval = 1; interval < 10; interval++)
164  {
165 
166  TPZFMatrix<STATE> actualstate(state);
167  TPZFMatrix<STATE> residual;
168 
169  for(int i=0; i<numrows; i++)
170  {
171  actualstate(i) += (STATE)(interval/10.)*(STATE)incval(i,0);
172  }
173 
174  obj.ResidualCheckConv(actualstate, residual,icase);
175  // residual.Print("residual");
176  residual -= ReferenceResidual;
177  // resnorm[interval] = Norm(residual);
178  residual = residual - EstimateRes*(STATE(interval/10.));
179  // residual.Print("residual adaptado");
180  difnorm[interval] = Norm(residual);
181  std::cout << "difnorm = " << difnorm[interval];
182 
183  }
184 
185  std::cout << "icase = " << icase << std::endl;
186  log << "icase = " << icase << std::endl;
187 
188  for(int interval = 2; interval<10; interval++) {
189 
190  if(fabs(difnorm[interval]) < REAL(1.e-12) || fabs(difnorm[interval-1]) <REAL(1.e-12)) {
191  std::cout << "residual too small\n";
192  log << "residual too small\n";
193  break;
194  }
195  std::cout << (log10(difnorm[interval])-log10(difnorm[interval-1]))/
196 
197  (log10((REAL)interval)-log10(interval-1.0)) << std::endl;
198  log << (log10(difnorm[interval])-log10(difnorm[interval-1]))/
199  (log10((REAL)interval)-log10(interval-1.0)) << std::endl;
200  }
201 
202  }
203  log.flush();
204 }
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
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ fabs
Definition: tfadfunc.h:140
virtual void Residual(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &rhs, TPZFMatrix< TVar > &res)
Computes res = rhs - this * x.
Definition: pzmatrix.h:827
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
Contains TPZMatrixclass which implements full matrix (using column major representation).
void TPZCheckConvergence(TConv &obj, TPZFMatrix< STATE > &state, TPZFMatrix< STATE > &range, bool direction_range=false)
Definition: checkconv.h:128
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
Definition: pzfmatrix.h:616
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ log
Definition: tfadfunc.h:130
virtual void Multiply(const TPZFMatrix< TVar > &A, TPZFMatrix< TVar > &res, int opt=0) const
It mutiplies itself by TPZMatrix<TVar>A putting the result in res.
Definition: pzmatrix.cpp:1916
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ log10
Definition: tfadfunc.h:135
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
obj
Definition: test.py:225