DUNE-ACFEM (2.5.1)

piecewisefunction.hh
1 #ifndef DUNE_ACFEM_PIECEWISEFUNCTION_HH
2 #define DUNE_ACFEM_PIECEWISEFUNCTION_HH
3 
4 #include "../functions/localfunctionwrapper.hh"
5 #include "../functions/gridfunctionexpression.hh"
6 
7 namespace Dune {
8  namespace ACFem {
9 
32  template<class CharacteristicFunction, class GridFunction0, class GridFunction1>
34  {
35  typedef CharacteristicFunction CharacteristicFunctionType;
36  typedef typename CharacteristicFunctionType::DiscreteFunctionSpaceType DiscreteCharacteristicFunctionSpaceType;
37  typedef typename CharacteristicFunctionType::LocalFunctionType LocalCharacteristicFunctionType;
38 
39  typedef GridFunction0 PiecewiseFunctionType0;
40  typedef typename PiecewiseFunctionType0::DiscreteFunctionSpaceType DiscreteFunctionSpaceType0;
41  typedef typename PiecewiseFunctionType0::LocalFunctionType LocalPiecewiseFunctionType0;
42  typedef GridFunction1 PiecewiseFunctionType1;
43  typedef typename PiecewiseFunctionType1::DiscreteFunctionSpaceType DiscreteFunctionSpaceType1;
44  typedef typename PiecewiseFunctionType1::LocalFunctionType LocalPiecewiseFunctionType1;
45 
46  public:
47  typedef typename DiscreteFunctionSpaceType0::FunctionSpaceType FunctionSpaceType;
48 
49  static_assert(std::is_same<FunctionSpaceType, typename DiscreteFunctionSpaceType1::FunctionSpaceType>::value,
50  "Piecewise Adapter needs same function space for underlying functions");
51  typedef typename FunctionSpaceType::ScalarFunctionSpaceType ScalarFunctionSpaceType;
52  static_assert(std::is_same<ScalarFunctionSpaceType, typename DiscreteCharacteristicFunctionSpaceType::FunctionSpaceType>::value,
53  "Piecewise Adapter needs a scalar characteristic function");
54 
55  static_assert(std::is_base_of<Fem::HasLocalFunction, CharacteristicFunction>::value,
56  "Missing HasLocalFunction property in CharacteristicFunction");
57  static_assert(std::is_base_of<Fem::HasLocalFunction, GridFunction0>::value,
58  "Missing HasLocalFunction property in GridFunction0");
59  static_assert(std::is_base_of<Fem::HasLocalFunction, GridFunction1>::value,
60  "Missing HasLocalFunction property in GridFunction1");
61 
62  typedef typename CharacteristicFunction::GridPartType GridPartType;
63  typedef typename GridPartType::template Codim<0>::EntityType EntityType;
64 
65  typedef typename FunctionSpaceType::RangeType RangeType;
66  typedef typename FunctionSpaceType::JacobianRangeType JacobianRangeType;
67  typedef typename FunctionSpaceType::HessianRangeType HessianRangeType;
68 
69  LocalPiecewiseAdapter(const Fem::Function<ScalarFunctionSpaceType,
70  CharacteristicFunctionType>& characteristic,
71  const Fem::Function<FunctionSpaceType,
72  PiecewiseFunctionType0>& function0,
73  const Fem::Function<FunctionSpaceType,
74  PiecewiseFunctionType1>& function1,
75  const std::string& name = "")
76  : characteristicFunction_(characteristic),
77  localCharacteristicFunction_(characteristicFunction_()),
78  function0_(function0),
79  localFunction0_(function0_()),
80  function1_(function1),
81  localFunction1_(function1_()),
82  name_(name == "" ? "(" + characteristicFunction_().name() + "*" + function1_().name() + "+" + "(1-" + characteristicFunction_().name() + ")*" + function0_().name() +")" : name)
83  {}
84 
86  : characteristicFunction_(other.characteristicFunction_()),
87  localCharacteristicFunction_(characteristicFunction_()),
88  function0_(other.function0_()),
89  localFunction0_(function0_()),
90  function1_(other.function1_()),
91  localFunction1_(function1_()),
92  name_(other.name_)
93  {}
94 
95  const std::string& name() const { return name_; }
96 
97  // Implement needed interface method for the adapter class
98  void init(const EntityType& entity)
99  {
100  localCharacteristicFunction_.init(entity);
101  localFunction0_.init(entity);
102  localFunction1_.init(entity);
103  }
104 
106  template<class PointType>
107  void evaluate(const PointType& x, RangeType& ret) const
108  {
109  typename ScalarFunctionSpaceType::RangeType indicator;
110  localCharacteristicFunction_.evaluate(x, indicator);
111  if (indicator > 0.5)
112  localFunction1_.evaluate(x, ret);
113  else
114  localFunction0_.evaluate(x, ret);
115  }
116 
118  template<class PointType>
119  void jacobian(const PointType& x, JacobianRangeType& ret) const
120  {
121 #if __DUNE_ACFEM_MAKE_CHECK__
122  // in order not to disturb the test-suite, which greedily takes hessians from anything
123  ret = 0;
124 #else
125  typename ScalarFunctionSpaceType::RangeType indicator;
126  localCharacteristicFunction_.evaluate(x, indicator);
127  if (indicator > 0.5)
128  localFunction1_.jacobian(x, ret);
129  else
130  localFunction0_.jacobian(x, ret);//DUNE_THROW(NotImplemented, "Jacobian of the piecewise of a given function is a singularity.");
131 #endif
132  }
133 
134  // hessian of local function
135  template<class PointType>
136  void hessian(const PointType& x, HessianRangeType& ret) const
137  {
138 #if __DUNE_ACFEM_MAKE_CHECK__
139  // in order not to disturb the test-suite, which greedily takes hessians from anything
140  ret = 0;
141 #else
142  DUNE_THROW(NotImplemented, "Hessian of the piecewise of a given function is NEVER implemented.");
143 #endif
144  }
145 
146  int order() const
147  {
148  // TODO: make use of characteristic function here.
149  // what about boundary elements?
150  return std::max(localFunction0_.order(), localFunction1_.order());
151  }
152 
153  protected:
154  ExpressionStorage<CharacteristicFunctionType> characteristicFunction_;
155  mutable LocalCharacteristicFunctionType localCharacteristicFunction_;
156  ExpressionStorage<PiecewiseFunctionType0> function0_;
157  mutable LocalPiecewiseFunctionType0 localFunction0_;
158  ExpressionStorage<PiecewiseFunctionType1> function1_;
159  mutable LocalPiecewiseFunctionType1 localFunction1_;
160  const std::string name_;
161  };
162 
164 
166 
167  } // ACFem::
168 
169 } // Dune::
170 
171 
172 #endif // DUNE_ACFEM_PIECEWISEFUNCTION_HH
An adapter class to form a piecewise defined function where the domain decomposition is given by the ...
Definition: piecewisefunction.hh:34
void evaluate(const PointType &x, RangeType &ret) const
evaluate local function
Definition: piecewisefunction.hh:107
void jacobian(const PointType &x, JacobianRangeType &ret) const
jacobian of local function
Definition: piecewisefunction.hh:119
LocalFunctionWrapper< LocalMaxAdapter< GridFunction1, GridFunction2 >, typename GridFunction1::GridPartType > max(const Fem::Function< typename GridFunction1::FunctionSpaceType, GridFunction1 > &f1, const Fem::Function< typename GridFunction2::FunctionSpaceType, GridFunction2 > &f2, const std::string &name="")
Pointwise maximum of two given functions.
Definition: maxfunction.hh:121
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)