MGCL V10  V10
MGCL V10
 全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 フレンド グループ ページ
defint.h
1 /********************************************************************/
2 /* Copyright (c) 2015 DG Technologies Inc. and Yuzi Mizuno */
3 /* All rights reserved. */
4 /********************************************************************/
5 #ifndef _MGDefint_HH_
6 #define _MGDefint_HH_
7 
12 #include <math.h>
13 #include "mg/defintArea.h"
14 
23 template<class func>
24 double mgDefint(
25  func& f,
26  double a,
28  double b,
29  double eps,
30  int l=0
31 ){
38  if(l!=0 && l!=1)
39  return 0.;
40 
41  double d;
42  int i;
43  double wm, wp, fac;
44 
45  MGDefintArea::instance().init();//Check if defintArea is initialized.
46  int npow=MGDefintArea::instance().m_npow, nend=MGDefintArea::instance().m_nend;
47  double *a0=MGDefintArea::instance().m_a0,
48  *am=MGDefintArea::instance().m_am,
49  *ap=MGDefintArea::instance().m_ap;
50  double b0=MGDefintArea::instance().m_b0, *bb=MGDefintArea::instance().m_bb;
51  double eps0=MGDefintArea::instance().m_eps0;
52 
53  double shfm, shfp;
54  fac = (b-a)*.5;
55  if(l == 0){
56  shfm = (b+a)*.5;
57  shfp = shfm;
58  }else{
59  shfm = 0.;
60  shfp = 0.;
61  }
62 
63  const int lp1 = l + 1;
64  const int id=lp1*mgdefintlen-(mgdefintlen+1);
65  double epsv;
66  eps=fabs(eps);
67  if(eps>=eps0)
68  epsv = eps;
69  else epsv = eps0;
70  double epsq = sqrt(epsv) * .2;
71 
72  d = a0[l] * fac + shfp;
73  double vnew = f(d) * b0;
74 
75 /* ---- initial step ---- */
76 /* integrate with mesh size = 0.5 and check decay of integrand */
77  double h = .5;
78  int is=1; is=is<<npow;
79  int im = is;
80  int km=0, kp=0, nm=0, np=0;
81  for(i=is; im<0 ? i>=nend : i<=nend; i+=im){
82 
83  if(km<=1){
84  d = am[i+id]*fac + shfm;
85  wm = f(d) * bb[i-1];
86  vnew += wm;
87  if(fabs(wm)<=epsv){
88  ++km;
89  if (km >= 2) nm = i - im;
90  }else
91  km = 0;
92  }
93  if (kp<=1) {
94  d = ap[i+id]*fac + shfp;
95  wp = f(d) * bb[i-1];
96  vnew += wp;
97  if(fabs(wp)<=epsv){
98  ++kp;
99  if (kp>=2) np=i-im;
100  }else
101  kp = 0;
102  }
103  if(km==2 && kp==2)
104  break;
105 
106  }
107 
108  double epsm = 0., epsp = 0.;
109  if (nm == 0) {
110  nm = nend;
111  epsm = sqrt(fabs(wm));
112  }
113 
114  if(np == 0){
115  np = nend;
116  epsp = sqrt(fabs(wp));
117  }
118 
119  if (epsq<epsm)
120  epsq = epsm;
121  if (epsq<epsp)
122  epsq = epsp;
123 
124 /* ---- general step ---- */
125  double vold = h * fac * vnew;
126  for(int mstep = 1; mstep<=npow; ++mstep){
127  vnew = 0.;
128  int ih = is;
129  is /= 2;
130 
131  for(i=is; ih<0 ? i>=nm : i<=nm; i+=ih) {
132  d = am[i+id] * fac + shfm;
133  vnew += f(d) * bb[i - 1];
134  }
135 
136  for(i=is; ih<0 ? i>=np : i<=np; i+=ih) {
137  d = ap[i+id] * fac + shfp;
138  vnew += f(d) * bb[i-1];
139  }
140 
141  vnew = (vold + h*fac*vnew) * .5;
142  if((d=vnew-vold,fabs(d))<epsq)
143  return vnew;//converged and return
144 
145  h *= .5;
146  vold = vnew;
147  }
148  return vnew;
149 }
150  // end of ALGORITHM group
152 
153 #endif
double mgDefint(func &f, double a, double b, double eps, int l=0)
Integrate f(x)(l=0) or F(x)(l=1, see below) over finite interval (a,b). The DE formula (double expone...
Definition: defint.h:24