12#ifndef DUMUX_DISCRETIZATION_L2_PROJECTION_HH
13#define DUMUX_DISCRETIZATION_L2_PROJECTION_HH
14#if HAVE_DUNE_FUNCTIONS
18#include <dune/common/fvector.hh>
19#include <dune/common/fmatrix.hh>
20#include <dune/common/parametertree.hh>
21#include <dune/geometry/quadraturerules.hh>
22#include <dune/istl/bcrsmatrix.hh>
23#include <dune/istl/bvector.hh>
24#include <dune/functions/gridfunctions/gridviewfunction.hh>
34template <
class FEBasis>
37 static constexpr int dim = FEBasis::GridView::dimension;
38 using FiniteElement =
typename FEBasis::LocalView::Tree::FiniteElement;
39 using Scalar =
typename FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType;
40 using ShapeValue =
typename FiniteElement::Traits::LocalBasisType::Traits::RangeType;
43 using CoefficientVector = Dune::BlockVector<Dune::FieldVector<Scalar, 1>>;
48 std::size_t maxIterations{100};
49 Scalar residualReduction{1e-13};
53 L2Projection(
const FEBasis& feBasis)
57 solver_.setMatrix(std::make_shared<Matrix>(createMassMatrix_(feBasis)));
60 template <
class Function>
61 CoefficientVector project(Function&& function,
const Params& params = Params{})
const
65 rhs.resize(feBasis_.size());
69 auto localFunc = localFunction(Dune::Functions::makeGridViewFunction(function, feBasis_.gridView()));
71 for (
const auto& element : elements(feBasis_.gridView()))
74 localFunc.bind(element);
76 const auto& localFiniteElement =
localView.tree().finiteElement();
77 const int order = dim*localFiniteElement.localBasis().order();
78 const auto& quad = Dune::QuadratureRules<Scalar, dim>::rule(
element.type(), order);
79 const auto geometry =
element.geometry();
81 for (
auto&& qp : quad)
83 const auto weight = qp.weight();
84 const auto ie = geometry.integrationElement(qp.position());
85 const auto globalPos = geometry.global(qp.position());
87 std::vector<ShapeValue> shapeValues;
88 localFiniteElement.localBasis().evaluateFunction(geometry.local(globalPos), shapeValues);
89 const auto functionValue = localFunc(qp.position());
91 for (
int i = 0; i < localFiniteElement.localBasis().size(); ++i)
94 rhs[globalI] += ie*weight*shapeValues[i]*functionValue;
100 Dune::ParameterTree solverParams;
101 solverParams[
"maxit"] = std::to_string(params.maxIterations);
102 solverParams[
"reduction"] = std::to_string(params.residualReduction);
103 solverParams[
"verbose"] = std::to_string(params.verbosity);
104 auto solver = solver_;
105 solver.setParams(solverParams);
112 Matrix createMassMatrix_(
const FEBasis& feBasis)
const
117 pattern.exportIdx(massMatrix);
120 for (
const auto& element : elements(feBasis.gridView()))
124 const auto& localFiniteElement =
localView.tree().finiteElement();
125 const int order = 2*(dim*localFiniteElement.localBasis().order()-1);
126 const auto& quad = Dune::QuadratureRules<Scalar, dim>::rule(
element.type(), order);
127 const auto geometry =
element.geometry();
129 for (
auto&& qp : quad)
131 const auto weight = qp.weight();
132 const auto ie = geometry.integrationElement(qp.position());
133 const auto globalPos = geometry.global(qp.position());
135 std::vector<ShapeValue> shapeValues;
136 localFiniteElement.localBasis().evaluateFunction(geometry.local(globalPos), shapeValues);
138 for (
int i = 0; i < localFiniteElement.localBasis().size(); ++i)
142 massMatrix[globalI][globalI] += ie*weight*shapeValues[i]*shapeValues[i];
144 for (
int j = i+1; j < localFiniteElement.localBasis().size(); ++j)
147 const auto value = ie*weight*shapeValues[i]*shapeValues[j];
148 massMatrix[globalI][globalJ] += value;
149 massMatrix[globalJ][globalI] += value;
158 const FEBasis& feBasis_;
160 SeqLinearSolverTraits, LinearAlgebraTraits<Matrix, CoefficientVector>
Dune::MatrixIndexSet getFEJacobianPattern(const FEBasis &feBasis)
Helper function to generate Jacobian pattern for finite element scheme.
Definition: jacobianpattern.hh:106
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:26
Detail::IstlIterativeLinearSolver< LSTraits, LATraits, Dune::CGSolver< typename LATraits::Vector >, Detail::IstlSolvers::IstlDefaultBlockLevelPreconditionerFactory< Dune::SeqSSOR > > SSORCGIstlSolver
An SSOR-preconditioned CG solver using dune-istl.
Definition: istlsolvers.hh:657
Linear solvers from dune-istl.
Helper function to generate Jacobian pattern for different discretization methods.
Define traits for linear algebra backends.
Define traits for linear solvers.
constexpr Projection projection
Definition: couplingmanager1d3d_projection.hh:263