Newer
Older
subtitle: Flexible compile-time customization
# System Design
## Goals
- Easy to change parts of the simulation
- Add a energy equation
- Reuse specializations efficiently
- Test various discretization schemes for same application
- Group properties of the simulations
- Improves readability
## Template parameters
- C++ supports _generic programming_ via __templates__
- e.g. classes defined in terms of other types
- concrete versions of templates are stamped out upon compilation
- __Flexible__: implementation not restricted to _concrete types_
- __Efficient__: decisions made at compile-time
## Template parameters
An example - `std::vector`
```cpp
// Declaration of the class template, usable with any
// `T` that fulfills the requirements that `vector` poses on it.
template<typename T, typename A = std::allocator<T>>
class vector;
// Instantiation of a concrete vector - a vector of ints.
// The compiler will define this concrete type for us,
// using the definition of the class template.
std::vector<int> v;
```
## Template parameters
An example - `std::vector`
<img src="./img/template_example.png" width="800"/>
## Template specializations
Template implementations can be specialized for concrete types
```cpp
template<typename T>
class MyVector
{
// Generic implementation for any T
};
template<>
{
// specialized implementation for `int`
};
```
## Too many template parameters
For some classes, providing all template parameters can be very cumbersome and error-prone.
```cpp
// Example from dune-pdelab. 9 template parameters!
using GOF0 = Dune::GridOperator<
GFS, GFS, LOP, MBE,
RF, RF, RF,
CF, CF
>;
DGGO2 dggo2(gfs, cd, gfs, cf, lop, mbe);
```
## Traits classes
A usual way to group template parameters
```cpp
template<class PV, class FSY, class FST, class SSY, class SST, class PT, class MT, class SR>
struct TwoPVolumeVariablesTraits
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using PrimaryVariables = PV;
using FluidSystem = FSY;
using FluidState = FST;
using SolidSystem = SSY;
using SolidState = SST;
using PermeabilityType = PT;
using ModelTraits = MT;
using SaturationReconstruction = SR;
};
```
## Traits classes
Making it usable using a single template parameter
```c++
template<class TypeTag>
struct VolumeVariables<TypeTag, TTag::TwoP>
{
private:
using PV = GetPropType<TypeTag, Properties::PrimaryVariables>;
using FSY = GetPropType<TypeTag, Properties::FluidSystem>;
using FST = GetPropType<TypeTag, Properties::FluidState>;
using SSY = GetPropType<TypeTag, Properties::SolidSystem>;
using SST = GetPropType<TypeTag, Properties::SolidState>;
using MT = GetPropType<TypeTag, Properties::ModelTraits>;
using PT = typename GetPropType<TypeTag, Properties::SpatialParams>::PermeabilityType;
using DM = typename GetPropType<TypeTag, Properties::GridGeometry>::DiscretizationMethod;
static constexpr bool enableIS = getPropValue<TypeTag, Properties::EnableBoxInterfaceSolver>();
// class used for scv-wise reconstruction of nonwetting phase saturations
using SR = TwoPScvSaturationReconstruction<DM, enableIS>;
using Traits = TwoPVolumeVariablesTraits<PV, FSY, FST, SSY, SST, PT, MT, SR>;
public:
using type = TwoPVolumeVariables<Traits>;
};
```
## Inheriting from traits classes
Inheritance may lead to unexpected results
```cpp
struct MyBaseTraits
{
using Scalar = int;
using Vector = std::vector<Scalar>;
};
struct MyDoubleTraits : public MyBaseTraits
{
using Scalar = double;
};
// this is a vector of ints!
typename MyDoubleTraits::Vector v{1.14142, 1.73205};
```
## Type traits
Based on template specialization
```cpp
template<typename T> struct ValueType;
// Specialization for vectors of T
template<typename T, typename Allocator>
struct ValueType<std::vector<T, Allocator>> { using type = T; };
// Specialization for Dune::FieldVector
template<typename T, int size>
struct ValueType<Dune::FieldVector<T, size>> { using type = T; };
```
# The DuMuX Property System
## Property System Design
- Based on __C++ template specialization__ (_type traits_)
- From a __type tag__, one can extract __properties__ defined for it
- A __property tag__ is a type trait declaration (or default implementation)
- A __property__ is exported from a __property tag__ specialization for a __type tag__
- (The property system also supports definitions of traits classes - see later)
A simplified example to illustrate the idea
namespace TTag { struct MyTypeTag {}; }
template<typename TypeTag> struct PropTagA;
// property definition for MyTypeTag
template<>
{ using type = /*the actual property*/; };
```
## Property System Design
A simplified example to illustrate the idea
```cpp
template<class TypeTag>
class GenericClass
{
using PropA = typename Properties::PropTagA<TypeTag>::type;
using PropB = typename Properties::PropTagB<TypeTag>::type;
// ...
// A property could be, for instance, a fluid system
using FS = typename Properties::FluidSystem<TypeTag>::type;
};
```
## Property System Design
__Issue__: Inheritance not (easily) possible. All type traits need to be specialized for `MyTypeTag`.
__Goal__: We would like __type tags__ to be composable via inheritance, while providing a mechanism for customizing any property defined in the hierarchy.
## Actual Design
- A hierarchy of nodes -- called __type tags__ -- is defined (via inheritance)
- __Properties__ are defined for the appropriate nodes in this hierarchy.
- The definition of __properties__ may _depend on_ arbitrary other properties, which may be _overwritten_ at any higher node of the hierarchy
- The only requirement for properties is that they may not exhibit __cyclic dependencies__
## Actual Design
Let's implement the `Vector` example using the property system
```cpp
namespace TTag { struct BaseTag {}; }
// specialization of the Scalar property for BaseTag
template<class TypeTag>
struct Scalar<TypeTag, TTag::BaseTag> { using type = int; };
// specialization of the Vector property for BaseTag
template<class TypeTag>
struct Vector<TypeTag, TTag::BaseTag> {
private: using Scalar = GetPropType<TypeTag, Properties::Scalar>;
public: using type = std::vector<Scalar>;
## Actual Design
Let's implement the `Vector` example using the property system
namespace Properties {
namespace TTag {
struct DoubleTag { using InheritsFrom = std::tuple<BaseTag>; };
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Specialization of the Scalar property for DoubleTag
template<class TypeTag>
struct Scalar<TypeTag, TTag::DoubleTag>
{ using type = double; };
```
```cpp
// using the property
using Vector = GetPropType<DoubleTag, Properties::Vector>;
Vector v{1.41421, 1.73205}; // v is a std::vector<double>!
```
## Using __type tags__ as traits classes
```cpp
struct BaseTag
{
using Scalar = double;
// Important: do not directly use Scalar here as it would
// break the possibility to overwrite it in a child node
template<typename TypeTag>
using Vector = std::vector<
GetPropType<TypeTag, Properties::Scalar>
>;
};
```
## The DuMu<sup>x</sup> property system
- Extension $\leftrightarrow$ tree of so called TypeTag nodes
- Each TypeTag is associated with Properties

## The DuMu<sup>x</sup> property system
- Hierarchy / Inheritance
- TypeTags can inherit properties from other TypeTags
- Properties with the same name are overwritten

## How to use I
Creating new <span style="color:blue">TypeTag</span> nodes
```cpp
namespace Dumux::Properties::TTag {
struct MyOtherTypeTag
{ using InheritsFrom = std::tuple<MyTypeTag>; }
} // end namespace Dumux::Properties::TTag
```
## How to use II
Creating new <span style="color:blue">property tags</span> (empty, unset properties) $\leftrightarrow$ Property names are unique!
```cpp
} // end namespace Dumux::Properties
Usually not needed in user code because
all necessary properties are already defined in [dumux/common/properties.hh](https://git.iws.uni-stuttgart.de/dumux-repositories/dumux/-/blob/master/dumux/common/properties.hh).
Setting **type** properties for a specific type tag `MyTypeTag`
```cpp
namespace Properties {
template<class TypeTag>
struct Problem<TypeTag, TTag::MyTypeTag>
{ using type = Dumux::MyProblem<TypeTag>; };
} // end namespace Properties
```
## How to use III (alternative)
Alternatively, using **type** alias properties for a specific type tag `MyTypeTag`
```cpp
namespace Properties::TTag {
struct MyTypeTag {
...
template<class TypeTag>
using Problem = Dumux::MyProblem<TypeTag>;
...
};
} // end namespace Properties
```
Setting **value** properties for a specific type tag `MyTypeTag`
```cpp
namespace Properties{
template<class TypeTag>
struct EnableBoxInterfaceSolver<TypeTag, TTag::MyTypeTag>
{ static constexpr bool value = true; }
} // end namespace Properties
```
## How to use IV (alternative)
Setting **value** for a alias property for a specific type tag `MyTypeTag`
```cpp
namespace Properties {
struct MyTypeTag {
...
using EnableBoxInterfaceSolver = std::true_type;
...
};
} // end namespace Properties
```
```cpp
namespace Dumux{
template <class TypeTag>
class Problem
{
using Scalar = GetPropType<TypeTag, Properties::Scalar>;
constexpr auto useIFS = getPropValue<
TypeTag, Properties::EnableBoxInterfaceSolver
>();
};
} // end namespace Dumux
```
## Summary
- "Top-level" classes in DuMu<sup>x</sup> depend on a __type tag__ and use the property system to obtain other types
- Setting a property for your __type tag__ will affect all classes using the same __type tag__
- Each model defines a set of properties grouped in a __type tag__
* e.g. <span style="color:blue">TwoP, TwoPTwoC, TwoPNI</span>
- By deriving your __type tag__ from those, your problem inherits all type information needed to set up the model at compile time!
- Example: see Exercise