24#ifndef DUMUX_COMMON_METADATA_HH
25#define DUMUX_COMMON_METADATA_HH
30#include <unordered_map>
36#include <dune/common/hybridutilities.hh>
37#include <dune/common/indices.hh>
38#include <dune/common/concept.hh>
39#include <dune/common/classname.hh>
40#include <dune/grid/common/gridview.hh>
76 Dune::Concept::requireType<typename GV::GridVolumeVariables>(),
77 Dune::Concept::requireType<typename GV::VolumeVariables>(),
78 Dune::Concept::requireType<typename GV::GridFluxVariablesCache>()
87 Dune::Concept::requireBaseOf<Dune::GridView<typename GV::Traits>, GV>()
97 std::size_t last = s.find_last_of(
"::");
99 if(last != std::string::npos)
105template<
class TTagTuple,
class Collector>
108 using namespace Dune::Hybrid;
109 forEach(std::make_index_sequence<std::tuple_size_v<TTagTuple>>{}, [&](
auto i)
111 using type =
typename std::tuple_element<i, TTagTuple>::type;
112 collector.push_back(std::tuple<int, int, std::string>{depth, parentBranch,
removeNamespace(Dune::className<type>())});
113 if constexpr (Dumux::Properties::Detail::hasParentTypeTag<type>(
int{}))
114 collectTypeTagsFromTuple<typename type::InheritsFrom>(collector,
int{depth+1}, i);
158 const auto& tree = collector.
getTree();
159 for (
const auto& [key, values] : tree.items())
161 auto& dataAtKey = this->
getTree()[key];
162 if(dataAtKey.is_array())
164 if(values.is_array())
165 dataAtKey.insert(dataAtKey.end(), values.begin(), values.end());
167 dataAtKey.push_back(values);
169 else if(dataAtKey.is_null())
173 else if(convertToArrays)
177 dataAtKey = JsonTree::array({val});
178 if(values.is_array())
179 dataAtKey.insert(dataAtKey.end(), values.begin(), values.end());
181 dataAtKey.push_back(values);
184 DUNE_THROW(Dune::InvalidStateException,
"Unclear how to append data without conversion to array!");
195 static std::string
className(
const T& c,
bool hideTemplates)
208 std::size_t first = s.find(
"<");
209 std::size_t last = s.find_last_of(
">");
211 if(first != std::string::npos && last != std::string::npos)
212 s.replace(first, last-first+1,
"<...>");
214 s.erase(std::unique(std::begin(s), std::end(s),
215 [](
unsigned char a,
unsigned char b){
return std::isspace(a) && std::isspace(b);}), std::end(s));
227 std::ifstream infile(fileName +
".json");
228 return infile.good();
232template<
class Collector>
235 std::ifstream i(fileName +
".json");
240template<
class Collector>
243 std::ofstream o(fileName +
".json");
244 o << std::setw(4) << collector.
getTree() << std::endl;
248template<
class Collector>
251 std::cout << collector.
getTree().dump(4) << std::endl;
254template<
class Collector,
class TypeTag, DiffMethod diffmethod,
bool isImplicit>
257 auto& obj = collector[
"Assembler"];
262template<
class Collector,
class Gr
idGeometry>
264->
typename std::enable_if_t<Dune::models<Concept::GridGeometry, GridGeometry>()>
266 auto& obj = collector[
"GridGeometry"];
268 obj[
"IsPeriodic"] = gg.isPeriodic();
269 obj[
"DiscretizationMethod"] = GridGeometry::discMethod.name();
270 obj[
"NumScvs"] = gg.numScv();
271 obj[
"NumScvfs"] = gg.numScvf();
272 obj[
"NumBoundaryScvfs"] = gg.numBoundaryScvf();
273 obj[
"NumDofs"] = gg.numDofs();
276template<
class Collector,
class Gr
idVariables>
278->
typename std::enable_if_t<Dune::models<Concept::GridVariables, GridVariables>()>
280 auto& obj = collector[
"GridVariables"];
282 obj[
"GridVolumeVariables"][
"Type"] = Collector::template className<typename GridVariables::GridVolumeVariables>(hideTemplates);
283 obj[
"VolumeVariables"][
"Type"] = Collector::template className<typename GridVariables::VolumeVariables>(hideTemplates);
284 obj[
"GridFluxVariablesCache"][
"Type"] = Collector::template className<typename GridVariables::GridFluxVariablesCache>(hideTemplates);
287template<
class Collector,
class Gr
idView>
289->
typename std::enable_if_t<Dune::models<Concept::GridView, GridView>()>
291 auto& obj = collector[
"GridView"];
293 obj[
"dimension"] = GridView::dimension;
294 obj[
"dimensionWorld"] = GridView::dimensionworld;
295 obj[
"conforming"] = GridView::conforming;
297 for(
int codim = 0; codim < GridView::dimension; ++codim)
298 obj[
"numEntities"][
"codim " + std::to_string(codim) ] = gridView.size(codim);
303template<
class TypeTag,
class Collector>
306 auto& obj = collector[
"TTags"];
307 obj = Dumux::Json::JsonTree::array();
308 Detail::collectTypeTagsFromTuple<std::tuple<TypeTag>>(obj);
An enum class to define various differentiation methods available in order to compute the derivatives...
A helper function for class member function introspection.
Utilities for template meta programming.
Base class for grid geometries.
Collection of json classes from JSON for Modern C++ library.
Definition: metadata.hh:53
auto collectTypeTags(Collector &collector)
Definition: metadata.hh:304
void writeJsonFile(const Collector &collector, const std::string &fileName)
writes a json tree to file
Definition: metadata.hh:241
void readJsonFile(Collector &collector, const std::string &fileName)
reads a json file into a tree
Definition: metadata.hh:233
bool jsonFileExists(const std::string &fileName)
convenience function to check if file exists
Definition: metadata.hh:225
void collectMetaData(Collector &collector, const FVAssembler< TypeTag, diffmethod, isImplicit > &a, bool hideTemplates=true)
Definition: metadata.hh:255
void print(const Collector &collector)
prints json tree
Definition: metadata.hh:249
std::string removeNamespace(std::string &&s)
Definition: metadata.hh:95
void collectTypeTagsFromTuple(Collector &collector, int depth=0, int parentBranch=-1)
Definition: metadata.hh:106
nlohmann::json JsonTree
Definition: json.hh:27
A linear system assembler (residual and Jacobian) for finite volume schemes (box, tpfa,...
Definition: assembly/fvassembler.hh:119
bool isStationaryProblem() const
Whether we are assembling a stationary or instationary problem.
Definition: assembly/fvassembler.hh:382
Concept of GridGeometry.
Definition: metadata.hh:59
auto require(const GG &gg) -> decltype(gg.isPeriodic(), gg.numScv(), gg.numScvf(), gg.numBoundaryScvf(), gg.numDofs(), GG::discMethod)
Concept of GridVariables.
Definition: metadata.hh:73
auto require(const GV &gv) -> decltype(Dune::Concept::requireType< typename GV::GridVolumeVariables >(), Dune::Concept::requireType< typename GV::VolumeVariables >(), Dune::Concept::requireType< typename GV::GridFluxVariablesCache >())
Concept of GridView.
Definition: metadata.hh:84
auto require(const GV &gv) -> decltype(Dune::Concept::requireBaseOf< Dune::GridView< typename GV::Traits >, GV >())
Class to collect metadata.
Definition: metadata.hh:125
auto & operator[](const std::string &id)
returns the object with id of the json tree
Definition: metadata.hh:191
static std::string className(const T &c, bool hideTemplates)
Definition: metadata.hh:195
void merge(const Collector &collector)
Merges two trees by overwriting existing values.
Definition: metadata.hh:146
void append(const Collector &collector, bool convertToArrays=false)
Append data from another collector.
Definition: metadata.hh:156
const JsonTree & getTree() const
Definition: metadata.hh:138
JsonTree & getTree()
Get the json tree.
Definition: metadata.hh:133
static std::string hideTemplateArguments(std::string &&s)
Definition: metadata.hh:206
static std::string className(bool hideTemplates)
Definition: metadata.hh:201
A linear system assembler (residual and Jacobian) for finite volume schemes.
Declares all properties used in Dumux.
The grid variable class for finite volume schemes, storing variables on scv and scvf (volume and flux...