From bc54169b15f00308d5a3aebb82369faa86de46e5 Mon Sep 17 00:00:00 2001 From: Ulf Schiller Date: Thu, 16 Apr 2026 13:23:09 -0400 Subject: [PATCH 01/19] fixup! syntax formating with clang and costexpr allcaps --- src/LATBOLTZ/fix_lb_multicomponent.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LATBOLTZ/fix_lb_multicomponent.h b/src/LATBOLTZ/fix_lb_multicomponent.h index 95f4857628f..4c491e3762d 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.h +++ b/src/LATBOLTZ/fix_lb_multicomponent.h @@ -19,7 +19,7 @@ ------------------------------------------------------------------------- */ #ifdef FIX_CLASS -FixStyle(lb / multicomponent, FixLbMulticomponent) +FixStyle(lb/multicomponent, FixLbMulticomponent) #else #ifndef LMP_FIX_LB_MULTICOMPONENT_H From 43b3849a3c0c794bff1799377b718a1af258a5e8 Mon Sep 17 00:00:00 2001 From: abhin suresh <47874320+abhinsuresh@users.noreply.github.com> Date: Tue, 12 May 2026 10:05:33 -0400 Subject: [PATCH 02/19] Feature/ci (#1) * Create ctest ci.yml which runs on push * adding changes to src/version.h for ci test * adding small changes to version.h to make it pass * Update ci.yml --- .github/workflows/ci.yml | 26 ++++++++++++++++++++++++++ src/version.h | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..c03bc09b726 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: ctest CI + +on: + pull_request: + branches: + - ternary-lbm + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Configure with tests + run: | + cmake -S cmake -B build -D PACKAGE_LATBOLTZ=yes \ + -D ENABLE_TESTING=yes + + - name: Build + run: | + cmake --build build --parallel 4 + + - name: Run tests + run: ctest --test-dir build --output-on-failure diff --git a/src/version.h b/src/version.h index b64909e129c..f4588344634 100644 --- a/src/version.h +++ b/src/version.h @@ -1,2 +1,2 @@ #define LAMMPS_VERSION "27 Dec 2024" -#define LAMMPS_UPDATE "Development @ 4a3bfb5" +#define LAMMPS_UPDATE "Update 10" From abb17321ae5831a80384c17010ddf03400cbcbc6 Mon Sep 17 00:00:00 2001 From: abhin suresh <47874320+abhinsuresh@users.noreply.github.com> Date: Tue, 12 May 2026 11:27:01 -0400 Subject: [PATCH 03/19] adding small changes to src/version.h to see PR failing (#2) * adding small changes to src/version.h to see PR failing * adding small changes to src/version.h to fix PR fail From 349ce2547af46d07db5d163251555a707094b72c Mon Sep 17 00:00:00 2001 From: abhin suresh <47874320+abhinsuresh@users.noreply.github.com> Date: Tue, 12 May 2026 14:48:49 -0400 Subject: [PATCH 04/19] adding ctest for checking lb_multicomponent test (#3) * adding ctest for checking lb_multicomponent test * fix in spacing, fix error in cmake PACKAGE_ to PKG_, fix error in ci cmake latboltz needs BUILD_MPI * adding install dependencies like mpi on ci.yml * fixing install dependencies like mpi on ci.yml * fixing install dependencies space after \ on ci.yml * only running ctest for the lb_mult smoke ci.yml --- .github/workflows/ci.yml | 9 ++++++-- unittest/CMakeLists.txt | 1 + unittest/latboltz/CMakeLists.txt | 9 ++++++++ unittest/latboltz/in.lb_multicomponent_smoke | 22 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 unittest/latboltz/CMakeLists.txt create mode 100755 unittest/latboltz/in.lb_multicomponent_smoke diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c03bc09b726..13f532c66cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,9 +13,14 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Install dependencies + run: | + sudo apt-get install -y openmpi-bin libopenmpi-dev + - name: Configure with tests run: | - cmake -S cmake -B build -D PACKAGE_LATBOLTZ=yes \ + cmake -S cmake -B build -D BUILD_MPI=yes \ + -D PKG_LATBOLTZ=yes \ -D ENABLE_TESTING=yes - name: Build @@ -23,4 +28,4 @@ jobs: cmake --build build --parallel 4 - name: Run tests - run: ctest --test-dir build --output-on-failure + run: ctest --test-dir build -R lb_multicomponent_smoke --output-on-failure diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 9819a53e103..eee77c09d5b 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -98,6 +98,7 @@ add_subdirectory(fortran) add_subdirectory(python) add_subdirectory(tools) add_subdirectory(force-styles) +add_subdirectory(latboltz) # clang-format support for test sources diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt new file mode 100644 index 00000000000..f8a74ec8065 --- /dev/null +++ b/unittest/latboltz/CMakeLists.txt @@ -0,0 +1,9 @@ +add_test( + NAME lb_multicomponent_smoke + COMMAND $ -log none -echo none -in + ${CMAKE_CURRENT_SOURCE_DIR}/in.lb_multicomponent_smoke +) + +set_tests_properties(lb_multicomponent_smoke PROPERTIES + PASS_REGULAR_EXPRESSION "Total wall time" +) diff --git a/unittest/latboltz/in.lb_multicomponent_smoke b/unittest/latboltz/in.lb_multicomponent_smoke new file mode 100755 index 00000000000..0e3bea75b89 --- /dev/null +++ b/unittest/latboltz/in.lb_multicomponent_smoke @@ -0,0 +1,22 @@ +#===========================================================================# +# Decomposition of a ternary fluid mixture # +#===========================================================================# +region fluid block -16 16 -16 16 -3 3 +create_box 0 fluid + +timestep 1.0 + +#--------------------------------------------------------------------------- +# Create a multicomponent lattice Boltzmann mixture in the simulation domain +# Use D3Q19 lattice with standard parameters +# fluid density = 1.0, fluid viscosity = 1/6, lattice spacing dx=1.0 +#---------------------------------------------------------------------------- +variable C1 equal 0.333333 +variable C2 equal 0.333333 +variable C3 equal 0.333334 +variable tag string ternary_mixture-C1=${C1}_C2=${C2}_C3=${C3} +variable nsteps equal 100 + +fix mcmp all lb/multicomponent 1 0.166667 1.0 D3Q19 dx 1 dumpxdmf 100 ${tag} C1 ${C1} C2 ${C2} C3 ${C3} kappa1 0.01 kappa2 0.02 kappa3 0.05 init mixture + +run ${nsteps} From 850ff07c1187efb2ec11fe6a31cf6376da80d2c4 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Mon, 24 Aug 2026 17:55:24 -0400 Subject: [PATCH 05/19] lbmulticomponnent unit test working executable added --- unittest/force-styles/CMakeLists.txt | 10 ++++ .../test_fix_lb_multicomponent.cpp | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 unittest/force-styles/test_fix_lb_multicomponent.cpp diff --git a/unittest/force-styles/CMakeLists.txt b/unittest/force-styles/CMakeLists.txt index 16a704ce576..dca2d47b086 100644 --- a/unittest/force-styles/CMakeLists.txt +++ b/unittest/force-styles/CMakeLists.txt @@ -214,6 +214,16 @@ endif() set_tests_properties(${TNAME} PROPERTIES LABELS "${TEST_TAGS}") endforeach() +#test for lattice boltzman multicomponent +add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) +if(NOT BUILD_SHARED_LIBS) + target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) +endif() +target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps GTest::GTest) + +add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent) + + # dihedral style tester add_executable(test_dihedral_style test_dihedral_style.cpp) target_link_libraries(test_dihedral_style PRIVATE lammps style_tests) diff --git a/unittest/force-styles/test_fix_lb_multicomponent.cpp b/unittest/force-styles/test_fix_lb_multicomponent.cpp new file mode 100644 index 00000000000..6eee1a63835 --- /dev/null +++ b/unittest/force-styles/test_fix_lb_multicomponent.cpp @@ -0,0 +1,54 @@ +#include "gtest/gtest.h" + +#include "input.h" +#include "lammps.h" + +#include + +#include + +using namespace LAMMPS_NS; + +TEST(FixLBMulticomponent, plain) +{ + const char *args[] = {"FixLBMulticomponent", "-log", "none", + "-echo", "screen", "-nocite"}; + + char **argv = (char **)args; + int argc = sizeof(args) / sizeof(char *); + + LAMMPS *lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); + + auto command = [&](const std::string &line) + { + lmp->input->one(line); + }; + + command("region fluid block -16 16 -16 16 -3 3"); + command("create_box 0 fluid"); + + command("timestep 1.0"); + + command( + "fix mcmp all lb/multicomponent " + "1 0.166667 1.0 D3Q19 dx 1 " + "C1 0.333333 C2 0.333333 C3 0.333334 " + "kappa1 0.01 kappa2 0.02 kappa3 0.05 " + "init mixture" + ); + + command("run 10"); + + delete lmp; +} + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + + ::testing::InitGoogleTest(&argc, argv); + int result = RUN_ALL_TESTS(); + + MPI_Finalize(); + return result; +} From 277994173659eedca613a3a3f18749887fd33f7d Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Tue, 25 Aug 2026 15:53:29 -0400 Subject: [PATCH 06/19] moved basic unit/smoke test from force-style to folder latboltz --- unittest/force-styles/CMakeLists.txt | 10 ----- unittest/latboltz/CMakeLists.txt | 15 ++++--- unittest/latboltz/in.lb_multicomponent_smoke | 22 ----------- .../test_fix_lb_multicomponent.cpp | 39 +++++++++++++------ 4 files changed, 35 insertions(+), 51 deletions(-) delete mode 100755 unittest/latboltz/in.lb_multicomponent_smoke rename unittest/{force-styles => latboltz}/test_fix_lb_multicomponent.cpp (59%) diff --git a/unittest/force-styles/CMakeLists.txt b/unittest/force-styles/CMakeLists.txt index dca2d47b086..16a704ce576 100644 --- a/unittest/force-styles/CMakeLists.txt +++ b/unittest/force-styles/CMakeLists.txt @@ -214,16 +214,6 @@ endif() set_tests_properties(${TNAME} PROPERTIES LABELS "${TEST_TAGS}") endforeach() -#test for lattice boltzman multicomponent -add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) -if(NOT BUILD_SHARED_LIBS) - target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) -endif() -target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps GTest::GTest) - -add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent) - - # dihedral style tester add_executable(test_dihedral_style test_dihedral_style.cpp) target_link_libraries(test_dihedral_style PRIVATE lammps style_tests) diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index f8a74ec8065..e519d62d588 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -1,9 +1,8 @@ -add_test( - NAME lb_multicomponent_smoke - COMMAND $ -log none -echo none -in - ${CMAKE_CURRENT_SOURCE_DIR}/in.lb_multicomponent_smoke -) +#test for lattice boltzman multicomponent +add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) +if(NOT BUILD_SHARED_LIBS) + target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) +endif() +target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps GTest::GTest) -set_tests_properties(lb_multicomponent_smoke PROPERTIES - PASS_REGULAR_EXPRESSION "Total wall time" -) +add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent) diff --git a/unittest/latboltz/in.lb_multicomponent_smoke b/unittest/latboltz/in.lb_multicomponent_smoke deleted file mode 100755 index 0e3bea75b89..00000000000 --- a/unittest/latboltz/in.lb_multicomponent_smoke +++ /dev/null @@ -1,22 +0,0 @@ -#===========================================================================# -# Decomposition of a ternary fluid mixture # -#===========================================================================# -region fluid block -16 16 -16 16 -3 3 -create_box 0 fluid - -timestep 1.0 - -#--------------------------------------------------------------------------- -# Create a multicomponent lattice Boltzmann mixture in the simulation domain -# Use D3Q19 lattice with standard parameters -# fluid density = 1.0, fluid viscosity = 1/6, lattice spacing dx=1.0 -#---------------------------------------------------------------------------- -variable C1 equal 0.333333 -variable C2 equal 0.333333 -variable C3 equal 0.333334 -variable tag string ternary_mixture-C1=${C1}_C2=${C2}_C3=${C3} -variable nsteps equal 100 - -fix mcmp all lb/multicomponent 1 0.166667 1.0 D3Q19 dx 1 dumpxdmf 100 ${tag} C1 ${C1} C2 ${C2} C3 ${C3} kappa1 0.01 kappa2 0.02 kappa3 0.05 init mixture - -run ${nsteps} diff --git a/unittest/force-styles/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp similarity index 59% rename from unittest/force-styles/test_fix_lb_multicomponent.cpp rename to unittest/latboltz/test_fix_lb_multicomponent.cpp index 6eee1a63835..c921e3c739c 100644 --- a/unittest/force-styles/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" +#include "fix.h" #include "input.h" #include "lammps.h" +#include "modify.h" #include @@ -9,11 +11,12 @@ using namespace LAMMPS_NS; -TEST(FixLBMulticomponent, plain) + +LAMMPS *init_lammps() { const char *args[] = {"FixLBMulticomponent", "-log", "none", "-echo", "screen", "-nocite"}; - + char **argv = (char **)args; int argc = sizeof(args) / sizeof(char *); @@ -26,18 +29,32 @@ TEST(FixLBMulticomponent, plain) command("region fluid block -16 16 -16 16 -3 3"); command("create_box 0 fluid"); - command("timestep 1.0"); + command("fix mcmp all lb/multicomponent " + "1 0.166667 1.0 D3Q19 dx 1 " + "C1 0.333333 C2 0.333333 C3 0.333334 " + "kappa1 0.01 kappa2 0.02 kappa3 0.05 " + "init mixture"); + + return lmp; +} + +void run_lammps(LAMMPS *lmp) +{ + lmp->input->one("run 10"); +} + +TEST(FixLBMulticomponent, plain) +{ + + LAMMPS *lmp = init_lammps(); - command( - "fix mcmp all lb/multicomponent " - "1 0.166667 1.0 D3Q19 dx 1 " - "C1 0.333333 C2 0.333333 C3 0.333334 " - "kappa1 0.01 kappa2 0.02 kappa3 0.05 " - "init mixture" - ); + auto fixes = lmp->modify->get_fix_by_style("lb/multicomponent"); + + ASSERT_EQ(fixes.size(), 1); + ASSERT_NE(fixes[0], nullptr); - command("run 10"); + run_lammps(lmp); delete lmp; } From 323fbab287081a803eb0dbce2a215036d7e245e5 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Tue, 25 Aug 2026 16:12:33 -0400 Subject: [PATCH 07/19] changed the ci for latboltz to the new one --- .github/workflows/ci.yml | 2 +- unittest/latboltz/CMakeLists.txt | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 13f532c66cc..d914862a280 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,4 +28,4 @@ jobs: cmake --build build --parallel 4 - name: Run tests - run: ctest --test-dir build -R lb_multicomponent_smoke --output-on-failure + run: ctest --test-dir build -R 'FixLBMulticomponent:plain' --output-on-failure diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index e519d62d588..63ba431b8a0 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -1,8 +1,13 @@ #test for lattice boltzman multicomponent add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) + if(NOT BUILD_SHARED_LIBS) target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) endif() + target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps GTest::GTest) add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent) + +set_tests_properties(FixLBMulticomponent:plain PROPERTIES + PASS_REGULAR_EXPRESSION "Total wall time") From 4dadbbb2f070b1c64cf128b746b7cabb2e2e0ffc Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Wed, 26 Aug 2026 15:32:11 -0400 Subject: [PATCH 08/19] basic code of generating and reading yaml file added to the current smoke/unit test --- unittest/latboltz/CMakeLists.txt | 24 +++-- .../latboltz/test_fix_lb_multicomponent.cpp | 18 ++++ unittest/latboltz/test_lb_main.cpp | 87 +++++++++++++++++++ unittest/latboltz/test_lb_main.h | 17 ++++ .../latboltz/tests/fix_lb_multicomponent.yaml | 6 ++ 5 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 unittest/latboltz/test_lb_main.cpp create mode 100644 unittest/latboltz/test_lb_main.h create mode 100644 unittest/latboltz/tests/fix_lb_multicomponent.yaml diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index 63ba431b8a0..998d600bf08 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -1,13 +1,27 @@ +set(FORCE_STYLE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../force-styles) + #test for lattice boltzman multicomponent -add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) +add_executable(test_fix_lb_multicomponent + test_fix_lb_multicomponent.cpp + test_lb_main.cpp + ${FORCE_STYLE_TEST_DIR}/yaml_writer.cpp + ${FORCE_STYLE_TEST_DIR}/test_config_reader.cpp) if(NOT BUILD_SHARED_LIBS) target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) endif() -target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps GTest::GTest) +target_include_directories(test_fix_lb_multicomponent PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${FORCE_STYLE_TEST_DIR} + ${LAMMPS_SOURCE_DIR}) + +target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps + GTest::GTest Yaml::Yaml) + -add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent) +add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent + ${CMAKE_CURRENT_SOURCE_DIR}/tests/fix_lb_multicomponent.yaml) -set_tests_properties(FixLBMulticomponent:plain PROPERTIES - PASS_REGULAR_EXPRESSION "Total wall time") +#set_tests_properties(FixLBMulticomponent:plain PROPERTIES +# PASS_REGULAR_EXPRESSION "Total wall time") diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index c921e3c739c..6e9113bd744 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -1,4 +1,6 @@ #include "gtest/gtest.h" +#include "test_lb_main.h" +#include "yaml_writer.h" #include "fix.h" #include "input.h" @@ -22,6 +24,7 @@ LAMMPS *init_lammps() LAMMPS *lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); + // utility lambda to improve readability auto command = [&](const std::string &line) { lmp->input->one(line); @@ -42,6 +45,18 @@ LAMMPS *init_lammps() void run_lammps(LAMMPS *lmp) { lmp->input->one("run 10"); +} + +void generate_yaml_file(const char *outfile, + const TestConfig &config) +{ + LAMMPS *lmp = init_lammps(); + + YamlWriter writer(outfile); + + write_yaml_header(&writer, &config, lmp->version); + + delete lmp; } TEST(FixLBMulticomponent, plain) @@ -59,6 +74,7 @@ TEST(FixLBMulticomponent, plain) delete lmp; } +/* int main(int argc, char **argv) { MPI_Init(&argc, &argv); @@ -69,3 +85,5 @@ int main(int argc, char **argv) MPI_Finalize(); return result; } +*/ + diff --git a/unittest/latboltz/test_lb_main.cpp b/unittest/latboltz/test_lb_main.cpp new file mode 100644 index 00000000000..e9702b5f63a --- /dev/null +++ b/unittest/latboltz/test_lb_main.cpp @@ -0,0 +1,87 @@ +#include "test_lb_main.h" + +#include "test_config_reader.h" +#include "yaml_writer.h" + +#include "gtest/gtest.h" + +#include +#include +#include + +TestConfig test_config; + +bool read_yaml_file(const char *infile, TestConfig &config) +{ + auto reader = TestConfigReader(config); + + if (reader.parse_file(infile)) + return false; + + config.basename = reader.get_basename(); + return true; +} + + +void write_yaml_header(YamlWriter *writer, + const TestConfig *config, + const char *version) +{ + writer->emit("lammps_version", version); + writer->emit("epsilon", config->epsilon); + + std::string block; + + for (auto &command : config->pre_commands) + block += command + "\n"; + + writer->emit_block("pre_commands", block); + + block.clear(); + + for (auto &command : config->post_commands) + block += command + "\n"; + + writer->emit_block("post_commands", block); +} + + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + + ::testing::InitGoogleTest(&argc, argv); + + if (argc == 1){ + int result = RUN_ALL_TESTS(); + MPI_Finalize(); + return result; + } + + if (std::string(argv[1]) == "--generate") { + if (argc < 3) { + std::cerr << "Missing YAML output filename\n"; + MPI_Finalize(); + return 1; + } + + generate_yaml_file(argv[2], test_config); + + MPI_Finalize(); + return 0; + } + + if (!read_yaml_file(argv[1], test_config)) { + std::cerr << "Error reading YAML file: " + << argv[1] << "\n"; + + MPI_Finalize(); + return 2; + } + + int result = RUN_ALL_TESTS(); + + MPI_Finalize(); + + return result; +} diff --git a/unittest/latboltz/test_lb_main.h b/unittest/latboltz/test_lb_main.h new file mode 100644 index 00000000000..4cfc003a1ea --- /dev/null +++ b/unittest/latboltz/test_lb_main.h @@ -0,0 +1,17 @@ +#ifndef LMP_TEST_LB_MAIN_H +#define LMP_TEST_LB_MAIN_H + +#include "test_config.h" + +class YamlWriter; + +extern TestConfig test_config; +// extern std::string INPUT_FOLDER; + +bool read_yaml_file(const char *, TestConfig &); + +void write_yaml_header(YamlWriter *, const TestConfig *, const char *); + +void generate_yaml_file(const char *, const TestConfig &); + +#endif diff --git a/unittest/latboltz/tests/fix_lb_multicomponent.yaml b/unittest/latboltz/tests/fix_lb_multicomponent.yaml new file mode 100644 index 00000000000..00559cf0e8f --- /dev/null +++ b/unittest/latboltz/tests/fix_lb_multicomponent.yaml @@ -0,0 +1,6 @@ +--- +lammps_version: 27 Dec 2024 +epsilon: 1e-14 +pre_commands: ! "" +post_commands: ! "" +... From 09c519df9e6801cdac23c1128fa8449288893f27 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Thu, 27 Aug 2026 16:15:33 -0400 Subject: [PATCH 09/19] moved the lb test from local main to the main in force-styles --- unittest/latboltz/CMakeLists.txt | 22 +++++-------- .../latboltz/test_fix_lb_multicomponent.cpp | 7 ++--- unittest/latboltz/test_lb_main.cpp | 31 ++++++++++++++++--- .../latboltz/tests/fix_lb_multicomponent.yaml | 6 ---- .../tests/test_lb_multicomponent.yaml | 11 +++++++ 5 files changed, 48 insertions(+), 29 deletions(-) delete mode 100644 unittest/latboltz/tests/fix_lb_multicomponent.yaml create mode 100644 unittest/latboltz/tests/test_lb_multicomponent.yaml diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index 998d600bf08..52512d6c2e8 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -1,27 +1,21 @@ set(FORCE_STYLE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../force-styles) +set(TEST_INPUT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/tests) #test for lattice boltzman multicomponent -add_executable(test_fix_lb_multicomponent - test_fix_lb_multicomponent.cpp - test_lb_main.cpp - ${FORCE_STYLE_TEST_DIR}/yaml_writer.cpp - ${FORCE_STYLE_TEST_DIR}/test_config_reader.cpp) +add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) if(NOT BUILD_SHARED_LIBS) target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) endif() -target_include_directories(test_fix_lb_multicomponent PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR} - ${FORCE_STYLE_TEST_DIR} - ${LAMMPS_SOURCE_DIR}) +target_include_directories(test_fix_lb_multicomponent PRIVATE ${FORCE_STYLE_TEST_DIR}) -target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps - GTest::GTest Yaml::Yaml) +target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps style_tests) -add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent - ${CMAKE_CURRENT_SOURCE_DIR}/tests/fix_lb_multicomponent.yaml) - +file(GLOB LB_FLUID_TESTS LIST_DIRECTORIES false ${TEST_INPUT_FOLDER}/test_lb_multicomponent*.yaml) +foreach(TEST ${LB_FLUID_TESTS}) +add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent ${TEST}) +endforeach() #set_tests_properties(FixLBMulticomponent:plain PROPERTIES # PASS_REGULAR_EXPRESSION "Total wall time") diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index 6e9113bd744..3c45ab2847b 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -1,5 +1,5 @@ #include "gtest/gtest.h" -#include "test_lb_main.h" +#include "test_main.h" #include "yaml_writer.h" #include "fix.h" @@ -47,14 +47,13 @@ void run_lammps(LAMMPS *lmp) lmp->input->one("run 10"); } -void generate_yaml_file(const char *outfile, - const TestConfig &config) +void generate_yaml_file(const char *outfile, const TestConfig &config) { LAMMPS *lmp = init_lammps(); YamlWriter writer(outfile); - write_yaml_header(&writer, &config, lmp->version); + write_yaml_header(&writer, &test_config, lmp->version); delete lmp; } diff --git a/unittest/latboltz/test_lb_main.cpp b/unittest/latboltz/test_lb_main.cpp index e9702b5f63a..b9e9e0583c4 100644 --- a/unittest/latboltz/test_lb_main.cpp +++ b/unittest/latboltz/test_lb_main.cpp @@ -2,15 +2,20 @@ #include "test_config_reader.h" #include "yaml_writer.h" - #include "gtest/gtest.h" +#include "utils.h" #include #include #include +#include + +using LAMMPS_NS::utils::trim; +// test configuration settings read from yaml file TestConfig test_config; +// common read_yaml_file function bool read_yaml_file(const char *infile, TestConfig &config) { auto reader = TestConfigReader(config); @@ -27,22 +32,34 @@ void write_yaml_header(YamlWriter *writer, const TestConfig *config, const char *version) { + // lammps version writer->emit("lammps_version", version); + + // date_generated + std::time_t now = time(nullptr); + std::string block = trim(ctime(&now)); + writer->emit("date_generated", block); writer->emit("epsilon", config->epsilon); + /* + block.clear(); std::string block; - + + // pres_commands for (auto &command : config->pre_commands) block += command + "\n"; writer->emit_block("pre_commands", block); block.clear(); - + + // post_commands for (auto &command : config->post_commands) block += command + "\n"; - +26-2004080931 writer->emit_block("post_commands", block); + */ + writer->emit("input_file", config->input_file); } @@ -52,12 +69,14 @@ int main(int argc, char **argv) ::testing::InitGoogleTest(&argc, argv); + // if no args provided if (argc == 1){ int result = RUN_ALL_TESTS(); MPI_Finalize(); return result; } + // if generate yaml is the 2nd arg, need 3 args if (std::string(argv[1]) == "--generate") { if (argc < 3) { std::cerr << "Missing YAML output filename\n"; @@ -71,6 +90,7 @@ int main(int argc, char **argv) return 0; } + // if yaml file provided but can't read if (!read_yaml_file(argv[1], test_config)) { std::cerr << "Error reading YAML file: " << argv[1] << "\n"; @@ -78,7 +98,8 @@ int main(int argc, char **argv) MPI_Finalize(); return 2; } - + + // if one arg provided, which can be read by yaml int result = RUN_ALL_TESTS(); MPI_Finalize(); diff --git a/unittest/latboltz/tests/fix_lb_multicomponent.yaml b/unittest/latboltz/tests/fix_lb_multicomponent.yaml deleted file mode 100644 index 00559cf0e8f..00000000000 --- a/unittest/latboltz/tests/fix_lb_multicomponent.yaml +++ /dev/null @@ -1,6 +0,0 @@ ---- -lammps_version: 27 Dec 2024 -epsilon: 1e-14 -pre_commands: ! "" -post_commands: ! "" -... diff --git a/unittest/latboltz/tests/test_lb_multicomponent.yaml b/unittest/latboltz/tests/test_lb_multicomponent.yaml new file mode 100644 index 00000000000..3d94a513926 --- /dev/null +++ b/unittest/latboltz/tests/test_lb_multicomponent.yaml @@ -0,0 +1,11 @@ +--- +lammps_version: 27 Dec 2024 +tags: generated +date_generated: Thu Aug 27 14:52:39 2026 +epsilon: 1e-14 +skip_tests: +prerequisites: ! "" +pre_commands: ! "" +post_commands: ! "" +input_file: +... From 2dc491c1cf629ac76e1968f0c97717a785dbfdd2 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Mon, 31 Aug 2026 17:25:48 -0400 Subject: [PATCH 10/19] added a global function to calculate total momentum to use in testing --- src/LATBOLTZ/fix_lb_multicomponent.cpp | 23 +++++++++++++++++++++++ src/LATBOLTZ/fix_lb_multicomponent.h | 1 + 2 files changed, 24 insertions(+) diff --git a/src/LATBOLTZ/fix_lb_multicomponent.cpp b/src/LATBOLTZ/fix_lb_multicomponent.cpp index 1bd825f0c7e..c57862c4a2a 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.cpp +++ b/src/LATBOLTZ/fix_lb_multicomponent.cpp @@ -67,6 +67,29 @@ void FixLbMulticomponent::end_of_step() dump_xdmf(update->ntimestep); } +void FixLbMulticomponent::get_total_momentum(double &jx, double &jy, double &jz) +{ + double local[3] = {0.0, 0.0, 0.0}; + double global[3]; + + for (int x = halo_extent[0]; x < subNbx - halo_extent[0]; ++x) + for (int y = halo_extent[1]; y < subNby - halo_extent[1]; ++y) + for (int z = halo_extent[2]; z < subNbz - halo_extent[2]; ++z) + for(int i = 0; i < numvel; ++i){ + local[0] += f_lb[x][y][z][i] * e19[i][0]; + local[1] += f_lb[x][y][z][i] * e19[i][1]; + local[2] += f_lb[x][y][z][i] * e19[i][2]; + } + + MPI_Reduce(local, global, 3, MPI_DOUBLE, MPI_SUM, 0, world); + + if (me == 0){ + jx = global[0]; + jy = global[1]; + jz = global[2]; + } +} + void FixLbMulticomponent::lb_update() { diff --git a/src/LATBOLTZ/fix_lb_multicomponent.h b/src/LATBOLTZ/fix_lb_multicomponent.h index 4c491e3762d..25a824c5bd6 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.h +++ b/src/LATBOLTZ/fix_lb_multicomponent.h @@ -61,6 +61,7 @@ class FixLbMulticomponent : public FixLbFluid { int setmask() override; void initial_integrate(int) override; void end_of_step() override; + void get_total_momentum(double &, double &, double &); private: double tau_r, tau_p, tau_s; From 2d671f42ba1dbd088d8e9ea738b5c3e79c0ba85c Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Mon, 31 Aug 2026 17:51:26 -0400 Subject: [PATCH 11/19] linked the simple test to run in gtest --- unittest/latboltz/CMakeLists.txt | 3 ++- unittest/latboltz/test_fix_lb_multicomponent.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index 52512d6c2e8..3f1d3b1f108 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -8,7 +8,8 @@ if(NOT BUILD_SHARED_LIBS) target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) endif() -target_include_directories(test_fix_lb_multicomponent PRIVATE ${FORCE_STYLE_TEST_DIR}) +target_include_directories(test_fix_lb_multicomponent PRIVATE ${FORCE_STYLE_TEST_DIR} + ${LAMMPS_SOURCE_DIR}/LATBOLTZ) target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps style_tests) diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index 3c45ab2847b..f1c4fac6884 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -3,6 +3,7 @@ #include "yaml_writer.h" #include "fix.h" +#include "fix_lb_multicomponent.h" #include "input.h" #include "lammps.h" #include "modify.h" @@ -68,8 +69,22 @@ TEST(FixLBMulticomponent, plain) ASSERT_EQ(fixes.size(), 1); ASSERT_NE(fixes[0], nullptr); + auto *fix = dynamic_cast(fixes[0]); + + ASSERT_NE(fix, nullptr); + + double jx0, jy0, jz0; + fix->get_total_momentum(jx0, jy0, jz0); + run_lammps(lmp); + double jx1, jy1, jz1; + fix->get_total_momentum(jx1, jy1, jz1); + + EXPECT_NEAR(jx1, jx0, 1e-3); + EXPECT_NEAR(jy1, jy0, 1e-3); + EXPECT_NEAR(jz1, jz0, 1e-3); + delete lmp; } From e48062fb2b8ca41febd17002037d70296e497355 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Tue, 1 Sep 2026 10:17:43 -0400 Subject: [PATCH 12/19] added some cout options and make test run with 2 mpi ranks --- unittest/latboltz/CMakeLists.txt | 4 +++- .../latboltz/test_fix_lb_multicomponent.cpp | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index 3f1d3b1f108..71eb03dabb4 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -16,7 +16,9 @@ target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps style_tests) file(GLOB LB_FLUID_TESTS LIST_DIRECTORIES false ${TEST_INPUT_FOLDER}/test_lb_multicomponent*.yaml) foreach(TEST ${LB_FLUID_TESTS}) -add_test(NAME FixLBMulticomponent:plain COMMAND test_fix_lb_multicomponent ${TEST}) +add_test(NAME FixLBMulticomponent:plain COMMAND + ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 2 + $ ${TEST}) endforeach() #set_tests_properties(FixLBMulticomponent:plain PROPERTIES # PASS_REGULAR_EXPRESSION "Total wall time") diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index f1c4fac6884..bb1e103b328 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -9,7 +9,7 @@ #include "modify.h" #include - +#include #include using namespace LAMMPS_NS; @@ -45,7 +45,7 @@ LAMMPS *init_lammps() void run_lammps(LAMMPS *lmp) { - lmp->input->one("run 10"); + lmp->input->one("run 100"); } void generate_yaml_file(const char *outfile, const TestConfig &config) @@ -63,6 +63,9 @@ TEST(FixLBMulticomponent, plain) { LAMMPS *lmp = init_lammps(); + + int rank; + MPI_Comm_rank(MPI_COMM_WORLD, &rank); auto fixes = lmp->modify->get_fix_by_style("lb/multicomponent"); @@ -75,16 +78,19 @@ TEST(FixLBMulticomponent, plain) double jx0, jy0, jz0; fix->get_total_momentum(jx0, jy0, jz0); - + if (rank == 0) + std::cout << "Intial P: " << jx0 << " " << jy0 << " " << jz0 << std::endl; run_lammps(lmp); double jx1, jy1, jz1; fix->get_total_momentum(jx1, jy1, jz1); - EXPECT_NEAR(jx1, jx0, 1e-3); - EXPECT_NEAR(jy1, jy0, 1e-3); - EXPECT_NEAR(jz1, jz0, 1e-3); - + if (rank == 0){ + std::cout << "Final P: " << jx1 << " " << jy1 << " " << jz1 << std::endl; + EXPECT_NEAR(jx1, jx0, 1e-6); + EXPECT_NEAR(jy1, jy0, 1e-6); + EXPECT_NEAR(jz1, jz0, 1e-6); + } delete lmp; } From 823143d44e71c3e9debba0fb71824c5fcbd3d97b Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Tue, 1 Sep 2026 15:18:23 -0400 Subject: [PATCH 13/19] added condition to check pkg before compiling and adding test --- unittest/latboltz/CMakeLists.txt | 31 ++--- .../latboltz/test_fix_lb_multicomponent.cpp | 51 +++++++-- unittest/latboltz/test_lb_main.cpp | 108 ------------------ unittest/latboltz/test_lb_main.h | 17 --- 4 files changed, 59 insertions(+), 148 deletions(-) delete mode 100644 unittest/latboltz/test_lb_main.cpp delete mode 100644 unittest/latboltz/test_lb_main.h diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index 71eb03dabb4..854aff1e1df 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -1,24 +1,25 @@ set(FORCE_STYLE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../force-styles) set(TEST_INPUT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/tests) -#test for lattice boltzman multicomponent -add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) +if (PKG_LATBOLTZ) + #test for lattice boltzman multicomponent + add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) -if(NOT BUILD_SHARED_LIBS) - target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) -endif() - -target_include_directories(test_fix_lb_multicomponent PRIVATE ${FORCE_STYLE_TEST_DIR} - ${LAMMPS_SOURCE_DIR}/LATBOLTZ) + if(NOT BUILD_SHARED_LIBS) + target_compile_definitions(test_fix_lb_multicomponent PRIVATE USING_STATIC_LIBS=1) + endif() -target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps style_tests) + target_include_directories(test_fix_lb_multicomponent PRIVATE ${FORCE_STYLE_TEST_DIR} + ${LAMMPS_SOURCE_DIR}/LATBOLTZ) + target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps style_tests) -file(GLOB LB_FLUID_TESTS LIST_DIRECTORIES false ${TEST_INPUT_FOLDER}/test_lb_multicomponent*.yaml) -foreach(TEST ${LB_FLUID_TESTS}) -add_test(NAME FixLBMulticomponent:plain COMMAND - ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 2 - $ ${TEST}) -endforeach() + file(GLOB LB_FLUID_TESTS LIST_DIRECTORIES false ${TEST_INPUT_FOLDER}/test_lb_multicomponent*.yaml) + foreach(TEST ${LB_FLUID_TESTS}) + add_test(NAME FixLBMulticomponent:plain COMMAND + ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 2 + $ ${TEST}) + endforeach() +endif() #set_tests_properties(FixLBMulticomponent:plain PROPERTIES # PASS_REGULAR_EXPRESSION "Total wall time") diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index bb1e103b328..cec9ce2bd46 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -1,20 +1,39 @@ -#include "gtest/gtest.h" +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://www.lammps.org/, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing authors: Abhin Suresh (University of Delaware) +------------------------------------------------------------------------- */ + +// unit tests for fix style multicomponent fluid dynamics + #include "test_main.h" #include "yaml_writer.h" +#include "gtest/gtest.h" + +#include "input.h" #include "fix.h" #include "fix_lb_multicomponent.h" -#include "input.h" #include "lammps.h" #include "modify.h" -#include #include +#include #include using namespace LAMMPS_NS; - LAMMPS *init_lammps() { const char *args[] = {"FixLBMulticomponent", "-log", "none", @@ -26,9 +45,8 @@ LAMMPS *init_lammps() LAMMPS *lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); // utility lambda to improve readability - auto command = [&](const std::string &line) - { - lmp->input->one(line); + auto command = [&](const std::string &line){ + lmp->input->one(line); }; command("region fluid block -16 16 -16 16 -3 3"); @@ -61,30 +79,47 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) TEST(FixLBMulticomponent, plain) { - LAMMPS *lmp = init_lammps(); + // Check for LAMMPS initailization with the fix, else skip the test + if (!lmp) { + std::cerr << "One or more prerequisite styles are not available " + "in this LAMMPS configuration:\n"; + for (auto &prerequisite : test_config.prerequisites) + std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n"; + + GTEST_SKIP(); + } + int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); + // get the base-class Fix * matching the style auto fixes = lmp->modify->get_fix_by_style("lb/multicomponent"); ASSERT_EQ(fixes.size(), 1); ASSERT_NE(fixes[0], nullptr); + // cast to FixLbMulticomponent * is it exists auto *fix = dynamic_cast(fixes[0]); + // verify the cast to FixLbMulticomponent succeeded ASSERT_NE(fix, nullptr); + // obtain initial total momentum double jx0, jy0, jz0; fix->get_total_momentum(jx0, jy0, jz0); if (rank == 0) std::cout << "Intial P: " << jx0 << " " << jy0 << " " << jz0 << std::endl; + + // runs lammps time steps invoking fix_lb_multicomponent run_lammps(lmp); + // obtain final total momentum double jx1, jy1, jz1; fix->get_total_momentum(jx1, jy1, jz1); + // verify conservation total momentum if (rank == 0){ std::cout << "Final P: " << jx1 << " " << jy1 << " " << jz1 << std::endl; EXPECT_NEAR(jx1, jx0, 1e-6); diff --git a/unittest/latboltz/test_lb_main.cpp b/unittest/latboltz/test_lb_main.cpp deleted file mode 100644 index b9e9e0583c4..00000000000 --- a/unittest/latboltz/test_lb_main.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "test_lb_main.h" - -#include "test_config_reader.h" -#include "yaml_writer.h" -#include "gtest/gtest.h" -#include "utils.h" - -#include -#include -#include -#include - -using LAMMPS_NS::utils::trim; - -// test configuration settings read from yaml file -TestConfig test_config; - -// common read_yaml_file function -bool read_yaml_file(const char *infile, TestConfig &config) -{ - auto reader = TestConfigReader(config); - - if (reader.parse_file(infile)) - return false; - - config.basename = reader.get_basename(); - return true; -} - - -void write_yaml_header(YamlWriter *writer, - const TestConfig *config, - const char *version) -{ - // lammps version - writer->emit("lammps_version", version); - - // date_generated - std::time_t now = time(nullptr); - std::string block = trim(ctime(&now)); - writer->emit("date_generated", block); - writer->emit("epsilon", config->epsilon); - - /* - block.clear(); - std::string block; - - // pres_commands - for (auto &command : config->pre_commands) - block += command + "\n"; - - writer->emit_block("pre_commands", block); - - block.clear(); - - // post_commands - for (auto &command : config->post_commands) - block += command + "\n"; -26-2004080931 - writer->emit_block("post_commands", block); - */ - writer->emit("input_file", config->input_file); -} - - -int main(int argc, char **argv) -{ - MPI_Init(&argc, &argv); - - ::testing::InitGoogleTest(&argc, argv); - - // if no args provided - if (argc == 1){ - int result = RUN_ALL_TESTS(); - MPI_Finalize(); - return result; - } - - // if generate yaml is the 2nd arg, need 3 args - if (std::string(argv[1]) == "--generate") { - if (argc < 3) { - std::cerr << "Missing YAML output filename\n"; - MPI_Finalize(); - return 1; - } - - generate_yaml_file(argv[2], test_config); - - MPI_Finalize(); - return 0; - } - - // if yaml file provided but can't read - if (!read_yaml_file(argv[1], test_config)) { - std::cerr << "Error reading YAML file: " - << argv[1] << "\n"; - - MPI_Finalize(); - return 2; - } - - // if one arg provided, which can be read by yaml - int result = RUN_ALL_TESTS(); - - MPI_Finalize(); - - return result; -} diff --git a/unittest/latboltz/test_lb_main.h b/unittest/latboltz/test_lb_main.h deleted file mode 100644 index 4cfc003a1ea..00000000000 --- a/unittest/latboltz/test_lb_main.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef LMP_TEST_LB_MAIN_H -#define LMP_TEST_LB_MAIN_H - -#include "test_config.h" - -class YamlWriter; - -extern TestConfig test_config; -// extern std::string INPUT_FOLDER; - -bool read_yaml_file(const char *, TestConfig &); - -void write_yaml_header(YamlWriter *, const TestConfig *, const char *); - -void generate_yaml_file(const char *, const TestConfig &); - -#endif From a30755536bf7c0a84a537cf40ab703aa63e4c636 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Wed, 2 Sep 2026 16:09:21 -0400 Subject: [PATCH 14/19] limting build to 4 parallel jobs --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9711a858836..1f5cf3415c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: - name: Build run: | - cmake --build build -j + cmake --build build -j4 - name: Run tests run: ctest --test-dir build -R 'FixLBMulticomponent:plain' --output-on-failure From af725d6d4415eff90f7381a46e16eb5c3f060bbc Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Thu, 3 Sep 2026 11:58:08 -0400 Subject: [PATCH 15/19] added conservation of fictitious mass --- src/LATBOLTZ/fix_lb_multicomponent.cpp | 20 ++++++++++++++- src/LATBOLTZ/fix_lb_multicomponent.h | 1 + unittest/latboltz/CMakeLists.txt | 2 +- .../latboltz/test_fix_lb_multicomponent.cpp | 25 +++++++++++++------ 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/LATBOLTZ/fix_lb_multicomponent.cpp b/src/LATBOLTZ/fix_lb_multicomponent.cpp index c57862c4a2a..240db682257 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.cpp +++ b/src/LATBOLTZ/fix_lb_multicomponent.cpp @@ -70,7 +70,7 @@ void FixLbMulticomponent::end_of_step() void FixLbMulticomponent::get_total_momentum(double &jx, double &jy, double &jz) { double local[3] = {0.0, 0.0, 0.0}; - double global[3]; + double global[3] = {0.0, 0.0, 0.0}; for (int x = halo_extent[0]; x < subNbx - halo_extent[0]; ++x) for (int y = halo_extent[1]; y < subNby - halo_extent[1]; ++y) @@ -90,6 +90,24 @@ void FixLbMulticomponent::get_total_momentum(double &jx, double &jy, double &jz) } } +void FixLbMulticomponent::get_total_mass(double &m) +{ + double local = 0.0; + double global = 0.0; + + for (int x = halo_extent[0]; x < subNbx - halo_extent[0]; ++x) + for (int y = halo_extent[1]; y < subNby - halo_extent[1]; ++y) + for (int z = halo_extent[2]; z < subNbz - halo_extent[2]; ++z) + for(int i = 0; i < numvel; ++i){ + local += f_lb[x][y][z][i]; + } + + MPI_Reduce(&local, &global, 1, MPI_DOUBLE, MPI_SUM, 0, world); + + if (me == 0) + m = global; +} + void FixLbMulticomponent::lb_update() { diff --git a/src/LATBOLTZ/fix_lb_multicomponent.h b/src/LATBOLTZ/fix_lb_multicomponent.h index 413f3e345d3..fbee6078f9e 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.h +++ b/src/LATBOLTZ/fix_lb_multicomponent.h @@ -64,6 +64,7 @@ class FixLbMulticomponent : public FixLbFluid { void initial_integrate(int) override; void end_of_step() override; void get_total_momentum(double &, double &, double &); + void get_total_mass(double &); private: double tau_r, tau_p, tau_s; diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index 854aff1e1df..ff0a804cfd6 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -12,7 +12,7 @@ if (PKG_LATBOLTZ) target_include_directories(test_fix_lb_multicomponent PRIVATE ${FORCE_STYLE_TEST_DIR} ${LAMMPS_SOURCE_DIR}/LATBOLTZ) - target_link_libraries(test_fix_lb_multicomponent PRIVATE lammps style_tests) + target_link_libraries(test_fix_lb_multicomponent PRIVATE style_tests) file(GLOB LB_FLUID_TESTS LIST_DIRECTORIES false ${TEST_INPUT_FOLDER}/test_lb_multicomponent*.yaml) foreach(TEST ${LB_FLUID_TESTS}) diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index cec9ce2bd46..958c6d4a01f 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -107,24 +107,35 @@ TEST(FixLBMulticomponent, plain) ASSERT_NE(fix, nullptr); // obtain initial total momentum - double jx0, jy0, jz0; + double jx0, jy0, jz0, m0; + jx0 = jy0 = jz0 = m0 = 0.0; fix->get_total_momentum(jx0, jy0, jz0); - if (rank == 0) - std::cout << "Intial P: " << jx0 << " " << jy0 << " " << jz0 << std::endl; + fix->get_total_mass(m0); + if (rank == 0){ + std::cout << "Initial P: " << jx0 << " " << jy0 << " " << jz0 << std::endl; + std::cout << "Initial M: " << m0 << std::endl; + EXPECT_NEAR(jx0, 0, 1e-6); + EXPECT_NEAR(jy0, 0, 1e-6); + EXPECT_NEAR(jz0, 0, 1e-6); + } // runs lammps time steps invoking fix_lb_multicomponent run_lammps(lmp); // obtain final total momentum - double jx1, jy1, jz1; + double jx1, jy1, jz1, m1; + jx1 = jy1 = jz1 = m1 = 0.0; fix->get_total_momentum(jx1, jy1, jz1); + fix->get_total_mass(m1); // verify conservation total momentum if (rank == 0){ std::cout << "Final P: " << jx1 << " " << jy1 << " " << jz1 << std::endl; - EXPECT_NEAR(jx1, jx0, 1e-6); - EXPECT_NEAR(jy1, jy0, 1e-6); - EXPECT_NEAR(jz1, jz0, 1e-6); + std::cout << "Final M: " << m1 << std::endl; + EXPECT_NEAR(jx1, 0, 1e-6); + EXPECT_NEAR(jy1, 0, 1e-6); + EXPECT_NEAR(jz1, 0, 1e-6); + EXPECT_NEAR(m0, m1, 1e-6); } delete lmp; } From b44c96daa789b86c836c08adb465a203bc19a521 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Mon, 7 Sep 2026 12:18:21 -0400 Subject: [PATCH 16/19] adding small change in init droplet --- src/LATBOLTZ/fix_lb_multicomponent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LATBOLTZ/fix_lb_multicomponent.cpp b/src/LATBOLTZ/fix_lb_multicomponent.cpp index 240db682257..284a9f1a9d4 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.cpp +++ b/src/LATBOLTZ/fix_lb_multicomponent.cpp @@ -547,7 +547,7 @@ void FixLbMulticomponent::init_droplet(double radius) for (z = 0; z < subNbz; z++) { pos[2] = domain->sublo[2] + (z - halo_extent[2]) * dx_lb; r2 = pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]; - phi = r2 < radius * radius ? 1.0 : -1.0; + phi = r2 < radius * radius ? 1.0 - 0.000125 : -1.0 + 0.000125; for (i = 0; i < numvel; i++) { f_lb[x][y][z][i] = w_lb19[i] * rho * densityinit; g_lb[x][y][z][i] = w_lb19[i] * phi * densityinit; From d72fd283616fd3eae622be482e1b54435a5f29a6 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Wed, 23 Sep 2026 11:18:03 -0400 Subject: [PATCH 17/19] adding unit test on moments and calc_gradients --- src/LATBOLTZ/fix_lb_multicomponent.cpp | 2 +- src/LATBOLTZ/fix_lb_multicomponent.h | 2 + unittest/latboltz/CMakeLists.txt | 13 +- unittest/latboltz/README.md | 280 ++++++++++++++++++ .../latboltz/test_fix_lb_multicomponent.cpp | 101 ++++++- 5 files changed, 392 insertions(+), 6 deletions(-) create mode 100644 unittest/latboltz/README.md diff --git a/src/LATBOLTZ/fix_lb_multicomponent.cpp b/src/LATBOLTZ/fix_lb_multicomponent.cpp index 284a9f1a9d4..240db682257 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.cpp +++ b/src/LATBOLTZ/fix_lb_multicomponent.cpp @@ -547,7 +547,7 @@ void FixLbMulticomponent::init_droplet(double radius) for (z = 0; z < subNbz; z++) { pos[2] = domain->sublo[2] + (z - halo_extent[2]) * dx_lb; r2 = pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]; - phi = r2 < radius * radius ? 1.0 - 0.000125 : -1.0 + 0.000125; + phi = r2 < radius * radius ? 1.0 : -1.0; for (i = 0; i < numvel; i++) { f_lb[x][y][z][i] = w_lb19[i] * rho * densityinit; g_lb[x][y][z][i] = w_lb19[i] * phi * densityinit; diff --git a/src/LATBOLTZ/fix_lb_multicomponent.h b/src/LATBOLTZ/fix_lb_multicomponent.h index fbee6078f9e..809622c2df2 100644 --- a/src/LATBOLTZ/fix_lb_multicomponent.h +++ b/src/LATBOLTZ/fix_lb_multicomponent.h @@ -67,6 +67,8 @@ class FixLbMulticomponent : public FixLbFluid { void get_total_mass(double &); private: + friend class FixLbMulticomponentTestAccess; + double tau_r, tau_p, tau_s; double gamma_p, gamma_s; double kappa1, kappa2, kappa3; diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index ff0a804cfd6..a21468f09ef 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -1,7 +1,7 @@ set(FORCE_STYLE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../force-styles) set(TEST_INPUT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/tests) -if (PKG_LATBOLTZ) +if (PKG_LATBOLTZ AND BUILD_MPI) #test for lattice boltzman multicomponent add_executable(test_fix_lb_multicomponent test_fix_lb_multicomponent.cpp) @@ -16,9 +16,14 @@ if (PKG_LATBOLTZ) file(GLOB LB_FLUID_TESTS LIST_DIRECTORIES false ${TEST_INPUT_FOLDER}/test_lb_multicomponent*.yaml) foreach(TEST ${LB_FLUID_TESTS}) - add_test(NAME FixLBMulticomponent:plain COMMAND - ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 2 - $ ${TEST}) + add_mpi_test(NAME FixLBMulticomponent:plain NUM_PROCS 2 COMMAND + $ ${TEST} + --gtest_filter=FixLBMulticomponent.plain) + add_mpi_test(NAME FixLBMulticomponent:moments NUM_PROCS 1 COMMAND + $ ${TEST} + --gtest_filter=LBMomentReconstruction.*) + set_tests_properties(FixLBMulticomponent:plain PROPERTIES PROCESSORS 2 TIMEOUT 120) + set_tests_properties(FixLBMulticomponent:moments PROPERTIES PROCESSORS 1 TIMEOUT 30) endforeach() endif() #set_tests_properties(FixLBMulticomponent:plain PROPERTIES diff --git a/unittest/latboltz/README.md b/unittest/latboltz/README.md new file mode 100644 index 00000000000..d0c0cbdcb47 --- /dev/null +++ b/unittest/latboltz/README.md @@ -0,0 +1,280 @@ +# Multicomponent lattice-Boltzmann unit tests + +This document describes the implemented moment-reconstruction test and the +proposed first-priority test of the gradient and Laplacian stencil. + +| Unit | Status | Function under test | +| --- | --- | --- | +| Local moment reconstruction | Implemented | `FixLbMulticomponent::calc_moments()` | +| Local spatial derivatives | Proposed; not implemented | `FixLbMulticomponent::calc_gradient_laplacian()` | + +Source: [multicomponent implementation](../../src/LATBOLTZ/fix_lb_multicomponent.cpp), +[test source](test_fix_lb_multicomponent.cpp), and [CTest registration](CMakeLists.txt). + +## 1. Implemented: moment reconstruction + +### Physical meaning and equations + +Each D3Q19 site stores three sets of 19 populations: `f_lb`, `g_lb`, and `k_lb`. +D3Q19 has one rest direction, six axial directions, and twelve face-diagonal +directions. Let $\mathbf{e}_i$ denote direction $i$, for $i=0,\ldots,18$. + +The local density and composition fields are the zeroth moments: + +$$ +\rho = \sum_{i=0}^{18} f_i, \qquad +\phi = \sum_{i=0}^{18} g_i, \qquad +\psi = \sum_{i=0}^{18} k_i. +$$ + +The momentum density is the first moment of the fluid populations. Velocity +is momentum density divided by mass density: + +$$ +\mathbf{j} = \sum_{i=0}^{18} f_i\mathbf{e}_i, \qquad +\mathbf{u} = \frac{\mathbf{j}}{\rho}. +$$ + +The corresponding component densities are + +$$ +\rho_1 = \frac{\rho+\phi-\psi}{2}, \qquad +\rho_2 = \frac{\rho-\phi-\psi}{2}, \qquad +\rho_3 = \psi. +$$ + +These are component densities; divide by total density to obtain component +fractions. The current test asserts $\rho$, $\phi$, $\psi$, and all three +velocity components directly. + +### Controlled input and expected result + +All populations initially equal zero. The test then assigns: + +| Direction | $f_i$ | $g_i$ | $k_i$ | +| --- | ---: | ---: | ---: | +| Rest | 2.0 | 0.3 | 0.4 | +| $+x$ | 0.4 | 0 | 0 | +| $-x$ | 0.1 | 0 | 0 | +| $+y$ | 0.2 | 0 | 0 | +| $-y$ | 0.5 | 0 | 0 | +| $+z$ | 0.6 | 0 | 0 | +| $-z$ | 0.2 | 0 | 0 | +| All diagonals | 0 | 0 | 0 | + +Thus the independent, hand-calculated answers are + +$$ +\rho = 2+0.4+0.1+0.2+0.5+0.6+0.2 = 4, +\qquad \phi=0.3, \qquad \psi=0.4, +$$ + +$$ +\mathbf{j} = (0.4-0.1,\;0.2-0.5,\;0.6-0.2) + = (0.3,-0.3,0.4), +$$ + +$$ +\mathbf{u} = (0.075,-0.075,0.100). +$$ + +Unequal opposite populations check momentum signs. Density differs from one +to expose missing velocity normalization. Distinct values of $\phi$ and $\psi$ +help expose swapped composition fields. These prescribed populations are +numerical test inputs and need not be an equilibrium distribution. + +### Implementation walkthrough + +1. **Access declaration:** `FixLbMulticomponent` declares + `friend class FixLbMulticomponentTestAccess`. No production method is made + public. The helper is defined in the test source in `LAMMPS_NS`. +2. **Fixture setup:** `LBMomentReconstruction::SetUp()` constructs LAMMPS, + creates a periodic, atom-free $8\times8\times8$ box with lattice spacing + and timestep equal to one, creates the fix, and validates its pointer. +3. **Choose a site:** `owned_site()` returns the midpoint of the local + allocated lattice. For this fixture it is inside the owned region, + away from halo cells. +4. **Supply inputs:** `set_populations()` overwrites all 19 entries of each + population family at that site. This removes dependence on the random + mixture initialization at the tested site. +5. **Execute production code:** `reconstruct()` calls `calc_moments()` and + returns copies of `density_lb`, `phi_lb`, `psi_lb`, and `u_lb`. It does + not recalculate the expected answer. +6. **Compare:** `ReconstructsDensityCompositionAndVelocity` uses six + `EXPECT_NEAR` checks with absolute tolerance $10^{-13}$. +7. **Clean up:** `TearDown()` resets the owning `std::unique_ptr`. + LAMMPS destroys its fix and associated resources. + +No timesteps or halo exchanges are required: `calc_moments()` reads only +populations at the selected site. The shared test main initializes MPI; +the test runs with one MPI rank. + +### Coverage and limitations + +This test checks local population sums, axial momentum signs, field assignment, +and division by density. It does not check nonzero diagonal contributions, +distributed `g`/`k` populations, pressure correctness, collision, streaming, +global conservation, or MPI communication. Although `calc_moments()` also +updates pressure, no pressure assertion is currently included. + +During initial validation, removing division by density from the x-velocity +calculation in a temporary source copy caused this test to fail: the actual +value became 0.3 instead of 0.075. The temporary change was restored, and the +reconstruction and existing conservation tests passed. This documents that +validation run, not a guarantee about subsequent source edits. + +## 2. Proposed: gradient and Laplacian + +### Physical relevance + +`calc_equilibrium()` applies `calc_gradient_laplacian()` to $\rho$, $\phi$, +and $\psi$. Their derivatives enter chemical potentials and interfacial +stress terms. A derivative error can change diffusion and interface dynamics +even when total mass remains conserved. + +### Discrete equations used by the code + +For a scalar field $q$ at lattice site $\mathbf{n}$, the implementation uses + +$$ +(\nabla_{\mathrm{LB}}q)_\alpha(\mathbf{n}) += 3\sum_{i=0}^{18} w_i\,q(\mathbf{n}+\mathbf{e}_i)e_{i\alpha}, +\qquad \alpha\in\{x,y,z\}, +$$ + +$$ +\nabla_{\mathrm{LB}}^2q(\mathbf{n}) += 6\sum_{i=0}^{18}w_i +\left[q(\mathbf{n}+\mathbf{e}_i)-q(\mathbf{n})\right]. +$$ + +The D3Q19 weights are + +$$ +w_i = +\begin{cases} +1/3 & \text{rest direction},\\ +1/18 & \text{six axial directions},\\ +1/36 & \text{twelve face-diagonal directions}. +\end{cases} +$$ + +These are derivatives in lattice coordinates: the function contains no +explicit factors of `dx_lb`. For unchanged field units and physical spacing +$h$, physical derivatives are approximated by + +$$ +\nabla_{\mathrm{phys}}q \approx \frac{1}{h}\nabla_{\mathrm{LB}}q, +\qquad +\nabla_{\mathrm{phys}}^2q \approx \frac{1}{h^2}\nabla_{\mathrm{LB}}^2q. +$$ + +The proposed unit tests use $h=1$. Conversion of field units is a separate +concern. + +### Why polynomial fields provide independent answers + +The stencil satisfies + +$$ +\sum_i w_i\mathbf{e}_i=0, \qquad +\sum_i w_i e_{i\alpha}e_{i\beta}=\frac{1}{3}\delta_{\alpha\beta}, +$$ + +where $\delta_{\alpha\beta}$ is one for equal indices and zero otherwise. +Opposite directions also cancel odd-order terms. Expanding a quadratic field +about the tested site therefore gives its exact analytic gradient and +Laplacian, up to floating-point rounding. General smooth fields have +discretization error and should not be required to match continuum derivatives +to roundoff. + +### Proposed test cases + +Use local coordinates $(X,Y,Z)$ relative to the chosen site, so the tested +site is $(0,0,0)$. Evaluate each polynomial over its complete stencil neighborhood. + +| Field $q(X,Y,Z)$ | Expected gradient at the site | Expected Laplacian | Purpose | +| --- | --- | ---: | --- | +| $5$ | $(0,0,0)$ | 0 | Constant cancellation | +| $X$, $Y$, $Z$, separately | Corresponding unit vector | 0 | Each derivative direction | +| $5+2X-3Y+4Z$ | $(2,-3,4)$ | 0 | Signs and axis assignment | +| $X^2$, $Y^2$, $Z^2$, separately | $(0,0,0)$ | 2 | Curvature along each axis | +| $X^2+2Y^2+3Z^2$ | $(0,0,0)$ | 12 | Unequal curvature contributions | +| $(X+1)(Y+2)$ | $(2,1,0)$ | 0 | Mixed term in the xy plane | +| $(Y+1)(Z+2)$ | $(0,2,1)$ | 0 | Mixed term in the yz plane | +| $(Z+1)(X+2)$ | $(1,0,2)$ | 0 | Mixed term in the zx plane | + +For example, + +$$ +q=5+2X-3Y+4Z+X^2+2Y^2+3Z^2 +$$ + +has, at the tested site, + +$$ +\nabla q=(2,-3,4), \qquad \nabla^2q=2+4+6=12. +$$ + +This combined case is useful in addition to the isolated cases, which make +failures easier to diagnose. + +### Implementation plan + +1. Reuse the small one-rank fixture and friend-access mechanism. Keep + `calc_gradient_laplacian()` private. +2. Extend the test helper to fill a selected scalar field in the complete + $3\times3\times3$ neighborhood around an owned interior site. D3Q19 reads + only the center, axial neighbors, and face diagonals; initializing the + whole neighborhood keeps setup simple. +3. Add a helper that calls the production derivative function and returns + copies of its three gradient components and one Laplacian value. +4. Parameterize the analytic cases above. Expected values must come from + polynomial derivatives, not a second implementation of the stencil sum. +5. Start with absolute tolerance $10^{-13}$ for these small local values. + Reassess roundoff bounds if field magnitudes are increased. +6. Call the function twice at the same site with different fields, ending + with a constant field. Confirm outputs are reset rather than accumulated. +7. Exercise the `rho`, `phi`, and `psi` field/output pairs. Checking the + automatic wiring through `calc_equilibrium()` can be a separate integration + test; direct stencil tests alone do not validate that wiring. +8. Register a separate one-rank CTest entry, proposed name + `FixLBMulticomponent:derivatives`, with a matching GoogleTest filter. + +The local polynomials need not be periodic over the whole box because this +test neither exchanges halos nor advances the fluid. It requires a valid +neighborhood around the selected site. Boundary and MPI halo behavior require +separate tests. + +### Acceptance criteria + +- Every analytic case passes for all three gradient components and the Laplacian. +- Repeated calls overwrite previous results. +- A deliberate sign or normalization error makes an appropriate case fail. +- Existing reconstruction and conservation tests still pass. +- No production numerical behavior is changed merely to accommodate the tests. + +## 3. Build and run the implemented tests + +From a clean CMake-compatible source tree with an MPI toolchain: + +```sh +cmake -S cmake -B build-latboltz -D BUILD_MPI=ON -D PKG_LATBOLTZ=ON -D ENABLE_TESTING=ON +cmake --build build-latboltz --target test_fix_lb_multicomponent -j 4 +ctest --test-dir build-latboltz -R '^FixLBMulticomponent:moments$' --output-on-failure +``` + +Run both currently registered LATBOLTZ tests with: + +```sh +ctest --test-dir build-latboltz -R '^FixLBMulticomponent:' --output-on-failure +``` + +`FixLBMulticomponent:moments` uses one MPI rank; the existing +`FixLBMulticomponent:plain` conservation test uses two. GoogleTest filters +select the intended test from their shared executable. The executable still +receives the existing YAML file because the shared force-style test main +expects it; the moment test uses its C++ tolerance, not the YAML epsilon. + +The derivative test name above is proposed and will not run until implemented +and registered in CMake. diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index 958c6d4a01f..2e668bdf7f8 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -27,13 +27,113 @@ #include "fix_lb_multicomponent.h" #include "lammps.h" #include "modify.h" +#include "latboltz_const.h" +#include #include +#include #include #include using namespace LAMMPS_NS; +namespace LAMMPS_NS { + +// Keep access to lattice internals local to this test executable. +class FixLbMulticomponentTestAccess { +public: + using Populations = std::array; + struct Site { int x, y, z; }; + struct Moments { + double rho, phi, psi; + std::array velocity; + }; + + static Site owned_site(const FixLbMulticomponent &fix) + { + return {fix.subNbx / 2, fix.subNby / 2, fix.subNbz / 2}; + } + + static void set_populations(FixLbMulticomponent &fix, Site site, + const Populations &f, const Populations &g, + const Populations &k) + { + for (int i = 0; i < 19; ++i) { + fix.f_lb[site.x][site.y][site.z][i] = f[i]; + fix.g_lb[site.x][site.y][site.z][i] = g[i]; + fix.k_lb[site.x][site.y][site.z][i] = k[i]; + } + } + + static Moments reconstruct(FixLbMulticomponent &fix, Site site) + { + fix.calc_moments(site.x, site.y, site.z); + const auto *u = fix.u_lb[site.x][site.y][site.z]; + return {fix.density_lb[site.x][site.y][site.z], + fix.phi_lb[site.x][site.y][site.z], + fix.psi_lb[site.x][site.y][site.z], {u[0], u[1], u[2]}}; + } +}; + +} // namespace LAMMPS_NS + +class LBMomentReconstruction : public ::testing::Test { +protected: + std::unique_ptr lmp; + FixLbMulticomponent *fix = nullptr; + + void SetUp() override + { + const char *args[] = {"LBMomentReconstruction", "-log", "none", "-screen", "none", + "-nocite"}; + lmp.reset(new LAMMPS(sizeof(args) / sizeof(args[0]), + const_cast(args), MPI_COMM_WORLD)); + lmp->input->one("boundary p p p"); + lmp->input->one("region fluid block 0 8 0 8 0 8"); + lmp->input->one("create_box 0 fluid"); + lmp->input->one("timestep 1.0"); + lmp->input->one("fix mcmp all lb/multicomponent 1 0.166667 1.0 D3Q19 dx 1 init mixture"); + auto fixes = lmp->modify->get_fix_by_style("lb/multicomponent"); + ASSERT_EQ(fixes.size(), 1); + fix = dynamic_cast(fixes[0]); + ASSERT_NE(fix, nullptr); + } + + void TearDown() override { lmp.reset(); } +}; + +TEST_F(LBMomentReconstruction, ReconstructsDensityCompositionAndVelocity) +{ + using Access = FixLbMulticomponentTestAccess; + Access::Populations f{}, g{}, k{}; + // Resolve direction indices from D3Q19, while keeping expected moments explicit. + for (int i = 0; i < 19; ++i) { + const int x = e19[i][0], y = e19[i][1], z = e19[i][2]; + if (x == 0 && y == 0 && z == 0) { + f[i] = 2.0; + g[i] = 0.3; + k[i] = 0.4; + } else if (y == 0 && z == 0) { + f[i] = x == 1 ? 0.4 : 0.1; + } else if (x == 0 && z == 0) { + f[i] = y == 1 ? 0.2 : 0.5; + } else if (x == 0 && y == 0) { + f[i] = z == 1 ? 0.6 : 0.2; + } + } + const auto site = Access::owned_site(*fix); + Access::set_populations(*fix, site, f, g, k); + const auto actual = Access::reconstruct(*fix, site); + + constexpr double tolerance = 1e-13; + EXPECT_NEAR(actual.rho, 4.0, tolerance); + EXPECT_NEAR(actual.phi, 0.3, tolerance); + EXPECT_NEAR(actual.psi, 0.4, tolerance); + EXPECT_NEAR(actual.velocity[0], 0.075, tolerance); + EXPECT_NEAR(actual.velocity[1], -0.075, tolerance); + EXPECT_NEAR(actual.velocity[2], 0.100, tolerance); +} + LAMMPS *init_lammps() { const char *args[] = {"FixLBMulticomponent", "-log", "none", @@ -152,4 +252,3 @@ int main(int argc, char **argv) return result; } */ - From 541698cacd0fecbee2aa8bc57d33dc66ab93a632 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Tue, 6 Oct 2026 17:03:57 -0400 Subject: [PATCH 18/19] giving more structure to the test --- .../latboltz/test_fix_lb_multicomponent.cpp | 90 +++++++++---------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index 2e668bdf7f8..6028ebd0a3c 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -44,16 +44,15 @@ class FixLbMulticomponentTestAccess { public: using Populations = std::array; struct Site { int x, y, z; }; - struct Moments { - double rho, phi, psi; - std::array velocity; - }; + struct Moments { double rho, phi, psi; + std::array velocity; }; - static Site owned_site(const FixLbMulticomponent &fix) - { - return {fix.subNbx / 2, fix.subNby / 2, fix.subNbz / 2}; + // return a interior site of subdomain, avoiding halo sites + static Site interior_site(const FixLbMulticomponent &fix){ + return {fix.halo_extent[0], fix.halo_extent[1], fix.halo_extent[2]}; } + // Populate fix object f_lb, g_lb, k_lb static void set_populations(FixLbMulticomponent &fix, Site site, const Populations &f, const Populations &g, const Populations &k) @@ -65,25 +64,26 @@ class FixLbMulticomponentTestAccess { } } - static Moments reconstruct(FixLbMulticomponent &fix, Site site) + // Call the unit calc_moments of the fix object + static Moments calculate_moments(FixLbMulticomponent &fix, Site site) { fix.calc_moments(site.x, site.y, site.z); const auto *u = fix.u_lb[site.x][site.y][site.z]; return {fix.density_lb[site.x][site.y][site.z], fix.phi_lb[site.x][site.y][site.z], - fix.psi_lb[site.x][site.y][site.z], {u[0], u[1], u[2]}}; + fix.psi_lb[site.x][site.y][site.z], + {u[0], u[1], u[2]}}; } }; } // namespace LAMMPS_NS -class LBMomentReconstruction : public ::testing::Test { +class FixLbMulticomponentTest : public ::testing::Test { protected: std::unique_ptr lmp; FixLbMulticomponent *fix = nullptr; - void SetUp() override - { + void SetUp() override{ const char *args[] = {"LBMomentReconstruction", "-log", "none", "-screen", "none", "-nocite"}; lmp.reset(new LAMMPS(sizeof(args) / sizeof(args[0]), @@ -99,39 +99,48 @@ class LBMomentReconstruction : public ::testing::Test { ASSERT_NE(fix, nullptr); } - void TearDown() override { lmp.reset(); } + void TearDown() override { + lmp.reset(); + } }; -TEST_F(LBMomentReconstruction, ReconstructsDensityCompositionAndVelocity) +// TEST_F(FixtureClassName, TestName) +TEST_F(FixLbMulticomponentTest, ReconstructsDensityCompositionAndVelocity) { using Access = FixLbMulticomponentTestAccess; Access::Populations f{}, g{}, k{}; // Resolve direction indices from D3Q19, while keeping expected moments explicit. - for (int i = 0; i < 19; ++i) { - const int x = e19[i][0], y = e19[i][1], z = e19[i][2]; - if (x == 0 && y == 0 && z == 0) { + for (int i = 0; i < 19; ++i) + { + const int ex = e19[i][0], ey = e19[i][1], ez = e19[i][2]; + if (ex == 0 && ey == 0 && ez == 0) + { f[i] = 2.0; g[i] = 0.3; k[i] = 0.4; - } else if (y == 0 && z == 0) { - f[i] = x == 1 ? 0.4 : 0.1; - } else if (x == 0 && z == 0) { - f[i] = y == 1 ? 0.2 : 0.5; - } else if (x == 0 && y == 0) { - f[i] = z == 1 ? 0.6 : 0.2; - } - } - const auto site = Access::owned_site(*fix); + } + else if (ey == 0 && ez == 0) + f[i] = ex == 1 ? 0.4 : 0.1; + else if (ex == 0 && ez == 0) + f[i] = ey == 1 ? 0.2 : 0.5; + else if (ex == 0 && ey == 0) + f[i] = ez == 1 ? 0.6 : 0.2; + } + // Get interior lattice site of the subdomain + const auto site = Access::interior_site(*fix); + // Set fix object f_lb, g_lb, k_lb values Access::set_populations(*fix, site, f, g, k); - const auto actual = Access::reconstruct(*fix, site); + // Call the unit fix.calc_moments and returns density, phi, psi, velocity + const auto calculated = Access::calculate_moments(*fix, site); constexpr double tolerance = 1e-13; - EXPECT_NEAR(actual.rho, 4.0, tolerance); - EXPECT_NEAR(actual.phi, 0.3, tolerance); - EXPECT_NEAR(actual.psi, 0.4, tolerance); - EXPECT_NEAR(actual.velocity[0], 0.075, tolerance); - EXPECT_NEAR(actual.velocity[1], -0.075, tolerance); - EXPECT_NEAR(actual.velocity[2], 0.100, tolerance); + // check calcualated values against reference + EXPECT_NEAR(calculated.rho, 4.0, tolerance); + EXPECT_NEAR(calculated.phi, 0.3, tolerance); + EXPECT_NEAR(calculated.psi, 0.4, tolerance); + EXPECT_NEAR(calculated.velocity[0], 0.075, tolerance); + EXPECT_NEAR(calculated.velocity[1], -0.075, tolerance); + EXPECT_NEAR(calculated.velocity[2], 0.100, tolerance); } LAMMPS *init_lammps() @@ -176,7 +185,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) delete lmp; } - +// TEST(TestSuitName, TestName) TEST(FixLBMulticomponent, plain) { LAMMPS *lmp = init_lammps(); @@ -239,16 +248,3 @@ TEST(FixLBMulticomponent, plain) } delete lmp; } - -/* -int main(int argc, char **argv) -{ - MPI_Init(&argc, &argv); - - ::testing::InitGoogleTest(&argc, argv); - int result = RUN_ALL_TESTS(); - - MPI_Finalize(); - return result; -} -*/ From 26dd6a9aa039bbb9224318603d34e835f3f8b795 Mon Sep 17 00:00:00 2001 From: Abhin Suresh Date: Thu, 8 Oct 2026 17:06:56 -0400 Subject: [PATCH 19/19] addding gradient testing using TEST_P --- unittest/latboltz/CMakeLists.txt | 6 +- unittest/latboltz/README.md | 295 ++++++++++++------ .../latboltz/test_fix_lb_multicomponent.cpp | 260 +++++++++++++-- 3 files changed, 438 insertions(+), 123 deletions(-) diff --git a/unittest/latboltz/CMakeLists.txt b/unittest/latboltz/CMakeLists.txt index a21468f09ef..0eb1b399332 100644 --- a/unittest/latboltz/CMakeLists.txt +++ b/unittest/latboltz/CMakeLists.txt @@ -21,9 +21,13 @@ if (PKG_LATBOLTZ AND BUILD_MPI) --gtest_filter=FixLBMulticomponent.plain) add_mpi_test(NAME FixLBMulticomponent:moments NUM_PROCS 1 COMMAND $ ${TEST} - --gtest_filter=LBMomentReconstruction.*) + --gtest_filter=FixLbMulticomponentTest.*) + add_mpi_test(NAME FixLBMulticomponent:derivatives NUM_PROCS 1 COMMAND + $ ${TEST} + --gtest_filter=FixLbDerivativeTest.*:*/FixLbDerivativePolynomialTest.*) set_tests_properties(FixLBMulticomponent:plain PROPERTIES PROCESSORS 2 TIMEOUT 120) set_tests_properties(FixLBMulticomponent:moments PROPERTIES PROCESSORS 1 TIMEOUT 30) + set_tests_properties(FixLBMulticomponent:derivatives PROPERTIES PROCESSORS 1 TIMEOUT 30) endforeach() endif() #set_tests_properties(FixLBMulticomponent:plain PROPERTIES diff --git a/unittest/latboltz/README.md b/unittest/latboltz/README.md index d0c0cbdcb47..fdbecec2bf4 100644 --- a/unittest/latboltz/README.md +++ b/unittest/latboltz/README.md @@ -1,12 +1,12 @@ # Multicomponent lattice-Boltzmann unit tests -This document describes the implemented moment-reconstruction test and the -proposed first-priority test of the gradient and Laplacian stencil. +This document describes the implemented moment-reconstruction and +gradient/Laplacian stencil tests. | Unit | Status | Function under test | | --- | --- | --- | | Local moment reconstruction | Implemented | `FixLbMulticomponent::calc_moments()` | -| Local spatial derivatives | Proposed; not implemented | `FixLbMulticomponent::calc_gradient_laplacian()` | +| Local spatial derivatives | Implemented | `FixLbMulticomponent::calc_gradient_laplacian()` | Source: [multicomponent implementation](../../src/LATBOLTZ/fix_lb_multicomponent.cpp), [test source](test_fix_lb_multicomponent.cpp), and [CTest registration](CMakeLists.txt). @@ -44,63 +44,118 @@ $$ $$ These are component densities; divide by total density to obtain component -fractions. The current test asserts $\rho$, $\phi$, $\psi$, and all three -velocity components directly. +fractions. `calc_moments()` also stores the bulk pressure +$p_0(\rho,\phi,\psi)$ (Eq. (43) of Semprebon et al.). With +$\kappa_1=\kappa_2=\kappa_3=0$ every free-energy term vanishes and + +$$ +p_0 = c_s^2\rho = \frac{\rho}{3}. +$$ + +The test asserts $\rho$, $\phi$, $\psi$, all three velocity components, and +$p_0$ directly. ### Controlled input and expected result -All populations initially equal zero. The test then assigns: +The fixture creates the fix with `kappa1 0 kappa2 0 kappa3 0`, so the pressure +reference is independent of the free-energy expression. At the tested site the +test assigns every population from a table keyed by lattice vector, so the +inputs do not depend on the index order of `e19`. The test asserts that each of +the 19 D3Q19 directions is assigned exactly once. -| Direction | $f_i$ | $g_i$ | $k_i$ | +| $\mathbf{e}_i$ | $f_i$ | $g_i$ | $k_i$ | | --- | ---: | ---: | ---: | -| Rest | 2.0 | 0.3 | 0.4 | -| $+x$ | 0.4 | 0 | 0 | -| $-x$ | 0.1 | 0 | 0 | -| $+y$ | 0.2 | 0 | 0 | -| $-y$ | 0.5 | 0 | 0 | -| $+z$ | 0.6 | 0 | 0 | -| $-z$ | 0.2 | 0 | 0 | -| All diagonals | 0 | 0 | 0 | +| $(0,0,0)$ | 1.30 | 0.10 | 0.20 | +| $(+1,0,0)$ | 0.40 | 0.05 | 0 | +| $(-1,0,0)$ | 0.10 | 0 | 0.03 | +| $(0,+1,0)$ | 0.20 | 0 | 0 | +| $(0,-1,0)$ | 0.50 | $-0.04$ | 0 | +| $(0,0,+1)$ | 0.60 | 0 | 0.05 | +| $(0,0,-1)$ | 0.20 | 0 | 0 | +| $(+1,+1,0)$ | 0.10 | 0.03 | 0 | +| $(-1,-1,0)$ | 0.05 | 0 | 0 | +| $(+1,-1,0)$ | 0.07 | 0 | 0 | +| $(-1,+1,0)$ | 0.03 | 0 | 0.04 | +| $(+1,0,+1)$ | 0.08 | 0 | 0 | +| $(-1,0,-1)$ | 0.02 | 0.14 | 0 | +| $(+1,0,-1)$ | 0.04 | 0 | 0.06 | +| $(-1,0,+1)$ | 0.06 | 0 | 0 | +| $(0,+1,+1)$ | 0.09 | 0 | 0.02 | +| $(0,-1,-1)$ | 0.01 | 0 | 0 | +| $(0,+1,-1)$ | 0.05 | 0 | 0 | +| $(0,-1,+1)$ | 0.10 | 0.02 | 0 | + +The independent, hand-calculated answers follow by grouping rest, axial, and +diagonal contributions. For the density, + +$$ +\rho = \underbrace{1.3}_{\text{rest}} + + \underbrace{(0.4+0.1+0.2+0.5+0.6+0.2)}_{\text{axial}=2.0} + + \underbrace{(0.25+0.20+0.25)}_{\text{diagonal}=0.7} = 4, +$$ -Thus the independent, hand-calculated answers are +where the diagonal sums are taken over the $xy$, $xz$, and $yz$ planes. The +composition fields are $$ -\rho = 2+0.4+0.1+0.2+0.5+0.6+0.2 = 4, -\qquad \phi=0.3, \qquad \psi=0.4, +\phi = 0.10+0.05-0.04+0.03+0.14+0.02 = 0.30, \qquad +\psi = 0.20+0.03+0.05+0.04+0.06+0.02 = 0.40. $$ +Each momentum component is the axial difference plus the diagonal +differences in the two planes containing that axis: + $$ -\mathbf{j} = (0.4-0.1,\;0.2-0.5,\;0.6-0.2) - = (0.3,-0.3,0.4), +\begin{aligned} +j_x &= (0.4-0.1) + \underbrace{(0.17-0.08)}_{xy} + \underbrace{(0.12-0.08)}_{xz} = 0.43,\\ +j_y &= (0.2-0.5) + \underbrace{(0.13-0.12)}_{xy} + \underbrace{(0.14-0.11)}_{yz} = -0.26,\\ +j_z &= (0.6-0.2) + \underbrace{(0.14-0.06)}_{xz} + \underbrace{(0.19-0.06)}_{yz} = 0.61, +\end{aligned} $$ +so that + $$ -\mathbf{u} = (0.075,-0.075,0.100). +\mathbf{u} = \mathbf{j}/\rho = (0.1075,\,-0.065,\,0.1525), \qquad +p_0 = 4/3. $$ -Unequal opposite populations check momentum signs. Density differs from one -to expose missing velocity normalization. Distinct values of $\phi$ and $\psi$ -help expose swapped composition fields. These prescribed populations are -numerical test inputs and need not be an equilibrium distribution. +Why these inputs: + +- Every diagonal is nonzero, so a loop that skips diagonals (e.g. stops at + $i<7$) gives $\rho=3.3$ and fails. +- Opposite populations are unequal on axial and diagonal links, so a sign + error in any `e19` component changes $\mathbf{j}$. +- $g$ and $k$ are spread over rest, axial, and diagonal directions, so summing + only part of the stencil changes $\phi$ or $\psi$. The negative $g$ value + catches clamping or absolute-value errors. +- $\rho\neq1$ exposes a missing division by density; $\phi\neq\psi$ exposes + swapped composition fields. + +These prescribed populations are numerical test inputs and need not be an +equilibrium distribution. ### Implementation walkthrough 1. **Access declaration:** `FixLbMulticomponent` declares `friend class FixLbMulticomponentTestAccess`. No production method is made public. The helper is defined in the test source in `LAMMPS_NS`. -2. **Fixture setup:** `LBMomentReconstruction::SetUp()` constructs LAMMPS, +2. **Fixture setup:** `FixLbMulticomponentTest::SetUp()` constructs LAMMPS, creates a periodic, atom-free $8\times8\times8$ box with lattice spacing - and timestep equal to one, creates the fix, and validates its pointer. -3. **Choose a site:** `owned_site()` returns the midpoint of the local - allocated lattice. For this fixture it is inside the owned region, - away from halo cells. -4. **Supply inputs:** `set_populations()` overwrites all 19 entries of each - population family at that site. This removes dependence on the random - mixture initialization at the tested site. -5. **Execute production code:** `reconstruct()` calls `calc_moments()` and - returns copies of `density_lb`, `phi_lb`, `psi_lb`, and `u_lb`. It does - not recalculate the expected answer. -6. **Compare:** `ReconstructsDensityCompositionAndVelocity` uses six + and timestep equal to one, creates the fix with all $\kappa=0$, and + validates its pointer. +3. **Choose a site:** `interior_site()` returns + `{halo_extent[0], halo_extent[1], halo_extent[2]}`, i.e. local index + $(2,2,2)$: the first owned site, just inside the halo. Its neighbours at + index 1 are allocated halo cells. +4. **Supply inputs:** the test maps the table above onto D3Q19 indices, then + `set_populations()` overwrites all 19 entries of each population family at + that site. This removes dependence on the random mixture initialization at + the tested site. +5. **Execute production code:** `calculate_moments()` calls `calc_moments()` + and returns copies of `density_lb`, `phi_lb`, `psi_lb`, `pressure_lb`, and + `u_lb`. It does not recalculate the expected answer. +6. **Compare:** `ReconstructsDensityCompositionAndVelocity` uses seven `EXPECT_NEAR` checks with absolute tolerance $10^{-13}$. 7. **Clean up:** `TearDown()` resets the owning `std::unique_ptr`. LAMMPS destroys its fix and associated resources. @@ -111,19 +166,29 @@ the test runs with one MPI rank. ### Coverage and limitations -This test checks local population sums, axial momentum signs, field assignment, -and division by density. It does not check nonzero diagonal contributions, -distributed `g`/`k` populations, pressure correctness, collision, streaming, -global conservation, or MPI communication. Although `calc_moments()` also -updates pressure, no pressure assertion is currently included. +This test checks local zeroth and first moments over all 19 directions, +momentum signs on axial and diagonal links, field assignment, division by +density, and the ideal-gas part of the pressure. It does not check the +$\kappa$-dependent pressure terms, collision, streaming, global conservation, +or MPI communication. + +### Mutation check -During initial validation, removing division by density from the x-velocity -calculation in a temporary source copy caused this test to fail: the actual -value became 0.3 instead of 0.075. The temporary change was restored, and the -reconstruction and existing conservation tests passed. This documents that -validation run, not a guarantee about subsequent source edits. +To confirm the test can fail, each of the following temporary edits was made +in `calc_moments()`, rebuilt, and run; every one made the test fail. The source +was then restored and both registered tests passed again. -## 2. Proposed: gradient and Laplacian +| Temporary edit | Assertions that failed | +| --- | --- | +| Loop bound `i < numvel` → `i < 7` (skip diagonals) | all seven ($\rho=3.3$) | +| `j[1] += fi * e19[i][1]` → `e19[i][0]` | $u_y$ (0.1075 instead of $-0.065$) | +| Swap the `phi_lb` / `psi_lb` assignments | $\phi$ and $\psi$ | +| Remove `/ rho` from $u_x$ | $u_x$ (0.43 instead of 0.1075) | + +This documents that validation run, not a guarantee about subsequent source +edits. + +## 2. Implemented: gradient and Laplacian ### Physical relevance @@ -169,7 +234,7 @@ $$ \nabla_{\mathrm{phys}}^2q \approx \frac{1}{h^2}\nabla_{\mathrm{LB}}^2q. $$ -The proposed unit tests use $h=1$. Conversion of field units is a separate +The unit tests use $h=1$. Conversion of field units is a separate concern. ### Why polynomial fields provide independent answers @@ -188,10 +253,10 @@ Laplacian, up to floating-point rounding. General smooth fields have discretization error and should not be required to match continuum derivatives to roundoff. -### Proposed test cases +### Test cases -Use local coordinates $(X,Y,Z)$ relative to the chosen site, so the tested -site is $(0,0,0)$. Evaluate each polynomial over its complete stencil neighborhood. +The test uses local coordinates $(X,Y,Z)$ relative to the chosen site, so the tested +site is $(0,0,0)$, and evaluates each polynomial over its complete stencil neighborhood. | Field $q(X,Y,Z)$ | Expected gradient at the site | Expected Laplacian | Purpose | | --- | --- | ---: | --- | @@ -217,42 +282,76 @@ $$ $$ This combined case is useful in addition to the isolated cases, which make -failures easier to diagnose. - -### Implementation plan - -1. Reuse the small one-rank fixture and friend-access mechanism. Keep - `calc_gradient_laplacian()` private. -2. Extend the test helper to fill a selected scalar field in the complete - $3\times3\times3$ neighborhood around an owned interior site. D3Q19 reads - only the center, axial neighbors, and face diagonals; initializing the - whole neighborhood keeps setup simple. -3. Add a helper that calls the production derivative function and returns - copies of its three gradient components and one Laplacian value. -4. Parameterize the analytic cases above. Expected values must come from - polynomial derivatives, not a second implementation of the stencil sum. -5. Start with absolute tolerance $10^{-13}$ for these small local values. - Reassess roundoff bounds if field magnitudes are increased. -6. Call the function twice at the same site with different fields, ending - with a constant field. Confirm outputs are reset rather than accumulated. -7. Exercise the `rho`, `phi`, and `psi` field/output pairs. Checking the - automatic wiring through `calc_equilibrium()` can be a separate integration - test; direct stencil tests alone do not validate that wiring. -8. Register a separate one-rank CTest entry, proposed name - `FixLBMulticomponent:derivatives`, with a matching GoogleTest filter. +failures easier to diagnose. Every case in the table, plus this combined case +(13 polynomials), runs for each of the `rho`, `phi`, and `psi` field/output +triples, giving 39 parameterized tests. -The local polynomials need not be periodic over the whole box because this -test neither exchanges halos nor advances the fluid. It requires a valid -neighborhood around the selected site. Boundary and MPI halo behavior require -separate tests. +**Negative control.** For $q=X^3$ the continuum gradient at the origin is +zero, but the second-order stencil gives -### Acceptance criteria +$$ +(\nabla_{\mathrm{LB}}q)_x = 3\sum_i w_i e_{ix}^4 += 3\left(2\cdot\tfrac{1}{18} + 8\cdot\tfrac{1}{36}\right) = 1, +\qquad \nabla_{\mathrm{LB}}^2 q = 6\sum_i w_i e_{ix}^3 = 0. +$$ -- Every analytic case passes for all three gradient components and the Laplacian. -- Repeated calls overwrite previous results. -- A deliberate sign or normalization error makes an appropriate case fail. -- Existing reconstruction and conservation tests still pass. -- No production numerical behavior is changed merely to accommodate the tests. +`CubicShowsSecondOrderTruncation` asserts these discrete values. It pins the +known truncation error and documents why only polynomials up to second order +are compared with continuum derivatives. + +### Implementation walkthrough + +1. **Access:** the existing `FixLbMulticomponentTestAccess` friend helper + gains `Field {Rho, Phi, Psi}` and maps it to the same field/output triples + that `calc_equilibrium()` passes: (`density_lb`, `density_gradient`, + `laplace_rho`), (`phi_lb`, `phi_gradient`, `laplace_phi`), and + (`psi_lb`, `psi_gradient`, `laplace_psi`). `calc_gradient_laplacian()` + stays private. +2. **Fixture:** `FixLbDerivativeTest` derives from `FixLbMulticomponentTest` + (same one-rank $8\times8\times8$ box) so the derivative tests get their own + suite name for the CTest filter. +3. **Supply inputs:** `set_scalar_field()` evaluates the polynomial on the + whole $3\times3\times3$ block around `interior_site()` = $(2,2,2)$. D3Q19 + reads only the centre, axial neighbours, and face diagonals; filling the + block keeps setup simple. Neighbours at index 1 are allocated halo cells. +4. **Sentinel outputs:** before the call, `fill_derivatives()` sets every + gradient component and Laplacian on that block to $10^3$. This replaces the + planned "call twice" check: if the function accumulated into its outputs + instead of resetting them, the tested site would be off by $10^3$. +5. **Execute production code:** `calculate_derivatives()` calls + `calc_gradient_laplacian()` with the selected triple and returns copies of + the three gradient components and the Laplacian at the site. +6. **Compare:** `FixLbDerivativePolynomialTest.MatchesAnalyticDerivatives` is + parameterized over (field, polynomial) and checks the four values against + the analytic derivatives with absolute tolerance $10^{-13}$. It also checks + that all 26 neighbours still hold the sentinel, i.e. the function writes + only to the tested site. + +The local polynomials need not be periodic over the whole box because this +test neither exchanges halos nor advances the fluid. Boundary and MPI halo +behaviour require separate tests. Because the helper passes the triple +explicitly, the test does not validate the wiring inside `calc_equilibrium()`; +that needs a separate integration test. + +### Mutation check + +Each of the following temporary edits to `calc_gradient_laplacian()` was +rebuilt and run against the 40 derivative tests; every one made tests fail. +The source was then restored and all three registered tests passed again. + +| Temporary edit | Tests failed (of 40) | +| --- | ---: | +| Gradient factor `3.` → `2.` | 25 | +| Laplacian factor `6.` → `3.` | 15 | +| Gradient uses `e19[i][0]` for every direction | 25 | +| Laplacian output not reset to zero | 39 | +| Gradient outputs not reset to zero | 39 | +| Laplacian without subtracting the centre value | 18 | +| $x$ neighbour taken as `x - e19[i][0]` | 16 | + +The unaffected cases pass as expected; e.g. the Laplacian factor does not +change any case whose Laplacian is zero. This documents that validation run, +not a guarantee about subsequent source edits. ## 3. Build and run the implemented tests @@ -262,19 +361,27 @@ From a clean CMake-compatible source tree with an MPI toolchain: cmake -S cmake -B build-latboltz -D BUILD_MPI=ON -D PKG_LATBOLTZ=ON -D ENABLE_TESTING=ON cmake --build build-latboltz --target test_fix_lb_multicomponent -j 4 ctest --test-dir build-latboltz -R '^FixLBMulticomponent:moments$' --output-on-failure +ctest --test-dir build-latboltz -R '^FixLBMulticomponent:derivatives$' --output-on-failure ``` -Run both currently registered LATBOLTZ tests with: +Run all three registered LATBOLTZ tests with: ```sh ctest --test-dir build-latboltz -R '^FixLBMulticomponent:' --output-on-failure ``` -`FixLBMulticomponent:moments` uses one MPI rank; the existing -`FixLBMulticomponent:plain` conservation test uses two. GoogleTest filters -select the intended test from their shared executable. The executable still -receives the existing YAML file because the shared force-style test main -expects it; the moment test uses its C++ tolerance, not the YAML epsilon. - -The derivative test name above is proposed and will not run until implemented -and registered in CMake. +`FixLBMulticomponent:moments` and `FixLBMulticomponent:derivatives` use one MPI +rank; the `FixLBMulticomponent:plain` conservation test uses two. GoogleTest +filters select the intended tests from their shared executable: + +| CTest name | GoogleTest filter | +| --- | --- | +| `FixLBMulticomponent:plain` | `FixLBMulticomponent.plain` | +| `FixLBMulticomponent:moments` | `FixLbMulticomponentTest.*` | +| `FixLBMulticomponent:derivatives` | `FixLbDerivativeTest.*:*/FixLbDerivativePolynomialTest.*` | + +A filter that matches no test still exits successfully in GoogleTest 1.12, so +after renaming a fixture, run with `-V` and confirm the test count is non-zero. +The executable still receives the existing YAML file because the shared +force-style test main expects it; the unit tests use their C++ tolerances, not +the YAML epsilon. diff --git a/unittest/latboltz/test_fix_lb_multicomponent.cpp b/unittest/latboltz/test_fix_lb_multicomponent.cpp index 6028ebd0a3c..304e6f8faf4 100644 --- a/unittest/latboltz/test_fix_lb_multicomponent.cpp +++ b/unittest/latboltz/test_fix_lb_multicomponent.cpp @@ -34,6 +34,7 @@ #include #include #include +#include using namespace LAMMPS_NS; @@ -44,7 +45,7 @@ class FixLbMulticomponentTestAccess { public: using Populations = std::array; struct Site { int x, y, z; }; - struct Moments { double rho, phi, psi; + struct Moments { double rho, phi, psi, pressure; std::array velocity; }; // return a interior site of subdomain, avoiding halo sites @@ -71,9 +72,85 @@ class FixLbMulticomponentTestAccess { const auto *u = fix.u_lb[site.x][site.y][site.z]; return {fix.density_lb[site.x][site.y][site.z], fix.phi_lb[site.x][site.y][site.z], - fix.psi_lb[site.x][site.y][site.z], + fix.psi_lb[site.x][site.y][site.z], + fix.pressure_lb[site.x][site.y][site.z], {u[0], u[1], u[2]}}; } + + // Scalar fields passed to calc_gradient_laplacian by calc_equilibrium + enum class Field { Rho, Phi, Psi }; + struct Derivatives { std::array gradient; + double laplacian; }; + // polynomial q(X, Y, Z) in lattice coordinates relative to the tested site + using ScalarFunction = double (*)(double, double, double); + + // Fill the field on the 3x3x3 block around the site, which covers the D3Q19 stencil + static void set_scalar_field(FixLbMulticomponent &fix, Field field, Site site, + ScalarFunction q) + { + double ***values = scalar_field(fix, field); + for (int dx = -1; dx <= 1; ++dx) + for (int dy = -1; dy <= 1; ++dy) + for (int dz = -1; dz <= 1; ++dz) + values[site.x + dx][site.y + dy][site.z + dz] = q(dx, dy, dz); + } + + // Overwrite gradient and laplacian outputs on the 3x3x3 block around the site + static void fill_derivatives(FixLbMulticomponent &fix, Field field, Site site, double value) + { + double ****gradient = gradient_field(fix, field); + double ***laplacian = laplacian_field(fix, field); + for (int dx = -1; dx <= 1; ++dx) + for (int dy = -1; dy <= 1; ++dy) + for (int dz = -1; dz <= 1; ++dz) { + const int x = site.x + dx, y = site.y + dy, z = site.z + dz; + for (int dir = 0; dir < 3; ++dir) gradient[x][y][z][dir] = value; + laplacian[x][y][z] = value; + } + } + + // Return copies of the gradient and laplacian stored at the site + static Derivatives read_derivatives(FixLbMulticomponent &fix, Field field, Site site) + { + const double *g = gradient_field(fix, field)[site.x][site.y][site.z]; + return {{g[0], g[1], g[2]}, laplacian_field(fix, field)[site.x][site.y][site.z]}; + } + + // Call the unit calc_gradient_laplacian with the field/output triple used in calc_equilibrium + static Derivatives calculate_derivatives(FixLbMulticomponent &fix, Field field, Site site) + { + fix.calc_gradient_laplacian(site.x, site.y, site.z, scalar_field(fix, field), + gradient_field(fix, field), laplacian_field(fix, field)); + return read_derivatives(fix, field, site); + } + +private: + static double ***scalar_field(FixLbMulticomponent &fix, Field field) + { + switch (field) { + case Field::Rho: return fix.density_lb; + case Field::Phi: return fix.phi_lb; + default: return fix.psi_lb; + } + } + + static double ****gradient_field(FixLbMulticomponent &fix, Field field) + { + switch (field) { + case Field::Rho: return fix.density_gradient; + case Field::Phi: return fix.phi_gradient; + default: return fix.psi_gradient; + } + } + + static double ***laplacian_field(FixLbMulticomponent &fix, Field field) + { + switch (field) { + case Field::Rho: return fix.laplace_rho; + case Field::Phi: return fix.laplace_phi; + default: return fix.laplace_psi; + } + } }; } // namespace LAMMPS_NS @@ -84,7 +161,7 @@ class FixLbMulticomponentTest : public ::testing::Test { FixLbMulticomponent *fix = nullptr; void SetUp() override{ - const char *args[] = {"LBMomentReconstruction", "-log", "none", "-screen", "none", + const char *args[] = {"FixLbMulticomponentTest", "-log", "none", "-screen", "none", "-nocite"}; lmp.reset(new LAMMPS(sizeof(args) / sizeof(args[0]), const_cast(args), MPI_COMM_WORLD)); @@ -92,7 +169,9 @@ class FixLbMulticomponentTest : public ::testing::Test { lmp->input->one("region fluid block 0 8 0 8 0 8"); lmp->input->one("create_box 0 fluid"); lmp->input->one("timestep 1.0"); - lmp->input->one("fix mcmp all lb/multicomponent 1 0.166667 1.0 D3Q19 dx 1 init mixture"); + // zero kappas reduce the pressure to rho * c_s^2, an independent reference value + lmp->input->one("fix mcmp all lb/multicomponent 1 0.166667 1.0 D3Q19 dx 1 " + "kappa1 0 kappa2 0 kappa3 0 init mixture"); auto fixes = lmp->modify->get_fix_by_style("lb/multicomponent"); ASSERT_EQ(fixes.size(), 1); fix = dynamic_cast(fixes[0]); @@ -109,38 +188,163 @@ TEST_F(FixLbMulticomponentTest, ReconstructsDensityCompositionAndVelocity) { using Access = FixLbMulticomponentTestAccess; Access::Populations f{}, g{}, k{}; - // Resolve direction indices from D3Q19, while keeping expected moments explicit. - for (int i = 0; i < 19; ++i) - { - const int ex = e19[i][0], ey = e19[i][1], ez = e19[i][2]; - if (ex == 0 && ey == 0 && ez == 0) - { - f[i] = 2.0; - g[i] = 0.3; - k[i] = 0.4; - } - else if (ey == 0 && ez == 0) - f[i] = ex == 1 ? 0.4 : 0.1; - else if (ex == 0 && ez == 0) - f[i] = ey == 1 ? 0.2 : 0.5; - else if (ex == 0 && ey == 0) - f[i] = ez == 1 ? 0.6 : 0.2; - } + + std::array hits{}; + struct Entry { int ex, ey, ez; double f, g, k; }; + // Hand-chosen populations keyed by lattice vector, independent of the e19 ordering. + // Diagonals are nonzero and opposite pairs are unequal so every direction contributes. + const Entry table[] = { + { 0, 0, 0, 1.30, 0.10, 0.20}, + { 1, 0, 0, 0.40, 0.05, 0.00}, {-1, 0, 0, 0.10, 0.00, 0.03}, + { 0, 1, 0, 0.20, 0.00, 0.00}, { 0,-1, 0, 0.50, -0.04, 0.00}, + { 0, 0, 1, 0.60, 0.00, 0.05}, { 0, 0,-1, 0.20, 0.00, 0.00}, + { 1, 1, 0, 0.10, 0.03, 0.00}, {-1,-1, 0, 0.05, 0.00, 0.00}, + { 1,-1, 0, 0.07, 0.00, 0.00}, {-1, 1, 0, 0.03, 0.00, 0.04}, + { 1, 0, 1, 0.08, 0.00, 0.00}, {-1, 0,-1, 0.02, 0.14, 0.00}, + { 1, 0,-1, 0.04, 0.00, 0.06}, {-1, 0, 1, 0.06, 0.00, 0.00}, + { 0, 1, 1, 0.09, 0.00, 0.02}, { 0,-1,-1, 0.01, 0.00, 0.00}, + { 0, 1,-1, 0.05, 0.00, 0.00}, { 0,-1, 1, 0.10, 0.02, 0.00}, + }; + + for (const auto &t : table) + for (int i = 0; i < 19; ++i) + if (e19[i][0] == t.ex && e19[i][1] == t.ey && e19[i][2] == t.ez) { + f[i] = t.f; + g[i] = t.g; + k[i] = t.k; + ++hits[i]; + } + // every D3Q19 direction must be assigned exactly once + for (int i = 0; i < 19; ++i) ASSERT_EQ(hits[i], 1) << "direction " << i; + // Get interior lattice site of the subdomain const auto site = Access::interior_site(*fix); // Set fix object f_lb, g_lb, k_lb values Access::set_populations(*fix, site, f, g, k); - // Call the unit fix.calc_moments and returns density, phi, psi, velocity + // Call the unit fix.calc_moments and returns density, phi, psi, pressure, velocity const auto calculated = Access::calculate_moments(*fix, site); constexpr double tolerance = 1e-13; - // check calcualated values against reference + // rho = 1.3 (rest) + 2.0 (axial) + 0.7 (diagonal) EXPECT_NEAR(calculated.rho, 4.0, tolerance); - EXPECT_NEAR(calculated.phi, 0.3, tolerance); - EXPECT_NEAR(calculated.psi, 0.4, tolerance); - EXPECT_NEAR(calculated.velocity[0], 0.075, tolerance); - EXPECT_NEAR(calculated.velocity[1], -0.075, tolerance); - EXPECT_NEAR(calculated.velocity[2], 0.100, tolerance); + EXPECT_NEAR(calculated.phi, 0.30, tolerance); + EXPECT_NEAR(calculated.psi, 0.40, tolerance); + // j = (0.3 + 0.13, -0.3 + 0.04, 0.4 + 0.21), u = j / rho + EXPECT_NEAR(calculated.velocity[0], 0.1075, tolerance); + EXPECT_NEAR(calculated.velocity[1], -0.065, tolerance); + EXPECT_NEAR(calculated.velocity[2], 0.1525, tolerance); + // all kappa = 0 in the fixture, so p = rho * c_s^2 + EXPECT_NEAR(calculated.pressure, 4.0 / 3.0, tolerance); +} + +// Tests of calc_gradient_laplacian, kept in their own suites for the CTest filter +class FixLbDerivativeTest : public FixLbMulticomponentTest {}; + +// Quadratic polynomial with its analytic gradient and laplacian at the origin. +// The D3Q19 stencil is exact for polynomials up to second order. +struct PolynomialCase { + const char *name; + FixLbMulticomponentTestAccess::ScalarFunction q; + std::array gradient; + double laplacian; +}; + +void PrintTo(const PolynomialCase &c, std::ostream *os) { *os << c.name; } + +using Field = FixLbMulticomponentTestAccess::Field; + +std::string field_name(Field field) +{ + switch (field) { + case Field::Rho: return "rho"; + case Field::Phi: return "phi"; + default: return "psi"; + } +} + +const PolynomialCase polynomial_cases[] = { + {"Constant", [](double, double, double) { return 5.0; }, {0, 0, 0}, 0}, + {"X", [](double X, double, double) { return X; }, {1, 0, 0}, 0}, + {"Y", [](double, double Y, double) { return Y; }, {0, 1, 0}, 0}, + {"Z", [](double, double, double Z) { return Z; }, {0, 0, 1}, 0}, + {"Linear", [](double X, double Y, double Z) { return 5 + 2 * X - 3 * Y + 4 * Z; }, + {2, -3, 4}, 0}, + {"XX", [](double X, double, double) { return X * X; }, {0, 0, 0}, 2}, + {"YY", [](double, double Y, double) { return Y * Y; }, {0, 0, 0}, 2}, + {"ZZ", [](double, double, double Z) { return Z * Z; }, {0, 0, 0}, 2}, + {"UnequalCurvature", [](double X, double Y, double Z) { return X * X + 2 * Y * Y + 3 * Z * Z; }, + {0, 0, 0}, 12}, + {"MixedXY", [](double X, double Y, double) { return (X + 1) * (Y + 2); }, {2, 1, 0}, 0}, + {"MixedYZ", [](double, double Y, double Z) { return (Y + 1) * (Z + 2); }, {0, 2, 1}, 0}, + {"MixedZX", [](double X, double, double Z) { return (Z + 1) * (X + 2); }, {1, 0, 2}, 0}, + {"Combined", + [](double X, double Y, double Z) { + return 5 + 2 * X - 3 * Y + 4 * Z + X * X + 2 * Y * Y + 3 * Z * Z; + }, + {2, -3, 4}, 12}, +}; + +class FixLbDerivativePolynomialTest + : public FixLbDerivativeTest, + public ::testing::WithParamInterface> {}; + +TEST_P(FixLbDerivativePolynomialTest, MatchesAnalyticDerivatives) +{ + using Access = FixLbMulticomponentTestAccess; + const Field field = std::get<0>(GetParam()); + const PolynomialCase &c = std::get<1>(GetParam()); + const auto site = Access::interior_site(*fix); + + // Sentinel outputs: the site must be overwritten (not accumulated into), + // and its neighbours must be left untouched. + constexpr double sentinel = 1.0e3; + Access::set_scalar_field(*fix, field, site, c.q); + Access::fill_derivatives(*fix, field, site, sentinel); + const auto calculated = Access::calculate_derivatives(*fix, field, site); + + constexpr double tolerance = 1e-13; + EXPECT_NEAR(calculated.gradient[0], c.gradient[0], tolerance); + EXPECT_NEAR(calculated.gradient[1], c.gradient[1], tolerance); + EXPECT_NEAR(calculated.gradient[2], c.gradient[2], tolerance); + EXPECT_NEAR(calculated.laplacian, c.laplacian, tolerance); + + for (int dx = -1; dx <= 1; ++dx) + for (int dy = -1; dy <= 1; ++dy) + for (int dz = -1; dz <= 1; ++dz) { + if (dx == 0 && dy == 0 && dz == 0) continue; + const auto neighbour = Access::read_derivatives( + *fix, field, {site.x + dx, site.y + dy, site.z + dz}); + EXPECT_EQ(neighbour.gradient, (std::array{sentinel, sentinel, sentinel})) + << "neighbour (" << dx << "," << dy << "," << dz << ")"; + EXPECT_EQ(neighbour.laplacian, sentinel) + << "neighbour (" << dx << "," << dy << "," << dz << ")"; + } +} + +INSTANTIATE_TEST_SUITE_P( + Polynomials, FixLbDerivativePolynomialTest, + ::testing::Combine(::testing::Values(Field::Rho, Field::Phi, Field::Psi), + ::testing::ValuesIn(polynomial_cases)), + [](const ::testing::TestParamInfo &info) { + return field_name(std::get<0>(info.param)) + "_" + std::get<1>(info.param).name; + }); + +// Negative control: for q = X^3 the continuum gradient at the origin is zero, but the +// second-order stencil gives 3 * sum_i w_i e_ix^4 = 3 * (2/18 + 8/36) = 1. This pins the +// expected truncation error and shows the analytic cases are not trivially satisfied. +TEST_F(FixLbDerivativeTest, CubicShowsSecondOrderTruncation) +{ + using Access = FixLbMulticomponentTestAccess; + const auto site = Access::interior_site(*fix); + Access::set_scalar_field(*fix, Field::Phi, site, + [](double X, double, double) { return X * X * X; }); + const auto calculated = Access::calculate_derivatives(*fix, Field::Phi, site); + + constexpr double tolerance = 1e-13; + EXPECT_NEAR(calculated.gradient[0], 1.0, tolerance); + EXPECT_NEAR(calculated.gradient[1], 0.0, tolerance); + EXPECT_NEAR(calculated.gradient[2], 0.0, tolerance); + EXPECT_NEAR(calculated.laplacian, 0.0, tolerance); } LAMMPS *init_lammps()