Skip to content

Commit 1b64322

Browse files
committed
python bindings are working; local stress is not computed correctly
1 parent c62524e commit 1b64322

File tree

4 files changed

+97
-28
lines changed

4 files changed

+97
-28
lines changed

bindings/bind_py_models.cc

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,30 @@ namespace rascal {
201201
math::Vector_t & weights) {
202202
std::string neg_stress_name = compute_sparse_kernel_local_neg_stress(
203203
calculator, kernel, managers, sparse_points, weights);
204-
math::Matrix_t neg_stress_global{managers.size(), 6};
205-
size_t i_manager{0};
204+
size_t nb_centers_over_managers{0};
206205
for (const auto & manager : managers) {
206+
for (auto center : manager) {
207+
nb_centers_over_managers++;
208+
}
209+
}
210+
math::Matrix_t local_neg_stress{nb_centers_over_managers, 9};
211+
212+
size_t i_center{0};
213+
size_t i_manager_nb_center{0};
214+
for (const auto & manager : managers) {
215+
i_manager_nb_center = 0;
216+
for (auto center : manager) {
217+
i_manager_nb_center++;
218+
}
207219
auto && neg_stress{
208220
*manager
209-
->template get_property<Property<double, 0, Manager_t, 6>>(
210-
neg_stress_name, true)};
211-
neg_stress_global.block(i_manager, 0, 1, 6) =
212-
Eigen::Map<const math::Matrix_t>(neg_stress.view().data(), 1,
213-
6);
214-
i_manager++;
221+
->template get_property<Property<double, 1, Manager_t, 9>>(
222+
neg_stress_name, true, true, true)};
223+
local_neg_stress.block(i_center, 0, i_manager_nb_center, 9) =
224+
Eigen::Map<const math::Matrix_t>(neg_stress.view().data(), 1, 9);
225+
i_center += i_manager_nb_center;
215226
}
216-
return neg_stress_global;
227+
return local_neg_stress;
217228
},
218229
py::call_guard<py::gil_scoped_release>());
219230
}

