From 27241028ddf76fbd8f06525cda7809bd6be8d9d0 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 10 Nov 2023 13:45:55 -0600 Subject: [PATCH 1/5] Adding includes to interpolate header --- include/openmc/interpolate.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/openmc/interpolate.h b/include/openmc/interpolate.h index 05353f1306b..b3ad893ade8 100644 --- a/include/openmc/interpolate.h +++ b/include/openmc/interpolate.h @@ -4,7 +4,10 @@ #include #include +#include + #include "openmc/search.h" +#include "openmc/error.h" namespace openmc { From 022fa01c7432d857dce32ae1838ae485821b017d Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 10 Nov 2023 13:48:36 -0600 Subject: [PATCH 2/5] Adding new test file for interpolation --- tests/cpp_unit_tests/CMakeLists.txt | 1 + tests/cpp_unit_tests/test_interpolate.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/cpp_unit_tests/test_interpolate.cpp diff --git a/tests/cpp_unit_tests/CMakeLists.txt b/tests/cpp_unit_tests/CMakeLists.txt index 50ae9a20821..24ec1b7a90e 100644 --- a/tests/cpp_unit_tests/CMakeLists.txt +++ b/tests/cpp_unit_tests/CMakeLists.txt @@ -2,6 +2,7 @@ set(TEST_NAMES test_distribution test_file_utils test_tally + test_interpolate # Add additional unit test files here ) diff --git a/tests/cpp_unit_tests/test_interpolate.cpp b/tests/cpp_unit_tests/test_interpolate.cpp new file mode 100644 index 00000000000..60aa4713717 --- /dev/null +++ b/tests/cpp_unit_tests/test_interpolate.cpp @@ -0,0 +1,11 @@ +#include + +#include "openmc/interpolate.h" + +using namespace openmc; + +TEST_CASE("Test Lagranian Interpolation") +{ + + std::vector vals, xs, ys; +} \ No newline at end of file From 947caa7cd875bbc6a78c032dc183350e6540cd86 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 13 Nov 2023 15:57:21 -0600 Subject: [PATCH 3/5] Adding lagrangian interpolation tests --- tests/cpp_unit_tests/test_interpolate.cpp | 42 ++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/cpp_unit_tests/test_interpolate.cpp b/tests/cpp_unit_tests/test_interpolate.cpp index 60aa4713717..5c0a587c453 100644 --- a/tests/cpp_unit_tests/test_interpolate.cpp +++ b/tests/cpp_unit_tests/test_interpolate.cpp @@ -1,11 +1,51 @@ +#include +#include + #include +#include + #include "openmc/interpolate.h" +#include "openmc/search.h" using namespace openmc; TEST_CASE("Test Lagranian Interpolation") { + std::vector xs {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; + std::vector ys {0.0, 1.0, 1.0, 2.0, 3.0, 3.0, 5.0}; + + // ensure we get data points back at the x values + for (int n = 1; n <= 6; n++) { + for (int i = 0; i < xs.size(); i++) { + double x = xs[i]; + double y = ys[i]; + + size_t idx = lower_bound_index(xs.begin(), xs.end(), x); + idx = std::min(idx, xs.size() - n - 1); + double out = interpolate_lagrangian(xs, ys, idx, x, n); + REQUIRE(out == y); + } + } + + // spot checks based on an independent implementation of Lagrangian interpolation + std::map>> checks; + checks[1] = {{0.5, 0.5}, {4.5, 3.0}, {2.5, 1.5}, {5.5, 4.0}}; + checks[2] = {{2.5, 1.5}, {4.5, 2.75}, {4.9999, 3.0}, {4.00001, 3.0}}; + checks[3] = {{2.5592, 1.5}, {4.5, 2.9375}, {4.9999, 3.0}, {4.00001, 3.0}}; + + for (auto check_set : checks) { + int order = check_set.first; + auto checks = check_set.second; + + for (auto check : checks) { + double input = check.first; + double exp_output = check.second; - std::vector vals, xs, ys; + size_t idx = lower_bound_index(xs.begin(), xs.end(), input); + idx = std::min(idx, xs.size() - order - 1); + double out = interpolate_lagrangian(xs, ys, idx, input, order); + REQUIRE_THAT(out, Catch::Matchers::WithinAbs(exp_output, 1e-04)); + } + } } \ No newline at end of file From 8afd525af83440e6168f4c78c6fab5809cd96fbe Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 13 Nov 2023 16:06:41 -0600 Subject: [PATCH 4/5] Correction for index of y-value in Lagrangian interpolation --- include/openmc/interpolate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/openmc/interpolate.h b/include/openmc/interpolate.h index b3ad893ade8..4c88ac9bddf 100644 --- a/include/openmc/interpolate.h +++ b/include/openmc/interpolate.h @@ -50,7 +50,7 @@ inline double interpolate_lagrangian(gsl::span xs, numerator *= (x - xs[idx + j]); denominator *= (xs[idx + i] - xs[idx + j]); } - output += (numerator / denominator) * ys[i]; + output += (numerator / denominator) * ys[idx + i]; } return output; From e5f97796fff3004a25b5c97e1f75f47b8c4bb236 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 13 Nov 2023 16:09:18 -0600 Subject: [PATCH 5/5] Applying style guide --- include/openmc/interpolate.h | 2 +- tests/cpp_unit_tests/test_interpolate.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/openmc/interpolate.h b/include/openmc/interpolate.h index 4c88ac9bddf..e36fbb1d603 100644 --- a/include/openmc/interpolate.h +++ b/include/openmc/interpolate.h @@ -6,8 +6,8 @@ #include -#include "openmc/search.h" #include "openmc/error.h" +#include "openmc/search.h" namespace openmc { diff --git a/tests/cpp_unit_tests/test_interpolate.cpp b/tests/cpp_unit_tests/test_interpolate.cpp index 5c0a587c453..4f19f2b63f3 100644 --- a/tests/cpp_unit_tests/test_interpolate.cpp +++ b/tests/cpp_unit_tests/test_interpolate.cpp @@ -4,7 +4,6 @@ #include #include - #include "openmc/interpolate.h" #include "openmc/search.h" @@ -28,7 +27,8 @@ TEST_CASE("Test Lagranian Interpolation") } } - // spot checks based on an independent implementation of Lagrangian interpolation + // spot checks based on an independent implementation of Lagrangian + // interpolation std::map>> checks; checks[1] = {{0.5, 0.5}, {4.5, 3.0}, {2.5, 1.5}, {5.5, 4.0}}; checks[2] = {{2.5, 1.5}, {4.5, 2.75}, {4.9999, 3.0}, {4.00001, 3.0}};