13#ifndef MULTIVARIATIONALUTIL_HH
14#define MULTIVARIATIONALUTIL_HH
21#include <boost/fusion/algorithm.hpp>
22#include <boost/fusion/include/fold.hpp>
23#include <boost/fusion/include/make_vector.hpp>
24#include <boost/fusion/include/size.hpp>
25#include <boost/fusion/sequence.hpp>
26#include <boost/mpl/int.hpp>
27#include <boost/type_traits/remove_reference.hpp>
28#include <boost/type_traits/remove_const.hpp>
74 template <
int spaceId,
int components,
int Id=-1>
78 static int const id = Id;
80 static int const m = components;
91 template <
int spaceIndex>
99 template <
int components>
119 template <
typename A,
typename B>
122 static int const spaceIndex = A::spaceIndex>=0? A::spaceIndex : B::spaceIndex;
123 static int const m = A::m>=0? A::m : B::m;
125 static_assert(
spaceIndex>=0,
"Space index has to be nonnegative (space indices cover a contiguous range from 0 to k-1).");
126 static_assert(
m>=0,
"Number of components has to be nonnegative (in fact, 0 makes rarely sense as well).");
152 template <
class Spaces>
164 template <
typename Arg>
struct result {};
166 template <
class Variable>
169 typedef typename boost::remove_reference<Variable>::type
Var;
171 typedef typename Space::template Element_t<Var::m>
type;
177 template <
class Variable>
184 Spaces
const& spaces;
192 template <
class Spaces>
204 template <
typename Arg>
struct result {};
206 template <
class Variable>
209 typedef typename boost::remove_reference<Variable>::type
Var;
211 typedef typename Space::template Element_t<Var::m>::StorageType
StorageType;
223 template <
class Variable>
230 Spaces
const& spaces;
238 namespace Variables_Detail
243 template <
class Variables,
class RepresentationConstructor,
244 int first=0,
int last=result_of::size<Variables>::type::value>
245 class VariableRangeCreator
247 typedef typename result_of::begin<Variables>::type Begin;
248 typedef typename result_of::advance_c<Begin,first>::type First;
249 typedef typename result_of::advance_c<Begin,last>::type Last;
252 typedef typename boost::fusion::transform_view<iterator_range<First,Last>
const,
253 RepresentationConstructor> View;
254 using type =
typename result_of::as_vector<View>::type;
256 static View apply(RepresentationConstructor
const& c)
260 return transform(iterator_range<First,Last>(advance_c<first>(begin(vars)),advance_c<last>(begin(vars))),c);
266 struct AddIdToVariableDescription
268 template <
class Vd,
class Int>
271 using type = Kaskade::VariableDescription<Vd::spaceIndex, Vd::m, Int::value>;
278 template <
typename IncompleteVariableDescriptions>
279 struct ImplicitIdVariableDescriptions
281 using size =
typename result_of::size<IncompleteVariableDescriptions>::type;
282 using type =
typename result_of::as_vector<
typename boost::mpl::transform<IncompleteVariableDescriptions,
283 boost::mpl::range_c<int,0,size::value>,
284 AddIdToVariableDescription>::type
290 template <
typename Variables,
typename Spaces>
295 template <
typename C,
typename Var>
296 auto operator()(C,Var)
const
298 using SpacePtr =
typename result_of::value_at_c<Spaces,Var::spaceIndex>::type;
299 constexpr int m = std::remove_pointer_t<SpacePtr>::continuity;
300 return typename boost::mpl::min<C,boost::mpl::int_<m>>::type();
305 static int const value = result_of::fold<Variables,boost::mpl::int_<1000>,Min>::type::value;
334 template <
class VSDescriptions>
336 typename VSDescriptions::RepresentationData>
343 typedef typename VSDescriptions::RepresentationData
Functions;
351 typedef typename Descriptions::Grid Grid;
352 using GridView =
typename Descriptions::GridView;
368 : Base(static_cast<Base&&>(vs))
403 this->
data = std::move(v.data);
420 static_cast<Base&
>(*this) = s;
435 template <
int i,
class Evaluators>
438 static_assert(boost::fusion::result_of::size<Evaluators>::type::value==boost::fusion::result_of::size<typename Descriptions::Spaces>::type::value,
439 "Evaluators have to match the space list");
440 constexpr int spaceIndex = Descriptions::template spaceIndex<i>;
441 return component<i>(*this).value(boost::fusion::at_c<spaceIndex>(evals));
471 template <
int i,
class Evaluators>
474 int const spaceIndex = Descriptions::template spaceIndex<i>;
475 return component<i>(*this).derivative(boost::fusion::at_c<spaceIndex>(evals));
500 template <
int i,
class Evaluators>
503 int const spaceIndex = Descriptions::template spaceIndex<i>;
504 return component<i>(*this).hessian(boost::fusion::at_c<spaceIndex>(evals));
552 template <
class SpaceList,
class VariableList>
560 using Variables =
typename Variables_Detail::ImplicitIdVariableDescriptions<VariableList>::type;
595 static int const noOfVariables = boost::fusion::result_of::size<Variables>::type::value;
604 static int const continuity = Variables_Detail::Continuity<Variables,Spaces>::value;
609 typedef typename boost::fusion::result_of::as_vector<
610 typename boost::fusion::result_of::transform<Spaces, GetEvaluators<ShapeFunctionCache<Grid,Scalar>> >
::type
619 template <
class RAIter>
635 assert(
names.size()==names_.size());
636 std::copy(names_.begin(),names_.end(),
names.begin());
642 template <
class RAIter>
655 std::copy(roles_.begin(),roles_.end(),
roles.begin());
692 assert(0<=first && first<=last && last<=dimensions.size());
693 return std::accumulate(begin(dimensions)+first,begin(dimensions)+last,0);
701 std::array<size_t,noOfVariables> dimensions;
702 boost::fusion::for_each(
Variables(),ComputeDimension<noOfVariables>(dimensions,
spaces));
710 constexpr static int components = boost::fusion::result_of::value_at_c<Variables,i>::type::m;
714 struct [[deprecated]]
Components {
static int const m = boost::fusion::result_of::value_at_c<Variables,idx>::type::m; };
732 constexpr static int spaceIndex = boost::fusion::result_of::value_at_c<Variables,idx>::type::spaceIndex;
738 struct [[deprecated]]
SpaceIndex {
static int const spaceIndex = boost::fusion::result_of::value_at_c<Variables,idx>::type::spaceIndex; };
745 using Space = std::remove_pointer_t<typename boost::fusion::result_of::value_at_c<Spaces,spaceIndex<idx>>
::type>;
765 template <
int first=0,
int last=noOfVariables>
766 class CoefficientVectorRepresentation
768 using Creator = Variables_Detail::VariableRangeCreator<Variables,ConstructCoefficientVector<Spaces>,first,last>;
784 static typename Creator::View init(
Spaces const&
spaces)
799 template <
int first=0,
int last=noOfVariables>
805 template <
int first=0,
int last=noOfVariables>
808 using CVec = CoefficientVectorRepresentation<first,last>;
809 return typename CVec::type(CVec::init(
spaces));
815 template <
int first=0,
int last=noOfVariables>
856 std::array<std::string,noOfVariables>
names;
858 std::array<std::string,noOfVariables>
roles;
862 struct CheckHasEqualGridAndIndexSet
868 template <
class Space>
869 void operator()(
Space const* space)
const {
870 assert(&(space->gridManager())== gp);
871 assert(&(space->indexSet()) == isp);
879 struct ComputeDimension
881 ComputeDimension(std::array<size_t,nvars>& dims,
Spaces const& spaces_):
882 dims_(dims),
spaces(spaces_)
885 template <
class Variable>
886 void operator()(Variable
const& )
const
888 assert(dims_.size()>Variable::id);
889 dims_[Variable::id] =
Variable::m * boost::fusion::at_c<Variable::spaceIndex>(
spaces)->degreesOfFreedom();
892 std::array<size_t,nvars>& dims_;
904 template <
class VarSetDesc,
int varIdx>
922 template <
class ...Spaces>
925 return boost::fusion::make_vector(
const_cast<Spaces const*
>(
spaces)...);
941 template <
class SpaceList,
class VariableList>
943 VariableList
const& vars)
948 std::vector<std::string>
names;
949 for_each(vars,[&](
auto var) {
names.push_back(var.name); });
951 for (
int i=0; i<
names.size(); ++i)
952 if (
names[i].empty())
963 namespace Variables_Detail
965 template <
class AnsatzVariables,
class TestVariables>
966 auto makeAllSystemBlocks(AnsatzVariables a, TestVariables t)
970 auto appendRow = [=](
auto blocks,
auto rowVar)
972 auto thisRow = transform(a,[=](
auto colVar)
973 {
return std::pair(rowVar,colVar);
975 return join(blocks,thisRow);
978 return as_vector(accumulate(t,vector<>(),appendRow));
995 template <
class AnsatzVariables,
class TestVariables>
996 using AllSystemBlocks =
decltype(Variables_Detail::makeAllSystemBlocks(AnsatzVariables(),
1018 template <
class Variables,
class Functions,
class Evaluators,
class Method>
1021 Method
const& method = Method())
1026 auto doEval = [&] (
auto const& pair)
1029 static int const sid = std::remove_reference_t<
typename result_of::value_at_c<std::remove_reference_t<
decltype(pair)>,1>
::type>::spaceIndex;
1031 return method(at_c<0>(pair),at_c<sid>(eval));
1036 using VarDesc =
typename result_of::as_vector<Variables>::type;
1040 return as_vector(transform(zip(fs,VarDesc()),doEval));
1058 template <
class VariableSet,
class Method>
1060 typename VariableSet::Descriptions::Evaluators
const& eval,
1061 Method
const& method = Method())
1076 template <
class VariableSet,
class Method>
1078 std::declval<typename VariableSet::Descriptions::Evaluators>(),
1079 std::declval<Method>()));
1089 constexpr auto valueMethod = [](
auto const& f,
auto const& eval) {
return f.value(eval); };
1098 constexpr auto derivativeMethod = [](
auto const& f,
auto const& eval) {
return f.derivative(eval); };
Basic functionality for managing grids and their refinement.
LinearProductSpace()=delete
GridView const & gridView
VariableSet variableSet() const
Returns a zero-initialized variable set.
static size_t degreesOfFreedom(Spaces const &spaces, int first, int last)
Computes the total number of scalar degrees of freedom collected in the variables [first,...
VariableSetDescription(Spaces const &spaces_, RAIter nameIt, RAIter roleIt)
Constructor specifying for each variable both a name and a role.
size_t degreesOfFreedom(int first=0, int last=noOfVariables) const
Computes the total number of scalar degrees of freedom collected in the variables [first,...
std::array< std::string, noOfVariables > names
IndexSet const & indexSet
GridManagerBase< Grid > const & gridManager() const
The gridManager keeping the grid on which we operate.
VariableSetDescription(Spaces const &spaces_, std::vector< std::string > names_)
Constructor.
auto zeroCoefficientVector() const
Returns a zero initialized coefficient vector for variables [first,last[.
std::array< std::string, noOfVariables > roles
static int const continuity
typename CoefficientVectorRepresentation< first, last >::type CoefficientVector
static constexpr int components
static int const noOfVariables
boost::fusion::result_of::as_vector< typenameboost::fusion::result_of::transform< Spaces, GetEvaluators< ShapeFunctionCache< Grid, Scalar > > >::type >::type Evaluators
Variables_Detail::VariableRangeCreator< Variables, ConstructElement< Spaces > >::type RepresentationData
static std::array< size_t, noOfVariables > variableDimensions(Spaces const &spaces, int first=0, int last=noOfVariables)
Computes for each variable the number of scalar degrees of freedom.
Kaskade::VariableSet< VariableSetDescription< Spaces, ErrorFunctionVariables > > VariableSet
typename Variables_Detail::ImplicitIdVariableDescriptions< ErrorFunctionVariables >::type Variables
std::remove_pointer_t< typename boost::fusion::result_of::value_at_c< Spaces, spaceIndex< idx > >::type > Space
SpaceType< Spaces, 0 >::type::Grid Grid
static constexpr int spaceIndex
SpaceType< Spaces, 0 >::type::Scalar Scalar
SpaceType< Spaces, 0 >::type::IndexSet IndexSet
SpaceType< Spaces, 0 >::type::GridView GridView
VariableSetDescription(Spaces const &spaces_, RAIter nameIt)
Constructor, giving each variable a name (e.g. for output purposes)
VariableSetDescription(Spaces const &spaces_)
Constructor without naming the variables.
static auto zeroCoefficientVector(Spaces const &spaces)
Returns a zero initialized coefficient vector for variables [first,last[.
VariableSetDescription(Spaces const &spaces_, std::vector< std::string > names_, std::vector< std::string > roles_)
Constructor specifying for each variable both a name and a role.
A class for storing a heterogeneous collection of FunctionSpaceElement s.
auto derivative(GlobalPosition< GridView > const &x) const
derivative of an individual variable in the set. \WARNING This method is comparatively DEAD SLOW.
VSDescriptions::RepresentationData Functions
boost::fusion::vector of data elements (of type FunctionSpaceElement)
auto value(Cell< GridView > const &cell, LocalPosition< GridView > const &xi) const
Value of an individual variable in the set.
VSDescriptions Descriptions
Type of the VariableSetDescription.
VariableSet(Self const &vs)
Copy constructor.
auto derivative(Evaluators const &evals) const
Derivative of an individual variable in the set.
Descriptions const * descriptions
auto hessian(Cell< GridView > const &cell, LocalPosition< GridView > const &xi) const
Hessian of an individual variable in the set.
Self & operator=(S const &s)
Assignment from other (compatible) types.
Self & operator=(Self &&v)
Move assignment.
LinearProductSpace< Scalar, Functions > LinearSpace
auto value(Evaluators const &evals) const
Value of an individual variable in the set.
VariableSet(Self &&vs)
Move constructor.
auto hessian(GlobalPosition< GridView > const &x) const
Hessian of an individual variable in the set. \WARNING This method is comparatively DEAD SLOW.
auto value(GlobalPosition< GridView > const &x) const
Value of an individual variable in the set.
VariableSet(Descriptions const &d)
Constructor.
auto hessian(Evaluators const &evals) const
Hessian of an individual variable in the set.
auto derivative(Cell< GridView > const &cell, LocalPosition< GridView > const &xi) const
Derivative of an individual variable in the set.
Self & operator=(Self const &v)
Assignment.
This file contains various utility functions that augment the basic functionality of Dune.
FEFunctionSpace and FunctionSpaceElement and the like.
std::string paddedString(int n, int places=3)
creates a zero-padded string representation of the given number
typename boost::fusion::result_of::as_vector< typename boost::fusion::result_of::transform< Spaces, GetEvaluatorTypes >::type >::type Evaluators
the heterogeneous sequence type of Evaluators for the given spaces
typename GridView::template Codim< 0 >::Entity Cell
The type of cells (entities of codimension 0) in the grid view.
Dune::FieldVector< typename GridView::ctype, GridView::dimensionworld > GlobalPosition
The type of global positions within the grid view.
Dune::FieldVector< typename GridView::ctype, GridView::dimension > LocalPosition
The type of local positions within cells of the grid view.
auto const & component(LinearProductSpace< Scalar, Sequence > const &x)
auto evaluateFunctions(Functions const &fs, Evaluators const &eval, Method const &method=Method())
A function evaulating all functions in a variable set using the provided method.
decltype(valueMethod) ValueMethod
The type of the valueMethod helper functor.
auto makeSpaceList(Spaces *... spaces)
Creates a list of finite element spaces in the required format. Usage example (for Stokes flow):
constexpr auto valueMethod
Helper method for evaluating whole variable sets.
auto evaluateVariables(VariableSet const &vars, typename VariableSet::Descriptions::Evaluators const &eval, Method const &method=Method())
A function evaulating all functions in a variable set using the provided method.
decltype(derivativeMethod) DerivativeMethod
The type of the derivativeMethod helper functor.
VariableSetDescription< SpaceList, VariableList > makeVariableSetDescription(SpaceList const &spaces, VariableList const &vars)
Creates a variable set description from given spaces and variables.
constexpr auto derivativeMethod
Helper method for evaluating whole variable sets.
decltype(Variables_Detail::makeAllSystemBlocks(AnsatzVariables(), TestVariables())) AllSystemBlocks
A type list of all possible variable combinations of the ansatz and test variables provided.
decltype(evaluateVariables(std::declval< VariableSet >(), std::declval< typename VariableSet::Descriptions::Evaluators >(), std::declval< Method >())) EvaluateVariables
The type of evaluated variables as returned by evaluateVariables.
Output of mesh and solution for visualization software.
Helper class for specifying the number of components of a variable.
boost::remove_reference< Variable >::type Var
SpaceType< Spaces, Var::spaceIndex >::type Space
Space::template Element_t< Var::m >::StorageType StorageType
Dune::BlockVector< typename StorageType::block_type > type
A boost::fusion functor for generating coefficient vectors for given variables.
ConstructCoefficientVector(Spaces const &spaces_)
result< ConstructCoefficientVector< Spaces >(Variable)>::type operator()(Variable const &variable) const
Returns a FunctionSpaceElement from the FEFunctionSpace associated to the variable.
Space::template Element_t< Var::m > type
SpaceType< Spaces, Var::spaceIndex >::type Space
boost::remove_reference< Variable >::type Var
For a given variable, defines the correct FunctionSpaceElement type.
A boost::fusion functor for generating function space elements for given variables.
ConstructElement(Spaces const &spaces_)
result< ConstructElement< Spaces >(Variable)>::type operator()(Variable const &variable) const
Returns a FunctionSpaceElement element from the FEFunctionSpace associated to the Variable.
Abstract base class of creators that can be registered at the pluggable factory.
Helper class for specifying the FE space index of a variable.
Extracts the type of FE space with given index from set of spaces.
std::remove_pointer_t< typename boost::fusion::result_of::value_at_c< Spaces, Idx >::type > type
A class storing elementary information about a single variable.This includes the number of components...
static int const spaceIndex
number of the space, this variable is associated with
static int const m
number of component of this variable
A class defining elementary information about a single variable.
Variable(std::string const &name_)
Constructor.
static int const spaceIndex
Variable()=default
Constructor. This will create a default name based on the variable id.
DEPRECATED, use components instead.
DEPRECATED, use spaceIndex instead.
static int const spaceIndex