Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions docs/advanced/md.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions source/source_md/module_snap/snap_calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Calculator::Calculator(const ModelData& model)
if (model_.parameters.bzero_flag)
{
const std::vector<Neighbor> 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<std::complex<double> > self_u
= descriptor_evaluator_.accumulate_u(empty_neighbors, element);
const std::vector<double> self_bispectrum = descriptor_evaluator_.compute_bispectrum(self_u);
bzero_.push_back(self_bispectrum);
self_spectra_.push_back(self_bispectrum);
}
}
}
Expand All @@ -67,7 +67,7 @@ EnergyResult Calculator::compute_from_u(const std::vector<std::complex<double> >
{
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<double>& coefficients = model_.coefficients[central_element];
Expand Down
2 changes: 1 addition & 1 deletion source/source_md/module_snap/snap_calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Calculator
ModelData model_;
DescriptorEvaluator descriptor_evaluator_;
std::size_t descriptor_size_;
std::vector<std::vector<double> > bzero_;
std::vector<std::vector<double> > self_spectra_;
};

class CellAccumulator
Expand Down
8 changes: 4 additions & 4 deletions source/source_md/module_snap/snap_cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion source/source_md/module_snap/snap_cg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry> entries_;
std::map<std::tuple<int, int, int, int, int, int>, std::size_t> entry_indices_;
};
Expand Down
4 changes: 2 additions & 2 deletions source/source_md/module_snap/snap_derivative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>((j - ma) * dimension + (j - mb))];
const double second_root = rootpq_[static_cast<std::size_t>((ma + 1) * dimension + (j - mb))];
const double first_root = root_factors_[static_cast<std::size_t>((j - ma) * dimension + (j - mb))];
const double second_root = root_factors_[static_cast<std::size_t>((ma + 1) * dimension + (j - mb))];
raw[current] += Dual(first_root) * std::complex<Dual>(ar, -ai) * raw[previous];
raw[current + 1] = Dual(-second_root) * std::complex<Dual>(br, -bi) * raw[previous];
}
Expand Down
12 changes: 6 additions & 6 deletions source/source_md/module_snap/snap_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ DescriptorEvaluator::DescriptorEvaluator(const ModelParameters& parameters)
u_block_[static_cast<std::size_t>(j)] = u_size_;
u_size_ += static_cast<std::size_t>((j + 1) * (j + 1));
}
compute_rootpq();
init_root_factors();

for (int j1 = 0; j1 <= parameters_.twojmax; ++j1)
{
Expand All @@ -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<std::size_t>(dimension * dimension), 0.0);
root_factors_.assign(static_cast<std::size_t>(dimension * dimension), 0.0);
for (int p = 1; p <= parameters_.twojmax; ++p)
{
for (int q = 1; q <= parameters_.twojmax; ++q)
{
rootpq_[static_cast<std::size_t>(p * dimension + q)] = std::sqrt(static_cast<double>(p) / q);
root_factors_[static_cast<std::size_t>(p * dimension + q)] = std::sqrt(static_cast<double>(p) / q);
}
}
}
Expand Down Expand Up @@ -141,8 +141,8 @@ std::vector<std::complex<double> > 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<std::size_t>((j - ma) * dimension + (j - mb))];
const double second_root = rootpq_[static_cast<std::size_t>((ma + 1) * dimension + (j - mb))];
const double first_root = root_factors_[static_cast<std::size_t>((j - ma) * dimension + (j - mb))];
const double second_root = root_factors_[static_cast<std::size_t>((ma + 1) * dimension + (j - mb))];
result[current] += first_root * std::complex<double>(ar, -ai) * result[previous];
result[current + 1] = -second_root * std::complex<double>(br, -bi) * result[previous];
}
Expand Down
4 changes: 2 additions & 2 deletions source/source_md/module_snap/snap_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> rootpq_;
std::vector<double> root_factors_;
std::vector<std::size_t> u_block_;
std::vector<BispectrumIndex> bispectrum_indices_;
std::size_t u_size_;
Expand Down
13 changes: 0 additions & 13 deletions source/source_md/module_snap/snap_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 21 additions & 7 deletions source/source_md/module_snap/unittest/test_snap_calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::array<double, 3>, 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<std::array<double, 3>, 3> plus = positions;
std::array<std::array<double, 3>, 3> minus = positions;
plus[atom][static_cast<std::size_t>(direction)] += displacement;
minus[atom][static_cast<std::size_t>(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<std::size_t>(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<std::size_t>(direction)];
}
EXPECT_NEAR(total_force, 0.0, 1.0e-10);
}

const double strain = 1.0e-6;
Expand Down
65 changes: 48 additions & 17 deletions source/source_md/module_snap/unittest/test_snap_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleMD::SNAP::Neighbor> neighbors = {first, second};
const std::vector<std::complex<double> > u = evaluator.accumulate_u(neighbors, 0);
const std::vector<double> bispectrum = evaluator.compute_bispectrum(u);
ASSERT_EQ(bispectrum.size(), static_cast<std::size_t>(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<ModuleMD::SNAP::Neighbor> 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<ModuleMD::SNAP::Neighbor> neighbors = {first, second};
const std::vector<std::complex<double> > u = evaluator.accumulate_u(neighbors, 0);
const std::vector<double> 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<std::size_t>(5));
for (int index = 0; index < 5; ++index)

const std::vector<ModuleMD::SNAP::Neighbor> reordered = {second, first};
const std::vector<std::complex<double> > reordered_u = evaluator.accumulate_u(reordered, 0);
const std::vector<double> reordered_bispectrum = evaluator.compute_bispectrum(reordered_u);
ASSERT_EQ(bispectrum.size(), reordered_bispectrum.size());

std::vector<ModuleMD::SNAP::Neighbor> 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<std::complex<double> > rotated_u = evaluator.accumulate_u(rotated, 0);
const std::vector<double> 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<std::size_t>(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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/source_md/module_snap/unittest/test_snap_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
Loading