Skip to content

Commit

Permalink
Add support for calculating lin and log bin edges
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrete committed Oct 17, 2023
1 parent 5961d21 commit e515f7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/outputs/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,38 @@ namespace HistUtil {

ParArray1D<Real> GetEdges(ParameterInput *pin, const std::string &block_name,
const std::string &prefix) {

std::vector<Real> edges_in;

const auto edge_type_str = pin->GetString(block_name, prefix + "type");
if (edge_type_str == "lin") {
if (edge_type_str == "lin" || edge_type_str == "log") {
const auto edge_min = pin->GetReal(block_name, prefix + "min");
const auto edge_max = pin->GetReal(block_name, prefix + "max");
PARTHENON_REQUIRE_THROWS(edge_max > edge_min,
"Histogram max needs to be larger than min.")

const auto edge_num_bins = pin->GetReal(block_name, prefix + "num_bins");
PARTHENON_REQUIRE_THROWS(edge_num_bins >= 1, "Need at least one bin for histogram.");

if (edge_type_str == "lin") {
auto dbin = (edge_max - edge_min) / (edge_num_bins);
for (int i = 0; i < edge_num_bins; i++) {
edges_in.emplace_back(edge_min + i * dbin);
}
edges_in.emplace_back(edge_max);
} else if (edge_type_str == "log") {
PARTHENON_REQUIRE_THROWS(
edge_min > 0.0 && edge_max > 0.0,
"Log binning for negative values not implemented. However, you can specify "
"arbitrary bin edges through the 'list' edge type.")

} else if (edge_type_str == "log") {
auto dbin = (std::log10(edge_max) - std::log10(edge_min)) / (edge_num_bins);
for (int i = 0; i < edge_num_bins; i++) {
edges_in.emplace_back(std::pow(10., std::log10(edge_min) + i * dbin));
}
edges_in.emplace_back(edge_max);
} else {
PARTHENON_FAIL("Not sure how I got here...")
}

} else if (edge_type_str == "list") {
edges_in = pin->GetVector<Real>(block_name, prefix + "list");
Expand Down
6 changes: 4 additions & 2 deletions tst/regression/test_suites/output_hdf5/parthinput.advection
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ hist0_binned_variable_component = 0
# 2D histogram of a variable, binned by a coordinate and a different variable
hist1_ndim = 2
hist1_x_variable = HIST_COORD_X1
hist1_x_edges_type = list
hist1_x_edges_list = -0.5, -0.25, 0.0, 0.25, 0.5
hist1_x_edges_type = lin
hist1_x_edges_num_bins = 4
hist1_x_edges_min = -0.5
hist1_x_edges_max = 0.5
hist1_y_variable = one_minus_advected_sq
hist1_y_variable_component = 0
hist1_y_edges_type = list
Expand Down

0 comments on commit e515f7c

Please sign in to comment.