diff --git a/src/enum_parser.cc b/src/enum_parser.cc new file mode 100644 index 0000000000000000000000000000000000000000..d8bcd06c9bde519a458e2f0ee939c6fdf6b6b279 --- /dev/null +++ b/src/enum_parser.cc @@ -0,0 +1,23 @@ +// Copyright Carsten Graeser 2012 + +#include <dune/common/exceptions.hh> +#include <dune/common/typetraits.hh> + +template <class EnumType> struct StringToEnum : public Dune::NotImplemented {}; + +template <class EnumType> +typename Dune::enable_if< + !Dune::IsBaseOf<Dune::NotImplemented, StringToEnum<EnumType>>::value, + std::istream &>::type +operator>>(std::istream &lhs, EnumType &e) { + std::string s; + lhs >> s; + + try { + e = StringToEnum<EnumType>::convert(s); + } + catch (typename Dune::Exception) { + lhs.setstate(std::ios_base::failbit); + } + return lhs; +} diff --git a/src/enum_state_model.cc b/src/enum_state_model.cc new file mode 100644 index 0000000000000000000000000000000000000000..5982c432fedc0baae1da1a8020d90a435b4e5115 --- /dev/null +++ b/src/enum_state_model.cc @@ -0,0 +1,13 @@ +#include <dune/common/exceptions.hh> + +template <> struct StringToEnum<Config::state_model> { + static Config::state_model convert(std::string const &s) { + if (s == "Dieterich") + return Config::Dieterich; + + if (s == "Ruina") + return Config::Ruina; + + DUNE_THROW(Dune::Exception, "failed to parse enum"); + } +}; diff --git a/src/enums.hh b/src/enums.hh new file mode 100644 index 0000000000000000000000000000000000000000..ce43a1f05d5575f2041c770b911c35373fa862ec --- /dev/null +++ b/src/enums.hh @@ -0,0 +1,6 @@ +struct Config { + enum state_model { + Dieterich, + Ruina + }; +}; diff --git a/src/one-body-sample.cc b/src/one-body-sample.cc index 0f7b7c38350113c2ea8c1e0df107c0b6ae613b21..9716d8a881a9f6635c2242182fa5bdec056b9937 100644 --- a/src/one-body-sample.cc +++ b/src/one-body-sample.cc @@ -67,6 +67,10 @@ #include "mysolver.hh" #include "vtk.hh" +#include "enums.hh" +#include "enum_parser.cc" +#include "enum_state_model.cc" + int const dim = 2; template <class GridView> @@ -300,14 +304,16 @@ int main(int argc, char *argv[]) { // velocity // std::cout << std::log(L/unorm * h) << std::endl; - auto const model = - parset.get<std::string>("boundary.friction.state.model"); - if (model == std::string("Dieterich")) - (*s4_new)[i] = state_update_dieterich(h, unorm / L, s4_old[i]); - else if (model == std::string("Ruina")) - (*s4_new)[i] = state_update_ruina(h, unorm / L, s4_old[i]); - else - assert(false); + switch (parset.get<Config::state_model>( + "boundary.friction.state.model")) { + case Config::Dieterich: + (*s4_new)[i] = + state_update_dieterich(h, unorm / L, s4_old[i]); + break; + case Config::Ruina: + (*s4_new)[i] = state_update_ruina(h, unorm / L, s4_old[i]); + break; + } } } if (parset.get<bool>("printProgress")) {