From 221311fbab97420f975cea57cb09d6df963ab38e Mon Sep 17 00:00:00 2001 From: Ivan Buntic <st116086@stud.uni-stuttgart.de> Date: Tue, 26 Oct 2021 14:38:12 +0200 Subject: [PATCH] [disc] Introduce discretization tags in namespace DiscretizationMethods Tags have some advantages over enums. There are separate types and allow for things like tag dispatch. They are also extensible. That means you can now define your own discretization tag in a downstream module and specialize classes for it. With enums you always need to modify the dumux core, i.e. add the enum to the list of discretization method in this header. --- dumux/discretization/method.hh | 122 ++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/dumux/discretization/method.hh b/dumux/discretization/method.hh index 255c0061fa..8da080bdd7 100644 --- a/dumux/discretization/method.hh +++ b/dumux/discretization/method.hh @@ -27,6 +27,8 @@ #include <ostream> #include <string> +#include <dumux/common/tag.hh> + namespace Dumux { /*! @@ -41,10 +43,112 @@ enum class DiscretizationMethod none, box, cctpfa, ccmpfa, staggered, fem, fcstaggered }; + +namespace DiscretizationMethods { + +struct CCTpfa : public Utility::Tag<CCTpfa> { + static std::string name() { return "cctpfa"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::cctpfa; + } +}; + + +struct CCMpfa : public Utility::Tag<CCMpfa> { + static std::string name() { return "ccmpfa"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::ccmpfa; + } +}; + + +struct Box : public Utility::Tag<Box> { + static std::string name() { return "box"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::box; + } +}; + + + +struct Staggered : public Utility::Tag<Staggered> { + static std::string name() { return "staggered"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::staggered; + } +}; + + + +struct FEM : public Utility::Tag<FEM> { + static std::string name() { return "fem"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::fem; + } +}; + + + +struct FCStaggered : public Utility::Tag<FCStaggered> { + static std::string name() { return "fcstaggered"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::fcstaggered; + } +}; + + + +struct None : public Utility::Tag<None> { + static std::string name() { return "none"; } + + //conversion operator + [[deprecated("Conversion to enum is deprecated. Will be removed after 3.5. Use tags.")]] + constexpr operator DiscretizationMethod() const + { + return DiscretizationMethod::none; + } +}; + + +inline constexpr CCTpfa cctpfa{}; +inline constexpr CCMpfa ccmpfa{}; +inline constexpr Box box{}; +inline constexpr Staggered staggered{}; +inline constexpr FEM fem{}; +inline constexpr FCStaggered fcstaggered{}; +inline constexpr None none{}; + +} // end namespace DiscretizationMethods + /*! * \brief Convert discretization method to string * \ingroup Discretization */ +[[deprecated("Use discretization tags and their name attribute. Will be removed after 3.5")]] inline std::string toString(DiscretizationMethod m) { switch (m) @@ -63,8 +167,24 @@ inline std::string toString(DiscretizationMethod m) * \brief Write discretization method to stream * \ingroup Discretization */ +[[deprecated("Use discretization tags which also support operator <<. Will be removed after 3.5")]] inline std::ostream& operator<<(std::ostream& stream, DiscretizationMethod m) -{ stream << toString(m); return stream; } +{ + // get rid of deprecation warning in the transition period + const auto toStringImpl = [](DiscretizationMethod m){ + switch (m) + { + case DiscretizationMethod::box: return "box"; + case DiscretizationMethod::cctpfa: return "cctpfa"; + case DiscretizationMethod::ccmpfa: return "ccmpfa"; + case DiscretizationMethod::fcstaggered: return "fcstaggered"; + case DiscretizationMethod::fem: return "fem"; + case DiscretizationMethod::staggered: return "staggered"; + default: return "none"; + } + }; + stream << toStringImpl(m); return stream; +} } // end namespace Dumux -- GitLab