factory.hh 2.66 KB
Newer Older
1 2 3
#ifndef DUNE_DORIE_INITIAL_CONDITION_FACTORY_HH
#define DUNE_DORIE_INITIAL_CONDITION_FACTORY_HH

4 5 6 7 8
#include <string>
#include <memory>

#include <dune/common/exceptions.hh>

9
#include <dune/dorie/common/initial_condition/analytic.hh>
10
#include <dune/dorie/common/logging.hh>
11
#include <dune/dorie/common/initial_condition/h5.hh>
12 13 14 15 16 17 18 19

namespace Dune{
namespace Dorie{

/*-------------------------------------------------------------------------*//**
 * @brief      Abstract Base Class for initial conditions.
 *
 * @ingroup    InitialConditions
20
 * @author     Santiago Ospina De Los Ríos
21
 * @date       2019
22
 *
23
 * @tparam     T     BaseTraits
24
 */
25
template<class T>
26
class InitialConditionFactory
27
{
28 29 30 31 32 33
protected:

  /// Create a user defined initial condition
  static auto create(
    const Dune::ParameterTree& ini_file,
    const typename T::GV& grid_view,
34
    std::shared_ptr<spdlog::logger> log=get_logger(log_base)
35
  )
36
  {
37
    std::unique_ptr<InitialCondition<T>> ic;
38 39

    auto ic_type = ini_file.get<std::string>("initial.type");
40 41 42 43
    log->debug("Creating initial condition of type: {}", ic_type);

    if (ic_type == "data")
    {
44
      const auto ic_datafile = ini_file.get<std::string>("initial.file");
45 46 47 48 49

      // determine data type
      const auto ext_start = ic_datafile.find_last_of(".");
      if (ext_start == std::string::npos) {
        log->error("Cannot determine extension of initial condition "
50
                   "data file: {}",
51 52 53 54
                   ic_datafile);
        DUNE_THROW(IOError, "Initial condition datafile has no apparent "
                            "extension");
      }
55

56 57
      const auto file_type = ic_datafile.substr(ext_start + 1);
      if (file_type == "h5") {
58 59 60
        using ICH5 = InitialConditionH5<T>;
        ic = std::make_unique<ICH5>(grid_view, ini_file, log);

61
      } else if (file_type == "vtu") {
62 63
        DUNE_THROW(NotImplemented,
          "Initial condition from VTK data files is not implemented yet!");
64
      } else {
65 66 67
        log->error("Unsupported initial condition datafile type: .{}",
                   file_type);
        DUNE_THROW(NotImplemented,
68
                   "Unsupported initial condition data file type");
69 70
      }
    } // ic_type == "data"
71
    else if (ic_type == "analytic") 
72
    {
73
      auto equation_str = ini_file.get<std::string>("initial.equation");
74

75 76
      using ICAnalytic = InitialConditionAnalytic<T>;
      ic = std::make_unique<ICAnalytic>(log,grid_view,equation_str);
77 78 79 80
    }
    else {
      log->error("Unsupported initial condition type: {}", ic_type);
      DUNE_THROW(NotImplemented, "Unsupported initial condition type");
81 82 83 84 85 86 87 88 89 90
    }

    return ic;
  }

};

} // namespace Dorie
} // namespace Dune

91
#endif // DUNE_DORIE_INITIAL_CONDITION_FACTORY_HH