Code-Eli  0.3.6
d1o1.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_mutil_fd_d1o1_hpp
14 #define eli_mutil_fd_d1o1_hpp
15 
16 #include <vector>
17 
18 #include "eli/code_eli.hpp"
19 
20 namespace eli
21 {
22  namespace mutil
23  {
24  namespace fd
25  {
26  template<typename data__>
27  class d1o1
28  {
29  public:
30  enum stencil
31  {
32  LEFT=0,
34  };
35 
36  private:
37  const size_t nnodes;
38  const int n_order;
40 
41  protected:
42  template<typename itc__, typename itphi__> data__ calculate_dot(itc__ a, itphi__ itphi) const
43  {
44  data__ d(static_cast<data__>(0));
45 
46  for (size_t i=0; i<number_nodes(); ++i, ++itphi)
47  d+=a[i]*(*itphi);
48 
49  return d;
50  }
51 
52  public:
53  d1o1() : nnodes(2), n_order(1), st(LEFT)
54  {
55  }
56 
57  d1o1(const stencil &s) : nnodes(2), n_order(1), st(s)
58  {
59  }
60 
61  d1o1(const d1o1<data__> &d) : nnodes(2), n_order(1), st(d.st)
62  {
63  }
64 
66  {
67  }
68 
69  void set_stencil(const stencil &s)
70  {
71  st=s;
72  }
73 
74  const stencil & get_stencil() const
75  {
76  return st;
77  }
78 
79  int order(bool /*uniform*/) const
80  {
81  return n_order;
82  }
83 
84  size_t number_nodes() const
85  {
86  return nnodes;
87  }
88 
89  template<typename iti__> std::ptrdiff_t index(iti__ iti) const
90  {
91  size_t i0;
92 
93  switch(st)
94  {
95  case(LEFT):
96  {
97  i0=1;
98  (*iti)=-1;++iti;
99  (*iti)=0;
100  break;
101  }
102  case(RIGHT):
103  {
104  i0=0;
105  (*iti)=0;++iti;
106  (*iti)=1;
107  break;
108  }
109  default:
110  {
111  i0=3;
112  assert(false);
113  return i0;
114  }
115  }
116 
117  return i0;
118  }
119 
120  template<typename itphi__> int evaluate(data__ &d, itphi__ itphi, const data__ &dx) const
121  {
122  std::vector<data__> a(number_nodes());
123  int rtn;
124 
125  rtn=coefficients(a.begin(), dx);
126  if (rtn==0)
127  d=calculate_dot(a.begin(), itphi);
128 
129  return rtn;
130  }
131 
132  template<typename itphi__, typename itx__> int evaluate(data__ &d, itphi__ itphi, itx__ itx) const
133  {
134  std::vector<data__> a(number_nodes());
135  int rtn;
136 
137  rtn=coefficients(a.begin(), itx);
138  if (rtn==0)
139  d=calculate_dot(a.begin(), itphi);
140 
141  return rtn;
142  }
143 
144  template<typename itc__> int coefficients(itc__ itc, const data__ &dx) const
145  {
146  switch(st)
147  {
148  case(RIGHT):
149  case(LEFT):
150  {
151  (*itc)=static_cast<data__>(-1.0)/dx;++itc;
152  (*itc)=static_cast<data__>( 1.0)/dx;
153 
154  break;
155  }
156  default:
157  {
158  assert(false);
159  return -1;
160  }
161  }
162 
163  return 0;
164  }
165 
166  template<typename itc__, typename itx__> int coefficients(itc__ itc, itx__ itx) const
167  {
168  std::vector<data__> x(number_nodes());
169  data__ dx;
170 
171  // extract the x-locations
172  x[0]=(*itx); ++itx;
173  x[1]=(*itx);
174  dx=x[1]-x[0];
175 
176  return coefficients(itc, dx);
177  }
178 
179  int truncation_error(data__ &te, const data__ &phi2, const data__ &dx) const
180  {
181  switch(st)
182  {
183  case(LEFT):
184  {
185  te=phi2*dx/2;
186  break;
187  }
188  case(RIGHT):
189  {
190  te=-phi2*dx/2;
191  break;
192  }
193  default:
194  {
195  assert(false);
196  return -1;
197  }
198  }
199 
200  return 0;
201  }
202 
203  template<typename itx__> int truncation_error(data__ &te, const data__ &phi2, itx__ itx) const
204  {
205  std::vector<data__> x(number_nodes());
206  data__ dx;
207 
208  // extract the x-locations
209  x[0]=(*itx); ++itx;
210  x[1]=(*itx);
211  dx=x[1]-x[0];
212 
213  return truncation_error(te, phi2, dx);
214  }
215  };
216  }
217  }
218 }
219 
220 #endif
Definition: d1o1.hpp:32
int coefficients(itc__ itc, itx__ itx) const
Definition: d1o1.hpp:166
Definition: math.hpp:20
~d1o1()
Definition: d1o1.hpp:65
size_t number_nodes() const
Definition: d1o1.hpp:84
int truncation_error(data__ &te, const data__ &phi2, const data__ &dx) const
Definition: d1o1.hpp:179
d1o1(const stencil &s)
Definition: d1o1.hpp:57
const int n_order
Definition: d1o1.hpp:38
Definition: d1o1.hpp:33
int coefficients(itc__ itc, const data__ &dx) const
Definition: d1o1.hpp:144
stencil
Definition: d1o1.hpp:30
d1o1(const d1o1< data__ > &d)
Definition: d1o1.hpp:61
int evaluate(data__ &d, itphi__ itphi, itx__ itx) const
Definition: d1o1.hpp:132
int evaluate(data__ &d, itphi__ itphi, const data__ &dx) const
Definition: d1o1.hpp:120
d1o1()
Definition: d1o1.hpp:53
stencil st
Definition: d1o1.hpp:39
const size_t nnodes
Definition: d1o1.hpp:37
const stencil & get_stencil() const
Definition: d1o1.hpp:74
int order(bool) const
Definition: d1o1.hpp:79
data__ calculate_dot(itc__ a, itphi__ itphi) const
Definition: d1o1.hpp:42
int truncation_error(data__ &te, const data__ &phi2, itx__ itx) const
Definition: d1o1.hpp:203
void set_stencil(const stencil &s)
Definition: d1o1.hpp:69
Definition: d1o1.hpp:27
std::ptrdiff_t index(iti__ iti) const
Definition: d1o1.hpp:89