Skip to content

Commit

Permalink
Merge pull request #6448 from nilsdeppe/bns_improvements_12
Browse files Browse the repository at this point in the history
Allow observing names of tags in DataBox
  • Loading branch information
nilsdeppe authored Jan 24, 2025
2 parents f46bc13 + aeed8d2 commit 833b897
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 55 deletions.
3 changes: 3 additions & 0 deletions src/DataStructures/DataBox/Access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Access {
public:
virtual ~Access() = default;

/// Print the expanded type aliases of the derived `db::DataBox`
virtual std::string print_tags() const = 0;

/// Print the expanded type aliases of the derived `db::DataBox`
virtual std::string print_types() const = 0;

Expand Down
91 changes: 78 additions & 13 deletions src/DataStructures/DataBox/DataBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ class DataBox<tmpl::list<Tags...>> : public Access,

/// \endcond

/// Print the expanded type aliases
std::string print_tags() const override;

/// Print the expanded type aliases
std::string print_types() const override;

Expand Down Expand Up @@ -502,23 +505,85 @@ const typename DataBox<tmpl::list<Tags...>>::TagGraphs
DataBox<tmpl::list<Tags...>>::tag_graphs_ =
DataBox<tmpl::list<Tags...>>::compute_tag_graphs();

template <typename... Tags>
std::string DataBox<tmpl::list<Tags...>>::print_tags() const {
std::ostringstream os;
os << "Simple tags("
<< tmpl::size<mutable_item_creation_tags>::value << ") = [\n";
tmpl::for_each<mutable_item_creation_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "Simple tags subitems(" << tmpl::size<mutable_subitem_tags>::value
<< ") = [\n";
tmpl::for_each<mutable_subitem_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "Compute tags(" << tmpl::size<compute_item_tags>::value
<< ") = [\n";
tmpl::for_each<compute_item_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "Reference tags(" << tmpl::size<reference_item_tags>::value
<< ") = [\n";
tmpl::for_each<reference_item_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
return os.str();
}

template <typename... Tags>
std::string DataBox<tmpl::list<Tags...>>::print_types() const {
std::ostringstream os;
os << "DataBox type aliases:\n";
os << "using tags_list = " << pretty_type::get_name<tags_list>() << ";\n";
os << "using immutable_item_tags = "
<< pretty_type::get_name<immutable_item_tags>() << ";\n";
os << "using immutable_item_creation_tags = "
<< pretty_type::get_name<immutable_item_creation_tags>() << ";\n";
os << "using mutable_item_tags = "
<< pretty_type::get_name<mutable_item_tags>() << ";\n";
os << "using mutable_subitem_tags = "
<< pretty_type::get_name<mutable_subitem_tags>() << ";\n";
os << "using compute_item_tags = "
<< pretty_type::get_name<compute_item_tags>() << ";\n";
os << "using reference_item_tags = "
<< pretty_type::get_name<reference_item_tags>() << ";\n";
os << "using tags_list = [";
tmpl::for_each<tags_list>([&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using immutable_item_tags = [";
tmpl::for_each<immutable_item_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using immutable_item_creation_tags = [";
tmpl::for_each<immutable_item_creation_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using mutable_item_tags = [";
tmpl::for_each<mutable_item_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using mutable_subitem_tags = [";
tmpl::for_each<mutable_subitem_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using compute_item_tags = [";
tmpl::for_each<compute_item_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using reference_item_tags = [";
tmpl::for_each<reference_item_tags>(
[&os]<class Tag>(const tmpl::type_<Tag> /*meta*/) {
os << " " << pretty_type::get_name<Tag>() << ",\n";
});
os << "];\n";
os << "using edge_list = " << pretty_type::get_name<edge_list>() << ";\n";
return os.str();
}
Expand Down
3 changes: 3 additions & 0 deletions src/DataStructures/DataBox/ObservationBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ class ObservationBox<tmpl::list<ComputeTags...>, DataBoxType>
template <typename Tag>
const auto& get() const;

/// @{
/// Retrieve the underlying DataBox.
DataBoxType& databox() { return *databox_; }
const DataBoxType& databox() const { return *databox_; }
/// @}

/// Reset all the compute items, forcing reevaluation.
void reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
#include "ParallelAlgorithms/ApparentHorizonFinder/InterpolationTarget.hpp"
#include "ParallelAlgorithms/Events/Factory.hpp"
#include "ParallelAlgorithms/Events/ObserveAtExtremum.hpp"
#include "ParallelAlgorithms/Events/ObserveDataBox.hpp"
#include "ParallelAlgorithms/Events/ObserveTimeStepVolume.hpp"
#include "ParallelAlgorithms/EventsAndDenseTriggers/DenseTrigger.hpp"
#include "ParallelAlgorithms/EventsAndDenseTriggers/DenseTriggers/Factory.hpp"
Expand Down Expand Up @@ -601,7 +602,7 @@ struct GhValenciaDivCleanTemplateBase<
domain_creators<volume_dim>>>,
tmpl::pair<Event,
tmpl::flatten<tmpl::list<
Events::Completion,
Events::Completion, ::Events::ObserveDataBox,
dg::Events::field_observations<
volume_dim, observe_fields, non_tensor_compute_tags>,
Events::ObserveAtExtremum<observe_fields,
Expand Down
40 changes: 38 additions & 2 deletions src/ParallelAlgorithms/Events/ObserveDataBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,48 @@

#include "ParallelAlgorithms/Events/ObserveDataBox.hpp"

#include <fstream>
#include <pup.h>

#include "DataStructures/DataBox/Access.hpp"
#include "Utilities/GenerateInstantiations.hpp"

namespace Events {
ObserveDataBox::ObserveDataBox(CkMigrateMessage* /*m*/) {}
ObserveDataBox::ObserveDataBox(
std::optional<std::string> file_name_for_tag_names)
: file_name_for_tag_names_(std::move(file_name_for_tag_names)) {}

ObserveDataBox::ObserveDataBox(CkMigrateMessage* m) : Event{m} {}

void ObserveDataBox::pup(PUP::er& p) { Event::pup(p); }
void ObserveDataBox::pup(PUP::er& p) {
Event::pup(p);
p | file_name_for_tag_names_;
}

template <size_t VolumeDim>
void ObserveDataBox::impl(const db::Access& box_access,
const ElementId<VolumeDim>& array_index,
const ObservationValue& /*observation_value*/) const {
if (is_zeroth_element(array_index)) {
if (file_name_for_tag_names_.has_value()) {
std::ofstream of{file_name_for_tag_names_.value()};
of << box_access.print_tags() << "\n";
of.close();
}
}
}

PUP::able::PUP_ID ObserveDataBox::my_PUP_ID = 0; // NOLINT

#define DIM(data) BOOST_PP_TUPLE_ELEM(0, data)

#define INSTANTIATE(r, data) \
template void ObserveDataBox::impl<DIM(data)>( \
const db::Access&, const ElementId<DIM(data)>&, const ObservationValue&) \
const;

GENERATE_INSTANTIATIONS(INSTANTIATE, (1, 2, 3))

#undef INSTANTIATION
#undef DIM
} // namespace Events
42 changes: 32 additions & 10 deletions src/ParallelAlgorithms/Events/ObserveDataBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#include <tuple>
#include <vector>

#include "DataStructures/DataBox/Access.hpp"
#include "DataStructures/DataBox/DataBox.hpp"
#include "Domain/Structure/ElementId.hpp"
#include "IO/Observer/ObserverComponent.hpp"
#include "IO/Observer/ReductionActions.hpp"
#include "Options/Auto.hpp"
#include "Options/String.hpp"
#include "Parallel/DistributedObject.hpp"
#include "Parallel/GlobalCache.hpp"
Expand Down Expand Up @@ -103,30 +105,39 @@ struct ContributeDataBoxSize {
/// of each parallel component. There will be a column for each item in the
/// DataBox that is not a subitem or reference item.
class ObserveDataBox : public Event {
struct DoNotWrite {};

public:
/// \cond
explicit ObserveDataBox(CkMigrateMessage* m);
using PUP::able::register_constructor;
WRAPPED_PUPable_decl_template(ObserveDataBox); // NOLINT
/// \endcond

using options = tmpl::list<>;
struct WriteTagNamesToFile {
using type = ::Options::Auto<std::string, DoNotWrite>;
static constexpr Options::String help = {
"The text file to write the names of the DataBox tags to."};
};

using options = tmpl::list<WriteTagNamesToFile>;
static constexpr Options::String help = {
"Observe size (in MB) of each item (except reference items) in each "
"DataBox"};

ObserveDataBox() = default;
explicit ObserveDataBox(std::optional<std::string> file_name_for_tag_names);

using compute_tags_for_observation_box = tmpl::list<>;

using return_tags = tmpl::list<>;
using argument_tags = tmpl::list<::Tags::DataBox>;
using argument_tags = tmpl::list<::Tags::ObservationBox>;

template <typename DataBoxType, typename ArrayIndex,
typename ParallelComponent, typename Metavariables>
void operator()(const DataBoxType& box,
template <typename DataBoxType, size_t VolumeDim, typename ParallelComponent,
typename Metavariables, typename ComputeTagsList>
void operator()(const ObservationBox<DataBoxType, ComputeTagsList>& box,
Parallel::GlobalCache<Metavariables>& /*cache*/,
const ArrayIndex& array_index,
const ElementId<VolumeDim>& array_index,
const ParallelComponent* /*meta*/,
const ObservationValue& observation_value) const;

Expand All @@ -143,13 +154,23 @@ class ObserveDataBox : public Event {

// NOLINTNEXTLINE(google-runtime-references)
void pup(PUP::er& p) override;

private:
template <size_t VolumeDim>
void impl(const db::Access& box_access,
const ElementId<VolumeDim>& array_index,
const ObservationValue& observation_value) const;

std::optional<std::string> file_name_for_tag_names_;
};

template <typename DataBoxType, typename ArrayIndex, typename ParallelComponent,
typename Metavariables>
template <typename DataBoxType, size_t VolumeDim, typename ParallelComponent,
typename Metavariables, typename ComputeTagsList>
void ObserveDataBox::operator()(
const DataBoxType& /*box*/, Parallel::GlobalCache<Metavariables>& cache,
const ArrayIndex& array_index, const ParallelComponent* const /*meta*/,
const ObservationBox<DataBoxType, ComputeTagsList>& box,
Parallel::GlobalCache<Metavariables>& cache,
const ElementId<VolumeDim>& array_index,
const ParallelComponent* const /*meta*/,
const ObservationValue& observation_value) const {
if (is_zeroth_element(array_index)) {
using component_list =
Expand All @@ -162,5 +183,6 @@ void ObserveDataBox::operator()(
target_proxy, observation_value.value);
});
}
impl(box.databox(), array_index, observation_value);
}
} // namespace Events
81 changes: 52 additions & 29 deletions tests/Unit/DataStructures/DataBox/Test_DataBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3164,34 +3164,38 @@ void test_output() {
3.14, std::vector<double>{8.7, 93.2, 84.7}, "My Sample String"s);
std::string output_types = db::as_access(box).print_types();
std::string expected_types =
"DataBox type aliases:\n"
"using tags_list = brigand::list<(anonymous "
"namespace)::test_databox_tags::Tag0, (anonymous "
"namespace)::test_databox_tags::Tag1, (anonymous "
"namespace)::test_databox_tags::Tag2, (anonymous "
"namespace)::test_databox_tags::Tag4Compute, (anonymous "
"namespace)::test_databox_tags::Tag5Compute, (anonymous "
"namespace)::test_databox_tags::Tag0Reference>;\n"
"using immutable_item_tags "
"= brigand::list<(anonymous namespace)::test_databox_tags::Tag4Compute, "
"(anonymous namespace)::test_databox_tags::Tag5Compute, (anonymous "
"namespace)::test_databox_tags::Tag0Reference>;\n"
"using immutable_item_creation_tags = brigand::list<(anonymous "
"namespace)::test_databox_tags::Tag4Compute, (anonymous "
"namespace)::test_databox_tags::Tag5Compute, (anonymous "
"namespace)::test_databox_tags::Tag0Reference>;\n"
"using mutable_item_tags = brigand::list<(anonymous "
"namespace)::test_databox_tags::Tag0, (anonymous "
"namespace)::test_databox_tags::Tag1, (anonymous "
"namespace)::test_databox_tags::Tag2>;\n"
"using mutable_subitem_tags = brigand::list<>;\n"
"using compute_item_tags = brigand::list<(anonymous "
"namespace)::test_databox_tags::Tag4Compute, (anonymous "
"namespace)::test_databox_tags::Tag5Compute>;\n"
"using reference_item_tags = brigand::list<(anonymous "
"namespace)::test_databox_tags::Tag0Reference>;\n"
"using edge_list = "
"brigand::list<brigand::edge<(anonymous "
" DataBox type aliases:"
" using tags_list = [(anonymous namespace)::test_databox_tags::Tag0,"
"(anonymous namespace)::test_databox_tags::Tag1,"
"(anonymous namespace)::test_databox_tags::Tag2,"
"(anonymous namespace)::test_databox_tags::Tag4Compute,"
"(anonymous namespace)::test_databox_tags::Tag5Compute,"
"(anonymous namespace)::test_databox_tags::Tag0Reference,"
"];"
" using immutable_item_tags = ["
"(anonymous namespace)::test_databox_tags::Tag4Compute,"
"(anonymous namespace)::test_databox_tags::Tag5Compute,"
"(anonymous namespace)::test_databox_tags::Tag0Reference,"
"];"
" using immutable_item_creation_tags = ["
"(anonymous namespace)::test_databox_tags::Tag4Compute,"
"(anonymous namespace)::test_databox_tags::Tag5Compute,"
"(anonymous namespace)::test_databox_tags::Tag0Reference,"
"];"
" using mutable_item_tags = ["
"(anonymous namespace)::test_databox_tags::Tag0,"
"(anonymous namespace)::test_databox_tags::Tag1,"
"(anonymous namespace)::test_databox_tags::Tag2,"
"];"
" using mutable_subitem_tags = [];"
" using compute_item_tags = ["
"(anonymous namespace)::test_databox_tags::Tag4Compute,"
"(anonymous namespace)::test_databox_tags::Tag5Compute,"
"];"
" using reference_item_tags = ["
"(anonymous namespace)::test_databox_tags::Tag0Reference,"
"];"
" using edge_list = brigand::list<brigand::edge<(anonymous "
"namespace)::test_databox_tags::Tag0, (anonymous "
"namespace)::test_databox_tags::Tag4Compute, "
"brigand::integral_constant<int, 1> >, brigand::edge<(anonymous "
Expand All @@ -3203,7 +3207,7 @@ void test_output() {
"brigand::integral_constant<int, 1> >, brigand::edge<(anonymous "
"namespace)::test_databox_tags::Tag0, (anonymous "
"namespace)::test_databox_tags::Tag0Reference, "
"brigand::integral_constant<int, 1> > >;\n";
"brigand::integral_constant<int, 1> > >;";
// Remove whitespace since it may vary between compilers
auto remove_whitespace = [](std::string& str) {
str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end());
Expand All @@ -3212,6 +3216,25 @@ void test_output() {
remove_whitespace(expected_types);
CHECK(output_types == expected_types);

std::string output_tags = db::as_access(box).print_tags();
std::string expected_tags =
"Simpletags(3)=["
"(anonymousnamespace)::test_databox_tags::Tag0,"
"(anonymousnamespace)::test_databox_tags::Tag1,"
"(anonymousnamespace)::test_databox_tags::Tag2,"
"];"
"Simpletagssubitems(0)=[];"
"Computetags(2)=["
"(anonymousnamespace)::test_databox_tags::Tag4Compute,"
"(anonymousnamespace)::test_databox_tags::Tag5Compute,"
"];Referencetags(1)=["
"(anonymousnamespace)::test_databox_tags::Tag0Reference,"
"];";
// Remove whitespace since it may vary between compilers
remove_whitespace(output_tags);
remove_whitespace(expected_tags);
CHECK(output_tags == expected_tags);

std::string output_mutable_items = box.print_items<false>();
std::string expected_mutable_items =
"Items:\n"
Expand Down

0 comments on commit 833b897

Please sign in to comment.