Dune::Copasi
Loading...
Searching...
No Matches
interpolate.hh
Go to the documentation of this file.
1#ifndef DUNE_COPASI_MODEL_INTERPOLATE_HH
2#define DUNE_COPASI_MODEL_INTERPOLATE_HH
3
5
6#include <dune/pdelab/common/local_container.hh>
7#include <dune/pdelab/concepts/basis.hh>
8
9#include <dune/typetree/treecontainer.hh>
10
11#include <spdlog/spdlog.h>
12
13#include <optional>
14#include <string>
15#include <unordered_map>
16
17namespace Dune::Copasi {
18
19template<PDELab::Concept::Basis Basis, class GridFunction>
20 requires Concept::LocalBasisTree<typename Basis::LocalView::Tree>
21inline static void
22interpolate(const Basis& basis,
23 auto& coefficients,
24 const std::unordered_map<std::string, GridFunction>& initial)
25{
26 spdlog::info("Set initial state from grid functions");
27
28 if (basis.entitySet().size(0) == 0) {
29 return;
30 }
31
32 auto lspace = basis.localView();
33
34 PDELab::LocalContainerBuffer lcontainer{ basis, coefficients };
35 std::vector<double> buff;
36
37 // create a tree container with optional local functions
38 lspace.bind(*basis.entitySet().template begin<0>());
39 using LocalFunction = decltype(localFunction(std::declval<GridFunction>()));
40 auto lfuncs = TypeTree::makeTreeContainer(
41 lspace.tree(), [&](const auto& lbasis_node) -> std::optional<LocalFunction> {
42 const auto& compartment_basis = basis.subSpace(pop_back(lbasis_node.orderingViewPath()));
43 const auto& component_basis = basis.subSpace(lbasis_node.orderingViewPath());
44 auto it =
45 initial.find(fmt::format("{}.{}", compartment_basis.name(), component_basis.name()));
46 if (it != end(initial)) {
47 return localFunction(it->second);
48 }
49 return std::nullopt;
50 });
51 lspace.unbind();
52
53 // loop once over the grid and interpolate
54 for (const auto& entity : elements(basis.entitySet())) {
55 // bind local views to element
56 lspace.bind(entity);
57 lcontainer.clear(lspace);
58 buff.clear();
59
60 forEachLeafNode(lspace.tree(), [&](const auto& lbasis_node, auto path) {
61 auto& lfunc = lfuncs[path];
62 if (lbasis_node.size() == 0 or not lfunc) {
63 return;
64 }
65 lfunc->bind(lbasis_node.element());
66 const auto& fe = lbasis_node.finiteElement();
67 buff.assign(fe.size(), 0.);
68 fe.localInterpolation().interpolate(*lfunc, buff);
69 for (std::size_t dof = 0; dof != lbasis_node.size(); ++dof) {
70 lcontainer.accumulate(lbasis_node, dof, buff[dof]);
71 }
72 lfunc->unbind();
73 });
74
75 lcontainer.store(lspace);
76 lspace.unbind();
77 }
78}
79
80} // namespace Dune::Copasi
81
82#endif // DUNE_COPASI_MODEL_INTERPOLATE_HH
Definition axis_names.hh:7
constexpr bool is_bitflags_v
Alias for Bitflag indicator.
Definition bit_flags.hh:24