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>
129 static int const spaceIndex = A::spaceIndex>=0? A::spaceIndex : B::spaceIndex>=0? B::spaceIndex : C::spaceIndex;
130 static int const m = A::m>=0? A::m : B::m>=0? B::m : C::m;
132 static_assert(
spaceIndex>=0,
"Space index has to be nonnegative (space indices cover a contiguous range from 0 to k-1).");
133 static_assert(
m>=0,
"Number of components has to be nonnegative (in fact, 0 makes rarely sense as well).");
159 template <
class Spaces>
171 template <
typename Arg>
struct result {};
173 template <
class Variable>
176 typedef typename boost::remove_reference<Variable>::type
Var;
178 typedef typename Space::template Element<Var::m>::type
type;
184 template <
class Variable>
191 Spaces
const& spaces;
199 template <
class Spaces>
211 template <
typename Arg>
struct result {};
213 template <
class Variable>
216 typedef typename boost::remove_reference<Variable>::type
Var;
218 typedef typename Space::template Element<Var::m>::type::StorageType
StorageType;
230 template <
class Variable>
237 Spaces
const& spaces;
245 namespace Variables_Detail
250 template <
class Variables,
class RepresentationConstructor,
251 int first=0,
int last=result_of::size<Variables>::type::value>
252 class VariableRangeCreator
254 typedef typename result_of::begin<Variables>::type Begin;
255 typedef typename result_of::advance_c<Begin,first>::type First;
256 typedef typename result_of::advance_c<Begin,last>::type Last;
259 typedef typename boost::fusion::transform_view<iterator_range<First,Last>
const,
260 RepresentationConstructor> View;
261 using type =
typename result_of::as_vector<View>::type;
263 static View apply(RepresentationConstructor
const& c)
267 return transform(iterator_range<First,Last>(advance_c<first>(begin(vars)),advance_c<last>(begin(vars))),c);
273 struct AddIdToVariableDescription
275 template <
class Vd,
class Int>
284 template <
typename IncompleteVariableDescriptions>
285 struct ImplicitIdVariableDescriptions
287 using size =
typename result_of::size<IncompleteVariableDescriptions>::type;
288 using type =
typename result_of::as_vector<
typename boost::mpl::transform<IncompleteVariableDescriptions,
289 boost::mpl::range_c<int,0,size::value>,
290 AddIdToVariableDescription>::type
296 template <
typename Variables,
typename Spaces>
301 template <
typename C,
typename Var>
302 auto operator()(C,Var)
const
304 using SpacePtr =
typename result_of::value_at_c<Spaces,Var::spaceIndex>::type;
305 constexpr int m = std::remove_pointer_t<SpacePtr>::continuity;
306 return typename boost::mpl::min<C,boost::mpl::int_<m>>::type();
311 static int const value = result_of::fold<Variables,boost::mpl::int_<1000>,Min>::type::value;
338 template <
class VSDescriptions>
340 typename VSDescriptions::RepresentationData>
347 typedef typename VSDescriptions::RepresentationData
Functions;
355 typedef typename Descriptions::Grid Grid;
356 using GridView =
typename Descriptions::GridView;
401 static_cast<Base&
>(*this) = s;
416 template <
int i,
class Evaluators>
419 static_assert(boost::fusion::result_of::size<Evaluators>::type::value==boost::fusion::result_of::size<typename Descriptions::Spaces>::type::value,
420 "Evaluators have to match the space list");
421 constexpr int spaceIndex = Descriptions::template spaceIndex<i>;
422 return component<i>(*this).value(boost::fusion::at_c<spaceIndex>(evals));
434 return component<i>(*this).value(cell,xi);
446 return component<i>(*this).value(x);
452 template <
int i,
class Evaluators>
455 int const spaceIndex = Descriptions::template spaceIndex<i>;
456 return component<i>(*this).derivative(boost::fusion::at_c<spaceIndex>(evals));
465 return component<i>(*this).derivative(cell,xi);
475 return component<i>(*this).derivative(x);
481 template <
int i,
class Evaluators>
484 int const spaceIndex = Descriptions::template spaceIndex<i>;
485 return component<i>(*this).hessian(boost::fusion::at_c<spaceIndex>(evals));
494 return component<i>(*this).hessian(cell,xi);
504 return component<i>(*this).hessian(x);
535 template <
class SpaceList,
class VariableList>
543 using Variables =
typename Variables_Detail::ImplicitIdVariableDescriptions<VariableList>::type;
550 typedef typename Variables_Detail::VariableRangeCreator<Variables,ConstructElement<Spaces>>::type
RepresentationData;
578 static int const noOfVariables = boost::fusion::result_of::size<Variables>::type::value;
586 static int const continuity = Variables_Detail::Continuity<Variables,Spaces>::value;
591 typedef typename boost::fusion::result_of::as_vector<
592 typename boost::fusion::result_of::transform<Spaces, GetEvaluators<ShapeFunctionCache<Grid,Scalar>> >::type
601 template <
class RAIter>
617 assert(
names.size()==names_.size());
618 std::copy(names_.begin(),names_.end(),
names.begin());
624 template <
class RAIter>
637 std::copy(roles_.begin(),roles_.end(),
roles.begin());
674 assert(0<=first && first<=last && last<=dimensions.size());
675 return std::accumulate(begin(dimensions)+first,begin(dimensions)+last,0);
683 std::array<size_t,noOfVariables> dimensions;
684 boost::fusion::for_each(
Variables(),ComputeDimension<noOfVariables>(dimensions,
spaces));
692 constexpr static int components = boost::fusion::result_of::value_at_c<Variables,i>::type::m;
696 struct [[deprecated]]
Components {
static int const m = boost::fusion::result_of::value_at_c<Variables,idx>::type::m; };
714 constexpr static int spaceIndex = boost::fusion::result_of::value_at_c<Variables,idx>::type::spaceIndex;
720 struct [[deprecated]]
SpaceIndex {
static int const spaceIndex = boost::fusion::result_of::value_at_c<Variables,idx>::type::spaceIndex; };
727 using Space = std::remove_pointer_t<typename boost::fusion::result_of::value_at_c<Spaces,SpaceIndex<idx>::spaceIndex>::type>;
746 template <
int first=0,
int last=noOfVariables>
749 using Creator = Variables_Detail::VariableRangeCreator<Variables,ConstructCoefficientVector<Spaces>,first,last>;
779 template <
int first=0,
int last=noOfVariables>
785 template <
int first=0,
int last=noOfVariables>
789 return typename CVec::type(CVec::init(
spaces));
825 std::array<std::string,noOfVariables>
names;
827 std::array<std::string,noOfVariables>
roles;
831 struct CheckHasEqualGridAndIndexSet
837 template <
class Space>
838 void operator()(
Space const* space)
const {
839 assert(&(space->gridManager())== gp);
840 assert(&(space->indexSet()) == isp);
848 struct ComputeDimension
850 ComputeDimension(std::array<size_t,nvars>& dims,
Spaces const& spaces_):
851 dims_(dims),
spaces(spaces_)
854 template <
class Variable>
855 void operator()(Variable
const& )
const
857 assert(dims_.size()>Variable::id);
858 dims_[Variable::id] =
Variable::m * boost::fusion::at_c<Variable::spaceIndex>(
spaces)->degreesOfFreedom();
861 std::array<size_t,nvars>& dims_;
873 template <
class VarSetDesc,
int varIdx>
874 constexpr int spaceIndex = VarSetDesc::template spaceIndex<varIdx>;
891 template <
class ...Spaces>
894 return boost::fusion::make_vector(
const_cast<Spaces const*
>(spaces)...);
910 template <
class SpaceList,
class VariableList>
912 VariableList
const& vars)
917 std::vector<std::string> names;
918 for_each(vars,[&](
auto var) { names.push_back(var.name); });
920 for (
int i=0; i<names.size(); ++i)
921 if (names[i].empty())
947 template <
class Variables,
class Functions,
class Evaluators,
class Method>
950 Method
const& method = Method())
955 auto doEval = [&] (
auto const& pair)
958 static int const sid = std::remove_reference_t<
typename result_of::value_at_c<std::remove_reference_t<
decltype(pair)>,1>::type>::spaceIndex;
960 return method(at_c<0>(pair),at_c<sid>(eval));
965 using VarDesc =
typename result_of::as_vector<Variables>::type;
969 return as_vector(transform(zip(fs,VarDesc()),doEval));
987 template <
class VariableSet,
class Method>
990 Method
const& method = Method())
992 return evaluateFunctions<typename VariableSet::Descriptions::Variables>(vars.
data,eval,method);
1005 template <
class VariableSet,
class Method>
1007 std::declval<typename VariableSet::Descriptions::Evaluators>(),
1008 std::declval<Method>()));
1018 constexpr auto valueMethod = [](
auto const& f,
auto const& eval) {
return f.value(eval); };
1027 constexpr auto derivativeMethod = [](
auto const& f,
auto const& eval) {
return f.derivative(eval); };
Typedefs for coefficient vector representations of contiguous subranges of the variables.
static Creator::View init(Spaces const &spaces)
Returns a (small) initializer object for coefficient vectors.
A class that stores information about a set of variables.
GridView const & gridView
The grid view on which the variables live.
VariableSet variableSet() const
Returns a zero-initialized variable set.
std::remove_pointer_t< typename boost::fusion::result_of::value_at_c< Spaces, SpaceIndex< idx >::spaceIndex >::type > Space
The type of finite element space of the i-th variable.
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
A sequence of variable names.
IndexSet const & indexSet
index set
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
minimal continuity of any variable in the set
typename CoefficientVectorRepresentation< first, last >::type CoefficientVector
The type of coefficient vectors of variable sets (and subsets).
static constexpr int components
number of components of i'th variable
static int const noOfVariables
Number of variables in this set.
boost::fusion::result_of::as_vector< typenameboost::fusion::result_of::transform< Spaces, GetEvaluators< ShapeFunctionCache< Grid, Scalar > > >::type >::type Evaluators
The boost::fusion sequence of evaluators belonging to this variable set.
Variables_Detail::VariableRangeCreator< Variables, ConstructElement< Spaces > >::type RepresentationData
Type that contains a set of variables inside a boost vector, together with all the data.
SpaceList Spaces
type of boost::fusion::vector of FEFunctionSpace s needed
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, VariableList > > VariableSet
Type that contains a set of variable values with some functionality, such as simple vector arithmetic...
Spaces spaces
A heterogeneous sequence of pointers to (const) spaces involved.
typename Variables_Detail::ImplicitIdVariableDescriptions< VariableList >::type Variables
type of boost::fusion vector of VariableDescription s
SpaceType< Spaces, 0 >::type::Grid Grid
Grid type.
static constexpr int spaceIndex
space index of the i'th variable
SpaceType< Spaces, 0 >::type::Scalar Scalar
scalar field type
SpaceType< Spaces, 0 >::type::IndexSet IndexSet
IndexSet type.
SpaceType< Spaces, 0 >::type::GridView GridView
Grid view type.
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.
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.
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.
LinearProductSpace< Scalar, Functions > LinearSpace
Descriptions const & descriptions
Descriptions of variable set, of type VariableSetDescription (lots of useful infos)
auto value(Evaluators const &evals) const
Value of an individual variable in the set.
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.
decltype(evaluateVariables(std::declval< VariableSet >(), std::declval< typename VariableSet::Descriptions::Evaluators >(), std::declval< Method >())) EvaluateVariables
The type of evaluated variables as returned by evaluateVariables.
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.
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.
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(valueMethod) ValueMethod
The type of the valueMethod helper functor.
decltype(derivativeMethod) DerivativeMethod
The type of the derivativeMethod helper functor.
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< Var::m >::type::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.
SpaceType< Spaces, Var::spaceIndex >::type Space
Space::template Element< Var::m >::type type
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.
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.
Variable()=default
Constructor. This will create a default name based on the variable id.
static int const spaceIndex
DEPRECATED, use components instead.
DEPRECATED, use spaceIndex instead.