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

Fix Spatial Wrapped invalid env/radii detection #1182

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
68 changes: 56 additions & 12 deletions tests/test_cases/runtime/messaging/test_spatial_2d.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* Tests cover:
* > mandatory messaging, send/recieve
*/
#include <array>
#include "flamegpu/flamegpu.h"

#include "gtest/gtest.h"

namespace flamegpu {


namespace test_message_spatial2d {

FLAMEGPU_AGENT_FUNCTION(out_mandatory2D, MessageNone, MessageSpatial2D) {
Expand Down Expand Up @@ -916,15 +915,12 @@ FLAMEGPU_AGENT_FUNCTION(in_wrapped_EnvDimsNotFactor, MessageSpatial2D, MessageNo
}
return ALIVE;
}
TEST(Spatial2DMessageTest, Wrapped_EnvDimsNotFactor) {
// This tests that bug #1157 is fixed
// When the interaction radius is not a factor of the width
// that agent's near the max env bound all have the full interaction radius
void wrapped_2d_test_bounds(std::array<float, 2> lower, std::array<float, 2> upper, float radius, bool exceptionExpected) {
ModelDescription m("model");
MessageSpatial2D::Description message = m.newMessage<MessageSpatial2D>("location");
message.setMin(0, 0);
message.setMax(50.1f, 50.1f);
message.setRadius(10);
message.setMin(lower.at(0), lower.at(1));
message.setMax(upper.at(0), upper.at(1));
message.setRadius(radius);
message.newVariable<flamegpu::id_t>("id"); // unused by current test
AgentDescription agent = m.newAgent("agent");
agent.newVariable<float>("x");
Expand All @@ -945,11 +941,59 @@ TEST(Spatial2DMessageTest, Wrapped_EnvDimsNotFactor) {
// Vertical pair that can interact
// Top side
AgentVector::Agent i1 = population[0];
i1.setVariable<float>("x", 25.0f);
i1.setVariable<float>("y", 25.0f);
i1.setVariable<float>("x", upper.at(0));
i1.setVariable<float>("y", upper.at(1));
c.setPopulationData(population);
c.SimulationConfig().steps = 1;
EXPECT_THROW(c.simulate(), exception::DeviceError);
if (exceptionExpected) {
EXPECT_THROW(c.simulate(), exception::DeviceError);
} else {
EXPECT_NO_THROW(c.simulate());
}
}

template <typename T>
bool approxExactlyDivisible(T a, T b) {
// Scale machine epsilon by the magnitude of the larger value
T scaledEpsilon = std::max(std::abs(a), std::abs(b)) * std::numeric_limits<T>::epsilon();
// Compute the remainder
T v = std::fmod(a, b);
// approx equal if the remainder is within scaledEpsilon of 0 or b (fmod(1, 0.05f) returns ~0.05f)
return v <= scaledEpsilon || v > b - scaledEpsilon;
}

bool wrappedCompatible(float lower, float upper, float radius) {
// @todo - validate that upper is > lower?, upper != lower etc, radius != 0 && radius < upper-lower
return approxExactlyDivisible<float>(upper - lower, radius);
}


TEST(Spatial2DMessageTest, Wrapped_EnvDimsNotFactor) {
// This tests that bug #1157 is fixed
// When the interaction radius is not a factor of the width
// that agent's near the max env bound all have the full interaction radius
wrapped_2d_test_bounds({0, 0}, {50.1f, 50.1f}, 10, true);
// also includes a number of potential edge cases to ensure that no false positives are included (#1177)

wrappedCompatible(0, 1, 0.05f);
wrappedCompatible(0, 1, 0.04f);
wrappedCompatible(0, 2, 0.05f);
wrappedCompatible(0, 1, 0.005f);
wrappedCompatible(0, 1, 0.005f);

wrappedCompatible(0, 100000, 0.05f);
wrappedCompatible(0, 100000, 0.03f);


wrappedCompatible(0, 1, 0.03f);

wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.05f, false);
wrapped_2d_test_bounds({0, 0}, {2, 1}, 0.05f, false);
wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.04f, false);

wrapped_2d_test_bounds({0, 0}, {1, 1}, 0.03f, true);


}
#else
TEST(Spatial2DMessageTest, DISABLED_Wrapped_OutOfBounds) { }
Expand Down
37 changes: 23 additions & 14 deletions tests/test_cases/runtime/messaging/test_spatial_3d.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* Tests cover:
* > mandatory messaging, send/recieve
*/
#include <array>
#include "flamegpu/flamegpu.h"

#include "gtest/gtest.h"

namespace flamegpu {


namespace test_message_spatial3d {

FLAMEGPU_AGENT_FUNCTION(out_mandatory3D, MessageNone, MessageSpatial3D) {
Expand Down Expand Up @@ -898,7 +897,7 @@ FLAMEGPU_AGENT_FUNCTION(inWrapped3D, MessageSpatial3D, MessageNone) {
void wrapped_3d_test(const float x_offset, const float y_offset, const float z_offset, const float out_of_bounds = 0) {
std::unordered_map<int, unsigned int> bin_counts;
// Construct model
ModelDescription model("Spatial2DMessageTestModel");
ModelDescription model("Spatial3DMessageTestModel");
{ // Location message
MessageSpatial3D::Description message = model.newMessage<MessageSpatial3D>("location");
message.setMin(0 + x_offset, 0 + y_offset, 0 + z_offset);
Expand Down Expand Up @@ -985,15 +984,12 @@ FLAMEGPU_AGENT_FUNCTION(in_wrapped_EnvDimsNotFactor, MessageSpatial3D, MessageNo
}
return ALIVE;
}
TEST(Spatial3DMessageTest, Wrapped_EnvDimsNotFactor) {
// This tests that bug #1157 is fixed
// When the interaction radius is not a factor of the width
// that agent's near the max env bound all have the full interaction radius
void wrapped_3d_test_bounds(std::array<float, 3> lower, std::array<float, 3> upper, float radius, bool exceptionExpected) {
ModelDescription m("model");
MessageSpatial3D::Description message = m.newMessage<MessageSpatial3D>("location");
message.setMin(0, 0, 0);
message.setMax(50.1f, 50.1f, 50.1f);
message.setRadius(10);
message.setMin(lower.at(0), lower.at(1), lower.at(2));
message.setMax(upper.at(0), upper.at(1), upper.at(2));
message.setRadius(radius);
message.newVariable<flamegpu::id_t>("id"); // unused by current test
AgentDescription agent = m.newAgent("agent");
agent.newVariable<float>("x");
Expand All @@ -1015,12 +1011,25 @@ TEST(Spatial3DMessageTest, Wrapped_EnvDimsNotFactor) {
// Vertical pair that can interact
// Top side
AgentVector::Agent i1 = population[0];
i1.setVariable<float>("x", 25.0f);
i1.setVariable<float>("y", 25.0f);
i1.setVariable<float>("z", 25.0f);
i1.setVariable<float>("x", upper.at(0));
i1.setVariable<float>("y", upper.at(1));
i1.setVariable<float>("z", upper.at(2));
c.setPopulationData(population);
c.SimulationConfig().steps = 1;
EXPECT_THROW(c.simulate(), exception::DeviceError);
if (exceptionExpected) {
EXPECT_THROW(c.simulate(), exception::DeviceError);
} else {
EXPECT_NO_THROW(c.simulate());
}
}
TEST(Spatial3DMessageTest, Wrapped_EnvDimsNotFactor) {
// This tests that bug #1157 is fixed
// When the interaction radius is not a factor of the width
// that agent's near the max env bound all have the full interaction radius
wrapped_3d_test_bounds({0, 0, 0}, {50.1f, 50.1f, 50.1f}, 10, true);
// also includes a number of potential edge cases to ensure that no false positives are included (#1177)
wrapped_3d_test_bounds({0, 0, 0}, {1, 1, 1}, 0.05f, false);
wrapped_3d_test_bounds({0, 0, 0}, {3, 2, 1}, 0.05f, false);
}
#else
TEST(Spatial3DMessageTest, DISABLED_Wrapped_OutOfBounds) { }
Expand Down
Loading