NeoPZ
adapt.h
Go to the documentation of this file.
1 
11 #ifndef ADAPTH
12 #define ADAPTH
13 
14 #include "pzreal.h"
15 #include "pzvec.h"
16 #include "limits"
17 
18 struct Adapt
19 {
20 
21 public:
22 
23  Adapt(REAL tol);
24 
25  void SetPrecision(REAL tol);
26 
27  template <class T>
28  REAL integrate(T &func, const REAL a, const REAL b);
29 
30  template <class T>
31  TPZVec<REAL> Vintegrate(T &func, const int dim, const REAL a, const REAL b);
32 
33 
34 
35 private:
36 
37  template <class T>
38  REAL adaptlob(T &func, const REAL a, const REAL b, const REAL fa, const REAL fb, const REAL is);
39 
40  template <class T>
41  REAL adaptlob(T &func, const int pos, const REAL a, const REAL b, const REAL fa, const REAL fb, const REAL is);
42 
43  REAL TOL,toler;
45  static const REAL alpha,beta,x1,x2,x3,x[12];
46 };
47 
48 
49 inline Adapt::Adapt(REAL tol) : TOL(tol),terminate(true),out_of_tolerance(false)
50 {
51  const REAL EPS = std::numeric_limits<REAL>::epsilon();
52  if(TOL < 10.0 * EPS)
53  {
54  TOL = 10.0 * EPS;
55  }
56 }
57 
58 inline void Adapt::SetPrecision(REAL tol)
59 {
60  TOL = tol;
61 }
62 
63 template <class T>
64 inline REAL Adapt::integrate(T &func, const REAL a, const REAL b)
65 {
66  REAL m,h,fa,fb,i1,i2,is,erri1,erri2,r,y[13];
67  m = 0.5 * (a + b);
68  h = 0.5 * (b - a);
69  fa = y[0] = func(a);
70  fb = y[12] = func(b);
71  for(int i = 1; i < 12; i++ )
72  {
73  y[i] = func(m + x[i] * h);
74  }
75  i2 = (h/6.0) * (y[0] + y[12] + 5.0 * (y[4] + y[8]));
76  i1 = (h/1470.0) * (77.0 * (y[0] + y[12]) + 432.0 * (y[2] + y[10]) + 625.0 * (y[4] + y[8]) + 672.0 * y[6]);
77  is = h * (0.0158271919734802 * (y[0] + y[12]) + 0.0942738402188500 * (y[1] + y[11]) + 0.155071987336585 * (y[2] + y[10]) +
78  0.188821573960182 * (y[3] + y[9]) + 0.199773405226859 * (y[4] + y[8]) + 0.224926465333340 * (y[5] + y[7]) + 0.242611071901408 * y[6]);
79  erri1 = std::abs(i1 - is);
80  erri2 = std::abs(i2 - is);
81  r = (erri2 != 0.0) ? erri1/erri2 : 1.0;
82  toler = (r > 0.0 && r < 1.0) ? TOL/r : TOL;
83  if(is == 0.0)
84  {
85  is = b-a;
86  }
87  is = std::abs(is);
88 
89  REAL answ = adaptlob(func,a,b,fa,fb,is);
90 
91  return answ;
92 }
93 
94 template <class T>
95 inline TPZVec<REAL> Adapt::Vintegrate(T &func, const int dim, const REAL a, const REAL b)
96 {
97  TPZVec<REAL> Vansw(dim);
98 
99  TPZVec< TPZVec<REAL> > FUNCTy(13);
100 
101  REAL m,h,fa,fb,i1,i2,is,erri1,erri2,r,y[13];
102  m = 0.5 * (a + b);
103  h = 0.5 * (b - a);
104 
105  FUNCTy[0] = func(a);
106  for(int i = 1; i < 12; i++ )
107  {
108  FUNCTy[i] = func(m + x[i] * h);
109  }
110  FUNCTy[12] = func(b);
111 
112  for(int d = 0; d < dim; d++)
113  {
114  fa = y[0] = FUNCTy[0][d];
115  fb = y[12] = FUNCTy[12][d];
116  for(int i = 1; i < 12; i++ )
117  {
118  y[i] = FUNCTy[i][d];
119  }
120  i2 = (h/6.0) * (y[0] + y[12] + 5.0 * (y[4] + y[8]));
121  i1 = (h/1470.0) * (77.0 * (y[0] + y[12]) + 432.0 * (y[2] + y[10]) + 625.0 * (y[4] + y[8]) + 672.0 * y[6]);
122  is = h * (0.0158271919734802 * (y[0] + y[12]) + 0.0942738402188500 * (y[1] + y[11]) + 0.155071987336585 * (y[2] + y[10]) +
123  0.188821573960182 * (y[3] + y[9]) + 0.199773405226859 * (y[4] + y[8]) + 0.224926465333340 * (y[5] + y[7]) + 0.242611071901408 * y[6]);
124  erri1 = std::abs(i1 - is);
125  erri2 = std::abs(i2 - is);
126  r = (erri2 != 0.0) ? erri1/erri2 : 1.0;
127  toler = (r > 0.0 && r < 1.0) ? TOL/r : TOL;
128  if(is == 0.0)
129  {
130  is = b-a;
131  }
132  is = std::abs(is);
133 
134  REAL answ = adaptlob(func,d,a,b,fa,fb,is);
135 
136  Vansw[d] = answ;
137  }
138 
139  return Vansw;
140 }
141 
142 template <class T>
143 inline REAL Adapt::adaptlob(T &func, const REAL a, const REAL b, const REAL fa, const REAL fb, const REAL is)
144 {
145  REAL m,h,mll,ml,mr,mrr,fmll,fml,fm,fmrr,fmr,i1,i2;
146  m = 0.5 * (a + b);
147  h = 0.5 * (b-a);
148  mll = m-alpha * h;
149  ml = m-beta * h;
150  mr = m + beta * h;
151  mrr = m + alpha * h;
152  fmll = func(mll);
153  fml = func(ml);
154  fm = func(m);
155  fmr = func(mr);
156  fmrr = func(mrr);
157  i2 = h/6.0 * (fa + fb + 5.0 * (fml + fmr));
158  i1 = h/1470.0 * (77.0 * (fa + fb) + 432.0 * (fmll + fmrr) + 625.0 * (fml + fmr) + 672.0 * fm);
159  if(std::abs(i1 - i2) <= toler * is || mll <= a || b <= mrr)
160  {
161  if((mll <= a || b <= mrr) && terminate)
162  {
163  out_of_tolerance = true;
164  terminate = false;
165  }
166  return i1;
167  }
168  else
169  {
170  REAL val = adaptlob(func,a,mll,fa,fmll,is) + adaptlob(func,mll,ml,fmll,fml,is) + adaptlob(func,ml,m,fml,fm,is) +
171  adaptlob(func,m,mr,fm,fmr,is) + adaptlob(func,mr,mrr,fmr,fmrr,is) + adaptlob(func,mrr,b,fmrr,fb,is);
172 
173  return val;
174  }
175 }
176 
177 template <class T>
178 inline REAL Adapt::adaptlob(T &func, const int pos, const REAL a, const REAL b, const REAL fa, const REAL fb, const REAL is)
179 {
180  REAL m,h,mll,ml,mr,mrr,fmll,fml,fm,fmrr,fmr,i1,i2;
181  m = 0.5 * (a + b);
182  h = 0.5 * (b-a);
183  mll = m-alpha * h;
184  ml = m-beta * h;
185  mr = m + beta * h;
186  mrr = m + alpha * h;
187  fmll = func(mll)[pos];
188  fml = func(ml)[pos];
189  fm = func(m)[pos];
190  fmr = func(mr)[pos];
191  fmrr = func(mrr)[pos];
192  i2 = h/6.0 * (fa + fb + 5.0 * (fml + fmr));
193  i1 = h/1470.0 * (77.0 * (fa + fb) + 432.0 * (fmll + fmrr) + 625.0 * (fml + fmr) + 672.0 * fm);
194  if(std::abs(i1 - i2) <= toler * is || mll <= a || b <= mrr)
195  {
196  if((mll <= a || b <= mrr) && terminate)
197  {
198  out_of_tolerance = true;
199  terminate = false;
200  }
201  return i1;
202  }
203  else
204  {
205  REAL val = adaptlob(func,pos,a,mll,fa,fmll,is) + adaptlob(func,pos,mll,ml,fmll,fml,is) + adaptlob(func,pos,ml,m,fml,fm,is) +
206  adaptlob(func,pos,m,mr,fm,fmr,is) + adaptlob(func,pos,mr,mrr,fmr,fmrr,is) + adaptlob(func,pos,mrr,b,fmrr,fb,is);
207 
208  return val;
209  }
210 }
211 
212 
213 #endif
REAL adaptlob(T &func, const REAL a, const REAL b, const REAL fa, const REAL fb, const REAL is)
Definition: adapt.h:143
static const REAL x3
Definition: adapt.h:45
REAL integrate(T &func, const REAL a, const REAL b)
Definition: adapt.h:64
Adapt(REAL tol)
Definition: adapt.h:49
Templated vector implementation.
TPZVec< REAL > Vintegrate(T &func, const int dim, const REAL a, const REAL b)
Definition: adapt.h:95
static const REAL x2
Definition: adapt.h:45
clarg::argBool h("-h", "help message", false)
TinyFad< 8, T > abs(const TinyFad< 8, T > &in)
Definition: tinyfadeight.h:846
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
REAL TOL
Definition: adapt.h:43
static const REAL x1
Definition: adapt.h:45
static const double tol
Definition: pzgeoprism.cpp:23
static const REAL x[12]
Definition: adapt.h:45
static const REAL alpha
Definition: adapt.h:45
bool out_of_tolerance
Definition: adapt.h:44
REAL toler
Definition: adapt.h:43
Definition: adapt.h:18
bool terminate
Definition: adapt.h:44
void SetPrecision(REAL tol)
Definition: adapt.h:58
Contains the declaration of TPZFlopCounter class and TPZCounter struct.
static const REAL beta
Definition: adapt.h:45
clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt")