Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array<int> to options, additional limiters for grids (rebase) #2767

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions include/bout/options.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ public:
static void cleanup();

/// The type used to store values
using ValueType =
bout::utils::variant<bool, int, BoutReal, std::string, Field2D, Field3D, FieldPerp,
Array<BoutReal>, Matrix<BoutReal>, Tensor<BoutReal>>;
using ValueType = bout::utils::variant<bool, int, BoutReal, std::string, Field2D,
Field3D, FieldPerp, Array<BoutReal>, Array<int>,
Matrix<BoutReal>, Tensor<BoutReal>>;

/// The type used to store attributes
/// Extends the variant class so that cast operator can be implemented
Expand Down Expand Up @@ -874,6 +874,8 @@ void Options::assign<>(FieldPerp val, std::string source);
template <>
void Options::assign<>(Array<BoutReal> val, std::string source);
template <>
void Options::assign<>(Array<int> val, std::string source);
template <>
void Options::assign<>(Matrix<BoutReal> val, std::string source);
template <>
void Options::assign<>(Tensor<BoutReal> val, std::string source);
Expand Down Expand Up @@ -902,6 +904,8 @@ FieldPerp Options::as<FieldPerp>(const FieldPerp& similar_to) const;
template <>
Array<BoutReal> Options::as<Array<BoutReal>>(const Array<BoutReal>& similar_to) const;
template <>
Array<int> Options::as<Array<int>>(const Array<int>& similar_to) const;
template <>
Matrix<BoutReal> Options::as<Matrix<BoutReal>>(const Matrix<BoutReal>& similar_to) const;
template <>
Tensor<BoutReal> Options::as<Tensor<BoutReal>>(const Tensor<BoutReal>& similar_to) const;
Expand Down
4 changes: 2 additions & 2 deletions include/bout/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ std::string toString(const T& val) {
/// where the type may be std::string.
inline std::string toString(const std::string& val) { return val; }

template <>
inline std::string toString<>(const Array<BoutReal>& UNUSED(val)) {
template <typename T>
inline std::string toString(const Array<T>& UNUSED(val)) {
return "<Array>";
}

Expand Down
32 changes: 26 additions & 6 deletions src/mesh/data/gridfromfile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ struct GetDimensions {
std::vector<int> operator()(MAYBE_UNUSED(int value)) { return {1}; }
std::vector<int> operator()(MAYBE_UNUSED(BoutReal value)) { return {1}; }
std::vector<int> operator()(MAYBE_UNUSED(const std::string& value)) { return {1}; }
std::vector<int> operator()(const Array<BoutReal>& array) { return {array.size()}; }
template <typename T>
std::vector<int> operator()(const Array<T>& array) {
return {array.size()};
}
std::vector<int> operator()(const Matrix<BoutReal>& array) {
const auto shape = array.shape();
return {std::get<0>(shape), std::get<1>(shape)};
Expand Down Expand Up @@ -471,13 +474,30 @@ void GridFile::readField(Mesh* m, const std::string& name, int UNUSED(ys), int U
}
}

bool GridFile::get(MAYBE_UNUSED(Mesh* m), MAYBE_UNUSED(std::vector<int>& var),
MAYBE_UNUSED(const std::string& name), MAYBE_UNUSED(int len),
MAYBE_UNUSED(int offset),
MAYBE_UNUSED(GridDataSource::Direction dir)) {
bool GridFile::get(Mesh* UNUSED(m), std::vector<int>& var, const std::string& name,
int len, int offset, GridDataSource::Direction UNUSED(dir)) {
TRACE("GridFile::get(vector<int>)");

return false;
if (not data.isSet(name)) {
return false;
}

const auto full_var = data[name].as<Array<int>>();

// Check size
if (full_var.size() < len + offset) {
throw BoutException("{} has length {}. Expected {} elements + {} offset", name,
full_var.size(), len, offset);
}

// Ensure that output variable has the correct size
var.resize(len);

const auto* it = std::begin(full_var);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable name 'it' is too short, expected at least 3 characters [readability-identifier-length]

  const auto* it = std::begin(full_var);
              ^

std::advance(it, offset);
std::copy_n(it, len, std::begin(var));

return true;
}

bool GridFile::get(Mesh* UNUSED(m), std::vector<BoutReal>& var, const std::string& name,
Expand Down
29 changes: 29 additions & 0 deletions src/mesh/impls/bout/boutmesh.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,35 @@ void BoutMesh::topology() {
add_target(ny_inner - 1, 0, nx);
}

// Additional limiters
// Each limiter needs 3 indices: A Y index, start and end X indices
int limiter_count = 0;
Mesh::get(limiter_count, "limiter_count", 0);
if (limiter_count > 0) {
std::vector<int> limiter_yinds, limiter_xstarts, limiter_xends;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration]

Suggested change
std::vector<int> limiter_yinds, limiter_xstarts, limiter_xends;
std::vector<int> limiter_yinds;
std::vector<int> limiter_xstarts;
std::vector<int> limiter_xends;

if (!source->get(this, limiter_yinds, "limiter_yinds", limiter_count)) {
throw BoutException("Couldn't read limiter_yinds vector of length {} from mesh",
limiter_count);
}
if (!source->get(this, limiter_xstarts, "limiter_xstarts", limiter_count)) {
throw BoutException("Couldn't read limiter_xstarts vector of length {} from mesh",
limiter_count);
}
if (!source->get(this, limiter_xends, "limiter_xends", limiter_count)) {
throw BoutException("Couldn't read limiter_xend vector of length {} from mesh",
limiter_count);
}

for (int i = 0; i < limiter_count; ++i) {
int yind = limiter_yinds[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'yind' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
int yind = limiter_yinds[i];
int const yind = limiter_yinds[i];

int xstart = limiter_xstarts[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'xstart' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
int xstart = limiter_xstarts[i];
int const xstart = limiter_xstarts[i];

int xend = limiter_xends[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'xend' of type 'int' can be declared 'const' [misc-const-correctness]

Suggested change
int xend = limiter_xends[i];
int const xend = limiter_xends[i];

output_info.write("Adding a limiter between y={} and {}. X indices {} to {}\n",
yind, yind + 1, xstart, xend);
add_target(yind, xstart, xend);
}
}

if ((ixseps_inner > 0)
&& (((PE_YIND * MYSUB > jyseps1_1) && (PE_YIND * MYSUB <= jyseps2_1))
|| ((PE_YIND * MYSUB > jyseps1_2) && (PE_YIND * MYSUB <= jyseps2_2)))) {
Expand Down
30 changes: 30 additions & 0 deletions src/sys/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ void Options::assign<>(Array<BoutReal> val, std::string source) {
_set_no_check(std::move(val), std::move(source));
}
template <>
void Options::assign<>(Array<int> val, std::string source) {
_set_no_check(std::move(val), std::move(source));
}
template <>
void Options::assign<>(Matrix<BoutReal> val, std::string source) {
_set_no_check(std::move(val), std::move(source));
}
Expand Down Expand Up @@ -723,6 +727,32 @@ Array<BoutReal> Options::as<Array<BoutReal>>(const Array<BoutReal>& similar_to)
return result;
}

template <>
Array<int> Options::as<Array<int>>(const Array<int>& similar_to) const {
if (is_section) {
throw BoutException(_("Option {:s} has no value"), full_name);
}

Array<int> result = bout::utils::visit(
ConvertContainer<Array<int>>{
fmt::format(_("Value for option {:s} cannot be converted to an Array<int>"),
full_name),
similar_to},
value);

// Mark this option as used
value_used = true;

output_info << _("\tOption ") << full_name << " = Array<int>";
if (hasAttribute("source")) {
// Specify the source of the setting
output_info << " (" << bout::utils::variantToString(attributes.at("source")) << ")";
}
output_info << endl;

return result;
}

template <>
Matrix<BoutReal> Options::as<Matrix<BoutReal>>(const Matrix<BoutReal>& similar_to) const {
if (is_section) {
Expand Down
4 changes: 4 additions & 0 deletions src/sys/options/options_netcdf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ void readGroup(const std::string& filename, const NcGroup& group, Options& resul
Array<double> value(static_cast<int>(dims[0].getSize()));
var.getVar(value.begin());
result[var_name] = value;
} else if (var_type == ncInt or var_type == ncShort) {
Array<int> value(static_cast<int>(dims[0].getSize()));
var.getVar(value.begin());
result[var_name] = value;
} else if ((var_type == ncString) or (var_type == ncChar)) {
std::string value;
value.resize(dims[0].getSize());
Expand Down
Loading