dune-pdelab  2.4.1
maxwelldg.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil -*-
2 #ifndef DUNE_PDELAB_MAXWELLDG_HH
3 #define DUNE_PDELAB_MAXWELLDG_HH
4 
5 #include<vector>
6 
7 #include<dune/common/exceptions.hh>
8 #include<dune/common/fvector.hh>
9 
10 #include <dune/geometry/referenceelements.hh>
11 
21 
22 #include"maxwellparameter.hh"
23 
24 namespace Dune {
25  namespace PDELab {
26 
27 
28  template<int dim>
30  {};
31 
37  template<>
39  {
40  enum { dim = 3 };
41  public:
42 
52  template<typename T1, typename T2, typename T3>
53  static void eigenvalues (T1 eps, T1 mu, const Dune::FieldVector<T2,2*dim>& e)
54  {
55  T1 s = 1.0/sqrt(mu*eps); //speed of light s = 1/sqrt(\mu \epsilon)
56  e[0] = s;
57  e[1] = s;
58  e[2] = -s;
59  e[3] = -s;
60  e[4] = 0;
61  e[5] = 0;
62  }
63 
75  template<typename T1, typename T2, typename T3>
76  static void eigenvectors (T1 eps, T1 mu, const Dune::FieldVector<T2,dim>& n, Dune::FieldMatrix<T3,2*dim,2*dim>& R)
77  {
78  T1 a=n[0], b=n[1], c=n[2];
79 
80  Dune::FieldVector<T2,dim> re, im;
81  if (std::abs(c)<0.5)
82  {
83  re[0]=a*c; re[1]=b*c; re[2]=c*c-1;
84  im[0]=-b; im[1]=a; im[2]=0;
85  }
86  else
87  {
88  re[0]=a*b; re[1]=b*b-1; re[2]=b*c;
89  im[0]=c; im[1]=0.0; im[2]=-a;
90  }
91 
92  // \lambda_0,1 = s
93  R[0][0] = re[0]; R[0][1] = -im[0];
94  R[1][0] = re[1]; R[1][1] = -im[1];
95  R[2][0] = re[2]; R[2][1] = -im[2];
96  R[3][0] = im[0]; R[3][1] = re[0];
97  R[4][0] = im[1]; R[4][1] = re[1];
98  R[5][0] = im[2]; R[5][1] = re[2];
99 
100  // \lambda_2,3 = -s
101  R[0][2] = im[0]; R[0][3] = re[0];
102  R[1][2] = im[1]; R[1][3] = re[1];
103  R[2][2] = im[2]; R[2][3] = re[2];
104  R[3][2] = re[0]; R[3][3] = -im[0];
105  R[4][2] = re[1]; R[4][3] = -im[1];
106  R[5][2] = re[2]; R[5][3] = -im[2];
107 
108  // \lambda_4,5 = 0
109  R[0][4] = a; R[0][5] = 0;
110  R[1][4] = b; R[1][5] = 0;
111  R[2][4] = c; R[2][5] = 0;
112  R[3][4] = 0; R[3][5] = a;
113  R[4][4] = 0; R[4][5] = b;
114  R[5][4] = 0; R[5][5] = c;
115 
116  // apply scaling
117  T1 weps=sqrt(eps);
118  T1 wmu=sqrt(mu);
119  for (std::size_t i=0; i<3; i++)
120  for (std::size_t j=0; j<6; j++)
121  R[i][j] *= weps;
122  for (std::size_t i=3; i<6; i++)
123  for (std::size_t j=0; j<6; j++)
124  R[i][j] *= wmu;
125 
126  return;
127 
128  // if (std::abs(std::abs(c)-1)<1e-10)
129  // {
130  // if (c>0)
131  // {
132  // // \lambda_0,1 = s
133  // R[0][0] = 0; R[0][1] = 1;
134  // R[1][0] = -1; R[1][1] = 0;
135  // R[2][0] = 0; R[2][1] = 0;
136  // R[3][0] = 1; R[3][1] = 0;
137  // R[4][0] = 0; R[4][1] = 1;
138  // R[5][0] = 0; R[5][1] = 0;
139 
140  // // \lambda_2,3 = -s
141  // R[0][2] = -1; R[0][3] = 0;
142  // R[1][2] = 0; R[1][3] = 1;
143  // R[2][2] = 0; R[2][3] = 0;
144  // R[3][2] = 0; R[3][3] = 1;
145  // R[4][2] = 1; R[4][3] = 0;
146  // R[5][2] = 0; R[5][3] = 0;
147  // }
148  // else
149  // {
150  // // \lambda_0,1 = s
151  // R[0][0] = -1; R[0][1] = 0;
152  // R[1][0] = 0; R[1][1] = 1;
153  // R[2][0] = 0; R[2][1] = 0;
154  // R[3][0] = 0; R[3][1] = 1;
155  // R[4][0] = 1; R[4][1] = 0;
156  // R[5][0] = 0; R[5][1] = 0;
157 
158  // // \lambda_2,3 = -s
159  // R[0][2] = 0; R[0][3] = 1;
160  // R[1][2] = -1; R[1][3] = 0;
161  // R[2][2] = 0; R[2][3] = 0;
162  // R[3][2] = 1; R[3][3] = 0;
163  // R[4][2] = 0; R[4][3] = 1;
164  // R[5][2] = 0; R[5][3] = 0;
165  // }
166  // }
167  // else if (std::abs(std::abs(b)-1)<1e-10)
168  // {
169  // if (b>0)
170  // {
171  // // \lambda_0,1 = s
172  // R[0][0] = -1; R[0][1] = 0;
173  // R[1][0] = 0; R[1][1] = 0;
174  // R[2][0] = 0; R[2][1] = 1;
175  // R[3][0] = 0; R[3][1] = 1;
176  // R[4][0] = 0; R[4][1] = 0;
177  // R[5][0] = 1; R[5][1] = 0;
178 
179  // // \lambda_2,3 = -s
180  // R[0][2] = 0; R[0][3] = 1;
181  // R[1][2] = 0; R[1][3] = 0;
182  // R[2][2] = -1; R[2][3] = 0;
183  // R[3][2] = 1; R[3][3] = 0;
184  // R[4][2] = 0; R[4][3] = 0;
185  // R[5][2] = 0; R[5][3] = 1;
186  // }
187  // else
188  // {
189  // // \lambda_0,1 = s
190  // R[0][0] = 0; R[0][1] = 1;
191  // R[1][0] = 0; R[1][1] = 0;
192  // R[2][0] = -1; R[2][1] = 0;
193  // R[3][0] = 1; R[3][1] = 0;
194  // R[4][0] = 0; R[4][1] = 0;
195  // R[5][0] = 0; R[5][1] = 1;
196 
197  // // \lambda_2,3 = -s
198  // R[0][2] = -1; R[0][3] = 0;
199  // R[1][2] = 0; R[1][3] = 0;
200  // R[2][2] = 0; R[2][3] = 1;
201  // R[3][2] = 0; R[3][3] = 1;
202  // R[4][2] = 0; R[4][3] = 0;
203  // R[5][2] = 1; R[5][3] = 0;
204  // }
205  // }
206  // else if (std::abs(std::abs(a)-1)<1e-10)
207  // {
208  // if (a>0)
209  // {
210  // // \lambda_0,1 = s
211  // R[0][0] = 0; R[0][1] = 0;
212  // R[1][0] = 0; R[1][1] = 1;
213  // R[2][0] = -1; R[2][1] = 0;
214  // R[3][0] = 0; R[3][1] = 0;
215  // R[4][0] = 1; R[4][1] = 0;
216  // R[5][0] = 0; R[5][1] = 1;
217 
218  // // \lambda_2,3 = -s
219  // R[0][2] = 0; R[0][3] = 0;
220  // R[1][2] = -1; R[1][3] = 0;
221  // R[2][2] = 0; R[2][3] = 1;
222  // R[3][2] = 0; R[3][3] = 0;
223  // R[4][2] = 0; R[4][3] = 1;
224  // R[5][2] = 1; R[5][3] = 0;
225  // }
226  // else
227  // {
228  // // \lambda_0,1 = s
229  // R[0][0] = 0; R[0][1] = 0;
230  // R[1][0] = -1; R[1][1] = 0;
231  // R[2][0] = 0; R[2][1] = 1;
232  // R[3][0] = 0; R[3][1] = 0;
233  // R[4][0] = 0; R[4][1] = 1;
234  // R[5][0] = 1; R[5][1] = 0;
235 
236  // // \lambda_2,3 = -s
237  // R[0][2] = 0; R[0][3] = 0;
238  // R[1][2] = 0; R[1][3] = 1;
239  // R[2][2] = -1; R[2][3] = 0;
240  // R[3][2] = 0; R[3][3] = 0;
241  // R[4][2] = 1; R[4][3] = 0;
242  // R[5][2] = 0; R[5][3] = 1;
243  // }
244  // }
245  // else
246  // {
247  // DUNE_THROW(Dune::Exception,"need axiparallel grids for now!");
248 
249  // // \lambda_0,1 = s
250  // R[0][0] = b; R[0][1] = -(b*b+c*c);
251  // R[1][0] = -a; R[1][1] = a*b;
252  // R[2][0] = 0; R[2][1] = a*c;
253  // R[3][0] = a*c; R[3][1] = 0;
254  // R[4][0] = b*c; R[4][1] = -c;
255  // R[5][0] = -(a*a+b*b); R[5][1] = b;
256 
257  // // \lambda_2,3 = -s
258  // R[0][2] = -b; R[0][3] = -(b*b+c*c);
259  // R[1][2] = a; R[1][3] = a*b;
260  // R[2][2] = 0; R[2][3] = a*c;
261  // R[3][2] = a*c; R[3][3] = 0;
262  // R[4][2] = b*c; R[4][3] = c;
263  // R[5][2] = -(a*a+b*b); R[5][3] = -b;
264  // }
265 
266  // // \lambda_4,5 = 0
267  // R[0][4] = 0; R[0][5] = a;
268  // R[1][4] = 0; R[1][5] = b;
269  // R[2][4] = 0; R[2][5] = c;
270  // R[3][4] = a; R[3][5] = 0;
271  // R[4][4] = b; R[4][5] = 0;
272  // R[5][4] = c; R[5][5] = 0;
273 
274  // // apply scaling
275  // T1 weps=sqrt(eps);
276  // T1 wmu=sqrt(mu);
277  // for (std::size_t i=0; i<3; i++)
278  // for (std::size_t j=0; j<6; j++)
279  // R[i][j] *= weps;
280  // for (std::size_t i=3; i<6; i++)
281  // for (std::size_t j=0; j<6; j++)
282  // R[i][j] *= wmu;
283 
284  //std::cout << R << std::endl;
285 
286  }
287  };
288 
303  template<typename T, typename FEM>
305  public NumericalJacobianApplyVolume<DGMaxwellSpatialOperator<T,FEM> >,
306  public NumericalJacobianVolume<DGMaxwellSpatialOperator<T,FEM> >,
307  public NumericalJacobianApplySkeleton<DGMaxwellSpatialOperator<T,FEM> >,
308  public NumericalJacobianSkeleton<DGMaxwellSpatialOperator<T,FEM> >,
309  public NumericalJacobianApplyBoundary<DGMaxwellSpatialOperator<T,FEM> >,
310  public NumericalJacobianBoundary<DGMaxwellSpatialOperator<T,FEM> >,
311  public FullSkeletonPattern,
312  public FullVolumePattern,
314  public InstationaryLocalOperatorDefaultMethods<typename T::Traits::RangeFieldType>
315  {
316  enum { dim = T::Traits::GridViewType::dimension };
317 
318  public:
319  // pattern assembly flags
320  enum { doPatternVolume = true };
321  enum { doPatternSkeleton = true };
322 
323  // residual assembly flags
324  enum { doAlphaVolume = true };
325  enum { doAlphaSkeleton = true };
326  enum { doAlphaBoundary = true };
327  enum { doLambdaVolume = true };
328 
329  // ! constructor
330  DGMaxwellSpatialOperator (T& param_, int overintegration_=0)
331  : param(param_), overintegration(overintegration_), cache(20)
332  {
333  }
334 
335  // volume integral depending on test and ansatz functions
336  template<typename EG, typename LFSU, typename X, typename LFSV, typename R>
337  void alpha_volume (const EG& eg, const LFSU& lfsu, const X& x, const LFSV& lfsv, R& r) const
338  {
339  // Define types
340  using namespace TypeTree::Indices;
341  using DGSpace = TypeTree::Child<LFSV,_0>;
342  using RF = typename DGSpace::Traits::FiniteElementType::Traits
343  ::LocalBasisType::Traits::RangeFieldType;
344  using size_type = typename DGSpace::Traits::SizeType;
345 
346  // paranoia check number of number of components
347  static_assert(LFSV::CHILDREN == dim*2,
348  "need exactly dim*2 components!");
349 
350  // get local function space that is identical for all components
351  using namespace TypeTree::Indices;
352  const auto& dgspace = child(lfsv,_0);
353 
354  // Reference to cell
355  const auto& cell = eg.entity();
356 
357  // Get geometry
358  auto geo = eg.geometry();
359 
360  // evaluate parameters (assumed constant per element)
361  auto ref_el = referenceElement(geo);
362  auto localcenter = ref_el.position(0,0);
363  auto mu = param.mu(cell,localcenter);
364  auto eps = param.eps(cell,localcenter);
365  auto sigma = param.sigma(cell,localcenter);
366  auto muinv = 1.0/mu;
367  auto epsinv = 1.0/eps;
368 
369  //std::cout << "alpha_volume center=" << eg.geometry().center() << std::endl;
370 
371  // Transformation
372  typename EG::Geometry::JacobianInverseTransposed jac;
373 
374  // Initialize vectors outside for loop
375  Dune::FieldVector<RF,dim*2> u(0.0);
376  std::vector<Dune::FieldVector<RF,dim> > gradphi(dgspace.size());
377 
378  // loop over quadrature points
379  const int order = dgspace.finiteElement().localBasis().order();
380  const int intorder = overintegration+2*order;
381  for (const auto &qp : quadratureRule(geo,intorder))
382  {
383  // evaluate basis functions
384  const auto& phi = cache[order].evaluateFunction
385  (qp.position(), dgspace.finiteElement().localBasis());
386 
387  // evaluate state vector u
388  for (size_type k=0; k<dim*2; k++){ // for all components
389  u[k] = 0.0;
390  for (size_type j=0; j<dgspace.size(); j++) // for all basis functions
391  u[k] += x(lfsv.child(k),j)*phi[j];
392  }
393  //std::cout << " u at " << qp.position() << " : " << u << std::endl;
394 
395  // evaluate gradient of basis functions (we assume Galerkin method lfsu=lfsv)
396  const auto& js = cache[order].evaluateJacobian
397  (qp.position(), dgspace.finiteElement().localBasis());
398 
399  // compute global gradients
400  jac = geo.jacobianInverseTransposed(qp.position());
401  for (size_type i=0; i<dgspace.size(); i++)
402  jac.mv(js[i][0],gradphi[i]);
403 
404  // integrate
405  auto factor = qp.weight() * geo.integrationElement(qp.position());
406 
407  Dune::FieldMatrix<RF,dim*2,dim> F;
408  F[0][0] = 0; F[0][1] = -muinv*u[5]; F[0][2] = muinv*u[4];
409  F[1][0] = muinv*u[5]; F[1][1] = 0; F[1][2] = -muinv*u[3];
410  F[2][0] =-muinv*u[4]; F[2][1] = muinv*u[3]; F[2][2] = 0;
411  F[3][0] = 0; F[3][1] = epsinv*u[2]; F[3][2] = -epsinv*u[1];
412  F[4][0] = -epsinv*u[2]; F[4][1] = 0; F[4][2] = epsinv*u[0];
413  F[5][0] = epsinv*u[1]; F[5][1] = -epsinv*u[0]; F[5][2] = 0;
414 
415  // for all components of the system
416  for (size_type i=0; i<dim*2; i++)
417  // for all test functions of this component
418  for (size_type k=0; k<dgspace.size(); k++)
419  // for all dimensions
420  for (size_type j=0; j<dim; j++)
421  r.accumulate(lfsv.child(i),k,-F[i][j]*gradphi[k][j]*factor);
422 
423  // for the first half of the system
424  for (size_type i=0; i<dim; i++)
425  // for all test functions of this component
426  for (size_type k=0; k<dgspace.size(); k++)
427  r.accumulate(lfsv.child(i),k,(sigma/eps)*u[i]*phi[k]*factor);
428 
429  // std::cout << " residual: ";
430  // for (size_type i=0; i<r.size(); i++) std::cout << r[i] << " ";
431  // std::cout << std::endl;
432  }
433  }
434 
435  // skeleton integral depending on test and ansatz functions
436  // each face is only visited ONCE!
437  template<typename IG, typename LFSU, typename X, typename LFSV, typename R>
438  void alpha_skeleton (const IG& ig,
439  const LFSU& lfsu_s, const X& x_s, const LFSV& lfsv_s,
440  const LFSU& lfsu_n, const X& x_n, const LFSV& lfsv_n,
441  R& r_s, R& r_n) const
442  {
443  using std::max;
444  using std::sqrt;
445 
446  // Define types
447  using namespace TypeTree::Indices;
448  using DGSpace = TypeTree::Child<LFSV,_0>;
449  using DF = typename DGSpace::Traits::FiniteElementType::
450  Traits::LocalBasisType::Traits::DomainFieldType;
451  using RF = typename DGSpace::Traits::FiniteElementType::
452  Traits::LocalBasisType::Traits::RangeFieldType;
453  using size_type = typename DGSpace::Traits::SizeType;
454 
455  // get local function space that is identical for all components
456  using namespace TypeTree::Indices;
457  const auto& dgspace_s = child(lfsv_s,_0);
458  const auto& dgspace_n = child(lfsv_n,_0);
459 
460  // References to inside and outside cells
461  const auto& cell_inside = ig.inside();
462  const auto& cell_outside = ig.outside();
463 
464  // Get geometries
465  auto geo = ig.geometry();
466  auto geo_inside = cell_inside.geometry();
467  auto geo_outside = cell_outside.geometry();
468 
469  // Geometry of intersection in local coordinates of inside_cell and outside_cell
470  auto geo_in_inside = ig.geometryInInside();
471  auto geo_in_outside = ig.geometryInOutside();
472 
473  // evaluate speed of sound (assumed constant per element)
474  auto ref_el_inside = referenceElement(geo_inside);
475  auto ref_el_outside = referenceElement(geo_outside);
476  auto local_inside = ref_el_inside.position(0,0);
477  auto local_outside = ref_el_outside.position(0,0);
478  // This is wrong -- this approach (with A- and A+) does not allow
479  // position-dependent eps and mu, so we should not allow the parameter
480  // class to specify them in a position-dependent manner. See my
481  // (Jorrit Fahlke) dissertation on how to do it right.
482  auto mu_s = param.mu(cell_inside,local_inside);
483  auto mu_n = param.mu(cell_outside,local_outside);
484  auto eps_s = param.eps(cell_inside,local_inside);
485  auto eps_n = param.eps(cell_outside,local_outside);
486  //auto sigma_s = param.sigma(ig.inside(),local_inside);
487  //auto sigma_n = param.sigma(ig.outside(),local_outside);
488 
489  // normal: assume faces are planar
490  const auto& n_F = ig.centerUnitOuterNormal();
491 
492  // compute A+ (outgoing waves)
493  Dune::FieldMatrix<DF,dim*2,dim*2> R_s;
494  MaxwellEigenvectors<dim>::eigenvectors(eps_s,mu_s,n_F,R_s);
495  Dune::FieldMatrix<DF,dim*2,dim*2> Dplus_s(0.0);
496  Dplus_s[0][0] = 1.0/sqrt(eps_s*mu_s);
497  Dplus_s[1][1] = 1.0/sqrt(eps_s*mu_s);
498  Dune::FieldMatrix<DF,dim*2,dim*2> Aplus_s(R_s);
499  Aplus_s.rightmultiply(Dplus_s);
500  R_s.invert();
501  Aplus_s.rightmultiply(R_s);
502 
503  // compute A- (incoming waves)
504  Dune::FieldMatrix<DF,dim*2,dim*2> R_n;
505  MaxwellEigenvectors<dim>::eigenvectors(eps_n,mu_n,n_F,R_n);
506  Dune::FieldMatrix<DF,dim*2,dim*2> Dminus_n(0.0);
507  Dminus_n[2][2] = -1.0/sqrt(eps_n*mu_n);
508  Dminus_n[3][3] = -1.0/sqrt(eps_n*mu_n);
509  Dune::FieldMatrix<DF,dim*2,dim*2> Aminus_n(R_n);
510  Aminus_n.rightmultiply(Dminus_n);
511  R_n.invert();
512  Aminus_n.rightmultiply(R_n);
513 
514  // Initialize vectors outside for loop
515  Dune::FieldVector<RF,dim*2> u_s(0.0);
516  Dune::FieldVector<RF,dim*2> u_n(0.0);
517  Dune::FieldVector<RF,dim*2> f(0.0);
518 
519  // std::cout << "alpha_skeleton center=" << ig.geometry().center() << std::endl;
520 
521  // loop over quadrature points
522  const int order_s = dgspace_s.finiteElement().localBasis().order();
523  const int order_n = dgspace_n.finiteElement().localBasis().order();
524  const int intorder = overintegration+1+2*max(order_s,order_n);
525  for (const auto& qp : quadratureRule(geo,intorder))
526  {
527  // position of quadrature point in local coordinates of elements
528  const auto& iplocal_s = geo_in_inside.global(qp.position());
529  const auto& iplocal_n = geo_in_outside.global(qp.position());
530 
531  // evaluate basis functions
532  const auto& phi_s = cache[order_s].evaluateFunction(iplocal_s,dgspace_s.finiteElement().localBasis());
533  const auto& phi_n = cache[order_n].evaluateFunction(iplocal_n,dgspace_n.finiteElement().localBasis());
534 
535  // evaluate u from inside and outside
536  for (size_type i=0; i<dim*2; i++){ // for all components
537  u_s[i] = 0.0;
538  for (size_type k=0; k<dgspace_s.size(); k++) // for all basis functions
539  u_s[i] += x_s(lfsv_s.child(i),k)*phi_s[k];
540  }
541  for (size_type i=0; i<dim*2; i++){ // for all components
542  u_n[i] = 0.0;
543  for (size_type k=0; k<dgspace_n.size(); k++) // for all basis functions
544  u_n[i] += x_n(lfsv_n.child(i),k)*phi_n[k];
545  }
546 
547  // compute numerical flux at integration point
548  f = 0.0;
549  Aplus_s.umv(u_s,f);
550  // std::cout << " after A_plus*u_s " << f << std::endl;
551  Aminus_n.umv(u_n,f);
552  // std::cout << " after A_minus*u_n " << f << std::endl;
553 
554  //std::cout << "f=" << f << " u_s=" << u_s << " u_n=" << u_n << std::endl;
555 
556  // integrate
557  auto factor = qp.weight() * geo.integrationElement(qp.position());
558  for (size_type k=0; k<dgspace_s.size(); k++) // loop over all vector-valued (!) basis functions (with identical components)
559  for (size_type i=0; i<dim*2; i++) // loop over all components
560  r_s.accumulate(lfsv_s.child(i),k,f[i]*phi_s[k]*factor);
561  for (size_type k=0; k<dgspace_n.size(); k++) // loop over all vector-valued (!) basis functions (with identical components)
562  for (size_type i=0; i<dim*2; i++) // loop over all components
563  r_n.accumulate(lfsv_n.child(i),k,-f[i]*phi_n[k]*factor);
564  }
565 
566  // std::cout << " residual_s: ";
567  // for (auto v : r_s) std::cout << v << " ";
568  // std::cout << std::endl;
569  // std::cout << " residual_n: ";
570  // for (auto v : r_n) std::cout << v << " ";
571  // std::cout << std::endl;
572  }
573 
574  // skeleton integral depending on test and ansatz functions
575  template<typename IG, typename LFSU, typename X, typename LFSV, typename R>
576  void alpha_boundary (const IG& ig,
577  const LFSU& lfsu_s, const X& x_s, const LFSV& lfsv_s,
578  R& r_s) const
579  {
580  // Define types
581  using namespace TypeTree::Indices;
582  using DGSpace = TypeTree::Child<LFSV,_0>;
583  using DF = typename DGSpace::Traits::FiniteElementType::
584  Traits::LocalBasisType::Traits::DomainFieldType;
585  using RF = typename DGSpace::Traits::FiniteElementType::
586  Traits::LocalBasisType::Traits::RangeFieldType;
587  using size_type = typename DGSpace::Traits::SizeType;
588 
589  // get local function space that is identical for all components
590  using namespace TypeTree::Indices;
591  const auto& dgspace_s = child(lfsv_s,_0);
592 
593  // References to inside cell
594  const auto& cell_inside = ig.inside();
595 
596  // Get geometries
597  auto geo = ig.geometry();
598  auto geo_inside = cell_inside.geometry();
599 
600  // Geometry of intersection in local coordinates of inside_cell
601  auto geo_in_inside = ig.geometryInInside();
602 
603  // evaluate speed of sound (assumed constant per element)
604  auto ref_el_inside = referenceElement(geo_inside);
605  auto local_inside = ref_el_inside.position(0,0);
606  auto mu_s = param.mu(cell_inside,local_inside);
607  auto eps_s = param.eps(cell_inside,local_inside);
608  //auto sigma_s = param.sigma(ig.inside(),local_inside );
609 
610  // normal: assume faces are planar
611  const auto& n_F = ig.centerUnitOuterNormal();
612 
613  // compute A+ (outgoing waves)
614  Dune::FieldMatrix<DF,dim*2,dim*2> R_s;
615  MaxwellEigenvectors<dim>::eigenvectors(eps_s,mu_s,n_F,R_s);
616  Dune::FieldMatrix<DF,dim*2,dim*2> Dplus_s(0.0);
617  Dplus_s[0][0] = 1.0/sqrt(eps_s*mu_s);
618  Dplus_s[1][1] = 1.0/sqrt(eps_s*mu_s);
619  Dune::FieldMatrix<DF,dim*2,dim*2> Aplus_s(R_s);
620  Aplus_s.rightmultiply(Dplus_s);
621  R_s.invert();
622  Aplus_s.rightmultiply(R_s);
623 
624  // compute A- (incoming waves)
625  Dune::FieldMatrix<DF,dim*2,dim*2> R_n;
626  MaxwellEigenvectors<dim>::eigenvectors(eps_s,mu_s,n_F,R_n);
627  Dune::FieldMatrix<DF,dim*2,dim*2> Dminus_n(0.0);
628  Dminus_n[2][2] = -1.0/sqrt(eps_s*mu_s);
629  Dminus_n[3][3] = -1.0/sqrt(eps_s*mu_s);
630  Dune::FieldMatrix<DF,dim*2,dim*2> Aminus_n(R_n);
631  Aminus_n.rightmultiply(Dminus_n);
632  R_n.invert();
633  Aminus_n.rightmultiply(R_n);
634 
635  // Initialize vectors outside for loop
636  Dune::FieldVector<RF,dim*2> u_s(0.0);
637  Dune::FieldVector<RF,dim*2> u_n;
638  Dune::FieldVector<RF,dim*2> f(0.0);
639 
640  // std::cout << "alpha_boundary center=" << ig.geometry().center() << std::endl;
641 
642  // loop over quadrature points
643  const int order_s = dgspace_s.finiteElement().localBasis().order();
644  const int intorder = overintegration+1+2*order_s;
645  for(const auto &qp : quadratureRule(geo,intorder))
646  {
647  // position of quadrature point in local coordinates of elements
648  const auto& iplocal_s = geo_in_inside.global(qp.position());
649 
650  // evaluate basis functions
651  const auto& phi_s = cache[order_s].evaluateFunction
652  (iplocal_s,dgspace_s.finiteElement().localBasis());
653 
654  // evaluate u from inside and outside
655  for (size_type i=0; i<dim*2; i++){ // for all components
656  u_s[i] = 0.0;
657  for (size_type k=0; k<dgspace_s.size(); k++) // for all basis functions
658  u_s[i] += x_s(lfsv_s.child(i),k)*phi_s[k];
659  }
660  // std::cout << " u_s " << u_s << std::endl;
661 
662  // evaluate boundary condition
663  u_n = (param.g(ig.intersection(),qp.position(),u_s));
664  // std::cout << " u_n " << u_n << " bc: " << param.g(ig.intersection(),qp.position(),u_s) << std::endl;
665 
666  // compute numerical flux at integration point
667  f = 0.0;
668  Aplus_s.umv(u_s,f);
669  // std::cout << " after A_plus*u_s " << f << std::endl;
670  Aminus_n.umv(u_n,f);
671  // std::cout << " after A_minus*u_n " << f << std::endl;
672 
673  // integrate
674  auto factor = qp.weight() * geo.integrationElement(qp.position());
675  for (size_type k=0; k<dgspace_s.size(); k++) // loop over all vector-valued (!) basis functions (with identical components)
676  for (size_type i=0; i<dim*2; i++) // loop over all components
677  r_s.accumulate(lfsv_s.child(i),k,f[i]*phi_s[k]*factor);
678  }
679 
680  // std::cout << " residual_s: ";
681  // for (size_type i=0; i<r_s.size(); i++) std::cout << r_s[i] << " ";
682  // std::cout << std::endl;
683  }
684 
685  // volume integral depending only on test functions
686  template<typename EG, typename LFSV, typename R>
687  void lambda_volume (const EG& eg, const LFSV& lfsv, R& r) const
688  {
689  // Define types
690  using namespace TypeTree::Indices;
691  using DGSpace = TypeTree::Child<LFSV,_0>;
692  using size_type = typename DGSpace::Traits::SizeType;
693 
694  // Get local function space that is identical for all components
695  using namespace TypeTree::Indices;
696  const auto& dgspace = child(lfsv,_0);
697 
698  // Reference to cell
699  const auto& cell = eg.entity();
700 
701  // Get geometry
702  auto geo = eg.geometry();
703 
704  // loop over quadrature points
705  const int order_s = dgspace.finiteElement().localBasis().order();
706  const int intorder = overintegration+2*order_s;
707  for (const auto &qp : quadratureRule(geo,intorder))
708  {
709  // evaluate right hand side
710  auto j = param.j(cell,qp.position());
711 
712  // evaluate basis functions
713  const auto& phi = cache[order_s].evaluateFunction(qp.position(),dgspace.finiteElement().localBasis());
714 
715  // integrate
716  auto factor = qp.weight() * geo.integrationElement(qp.position());
717  for (size_type k=0; k<dim*2; k++) // for all components
718  for (size_type i=0; i<dgspace.size(); i++) // for all test functions of this component
719  r.accumulate(lfsv.child(k),i,-j[k]*phi[i]*factor);
720  }
721  }
722 
724  void setTime (typename T::Traits::RangeFieldType t)
725  {
726  }
727 
729  void preStep (typename T::Traits::RangeFieldType time, typename T::Traits::RangeFieldType dt,
730  int stages)
731  {
732  }
733 
735  void preStage (typename T::Traits::RangeFieldType time, int r)
736  {
737  }
738 
740  void postStage ()
741  {
742  }
743 
745  typename T::Traits::RangeFieldType suggestTimestep (typename T::Traits::RangeFieldType dt) const
746  {
747  return dt;
748  }
749 
750  private:
751  T& param;
752  int overintegration;
753  typedef typename FEM::Traits::FiniteElementType::Traits::LocalBasisType LocalBasisType;
755  std::vector<Cache> cache;
756  };
757 
758 
759 
771  template<typename T, typename FEM>
773  public NumericalJacobianApplyVolume<DGMaxwellTemporalOperator<T,FEM> >,
775  public InstationaryLocalOperatorDefaultMethods<typename T::Traits::RangeFieldType>
776  {
777  enum { dim = T::Traits::GridViewType::dimension };
778  public:
779  // pattern assembly flags
780  enum { doPatternVolume = true };
781 
782  // residual assembly flags
783  enum { doAlphaVolume = true };
784 
785  DGMaxwellTemporalOperator (T& param_, int overintegration_=0)
786  : param(param_), overintegration(overintegration_), cache(20)
787  {}
788 
789  // define sparsity pattern of operator representation
790  template<typename LFSU, typename LFSV, typename LocalPattern>
791  void pattern_volume (const LFSU& lfsu, const LFSV& lfsv,
792  LocalPattern& pattern) const
793  {
794  // paranoia check number of number of components
795  static_assert(LFSU::CHILDREN==LFSV::CHILDREN, "need U=V!");
796  static_assert(LFSV::CHILDREN==dim*2, "need exactly dim*2 components!");
797 
798  for (size_t k=0; k<LFSV::CHILDREN; k++)
799  for (size_t i=0; i<lfsv.child(k).size(); ++i)
800  for (size_t j=0; j<lfsu.child(k).size(); ++j)
801  pattern.addLink(lfsv.child(k),i,lfsu.child(k),j);
802  }
803 
804  // volume integral depending on test and ansatz functions
805  template<typename EG, typename LFSU, typename X, typename LFSV, typename R>
806  void alpha_volume (const EG& eg, const LFSU& lfsu, const X& x, const LFSV& lfsv, R& r) const
807  {
808  // Define types
809  using namespace TypeTree::Indices;
810  using DGSpace = TypeTree::Child<LFSV,_0>;
811  using RF = typename DGSpace::Traits::FiniteElementType::
812  Traits::LocalBasisType::Traits::RangeFieldType;
813  using size_type = typename DGSpace::Traits::SizeType;
814 
815  // get local function space that is identical for all components
816  using namespace TypeTree::Indices;
817  const auto& dgspace = child(lfsv,_0);
818 
819  // Get geometry
820  auto geo = eg.geometry();
821 
822  // Initialize vectors outside for loop
823  Dune::FieldVector<RF,dim*2> u(0.0);
824 
825  // loop over quadrature points
826  const int order = dgspace.finiteElement().localBasis().order();
827  const int intorder = overintegration+2*order;
828  for (const auto& qp : quadratureRule(geo,intorder))
829  {
830  // evaluate basis functions
831  const auto& phi = cache[order].evaluateFunction(qp.position(),dgspace.finiteElement().localBasis());
832 
833  // evaluate u
834  for (size_type k=0; k<dim*2; k++){ // for all components
835  u[k] = 0.0;
836  for (size_type j=0; j<dgspace.size(); j++) // for all basis functions
837  u[k] += x(lfsv.child(k),j)*phi[j];
838  }
839 
840  // integrate
841  auto factor = qp.weight() * geo.integrationElement(qp.position());
842  for (size_type k=0; k<dim*2; k++) // for all components
843  for (size_type i=0; i<dgspace.size(); i++) // for all test functions of this component
844  r.accumulate(lfsv.child(k),i,u[k]*phi[i]*factor);
845  }
846  }
847 
848  // jacobian of volume term
849  template<typename EG, typename LFSU, typename X, typename LFSV, typename M>
850  void jacobian_volume (const EG& eg, const LFSU& lfsu, const X& x, const LFSV& lfsv,
851  M& mat) const
852  {
853  // Define types
854  using namespace TypeTree::Indices;
855  using DGSpace = TypeTree::Child<LFSV,_0>;
856  using size_type = typename DGSpace::Traits::SizeType;
857 
858  // Get local function space that is identical for all components
859  using namespace TypeTree::Indices;
860  const auto& dgspace = child(lfsv,_0);
861 
862  // Get geometry
863  auto geo = eg.geometry();
864 
865  // Loop over quadrature points
866  const int order = dgspace.finiteElement().localBasis().order();
867  const int intorder = overintegration+2*order;
868  for (const auto& qp : quadratureRule(geo,intorder))
869  {
870  // Evaluate basis functions
871  const auto& phi = cache[order].evaluateFunction(qp.position(),dgspace.finiteElement().localBasis());
872 
873  // Integrate
874  auto factor = qp.weight() * geo.integrationElement(qp.position());
875  for (size_type k=0; k<dim*2; k++) // for all components
876  for (size_type i=0; i<dgspace.size(); i++) // for all test functions of this component
877  for (size_type j=0; j<dgspace.size(); j++) // for all ansatz functions of this component
878  mat.accumulate(lfsv.child(k),i,lfsu.child(k),j,phi[j]*phi[i]*factor);
879  }
880  }
881 
882  private:
883  T& param;
884  int overintegration;
885  typedef typename FEM::Traits::FiniteElementType::Traits::LocalBasisType LocalBasisType;
887  std::vector<Cache> cache;
888  };
889 
890  }
891 }
892 
893 #endif
void preStep(typename T::Traits::RangeFieldType time, typename T::Traits::RangeFieldType dt, int stages)
to be called once before each time step
Definition: maxwelldg.hh:729
Definition: maxwelldg.hh:772
static void eigenvectors(T1 eps, T1 mu, const Dune::FieldVector< T2, dim > &n, Dune::FieldMatrix< T3, 2 *dim, 2 *dim > &R)
Definition: maxwelldg.hh:76
void jacobian_volume(const EG &eg, const LFSU &lfsu, const X &x, const LFSV &lfsv, M &mat) const
Definition: maxwelldg.hh:850
static void eigenvalues(T1 eps, T1 mu, const Dune::FieldVector< T2, 2 *dim > &e)
Definition: maxwelldg.hh:53
sparsity pattern generator
Definition: pattern.hh:13
Implements linear and nonlinear versions of jacobian_apply_skeleton() based on alpha_skeleton() ...
Definition: numericaljacobianapply.hh:180
const IG & ig
Definition: constraints.hh:148
static const int dim
Definition: adaptivity.hh:83
void alpha_boundary(const IG &ig, const LFSU &lfsu_s, const X &x_s, const LFSV &lfsv_s, R &r_s) const
Definition: maxwelldg.hh:576
Implement jacobian_boundary() based on alpha_boundary()
Definition: numericaljacobian.hh:250
ReferenceElementWrapper< ReferenceElement< typename Geometry::ctype, Geometry::mydimension > > referenceElement(const Geometry &geo)
Returns the reference element for the given geometry.
Definition: referenceelements.hh:144
Definition: adaptivity.hh:27
sparsity pattern generator
Definition: pattern.hh:29
T::Traits::RangeFieldType suggestTimestep(typename T::Traits::RangeFieldType dt) const
to be called once before each stage
Definition: maxwelldg.hh:745
Implements linear and nonlinear versions of jacobian_apply_boundary() based on alpha_boundary() ...
Definition: numericaljacobianapply.hh:285
Default flags for all local operators.
Definition: flags.hh:18
void alpha_volume(const EG &eg, const LFSU &lfsu, const X &x, const LFSV &lfsv, R &r) const
Definition: maxwelldg.hh:806
const Entity & e
Definition: localfunctionspace.hh:111
QuadratureRuleWrapper< QuadratureRule< typename Geometry::ctype, Geometry::mydimension > > quadratureRule(const Geometry &geo, std::size_t order, QuadratureType::Enum quadrature_type=QuadratureType::GaussLegendre)
Returns a quadrature rule for the given geometry.
Definition: quadraturerules.hh:117
void preStage(typename T::Traits::RangeFieldType time, int r)
to be called once before each stage
Definition: maxwelldg.hh:735
Definition: maxwelldg.hh:29
Implement jacobian_volume() based on alpha_volume()
Definition: numericaljacobian.hh:31
Default class for additional methods in instationary local operators.
Definition: idefault.hh:89
void setTime(typename T::Traits::RangeFieldType t)
set time in parameter class
Definition: maxwelldg.hh:724
DGMaxwellTemporalOperator(T &param_, int overintegration_=0)
Definition: maxwelldg.hh:785
void alpha_skeleton(const IG &ig, const LFSU &lfsu_s, const X &x_s, const LFSV &lfsv_s, const LFSU &lfsu_n, const X &x_n, const LFSV &lfsv_n, R &r_s, R &r_n) const
Definition: maxwelldg.hh:438
void pattern_volume(const LFSU &lfsu, const LFSV &lfsv, LocalPattern &pattern) const
Definition: maxwelldg.hh:791
const std::string s
Definition: function.hh:1102
store values of basis functions and gradients in a cache
Definition: localbasiscache.hh:17
Implements linear and nonlinear versions of jacobian_apply_volume() based on alpha_volume() ...
Definition: numericaljacobianapply.hh:32
DGMaxwellSpatialOperator(T &param_, int overintegration_=0)
Definition: maxwelldg.hh:330
void postStage()
to be called once at the end of each stage
Definition: maxwelldg.hh:740
void alpha_volume(const EG &eg, const LFSU &lfsu, const X &x, const LFSV &lfsv, R &r) const
Definition: maxwelldg.hh:337
Definition: maxwelldg.hh:304
Implement jacobian_skeleton() based on alpha_skeleton()
Definition: numericaljacobian.hh:156
void lambda_volume(const EG &eg, const LFSV &lfsv, R &r) const
Definition: maxwelldg.hh:687