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

observables: make observables immutable. #4211

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ doc/dg/background_errors.unsorted
.ycm_extra_conf.py
.ipynb_checkpoints

build*/
/*build*/
2 changes: 1 addition & 1 deletion src/core/accumulators/MeanVarianceCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MeanVarianceCalculator : public AccumulatorBase {
public:
// The accumulator struct has to be initialized with the correct vector size,
// therefore the order of init is important.
MeanVarianceCalculator(std::shared_ptr<Observables::Observable> const &obs,
MeanVarianceCalculator(std::shared_ptr<Observables::Observable> obs,
int delta_N)
: AccumulatorBase(delta_N), m_obs(obs), m_acc(obs->n_values()) {}

Expand Down
2 changes: 1 addition & 1 deletion src/core/observables/CylindricalDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CylindricalDensityProfile : public CylindricalPidProfileObservable {
std::vector<double>
evaluate(Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const override {
Utils::CylindricalHistogram<double, 3> histogram(n_bins, 1, limits);
Utils::CylindricalHistogram<double, 3> histogram(n_bins(), 1, limits());

for (auto p : particles) {
histogram.update(Utils::transform_coordinate_cartesian_to_cylinder(
Expand Down
5 changes: 3 additions & 2 deletions src/core/observables/CylindricalFluxDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CylindricalFluxDensityProfile : public CylindricalPidProfileObservable {
std::vector<double>
evaluate(Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const override {
Utils::CylindricalHistogram<double, 3> histogram(n_bins, 3, limits);
Utils::CylindricalHistogram<double, 3> histogram(n_bins(), 3, limits());

// Write data to the histogram
for (auto p : particles) {
Expand All @@ -55,7 +55,8 @@ class CylindricalFluxDensityProfile : public CylindricalPidProfileObservable {
return histogram.get_histogram();
}
std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ std::vector<double>
CylindricalLBFluxDensityProfileAtParticlePositions::evaluate(
Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const {

Utils::CylindricalHistogram<double, 3> histogram(n_bins, 3, limits);
Utils::CylindricalHistogram<double, 3> histogram(n_bins(), 3, limits());
// First collect all positions (since we want to call the LB function to
// get the fluid velocities only once).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class CylindricalLBFluxDensityProfileAtParticlePositions
const ParticleObservables::traits<Particle> &traits) const override;

std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/core/observables/CylindricalLBProfileObservable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ class CylindricalLBProfileObservable : public CylindricalProfileObservable {
calculate_sampling_positions();
}
void calculate_sampling_positions() {
auto const lim = limits();
auto const b = n_bins();
sampling_positions = Utils::get_cylindrical_sampling_positions(
limits[0], limits[1], limits[2], n_bins[0], n_bins[1], n_bins[2],
sampling_density);
lim[0], lim[1], lim[2], b[0], b[1], b[2], sampling_density);
for (auto &p : sampling_positions) {
auto p_cart = Utils::transform_coordinate_cylinder_to_cartesian(p);
// We have to rotate the coordinates since the utils function assumes
Expand Down
2 changes: 1 addition & 1 deletion src/core/observables/CylindricalLBVelocityProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace Observables {

std::vector<double> CylindricalLBVelocityProfile::operator()() const {
Utils::CylindricalHistogram<double, 3> histogram(n_bins, 3, limits);
Utils::CylindricalHistogram<double, 3> histogram(n_bins(), 3, limits());
for (auto const &p : sampling_positions) {
auto const velocity = lb_lbfluid_get_interpolated_velocity(p) *
lb_lbfluid_get_lattice_speed();
Expand Down
3 changes: 2 additions & 1 deletion src/core/observables/CylindricalLBVelocityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class CylindricalLBVelocityProfile : public CylindricalLBProfileObservable {
using CylindricalLBProfileObservable::CylindricalLBProfileObservable;
std::vector<double> operator()() const override;
std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Observables {
std::vector<double> CylindricalLBVelocityProfileAtParticlePositions::evaluate(
Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const {
Utils::CylindricalHistogram<double, 3> histogram(n_bins, 3, limits);
Utils::CylindricalHistogram<double, 3> histogram(n_bins(), 3, limits());

for (auto p : particles) {
auto const pos = folded_position(traits.position(p), box_geo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class CylindricalLBVelocityProfileAtParticlePositions
const ParticleObservables::traits<Particle> &traits) const override;

std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/core/observables/CylindricalVelocityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CylindricalVelocityProfile : public CylindricalPidProfileObservable {
std::vector<double>
evaluate(Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const override {
Utils::CylindricalHistogram<double, 3> histogram(n_bins, 3, limits);
Utils::CylindricalHistogram<double, 3> histogram(n_bins(), 3, limits());

for (auto p : particles) {
auto const pos = folded_position(traits.position(p), box_geo) -
Expand All @@ -62,7 +62,8 @@ class CylindricalVelocityProfile : public CylindricalPidProfileObservable {
return hist_tmp;
}
std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/observables/DensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DensityProfile : public PidProfileObservable {
std::vector<double>
evaluate(Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const override {
Utils::Histogram<double, 3> histogram(n_bins, 1, limits);
Utils::Histogram<double, 3> histogram(n_bins(), 1, limits());

for (auto p : particles) {
histogram.update(folded_position(traits.position(p), box_geo));
Expand Down
5 changes: 3 additions & 2 deletions src/core/observables/FluxDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class FluxDensityProfile : public PidProfileObservable {
public:
using PidProfileObservable::PidProfileObservable;
std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}

std::vector<double>
evaluate(Utils::Span<std::reference_wrapper<const Particle>> particles,
const ParticleObservables::traits<Particle> &traits) const override {
Utils::Histogram<double, 3> histogram(n_bins, 3, limits);
Utils::Histogram<double, 3> histogram(n_bins(), 3, limits());

for (auto p : particles) {
auto const ppos = folded_position(traits.position(p), box_geo);
Expand Down
5 changes: 3 additions & 2 deletions src/core/observables/ForceDensityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ class ForceDensityProfile : public PidProfileObservable {
public:
using PidProfileObservable::PidProfileObservable;
std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}

std::vector<double>
evaluate(ParticleReferenceRange particles,
const ParticleObservables::traits<Particle> &traits) const override {
Utils::Histogram<double, 3> histogram(n_bins, 3, limits);
Utils::Histogram<double, 3> histogram(n_bins(), 3, limits());
for (auto p : particles) {
histogram.update(folded_position(p.get().r.p, box_geo), p.get().f.f);
}
Expand Down
13 changes: 7 additions & 6 deletions src/core/observables/LBProfileObservable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,26 @@ class LBProfileObservable : public ProfileObservable {
bool allow_empty_bins;
std::vector<Utils::Vector3d> sampling_positions;
void calculate_sampling_positions() {
auto const lim = limits();
sampling_positions.clear();
if (sampling_delta[0] == 0 or sampling_delta[1] == 0 or
sampling_delta[2] == 0)
throw std::runtime_error("Parameter delta_x/y/z must not be zero!");
const auto n_samples_x = static_cast<size_t>(
std::rint((limits[0].second - limits[0].first) / sampling_delta[0]));
std::rint((lim[0].second - lim[0].first) / sampling_delta[0]));
const auto n_samples_y = static_cast<size_t>(
std::rint((limits[1].second - limits[1].first) / sampling_delta[1]));
std::rint((lim[1].second - lim[1].first) / sampling_delta[1]));
const auto n_samples_z = static_cast<size_t>(
std::rint((limits[2].second - limits[2].first) / sampling_delta[2]));
std::rint((lim[2].second - lim[2].first) / sampling_delta[2]));
for (size_t x = 0; x < n_samples_x; ++x) {
for (size_t y = 0; y < n_samples_y; ++y) {
for (size_t z = 0; z < n_samples_z; ++z) {
sampling_positions.push_back(Utils::Vector3d{
{limits[0].first + sampling_offset[0] +
{lim[0].first + sampling_offset[0] +
static_cast<double>(x) * sampling_delta[0],
limits[1].first + sampling_offset[1] +
lim[1].first + sampling_offset[1] +
static_cast<double>(y) * sampling_delta[1],
limits[2].first + sampling_offset[2] +
lim[2].first + sampling_offset[2] +
static_cast<double>(z) * sampling_delta[2]}});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/observables/LBVelocityProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace Observables {

std::vector<double> LBVelocityProfile::operator()() const {
Utils::Histogram<double, 3> histogram(n_bins, 3, limits);
Utils::Histogram<double, 3> histogram(n_bins(), 3, limits());
for (auto const &p : sampling_positions) {
const auto v = lb_lbfluid_get_interpolated_velocity(p) *
lb_lbfluid_get_lattice_speed();
Expand Down
3 changes: 2 additions & 1 deletion src/core/observables/LBVelocityProfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class LBVelocityProfile : public LBProfileObservable {
public:
using LBProfileObservable::LBProfileObservable;
std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2], 3};
auto const b = n_bins();
return {b[0], b[1], b[2], 3};
}
std::vector<double> operator()() const override;
};
Expand Down
56 changes: 35 additions & 21 deletions src/core/observables/ProfileObservable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,51 @@ namespace Observables {

/** Cartesian profile observable */
class ProfileObservable : virtual public Observable {
private:
/** Range of the profile edges. */
std::array<std::pair<double, double>, 3> m_limits;
/** Number of bins for each coordinate. */
std::array<size_t, 3> m_n_bins;

public:
ProfileObservable(int n_x_bins, int n_y_bins, int n_z_bins, double min_x,
double max_x, double min_y, double max_y, double min_z,
double max_z)
: limits{{std::make_pair(min_x, max_x), std::make_pair(min_y, max_y),
std::make_pair(min_z, max_z)}},
n_bins{{static_cast<size_t>(n_x_bins), static_cast<size_t>(n_y_bins),
static_cast<size_t>(n_z_bins)}} {}
/** Range of the profile edges. */
std::array<std::pair<double, double>, 3> limits;
/** Number of bins for each coordinate. */
std::array<size_t, 3> n_bins;
: m_limits{{std::make_pair(min_x, max_x), std::make_pair(min_y, max_y),
std::make_pair(min_z, max_z)}},
m_n_bins{{static_cast<size_t>(n_x_bins), static_cast<size_t>(n_y_bins),
static_cast<size_t>(n_z_bins)}} {
if (max_x <= min_x)
throw std::runtime_error("max_x has to be > min_x");
if (max_y <= min_y)
throw std::runtime_error("max_y has to be > min_y");
if (max_z <= min_z)
throw std::runtime_error("max_z has to be > min_z");
}

std::vector<size_t> shape() const override {
return {n_bins[0], n_bins[1], n_bins[2]};
return {m_n_bins[0], m_n_bins[1], m_n_bins[2]};
}

auto n_bins() const { return m_n_bins; }

auto limits() const { return m_limits; }

/** Calculate the bin edges for each dimension */
std::array<std::vector<double>, 3> edges() {
std::array<std::vector<double>, 3> edges() const {
std::array<std::vector<double>, 3> profile_edges = {
{std::vector<double>(n_bins[0] + 1), std::vector<double>(n_bins[1] + 1),
std::vector<double>(n_bins[2] + 1)}};
boost::copy(
Utils::make_lin_space(limits[0].first, limits[0].second, n_bins[0] + 1),
profile_edges[0].begin());
boost::copy(
Utils::make_lin_space(limits[1].first, limits[1].second, n_bins[1] + 1),
profile_edges[1].begin());
boost::copy(
Utils::make_lin_space(limits[2].first, limits[2].second, n_bins[2] + 1),
profile_edges[2].begin());
{std::vector<double>(m_n_bins[0] + 1),
std::vector<double>(m_n_bins[1] + 1),
std::vector<double>(m_n_bins[2] + 1)}};
boost::copy(Utils::make_lin_space(m_limits[0].first, m_limits[0].second,
m_n_bins[0] + 1),
profile_edges[0].begin());
boost::copy(Utils::make_lin_space(m_limits[1].first, m_limits[1].second,
m_n_bins[1] + 1),
profile_edges[1].begin());
boost::copy(Utils::make_lin_space(m_limits[2].first, m_limits[2].second,
m_n_bins[2] + 1),
profile_edges[2].begin());
return profile_edges;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CylindricalTransformationParameters
[this]() { return m_transform_params->orientation(); }}});
}
std::shared_ptr<::Utils::CylindricalTransformationParameters>
cyl_transform_params() {
cyl_transform_params() const {
return m_transform_params;
}
void do_construct(VariantMap const &params) override {
Expand Down
Loading