Skip to content

Commit

Permalink
Construct DataVector from STL containers
Browse files Browse the repository at this point in the history
Various places in the code rolled their own solution to this.
  • Loading branch information
nilsvu committed Aug 21, 2024
1 parent 8c486aa commit 2555874
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 131 deletions.
2 changes: 0 additions & 2 deletions src/ControlSystem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ spectre_target_sources(
Averager.cpp
CalculateMeasurementTimescales.cpp
Controller.cpp
DataVectorHelpers.cpp
ExpirationTimes.cpp
FutureMeasurements.cpp
TimescaleTuner.cpp
Expand All @@ -27,7 +26,6 @@ spectre_target_headers(
CombinedName.hpp
Component.hpp
Controller.hpp
DataVectorHelpers.hpp
ExpirationTimes.hpp
FutureMeasurements.hpp
IsSize.hpp
Expand Down
1 change: 0 additions & 1 deletion src/ControlSystem/ControlErrors/Rotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <cstddef>
#include <pup.h>

#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Protocols/ControlError.hpp"
#include "ControlSystem/Tags/QueueTags.hpp"
#include "ControlSystem/Tags/SystemTags.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/ControlSystem/ControlErrors/Translation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "ControlSystem/ControlErrors/Expansion.hpp"
#include "ControlSystem/ControlErrors/Rotation.hpp"
#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Protocols/ControlError.hpp"
#include "ControlSystem/Tags/QueueTags.hpp"
#include "ControlSystem/Tags/SystemTags.hpp"
Expand Down
18 changes: 0 additions & 18 deletions src/ControlSystem/DataVectorHelpers.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/ControlSystem/DataVectorHelpers.hpp

This file was deleted.

12 changes: 4 additions & 8 deletions src/ControlSystem/Systems/Expansion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "ControlSystem/Component.hpp"
#include "ControlSystem/ControlErrors/Expansion.hpp"
#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Measurements/BNSCenterOfMass.hpp"
#include "ControlSystem/Measurements/BothHorizons.hpp"
#include "ControlSystem/Protocols/ControlError.hpp"
Expand Down Expand Up @@ -115,13 +114,12 @@ struct Expansion : tt::ConformsTo<protocols::ControlSystem> {
ControlComponent<Metavariables, Expansion<DerivOrder, Measurement>>>(
cache);

const DataVector center =
array_to_datavector(horizon_strahlkorper.physical_center());
DataVector center(horizon_strahlkorper.physical_center());

Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<Horizon>, MeasurementQueue,
UpdateControlSystem<Expansion>>>(control_sys_proxy, measurement_id,
center);
std::move(center));

if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) {
Parallel::printf("%s, time = %.16f: Received measurement '%s'.\n",
Expand All @@ -141,16 +139,14 @@ struct Expansion : tt::ConformsTo<protocols::ControlSystem> {
ControlComponent<Metavariables, Expansion<DerivOrder, Measurement>>>(
cache);

const DataVector center_a_dv = array_to_datavector(center_a);
Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<::domain::ObjectLabel::A>, MeasurementQueue,
UpdateControlSystem<Expansion>>>(control_sys_proxy, measurement_id,
center_a_dv);
const DataVector center_b_dv = array_to_datavector(center_b);
DataVector(center_a));
Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<::domain::ObjectLabel::B>, MeasurementQueue,
UpdateControlSystem<Expansion>>>(control_sys_proxy, measurement_id,
center_b_dv);
DataVector(center_b));

if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) {
Parallel::printf("%s, time = %.16f: Received measurement '%s'.\n",
Expand Down
12 changes: 4 additions & 8 deletions src/ControlSystem/Systems/Rotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "ControlSystem/Component.hpp"
#include "ControlSystem/ControlErrors/Rotation.hpp"
#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Measurements/BNSCenterOfMass.hpp"
#include "ControlSystem/Measurements/BothHorizons.hpp"
#include "ControlSystem/Protocols/ControlError.hpp"
Expand Down Expand Up @@ -121,13 +120,12 @@ struct Rotation : tt::ConformsTo<protocols::ControlSystem> {
ControlComponent<Metavariables, Rotation<DerivOrder, Measurement>>>(
cache);

const DataVector center =
array_to_datavector(strahlkorper.physical_center());
DataVector center(strahlkorper.physical_center());

Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<Horizon>, MeasurementQueue,
UpdateControlSystem<Rotation>>>(control_sys_proxy, measurement_id,
center);
std::move(center));

if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) {
Parallel::printf("%s, time = %.16f: Received measurement '%s'.\n",
Expand All @@ -147,16 +145,14 @@ struct Rotation : tt::ConformsTo<protocols::ControlSystem> {
ControlComponent<Metavariables, Rotation<DerivOrder, Measurement>>>(
cache);

const DataVector center_a_dv = array_to_datavector(center_a);
Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<::domain::ObjectLabel::A>, MeasurementQueue,
UpdateControlSystem<Rotation>>>(control_sys_proxy, measurement_id,
center_a_dv);
const DataVector center_b_dv = array_to_datavector(center_b);
DataVector(center_a));
Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<::domain::ObjectLabel::B>, MeasurementQueue,
UpdateControlSystem<Rotation>>>(control_sys_proxy, measurement_id,
center_b_dv);
DataVector(center_b));

if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) {
Parallel::printf("%s, time = %.16f: Received measurement '%s'.\n",
Expand Down
12 changes: 4 additions & 8 deletions src/ControlSystem/Systems/Translation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "ControlSystem/Component.hpp"
#include "ControlSystem/ControlErrors/Translation.hpp"
#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Measurements/BNSCenterOfMass.hpp"
#include "ControlSystem/Measurements/BothHorizons.hpp"
#include "ControlSystem/Protocols/ControlError.hpp"
Expand Down Expand Up @@ -108,13 +107,12 @@ struct Translation : tt::ConformsTo<protocols::ControlSystem> {
Parallel::get_parallel_component<ControlComponent<
Metavariables, Translation<DerivOrder, Measurement>>>(cache);

const DataVector center =
array_to_datavector(strahlkorper.physical_center());
DataVector center(strahlkorper.physical_center());

Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<Horizon>, MeasurementQueue,
UpdateControlSystem<Translation>>>(control_sys_proxy, measurement_id,
center);
std::move(center));

if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) {
Parallel::printf("%s, time = %.16f: Received measurement '%s'.\n",
Expand All @@ -134,16 +132,14 @@ struct Translation : tt::ConformsTo<protocols::ControlSystem> {
Parallel::get_parallel_component<ControlComponent<
Metavariables, Translation<DerivOrder, Measurement>>>(cache);

const DataVector center_a_dv = array_to_datavector(center_a);
Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<::domain::ObjectLabel::A>, MeasurementQueue,
UpdateControlSystem<Translation>>>(control_sys_proxy, measurement_id,
center_a_dv);
const DataVector center_b_dv = array_to_datavector(center_b);
DataVector(center_a));
Parallel::simple_action<::Actions::UpdateMessageQueue<
QueueTags::Center<::domain::ObjectLabel::B>, MeasurementQueue,
UpdateControlSystem<Translation>>>(control_sys_proxy, measurement_id,
center_b_dv);
DataVector(center_b));

if (Parallel::get<Tags::Verbosity>(cache) >= ::Verbosity::Verbose) {
Parallel::printf("%s, time = %.16f: Received measurement '%s'.\n",
Expand Down
18 changes: 18 additions & 0 deletions src/DataStructures/VectorImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "Utilities/SetNumberOfGridPoints.hpp"
#include "Utilities/StdArrayHelpers.hpp"
#include "Utilities/TypeTraits/IsComplexOfFundamental.hpp"
#include "Utilities/TypeTraits/IsStdArray.hpp"

class ComplexDataVector;
class ComplexModalVector;
Expand Down Expand Up @@ -199,6 +200,23 @@ class VectorImpl
std::fill(data(), data() + set_size, value);
}

/// Create from a copy of the given container
///
/// \param container A container with a `value_type` that is the same as `T`.
/// Currently restricted to `std::vector<T>` and `std::array<T>`.
template <
typename Container,
Requires<std::is_same_v<typename Container::value_type, T>> = nullptr>
explicit VectorImpl(const Container& container)
: owned_data_(heap_alloc_if_necessary(container.size())) {
static_assert(std::is_same_v<Container, std::vector<T>> or
tt::is_std_array_v<Container>,
"This constructor is currently restricted to std::vector and "
"std::array out of caution.");
reset_pointer_vector(container.size());
std::copy(container.begin(), container.end(), data());
}

/// Create a non-owning VectorImpl that points to `start`
VectorImpl(T* start, size_t set_size)
: BaseType(start, set_size), owning_(false) {}
Expand Down
52 changes: 22 additions & 30 deletions src/IO/External/InterpolateFromFuka.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ std::array<std::vector<double>, 22> KadathExportBHNS(

namespace io {

namespace {
DataVector to_datavector(std::vector<double> vec) {
DataVector result(vec.size());
std::copy(vec.begin(), vec.end(), result.begin());
return result;
}
} // namespace

template <FukaIdType IdType>
tuples::tagged_tuple_from_typelist<fuka_tags<IdType>> interpolate_from_fuka(
const gsl::not_null<std::mutex*> fuka_lock,
Expand Down Expand Up @@ -109,41 +101,41 @@ tuples::tagged_tuple_from_typelist<fuka_tags<IdType>> interpolate_from_fuka(
// The FUKA functions return tensor components in this order.
// See: https://bitbucket.org/fukaws/kadathimporter/src/master/src/importer.h
auto& lapse = get<gr::Tags::Lapse<DataVector>>(result);
get(lapse) = to_datavector(fuka_data[0]);
get(lapse) = DataVector(fuka_data[0]);
auto& shift = get<gr::Tags::Shift<DataVector, 3>>(result);
get<0>(shift) = to_datavector(fuka_data[1]);
get<1>(shift) = to_datavector(fuka_data[2]);
get<2>(shift) = to_datavector(fuka_data[3]);
get<0>(shift) = DataVector(fuka_data[1]);
get<1>(shift) = DataVector(fuka_data[2]);
get<2>(shift) = DataVector(fuka_data[3]);
auto& spatial_metric = get<gr::Tags::SpatialMetric<DataVector, 3>>(result);
get<0, 0>(spatial_metric) = to_datavector(fuka_data[4]);
get<0, 1>(spatial_metric) = to_datavector(fuka_data[5]);
get<0, 2>(spatial_metric) = to_datavector(fuka_data[6]);
get<1, 1>(spatial_metric) = to_datavector(fuka_data[7]);
get<1, 2>(spatial_metric) = to_datavector(fuka_data[8]);
get<2, 2>(spatial_metric) = to_datavector(fuka_data[9]);
get<0, 0>(spatial_metric) = DataVector(fuka_data[4]);
get<0, 1>(spatial_metric) = DataVector(fuka_data[5]);
get<0, 2>(spatial_metric) = DataVector(fuka_data[6]);
get<1, 1>(spatial_metric) = DataVector(fuka_data[7]);
get<1, 2>(spatial_metric) = DataVector(fuka_data[8]);
get<2, 2>(spatial_metric) = DataVector(fuka_data[9]);
auto& extrinsic_curvature =
get<gr::Tags::ExtrinsicCurvature<DataVector, 3>>(result);
get<0, 0>(extrinsic_curvature) = to_datavector(fuka_data[10]);
get<0, 1>(extrinsic_curvature) = to_datavector(fuka_data[11]);
get<0, 2>(extrinsic_curvature) = to_datavector(fuka_data[12]);
get<1, 1>(extrinsic_curvature) = to_datavector(fuka_data[13]);
get<1, 2>(extrinsic_curvature) = to_datavector(fuka_data[14]);
get<2, 2>(extrinsic_curvature) = to_datavector(fuka_data[15]);
get<0, 0>(extrinsic_curvature) = DataVector(fuka_data[10]);
get<0, 1>(extrinsic_curvature) = DataVector(fuka_data[11]);
get<0, 2>(extrinsic_curvature) = DataVector(fuka_data[12]);
get<1, 1>(extrinsic_curvature) = DataVector(fuka_data[13]);
get<1, 2>(extrinsic_curvature) = DataVector(fuka_data[14]);
get<2, 2>(extrinsic_curvature) = DataVector(fuka_data[15]);
if constexpr (IdType == FukaIdType::Ns or IdType == FukaIdType::Bns or
IdType == FukaIdType::Bhns) {
auto& rest_mass_density =
get<hydro::Tags::RestMassDensity<DataVector>>(result);
get(rest_mass_density) = to_datavector(fuka_data[16]);
get(rest_mass_density) = DataVector(fuka_data[16]);
auto& specific_internal_energy =
get<hydro::Tags::SpecificInternalEnergy<DataVector>>(result);
get(specific_internal_energy) = to_datavector(fuka_data[17]);
get(specific_internal_energy) = DataVector(fuka_data[17]);
auto& pressure = get<hydro::Tags::Pressure<DataVector>>(result);
get(pressure) = to_datavector(fuka_data[18]);
get(pressure) = DataVector(fuka_data[18]);
auto& spatial_velocity =
get<hydro::Tags::SpatialVelocity<DataVector, 3>>(result);
get<0>(spatial_velocity) = to_datavector(fuka_data[19]);
get<1>(spatial_velocity) = to_datavector(fuka_data[20]);
get<2>(spatial_velocity) = to_datavector(fuka_data[21]);
get<0>(spatial_velocity) = DataVector(fuka_data[19]);
get<1>(spatial_velocity) = DataVector(fuka_data[20]);
get<2>(spatial_velocity) = DataVector(fuka_data[21]);
}
return result;
}
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/ControlSystem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ set(LIBRARY_SOURCES
Test_Averager.cpp
Test_CombinedName.cpp
Test_Controller.cpp
Test_DataVectorHelpers.cpp
Test_EventTriggerMetafunctions.cpp
Test_ExpirationTimes.cpp
Test_FutureMeasurements.cpp
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/ControlSystem/ControlErrors/Test_Rotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <cstddef>
#include <string>

#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Tags/MeasurementTimescales.hpp"
#include "ControlSystem/Tags/QueueTags.hpp"
#include "ControlSystem/Tags/SystemTags.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "ControlSystem/ControlErrors/Expansion.hpp"
#include "ControlSystem/ControlErrors/Rotation.hpp"
#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/Tags/MeasurementTimescales.hpp"
#include "ControlSystem/Tags/QueueTags.hpp"
#include "ControlSystem/Tags/SystemTags.hpp"
Expand Down
7 changes: 3 additions & 4 deletions tests/Unit/ControlSystem/Systems/Test_RotScaleTrans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,10 @@ void test_rotscaletrans_control_system(const double rotation_eps = 5.0e-5) {
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[2],
expected_translation_2nd_deriv, custom_approx1);
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[1],
array_to_datavector(initial_velocity),
DataVector(initial_velocity), custom_approx1);
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[0],
DataVector(initial_velocity * final_time),
custom_approx1);
CHECK_ITERABLE_CUSTOM_APPROX(
trans_and_2_derivs[0], array_to_datavector(initial_velocity * final_time),
custom_approx1);
CHECK_ITERABLE_CUSTOM_APPROX(
binary_trajectories.separation(final_time) / initial_separation,
expansion_factor, custom_approx1);
Expand Down
7 changes: 3 additions & 4 deletions tests/Unit/ControlSystem/Systems/Test_Translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,10 @@ void test_translation_control_system() {
const DataVector expected_translation_2nd_deriv{3, 0.0};
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[2],
expected_translation_2nd_deriv, custom_approx);
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[1],
array_to_datavector(velocity), custom_approx);
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[0],
array_to_datavector(velocity * final_time),
CHECK_ITERABLE_CUSTOM_APPROX(trans_and_2_derivs[1], DataVector(velocity),
custom_approx);
CHECK_ITERABLE_CUSTOM_APPROX(
trans_and_2_derivs[0], DataVector(velocity * final_time), custom_approx);

CHECK_ITERABLE_CUSTOM_APPROX(expected_grid_position_of_a, grid_position_of_a,
custom_approx);
Expand Down
29 changes: 0 additions & 29 deletions tests/Unit/ControlSystem/Test_DataVectorHelpers.cpp

This file was deleted.

1 change: 0 additions & 1 deletion tests/Unit/Helpers/ControlSystem/SystemHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "ControlSystem/ControlErrors/Shape.hpp"
#include "ControlSystem/ControlErrors/Translation.hpp"
#include "ControlSystem/Controller.hpp"
#include "ControlSystem/DataVectorHelpers.hpp"
#include "ControlSystem/ExpirationTimes.hpp"
#include "ControlSystem/Measurements/BothHorizons.hpp"
#include "ControlSystem/Measurements/SingleHorizon.hpp"
Expand Down
Loading

0 comments on commit 2555874

Please sign in to comment.