bindings/rascal/models/krr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ def predict_stress(self, managers, KNM=None, local_stress=False):
398398
)
399399
else:
400400
if local_stress:
401-
raise ValueError("Option local_stress does not work with KNM.
402-
Please set KNM to None.")
401+
raise ValueError("Option local_stress does not work with KNM."
402+
" Please set KNM to None.")
403403
if 6 * len(managers) != KNM.shape[0]:
404404
raise ValueError(
405405
"KNM size mismatch {}!={}".format(6 * len(managers), KNM.shape[0])

src/rascal/models/sparse_kernel_predict.hh

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,11 @@ namespace rascal {
423423
// xz, yx, zy exploiting the symmetry of the stress tensor
424424
// thus yx=xy and zx=xz
425425
// array accessed by voigt_idx and returns spatial_dim_idx
426-
const std::array<std::array<int, 2>, ThreeD> voigt_id_to_spatial_dim = {
427-
{ // voigt_idx, spatial_dim_idx
428-
{{4, 2}}, // xz, z
429-
{{5, 0}}, // xy, x
430-
{{3, 1}}}}; // yz, y
426+
//const std::array<std::array<int, 2>, ThreeD> voigt_id_to_spatial_dim = {
427+
// { // voigt_idx, spatial_dim_idx
428+
// {{4, 2}}, // xz, z
429+
// {{5, 0}}, // xy, x
430+
// {{3, 1}}}}; // yz, y
431431

432432
internal::Hash<math::Vector_t, double> hasher{};
433433
auto kernel_type_str = kernel.parameters.at("name").get<std::string>();
@@ -439,7 +439,7 @@ namespace rascal {
439439
std::string neg_stress_name =
440440
std::string("kernel: ") + kernel_type_str + std::string(" ; ") +
441441
representation_grad_name +
442-
std::string(" negative stress; weight_hash:") + weight_hash;
442+
std::string(" negative local stress; weight_hash:") + weight_hash;
443443

444444
for (const auto & manager : managers) {
445445
if (kernel_type_str == "GAP") {
@@ -450,8 +450,10 @@ namespace rascal {
450450
representation_grad_name, pair_grad_atom_i_r_j_name);
451451
}
452452

453+
//auto && expansions_coefficients{*manager->template get_property<prop_t>(
454+
// this->get_name(), true, true, ExcludeGhosts)};
453455
auto && neg_stress{
454-
*manager->template get_property<Property<double, 0, Manager_t, 6>>(
456+
*manager->template get_property<Property<double, 1, Manager_t, 9>>(
455457
neg_stress_name, true, true, true)};
456458
if (neg_stress.is_updated()) {
457459
continue;
@@ -463,25 +465,37 @@ namespace rascal {
463465
auto && pair_grad_atom_i_r_j{*manager->template get_property<
464466
Property<double, 2, Manager_t, 1, ThreeD>>(pair_grad_atom_i_r_j_name,
465467
true)};
468+
469+
auto manager_root = extract_underlying_manager<0>(manager);
470+
json structure_copy = manager_root->get_atomic_structure();
471+
auto atomic_structure =
472+
structure_copy.template get<AtomicStructure<ThreeD>>();
473+
474+
size_t i_center{0};
466475
for (auto center : manager) {
476+
auto && local_neg_stress = neg_stress[center];
467477
Eigen::Vector3d r_i = center.get_position();
468478
// accumulate partial gradients onto gradients
469479
for (auto neigh : center.pairs_with_self_pair()) {
470480
Eigen::Vector3d r_ji = r_i - neigh.get_position();
471481
for (int i_der{0}; i_der < ThreeD; i_der++) {
472-
const auto & voigt = voigt_id_to_spatial_dim[i_der];
473-
neg_stress(i_der) +=
474-
r_ji(i_der) * pair_grad_atom_i_r_j[neigh](i_der);
475-
neg_stress(voigt[0]) +=
476-
r_ji(voigt[1]) * pair_grad_atom_i_r_j[neigh](i_der);
482+
for (int k_der{0}; k_der < ThreeD; k_der++) {
483+
// HERE YOU CAN DEBUG
484+
local_neg_stress(i_der*3 + k_der) +=
485+
r_ji(i_der) * pair_grad_atom_i_r_j[neigh](k_der);
486+
487+
// ORIGINAL CODE only using one for-loop ofer i_der
488+
//const auto & voigt = voigt_id_to_spatial_dim[i_der];
489+
//neg_stress(i_der) +=
490+
// r_ji(i_der) * pair_grad_atom_i_r_j[neigh](i_der);
491+
//neg_stress(voigt[0]) +=
492+
// r_ji(voigt[1]) * pair_grad_atom_i_r_j[neigh](i_der);
493+
}
477494
}
478495
}
496+
local_neg_stress[i_center] /= atomic_structure.get_volume();
497+
i_center++;
479498
}
480-
auto manager_root = extract_underlying_manager<0>(manager);
481-
json structure_copy = manager_root->get_atomic_structure();
482-
auto atomic_structure =
483-
structure_copy.template get<AtomicStructure<ThreeD>>();
484-
neg_stress[0] /= atomic_structure.get_volume();
485499
neg_stress.set_updated_status(true);
486500
} // manager
487501
return neg_stress_name;

tests/python/python_krr_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# TMP line for debugging for alex
2+
import sys
3+
sys.path.insert(0, "/home/goscinsk/code/librascal-local-stress/build")
4+
5+
import rascal
6+
print(rascal.__file__)
7+
import unittest
8+
# TMP END
9+
10+
11+
import json
12+
13+
import numpy as np
14+
import ase.io
15+
16+
from rascal.utils import load_obj
17+
18+
class TestKRR(unittest.TestCase):
19+
def setUp(self):
20+
"""
21+
"""
22+
self.model = load_obj("reference_data/tests_only/simple_gap_model.json")
23+
# the file is extracted from the "reference_data/tests_only/simple_gap_fit_params.json"
24+
self.frames = ase.io.read("reference_data/inputs/methane_dimer_sample.xyz", ":")
25+
26+
def test_local_neg_stress(self):
27+
"""..."""
28+
calculator = self.model.get_representation_calculator()
29+
for frame in self.frames:
30+
frame.wrap(eps=1e-12)
31+
manager = calculator.transform(frame)
32+
global_stress = self.model.predict_stress(manager)
33+
print(global_stress.shape)
34+
print(global_stress)
35+
36+
# compare the to outputs
37+
local_stress = self.model.predict_stress(manager, local_stress=True)
38+
print(local_stress.shape)
39+
print(np.sum(local_stress, axis=0))
40+
41+
# test should do something like, but does no work atm
42+
#self.assertTrue(np.allclose(global_stress, np.sum(local_stress, axis=0)))
43+
break
44+

0 commit comments

Comments
 (0)