From 64ec076271a22f45f0bb3068b70e5aa9fa4ba0a1 Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Wed, 4 Nov 2026 15:45:00 +0800 Subject: [PATCH 1/2] refactor(snap): simplify internals and tighten model parsing --- .../source_md/module_snap/snap_calculator.cpp | 6 +- .../source_md/module_snap/snap_calculator.h | 2 +- source/source_md/module_snap/snap_cg.cpp | 8 +-- source/source_md/module_snap/snap_cg.h | 2 +- .../source_md/module_snap/snap_derivative.cpp | 4 +- .../source_md/module_snap/snap_descriptor.cpp | 12 ++-- .../source_md/module_snap/snap_descriptor.h | 4 +- source/source_md/module_snap/snap_model.cpp | 13 ---- .../unittest/test_snap_calculator.cpp | 28 ++++++-- .../unittest/test_snap_descriptor.cpp | 65 ++++++++++++++----- .../module_snap/unittest/test_snap_model.cpp | 4 +- 11 files changed, 90 insertions(+), 58 deletions(-) diff --git a/source/source_md/module_snap/snap_calculator.cpp b/source/source_md/module_snap/snap_calculator.cpp index 424c198149..d646507fbd 100644 --- a/source/source_md/module_snap/snap_calculator.cpp +++ b/source/source_md/module_snap/snap_calculator.cpp @@ -36,13 +36,13 @@ Calculator::Calculator(const ModelData& model) if (model_.parameters.bzero_flag) { const std::vector empty_neighbors; - bzero_.reserve(model_.coefficients.size()); + self_spectra_.reserve(model_.coefficients.size()); for (std::size_t element = 0; element < model_.coefficients.size(); ++element) { const std::vector > self_u = descriptor_evaluator_.accumulate_u(empty_neighbors, element); const std::vector self_bispectrum = descriptor_evaluator_.compute_bispectrum(self_u); - bzero_.push_back(self_bispectrum); + self_spectra_.push_back(self_bispectrum); } } } @@ -67,7 +67,7 @@ EnergyResult Calculator::compute_from_u(const std::vector > { for (std::size_t index = 0; index < result.descriptor.size(); ++index) { - result.descriptor[index] -= bzero_[central_element][index]; + result.descriptor[index] -= self_spectra_[central_element][index]; } } const std::vector& coefficients = model_.coefficients[central_element]; diff --git a/source/source_md/module_snap/snap_calculator.h b/source/source_md/module_snap/snap_calculator.h index 077cd4f5ca..b8ea6dd6ae 100644 --- a/source/source_md/module_snap/snap_calculator.h +++ b/source/source_md/module_snap/snap_calculator.h @@ -46,7 +46,7 @@ class Calculator ModelData model_; DescriptorEvaluator descriptor_evaluator_; std::size_t descriptor_size_; - std::vector > bzero_; + std::vector > self_spectra_; }; class CellAccumulator diff --git a/source/source_md/module_snap/snap_cg.cpp b/source/source_md/module_snap/snap_cg.cpp index e9f2ca0ffb..e446f8a054 100644 --- a/source/source_md/module_snap/snap_cg.cpp +++ b/source/source_md/module_snap/snap_cg.cpp @@ -17,12 +17,12 @@ int half_sum(const int left, const int right) ClebschGordanTable::ClebschGordanTable(const int twojmax) { - twojmax_ = twojmax < 0 ? 0 : twojmax; - for (int j1 = 0; j1 <= twojmax_; ++j1) + max_j_ = twojmax < 0 ? 0 : twojmax; + for (int j1 = 0; j1 <= max_j_; ++j1) { - for (int j2 = 0; j2 <= twojmax_; ++j2) + for (int j2 = 0; j2 <= max_j_; ++j2) { - for (int j = std::abs(j1 - j2); j <= std::min(twojmax_, j1 + j2); j += 2) + for (int j = std::abs(j1 - j2); j <= std::min(max_j_, j1 + j2); j += 2) { for (int m1 = -j1; m1 <= j1; m1 += 2) { diff --git a/source/source_md/module_snap/snap_cg.h b/source/source_md/module_snap/snap_cg.h index 7198536882..4d26ce556e 100644 --- a/source/source_md/module_snap/snap_cg.h +++ b/source/source_md/module_snap/snap_cg.h @@ -40,7 +40,7 @@ class ClebschGordanTable static double factorial(int value); const Entry* find(int j1, int m1, int j2, int m2, int j, int m) const; - int twojmax_; + int max_j_; std::vector entries_; std::map, std::size_t> entry_indices_; }; diff --git a/source/source_md/module_snap/snap_derivative.cpp b/source/source_md/module_snap/snap_derivative.cpp index 2c64668b26..fc3e15d568 100644 --- a/source/source_md/module_snap/snap_derivative.cpp +++ b/source/source_md/module_snap/snap_derivative.cpp @@ -164,8 +164,8 @@ UWithDerivative DescriptorEvaluator::compute_u_with_derivative(const Neighbor& n { const std::size_t current = u_index(j, ma, mb); const std::size_t previous = u_index(j - 1, ma, mb); - const double first_root = rootpq_[static_cast((j - ma) * dimension + (j - mb))]; - const double second_root = rootpq_[static_cast((ma + 1) * dimension + (j - mb))]; + const double first_root = root_factors_[static_cast((j - ma) * dimension + (j - mb))]; + const double second_root = root_factors_[static_cast((ma + 1) * dimension + (j - mb))]; raw[current] += Dual(first_root) * std::complex(ar, -ai) * raw[previous]; raw[current + 1] = Dual(-second_root) * std::complex(br, -bi) * raw[previous]; } diff --git a/source/source_md/module_snap/snap_descriptor.cpp b/source/source_md/module_snap/snap_descriptor.cpp index fe9afc1560..9b36bbec28 100644 --- a/source/source_md/module_snap/snap_descriptor.cpp +++ b/source/source_md/module_snap/snap_descriptor.cpp @@ -26,7 +26,7 @@ DescriptorEvaluator::DescriptorEvaluator(const ModelParameters& parameters) u_block_[static_cast(j)] = u_size_; u_size_ += static_cast((j + 1) * (j + 1)); } - compute_rootpq(); + init_root_factors(); for (int j1 = 0; j1 <= parameters_.twojmax; ++j1) { @@ -47,15 +47,15 @@ DescriptorEvaluator::DescriptorEvaluator(const ModelParameters& parameters) } } -void DescriptorEvaluator::compute_rootpq() +void DescriptorEvaluator::init_root_factors() { const int dimension = parameters_.twojmax + 1; - rootpq_.assign(static_cast(dimension * dimension), 0.0); + root_factors_.assign(static_cast(dimension * dimension), 0.0); for (int p = 1; p <= parameters_.twojmax; ++p) { for (int q = 1; q <= parameters_.twojmax; ++q) { - rootpq_[static_cast(p * dimension + q)] = std::sqrt(static_cast(p) / q); + root_factors_[static_cast(p * dimension + q)] = std::sqrt(static_cast(p) / q); } } } @@ -141,8 +141,8 @@ std::vector > DescriptorEvaluator::compute_u(const Neighbor { const std::size_t current = u_index(j, ma, mb); const std::size_t previous = u_index(j - 1, ma, mb); - const double first_root = rootpq_[static_cast((j - ma) * dimension + (j - mb))]; - const double second_root = rootpq_[static_cast((ma + 1) * dimension + (j - mb))]; + const double first_root = root_factors_[static_cast((j - ma) * dimension + (j - mb))]; + const double second_root = root_factors_[static_cast((ma + 1) * dimension + (j - mb))]; result[current] += first_root * std::complex(ar, -ai) * result[previous]; result[current + 1] = -second_root * std::complex(br, -bi) * result[previous]; } diff --git a/source/source_md/module_snap/snap_descriptor.h b/source/source_md/module_snap/snap_descriptor.h index e3759d4eca..f9eb917b2b 100644 --- a/source/source_md/module_snap/snap_descriptor.h +++ b/source/source_md/module_snap/snap_descriptor.h @@ -76,11 +76,11 @@ class DescriptorEvaluator std::size_t u_index(int j, int ma, int mb) const; SwitchValue switching(double distance, const Neighbor& neighbor, double cutoff_radius) const; - void compute_rootpq(); + void init_root_factors(); ModelParameters parameters_; ClebschGordanTable cg_table_; - std::vector rootpq_; + std::vector root_factors_; std::vector u_block_; std::vector bispectrum_indices_; std::size_t u_size_; diff --git a/source/source_md/module_snap/snap_model.cpp b/source/source_md/module_snap/snap_model.cpp index caf8caa866..131e3d2ede 100644 --- a/source/source_md/module_snap/snap_model.cpp +++ b/source/source_md/module_snap/snap_model.cpp @@ -413,19 +413,6 @@ bool ModelData::load(const std::string& coefficient_file, has_dinner = true; } } - else if (keyword == "chunksize" || keyword == "parallelthresh") - { - int value = 0; - if (!read_parameter_value(words, value, keyword, error)) - { - return false; - } - if (value <= 0) - { - error = "SNAP " + keyword + " must be positive."; - return false; - } - } else { error = "Unknown SNAP parameter: " + keyword; diff --git a/source/source_md/module_snap/unittest/test_snap_calculator.cpp b/source/source_md/module_snap/unittest/test_snap_calculator.cpp index fd719d2329..927a113890 100644 --- a/source/source_md/module_snap/unittest/test_snap_calculator.cpp +++ b/source/source_md/module_snap/unittest/test_snap_calculator.cpp @@ -444,24 +444,38 @@ TEST(SnapCalculator, ZeroWeightSkipsInvalidRadius) EXPECT_THROW(calculator.compute_with_analytic_force(active, 0), std::domain_error); } -TEST(SnapCalculator, AngularQuadraticForcesMatchLammpsAndStrain) +TEST(SnapCalculator, AngularQuadraticForcesMatchEnergyGradientAndStrain) { const ModuleMD::SNAP::Calculator calculator(make_angular_quadratic_model()); const std::array, 3> positions = {{{0.0, 0.0, 0.0}, {1.058354, 0.0, 0.0}, {0.2645885, 1.3229425, 0.0}}}; const SmallCellResult result = evaluate_small_cell(calculator, positions); - const double lammps_forces[3][2] = {{67.2928038408958, 57.5476436778783}, - {-92.1399449575303, 60.5944497536836}, - {24.8471411166345, -118.142093431562}}; - EXPECT_NEAR(result.energy, 54.909561, 1.0e-5); + const double displacement = 1.0e-6; for (std::size_t atom = 0; atom < positions.size(); ++atom) { - for (int direction = 0; direction < 2; ++direction) + for (int direction = 0; direction < 3; ++direction) { + std::array, 3> plus = positions; + std::array, 3> minus = positions; + plus[atom][static_cast(direction)] += displacement; + minus[atom][static_cast(direction)] -= displacement; + const double energy_gradient = (evaluate_small_cell(calculator, plus).energy + - evaluate_small_cell(calculator, minus).energy) + / (2.0 * displacement); EXPECT_NEAR(result.forces[atom][static_cast(direction)], - lammps_forces[atom][direction], 2.0e-4); + -energy_gradient, 1.0e-4); + } + } + + for (int direction = 0; direction < 3; ++direction) + { + double total_force = 0.0; + for (std::size_t atom = 0; atom < positions.size(); ++atom) + { + total_force += result.forces[atom][static_cast(direction)]; } + EXPECT_NEAR(total_force, 0.0, 1.0e-10); } const double strain = 1.0e-6; diff --git a/source/source_md/module_snap/unittest/test_snap_descriptor.cpp b/source/source_md/module_snap/unittest/test_snap_descriptor.cpp index c716c492ef..658d8a383f 100644 --- a/source/source_md/module_snap/unittest/test_snap_descriptor.cpp +++ b/source/source_md/module_snap/unittest/test_snap_descriptor.cpp @@ -98,28 +98,59 @@ TEST(SnapDescriptor, SmallNonzeroDistanceIsNotDiscarded) EXPECT_GT(largest_derivative, 0.1); } -TEST(SnapDescriptor, MatchesLammpsSingleElementReference) +TEST(SnapDescriptor, OrderZeroBispectrumHasAnalyticValue) +{ + ModuleMD::SNAP::ModelParameters parameters = make_parameters(); + parameters.twojmax = 0; + parameters.switch_flag = false; + const ModuleMD::SNAP::DescriptorEvaluator evaluator(parameters); + ModuleMD::SNAP::Neighbor first; + first.x = 1.0; + first.weight = 0.7; + ModuleMD::SNAP::Neighbor second; + second.y = 2.0; + second.weight = 1.3; + const std::vector neighbors = {first, second}; + const std::vector > u = evaluator.accumulate_u(neighbors, 0); + const std::vector bispectrum = evaluator.compute_bispectrum(u); + ASSERT_EQ(bispectrum.size(), static_cast(1)); + EXPECT_NEAR(bispectrum[0], 27.0, 1.0e-12); +} + +TEST(SnapDescriptor, BispectrumIsInvariantUnderRotationAndNeighborOrder) { const ModuleMD::SNAP::DescriptorEvaluator evaluator(make_parameters()); - const double positions[3][3] = {{1.0, 1.0, 1.0}, {2.0, 1.0, 1.0}, {1.0, 3.0, 1.0}}; - std::vector neighbors; - for (int atom = 1; atom < 3; ++atom) - { - ModuleMD::SNAP::Neighbor neighbor; - neighbor.x = positions[atom][0] - positions[0][0]; - neighbor.y = positions[atom][1] - positions[0][1]; - neighbor.z = positions[atom][2] - positions[0][2]; - neighbor.cutoff_radius = 4.0; - neighbors.push_back(neighbor); - } + ModuleMD::SNAP::Neighbor first; + first.x = 1.0; + first.y = 0.3; + first.z = -0.2; + ModuleMD::SNAP::Neighbor second; + second.x = -0.4; + second.y = 1.7; + second.z = 0.5; + const std::vector neighbors = {first, second}; const std::vector > u = evaluator.accumulate_u(neighbors, 0); const std::vector bispectrum = evaluator.compute_bispectrum(u); - const double reference[5] = { - 13.0368348609, 15.0992648922, 11.7631725456, 13.7069739819, 6.07477849248}; - ASSERT_EQ(bispectrum.size(), static_cast(5)); - for (int index = 0; index < 5; ++index) + + const std::vector reordered = {second, first}; + const std::vector > reordered_u = evaluator.accumulate_u(reordered, 0); + const std::vector reordered_bispectrum = evaluator.compute_bispectrum(reordered_u); + ASSERT_EQ(bispectrum.size(), reordered_bispectrum.size()); + + std::vector rotated = neighbors; + for (ModuleMD::SNAP::Neighbor& neighbor : rotated) + { + const double old_x = neighbor.x; + neighbor.x = -neighbor.y; + neighbor.y = old_x; + } + const std::vector > rotated_u = evaluator.accumulate_u(rotated, 0); + const std::vector rotated_bispectrum = evaluator.compute_bispectrum(rotated_u); + ASSERT_EQ(bispectrum.size(), rotated_bispectrum.size()); + for (std::size_t index = 0; index < bispectrum.size(); ++index) { - EXPECT_NEAR(bispectrum[static_cast(index)], reference[index], 1.0e-9); + EXPECT_NEAR(bispectrum[index], reordered_bispectrum[index], 1.0e-10); + EXPECT_NEAR(bispectrum[index], rotated_bispectrum[index], 1.0e-10); } } diff --git a/source/source_md/module_snap/unittest/test_snap_model.cpp b/source/source_md/module_snap/unittest/test_snap_model.cpp index 118f4fa6a9..2b9e41ed72 100644 --- a/source/source_md/module_snap/unittest/test_snap_model.cpp +++ b/source/source_md/module_snap/unittest/test_snap_model.cpp @@ -91,7 +91,7 @@ TEST(SnapModel, RejectsRminOutsideSmallPairCutoff) EXPECT_NE(error.find("Small-Small"), std::string::npos); } -TEST(SnapModel, LoadsLammpsStyleCoefficientAndParameterFiles) +TEST(SnapModel, LoadsSnapCoefficientAndParameterFiles) { const std::string coefficient_file = testing::TempDir() + "snap_coeff_test.txt"; const std::string parameter_file = testing::TempDir() + "snap_param_test.txt"; @@ -229,7 +229,7 @@ TEST(SnapModel, ReadsInnerSwitchAndRejectsUnknownParameter) { std::ofstream output(parameter_file.c_str()); output << "rcutfac 1.0\ntwojmax 0\nswitchinnerflag 1\n"; - output << "sinner 1.5\ndinner 0.4\nchunksize 1024\n"; + output << "sinner 1.5\ndinner 0.4\n"; } ModuleMD::SNAP::ModelData model; std::string error; From 6a1f6742160c30bb6f5f0b00a99d0f53c90efb5a Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Wed, 4 Nov 2026 15:45:56 +0800 Subject: [PATCH 2/2] docs(snap): align parameter guide with parser --- docs/advanced/md.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/advanced/md.md b/docs/advanced/md.md index 98d17de43b..621aef3094 100644 --- a/docs/advanced/md.md +++ b/docs/advanced/md.md @@ -113,8 +113,7 @@ change the potential. The values in `sinner` and `dinner` also follow STRU element order. This corresponds to the first-appearance order of elements in LAMMPS `pair_coeff` mappings. -The CPU implementation accepts but does not use the LAMMPS GPU execution -options `chunksize` and `parallelthresh`. Unknown SNAP parameter keywords are -rejected. Each element-pair cutoff must exceed `rmin0`; a contributing neighbor -at or below `rmin0` causes an error. The radial mapping parameter `rfac0` must +Unknown SNAP parameter keywords are rejected. Each element-pair cutoff must +exceed `rmin0`; a contributing neighbor at or below `rmin0` causes an error. +The radial mapping parameter `rfac0` must satisfy `0 < rfac0 <= 1` so that its angle does not cross pi inside a pair cutoff.