From a39afeaa83f9d5df6fbdebd7abc1406425f6875c Mon Sep 17 00:00:00 2001 From: Timo Koch <timo.koch@iws.uni-stuttgart.de> Date: Tue, 31 Mar 2020 21:36:10 +0200 Subject: [PATCH] [examples][swe] Separate properties into separate header --- examples/shallowwaterfriction/.doc_config | 1 + examples/shallowwaterfriction/README.md | 216 ++++++++------------ examples/shallowwaterfriction/main.cc | 4 +- examples/shallowwaterfriction/problem.hh | 80 +------- examples/shallowwaterfriction/properties.hh | 87 ++++++++ 5 files changed, 188 insertions(+), 200 deletions(-) create mode 100644 examples/shallowwaterfriction/properties.hh diff --git a/examples/shallowwaterfriction/.doc_config b/examples/shallowwaterfriction/.doc_config index ae4ff6277f..7ac7201b34 100644 --- a/examples/shallowwaterfriction/.doc_config +++ b/examples/shallowwaterfriction/.doc_config @@ -1,6 +1,7 @@ { "README.md" : [ "doc/intro.md", + "properties.hh", "spatialparams.hh", "problem.hh", "main.cc" diff --git a/examples/shallowwaterfriction/README.md b/examples/shallowwaterfriction/README.md index 52e9a2d10d..0bfd204ded 100644 --- a/examples/shallowwaterfriction/README.md +++ b/examples/shallowwaterfriction/README.md @@ -95,6 +95,89 @@ for the simulation are given in the file `params.input`. # Implementation +## The file `properties.hh` + +The header includes will be mentioned in the text below. +<details><summary>Click to show the header includes</summary> + +```cpp +#include <dune/grid/yaspgrid.hh> + +#include <dumux/common/properties.hh> +#include <dumux/discretization/cctpfa.hh> +#include <dumux/freeflow/shallowwater/model.hh> + +#include "spatialparams.hh" +#include "problem.hh" +``` + +</details> + +Let's define the properties for our simulation + +```cpp +namespace Dumux::Properties { +``` + +First, a so-called TypeTag is created. Properties are traits specialized for this TypeTag (a simple `struct`). +The properties of two other TypeTags are inherited by adding the alias `InheritsFrom`. +Here, properties from the shallow water model (`TTag::ShallowWater`) and the +cell-centered finite volume scheme with two-point-flux approximation (`TTag::CCTpfaModel`) +are inherited. These other TypeTag definitions can be found in the included +headers `dumux/freeflow/shallowwater/model.hh` and `dumux/discretization/cctpfa.hh`. + +```cpp +namespace TTag { +struct RoughChannel { using InheritsFrom = std::tuple<ShallowWater, CCTpfaModel>; }; +} +``` + +We use a structured Cartesian grid with tensor product structure. +`Dune::YaspGrid` (Yet Another Structure Parallel Grid) is defined in `dune/grid/yaspgrid.hh` +in the Dune module `dune-grid`. + +```cpp +template<class TypeTag> +struct Grid<TypeTag, TTag::RoughChannel> +{ using type = Dune::YaspGrid<2, Dune::TensorProductCoordinates<GetPropType<TypeTag, Properties::Scalar>, 2> >; }; +``` + +Next, we specialize the properties `Problem` and `SpatialParams` for our new TypeTag and +set the type to our problem and spatial parameter classes implemented +in `problem.hh` and `spatialparams.hh`. + +```cpp +template<class TypeTag> +struct Problem<TypeTag, TTag::RoughChannel> +{ using type = Dumux::RoughChannelProblem<TypeTag>; }; + +template<class TypeTag> +struct SpatialParams<TypeTag, TTag::RoughChannel> +{ + using GridGeometry = GetPropType<TypeTag, Properties::GridGeometry>; + using Scalar = GetPropType<TypeTag, Properties::Scalar>; + using ElementVolumeVariables = typename GetPropType<TypeTag, Properties::GridVolumeVariables>::LocalView; + using VolumeVariables = typename ElementVolumeVariables::VolumeVariables; + + using type = RoughChannelSpatialParams<GridGeometry, Scalar, VolumeVariables>; +}; +``` + +Finally, we enable caching for the grid geometry. The cache +stores values that were already calculated for later usage. +This makes the simulation run faster but it uses more memory. + +```cpp +template<class TypeTag> +struct EnableGridGeometryCache<TypeTag, TTag::RoughChannel> +{ static constexpr bool value = true; }; + +} // end namespace Dumux::Properties +``` + + + + ## The file `spatialparams.hh` @@ -238,136 +321,21 @@ end of namespace Dumux. ## The file `problem.hh` - - -## Include files -We use the dune yasp grid. - -```cpp -#include <dune/grid/yaspgrid.hh> -``` - -We include the cell centered, two-point-flux discretization scheme. - -```cpp -#include <dumux/discretization/cctpfa.hh> -``` - -The parameters header is needed to retrieve run-time parameters. +We start with includes ```cpp #include <dumux/common/parameters.hh> -``` - -We include the header which are needed for shallow water models. - -```cpp -#include <dumux/freeflow/shallowwater/model.hh> +#include <dumux/common/properties.hh> #include <dumux/freeflow/shallowwater/problem.hh> #include <dumux/freeflow/shallowwater/boundaryfluxes.hh> ``` -We include the header that specifies all spatially variable parameters. - -```cpp -#include "spatialparams.hh" -``` - -## Define basic properties for our simulation -We enter the namespace Dumux. All Dumux functions and classes are in a namespace Dumux, to make sure they don't clash with symbols from other libraries you may want to use in conjunction with Dumux. One could use these functions and classes by prefixing every use of these names by ::, but that would quickly become cumbersome and annoying. Rather, we simply import the entire Dumux namespace for general use. - -```cpp -namespace Dumux { -``` - -The problem class is forward declared. - -```cpp -template <class TypeTag> -class RoughChannelProblem; -``` - -We enter the namespace Properties, which is a sub-namespace of the namespace Dumux. - -```cpp -namespace Properties { -``` - -A TypeTag for our simulation is created which inherits from the shallow water model and the -cell centered, two-point-flux discretization scheme. - -```cpp -namespace TTag { -struct RoughChannel { using InheritsFrom = std::tuple<ShallowWater, CCTpfaModel>; }; -} -``` - -We define the grid of our simulation. We use a two-dimensional Yasp Grid. - -```cpp -template<class TypeTag> -struct Grid<TypeTag, TTag::RoughChannel> -{ using type = Dune::YaspGrid<2, Dune::TensorProductCoordinates<GetPropType<TypeTag, Properties::Scalar>, 2> >; }; -``` - -We set the problem. The problem class specifies initial and boundary conditions and is defined below. - -```cpp -template<class TypeTag> -struct Problem<TypeTag, TTag::RoughChannel> -{ using type = Dumux::RoughChannelProblem<TypeTag>; }; -``` - -We define the spatial parameters for our simulation. The values are specified in the corresponding spatialparameters header file, which is included above. - -```cpp -template<class TypeTag> -struct SpatialParams<TypeTag, TTag::RoughChannel> -{ -private: -``` - -We define convenient shortcuts to the properties GridGeometry, Scalar, ElementVolumeVariables and VolumeVariables: - -```cpp - using GridGeometry = GetPropType<TypeTag, Properties::GridGeometry>; - using Scalar = GetPropType<TypeTag, Properties::Scalar>; - using ElementVolumeVariables = typename GetPropType<TypeTag, Properties::GridVolumeVariables>::LocalView; - using VolumeVariables = typename ElementVolumeVariables::VolumeVariables; -``` - -Finally we set the spatial parameters: - -```cpp -public: - using type = RoughChannelSpatialParams<GridGeometry, Scalar, VolumeVariables>; -}; -``` - -We enable caching for the FV grid geometry and the grid volume variables. The cache -stores values that were already calculated for later usage. This makes the simulation faster. - -```cpp -template<class TypeTag> -struct EnableGridGeometryCache<TypeTag, TTag::RoughChannel> -{ static constexpr bool value = true; }; - -template<class TypeTag> -struct EnableGridVolumeVariablesCache<TypeTag, TTag::RoughChannel> -{ static constexpr bool value = false; }; -``` - -We leave the namespace Properties. - -```cpp -} -``` - -## The problem class We enter the problem class where all necessary boundary conditions and initial conditions are set for our simulation. As this is a shallow water problem, we inherit from the basic ShallowWaterProblem. ```cpp +namespace Dumux { + template <class TypeTag> class RoughChannelProblem : public ShallowWaterProblem<TypeTag> { @@ -711,12 +679,8 @@ eps is used as a small value for the definition of the boundry conditions static constexpr Scalar eps_ = 1.0e-6; std::string name_; }; -``` - -We leave the namespace Dumux. -```cpp -} +} // end namespace Dumux ``` @@ -803,10 +767,10 @@ Further we include assembler, which assembles the linear systems for finite volu #include <dumux/assembly/fvassembler.hh> ``` -We include the problem file which defines initial and boundary conditions to describe our example problem +We include the properties ```cpp -#include "problem.hh" +#include "properties.hh" ``` ### Beginning of the main function diff --git a/examples/shallowwaterfriction/main.cc b/examples/shallowwaterfriction/main.cc index dd4ef4951a..de91945bb9 100644 --- a/examples/shallowwaterfriction/main.cc +++ b/examples/shallowwaterfriction/main.cc @@ -54,8 +54,8 @@ #include <dumux/nonlinear/newtonsolver.hh> // Further we include assembler, which assembles the linear systems for finite volume schemes (box-scheme, tpfa-approximation, mpfa-approximation) #include <dumux/assembly/fvassembler.hh> -// We include the problem file which defines initial and boundary conditions to describe our example problem -#include "problem.hh" +// We include the properties +#include "properties.hh" // ### Beginning of the main function int main(int argc, char** argv) try diff --git a/examples/shallowwaterfriction/problem.hh b/examples/shallowwaterfriction/problem.hh index 5da2f73a7a..12bfbe3261 100644 --- a/examples/shallowwaterfriction/problem.hh +++ b/examples/shallowwaterfriction/problem.hh @@ -17,84 +17,20 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * *****************************************************************************/ -#ifndef DUMUX_ROUGH_CHANNEL_TEST_PROBLEM_HH -#define DUMUX_ROUGH_CHANNEL_TEST_PROBLEM_HH +#ifndef DUMUX_EXAMPLE_SHALLOWWATER_FRICTION_PROBLEM_HH +#define DUMUX_EXAMPLE_SHALLOWWATER_FRICTION_PROBLEM_HH // ## The file `problem.hh` -// -// -// ## Include files -// We use the dune yasp grid. -#include <dune/grid/yaspgrid.hh> -// We include the cell centered, two-point-flux discretization scheme. -#include <dumux/discretization/cctpfa.hh> -// The parameters header is needed to retrieve run-time parameters. +// We start with includes #include <dumux/common/parameters.hh> - -// We include the header which are needed for shallow water models. -#include <dumux/freeflow/shallowwater/model.hh> +#include <dumux/common/properties.hh> #include <dumux/freeflow/shallowwater/problem.hh> #include <dumux/freeflow/shallowwater/boundaryfluxes.hh> -// We include the header that specifies all spatially variable parameters. -#include "spatialparams.hh" - -// ## Define basic properties for our simulation -// We enter the namespace Dumux. All Dumux functions and classes are in a namespace Dumux, to make sure they don't clash with symbols from other libraries you may want to use in conjunction with Dumux. One could use these functions and classes by prefixing every use of these names by ::, but that would quickly become cumbersome and annoying. Rather, we simply import the entire Dumux namespace for general use. -namespace Dumux { -// The problem class is forward declared. -template <class TypeTag> -class RoughChannelProblem; - -// We enter the namespace Properties, which is a sub-namespace of the namespace Dumux. -namespace Properties { - -// A TypeTag for our simulation is created which inherits from the shallow water model and the -// cell centered, two-point-flux discretization scheme. -namespace TTag { -struct RoughChannel { using InheritsFrom = std::tuple<ShallowWater, CCTpfaModel>; }; -} -// We define the grid of our simulation. We use a two-dimensional Yasp Grid. -template<class TypeTag> -struct Grid<TypeTag, TTag::RoughChannel> -{ using type = Dune::YaspGrid<2, Dune::TensorProductCoordinates<GetPropType<TypeTag, Properties::Scalar>, 2> >; }; - -// We set the problem. The problem class specifies initial and boundary conditions and is defined below. -template<class TypeTag> -struct Problem<TypeTag, TTag::RoughChannel> -{ using type = Dumux::RoughChannelProblem<TypeTag>; }; - -// We define the spatial parameters for our simulation. The values are specified in the corresponding spatialparameters header file, which is included above. -template<class TypeTag> -struct SpatialParams<TypeTag, TTag::RoughChannel> -{ -private: - // We define convenient shortcuts to the properties GridGeometry, Scalar, ElementVolumeVariables and VolumeVariables: - using GridGeometry = GetPropType<TypeTag, Properties::GridGeometry>; - using Scalar = GetPropType<TypeTag, Properties::Scalar>; - using ElementVolumeVariables = typename GetPropType<TypeTag, Properties::GridVolumeVariables>::LocalView; - using VolumeVariables = typename ElementVolumeVariables::VolumeVariables; - // Finally we set the spatial parameters: -public: - using type = RoughChannelSpatialParams<GridGeometry, Scalar, VolumeVariables>; -}; - -// We enable caching for the FV grid geometry and the grid volume variables. The cache -// stores values that were already calculated for later usage. This makes the simulation faster. -template<class TypeTag> -struct EnableGridGeometryCache<TypeTag, TTag::RoughChannel> -{ static constexpr bool value = true; }; - -template<class TypeTag> -struct EnableGridVolumeVariablesCache<TypeTag, TTag::RoughChannel> -{ static constexpr bool value = false; }; - -// We leave the namespace Properties. -} - -// ## The problem class // We enter the problem class where all necessary boundary conditions and initial conditions are set for our simulation. // As this is a shallow water problem, we inherit from the basic ShallowWaterProblem. +namespace Dumux { + template <class TypeTag> class RoughChannelProblem : public ShallowWaterProblem<TypeTag> { @@ -321,7 +257,7 @@ private: static constexpr Scalar eps_ = 1.0e-6; std::string name_; }; -// We leave the namespace Dumux. -} + +} // end namespace Dumux #endif diff --git a/examples/shallowwaterfriction/properties.hh b/examples/shallowwaterfriction/properties.hh new file mode 100644 index 0000000000..d1e87e9aa4 --- /dev/null +++ b/examples/shallowwaterfriction/properties.hh @@ -0,0 +1,87 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/***************************************************************************** + * See the file COPYING for full copying permissions. * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *****************************************************************************/ + +#ifndef DUMUX_EXAMPLE_SHALLOWWATER_FRICTION_PROPERTIES_HH +#define DUMUX_EXAMPLE_SHALLOWWATER_FRICTION_PROPERTIES_HH + +// ## The file `properties.hh` +// +// The header includes will be mentioned in the text below. +// <details><summary>Click to show the header includes</summary> +#include <dune/grid/yaspgrid.hh> + +#include <dumux/common/properties.hh> +#include <dumux/discretization/cctpfa.hh> +#include <dumux/freeflow/shallowwater/model.hh> + +#include "spatialparams.hh" +#include "problem.hh" +// </details> +// + +// Let's define the properties for our simulation +namespace Dumux::Properties { + +// First, a so-called TypeTag is created. Properties are traits specialized for this TypeTag (a simple `struct`). +// The properties of two other TypeTags are inherited by adding the alias `InheritsFrom`. +// Here, properties from the shallow water model (`TTag::ShallowWater`) and the +// cell-centered finite volume scheme with two-point-flux approximation (`TTag::CCTpfaModel`) +// are inherited. These other TypeTag definitions can be found in the included +// headers `dumux/freeflow/shallowwater/model.hh` and `dumux/discretization/cctpfa.hh`. +namespace TTag { +struct RoughChannel { using InheritsFrom = std::tuple<ShallowWater, CCTpfaModel>; }; +} + +// We use a structured Cartesian grid with tensor product structure. +// `Dune::YaspGrid` (Yet Another Structure Parallel Grid) is defined in `dune/grid/yaspgrid.hh` +// in the Dune module `dune-grid`. +template<class TypeTag> +struct Grid<TypeTag, TTag::RoughChannel> +{ using type = Dune::YaspGrid<2, Dune::TensorProductCoordinates<GetPropType<TypeTag, Properties::Scalar>, 2> >; }; + +// Next, we specialize the properties `Problem` and `SpatialParams` for our new TypeTag and +// set the type to our problem and spatial parameter classes implemented +// in `problem.hh` and `spatialparams.hh`. +// [[codeblock]] +template<class TypeTag> +struct Problem<TypeTag, TTag::RoughChannel> +{ using type = Dumux::RoughChannelProblem<TypeTag>; }; + +template<class TypeTag> +struct SpatialParams<TypeTag, TTag::RoughChannel> +{ + using GridGeometry = GetPropType<TypeTag, Properties::GridGeometry>; + using Scalar = GetPropType<TypeTag, Properties::Scalar>; + using ElementVolumeVariables = typename GetPropType<TypeTag, Properties::GridVolumeVariables>::LocalView; + using VolumeVariables = typename ElementVolumeVariables::VolumeVariables; + + using type = RoughChannelSpatialParams<GridGeometry, Scalar, VolumeVariables>; +}; +// [[/codeblock]] + +// Finally, we enable caching for the grid geometry. The cache +// stores values that were already calculated for later usage. +// This makes the simulation run faster but it uses more memory. +template<class TypeTag> +struct EnableGridGeometryCache<TypeTag, TTag::RoughChannel> +{ static constexpr bool value = true; }; + +} // end namespace Dumux::Properties + +#endif -- GitLab