Dune Core Modules (2.6.0)

pk2dlocalbasis.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_PK2DLOCALBASIS_HH
4 #define DUNE_PK2DLOCALBASIS_HH
5 
6 #include <numeric>
7 
8 #include <dune/common/fmatrix.hh>
9 
10 #include <dune/localfunctions/common/localbasis.hh>
11 
12 namespace Dune
13 {
26  template<class D, class R, unsigned int k>
28  {
29  public:
30 
32  enum {N = (k+1)*(k+2)/2};
33 
37  enum {O = k};
38 
41 
44  {
45  for (unsigned int i=0; i<=k; i++)
46  pos_[i] = (1.0*i)/std::max(k,(unsigned int)1);
47  }
48 
50  unsigned int size () const
51  {
52  return N;
53  }
54 
56  inline void evaluateFunction (const typename Traits::DomainType& x,
57  std::vector<typename Traits::RangeType>& out) const
58  {
59  out.resize(N);
60  // specialization for k==0, not clear whether that is needed
61  if (k==0) {
62  out[0] = 1;
63  return;
64  }
65 
66  int n=0;
67  for (unsigned int j=0; j<=k; j++)
68  for (unsigned int i=0; i<=k-j; i++)
69  {
70  out[n] = 1.0;
71  for (unsigned int alpha=0; alpha<i; alpha++)
72  out[n] *= (x[0]-pos_[alpha])/(pos_[i]-pos_[alpha]);
73  for (unsigned int beta=0; beta<j; beta++)
74  out[n] *= (x[1]-pos_[beta])/(pos_[j]-pos_[beta]);
75  for (unsigned int gamma=i+j+1; gamma<=k; gamma++)
76  out[n] *= (pos_[gamma]-x[0]-x[1])/(pos_[gamma]-pos_[i]-pos_[j]);
77  n++;
78  }
79  }
80 
82  inline void
83  evaluateJacobian (const typename Traits::DomainType& x, // position
84  std::vector<typename Traits::JacobianType>& out) const // return value
85  {
86  out.resize(N);
87 
88  // specialization for k==0, not clear whether that is needed
89  if (k==0) {
90  out[0][0][0] = 0; out[0][0][1] = 0;
91  return;
92  }
93 
94  int n=0;
95  for (unsigned int j=0; j<=k; j++)
96  for (unsigned int i=0; i<=k-j; i++)
97  {
98  // x_0 derivative
99  out[n][0][0] = 0.0;
100  R factor=1.0;
101  for (unsigned int beta=0; beta<j; beta++)
102  factor *= (x[1]-pos_[beta])/(pos_[j]-pos_[beta]);
103  for (unsigned int a=0; a<i; a++)
104  {
105  R product=factor;
106  for (unsigned int alpha=0; alpha<i; alpha++)
107  if (alpha==a)
108  product *= D(1)/(pos_[i]-pos_[alpha]);
109  else
110  product *= (x[0]-pos_[alpha])/(pos_[i]-pos_[alpha]);
111  for (unsigned int gamma=i+j+1; gamma<=k; gamma++)
112  product *= (pos_[gamma]-x[0]-x[1])/(pos_[gamma]-pos_[i]-pos_[j]);
113  out[n][0][0] += product;
114  }
115  for (unsigned int c=i+j+1; c<=k; c++)
116  {
117  R product=factor;
118  for (unsigned int alpha=0; alpha<i; alpha++)
119  product *= (x[0]-pos_[alpha])/(pos_[i]-pos_[alpha]);
120  for (unsigned int gamma=i+j+1; gamma<=k; gamma++)
121  if (gamma==c)
122  product *= -D(1)/(pos_[gamma]-pos_[i]-pos_[j]);
123  else
124  product *= (pos_[gamma]-x[0]-x[1])/(pos_[gamma]-pos_[i]-pos_[j]);
125  out[n][0][0] += product;
126  }
127 
128  // x_1 derivative
129  out[n][0][1] = 0.0;
130  factor = 1.0;
131  for (unsigned int alpha=0; alpha<i; alpha++)
132  factor *= (x[0]-pos_[alpha])/(pos_[i]-pos_[alpha]);
133  for (unsigned int b=0; b<j; b++)
134  {
135  R product=factor;
136  for (unsigned int beta=0; beta<j; beta++)
137  if (beta==b)
138  product *= D(1)/(pos_[j]-pos_[beta]);
139  else
140  product *= (x[1]-pos_[beta])/(pos_[j]-pos_[beta]);
141  for (unsigned int gamma=i+j+1; gamma<=k; gamma++)
142  product *= (pos_[gamma]-x[0]-x[1])/(pos_[gamma]-pos_[i]-pos_[j]);
143  out[n][0][1] += product;
144  }
145  for (unsigned int c=i+j+1; c<=k; c++)
146  {
147  R product=factor;
148  for (unsigned int beta=0; beta<j; beta++)
149  product *= (x[1]-pos_[beta])/(pos_[j]-pos_[beta]);
150  for (unsigned int gamma=i+j+1; gamma<=k; gamma++)
151  if (gamma==c)
152  product *= -D(1)/(pos_[gamma]-pos_[i]-pos_[j]);
153  else
154  product *= (pos_[gamma]-x[0]-x[1])/(pos_[gamma]-pos_[i]-pos_[j]);
155  out[n][0][1] += product;
156  }
157 
158  n++;
159  }
160 
161  }
162 
168  void partial(const std::array<unsigned int,2>& order,
169  const typename Traits::DomainType& in,
170  std::vector<typename Traits::RangeType>& out) const
171  {
172  auto totalOrder = std::accumulate(order.begin(), order.end(), 0);
173 
174  switch (totalOrder)
175  {
176  case 0:
177  evaluateFunction(in,out);
178  break;
179  case 1:
180  {
181  int direction = std::find(order.begin(), order.end(), 1)-order.begin();
182 
183  out.resize(N);
184 
185  int n=0;
186  for (unsigned int j=0; j<=k; j++)
187  {
188  for (unsigned int i=0; i<=k-j; i++, n++)
189  {
190  out[n] = 0.0;
191  for (unsigned int no1=0; no1 < k; no1++)
192  {
193  R factor = lagrangianFactorDerivative(direction, no1, i, j, in);
194  for (unsigned int no2=0; no2 < k; no2++)
195  if (no1 != no2)
196  factor *= lagrangianFactor(no2, i, j, in);
197 
198  out[n] += factor;
199  }
200  }
201  }
202 
203  break;
204  }
205  case 2:
206  {
207  out.resize(N);
208 
209  // specialization for k<2, not clear whether that is needed
210  if (k<2)
211  {
212  std::fill(out.begin(), out.end(), 0.0);
213  return;
214  }
215 
216  std::array<int,2> directions;
217  unsigned int counter = 0;
218  auto nonconstOrder = order; // need a copy that I can modify
219  for (int i=0; i<2; i++)
220  {
221  while (nonconstOrder[i])
222  {
223  directions[counter++] = i;
224  nonconstOrder[i]--;
225  }
226  }
227 
228  //f = prod_{i} f_i -> dxa dxb f = sum_{i} {dxa f_i sum_{k \neq i} dxb f_k prod_{l \neq k,i} f_l
229  int n=0;
230  for (unsigned int j=0; j<=k; j++)
231  {
232  for (unsigned int i=0; i<=k-j; i++, n++)
233  {
234  R res = 0.0;
235 
236  for (unsigned int no1=0; no1 < k; no1++)
237  {
238  R factor1 = lagrangianFactorDerivative(directions[0], no1, i, j, in);
239  for (unsigned int no2=0; no2 < k; no2++)
240  {
241  if (no1 == no2)
242  continue;
243  R factor2 = factor1*lagrangianFactorDerivative(directions[1], no2, i, j, in);
244  for (unsigned int no3=0; no3 < k; no3++)
245  {
246  if (no3 == no1 || no3 == no2)
247  continue;
248  factor2 *= lagrangianFactor(no3, i, j, in);
249  }
250  res += factor2;
251  }
252  }
253  out[n] = res;
254  }
255  }
256 
257  break;
258  }
259  default:
260  DUNE_THROW(NotImplemented, "Desired derivative order is not implemented");
261  }
262  }
263 
265  unsigned int order () const
266  {
267  return k;
268  }
269 
270  private:
272  typename Traits::RangeType lagrangianFactor(const int no, const int i, const int j, const typename Traits::DomainType& x) const
273  {
274  if ( no < i)
275  return (x[0]-pos_[no])/(pos_[i]-pos_[no]);
276  if (no < i+j)
277  return (x[1]-pos_[no-i])/(pos_[j]-pos_[no-i]);
278  return (pos_[no+1]-x[0]-x[1])/(pos_[no+1]-pos_[i]-pos_[j]);
279  }
280 
284  typename Traits::RangeType lagrangianFactorDerivative(const int direction, const int no, const int i, const int j, const typename Traits::DomainType& x) const
285  {
286  if ( no < i)
287  return (direction == 0) ? 1.0/(pos_[i]-pos_[no]) : 0;
288 
289  if (no < i+j)
290  return (direction == 0) ? 0: 1.0/(pos_[j]-pos_[no-i]);
291 
292  return -1.0/(pos_[no+1]-pos_[i]-pos_[j]);
293  }
294 
295  D pos_[k+1]; // positions on the interval
296  };
297 
298 }
299 #endif
A dense n x m matrix.
Definition: fmatrix.hh:68
vector space out of a tensor product of fields.
Definition: fvector.hh:93
Default exception for dummy implementations.
Definition: exceptions.hh:261
Lagrange shape functions of arbitrary order on the reference triangle.
Definition: pk2dlocalbasis.hh:28
void evaluateFunction(const typename Traits::DomainType &x, std::vector< typename Traits::RangeType > &out) const
Evaluate all shape functions.
Definition: pk2dlocalbasis.hh:56
unsigned int size() const
number of shape functions
Definition: pk2dlocalbasis.hh:50
void partial(const std::array< unsigned int, 2 > &order, const typename Traits::DomainType &in, std::vector< typename Traits::RangeType > &out) const
Evaluate partial derivatives of any order of all shape functions.
Definition: pk2dlocalbasis.hh:168
void evaluateJacobian(const typename Traits::DomainType &x, std::vector< typename Traits::JacobianType > &out) const
Evaluate Jacobian of all shape functions.
Definition: pk2dlocalbasis.hh:83
Pk2DLocalBasis()
Standard constructor.
Definition: pk2dlocalbasis.hh:43
unsigned int order() const
Polynomial order of the shape functions.
Definition: pk2dlocalbasis.hh:265
Implements a matrix constructed from a given type representing a field and compile-time given number ...
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:331
Dune namespace.
Definition: alignedallocator.hh:10
Type traits for LocalBasisVirtualInterface.
Definition: localbasis.hh:32
D DomainType
domain type
Definition: localbasis.hh:43
R RangeType
range type
Definition: localbasis.hh:55
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 5, 22:29, 2024)