Skip to content

Commit

Permalink
Fix assertion when a calculator does not return anything (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
liam-o-marsh authored Jun 11, 2024
1 parent 13c27e2 commit 22545b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rascaline/src/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ impl<'a> LabelsSelection<'a> {
G: FnOnce(&Labels) -> Result<Vec<Labels>, Error>,
H: Fn(TensorBlockRef<'_>) -> Labels,
{
assert_ne!(keys.count(), 0);
if keys.count() == 0 {
return Ok(vec![]);
}

match *self {
LabelsSelection::All => {
Expand Down Expand Up @@ -294,6 +296,7 @@ impl Calculator {
#[time_graph::instrument(name="Calculator::prepare")]
fn prepare(&mut self, systems: &mut [Box<dyn System>], options: CalculationOptions) -> Result<TensorMap, Error> {
let default_keys = self.implementation.keys(systems)?;

let keys = match options.selected_keys {
Some(keys) if keys.is_empty() => {
return Err(Error::InvalidParameter("selected keys can not be empty".into()));
Expand Down
17 changes: 17 additions & 0 deletions rascaline/src/calculators/neighbor_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,21 @@ mod tests {
));

}
#[test]
fn check_empty_response() {
let mut calculator = Calculator::from(Box::new(NeighborList {
cutoff: 0.1,
full_neighbor_list: true,
self_pairs: false,
}) as Box<dyn CalculatorBase>);
let mut systems = test_systems(&["water"]);

let descriptor = calculator.compute(&mut systems, Default::default()).unwrap();

// cutoff too low, TensorMap is empty!
assert_eq!(descriptor.keys(), &Labels::new::<i32,2>(
["first_atom_type", "second_atom_type"],
&[],
));
}
}

0 comments on commit 22545b4

Please sign in to comment.