Skip to content

Commit b60fbc5

Browse files
committed
Allow to add data to a System
1 parent 5326b6e commit b60fbc5

39 files changed

Lines changed: 237 additions & 191 deletions

rascaline-c-api/src/calculator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ pub unsafe extern fn rascal_calculator_compute(
402402
}
403403
check_pointers!(calculator, descriptor, systems);
404404

405-
// Create a Vec<Box<dyn System>> from the passed systems
405+
// Create a Vec<System> from the passed systems
406406
let c_systems = if systems_count == 0 {
407407
&mut []
408408
} else {
@@ -411,7 +411,7 @@ pub unsafe extern fn rascal_calculator_compute(
411411
};
412412
let mut systems = Vec::with_capacity(c_systems.len());
413413
for system in c_systems {
414-
systems.push(Box::new(system) as Box<dyn System>);
414+
systems.push(System::new(system));
415415
}
416416

417417
let c_gradients = if options.gradients_count == 0 {

rascaline-c-api/src/system.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ffi::CStr;
33

44
use rascaline::types::{Vector3D, Matrix3};
55
use rascaline::systems::{SimpleSystem, Pair, UnitCell};
6-
use rascaline::{Error, System};
6+
use rascaline::{Error, SystemBase};
77

88
use crate::RASCAL_SYSTEM_ERROR;
99

@@ -111,7 +111,7 @@ pub struct rascal_system_t {
111111
unsafe impl Send for rascal_system_t {}
112112
unsafe impl Sync for rascal_system_t {}
113113

114-
impl<'a> System for &'a mut rascal_system_t {
114+
impl<'a> SystemBase for &'a mut rascal_system_t {
115115
fn size(&self) -> Result<usize, Error> {
116116
let function = self.size.ok_or_else(|| Error::External {
117117
status: RASCAL_SYSTEM_ERROR,
@@ -442,7 +442,7 @@ pub unsafe extern fn rascal_basic_systems_read(
442442
catch_unwind(move || {
443443
check_pointers!(path, systems, count);
444444
let path = CStr::from_ptr(path).to_str()?;
445-
let simple_systems = rascaline::systems::read_from_file(path)?;
445+
let simple_systems = rascaline::systems::read_simple_systems_from_file(path)?;
446446

447447
let mut c_systems = Vec::with_capacity(simple_systems.len());
448448
for system in simple_systems {

rascaline/benches/lode-spherical-expansion.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
#![allow(clippy::needless_return)]
2-
use rascaline::{Calculator, System, CalculationOptions};
2+
use rascaline::{Calculator, CalculationOptions};
33

44
use criterion::{BenchmarkGroup, Criterion, measurement::WallTime, SamplingMode};
55
use criterion::{criterion_group, criterion_main};
66

7-
fn load_systems(path: &str) -> Vec<Box<dyn System>> {
8-
let systems = rascaline::systems::read_from_file(format!("benches/data/{}", path))
9-
.expect("failed to read file");
10-
11-
return systems.into_iter()
12-
.map(|s| Box::new(s) as Box<dyn System>)
13-
.collect()
14-
}
15-
167
fn run_spherical_expansion(mut group: BenchmarkGroup<WallTime>,
178
path: &str,
189
gradients: bool,
1910
test_mode: bool,
2011
) {
21-
let mut systems = load_systems(path);
12+
let mut systems = rascaline::systems::read_from_file(format!("benches/data/{}", path))
13+
.expect("failed to read file");
2214

2315
if test_mode {
2416
// Reduce the time/RAM required to test the benchmarks code.

rascaline/benches/soap-power-spectrum.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
#![allow(clippy::needless_return)]
22

3-
use rascaline::{Calculator, System, CalculationOptions};
3+
use rascaline::{Calculator, CalculationOptions};
44

55
use criterion::{BenchmarkGroup, Criterion, measurement::WallTime, SamplingMode};
66
use criterion::{criterion_group, criterion_main};
77

8-
9-
fn load_systems(path: &str) -> Vec<Box<dyn System>> {
10-
let systems = rascaline::systems::read_from_file(format!("benches/data/{}", path))
11-
.expect("failed to read file");
12-
13-
return systems.into_iter()
14-
.map(|s| Box::new(s) as Box<dyn System>)
15-
.collect()
16-
}
17-
188
fn run_soap_power_spectrum(
199
mut group: BenchmarkGroup<WallTime>,
2010
path: &str,
2111
gradients: bool,
2212
test_mode: bool,
2313
) {
24-
let mut systems = load_systems(path);
14+
let mut systems = rascaline::systems::read_from_file(format!("benches/data/{}", path))
15+
.expect("failed to read file");
2516

2617
if test_mode {
2718
// Reduce the time/RAM required to test the benchmarks code.

rascaline/benches/soap-spherical-expansion.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
#![allow(clippy::needless_return)]
2-
use rascaline::{Calculator, System, CalculationOptions};
2+
use rascaline::{Calculator, CalculationOptions};
33

44
use criterion::{BenchmarkGroup, Criterion, measurement::WallTime, SamplingMode};
55
use criterion::{criterion_group, criterion_main};
66

7-
fn load_systems(path: &str) -> Vec<Box<dyn System>> {
8-
let systems = rascaline::systems::read_from_file(format!("benches/data/{}", path))
9-
.expect("failed to read file");
10-
11-
return systems.into_iter()
12-
.map(|s| Box::new(s) as Box<dyn System>)
13-
.collect()
14-
}
15-
167
fn run_spherical_expansion(mut group: BenchmarkGroup<WallTime>,
178
path: &str,
189
gradients: bool,
1910
test_mode: bool,
2011
) {
21-
let mut systems = load_systems(path);
12+
let mut systems = rascaline::systems::read_from_file(format!("benches/data/{}", path))
13+
.expect("failed to read file");
2214

2315
if test_mode {
2416
// Reduce the time/RAM required to test the benchmarks code.

rascaline/examples/compute-soap.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use metatensor::Labels;
2-
use rascaline::{Calculator, System, CalculationOptions};
2+
use rascaline::{Calculator, CalculationOptions};
33

44
fn main() -> Result<(), Box<dyn std::error::Error>> {
55
// load the systems from command line argument
66
let path = std::env::args().nth(1).expect("expected a command line argument");
7-
let systems = rascaline::systems::read_from_file(path)?;
8-
// transform systems into a vector of trait objects (`Vec<Box<dyn System>>`)
9-
let mut systems = systems.into_iter()
10-
.map(|s| Box::new(s) as Box<dyn System>)
11-
.collect::<Vec<_>>();
7+
let mut systems = rascaline::systems::read_from_file(path)?;
128

139
// pass hyper-parameters as JSON
1410
let parameters = r#"{

rascaline/examples/profiling.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use metatensor::{TensorMap, Labels};
2-
use rascaline::{Calculator, System, CalculationOptions};
2+
use rascaline::{Calculator, CalculationOptions};
33

44
fn main() -> Result<(), Box<dyn std::error::Error>> {
55
let path = std::env::args().nth(1).expect("expected a command line argument");
@@ -28,10 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2828
/// Compute SOAP power spectrum, this is the same code as the 'compute-soap'
2929
/// example
3030
fn compute_soap(path: &str) -> Result<TensorMap, Box<dyn std::error::Error>> {
31-
let systems = rascaline::systems::read_from_file(path)?;
32-
let mut systems = systems.into_iter()
33-
.map(|s| Box::new(s) as Box<dyn System>)
34-
.collect::<Vec<_>>();
31+
let mut systems = rascaline::systems::read_from_file(path)?;
3532

3633
let parameters = r#"{
3734
"cutoff": 5.0,

rascaline/src/calculator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl Calculator {
285285
}
286286

287287
#[time_graph::instrument(name="Calculator::prepare")]
288-
fn prepare(&mut self, systems: &mut [Box<dyn System>], options: CalculationOptions) -> Result<TensorMap, Error> {
288+
fn prepare(&mut self, systems: &mut [System], options: CalculationOptions) -> Result<TensorMap, Error> {
289289
let default_keys = self.implementation.keys(systems)?;
290290

291291
let keys = match options.selected_keys {
@@ -503,14 +503,14 @@ impl Calculator {
503503
/// features.
504504
pub fn compute(
505505
&mut self,
506-
systems: &mut [Box<dyn System>],
506+
systems: &mut [System],
507507
options: CalculationOptions,
508508
) -> Result<TensorMap, Error> {
509509
let mut native_systems;
510510
let systems = if options.use_native_system {
511511
native_systems = Vec::with_capacity(systems.len());
512512
for system in systems {
513-
native_systems.push(Box::new(SimpleSystem::try_from(&**system)?) as Box<dyn System>);
513+
native_systems.push(System::new(SimpleSystem::try_from(&**system)?) as System);
514514
}
515515
&mut native_systems
516516
} else {

rascaline/src/calculators/atomic_composition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl CalculatorBase for AtomicComposition {
3636
&[]
3737
}
3838

39-
fn keys(&self, systems: &mut [Box<dyn System>]) -> Result<Labels, Error> {
39+
fn keys(&self, systems: &mut [System]) -> Result<Labels, Error> {
4040
return CenterTypesKeys.keys(systems);
4141
}
4242

@@ -48,7 +48,7 @@ impl CalculatorBase for AtomicComposition {
4848
return vec!["system", "atom"];
4949
}
5050

51-
fn samples(&self, keys: &Labels, systems: &mut [Box<dyn System>]) -> Result<Vec<Labels>, Error> {
51+
fn samples(&self, keys: &Labels, systems: &mut [System]) -> Result<Vec<Labels>, Error> {
5252
assert_eq!(keys.names(), ["center_type"]);
5353
let mut samples = Vec::new();
5454
for [center_type_key] in keys.iter_fixed_size() {
@@ -84,7 +84,7 @@ impl CalculatorBase for AtomicComposition {
8484
&self,
8585
keys: &Labels,
8686
_samples: &[Labels],
87-
_systems: &mut [Box<dyn System>],
87+
_systems: &mut [System],
8888
) -> Result<Vec<Labels>, Error> {
8989
// Positions/cell gradients of the composition are zero everywhere.
9090
// Therefore, we only return a vector of empty labels (one for each key).
@@ -110,7 +110,7 @@ impl CalculatorBase for AtomicComposition {
110110

111111
fn compute(
112112
&mut self,
113-
systems: &mut [Box<dyn System>],
113+
systems: &mut [System],
114114
descriptor: &mut TensorMap,
115115
) -> Result<(), Error> {
116116
assert_eq!(descriptor.keys().names(), ["center_type"]);

rascaline/src/calculators/dummy_calculator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ impl CalculatorBase for DummyCalculator {
4343
std::slice::from_ref(&self.cutoff)
4444
}
4545

46-
fn keys(&self, systems: &mut [Box<dyn System>]) -> Result<Labels, Error> {
46+
fn keys(&self, systems: &mut [System]) -> Result<Labels, Error> {
4747
return CenterTypesKeys.keys(systems);
4848
}
4949

5050
fn sample_names(&self) -> Vec<&str> {
5151
AtomCenteredSamples::sample_names()
5252
}
5353

54-
fn samples(&self, keys: &Labels, systems: &mut [Box<dyn System>]) -> Result<Vec<Labels>, Error> {
54+
fn samples(&self, keys: &Labels, systems: &mut [System]) -> Result<Vec<Labels>, Error> {
5555
assert_eq!(keys.names(), ["center_type"]);
5656
let mut samples = Vec::new();
5757
for [center_type] in keys.iter_fixed_size() {
@@ -75,7 +75,7 @@ impl CalculatorBase for DummyCalculator {
7575
}
7676
}
7777

78-
fn positions_gradient_samples(&self, keys: &Labels, samples: &[Labels], systems: &mut [Box<dyn System>]) -> Result<Vec<Labels>, Error> {
78+
fn positions_gradient_samples(&self, keys: &Labels, samples: &[Labels], systems: &mut [System]) -> Result<Vec<Labels>, Error> {
7979
debug_assert_eq!(keys.count(), samples.len());
8080
let mut gradient_samples = Vec::new();
8181
for ([center_type], samples) in keys.iter_fixed_size().zip(samples) {
@@ -110,7 +110,7 @@ impl CalculatorBase for DummyCalculator {
110110
}
111111

112112
#[time_graph::instrument(name = "DummyCalculator::compute")]
113-
fn compute(&mut self, systems: &mut [Box<dyn System>], descriptor: &mut TensorMap) -> Result<(), Error> {
113+
fn compute(&mut self, systems: &mut [System], descriptor: &mut TensorMap) -> Result<(), Error> {
114114
if self.name.contains("log-test-info:") {
115115
info!("{}", self.name);
116116
} else if self.name.contains("log-test-warn:") {

0 commit comments

Comments
 (0)