Skip to content

Commit 30d6f47

Browse files
committed
fix docs and some cleanup
1 parent fd77379 commit 30d6f47

File tree

5 files changed

+55
-104
lines changed

5 files changed

+55
-104
lines changed

rascaline/src/calculators/bondatom_neighbor_list.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -162,38 +162,40 @@ fn rotate_vector_to_z_derivatives(vec: Vector3D) -> (Matrix3,Matrix3,Matrix3) {
162162

163163

164164

165-
/// TODO fix docstring
166-
/// This calculator computes the neighbor list for a given spherical cutoff, and
167-
/// returns the list of distance vectors between all pairs of atoms strictly
168-
/// inside the cutoff.
165+
/// Manages a list of 'neighbors', where one neighbor is the center of a pair of atoms
166+
/// (first and second atom), and the other neighbor is a simple atom (third atom).
167+
/// Both the length of the bond and the distance between neighbors are subjected to a spherical cutoff.
168+
///
169+
/// Unlike the corresponding pre_calculator, this calculator focuses on storing
170+
/// the canonical-orientation vector between bond and atom, rather than the bond vector and 'third vector'.
169171
///
170172
/// Users can request either a "full" neighbor list (including an entry for both
171-
/// `i - j` pairs and `j - i` pairs) or save memory/computational by only
172-
/// working with "half" neighbor list (only including one entry for each `i/j`
173-
/// pair)
173+
/// `i - j` bonds and `j - i` bonds) or save memory/computational by only
174+
/// working with "half" neighbor list (only including one entry for each `i - j`
175+
/// bond)
176+
/// if memory is saved, the order of i and j is that the atom with
177+
/// the smallest Z (or species ID in general) comes first.
174178
///
175-
/// Self pairs (pairs between an atom and periodic copy itself) can appear when
176-
/// the cutoff is larger than the cell under periodic boundary conditions. Self
177-
/// pairs with a distance of 0 are not included in this calculator, even though
178-
/// they are required when computing SOAP.
179+
/// The two first atoms must not be the same atom, but the third atom may be one of them,
180+
/// if the `bond_conbtribution` option is active
181+
/// (When periodic boundaries arise, atom which must not be the same may be images of each other.)
179182
///
180183
/// This sample produces a single property (`"distance"`) with three components
181184
/// (`"vector_direction"`) containing the x, y, and z component of the vector from
182-
/// the first atom in the pair to the second. In addition to the atom indexes,
183-
/// the samples also contain a pair index, to be able to distinguish between
184-
/// multiple pairs between the same atom (if the cutoff is larger than the
185-
/// cell).
185+
/// the center of the triplet's 'bond' to the triplet's 'third atom', in the bond's canonical orientation.
186+
///
187+
/// In addition to the atom indexes, the samples also contain a pair and triplet index,
188+
/// to be able to distinguish between multiple triplets involving the same atoms
189+
/// (which can occur in periodic boundary conditions when the cutoffs are larger than the unit cell).
186190
#[derive(Debug, Clone)]
187191
#[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
188192
pub struct BANeighborList {
189193
/// the pre-calculator responsible for making a raw enumeration of the system's bond-atom triplets
190194
pub raw_triplets: BATripletNeighborList,
191-
/// Should individual atoms be considered their own neighbor? Setting this
192-
/// to `true` will add "self pairs", i.e. pairs between an atom and itself,
193-
/// with the distance 0. The `pair_i` of such pairs is set to -1.
195+
/// Should we include triplets where the third atom is one of the bond's atoms?
194196
pub bond_contribution: bool,
195-
/// Should we compute a full neighbor list (each pair appears twice, once as
196-
/// `i-j` and once as `j-i`), or a half neighbor list (each pair only
197+
/// Should we compute a full neighbor list (each triplet appears twice, once as
198+
/// `i-j +k` and once as `j-i +k`), or a half neighbor list (each triplet only
197199
/// appears once, (such that `species_i <= species_j`))
198200
pub use_half_enumeration: bool,
199201
}
@@ -224,11 +226,7 @@ impl BANeighborList {
224226
self.raw_triplets.third_cutoff()
225227
}
226228

227-
/// validate that the cutoffs make sense
228-
fn validate_cutoffs(&self) {
229-
self.raw_triplets.validate_cutoffs()
230-
}
231-
229+
/// a "flatter" initialisation method than the structure-based one
232230
pub fn from_params(cutoffs: [f64;2], use_half_enumeration: bool, bond_contribution: bool) -> Self {
233231
Self{
234232
raw_triplets: BATripletNeighborList {
@@ -239,7 +237,8 @@ impl BANeighborList {
239237
}
240238
}
241239

242-
240+
/// the core of the calculation being done here:
241+
/// computing the canonical-orientation vector and distance of a given bond-atom triplet.
243242
pub(super) fn compute_single_triplet(
244243
triplet: &BATripletInfo,
245244
invert: bool,
@@ -334,6 +333,8 @@ impl BANeighborList {
334333
return Ok(res);
335334
}
336335

336+
/// get the canonical-orientation vector and distance of a triplet
337+
/// and store it in a TensorBlock
337338
fn compute_single_triplet_inplace(
338339
triplet: &BATripletInfo,
339340
out_block: &mut TensorBlockRefMut,
@@ -400,8 +401,6 @@ impl CalculatorBase for BANeighborList {
400401
}
401402

402403
fn keys(&self, systems: &mut [System]) -> Result<Labels, Error> {
403-
self.validate_cutoffs();
404-
405404
let mut all_species_triplets = BTreeSet::new();
406405
for system in systems {
407406
self.raw_triplets.ensure_computed_for_system(system)?;
@@ -437,7 +436,6 @@ impl CalculatorBase for BANeighborList {
437436
}
438437

439438
fn samples(&self, keys: &Labels, systems: &mut [System]) -> Result<Vec<Labels>, Error> {
440-
self.validate_cutoffs();
441439
let mut results = Vec::new();
442440

443441
for &[species_first, species_second, species_third] in keys.iter_fixed_size() {

rascaline/src/calculators/soap/spherical_expansion_bondcentered_pair.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ use super::spherical_expansion_pair::{
3030
PairContribution
3131
};
3232

33-
/// Parameters for spherical expansion calculator.
33+
/// Parameters for spherical expansion calculator for bond-centered neighbor densities.
3434
///
35-
/// The spherical expansion is at the core of representations in the SOAP
35+
/// (The spherical expansion is at the core of representations in the SOAP
3636
/// (Smooth Overlap of Atomic Positions) family. See [this review
3737
/// article](https://doi.org/10.1063/1.5090481) for more information on the SOAP
3838
/// representation, and [this paper](https://doi.org/10.1063/5.0044689) for
39-
/// information on how it is implemented in rascaline.
39+
/// information on how it is implemented in rascaline.)
40+
///
41+
/// This calculator is only needed to characterize local environments that are centered
42+
/// on a pair of atoms rather than a single one.
4043
#[derive(Debug, Clone)]
4144
#[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
4245
pub struct SphericalExpansionForBondsParameters {
4346
/// Spherical cutoffs to use for atomic environments
44-
pub(super) cutoffs: [f64;2], // bond_, third_cutoff
45-
//pub bond_cutoff: f64,
46-
//pub third_cutoff: f64,
47-
//pub compute_both_sides: bool,
47+
pub(super) cutoffs: [f64;2],
4848
/// Number of radial basis function to use in the expansion
4949
pub max_radial: usize,
5050
/// Number of spherical harmonics to use in the expansion
@@ -277,26 +277,8 @@ impl SphericalExpansionForBondType {
277277
let max_radial = self.parameters.max_radial;
278278
let species = system.species().unwrap();
279279

280-
//let _ = system.triplets()?;
281-
// for s3 in s3_list{
282-
// let triplets = system.triplets_with_species(s1, s2, *s3)?;
283-
// if let Err(error) = triplets {
284-
// return Err(error);
285-
// }
286-
// }
287280

288281
let pre_iter = s3_list.iter().flat_map(|s3|{
289-
// let all_triplets = system.triplets().unwrap();
290-
// let triplets = system.triplets_with_species(s1, s2, *s3).unwrap();
291-
// triplets.iter().map(|triplet_i|{
292-
// let triplet = &all_triplets[*triplet_i];
293-
// #[cfg(debug_assertions)]{
294-
// assert_eq!(species[triplet.bond.first],s1);
295-
// assert_eq!(species[triplet.bond.second],s2);
296-
// assert_eq!(species[triplet.third],*s3);
297-
// }
298-
// (*triplet_i,triplet)
299-
// })
300282
self.distance_calculator.raw_triplets.get_per_system_per_species(system,s1,s2,*s3,true).unwrap().into_iter()
301283
}).flat_map(|triplet| {
302284
let invert: &'static [bool] = {

rascaline/src/labels/keys.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ impl KeysBuilder for CenterSingleNeighborsSpeciesKeys {
9999
/// and the species of a third, neighbor atom, within a cutoff of the first two.
100100
pub struct TwoCentersSingleNeighborsSpeciesKeys<'a> {
101101
/// Spherical cutoff to use when searching for neighbors around an atom
102-
pub(crate) cutoffs: [f64;2], // bond_, third_cutoff
103-
//pub bond_cutoff: f64,
104-
//pub third_cutoff: f64,
102+
pub(crate) cutoffs: [f64;2],
105103
/// Should we consider an atom to be it's own neighbor or not?
106104
pub self_contributions: bool,
107105
pub raw_triplets: &'a BATripletNeighborList,
@@ -123,7 +121,6 @@ impl<'a> KeysBuilder for TwoCentersSingleNeighborsSpeciesKeys<'a> {
123121

124122
let mut all_species_triplets = BTreeSet::new();
125123
for system in systems {
126-
//system.compute_triplet_neighbors(self.bond_cutoff(), self.third_cutoff())?;
127124
self.raw_triplets.ensure_computed_for_system(system)?;
128125

129126
let species = system.species()?;

rascaline/src/labels/samples/bond_centered.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ use super::super::super::pre_calculators::BATripletNeighborList;
1616
/// optionally filtering on the neighbor atom species.
1717
pub struct BondCenteredSamples<'a> {
1818
/// spherical cutoff radius used to construct the atom-centered environments
19-
pub cutoffs: [f64;2], // bond_, third_cutoff
20-
//pub bond_cutoff: f64,
21-
//pub third_cutoff: f64,
19+
pub cutoffs: [f64;2],
2220
/// Filter for the central atom species
2321
pub species_center_1: SpeciesFilter,
2422
pub species_center_2: SpeciesFilter,

rascaline/src/pre_calculators/bond_atom_triplet_neighbors.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@ pub struct BATripletInfo{
4848

4949

5050
/// Manages a list of 'neighbors', where one neighbor is the center of a pair of atoms
51-
/// (first and second atom), and the other neighbot is a simple atom (third atom).
51+
/// (first and second atom), and the other neighbor is a simple atom (third atom).
5252
/// Both the length of the bond and the distance between neighbors are subjected to a spherical cutoff.
5353
/// This pre-calculator can compute and cache this list within a given system
5454
/// (with two distance vectors per entry: one within the bond and one between neighbors).
5555
/// Then, it can re-enumerate those neighbors, either for a full system, or with restrictions on the atoms or their species.
5656
///
57-
/// Users can request either a "full" neighbor list (including an entry for both
58-
/// `i - j` bonds and `j - i` bonds) or save memory/computational by only
59-
/// working with "half" neighbor list (only including one entry for each `i - j`
60-
/// bond)
57+
/// This saves memory/computational power by only working with "half" neighbor list
58+
/// This is done by only including one entry for each `i - j` bond, not both `i - j` and `j - i`.
59+
/// The order of i and j is that the atom with the smallest Z (or species ID in general) comes first.
6160
///
62-
/// The two first atoms may not be the same atom, but the third atom may be one of them.
61+
/// The two first atoms must not be the same atom, but the third atom may be one of them.
6362
/// (When periodic boundaries arise, the two first atoms may be images of each other.)
6463
#[derive(Debug,Clone)]
6564
#[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
@@ -72,6 +71,7 @@ pub struct BATripletNeighborList {
7271
pub cutoffs: [f64;2], // bond_, third_cutoff
7372
}
7473

74+
/// the internal function doing the triplet computing itself
7575
fn list_raw_triplets(system: &mut dyn SystemBase, bond_cutoff: f64, third_cutoff: f64) -> Result<Vec<BATripletInfo>,Error> {
7676
system.compute_neighbors(bond_cutoff)?;
7777
let bonds = system.pairs()?.to_owned();
@@ -206,53 +206,18 @@ impl BATripletNeighborList {
206206
}
207207

208208
/// validate that the cutoffs make sense
209-
pub(crate) fn validate_cutoffs(&self) { // TODO: un-pub
209+
pub fn validate_cutoffs(&self) {
210210
let (bond_cutoff, third_cutoff) = (self.bond_cutoff(), self.third_cutoff());
211211
assert!(bond_cutoff > 0.0 && bond_cutoff.is_finite());
212212
assert!(third_cutoff >= bond_cutoff && third_cutoff.is_finite());
213213
}
214214

215+
/// internal function that deletages computing the triplets, but deals with storing them for a given system.
215216
fn do_compute_for_system(&self, system: &mut System) -> Result<(), Error> {
216217
// let triplets_raw = TripletNeighborsList::for_system(&**system, self.bond_cutoff(), self.third_cutoff())?;
217218
// let triplets = triplets_raw.triplets();
218219
let triplets = list_raw_triplets(&mut **system, self.cutoffs[0], self.cutoffs[1])?;
219220

220-
//let species = system.species()?;
221-
222-
// let triplets = triplets.into_iter().map(|tr|{
223-
// if species[tr.atom_i] <= species[tr.atom_j] {
224-
// tr
225-
// } else {
226-
// BATripletInfo{
227-
// atom_i: tr.atom_j,
228-
// atom_j: tr.atom_i,
229-
// atom_k: tr.atom_k,
230-
// bond_i: tr.bond_i,
231-
// triplet_i: tr.triplet_i,
232-
// is_self_contrib: tr.is_self_contrib,
233-
// bond_vector: tr.bond_vector.map(|v|-v),
234-
// third_vector: tr.third_vector,
235-
// }
236-
// // BATriplet{
237-
// // bond_i: tr.bond_i,
238-
// // bond: Pair {
239-
// // first: tr.bond.second,
240-
// // second: tr.bond.first,
241-
// // distance: tr.bond.distance,
242-
// // vector: -tr.bond.vector,
243-
// // cell_shift_indices: {let c = &tr.bond.cell_shift_indices; [-c[0],-c[1],-c[2]]},
244-
// // },
245-
// // third: tr.third, third_vector: tr.third_vector,
246-
// // is_self_contribution: tr.is_self_contribution,
247-
// // distance: tr.distance,
248-
// // cell_shift_indices: {
249-
// // let (c1,c2) = (&tr.cell_shift_indices,&tr.bond.cell_shift_indices);
250-
// // [c1[0]-c2[0],c1[1]-c2[1],c1[2]-c2[2]]
251-
// // },
252-
// // }
253-
// }
254-
// }).collect::<Vec<_>>();
255-
256221
let components = [Labels::new(
257222
["vector_pair_component"],
258223
&[[0x00_i32],[0x01],[0x02], [0x10],[0x11],[0x12]],
@@ -332,7 +297,10 @@ impl BATripletNeighborList {
332297
Ok(())
333298
}
334299

300+
/// check that the precalculator has computed its values for a given system,
301+
/// and if not, compute them.
335302
pub fn ensure_computed_for_system(&self, system: &mut System) -> Result<(),Error> {
303+
self.validate_cutoffs();
336304
'cached_path: {
337305
let cutoffs2: &[f64;2] = match system.data(Self::CACHE_NAME_ATTR.into()) {
338306
Some(cutoff) => cutoff.downcast_ref()
@@ -349,6 +317,8 @@ impl BATripletNeighborList {
349317
return self.do_compute_for_system(system);
350318
}
351319

320+
/// for a given system, get a copy of all the bond-atom triplets.
321+
/// optionally include the vectors tied to these triplets
352322
pub fn get_for_system(&self, system: &System, with_vectors: bool) -> Result<Vec<BATripletInfo>, Error>{
353323
let block: &TensorBlock = system.data(&Self::CACHE_NAME1)
354324
.ok_or_else(||Error::Internal("triplets not yet computed".into()))?
@@ -375,6 +345,9 @@ impl BATripletNeighborList {
375345
Ok(res)
376346
}
377347

348+
/// for a given system, get a copy of the bond-atom triplets of given set of atomic species.
349+
/// optionally include the vectors tied to these triplets
350+
/// note: inverting s1 and s2 does not change the result, and the returned triplets may have these species swapped
378351
pub fn get_per_system_per_species(&self, system: &System, s1:i32,s2:i32,s3:i32, with_vectors: bool) -> Result<Vec<BATripletInfo>, Error>{
379352
let block: &TensorBlock = system.data(&Self::CACHE_NAME1)
380353
.ok_or_else(||Error::Internal("triplets not yet computed".into()))?
@@ -414,6 +387,9 @@ impl BATripletNeighborList {
414387
Ok(res)
415388
}
416389

390+
/// for a given system, get a copy of the bond-atom triplets of given set of atomic species.
391+
/// optionally include the vectors tied to these triplets
392+
/// note: the triplets may be for (c2,c1) rather than (c1,c2)
417393
pub fn get_per_system_per_center(&self, system: &System, c1:usize,c2:usize, with_vectors: bool) -> Result<Vec<BATripletInfo>, Error>{
418394
{
419395
let sz = system.size()?;

0 commit comments

Comments
 (0)