1#ifndef DUNE_COPASI_MODEL_MAKE_INITIAL_HH
2#define DUNE_COPASI_MODEL_MAKE_INITIAL_HH
8#include <dune/pdelab/common/trace.hh>
10#include <dune/functions/gridfunctions/analyticgridviewfunction.hh>
12#include <dune/grid/concepts/grid.hh>
14#include <dune/common/exceptions.hh>
16#include <spdlog/spdlog.h>
20#include <unordered_map>
24template<
class Gr
idFunction, Dune::Concept::Gr
id Gr
id>
25[[nodiscard]]
inline static std::unordered_map<std::string, GridFunction>
26make_initial(
const Grid& grid,
27 const ParameterTree& config,
28 const FunctorFactory<Grid::dimensionworld>& functor_factory)
30 TRACE_EVENT(
"dune",
"InitialCondition");
32 std::unordered_map<std::string, GridFunction> functions;
34 auto time = config.get(
"time_step_operator.time_begin",
double{ 0. });
36 const auto& compartments_config = config.sub(
"compartments",
true);
38 for (
const auto& compartment : compartments_config.getSubKeys()) {
41 auto sub_grid_view = [&]() {
42 const auto& compartment_config = compartments_config.sub(compartment);
43 if constexpr (Concept::MultiDomainGrid<Grid>) {
44 using SubDomainIndex =
typename Grid::SubDomainIndex;
45 auto const domain_id = compartment_config.template get<SubDomainIndex>(
"id");
46 return grid.subDomain(domain_id).leafGridView();
48 auto const domain_id = compartment_config.template get<std::size_t>(
"id");
52 return grid.leafGridView();
56 auto components = config.sub(
"scalar_field",
true).getSubKeys();
57 for (
const auto& component : components) {
58 auto prefix = fmt::format(
"scalar_field.{}.initial", component);
59 if (not config.hasSub(prefix)) {
62 auto local_domain = std::make_shared<LocalDomain<Grid::dimensionworld>>();
63 local_domain->time = time;
64 auto fcn = functor_factory.make_scalar(prefix, config.sub(prefix), *local_domain);
68 auto shared_fnc = std::make_shared<decltype(fcn)>(std::move(fcn));
70 typename decltype(sub_grid_view)::template Codim<0>::Geometry::GlobalCoordinate;
71 std::function<double(GridDomain)> analytic_fcn =
72 [lcl_domain = std::move(local_domain),
73 _fnc = std::move(shared_fnc)](GridDomain global_position) {
74 lcl_domain->position = global_position;
75 return std::invoke(*_fnc);
79 Dune::Functions::makeAnalyticGridViewFunction(std::move(analytic_fcn), sub_grid_view);
81 std::string
const component_id = fmt::format(
"{}.{}", compartment, component);
82 auto [_, inserted] = functions.try_emplace(component_id, std::move(grid_function));
84 throw format_exception(IOError{},
"Two or more components share the same name");
Definition: axis_names.hh:7
auto format_exception(Exception &&e, fmt::format_string< Args... > format, Args &&... args)
Definition: exceptions.hh:23