Skip to content

Commit

Permalink
ShapeFunction supplemented by getPrimaryName()
Browse files Browse the repository at this point in the history
  • Loading branch information
PKua007 committed Sep 19, 2023
1 parent 9ccf854 commit b6e7baf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/core/observables/BinAveragedFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class BinAveragedFunction : public BulkObservable {
/**
* @brief Returns "rho_xyz" as the signature name.
*/
[[nodiscard]] std::string getSignatureName() const override { return "temp_xyz"; };
[[nodiscard]] std::string getSignatureName() const override {
return this->shapeFunction->getPrimaryName() + "_xyz";
}

/**
* @brief Dumps a flat list of HistogramBuilder3D::BinValue -s averaged over snapshots.
Expand Down
2 changes: 2 additions & 0 deletions src/core/observables/ShapeFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ShapeFunction {
*/
virtual void calculate(const Shape &shape, const ShapeTraits &traits) = 0;

[[nodiscard]] virtual std::string getPrimaryName() const = 0;

[[nodiscard]] virtual std::vector<std::string> getNames() const = 0;

[[nodiscard]] virtual std::vector<double> getValues() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ConstantShapeFunction : public ShapeFunction {

void calculate([[maybe_unused]] const Shape &shape, [[maybe_unused]] const ShapeTraits &traits) override { }

[[nodiscard]] std::string getPrimaryName() const override { return "const"; }
[[nodiscard]] std::vector<std::string> getNames() const override { return {"const"}; }
[[nodiscard]] std::vector<double> getValues() const override { return {this->value}; }
};
Expand Down
3 changes: 2 additions & 1 deletion src/core/observables/shape_functions/ShapeAxisCoordinate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class ShapeAxisCoordinate : public ShapeFunction {

void calculate(const Shape &shape, const ShapeTraits &traits) override;

[[nodiscard]] std::vector<double> getValues() const override { return {this->value}; }
[[nodiscard]] std::string getPrimaryName() const override { return this->name; }
[[nodiscard]] std::vector<std::string> getNames() const override { return {this->name}; }
[[nodiscard]] std::vector<double> getValues() const override { return {this->value}; }
};


Expand Down
1 change: 1 addition & 0 deletions test/mocks/MockShapeFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MockShapeFunction : public trompeloeil::mock_interface<ShapeFunction> {
IMPLEMENT_MOCK2(calculate);
IMPLEMENT_CONST_MOCK0(getValues);
IMPLEMENT_CONST_MOCK0(getNames);
IMPLEMENT_CONST_MOCK0(getPrimaryName);
};


Expand Down
39 changes: 23 additions & 16 deletions test/unit_tests/core/observables/BinAveragedFunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,38 @@ TEST_CASE("BinAveragedFunction") {
ALLOW_CALL(*mockShapeFunction, calculate(std::ref(packing[2]), _)).LR_SIDE_EFFECT(functionValue = 4);
ALLOW_CALL(*mockShapeFunction, calculate(std::ref(packing[3]), _)).LR_SIDE_EFFECT(functionValue = 5);
ALLOW_CALL(*mockShapeFunction, getValues()).LR_RETURN(std::vector<double>{functionValue});
ALLOW_CALL(*mockShapeFunction, getPrimaryName()).RETURN("func");

auto densityFunction = std::make_shared<ConstantShapeFunction>();
auto tracker = std::make_unique<FourierTracker>(std::array<std::size_t, 3>{1, 0, 0}, densityFunction);
BinAveragedFunction binAveragedFunction({10, 0, 0}, std::move(mockShapeFunction), std::move(tracker));

// We do Fourier tracking on the density - in each snapshot we move a bit in the x direction
for (std::size_t i{}; i < 10; i++) {
binAveragedFunction.addSnapshot(packing, 1, 1, traits);
for (std::size_t j{}; j < packing.size(); j++) {
packing.tryTranslation(j, {0.3, 0, 0}, traits.getInteraction());
packing.acceptTranslation();
SECTION("the histogram") {
// We do Fourier tracking on the density - in each snapshot we move a bit in the x direction
for (std::size_t i{}; i < 10; i++) {
binAveragedFunction.addSnapshot(packing, 1, 1, traits);
for (std::size_t j{}; j < packing.size(); j++) {
packing.tryTranslation(j, {0.3, 0, 0}, traits.getInteraction());
packing.acceptTranslation();
}
}
}

auto histogram = binAveragedFunction.dumpValues();
using BinValue = decltype(histogram)::value_type;
auto nanRemover = [](const BinValue &binValue) { return std::isnan(binValue.value[0]); };
histogram.erase(std::remove_if(histogram.begin(), histogram.end(), nanRemover), histogram.end());
std::vector<BinValue> expected = {
auto histogram = binAveragedFunction.dumpValues();
using BinValue = decltype(histogram)::value_type;
auto nanRemover = [](const BinValue &binValue) { return std::isnan(binValue.value[0]); };
histogram.erase(std::remove_if(histogram.begin(), histogram.end(), nanRemover), histogram.end());
std::vector<BinValue> expected = {
{{0.45, 0.5, 0.5}, {1}},
{{0.55, 0.5, 0.5}, {3}},
{{0.65, 0.5, 0.5}, {5}}
};
for (auto[expectedItem, actualItem] : Zip(expected, histogram)) {
CHECK_THAT(actualItem.binMiddle, IsApproxEqual(expectedItem.binMiddle, 1e-12));
CHECK(actualItem.value[0] == Approx(expectedItem.value[0]));
};
for (auto [expectedItem, actualItem]: Zip(expected, histogram)) {
CHECK_THAT(actualItem.binMiddle, IsApproxEqual(expectedItem.binMiddle, 1e-12));
CHECK(actualItem.value[0] == Approx(expectedItem.value[0]));
}
}

SECTION("signature name") {
CHECK(binAveragedFunction.getSignatureName() == "func_xyz");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace {
this->value = this->function(shape, traits);
}

[[nodiscard]] std::string getPrimaryName() const override { return this->name; }
[[nodiscard]] std::vector<std::string> getNames() const override { return {this->name}; }
[[nodiscard]] std::vector<double> getValues() const override { return {this->value}; }
};
Expand Down

0 comments on commit b6e7baf

Please sign in to comment.