Skip to content

Commit

Permalink
Check duplicate entries in input files (Exawind#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchdf authored May 23, 2024
1 parent 2a0db27 commit d6d4220
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions amr-wind/immersed_boundary/IB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "amr-wind/CFDSim.H"
#include "amr-wind/core/FieldRepo.H"
#include "amr-wind/core/MultiParser.H"
#include "amr-wind/utilities/io_utils.H"

#include <algorithm>

Expand All @@ -26,6 +27,9 @@ void IB::pre_init_actions()

amrex::Vector<std::string> labels;
pp.getarr("labels", labels);
ioutils::assert_with_message(
ioutils::all_distinct(labels),
"Duplicates in " + identifier() + ".labels");

const int n_ibs = static_cast<int>(labels.size());

Expand Down
2 changes: 2 additions & 0 deletions amr-wind/utilities/ascent/ascent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void AscentPostProcess::initialize()
{
amrex::ParmParse pp("ascent");
pp.getarr("fields", field_names);
ioutils::assert_with_message(
ioutils::all_distinct(field_names), "Duplicates in ascent.fields");
pp.query("output_frequency", m_out_freq);
}

Expand Down
13 changes: 7 additions & 6 deletions amr-wind/utilities/averaging/TimeAveraging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "amr-wind/utilities/averaging/TimeAveraging.H"
#include "amr-wind/utilities/averaging/ReAveraging.H"
#include "amr-wind/utilities/io_utils.H"
#include "amr-wind/CFDSim.H"

#include "AMReX_ParmParse.H"
Expand All @@ -21,6 +22,9 @@ void TimeAveraging::pre_init_actions()
{
amrex::ParmParse pp(m_label);
pp.getarr("labels", labels);
ioutils::assert_with_message(
ioutils::all_distinct(labels),
"Duplicates in " + m_label + ".labels");
pp.query("averaging_start_time", m_start_time);
pp.query("averaging_stop_time", m_stop_time);
pp.get("averaging_window", m_filter);
Expand All @@ -34,17 +38,14 @@ void TimeAveraging::pre_init_actions()

amrex::ParmParse pp1(pp_key);
pp1.getarr("fields", fnames);
ioutils::assert_with_message(
ioutils::all_distinct(fnames),
"Duplicates in " + pp_key + ".fields");
pp1.get("averaging_type", avg_type);

for (const auto& fname : fnames) {
const std::string key = fname + "_" + avg_type;

// Guard against multiple registrations of the field
const auto found = m_registered.find(key);
if (found != m_registered.end()) {
continue;
}

// Create the averaging entity
m_averages.emplace_back(
FieldTimeAverage::create(avg_type, m_sim, fname));
Expand Down
15 changes: 15 additions & 0 deletions amr-wind/utilities/io_utils.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ctime>
#include <string>
#include <sstream>
#include <unordered_set>
#include "AMReX_Vector.H"

namespace amrex {
Expand Down Expand Up @@ -59,6 +60,20 @@ inline void add_var_names(
}
}
}

template <typename T>
inline bool all_distinct(const amrex::Vector<T>& vec)
{
std::unordered_set<T> s(vec.begin(), vec.end());
return static_cast<amrex::Long>(s.size()) == vec.size();
}

inline void assert_with_message(const bool val, const std::string& msg)
{
if (!val) {
amrex::Abort(msg);
}
}
} // namespace amr_wind::ioutils

#endif /* IO_UTILS_H */
6 changes: 6 additions & 0 deletions amr-wind/utilities/sampling/Sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ void Sampling::initialize()
{
amrex::ParmParse pp(m_label);
pp.getarr("labels", labels);
ioutils::assert_with_message(
ioutils::all_distinct(labels),
"Duplicates in " + m_label + ".labels");
pp.getarr("fields", field_names);
ioutils::assert_with_message(
ioutils::all_distinct(field_names),
"Duplicates in " + m_label + ".fields");
pp.queryarr("derived_fields", derived_field_names);
pp.query("output_frequency", m_out_freq);
pp.query("output_format", m_out_fmt);
Expand Down
6 changes: 5 additions & 1 deletion amr-wind/wind_energy/actuator/Actuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "amr-wind/wind_energy/actuator/ActuatorContainer.H"
#include "amr-wind/CFDSim.H"
#include "amr-wind/core/FieldRepo.H"
#include "amr-wind/utilities/io_utils.H"

#include <algorithm>
#include <memory>
Expand All @@ -23,6 +24,9 @@ void Actuator::pre_init_actions()

amrex::Vector<std::string> labels;
pp.getarr("labels", labels);
ioutils::assert_with_message(
ioutils::all_distinct(labels),
"Duplicates in " + identifier() + ".labels");

const int nturbines = static_cast<int>(labels.size());

Expand Down Expand Up @@ -306,4 +310,4 @@ T* Actuator::get_actuator(std::string& key) const
amrex::Abort("Could not find actuator");
}

} // namespace amr_wind::actuator
} // namespace amr_wind::actuator

0 comments on commit d6d4220

Please sign in to comment.