Skip to content

Commit 0e4fcab

Browse files
committed
Add more repair tests and improve implementation
1 parent f856cd8 commit 0e4fcab

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

pineappl/src/packed_array.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ impl<T: Copy + Default + PartialEq> PackedArray<T> {
9393
/// Clear array, if it is empty.
9494
///
9595
/// See https://github.com/NNPDF/pineappl/issues/338 and https://github.com/NNPDF/pineappl/commit/591cdcfa434ef7028dbbdc5f8da2ab83b273029c.
96-
/// Return value indicates, whether it was cleared
96+
/// Return value indicates, whether it was cleared (now)
9797
pub fn clear_if_empty(&mut self) -> bool {
98-
if self.indexed_iter().count() == 0 {
98+
// 1) are there actually elements?
99+
// 2) some of them might be default (and filtered away)
100+
if self.lengths.len() > 0 && self.indexed_iter().count() == 0 {
99101
self.clear();
100102
return true;
101103
}
@@ -1012,24 +1014,28 @@ mod tests {
10121014

10131015
#[test]
10141016
fn clear_if_empty() {
1017+
// create an empty array
10151018
let mut array = PackedArray::new(vec![40, 50, 50]);
1019+
let was_repaired = array.clear_if_empty();
1020+
assert!(array.is_empty());
1021+
assert!(!was_repaired);
10161022

10171023
// set something, which is not nothing
10181024
array[[0, 0, 0]] = 1;
10191025
assert!(!array.is_empty());
1020-
let must_be_false = array.clear_if_empty();
1026+
let was_repaired = array.clear_if_empty();
10211027
assert!(!array.is_empty());
1022-
assert!(!must_be_false);
1028+
assert!(!was_repaired);
10231029

10241030
// setting the default value does not clear the array on it's own ...
10251031
array[[0, 0, 0]] = 0;
10261032
assert!(!array.is_empty());
10271033
assert_eq!(array.indexed_iter().count(), 0);
10281034

10291035
// ... one needs to make that explicitly
1030-
let must_be_true = array.clear_if_empty();
1036+
let was_repaired = array.clear_if_empty();
10311037
assert!(array.is_empty());
1032-
assert!(must_be_true);
1038+
assert!(was_repaired);
10331039
assert_eq!(array.indexed_iter().count(), 0);
10341040
}
10351041

pineappl/src/subgrid.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,30 @@ mod tests {
712712
);
713713
}
714714

715+
#[test]
716+
fn import_subgrid_v1_repair() {
717+
let x = vec![
718+
0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0,
719+
];
720+
// create empty grid
721+
let mut grid1: SubgridEnum = ImportSubgridV1::new(
722+
PackedArray::new(vec![1, 10, 10]),
723+
vec![vec![0.0], x.clone(), x.clone()],
724+
)
725+
.into();
726+
assert_eq!(grid1.node_values(), vec![vec![0.0], x.clone(), x.clone()]);
727+
assert!(grid1.is_empty());
728+
// set a default value
729+
if let SubgridEnum::ImportSubgridV1(ref mut x) = grid1 {
730+
x.array[[0, 1, 2]] = 0.0;
731+
}
732+
assert!(!grid1.is_empty());
733+
// now fix
734+
let has_repaired = grid1.repair();
735+
assert!(grid1.is_empty());
736+
assert!(has_repaired);
737+
}
738+
715739
#[test]
716740
fn interp_subgrid_v1_fill_zero() {
717741
let interps = v0::default_interps(false, 2);
@@ -799,4 +823,20 @@ mod tests {
799823

800824
assert_eq!(subgrid.shape(), [23, 1, 1]);
801825
}
826+
827+
#[test]
828+
fn interp_subgrid_v1_repair() {
829+
let interps = v0::default_interps(false, 2);
830+
let mut subgrid = InterpSubgridV1::new(&interps);
831+
// Fill something
832+
subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 1.0);
833+
assert!(!subgrid.is_empty());
834+
// The multiply with 0 trick is still possible at f856cd871afc2ba44d5e19e658dba3f38893fcab
835+
subgrid.scale(0.);
836+
assert!(subgrid.is_empty());
837+
// now fix
838+
let has_repaired = subgrid.repair();
839+
assert!(subgrid.is_empty());
840+
assert!(!has_repaired);
841+
}
802842
}

0 commit comments

Comments
 (0)