From f32902afa8348525d3994ee968a0765c52675deb Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 12 Jul 2024 16:40:23 +0200 Subject: [PATCH 001/213] i32, i32 -> Vec --- pineappl/src/boc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 62599b09..c0a1794d 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -264,7 +264,7 @@ impl Order { /// combination. #[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)] pub struct Channel { - entry: Vec<(i32, i32, f64)>, + entry: Vec<(Vec, f64)>, } impl Channel { @@ -306,7 +306,7 @@ impl Channel { /// let _ = Channel::new(vec![]); /// ``` #[must_use] - pub fn new(mut entry: Vec<(i32, i32, f64)>) -> Self { + pub fn new(mut entry: Vec<(Vec, f64)>) -> Self { assert!(!entry.is_empty()); // sort `entry` because the ordering doesn't matter and because it makes it easier to @@ -373,7 +373,7 @@ impl Channel { /// assert_eq!(entry.entry(), [(2, 2, 1.0), (4, 4, 1.0)]); /// ``` #[must_use] - pub fn entry(&self) -> &[(i32, i32, f64)] { + pub fn entry(&self) -> &[(Vec, f64)] { &self.entry } From 2417c9287b6ab12f3cde9ebd9787e5acb6778a69 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 18 Jul 2024 16:45:08 +0200 Subject: [PATCH 002/213] Migrate `Channel` methods to new definition --- pineappl/src/boc.rs | 129 ++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 66 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index c0a1794d..9965459d 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -311,22 +311,22 @@ impl Channel { // sort `entry` because the ordering doesn't matter and because it makes it easier to // compare `Channel` objects with each other - entry.sort_by(|x, y| (x.0, x.1).cmp(&(y.0, y.1))); + entry.sort_by(|x, y| x.0.cmp(&y.0)); Self { entry: entry .into_iter() .coalesce(|lhs, rhs| { // sum the factors of repeated elements - if (lhs.0, lhs.1) == (rhs.0, rhs.1) { - Ok((lhs.0, lhs.1, lhs.2 + rhs.2)) + if lhs.0 == rhs.0 { + Ok((lhs.0, lhs.1 + rhs.1)) } else { Err((lhs, rhs)) } }) // filter zeros // TODO: find a better than to hardcode the epsilon limit - .filter(|&(_, _, f)| !approx_eq!(f64, f.abs(), 0.0, epsilon = 1e-14)) + .filter(|&(_, f)| !approx_eq!(f64, f.abs(), 0.0, epsilon = 1e-14)) .collect(), } } @@ -347,17 +347,22 @@ impl Channel { /// assert_eq!(entry, channel![2, 11, 1.0; -2, 11, -1.0; 1, 11, -1.0; -1, 11, 1.0]); /// ``` pub fn translate(entry: &Self, translator: &dyn Fn(i32) -> Vec<(i32, f64)>) -> Self { - let mut tuples = Vec::new(); + let mut result = Vec::new(); - for &(a, b, factor) in &entry.entry { - for (aid, af) in translator(a) { - for (bid, bf) in translator(b) { - tuples.push((aid, bid, factor * af * bf)); - } + for &(pids, factor) in &entry.entry { + for tuples in pids + .iter() + .map(|&pid| translator(pid)) + .multi_cartesian_product() + { + result.push(( + tuples.iter().map(|&(pid, _)| pid).collect(), + tuples.iter().map(|(_, f)| f).product::(), + )); } } - Self::new(tuples) + Self::new(result) } /// Returns a tuple representation of this entry. @@ -377,11 +382,11 @@ impl Channel { &self.entry } - /// Creates a new object with the initial states transposed. - #[must_use] - pub fn transpose(&self) -> Self { - Self::new(self.entry.iter().map(|(a, b, c)| (*b, *a, *c)).collect()) - } + // /// Creates a new object with the initial states transposed. + // #[must_use] + // pub fn transpose(&self) -> Self { + // Self::new(self.entry.iter().map(|(a, b, c)| (*b, *a, *c)).collect()) + // } /// If `other` is the same channel when only comparing PIDs and neglecting the factors, return /// the number `f1 / f2`, where `f1` is the factor from `self` and `f2` is the factor from @@ -392,10 +397,10 @@ impl Channel { /// ```rust /// use pineappl::boc::Channel; /// - /// let entry1 = Channel::new(vec![(2, 2, 2.0), (4, 4, 2.0)]); - /// let entry2 = Channel::new(vec![(4, 4, 1.0), (2, 2, 1.0)]); - /// let entry3 = Channel::new(vec![(3, 4, 1.0), (2, 2, 1.0)]); - /// let entry4 = Channel::new(vec![(4, 3, 1.0), (2, 3, 2.0)]); + /// let entry1 = Channel::new(vec![(vec![2, 2], 2.0), (vec![4, 4], 2.0)]); + /// let entry2 = Channel::new(vec![(vec![4, 4], 1.0), (vec![2, 2], 1.0)]); + /// let entry3 = Channel::new(vec![(vec![3, 4], 1.0), (vec![2, 2], 1.0)]); + /// let entry4 = Channel::new(vec![(vec![4, 3], 1.0), (vec![2, 3], 2.0)]); /// /// assert_eq!(entry1.common_factor(&entry2), Some(2.0)); /// assert_eq!(entry1.common_factor(&entry3), None); @@ -411,7 +416,7 @@ impl Channel { .entry .iter() .zip(&other.entry) - .map(|(a, b)| ((a.0 == b.0) && (a.1 == b.1)).then_some(a.2 / b.2)) + .map(|(a, b)| (a == b).then_some(a.1 / b.1)) .collect(); result.and_then(|factors| { @@ -436,51 +441,43 @@ impl FromStr for Channel { type Err = ParseChannelError; fn from_str(s: &str) -> Result { - Ok(Self::new( - s.split('+') - .map(|sub| { - sub.split_once('*').map_or_else( - || Err(ParseChannelError(format!("missing '*' in '{sub}'"))), - |(factor, pids)| { - let tuple = pids.split_once(',').map_or_else( - || Err(ParseChannelError(format!("missing ',' in '{pids}'"))), - |(a, b)| { - Ok(( - a.trim() - .strip_prefix('(') - .ok_or_else(|| { - ParseChannelError(format!( - "missing '(' in '{pids}'" - )) - })? - .trim() - .parse::() - .map_err(|err| ParseChannelError(err.to_string()))?, - b.trim() - .strip_suffix(')') - .ok_or_else(|| { - ParseChannelError(format!( - "missing ')' in '{pids}'" - )) - })? - .trim() - .parse::() - .map_err(|err| ParseChannelError(err.to_string()))?, - )) - }, - )?; - - Ok(( - tuple.0, - tuple.1, - str::parse::(factor.trim()) - .map_err(|err| ParseChannelError(err.to_string()))?, - )) - }, - ) - }) - .collect::>()?, - )) + let result: Vec<_> = s + .split('+') + .map(|sub| { + sub.split_once('*').map_or_else( + || Err(ParseChannelError(format!("missing '*' in '{sub}'"))), + |(factor, pids)| { + let vector: Vec<_> = pids + .strip_prefix('(') + .ok_or_else(|| ParseChannelError(format!("missing '(' in '{pids}'")))? + .strip_suffix(')') + .ok_or_else(|| ParseChannelError(format!("missing ')' in '{pids}'")))? + .split(',') + .map(|pid| { + Ok(pid + .trim() + .parse::() + .map_err(|err| ParseChannelError(err.to_string()))?) + }) + .collect::>()?; + + Ok(( + vector, + str::parse::(factor.trim()) + .map_err(|err| ParseChannelError(err.to_string()))?, + )) + }, + ) + }) + .collect::>()?; + + if !result.iter().map(|(pids, _)| pids.len()).all_equal() { + return Err(ParseChannelError(format!( + "PID tuples have different lengths" + ))); + } + + Ok(Self::new(result)) } } From f8da72ef5511c30f23de14d7d99c5b336fb40dce Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 22 Jul 2024 20:49:45 +0200 Subject: [PATCH 003/213] update gluon_has_pid_zero to check vector of PIDs --- pineappl/src/evolution.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index cefcc209..5167bbb7 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -166,7 +166,7 @@ fn gluon_has_pid_zero(grid: &Grid) -> bool { // if there are any PID zero particles ... grid.channels() .iter() - .any(|entry| entry.entry().iter().any(|&(a, b, _)| (a == 0) || (b == 0))) + .any(|entry| entry.entry().iter().any(|&(ref pids, _)| pids.iter().any(|&pid| pid == 0))) // and if the particle IDs are encoded using PDG MC IDs && grid.pid_basis() == PidBasis::Pdg } From 03f2dc65c589a1668200923ffd20bfe543e32a86 Mon Sep 17 00:00:00 2001 From: t7phy Date: Wed, 24 Jul 2024 16:55:57 +0200 Subject: [PATCH 004/213] Fix remaining compilation errors --- pineappl/src/boc.rs | 29 +++++++++----- pineappl/src/evolution.rs | 66 ++++++++++++++----------------- pineappl/src/fk_table.rs | 6 +-- pineappl/src/grid.rs | 82 +++++++++++++++++++++++++-------------- pineappl/src/pids.rs | 6 +-- pineappl_capi/src/lib.rs | 6 +-- pineappl_cli/src/plot.rs | 7 ++-- pineappl_cli/src/read.rs | 4 +- pineappl_cli/src/write.rs | 32 ++++++--------- 9 files changed, 129 insertions(+), 109 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 9965459d..b71f9a95 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -339,17 +339,17 @@ impl Channel { /// use pineappl::boc::Channel; /// use pineappl::channel; /// - /// let entry = Channel::translate(&channel![103, 11, 1.0], &|evol_id| match evol_id { + /// let entry = Channel::translate(&channel![103, 11, 10], &|evol_id| match evol_id { /// 103 => vec![(2, 1.0), (-2, -1.0), (1, -1.0), (-1, 1.0)], /// _ => vec![(evol_id, 1.0)], /// }); /// - /// assert_eq!(entry, channel![2, 11, 1.0; -2, 11, -1.0; 1, 11, -1.0; -1, 11, 1.0]); + /// assert_eq!(entry, channel![2, 11, 10.0; -2, 11, -10.0; 1, 11, -10.0; -1, 11, 10.0]); /// ``` pub fn translate(entry: &Self, translator: &dyn Fn(i32) -> Vec<(i32, f64)>) -> Self { let mut result = Vec::new(); - for &(pids, factor) in &entry.entry { + for (pids, factor) in &entry.entry { for tuples in pids .iter() .map(|&pid| translator(pid)) @@ -357,7 +357,7 @@ impl Channel { { result.push(( tuples.iter().map(|&(pid, _)| pid).collect(), - tuples.iter().map(|(_, f)| f).product::(), + tuples.iter().map(|(_, f)| factor * f).product::(), )); } } @@ -382,11 +382,20 @@ impl Channel { &self.entry } - // /// Creates a new object with the initial states transposed. - // #[must_use] - // pub fn transpose(&self) -> Self { - // Self::new(self.entry.iter().map(|(a, b, c)| (*b, *a, *c)).collect()) - // } + /// Create a new object with the PIDs at index `i` and `j` transposed. + #[must_use] + pub fn transpose(&self, i: usize, j: usize) -> Self { + Self::new( + self.entry + .iter() + .map(|(pids, c)| { + let mut transposed = pids.clone(); + transposed.swap(i, j); + (transposed, *c) + }) + .collect(), + ) + } /// If `other` is the same channel when only comparing PIDs and neglecting the factors, return /// the number `f1 / f2`, where `f1` is the factor from `self` and `f2` is the factor from @@ -498,7 +507,7 @@ impl FromStr for Channel { #[macro_export] macro_rules! channel { ($a:expr, $b:expr, $factor:expr $(; $c:expr, $d:expr, $fac:expr)*) => { - $crate::boc::Channel::new(vec![($a, $b, $factor), $(($c, $d, $fac)),*]) + $crate::boc::Channel::new(vec![(vec![$a, $b], $factor), $((vec![$c, $d], $fac)),*]) }; } diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 5167bbb7..1bb7433e 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -386,6 +386,7 @@ fn ndarray_from_subgrid_orders_slice( Ok((vec![x1_a, x1_b], (!zero).then_some(array))) } +// TODO: merge this method into evolve_slice_with_two pub(crate) fn evolve_slice_with_one( grid: &Grid, operator: &ArrayView4, @@ -395,18 +396,22 @@ pub(crate) fn evolve_slice_with_one( alphas_table: &AlphasTable, ) -> Result<(Array3, Vec), GridError> { let gluon_has_pid_zero = gluon_has_pid_zero(grid); - let has_pdf1 = grid.convolutions()[0] != Convolution::None; - + // UNWRAP: there must be exactly one convolution that's not None + let index = grid + .convolutions() + .iter() + .position(|c| *c != Convolution::None) + .unwrap(); let (pid_indices, pids) = pid_slices(operator, info, gluon_has_pid_zero, &|pid| { grid.channels() .iter() .flat_map(Channel::entry) - .any(|&(a, b, _)| if has_pdf1 { a } else { b } == pid) + .any(|(pids, _)| pids[index] == pid) })?; let channels0 = channels0_with_one(&pids); let mut sub_fk_tables = Vec::with_capacity(grid.bin_info().bins() * channels0.len()); - let new_axis = if has_pdf1 { 2 } else { 1 }; + let new_axis = 2 - index; let mut last_x1 = Vec::new(); let mut ops = Vec::new(); @@ -429,7 +434,7 @@ pub(crate) fn evolve_slice_with_one( continue; }; - let x1 = if has_pdf1 { x1.remove(0) } else { x1.remove(1) }; + let x1 = x1.remove(index); if x1.is_empty() { continue; @@ -445,12 +450,7 @@ pub(crate) fn evolve_slice_with_one( last_x1 = x1; } - for (&pid1, &factor) in - channel1 - .entry() - .iter() - .map(|(a, b, f)| if has_pdf1 { (a, f) } else { (b, f) }) - { + for (pid1, &factor) in channel1.entry().iter().map(|(pids, f)| (pids[index], f)) { for (fk_table, op) in channels0 .iter() @@ -485,18 +485,22 @@ pub(crate) fn evolve_slice_with_one( ren: info.fac0, fac: info.fac0, }], - if has_pdf1 { info.x0.clone() } else { vec![1.0] }, - if has_pdf1 { vec![1.0] } else { info.x0.clone() }, + if index == 0 { + info.x0.clone() + } else { + vec![1.0] + }, + if index == 0 { + vec![1.0] + } else { + info.x0.clone() + }, ) .into() })); } - let pid = if grid.convolutions()[0] == Convolution::None { - grid.channels()[0].entry()[0].0 - } else { - grid.channels()[0].entry()[0].1 - }; + let pid = grid.channels()[0].entry()[0].0[index]; Ok(( Array1::from_iter(sub_fk_tables) @@ -506,8 +510,8 @@ pub(crate) fn evolve_slice_with_one( .iter() .map(|&a| { channel![ - if has_pdf1 { a } else { pid }, - if has_pdf1 { pid } else { a }, + if index == 0 { a } else { pid }, + if index == 0 { pid } else { a }, 1.0 ] }) @@ -532,12 +536,7 @@ pub(crate) fn evolve_slice_with_two( grid.channels() .iter() .flat_map(Channel::entry) - .any(|tuple| match d { - // TODO: `Channel::entry` should return a tuple of a `Vec` and an `f64` - 0 => tuple.0 == pid1, - 1 => tuple.1 == pid1, - _ => unreachable!(), - }) + .any(|(pids, _)| pids[d] == pid1) }) }) .collect::, _>>()? @@ -596,7 +595,7 @@ pub(crate) fn evolve_slice_with_two( for (pids1, factor) in channel1 .entry() .iter() - .map(|&(pida1, pidb1, factor)| ([pida1, pidb1], factor)) + .map(|(pids1, factor)| ([pids1[0], pids1[1]], factor)) { for (fk_table, ops) in channels0 @@ -615,7 +614,7 @@ pub(crate) fn evolve_slice_with_two( }) { linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, &mut tmp); - linalg::general_mat_mul(factor, ops[0], &tmp, 1.0, fk_table); + linalg::general_mat_mul(*factor, ops[0], &tmp, 1.0, fk_table); } } } @@ -675,12 +674,7 @@ pub(crate) fn evolve_slice_with_two2( grid.channels() .iter() .flat_map(Channel::entry) - .any(|tuple| match d { - // TODO: `Channel::entry` should return a tuple of a `Vec` and an `f64` - 0 => tuple.0 == pid1, - 1 => tuple.1 == pid1, - _ => unreachable!(), - }) + .any(|(pids, _)| pids[d] == pid1) }) }) .collect::, _>>()? @@ -747,7 +741,7 @@ pub(crate) fn evolve_slice_with_two2( for (pids1, factor) in channel1 .entry() .iter() - .map(|&(pida1, pidb1, factor)| ([pida1, pidb1], factor)) + .map(|(pids1, factor)| ([pids1[0], pids1[1]], factor)) { for (fk_table, ops) in channels0 @@ -768,7 +762,7 @@ pub(crate) fn evolve_slice_with_two2( // tmp = array * ops[1]^T linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, &mut tmp); // fk_table += factor * ops[0] * tmp - linalg::general_mat_mul(factor, ops[0], &tmp, 1.0, fk_table); + linalg::general_mat_mul(*factor, ops[0], &tmp, 1.0, fk_table); } } } diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index b7d04519..6b3bcb12 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -226,11 +226,11 @@ impl FkTable { /// Return the channel definition for this `FkTable`. All factors are `1.0`. #[must_use] - pub fn channels(&self) -> Vec<(i32, i32)> { + pub fn channels(&self) -> Vec> { self.grid .channels() .iter() - .map(|entry| (entry.entry()[0].0, entry.entry()[0].1)) + .map(|entry| entry.entry()[0].0.clone()) .collect() } @@ -390,7 +390,7 @@ impl TryFrom for FkTable { for channel in grid.channels() { let entry = channel.entry(); - if entry.len() != 1 || entry[0].2 != 1.0 { + if entry.len() != 1 || entry[0].1 != 1.0 { return Err(TryFromGridError::InvalidChannel); } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 86e744b9..18c36847 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -388,9 +388,10 @@ impl Grid { let mut lumi = 0.0; for entry in channel.entry() { - let xfx1 = lumi_cache.xfx1(entry.0, ix1, imu2); - let xfx2 = lumi_cache.xfx2(entry.1, ix2, imu2); - lumi += xfx1 * xfx2 * entry.2 / (x1 * x2); + debug_assert_eq!(entry.0.len(), 2); + let xfx1 = lumi_cache.xfx1(entry.0[0], ix1, imu2); + let xfx2 = lumi_cache.xfx2(entry.0[1], ix2, imu2); + lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); } let alphas = lumi_cache.alphas(imu2); @@ -454,9 +455,10 @@ impl Grid { let mut lumi = 0.0; for entry in channel.entry() { - let xfx1 = lumi_cache.xfx1(entry.0, ix1, imu2); - let xfx2 = lumi_cache.xfx2(entry.1, ix2, imu2); - lumi += xfx1 * xfx2 * entry.2 / (x1 * x2); + debug_assert_eq!(entry.0.len(), 2); + let xfx1 = lumi_cache.xfx1(entry.0[0], ix1, imu2); + let xfx2 = lumi_cache.xfx2(entry.0[1], ix2, imu2); + lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); } let alphas = lumi_cache.alphas(imu2); @@ -804,11 +806,7 @@ impl Grid { { Some(Ok(pid)) => { let condition = !self.channels().iter().all(|entry| { - entry.entry().iter().all(|&channels| match index { - 1 => channels.0 == pid, - 2 => channels.1 == pid, - _ => unreachable!(), - }) + entry.entry().iter().all(|(pids, _)| pids[index] == pid) }); if condition { @@ -1147,7 +1145,7 @@ impl Grid { // only keep channels that have non-zero factors and for which at least one subgrid is // non-empty for (channel, entry) in self.channels.iter().enumerate() { - if !entry.entry().iter().all(|&(_, _, factor)| factor == 0.0) + if !entry.entry().iter().all(|&(_, factor)| factor == 0.0) && !self .subgrids .slice(s![.., .., channel]) @@ -1196,6 +1194,8 @@ impl Grid { fn symmetrize_channels(&mut self) { let convolutions = self.convolutions(); + // TODO: generalize this method to n convolutions + assert_eq!(convolutions.len(), 2); if convolutions[0] != convolutions[1] { return; } @@ -1205,7 +1205,7 @@ impl Grid { while let Some(index) = indices.pop() { let channel_entry = &self.channels[index]; - if *channel_entry == channel_entry.transpose() { + if *channel_entry == channel_entry.transpose(0, 1) { // check if in all cases the limits are compatible with merging self.subgrids .slice_mut(s![.., .., index]) @@ -1218,7 +1218,7 @@ impl Grid { } else if let Some((j, &other_index)) = indices .iter() .enumerate() - .find(|(_, i)| self.channels[**i] == channel_entry.transpose()) + .find(|(_, i)| self.channels[**i] == channel_entry.transpose(0, 1)) { indices.remove(j); @@ -1289,6 +1289,9 @@ impl Grid { pub fn evolve_info(&self, order_mask: &[bool]) -> EvolveInfo { use super::evolution::EVOLVE_INFO_TOL_ULPS; + // TODO: generalize this method to n convolutions and different EKOs + assert_eq!(self.convolutions().len(), 2); + let has_pdf1 = self.convolutions()[0] != Convolution::None; let has_pdf2 = self.convolutions()[1] != Convolution::None; @@ -1324,10 +1327,20 @@ impl Grid { x1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); if has_pdf1 { - pids1.extend(self.channels()[channel].entry().iter().map(|(a, _, _)| a)); + pids1.extend( + self.channels()[channel] + .entry() + .iter() + .map(|(pids, _)| pids[0]), + ); } if has_pdf2 { - pids1.extend(self.channels()[channel].entry().iter().map(|(_, b, _)| b)); + pids1.extend( + self.channels()[channel] + .entry() + .iter() + .map(|(pids, _)| pids[1]), + ); } pids1.sort_unstable(); @@ -1753,6 +1766,9 @@ impl Grid { } pub(crate) fn rewrite_channels(&mut self, add: &[(i32, i32)], del: &[i32]) { + // TODO: generalize this method to n convolutions + assert_eq!(self.convolutions().len(), 2); + self.channels = self .channels() .iter() @@ -1761,21 +1777,29 @@ impl Grid { entry .entry() .iter() - .map(|(a, b, f)| { + .map(|(pids, f)| { ( - // if `a` is to be added to another pid replace it with this pid - add.iter().fold( - *a, - |id, &(source, target)| if id == source { target } else { id }, - ), - // if `b` is to be added to another pid replace it with this pid - add.iter().fold( - *b, - |id, &(source, target)| if id == source { target } else { id }, - ), + vec![ + // if `a` is to be added to another pid replace it with this pid + add.iter().fold(pids[0], |id, &(source, target)| { + if id == source { + target + } else { + id + } + }), + // if `b` is to be added to another pid replace it with this pid + add.iter().fold(pids[1], |id, &(source, target)| { + if id == source { + target + } else { + id + } + }), + ], // if any of the pids `a` or `b` are to b deleted set the factor to // zero - if del.iter().any(|id| id == a || id == b) { + if del.iter().any(|&id| id == pids[0] || id == pids[1]) { 0.0 } else { *f @@ -1805,7 +1829,7 @@ impl Grid { entry .entry() .iter() - .copied() + .cloned() .map(move |entry| Channel::new(vec![entry])) }) .collect(); diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index 8e23eaa4..fca5a5b3 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -894,9 +894,9 @@ mod tests { ); assert_eq!(result.entry().len(), 1); - assert_eq!(result.entry()[0].0, pid); - assert_eq!(result.entry()[0].1, pid); - assert_approx_eq!(f64, result.entry()[0].2, 1.0, ulps = 8); + assert_eq!(result.entry()[0].0[0], pid); + assert_eq!(result.entry()[0].0[1], pid); + assert_approx_eq!(f64, result.entry()[0].1, 1.0, ulps = 8); } } } diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 38c45d2d..cab56b19 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -1017,7 +1017,7 @@ pub unsafe extern "C" fn pineappl_lumi_add( pdg_id_pairs .chunks(2) .zip(factors) - .map(|x| (x.0[0], x.0[1], x.1)) + .map(|x| (vec![x.0[0], x.0[1]], x.1)) .collect(), )); } @@ -1076,12 +1076,12 @@ pub unsafe extern "C" fn pineappl_lumi_entry( entry .iter() - .flat_map(|(id1, id2, _)| vec![id1, id2]) + .flat_map(|(pids, _)| pids) .zip(pdg_ids.iter_mut()) .for_each(|(from, to)| *to = *from); entry .iter() - .map(|(_, _, factor)| factor) + .map(|(_, factor)| factor) .zip(factors.iter_mut()) .for_each(|(from, to)| *to = *from); } diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index 87acbe1b..ea948508 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -104,15 +104,16 @@ fn map_format_parton(parton: i32) -> &'static str { } } +// TODO: generalize this function to n convolutions fn map_format_channel(channel: &Channel, has_pdf1: bool, has_pdf2: bool) -> String { channel .entry() .iter() - .map(|&(a, b, _)| { + .map(|(pids, _)| { format!( "{}{}", - if has_pdf1 { map_format_parton(a) } else { "" }, - if has_pdf2 { map_format_parton(b) } else { "" } + if has_pdf1 { map_format_parton(pids[0]) } else { "" }, + if has_pdf2 { map_format_parton(pids[1]) } else { "" } ) }) .join(" + ") diff --git a/pineappl_cli/src/read.rs b/pineappl_cli/src/read.rs index b08e4e15..33230c7d 100644 --- a/pineappl_cli/src/read.rs +++ b/pineappl_cli/src/read.rs @@ -124,8 +124,8 @@ impl Subcommand for Opts { row.add_cell(cell!(format!("{index}"))); - for (id1, id2, factor) in channel.entry() { - row.add_cell(cell!(format!("{factor} \u{d7} ({id1:2}, {id2:2})"))); + for (pids, factor) in channel.entry() { + row.add_cell(cell!(format!("{factor} \u{d7} ({})", pids.iter().map(ToString::to_string).join(", ")))); } } } else if self.group.ew || self.group.qcd { diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index 3de51c3b..522cf53f 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -494,9 +494,13 @@ impl Subcommand for Opts { for arg in &self.more_args.args { match arg { + // TODO: generalize to arbitrary convolutions OpsArg::Cc1(true) | OpsArg::Cc2(true) => { - let cc1 = matches!(arg, OpsArg::Cc1(true)); - let cc2 = matches!(arg, OpsArg::Cc2(true)); + let index = match arg { + OpsArg::Cc1(true) => 0, + OpsArg::Cc2(true) => 1, + _ => unreachable!(), + }; let pid_basis = grid.pid_basis(); @@ -505,29 +509,17 @@ impl Subcommand for Opts { channel .entry() .iter() - .map(|&(a, b, f)| { - let (ap, f1) = if cc1 { - pid_basis.charge_conjugate(a) - } else { - (a, 1.0) - }; - let (bp, f2) = if cc2 { - pid_basis.charge_conjugate(b) - } else { - (b, 1.0) - }; - (ap, bp, f * f1 * f2) + .map(|(pids, f)| { + let mut cc_pids = pids.clone(); + let (cc_pid, f1) = pid_basis.charge_conjugate(pids[index]); + cc_pids[index] = cc_pid; + (cc_pids, f * f1) }) .collect(), ); } - if cc1 { - grid.set_convolution(0, grid.convolutions()[0].charge_conjugate()); - } - if cc2 { - grid.set_convolution(1, grid.convolutions()[1].charge_conjugate()); - } + grid.set_convolution(index, grid.convolutions()[index].charge_conjugate()); } OpsArg::DedupChannels(ulps) => { grid.dedup_channels(*ulps); From 5e281aefc3fd5ea1015ae187b036937dc5bf17c0 Mon Sep 17 00:00:00 2001 From: t7phy Date: Wed, 24 Jul 2024 17:25:21 +0200 Subject: [PATCH 005/213] Start implementation of a fragmentation scale --- pineappl/src/boc.rs | 17 +++++++++++++---- pineappl/src/fk_table.rs | 1 + pineappl/src/grid.rs | 4 ++-- pineappl_capi/src/lib.rs | 1 + pineappl_cli/src/export.rs | 2 ++ pineappl_cli/src/plot.rs | 12 ++++++++++-- pineappl_cli/src/read.rs | 6 +++++- 7 files changed, 34 insertions(+), 9 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index b71f9a95..cdc0cfcc 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -24,8 +24,10 @@ pub struct Order { pub alpha: u32, /// Exponent of the logarithm of the scale factor of the renomalization scale. pub logxir: u32, - /// Exponent of the logarithm of the scale factor of the factorization scale. + /// Exponent of the logarithm of the scale factor of the initial state factorization scale. pub logxif: u32, + /// Exponent of the logarithm of the scale factor of the final state factorization scale (fragmentation scale). + pub logxia: u32, } impl FromStr for Order { @@ -37,6 +39,7 @@ impl FromStr for Order { alpha: 0, logxir: 0, logxif: 0, + logxia: 0, }; for tuple in s @@ -61,6 +64,9 @@ impl FromStr for Order { ("lf", Ok(num)) => { result.logxif = num; } + ("la", Ok(num)) => { + result.logxia = num; + } (label, Err(err)) => { return Err(ParseOrderError(format!( "error while parsing exponent of '{label}': {err}" @@ -82,10 +88,11 @@ impl Ord for Order { // rest lexicographically (self.alphas + self.alpha) .cmp(&(other.alphas + other.alpha)) - .then((self.alpha, self.logxir, self.logxif).cmp(&( + .then((self.alpha, self.logxir, self.logxif, self.logxia).cmp(&( other.alpha, other.logxir, other.logxif, + other.logxia, ))) } } @@ -100,12 +107,13 @@ impl Order { /// Constructor. This function mainly exists to have a way of constructing `Order` that is less /// verbose. #[must_use] - pub const fn new(alphas: u32, alpha: u32, logxir: u32, logxif: u32) -> Self { + pub const fn new(alphas: u32, alpha: u32, logxir: u32, logxif: u32, logxia: u32) -> Self { Self { alphas, alpha, logxir, logxif, + logxia, } } @@ -238,8 +246,9 @@ impl Order { alpha, logxir, logxif, + logxia, }| { - if !logs && (logxir > 0 || logxif > 0) { + if !logs && (logxir > 0 || logxif > 0 || logxia > 0) { return false; } diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 6b3bcb12..056919c3 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -364,6 +364,7 @@ impl TryFrom for FkTable { alpha: 0, logxir: 0, logxif: 0, + logxia: 0, }] { return Err(TryFromGridError::NonTrivialOrder); diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 18c36847..998e8d27 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1458,7 +1458,7 @@ impl Grid { subgrids, channels, bin_limits: self.bin_limits.clone(), - orders: vec![Order::new(0, 0, 0, 0)], + orders: vec![Order::new(0, 0, 0, 0, 0)], subgrid_params: SubgridParams::default(), more_members: self.more_members.clone(), }; @@ -1594,7 +1594,7 @@ impl Grid { subgrids, channels, bin_limits: self.bin_limits.clone(), - orders: vec![Order::new(0, 0, 0, 0)], + orders: vec![Order::new(0, 0, 0, 0, 0)], subgrid_params: SubgridParams::default(), more_members: self.more_members.clone(), }; diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index cab56b19..9707d157 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -685,6 +685,7 @@ pub unsafe extern "C" fn pineappl_grid_new( alpha: s[1], logxir: s[2], logxif: s[3], + logxia: s[4], }) .collect(); diff --git a/pineappl_cli/src/export.rs b/pineappl_cli/src/export.rs index e4ee819d..124ad83a 100644 --- a/pineappl_cli/src/export.rs +++ b/pineappl_cli/src/export.rs @@ -118,6 +118,7 @@ impl Subcommand for Opts { alpha, logxir, logxif, + logxia, } in grid .orders() .iter() @@ -138,6 +139,7 @@ impl Subcommand for Opts { alpha, logxir, logxif, + logxia, }, keep, )| { diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index ea948508..657e98e0 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -112,8 +112,16 @@ fn map_format_channel(channel: &Channel, has_pdf1: bool, has_pdf2: bool) -> Stri .map(|(pids, _)| { format!( "{}{}", - if has_pdf1 { map_format_parton(pids[0]) } else { "" }, - if has_pdf2 { map_format_parton(pids[1]) } else { "" } + if has_pdf1 { + map_format_parton(pids[0]) + } else { + "" + }, + if has_pdf2 { + map_format_parton(pids[1]) + } else { + "" + } ) }) .join(" + ") diff --git a/pineappl_cli/src/read.rs b/pineappl_cli/src/read.rs index 33230c7d..a415b513 100644 --- a/pineappl_cli/src/read.rs +++ b/pineappl_cli/src/read.rs @@ -125,7 +125,10 @@ impl Subcommand for Opts { row.add_cell(cell!(format!("{index}"))); for (pids, factor) in channel.entry() { - row.add_cell(cell!(format!("{factor} \u{d7} ({})", pids.iter().map(ToString::to_string).join(", ")))); + row.add_cell(cell!(format!( + "{factor} \u{d7} ({})", + pids.iter().map(ToString::to_string).join(", ") + ))); } } } else if self.group.ew || self.group.qcd { @@ -210,6 +213,7 @@ impl Subcommand for Opts { alpha, logxir, logxif, + logxia, } = order; let order_string = [alphas, alpha, logxir, logxif] From 02e48e9c7ce90a4e20d99e76043b1f731fa05c08 Mon Sep 17 00:00:00 2001 From: t7phy Date: Wed, 24 Jul 2024 17:41:49 +0200 Subject: [PATCH 006/213] Fix some warnings --- pineappl_cli/src/export.rs | 3 ++- pineappl_cli/src/read.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pineappl_cli/src/export.rs b/pineappl_cli/src/export.rs index 124ad83a..4d8e4885 100644 --- a/pineappl_cli/src/export.rs +++ b/pineappl_cli/src/export.rs @@ -143,7 +143,8 @@ impl Subcommand for Opts { }, keep, )| { - (keep && (logxir == 0) && (logxif == 0)).then_some((alphas, alpha)) + (keep && (logxir == 0) && (logxif == 0) && (logxia == 0)) + .then_some((alphas, alpha)) }, ) .collect(); diff --git a/pineappl_cli/src/read.rs b/pineappl_cli/src/read.rs index a415b513..c7f8c447 100644 --- a/pineappl_cli/src/read.rs +++ b/pineappl_cli/src/read.rs @@ -216,9 +216,9 @@ impl Subcommand for Opts { logxia, } = order; - let order_string = [alphas, alpha, logxir, logxif] + let order_string = [alphas, alpha, logxir, logxif, logxia] .iter() - .zip(["as^", "a^", "lr^", "lf^"].iter()) + .zip(["as^", "a^", "lr^", "lf^", "la^"].iter()) .filter_map(|(num, string)| { if **num == 0 && self.group.orders { None From 0d360b4075d62c796fb3550e158a01a31601007e Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 5 Aug 2024 09:46:42 +0200 Subject: [PATCH 007/213] Print ignored fragmentation scale log --- pineappl_cli/src/export.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl_cli/src/export.rs b/pineappl_cli/src/export.rs index 4d8e4885..5fc83ef8 100644 --- a/pineappl_cli/src/export.rs +++ b/pineappl_cli/src/export.rs @@ -125,7 +125,7 @@ impl Subcommand for Opts { .zip(order_mask.iter()) .filter_map(|(order, keep)| (!keep).then_some(order.clone())) { - println!("WARNING: the order O(as^{alphas} a^{alpha} lr^{logxir} lf^{logxif}) isn't supported by {grid_type} and will be skipped."); + println!("WARNING: the order O(as^{alphas} a^{alpha} lr^{logxir} lf^{logxif} la^{logxia}) isn't supported by {grid_type} and will be skipped."); } let orders: Vec<_> = grid From 87b3bf3459cb1f46e3afb5c6e47f17e39fbca929 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 5 Aug 2024 09:48:00 +0200 Subject: [PATCH 008/213] Fix more compilation errors when features are enabled --- pineappl_cli/src/export/applgrid.rs | 57 ++++++++++++++++------------- pineappl_cli/src/import/applgrid.rs | 37 ++++++++++++------- pineappl_cli/src/import/fastnlo.rs | 45 ++++++++++++----------- pineappl_cli/src/import/fktable.rs | 7 +--- 4 files changed, 78 insertions(+), 68 deletions(-) diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 2f3ea6eb..e0666d7d 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -69,38 +69,43 @@ pub fn convert_into_applgrid( bail!("grid has non-consecutive bin limits, which APPLgrid does not support"); } + if grid.convolutions().len() > 2 { + bail!("APPLgrid does not support grids with more than two convolutions"); + } + let lumis = grid.channels().len(); let has_pdf1 = grid.convolutions()[0] != Convolution::None; let has_pdf2 = grid.convolutions()[1] != Convolution::None; // TODO: check that PDG MC IDs are used - let combinations: Vec<_> = iter::once(lumis.try_into().unwrap()) - .chain( - grid.channels() - .iter() - .enumerate() - .flat_map(|(index, entry)| { - [ - index.try_into().unwrap(), - entry.entry().len().try_into().unwrap(), - ] - .into_iter() - .chain(entry.entry().iter().flat_map(|&(a, b, factor)| { - // TODO: if the factors aren't trivial, we have to find some other way to - // propagate them - assert_eq!(factor, 1.0); - - match (has_pdf1, has_pdf2) { - (true, true) => [a, b], - (true, false) => [a, 0], - (false, true) => [b, 0], - (false, false) => unreachable!(), - } - })) - }), - ) - .collect(); + let combinations: Vec<_> = + iter::once(lumis.try_into().unwrap()) + .chain( + grid.channels() + .iter() + .enumerate() + .flat_map(|(index, entry)| { + [ + index.try_into().unwrap(), + entry.entry().len().try_into().unwrap(), + ] + .into_iter() + .chain(entry.entry().iter().flat_map(|&(ref pids, factor)| { + // TODO: if the factors aren't trivial, we have to find some other way to + // propagate them + assert_eq!(factor, 1.0); + + match (has_pdf1, has_pdf2) { + (true, true) => [pids[0], pids[1]], + (true, false) => [pids[0], 0], + (false, true) => [pids[1], 0], + (false, false) => unreachable!(), + } + })) + }), + ) + .collect(); // `id` must end with '.config' for APPLgrid to know its type is `lumi_pdf` let id = "PineAPPL-Lumi.config"; diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 94b17b70..fe722bea 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -22,11 +22,11 @@ fn convert_to_pdg_id(pid: usize) -> i32 { } } -fn reconstruct_luminosity_function(grid: &grid, order: i32, dis_pid: i32) -> Vec { +fn reconstruct_channels(grid: &grid, order: i32, dis_pid: i32) -> Vec { let pdf = unsafe { &*grid.genpdf(order, false) }; let nproc: usize = pdf.Nproc().try_into().unwrap(); - let mut lumis = vec![Vec::new(); nproc]; + let mut channels = vec![Vec::new(); nproc]; let mut xfx1 = [0.0; 14]; let mut xfx2 = [0.0; 14]; let mut results = vec![0.0; nproc]; @@ -41,7 +41,7 @@ fn reconstruct_luminosity_function(grid: &grid, order: i32, dis_pid: i32) -> Vec for i in 0..nproc { if results[i] != 0.0 { - lumis[i].push((convert_to_pdg_id(a), dis_pid, results[i])); + channels[i].push((vec![convert_to_pdg_id(a), dis_pid], results[i])); } } } else { @@ -54,7 +54,8 @@ fn reconstruct_luminosity_function(grid: &grid, order: i32, dis_pid: i32) -> Vec for i in 0..nproc { if results[i] != 0.0 { - lumis[i].push((convert_to_pdg_id(a), convert_to_pdg_id(b), results[i])); + channels[i] + .push((vec![convert_to_pdg_id(a), convert_to_pdg_id(b)], results[i])); } } @@ -65,7 +66,7 @@ fn reconstruct_luminosity_function(grid: &grid, order: i32, dis_pid: i32) -> Vec xfx1[a] = 0.0; } - lumis.into_iter().map(Channel::new).collect() + channels.into_iter().map(Channel::new).collect() } pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Result { @@ -80,13 +81,13 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul if grid.calculation() == ffi::grid_CALCULATION::AMCATNLO { alphas_factor = 2.0 * TAU; orders = if grid.nloops() == 0 { - vec![Order::new(leading_order, alpha, 0, 0)] + vec![Order::new(leading_order, alpha, 0, 0, 0)] } else if grid.nloops() == 1 { vec![ - Order::new(leading_order + 1, alpha, 0, 0), // NLO - Order::new(leading_order + 1, alpha, 1, 0), // NLO mur - Order::new(leading_order + 1, alpha, 0, 1), // NLO muf - Order::new(leading_order, alpha, 0, 0), // LO + Order::new(leading_order + 1, alpha, 0, 0, 0), // NLO + Order::new(leading_order + 1, alpha, 1, 0, 0), // NLO mur + Order::new(leading_order + 1, alpha, 0, 1, 0), // NLO muf + Order::new(leading_order, alpha, 0, 0, 0), // LO ] } else { unimplemented!("nloops = {} is not supported", grid.nloops()); @@ -94,7 +95,15 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul } else if grid.calculation() == ffi::grid_CALCULATION::STANDARD { alphas_factor = 1.0 / TAU; orders = (0..=grid.nloops()) - .map(|power| Order::new(leading_order + u32::try_from(power).unwrap(), alpha, 0, 0)) + .map(|power| { + Order::new( + leading_order + u32::try_from(power).unwrap(), + alpha, + 0, + 0, + 0, + ) + }) .collect(); } else { unimplemented!("calculation is not supported"); @@ -114,10 +123,10 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let mut grids = Vec::with_capacity(orders.len()); for (i, order) in orders.into_iter().enumerate() { - let lumis = reconstruct_luminosity_function(&grid, i.try_into().unwrap(), dis_pid); - let lumis_len = lumis.len(); + let channels = reconstruct_channels(&grid, i.try_into().unwrap(), dis_pid); + let lumis_len = channels.len(); let mut pgrid = Grid::new( - lumis, + channels, vec![order], bin_limits.clone(), SubgridParams::default(), diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 4e79d3f3..fad1fa10 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -22,16 +22,16 @@ fn pid_to_pdg_id(pid: i32) -> i32 { } } -fn create_lumi( +fn reconstruct_channels( table: &fastNLOCoeffAddBase, comb: &fastNLOPDFLinearCombinations, dis_pid: i32, ) -> Vec { let dis_pid = if table.GetNPDF() == 2 { 0 } else { dis_pid }; - let mut lumis = Vec::new(); + let mut channels = Vec::new(); - // if there's a (non-empty) PDF coefficient vector reconstruct the luminosity function; the - // advantage is that we preserve the order of the lumi entries in the PineAPPL grid + // if there's a (non-empty) PDF coefficient vector reconstruct the channels; the advantage is + // that we preserve the order of the channels in the PineAPPL grid for pdf_entry in 0..ffi::GetPDFCoeffSize(table) { let mut entries = Vec::new(); @@ -44,14 +44,14 @@ fn create_lumi( }; let f = 1.0; - entries.push((a, b, f)); + entries.push((vec![a, b], f)); } - lumis.push(Channel::new(entries)); + channels.push(Channel::new(entries)); } - // if the PDF coefficient vector was empty, we must reconstruct the lumi function - if lumis.is_empty() { + // if the PDF coefficient vector was empty, we must reconstruct the channels in a different way + if channels.is_empty() { let nsubproc = table.GetNSubproc().try_into().unwrap(); let mut xfx1 = [0.0; 13]; @@ -66,15 +66,15 @@ fn create_lumi( for b in 0..13 { xfx2[b] = 1.0; - let lumi = ffi::CalcPDFLinearCombination(comb, table, &xfx1, &xfx2, false); + let channel = ffi::CalcPDFLinearCombination(comb, table, &xfx1, &xfx2, false); - assert!(lumi.len() == nsubproc); + assert!(channel.len() == nsubproc); - for (i, l) in lumi.iter().copied().enumerate().filter(|(_, l)| *l != 0.0) { + for (i, &l) in channel.iter().enumerate().filter(|(_, &l)| l != 0.0) { let ap = pid_to_pdg_id(i32::try_from(a).unwrap() - 6); let bp = pid_to_pdg_id(i32::try_from(b).unwrap() - 6); - entries[i].push((ap, bp, l)); + entries[i].push((vec![ap, bp], l)); } xfx2[b] = 0.0; @@ -83,10 +83,10 @@ fn create_lumi( xfx1[a] = 0.0; } - lumis = entries.into_iter().map(Channel::new).collect(); + channels = entries.into_iter().map(Channel::new).collect(); } - lumis + channels } fn convert_coeff_add_fix( @@ -99,12 +99,13 @@ fn convert_coeff_add_fix( let table_as_add_base = ffi::downcast_coeff_add_fix_to_base(table); let mut grid = Grid::new( - create_lumi(table_as_add_base, comb, dis_pid), + reconstruct_channels(table_as_add_base, comb, dis_pid), vec![Order { alphas: table_as_add_base.GetNpow().try_into().unwrap(), alpha, logxir: 0, logxif: 0, + logxia: 0, }], (0..=bins) .map(|limit| u16::try_from(limit).unwrap().into()) @@ -234,12 +235,12 @@ fn convert_coeff_add_flex( let alphas = table_as_add_base.GetNpow().try_into().unwrap(); let orders: Vec<_> = [ - Order::new(alphas, alpha, 0, 0), - Order::new(alphas, alpha, 1, 0), - Order::new(alphas, alpha, 0, 1), - Order::new(alphas, alpha, 2, 0), - Order::new(alphas, alpha, 0, 2), - Order::new(alphas, alpha, 1, 1), + Order::new(alphas, alpha, 0, 0, 0), + Order::new(alphas, alpha, 1, 0, 0), + Order::new(alphas, alpha, 0, 1, 0), + Order::new(alphas, alpha, 2, 0, 0), + Order::new(alphas, alpha, 0, 2, 0), + Order::new(alphas, alpha, 1, 1, 0), ] .into_iter() .take(match table.GetNScaleDep() { @@ -253,7 +254,7 @@ fn convert_coeff_add_flex( let orders_len = orders.len(); let mut grid = Grid::new( - create_lumi(table_as_add_base, comb, dis_pid), + reconstruct_channels(table_as_add_base, comb, dis_pid), orders, (0..=bins) .map(|limit| u16::try_from(limit).unwrap().into()) diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 3fa6f2c2..68998ee7 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -100,12 +100,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { // construct `Grid` let mut fktable = Grid::new( lumis, - vec![Order { - alphas: 0, - alpha: 0, - logxir: 0, - logxif: 0, - }], + vec![Order::new(0, 0, 0, 0, 0)], (0..=ndata).map(Into::into).collect(), SubgridParams::default(), ); From eae77a58423b8b4aa72c45978805d29f923f35cd Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 5 Aug 2024 09:51:07 +0200 Subject: [PATCH 009/213] Remove fragmentation scale extraction in CAPI --- pineappl_capi/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 9707d157..3e8ff64c 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -643,6 +643,8 @@ pub unsafe extern "C" fn pineappl_grid_order_count(grid: *const Grid) -> usize { grid.orders().len() } +// TODO: add a function that supports fragmentation scale logs + /// Creates a new and empty grid. The creation requires four different sets of parameters: /// - The luminosity function `lumi`: A pointer to the luminosity function that specifies how the /// cross section should be reconstructed. @@ -685,7 +687,8 @@ pub unsafe extern "C" fn pineappl_grid_new( alpha: s[1], logxir: s[2], logxif: s[3], - logxia: s[4], + // this function doesn't support fragmentation scale logs + logxia: 0, }) .collect(); From ef2f531222b61f37649b9aa18ebc7978bda675a3 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 5 Aug 2024 09:59:47 +0200 Subject: [PATCH 010/213] Fix compilation errors in tests --- pineappl/src/boc.rs | 72 ++++----- pineappl/src/grid.rs | 21 +-- pineappl/tests/drell_yan_lo.rs | 3 + pineappl_cli/tests/import.rs | 260 ++++++++++++++++----------------- 4 files changed, 180 insertions(+), 176 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index cdc0cfcc..48ac9739 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -527,10 +527,10 @@ mod tests { #[test] fn order_from_str() { - assert_eq!("as1".parse(), Ok(Order::new(1, 0, 0, 0))); - assert_eq!("a1".parse(), Ok(Order::new(0, 1, 0, 0))); - assert_eq!("as1lr1".parse(), Ok(Order::new(1, 0, 1, 0))); - assert_eq!("as1lf1".parse(), Ok(Order::new(1, 0, 0, 1))); + assert_eq!("as1".parse(), Ok(Order::new(1, 0, 0, 0, 0))); + assert_eq!("a1".parse(), Ok(Order::new(0, 1, 0, 0, 0))); + assert_eq!("as1lr1".parse(), Ok(Order::new(1, 0, 1, 0, 0))); + assert_eq!("as1lf1".parse(), Ok(Order::new(1, 0, 0, 1, 0))); assert_eq!( "ab12".parse::(), Err(ParseOrderError("unknown coupling: 'ab'".to_owned())) @@ -547,36 +547,36 @@ mod tests { #[test] fn order_cmp() { let mut orders = [ - Order::new(1, 2, 1, 0), - Order::new(1, 2, 0, 1), - Order::new(1, 2, 0, 0), - Order::new(0, 3, 1, 0), - Order::new(0, 3, 0, 1), - Order::new(0, 3, 0, 0), - Order::new(0, 2, 0, 0), + Order::new(1, 2, 1, 0, 0), + Order::new(1, 2, 0, 1, 0), + Order::new(1, 2, 0, 0, 0), + Order::new(0, 3, 1, 0, 0), + Order::new(0, 3, 0, 1, 0), + Order::new(0, 3, 0, 0, 0), + Order::new(0, 2, 0, 0, 0), ]; orders.sort(); - assert_eq!(orders[0], Order::new(0, 2, 0, 0)); - assert_eq!(orders[1], Order::new(1, 2, 0, 0)); - assert_eq!(orders[2], Order::new(1, 2, 0, 1)); - assert_eq!(orders[3], Order::new(1, 2, 1, 0)); - assert_eq!(orders[4], Order::new(0, 3, 0, 0)); - assert_eq!(orders[5], Order::new(0, 3, 0, 1)); - assert_eq!(orders[6], Order::new(0, 3, 1, 0)); + assert_eq!(orders[0], Order::new(0, 2, 0, 0, 0)); + assert_eq!(orders[1], Order::new(1, 2, 0, 0, 0)); + assert_eq!(orders[2], Order::new(1, 2, 0, 1, 0)); + assert_eq!(orders[3], Order::new(1, 2, 1, 0, 0)); + assert_eq!(orders[4], Order::new(0, 3, 0, 0, 0)); + assert_eq!(orders[5], Order::new(0, 3, 0, 1, 0)); + assert_eq!(orders[6], Order::new(0, 3, 1, 0, 0)); } #[test] fn order_create_mask() { // Drell—Yan orders let orders = [ - Order::new(0, 2, 0, 0), // LO : alpha^2 - Order::new(1, 2, 0, 0), // NLO QCD : alphas alpha^2 - Order::new(0, 3, 0, 0), // NLO EW : alpha^3 - Order::new(2, 2, 0, 0), // NNLO QCD : alphas^2 alpha^2 - Order::new(1, 3, 0, 0), // NNLO QCD—EW : alphas alpha^3 - Order::new(0, 4, 0, 0), // NNLO EW : alpha^4 + Order::new(0, 2, 0, 0, 0), // LO : alpha^2 + Order::new(1, 2, 0, 0, 0), // NLO QCD : alphas alpha^2 + Order::new(0, 3, 0, 0, 0), // NLO EW : alpha^3 + Order::new(2, 2, 0, 0, 0), // NNLO QCD : alphas^2 alpha^2 + Order::new(1, 3, 0, 0, 0), // NNLO QCD—EW : alphas alpha^3 + Order::new(0, 4, 0, 0, 0), // NNLO EW : alpha^4 ]; assert_eq!( @@ -646,18 +646,18 @@ mod tests { // Top-pair production orders let orders = [ - Order::new(2, 0, 0, 0), // LO QCD : alphas^2 - Order::new(1, 1, 0, 0), // LO QCD—EW : alphas alpha - Order::new(0, 2, 0, 0), // LO EW : alpha^2 - Order::new(3, 0, 0, 0), // NLO QCD : alphas^3 - Order::new(2, 1, 0, 0), // NLO QCD—EW : alphas^2 alpha - Order::new(1, 2, 0, 0), // NLO QCD—EW : alphas alpha^2 - Order::new(0, 3, 0, 0), // NLO EW : alpha^3 - Order::new(4, 0, 0, 0), // NNLO QCD : alphas^4 - Order::new(3, 1, 0, 0), // NNLO QCD—EW : alphas^3 alpha - Order::new(2, 2, 0, 0), // NNLO QCD—EW : alphas^2 alpha^2 - Order::new(1, 3, 0, 0), // NNLO QCD—EW : alphas alpha^3 - Order::new(0, 4, 0, 0), // NNLO EW : alpha^4 + Order::new(2, 0, 0, 0, 0), // LO QCD : alphas^2 + Order::new(1, 1, 0, 0, 0), // LO QCD—EW : alphas alpha + Order::new(0, 2, 0, 0, 0), // LO EW : alpha^2 + Order::new(3, 0, 0, 0, 0), // NLO QCD : alphas^3 + Order::new(2, 1, 0, 0, 0), // NLO QCD—EW : alphas^2 alpha + Order::new(1, 2, 0, 0, 0), // NLO QCD—EW : alphas alpha^2 + Order::new(0, 3, 0, 0, 0), // NLO EW : alpha^3 + Order::new(4, 0, 0, 0, 0), // NNLO QCD : alphas^4 + Order::new(3, 1, 0, 0, 0), // NNLO QCD—EW : alphas^3 alpha + Order::new(2, 2, 0, 0, 0), // NNLO QCD—EW : alphas^2 alpha^2 + Order::new(1, 3, 0, 0, 0), // NNLO QCD—EW : alphas alpha^3 + Order::new(0, 4, 0, 0, 0), // NNLO EW : alpha^4 ]; assert_eq!( diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 998e8d27..33574c7e 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1865,7 +1865,7 @@ mod tests { channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], ], - vec![Order::new(0, 2, 0, 0)], + vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], SubgridParams::default(), ); @@ -1880,7 +1880,7 @@ mod tests { channel![1, 1, 1.0; 3, 3, 1.0], channel![2, 2, 1.0; 4, 4, 1.0], ], - vec![Order::new(1, 2, 0, 0), Order::new(1, 2, 0, 1)], + vec![Order::new(1, 2, 0, 0, 0), Order::new(1, 2, 0, 1, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], SubgridParams::default(), ); @@ -1900,7 +1900,7 @@ mod tests { channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], ], - vec![Order::new(0, 2, 0, 0)], + vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], SubgridParams::default(), ); @@ -1915,9 +1915,9 @@ mod tests { channel![1, 1, 1.0; 3, 3, 1.0], ], vec![ - Order::new(1, 2, 0, 0), - Order::new(1, 2, 0, 1), - Order::new(0, 2, 0, 0), + Order::new(1, 2, 0, 0, 0), + Order::new(1, 2, 0, 1, 0), + Order::new(0, 2, 0, 0, 0), ], vec![0.0, 0.25, 0.5, 0.75, 1.0], SubgridParams::default(), @@ -1961,7 +1961,7 @@ mod tests { channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], ], - vec![Order::new(0, 2, 0, 0)], + vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], SubgridParams::default(), ); @@ -1972,7 +1972,7 @@ mod tests { let mut other = Grid::new( vec![channel![22, 22, 1.0], channel![2, 2, 1.0; 4, 4, 1.0]], - vec![Order::new(0, 2, 0, 0)], + vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], SubgridParams::default(), ); @@ -2004,7 +2004,7 @@ mod tests { channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], ], - vec![Order::new(0, 2, 0, 0)], + vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5], SubgridParams::default(), ); @@ -2019,7 +2019,7 @@ mod tests { channel![1, 1, 1.0; 3, 3, 1.0], channel![2, 2, 1.0; 4, 4, 1.0], ], - vec![Order::new(0, 2, 0, 0)], + vec![Order::new(0, 2, 0, 0, 0)], vec![0.5, 0.75, 1.0], SubgridParams::default(), ); @@ -2054,6 +2054,7 @@ mod tests { alpha: 0, logxir: 0, logxif: 0, + logxia: 0, }], vec![0.0, 1.0], SubgridParams::default(), diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 0b53bc2b..019b61e8 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -140,6 +140,7 @@ fn fill_drell_yan_lo_grid( alpha: 2, logxir: 0, logxif: 0, + logxia: 0, }, // NLO QCD - won't be filled Order { @@ -147,12 +148,14 @@ fn fill_drell_yan_lo_grid( alpha: 2, logxir: 0, logxif: 0, + logxia: 0, }, Order { alphas: 1, alpha: 2, logxir: 0, logxif: 1, + logxia: 0, }, ]; diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index c66f66c2..b6145a85 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -915,51 +915,51 @@ fn import_hadronic_fktable() { assert_eq!( channels, [ - (100, 100), - (100, 21), - (100, 200), - (100, 203), - (100, 208), - (100, 215), - (100, 103), - (100, 108), - (100, 115), - (21, 21), - (21, 200), - (21, 203), - (21, 208), - (21, 215), - (21, 103), - (21, 108), - (21, 115), - (200, 200), - (200, 203), - (200, 208), - (200, 215), - (200, 103), - (200, 108), - (200, 115), - (203, 203), - (203, 208), - (203, 215), - (203, 103), - (203, 108), - (203, 115), - (208, 208), - (208, 215), - (208, 103), - (208, 108), - (208, 115), - (215, 215), - (215, 103), - (215, 108), - (215, 115), - (103, 103), - (103, 108), - (103, 115), - (108, 108), - (108, 115), - (115, 115) + [100, 100], + [100, 21], + [100, 200], + [100, 203], + [100, 208], + [100, 215], + [100, 103], + [100, 108], + [100, 115], + [21, 21], + [21, 200], + [21, 203], + [21, 208], + [21, 215], + [21, 103], + [21, 108], + [21, 115], + [200, 200], + [200, 203], + [200, 208], + [200, 215], + [200, 103], + [200, 108], + [200, 115], + [203, 203], + [203, 208], + [203, 215], + [203, 103], + [203, 108], + [203, 115], + [208, 208], + [208, 215], + [208, 103], + [208, 108], + [208, 115], + [215, 215], + [215, 103], + [215, 108], + [215, 115], + [103, 103], + [103, 108], + [103, 115], + [108, 108], + [108, 115], + [115, 115] ] ); assert_eq!(fk_table.muf2(), 1.65 * 1.65); @@ -1043,103 +1043,103 @@ fn import_hadronic_fktable() { assert_eq!( fk_table.channels(), [ - (100, 100), - (100, 21), - (100, 203), - (100, 208), - (100, 200), - (100, 103), - (100, 108), - (100, 115), - (21, 21), - (21, 203), - (21, 208), - (21, 200), - (21, 103), - (21, 108), - (21, 115), - (200, 203), - (200, 208), - (203, 203), - (203, 208), - (203, 103), - (203, 108), - (203, 115), - (208, 208), - (208, 103), - (208, 108), - (208, 115), - (200, 200), - (200, 103), - (200, 108), - (200, 115), - (103, 103), - (103, 108), - (103, 115), - (108, 108), - (108, 115), - (115, 115) + [100, 100], + [100, 21], + [100, 203], + [100, 208], + [100, 200], + [100, 103], + [100, 108], + [100, 115], + [21, 21], + [21, 203], + [21, 208], + [21, 200], + [21, 103], + [21, 108], + [21, 115], + [200, 203], + [200, 208], + [203, 203], + [203, 208], + [203, 103], + [203, 108], + [203, 115], + [208, 208], + [208, 103], + [208, 108], + [208, 115], + [200, 200], + [200, 103], + [200, 108], + [200, 115], + [103, 103], + [103, 108], + [103, 115], + [108, 108], + [108, 115], + [115, 115] ] ); fk_table.optimize(FkAssumptions::Nf3Ind); assert_eq!( fk_table.channels(), [ - (100, 21), - (100, 203), - (100, 208), - (100, 200), - (100, 103), - (100, 108), - (21, 21), - (21, 203), - (21, 208), - (21, 200), - (21, 103), - (21, 108), - (200, 203), - (200, 208), - (203, 203), - (203, 208), - (203, 103), - (203, 108), - (208, 208), - (208, 103), - (208, 108), - (200, 200), - (200, 103), - (200, 108), - (103, 103), - (103, 108), - (108, 108), - (100, 100) + [100, 21], + [100, 203], + [100, 208], + [100, 200], + [100, 103], + [100, 108], + [21, 21], + [21, 203], + [21, 208], + [21, 200], + [21, 103], + [21, 108], + [200, 203], + [200, 208], + [203, 203], + [203, 208], + [203, 103], + [203, 108], + [208, 208], + [208, 103], + [208, 108], + [200, 200], + [200, 103], + [200, 108], + [103, 103], + [103, 108], + [108, 108], + [100, 100] ] ); fk_table.optimize(FkAssumptions::Nf3Sym); assert_eq!( fk_table.channels(), [ - (100, 21), - (100, 203), - (100, 200), - (100, 103), - (100, 108), - (21, 21), - (21, 203), - (21, 200), - (21, 103), - (21, 108), - (200, 203), - (203, 203), - (203, 103), - (203, 108), - (200, 200), - (200, 103), - (200, 108), - (103, 103), - (103, 108), - (108, 108), - (100, 100) + [100, 21], + [100, 203], + [100, 200], + [100, 103], + [100, 108], + [21, 21], + [21, 203], + [21, 200], + [21, 103], + [21, 108], + [200, 203], + [203, 203], + [203, 103], + [203, 108], + [200, 200], + [200, 103], + [200, 108], + [103, 103], + [103, 108], + [108, 108], + [100, 100], ] ); } From 13556ecc484158cb5756287ac970e151fd9e5ace Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 5 Aug 2024 10:02:50 +0200 Subject: [PATCH 011/213] Fix off-by-one error in `Grid::convolutions` --- pineappl/src/grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 33574c7e..63286b28 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -806,7 +806,7 @@ impl Grid { { Some(Ok(pid)) => { let condition = !self.channels().iter().all(|entry| { - entry.entry().iter().all(|(pids, _)| pids[index] == pid) + entry.entry().iter().all(|(pids, _)| pids[index - 1] == pid) }); if condition { From 4590eb15146e66a435d46ea010c236531ace5005 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 11:32:32 +0200 Subject: [PATCH 012/213] Fix failing doctests --- pineappl/src/boc.rs | 48 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 48ac9739..7d964240 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -144,12 +144,12 @@ impl Order { /// use pineappl::boc::Order; /// /// let orders = [ - /// Order::new(0, 2, 0, 0), // LO : alpha^2 - /// Order::new(1, 2, 0, 0), // NLO QCD : alphas alpha^2 - /// Order::new(0, 3, 0, 0), // NLO EW : alpha^3 - /// Order::new(2, 2, 0, 0), // NNLO QCD : alphas^2 alpha^2 - /// Order::new(1, 3, 0, 0), // NNLO QCD—EW : alphas alpha^3 - /// Order::new(0, 4, 0, 0), // NNLO EW : alpha^4 + /// Order::new(0, 2, 0, 0, 0), // LO : alpha^2 + /// Order::new(1, 2, 0, 0, 0), // NLO QCD : alphas alpha^2 + /// Order::new(0, 3, 0, 0, 0), // NLO EW : alpha^3 + /// Order::new(2, 2, 0, 0, 0), // NNLO QCD : alphas^2 alpha^2 + /// Order::new(1, 3, 0, 0, 0), // NNLO QCD—EW : alphas alpha^3 + /// Order::new(0, 4, 0, 0, 0), // NNLO EW : alpha^4 /// ]; /// /// // LO EW @@ -175,11 +175,11 @@ impl Order { /// use pineappl::boc::Order; /// /// let orders = [ - /// Order::new(0, 2, 0, 0), // LO : alpha^2 - /// Order::new(1, 2, 0, 0), // NLO QCD : alphas alpha^2 - /// Order::new(1, 2, 1, 0), // NLO QCD : alphas alpha^2 logxif - /// Order::new(0, 3, 0, 0), // NLO EW : alpha^3 - /// Order::new(0, 3, 1, 0), // NLO EW : alpha^3 logxif + /// Order::new(0, 2, 0, 0, 0), // LO : alpha^2 + /// Order::new(1, 2, 0, 0, 0), // NLO QCD : alphas alpha^2 + /// Order::new(1, 2, 1, 0, 0), // NLO QCD : alphas alpha^2 logxif + /// Order::new(0, 3, 0, 0, 0), // NLO EW : alpha^3 + /// Order::new(0, 3, 1, 0, 0), // NLO EW : alpha^3 logxif /// ]; /// /// assert_eq!(Order::create_mask(&orders, 0, 2, true), [true, false, false, true, true]); @@ -192,13 +192,13 @@ impl Order { /// use pineappl::boc::Order; /// /// let orders = [ - /// Order::new(2, 0, 0, 0), // LO QCD : alphas^2 - /// Order::new(1, 1, 0, 0), // LO QCD—EW : alphas alpha - /// Order::new(0, 2, 0, 0), // LO EW : alpha^2 - /// Order::new(3, 0, 0, 0), // NLO QCD : alphas^3 - /// Order::new(2, 1, 0, 0), // NLO QCD—EW : alphas^2 alpha - /// Order::new(1, 2, 0, 0), // NLO QCD—EW : alphas alpha^2 - /// Order::new(0, 3, 0, 0), // NLO EW : alpha^3 + /// Order::new(2, 0, 0, 0, 0), // LO QCD : alphas^2 + /// Order::new(1, 1, 0, 0, 0), // LO QCD—EW : alphas alpha + /// Order::new(0, 2, 0, 0, 0), // LO EW : alpha^2 + /// Order::new(3, 0, 0, 0, 0), // NLO QCD : alphas^3 + /// Order::new(2, 1, 0, 0, 0), // NLO QCD—EW : alphas^2 alpha + /// Order::new(1, 2, 0, 0, 0), // NLO QCD—EW : alphas alpha^2 + /// Order::new(0, 3, 0, 0, 0), // NLO EW : alpha^3 /// ]; /// /// // LO EW @@ -287,8 +287,8 @@ impl Channel { /// ```rust /// use pineappl::boc::Channel; /// - /// let entry1 = Channel::new(vec![(2, 2, 1.0), (4, 4, 1.0)]); - /// let entry2 = Channel::new(vec![(4, 4, 1.0), (2, 2, 1.0)]); + /// let entry1 = Channel::new(vec![(vec![2, 2], 1.0), (vec![4, 4], 1.0)]); + /// let entry2 = Channel::new(vec![(vec![4, 4], 1.0), (vec![2, 2], 1.0)]); /// /// // checks that the ordering doesn't matter /// assert_eq!(entry1, entry2); @@ -299,8 +299,8 @@ impl Channel { /// ```rust /// use pineappl::boc::Channel; /// - /// let entry1 = Channel::new(vec![(1, 1, 1.0), (1, 1, 3.0), (3, 3, 1.0), (1, 1, 6.0)]); - /// let entry2 = Channel::new(vec![(1, 1, 10.0), (3, 3, 1.0)]); + /// let entry1 = Channel::new(vec![(vec![1, 1], 1.0), (vec![1, 1], 3.0), (vec![3, 3], 1.0), (vec![1, 1], 6.0)]); + /// let entry2 = Channel::new(vec![(vec![1, 1], 10.0), (vec![3, 3], 1.0)]); /// /// assert_eq!(entry1, entry2); /// ``` @@ -348,7 +348,7 @@ impl Channel { /// use pineappl::boc::Channel; /// use pineappl::channel; /// - /// let entry = Channel::translate(&channel![103, 11, 10], &|evol_id| match evol_id { + /// let entry = Channel::translate(&channel![103, 11, 10.0], &|evol_id| match evol_id { /// 103 => vec![(2, 1.0), (-2, -1.0), (1, -1.0), (-1, 1.0)], /// _ => vec![(evol_id, 1.0)], /// }); @@ -384,7 +384,7 @@ impl Channel { /// /// let entry = channel![4, 4, 1.0; 2, 2, 1.0]; /// - /// assert_eq!(entry.entry(), [(2, 2, 1.0), (4, 4, 1.0)]); + /// assert_eq!(entry.entry(), [(vec![2, 2], 1.0), (vec![4, 4], 1.0)]); /// ``` #[must_use] pub fn entry(&self) -> &[(Vec, f64)] { From 5afec894d93093013e69f809360553bf53e57b3e Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 11:34:52 +0200 Subject: [PATCH 013/213] Fix bugs in `Channel::from_str` --- pineappl/src/boc.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 7d964240..8473f133 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -466,6 +466,7 @@ impl FromStr for Channel { || Err(ParseChannelError(format!("missing '*' in '{sub}'"))), |(factor, pids)| { let vector: Vec<_> = pids + .trim() .strip_prefix('(') .ok_or_else(|| ParseChannelError(format!("missing '(' in '{pids}'")))? .strip_suffix(')') @@ -475,7 +476,7 @@ impl FromStr for Channel { Ok(pid .trim() .parse::() - .map_err(|err| ParseChannelError(err.to_string()))?) + .map_err(|err| ParseChannelError(format!("could not parse PID: '{pid}', '{}'", err.to_string())))?) }) .collect::>()?; @@ -764,7 +765,7 @@ mod tests { str::parse::(" 1 * ( 2 -2) + 2* (4,-4)") .unwrap_err() .to_string(), - "missing ',' in ' ( 2 -2) '" + "could not parse PID: ' 2 -2', 'invalid digit found in string'" ); assert_eq!( From aa6fe31c1c763ae78458580b4b28ef19953537b0 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 11:37:13 +0200 Subject: [PATCH 014/213] Increase file version from 0 to 1 --- pineappl/src/grid.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 63286b28..04ff8a9c 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -71,12 +71,10 @@ pub enum GridError { IoFailure(io::Error), /// Returned when trying to read a `PineAPPL` file with file format version that is not /// supported. - #[error("the file version is {file_version}, but supported is only {supported_version}")] - FileVersionMismatch { + #[error("file version {file_version} is not supported")] + FileVersionUnsupported { /// File format version of the file read. file_version: u64, - /// Maximum supported file format version for this library. - supported_version: u64, }, /// Returned from [`Grid::evolve`] if the evolution failed. #[error("failed to evolve grid: {0}")] @@ -533,14 +531,13 @@ impl Grid { 0 }; - if file_version != 0 { - return Err(GridError::FileVersionMismatch { + match file_version { + 0 => todo!(), + 1 => bincode::deserialize_from(reader).map_err(GridError::ReadFailure), + _ => Err(GridError::FileVersionUnsupported { file_version, - supported_version: 0, - }); + }), } - - bincode::deserialize_from(reader).map_err(GridError::ReadFailure) } /// Serializes `self` into `writer`. Writing is buffered. @@ -550,7 +547,7 @@ impl Grid { /// If writing fails an error is returned. pub fn write(&self, writer: impl Write) -> Result<(), GridError> { let mut writer = BufWriter::new(writer); - let file_header = b"PineAPPL\0\0\0\0\0\0\0\0"; + let file_header = b"PineAPPL\x01\0\0\0\0\0\0\0"; // first write PineAPPL file header writer.write(file_header).map_err(GridError::IoFailure)?; From 4f1ecf669c4b60b46426d9158380a091dddd7af9 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 11:38:31 +0200 Subject: [PATCH 015/213] Fix bug in `Channel::common_factor` --- pineappl/src/boc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 8473f133..64b8bae9 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -434,7 +434,7 @@ impl Channel { .entry .iter() .zip(&other.entry) - .map(|(a, b)| (a == b).then_some(a.1 / b.1)) + .map(|((pids_a, fa), (pids_b, fb))| (pids_a == pids_b).then_some(fa / fb)) .collect(); result.and_then(|factors| { From 815704b9c271fa5b7509282a4fe9a4b224d71470 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 11:39:18 +0200 Subject: [PATCH 016/213] Fix bug in `Channel::translate` --- pineappl/src/boc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 64b8bae9..626b2903 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -366,7 +366,7 @@ impl Channel { { result.push(( tuples.iter().map(|&(pid, _)| pid).collect(), - tuples.iter().map(|(_, f)| factor * f).product::(), + factor * tuples.iter().map(|(_, f)| f).product::(), )); } } From e2c9dbfbde4a140e309fc1860605d89629516c22 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 12:47:34 +0200 Subject: [PATCH 017/213] Reformat code --- pineappl/src/boc.rs | 10 ++++++---- pineappl/src/grid.rs | 4 +--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 626b2903..1aaf7927 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -473,10 +473,12 @@ impl FromStr for Channel { .ok_or_else(|| ParseChannelError(format!("missing ')' in '{pids}'")))? .split(',') .map(|pid| { - Ok(pid - .trim() - .parse::() - .map_err(|err| ParseChannelError(format!("could not parse PID: '{pid}', '{}'", err.to_string())))?) + Ok(pid.trim().parse::().map_err(|err| { + ParseChannelError(format!( + "could not parse PID: '{pid}', '{}'", + err.to_string() + )) + })?) }) .collect::>()?; diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 04ff8a9c..0840c6ec 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -534,9 +534,7 @@ impl Grid { match file_version { 0 => todo!(), 1 => bincode::deserialize_from(reader).map_err(GridError::ReadFailure), - _ => Err(GridError::FileVersionUnsupported { - file_version, - }), + _ => Err(GridError::FileVersionUnsupported { file_version }), } } From 6797caaf761c1b25e287a9be8038200d53ca4b04 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 12:46:54 +0200 Subject: [PATCH 018/213] Add new struct `PackedQ1X2SubgridV1` --- pineappl/src/lib.rs | 1 + pineappl/src/packed_subgrid.rs | 372 +++++++++++++++++++++++++++++++++ pineappl/src/subgrid.rs | 3 + pineappl_cli/src/subgrids.rs | 1 + 4 files changed, 377 insertions(+) create mode 100644 pineappl/src/packed_subgrid.rs diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index 5a038799..ad1d0cbd 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -46,6 +46,7 @@ pub mod import_only_subgrid; pub mod lagrange_subgrid; pub mod ntuple_subgrid; pub mod packed_array; +pub mod packed_subgrid; pub mod pids; pub mod sparse_array3; pub mod subgrid; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs new file mode 100644 index 00000000..14bffb40 --- /dev/null +++ b/pineappl/src/packed_subgrid.rs @@ -0,0 +1,372 @@ +//! TODO + +use super::grid::Ntuple; +use super::packed_array::PackedArray; +use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use serde::{Deserialize, Serialize}; +use std::borrow::Cow; +use std::mem; + +/// TODO +#[derive(Clone, Deserialize, Serialize)] +pub struct PackedQ1X2SubgridV1 { + array: PackedArray, + mu2_grid: Vec, + x1_grid: Vec, + x2_grid: Vec, +} + +impl PackedQ1X2SubgridV1 { + /// Constructor. + #[must_use] + pub fn new( + array: PackedArray, + mu2_grid: Vec, + x1_grid: Vec, + x2_grid: Vec, + ) -> Self { + Self { + array, + mu2_grid, + x1_grid, + x2_grid, + } + } + + /// Return the array containing the numerical values of the grid. + pub fn array_mut(&mut self) -> &mut PackedArray { + &mut self.array + } +} + +impl Subgrid for PackedQ1X2SubgridV1 { + fn convolve( + &self, + _: &[f64], + _: &[f64], + _: &[Mu2], + lumi: &mut dyn FnMut(usize, usize, usize) -> f64, + ) -> f64 { + self.array + .indexed_iter() + .map(|([imu2, ix1, ix2], sigma)| sigma * lumi(ix1, ix2, imu2)) + .sum() + } + + fn fill(&mut self, _: &Ntuple) { + panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); + } + + fn mu2_grid(&self) -> Cow<[Mu2]> { + Cow::Borrowed(&self.mu2_grid) + } + + fn x1_grid(&self) -> Cow<[f64]> { + Cow::Borrowed(&self.x1_grid) + } + + fn x2_grid(&self) -> Cow<[f64]> { + Cow::Borrowed(&self.x2_grid) + } + + fn is_empty(&self) -> bool { + self.array.is_empty() + } + + fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { + if let SubgridEnum::PackedQ1X2SubgridV1(other_grid) = other { + if self.array.is_empty() && !transpose { + mem::swap(&mut self.array, &mut other_grid.array); + } else { + let rhs_x1 = if transpose { + other_grid.x2_grid() + } else { + other_grid.x1_grid() + }; + let rhs_x2 = if transpose { + other_grid.x1_grid() + } else { + other_grid.x2_grid() + }; + + if (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { + let mut x1_grid = self.x1_grid.clone(); + let mut x2_grid = self.x2_grid.clone(); + + x1_grid.extend_from_slice(&rhs_x1); + x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x1_grid.dedup(); + x2_grid.extend_from_slice(&rhs_x2); + x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x2_grid.dedup(); + + let mut array = + PackedArray::new([self.array.shape()[0], x1_grid.len(), x2_grid.len()]); + + for ([i, j, k], value) in self.array.indexed_iter() { + let target_j = x1_grid + .iter() + .position(|&x| x == self.x1_grid[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = x2_grid + .iter() + .position(|&x| x == self.x2_grid[k]) + .unwrap_or_else(|| unreachable!()); + + array[[i, target_j, target_k]] = value; + } + + self.array = array; + self.x1_grid = x1_grid; + self.x2_grid = x2_grid; + } + + for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() { + let index = match self + .mu2_grid + .binary_search_by(|val| val.partial_cmp(mu2).unwrap()) + { + Ok(index) => index, + Err(index) => { + self.mu2_grid.insert(index, mu2.clone()); + //self.array.increase_x_at(index); + todo!(); + index + } + }; + + for ([_, j, k], value) in other_grid + .array + .indexed_iter() + .filter(|&([i, _, _], _)| i == other_index) + { + let (j, k) = if transpose { (k, j) } else { (j, k) }; + let target_j = self + .x1_grid + .iter() + .position(|&x| x == rhs_x1[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = self + .x2_grid + .iter() + .position(|&x| x == rhs_x2[k]) + .unwrap_or_else(|| unreachable!()); + + self.array[[index, target_j, target_k]] += value; + } + } + } + } else { + todo!(); + } + } + + fn scale(&mut self, factor: f64) { + if factor == 0.0 { + self.array.clear(); + } else { + // self.array.iter_mut().for_each(|x| *x *= factor); + todo!(); + } + } + + fn symmetrize(&mut self) { + let mut new_array = + PackedArray::new([self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()]); + + for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k >= j) { + new_array[[i, j, k]] = sigma; + } + // do not change the diagonal entries (k==j) + for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k < j) { + new_array[[i, k, j]] += sigma; + } + + mem::swap(&mut self.array, &mut new_array); + } + + fn clone_empty(&self) -> SubgridEnum { + Self { + array: PackedArray::new([self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()]), + mu2_grid: self.mu2_grid.clone(), + x1_grid: self.x1_grid.clone(), + x2_grid: self.x2_grid.clone(), + } + .into() + } + + fn indexed_iter(&self) -> SubgridIndexedIter { + Box::new( + self.array + .indexed_iter() + .map(|([o, b, c], v)| ((o, b, c), v)), + ) + } + + fn stats(&self) -> Stats { + Stats { + total: self.mu2_grid.len() * self.x1_grid.len() * self.x2_grid.len(), + allocated: self.array.non_zeros() + self.array.explicit_zeros(), + zeros: self.array.explicit_zeros(), + overhead: self.array.overhead(), + bytes_per_value: mem::size_of::(), + } + } + + fn static_scale(&self) -> Option { + if let [static_scale] = self.mu2_grid.as_slice() { + Some(static_scale.clone()) + } else { + None + } + } +} + +impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { + fn from(subgrid: &SubgridEnum) -> Self { + // find smallest ranges + let (mu2_range, x1_range, x2_range) = subgrid.indexed_iter().fold( + ( + subgrid.mu2_grid().len()..0, + subgrid.x1_grid().len()..0, + subgrid.x2_grid().len()..0, + ), + |prev, ((imu2, ix1, ix2), _)| { + ( + prev.0.start.min(imu2)..prev.0.end.max(imu2 + 1), + prev.1.start.min(ix1)..prev.1.end.max(ix1 + 1), + prev.2.start.min(ix2)..prev.2.end.max(ix2 + 1), + ) + }, + ); + + let (mu2_grid, static_scale) = subgrid.static_scale().map_or_else( + || (subgrid.mu2_grid()[mu2_range.clone()].to_vec(), false), + |scale| (vec![scale], true), + ); + let x1_grid = subgrid.x1_grid()[x1_range.clone()].to_vec(); + let x2_grid = subgrid.x2_grid()[x2_range.clone()].to_vec(); + + let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + + for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { + // if there's a static scale we want every value to be added to same grid point + let index = if static_scale { + 0 + } else { + imu2 - mu2_range.start + }; + + array[[index, ix1 - x1_range.start, ix2 - x2_range.start]] += value; + } + + Self { + array, + mu2_grid, + x1_grid, + x2_grid, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_v1() { + let x = vec![ + 0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0, + ]; + let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( + PackedArray::new([1, 10, 10]), + vec![Mu2 { ren: 0.0, fac: 0.0 }], + x.clone(), + x.clone(), + ) + .into(); + + let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0 }]; + + assert_eq!(grid1.mu2_grid().as_ref(), mu2); + assert_eq!(grid1.x1_grid().as_ref(), x); + assert_eq!(grid1.x2_grid(), grid1.x1_grid()); + + assert!(grid1.is_empty()); + + // only use exactly representable numbers here so that we can avoid using approx_eq + if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid1 { + x.array_mut()[[0, 1, 2]] = 1.0; + x.array_mut()[[0, 1, 3]] = 2.0; + x.array_mut()[[0, 4, 3]] = 4.0; + x.array_mut()[[0, 7, 1]] = 8.0; + } else { + unreachable!(); + } + + assert!(!grid1.is_empty()); + + assert_eq!(grid1.indexed_iter().next(), Some(((0, 1, 2), 1.0))); + assert_eq!(grid1.indexed_iter().nth(1), Some(((0, 1, 3), 2.0))); + assert_eq!(grid1.indexed_iter().nth(2), Some(((0, 4, 3), 4.0))); + assert_eq!(grid1.indexed_iter().nth(3), Some(((0, 7, 1), 8.0))); + + // symmetric luminosity function + let lumi = + &mut (|ix1, ix2, _| x[ix1] * x[ix2]) as &mut dyn FnMut(usize, usize, usize) -> f64; + + assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 0.228515625); + + // create grid with transposed entries, but different q2 + let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( + PackedArray::new([1, 10, 10]), + vec![Mu2 { ren: 1.0, fac: 1.0 }], + x.clone(), + x.clone(), + ) + .into(); + if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid2 { + x.array_mut()[[0, 2, 1]] = 1.0; + x.array_mut()[[0, 3, 1]] = 2.0; + x.array_mut()[[0, 3, 4]] = 4.0; + x.array_mut()[[0, 1, 7]] = 8.0; + } else { + unreachable!(); + } + assert_eq!(grid2.convolve(&x, &x, &mu2, lumi), 0.228515625); + + assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); + assert_eq!(grid2.indexed_iter().nth(1), Some(((0, 2, 1), 1.0))); + assert_eq!(grid2.indexed_iter().nth(2), Some(((0, 3, 1), 2.0))); + assert_eq!(grid2.indexed_iter().nth(3), Some(((0, 3, 4), 4.0))); + + grid1.merge(&mut grid2, false); + + assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); + + let mut grid1 = { + let mut g = grid1.clone_empty(); + g.merge(&mut grid1, false); + g + }; + + // the luminosity function is symmetric, so after symmetrization the result must be + // unchanged + grid1.symmetrize(); + assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); + + grid1.scale(2.0); + assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 4.0 * 0.228515625); + + assert_eq!( + grid1.stats(), + Stats { + total: 200, + allocated: 14, + zeros: 6, + overhead: 42, + bytes_per_value: 8, + } + ); + } +} diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 1dce12ab..4d2cbe0c 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -5,6 +5,7 @@ use super::grid::Ntuple; use super::import_only_subgrid::{ImportOnlySubgridV1, ImportOnlySubgridV2}; use super::lagrange_subgrid::{LagrangeSparseSubgridV1, LagrangeSubgridV1, LagrangeSubgridV2}; use super::ntuple_subgrid::NtupleSubgridV1; +use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; use ndarray::Array3; use serde::{Deserialize, Serialize}; @@ -30,6 +31,8 @@ pub enum SubgridEnum { /// Same as [`ImportOnlySubgridV1`], but with support for different renormalization and /// factorization scales choices. ImportOnlySubgridV2, + /// + PackedQ1X2SubgridV1, } /// Structure denoting renormalization and factorization scale values. diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index 3cdd5323..e86d7e63 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -109,6 +109,7 @@ impl Subcommand for Opts { SubgridEnum::ImportOnlySubgridV1(_) => "ImportOnlySubgridV1", SubgridEnum::ImportOnlySubgridV2(_) => "ImportOnlySubgridV2", SubgridEnum::EmptySubgridV1(_) => "EmptySubgridV1", + SubgridEnum::PackedQ1X2SubgridV1(_) => "PackedQ1X2SubgridV1", } )); } From 5e4922a97a1965ab76b918ca77ab2958184a1337 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 15:39:59 +0200 Subject: [PATCH 019/213] Implement remaining todos --- pineappl/src/packed_subgrid.rs | 82 ++++++++++++++++------------------ 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 14bffb40..79eca0da 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -78,6 +78,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { if self.array.is_empty() && !transpose { mem::swap(&mut self.array, &mut other_grid.array); } else { + let rhs_mu2 = other_grid.mu2_grid().into_owned(); let rhs_x1 = if transpose { other_grid.x2_grid() } else { @@ -89,10 +90,17 @@ impl Subgrid for PackedQ1X2SubgridV1 { other_grid.x2_grid() }; - if (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { + if (self.mu2_grid != rhs_mu2) + || (self.x1_grid() != rhs_x1) + || (self.x2_grid() != rhs_x2) + { + let mut mu2_grid = self.mu2_grid.clone(); let mut x1_grid = self.x1_grid.clone(); let mut x2_grid = self.x2_grid.clone(); + mu2_grid.extend_from_slice(&rhs_mu2); + mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + mu2_grid.dedup(); x1_grid.extend_from_slice(&rhs_x1); x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); x1_grid.dedup(); @@ -101,9 +109,13 @@ impl Subgrid for PackedQ1X2SubgridV1 { x2_grid.dedup(); let mut array = - PackedArray::new([self.array.shape()[0], x1_grid.len(), x2_grid.len()]); + PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for ([i, j, k], value) in self.array.indexed_iter() { + let target_i = mu2_grid + .iter() + .position(|&ref mu2| *mu2 == self.mu2_grid[i]) + .unwrap_or_else(|| unreachable!()); let target_j = x1_grid .iter() .position(|&x| x == self.x1_grid[j]) @@ -113,47 +125,34 @@ impl Subgrid for PackedQ1X2SubgridV1 { .position(|&x| x == self.x2_grid[k]) .unwrap_or_else(|| unreachable!()); - array[[i, target_j, target_k]] = value; + array[[target_i, target_j, target_k]] = value; } self.array = array; + self.mu2_grid = mu2_grid; self.x1_grid = x1_grid; self.x2_grid = x2_grid; } - for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() { - let index = match self + for ([i, j, k], value) in other_grid.array.indexed_iter() { + let (j, k) = if transpose { (k, j) } else { (j, k) }; + let target_i = self .mu2_grid - .binary_search_by(|val| val.partial_cmp(mu2).unwrap()) - { - Ok(index) => index, - Err(index) => { - self.mu2_grid.insert(index, mu2.clone()); - //self.array.increase_x_at(index); - todo!(); - index - } - }; - - for ([_, j, k], value) in other_grid - .array - .indexed_iter() - .filter(|&([i, _, _], _)| i == other_index) - { - let (j, k) = if transpose { (k, j) } else { (j, k) }; - let target_j = self - .x1_grid - .iter() - .position(|&x| x == rhs_x1[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = self - .x2_grid - .iter() - .position(|&x| x == rhs_x2[k]) - .unwrap_or_else(|| unreachable!()); - - self.array[[index, target_j, target_k]] += value; - } + .iter() + .position(|&ref x| *x == rhs_mu2[i]) + .unwrap_or_else(|| unreachable!()); + let target_j = self + .x1_grid + .iter() + .position(|&x| x == rhs_x1[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = self + .x2_grid + .iter() + .position(|&x| x == rhs_x2[k]) + .unwrap_or_else(|| unreachable!()); + + self.array[[target_i, target_j, target_k]] += value; } } } else { @@ -162,12 +161,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn scale(&mut self, factor: f64) { - if factor == 0.0 { - self.array.clear(); - } else { - // self.array.iter_mut().for_each(|x| *x *= factor); - todo!(); - } + self.array *= factor; } fn symmetrize(&mut self) { @@ -362,9 +356,9 @@ mod tests { grid1.stats(), Stats { total: 200, - allocated: 14, - zeros: 6, - overhead: 42, + allocated: 8, + zeros: 0, + overhead: 12, bytes_per_value: 8, } ); From e6d6a96dff4e4bbe2a8f8aa4972bfbf7f6fdd5c2 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 5 Aug 2024 16:55:40 +0200 Subject: [PATCH 020/213] Start implementation of backwards-compatible v0 import --- Cargo.lock | 40 +++++++++++++++++++++++++++-------- Cargo.toml | 2 +- pineappl/Cargo.toml | 1 + pineappl/src/grid.rs | 45 +++++++++++++++++++++++++++++++++++++++- pineappl_capi/Cargo.toml | 2 +- pineappl_cli/Cargo.toml | 6 +++--- pineappl_py/Cargo.toml | 2 +- xtask/Cargo.toml | 2 +- 8 files changed, 83 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 164f0ca6..426c3a49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1226,6 +1226,27 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pineappl" version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88cf595833093c1a56fdd940c66bc1cd7f03ce162f552b29febf7be62f3eea74" +dependencies = [ + "anyhow", + "arrayvec", + "bincode", + "bitflags 2.4.2", + "enum_dispatch", + "float-cmp", + "git-version", + "itertools", + "lz4_flex", + "ndarray", + "rustc-hash", + "serde", + "thiserror", +] + +[[package]] +name = "pineappl" +version = "1.0.0-alpha1" dependencies = [ "anyhow", "arrayvec", @@ -1240,6 +1261,7 @@ dependencies = [ "ndarray", "ndarray-npy", "num-complex", + "pineappl 0.8.2", "rand", "rand_pcg", "rustc-hash", @@ -1250,7 +1272,7 @@ dependencies = [ [[package]] name = "pineappl_applgrid" -version = "0.8.2" +version = "1.0.0-alpha1" dependencies = [ "cc", "cxx", @@ -1261,15 +1283,15 @@ dependencies = [ [[package]] name = "pineappl_capi" -version = "0.8.2" +version = "1.0.0-alpha1" dependencies = [ "itertools", - "pineappl", + "pineappl 1.0.0-alpha1", ] [[package]] name = "pineappl_cli" -version = "0.8.2" +version = "1.0.0-alpha1" dependencies = [ "anyhow", "assert_cmd", @@ -1287,7 +1309,7 @@ dependencies = [ "managed-lhapdf", "ndarray", "ndarray-npy", - "pineappl", + "pineappl 1.0.0-alpha1", "pineappl_applgrid", "pineappl_fastnlo", "predicates", @@ -1300,7 +1322,7 @@ dependencies = [ [[package]] name = "pineappl_fastnlo" -version = "0.8.2" +version = "1.0.0-alpha1" dependencies = [ "cxx", "cxx-build", @@ -1310,12 +1332,12 @@ dependencies = [ [[package]] name = "pineappl_py" -version = "0.8.2" +version = "1.0.0-alpha1" dependencies = [ "itertools", "ndarray", "numpy", - "pineappl", + "pineappl 1.0.0-alpha1", "pyo3", ] @@ -2480,7 +2502,7 @@ dependencies = [ [[package]] name = "xtask" -version = "0.8.2" +version = "1.0.0-alpha1" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 5e3d2445..d239b76d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ keywords = ["high-energy-physics", "physics"] license = "GPL-3.0-or-later" repository = "https://github.com/NNPDF/pineappl" rust-version = "1.70.0" -version = "0.8.2" +version = "1.0.0-alpha1" [workspace.lints.clippy] all = { level = "warn", priority = -1 } diff --git a/pineappl/Cargo.toml b/pineappl/Cargo.toml index c279937e..9c124be2 100644 --- a/pineappl/Cargo.toml +++ b/pineappl/Cargo.toml @@ -26,6 +26,7 @@ git-version = "0.3.5" itertools = "0.10.1" lz4_flex = "0.9.2" ndarray = { features = ["serde"], version = "0.15.4" } +pineappl-v0 = { package = "pineappl", version = "0.8.2" } rustc-hash = "1.1.0" serde = { features = ["derive"], version = "1.0.130" } thiserror = "1.0.30" diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 0840c6ec..0f9e63d3 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -532,12 +532,55 @@ impl Grid { }; match file_version { - 0 => todo!(), + 0 => Self::read_uncompressed_v0(reader), 1 => bincode::deserialize_from(reader).map_err(GridError::ReadFailure), _ => Err(GridError::FileVersionUnsupported { file_version }), } } + fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { + use super::packed_array::PackedArray; + use super::packed_subgrid::PackedQ1X2SubgridV1; + use pineappl_v0::grid::Grid as GridV0; + use pineappl_v0::subgrid::Subgrid as _; + + // TODO: convert error from v0 to v1 + let grid = GridV0::read(&mut reader).unwrap(); + Ok(Self { + subgrids: Array3::from_shape_vec( + grid.subgrids().dim(), + grid.subgrids() + .into_iter() + .map(|subgrid| { + let mu2_grid: Vec<_> = subgrid + .mu2_grid() + .into_iter() + .map(|mu2v0| Mu2 { + ren: mu2v0.ren, + fac: mu2v0.fac, + }) + .collect(); + let x1_grid = subgrid.x1_grid().into_owned(); + let x2_grid = subgrid.x2_grid().into_owned(); + let mut array = + PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + for ((o, b, c), v) in subgrid.indexed_iter() { + array[[o, b, c]] = v; + } + PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() + }) + .collect(), + ) + // UNWRAP: the dimensions must be the same as in the v0 grid + .unwrap(), + channels: todo!(), + bin_limits: todo!(), + orders: todo!(), + subgrid_params: todo!(), + more_members: todo!(), + }) + } + /// Serializes `self` into `writer`. Writing is buffered. /// /// # Errors diff --git a/pineappl_capi/Cargo.toml b/pineappl_capi/Cargo.toml index eeb0d044..bfd90bdb 100644 --- a/pineappl_capi/Cargo.toml +++ b/pineappl_capi/Cargo.toml @@ -16,7 +16,7 @@ version.workspace = true workspace = true [dependencies] -pineappl = { path = "../pineappl", version = "=0.8.2" } +pineappl = { path = "../pineappl", version = "=1.0.0-alpha1" } itertools = "0.10.1" [features] diff --git a/pineappl_cli/Cargo.toml b/pineappl_cli/Cargo.toml index bc0bdadb..0e2ebe82 100644 --- a/pineappl_cli/Cargo.toml +++ b/pineappl_cli/Cargo.toml @@ -30,9 +30,9 @@ lhapdf = { package = "managed-lhapdf", version = "0.3.2" } lz4_flex = { optional = true, version = "0.9.2" } ndarray = "0.15.4" ndarray-npy = { optional = true, version = "0.8.1" } -pineappl = { path = "../pineappl", version = "=0.8.2" } -pineappl_applgrid = { optional = true, path = "../pineappl_applgrid", version = "=0.8.2" } -pineappl_fastnlo = { optional = true, path = "../pineappl_fastnlo", version = "=0.8.2" } +pineappl = { path = "../pineappl", version = "=1.0.0-alpha1" } +pineappl_applgrid = { optional = true, path = "../pineappl_applgrid", version = "=1.0.0-alpha1" } +pineappl_fastnlo = { optional = true, path = "../pineappl_fastnlo", version = "=1.0.0-alpha1" } prettytable-rs = { default-features = false, features = ["win_crlf"], version = "0.10.0" } rayon = "1.5.1" serde = { features = ["derive"], optional = true, version = "1.0.130" } diff --git a/pineappl_py/Cargo.toml b/pineappl_py/Cargo.toml index 526e3dff..134ce107 100644 --- a/pineappl_py/Cargo.toml +++ b/pineappl_py/Cargo.toml @@ -30,5 +30,5 @@ crate-type = ["cdylib"] itertools = "0.10.1" ndarray = "0.15.4" numpy = "0.21.0" -pineappl = { path = "../pineappl", version = "=0.8.2" } +pineappl = { path = "../pineappl", version = "=1.0.0-alpha1" } pyo3 = { features = ["extension-module"], version = "0.21.2" } diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 908d0fc6..78d343eb 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -21,4 +21,4 @@ clap_mangen = "0.2.18" enum_dispatch = "0.3.7" #git2 = "0.17.2" #semver = "1.0.17" -pineappl_cli = { path = "../pineappl_cli", version = "=0.8.2" } +pineappl_cli = { path = "../pineappl_cli", version = "=1.0.0-alpha1" } From 5ad4281a999cba7459aa687eaa8d4bb97c78d2e7 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 6 Aug 2024 11:54:24 +0200 Subject: [PATCH 021/213] Do not convert empty subgrids --- pineappl/src/grid.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 0f9e63d3..3a696b5b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -552,22 +552,26 @@ impl Grid { grid.subgrids() .into_iter() .map(|subgrid| { - let mu2_grid: Vec<_> = subgrid - .mu2_grid() - .into_iter() - .map(|mu2v0| Mu2 { - ren: mu2v0.ren, - fac: mu2v0.fac, - }) - .collect(); - let x1_grid = subgrid.x1_grid().into_owned(); - let x2_grid = subgrid.x2_grid().into_owned(); - let mut array = - PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - for ((o, b, c), v) in subgrid.indexed_iter() { - array[[o, b, c]] = v; + if subgrid.is_empty() { + EmptySubgridV1.into() + } else { + let mu2_grid: Vec<_> = subgrid + .mu2_grid() + .into_iter() + .map(|mu2v0| Mu2 { + ren: mu2v0.ren, + fac: mu2v0.fac, + }) + .collect(); + let x1_grid = subgrid.x1_grid().into_owned(); + let x2_grid = subgrid.x2_grid().into_owned(); + let mut array = + PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + for ((o, b, c), v) in subgrid.indexed_iter() { + array[[o, b, c]] = v; + } + PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() } - PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() }) .collect(), ) From 10da731cad1107f3dc8b9e9d5b4accad98dec3a4 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 6 Aug 2024 11:55:54 +0200 Subject: [PATCH 022/213] Convert the rest of the grid --- pineappl/src/grid.rs | 56 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 3a696b5b..4a997a03 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -546,7 +546,9 @@ impl Grid { // TODO: convert error from v0 to v1 let grid = GridV0::read(&mut reader).unwrap(); - Ok(Self { + + // TODO: convert differently if grid only has one convolution + let result = Self { subgrids: Array3::from_shape_vec( grid.subgrids().dim(), grid.subgrids() @@ -577,12 +579,52 @@ impl Grid { ) // UNWRAP: the dimensions must be the same as in the v0 grid .unwrap(), - channels: todo!(), - bin_limits: todo!(), - orders: todo!(), - subgrid_params: todo!(), - more_members: todo!(), - }) + channels: grid + .channels() + .iter() + .map(|c| Channel::new(c.entry().iter().map(|&(a, b, f)| (vec![a, b], f)).collect())) + .collect(), + // TODO: change this member to something much easier to handle + bin_limits: BinLimits::new(if grid.remapper().is_none() { + let limits = &grid.bin_info().limits(); + iter::once(limits[0][0].0) + .chain(limits.iter().map(|v| v[0].1)) + .collect() + } else { + // if there is a BinRemapper this member will likely have no impact + (0..=grid.bin_info().bins()) + .map(|i| f64::from(u16::try_from(i).unwrap())) + .collect() + }), + orders: grid + .orders() + .iter() + .map(|o| Order { + alphas: o.alphas, + alpha: o.alpha, + logxir: o.logxir, + logxif: o.logxif, + logxia: 0, + }) + .collect(), + // TODO: remove this member + subgrid_params: SubgridParams::default(), + // TODO: make these proper members + more_members: MoreMembers::V3(Mmv3 { + remapper: grid.remapper().map(|r| { + // UNWRAP: if the old grid could be constructed with the given normalizations + // and limits we should be able to do the same without error + BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() + }), + key_value_db: grid.key_values().cloned().unwrap_or_default(), + // TODO: remove this member + subgrid_template: EmptySubgridV1.into(), + }), + }; + + assert_eq!(result.bin_info().bins(), grid.bin_info().bins()); + + Ok(result) } /// Serializes `self` into `writer`. Writing is buffered. From 2416f2006a91b7495f8f270b08c6aedba3df2708 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 6 Aug 2024 11:55:13 +0200 Subject: [PATCH 023/213] Fix more tests --- pineappl_cli/src/read.rs | 2 +- pineappl_cli/tests/export.rs | 12 +- pineappl_cli/tests/read.rs | 36 +- pineappl_cli/tests/subgrids.rs | 624 ++++++++++++++++----------------- 4 files changed, 337 insertions(+), 337 deletions(-) diff --git a/pineappl_cli/src/read.rs b/pineappl_cli/src/read.rs index c7f8c447..a2692367 100644 --- a/pineappl_cli/src/read.rs +++ b/pineappl_cli/src/read.rs @@ -127,7 +127,7 @@ impl Subcommand for Opts { for (pids, factor) in channel.entry() { row.add_cell(cell!(format!( "{factor} \u{d7} ({})", - pids.iter().map(ToString::to_string).join(", ") + pids.iter().map(|pid| format!("{pid:2}")).join(", ") ))); } } diff --git a/pineappl_cli/tests/export.rs b/pineappl_cli/tests/export.rs index 47fe293a..33b748f2 100644 --- a/pineappl_cli/tests/export.rs +++ b/pineappl_cli/tests/export.rs @@ -23,11 +23,11 @@ Options: #[cfg(feature = "applgrid")] const EXPORT_APPLGRID_STR: &str = - "WARNING: the order O(as^1 a^2 lr^1 lf^0) isn't supported by APPLgrid and will be skipped. -WARNING: the order O(as^1 a^2 lr^0 lf^1) isn't supported by APPLgrid and will be skipped. -WARNING: the order O(as^0 a^3 lr^0 lf^0) isn't supported by APPLgrid and will be skipped. -WARNING: the order O(as^0 a^3 lr^1 lf^0) isn't supported by APPLgrid and will be skipped. -WARNING: the order O(as^0 a^3 lr^0 lf^1) isn't supported by APPLgrid and will be skipped. + "WARNING: the order O(as^1 a^2 lr^1 lf^0 la^0) isn't supported by APPLgrid and will be skipped. +WARNING: the order O(as^1 a^2 lr^0 lf^1 la^0) isn't supported by APPLgrid and will be skipped. +WARNING: the order O(as^0 a^3 lr^0 lf^0 la^0) isn't supported by APPLgrid and will be skipped. +WARNING: the order O(as^0 a^3 lr^1 lf^0 la^0) isn't supported by APPLgrid and will be skipped. +WARNING: the order O(as^0 a^3 lr^0 lf^1 la^0) isn't supported by APPLgrid and will be skipped. b APPLgrid PineAPPL rel. diff --+------------+------------+-------------- 0 7.9566291e0 7.9566291e0 -2.5535130e-15 @@ -52,7 +52,7 @@ b APPLgrid PineAPPL rel. diff #[cfg(feature = "applgrid")] const EXPORT_DIS_APPLGRID_STR: &str = - "WARNING: the order O(as^1 a^0 lr^0 lf^1) isn't supported by APPLgrid and will be skipped. + "WARNING: the order O(as^1 a^0 lr^0 lf^1 la^0) isn't supported by APPLgrid and will be skipped. b APPLgrid PineAPPL rel. diff --+-----------+-----------+-------------- 0 2.8829972e0 2.8829972e0 3.3306691e-15 diff --git a/pineappl_cli/tests/read.rs b/pineappl_cli/tests/read.rs index 31ad9fab..f645362c 100644 --- a/pineappl_cli/tests/read.rs +++ b/pineappl_cli/tests/read.rs @@ -54,26 +54,26 @@ const ORDERS_STR: &str = "o order 6 O(a^3 lf^1) "; -const ORDERS_LONG_STR: &str = "o order --+--------------------- -0 O(as^0 a^2 lr^0 lf^0) -1 O(as^1 a^2 lr^0 lf^0) -2 O(as^1 a^2 lr^1 lf^0) -3 O(as^1 a^2 lr^0 lf^1) -4 O(as^0 a^3 lr^0 lf^0) -5 O(as^0 a^3 lr^1 lf^0) -6 O(as^0 a^3 lr^0 lf^1) +const ORDERS_LONG_STR: &str = "o order +-+-------------------------- +0 O(as^0 a^2 lr^0 lf^0 la^0) +1 O(as^1 a^2 lr^0 lf^0 la^0) +2 O(as^1 a^2 lr^1 lf^0 la^0) +3 O(as^1 a^2 lr^0 lf^1 la^0) +4 O(as^0 a^3 lr^0 lf^0 la^0) +5 O(as^0 a^3 lr^1 lf^0 la^0) +6 O(as^0 a^3 lr^0 lf^1 la^0) "; -const ORDERS_SPACES_STR: &str = "o order --+--------------------- -0 O( a^2 ) -1 O(as^1 a^2 ) -2 O(as^1 a^2 lr^1 ) -3 O(as^1 a^2 lf^1) -4 O( a^3 ) -5 O( a^3 lr^1 ) -6 O( a^3 lf^1) +const ORDERS_SPACES_STR: &str = "o order +-+-------------------------- +0 O( a^2 ) +1 O(as^1 a^2 ) +2 O(as^1 a^2 lr^1 ) +3 O(as^1 a^2 lf^1 ) +4 O( a^3 ) +5 O( a^3 lr^1 ) +6 O( a^3 lf^1 ) "; const FKTABLE_STR: &str = "no diff --git a/pineappl_cli/tests/subgrids.rs b/pineappl_cli/tests/subgrids.rs index 5e4cd977..bf6707bd 100644 --- a/pineappl_cli/tests/subgrids.rs +++ b/pineappl_cli/tests/subgrids.rs @@ -455,301 +455,301 @@ const MUR2_STR: &str = "o b c mur2 const STATS_STR: &str = "o b c total allocated zeros overhead -+-+-+-----+---------+-----+-------- -0 0 0 2500 1012 3 102 -0 1 0 2500 1009 8 102 -0 2 0 2500 1018 16 102 -0 3 0 2500 1006 16 102 -0 4 0 2500 983 21 102 -0 5 0 2500 945 45 102 -0 6 0 2500 1025 77 102 -0 7 0 2500 833 37 102 -1 0 0 2500 1013 0 102 -1 0 1 2500 1009 0 102 -1 0 3 2500 1011 0 102 -1 1 0 2500 1016 0 102 -1 1 1 2500 1005 0 102 -1 1 3 2500 1011 0 102 -1 2 0 2500 1031 0 102 -1 2 1 2500 1022 0 102 -1 2 3 2500 1030 0 102 -1 3 0 2500 1031 0 102 -1 3 1 2500 1026 0 102 -1 3 3 2500 1030 0 102 -1 4 0 2500 1038 2 102 -1 4 1 2500 1025 0 102 -1 4 3 2500 1027 0 102 -1 5 0 2500 1039 0 102 -1 5 1 2500 1022 2 102 -1 5 3 2500 1032 0 102 -1 6 0 2500 1033 0 102 -1 6 1 2500 1029 0 102 -1 6 3 2500 1032 0 102 -1 7 0 2500 982 0 102 -1 7 1 2500 980 2 102 -1 7 3 2500 981 0 102 -3 0 0 2500 1013 0 102 -3 0 1 2500 952 0 102 -3 0 3 2500 1011 2 102 -3 1 0 2500 1016 0 102 -3 1 1 2500 953 7 102 -3 1 3 2500 1005 0 102 -3 2 0 2500 1031 0 102 -3 2 1 2500 1018 6 102 -3 2 3 2500 1029 0 102 -3 3 0 2500 1031 0 102 -3 3 1 2500 998 0 102 -3 3 3 2500 1027 0 102 -3 4 0 2500 1038 2 102 -3 4 1 2500 1022 10 102 -3 4 3 2500 1021 0 102 -3 5 0 2500 1039 0 102 -3 5 1 2500 1007 18 102 -3 5 3 2500 1028 0 102 -3 6 0 2500 1033 0 102 -3 6 1 2500 1027 11 102 -3 6 3 2500 1028 0 102 -3 7 0 2500 982 0 102 -3 7 1 2500 976 58 102 -3 7 3 2500 973 0 102 -4 0 0 2500 1013 0 102 -4 0 2 2500 936 20 102 -4 0 4 2500 930 10 102 -4 1 0 2500 1013 0 102 -4 1 2 2500 938 20 102 -4 1 4 2500 949 16 102 -4 2 0 2500 1026 0 102 -4 2 2 2500 933 32 102 -4 2 4 2500 943 0 102 -4 3 0 2500 1031 0 102 -4 3 2 2500 884 6 102 -4 3 4 2500 983 11 102 -4 4 0 2500 1038 3 102 -4 4 2 2500 894 37 102 -4 4 4 2500 940 0 102 -4 5 0 2500 1035 0 102 -4 5 2 2500 869 16 102 -4 5 4 2500 962 14 102 -4 6 0 2500 1033 0 102 -4 6 2 2500 861 21 102 -4 6 4 2500 969 22 102 -4 7 0 2500 980 0 102 -4 7 2 2500 873 114 102 -4 7 4 2500 817 38 102 -6 0 0 2500 1013 3 102 -6 0 2 2500 767 37 102 -6 0 4 2500 868 11 102 -6 1 0 2500 1013 1 102 -6 1 2 2500 710 7 102 -6 1 4 2500 854 17 102 -6 2 0 2500 1018 3 102 -6 2 2 2500 832 22 102 -6 2 4 2500 898 17 102 -6 3 0 2500 1023 0 102 -6 3 2 2500 840 54 102 -6 3 4 2500 913 39 102 -6 4 0 2500 1038 3 102 -6 4 2 2500 825 80 102 -6 4 4 2500 877 9 102 -6 5 0 2500 1035 0 102 -6 5 2 2500 841 72 102 -6 5 4 2500 911 20 102 -6 6 0 2500 1033 0 102 -6 6 2 2500 855 116 102 -6 6 4 2500 867 13 102 -6 7 0 2500 980 0 102 -6 7 2 2500 869 173 102 -6 7 4 2500 745 36 102 +0 0 0 2500 1010 1 70 +0 1 0 2500 1003 2 72 +0 2 0 2500 1007 5 80 +0 3 0 2500 995 5 80 +0 4 0 2500 963 1 84 +0 5 0 2500 902 2 90 +0 6 0 2500 956 8 100 +0 7 0 2500 798 2 92 +1 0 0 2500 1013 0 68 +1 0 1 2500 1009 0 68 +1 0 3 2500 1011 0 68 +1 1 0 2500 1016 0 68 +1 1 1 2500 1005 0 68 +1 1 3 2500 1011 0 68 +1 2 0 2500 1031 0 70 +1 2 1 2500 1022 0 70 +1 2 3 2500 1030 0 70 +1 3 0 2500 1031 0 72 +1 3 1 2500 1026 0 72 +1 3 3 2500 1030 0 72 +1 4 0 2500 1036 0 74 +1 4 1 2500 1025 0 72 +1 4 3 2500 1027 0 72 +1 5 0 2500 1039 0 74 +1 5 1 2500 1020 0 76 +1 5 3 2500 1032 0 74 +1 6 0 2500 1033 0 76 +1 6 1 2500 1029 0 76 +1 6 3 2500 1032 0 76 +1 7 0 2500 982 0 78 +1 7 1 2500 980 2 78 +1 7 3 2500 981 0 78 +3 0 0 2500 1013 0 68 +3 0 1 2500 952 0 68 +3 0 3 2500 1009 0 70 +3 1 0 2500 1016 0 68 +3 1 1 2500 946 0 72 +3 1 3 2500 1005 0 68 +3 2 0 2500 1031 0 70 +3 2 1 2500 1016 4 72 +3 2 3 2500 1029 0 70 +3 3 0 2500 1031 0 72 +3 3 1 2500 998 0 72 +3 3 3 2500 1027 0 72 +3 4 0 2500 1036 0 74 +3 4 1 2500 1017 5 76 +3 4 3 2500 1021 0 72 +3 5 0 2500 1039 0 74 +3 5 1 2500 989 0 84 +3 5 3 2500 1028 0 74 +3 6 0 2500 1033 0 76 +3 6 1 2500 1017 1 80 +3 6 3 2500 1028 0 76 +3 7 0 2500 982 0 78 +3 7 1 2500 918 0 110 +3 7 3 2500 973 0 78 +4 0 0 2500 1013 0 68 +4 0 2 2500 921 5 80 +4 0 4 2500 920 0 78 +4 1 0 2500 1013 0 68 +4 1 2 2500 920 2 76 +4 1 4 2500 933 0 76 +4 2 0 2500 1026 0 70 +4 2 2 2500 901 0 80 +4 2 4 2500 943 0 70 +4 3 0 2500 1031 0 72 +4 3 2 2500 878 0 74 +4 3 4 2500 976 4 76 +4 4 0 2500 1035 0 74 +4 4 2 2500 860 3 82 +4 4 4 2500 940 0 72 +4 5 0 2500 1035 0 74 +4 5 2 2500 857 4 82 +4 5 4 2500 948 0 82 +4 6 0 2500 1033 0 76 +4 6 2 2500 844 4 86 +4 6 4 2500 947 0 82 +4 7 0 2500 980 0 78 +4 7 2 2500 759 0 116 +4 7 4 2500 779 0 88 +6 0 0 2500 1011 1 70 +6 0 2 2500 731 1 84 +6 0 4 2500 859 2 72 +6 1 0 2500 1013 1 68 +6 1 2 2500 710 7 68 +6 1 4 2500 837 0 78 +6 2 0 2500 1016 1 72 +6 2 2 2500 816 6 86 +6 2 4 2500 882 1 76 +6 3 0 2500 1023 0 72 +6 3 2 2500 791 5 104 +6 3 4 2500 876 2 86 +6 4 0 2500 1035 0 74 +6 4 2 2500 745 0 110 +6 4 4 2500 868 0 76 +6 5 0 2500 1035 0 74 +6 5 2 2500 771 2 108 +6 5 4 2500 893 2 78 +6 6 0 2500 1033 0 76 +6 6 2 2500 739 0 112 +6 6 4 2500 855 1 78 +6 7 0 2500 980 0 78 +6 7 2 2500 696 0 116 +6 7 4 2500 709 0 84 "; const TYPE_STR: &str = "o b c type -+-+-+------------------- -0 0 0 ImportOnlySubgridV1 -0 1 0 ImportOnlySubgridV1 -0 2 0 ImportOnlySubgridV1 -0 3 0 ImportOnlySubgridV1 -0 4 0 ImportOnlySubgridV1 -0 5 0 ImportOnlySubgridV1 -0 6 0 ImportOnlySubgridV1 -0 7 0 ImportOnlySubgridV1 -1 0 0 ImportOnlySubgridV1 -1 0 1 ImportOnlySubgridV1 -1 0 3 ImportOnlySubgridV1 -1 1 0 ImportOnlySubgridV1 -1 1 1 ImportOnlySubgridV1 -1 1 3 ImportOnlySubgridV1 -1 2 0 ImportOnlySubgridV1 -1 2 1 ImportOnlySubgridV1 -1 2 3 ImportOnlySubgridV1 -1 3 0 ImportOnlySubgridV1 -1 3 1 ImportOnlySubgridV1 -1 3 3 ImportOnlySubgridV1 -1 4 0 ImportOnlySubgridV1 -1 4 1 ImportOnlySubgridV1 -1 4 3 ImportOnlySubgridV1 -1 5 0 ImportOnlySubgridV1 -1 5 1 ImportOnlySubgridV1 -1 5 3 ImportOnlySubgridV1 -1 6 0 ImportOnlySubgridV1 -1 6 1 ImportOnlySubgridV1 -1 6 3 ImportOnlySubgridV1 -1 7 0 ImportOnlySubgridV1 -1 7 1 ImportOnlySubgridV1 -1 7 3 ImportOnlySubgridV1 -3 0 0 ImportOnlySubgridV1 -3 0 1 ImportOnlySubgridV1 -3 0 3 ImportOnlySubgridV1 -3 1 0 ImportOnlySubgridV1 -3 1 1 ImportOnlySubgridV1 -3 1 3 ImportOnlySubgridV1 -3 2 0 ImportOnlySubgridV1 -3 2 1 ImportOnlySubgridV1 -3 2 3 ImportOnlySubgridV1 -3 3 0 ImportOnlySubgridV1 -3 3 1 ImportOnlySubgridV1 -3 3 3 ImportOnlySubgridV1 -3 4 0 ImportOnlySubgridV1 -3 4 1 ImportOnlySubgridV1 -3 4 3 ImportOnlySubgridV1 -3 5 0 ImportOnlySubgridV1 -3 5 1 ImportOnlySubgridV1 -3 5 3 ImportOnlySubgridV1 -3 6 0 ImportOnlySubgridV1 -3 6 1 ImportOnlySubgridV1 -3 6 3 ImportOnlySubgridV1 -3 7 0 ImportOnlySubgridV1 -3 7 1 ImportOnlySubgridV1 -3 7 3 ImportOnlySubgridV1 -4 0 0 ImportOnlySubgridV1 -4 0 2 ImportOnlySubgridV1 -4 0 4 ImportOnlySubgridV1 -4 1 0 ImportOnlySubgridV1 -4 1 2 ImportOnlySubgridV1 -4 1 4 ImportOnlySubgridV1 -4 2 0 ImportOnlySubgridV1 -4 2 2 ImportOnlySubgridV1 -4 2 4 ImportOnlySubgridV1 -4 3 0 ImportOnlySubgridV1 -4 3 2 ImportOnlySubgridV1 -4 3 4 ImportOnlySubgridV1 -4 4 0 ImportOnlySubgridV1 -4 4 2 ImportOnlySubgridV1 -4 4 4 ImportOnlySubgridV1 -4 5 0 ImportOnlySubgridV1 -4 5 2 ImportOnlySubgridV1 -4 5 4 ImportOnlySubgridV1 -4 6 0 ImportOnlySubgridV1 -4 6 2 ImportOnlySubgridV1 -4 6 4 ImportOnlySubgridV1 -4 7 0 ImportOnlySubgridV1 -4 7 2 ImportOnlySubgridV1 -4 7 4 ImportOnlySubgridV1 -6 0 0 ImportOnlySubgridV1 -6 0 2 ImportOnlySubgridV1 -6 0 4 ImportOnlySubgridV1 -6 1 0 ImportOnlySubgridV1 -6 1 2 ImportOnlySubgridV1 -6 1 4 ImportOnlySubgridV1 -6 2 0 ImportOnlySubgridV1 -6 2 2 ImportOnlySubgridV1 -6 2 4 ImportOnlySubgridV1 -6 3 0 ImportOnlySubgridV1 -6 3 2 ImportOnlySubgridV1 -6 3 4 ImportOnlySubgridV1 -6 4 0 ImportOnlySubgridV1 -6 4 2 ImportOnlySubgridV1 -6 4 4 ImportOnlySubgridV1 -6 5 0 ImportOnlySubgridV1 -6 5 2 ImportOnlySubgridV1 -6 5 4 ImportOnlySubgridV1 -6 6 0 ImportOnlySubgridV1 -6 6 2 ImportOnlySubgridV1 -6 6 4 ImportOnlySubgridV1 -6 7 0 ImportOnlySubgridV1 -6 7 2 ImportOnlySubgridV1 -6 7 4 ImportOnlySubgridV1 +0 0 0 PackedQ1X2SubgridV1 +0 1 0 PackedQ1X2SubgridV1 +0 2 0 PackedQ1X2SubgridV1 +0 3 0 PackedQ1X2SubgridV1 +0 4 0 PackedQ1X2SubgridV1 +0 5 0 PackedQ1X2SubgridV1 +0 6 0 PackedQ1X2SubgridV1 +0 7 0 PackedQ1X2SubgridV1 +1 0 0 PackedQ1X2SubgridV1 +1 0 1 PackedQ1X2SubgridV1 +1 0 3 PackedQ1X2SubgridV1 +1 1 0 PackedQ1X2SubgridV1 +1 1 1 PackedQ1X2SubgridV1 +1 1 3 PackedQ1X2SubgridV1 +1 2 0 PackedQ1X2SubgridV1 +1 2 1 PackedQ1X2SubgridV1 +1 2 3 PackedQ1X2SubgridV1 +1 3 0 PackedQ1X2SubgridV1 +1 3 1 PackedQ1X2SubgridV1 +1 3 3 PackedQ1X2SubgridV1 +1 4 0 PackedQ1X2SubgridV1 +1 4 1 PackedQ1X2SubgridV1 +1 4 3 PackedQ1X2SubgridV1 +1 5 0 PackedQ1X2SubgridV1 +1 5 1 PackedQ1X2SubgridV1 +1 5 3 PackedQ1X2SubgridV1 +1 6 0 PackedQ1X2SubgridV1 +1 6 1 PackedQ1X2SubgridV1 +1 6 3 PackedQ1X2SubgridV1 +1 7 0 PackedQ1X2SubgridV1 +1 7 1 PackedQ1X2SubgridV1 +1 7 3 PackedQ1X2SubgridV1 +3 0 0 PackedQ1X2SubgridV1 +3 0 1 PackedQ1X2SubgridV1 +3 0 3 PackedQ1X2SubgridV1 +3 1 0 PackedQ1X2SubgridV1 +3 1 1 PackedQ1X2SubgridV1 +3 1 3 PackedQ1X2SubgridV1 +3 2 0 PackedQ1X2SubgridV1 +3 2 1 PackedQ1X2SubgridV1 +3 2 3 PackedQ1X2SubgridV1 +3 3 0 PackedQ1X2SubgridV1 +3 3 1 PackedQ1X2SubgridV1 +3 3 3 PackedQ1X2SubgridV1 +3 4 0 PackedQ1X2SubgridV1 +3 4 1 PackedQ1X2SubgridV1 +3 4 3 PackedQ1X2SubgridV1 +3 5 0 PackedQ1X2SubgridV1 +3 5 1 PackedQ1X2SubgridV1 +3 5 3 PackedQ1X2SubgridV1 +3 6 0 PackedQ1X2SubgridV1 +3 6 1 PackedQ1X2SubgridV1 +3 6 3 PackedQ1X2SubgridV1 +3 7 0 PackedQ1X2SubgridV1 +3 7 1 PackedQ1X2SubgridV1 +3 7 3 PackedQ1X2SubgridV1 +4 0 0 PackedQ1X2SubgridV1 +4 0 2 PackedQ1X2SubgridV1 +4 0 4 PackedQ1X2SubgridV1 +4 1 0 PackedQ1X2SubgridV1 +4 1 2 PackedQ1X2SubgridV1 +4 1 4 PackedQ1X2SubgridV1 +4 2 0 PackedQ1X2SubgridV1 +4 2 2 PackedQ1X2SubgridV1 +4 2 4 PackedQ1X2SubgridV1 +4 3 0 PackedQ1X2SubgridV1 +4 3 2 PackedQ1X2SubgridV1 +4 3 4 PackedQ1X2SubgridV1 +4 4 0 PackedQ1X2SubgridV1 +4 4 2 PackedQ1X2SubgridV1 +4 4 4 PackedQ1X2SubgridV1 +4 5 0 PackedQ1X2SubgridV1 +4 5 2 PackedQ1X2SubgridV1 +4 5 4 PackedQ1X2SubgridV1 +4 6 0 PackedQ1X2SubgridV1 +4 6 2 PackedQ1X2SubgridV1 +4 6 4 PackedQ1X2SubgridV1 +4 7 0 PackedQ1X2SubgridV1 +4 7 2 PackedQ1X2SubgridV1 +4 7 4 PackedQ1X2SubgridV1 +6 0 0 PackedQ1X2SubgridV1 +6 0 2 PackedQ1X2SubgridV1 +6 0 4 PackedQ1X2SubgridV1 +6 1 0 PackedQ1X2SubgridV1 +6 1 2 PackedQ1X2SubgridV1 +6 1 4 PackedQ1X2SubgridV1 +6 2 0 PackedQ1X2SubgridV1 +6 2 2 PackedQ1X2SubgridV1 +6 2 4 PackedQ1X2SubgridV1 +6 3 0 PackedQ1X2SubgridV1 +6 3 2 PackedQ1X2SubgridV1 +6 3 4 PackedQ1X2SubgridV1 +6 4 0 PackedQ1X2SubgridV1 +6 4 2 PackedQ1X2SubgridV1 +6 4 4 PackedQ1X2SubgridV1 +6 5 0 PackedQ1X2SubgridV1 +6 5 2 PackedQ1X2SubgridV1 +6 5 4 PackedQ1X2SubgridV1 +6 6 0 PackedQ1X2SubgridV1 +6 6 2 PackedQ1X2SubgridV1 +6 6 4 PackedQ1X2SubgridV1 +6 7 0 PackedQ1X2SubgridV1 +6 7 2 PackedQ1X2SubgridV1 +6 7 4 PackedQ1X2SubgridV1 "; const TYPE_SHOW_EMPTY_STR: &str = "o b c type -+-+-+------------------- -0 0 0 ImportOnlySubgridV1 +0 0 0 PackedQ1X2SubgridV1 0 0 1 EmptySubgridV1 0 0 2 EmptySubgridV1 0 0 3 EmptySubgridV1 0 0 4 EmptySubgridV1 -0 1 0 ImportOnlySubgridV1 +0 1 0 PackedQ1X2SubgridV1 0 1 1 EmptySubgridV1 0 1 2 EmptySubgridV1 0 1 3 EmptySubgridV1 0 1 4 EmptySubgridV1 -0 2 0 ImportOnlySubgridV1 +0 2 0 PackedQ1X2SubgridV1 0 2 1 EmptySubgridV1 0 2 2 EmptySubgridV1 0 2 3 EmptySubgridV1 0 2 4 EmptySubgridV1 -0 3 0 ImportOnlySubgridV1 +0 3 0 PackedQ1X2SubgridV1 0 3 1 EmptySubgridV1 0 3 2 EmptySubgridV1 0 3 3 EmptySubgridV1 0 3 4 EmptySubgridV1 -0 4 0 ImportOnlySubgridV1 +0 4 0 PackedQ1X2SubgridV1 0 4 1 EmptySubgridV1 0 4 2 EmptySubgridV1 0 4 3 EmptySubgridV1 0 4 4 EmptySubgridV1 -0 5 0 ImportOnlySubgridV1 +0 5 0 PackedQ1X2SubgridV1 0 5 1 EmptySubgridV1 0 5 2 EmptySubgridV1 0 5 3 EmptySubgridV1 0 5 4 EmptySubgridV1 -0 6 0 ImportOnlySubgridV1 +0 6 0 PackedQ1X2SubgridV1 0 6 1 EmptySubgridV1 0 6 2 EmptySubgridV1 0 6 3 EmptySubgridV1 0 6 4 EmptySubgridV1 -0 7 0 ImportOnlySubgridV1 +0 7 0 PackedQ1X2SubgridV1 0 7 1 EmptySubgridV1 0 7 2 EmptySubgridV1 0 7 3 EmptySubgridV1 0 7 4 EmptySubgridV1 -1 0 0 ImportOnlySubgridV1 -1 0 1 ImportOnlySubgridV1 +1 0 0 PackedQ1X2SubgridV1 +1 0 1 PackedQ1X2SubgridV1 1 0 2 EmptySubgridV1 -1 0 3 ImportOnlySubgridV1 +1 0 3 PackedQ1X2SubgridV1 1 0 4 EmptySubgridV1 -1 1 0 ImportOnlySubgridV1 -1 1 1 ImportOnlySubgridV1 +1 1 0 PackedQ1X2SubgridV1 +1 1 1 PackedQ1X2SubgridV1 1 1 2 EmptySubgridV1 -1 1 3 ImportOnlySubgridV1 +1 1 3 PackedQ1X2SubgridV1 1 1 4 EmptySubgridV1 -1 2 0 ImportOnlySubgridV1 -1 2 1 ImportOnlySubgridV1 +1 2 0 PackedQ1X2SubgridV1 +1 2 1 PackedQ1X2SubgridV1 1 2 2 EmptySubgridV1 -1 2 3 ImportOnlySubgridV1 +1 2 3 PackedQ1X2SubgridV1 1 2 4 EmptySubgridV1 -1 3 0 ImportOnlySubgridV1 -1 3 1 ImportOnlySubgridV1 +1 3 0 PackedQ1X2SubgridV1 +1 3 1 PackedQ1X2SubgridV1 1 3 2 EmptySubgridV1 -1 3 3 ImportOnlySubgridV1 +1 3 3 PackedQ1X2SubgridV1 1 3 4 EmptySubgridV1 -1 4 0 ImportOnlySubgridV1 -1 4 1 ImportOnlySubgridV1 +1 4 0 PackedQ1X2SubgridV1 +1 4 1 PackedQ1X2SubgridV1 1 4 2 EmptySubgridV1 -1 4 3 ImportOnlySubgridV1 +1 4 3 PackedQ1X2SubgridV1 1 4 4 EmptySubgridV1 -1 5 0 ImportOnlySubgridV1 -1 5 1 ImportOnlySubgridV1 +1 5 0 PackedQ1X2SubgridV1 +1 5 1 PackedQ1X2SubgridV1 1 5 2 EmptySubgridV1 -1 5 3 ImportOnlySubgridV1 +1 5 3 PackedQ1X2SubgridV1 1 5 4 EmptySubgridV1 -1 6 0 ImportOnlySubgridV1 -1 6 1 ImportOnlySubgridV1 +1 6 0 PackedQ1X2SubgridV1 +1 6 1 PackedQ1X2SubgridV1 1 6 2 EmptySubgridV1 -1 6 3 ImportOnlySubgridV1 +1 6 3 PackedQ1X2SubgridV1 1 6 4 EmptySubgridV1 -1 7 0 ImportOnlySubgridV1 -1 7 1 ImportOnlySubgridV1 +1 7 0 PackedQ1X2SubgridV1 +1 7 1 PackedQ1X2SubgridV1 1 7 2 EmptySubgridV1 -1 7 3 ImportOnlySubgridV1 +1 7 3 PackedQ1X2SubgridV1 1 7 4 EmptySubgridV1 2 0 0 EmptySubgridV1 2 0 1 EmptySubgridV1 @@ -791,86 +791,86 @@ const TYPE_SHOW_EMPTY_STR: &str = "o b c type 2 7 2 EmptySubgridV1 2 7 3 EmptySubgridV1 2 7 4 EmptySubgridV1 -3 0 0 ImportOnlySubgridV1 -3 0 1 ImportOnlySubgridV1 +3 0 0 PackedQ1X2SubgridV1 +3 0 1 PackedQ1X2SubgridV1 3 0 2 EmptySubgridV1 -3 0 3 ImportOnlySubgridV1 +3 0 3 PackedQ1X2SubgridV1 3 0 4 EmptySubgridV1 -3 1 0 ImportOnlySubgridV1 -3 1 1 ImportOnlySubgridV1 +3 1 0 PackedQ1X2SubgridV1 +3 1 1 PackedQ1X2SubgridV1 3 1 2 EmptySubgridV1 -3 1 3 ImportOnlySubgridV1 +3 1 3 PackedQ1X2SubgridV1 3 1 4 EmptySubgridV1 -3 2 0 ImportOnlySubgridV1 -3 2 1 ImportOnlySubgridV1 +3 2 0 PackedQ1X2SubgridV1 +3 2 1 PackedQ1X2SubgridV1 3 2 2 EmptySubgridV1 -3 2 3 ImportOnlySubgridV1 +3 2 3 PackedQ1X2SubgridV1 3 2 4 EmptySubgridV1 -3 3 0 ImportOnlySubgridV1 -3 3 1 ImportOnlySubgridV1 +3 3 0 PackedQ1X2SubgridV1 +3 3 1 PackedQ1X2SubgridV1 3 3 2 EmptySubgridV1 -3 3 3 ImportOnlySubgridV1 +3 3 3 PackedQ1X2SubgridV1 3 3 4 EmptySubgridV1 -3 4 0 ImportOnlySubgridV1 -3 4 1 ImportOnlySubgridV1 +3 4 0 PackedQ1X2SubgridV1 +3 4 1 PackedQ1X2SubgridV1 3 4 2 EmptySubgridV1 -3 4 3 ImportOnlySubgridV1 +3 4 3 PackedQ1X2SubgridV1 3 4 4 EmptySubgridV1 -3 5 0 ImportOnlySubgridV1 -3 5 1 ImportOnlySubgridV1 +3 5 0 PackedQ1X2SubgridV1 +3 5 1 PackedQ1X2SubgridV1 3 5 2 EmptySubgridV1 -3 5 3 ImportOnlySubgridV1 +3 5 3 PackedQ1X2SubgridV1 3 5 4 EmptySubgridV1 -3 6 0 ImportOnlySubgridV1 -3 6 1 ImportOnlySubgridV1 +3 6 0 PackedQ1X2SubgridV1 +3 6 1 PackedQ1X2SubgridV1 3 6 2 EmptySubgridV1 -3 6 3 ImportOnlySubgridV1 +3 6 3 PackedQ1X2SubgridV1 3 6 4 EmptySubgridV1 -3 7 0 ImportOnlySubgridV1 -3 7 1 ImportOnlySubgridV1 +3 7 0 PackedQ1X2SubgridV1 +3 7 1 PackedQ1X2SubgridV1 3 7 2 EmptySubgridV1 -3 7 3 ImportOnlySubgridV1 +3 7 3 PackedQ1X2SubgridV1 3 7 4 EmptySubgridV1 -4 0 0 ImportOnlySubgridV1 +4 0 0 PackedQ1X2SubgridV1 4 0 1 EmptySubgridV1 -4 0 2 ImportOnlySubgridV1 +4 0 2 PackedQ1X2SubgridV1 4 0 3 EmptySubgridV1 -4 0 4 ImportOnlySubgridV1 -4 1 0 ImportOnlySubgridV1 +4 0 4 PackedQ1X2SubgridV1 +4 1 0 PackedQ1X2SubgridV1 4 1 1 EmptySubgridV1 -4 1 2 ImportOnlySubgridV1 +4 1 2 PackedQ1X2SubgridV1 4 1 3 EmptySubgridV1 -4 1 4 ImportOnlySubgridV1 -4 2 0 ImportOnlySubgridV1 +4 1 4 PackedQ1X2SubgridV1 +4 2 0 PackedQ1X2SubgridV1 4 2 1 EmptySubgridV1 -4 2 2 ImportOnlySubgridV1 +4 2 2 PackedQ1X2SubgridV1 4 2 3 EmptySubgridV1 -4 2 4 ImportOnlySubgridV1 -4 3 0 ImportOnlySubgridV1 +4 2 4 PackedQ1X2SubgridV1 +4 3 0 PackedQ1X2SubgridV1 4 3 1 EmptySubgridV1 -4 3 2 ImportOnlySubgridV1 +4 3 2 PackedQ1X2SubgridV1 4 3 3 EmptySubgridV1 -4 3 4 ImportOnlySubgridV1 -4 4 0 ImportOnlySubgridV1 +4 3 4 PackedQ1X2SubgridV1 +4 4 0 PackedQ1X2SubgridV1 4 4 1 EmptySubgridV1 -4 4 2 ImportOnlySubgridV1 +4 4 2 PackedQ1X2SubgridV1 4 4 3 EmptySubgridV1 -4 4 4 ImportOnlySubgridV1 -4 5 0 ImportOnlySubgridV1 +4 4 4 PackedQ1X2SubgridV1 +4 5 0 PackedQ1X2SubgridV1 4 5 1 EmptySubgridV1 -4 5 2 ImportOnlySubgridV1 +4 5 2 PackedQ1X2SubgridV1 4 5 3 EmptySubgridV1 -4 5 4 ImportOnlySubgridV1 -4 6 0 ImportOnlySubgridV1 +4 5 4 PackedQ1X2SubgridV1 +4 6 0 PackedQ1X2SubgridV1 4 6 1 EmptySubgridV1 -4 6 2 ImportOnlySubgridV1 +4 6 2 PackedQ1X2SubgridV1 4 6 3 EmptySubgridV1 -4 6 4 ImportOnlySubgridV1 -4 7 0 ImportOnlySubgridV1 +4 6 4 PackedQ1X2SubgridV1 +4 7 0 PackedQ1X2SubgridV1 4 7 1 EmptySubgridV1 -4 7 2 ImportOnlySubgridV1 +4 7 2 PackedQ1X2SubgridV1 4 7 3 EmptySubgridV1 -4 7 4 ImportOnlySubgridV1 +4 7 4 PackedQ1X2SubgridV1 5 0 0 EmptySubgridV1 5 0 1 EmptySubgridV1 5 0 2 EmptySubgridV1 @@ -911,46 +911,46 @@ const TYPE_SHOW_EMPTY_STR: &str = "o b c type 5 7 2 EmptySubgridV1 5 7 3 EmptySubgridV1 5 7 4 EmptySubgridV1 -6 0 0 ImportOnlySubgridV1 +6 0 0 PackedQ1X2SubgridV1 6 0 1 EmptySubgridV1 -6 0 2 ImportOnlySubgridV1 +6 0 2 PackedQ1X2SubgridV1 6 0 3 EmptySubgridV1 -6 0 4 ImportOnlySubgridV1 -6 1 0 ImportOnlySubgridV1 +6 0 4 PackedQ1X2SubgridV1 +6 1 0 PackedQ1X2SubgridV1 6 1 1 EmptySubgridV1 -6 1 2 ImportOnlySubgridV1 +6 1 2 PackedQ1X2SubgridV1 6 1 3 EmptySubgridV1 -6 1 4 ImportOnlySubgridV1 -6 2 0 ImportOnlySubgridV1 +6 1 4 PackedQ1X2SubgridV1 +6 2 0 PackedQ1X2SubgridV1 6 2 1 EmptySubgridV1 -6 2 2 ImportOnlySubgridV1 +6 2 2 PackedQ1X2SubgridV1 6 2 3 EmptySubgridV1 -6 2 4 ImportOnlySubgridV1 -6 3 0 ImportOnlySubgridV1 +6 2 4 PackedQ1X2SubgridV1 +6 3 0 PackedQ1X2SubgridV1 6 3 1 EmptySubgridV1 -6 3 2 ImportOnlySubgridV1 +6 3 2 PackedQ1X2SubgridV1 6 3 3 EmptySubgridV1 -6 3 4 ImportOnlySubgridV1 -6 4 0 ImportOnlySubgridV1 +6 3 4 PackedQ1X2SubgridV1 +6 4 0 PackedQ1X2SubgridV1 6 4 1 EmptySubgridV1 -6 4 2 ImportOnlySubgridV1 +6 4 2 PackedQ1X2SubgridV1 6 4 3 EmptySubgridV1 -6 4 4 ImportOnlySubgridV1 -6 5 0 ImportOnlySubgridV1 +6 4 4 PackedQ1X2SubgridV1 +6 5 0 PackedQ1X2SubgridV1 6 5 1 EmptySubgridV1 -6 5 2 ImportOnlySubgridV1 +6 5 2 PackedQ1X2SubgridV1 6 5 3 EmptySubgridV1 -6 5 4 ImportOnlySubgridV1 -6 6 0 ImportOnlySubgridV1 +6 5 4 PackedQ1X2SubgridV1 +6 6 0 PackedQ1X2SubgridV1 6 6 1 EmptySubgridV1 -6 6 2 ImportOnlySubgridV1 +6 6 2 PackedQ1X2SubgridV1 6 6 3 EmptySubgridV1 -6 6 4 ImportOnlySubgridV1 -6 7 0 ImportOnlySubgridV1 +6 6 4 PackedQ1X2SubgridV1 +6 7 0 PackedQ1X2SubgridV1 6 7 1 EmptySubgridV1 -6 7 2 ImportOnlySubgridV1 +6 7 2 PackedQ1X2SubgridV1 6 7 3 EmptySubgridV1 -6 7 4 ImportOnlySubgridV1 +6 7 4 PackedQ1X2SubgridV1 "; const X1_STR: &str = "o b c x1 From e8dba2bec56646bb6e2a4b8f9c419da47cab662a Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 6 Aug 2024 12:39:30 +0200 Subject: [PATCH 024/213] Fix bug in DIS evolution --- pineappl/src/evolution.rs | 2 +- pineappl/src/grid.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 1bb7433e..d3f6a970 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -500,7 +500,7 @@ pub(crate) fn evolve_slice_with_one( })); } - let pid = grid.channels()[0].entry()[0].0[index]; + let pid = grid.channels()[0].entry()[0].0[1 - index]; Ok(( Array1::from_iter(sub_fk_tables) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 4a997a03..baf2dfc4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1582,6 +1582,9 @@ impl Grid { // TODO: convert this unwrap into error let grid = lhs.unwrap(); + // check that the convolutions are unchanged + assert_eq!(self.convolutions(), grid.convolutions()); + // UNWRAP: merging evolved slices should be a proper FkTable again Ok(FkTable::try_from(grid).unwrap_or_else(|_| unreachable!())) } From 47a32780c9ad6112e7494fe0477f4fe0fd84e113 Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 6 Aug 2024 15:16:23 +0200 Subject: [PATCH 025/213] Add fragmentation scale to `Mu2` --- pineappl/src/evolution.rs | 11 ++++++++++- pineappl/src/grid.rs | 1 + pineappl/src/import_only_subgrid.rs | 13 +++++++------ pineappl/src/lagrange_subgrid.rs | 7 ++++--- pineappl/src/packed_subgrid.rs | 6 +++--- pineappl/src/subgrid.rs | 2 ++ 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index d3f6a970..67284594 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -352,7 +352,10 @@ fn ndarray_from_subgrid_orders_slice( .collect::>()?; for ((ifac1, ix1, ix2), value) in subgrid.indexed_iter() { - let Mu2 { ren, fac } = subgrid.mu2_grid()[ifac1]; + let Mu2 { ren, fac, frg } = subgrid.mu2_grid()[ifac1]; + + // TODO: implement evolution for non-zero fragmentation scales + assert_eq!(frg, -1.0); if !approx_eq!(f64, xif * xif * fac, fac1, ulps = EVOLUTION_TOL_ULPS) { continue; @@ -484,6 +487,8 @@ pub(crate) fn evolve_slice_with_one( //ren: -1.0, ren: info.fac0, fac: info.fac0, + // TODO: implement evolution for non-zero fragmentation scales + frg: -1.0, }], if index == 0 { info.x0.clone() @@ -627,6 +632,8 @@ pub(crate) fn evolve_slice_with_two( //ren: -1.0, ren: info.fac0, fac: info.fac0, + // TODO: implement evolution for non-zero fragmentation scales + frg: -1.0, }], info.x0.clone(), info.x0.clone(), @@ -775,6 +782,8 @@ pub(crate) fn evolve_slice_with_two2( //ren: -1.0, ren: infos[0].fac0, fac: infos[0].fac0, + // TODO: implement evolution for non-zero fragmentation scales + frg: -1.0, }], infos[0].x0.clone(), infos[1].x0.clone(), diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index baf2dfc4..80ebeac4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -563,6 +563,7 @@ impl Grid { .map(|mu2v0| Mu2 { ren: mu2v0.ren, fac: mu2v0.fac, + frg: -1.0, }) .collect(); let x1_grid = subgrid.x1_grid().into_owned(); diff --git a/pineappl/src/import_only_subgrid.rs b/pineappl/src/import_only_subgrid.rs index 04624c09..8c5fadf8 100644 --- a/pineappl/src/import_only_subgrid.rs +++ b/pineappl/src/import_only_subgrid.rs @@ -61,7 +61,7 @@ impl Subgrid for ImportOnlySubgridV1 { self.q2_grid .iter() .copied() - .map(|q2| Mu2 { ren: q2, fac: q2 }) + .map(|q2| Mu2 { ren: q2, fac: q2, frg: -1.0 }) .collect() } @@ -170,6 +170,7 @@ impl Subgrid for ImportOnlySubgridV1 { Some(Mu2 { ren: static_scale, fac: static_scale, + frg: -1.0, }) } else { None @@ -467,7 +468,7 @@ mod tests { } ); - let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0 }]; + let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, frg: -1.0 }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); assert_eq!(grid1.x1_grid().as_ref(), x); @@ -558,13 +559,13 @@ mod tests { ]; let mut grid1: SubgridEnum = ImportOnlySubgridV2::new( SparseArray3::new(1, 10, 10), - vec![Mu2 { ren: 0.0, fac: 0.0 }], + vec![Mu2 { ren: 0.0, fac: 0.0, frg: -1.0 }], x.clone(), x.clone(), ) .into(); - let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0 }]; + let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, frg: -1.0 }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); assert_eq!(grid1.x1_grid().as_ref(), x); @@ -598,7 +599,7 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = ImportOnlySubgridV2::new( SparseArray3::new(1, 10, 10), - vec![Mu2 { ren: 1.0, fac: 1.0 }], + vec![Mu2 { ren: 1.0, fac: 1.0, frg: -1.0 }], x.clone(), x.clone(), ) @@ -667,7 +668,7 @@ mod tests { fn fill_panic_v2() { let mut grid = ImportOnlySubgridV2::new( SparseArray3::new(1, 1, 1), - vec![Mu2 { ren: 1.0, fac: 1.0 }], + vec![Mu2 { ren: 1.0, fac: 1.0, frg: -1.0 }], vec![1.0], vec![1.0], ); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index f3ccf2a5..56e68843 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -231,7 +231,7 @@ impl Subgrid for LagrangeSubgridV1 { (0..self.ntau) .map(|itau| { let q2 = fq2(self.gettau(itau)); - Mu2 { ren: q2, fac: q2 } + Mu2 { ren: q2, fac: q2, frg: -1.0 } }) .collect() } @@ -614,7 +614,7 @@ impl Subgrid for LagrangeSubgridV2 { (0..self.ntau) .map(|itau| { let q2 = fq2(self.gettau(itau)); - Mu2 { ren: q2, fac: q2 } + Mu2 { ren: q2, fac: q2, frg: -1.0 } }) .collect() } @@ -783,6 +783,7 @@ impl Subgrid for LagrangeSubgridV2 { (self.static_q2 > 0.0).then_some(Mu2 { ren: self.static_q2, fac: self.static_q2, + frg: -1.0, }) } } @@ -926,7 +927,7 @@ impl Subgrid for LagrangeSparseSubgridV1 { (0..self.ntau) .map(|itau| { let q2 = fq2(self.gettau(itau)); - Mu2 { ren: q2, fac: q2 } + Mu2 { ren: q2, fac: q2, frg: -1.0 } }) .collect() } diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 79eca0da..a9b340a6 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -274,13 +274,13 @@ mod tests { ]; let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new([1, 10, 10]), - vec![Mu2 { ren: 0.0, fac: 0.0 }], + vec![Mu2 { ren: 0.0, fac: 0.0, frg: 0.0 }], x.clone(), x.clone(), ) .into(); - let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0 }]; + let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, frg: 0.0 }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); assert_eq!(grid1.x1_grid().as_ref(), x); @@ -314,7 +314,7 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new([1, 10, 10]), - vec![Mu2 { ren: 1.0, fac: 1.0 }], + vec![Mu2 { ren: 1.0, fac: 1.0, frg: 1.0 }], x.clone(), x.clone(), ) diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 4d2cbe0c..81291938 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -42,6 +42,8 @@ pub struct Mu2 { pub ren: f64, /// The (squared) factorization scale value. pub fac: f64, + /// The (squared) fragmentation scale value. + pub frg: f64, } /// Size-related statistics for a subgrid. From edad700f8d67f093d1e83451d4939ecd7ccf83fc Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 6 Aug 2024 16:55:36 +0200 Subject: [PATCH 026/213] Make metadata member of `Grid` --- pineappl/src/fk_table.rs | 16 +- pineappl/src/grid.rs | 293 ++++++++++++++---------------------- pineappl_capi/src/lib.rs | 25 +-- pineappl_cli/src/helpers.rs | 20 +-- pineappl_cli/src/plot.rs | 26 ++-- pineappl_cli/src/read.rs | 47 ++---- pineappl_cli/src/write.rs | 8 +- 7 files changed, 171 insertions(+), 264 deletions(-) diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 056919c3..19119339 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -6,7 +6,6 @@ use super::grid::{Grid, GridError}; use super::subgrid::Subgrid; use float_cmp::approx_eq; use ndarray::Array4; -use std::collections::HashMap; use std::fmt::{self, Display, Formatter}; use std::io::Write; use std::str::FromStr; @@ -218,12 +217,6 @@ impl FkTable { self.grid.bin_info().right(dimension) } - /// Access meta data - #[must_use] - pub const fn key_values(&self) -> Option<&HashMap> { - self.grid.key_values() - } - /// Return the channel definition for this `FkTable`. All factors are `1.0`. #[must_use] pub fn channels(&self) -> Vec> { @@ -281,9 +274,11 @@ impl FkTable { .convolve(lumi_cache, &[], bin_indices, channel_mask, &[(1.0, 1.0)]) } - /// Set a metadata key-value pair + /// Set a metadata key-value pair. pub fn set_key_value(&mut self, key: &str, value: &str) { - self.grid.set_key_value(key, value); + self.grid + .metadata_mut() + .insert(key.to_owned(), value.to_owned()); } /// Optimizes the storage of FK tables based of assumptions of the PDFs at the FK table's @@ -347,7 +342,8 @@ impl FkTable { // store the assumption so that we can check it later on self.grid - .set_key_value("fk_assumptions", &assumptions.to_string()); + .metadata_mut() + .insert("fk_assumptions".to_owned(), assumptions.to_string()); self.grid.optimize(); } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 80ebeac4..d0afac70 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -16,9 +16,9 @@ use float_cmp::approx_eq; use git_version::git_version; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; use ndarray::{s, Array3, ArrayView3, ArrayView5, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::borrow::Cow; -use std::collections::{BTreeMap, HashMap}; +use std::collections::BTreeMap; use std::io::{self, BufRead, BufReader, BufWriter, Read, Write}; use std::iter; use std::mem; @@ -84,103 +84,53 @@ pub enum GridError { Other(#[from] anyhow::Error), } -#[derive(Clone, Deserialize, Serialize)] -struct Mmv1; - -#[derive(Clone, Deserialize, Serialize)] -struct Mmv2 { - remapper: Option, - key_value_db: HashMap, -} - -fn ordered_map_serialize( - value: &HashMap, - serializer: S, -) -> Result -where - S: Serializer, -{ - let ordered: BTreeMap<_, _> = value.iter().collect(); - ordered.serialize(serializer) -} - #[derive(Clone, Deserialize, Serialize)] struct Mmv3 { remapper: Option, - // order the HashMap before serializing it to make the output stable - #[serde(serialize_with = "ordered_map_serialize")] - key_value_db: HashMap, subgrid_template: SubgridEnum, } -impl Default for Mmv2 { - fn default() -> Self { - Self { - remapper: None, - key_value_db: [ - ( - "pineappl_gitversion".to_owned(), - git_version!( - args = ["--always", "--dirty", "--long", "--tags"], - cargo_prefix = "cargo:", - fallback = "unknown" - ) - .to_owned(), - ), - // by default we assume there are protons in the initial state - ("initial_state_1".to_owned(), "2212".to_owned()), - ("initial_state_2".to_owned(), "2212".to_owned()), - ] - .iter() - .cloned() - .collect(), - } - } -} - impl Mmv3 { fn new(subgrid_template: SubgridEnum) -> Self { Self { remapper: None, - key_value_db: [ - ( - "pineappl_gitversion".to_owned(), - git_version!( - args = ["--always", "--dirty", "--long", "--tags"], - cargo_prefix = "cargo:", - fallback = "unknown" - ) - .to_owned(), - ), - // by default we assume there are unpolarized protons in the initial state - // do not change these to the new metadata to not break backwards compatibility - ("initial_state_1".to_owned(), "2212".to_owned()), - ("initial_state_2".to_owned(), "2212".to_owned()), - ] - .iter() - .cloned() - .collect(), subgrid_template, } } } +fn default_metadata() -> BTreeMap { + [ + ( + "pineappl_gitversion".to_owned(), + git_version!( + args = ["--always", "--dirty", "--long", "--tags"], + cargo_prefix = "cargo:", + fallback = "unknown" + ) + .to_owned(), + ), + // by default we assume there are unpolarized protons in the initial state + // do not change these to the new metadata to not break backwards compatibility + ("initial_state_1".to_owned(), "2212".to_owned()), + ("initial_state_2".to_owned(), "2212".to_owned()), + ] + .iter() + .cloned() + .collect() +} + // ALLOW: fixing the warning will break the file format #[allow(clippy::large_enum_variant)] #[derive(Clone, Deserialize, Serialize)] enum MoreMembers { - V1(Mmv1), - V2(Mmv2), V3(Mmv3), } impl MoreMembers { fn upgrade(&mut self) { match self { - Self::V1(_) => { - *self = Self::V2(Mmv2::default()); - } - Self::V2(_) | Self::V3(_) => {} + Self::V3(_) => {} } } } @@ -218,6 +168,7 @@ pub struct Grid { bin_limits: BinLimits, orders: Vec, subgrid_params: SubgridParams, + metadata: BTreeMap, more_members: MoreMembers, } @@ -238,6 +189,7 @@ impl Grid { orders, channels, bin_limits: BinLimits::new(bin_limits), + metadata: default_metadata(), more_members: MoreMembers::V3(Mmv3::new( LagrangeSubgridV2::new(&subgrid_params, &ExtraSubgridParams::from(&subgrid_params)) .into(), @@ -282,6 +234,7 @@ impl Grid { channels, bin_limits: BinLimits::new(bin_limits), subgrid_params, + metadata: default_metadata(), more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), }) } @@ -289,13 +242,11 @@ impl Grid { /// Return by which convention the particle IDs are encoded. #[must_use] pub fn pid_basis(&self) -> PidBasis { - if let Some(key_values) = self.key_values() { - if let Some(lumi_id_types) = key_values.get("lumi_id_types") { - match lumi_id_types.as_str() { - "pdg_mc_ids" => return PidBasis::Pdg, - "evol" => return PidBasis::Evol, - _ => unimplemented!("unknown particle ID convention {lumi_id_types}"), - } + if let Some(lumi_id_types) = self.metadata().get("lumi_id_types") { + match lumi_id_types.as_str() { + "pdg_mc_ids" => return PidBasis::Pdg, + "evol" => return PidBasis::Evol, + _ => unimplemented!("unknown particle ID convention {lumi_id_types}"), } } @@ -306,9 +257,13 @@ impl Grid { /// Set the convention by which PIDs of channels are interpreted. pub fn set_pid_basis(&mut self, pid_basis: PidBasis) { match pid_basis { - PidBasis::Pdg => self.set_key_value("lumi_id_types", "pdg_mc_ids"), - PidBasis::Evol => self.set_key_value("lumi_id_types", "evol"), - } + PidBasis::Pdg => self + .metadata_mut() + .insert("lumi_id_types".to_owned(), "pdg_mc_ids".to_owned()), + PidBasis::Evol => self + .metadata_mut() + .insert("lumi_id_types".to_owned(), "evol".to_owned()), + }; } fn pdg_channels(&self) -> Cow<[Channel]> { @@ -487,11 +442,8 @@ impl Grid { if let Some(bin) = self.bin_limits.index(observable) { let subgrid = &mut self.subgrids[[order, bin, channel]]; if let SubgridEnum::EmptySubgridV1(_) = subgrid { - if let MoreMembers::V3(mmv3) = &self.more_members { - *subgrid = mmv3.subgrid_template.clone_empty(); - } else { - unreachable!(); - } + let MoreMembers::V3(mmv3) = &self.more_members; + *subgrid = mmv3.subgrid_template.clone_empty(); } subgrid.fill(ntuple); @@ -610,6 +562,12 @@ impl Grid { .collect(), // TODO: remove this member subgrid_params: SubgridParams::default(), + metadata: grid + .key_values() + .cloned() + .unwrap_or_default() + .into_iter() + .collect(), // TODO: make these proper members more_members: MoreMembers::V3(Mmv3 { remapper: grid.remapper().map(|r| { @@ -617,7 +575,6 @@ impl Grid { // and limits we should be able to do the same without error BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() }), - key_value_db: grid.key_values().cloned().unwrap_or_default(), // TODO: remove this member subgrid_template: EmptySubgridV1.into(), }), @@ -862,71 +819,69 @@ impl Grid { /// or `convolution_particle_2` and `convolution_type_2` are not correctly set. #[must_use] pub fn convolutions(&self) -> Vec { - self.key_values().map_or_else( - // if there isn't any metadata, we assume two unpolarized proton-PDFs are used - || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], - |kv| { - // the current file format only supports exactly two convolutions - (1..=2) - .map(|index| { - // if there are key-value pairs `convolution_particle_1` and - // `convolution_type_1` and the same with a higher index, we convert this - // metadata into `Convolution` - match ( - kv.get(&format!("convolution_particle_{index}")) - .map(|s| s.parse::()), - kv.get(&format!("convolution_type_{index}")) - .map(String::as_str), - ) { - (_, Some("None")) => Convolution::None, - (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), - (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), - (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), - (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), - (None, None) => { - // if these key-value pairs are missing use the old metadata - match kv - .get(&format!("initial_state_{index}")) - .map(|s| s.parse::()) - { - Some(Ok(pid)) => { - let condition = !self.channels().iter().all(|entry| { - entry.entry().iter().all(|(pids, _)| pids[index - 1] == pid) - }); - - if condition { - Convolution::UnpolPDF(pid) - } else { - Convolution::None - } - } - None => Convolution::UnpolPDF(2212), - Some(Err(err)) => panic!("metadata 'initial_state_{index}' could not be parsed: {err}"), + let kv = self.metadata(); + + // the current file format only supports exactly two convolutions + (1..=2) + .map(|index| { + // if there are key-value pairs `convolution_particle_1` and + // `convolution_type_1` and the same with a higher index, we convert this + // metadata into `Convolution` + match ( + kv.get(&format!("convolution_particle_{index}")) + .map(|s| s.parse::()), + kv.get(&format!("convolution_type_{index}")) + .map(String::as_str), + ) { + (_, Some("None")) => Convolution::None, + (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), + (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), + (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), + (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), + (None, None) => { + // if these key-value pairs are missing use the old metadata + match kv + .get(&format!("initial_state_{index}")) + .map(|s| s.parse::()) + { + Some(Ok(pid)) => { + let condition = !self.channels().iter().all(|entry| { + entry.entry().iter().all(|(pids, _)| pids[index - 1] == pid) + }); + + if condition { + Convolution::UnpolPDF(pid) + } else { + Convolution::None } } - (None, Some(_)) => { - panic!("metadata 'convolution_type_{index}' is missing") - } - (Some(_), None) => { - panic!("metadata 'convolution_particle_{index}' is missing") - } - (Some(Ok(_)), Some(type_)) => { - panic!("metadata 'convolution_type_{index} = {type_}' is unknown") - } - (Some(Err(err)), Some(_)) => panic!( - "metadata 'convolution_particle_{index}' could not be parsed: {err}" + None => Convolution::UnpolPDF(2212), + Some(Err(err)) => panic!( + "metadata 'initial_state_{index}' could not be parsed: {err}" ), } - }) - .collect() - }, - ) + } + (None, Some(_)) => { + panic!("metadata 'convolution_type_{index}' is missing") + } + (Some(_), None) => { + panic!("metadata 'convolution_particle_{index}' is missing") + } + (Some(Ok(_)), Some(type_)) => { + panic!("metadata 'convolution_type_{index} = {type_}' is unknown") + } + (Some(Err(err)), Some(_)) => { + panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") + } + } + }) + .collect() } /// Set the convolution type for this grid for the corresponding `index`. pub fn set_convolution(&mut self, index: usize, convolution: Convolution) { // remove outdated metadata - self.key_values_mut() + self.metadata_mut() .remove(&format!("initial_state_{}", index + 1)); let (type_, particle) = match convolution { @@ -937,8 +892,10 @@ impl Grid { Convolution::None => ("None".to_owned(), String::new()), }; - self.set_key_value(&format!("convolution_type_{}", index + 1), &type_); - self.set_key_value(&format!("convolution_particle_{}", index + 1), &particle); + self.metadata_mut() + .insert(format!("convolution_type_{}", index + 1), type_); + self.metadata_mut() + .insert(format!("convolution_particle_{}", index + 1), particle); } fn increase_shape(&mut self, new_dim: &(usize, usize, usize)) { @@ -1058,8 +1015,6 @@ impl Grid { self.more_members.upgrade(); match &mut self.more_members { - MoreMembers::V1(_) => unreachable!(), - MoreMembers::V2(mmv2) => mmv2.remapper = Some(remapper), MoreMembers::V3(mmv3) => mmv3.remapper = Some(remapper), } @@ -1070,16 +1025,12 @@ impl Grid { #[must_use] pub const fn remapper(&self) -> Option<&BinRemapper> { match &self.more_members { - MoreMembers::V1(_) => None, - MoreMembers::V2(mmv2) => mmv2.remapper.as_ref(), MoreMembers::V3(mmv3) => mmv3.remapper.as_ref(), } } fn remapper_mut(&mut self) -> Option<&mut BinRemapper> { match &mut self.more_members { - MoreMembers::V1(_) => None, - MoreMembers::V2(mmv2) => mmv2.remapper.as_mut(), MoreMembers::V3(mmv3) => mmv3.remapper.as_mut(), } } @@ -1332,40 +1283,20 @@ impl Grid { self.more_members.upgrade(); } - /// Returns a map with key-value pairs, if there are any stored in this grid. + /// Return the metadata of this grid. #[must_use] - pub const fn key_values(&self) -> Option<&HashMap> { - match &self.more_members { - MoreMembers::V3(mmv3) => Some(&mmv3.key_value_db), - MoreMembers::V2(mmv2) => Some(&mmv2.key_value_db), - MoreMembers::V1(_) => None, - } + pub const fn metadata(&self) -> &BTreeMap { + &self.metadata } - /// Returns a map with key-value pairs, if there are any stored in this grid. + /// Return the metadata of this grid. /// /// # Panics /// /// TODO #[must_use] - pub fn key_values_mut(&mut self) -> &mut HashMap { - self.more_members.upgrade(); - - match &mut self.more_members { - MoreMembers::V1(_) => unreachable!(), - MoreMembers::V2(mmv2) => &mut mmv2.key_value_db, - MoreMembers::V3(mmv3) => &mut mmv3.key_value_db, - } - } - - /// Sets a specific key-value pair in this grid. - /// - /// # Panics - /// - /// TODO - pub fn set_key_value(&mut self, key: &str, value: &str) { - self.key_values_mut() - .insert(key.to_owned(), value.to_owned()); + pub fn metadata_mut(&mut self) -> &mut BTreeMap { + &mut self.metadata } /// Returns information for the generation of evolution operators that are being used in @@ -1545,6 +1476,7 @@ impl Grid { bin_limits: self.bin_limits.clone(), orders: vec![Order::new(0, 0, 0, 0, 0)], subgrid_params: SubgridParams::default(), + metadata: self.metadata.clone(), more_members: self.more_members.clone(), }; @@ -1684,6 +1616,7 @@ impl Grid { bin_limits: self.bin_limits.clone(), orders: vec![Order::new(0, 0, 0, 0, 0)], subgrid_params: SubgridParams::default(), + metadata: self.metadata.clone(), more_members: self.more_members.clone(), }; diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 3e8ff64c..315d6c2d 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -710,11 +710,17 @@ pub unsafe extern "C" fn pineappl_grid_new( if let Some(keyval) = key_vals { if let Some(value) = keyval.strings.get("initial_state_1") { - grid.set_key_value("initial_state_1", value.to_str().unwrap()); + grid.metadata_mut().insert( + "initial_state_1".to_owned(), + value.to_str().unwrap().to_owned(), + ); } if let Some(value) = keyval.strings.get("initial_state_2") { - grid.set_key_value("initial_state_2", value.to_str().unwrap()); + grid.metadata_mut().insert( + "initial_state_2".to_owned(), + value.to_str().unwrap().to_owned(), + ); } } @@ -895,12 +901,9 @@ pub unsafe extern "C" fn pineappl_grid_key_value( let key = unsafe { CStr::from_ptr(key) }; let key = key.to_string_lossy(); - CString::new( - grid.key_values() - .map_or("", |kv| kv.get(key.as_ref()).map_or("", String::as_str)), - ) - .unwrap() - .into_raw() + CString::new(grid.metadata().get(key.as_ref()).map_or("", String::as_str)) + .unwrap() + .into_raw() } /// Sets an internal key-value pair for the grid. @@ -924,9 +927,9 @@ pub unsafe extern "C" fn pineappl_grid_set_key_value( let key = unsafe { CStr::from_ptr(key) }; let value = unsafe { CStr::from_ptr(value) }; - grid.set_key_value( - key.to_string_lossy().as_ref(), - value.to_string_lossy().as_ref(), + grid.metadata_mut().insert( + key.to_string_lossy().into_owned(), + value.to_string_lossy().into_owned(), ); } diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 97ec9ecd..aecf1902 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -142,17 +142,18 @@ pub const SCALES_VECTOR: [(f64, f64); 9] = [ ]; pub fn labels_and_units(grid: &Grid, integrated: bool) -> (Vec<(String, &str)>, &str, &str) { - let key_values = grid.key_values(); + let metadata = grid.metadata(); ( (0..grid.bin_info().dimensions()) .map(|d| { ( - key_values - .and_then(|kv| kv.get(&format!("x{}_label", d + 1)).cloned()) + metadata + .get(&format!("x{}_label", d + 1)) + .cloned() .unwrap_or_else(|| format!("x{}", d + 1)), - key_values - .and_then(|kv| kv.get(&format!("x{}_unit", d + 1))) + metadata + .get(&format!("x{}_unit", d + 1)) .map_or("", String::as_str), ) }) @@ -160,16 +161,15 @@ pub fn labels_and_units(grid: &Grid, integrated: bool) -> (Vec<(String, &str)>, if integrated { "integ" } else { - key_values - .and_then(|kv| kv.get("y_label").map(String::as_str)) + metadata + .get("y_label") + .map(String::as_str) .unwrap_or("diff") }, if integrated { "" // TODO: compute the units for the integrated cross section } else { - key_values - .and_then(|kv| kv.get("y_unit").map(String::as_str)) - .unwrap_or("") + metadata.get("y_unit").map(String::as_str).unwrap_or("") }, ) } diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index 657e98e0..45077178 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -216,14 +216,16 @@ impl Subcommand for Opts { "$\\SI{{{left}}}{{{unit}}} < {obs} < \\SI{{{right}}}{{{unit}}}$", left = grid.bin_info().left(d)[begin], obs = grid - .key_values() - .and_then(|map| map.get(&format!("x{}_label_tex", d + 1)).cloned()) + .metadata() + .get(&format!("x{}_label_tex", d + 1)) + .cloned() .unwrap_or_else(|| format!("x{}", d + 1)) .replace('$', ""), right = grid.bin_info().right(d)[end - 1], unit = grid - .key_values() - .and_then(|map| map.get(&format!("x{}_unit", d + 1)).cloned()) + .metadata() + .get(&format!("x{}_unit", d + 1)) + .cloned() .unwrap_or_default() ) }) @@ -472,10 +474,8 @@ impl Subcommand for Opts { data_string.push_str(" ]"); // prepare metadata - let key_values = grid.key_values().cloned().unwrap_or_default(); - let mut vector: Vec<_> = key_values.iter().collect(); - vector.sort(); - let vector = vector; + let metadata = grid.metadata(); + let vector: Vec<_> = metadata.iter().collect(); let mut output = self.input.clone(); @@ -492,12 +492,12 @@ impl Subcommand for Opts { } let xaxis = format!("x{}", grid.bin_info().dimensions()); - let xunit = key_values + let xunit = metadata .get(&format!("{xaxis}_unit")) .map_or("", String::as_str); let xlabel = format!( "{}{}", - key_values + metadata .get(&format!("{xaxis}_label_tex")) .map_or("", String::as_str), if xunit.is_empty() { @@ -506,10 +506,10 @@ impl Subcommand for Opts { format!(" [\\si{{{xunit}}}]") } ); - let yunit = key_values.get("y_unit").map_or("", String::as_str); + let yunit = metadata.get("y_unit").map_or("", String::as_str); let ylabel = format!( "{}{}", - key_values.get("y_label_tex").map_or("", String::as_str), + metadata.get("y_label_tex").map_or("", String::as_str), if yunit.is_empty() { String::new() } else { @@ -518,7 +518,7 @@ impl Subcommand for Opts { ); let xlog = if xunit.is_empty() { "False" } else { "True" }; let ylog = xlog; - let title = key_values.get("description").map_or("", String::as_str); + let title = metadata.get("description").map_or("", String::as_str); let bins = grid.bin_info().bins(); let nconvs = self.conv_funs.len(); diff --git a/pineappl_cli/src/read.rs b/pineappl_cli/src/read.rs index a2692367..94a293b7 100644 --- a/pineappl_cli/src/read.rs +++ b/pineappl_cli/src/read.rs @@ -60,7 +60,7 @@ pub struct Opts { impl Subcommand for Opts { fn run(&self, _: &GlobalConfiguration) -> Result { - let mut grid = helpers::read_grid(&self.input)?; + let grid = helpers::read_grid(&self.input)?; let mut table = helpers::create_table(); @@ -164,44 +164,17 @@ impl Subcommand for Opts { println!("{orders}"); } else if let Some(key) = &self.group.get { - grid.upgrade(); - - grid.key_values().map_or_else( - || unreachable!(), - |key_values| { - if let Some(value) = key_values.get(key) { - println!("{value}"); - } - }, - ); + if let Some(value) = grid.metadata().get(key) { + println!("{value}"); + } } else if self.group.keys { - grid.upgrade(); - - grid.key_values().map_or_else( - || unreachable!(), - |key_values| { - let mut vector = key_values.iter().collect::>(); - vector.sort(); - - for (key, _) in &vector { - println!("{key}"); - } - }, - ); + for (key, _) in grid.metadata() { + println!("{key}"); + } } else if self.group.show { - grid.upgrade(); - - grid.key_values().map_or_else( - || unreachable!(), - |key_values| { - let mut vector = key_values.iter().collect::>(); - vector.sort(); - - for (key, value) in &vector { - println!("{key}: {value}"); - } - }, - ); + for (key, value) in grid.metadata() { + println!("{key}: {value}"); + } } else { table.set_titles(row![c => "o", "order"]); diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index 522cf53f..32b77bb4 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -531,7 +531,7 @@ impl Subcommand for Opts { grid.delete_channels(&ranges.iter().flat_map(Clone::clone).collect::>()); } OpsArg::DeleteKey(key) => { - grid.key_values_mut().remove(key); + grid.metadata_mut().remove(key); } OpsArg::MergeBins(ranges) => { // TODO: sort after increasing start indices @@ -598,10 +598,12 @@ impl Subcommand for Opts { grid.scale_by_order(factors[0], factors[1], factors[2], factors[3], 1.0); } OpsArg::SetKeyValue(key_value) => { - grid.set_key_value(&key_value[0], &key_value[1]); + grid.metadata_mut() + .insert(key_value[0].clone(), key_value[1].clone()); } OpsArg::SetKeyFile(key_file) => { - grid.set_key_value(&key_file[0], &fs::read_to_string(&key_file[1])?); + grid.metadata_mut() + .insert(key_file[0].clone(), fs::read_to_string(&key_file[1])?); } OpsArg::SplitChannels(true) => grid.split_channels(), OpsArg::Upgrade(true) => grid.upgrade(), From c4f871e0abd1eabea27a3667b41f34b49b806845 Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 6 Aug 2024 16:55:57 +0200 Subject: [PATCH 027/213] Reformat code --- pineappl/src/import_only_subgrid.rs | 36 ++++++++++++++++++++++++----- pineappl/src/lagrange_subgrid.rs | 18 ++++++++++++--- pineappl/src/packed_subgrid.rs | 18 ++++++++++++--- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/pineappl/src/import_only_subgrid.rs b/pineappl/src/import_only_subgrid.rs index 8c5fadf8..4cff492e 100644 --- a/pineappl/src/import_only_subgrid.rs +++ b/pineappl/src/import_only_subgrid.rs @@ -61,7 +61,11 @@ impl Subgrid for ImportOnlySubgridV1 { self.q2_grid .iter() .copied() - .map(|q2| Mu2 { ren: q2, fac: q2, frg: -1.0 }) + .map(|q2| Mu2 { + ren: q2, + fac: q2, + frg: -1.0, + }) .collect() } @@ -468,7 +472,11 @@ mod tests { } ); - let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, frg: -1.0 }]; + let mu2 = vec![Mu2 { + ren: 0.0, + fac: 0.0, + frg: -1.0, + }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); assert_eq!(grid1.x1_grid().as_ref(), x); @@ -559,13 +567,21 @@ mod tests { ]; let mut grid1: SubgridEnum = ImportOnlySubgridV2::new( SparseArray3::new(1, 10, 10), - vec![Mu2 { ren: 0.0, fac: 0.0, frg: -1.0 }], + vec![Mu2 { + ren: 0.0, + fac: 0.0, + frg: -1.0, + }], x.clone(), x.clone(), ) .into(); - let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, frg: -1.0 }]; + let mu2 = vec![Mu2 { + ren: 0.0, + fac: 0.0, + frg: -1.0, + }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); assert_eq!(grid1.x1_grid().as_ref(), x); @@ -599,7 +615,11 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = ImportOnlySubgridV2::new( SparseArray3::new(1, 10, 10), - vec![Mu2 { ren: 1.0, fac: 1.0, frg: -1.0 }], + vec![Mu2 { + ren: 1.0, + fac: 1.0, + frg: -1.0, + }], x.clone(), x.clone(), ) @@ -668,7 +688,11 @@ mod tests { fn fill_panic_v2() { let mut grid = ImportOnlySubgridV2::new( SparseArray3::new(1, 1, 1), - vec![Mu2 { ren: 1.0, fac: 1.0, frg: -1.0 }], + vec![Mu2 { + ren: 1.0, + fac: 1.0, + frg: -1.0, + }], vec![1.0], vec![1.0], ); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 56e68843..71995db6 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -231,7 +231,11 @@ impl Subgrid for LagrangeSubgridV1 { (0..self.ntau) .map(|itau| { let q2 = fq2(self.gettau(itau)); - Mu2 { ren: q2, fac: q2, frg: -1.0 } + Mu2 { + ren: q2, + fac: q2, + frg: -1.0, + } }) .collect() } @@ -614,7 +618,11 @@ impl Subgrid for LagrangeSubgridV2 { (0..self.ntau) .map(|itau| { let q2 = fq2(self.gettau(itau)); - Mu2 { ren: q2, fac: q2, frg: -1.0 } + Mu2 { + ren: q2, + fac: q2, + frg: -1.0, + } }) .collect() } @@ -927,7 +935,11 @@ impl Subgrid for LagrangeSparseSubgridV1 { (0..self.ntau) .map(|itau| { let q2 = fq2(self.gettau(itau)); - Mu2 { ren: q2, fac: q2, frg: -1.0 } + Mu2 { + ren: q2, + fac: q2, + frg: -1.0, + } }) .collect() } diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index a9b340a6..39181494 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -274,13 +274,21 @@ mod tests { ]; let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new([1, 10, 10]), - vec![Mu2 { ren: 0.0, fac: 0.0, frg: 0.0 }], + vec![Mu2 { + ren: 0.0, + fac: 0.0, + frg: 0.0, + }], x.clone(), x.clone(), ) .into(); - let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, frg: 0.0 }]; + let mu2 = vec![Mu2 { + ren: 0.0, + fac: 0.0, + frg: 0.0, + }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); assert_eq!(grid1.x1_grid().as_ref(), x); @@ -314,7 +322,11 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new([1, 10, 10]), - vec![Mu2 { ren: 1.0, fac: 1.0, frg: 1.0 }], + vec![Mu2 { + ren: 1.0, + fac: 1.0, + frg: 1.0, + }], x.clone(), x.clone(), ) From b4d665d9e21beaafec0f9fbb42c50c05eb89965c Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 6 Aug 2024 17:58:00 +0200 Subject: [PATCH 028/213] Make convolutions member of `Grid` --- pineappl/src/convolutions.rs | 3 +- pineappl/src/grid.rs | 193 +++++++++++++++++------------------ pineappl_cli/src/write.rs | 2 +- 3 files changed, 95 insertions(+), 103 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index ea726c44..2c635d89 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -4,6 +4,7 @@ use super::grid::Grid; use super::pids; use super::subgrid::{Mu2, Subgrid}; use rustc_hash::FxHashMap; +use serde::{Deserialize, Serialize}; enum Pdfs<'a> { Two { @@ -343,7 +344,7 @@ impl<'a> LumiCache<'a> { } /// Data type that indentifies different types of convolutions. -#[derive(Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum Convolution { // TODO: eventually get rid of this value /// No convolution. diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index d0afac70..b2fa5eb4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -9,6 +9,8 @@ use super::fk_table::FkTable; use super::import_only_subgrid::ImportOnlySubgridV2; use super::lagrange_subgrid::{LagrangeSparseSubgridV1, LagrangeSubgridV1, LagrangeSubgridV2}; use super::ntuple_subgrid::NtupleSubgridV1; +use super::packed_array::PackedArray; +use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; use super::subgrid::{ExtraSubgridParams, Mu2, Subgrid, SubgridEnum, SubgridParams}; use bitflags::bitflags; @@ -16,6 +18,7 @@ use float_cmp::approx_eq; use git_version::git_version; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; use ndarray::{s, Array3, ArrayView3, ArrayView5, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; +use pineappl_v0::grid::Grid as GridV0; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::collections::BTreeMap; @@ -100,21 +103,15 @@ impl Mmv3 { } fn default_metadata() -> BTreeMap { - [ - ( - "pineappl_gitversion".to_owned(), - git_version!( - args = ["--always", "--dirty", "--long", "--tags"], - cargo_prefix = "cargo:", - fallback = "unknown" - ) - .to_owned(), - ), - // by default we assume there are unpolarized protons in the initial state - // do not change these to the new metadata to not break backwards compatibility - ("initial_state_1".to_owned(), "2212".to_owned()), - ("initial_state_2".to_owned(), "2212".to_owned()), - ] + [( + "pineappl_gitversion".to_owned(), + git_version!( + args = ["--always", "--dirty", "--long", "--tags"], + cargo_prefix = "cargo:", + fallback = "unknown" + ) + .to_owned(), + )] .iter() .cloned() .collect() @@ -169,6 +166,7 @@ pub struct Grid { orders: Vec, subgrid_params: SubgridParams, metadata: BTreeMap, + convolutions: Vec, more_members: MoreMembers, } @@ -181,19 +179,22 @@ impl Grid { bin_limits: Vec, subgrid_params: SubgridParams, ) -> Self { + // TODO: check that channels has same number of PIDs everywhere Self { subgrids: Array3::from_shape_simple_fn( (orders.len(), bin_limits.len() - 1, channels.len()), || EmptySubgridV1.into(), ), orders, - channels, bin_limits: BinLimits::new(bin_limits), metadata: default_metadata(), more_members: MoreMembers::V3(Mmv3::new( LagrangeSubgridV2::new(&subgrid_params, &ExtraSubgridParams::from(&subgrid_params)) .into(), )), + // TODO: add this as new parameter + convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], + channels, subgrid_params, } } @@ -231,10 +232,11 @@ impl Grid { || EmptySubgridV1.into(), ), orders, - channels, bin_limits: BinLimits::new(bin_limits), subgrid_params, metadata: default_metadata(), + convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], + channels, more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), }) } @@ -491,9 +493,6 @@ impl Grid { } fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { - use super::packed_array::PackedArray; - use super::packed_subgrid::PackedQ1X2SubgridV1; - use pineappl_v0::grid::Grid as GridV0; use pineappl_v0::subgrid::Subgrid as _; // TODO: convert error from v0 to v1 @@ -568,6 +567,7 @@ impl Grid { .unwrap_or_default() .into_iter() .collect(), + convolutions: Self::read_convolutions_from_metadata(&grid), // TODO: make these proper members more_members: MoreMembers::V3(Mmv3 { remapper: grid.remapper().map(|r| { @@ -585,6 +585,69 @@ impl Grid { Ok(result) } + fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { + grid.key_values().map_or_else( + // if there isn't any metadata, we assume two unpolarized proton-PDFs are used + || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + |kv| { + // file format v0 only supports exactly two convolutions + (1..=2) + .map(|index| { + // if there are key-value pairs `convolution_particle_1` and + // `convolution_type_1` and the same with a higher index, we convert this + // metadata into `Convolution` + match ( + kv.get(&format!("convolution_particle_{index}")) + .map(|s| s.parse::()), + kv.get(&format!("convolution_type_{index}")) + .map(String::as_str), + ) { + (_, Some("None")) => Convolution::None, + (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), + (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), + (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), + (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), + (None, None) => { + // if these key-value pairs are missing use the old metadata + match kv + .get(&format!("initial_state_{index}")) + .map(|s| s.parse::()) + { + Some(Ok(pid)) => { + let condition = !grid.channels().iter().all(|entry| { + entry.entry().iter().all(|&(a, b, _)| match index {1 => a, 2 => b, _ => unreachable!()} == pid) + }); + + if condition { + Convolution::UnpolPDF(pid) + } else { + Convolution::None + } + } + None => Convolution::UnpolPDF(2212), + Some(Err(err)) => panic!( + "metadata 'initial_state_{index}' could not be parsed: {err}" + ), + } + } + (None, Some(_)) => { + panic!("metadata 'convolution_type_{index}' is missing") + } + (Some(_), None) => { + panic!("metadata 'convolution_particle_{index}' is missing") + } + (Some(Ok(_)), Some(type_)) => { + panic!("metadata 'convolution_type_{index} = {type_}' is unknown") + } + (Some(Err(err)), Some(_)) => { + panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") + } + } + }) + .collect() + }, + ) + } /// Serializes `self` into `writer`. Writing is buffered. /// /// # Errors @@ -818,84 +881,13 @@ impl Grid { /// Panics if the metadata key--value pairs `convolution_particle_1` and `convolution_type_1`, /// or `convolution_particle_2` and `convolution_type_2` are not correctly set. #[must_use] - pub fn convolutions(&self) -> Vec { - let kv = self.metadata(); - - // the current file format only supports exactly two convolutions - (1..=2) - .map(|index| { - // if there are key-value pairs `convolution_particle_1` and - // `convolution_type_1` and the same with a higher index, we convert this - // metadata into `Convolution` - match ( - kv.get(&format!("convolution_particle_{index}")) - .map(|s| s.parse::()), - kv.get(&format!("convolution_type_{index}")) - .map(String::as_str), - ) { - (_, Some("None")) => Convolution::None, - (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), - (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), - (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), - (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), - (None, None) => { - // if these key-value pairs are missing use the old metadata - match kv - .get(&format!("initial_state_{index}")) - .map(|s| s.parse::()) - { - Some(Ok(pid)) => { - let condition = !self.channels().iter().all(|entry| { - entry.entry().iter().all(|(pids, _)| pids[index - 1] == pid) - }); - - if condition { - Convolution::UnpolPDF(pid) - } else { - Convolution::None - } - } - None => Convolution::UnpolPDF(2212), - Some(Err(err)) => panic!( - "metadata 'initial_state_{index}' could not be parsed: {err}" - ), - } - } - (None, Some(_)) => { - panic!("metadata 'convolution_type_{index}' is missing") - } - (Some(_), None) => { - panic!("metadata 'convolution_particle_{index}' is missing") - } - (Some(Ok(_)), Some(type_)) => { - panic!("metadata 'convolution_type_{index} = {type_}' is unknown") - } - (Some(Err(err)), Some(_)) => { - panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") - } - } - }) - .collect() + pub fn convolutions(&self) -> &[Convolution] { + &self.convolutions } - /// Set the convolution type for this grid for the corresponding `index`. - pub fn set_convolution(&mut self, index: usize, convolution: Convolution) { - // remove outdated metadata - self.metadata_mut() - .remove(&format!("initial_state_{}", index + 1)); - - let (type_, particle) = match convolution { - Convolution::UnpolPDF(pid) => ("UnpolPDF".to_owned(), pid.to_string()), - Convolution::PolPDF(pid) => ("PolPDF".to_owned(), pid.to_string()), - Convolution::UnpolFF(pid) => ("UnpolFF".to_owned(), pid.to_string()), - Convolution::PolFF(pid) => ("PolFF".to_owned(), pid.to_string()), - Convolution::None => ("None".to_owned(), String::new()), - }; - - self.metadata_mut() - .insert(format!("convolution_type_{}", index + 1), type_); - self.metadata_mut() - .insert(format!("convolution_particle_{}", index + 1), particle); + /// Return the convolution types. + pub fn convolutions_mut(&mut self) -> &mut [Convolution] { + &mut self.convolutions } fn increase_shape(&mut self, new_dim: &(usize, usize, usize)) { @@ -1477,6 +1469,7 @@ impl Grid { orders: vec![Order::new(0, 0, 0, 0, 0)], subgrid_params: SubgridParams::default(), metadata: self.metadata.clone(), + convolutions: self.convolutions.clone(), more_members: self.more_members.clone(), }; @@ -1515,9 +1508,6 @@ impl Grid { // TODO: convert this unwrap into error let grid = lhs.unwrap(); - // check that the convolutions are unchanged - assert_eq!(self.convolutions(), grid.convolutions()); - // UNWRAP: merging evolved slices should be a proper FkTable again Ok(FkTable::try_from(grid).unwrap_or_else(|_| unreachable!())) } @@ -1617,6 +1607,7 @@ impl Grid { orders: vec![Order::new(0, 0, 0, 0, 0)], subgrid_params: SubgridParams::default(), metadata: self.metadata.clone(), + convolutions: self.convolutions.clone(), more_members: self.more_members.clone(), }; @@ -2087,8 +2078,8 @@ mod tests { [Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)] ); - grid.set_convolution(0, Convolution::UnpolPDF(-2212)); - grid.set_convolution(1, Convolution::UnpolPDF(-2212)); + grid.convolutions_mut()[0] = Convolution::UnpolPDF(-2212); + grid.convolutions_mut()[1] = Convolution::UnpolPDF(-2212); assert_eq!( grid.convolutions(), diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index 32b77bb4..dc616f09 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -519,7 +519,7 @@ impl Subcommand for Opts { ); } - grid.set_convolution(index, grid.convolutions()[index].charge_conjugate()); + grid.convolutions_mut()[index] = grid.convolutions()[index].charge_conjugate(); } OpsArg::DedupChannels(ulps) => { grid.dedup_channels(*ulps); From 31aeb2ffb4c02dc621e8909588311294ad25313f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 7 Aug 2024 09:01:14 +0200 Subject: [PATCH 029/213] Fix remaining compilation errors --- pineappl_cli/src/export/applgrid.rs | 12 ++++++-- pineappl_cli/src/import/applgrid.rs | 10 ++++-- pineappl_cli/src/import/fastnlo.rs | 48 ++++++++++++++--------------- pineappl_cli/src/import/fktable.rs | 6 ++-- 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index e0666d7d..74e6b38b 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -25,7 +25,11 @@ fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result 0.0 { + bail!("subgrid has mua2 > 0.0, which APPLgrid does not support"); + } + if !approx_eq!(f64, ren, fac, ulps = 128) { bail!("subgrid has mur2 != muf2, which APPLgrid does not support"); } @@ -185,7 +189,11 @@ pub fn convert_into_applgrid( let appl_q2_idx: Vec<_> = subgrid .mu2_grid() .iter() - .map(|&Mu2 { ren, fac }| { + .map(|&Mu2 { ren, fac, frg }| { + if frg > 0.0 { + bail!("subgrid has mua2 > 0.0, which APPLgrid does not support"); + } + if !approx_eq!(f64, ren, fac, ulps = 128) { bail!("subgrid has mur2 != muf2, which APPLgrid does not support"); } diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index fe722bea..fb8109e5 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -133,10 +133,10 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul ); // from APPLgrid alone we don't know what type of convolution we have - pgrid.set_convolution(0, Convolution::UnpolPDF(2212)); + pgrid.convolutions_mut()[0] = Convolution::UnpolPDF(2212); if grid.isDIS() { - pgrid.set_convolution(1, Convolution::None); + pgrid.convolutions_mut()[1] = Convolution::None; } for bin in 0..grid.Nobs_internal() { @@ -147,7 +147,11 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let mu2_values: Vec<_> = (0..igrid.Ntau()) .map(|i| { let q2 = igrid.getQ2(i); - Mu2 { ren: q2, fac: q2 } + Mu2 { + ren: q2, + fac: q2, + frg: -1.0, + } }) .collect(); let x1_values: Vec<_> = (0..igrid.Ny1()) diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index fad1fa10..74dfbc5e 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -118,15 +118,12 @@ fn convert_coeff_add_fix( assert!(npdf <= 2); for index in 0..2 { - grid.set_convolution( - index, - if index < npdf { - // TODO: how do we determined the PID/type of the convolution for fixed tables? - Convolution::UnpolPDF(2212) - } else { - Convolution::None - }, - ); + grid.convolutions_mut()[index] = if index < npdf { + // TODO: how do we determined the PID/type of the convolution for fixed tables? + Convolution::UnpolPDF(2212) + } else { + Convolution::None + }; } let total_scalenodes: usize = table.GetTotalScalenodes().try_into().unwrap(); @@ -156,6 +153,7 @@ fn convert_coeff_add_fix( .map(|q| Mu2 { ren: q * q, fac: q * q, + frg: -1.0, }) .collect(); @@ -266,15 +264,12 @@ fn convert_coeff_add_flex( assert!(npdf <= 2); for index in 0..2 { - grid.set_convolution( - // UNWRAP: index is smaller than 2 - index.try_into().unwrap(), - if index < npdf { - Convolution::UnpolPDF(table.GetPDFPDG(index)) - } else { - Convolution::None - }, - ); + // UNWRAP: index is smaller than 2 + grid.convolutions_mut()[usize::try_from(index).unwrap()] = if index < npdf { + Convolution::UnpolPDF(table.GetPDFPDG(index)) + } else { + Convolution::None + }; } let rescale = 0.1_f64.powi(table.GetIXsectUnits() - ipub_units); @@ -295,6 +290,7 @@ fn convert_coeff_add_flex( .map(|(&s1, &s2)| Mu2 { ren: mur_ff.compute_scale(s1, s2), fac: muf_ff.compute_scale(s1, s2), + frg: -1.0, }) .collect(); let nx = ffi::GetNx(table, obs); @@ -470,14 +466,18 @@ pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> assert_eq!(labels.len(), dimensions); - for (dimension, label) in labels.iter().enumerate() { - result.set_key_value(&format!("x{}_label", dimension + 1), label); + for (dimension, label) in labels.into_iter().enumerate() { + result + .metadata_mut() + .insert(format!("x{}_label", dimension + 1), label); } - result.set_key_value("y_label", &ffi::GetXSDescr(file_as_table)); - result.set_key_value( - "fastnlo_scenario", - &ffi::GetScDescr(file_as_table).join("\n"), + result + .metadata_mut() + .insert("y_label".to_owned(), ffi::GetXSDescr(file_as_table)); + result.metadata_mut().insert( + "fastnlo_scenario".to_owned(), + ffi::GetScDescr(file_as_table).join("\n"), ); Ok(result) diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 68998ee7..ffe32245 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -109,12 +109,12 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { fktable.set_pid_basis(PidBasis::Evol); // legacy FK-tables only support unpolarized proton PDFs - fktable.set_convolution(0, Convolution::UnpolPDF(2212)); + fktable.convolutions_mut()[0] = Convolution::UnpolPDF(2212); if hadronic { - fktable.set_convolution(1, Convolution::UnpolPDF(2212)); + fktable.convolutions_mut()[1] = Convolution::UnpolPDF(2212); } else { - fktable.set_convolution(1, Convolution::None); + fktable.convolutions_mut()[1] = Convolution::None; } grid = Some(fktable); From 0d6e60272dc38d2369cbeba744d3951367170d8f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 7 Aug 2024 10:03:14 +0200 Subject: [PATCH 030/213] Make `pid_basis` a member of `Grid` --- pineappl/src/evolution.rs | 2 +- pineappl/src/grid.rs | 56 +++++++++++++----------------- pineappl/src/pids.rs | 3 +- pineappl_cli/src/import/fktable.rs | 2 +- pineappl_cli/src/write.rs | 2 +- 5 files changed, 30 insertions(+), 35 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 67284594..94fa299b 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -168,7 +168,7 @@ fn gluon_has_pid_zero(grid: &Grid) -> bool { .iter() .any(|entry| entry.entry().iter().any(|&(ref pids, _)| pids.iter().any(|&pid| pid == 0))) // and if the particle IDs are encoded using PDG MC IDs - && grid.pid_basis() == PidBasis::Pdg + && *grid.pid_basis() == PidBasis::Pdg } type Pid01IndexTuples = Vec<(usize, usize)>; diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index b2fa5eb4..cc87919d 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -167,6 +167,7 @@ pub struct Grid { subgrid_params: SubgridParams, metadata: BTreeMap, convolutions: Vec, + pid_basis: PidBasis, more_members: MoreMembers, } @@ -194,6 +195,8 @@ impl Grid { )), // TODO: add this as new parameter convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], + // TODO: make this a new parameter + pid_basis: PidBasis::Pdg, channels, subgrid_params, } @@ -236,36 +239,21 @@ impl Grid { subgrid_params, metadata: default_metadata(), convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], + pid_basis: PidBasis::Pdg, channels, more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), }) } - /// Return by which convention the particle IDs are encoded. + /// Return the convention by which the channels' PIDs are encoded. #[must_use] - pub fn pid_basis(&self) -> PidBasis { - if let Some(lumi_id_types) = self.metadata().get("lumi_id_types") { - match lumi_id_types.as_str() { - "pdg_mc_ids" => return PidBasis::Pdg, - "evol" => return PidBasis::Evol, - _ => unimplemented!("unknown particle ID convention {lumi_id_types}"), - } - } - - // if there's no basis explicitly set we're assuming to use PDG IDs - PidBasis::Pdg + pub fn pid_basis(&self) -> &PidBasis { + &self.pid_basis } /// Set the convention by which PIDs of channels are interpreted. - pub fn set_pid_basis(&mut self, pid_basis: PidBasis) { - match pid_basis { - PidBasis::Pdg => self - .metadata_mut() - .insert("lumi_id_types".to_owned(), "pdg_mc_ids".to_owned()), - PidBasis::Evol => self - .metadata_mut() - .insert("lumi_id_types".to_owned(), "evol".to_owned()), - }; + pub fn pid_basis_mut(&mut self) -> &mut PidBasis { + &mut self.pid_basis } fn pdg_channels(&self) -> Cow<[Channel]> { @@ -568,6 +556,16 @@ impl Grid { .into_iter() .collect(), convolutions: Self::read_convolutions_from_metadata(&grid), + pid_basis: grid + .key_values() + .and_then(|kv| kv.get("lumi_id_types")) + .map_or(PidBasis::Pdg, |lumi_id_types| { + match lumi_id_types.as_str() { + "pdg_mc_ids" => PidBasis::Pdg, + "evol" => PidBasis::Evol, + _ => panic!("unknown PID basis '{lumi_id_types}'"), + } + }), // TODO: make these proper members more_members: MoreMembers::V3(Mmv3 { remapper: grid.remapper().map(|r| { @@ -1462,7 +1460,7 @@ impl Grid { evolution::evolve_slice_with_one(self, &view, &info, order_mask, xi, alphas_table) }?; - let mut rhs = Self { + let rhs = Self { subgrids, channels, bin_limits: self.bin_limits.clone(), @@ -1470,12 +1468,10 @@ impl Grid { subgrid_params: SubgridParams::default(), metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), + pid_basis: info.pid_basis, more_members: self.more_members.clone(), }; - // TODO: use a new constructor to set this information - rhs.set_pid_basis(info.pid_basis); - if let Some(lhs) = &mut lhs { lhs.merge(rhs)?; } else { @@ -1600,7 +1596,7 @@ impl Grid { ) }?; - let mut rhs = Self { + let rhs = Self { subgrids, channels, bin_limits: self.bin_limits.clone(), @@ -1608,14 +1604,12 @@ impl Grid { subgrid_params: SubgridParams::default(), metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), + pid_basis: infos[0].pid_basis, more_members: self.more_members.clone(), }; assert_eq!(infos[0].pid_basis, infos[1].pid_basis); - // TODO: use a new constructor to set this information - rhs.set_pid_basis(infos[0].pid_basis); - if let Some(lhs) = &mut lhs { lhs.merge(rhs)?; } else { @@ -1738,7 +1732,7 @@ impl Grid { .map(|channel| Channel::translate(channel, &pids::pdg_mc_pids_to_evol)) .collect(); - self.set_pid_basis(PidBasis::Evol); + *self.pid_basis_mut() = PidBasis::Evol; } (PidBasis::Evol, PidBasis::Pdg) => { self.channels = self @@ -1747,7 +1741,7 @@ impl Grid { .map(|channel| Channel::translate(channel, &pids::evol_to_pdg_mc_ids)) .collect(); - self.set_pid_basis(PidBasis::Pdg); + *self.pid_basis_mut() = PidBasis::Pdg; } (PidBasis::Evol, PidBasis::Evol) | (PidBasis::Pdg, PidBasis::Pdg) => { // here's nothing to do diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index fca5a5b3..ea10dbb6 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -1,5 +1,6 @@ //! TODO +use serde::{Deserialize, Serialize}; use std::str::FromStr; use thiserror::Error; @@ -8,7 +9,7 @@ const EVOL_BASIS_IDS: [i32; 12] = [100, 103, 108, 115, 124, 135, 200, 203, 208, /// Particle ID bases. In `PineAPPL` every particle is identified using a particle identifier /// (PID), which is represented as an `i32`. The values of this `enum` specify how this value is /// interpreted. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum PidBasis { /// This basis uses the [particle data group](https://pdg.lbl.gov/) (PDG) PIDs. For a complete /// definition see the section 'Monte Carlo Particle Numbering Scheme' of the PDG Review, for diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index ffe32245..8020f7cc 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -106,7 +106,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { ); // explicitly set the evolution basis - fktable.set_pid_basis(PidBasis::Evol); + *fktable.pid_basis_mut() = PidBasis::Evol; // legacy FK-tables only support unpolarized proton PDFs fktable.convolutions_mut()[0] = Convolution::UnpolPDF(2212); diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index dc616f09..305acd75 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -502,7 +502,7 @@ impl Subcommand for Opts { _ => unreachable!(), }; - let pid_basis = grid.pid_basis(); + let pid_basis = *grid.pid_basis(); for channel in grid.channels_mut() { *channel = Channel::new( From bce1760dd9212f458a5a06eed86e3f60f8fa7aa3 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 7 Aug 2024 10:22:31 +0200 Subject: [PATCH 031/213] Prepare fixing the CAPI --- pineappl_capi/src/lib.rs | 56 +++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 315d6c2d..9e973ef3 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -696,7 +696,20 @@ pub unsafe extern "C" fn pineappl_grid_new( let (subgrid_type, subgrid_params, extra) = grid_params(key_vals); let lumi = unsafe { &*lumi }; - let mut grid = Box::new( + + if let Some(keyval) = key_vals { + if let Some(_value) = keyval.strings.get("initial_state_1") { + // TODO: set the first convolution type + todo!(); + } + + if let Some(_value) = keyval.strings.get("initial_state_2") { + // TODO: set the second convolution type + todo!(); + } + } + + let grid = Box::new( Grid::with_subgrid_type( lumi.0.clone(), orders, @@ -708,22 +721,6 @@ pub unsafe extern "C" fn pineappl_grid_new( .unwrap(), ); - if let Some(keyval) = key_vals { - if let Some(value) = keyval.strings.get("initial_state_1") { - grid.metadata_mut().insert( - "initial_state_1".to_owned(), - value.to_str().unwrap().to_owned(), - ); - } - - if let Some(value) = keyval.strings.get("initial_state_2") { - grid.metadata_mut().insert( - "initial_state_2".to_owned(), - value.to_str().unwrap().to_owned(), - ); - } - } - grid } @@ -901,6 +898,12 @@ pub unsafe extern "C" fn pineappl_grid_key_value( let key = unsafe { CStr::from_ptr(key) }; let key = key.to_string_lossy(); + // backwards compatibility + match key.as_ref() { + "initial_state_1" | "initial_state_2" => todo!(), + _ => {} + } + CString::new(grid.metadata().get(key.as_ref()).map_or("", String::as_str)) .unwrap() .into_raw() @@ -924,13 +927,20 @@ pub unsafe extern "C" fn pineappl_grid_set_key_value( value: *const c_char, ) { let grid = unsafe { &mut *grid }; - let key = unsafe { CStr::from_ptr(key) }; - let value = unsafe { CStr::from_ptr(value) }; + let key = unsafe { CStr::from_ptr(key) } + .to_string_lossy() + .into_owned(); + let value = unsafe { CStr::from_ptr(value) } + .to_string_lossy() + .into_owned(); + + // backwards compatibility + match key.as_str() { + "initial_state_1" | "initial_state_2" => todo!(), + _ => {} + } - grid.metadata_mut().insert( - key.to_string_lossy().into_owned(), - value.to_string_lossy().into_owned(), - ); + grid.metadata_mut().insert(key, value); } /// Sets a remapper for the grid. This can be used to 'upgrade' one-dimensional bin limits to From 5436765c319f20ace222ff7e2cb36f0fa0aa88b2 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 8 Aug 2024 11:15:41 +0200 Subject: [PATCH 032/213] Remove obsolete subgrid types --- pineappl/src/grid.rs | 11 +- pineappl/src/import_only_subgrid.rs | 301 ------- pineappl/src/lagrange_subgrid.rs | 1229 +++++---------------------- pineappl/src/lib.rs | 1 - pineappl/src/ntuple_subgrid.rs | 198 ----- pineappl/src/subgrid.rs | 13 +- pineappl/tests/drell_yan_lo.rs | 76 -- pineappl_cli/src/subgrids.rs | 4 - 8 files changed, 217 insertions(+), 1616 deletions(-) delete mode 100644 pineappl/src/ntuple_subgrid.rs diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index cc87919d..dbe7e903 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -7,8 +7,7 @@ use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorInfo, OperatorSliceInfo}; use super::fk_table::FkTable; use super::import_only_subgrid::ImportOnlySubgridV2; -use super::lagrange_subgrid::{LagrangeSparseSubgridV1, LagrangeSubgridV1, LagrangeSubgridV2}; -use super::ntuple_subgrid::NtupleSubgridV1; +use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; @@ -205,8 +204,7 @@ impl Grid { /// Constructor. This function can be used like `new`, but the additional parameter /// `subgrid_type` selects the underlying `Subgrid` type. Supported values are: /// - `LagrangeSubgrid` - /// - `LagrangeSparseSubgrid` - /// - `NtupleSubgrid` + /// - `LagrangeSubgridV2` /// /// # Errors /// @@ -223,9 +221,6 @@ impl Grid { "LagrangeSubgrid" | "LagrangeSubgridV2" => { LagrangeSubgridV2::new(&subgrid_params, &extra).into() } - "LagrangeSubgridV1" => LagrangeSubgridV1::new(&subgrid_params).into(), - "NtupleSubgrid" => NtupleSubgridV1::new().into(), - "LagrangeSparseSubgrid" => LagrangeSparseSubgridV1::new(&subgrid_params).into(), _ => return Err(GridError::UnknownSubgridType(subgrid_type.to_owned())), }; @@ -1065,8 +1060,6 @@ impl Grid { _ if subgrid.is_empty() => { *subgrid = EmptySubgridV1.into(); } - // can't be optimized without losing information - SubgridEnum::NtupleSubgridV1(_) => continue, _ => { // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should // replace this with a method diff --git a/pineappl/src/import_only_subgrid.rs b/pineappl/src/import_only_subgrid.rs index 4cff492e..4ff5d622 100644 --- a/pineappl/src/import_only_subgrid.rs +++ b/pineappl/src/import_only_subgrid.rs @@ -7,181 +7,6 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::mem; -/// TODO -#[derive(Clone, Deserialize, Serialize)] -pub struct ImportOnlySubgridV1 { - array: SparseArray3, - q2_grid: Vec, - x1_grid: Vec, - x2_grid: Vec, -} - -impl ImportOnlySubgridV1 { - /// Constructor. - #[must_use] - pub fn new( - array: SparseArray3, - q2_grid: Vec, - x1_grid: Vec, - x2_grid: Vec, - ) -> Self { - Self { - array, - q2_grid, - x1_grid, - x2_grid, - } - } - - /// Return the array containing the numerical values of the grid. - pub fn array_mut(&mut self) -> &mut SparseArray3 { - &mut self.array - } -} - -impl Subgrid for ImportOnlySubgridV1 { - fn convolve( - &self, - _: &[f64], - _: &[f64], - _: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - self.array - .indexed_iter() - .map(|((imu2, ix1, ix2), sigma)| sigma * lumi(ix1, ix2, imu2)) - .sum() - } - - fn fill(&mut self, _: &Ntuple) { - panic!("ImportOnlySubgridV1 doesn't support the fill operation"); - } - - fn mu2_grid(&self) -> Cow<[Mu2]> { - self.q2_grid - .iter() - .copied() - .map(|q2| Mu2 { - ren: q2, - fac: q2, - frg: -1.0, - }) - .collect() - } - - fn x1_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&self.x1_grid) - } - - fn x2_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&self.x2_grid) - } - - fn is_empty(&self) -> bool { - self.array.is_empty() - } - - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - if let SubgridEnum::ImportOnlySubgridV1(other_grid) = other { - if self.array.is_empty() && !transpose { - mem::swap(&mut self.array, &mut other_grid.array); - } else { - // TODO: the general case isn't implemented - assert!(self.x1_grid() == other_grid.x1_grid()); - assert!(self.x2_grid() == other_grid.x2_grid()); - - for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() { - // the following should always be the case - assert_eq!(mu2.ren, mu2.fac); - let q2 = &mu2.ren; - - let index = match self - .q2_grid - .binary_search_by(|val| val.partial_cmp(q2).unwrap()) - { - Ok(index) => index, - Err(index) => { - self.q2_grid.insert(index, *q2); - self.array.increase_x_at(index); - index - } - }; - - for ((_, j, k), value) in other_grid - .array - .indexed_iter() - .filter(|&((i, _, _), _)| i == other_index) - { - let (j, k) = if transpose { (k, j) } else { (j, k) }; - self.array[[index, j, k]] += value; - } - } - } - } else { - todo!(); - } - } - - fn scale(&mut self, factor: f64) { - if factor == 0.0 { - self.array.clear(); - } else { - self.array.iter_mut().for_each(|x| *x *= factor); - } - } - - fn symmetrize(&mut self) { - let mut new_array = - SparseArray3::new(self.q2_grid.len(), self.x1_grid.len(), self.x2_grid.len()); - - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k >= j) { - new_array[[i, j, k]] = sigma; - } - // do not change the diagonal entries (k==j) - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k < j) { - new_array[[i, k, j]] += sigma; - } - - mem::swap(&mut self.array, &mut new_array); - } - - fn clone_empty(&self) -> SubgridEnum { - Self { - array: SparseArray3::new(self.q2_grid.len(), self.x1_grid.len(), self.x2_grid.len()), - q2_grid: self.q2_grid.clone(), - x1_grid: self.x1_grid.clone(), - x2_grid: self.x2_grid.clone(), - } - .into() - } - - fn indexed_iter(&self) -> SubgridIndexedIter { - Box::new(self.array.indexed_iter()) - } - - fn stats(&self) -> Stats { - Stats { - total: self.q2_grid.len() * self.x1_grid.len() * self.x2_grid.len(), - allocated: self.array.len() + self.array.zeros(), - zeros: self.array.zeros(), - overhead: self.array.overhead(), - bytes_per_value: mem::size_of::(), - } - } - - fn static_scale(&self) -> Option { - if let &[static_scale] = self.q2_grid.as_slice() { - Some(Mu2 { - ren: static_scale, - fac: static_scale, - frg: -1.0, - }) - } else { - None - } - } -} - /// TODO #[derive(Clone, Deserialize, Serialize)] pub struct ImportOnlySubgridV2 { @@ -448,118 +273,6 @@ mod tests { use rand::Rng; use rand_pcg::Pcg64; - #[test] - fn test_v1() { - let x = vec![ - 0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0, - ]; - let mut grid1: SubgridEnum = ImportOnlySubgridV1::new( - SparseArray3::new(1, 10, 10), - vec![0.0], - x.clone(), - x.clone(), - ) - .into(); - - assert_eq!( - grid1.stats(), - Stats { - total: 100, - allocated: 0, - zeros: 0, - overhead: 2, - bytes_per_value: 8, - } - ); - - let mu2 = vec![Mu2 { - ren: 0.0, - fac: 0.0, - frg: -1.0, - }]; - - assert_eq!(grid1.mu2_grid().as_ref(), mu2); - assert_eq!(grid1.x1_grid().as_ref(), x); - assert_eq!(grid1.x2_grid(), grid1.x1_grid()); - - assert!(grid1.is_empty()); - - // only use exactly representable numbers here so that we can avoid using approx_eq - if let SubgridEnum::ImportOnlySubgridV1(ref mut x) = grid1 { - x.array_mut()[[0, 1, 2]] = 1.0; - x.array_mut()[[0, 1, 3]] = 2.0; - x.array_mut()[[0, 4, 3]] = 4.0; - x.array_mut()[[0, 7, 1]] = 8.0; - } else { - unreachable!(); - } - - assert!(!grid1.is_empty()); - - assert_eq!(grid1.indexed_iter().next(), Some(((0, 1, 2), 1.0))); - assert_eq!(grid1.indexed_iter().nth(1), Some(((0, 1, 3), 2.0))); - assert_eq!(grid1.indexed_iter().nth(2), Some(((0, 4, 3), 4.0))); - assert_eq!(grid1.indexed_iter().nth(3), Some(((0, 7, 1), 8.0))); - - // symmetric luminosity function - let lumi = - &mut (|ix1, ix2, _| x[ix1] * x[ix2]) as &mut dyn FnMut(usize, usize, usize) -> f64; - - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 0.228515625); - - // create grid with transposed entries, but different q2 - let mut grid2: SubgridEnum = ImportOnlySubgridV1::new( - SparseArray3::new(1, 10, 10), - vec![1.0], - x.clone(), - x.clone(), - ) - .into(); - if let SubgridEnum::ImportOnlySubgridV1(ref mut x) = grid2 { - x.array_mut()[[0, 2, 1]] = 1.0; - x.array_mut()[[0, 3, 1]] = 2.0; - x.array_mut()[[0, 3, 4]] = 4.0; - x.array_mut()[[0, 1, 7]] = 8.0; - } else { - unreachable!(); - } - assert_eq!(grid2.convolve(&x, &x, &mu2, lumi), 0.228515625); - - assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); - assert_eq!(grid2.indexed_iter().nth(1), Some(((0, 2, 1), 1.0))); - assert_eq!(grid2.indexed_iter().nth(2), Some(((0, 3, 1), 2.0))); - assert_eq!(grid2.indexed_iter().nth(3), Some(((0, 3, 4), 4.0))); - - grid1.merge(&mut grid2, false); - - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); - - let mut grid1 = { - let mut g = grid1.clone_empty(); - g.merge(&mut grid1, false); - g - }; - - // the luminosity function is symmetric, so after symmetrization the result must be - // unchanged - grid1.symmetrize(); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); - - grid1.scale(2.0); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 4.0 * 0.228515625); - - assert_eq!( - grid1.stats(), - Stats { - total: 200, - allocated: 14, - zeros: 6, - overhead: 42, - bytes_per_value: 8, - } - ); - } - #[test] fn test_v2() { let x = vec![ @@ -669,20 +382,6 @@ mod tests { ); } - #[test] - #[should_panic(expected = "ImportOnlySubgridV1 doesn't support the fill operation")] - fn fill_panic_v1() { - let mut grid = - ImportOnlySubgridV1::new(SparseArray3::new(1, 1, 1), vec![1.0], vec![1.0], vec![1.0]); - - grid.fill(&Ntuple { - x1: 0.0, - x2: 0.0, - q2: 0.0, - weight: 1.0, - }); - } - #[test] #[should_panic(expected = "ImportOnlySubgridV2 doesn't support the fill operation")] fn fill_panic_v2() { diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 71995db6..ba6642bb 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -2,7 +2,6 @@ use super::convert::{f64_from_usize, usize_from_f64}; use super::grid::Ntuple; -use super::sparse_array3::SparseArray3; use super::subgrid::{ ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, }; @@ -61,61 +60,96 @@ fn fi(i: usize, n: usize, u: f64) -> f64 { /// Subgrid which uses Lagrange-interpolation. #[derive(Clone, Deserialize, Serialize)] -pub struct LagrangeSubgridV1 { +pub struct LagrangeSubgridV2 { grid: Option>, ntau: usize, - ny: usize, - yorder: usize, + ny1: usize, + ny2: usize, + y1order: usize, + y2order: usize, tauorder: usize, itaumin: usize, itaumax: usize, - reweight: bool, - ymin: f64, - ymax: f64, + reweight1: bool, + reweight2: bool, + y1min: f64, + y1max: f64, + y2min: f64, + y2max: f64, taumin: f64, taumax: f64, + pub(crate) static_q2: f64, } -impl LagrangeSubgridV1 { +impl LagrangeSubgridV2 { /// Constructor. #[must_use] - pub fn new(subgrid_params: &SubgridParams) -> Self { + pub fn new(subgrid_params: &SubgridParams, extra_params: &ExtraSubgridParams) -> Self { Self { grid: None, ntau: subgrid_params.q2_bins(), - ny: subgrid_params.x_bins(), - yorder: subgrid_params.x_order(), + ny1: subgrid_params.x_bins(), + ny2: extra_params.x2_bins(), + y1order: subgrid_params.x_order(), + y2order: extra_params.x2_order(), tauorder: subgrid_params.q2_order(), itaumin: 0, itaumax: 0, - reweight: subgrid_params.reweight(), - ymin: fy(subgrid_params.x_max()), - ymax: fy(subgrid_params.x_min()), + reweight1: subgrid_params.reweight(), + reweight2: extra_params.reweight2(), + y1min: fy(subgrid_params.x_max()), + y1max: fy(subgrid_params.x_min()), + y2min: fy(extra_params.x2_max()), + y2max: fy(extra_params.x2_min()), taumin: ftau(subgrid_params.q2_min()), taumax: ftau(subgrid_params.q2_max()), + static_q2: 0.0, } } - fn deltay(&self) -> f64 { - (self.ymax - self.ymin) / f64_from_usize(self.ny - 1) + fn deltay1(&self) -> f64 { + (self.y1max - self.y1min) / f64_from_usize(self.ny1 - 1) + } + + fn deltay2(&self) -> f64 { + (self.y1max - self.y2min) / f64_from_usize(self.ny2 - 1) } fn deltatau(&self) -> f64 { (self.taumax - self.taumin) / f64_from_usize(self.ntau - 1) } - fn gety(&self, iy: usize) -> f64 { - f64_from_usize(iy).mul_add(self.deltay(), self.ymin) + fn gety1(&self, iy: usize) -> f64 { + if self.y1min == self.y1max { + debug_assert_eq!(iy, 0); + self.y1min + } else { + f64_from_usize(iy).mul_add(self.deltay1(), self.y1min) + } + } + + fn gety2(&self, iy: usize) -> f64 { + if self.y2min == self.y2max { + debug_assert_eq!(iy, 0); + self.y2min + } else { + f64_from_usize(iy).mul_add(self.deltay2(), self.y2min) + } } fn gettau(&self, iy: usize) -> f64 { - f64_from_usize(iy).mul_add(self.deltatau(), self.taumin) + if self.taumin == self.taumax { + debug_assert_eq!(iy, 0); + self.taumin + } else { + f64_from_usize(iy).mul_add(self.deltatau(), self.taumin) + } } fn increase_tau(&mut self, new_itaumin: usize, new_itaumax: usize) { let min_diff = self.itaumin - new_itaumin; - let mut new_grid = Array3::zeros((new_itaumax - new_itaumin, self.ny, self.ny)); + let mut new_grid = Array3::zeros((new_itaumax - new_itaumin, self.ny1, self.ny2)); for ((i, j, k), value) in self.grid.as_ref().unwrap().indexed_iter() { new_grid[[i + min_diff, j, k]] = *value; @@ -128,7 +162,7 @@ impl LagrangeSubgridV1 { } } -impl Subgrid for LagrangeSubgridV1 { +impl Subgrid for LagrangeSubgridV2 { fn convolve( &self, x1: &[f64], @@ -143,8 +177,11 @@ impl Subgrid for LagrangeSubgridV1 { 0.0 } else { let mut value = sigma * lumi(ix1, ix2, imu2 + self.itaumin); - if self.reweight { - value *= weightfun(x1[ix1]) * weightfun(x2[ix2]); + if self.reweight1 { + value *= weightfun(x1[ix1]); + } + if self.reweight2 { + value *= weightfun(x2[ix2]); } value } @@ -162,410 +199,37 @@ impl Subgrid for LagrangeSubgridV1 { let y2 = fy(ntuple.x2); let tau = ftau(ntuple.q2); - if (y2 < self.ymin) - || (y2 > self.ymax) - || (y1 < self.ymin) - || (y1 > self.ymax) + if self.static_q2 == 0.0 { + self.static_q2 = ntuple.q2; + } else if (self.static_q2 != -1.0) && (self.static_q2 != ntuple.q2) { + self.static_q2 = -1.0; + } + + if (y2 < self.y2min) + || (y2 > self.y2max) + || (y1 < self.y1min) + || (y1 > self.y1max) || (tau < self.taumin) || (tau > self.taumax) { return; } - let k1 = usize_from_f64((y1 - self.ymin) / self.deltay() - f64_from_usize(self.yorder / 2)) - .min(self.ny - 1 - self.yorder); - let k2 = usize_from_f64((y2 - self.ymin) / self.deltay() - f64_from_usize(self.yorder / 2)) - .min(self.ny - 1 - self.yorder); + let k1 = + usize_from_f64((y1 - self.y1min) / self.deltay1() - f64_from_usize(self.y1order / 2)) + .min(self.ny1 - 1 - self.y1order); + let k2 = + usize_from_f64((y2 - self.y2min) / self.deltay2() - f64_from_usize(self.y2order / 2)) + .min(self.ny2 - 1 - self.y2order); - let u_y1 = (y1 - self.gety(k1)) / self.deltay(); - let u_y2 = (y2 - self.gety(k2)) / self.deltay(); + let u_y1 = (y1 - self.gety1(k1)) / self.deltay1(); + let u_y2 = (y2 - self.gety2(k2)) / self.deltay2(); - let fi1: ArrayVec<_, 8> = (0..=self.yorder) - .map(|i| fi(i, self.yorder, u_y1)) + let fi1: ArrayVec<_, 8> = (0..=self.y1order) + .map(|i| fi(i, self.y1order, u_y1)) .collect(); - let fi2: ArrayVec<_, 8> = (0..=self.yorder) - .map(|i| fi(i, self.yorder, u_y2)) - .collect(); - - let k3 = usize_from_f64( - (tau - self.taumin) / self.deltatau() - f64_from_usize(self.tauorder / 2), - ) - .min(self.ntau - 1 - self.tauorder); - - let u_tau = (tau - self.gettau(k3)) / self.deltatau(); - - let factor = if self.reweight { - 1.0 / (weightfun(ntuple.x1) * weightfun(ntuple.x2)) - } else { - 1.0 - }; - - let size = self.tauorder + 1; - let ny = self.ny; - - if self.grid.is_none() { - self.itaumin = k3; - self.itaumax = k3 + size; - } else if k3 < self.itaumin || k3 + size > self.itaumax { - self.increase_tau(self.itaumin.min(k3), self.itaumax.max(k3 + size)); - } - - for i3 in 0..=self.tauorder { - let fi3i3 = fi(i3, self.tauorder, u_tau); - - for (i1, fi1i1) in fi1.iter().enumerate() { - for (i2, fi2i2) in fi2.iter().enumerate() { - let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * ntuple.weight; - - let grid = self - .grid - .get_or_insert_with(|| Array3::zeros((size, ny, ny))); - - grid[[k3 + i3 - self.itaumin, k1 + i1, k2 + i2]] += fillweight; - } - } - } - } - - fn mu2_grid(&self) -> Cow<[Mu2]> { - (0..self.ntau) - .map(|itau| { - let q2 = fq2(self.gettau(itau)); - Mu2 { - ren: q2, - fac: q2, - frg: -1.0, - } - }) - .collect() - } - - fn x1_grid(&self) -> Cow<[f64]> { - (0..self.ny).map(|iy| fx(self.gety(iy))).collect() - } - - fn x2_grid(&self) -> Cow<[f64]> { - self.x1_grid() - } - - fn is_empty(&self) -> bool { - self.grid.is_none() - } - - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - let x1_equal = self.x1_grid() == other.x1_grid(); - let x2_equal = self.x2_grid() == other.x2_grid(); - - if let SubgridEnum::LagrangeSubgridV1(other_grid) = other { - if let Some(other_grid_grid) = &mut other_grid.grid { - if self.grid.is_some() { - // TODO: the general case isn't implemented - assert!(x1_equal); - assert!(x2_equal); - - let new_itaumin = self.itaumin.min(other_grid.itaumin); - let new_itaumax = self.itaumax.max(other_grid.itaumax); - let offset = other_grid.itaumin.saturating_sub(self.itaumin); - - // TODO: we need much more checks here if there subgrids are compatible at all - - if (self.itaumin != new_itaumin) || (self.itaumax != new_itaumax) { - self.increase_tau(new_itaumin, new_itaumax); - } - - let self_grid = self.grid.as_mut().unwrap(); - - if transpose { - for ((i, k, j), value) in other_grid_grid.indexed_iter() { - self_grid[[i + offset, j, k]] += value; - } - } else { - for ((i, j, k), value) in other_grid_grid.indexed_iter() { - self_grid[[i + offset, j, k]] += value; - } - } - } else { - self.grid = other_grid.grid.take(); - self.itaumin = other_grid.itaumin; - self.itaumax = other_grid.itaumax; - - if transpose { - if let Some(grid) = &mut self.grid { - grid.swap_axes(1, 2); - } - } - } - } - } else { - todo!(); - } - } - - fn scale(&mut self, factor: f64) { - if factor == 0.0 { - self.grid = None; - } else if let Some(self_grid) = &mut self.grid { - self_grid.iter_mut().for_each(|x| *x *= factor); - } - } - - fn symmetrize(&mut self) { - if let Some(grid) = self.grid.as_mut() { - let (i_size, j_size, k_size) = grid.dim(); - - for i in 0..i_size { - for j in 0..j_size { - for k in j + 1..k_size { - grid[[i, j, k]] += grid[[i, k, j]]; - grid[[i, k, j]] = 0.0; - } - } - } - } - } - - fn clone_empty(&self) -> SubgridEnum { - Self { - grid: None, - ntau: self.ntau, - ny: self.ny, - yorder: self.yorder, - tauorder: self.tauorder, - itaumin: 0, - itaumax: 0, - reweight: self.reweight, - ymin: self.ymin, - ymax: self.ymax, - taumin: self.taumin, - taumax: self.taumax, - } - .into() - } - - fn indexed_iter(&self) -> SubgridIndexedIter { - self.grid.as_ref().map_or_else( - || Box::new(iter::empty()) as Box>, - |grid| { - Box::new(grid.indexed_iter().filter(|(_, &value)| value != 0.0).map( - |(tuple, &value)| { - ( - (self.itaumin + tuple.0, tuple.1, tuple.2), - value - * if self.reweight { - weightfun(fx(self.gety(tuple.1))) - * weightfun(fx(self.gety(tuple.2))) - } else { - 1.0 - }, - ) - }, - )) - }, - ) - } - - fn stats(&self) -> Stats { - let (non_zeros, zeros) = self.grid.as_ref().map_or((0, 0), |array| { - array.iter().fold((0, 0), |mut result, value| { - if *value == 0.0 { - result.0 += 1; - } else { - result.1 += 1; - } - result - }) - }); - - Stats { - total: non_zeros + zeros, - allocated: non_zeros + zeros, - zeros, - overhead: 0, - bytes_per_value: mem::size_of::(), - } - } - - fn static_scale(&self) -> Option { - if let [static_scale] = self.mu2_grid().as_ref() { - Some(static_scale.clone()) - } else { - None - } - } -} - -/// Subgrid which uses Lagrange-interpolation. -#[derive(Clone, Deserialize, Serialize)] -pub struct LagrangeSubgridV2 { - grid: Option>, - ntau: usize, - ny1: usize, - ny2: usize, - y1order: usize, - y2order: usize, - tauorder: usize, - itaumin: usize, - itaumax: usize, - reweight1: bool, - reweight2: bool, - y1min: f64, - y1max: f64, - y2min: f64, - y2max: f64, - taumin: f64, - taumax: f64, - pub(crate) static_q2: f64, -} - -impl LagrangeSubgridV2 { - /// Constructor. - #[must_use] - pub fn new(subgrid_params: &SubgridParams, extra_params: &ExtraSubgridParams) -> Self { - Self { - grid: None, - ntau: subgrid_params.q2_bins(), - ny1: subgrid_params.x_bins(), - ny2: extra_params.x2_bins(), - y1order: subgrid_params.x_order(), - y2order: extra_params.x2_order(), - tauorder: subgrid_params.q2_order(), - itaumin: 0, - itaumax: 0, - reweight1: subgrid_params.reweight(), - reweight2: extra_params.reweight2(), - y1min: fy(subgrid_params.x_max()), - y1max: fy(subgrid_params.x_min()), - y2min: fy(extra_params.x2_max()), - y2max: fy(extra_params.x2_min()), - taumin: ftau(subgrid_params.q2_min()), - taumax: ftau(subgrid_params.q2_max()), - static_q2: 0.0, - } - } - - fn deltay1(&self) -> f64 { - (self.y1max - self.y1min) / f64_from_usize(self.ny1 - 1) - } - - fn deltay2(&self) -> f64 { - (self.y1max - self.y2min) / f64_from_usize(self.ny2 - 1) - } - - fn deltatau(&self) -> f64 { - (self.taumax - self.taumin) / f64_from_usize(self.ntau - 1) - } - - fn gety1(&self, iy: usize) -> f64 { - if self.y1min == self.y1max { - debug_assert_eq!(iy, 0); - self.y1min - } else { - f64_from_usize(iy).mul_add(self.deltay1(), self.y1min) - } - } - - fn gety2(&self, iy: usize) -> f64 { - if self.y2min == self.y2max { - debug_assert_eq!(iy, 0); - self.y2min - } else { - f64_from_usize(iy).mul_add(self.deltay2(), self.y2min) - } - } - - fn gettau(&self, iy: usize) -> f64 { - if self.taumin == self.taumax { - debug_assert_eq!(iy, 0); - self.taumin - } else { - f64_from_usize(iy).mul_add(self.deltatau(), self.taumin) - } - } - - fn increase_tau(&mut self, new_itaumin: usize, new_itaumax: usize) { - let min_diff = self.itaumin - new_itaumin; - - let mut new_grid = Array3::zeros((new_itaumax - new_itaumin, self.ny1, self.ny2)); - - for ((i, j, k), value) in self.grid.as_ref().unwrap().indexed_iter() { - new_grid[[i + min_diff, j, k]] = *value; - } - - self.itaumin = new_itaumin; - self.itaumax = new_itaumax; - - mem::swap(&mut self.grid, &mut Some(new_grid)); - } -} - -impl Subgrid for LagrangeSubgridV2 { - fn convolve( - &self, - x1: &[f64], - x2: &[f64], - _: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - self.grid.as_ref().map_or(0.0, |grid| { - grid.indexed_iter() - .map(|((imu2, ix1, ix2), &sigma)| { - if sigma == 0.0 { - 0.0 - } else { - let mut value = sigma * lumi(ix1, ix2, imu2 + self.itaumin); - if self.reweight1 { - value *= weightfun(x1[ix1]); - } - if self.reweight2 { - value *= weightfun(x2[ix2]); - } - value - } - }) - .sum() - }) - } - - fn fill(&mut self, ntuple: &Ntuple) { - if ntuple.weight == 0.0 { - return; - } - - let y1 = fy(ntuple.x1); - let y2 = fy(ntuple.x2); - let tau = ftau(ntuple.q2); - - if self.static_q2 == 0.0 { - self.static_q2 = ntuple.q2; - } else if (self.static_q2 != -1.0) && (self.static_q2 != ntuple.q2) { - self.static_q2 = -1.0; - } - - if (y2 < self.y2min) - || (y2 > self.y2max) - || (y1 < self.y1min) - || (y1 > self.y1max) - || (tau < self.taumin) - || (tau > self.taumax) - { - return; - } - - let k1 = - usize_from_f64((y1 - self.y1min) / self.deltay1() - f64_from_usize(self.y1order / 2)) - .min(self.ny1 - 1 - self.y1order); - let k2 = - usize_from_f64((y2 - self.y2min) / self.deltay2() - f64_from_usize(self.y2order / 2)) - .min(self.ny2 - 1 - self.y2order); - - let u_y1 = (y1 - self.gety1(k1)) / self.deltay1(); - let u_y2 = (y2 - self.gety2(k2)) / self.deltay2(); - - let fi1: ArrayVec<_, 8> = (0..=self.y1order) - .map(|i| fi(i, self.y1order, u_y1)) - .collect(); - let fi2: ArrayVec<_, 8> = (0..=self.y2order) - .map(|i| fi(i, self.y2order, u_y2)) + let fi2: ArrayVec<_, 8> = (0..=self.y2order) + .map(|i| fi(i, self.y2order, u_y2)) .collect(); let k3 = usize_from_f64( @@ -581,342 +245,21 @@ impl Subgrid for LagrangeSubgridV2 { } else { 1.0 } * if self.reweight2 { - weightfun(ntuple.x2) - } else { - 1.0 - }); - - let size = self.tauorder + 1; - let ny1 = self.ny1; - let ny2 = self.ny2; - - if self.grid.is_none() { - self.itaumin = k3; - self.itaumax = k3 + size; - } else if k3 < self.itaumin || k3 + size > self.itaumax { - self.increase_tau(self.itaumin.min(k3), self.itaumax.max(k3 + size)); - } - - for i3 in 0..=self.tauorder { - let fi3i3 = fi(i3, self.tauorder, u_tau); - - for (i1, fi1i1) in fi1.iter().enumerate() { - for (i2, fi2i2) in fi2.iter().enumerate() { - let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * ntuple.weight; - - let grid = self - .grid - .get_or_insert_with(|| Array3::zeros((size, ny1, ny2))); - - grid[[k3 + i3 - self.itaumin, k1 + i1, k2 + i2]] += fillweight; - } - } - } - } - - fn mu2_grid(&self) -> Cow<[Mu2]> { - (0..self.ntau) - .map(|itau| { - let q2 = fq2(self.gettau(itau)); - Mu2 { - ren: q2, - fac: q2, - frg: -1.0, - } - }) - .collect() - } - - fn x1_grid(&self) -> Cow<[f64]> { - (0..self.ny1).map(|iy| fx(self.gety1(iy))).collect() - } - - fn x2_grid(&self) -> Cow<[f64]> { - (0..self.ny2).map(|iy| fx(self.gety2(iy))).collect() - } - - fn is_empty(&self) -> bool { - self.grid.is_none() - } - - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - let x1_equal = self.x1_grid() == other.x1_grid(); - let x2_equal = self.x2_grid() == other.x2_grid(); - - if let SubgridEnum::LagrangeSubgridV2(other_grid) = other { - if let Some(other_grid_grid) = &mut other_grid.grid { - if self.grid.is_some() { - // TODO: the general case isn't implemented - assert!(x1_equal); - assert!(x2_equal); - - let new_itaumin = self.itaumin.min(other_grid.itaumin); - let new_itaumax = self.itaumax.max(other_grid.itaumax); - let offset = other_grid.itaumin.saturating_sub(self.itaumin); - - // TODO: we need much more checks here if there subgrids are compatible at all - - if (self.itaumin != new_itaumin) || (self.itaumax != new_itaumax) { - self.increase_tau(new_itaumin, new_itaumax); - } - - if (other_grid.static_q2 == -1.0) || (self.static_q2 != other_grid.static_q2) { - self.static_q2 = -1.0; - } - - let self_grid = self.grid.as_mut().unwrap(); - - if transpose { - for ((i, k, j), value) in other_grid_grid.indexed_iter() { - self_grid[[i + offset, j, k]] += value; - } - } else { - for ((i, j, k), value) in other_grid_grid.indexed_iter() { - self_grid[[i + offset, j, k]] += value; - } - } - } else { - self.grid = other_grid.grid.take(); - self.itaumin = other_grid.itaumin; - self.itaumax = other_grid.itaumax; - self.static_q2 = other_grid.static_q2; - - if transpose { - if let Some(grid) = &mut self.grid { - grid.swap_axes(1, 2); - } - } - } - } - } else { - todo!(); - } - } - - fn scale(&mut self, factor: f64) { - if factor == 0.0 { - self.grid = None; - } else if let Some(self_grid) = &mut self.grid { - self_grid.iter_mut().for_each(|x| *x *= factor); - } - } - - fn symmetrize(&mut self) { - if let Some(grid) = self.grid.as_mut() { - let (i_size, j_size, k_size) = grid.dim(); - - for i in 0..i_size { - for j in 0..j_size { - for k in j + 1..k_size { - grid[[i, j, k]] += grid[[i, k, j]]; - grid[[i, k, j]] = 0.0; - } - } - } - } - } - - fn clone_empty(&self) -> SubgridEnum { - Self { - grid: None, - ntau: self.ntau, - ny1: self.ny1, - ny2: self.ny2, - y1order: self.y1order, - y2order: self.y2order, - tauorder: self.tauorder, - itaumin: 0, - itaumax: 0, - reweight1: self.reweight1, - reweight2: self.reweight2, - y1min: self.y1min, - y1max: self.y1max, - y2min: self.y2min, - y2max: self.y2max, - taumin: self.taumin, - taumax: self.taumax, - static_q2: 0.0, - } - .into() - } - - fn indexed_iter(&self) -> SubgridIndexedIter { - self.grid.as_ref().map_or_else( - || Box::new(iter::empty()) as Box>, - |grid| { - Box::new(grid.indexed_iter().filter(|(_, &value)| value != 0.0).map( - |(tuple, &value)| { - ( - (self.itaumin + tuple.0, tuple.1, tuple.2), - value - * if self.reweight1 { - weightfun(fx(self.gety1(tuple.1))) - } else { - 1.0 - } - * if self.reweight2 { - weightfun(fx(self.gety2(tuple.2))) - } else { - 1.0 - }, - ) - }, - )) - }, - ) - } - - fn stats(&self) -> Stats { - let (non_zeros, zeros) = self.grid.as_ref().map_or((0, 0), |array| { - array.iter().fold((0, 0), |mut result, value| { - if *value == 0.0 { - result.0 += 1; - } else { - result.1 += 1; - } - result - }) - }); - - Stats { - total: non_zeros + zeros, - allocated: non_zeros + zeros, - zeros, - overhead: 0, - bytes_per_value: mem::size_of::(), - } - } - - fn static_scale(&self) -> Option { - (self.static_q2 > 0.0).then_some(Mu2 { - ren: self.static_q2, - fac: self.static_q2, - frg: -1.0, - }) - } -} - -/// Subgrid which uses Lagrange-interpolation, but also stores its contents in a space-efficient -/// structure. -#[derive(Clone, Deserialize, Serialize)] -pub struct LagrangeSparseSubgridV1 { - array: SparseArray3, - ntau: usize, - ny: usize, - yorder: usize, - tauorder: usize, - reweight: bool, - ymin: f64, - ymax: f64, - taumin: f64, - taumax: f64, -} - -impl LagrangeSparseSubgridV1 { - /// Constructor. - #[must_use] - pub fn new(subgrid_params: &SubgridParams) -> Self { - Self { - array: SparseArray3::new( - subgrid_params.q2_bins(), - subgrid_params.x_bins(), - subgrid_params.x_bins(), - ), - ntau: subgrid_params.q2_bins(), - ny: subgrid_params.x_bins(), - yorder: subgrid_params.x_order(), - tauorder: subgrid_params.q2_order(), - reweight: subgrid_params.reweight(), - ymin: fy(subgrid_params.x_max()), - ymax: fy(subgrid_params.x_min()), - taumin: ftau(subgrid_params.q2_min()), - taumax: ftau(subgrid_params.q2_max()), - } - } - - fn deltay(&self) -> f64 { - (self.ymax - self.ymin) / f64_from_usize(self.ny - 1) - } - - fn deltatau(&self) -> f64 { - (self.taumax - self.taumin) / f64_from_usize(self.ntau - 1) - } - - fn gety(&self, iy: usize) -> f64 { - f64_from_usize(iy).mul_add(self.deltay(), self.ymin) - } - - fn gettau(&self, iy: usize) -> f64 { - f64_from_usize(iy).mul_add(self.deltatau(), self.taumin) - } -} - -impl Subgrid for LagrangeSparseSubgridV1 { - fn convolve( - &self, - x1: &[f64], - x2: &[f64], - _: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - self.array - .indexed_iter() - .map(|((imu2, ix1, ix2), sigma)| { - let mut value = sigma * lumi(ix1, ix2, imu2); - if self.reweight { - value *= weightfun(x1[ix1]) * weightfun(x2[ix2]); - } - value - }) - .sum() - } - - fn fill(&mut self, ntuple: &Ntuple) { - if ntuple.weight == 0.0 { - return; - } - - let y1 = fy(ntuple.x1); - let y2 = fy(ntuple.x2); - let tau = ftau(ntuple.q2); - - if (y2 < self.ymin) - || (y2 > self.ymax) - || (y1 < self.ymin) - || (y1 > self.ymax) - || (tau < self.taumin) - || (tau > self.taumax) - { - return; - } - - let k1 = usize_from_f64((y1 - self.ymin) / self.deltay() - f64_from_usize(self.yorder / 2)) - .min(self.ny - 1 - self.yorder); - let k2 = usize_from_f64((y2 - self.ymin) / self.deltay() - f64_from_usize(self.yorder / 2)) - .min(self.ny - 1 - self.yorder); - - let u_y1 = (y1 - self.gety(k1)) / self.deltay(); - let u_y2 = (y2 - self.gety(k2)) / self.deltay(); - - let fi1: ArrayVec<_, 8> = (0..=self.yorder) - .map(|i| fi(i, self.yorder, u_y1)) - .collect(); - let fi2: ArrayVec<_, 8> = (0..=self.yorder) - .map(|i| fi(i, self.yorder, u_y2)) - .collect(); - - let k3 = usize_from_f64( - (tau - self.taumin) / self.deltatau() - f64_from_usize(self.tauorder / 2), - ) - .min(self.ntau - 1 - self.tauorder); + weightfun(ntuple.x2) + } else { + 1.0 + }); - let u_tau = (tau - self.gettau(k3)) / self.deltatau(); + let size = self.tauorder + 1; + let ny1 = self.ny1; + let ny2 = self.ny2; - let factor = if self.reweight { - 1.0 / (weightfun(ntuple.x1) * weightfun(ntuple.x2)) - } else { - 1.0 - }; + if self.grid.is_none() { + self.itaumin = k3; + self.itaumax = k3 + size; + } else if k3 < self.itaumin || k3 + size > self.itaumax { + self.increase_tau(self.itaumin.min(k3), self.itaumax.max(k3 + size)); + } for i3 in 0..=self.tauorder { let fi3i3 = fi(i3, self.tauorder, u_tau); @@ -925,7 +268,11 @@ impl Subgrid for LagrangeSparseSubgridV1 { for (i2, fi2i2) in fi2.iter().enumerate() { let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * ntuple.weight; - self.array[[k3 + i3, k1 + i1, k2 + i2]] += fillweight; + let grid = self + .grid + .get_or_insert_with(|| Array3::zeros((size, ny1, ny2))); + + grid[[k3 + i3 - self.itaumin, k1 + i1, k2 + i2]] += fillweight; } } } @@ -945,35 +292,63 @@ impl Subgrid for LagrangeSparseSubgridV1 { } fn x1_grid(&self) -> Cow<[f64]> { - (0..self.ny).map(|iy| fx(self.gety(iy))).collect() + (0..self.ny1).map(|iy| fx(self.gety1(iy))).collect() } fn x2_grid(&self) -> Cow<[f64]> { - self.x1_grid() + (0..self.ny2).map(|iy| fx(self.gety2(iy))).collect() } fn is_empty(&self) -> bool { - self.array.is_empty() + self.grid.is_none() } fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - if let SubgridEnum::LagrangeSparseSubgridV1(other_grid) = other { - if self.array.is_empty() && !transpose { - mem::swap(&mut self.array, &mut other_grid.array); - } else { - // TODO: the general case isn't implemented - assert!(self.x1_grid() == other_grid.x1_grid()); - assert!(self.x2_grid() == other_grid.x2_grid()); + let x1_equal = self.x1_grid() == other.x1_grid(); + let x2_equal = self.x2_grid() == other.x2_grid(); + + if let SubgridEnum::LagrangeSubgridV2(other_grid) = other { + if let Some(other_grid_grid) = &mut other_grid.grid { + if self.grid.is_some() { + // TODO: the general case isn't implemented + assert!(x1_equal); + assert!(x2_equal); + + let new_itaumin = self.itaumin.min(other_grid.itaumin); + let new_itaumax = self.itaumax.max(other_grid.itaumax); + let offset = other_grid.itaumin.saturating_sub(self.itaumin); + + // TODO: we need much more checks here if there subgrids are compatible at all + + if (self.itaumin != new_itaumin) || (self.itaumax != new_itaumax) { + self.increase_tau(new_itaumin, new_itaumax); + } + + if (other_grid.static_q2 == -1.0) || (self.static_q2 != other_grid.static_q2) { + self.static_q2 = -1.0; + } - // TODO: we need much more checks here if there subgrids are compatible at all + let self_grid = self.grid.as_mut().unwrap(); - if transpose { - for ((i, k, j), value) in other_grid.array.indexed_iter() { - self.array[[i, j, k]] += value; + if transpose { + for ((i, k, j), value) in other_grid_grid.indexed_iter() { + self_grid[[i + offset, j, k]] += value; + } + } else { + for ((i, j, k), value) in other_grid_grid.indexed_iter() { + self_grid[[i + offset, j, k]] += value; + } } } else { - for ((i, j, k), value) in other_grid.array.indexed_iter() { - self.array[[i, j, k]] += value; + self.grid = other_grid.grid.take(); + self.itaumin = other_grid.itaumin; + self.itaumax = other_grid.itaumax; + self.static_q2 = other_grid.static_q2; + + if transpose { + if let Some(grid) = &mut self.grid { + grid.swap_axes(1, 2); + } } } } @@ -984,91 +359,104 @@ impl Subgrid for LagrangeSparseSubgridV1 { fn scale(&mut self, factor: f64) { if factor == 0.0 { - self.array.clear(); - } else { - self.array.iter_mut().for_each(|x| *x *= factor); + self.grid = None; + } else if let Some(self_grid) = &mut self.grid { + self_grid.iter_mut().for_each(|x| *x *= factor); } } fn symmetrize(&mut self) { - let mut new_array = SparseArray3::new(self.ntau, self.ny, self.ny); + if let Some(grid) = self.grid.as_mut() { + let (i_size, j_size, k_size) = grid.dim(); - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k >= j) { - new_array[[i, j, k]] = sigma; - } - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k < j) { - new_array[[i, k, j]] += sigma; + for i in 0..i_size { + for j in 0..j_size { + for k in j + 1..k_size { + grid[[i, j, k]] += grid[[i, k, j]]; + grid[[i, k, j]] = 0.0; + } + } + } } - - mem::swap(&mut self.array, &mut new_array); } fn clone_empty(&self) -> SubgridEnum { Self { - array: SparseArray3::new(self.ntau, self.ny, self.ny), + grid: None, ntau: self.ntau, - ny: self.ny, - yorder: self.yorder, + ny1: self.ny1, + ny2: self.ny2, + y1order: self.y1order, + y2order: self.y2order, tauorder: self.tauorder, - reweight: self.reweight, - ymin: self.ymin, - ymax: self.ymax, + itaumin: 0, + itaumax: 0, + reweight1: self.reweight1, + reweight2: self.reweight2, + y1min: self.y1min, + y1max: self.y1max, + y2min: self.y2min, + y2max: self.y2max, taumin: self.taumin, taumax: self.taumax, + static_q2: 0.0, } .into() } fn indexed_iter(&self) -> SubgridIndexedIter { - Box::new(self.array.indexed_iter().map(|(tuple, value)| { - ( - tuple, - value - * if self.reweight { - weightfun(fx(self.gety(tuple.1))) * weightfun(fx(self.gety(tuple.2))) - } else { - 1.0 + self.grid.as_ref().map_or_else( + || Box::new(iter::empty()) as Box>, + |grid| { + Box::new(grid.indexed_iter().filter(|(_, &value)| value != 0.0).map( + |(tuple, &value)| { + ( + (self.itaumin + tuple.0, tuple.1, tuple.2), + value + * if self.reweight1 { + weightfun(fx(self.gety1(tuple.1))) + } else { + 1.0 + } + * if self.reweight2 { + weightfun(fx(self.gety2(tuple.2))) + } else { + 1.0 + }, + ) }, - ) - })) + )) + }, + ) } fn stats(&self) -> Stats { + let (non_zeros, zeros) = self.grid.as_ref().map_or((0, 0), |array| { + array.iter().fold((0, 0), |mut result, value| { + if *value == 0.0 { + result.0 += 1; + } else { + result.1 += 1; + } + result + }) + }); + Stats { - total: self.ntau * self.ny * self.ny, - allocated: self.array.len() + self.array.zeros(), - zeros: self.array.zeros(), - overhead: self.array.overhead(), + total: non_zeros + zeros, + allocated: non_zeros + zeros, + zeros, + overhead: 0, bytes_per_value: mem::size_of::(), } } fn static_scale(&self) -> Option { - if let [static_scale] = self.mu2_grid().as_ref() { - Some(static_scale.clone()) - } else { - None - } - } -} - -impl From<&LagrangeSubgridV1> for LagrangeSparseSubgridV1 { - fn from(subgrid: &LagrangeSubgridV1) -> Self { - Self { - array: subgrid.grid.as_ref().map_or_else( - || SparseArray3::new(subgrid.ntau, subgrid.ny, subgrid.ny), - |grid| SparseArray3::from_ndarray(grid.view(), subgrid.itaumin, subgrid.ntau), - ), - ntau: subgrid.ntau, - ny: subgrid.ny, - yorder: subgrid.yorder, - tauorder: subgrid.tauorder, - reweight: subgrid.reweight, - ymin: subgrid.ymin, - ymax: subgrid.ymax, - taumin: subgrid.taumin, - taumax: subgrid.taumax, - } + (self.static_q2 > 0.0).then_some(Mu2 { + ren: self.static_q2, + fac: self.static_q2, + frg: -1.0, + }) } } @@ -1258,22 +646,6 @@ mod tests { assert_eq!(result, 0.0); } - #[test] - fn q2_slice_v1() { - let subgrid = test_q2_slice_methods(LagrangeSubgridV1::new(&SubgridParams::default())); - - assert_eq!( - subgrid.stats(), - Stats { - total: 10000, - allocated: 10000, - zeros: 256, - overhead: 0, - bytes_per_value: 8 - } - ); - } - #[test] fn q2_slice_v2() { let subgrid = test_q2_slice_methods(LagrangeSubgridV2::new( @@ -1293,53 +665,6 @@ mod tests { ); } - #[test] - fn sparse_q2_slice() { - let subgrid = - test_q2_slice_methods(LagrangeSparseSubgridV1::new(&SubgridParams::default())); - - assert_eq!( - subgrid.stats(), - Stats { - total: 100000, - allocated: 432, - zeros: 176, - overhead: 402, - bytes_per_value: 8 - } - ); - } - - #[test] - fn fill_zero_v1() { - let mut subgrid = LagrangeSubgridV1::new(&SubgridParams::default()); - - subgrid.fill(&Ntuple { - x1: 0.5, - x2: 0.5, - q2: 1000.0, - weight: 0.0, - }); - - assert!(subgrid.is_empty()); - assert_eq!(subgrid.indexed_iter().count(), 0); - } - - #[test] - fn fill_zero_v1_sparse() { - let mut subgrid = LagrangeSparseSubgridV1::new(&SubgridParams::default()); - - subgrid.fill(&Ntuple { - x1: 0.5, - x2: 0.5, - q2: 1000.0, - weight: 0.0, - }); - - assert!(subgrid.is_empty()); - assert_eq!(subgrid.indexed_iter().count(), 0); - } - #[test] fn fill_zero_v2() { let mut subgrid = @@ -1356,115 +681,6 @@ mod tests { assert_eq!(subgrid.indexed_iter().count(), 0); } - #[test] - fn from() { - // check conversion of empty grids - let mut dense = LagrangeSubgridV1::new(&SubgridParams::default()); - assert!(dense.is_empty()); - let sparse = LagrangeSparseSubgridV1::from(&dense); - assert!(sparse.is_empty()); - - let mu2 = dense.mu2_grid().into_owned(); - let x1 = dense.x1_grid().into_owned(); - let x2 = dense.x2_grid().into_owned(); - - assert_eq!(mu2, *sparse.mu2_grid()); - assert_eq!(x1, *sparse.x1_grid()); - assert_eq!(x2, *sparse.x2_grid()); - - // check conversion of a filled grid - dense.fill(&Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - dense.fill(&Ntuple { - x1: 0.9, - x2: 0.1, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - - assert!(!dense.is_empty()); - - let sparse = LagrangeSparseSubgridV1::from(&dense); - assert!(!sparse.is_empty()); - - let reference = - dense.convolve(&x1, &x2, &mu2, &mut |ix1, ix2, _| 1.0 / (x1[ix1] * x2[ix2])); - let converted = - sparse.convolve(&x1, &x2, &mu2, &mut |ix1, ix2, _| 1.0 / (x1[ix1] * x2[ix2])); - - assert_approx_eq!(f64, reference, converted, ulps = 8); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn merge_dense_v1_with_sparse() { - let mut dense = LagrangeSubgridV1::new(&SubgridParams::default()); - let sparse = LagrangeSparseSubgridV1::new(&SubgridParams::default()); - - dense.merge(&mut sparse.into(), false); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn merge_dense_v1_with_dense_v2() { - let mut one = LagrangeSubgridV1::new(&SubgridParams::default()); - let two = LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); - - one.merge(&mut two.into(), false); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn merge_dense_v2_with_dense_v1() { - let mut two = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); - let one = LagrangeSubgridV1::new(&SubgridParams::default()); - - two.merge(&mut one.into(), false); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn merge_dense_v2_with_sparse() { - let mut dense = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); - let sparse = LagrangeSparseSubgridV1::new(&SubgridParams::default()); - - dense.merge(&mut sparse.into(), false); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn merge_sparse_with_dense_v1() { - let mut sparse = LagrangeSparseSubgridV1::new(&SubgridParams::default()); - let dense = LagrangeSubgridV1::new(&SubgridParams::default()); - - sparse.merge(&mut dense.into(), false); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn merge_sparse_with_dense_v2() { - let mut sparse = LagrangeSparseSubgridV1::new(&SubgridParams::default()); - let dense = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); - - sparse.merge(&mut dense.into(), false); - } - - #[test] - fn merge_dense_v1() { - test_merge_method( - LagrangeSubgridV1::new(&SubgridParams::default()), - LagrangeSubgridV1::new(&SubgridParams::default()), - LagrangeSubgridV1::new(&SubgridParams::default()), - ); - } - #[test] fn merge_dense_v2() { test_merge_method( @@ -1474,20 +690,6 @@ mod tests { ); } - #[test] - fn merge_sparse() { - test_merge_method( - LagrangeSparseSubgridV1::new(&SubgridParams::default()), - LagrangeSparseSubgridV1::new(&SubgridParams::default()), - LagrangeSparseSubgridV1::new(&SubgridParams::default()), - ); - } - - #[test] - fn empty_v1() { - test_empty_subgrid(LagrangeSubgridV1::new(&SubgridParams::default())); - } - #[test] fn empty_v2() { test_empty_subgrid(LagrangeSubgridV2::new( @@ -1495,9 +697,4 @@ mod tests { &ExtraSubgridParams::default(), )); } - - #[test] - fn empty_sparse() { - test_empty_subgrid(LagrangeSparseSubgridV1::new(&SubgridParams::default())); - } } diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index ad1d0cbd..9d64c127 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -44,7 +44,6 @@ pub mod fk_table; pub mod grid; pub mod import_only_subgrid; pub mod lagrange_subgrid; -pub mod ntuple_subgrid; pub mod packed_array; pub mod packed_subgrid; pub mod pids; diff --git a/pineappl/src/ntuple_subgrid.rs b/pineappl/src/ntuple_subgrid.rs deleted file mode 100644 index 282d9fff..00000000 --- a/pineappl/src/ntuple_subgrid.rs +++ /dev/null @@ -1,198 +0,0 @@ -//! Provides an implementation of the `Grid` trait with n-tuples. - -use super::grid::Ntuple; -use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; -use serde::{Deserialize, Serialize}; -use std::borrow::Cow; -use std::mem; - -/// Structure holding a grid with an n-tuple as the storage method for weights. -#[derive(Clone, Default, Deserialize, Serialize)] -pub struct NtupleSubgridV1 { - ntuples: Vec>, -} - -impl NtupleSubgridV1 { - /// Constructor. - #[must_use] - pub const fn new() -> Self { - Self { ntuples: vec![] } - } -} - -impl Subgrid for NtupleSubgridV1 { - fn convolve( - &self, - _: &[f64], - _: &[f64], - _: &[Mu2], - _: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - panic!("NtupleSubgridV1 doesn't support the convolve operation"); - } - - fn fill(&mut self, ntuple: &Ntuple) { - if ntuple.weight == 0.0 { - return; - } - - self.ntuples.push(ntuple.clone()); - } - - fn mu2_grid(&self) -> Cow<[Mu2]> { - Cow::Borrowed(&[]) - } - - fn x1_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&[]) - } - - fn x2_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&[]) - } - - fn is_empty(&self) -> bool { - self.ntuples.is_empty() - } - - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - assert!(!transpose); - - if let SubgridEnum::NtupleSubgridV1(other_grid) = other { - self.ntuples.append(&mut other_grid.ntuples); - } else { - panic!("NtupleSubgridV1 doesn't support the merge operation with subgrid types other than itself"); - } - } - - fn scale(&mut self, factor: f64) { - self.ntuples.iter_mut().for_each(|t| t.weight *= factor); - } - - fn symmetrize(&mut self) {} - - fn clone_empty(&self) -> SubgridEnum { - Self::new().into() - } - - fn indexed_iter(&self) -> SubgridIndexedIter { - panic!("NtupleSubgridV1 doesn't support the indexed_iter operation"); - } - - fn stats(&self) -> Stats { - Stats { - total: self.ntuples.len(), - allocated: self.ntuples.len(), - zeros: 0, - overhead: 0, - bytes_per_value: mem::size_of::>(), - } - } - - fn static_scale(&self) -> Option { - todo!() - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::lagrange_subgrid::LagrangeSubgridV2; - use crate::subgrid::{ExtraSubgridParams, SubgridParams}; - - #[test] - #[should_panic(expected = "NtupleSubgridV1 doesn't support the convolve operation")] - fn convolve() { - NtupleSubgridV1::new().convolve(&[], &[], &[], &mut |_, _, _| 0.0); - } - - #[test] - fn fill_zero() { - let mut subgrid = NtupleSubgridV1::new(); - - subgrid.fill(&Ntuple { - x1: 0.5, - x2: 0.5, - q2: 1000.0, - weight: 0.0, - }); - - assert!(subgrid.is_empty()); - } - - #[test] - #[should_panic(expected = "NtupleSubgridV1 doesn't support the indexed_iter operation")] - fn indexed_iter() { - // `next` isn't called because `indexed_iter` panics, but it suppresses a warning about an - // unused result - NtupleSubgridV1::new().indexed_iter().next(); - } - - #[test] - fn stats() { - let subgrid = NtupleSubgridV1::new(); - assert_eq!( - subgrid.stats(), - Stats { - total: 0, - allocated: 0, - zeros: 0, - overhead: 0, - bytes_per_value: 32, - } - ); - } - - #[test] - #[should_panic(expected = "not yet implemented")] - fn static_scale() { - let subgrid = NtupleSubgridV1::new(); - subgrid.static_scale(); - } - - #[test] - #[should_panic( - expected = "NtupleSubgridV1 doesn't support the merge operation with subgrid types other than itself" - )] - fn merge_with_lagrange_subgrid() { - let mut subgrid = NtupleSubgridV1::new(); - let mut other = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()) - .into(); - subgrid.merge(&mut other, false); - } - - #[test] - fn test() { - let mut subgrid1: SubgridEnum = NtupleSubgridV1::new().into(); - - assert!(subgrid1.is_empty()); - - subgrid1.fill(&Ntuple { - x1: 0.0, - x2: 0.0, - q2: 0.0, - weight: 1.0, - }); - - assert!(!subgrid1.is_empty()); - - assert_eq!(subgrid1.mu2_grid().as_ref(), []); - assert_eq!(subgrid1.x1_grid().as_ref(), []); - assert_eq!(subgrid1.x2_grid().as_ref(), []); - - subgrid1.symmetrize(); - subgrid1.scale(2.0); - - let mut subgrid2: SubgridEnum = subgrid1.clone_empty(); - - subgrid2.fill(&Ntuple { - x1: 0.0, - x2: 0.0, - q2: 0.0, - weight: 1.0, - }); - - subgrid2.merge(&mut subgrid1, false); - } -} diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 81291938..8479846b 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -2,9 +2,8 @@ use super::empty_subgrid::EmptySubgridV1; use super::grid::Ntuple; -use super::import_only_subgrid::{ImportOnlySubgridV1, ImportOnlySubgridV2}; -use super::lagrange_subgrid::{LagrangeSparseSubgridV1, LagrangeSubgridV1, LagrangeSubgridV2}; -use super::ntuple_subgrid::NtupleSubgridV1; +use super::import_only_subgrid::ImportOnlySubgridV2; +use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; use ndarray::Array3; @@ -16,16 +15,8 @@ use std::borrow::Cow; #[derive(Clone, Deserialize, Serialize)] pub enum SubgridEnum { // WARNING: never change the order or content of this enum, only add to the end of it - /// Lagrange-interpolation subgrid. - LagrangeSubgridV1, - /// N-tuple subgrid. - NtupleSubgridV1, - /// Lagrange-interpolation subgrid. - LagrangeSparseSubgridV1, /// Lagrange-interpolation subgrid with possibly different x1 and x2 bins. LagrangeSubgridV2, - /// Import-only sparse subgrid with possibly different x1 and x2 bins. - ImportOnlySubgridV1, /// Empty subgrid. EmptySubgridV1, /// Same as [`ImportOnlySubgridV1`], but with support for different renormalization and diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 019b61e8..c7f677f5 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -609,25 +609,6 @@ fn drell_yan_lagrange_static() -> Result<()> { ) } -#[test] -fn drell_yan_lagrange_v1_static() -> Result<()> { - perform_grid_tests( - "LagrangeSubgridV1", - false, - &STATIC_REFERENCE, - &STATIC_REFERENCE, // LagrangeSubgridV1 doesn't have static-scale detection - &[ - 0.030521584007828916, - 0.02108918668378717, - 0.014375068581090129, - 0.009699159574043398, - 0.006496206194633799, - 0.004328500638820811, - ], - true, - ) -} - #[test] fn drell_yan_lagrange_v2_static() -> Result<()> { perform_grid_tests( @@ -666,44 +647,6 @@ fn drell_yan_lagrange_dynamic() -> Result<()> { ) } -#[test] -fn drell_yan_lagrange_v1_dynamic() -> Result<()> { - perform_grid_tests( - "LagrangeSubgridV1", - true, - &DYNAMIC_REFERENCE, - &DYNAMIC_REFERENCE, - &[ - 0.030521584007828916, - 0.02108918668378717, - 0.014375068581090129, - 0.009699159574043398, - 0.006496206194633799, - 0.004328500638820811, - ], - true, - ) -} - -#[test] -fn drell_yan_lagrange_v1_dynamic_no_reweight() -> Result<()> { - perform_grid_tests( - "LagrangeSubgridV1", - true, - &DYNAMIC_REFERENCE_NO_REWEIGHT, - &DYNAMIC_REFERENCE_NO_REWEIGHT, - &[ - 0.030521584007828916, - 0.02108918668378717, - 0.014375068581090129, - 0.009699159574043398, - 0.006496206194633799, - 0.004328500638820811, - ], - false, - ) -} - #[test] fn drell_yan_lagrange_v2_dynamic() -> Result<()> { perform_grid_tests( @@ -742,25 +685,6 @@ fn drell_yan_lagrange_v2_dynamic_no_reweight() -> Result<()> { ) } -#[test] -fn drell_yan_lagrange_sparse_dynamic() -> Result<()> { - perform_grid_tests( - "LagrangeSparseSubgrid", - true, - &DYNAMIC_REFERENCE, - &DYNAMIC_REFERENCE, - &[ - 0.030521584007828916, - 0.02108918668378717, - 0.014375068581090129, - 0.009699159574043398, - 0.006496206194633799, - 0.004328500638820811, - ], - true, - ) -} - #[test] fn grid_optimize() -> Result<()> { let mut grid = generate_grid("LagrangeSubgridV2", false, false)?; diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index e86d7e63..43700c9c 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -102,11 +102,7 @@ impl Subcommand for Opts { if self.group.type_ { row.add_cell(cell!(l-> match subgrid { - SubgridEnum::LagrangeSubgridV1(_) => "LagrangeSubgridV1", - SubgridEnum::NtupleSubgridV1(_) => "NtupleSubgridV1", - SubgridEnum::LagrangeSparseSubgridV1(_) => "LagrangeSparseSubgridV1", SubgridEnum::LagrangeSubgridV2(_) => "LagrangeSubgridV2", - SubgridEnum::ImportOnlySubgridV1(_) => "ImportOnlySubgridV1", SubgridEnum::ImportOnlySubgridV2(_) => "ImportOnlySubgridV2", SubgridEnum::EmptySubgridV1(_) => "EmptySubgridV1", SubgridEnum::PackedQ1X2SubgridV1(_) => "PackedQ1X2SubgridV1", From cbe586cec60b98677bd1307fb78ce8075860296d Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 8 Aug 2024 11:17:29 +0200 Subject: [PATCH 033/213] Do not run python CI in this branch --- .github/workflows/python.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index a28f8b3a..98cf11d2 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -4,6 +4,7 @@ on: push: branches-ignore: - pycli + - v1-file-format jobs: test: From 2ba8cc220dd2de609abe265a7a28b435b72afaa0 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 8 Aug 2024 12:01:37 +0200 Subject: [PATCH 034/213] Remove leftover `ImportOnlySubgridV1` --- pineappl_cli/src/import/fktable.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 8020f7cc..61fef0fc 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -5,10 +5,10 @@ use pineappl::boc::Order; use pineappl::channel; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; -use pineappl::import_only_subgrid::ImportOnlySubgridV1; +use pineappl::import_only_subgrid::ImportOnlySubgridV2; use pineappl::pids::PidBasis; use pineappl::sparse_array3::SparseArray3; -use pineappl::subgrid::SubgridParams; +use pineappl::subgrid::{Mu2, SubgridParams}; use std::fs::File; use std::io::{BufRead, BufReader}; use std::iter; @@ -181,9 +181,14 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .iter_mut() .zip(arrays.into_iter()) { - *subgrid = ImportOnlySubgridV1::new( + *subgrid = ImportOnlySubgridV2::new( array, - vec![q0 * q0], + // TODO: remove the renormalization scale + vec![Mu2 { + ren: q0 * q0, + fac: q0 * q0, + frg: -1.0, + }], x_grid.clone(), if hadronic { x_grid.clone() } else { vec![1.0] }, ) @@ -237,9 +242,14 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .iter_mut() .zip(arrays.into_iter()) { - *subgrid = ImportOnlySubgridV1::new( + *subgrid = ImportOnlySubgridV2::new( array, - vec![q0 * q0], + // TODO: remove the renormalization scale + vec![Mu2 { + ren: q0 * q0, + fac: q0 * q0, + frg: -1.0, + }], x_grid.clone(), if hadronic { x_grid.clone() } else { vec![1.0] }, ) From 63ab6ea7e90b43ecd11353eb31556a65bfef5d18 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 8 Aug 2024 12:35:18 +0200 Subject: [PATCH 035/213] Remove `ImportOnlySubgridV2` --- pineappl/src/evolution.rs | 16 +- pineappl/src/grid.rs | 3 +- pineappl/src/import_only_subgrid.rs | 509 ---------------------------- pineappl/src/lib.rs | 1 - pineappl/src/subgrid.rs | 4 - pineappl/tests/drell_yan_lo.rs | 4 +- pineappl_cli/src/import/applgrid.rs | 8 +- pineappl_cli/src/import/fastnlo.rs | 12 +- pineappl_cli/src/import/fktable.rs | 12 +- pineappl_cli/src/subgrids.rs | 1 - 10 files changed, 27 insertions(+), 543 deletions(-) delete mode 100644 pineappl/src/import_only_subgrid.rs diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 94fa299b..d5162658 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -4,9 +4,9 @@ use super::boc::{Channel, Order}; use super::channel; use super::convolutions::Convolution; use super::grid::{Grid, GridError}; -use super::import_only_subgrid::ImportOnlySubgridV2; +use super::packed_array::PackedArray; +use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::PidBasis; -use super::sparse_array3::SparseArray3; use super::subgrid::{Mu2, Subgrid, SubgridEnum}; use float_cmp::approx_eq; use itertools::izip; @@ -473,8 +473,8 @@ pub(crate) fn evolve_slice_with_one( } sub_fk_tables.extend(tables.into_iter().map(|table| { - ImportOnlySubgridV2::new( - SparseArray3::from_ndarray( + PackedQ1X2SubgridV1::new( + PackedArray::from_ndarray( table .insert_axis(Axis(0)) .insert_axis(Axis(new_axis)) @@ -625,8 +625,8 @@ pub(crate) fn evolve_slice_with_two( } sub_fk_tables.extend(tables.into_iter().map(|table| { - ImportOnlySubgridV2::new( - SparseArray3::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), + PackedQ1X2SubgridV1::new( + PackedArray::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), vec![Mu2 { // TODO: FK tables don't depend on the renormalization scale //ren: -1.0, @@ -775,8 +775,8 @@ pub(crate) fn evolve_slice_with_two2( } sub_fk_tables.extend(tables.into_iter().map(|table| { - ImportOnlySubgridV2::new( - SparseArray3::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), + PackedQ1X2SubgridV1::new( + PackedArray::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), vec![Mu2 { // TODO: FK tables don't depend on the renormalization scale //ren: -1.0, diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index dbe7e903..b5e54567 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -6,7 +6,6 @@ use super::convolutions::{Convolution, LumiCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorInfo, OperatorSliceInfo}; use super::fk_table::FkTable; -use super::import_only_subgrid::ImportOnlySubgridV2; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; @@ -1070,7 +1069,7 @@ impl Grid { } } - let mut new_subgrid = ImportOnlySubgridV2::from(&*subgrid).into(); + let mut new_subgrid = PackedQ1X2SubgridV1::from(&*subgrid).into(); mem::swap(subgrid, &mut new_subgrid); } } diff --git a/pineappl/src/import_only_subgrid.rs b/pineappl/src/import_only_subgrid.rs deleted file mode 100644 index 4ff5d622..00000000 --- a/pineappl/src/import_only_subgrid.rs +++ /dev/null @@ -1,509 +0,0 @@ -//! TODO - -use super::grid::Ntuple; -use super::sparse_array3::SparseArray3; -use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; -use serde::{Deserialize, Serialize}; -use std::borrow::Cow; -use std::mem; - -/// TODO -#[derive(Clone, Deserialize, Serialize)] -pub struct ImportOnlySubgridV2 { - array: SparseArray3, - mu2_grid: Vec, - x1_grid: Vec, - x2_grid: Vec, -} - -impl ImportOnlySubgridV2 { - /// Constructor. - #[must_use] - pub fn new( - array: SparseArray3, - mu2_grid: Vec, - x1_grid: Vec, - x2_grid: Vec, - ) -> Self { - Self { - array, - mu2_grid, - x1_grid, - x2_grid, - } - } - - /// Return the array containing the numerical values of the grid. - pub fn array_mut(&mut self) -> &mut SparseArray3 { - &mut self.array - } -} - -impl Subgrid for ImportOnlySubgridV2 { - fn convolve( - &self, - _: &[f64], - _: &[f64], - _: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - self.array - .indexed_iter() - .map(|((imu2, ix1, ix2), sigma)| sigma * lumi(ix1, ix2, imu2)) - .sum() - } - - fn fill(&mut self, _: &Ntuple) { - panic!("ImportOnlySubgridV2 doesn't support the fill operation"); - } - - fn mu2_grid(&self) -> Cow<[Mu2]> { - Cow::Borrowed(&self.mu2_grid) - } - - fn x1_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&self.x1_grid) - } - - fn x2_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&self.x2_grid) - } - - fn is_empty(&self) -> bool { - self.array.is_empty() - } - - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - if let SubgridEnum::ImportOnlySubgridV2(other_grid) = other { - if self.array.is_empty() && !transpose { - mem::swap(&mut self.array, &mut other_grid.array); - } else { - let rhs_x1 = if transpose { - other_grid.x2_grid() - } else { - other_grid.x1_grid() - }; - let rhs_x2 = if transpose { - other_grid.x1_grid() - } else { - other_grid.x2_grid() - }; - - if (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { - let mut x1_grid = self.x1_grid.clone(); - let mut x2_grid = self.x2_grid.clone(); - - x1_grid.extend_from_slice(&rhs_x1); - x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x1_grid.dedup(); - x2_grid.extend_from_slice(&rhs_x2); - x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x2_grid.dedup(); - - let mut array = - SparseArray3::new(self.array.dimensions().0, x1_grid.len(), x2_grid.len()); - - for ((i, j, k), value) in self.array.indexed_iter() { - let target_j = x1_grid - .iter() - .position(|&x| x == self.x1_grid[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = x2_grid - .iter() - .position(|&x| x == self.x2_grid[k]) - .unwrap_or_else(|| unreachable!()); - - array[[i, target_j, target_k]] = value; - } - - self.array = array; - self.x1_grid = x1_grid; - self.x2_grid = x2_grid; - } - - for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() { - let index = match self - .mu2_grid - .binary_search_by(|val| val.partial_cmp(mu2).unwrap()) - { - Ok(index) => index, - Err(index) => { - self.mu2_grid.insert(index, mu2.clone()); - self.array.increase_x_at(index); - index - } - }; - - for ((_, j, k), value) in other_grid - .array - .indexed_iter() - .filter(|&((i, _, _), _)| i == other_index) - { - let (j, k) = if transpose { (k, j) } else { (j, k) }; - let target_j = self - .x1_grid - .iter() - .position(|&x| x == rhs_x1[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = self - .x2_grid - .iter() - .position(|&x| x == rhs_x2[k]) - .unwrap_or_else(|| unreachable!()); - - self.array[[index, target_j, target_k]] += value; - } - } - } - } else { - todo!(); - } - } - - fn scale(&mut self, factor: f64) { - if factor == 0.0 { - self.array.clear(); - } else { - self.array.iter_mut().for_each(|x| *x *= factor); - } - } - - fn symmetrize(&mut self) { - let mut new_array = - SparseArray3::new(self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()); - - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k >= j) { - new_array[[i, j, k]] = sigma; - } - // do not change the diagonal entries (k==j) - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k < j) { - new_array[[i, k, j]] += sigma; - } - - mem::swap(&mut self.array, &mut new_array); - } - - fn clone_empty(&self) -> SubgridEnum { - Self { - array: SparseArray3::new(self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()), - mu2_grid: self.mu2_grid.clone(), - x1_grid: self.x1_grid.clone(), - x2_grid: self.x2_grid.clone(), - } - .into() - } - - fn indexed_iter(&self) -> SubgridIndexedIter { - Box::new(self.array.indexed_iter()) - } - - fn stats(&self) -> Stats { - Stats { - total: self.mu2_grid.len() * self.x1_grid.len() * self.x2_grid.len(), - allocated: self.array.len() + self.array.zeros(), - zeros: self.array.zeros(), - overhead: self.array.overhead(), - bytes_per_value: mem::size_of::(), - } - } - - fn static_scale(&self) -> Option { - if let [static_scale] = self.mu2_grid.as_slice() { - Some(static_scale.clone()) - } else { - None - } - } -} - -impl From<&SubgridEnum> for ImportOnlySubgridV2 { - fn from(subgrid: &SubgridEnum) -> Self { - // find smallest ranges - let (mu2_range, x1_range, x2_range) = subgrid.indexed_iter().fold( - ( - subgrid.mu2_grid().len()..0, - subgrid.x1_grid().len()..0, - subgrid.x2_grid().len()..0, - ), - |prev, ((imu2, ix1, ix2), _)| { - ( - prev.0.start.min(imu2)..prev.0.end.max(imu2 + 1), - prev.1.start.min(ix1)..prev.1.end.max(ix1 + 1), - prev.2.start.min(ix2)..prev.2.end.max(ix2 + 1), - ) - }, - ); - - let (mu2_grid, static_scale) = subgrid.static_scale().map_or_else( - || (subgrid.mu2_grid()[mu2_range.clone()].to_vec(), false), - |scale| (vec![scale], true), - ); - let x1_grid = subgrid.x1_grid()[x1_range.clone()].to_vec(); - let x2_grid = subgrid.x2_grid()[x2_range.clone()].to_vec(); - - let mut array = SparseArray3::new(mu2_grid.len(), x1_grid.len(), x2_grid.len()); - - for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { - // if there's a static scale we want every value to be added to same grid point - let index = if static_scale { - 0 - } else { - imu2 - mu2_range.start - }; - - array[[index, ix1 - x1_range.start, ix2 - x2_range.start]] += value; - } - - Self { - array, - mu2_grid, - x1_grid, - x2_grid, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::lagrange_subgrid::LagrangeSubgridV2; - use crate::subgrid::{ExtraSubgridParams, SubgridParams}; - use float_cmp::assert_approx_eq; - use rand::distributions::{Distribution, Uniform}; - use rand::Rng; - use rand_pcg::Pcg64; - - #[test] - fn test_v2() { - let x = vec![ - 0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0, - ]; - let mut grid1: SubgridEnum = ImportOnlySubgridV2::new( - SparseArray3::new(1, 10, 10), - vec![Mu2 { - ren: 0.0, - fac: 0.0, - frg: -1.0, - }], - x.clone(), - x.clone(), - ) - .into(); - - let mu2 = vec![Mu2 { - ren: 0.0, - fac: 0.0, - frg: -1.0, - }]; - - assert_eq!(grid1.mu2_grid().as_ref(), mu2); - assert_eq!(grid1.x1_grid().as_ref(), x); - assert_eq!(grid1.x2_grid(), grid1.x1_grid()); - - assert!(grid1.is_empty()); - - // only use exactly representable numbers here so that we can avoid using approx_eq - if let SubgridEnum::ImportOnlySubgridV2(ref mut x) = grid1 { - x.array_mut()[[0, 1, 2]] = 1.0; - x.array_mut()[[0, 1, 3]] = 2.0; - x.array_mut()[[0, 4, 3]] = 4.0; - x.array_mut()[[0, 7, 1]] = 8.0; - } else { - unreachable!(); - } - - assert!(!grid1.is_empty()); - - assert_eq!(grid1.indexed_iter().next(), Some(((0, 1, 2), 1.0))); - assert_eq!(grid1.indexed_iter().nth(1), Some(((0, 1, 3), 2.0))); - assert_eq!(grid1.indexed_iter().nth(2), Some(((0, 4, 3), 4.0))); - assert_eq!(grid1.indexed_iter().nth(3), Some(((0, 7, 1), 8.0))); - - // symmetric luminosity function - let lumi = - &mut (|ix1, ix2, _| x[ix1] * x[ix2]) as &mut dyn FnMut(usize, usize, usize) -> f64; - - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 0.228515625); - - // create grid with transposed entries, but different q2 - let mut grid2: SubgridEnum = ImportOnlySubgridV2::new( - SparseArray3::new(1, 10, 10), - vec![Mu2 { - ren: 1.0, - fac: 1.0, - frg: -1.0, - }], - x.clone(), - x.clone(), - ) - .into(); - if let SubgridEnum::ImportOnlySubgridV2(ref mut x) = grid2 { - x.array_mut()[[0, 2, 1]] = 1.0; - x.array_mut()[[0, 3, 1]] = 2.0; - x.array_mut()[[0, 3, 4]] = 4.0; - x.array_mut()[[0, 1, 7]] = 8.0; - } else { - unreachable!(); - } - assert_eq!(grid2.convolve(&x, &x, &mu2, lumi), 0.228515625); - - assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); - assert_eq!(grid2.indexed_iter().nth(1), Some(((0, 2, 1), 1.0))); - assert_eq!(grid2.indexed_iter().nth(2), Some(((0, 3, 1), 2.0))); - assert_eq!(grid2.indexed_iter().nth(3), Some(((0, 3, 4), 4.0))); - - grid1.merge(&mut grid2, false); - - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); - - let mut grid1 = { - let mut g = grid1.clone_empty(); - g.merge(&mut grid1, false); - g - }; - - // the luminosity function is symmetric, so after symmetrization the result must be - // unchanged - grid1.symmetrize(); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); - - grid1.scale(2.0); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 4.0 * 0.228515625); - - assert_eq!( - grid1.stats(), - Stats { - total: 200, - allocated: 14, - zeros: 6, - overhead: 42, - bytes_per_value: 8, - } - ); - } - - #[test] - #[should_panic(expected = "ImportOnlySubgridV2 doesn't support the fill operation")] - fn fill_panic_v2() { - let mut grid = ImportOnlySubgridV2::new( - SparseArray3::new(1, 1, 1), - vec![Mu2 { - ren: 1.0, - fac: 1.0, - frg: -1.0, - }], - vec![1.0], - vec![1.0], - ); - - grid.fill(&Ntuple { - x1: 0.0, - x2: 0.0, - q2: 0.0, - weight: 1.0, - }); - } - - #[test] - fn from_lagrange_subgrid_v2() { - let mut lagrange = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); - - // by default this should have 40 grid points - assert_eq!(lagrange.mu2_grid().len(), 40); - - // only `q2` are important: they're not static and fall between two grid points - lagrange.fill(&Ntuple { - x1: 0.25, - x2: 0.5, - q2: 10000.0, - weight: 1.0, - }); - lagrange.fill(&Ntuple { - x1: 0.0625, - x2: 0.125, - q2: 10001.0, - weight: 1.0, - }); - lagrange.fill(&Ntuple { - x1: 0.5, - x2: 0.0625, - q2: 10002.0, - weight: 1.0, - }); - lagrange.fill(&Ntuple { - x1: 0.1, - x2: 0.2, - q2: 10003.0, - weight: 1.0, - }); - - let x1 = lagrange.x1_grid().to_vec(); - let x2 = lagrange.x2_grid().to_vec(); - let mu2 = lagrange.mu2_grid().to_vec(); - - let lumi = &mut (|_, _, _| 1.0) as &mut dyn FnMut(usize, usize, usize) -> f64; - let reference = lagrange.convolve(&x1, &x2, &mu2, lumi); - - let imported = ImportOnlySubgridV2::from(&lagrange.into()); - let test = imported.convolve(&x1, &x2, &mu2, lumi); - - // make sure the conversion did not change the results - assert_approx_eq!(f64, reference, test, ulps = 8); - - // all unneccessary grid points should be gone; since we are inserting between two - // interpolation grid points, the imported grid should have as many interpolation grid - // points as its interpolation order - assert_eq!(imported.mu2_grid().len(), 4); - } - - #[test] - fn merge_with_different_x_grids() { - let mut params = SubgridParams::default(); - let mut grid1 = LagrangeSubgridV2::new(¶ms, &ExtraSubgridParams::default()); - - // change parameters of the second grid to force non-trivial merging - params.set_x_min(0.2); - params.set_x_max(0.5); - - let mut grid2 = LagrangeSubgridV2::new(¶ms, &ExtraSubgridParams::default()); - let mut rng = Pcg64::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7ac28fa16a64abf96); - let q2_range = Uniform::new(1e4, 1e8); - - for _ in 0..1000 { - grid1.fill(&Ntuple { - x1: rng.gen(), - x2: rng.gen(), - q2: q2_range.sample(&mut rng), - weight: 1.0, - }); - grid2.fill(&Ntuple { - x1: rng.gen(), - x2: rng.gen(), - q2: q2_range.sample(&mut rng), - weight: 1.0, - }); - } - - let lumi = &mut (|_, _, _| 1.0) as &mut dyn FnMut(usize, usize, usize) -> f64; - let result1 = grid1.convolve(&grid1.x1_grid(), &grid1.x2_grid(), &grid1.mu2_grid(), lumi); - let result2 = grid2.convolve(&grid2.x1_grid(), &grid2.x2_grid(), &grid2.mu2_grid(), lumi); - - let mut grid1: SubgridEnum = ImportOnlySubgridV2::from(&grid1.into()).into(); - let mut grid2: SubgridEnum = ImportOnlySubgridV2::from(&grid2.into()).into(); - - let result3 = grid1.convolve(&grid1.x1_grid(), &grid1.x2_grid(), &grid1.mu2_grid(), lumi); - let result4 = grid2.convolve(&grid2.x1_grid(), &grid2.x2_grid(), &grid2.mu2_grid(), lumi); - - // conversion from LangrangeSubgridV2 to ImportOnlySubgridV2 shouldn't change the results - assert!((result3 / result1 - 1.0).abs() < 1e-13); - assert!((result4 / result2 - 1.0).abs() < 1e-13); - - grid1.merge(&mut grid2, false); - - let result5 = grid1.convolve(&grid1.x1_grid(), &grid1.x2_grid(), &grid1.mu2_grid(), lumi); - - // merging the two grids should give the sum of the two results - assert!((result5 / (result3 + result4) - 1.0).abs() < 1e-12); - } -} diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index 9d64c127..67b0684f 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -42,7 +42,6 @@ pub mod empty_subgrid; pub mod evolution; pub mod fk_table; pub mod grid; -pub mod import_only_subgrid; pub mod lagrange_subgrid; pub mod packed_array; pub mod packed_subgrid; diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 8479846b..45e8b8ef 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -2,7 +2,6 @@ use super::empty_subgrid::EmptySubgridV1; use super::grid::Ntuple; -use super::import_only_subgrid::ImportOnlySubgridV2; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; @@ -19,9 +18,6 @@ pub enum SubgridEnum { LagrangeSubgridV2, /// Empty subgrid. EmptySubgridV1, - /// Same as [`ImportOnlySubgridV1`], but with support for different renormalization and - /// factorization scales choices. - ImportOnlySubgridV2, /// PackedQ1X2SubgridV1, } diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index c7f677f5..f62a6988 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -705,7 +705,7 @@ fn grid_optimize() -> Result<()> { // `OPTIMIZE_SUBGRID_TYPE` changes the subgrid type ... assert!(matches!( grid2.subgrids()[[0, 0, 0]], - SubgridEnum::ImportOnlySubgridV2 { .. } + SubgridEnum::PackedQ1X2SubgridV1 { .. } )); // and the dimensions of the subgrid assert_eq!(grid2.subgrids()[[0, 0, 0]].x1_grid().len(), 6); @@ -716,7 +716,7 @@ fn grid_optimize() -> Result<()> { assert!(matches!( grid.subgrids()[[0, 0, 0]], - SubgridEnum::ImportOnlySubgridV2 { .. } + SubgridEnum::PackedQ1X2SubgridV1 { .. } )); // if `STATIC_SCALE_DETECTION` is present the `mu2_grid` dimension are better optimized assert_eq!(grid.subgrids()[[0, 0, 0]].x1_grid().len(), 6); diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index fb8109e5..ee03ad04 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -3,13 +3,13 @@ use lhapdf::Pdf; use pineappl::boc::{Channel, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; -use pineappl::import_only_subgrid::ImportOnlySubgridV2; -use pineappl::sparse_array3::SparseArray3; +use pineappl::packed_array::PackedArray; use pineappl::subgrid::{Mu2, SubgridParams}; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::pin::Pin; use std::ptr; +use pineappl::packed_subgrid::PackedQ1X2SubgridV1; fn convert_to_pdg_id(pid: usize) -> i32 { let pid = i32::try_from(pid).unwrap() - 6; @@ -185,7 +185,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let matrix = unsafe { &*matrix }; let mut array = - SparseArray3::new(mu2_values.len(), x1_values.len(), x2_values.len()); + PackedArray::new([mu2_values.len(), x1_values.len(), x2_values.len()]); for itau in 0..mu2_values.len() { for ix1 in 0..x1_values.len() { @@ -206,7 +206,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul if !array.is_empty() { pgrid.subgrids_mut()[[0, bin.try_into().unwrap(), lumi]] = - ImportOnlySubgridV2::new( + PackedQ1X2SubgridV1::new( array, mu2_values.clone(), x1_values.clone(), diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 74dfbc5e..7c8ce1cb 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -5,8 +5,8 @@ use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; -use pineappl::import_only_subgrid::ImportOnlySubgridV2; -use pineappl::sparse_array3::SparseArray3; +use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::packed_array::PackedArray; use pineappl::subgrid::{Mu2, SubgridParams}; use pineappl_fastnlo::ffi::{ self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF, @@ -158,7 +158,7 @@ fn convert_coeff_add_fix( .collect(); let mut array = - SparseArray3::new(mu2_values.len(), x1_values.len(), x2_values.len()); + PackedArray::new([mu2_values.len(), x1_values.len(), x2_values.len()]); // TODO: figure out what the general case is supposed to be assert_eq!(j, 0); @@ -204,7 +204,7 @@ fn convert_coeff_add_fix( if !array.is_empty() { grid.subgrids_mut() [[0, obs.try_into().unwrap(), subproc.try_into().unwrap()]] = - ImportOnlySubgridV2::new( + PackedQ1X2SubgridV1::new( array, mu2_values, x1_values.clone(), @@ -299,7 +299,7 @@ fn convert_coeff_add_flex( let factor = rescale / table_as_add_base.GetNevt(obs.try_into().unwrap(), subproc); let mut arrays = vec![ - SparseArray3::new(mu2_values.len(), x1_values.len(), x2_values.len()); + PackedArray::new([mu2_values.len(), x1_values.len(), x2_values.len()]); orders_len ]; @@ -367,7 +367,7 @@ fn convert_coeff_add_flex( continue; } - *subgrid = ImportOnlySubgridV2::new( + *subgrid = PackedQ1X2SubgridV1::new( array, mu2_values.clone(), x1_values.clone(), diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 61fef0fc..146c8d8c 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -5,9 +5,9 @@ use pineappl::boc::Order; use pineappl::channel; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; -use pineappl::import_only_subgrid::ImportOnlySubgridV2; +use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::pids::PidBasis; -use pineappl::sparse_array3::SparseArray3; +use pineappl::packed_array::PackedArray; use pineappl::subgrid::{Mu2, SubgridParams}; use std::fs::File; use std::io::{BufRead, BufReader}; @@ -119,7 +119,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { grid = Some(fktable); - arrays = iter::repeat(SparseArray3::new(1, nx1, nx2)) + arrays = iter::repeat(PackedArray::new([1, nx1, nx2])) .take(flavor_mask.iter().filter(|&&value| value).count()) .collect(); } @@ -181,7 +181,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .iter_mut() .zip(arrays.into_iter()) { - *subgrid = ImportOnlySubgridV2::new( + *subgrid = PackedQ1X2SubgridV1::new( array, // TODO: remove the renormalization scale vec![Mu2 { @@ -195,7 +195,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .into(); } - arrays = iter::repeat(SparseArray3::new(1, nx1, nx2)) + arrays = iter::repeat(PackedArray::new([1, nx1, nx2])) .take(flavor_mask.iter().filter(|&&value| value).count()) .collect(); last_bin = bin; @@ -242,7 +242,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .iter_mut() .zip(arrays.into_iter()) { - *subgrid = ImportOnlySubgridV2::new( + *subgrid = PackedQ1X2SubgridV1::new( array, // TODO: remove the renormalization scale vec![Mu2 { diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index 43700c9c..64753a37 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -103,7 +103,6 @@ impl Subcommand for Opts { row.add_cell(cell!(l-> match subgrid { SubgridEnum::LagrangeSubgridV2(_) => "LagrangeSubgridV2", - SubgridEnum::ImportOnlySubgridV2(_) => "ImportOnlySubgridV2", SubgridEnum::EmptySubgridV1(_) => "EmptySubgridV1", SubgridEnum::PackedQ1X2SubgridV1(_) => "PackedQ1X2SubgridV1", } From 53f33ce957a8e4af455a274da347b0277ecb4eab Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 31 Jul 2024 15:12:37 +0200 Subject: [PATCH 036/213] Fix `build.rs` already triggering rebuild --- pineappl_applgrid/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl_applgrid/build.rs b/pineappl_applgrid/build.rs index 7719d3e7..34b24d74 100644 --- a/pineappl_applgrid/build.rs +++ b/pineappl_applgrid/build.rs @@ -154,6 +154,6 @@ fn main() { println!("cargo:rerun-if-changed=src/lib.rs"); println!("cargo:rerun-if-changed=src/applgrid.cpp"); println!("cargo:rerun-if-changed=src/applgrid.hpp"); - println!("cargo:rerun-if-changed=src/calculation.hpp"); + println!("cargo:rerun-if-changed=src/helpers.hpp"); println!("cargo:rerun-if-changed=src/check_appl_igrid.cpp"); } From ca8d029faf263038dfc4387bc4788743bb828d1c Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 8 Aug 2024 13:19:36 +0200 Subject: [PATCH 037/213] Fix numerical noise --- pineappl_cli/tests/import.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index b6145a85..50447a7c 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -52,8 +52,8 @@ Options: #[cfg(feature = "fastnlo")] const IMPORT_FIX_GRID_STR: &str = "b PineAPPL fastNLO rel. diff -+------------+------------+-------------- -0 2.9158424e-4 2.9158424e-4 -2.9976022e-15 -1 2.4657895e-4 2.4657895e-4 -2.8865799e-15 +0 2.9158424e-4 2.9158424e-4 -2.7755576e-15 +1 2.4657895e-4 2.4657895e-4 -2.6645353e-15 "; #[cfg(feature = "fastnlo")] @@ -236,9 +236,9 @@ const IMPORT_HADRONIC_FKTABLE_STR: &str = "b x1 diff "; #[cfg(feature = "applgrid")] -const IMPORT_PHOTON_GRID_STR: &str = "b PineAPPL APPLgrid rel. diff --+------------+------------+----------- -0 5.5621307e-4 5.5621307e-4 0.0000000e0 +const IMPORT_PHOTON_GRID_STR: &str = "b PineAPPL APPLgrid rel. diff +-+------------+------------+-------------- +0 5.5621307e-4 5.5621307e-4 -1.5543122e-15 "; #[cfg(feature = "applgrid")] From d1be6409b75e969d646489ad3944057a48c8539d Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 8 Aug 2024 16:02:23 +0200 Subject: [PATCH 038/213] Add convolutions to `Grid` constructor --- pineappl/src/grid.rs | 13 ++++++- pineappl_capi/src/lib.rs | 43 +++++++++++++++------- pineappl_cli/src/import/applgrid.rs | 17 +++++---- pineappl_cli/src/import/fastnlo.rs | 57 ++++++++++++++++------------- pineappl_cli/src/import/fktable.rs | 20 +++++----- 5 files changed, 90 insertions(+), 60 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index b5e54567..34602d8b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -176,6 +176,7 @@ impl Grid { channels: Vec, orders: Vec, bin_limits: Vec, + convolutions: Vec, subgrid_params: SubgridParams, ) -> Self { // TODO: check that channels has same number of PIDs everywhere @@ -191,8 +192,7 @@ impl Grid { LagrangeSubgridV2::new(&subgrid_params, &ExtraSubgridParams::from(&subgrid_params)) .into(), )), - // TODO: add this as new parameter - convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], + convolutions, // TODO: make this a new parameter pid_basis: PidBasis::Pdg, channels, @@ -1865,6 +1865,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -1880,6 +1881,7 @@ mod tests { ], vec![Order::new(1, 2, 0, 0, 0), Order::new(1, 2, 0, 1, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -1900,6 +1902,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -1918,6 +1921,7 @@ mod tests { Order::new(0, 2, 0, 0, 0), ], vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -1961,6 +1965,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -1972,6 +1977,7 @@ mod tests { vec![channel![22, 22, 1.0], channel![2, 2, 1.0; 4, 4, 1.0]], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -2004,6 +2010,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -2019,6 +2026,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.5, 0.75, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); @@ -2055,6 +2063,7 @@ mod tests { logxia: 0, }], vec![0.0, 1.0], + vec![Convolution::UnpolPDF(2212); 2], SubgridParams::default(), ); diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 9e973ef3..7c0553da 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -58,7 +58,7 @@ use itertools::izip; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Order}; -use pineappl::convolutions::LumiCache; +use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags, Ntuple}; use pineappl::subgrid::{ExtraSubgridParams, SubgridParams}; use std::collections::HashMap; @@ -697,19 +697,19 @@ pub unsafe extern "C" fn pineappl_grid_new( let lumi = unsafe { &*lumi }; + let mut convolutions = vec![Convolution::UnpolPDF(2212); 2]; + if let Some(keyval) = key_vals { - if let Some(_value) = keyval.strings.get("initial_state_1") { - // TODO: set the first convolution type - todo!(); + if let Some(value) = keyval.strings.get("initial_state_1") { + convolutions[0] = Convolution::UnpolPDF(value.to_string_lossy().parse().unwrap()); } - if let Some(_value) = keyval.strings.get("initial_state_2") { - // TODO: set the second convolution type - todo!(); + if let Some(value) = keyval.strings.get("initial_state_2") { + convolutions[1] = Convolution::UnpolPDF(value.to_string_lossy().parse().unwrap()); } } - let grid = Box::new( + let mut grid = Box::new( Grid::with_subgrid_type( lumi.0.clone(), orders, @@ -721,6 +721,9 @@ pub unsafe extern "C" fn pineappl_grid_new( .unwrap(), ); + // TODO: set the convolutions using a new constructor + grid.convolutions_mut().clone_from_slice(&convolutions); + grid } @@ -899,9 +902,16 @@ pub unsafe extern "C" fn pineappl_grid_key_value( let key = key.to_string_lossy(); // backwards compatibility - match key.as_ref() { - "initial_state_1" | "initial_state_2" => todo!(), - _ => {} + let index = match key.as_ref() { + "initial_state_1" => Some(0), + "initial_state_2" => Some(1), + _ => None, + }; + + if let Some(index) = index { + return CString::new(grid.convolutions()[index].pid().unwrap().to_string()) + .unwrap() + .into_raw(); } CString::new(grid.metadata().get(key.as_ref()).map_or("", String::as_str)) @@ -935,9 +945,14 @@ pub unsafe extern "C" fn pineappl_grid_set_key_value( .into_owned(); // backwards compatibility - match key.as_str() { - "initial_state_1" | "initial_state_2" => todo!(), - _ => {} + let index = match key.as_str() { + "initial_state_1" => Some(0), + "initial_state_2" => Some(1), + _ => None, + }; + + if let Some(index) = index { + grid.convolutions_mut()[index] = Convolution::UnpolPDF(value.parse().unwrap()); } grid.metadata_mut().insert(key, value); diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index ee03ad04..e775b55e 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -4,12 +4,12 @@ use pineappl::boc::{Channel, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::packed_array::PackedArray; +use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::subgrid::{Mu2, SubgridParams}; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::pin::Pin; use std::ptr; -use pineappl::packed_subgrid::PackedQ1X2SubgridV1; fn convert_to_pdg_id(pid: usize) -> i32 { let pid = i32::try_from(pid).unwrap() - 6; @@ -122,6 +122,13 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let mut grids = Vec::with_capacity(orders.len()); + // from APPLgrid alone we don't know what type of convolution we have + let mut convolutions = vec![Convolution::UnpolPDF(2212); 2]; + + if grid.isDIS() { + convolutions[1] = Convolution::None; + } + for (i, order) in orders.into_iter().enumerate() { let channels = reconstruct_channels(&grid, i.try_into().unwrap(), dis_pid); let lumis_len = channels.len(); @@ -129,16 +136,10 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul channels, vec![order], bin_limits.clone(), + convolutions.clone(), SubgridParams::default(), ); - // from APPLgrid alone we don't know what type of convolution we have - pgrid.convolutions_mut()[0] = Convolution::UnpolPDF(2212); - - if grid.isDIS() { - pgrid.convolutions_mut()[1] = Convolution::None; - } - for bin in 0..grid.Nobs_internal() { let igrid = grid.weightgrid(i.try_into().unwrap(), bin); let igrid = unsafe { &*igrid }; diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 7c8ce1cb..f96afd5f 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -5,8 +5,8 @@ use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; -use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::packed_array::PackedArray; +use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::subgrid::{Mu2, SubgridParams}; use pineappl_fastnlo::ffi::{ self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF, @@ -98,6 +98,21 @@ fn convert_coeff_add_fix( ) -> Grid { let table_as_add_base = ffi::downcast_coeff_add_fix_to_base(table); + // UNWRAP: shouldn't be larger than `2` + let npdf = usize::try_from(table_as_add_base.GetNPDF()).unwrap(); + assert!(npdf <= 2); + + let mut convolutions = vec![Convolution::UnpolPDF(2212); npdf]; + + for index in 0..2 { + convolutions[index] = if index < npdf { + // TODO: how do we determined the PID/type of the convolution for fixed tables? + Convolution::UnpolPDF(2212) + } else { + Convolution::None + }; + } + let mut grid = Grid::new( reconstruct_channels(table_as_add_base, comb, dis_pid), vec![Order { @@ -110,22 +125,10 @@ fn convert_coeff_add_fix( (0..=bins) .map(|limit| u16::try_from(limit).unwrap().into()) .collect(), + convolutions, SubgridParams::default(), ); - // UNWRAP: shouldn't be larger than `2` - let npdf = usize::try_from(table_as_add_base.GetNPDF()).unwrap(); - assert!(npdf <= 2); - - for index in 0..2 { - grid.convolutions_mut()[index] = if index < npdf { - // TODO: how do we determined the PID/type of the convolution for fixed tables? - Convolution::UnpolPDF(2212) - } else { - Convolution::None - }; - } - let total_scalenodes: usize = table.GetTotalScalenodes().try_into().unwrap(); for obs in 0..table_as_add_base.GetNObsBin() { @@ -251,27 +254,29 @@ fn convert_coeff_add_flex( .collect(); let orders_len = orders.len(); + let npdf = table_as_add_base.GetNPDF(); + assert!(npdf <= 2); + + let convolutions = (0..2) + .map(|index| { + if index < npdf { + Convolution::UnpolPDF(table.GetPDFPDG(index)) + } else { + Convolution::None + } + }) + .collect(); + let mut grid = Grid::new( reconstruct_channels(table_as_add_base, comb, dis_pid), orders, (0..=bins) .map(|limit| u16::try_from(limit).unwrap().into()) .collect(), + convolutions, SubgridParams::default(), ); - let npdf = table_as_add_base.GetNPDF(); - assert!(npdf <= 2); - - for index in 0..2 { - // UNWRAP: index is smaller than 2 - grid.convolutions_mut()[usize::try_from(index).unwrap()] = if index < npdf { - Convolution::UnpolPDF(table.GetPDFPDG(index)) - } else { - Convolution::None - }; - } - let rescale = 0.1_f64.powi(table.GetIXsectUnits() - ipub_units); for obs in 0..bins { diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 146c8d8c..70731b3b 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -5,9 +5,9 @@ use pineappl::boc::Order; use pineappl::channel; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; +use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::pids::PidBasis; -use pineappl::packed_array::PackedArray; use pineappl::subgrid::{Mu2, SubgridParams}; use std::fs::File; use std::io::{BufRead, BufReader}; @@ -102,21 +102,21 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { lumis, vec![Order::new(0, 0, 0, 0, 0)], (0..=ndata).map(Into::into).collect(), + // legacy FK-tables only support unpolarized proton PDFs + vec![ + Convolution::UnpolPDF(2212), + if hadronic { + Convolution::UnpolPDF(2212) + } else { + Convolution::None + }, + ], SubgridParams::default(), ); // explicitly set the evolution basis *fktable.pid_basis_mut() = PidBasis::Evol; - // legacy FK-tables only support unpolarized proton PDFs - fktable.convolutions_mut()[0] = Convolution::UnpolPDF(2212); - - if hadronic { - fktable.convolutions_mut()[1] = Convolution::UnpolPDF(2212); - } else { - fktable.convolutions_mut()[1] = Convolution::None; - } - grid = Some(fktable); arrays = iter::repeat(PackedArray::new([1, nx1, nx2])) From 1e2f4d8698d7c8728981635503fcc251bab88466 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 9 Aug 2024 08:46:05 +0200 Subject: [PATCH 039/213] Fix a few clippy warnings --- pineappl/src/boc.rs | 13 ++++++------- pineappl/src/evolution.rs | 2 +- pineappl/src/grid.rs | 12 +++++------- pineappl/src/packed_subgrid.rs | 6 +++--- pineappl/src/subgrid.rs | 2 +- pineappl_capi/src/lib.rs | 8 ++++++++ pineappl_cli/src/helpers.rs | 7 ++----- pineappl_cli/src/import/fastnlo.rs | 20 ++++++++++---------- pineappl_cli/src/read.rs | 2 +- 9 files changed, 37 insertions(+), 35 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 1aaf7927..8dd86c4e 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -473,12 +473,11 @@ impl FromStr for Channel { .ok_or_else(|| ParseChannelError(format!("missing ')' in '{pids}'")))? .split(',') .map(|pid| { - Ok(pid.trim().parse::().map_err(|err| { + pid.trim().parse::().map_err(|err| { ParseChannelError(format!( - "could not parse PID: '{pid}', '{}'", - err.to_string() + "could not parse PID: '{pid}', '{err}'" )) - })?) + }) }) .collect::>()?; @@ -493,9 +492,9 @@ impl FromStr for Channel { .collect::>()?; if !result.iter().map(|(pids, _)| pids.len()).all_equal() { - return Err(ParseChannelError(format!( - "PID tuples have different lengths" - ))); + return Err(ParseChannelError( + "PID tuples have different lengths".to_owned(), + )); } Ok(Self::new(result)) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index d5162658..e8ac23f9 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -166,7 +166,7 @@ fn gluon_has_pid_zero(grid: &Grid) -> bool { // if there are any PID zero particles ... grid.channels() .iter() - .any(|entry| entry.entry().iter().any(|&(ref pids, _)| pids.iter().any(|&pid| pid == 0))) + .any(|entry| entry.entry().iter().any(|(pids, _)| pids.iter().any(|&pid| pid == 0))) // and if the particle IDs are encoded using PDG MC IDs && *grid.pid_basis() == PidBasis::Pdg } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 34602d8b..c232056e 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -92,7 +92,7 @@ struct Mmv3 { } impl Mmv3 { - fn new(subgrid_template: SubgridEnum) -> Self { + const fn new(subgrid_template: SubgridEnum) -> Self { Self { remapper: None, subgrid_template, @@ -110,8 +110,7 @@ fn default_metadata() -> BTreeMap { ) .to_owned(), )] - .iter() - .cloned() + .into_iter() .collect() } @@ -241,7 +240,7 @@ impl Grid { /// Return the convention by which the channels' PIDs are encoded. #[must_use] - pub fn pid_basis(&self) -> &PidBasis { + pub const fn pid_basis(&self) -> &PidBasis { &self.pid_basis } @@ -477,8 +476,7 @@ impl Grid { fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { use pineappl_v0::subgrid::Subgrid as _; - // TODO: convert error from v0 to v1 - let grid = GridV0::read(&mut reader).unwrap(); + let grid = GridV0::read(&mut reader).map_err(|err| GridError::Other(err.into()))?; // TODO: convert differently if grid only has one convolution let result = Self { @@ -492,7 +490,7 @@ impl Grid { } else { let mu2_grid: Vec<_> = subgrid .mu2_grid() - .into_iter() + .iter() .map(|mu2v0| Mu2 { ren: mu2v0.ren, fac: mu2v0.fac, diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 39181494..eeffeffe 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -19,7 +19,7 @@ pub struct PackedQ1X2SubgridV1 { impl PackedQ1X2SubgridV1 { /// Constructor. #[must_use] - pub fn new( + pub const fn new( array: PackedArray, mu2_grid: Vec, x1_grid: Vec, @@ -114,7 +114,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { for ([i, j, k], value) in self.array.indexed_iter() { let target_i = mu2_grid .iter() - .position(|&ref mu2| *mu2 == self.mu2_grid[i]) + .position(|mu2| *mu2 == self.mu2_grid[i]) .unwrap_or_else(|| unreachable!()); let target_j = x1_grid .iter() @@ -139,7 +139,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { let target_i = self .mu2_grid .iter() - .position(|&ref x| *x == rhs_mu2[i]) + .position(|x| *x == rhs_mu2[i]) .unwrap_or_else(|| unreachable!()); let target_j = self .x1_grid diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 45e8b8ef..af6e1e21 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -18,7 +18,7 @@ pub enum SubgridEnum { LagrangeSubgridV2, /// Empty subgrid. EmptySubgridV1, - /// + /// TODO PackedQ1X2SubgridV1, } diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 7c0553da..5905e118 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -292,6 +292,10 @@ pub unsafe extern "C" fn pineappl_grid_clone(grid: *const Grid) -> Box { } /// Wrapper for [`pineappl_grid_convolve_with_one`]. +/// +/// # Safety +/// +/// See [`pineappl_grid_convolve_with_one`]. #[deprecated( since = "0.8.0", note = "please use `pineappl_grid_convolve_with_one` instead" @@ -326,6 +330,10 @@ pub unsafe extern "C" fn pineappl_grid_convolute_with_one( } /// Wrapper for [`pineappl_grid_convolve_with_two`]. +/// +/// # Safety +/// +/// See [`pineappl_grid_convolve_with_two`]. #[deprecated( since = "0.8.0", note = "please use `pineappl_grid_convolve_with_two` instead" diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index aecf1902..a50912aa 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -161,15 +161,12 @@ pub fn labels_and_units(grid: &Grid, integrated: bool) -> (Vec<(String, &str)>, if integrated { "integ" } else { - metadata - .get("y_label") - .map(String::as_str) - .unwrap_or("diff") + metadata.get("y_label").map_or("diff", String::as_str) }, if integrated { "" // TODO: compute the units for the integrated cross section } else { - metadata.get("y_unit").map(String::as_str).unwrap_or("") + metadata.get("y_unit").map_or("", String::as_str) }, ) } diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index f96afd5f..2c937060 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -102,16 +102,16 @@ fn convert_coeff_add_fix( let npdf = usize::try_from(table_as_add_base.GetNPDF()).unwrap(); assert!(npdf <= 2); - let mut convolutions = vec![Convolution::UnpolPDF(2212); npdf]; - - for index in 0..2 { - convolutions[index] = if index < npdf { - // TODO: how do we determined the PID/type of the convolution for fixed tables? - Convolution::UnpolPDF(2212) - } else { - Convolution::None - }; - } + let convolutions = (0..2) + .map(|index| { + if index < npdf { + // TODO: how do we determined the PID/type of the convolution for fixed tables? + Convolution::UnpolPDF(2212) + } else { + Convolution::None + } + }) + .collect(); let mut grid = Grid::new( reconstruct_channels(table_as_add_base, comb, dis_pid), diff --git a/pineappl_cli/src/read.rs b/pineappl_cli/src/read.rs index 94a293b7..48bf56ab 100644 --- a/pineappl_cli/src/read.rs +++ b/pineappl_cli/src/read.rs @@ -168,7 +168,7 @@ impl Subcommand for Opts { println!("{value}"); } } else if self.group.keys { - for (key, _) in grid.metadata() { + for key in grid.metadata().keys() { println!("{key}"); } } else if self.group.show { From 41a67820d4aadc861e8504c29a49b6e80fb82994 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 9 Aug 2024 08:54:54 +0200 Subject: [PATCH 040/213] Remove obsolete `SparseArray3` --- pineappl/src/lib.rs | 1 - pineappl/src/sparse_array3.rs | 1135 --------------------------------- 2 files changed, 1136 deletions(-) delete mode 100644 pineappl/src/sparse_array3.rs diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index 67b0684f..ba3c62fe 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -46,5 +46,4 @@ pub mod lagrange_subgrid; pub mod packed_array; pub mod packed_subgrid; pub mod pids; -pub mod sparse_array3; pub mod subgrid; diff --git a/pineappl/src/sparse_array3.rs b/pineappl/src/sparse_array3.rs deleted file mode 100644 index 1debae7e..00000000 --- a/pineappl/src/sparse_array3.rs +++ /dev/null @@ -1,1135 +0,0 @@ -//! Module containing the `SparseArray3` struct. - -use ndarray::{ArrayView3, Axis}; -use serde::{Deserialize, Serialize}; -use std::iter; -use std::mem; -use std::ops::{Index, IndexMut, Range}; -use std::slice::{Iter, IterMut}; - -/// Struct for a sparse three-dimensional array, which is optimized for the sparsity of -/// interpolation grids. -#[derive(Clone, Deserialize, Serialize)] -pub struct SparseArray3 { - entries: Vec, - indices: Vec<(usize, usize)>, - start: usize, - dimensions: (usize, usize, usize), -} - -// TODO: write panic messages - -impl Index<[usize; 3]> for SparseArray3 { - type Output = T; - - fn index(&self, mut index: [usize; 3]) -> &Self::Output { - // index too small - assert!(index[0] >= self.start); - - let dim1 = if self.dimensions.1 > self.dimensions.2 { - index.swap(1, 2); - self.dimensions.2 - } else { - self.dimensions.1 - }; - - // index too large - assert!(index[0] < (self.start + (self.indices.len() - 1) / dim1)); - - // index too large - assert!(index[1] < dim1); - - let forward = dim1 * (index[0] - self.start) + index[1]; - let indices_a = &self.indices[forward]; - let indices_b = &self.indices[forward + 1]; - - let zeros_left = indices_a.0; - let offset = indices_a.1; - let non_zeros = indices_b.1 - offset; - - // index too small - assert!(index[2] >= zeros_left); - - // index too large - assert!(index[2] < (non_zeros + zeros_left)); - - &self.entries[offset + (index[2] - zeros_left)] - } -} - -impl IndexMut<[usize; 3]> for SparseArray3 { - fn index_mut(&mut self, mut index: [usize; 3]) -> &mut Self::Output { - let dim1 = if self.dimensions.1 > self.dimensions.2 { - index.swap(1, 2); - self.dimensions.2 - } else { - self.dimensions.1 - }; - - let max_index0 = self.start + (self.indices.len() - 1) / dim1; - - if index[0] < self.start { - let elements = self.start - index[0]; - self.start = index[0]; - self.indices - .splice(0..0, iter::repeat((0, 0)).take(elements * dim1)); - } else if index[0] >= self.dimensions.0 { - panic!(); - } else if self.entries.is_empty() || (index[0] >= max_index0) { - let elements = if self.entries.is_empty() { - self.start = index[0]; - 1 - } else { - index[0] - max_index0 + 1 - }; - - let insert = self.indices.len() - 1; - self.indices.splice( - insert..insert, - iter::repeat((0, self.indices.last().unwrap().1)).take(elements * dim1), - ); - } - - // index too large - assert!(index[1] < dim1); - - let forward = dim1 * (index[0] - self.start) + index[1]; - let indices_a = &self.indices[forward]; - let indices_b = &self.indices[forward + 1]; - - let zeros_left = indices_a.0; - let offset = indices_a.1; - let non_zeros = indices_b.1 - offset; - - let elements; - let insert; - - if index[2] < zeros_left { - elements = zeros_left - index[2]; - insert = offset; - self.indices[forward].0 -= elements; - } else if index[2] >= self.dimensions.2.max(self.dimensions.1) { - panic!(); - } else if non_zeros == 0 { - elements = 1; - insert = offset; - self.indices[forward].0 = index[2]; - } else if index[2] >= (zeros_left + non_zeros) { - elements = index[2] - (zeros_left + non_zeros) + 1; - insert = offset + non_zeros; - } else { - return &mut self.entries[offset + (index[2] - zeros_left)]; - } - - self.entries - .splice(insert..insert, iter::repeat(T::default()).take(elements)); - self.indices - .iter_mut() - .skip(forward + 1) - .for_each(|ix| ix.1 += elements); - - &mut self.entries[offset + (index[2] - self.indices[forward].0)] - } -} - -/// Immutable iterator over the elements of a `SparseArray3`. -pub struct IndexedIter<'a, T> { - entry_iter: Iter<'a, T>, - index_iter: Iter<'a, (usize, usize)>, - offset_a: Option<&'a (usize, usize)>, - offset_b: Option<&'a (usize, usize)>, - tuple: (usize, usize, usize), - dimensions: (usize, usize, usize), -} - -impl<'a, T: Copy + Default + PartialEq> Iterator for IndexedIter<'a, T> { - type Item = ((usize, usize, usize), T); - - fn next(&mut self) -> Option { - if let Some(element) = self.entry_iter.next() { - let offset_a = self.offset_a.unwrap(); - let offset_b = self.offset_b.unwrap(); - - if self.dimensions.1 > self.dimensions.2 { - self.tuple.1 = self.tuple.1.max(offset_a.0); - - if self.tuple.1 >= (offset_b.1 - offset_a.1 + offset_a.0) { - loop { - self.offset_a = self.offset_b; - self.offset_b = self.index_iter.next(); - - let offset_a = self.offset_a.unwrap(); - let offset_b = self.offset_b?; - - self.tuple.2 += 1; - - if self.tuple.2 >= self.dimensions.2 { - self.tuple.0 += 1; - self.tuple.2 = 0; - } - - if (offset_b.1 - offset_a.1) != 0 { - self.tuple.1 = offset_a.0; - break; - } - } - } - - if *element == T::default() { - self.tuple.1 += 1; - self.next() - } else { - let result = Some((self.tuple, *element)); - self.tuple.1 += 1; - result - } - } else { - self.tuple.2 = self.tuple.2.max(offset_a.0); - - if self.tuple.2 >= (offset_b.1 - offset_a.1 + offset_a.0) { - loop { - self.offset_a = self.offset_b; - self.offset_b = self.index_iter.next(); - - let offset_a = self.offset_a.unwrap(); - let offset_b = self.offset_b?; - - self.tuple.1 += 1; - - if self.tuple.1 >= self.dimensions.1 { - self.tuple.0 += 1; - self.tuple.1 = 0; - } - - if (offset_b.1 - offset_a.1) != 0 { - self.tuple.2 = offset_a.0; - break; - } - } - } - - if *element == T::default() { - self.tuple.2 += 1; - self.next() - } else { - let result = Some((self.tuple, *element)); - self.tuple.2 += 1; - result - } - } - } else { - None - } - } -} - -impl SparseArray3 { - /// Constructs a new and empty `SparseArray3` with the specified dimensions `nx`, `ny` and - /// `nz`. - #[must_use] - pub fn new(nx: usize, ny: usize, nz: usize) -> Self { - Self { - entries: vec![], - indices: vec![(0, 0)], - start: 0, - dimensions: (nx, ny, nz), - } - } - - /// Converts `array` into a `SparseArray3`. - #[must_use] - pub fn from_ndarray(array: ArrayView3, xstart: usize, xsize: usize) -> Self { - let (_, ny, nz) = array.dim(); - let array = if ny > nz { - let mut array = array; - array.swap_axes(1, 2); - array - } else { - array - }; - - let dimensions = (xsize, ny, nz); - let mut entries = vec![]; - let mut indices = vec![]; - - let mut offset = 0; - - for array2 in array.axis_iter(Axis(0)) { - for array1 in array2.axis_iter(Axis(0)) { - let start = array1.iter().position(|x| *x != T::default()); - - if let Some(start) = start { - let end = array1.iter().enumerate().skip(start).fold( - start, - |last_non_zero, (index, x)| { - if *x == T::default() { - last_non_zero - } else { - index - } - }, - ) + 1; - indices.push((start, offset)); - offset += end - start; - entries.splice( - entries.len()..entries.len(), - array1.iter().skip(start).take(end - start).cloned(), - ); - } else { - indices.push((0, offset)); - } - } - } - - indices.push((0, offset)); - - Self { - entries, - indices, - start: xstart, - dimensions, - } - } - - /// Clear the contents of the array. - pub fn clear(&mut self) { - self.entries.clear(); - self.indices.clear(); - self.indices.push((0, 0)); - self.start = 0; - } - - /// Returns the dimensions of this array. - #[must_use] - pub const fn dimensions(&self) -> (usize, usize, usize) { - self.dimensions - } - - /// Returns the overhead for storing the explicitly zero and non-zero elements. - #[must_use] - pub fn overhead(&self) -> usize { - (2 * self.indices.len() * mem::size_of::()) / mem::size_of::() - } - - /// Returns the number of default (zero) elements in this array. - #[must_use] - pub fn zeros(&self) -> usize { - self.entries.iter().filter(|x| **x == T::default()).count() - } - - /// Returns the number of non-default (non-zero) elements in this array. - #[must_use] - pub fn len(&self) -> usize { - self.entries.iter().filter(|x| **x != T::default()).count() - } - - /// Returns `true` if the array contains no element. - #[must_use] - pub fn is_empty(&self) -> bool { - self.entries.is_empty() - } - - /// Return an indexed `Iterator` over the non-zero elements of this array. The iterator element - /// type is `((usize, usize, usize), T)`. - #[must_use] - pub fn indexed_iter(&self) -> IndexedIter<'_, T> { - let mut result = IndexedIter { - entry_iter: self.entries.iter(), - index_iter: self.indices.iter(), - offset_a: None, - offset_b: None, - tuple: (self.start, 0, 0), - dimensions: self.dimensions, - }; - - result.offset_a = result.index_iter.next(); - result.offset_b = result.index_iter.next(); - - result - } - - /// Return an iterator over the elements, including zero elements. - pub fn iter_mut(&mut self) -> IterMut<'_, T> { - self.entries.iter_mut() - } - - /// Return a half-open interval of indices that are filled for the first dimension. - #[must_use] - pub fn x_range(&self) -> Range { - self.start - ..(self.start + (self.indices.len() - 1) / self.dimensions.1.min(self.dimensions.2)) - } - - /// Increase the number of entries of the x-axis by one by inserting zeros at `x`. - pub fn increase_x_at(&mut self, x: usize) { - let dim1 = self.dimensions.1.min(self.dimensions.2); - let nx = (self.indices.len() - 1) / dim1; - - if x <= self.start { - self.start += 1; - } else if x < self.start + nx { - let at = (x - self.start) * dim1; - let offset = self.indices[at].1; - self.indices - .splice(at..at, iter::repeat((0, offset)).take(dim1)); - } else if x <= self.dimensions.0 { - // nothing to do here - } else { - self.dimensions.0 = x; - } - - self.dimensions.0 += 1; - } - - /// Removes all elements with the specified x coordinate. - /// - /// # Panics - /// - /// TODO - pub fn remove_x(&mut self, x: usize) { - let dim1 = self.dimensions.1.min(self.dimensions.2); - let nx = (self.indices.len() - 1) / dim1; - - assert!((x >= self.start) && (x < self.start + nx)); - - let index_a = (x - self.start) * dim1; - let index_b = (x - self.start + 1) * dim1; - let offset_a = self.indices[index_a].1; - let offset_b = self.indices[index_b].1; - - self.entries.drain(offset_a..offset_b); - self.indices - .iter_mut() - .skip(index_b) - .for_each(|o| o.1 -= offset_b - offset_a); - - if (x != self.start) && (x != (self.start + nx - 1)) { - self.indices - .splice(index_a..index_b, iter::repeat((0, offset_a)).take(dim1)); - } else { - if x == self.start { - self.start += 1; - } - - self.indices.drain(index_a..index_b); - } - - if self.indices.last().unwrap().1 == 0 { - self.clear(); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use ndarray::Array3; - - #[test] - fn index_access() { - let mut array = SparseArray3::new(40, 50, 50); - - // after creation the array must be empty - assert_eq!(array.x_range(), 0..0); - assert_eq!(array.overhead(), 2); - assert!(array.is_empty()); - - // insert the first element - array[[5, 10, 10]] = 1.0; - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 1); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 5..6); - assert_eq!(array.overhead(), 102); - assert!(!array.is_empty()); - - // insert an element after the first one - array[[8, 10, 10]] = 2.0; - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 2); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 5..9); - assert_eq!(array.overhead(), 402); - assert!(!array.is_empty()); - - // insert an element before the first one - array[[1, 10, 10]] = 3.0; - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 3); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - array[[1, 10, 11]] = 4.0; - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 4); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - array[[1, 10, 9]] = 5.0; - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 5); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - array[[1, 10, 0]] = 6.0; - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 6); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - // check zeros - assert_eq!(array[[1, 10, 1]], 0.0); - assert_eq!(array[[1, 10, 2]], 0.0); - assert_eq!(array[[1, 10, 3]], 0.0); - assert_eq!(array[[1, 10, 4]], 0.0); - assert_eq!(array[[1, 10, 5]], 0.0); - assert_eq!(array[[1, 10, 6]], 0.0); - assert_eq!(array[[1, 10, 7]], 0.0); - assert_eq!(array[[1, 10, 8]], 0.0); - assert_eq!(array.zeros(), 8); - - // insert where previously a zero was - array[[1, 10, 2]] = 7.0; - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 7); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - // check zeros - assert_eq!(array[[1, 10, 1]], 0.0); - assert_eq!(array[[1, 10, 3]], 0.0); - assert_eq!(array[[1, 10, 4]], 0.0); - assert_eq!(array[[1, 10, 5]], 0.0); - assert_eq!(array[[1, 10, 6]], 0.0); - assert_eq!(array[[1, 10, 7]], 0.0); - assert_eq!(array[[1, 10, 8]], 0.0); - assert_eq!(array.zeros(), 7); - - array[[1, 15, 2]] = 8.0; - assert_eq!(array[[1, 15, 2]], 8.0); - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 8); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - // check zeros - assert_eq!(array[[1, 10, 1]], 0.0); - assert_eq!(array[[1, 10, 3]], 0.0); - assert_eq!(array[[1, 10, 4]], 0.0); - assert_eq!(array[[1, 10, 5]], 0.0); - assert_eq!(array[[1, 10, 6]], 0.0); - assert_eq!(array[[1, 10, 7]], 0.0); - assert_eq!(array[[1, 10, 8]], 0.0); - assert_eq!(array.zeros(), 7); - - array[[1, 15, 4]] = 9.0; - assert_eq!(array[[1, 15, 4]], 9.0); - assert_eq!(array[[1, 15, 2]], 8.0); - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 9); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - // check zeros - assert_eq!(array[[1, 15, 3]], 0.0); - assert_eq!(array[[1, 10, 1]], 0.0); - assert_eq!(array[[1, 10, 3]], 0.0); - assert_eq!(array[[1, 10, 4]], 0.0); - assert_eq!(array[[1, 10, 5]], 0.0); - assert_eq!(array[[1, 10, 6]], 0.0); - assert_eq!(array[[1, 10, 7]], 0.0); - assert_eq!(array[[1, 10, 8]], 0.0); - assert_eq!(array.zeros(), 8); - - array[[1, 15, 0]] = 10.0; - assert_eq!(array[[1, 15, 0]], 10.0); - assert_eq!(array[[1, 15, 4]], 9.0); - assert_eq!(array[[1, 15, 2]], 8.0); - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); - assert_eq!(array.len(), 10); - assert_eq!(array.x_range(), 1..9); - assert_eq!(array.overhead(), 802); - assert!(!array.is_empty()); - - // check zeros - assert_eq!(array[[1, 15, 1]], 0.0); - assert_eq!(array[[1, 15, 3]], 0.0); - assert_eq!(array[[1, 10, 1]], 0.0); - assert_eq!(array[[1, 10, 3]], 0.0); - assert_eq!(array[[1, 10, 4]], 0.0); - assert_eq!(array[[1, 10, 5]], 0.0); - assert_eq!(array[[1, 10, 6]], 0.0); - assert_eq!(array[[1, 10, 7]], 0.0); - assert_eq!(array[[1, 10, 8]], 0.0); - assert_eq!(array.zeros(), 9); - } - - #[test] - #[should_panic(expected = "explicit panic")] - fn index_mut_panic_dim0() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[40, 0, 50]] = 1.0; - } - - #[test] - #[should_panic(expected = "assertion failed: index[1] < dim1")] - fn index_mut_panic_dim1() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[0, 50, 0]] = 1.0; - } - - #[test] - #[should_panic(expected = "explicit panic")] - fn index_mut_panic_dim2() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[0, 0, 50]] = 1.0; - } - - #[test] - #[should_panic(expected = "assertion failed: index[0] >= self.start")] - fn index_panic_dim0_0() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[1, 0, 0]] = 1.0; - - assert_eq!(array[[0, 0, 0]], 0.0); - } - - #[test] - #[should_panic( - expected = "assertion failed: index[0] < (self.start + (self.indices.len() - 1) / dim1)" - )] - fn index_panic_dim0_1() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[1, 0, 0]] = 1.0; - - assert_eq!(array[[2, 0, 0]], 0.0); - } - - #[test] - #[should_panic(expected = "assertion failed: index[1] < dim1")] - fn index_panic_dim1() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[1, 0, 0]] = 1.0; - - assert_eq!(array[[1, 50, 0]], 0.0); - } - - #[test] - #[should_panic(expected = "assertion failed: index[2] >= zeros_left")] - fn index_panic_dim2_0() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[0, 0, 1]] = 1.0; - - assert_eq!(array[[0, 0, 0]], 0.0); - } - - #[test] - #[should_panic(expected = "assertion failed: index[2] < (non_zeros + zeros_left)")] - fn index_panic_dim2_1() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[0, 0, 1]] = 1.0; - - assert_eq!(array[[0, 0, 2]], 0.0); - } - - #[test] - fn indexed_iter() { - let mut array = SparseArray3::new(40, 50, 50); - - // check empty iterator - assert_eq!(array.indexed_iter().next(), None); - - // insert an element - array[[2, 3, 4]] = 1.0; - - let mut iter = array.indexed_iter(); - - // check iterator with one element - assert_eq!(iter.next(), Some(((2, 3, 4), 1.0))); - assert_eq!(iter.next(), None); - - // insert another element - array[[2, 3, 6]] = 2.0; - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((2, 3, 4), 1.0))); - assert_eq!(iter.next(), Some(((2, 3, 6), 2.0))); - assert_eq!(iter.next(), None); - - // insert yet another element - array[[4, 5, 7]] = 3.0; - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((2, 3, 4), 1.0))); - assert_eq!(iter.next(), Some(((2, 3, 6), 2.0))); - assert_eq!(iter.next(), Some(((4, 5, 7), 3.0))); - assert_eq!(iter.next(), None); - - // insert at the very first position - array[[2, 0, 0]] = 4.0; - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((2, 0, 0), 4.0))); - assert_eq!(iter.next(), Some(((2, 3, 4), 1.0))); - assert_eq!(iter.next(), Some(((2, 3, 6), 2.0))); - assert_eq!(iter.next(), Some(((4, 5, 7), 3.0))); - assert_eq!(iter.next(), None); - } - - #[test] - fn iter_mut() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[3, 5, 1]] = 1.0; - array[[7, 8, 9]] = 2.0; - array[[7, 8, 13]] = 3.0; - array[[9, 1, 4]] = 4.0; - - let mut iter = array.iter_mut(); - - assert_eq!(iter.next(), Some(&mut 1.0)); - assert_eq!(iter.next(), Some(&mut 2.0)); - assert_eq!(iter.next(), Some(&mut 0.0)); - assert_eq!(iter.next(), Some(&mut 0.0)); - assert_eq!(iter.next(), Some(&mut 0.0)); - assert_eq!(iter.next(), Some(&mut 3.0)); - assert_eq!(iter.next(), Some(&mut 4.0)); - assert_eq!(iter.next(), None); - } - - #[test] - fn clear() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[3, 5, 1]] = 1.0; - array[[7, 8, 9]] = 2.0; - array[[9, 1, 4]] = 3.0; - - assert!(!array.is_empty()); - assert_eq!(array.len(), 3); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 3..10); - - array.clear(); - - assert!(array.is_empty()); - assert_eq!(array.len(), 0); - assert_eq!(array.zeros(), 0); - assert_eq!(array.x_range(), 0..0); - } - - #[test] - fn remove_x() { - let mut array = SparseArray3::new(40, 50, 50); - - array[[1, 5, 6]] = 1.0; - array[[1, 6, 5]] = 2.0; - array[[1, 2, 3]] = 3.0; - array[[1, 9, 3]] = 4.0; - array[[1, 8, 4]] = 5.0; - array[[2, 0, 0]] = 6.0; - array[[3, 4, 5]] = 7.0; - array[[3, 4, 6]] = 8.0; - array[[3, 4, 7]] = 9.0; - array[[4, 0, 2]] = 10.0; - array[[4, 0, 3]] = 11.0; - array[[5, 0, 1]] = 12.0; - array[[5, 0, 2]] = 13.0; - - assert_eq!(array.x_range(), 1..6); - assert_eq!(array.len(), 13); - assert_eq!(array.zeros(), 0); - - // remove the first five entries - array.remove_x(1); - - assert_eq!(array.x_range(), 2..6); - assert_eq!(array.len(), 8); - assert_eq!(array.zeros(), 0); - - // remove the last two entries - array.remove_x(5); - - assert_eq!(array.x_range(), 2..5); - assert_eq!(array.len(), 6); - assert_eq!(array.zeros(), 0); - - // remove the from the middle - array.remove_x(3); - - assert_eq!(array.x_range(), 2..5); - assert_eq!(array.len(), 3); - assert_eq!(array.zeros(), 0); - - // remove also the rest - array.remove_x(4); - array.remove_x(2); - - assert_eq!(array.x_range(), 0..0); - assert_eq!(array.len(), 0); - assert_eq!(array.zeros(), 0); - } - - #[test] - #[should_panic(expected = "assertion failed: (x >= self.start) && (x < self.start + nx)")] - fn remove_x_panic() { - let mut array = SparseArray3::::new(40, 50, 50); - - array.remove_x(0); - } - - #[test] - fn increase_at_x() { - let mut array = SparseArray3::new(1, 50, 50); - - array[[0, 0, 0]] = 1.0; - array[[0, 2, 3]] = 2.0; - array[[0, 2, 4]] = 3.0; - array[[0, 2, 5]] = 4.0; - array[[0, 3, 0]] = 5.0; - array[[0, 49, 49]] = 6.0; - - assert_eq!(array.dimensions(), (1, 50, 50)); - assert_eq!(array[[0, 0, 0]], 1.0); - assert_eq!(array[[0, 2, 3]], 2.0); - assert_eq!(array[[0, 2, 4]], 3.0); - assert_eq!(array[[0, 2, 5]], 4.0); - assert_eq!(array[[0, 3, 0]], 5.0); - assert_eq!(array[[0, 49, 49]], 6.0); - - // increase at the end - array.increase_x_at(1); - - assert_eq!(array.dimensions(), (2, 50, 50)); - assert_eq!(array[[0, 0, 0]], 1.0); - assert_eq!(array[[0, 2, 3]], 2.0); - assert_eq!(array[[0, 2, 4]], 3.0); - assert_eq!(array[[0, 2, 5]], 4.0); - assert_eq!(array[[0, 3, 0]], 5.0); - assert_eq!(array[[0, 49, 49]], 6.0); - - array[[1, 5, 0]] = 7.0; - array[[1, 5, 5]] = 8.0; - array[[1, 6, 3]] = 9.0; - array[[1, 6, 0]] = 10.0; - - assert_eq!(array[[0, 0, 0]], 1.0); - assert_eq!(array[[0, 2, 3]], 2.0); - assert_eq!(array[[0, 2, 4]], 3.0); - assert_eq!(array[[0, 2, 5]], 4.0); - assert_eq!(array[[0, 3, 0]], 5.0); - assert_eq!(array[[0, 49, 49]], 6.0); - assert_eq!(array[[1, 5, 0]], 7.0); - assert_eq!(array[[1, 5, 5]], 8.0); - assert_eq!(array[[1, 6, 3]], 9.0); - assert_eq!(array[[1, 6, 0]], 10.0); - - // increase at the start - array.increase_x_at(0); - - assert_eq!(array.dimensions(), (3, 50, 50)); - assert_eq!(array[[1, 0, 0]], 1.0); - assert_eq!(array[[1, 2, 3]], 2.0); - assert_eq!(array[[1, 2, 4]], 3.0); - assert_eq!(array[[1, 2, 5]], 4.0); - assert_eq!(array[[1, 3, 0]], 5.0); - assert_eq!(array[[1, 49, 49]], 6.0); - assert_eq!(array[[2, 5, 0]], 7.0); - assert_eq!(array[[2, 5, 5]], 8.0); - assert_eq!(array[[2, 6, 3]], 9.0); - assert_eq!(array[[2, 6, 0]], 10.0); - - // increase at the end - array.increase_x_at(3); - - assert_eq!(array.dimensions(), (4, 50, 50)); - assert_eq!(array[[1, 0, 0]], 1.0); - assert_eq!(array[[1, 2, 3]], 2.0); - assert_eq!(array[[1, 2, 4]], 3.0); - assert_eq!(array[[1, 2, 5]], 4.0); - assert_eq!(array[[1, 3, 0]], 5.0); - assert_eq!(array[[1, 49, 49]], 6.0); - assert_eq!(array[[2, 5, 0]], 7.0); - assert_eq!(array[[2, 5, 5]], 8.0); - assert_eq!(array[[2, 6, 3]], 9.0); - assert_eq!(array[[2, 6, 0]], 10.0); - - // increase after the end - array.increase_x_at(5); - - assert_eq!(array.dimensions(), (6, 50, 50)); - assert_eq!(array[[1, 0, 0]], 1.0); - assert_eq!(array[[1, 2, 3]], 2.0); - assert_eq!(array[[1, 2, 4]], 3.0); - assert_eq!(array[[1, 2, 5]], 4.0); - assert_eq!(array[[1, 3, 0]], 5.0); - assert_eq!(array[[1, 49, 49]], 6.0); - assert_eq!(array[[2, 5, 0]], 7.0); - assert_eq!(array[[2, 5, 5]], 8.0); - assert_eq!(array[[2, 6, 3]], 9.0); - assert_eq!(array[[2, 6, 0]], 10.0); - - // increase in the middle - array.increase_x_at(2); - - assert_eq!(array.dimensions(), (7, 50, 50)); - assert_eq!(array[[1, 0, 0]], 1.0); - assert_eq!(array[[1, 2, 3]], 2.0); - assert_eq!(array[[1, 2, 4]], 3.0); - assert_eq!(array[[1, 2, 5]], 4.0); - assert_eq!(array[[1, 3, 0]], 5.0); - assert_eq!(array[[1, 49, 49]], 6.0); - assert_eq!(array[[3, 5, 0]], 7.0); - assert_eq!(array[[3, 5, 5]], 8.0); - assert_eq!(array[[3, 6, 3]], 9.0); - assert_eq!(array[[3, 6, 0]], 10.0); - } - - #[test] - fn from_ndarray() { - let mut ndarray = Array3::zeros((2, 50, 50)); - - ndarray[[0, 4, 3]] = 1.0; - ndarray[[0, 4, 4]] = 2.0; - ndarray[[0, 4, 6]] = 3.0; - ndarray[[0, 5, 1]] = 4.0; - ndarray[[0, 5, 7]] = 5.0; - ndarray[[1, 3, 9]] = 6.0; - - let array = SparseArray3::from_ndarray(ndarray.view(), 3, 40); - - assert_eq!(array[[3, 4, 3]], 1.0); - assert_eq!(array[[3, 4, 4]], 2.0); - assert_eq!(array[[3, 4, 5]], 0.0); - assert_eq!(array[[3, 4, 6]], 3.0); - assert_eq!(array[[3, 5, 1]], 4.0); - assert_eq!(array[[3, 5, 2]], 0.0); - assert_eq!(array[[3, 5, 3]], 0.0); - assert_eq!(array[[3, 5, 4]], 0.0); - assert_eq!(array[[3, 5, 5]], 0.0); - assert_eq!(array[[3, 5, 6]], 0.0); - assert_eq!(array[[3, 5, 7]], 5.0); - assert_eq!(array[[4, 3, 9]], 6.0); - - assert_eq!(array.len(), 6); - assert_eq!(array.zeros(), 6); - } - - #[test] - fn test_index_swap() { - let mut array = SparseArray3::new(5, 50, 2); - - array[[0, 0, 0]] = 1.0; - array[[0, 0, 1]] = 2.0; - array[[1, 2, 1]] = 3.0; - array[[1, 5, 1]] = 4.0; - array[[1, 6, 0]] = 5.0; - array[[1, 8, 0]] = 6.0; - array[[1, 9, 0]] = 7.0; - array[[2, 0, 0]] = 8.0; - array[[3, 2, 1]] = 9.0; - array[[3, 4, 0]] = 10.0; - array[[3, 4, 1]] = 11.0; - array[[4, 0, 0]] = 12.0; - array[[4, 0, 1]] = 13.0; - - assert_eq!(array[[0, 0, 0]], 1.0); - assert_eq!(array[[0, 0, 1]], 2.0); - assert_eq!(array[[1, 2, 1]], 3.0); - assert_eq!(array[[1, 5, 1]], 4.0); - assert_eq!(array[[1, 6, 0]], 5.0); - assert_eq!(array[[1, 8, 0]], 6.0); - assert_eq!(array[[1, 9, 0]], 7.0); - assert_eq!(array[[2, 0, 0]], 8.0); - assert_eq!(array[[3, 2, 1]], 9.0); - assert_eq!(array[[3, 4, 0]], 10.0); - assert_eq!(array[[3, 4, 1]], 11.0); - assert_eq!(array[[4, 0, 0]], 12.0); - assert_eq!(array[[4, 0, 1]], 13.0); - - assert_eq!(array.x_range(), 0..5); - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((0, 0, 0), 1.0))); - assert_eq!(iter.next(), Some(((0, 0, 1), 2.0))); - assert_eq!(iter.next(), Some(((1, 6, 0), 5.0))); - assert_eq!(iter.next(), Some(((1, 8, 0), 6.0))); - assert_eq!(iter.next(), Some(((1, 9, 0), 7.0))); - assert_eq!(iter.next(), Some(((1, 2, 1), 3.0))); - assert_eq!(iter.next(), Some(((1, 5, 1), 4.0))); - assert_eq!(iter.next(), Some(((2, 0, 0), 8.0))); - assert_eq!(iter.next(), Some(((3, 4, 0), 10.0))); - assert_eq!(iter.next(), Some(((3, 2, 1), 9.0))); - assert_eq!(iter.next(), Some(((3, 4, 1), 11.0))); - assert_eq!(iter.next(), Some(((4, 0, 0), 12.0))); - assert_eq!(iter.next(), Some(((4, 0, 1), 13.0))); - assert_eq!(iter.next(), None); - - let mut ndarray = Array3::zeros((5, 50, 2)); - - ndarray[[0, 0, 0]] = 1.0; - ndarray[[0, 0, 1]] = 2.0; - ndarray[[1, 2, 1]] = 3.0; - ndarray[[1, 5, 1]] = 4.0; - ndarray[[1, 6, 0]] = 5.0; - ndarray[[1, 8, 0]] = 6.0; - ndarray[[1, 9, 0]] = 7.0; - ndarray[[2, 0, 0]] = 8.0; - ndarray[[3, 2, 1]] = 9.0; - ndarray[[3, 4, 0]] = 10.0; - ndarray[[3, 4, 1]] = 11.0; - ndarray[[4, 0, 0]] = 12.0; - ndarray[[4, 0, 1]] = 13.0; - - let mut other = SparseArray3::from_ndarray(ndarray.view(), 0, 5); - - assert_eq!(other[[0, 0, 0]], 1.0); - assert_eq!(other[[0, 0, 1]], 2.0); - assert_eq!(other[[1, 2, 1]], 3.0); - assert_eq!(other[[1, 5, 1]], 4.0); - assert_eq!(other[[1, 6, 0]], 5.0); - assert_eq!(other[[1, 8, 0]], 6.0); - assert_eq!(other[[1, 9, 0]], 7.0); - assert_eq!(other[[2, 0, 0]], 8.0); - assert_eq!(other[[3, 2, 1]], 9.0); - assert_eq!(other[[3, 4, 0]], 10.0); - assert_eq!(other[[3, 4, 1]], 11.0); - assert_eq!(other[[4, 0, 0]], 12.0); - assert_eq!(other[[4, 0, 1]], 13.0); - - assert_eq!(other.x_range(), 0..5); - - other.remove_x(0); - - assert_eq!(other[[1, 2, 1]], 3.0); - assert_eq!(other[[1, 5, 1]], 4.0); - assert_eq!(other[[1, 6, 0]], 5.0); - assert_eq!(other[[1, 8, 0]], 6.0); - assert_eq!(other[[1, 9, 0]], 7.0); - assert_eq!(other[[2, 0, 0]], 8.0); - assert_eq!(other[[3, 2, 1]], 9.0); - assert_eq!(other[[3, 4, 0]], 10.0); - assert_eq!(other[[3, 4, 1]], 11.0); - assert_eq!(other[[4, 0, 0]], 12.0); - assert_eq!(other[[4, 0, 1]], 13.0); - - other.remove_x(3); - - assert_eq!(other[[1, 2, 1]], 3.0); - assert_eq!(other[[1, 5, 1]], 4.0); - assert_eq!(other[[1, 6, 0]], 5.0); - assert_eq!(other[[1, 8, 0]], 6.0); - assert_eq!(other[[1, 9, 0]], 7.0); - assert_eq!(other[[2, 0, 0]], 8.0); - assert_eq!(other[[4, 0, 0]], 12.0); - assert_eq!(other[[4, 0, 1]], 13.0); - - other.remove_x(4); - - assert_eq!(other[[1, 2, 1]], 3.0); - assert_eq!(other[[1, 5, 1]], 4.0); - assert_eq!(other[[1, 6, 0]], 5.0); - assert_eq!(other[[1, 8, 0]], 6.0); - assert_eq!(other[[1, 9, 0]], 7.0); - assert_eq!(other[[2, 0, 0]], 8.0); - } - - // https://github.com/NNPDF/pineappl/issues/220 - #[test] - fn regression_test_220() { - let mut array = SparseArray3::new(1, 2, 4); - - array[[0, 0, 0]] = 1.0; - - assert_eq!(array[[0, 0, 0]], 1.0); - - assert_eq!(array.x_range(), 0..1); - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((0, 0, 0), 1.0))); - assert_eq!(iter.next(), None); - - array.increase_x_at(0); - - array[[0, 0, 0]] = 2.0; - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((0, 0, 0), 2.0))); - assert_eq!(iter.next(), Some(((1, 0, 0), 1.0))); - assert_eq!(iter.next(), None); - - array.increase_x_at(1); - - array[[1, 0, 0]] = 3.0; - - let mut iter = array.indexed_iter(); - - assert_eq!(iter.next(), Some(((0, 0, 0), 2.0))); - assert_eq!(iter.next(), Some(((1, 0, 0), 3.0))); - assert_eq!(iter.next(), Some(((2, 0, 0), 1.0))); - assert_eq!(iter.next(), None); - } -} From 4d1fc3996f3cf24d426737763b458a0966ed44d5 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 9 Aug 2024 09:04:52 +0200 Subject: [PATCH 041/213] Use integers to avoid floating point comparisons --- pineappl/src/packed_array.rs | 290 +++++++++++++++++------------------ 1 file changed, 145 insertions(+), 145 deletions(-) diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 4f29764a..4c205437 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -320,61 +320,61 @@ mod tests { #[test] fn index() { - let mut a = PackedArray::::new([4, 2]); + let mut a = PackedArray::new([4, 2]); - a[[0, 0]] = 1.0; - assert_eq!(a[[0, 0]], 1.0); - assert_eq!(a.entries, vec![1.0]); + a[[0, 0]] = 1; + assert_eq!(a[[0, 0]], 1); + assert_eq!(a.entries, vec![1]); assert_eq!(a.start_indices, vec![0]); assert_eq!(a.lengths, vec![1]); - a[[3, 0]] = 2.0; - assert_eq!(a[[0, 0]], 1.0); - assert_eq!(a[[3, 0]], 2.0); - assert_eq!(a.entries, vec![1.0, 2.0]); + a[[3, 0]] = 2; + assert_eq!(a[[0, 0]], 1); + assert_eq!(a[[3, 0]], 2); + assert_eq!(a.entries, vec![1, 2]); assert_eq!(a.start_indices, vec![0, 6]); assert_eq!(a.lengths, vec![1, 1]); - a[[3, 1]] = 3.0; - assert_eq!(a[[0, 0]], 1.0); - assert_eq!(a[[3, 0]], 2.0); - assert_eq!(a[[3, 1]], 3.0); - assert_eq!(a.entries, vec![1.0, 2.0, 3.0]); + a[[3, 1]] = 3; + assert_eq!(a[[0, 0]], 1); + assert_eq!(a[[3, 0]], 2); + assert_eq!(a[[3, 1]], 3); + assert_eq!(a.entries, vec![1, 2, 3]); assert_eq!(a.start_indices, vec![0, 6]); assert_eq!(a.lengths, vec![1, 2]); - a[[2, 0]] = 3.5; - assert_eq!(a[[0, 0]], 1.0); - assert_eq!(a[[3, 0]], 2.0); - assert_eq!(a[[3, 1]], 3.0); - assert_eq!(a[[2, 0]], 3.5); - assert_eq!(a.entries, vec![1.0, 3.5, 0.0, 2.0, 3.0]); + a[[2, 0]] = 9; + assert_eq!(a[[0, 0]], 1); + assert_eq!(a[[3, 0]], 2); + assert_eq!(a[[3, 1]], 3); + assert_eq!(a[[2, 0]], 9); + assert_eq!(a.entries, vec![1, 9, 0, 2, 3]); assert_eq!(a.start_indices, vec![0, 4]); assert_eq!(a.lengths, vec![1, 4]); - a[[2, 0]] = 4.0; - assert_eq!(a[[0, 0]], 1.0); - assert_eq!(a[[3, 0]], 2.0); - assert_eq!(a[[3, 1]], 3.0); - assert_eq!(a[[2, 0]], 4.0); - assert_eq!(a.entries, vec![1.0, 4.0, 0.0, 2.0, 3.0]); + a[[2, 0]] = 4; + assert_eq!(a[[0, 0]], 1); + assert_eq!(a[[3, 0]], 2); + assert_eq!(a[[3, 1]], 3); + assert_eq!(a[[2, 0]], 4); + assert_eq!(a.entries, vec![1, 4, 0, 2, 3]); assert_eq!(a.start_indices, vec![0, 4]); assert_eq!(a.lengths, vec![1, 4]); - a[[1, 0]] = 5.0; - assert_eq!(a[[0, 0]], 1.0); - assert_eq!(a[[3, 0]], 2.0); - assert_eq!(a[[3, 1]], 3.0); - assert_eq!(a[[2, 0]], 4.0); - assert_eq!(a[[1, 0]], 5.0); - assert_eq!(a.entries, vec![1.0, 0.0, 5.0, 0.0, 4.0, 0.0, 2.0, 3.0]); + a[[1, 0]] = 5; + assert_eq!(a[[0, 0]], 1); + assert_eq!(a[[3, 0]], 2); + assert_eq!(a[[3, 1]], 3); + assert_eq!(a[[2, 0]], 4); + assert_eq!(a[[1, 0]], 5); + assert_eq!(a.entries, vec![1, 0, 5, 0, 4, 0, 2, 3]); assert_eq!(a.start_indices, vec![0]); assert_eq!(a.lengths, vec![8]); } #[test] fn iter() { - let mut a = PackedArray::::new([6, 5]); + let mut a = PackedArray::new([6, 5]); a[[2, 2]] = 1; a[[2, 4]] = 2; a[[4, 1]] = 3; @@ -401,48 +401,48 @@ mod tests { assert!(array.is_empty()); // insert the first element - array[[5, 10, 10]] = 1.0; - assert_eq!(array[[5, 10, 10]], 1.0); + array[[5, 10, 10]] = 1; + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 1); assert_eq!(array.explicit_zeros(), 0); assert_eq!(array.overhead(), 2); assert!(!array.is_empty()); // insert an element after the first one - array[[8, 10, 10]] = 2.0; - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[8, 10, 10]] = 2; + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 2); assert_eq!(array.explicit_zeros(), 0); assert_eq!(array.overhead(), 4); assert!(!array.is_empty()); // insert an element before the first one - array[[1, 10, 10]] = 3.0; - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 10, 10]] = 3; + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 3); assert_eq!(array.explicit_zeros(), 0); assert_eq!(array.overhead(), 6); assert!(!array.is_empty()); - array[[1, 10, 11]] = 4.0; - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 10, 11]] = 4; + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 4); assert_eq!(array.explicit_zeros(), 0); assert_eq!(array.overhead(), 6); assert!(!array.is_empty()); - array[[1, 10, 9]] = 5.0; - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 10, 9]] = 5; + assert_eq!(array[[1, 10, 9]], 5); + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 5); assert_eq!(array.explicit_zeros(), 0); // dbg!(&array.start_indices); @@ -450,89 +450,89 @@ mod tests { assert_eq!(array.overhead(), 6); assert!(!array.is_empty()); - array[[1, 10, 0]] = 6.0; - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 10, 0]] = 6; + assert_eq!(array[[1, 10, 0]], 6); + assert_eq!(array[[1, 10, 9]], 5); + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 6); assert_eq!(array.explicit_zeros(), 0); assert_eq!(array.overhead(), 8); assert!(!array.is_empty()); - array[[1, 10, 2]] = 7.0; - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 10, 2]] = 7; + assert_eq!(array[[1, 10, 2]], 7); + assert_eq!(array[[1, 10, 0]], 6); + assert_eq!(array[[1, 10, 9]], 5); + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 7); assert_eq!(array.overhead(), 8); assert!(!array.is_empty()); // check zeros - assert_eq!(array[[1, 10, 1]], 0.0); + assert_eq!(array[[1, 10, 1]], 0); assert_eq!(array.explicit_zeros(), 1); - array[[1, 15, 2]] = 8.0; - assert_eq!(array[[1, 15, 2]], 8.0); - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 15, 2]] = 8; + assert_eq!(array[[1, 15, 2]], 8); + assert_eq!(array[[1, 10, 2]], 7); + assert_eq!(array[[1, 10, 0]], 6); + assert_eq!(array[[1, 10, 9]], 5); + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 8); assert_eq!(array.overhead(), 10); assert!(!array.is_empty()); // check zeros - assert_eq!(array[[1, 10, 1]], 0.0); + assert_eq!(array[[1, 10, 1]], 0); assert_eq!(array.explicit_zeros(), 1); - array[[1, 15, 4]] = 9.0; - assert_eq!(array[[1, 15, 4]], 9.0); - assert_eq!(array[[1, 15, 2]], 8.0); - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 15, 4]] = 9; + assert_eq!(array[[1, 15, 4]], 9); + assert_eq!(array[[1, 15, 2]], 8); + assert_eq!(array[[1, 10, 2]], 7); + assert_eq!(array[[1, 10, 0]], 6); + assert_eq!(array[[1, 10, 9]], 5); + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 9); assert_eq!(array.overhead(), 10); assert!(!array.is_empty()); // check zeros - assert_eq!(array[[1, 15, 3]], 0.0); - assert_eq!(array[[1, 10, 1]], 0.0); + assert_eq!(array[[1, 15, 3]], 0); + assert_eq!(array[[1, 10, 1]], 0); assert_eq!(array.explicit_zeros(), 2); - array[[1, 15, 0]] = 10.0; - assert_eq!(array[[1, 15, 0]], 10.0); - assert_eq!(array[[1, 15, 4]], 9.0); - assert_eq!(array[[1, 15, 2]], 8.0); - assert_eq!(array[[1, 10, 2]], 7.0); - assert_eq!(array[[1, 10, 0]], 6.0); - assert_eq!(array[[1, 10, 9]], 5.0); - assert_eq!(array[[1, 10, 11]], 4.0); - assert_eq!(array[[1, 10, 10]], 3.0); - assert_eq!(array[[8, 10, 10]], 2.0); - assert_eq!(array[[5, 10, 10]], 1.0); + array[[1, 15, 0]] = 10; + assert_eq!(array[[1, 15, 0]], 10); + assert_eq!(array[[1, 15, 4]], 9); + assert_eq!(array[[1, 15, 2]], 8); + assert_eq!(array[[1, 10, 2]], 7); + assert_eq!(array[[1, 10, 0]], 6); + assert_eq!(array[[1, 10, 9]], 5); + assert_eq!(array[[1, 10, 11]], 4); + assert_eq!(array[[1, 10, 10]], 3); + assert_eq!(array[[8, 10, 10]], 2); + assert_eq!(array[[5, 10, 10]], 1); assert_eq!(array.non_zeros(), 10); assert_eq!(array.overhead(), 10); assert!(!array.is_empty()); // check zeros - assert_eq!(array[[1, 15, 1]], 0.0); - assert_eq!(array[[1, 15, 3]], 0.0); - assert_eq!(array[[1, 10, 1]], 0.0); + assert_eq!(array[[1, 15, 1]], 0); + assert_eq!(array[[1, 15, 3]], 0); + assert_eq!(array[[1, 10, 1]], 0); assert_eq!(array.explicit_zeros(), 3); } @@ -565,9 +565,9 @@ mod tests { fn index_panic_dim0_0() { let mut array = PackedArray::new([40, 50, 50]); - array[[1, 0, 0]] = 1.0; + array[[1, 0, 0]] = 1; - assert_eq!(array[[0, 0, 0]], 0.0); + assert_eq!(array[[0, 0, 0]], 0); } #[test] @@ -575,9 +575,9 @@ mod tests { fn index_panic_dim0_1() { let mut array = PackedArray::new([40, 50, 50]); - array[[1, 0, 0]] = 1.0; + array[[1, 0, 0]] = 1; - assert_eq!(array[[2, 0, 0]], 0.0); + assert_eq!(array[[2, 0, 0]], 0); } #[test] @@ -585,9 +585,9 @@ mod tests { fn index_panic_dim1() { let mut array = PackedArray::new([40, 50, 50]); - array[[1, 0, 0]] = 1.0; + array[[1, 0, 0]] = 1; - assert_eq!(array[[1, 50, 0]], 0.0); + assert_eq!(array[[1, 50, 0]], 0); } #[test] @@ -595,9 +595,9 @@ mod tests { fn index_panic_dim2_0() { let mut array = PackedArray::new([40, 50, 50]); - array[[0, 0, 1]] = 1.0; + array[[0, 0, 1]] = 1; - assert_eq!(array[[0, 0, 0]], 0.0); + assert_eq!(array[[0, 0, 0]], 0); } #[test] @@ -605,9 +605,9 @@ mod tests { fn index_panic_dim2_1() { let mut array = PackedArray::new([40, 50, 50]); - array[[0, 0, 1]] = 1.0; + array[[0, 0, 1]] = 1; - assert_eq!(array[[0, 0, 2]], 0.0); + assert_eq!(array[[0, 0, 2]], 0); } #[test] @@ -618,48 +618,48 @@ mod tests { assert_eq!(array.indexed_iter().next(), None); // insert an element - array[[2, 3, 4]] = 1.0; + array[[2, 3, 4]] = 1; let mut iter = array.indexed_iter(); // check iterator with one element - assert_eq!(iter.next(), Some(([2, 3, 4], 1.0))); + assert_eq!(iter.next(), Some(([2, 3, 4], 1))); assert_eq!(iter.next(), None); mem::drop(iter); // insert another element - array[[2, 3, 6]] = 2.0; + array[[2, 3, 6]] = 2; let mut iter = array.indexed_iter(); - assert_eq!(iter.next(), Some(([2, 3, 4], 1.0))); - assert_eq!(iter.next(), Some(([2, 3, 6], 2.0))); + assert_eq!(iter.next(), Some(([2, 3, 4], 1))); + assert_eq!(iter.next(), Some(([2, 3, 6], 2))); assert_eq!(iter.next(), None); mem::drop(iter); // insert yet another element - array[[4, 5, 7]] = 3.0; + array[[4, 5, 7]] = 3; let mut iter = array.indexed_iter(); - assert_eq!(iter.next(), Some(([2, 3, 4], 1.0))); - assert_eq!(iter.next(), Some(([2, 3, 6], 2.0))); - assert_eq!(iter.next(), Some(([4, 5, 7], 3.0))); + assert_eq!(iter.next(), Some(([2, 3, 4], 1))); + assert_eq!(iter.next(), Some(([2, 3, 6], 2))); + assert_eq!(iter.next(), Some(([4, 5, 7], 3))); assert_eq!(iter.next(), None); mem::drop(iter); // insert at the very first position - array[[2, 0, 0]] = 4.0; + array[[2, 0, 0]] = 4; let mut iter = array.indexed_iter(); - assert_eq!(iter.next(), Some(([2, 0, 0], 4.0))); - assert_eq!(iter.next(), Some(([2, 3, 4], 1.0))); - assert_eq!(iter.next(), Some(([2, 3, 6], 2.0))); - assert_eq!(iter.next(), Some(([4, 5, 7], 3.0))); + assert_eq!(iter.next(), Some(([2, 0, 0], 4))); + assert_eq!(iter.next(), Some(([2, 3, 4], 1))); + assert_eq!(iter.next(), Some(([2, 3, 6], 2))); + assert_eq!(iter.next(), Some(([4, 5, 7], 3))); assert_eq!(iter.next(), None); } @@ -667,9 +667,9 @@ mod tests { fn clear() { let mut array = PackedArray::new([40, 50, 50]); - array[[3, 5, 1]] = 1.0; - array[[7, 8, 9]] = 2.0; - array[[9, 1, 4]] = 3.0; + array[[3, 5, 1]] = 1; + array[[7, 8, 9]] = 2; + array[[9, 1, 4]] = 3; assert!(!array.is_empty()); assert_eq!(array.non_zeros(), 3); @@ -686,22 +686,22 @@ mod tests { fn from_ndarray() { let mut ndarray = Array3::zeros((2, 50, 50)); - ndarray[[0, 4, 3]] = 1.0; - ndarray[[0, 4, 4]] = 2.0; - ndarray[[0, 4, 6]] = 3.0; - ndarray[[0, 5, 1]] = 4.0; - ndarray[[0, 5, 7]] = 5.0; - ndarray[[1, 3, 9]] = 6.0; + ndarray[[0, 4, 3]] = 1; + ndarray[[0, 4, 4]] = 2; + ndarray[[0, 4, 6]] = 3; + ndarray[[0, 5, 1]] = 4; + ndarray[[0, 5, 7]] = 5; + ndarray[[1, 3, 9]] = 6; let array = PackedArray::from_ndarray(ndarray.view(), 3, 40); - assert_eq!(array[[3, 4, 3]], 1.0); - assert_eq!(array[[3, 4, 4]], 2.0); - assert_eq!(array[[3, 4, 5]], 0.0); - assert_eq!(array[[3, 4, 6]], 3.0); - assert_eq!(array[[3, 5, 1]], 4.0); - assert_eq!(array[[3, 5, 7]], 5.0); - assert_eq!(array[[4, 3, 9]], 6.0); + assert_eq!(array[[3, 4, 3]], 1); + assert_eq!(array[[3, 4, 4]], 2); + assert_eq!(array[[3, 4, 5]], 0); + assert_eq!(array[[3, 4, 6]], 3); + assert_eq!(array[[3, 5, 1]], 4); + assert_eq!(array[[3, 5, 7]], 5); + assert_eq!(array[[4, 3, 9]], 6); assert_eq!(array.explicit_zeros(), 1); } From 0e30f002cf38db87894cab53919c38e539480b19 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 9 Aug 2024 09:08:47 +0200 Subject: [PATCH 042/213] Fix documentation links --- pineappl/src/boc.rs | 2 ++ pineappl/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 8dd86c4e..342e9339 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -1,5 +1,7 @@ //! Module containing structures for the 3 dimensions of a [`Grid`]: bins, [`Order`] and channels //! (`boc`). +//! +//! [`Grid`]: super::grid::Grid use float_cmp::approx_eq; use itertools::Itertools; diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index ba3c62fe..4ffebba4 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -20,7 +20,7 @@ //! [`Grid::orders()`]: grid::Grid::orders //! [`Subgrid`]: subgrid::Subgrid //! [`SubgridEnum`]: subgrid::SubgridEnum -//! [`Order`]: order::Order +//! [`Order`]: boc::Order //! //! ## Metadata //! From 34c14ec34ee19740fe2018b8679bf3b672a5979d Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 9 Aug 2024 17:15:26 +0200 Subject: [PATCH 043/213] Prepare code for fragmentation scale variation --- pineappl/src/fk_table.rs | 2 +- pineappl/src/grid.rs | 10 ++-- pineappl/tests/drell_yan_lo.rs | 16 +++---- pineappl_capi/src/lib.rs | 4 +- pineappl_cli/src/evolve.rs | 7 ++- pineappl_cli/src/helpers.rs | 87 +++++++++++++++++++++++++++------- pineappl_cli/src/import.rs | 7 +-- pineappl_cli/src/uncert.rs | 17 +++---- pineappl_cli/tests/evolve.rs | 1 + pineappl_cli/tests/import.rs | 2 +- pineappl_cli/tests/uncert.rs | 29 ++++++------ 11 files changed, 123 insertions(+), 59 deletions(-) diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 19119339..344cca67 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -271,7 +271,7 @@ impl FkTable { channel_mask: &[bool], ) -> Vec { self.grid - .convolve(lumi_cache, &[], bin_indices, channel_mask, &[(1.0, 1.0)]) + .convolve(lumi_cache, &[], bin_indices, channel_mask, &[(1.0, 1.0, 1.0)]) } /// Set a metadata key-value pair. diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index c232056e..3a29e0b5 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -275,9 +275,11 @@ impl Grid { order_mask: &[bool], bin_indices: &[usize], channel_mask: &[bool], - xi: &[(f64, f64)], + xi: &[(f64, f64, f64)], ) -> Vec { - lumi_cache.setup(self, xi).unwrap(); + assert!(xi.iter().all(|&(_, _, xia)| approx_eq!(f64, xia, 1.0, ulps = 2))); + let xi = xi.iter().map(|&(xir, xif, _)| (xir, xif)).collect::>(); + lumi_cache.setup(self, &xi).unwrap(); let bin_indices = if bin_indices.is_empty() { (0..self.bin_info().bins()).collect() @@ -365,9 +367,9 @@ impl Grid { ord: usize, bin: usize, channel: usize, - xir: f64, - xif: f64, + (xir, xif, xia): (f64, f64, f64), ) -> Array3 { + assert_eq!(xia, 1.0); lumi_cache.setup(self, &[(xir, xif)]).unwrap(); let normalizations = self.bin_info().normalizations(); diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index f62a6988..963af953 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -333,7 +333,7 @@ fn perform_grid_tests( // TEST 5: `convolve` let mut lumi_cache = LumiCache::with_one(2212, &mut xfx, &mut alphas); - let bins = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0)]); + let bins = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins.iter().zip(reference.iter()) { assert_approx_eq!(f64, *result, *reference, ulps = 16); @@ -344,7 +344,7 @@ fn perform_grid_tests( let mut xfx2 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas2 = |_| 0.0; let mut lumi_cache2 = LumiCache::with_two(2212, &mut xfx1, 2212, &mut xfx2, &mut alphas2); - let bins2 = grid.convolve(&mut lumi_cache2, &[], &[], &[], &[(1.0, 1.0)]); + let bins2 = grid.convolve(&mut lumi_cache2, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins2.iter().zip(reference.iter()) { assert_approx_eq!(f64, *result, *reference, ulps = 16); @@ -358,7 +358,7 @@ fn perform_grid_tests( .map(|bin| { (0..grid.channels().len()) .map(|channel| { - grid.convolve_subgrid(&mut lumi_cache, 0, bin, channel, 1.0, 1.0) + grid.convolve_subgrid(&mut lumi_cache, 0, bin, channel, (1.0, 1.0, 1.0)) .sum() }) .sum() @@ -383,7 +383,7 @@ fn perform_grid_tests( .map(|bin| { (0..grid.channels().len()) .map(|channel| { - grid.convolve_subgrid(&mut lumi_cache, 0, bin, channel, 1.0, 1.0) + grid.convolve_subgrid(&mut lumi_cache, 0, bin, channel, (1.0, 1.0, 1.0)) .sum() }) .sum() @@ -394,7 +394,7 @@ fn perform_grid_tests( assert_approx_eq!(f64, *result, *reference_after_ssd, ulps = 24); } - let bins = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0)]); + let bins = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference_after_ssd) in bins.iter().zip(reference_after_ssd.iter()) { assert_approx_eq!(f64, *result, *reference_after_ssd, ulps = 24); @@ -427,7 +427,7 @@ fn perform_grid_tests( grid.merge_bins(bin..bin + 2)?; } - let merged2 = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0)]); + let merged2 = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference_after_ssd) in merged2.iter().zip( reference_after_ssd @@ -442,7 +442,7 @@ fn perform_grid_tests( // delete a few bins from the start grid.delete_bins(&[0, 1]); - let deleted = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0)]); + let deleted = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); assert_eq!(deleted.len(), 10); @@ -458,7 +458,7 @@ fn perform_grid_tests( // delete a few bins from the ending grid.delete_bins(&[8, 9]); - let deleted2 = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0)]); + let deleted2 = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); assert_eq!(deleted2.len(), 8); diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 5905e118..75575c05 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -425,7 +425,7 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_one( &order_mask, &[], &channel_mask, - &[(xi_ren, xi_fac)], + &[(xi_ren, xi_fac, 1.0)], )); } @@ -487,7 +487,7 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_two( &order_mask, &[], &channel_mask, - &[(xi_ren, xi_fac)], + &[(xi_ren, xi_fac, 1.0)], )); } diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index 5b084ea5..0cbd6cb6 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -556,6 +556,9 @@ pub struct Opts { /// Rescale the factorization scale with this factor. #[arg(default_value_t = 1.0, long)] xif: f64, + /// Rescale the fragmentation scale with this factor. + #[arg(default_value_t = 1.0, long)] + xia: f64, #[arg(hide = true, long)] use_old_evolve: bool, } @@ -572,7 +575,7 @@ impl Subcommand for Opts { &self.orders, &[], &[], - &[(self.xir, self.xif)], + &[(self.xir, self.xif, self.xia)], ConvoluteMode::Normal, cfg, ); @@ -596,7 +599,7 @@ impl Subcommand for Opts { &[], &[], &[], - &[(1.0, 1.0)], + &[(1.0, 1.0, 1.0)], ConvoluteMode::Normal, cfg, ); diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index a50912aa..9ec9917b 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -129,16 +129,58 @@ pub fn create_table() -> Table { table } -pub const SCALES_VECTOR: [(f64, f64); 9] = [ - (1.0, 1.0), - (2.0, 2.0), - (0.5, 0.5), - (2.0, 1.0), - (1.0, 2.0), - (0.5, 1.0), - (1.0, 0.5), - (2.0, 0.5), - (0.5, 2.0), +pub const SCALES_VECTOR_REN_FAC: [(f64, f64, f64); 9] = [ + (1.0, 1.0, 1.0), + (2.0, 2.0, 1.0), + (0.5, 0.5, 1.0), + (2.0, 1.0, 1.0), + (1.0, 2.0, 1.0), + (0.5, 1.0, 1.0), + (1.0, 0.5, 1.0), + (2.0, 0.5, 1.0), + (0.5, 2.0, 1.0), +]; + +// const SCALES_VECTOR_REN_FRG: [(f64, f64, f64); 9] = [ +// (1.0, 1.0, 1.0), +// (2.0, 1.0, 2.0), +// (0.5, 1.0, 0.5), +// (2.0, 1.0, 1.0), +// (1.0, 1.0, 2.0), +// (0.5, 1.0, 1.0), +// (1.0, 1.0, 0.5), +// (2.0, 1.0, 0.5), +// (0.5, 1.0, 2.0), +// ]; + +const SCALES_VECTOR_27: [(f64, f64, f64); 27] = [ + (1.0, 1.0, 1.0), + (2.0, 2.0, 2.0), + (0.5, 0.5, 0.5), + (0.5, 0.5, 1.0), + (0.5, 1.0, 0.5), + (0.5, 1.0, 1.0), + (0.5, 1.0, 2.0), + (1.0, 0.5, 0.5), + (1.0, 0.5, 1.0), + (1.0, 1.0, 0.5), + (1.0, 1.0, 2.0), + (1.0, 2.0, 1.0), + (1.0, 2.0, 2.0), + (2.0, 1.0, 0.5), + (2.0, 1.0, 1.0), + (2.0, 1.0, 2.0), + (2.0, 2.0, 1.0), + (2.0, 0.5, 0.5), + (0.5, 2.0, 0.5), + (1.0, 2.0, 0.5), + (2.0, 2.0, 0.5), + (2.0, 0.5, 1.0), + (0.5, 2.0, 1.0), + (0.5, 0.5, 2.0), + (1.0, 0.5, 2.0), + (2.0, 0.5, 2.0), + (0.5, 2.0, 2.0), ]; pub fn labels_and_units(grid: &Grid, integrated: bool) -> (Vec<(String, &str)>, &str, &str) { @@ -184,7 +226,7 @@ pub fn convolve_scales( orders: &[(u32, u32)], bins: &[usize], channels: &[bool], - scales: &[(f64, f64)], + scales: &[(f64, f64, f64)], mode: ConvoluteMode, cfg: &GlobalConfiguration, ) -> Vec { @@ -230,7 +272,7 @@ pub fn convolve_scales( let mut cache = LumiCache::with_one(pdg_id, &mut fun, &mut alphas); - grid.convolve(&mut cache, &orders, bins, channels, scales) + grid.convolve(&mut cache, &orders, bins, channels, &scales) } [fun1, fun2] => { let pdg_id1 = fun1 @@ -277,7 +319,7 @@ pub fn convolve_scales( let mut cache = LumiCache::with_two(pdg_id1, &mut fun1, pdg_id2, &mut fun2, &mut alphas); - grid.convolve(&mut cache, &orders, bins, channels, scales) + grid.convolve(&mut cache, &orders, bins, channels, &scales) } _ => unimplemented!(), }; @@ -322,6 +364,19 @@ pub fn convolve_scales( } } +pub fn scales_vector(_grid: &Grid, scales: usize) -> &[(f64, f64, f64)] { + match scales { + 1 => &SCALES_VECTOR_27[0..1], + 3 => &SCALES_VECTOR_27[0..3], + // TODO: fix 7 and 9 for cases where there is a fragmentation scale + 7 => &SCALES_VECTOR_REN_FAC[0..7], + 9 => &SCALES_VECTOR_REN_FAC[..], + 17 => &SCALES_VECTOR_27[0..17], + 27 => &SCALES_VECTOR_27[..], + _ => unreachable!(), + } +} + pub fn convolve( grid: &Grid, conv_funs: &mut [Pdf], @@ -338,7 +393,7 @@ pub fn convolve( orders, bins, lumis, - &SCALES_VECTOR[0..scales], + scales_vector(grid, scales), mode, cfg, ) @@ -398,7 +453,7 @@ pub fn convolve_subgrid( let mut cache = LumiCache::with_one(pdg_id, &mut fun, &mut alphas); - grid.convolve_subgrid(&mut cache, order, bin, lumi, 1.0, 1.0) + grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)) } [fun1, fun2] => { let pdg_id1 = fun1 @@ -445,7 +500,7 @@ pub fn convolve_subgrid( let mut cache = LumiCache::with_two(pdg_id1, &mut fun1, pdg_id2, &mut fun2, &mut alphas); - grid.convolve_subgrid(&mut cache, order, bin, lumi, 1.0, 1.0) + grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)) } _ => unimplemented!(), } diff --git a/pineappl_cli/src/import.rs b/pineappl_cli/src/import.rs index e349640b..e16bcafe 100644 --- a/pineappl_cli/src/import.rs +++ b/pineappl_cli/src/import.rs @@ -91,10 +91,11 @@ fn convert_fastnlo( 1 }; - let unpermuted_results: Vec<_> = helpers::SCALES_VECTOR[0..scales] + // fastNLO does not support a fragmentation scale + let unpermuted_results: Vec<_> = helpers::SCALES_VECTOR_REN_FAC[0..scales] .iter() - .map(|&(mur, muf)| { - if !reader.as_mut().SetScaleFactorsMuRMuF(mur, muf) { + .map(|&(xir, xif, _)| { + if !reader.as_mut().SetScaleFactorsMuRMuF(xir, xif) { return None; } reader.as_mut().CalcCrossSection(); diff --git a/pineappl_cli/src/uncert.rs b/pineappl_cli/src/uncert.rs index ffe14dc6..149faeec 100644 --- a/pineappl_cli/src/uncert.rs +++ b/pineappl_cli/src/uncert.rs @@ -30,7 +30,7 @@ struct Group { long, require_equals = true, value_name = "SCALES", - value_parser = PossibleValuesParser::new(["3", "7", "9"]).try_map(|s| s.parse::()) + value_parser = PossibleValuesParser::new(["3", "7", "9", "17", "27"]).try_map(|s| s.parse::()) )] scale_abs: Option, /// Calculate scale uncertainties using the covariance method. @@ -40,17 +40,17 @@ struct Group { long, require_equals = true, value_name = "SCALES", - value_parser = PossibleValuesParser::new(["3", "7", "9"]).try_map(|s| s.parse::()) + value_parser = PossibleValuesParser::new(["3", "7", "9", "17", "27"]).try_map(|s| s.parse::()) )] scale_cov: Option, - /// Calculate the envelope of results where renormalization and factorization scales varied. + /// Calculate the envelope of results where renormalization, factorization and fragmentation scales are varied. #[arg( default_missing_value = "7", num_args = 0..=1, long, require_equals = true, value_name = "SCALES", - value_parser = PossibleValuesParser::new(["3", "7", "9"]).try_map(|s| s.parse::()) + value_parser = PossibleValuesParser::new(["3", "7", "9", "17", "27"]).try_map(|s| s.parse::()) )] scale_env: Option, } @@ -157,13 +157,14 @@ impl Subcommand for Opts { .map(|&x| usize::from(x)) .max() .unwrap_or(1); - let scale_results = helpers::convolve( + let scale_tuples = helpers::scales_vector(&grid, scales_max); + let scale_results = helpers::convolve_scales( &grid, &mut conv_funs, &self.orders, &[], &[], - scales_max, + scale_tuples, if self.integrated { ConvoluteMode::Integrated } else { @@ -191,8 +192,8 @@ impl Subcommand for Opts { } if let Some(scales) = self.group.scale_abs { - for scale in &helpers::SCALES_VECTOR[0..scales.into()] { - title.add_cell(cell!(c->format!("(r={},f={})\n[{}]", scale.0, scale.1, y_unit))); + for (xir, xif, xia) in &scale_tuples[0..scales.into()] { + title.add_cell(cell!(c->format!("{xir},{xif},{xia}\n(r,f,a)\n[{y_unit}]"))); } } diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index fcf25ad3..eeb5edec 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -21,6 +21,7 @@ Options: -o, --orders Select which orders to evolve --xir Rescale the renormalization scale with this factor [default: 1] --xif Rescale the factorization scale with this factor [default: 1] + --xia Rescale the fragmentation scale with this factor [default: 1] -h, --help Print help "; diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 50447a7c..b3f48156 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -876,7 +876,7 @@ fn import_hadronic_fktable() { let mut xfx = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas = |_| 0.0; let mut lumi_cache = LumiCache::with_one(2212, &mut xfx, &mut alphas); - let results = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0)]); + let results = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); let mut fk_table = FkTable::try_from(grid).unwrap(); let table = fk_table.table(); diff --git a/pineappl_cli/tests/uncert.rs b/pineappl_cli/tests/uncert.rs index c2e1da4d..93c482c4 100644 --- a/pineappl_cli/tests/uncert.rs +++ b/pineappl_cli/tests/uncert.rs @@ -12,9 +12,9 @@ Arguments: Options: --conv-fun[=] Calculate convolution function uncertainties - --scale-abs[=] Show absolute numbers of the scale-varied results [possible values: 3, 7, 9] - --scale-cov[=] Calculate scale uncertainties using the covariance method [possible values: 3, 7, 9] - --scale-env[=] Calculate the envelope of results where renormalization and factorization scales varied [possible values: 3, 7, 9] + --scale-abs[=] Show absolute numbers of the scale-varied results [possible values: 3, 7, 9, 17, 27] + --scale-cov[=] Calculate scale uncertainties using the covariance method [possible values: 3, 7, 9, 17, 27] + --scale-env[=] Calculate the envelope of results where renormalization, factorization and fragmentation scales are varied [possible values: 3, 7, 9, 17, 27] --cl Confidence level in per cent, for convolution function uncertainties [default: 68.26894921370858] -i, --integrated Show integrated numbers (without bin widths) instead of differential ones -o, --orders Select orders manually @@ -78,17 +78,18 @@ const ORDERS_A2_AS1A2_STR: &str = "b etal dsig/detal NNPDF31_nlo_as_0118 "; const SCALE_ABS_STR: &str = -"b etal dsig/detal (r=1,f=1) (r=2,f=2) (r=0.5,f=0.5) (r=2,f=1) (r=1,f=2) (r=0.5,f=1) (r=1,f=0.5) - [] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] --+----+----+-----------+-----------+-----------+-------------+-----------+-----------+-----------+----------- -0 2 2.25 7.5459110e2 7.5459110e2 7.6745431e2 7.4296019e2 7.4384068e2 7.7529764e2 7.6796494e2 7.2595107e2 -1 2.25 2.5 6.9028342e2 6.9028342e2 7.0221920e2 6.7923774e2 6.8058382e2 7.0957480e2 7.0235002e2 6.6417441e2 -2 2.5 2.75 6.0025198e2 6.0025198e2 6.1056383e2 5.9046454e2 5.9160658e2 6.1750966e2 6.1100712e2 5.7747295e2 -3 2.75 3 4.8552235e2 4.8552235e2 4.9366919e2 4.7761552e2 4.7841022e2 4.9966237e2 4.9437007e2 4.6723687e2 -4 3 3.25 3.6195456e2 3.6195456e2 3.6783089e2 3.5611822e2 3.5652780e2 3.7261436e2 3.6870561e2 3.4843600e2 -5 3.25 3.5 2.4586691e2 2.4586691e2 2.4967698e2 2.4198770e2 2.4207028e2 2.5316566e2 2.5059003e2 2.3677625e2 -6 3.5 4 1.1586851e2 1.1586851e2 1.1746280e2 1.1418227e2 1.1396174e2 1.1930157e2 1.1824058e2 1.1166942e2 -7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 2.7002241e1 2.8306905e1 2.8157972e1 2.6562471e1 +"b etal dsig/detal 1,1,1 2,2,1 0.5,0.5,1 2,1,1 1,2,1 0.5,1,1 1,0.5,1 + [] [pb] (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) + [pb] [pb] [pb] [pb] [pb] [pb] [pb] +-+----+----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+----------- +0 2 2.25 7.5459110e2 7.5459110e2 7.6745431e2 7.4296019e2 7.4384068e2 7.7529764e2 7.6796494e2 7.2595107e2 +1 2.25 2.5 6.9028342e2 6.9028342e2 7.0221920e2 6.7923774e2 6.8058382e2 7.0957480e2 7.0235002e2 6.6417441e2 +2 2.5 2.75 6.0025198e2 6.0025198e2 6.1056383e2 5.9046454e2 5.9160658e2 6.1750966e2 6.1100712e2 5.7747295e2 +3 2.75 3 4.8552235e2 4.8552235e2 4.9366919e2 4.7761552e2 4.7841022e2 4.9966237e2 4.9437007e2 4.6723687e2 +4 3 3.25 3.6195456e2 3.6195456e2 3.6783089e2 3.5611822e2 3.5652780e2 3.7261436e2 3.6870561e2 3.4843600e2 +5 3.25 3.5 2.4586691e2 2.4586691e2 2.4967698e2 2.4198770e2 2.4207028e2 2.5316566e2 2.5059003e2 2.3677625e2 +6 3.5 4 1.1586851e2 1.1586851e2 1.1746280e2 1.1418227e2 1.1396174e2 1.1930157e2 1.1824058e2 1.1166942e2 +7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 2.7002241e1 2.8306905e1 2.8157972e1 2.6562471e1 "; const SCALE_COV_STR: &str = "b etal dsig/detal 7pt scale (cov) From df2cb576d0b6b53dcf5eeec0d7f1e0038e9e0420 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 16 Aug 2024 15:08:56 +0200 Subject: [PATCH 044/213] Replace `Ntuple` with slice --- pineappl/src/empty_subgrid.rs | 10 +- pineappl/src/fk_table.rs | 9 +- pineappl/src/grid.rs | 111 +++++---------------- pineappl/src/lagrange_subgrid.rs | 164 ++++++------------------------- pineappl/src/packed_subgrid.rs | 3 +- pineappl/src/subgrid.rs | 3 +- pineappl/tests/drell_yan_lo.rs | 32 ++---- pineappl_capi/src/lib.rs | 20 ++-- 8 files changed, 78 insertions(+), 274 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 79640e65..a330d05e 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -1,6 +1,5 @@ //! TODO -use super::grid::Ntuple; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -21,7 +20,7 @@ impl Subgrid for EmptySubgridV1 { 0.0 } - fn fill(&mut self, _: &Ntuple) { + fn fill(&mut self, _: &[f64], _: f64) { panic!("EmptySubgridV1 doesn't support the fill operation"); } @@ -105,12 +104,7 @@ mod tests { #[should_panic(expected = "EmptySubgridV1 doesn't support the fill operation")] fn fill() { let mut subgrid = EmptySubgridV1; - subgrid.fill(&Ntuple { - x1: 0.0, - x2: 0.0, - q2: 0.0, - weight: 0.0, - }); + subgrid.fill(&[0.0; 4], 0.0); } #[test] diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 344cca67..5d146746 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -270,8 +270,13 @@ impl FkTable { bin_indices: &[usize], channel_mask: &[bool], ) -> Vec { - self.grid - .convolve(lumi_cache, &[], bin_indices, channel_mask, &[(1.0, 1.0, 1.0)]) + self.grid.convolve( + lumi_cache, + &[], + bin_indices, + channel_mask, + &[(1.0, 1.0, 1.0)], + ) } /// Set a metadata key-value pair. diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 3a29e0b5..88f0bbad 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -26,21 +26,6 @@ use std::mem; use std::ops::Range; use thiserror::Error; -/// This structure represents a position (`x1`, `x2`, `q2`) in a `Subgrid` together with a -/// corresponding `weight`. The type `W` can either be a `f64` or `()`, which is used when multiple -/// weights should be signaled. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct Ntuple { - /// Momentum fraction of the first parton. - pub x1: f64, - /// Momentum fraction of the second parton. - pub x2: f64, - /// Squared scale. - pub q2: f64, - /// Weight of this entry. - pub weight: W, -} - /// Error returned when merging two grids fails. #[derive(Debug, Error)] pub enum GridError { @@ -277,8 +262,13 @@ impl Grid { channel_mask: &[bool], xi: &[(f64, f64, f64)], ) -> Vec { - assert!(xi.iter().all(|&(_, _, xia)| approx_eq!(f64, xia, 1.0, ulps = 2))); - let xi = xi.iter().map(|&(xir, xif, _)| (xir, xif)).collect::>(); + assert!(xi + .iter() + .all(|&(_, _, xia)| approx_eq!(f64, xia, 1.0, ulps = 2))); + let xi = xi + .iter() + .map(|&(xir, xif, _)| (xir, xif)) + .collect::>(); lumi_cache.setup(self, &xi).unwrap(); let bin_indices = if bin_indices.is_empty() { @@ -423,7 +413,14 @@ impl Grid { /// # Panics /// /// TODO - pub fn fill(&mut self, order: usize, observable: f64, channel: usize, ntuple: &Ntuple) { + pub fn fill( + &mut self, + order: usize, + observable: f64, + channel: usize, + ntuple: &[f64], + weight: f64, + ) { if let Some(bin) = self.bin_limits.index(observable) { let subgrid = &mut self.subgrids[[order, bin, channel]]; if let SubgridEnum::EmptySubgridV1(_) = subgrid { @@ -431,7 +428,7 @@ impl Grid { *subgrid = mmv3.subgrid_template.clone_empty(); } - subgrid.fill(ntuple); + subgrid.fill(ntuple, weight); } } @@ -674,31 +671,6 @@ impl Grid { Ok(()) } - /// Fills the grid with events for the parton momentum fractions `x1` and `x2`, the scale `q2`, - /// and the `order` and `observable`. The events are stored in `weights` and their ordering - /// corresponds to the ordering of [`Grid::channels`]. - pub fn fill_all( - &mut self, - order: usize, - observable: f64, - ntuple: &Ntuple<()>, - weights: &[f64], - ) { - for (channel, weight) in weights.iter().enumerate() { - self.fill( - order, - observable, - channel, - &Ntuple { - x1: ntuple.x1, - x2: ntuple.x2, - q2: ntuple.q2, - weight: *weight, - }, - ); - } - } - /// Return the channels for this `Grid`. #[must_use] pub fn channels(&self) -> &[Channel] { @@ -1925,28 +1897,10 @@ mod tests { SubgridParams::default(), ); - other.fill_all( - 0, - 0.1, - &Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: (), - }, - &[1.0, 2.0], - ); - other.fill_all( - 1, - 0.1, - &Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: (), - }, - &[1.0, 2.0], - ); + other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 1.0); + other.fill(0, 0.1, 1, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); + other.fill(1, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 1.0); + other.fill(1, 0.1, 1, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); // merge with four non-empty subgrids grid.merge(other).unwrap(); @@ -1982,17 +1936,7 @@ mod tests { ); // fill the photon-photon entry - other.fill( - 0, - 0.1, - 0, - &Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: 3.0, - }, - ); + other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 3.0); grid.merge(other).unwrap(); @@ -2030,17 +1974,8 @@ mod tests { SubgridParams::default(), ); - other.fill_all( - 0, - 0.1, - &Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: (), - }, - &[2.0, 3.0], - ); + other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); + other.fill(0, 0.1, 1, &[0.1, 0.2, 90.0_f64.powi(2)], 3.0); grid.merge(other).unwrap(); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index ba6642bb..fd1ad051 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -1,7 +1,6 @@ //! Module containing the Lagrange-interpolation subgrid. use super::convert::{f64_from_usize, usize_from_f64}; -use super::grid::Ntuple; use super::subgrid::{ ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, }; @@ -190,18 +189,21 @@ impl Subgrid for LagrangeSubgridV2 { }) } - fn fill(&mut self, ntuple: &Ntuple) { - if ntuple.weight == 0.0 { + fn fill(&mut self, ntuple: &[f64], weight: f64) { + if weight == 0.0 { return; } - let y1 = fy(ntuple.x1); - let y2 = fy(ntuple.x2); - let tau = ftau(ntuple.q2); + let &[x1, x2, q2] = ntuple else { + unreachable!(); + }; + let y1 = fy(x1); + let y2 = fy(x2); + let tau = ftau(q2); if self.static_q2 == 0.0 { - self.static_q2 = ntuple.q2; - } else if (self.static_q2 != -1.0) && (self.static_q2 != ntuple.q2) { + self.static_q2 = q2; + } else if (self.static_q2 != -1.0) && (self.static_q2 != q2) { self.static_q2 = -1.0; } @@ -240,15 +242,8 @@ impl Subgrid for LagrangeSubgridV2 { let u_tau = (tau - self.gettau(k3)) / self.deltatau(); let factor = 1.0 - / (if self.reweight1 { - weightfun(ntuple.x1) - } else { - 1.0 - } * if self.reweight2 { - weightfun(ntuple.x2) - } else { - 1.0 - }); + / (if self.reweight1 { weightfun(x1) } else { 1.0 } + * if self.reweight2 { weightfun(x2) } else { 1.0 }); let size = self.tauorder + 1; let ny1 = self.ny1; @@ -266,7 +261,7 @@ impl Subgrid for LagrangeSubgridV2 { for (i1, fi1i1) in fi1.iter().enumerate() { for (i2, fi2i2) in fi2.iter().enumerate() { - let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * ntuple.weight; + let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * weight; let grid = self .grid @@ -466,30 +461,10 @@ mod tests { use float_cmp::assert_approx_eq; fn test_q2_slice_methods(mut grid: G) -> G { - grid.fill(&Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid.fill(&Ntuple { - x1: 0.9, - x2: 0.1, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid.fill(&Ntuple { - x1: 0.009, - x2: 0.01, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid.fill(&Ntuple { - x1: 0.009, - x2: 0.5, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); + grid.fill(&[0.1, 0.2, 90.0_f64.powi(2)], 1.0); + grid.fill(&[0.9, 0.1, 90.0_f64.powi(2)], 1.0); + grid.fill(&[0.009, 0.01, 90.0_f64.powi(2)], 1.0); + grid.fill(&[0.009, 0.5, 90.0_f64.powi(2)], 1.0); // the grid must not be empty assert!(!grid.is_empty()); @@ -516,30 +491,10 @@ mod tests { where SubgridEnum: From, { - grid1.fill(&Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid1.fill(&Ntuple { - x1: 0.9, - x2: 0.1, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid1.fill(&Ntuple { - x1: 0.009, - x2: 0.01, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid1.fill(&Ntuple { - x1: 0.009, - x2: 0.5, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); + grid1.fill(&[0.1, 0.2, 90.0_f64.powi(2)], 1.0); + grid1.fill(&[0.9, 0.1, 90.0_f64.powi(2)], 1.0); + grid1.fill(&[0.009, 0.01, 90.0_f64.powi(2)], 1.0); + grid1.fill(&[0.009, 0.5, 90.0_f64.powi(2)], 1.0); assert!(!grid1.is_empty()); assert!(grid2.is_empty()); @@ -559,30 +514,10 @@ mod tests { assert_approx_eq!(f64, reference, merged, ulps = 8); - grid3.fill(&Ntuple { - x1: 0.1, - x2: 0.2, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid3.fill(&Ntuple { - x1: 0.9, - x2: 0.1, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid3.fill(&Ntuple { - x1: 0.009, - x2: 0.01, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); - grid3.fill(&Ntuple { - x1: 0.009, - x2: 0.5, - q2: 90.0_f64.powi(2), - weight: 1.0, - }); + grid3.fill(&[0.1, 0.2, 90.0_f64.powi(2)], 1.0); + grid3.fill(&[0.9, 0.1, 90.0_f64.powi(2)], 1.0); + grid3.fill(&[0.009, 0.01, 90.0_f64.powi(2)], 1.0); + grid3.fill(&[0.009, 0.5, 90.0_f64.powi(2)], 1.0); grid2.merge(&mut grid3.into(), false); @@ -595,47 +530,17 @@ mod tests { // this following events should be skipped // q2 is too large - grid.fill(&Ntuple { - x1: 0.5, - x2: 0.5, - q2: 2e+8, - weight: 1.0, - }); + grid.fill(&[0.5, 0.5, 2e+8], 1.0); // q2 is too small - grid.fill(&Ntuple { - x1: 0.5, - x2: 0.5, - q2: 5e+1, - weight: 1.0, - }); + grid.fill(&[0.5, 0.5, 5e+1], 1.0); // x1 is too large - grid.fill(&Ntuple { - x1: 1.1, - x2: 0.5, - q2: 1e+3, - weight: 1.0, - }); + grid.fill(&[1.1, 0.5, 1e+3], 1.0); // x1 is too small - grid.fill(&Ntuple { - x1: 0.5, - x2: 1e-7, - q2: 1e+3, - weight: 1.0, - }); + grid.fill(&[0.5, 1e-7, 1e+3], 1.0); // x1 is too large - grid.fill(&Ntuple { - x1: 0.5, - x2: 1.1, - q2: 1e+3, - weight: 1.0, - }); + grid.fill(&[0.5, 1.1, 1e+3], 1.0); // x1 is too small - grid.fill(&Ntuple { - x1: 1e-7, - x2: 0.5, - q2: 1e+3, - weight: 1.0, - }); + grid.fill(&[1e-7, 0.5, 1e+3], 1.0); let x1 = grid.x1_grid(); let x2 = grid.x2_grid(); @@ -670,12 +575,7 @@ mod tests { let mut subgrid = LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); - subgrid.fill(&Ntuple { - x1: 0.5, - x2: 0.5, - q2: 1000.0, - weight: 0.0, - }); + subgrid.fill(&[0.5, 0.5, 1000.0], 0.0); assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index eeffeffe..e0c10c32 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -1,6 +1,5 @@ //! TODO -use super::grid::Ntuple; use super::packed_array::PackedArray; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; @@ -53,7 +52,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { .sum() } - fn fill(&mut self, _: &Ntuple) { + fn fill(&mut self, _: &[f64], _: f64) { panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); } diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index af6e1e21..11934f38 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,7 +1,6 @@ //! Module containing the trait `Subgrid` and supporting structs. use super::empty_subgrid::EmptySubgridV1; -use super::grid::Ntuple; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; @@ -82,7 +81,7 @@ pub trait Subgrid { /// Fills the subgrid with `weight` for the parton momentum fractions `x1` and `x2`, and the /// scale `q2`. Filling is currently only support where both renormalization and factorization /// scale have the same value. - fn fill(&mut self, ntuple: &Ntuple); + fn fill(&mut self, ntuple: &[f64], weight: f64); /// Returns true if `fill` was never called for this grid. fn is_empty(&self) -> bool; diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 963af953..c1ecd53c 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -6,7 +6,7 @@ use pineappl::bin::BinRemapper; use pineappl::boc::Order; use pineappl::channel; use pineappl::convolutions::LumiCache; -use pineappl::grid::{Grid, GridOptFlags, Ntuple}; +use pineappl::grid::{Grid, GridOptFlags}; use pineappl::subgrid::{ExtraSubgridParams, Subgrid, SubgridEnum, SubgridParams}; use rand::Rng; use rand_pcg::Pcg64; @@ -229,55 +229,35 @@ fn fill_drell_yan_lo_grid( let pto = 0; let channel = 0; - grid.fill(pto, yll.abs(), channel, &Ntuple { x1, x2, q2, weight }); + grid.fill(pto, yll.abs(), channel, &[x1, x2, q2], weight); // LO up-antiup-type channel let weight = jacobian * int_quark(s, t, u, 2.0 / 3.0, 0.5); let pto = 0; let channel = 1; - grid.fill(pto, yll.abs(), channel, &Ntuple { x1, x2, q2, weight }); + grid.fill(pto, yll.abs(), channel, &[x1, x2, q2], weight); // LO antiup-up-type channel - swap (x1 <-> x2) and (t <-> u) let weight = jacobian * int_quark(s, u, t, 2.0 / 3.0, 0.5); let pto = 0; let channel = 2; - grid.fill( - pto, - yll.abs(), - channel, - &Ntuple { - x1: x2, - x2: x1, - q2, - weight, - }, - ); + grid.fill(pto, yll.abs(), channel, &[x2, x1, q2], weight); // LO down-antidown-type channel let weight = jacobian * int_quark(s, t, u, -1.0 / 3.0, -0.5); let pto = 0; let channel = 3; - grid.fill(pto, yll.abs(), channel, &Ntuple { x1, x2, q2, weight }); + grid.fill(pto, yll.abs(), channel, &[x1, x2, q2], weight); // LO antidown-down-type channel - swap (x1 <-> x2) and (t <-> u) let weight = jacobian * int_quark(s, u, t, -1.0 / 3.0, -0.5); let pto = 0; let channel = 4; - grid.fill( - pto, - yll.abs(), - channel, - &Ntuple { - x1: x2, - x2: x1, - q2, - weight, - }, - ); + grid.fill(pto, yll.abs(), channel, &[x2, x1, q2], weight); } Ok(grid) diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 75575c05..5392905a 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -59,7 +59,7 @@ use itertools::izip; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Order}; use pineappl::convolutions::{Convolution, LumiCache}; -use pineappl::grid::{Grid, GridOptFlags, Ntuple}; +use pineappl::grid::{Grid, GridOptFlags}; use pineappl::subgrid::{ExtraSubgridParams, SubgridParams}; use std::collections::HashMap; use std::ffi::{CStr, CString}; @@ -531,7 +531,7 @@ pub unsafe extern "C" fn pineappl_grid_fill( ) { let grid = unsafe { &mut *grid }; - grid.fill(order, observable, lumi, &Ntuple { x1, x2, q2, weight }); + grid.fill(order, observable, lumi, &[x1, x2, q2], weight); } /// Fill `grid` for the given momentum fractions `x1` and `x2`, at the scale `q2` for the given @@ -555,17 +555,9 @@ pub unsafe extern "C" fn pineappl_grid_fill_all( let grid = unsafe { &mut *grid }; let weights = unsafe { slice::from_raw_parts(weights, grid.channels().len()) }; - grid.fill_all( - order, - observable, - &Ntuple { - x1, - x2, - q2, - weight: (), - }, - weights, - ); + for (channel, &weight) in weights.iter().enumerate() { + grid.fill(order, observable, channel, &[x1, x2, q2], weight); + } } /// Fill `grid` with as many points as indicated by `size`. @@ -599,7 +591,7 @@ pub unsafe extern "C" fn pineappl_grid_fill_array( for (&x1, &x2, &q2, &order, &observable, &lumi, &weight) in izip!(x1, x2, q2, orders, observables, lumis, weights) { - grid.fill(order, observable, lumi, &Ntuple { x1, x2, q2, weight }); + grid.fill(order, observable, lumi, &[x1, x2, q2], weight); } } From 5d71c36dda83a9951d953fc65a054a3217a6b074 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 16 Aug 2024 15:30:32 +0200 Subject: [PATCH 045/213] Remove `Subgrid::convolve` --- pineappl/src/empty_subgrid.rs | 11 --- pineappl/src/grid.rs | 34 +++---- pineappl/src/lagrange_subgrid.rs | 155 ------------------------------- pineappl/src/packed_subgrid.rs | 24 ----- pineappl/src/subgrid.rs | 10 -- 5 files changed, 18 insertions(+), 216 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index a330d05e..7a543d25 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -10,16 +10,6 @@ use std::iter; pub struct EmptySubgridV1; impl Subgrid for EmptySubgridV1 { - fn convolve( - &self, - _: &[f64], - _: &[f64], - _: &[Mu2], - _: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - 0.0 - } - fn fill(&mut self, _: &[f64], _: f64) { panic!("EmptySubgridV1 doesn't support the fill operation"); } @@ -81,7 +71,6 @@ mod tests { #[test] fn create_empty() { let mut subgrid = EmptySubgridV1; - assert_eq!(subgrid.convolve(&[], &[], &[], &mut |_, _, _| 0.0), 0.0,); assert!(subgrid.is_empty()); subgrid.merge(&mut EmptySubgridV1.into(), false); subgrid.scale(2.0); diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 88f0bbad..08117773 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -309,24 +309,26 @@ impl Grid { lumi_cache.set_grids(&mu2_grid, &x1_grid, &x2_grid, xir, xif); - let mut value = - subgrid.convolve(&x1_grid, &x2_grid, &mu2_grid, &mut |ix1, ix2, imu2| { - let x1 = x1_grid[ix1]; - let x2 = x2_grid[ix2]; - let mut lumi = 0.0; - - for entry in channel.entry() { - debug_assert_eq!(entry.0.len(), 2); - let xfx1 = lumi_cache.xfx1(entry.0[0], ix1, imu2); - let xfx2 = lumi_cache.xfx2(entry.0[1], ix2, imu2); - lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); - } + let mut value = 0.0; + + for ((imu2, ix1, ix2), v) in subgrid.indexed_iter() { + let x1 = x1_grid[ix1]; + let x2 = x2_grid[ix2]; + let mut lumi = 0.0; + + for entry in channel.entry() { + debug_assert_eq!(entry.0.len(), 2); + let xfx1 = lumi_cache.xfx1(entry.0[0], ix1, imu2); + let xfx2 = lumi_cache.xfx2(entry.0[1], ix2, imu2); + lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); + } - let alphas = lumi_cache.alphas(imu2); + let alphas = lumi_cache.alphas(imu2); - lumi *= alphas.powi(order.alphas.try_into().unwrap()); - lumi - }); + lumi *= alphas.powi(order.alphas.try_into().unwrap()); + + value += lumi * v; + } if order.logxir > 0 { value *= (xir * xir).ln().powi(order.logxir.try_into().unwrap()); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index fd1ad051..7eab3565 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -162,33 +162,6 @@ impl LagrangeSubgridV2 { } impl Subgrid for LagrangeSubgridV2 { - fn convolve( - &self, - x1: &[f64], - x2: &[f64], - _: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - self.grid.as_ref().map_or(0.0, |grid| { - grid.indexed_iter() - .map(|((imu2, ix1, ix2), &sigma)| { - if sigma == 0.0 { - 0.0 - } else { - let mut value = sigma * lumi(ix1, ix2, imu2 + self.itaumin); - if self.reweight1 { - value *= weightfun(x1[ix1]); - } - if self.reweight2 { - value *= weightfun(x2[ix2]); - } - value - } - }) - .sum() - }) - } - fn fill(&mut self, ntuple: &[f64], weight: f64) { if weight == 0.0 { return; @@ -458,117 +431,6 @@ impl Subgrid for LagrangeSubgridV2 { #[cfg(test)] mod tests { use super::*; - use float_cmp::assert_approx_eq; - - fn test_q2_slice_methods(mut grid: G) -> G { - grid.fill(&[0.1, 0.2, 90.0_f64.powi(2)], 1.0); - grid.fill(&[0.9, 0.1, 90.0_f64.powi(2)], 1.0); - grid.fill(&[0.009, 0.01, 90.0_f64.powi(2)], 1.0); - grid.fill(&[0.009, 0.5, 90.0_f64.powi(2)], 1.0); - - // the grid must not be empty - assert!(!grid.is_empty()); - - let x1 = grid.x1_grid(); - let x2 = grid.x2_grid(); - let mu2 = grid.mu2_grid(); - - let reference = grid.convolve(&x1, &x2, &mu2, &mut |ix1, ix2, _| 1.0 / (x1[ix1] * x2[ix2])); - - let mut test = 0.0; - - // check `reference` against manually calculated result from q2 slices - for ((_, ix1, ix2), value) in grid.indexed_iter() { - test += value / (x1[ix1] * x2[ix2]); - } - - assert_approx_eq!(f64, test, reference, ulps = 8); - - grid - } - - fn test_merge_method(mut grid1: G, mut grid2: G, mut grid3: G) - where - SubgridEnum: From, - { - grid1.fill(&[0.1, 0.2, 90.0_f64.powi(2)], 1.0); - grid1.fill(&[0.9, 0.1, 90.0_f64.powi(2)], 1.0); - grid1.fill(&[0.009, 0.01, 90.0_f64.powi(2)], 1.0); - grid1.fill(&[0.009, 0.5, 90.0_f64.powi(2)], 1.0); - - assert!(!grid1.is_empty()); - assert!(grid2.is_empty()); - - let x1 = grid1.x1_grid().into_owned(); - let x2 = grid1.x2_grid().into_owned(); - let mu2 = grid1.mu2_grid().into_owned(); - - let reference = - grid1.convolve(&x1, &x2, &mu2, &mut |ix1, ix2, _| 1.0 / (x1[ix1] * x2[ix2])); - - // merge filled grid into empty one - grid2.merge(&mut grid1.into(), false); - assert!(!grid2.is_empty()); - - let merged = grid2.convolve(&x1, &x2, &mu2, &mut |ix1, ix2, _| 1.0 / (x1[ix1] * x2[ix2])); - - assert_approx_eq!(f64, reference, merged, ulps = 8); - - grid3.fill(&[0.1, 0.2, 90.0_f64.powi(2)], 1.0); - grid3.fill(&[0.9, 0.1, 90.0_f64.powi(2)], 1.0); - grid3.fill(&[0.009, 0.01, 90.0_f64.powi(2)], 1.0); - grid3.fill(&[0.009, 0.5, 90.0_f64.powi(2)], 1.0); - - grid2.merge(&mut grid3.into(), false); - - let merged = grid2.convolve(&x1, &x2, &mu2, &mut |ix1, ix2, _| 1.0 / (x1[ix1] * x2[ix2])); - - assert_approx_eq!(f64, 2.0 * reference, merged, ulps = 8); - } - - fn test_empty_subgrid(mut grid: G) { - // this following events should be skipped - - // q2 is too large - grid.fill(&[0.5, 0.5, 2e+8], 1.0); - // q2 is too small - grid.fill(&[0.5, 0.5, 5e+1], 1.0); - // x1 is too large - grid.fill(&[1.1, 0.5, 1e+3], 1.0); - // x1 is too small - grid.fill(&[0.5, 1e-7, 1e+3], 1.0); - // x1 is too large - grid.fill(&[0.5, 1.1, 1e+3], 1.0); - // x1 is too small - grid.fill(&[1e-7, 0.5, 1e+3], 1.0); - - let x1 = grid.x1_grid(); - let x2 = grid.x2_grid(); - let mu2 = grid.mu2_grid(); - - let result = grid.convolve(&x1, &x2, &mu2, &mut |_, _, _| 1.0); - - assert_eq!(result, 0.0); - } - - #[test] - fn q2_slice_v2() { - let subgrid = test_q2_slice_methods(LagrangeSubgridV2::new( - &SubgridParams::default(), - &ExtraSubgridParams::default(), - )); - - assert_eq!( - subgrid.stats(), - Stats { - total: 10000, - allocated: 10000, - zeros: 256, - overhead: 0, - bytes_per_value: 8 - } - ); - } #[test] fn fill_zero_v2() { @@ -580,21 +442,4 @@ mod tests { assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); } - - #[test] - fn merge_dense_v2() { - test_merge_method( - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()), - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()), - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()), - ); - } - - #[test] - fn empty_v2() { - test_empty_subgrid(LagrangeSubgridV2::new( - &SubgridParams::default(), - &ExtraSubgridParams::default(), - )); - } } diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index e0c10c32..8c163be0 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -39,19 +39,6 @@ impl PackedQ1X2SubgridV1 { } impl Subgrid for PackedQ1X2SubgridV1 { - fn convolve( - &self, - _: &[f64], - _: &[f64], - _: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64 { - self.array - .indexed_iter() - .map(|([imu2, ix1, ix2], sigma)| sigma * lumi(ix1, ix2, imu2)) - .sum() - } - fn fill(&mut self, _: &[f64], _: f64) { panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); } @@ -312,12 +299,6 @@ mod tests { assert_eq!(grid1.indexed_iter().nth(2), Some(((0, 4, 3), 4.0))); assert_eq!(grid1.indexed_iter().nth(3), Some(((0, 7, 1), 8.0))); - // symmetric luminosity function - let lumi = - &mut (|ix1, ix2, _| x[ix1] * x[ix2]) as &mut dyn FnMut(usize, usize, usize) -> f64; - - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 0.228515625); - // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new([1, 10, 10]), @@ -338,7 +319,6 @@ mod tests { } else { unreachable!(); } - assert_eq!(grid2.convolve(&x, &x, &mu2, lumi), 0.228515625); assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); assert_eq!(grid2.indexed_iter().nth(1), Some(((0, 2, 1), 1.0))); @@ -347,8 +327,6 @@ mod tests { grid1.merge(&mut grid2, false); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); - let mut grid1 = { let mut g = grid1.clone_empty(); g.merge(&mut grid1, false); @@ -358,10 +336,8 @@ mod tests { // the luminosity function is symmetric, so after symmetrization the result must be // unchanged grid1.symmetrize(); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 2.0 * 0.228515625); grid1.scale(2.0); - assert_eq!(grid1.convolve(&x, &x, &mu2, lumi), 4.0 * 0.228515625); assert_eq!( grid1.stats(), diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 11934f38..4330b9ba 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -68,16 +68,6 @@ pub trait Subgrid { /// return an empty slice. fn x2_grid(&self) -> Cow<[f64]>; - /// Convolute the subgrid with a luminosity function, which takes indices as arguments that - /// correspond to the entries given in the slices `x1`, `x2` and `mu2`. - fn convolve( - &self, - x1: &[f64], - x2: &[f64], - mu2: &[Mu2], - lumi: &mut dyn FnMut(usize, usize, usize) -> f64, - ) -> f64; - /// Fills the subgrid with `weight` for the parton momentum fractions `x1` and `x2`, and the /// scale `q2`. Filling is currently only support where both renormalization and factorization /// scale have the same value. From a2cd483d5879173f6dbeca4770785b10145ef11e Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 18 Aug 2024 11:14:45 +0200 Subject: [PATCH 046/213] Remove three clippy warnings --- pineappl/src/grid.rs | 5 ++--- pineappl_cli/src/helpers.rs | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 08117773..83ff711b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -86,7 +86,7 @@ impl Mmv3 { } fn default_metadata() -> BTreeMap { - [( + iter::once(( "pineappl_gitversion".to_owned(), git_version!( args = ["--always", "--dirty", "--long", "--tags"], @@ -94,8 +94,7 @@ fn default_metadata() -> BTreeMap { fallback = "unknown" ) .to_owned(), - )] - .into_iter() + )) .collect() } diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 9ec9917b..bd8c10c8 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -272,7 +272,7 @@ pub fn convolve_scales( let mut cache = LumiCache::with_one(pdg_id, &mut fun, &mut alphas); - grid.convolve(&mut cache, &orders, bins, channels, &scales) + grid.convolve(&mut cache, &orders, bins, channels, scales) } [fun1, fun2] => { let pdg_id1 = fun1 @@ -319,7 +319,7 @@ pub fn convolve_scales( let mut cache = LumiCache::with_two(pdg_id1, &mut fun1, pdg_id2, &mut fun2, &mut alphas); - grid.convolve(&mut cache, &orders, bins, channels, &scales) + grid.convolve(&mut cache, &orders, bins, channels, scales) } _ => unimplemented!(), }; From c3dc6f31a823d7ab0e39d24e8d4b43a614a079bf Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 18 Aug 2024 17:34:06 +0200 Subject: [PATCH 047/213] Improve `packed_subgrid.rs` --- pineappl/src/packed_array.rs | 11 +++ pineappl/src/packed_subgrid.rs | 175 +++++++++++++++++---------------- 2 files changed, 100 insertions(+), 86 deletions(-) diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 4c205437..26949cc2 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -23,6 +23,17 @@ pub struct PackedArray { shape: Vec, } +impl Default for PackedArray { + fn default() -> Self { + Self { + entries: Vec::new(), + start_indices: Vec::new(), + lengths: Vec::new(), + shape: vec![0; D], + } + } +} + impl PackedArray { /// Constructs a new and empty `PackedArray` of shape `shape`. #[must_use] diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 8c163be0..6904c65e 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -7,7 +7,7 @@ use std::borrow::Cow; use std::mem; /// TODO -#[derive(Clone, Deserialize, Serialize)] +#[derive(Clone, Default, Deserialize, Serialize)] pub struct PackedQ1X2SubgridV1 { array: PackedArray, mu2_grid: Vec, @@ -60,89 +60,84 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - if let SubgridEnum::PackedQ1X2SubgridV1(other_grid) = other { - if self.array.is_empty() && !transpose { - mem::swap(&mut self.array, &mut other_grid.array); - } else { - let rhs_mu2 = other_grid.mu2_grid().into_owned(); - let rhs_x1 = if transpose { - other_grid.x2_grid() - } else { - other_grid.x1_grid() - }; - let rhs_x2 = if transpose { - other_grid.x1_grid() - } else { - other_grid.x2_grid() - }; - - if (self.mu2_grid != rhs_mu2) - || (self.x1_grid() != rhs_x1) - || (self.x2_grid() != rhs_x2) - { - let mut mu2_grid = self.mu2_grid.clone(); - let mut x1_grid = self.x1_grid.clone(); - let mut x2_grid = self.x2_grid.clone(); - - mu2_grid.extend_from_slice(&rhs_mu2); - mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - mu2_grid.dedup(); - x1_grid.extend_from_slice(&rhs_x1); - x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x1_grid.dedup(); - x2_grid.extend_from_slice(&rhs_x2); - x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x2_grid.dedup(); - - let mut array = - PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - - for ([i, j, k], value) in self.array.indexed_iter() { - let target_i = mu2_grid - .iter() - .position(|mu2| *mu2 == self.mu2_grid[i]) - .unwrap_or_else(|| unreachable!()); - let target_j = x1_grid - .iter() - .position(|&x| x == self.x1_grid[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = x2_grid - .iter() - .position(|&x| x == self.x2_grid[k]) - .unwrap_or_else(|| unreachable!()); - - array[[target_i, target_j, target_k]] = value; - } - - self.array = array; - self.mu2_grid = mu2_grid; - self.x1_grid = x1_grid; - self.x2_grid = x2_grid; - } - - for ([i, j, k], value) in other_grid.array.indexed_iter() { - let (j, k) = if transpose { (k, j) } else { (j, k) }; - let target_i = self - .mu2_grid - .iter() - .position(|x| *x == rhs_mu2[i]) - .unwrap_or_else(|| unreachable!()); - let target_j = self - .x1_grid - .iter() - .position(|&x| x == rhs_x1[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = self - .x2_grid - .iter() - .position(|&x| x == rhs_x2[k]) - .unwrap_or_else(|| unreachable!()); - - self.array[[target_i, target_j, target_k]] += value; - } + if self.is_empty() && !transpose { + if let SubgridEnum::PackedQ1X2SubgridV1(other) = other { + *self = mem::take(other); + return; } + } + + let rhs_mu2 = other.mu2_grid().into_owned(); + let rhs_x1 = if transpose { + other.x2_grid() + } else { + other.x1_grid() + }; + let rhs_x2 = if transpose { + other.x1_grid() } else { - todo!(); + other.x2_grid() + }; + + if (self.mu2_grid != rhs_mu2) || (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { + let mut mu2_grid = self.mu2_grid.clone(); + let mut x1_grid = self.x1_grid.clone(); + let mut x2_grid = self.x2_grid.clone(); + + mu2_grid.extend_from_slice(&rhs_mu2); + mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + mu2_grid.dedup(); + x1_grid.extend_from_slice(&rhs_x1); + x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x1_grid.dedup(); + x2_grid.extend_from_slice(&rhs_x2); + x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x2_grid.dedup(); + + let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + + for ([i, j, k], value) in self.array.indexed_iter() { + let target_i = mu2_grid + .iter() + .position(|mu2| *mu2 == self.mu2_grid[i]) + .unwrap_or_else(|| unreachable!()); + let target_j = x1_grid + .iter() + .position(|&x| x == self.x1_grid[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = x2_grid + .iter() + .position(|&x| x == self.x2_grid[k]) + .unwrap_or_else(|| unreachable!()); + + array[[target_i, target_j, target_k]] = value; + } + + self.array = array; + self.mu2_grid = mu2_grid; + self.x1_grid = x1_grid; + self.x2_grid = x2_grid; + } + + for ((i, j, k), value) in other.indexed_iter() { + let (j, k) = if transpose { (k, j) } else { (j, k) }; + let target_i = self + .mu2_grid + .iter() + .position(|x| *x == rhs_mu2[i]) + .unwrap_or_else(|| unreachable!()); + let target_j = self + .x1_grid + .iter() + .position(|&x| x == rhs_x1[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = self + .x2_grid + .iter() + .position(|&x| x == rhs_x2[k]) + .unwrap_or_else(|| unreachable!()); + + self.array[[target_i, target_j, target_k]] += value; } } @@ -253,6 +248,18 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { mod tests { use super::*; + #[test] + #[should_panic(expected = "PackedQ1X2SubgridV1 doesn't support the fill operation")] + fn fill_packed_q1x2_subgrid_v1() { + let mut subgrid = PackedQ1X2SubgridV1::new( + PackedArray::new([0, 0, 0]), + Vec::new(), + Vec::new(), + Vec::new(), + ); + subgrid.fill(&[0.0; 4], 0.0); + } + #[test] fn test_v1() { let x = vec![ @@ -288,8 +295,6 @@ mod tests { x.array_mut()[[0, 1, 3]] = 2.0; x.array_mut()[[0, 4, 3]] = 4.0; x.array_mut()[[0, 7, 1]] = 8.0; - } else { - unreachable!(); } assert!(!grid1.is_empty()); @@ -316,8 +321,6 @@ mod tests { x.array_mut()[[0, 3, 1]] = 2.0; x.array_mut()[[0, 3, 4]] = 4.0; x.array_mut()[[0, 1, 7]] = 8.0; - } else { - unreachable!(); } assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); From 33a49576b72ecc5b1caab1f418772a417bafc762 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 08:54:05 +0200 Subject: [PATCH 048/213] Increase code coverage --- pineappl/src/packed_array.rs | 5 +++++ pineappl/src/pids.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 26949cc2..ca6561f0 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -625,6 +625,9 @@ mod tests { fn indexed_iter() { let mut array = PackedArray::new([40, 50, 50]); + // check shape + assert_eq!(array.shape(), [40, 50, 50]); + // check empty iterator assert_eq!(array.indexed_iter().next(), None); @@ -706,6 +709,8 @@ mod tests { let array = PackedArray::from_ndarray(ndarray.view(), 3, 40); + assert_eq!(array.shape(), [40, 50, 50]); + assert_eq!(array[[3, 4, 3]], 1); assert_eq!(array[[3, 4, 4]], 2); assert_eq!(array[[3, 4, 5]], 0); diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index ea10dbb6..75eeb732 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -900,4 +900,34 @@ mod tests { assert_approx_eq!(f64, result.entry()[0].1, 1.0, ulps = 8); } } + + #[test] + fn pid_basis_charge_conjugate() { + assert_eq!(PidBasis::Evol.charge_conjugate(100), (100, 1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(103), (103, 1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(108), (108, 1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(115), (115, 1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(124), (124, 1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(135), (135, 1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(200), (200, -1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(203), (203, -1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(208), (208, -1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(215), (215, -1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(224), (224, -1.0)); + assert_eq!(PidBasis::Evol.charge_conjugate(235), (235, -1.0)); + + assert_eq!(PidBasis::Pdg.charge_conjugate(21), (21, 1.0)); + assert_eq!(PidBasis::Pdg.charge_conjugate(22), (22, 1.0)); + } + + #[test] + fn pid_basis_from_str() { + assert_eq!(PidBasis::from_str("EVOL").unwrap(), PidBasis::Evol); + assert_eq!(PidBasis::from_str("PDG").unwrap(), PidBasis::Pdg); + + assert_eq!( + PidBasis::from_str("XXX").unwrap_err().to_string(), + "unknown PID basis: XXX".to_owned() + ); + } } From cb937f65e25ff6dfa130a8c5cd431b19b942a9df Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:18:16 +0200 Subject: [PATCH 049/213] Fix bug in `LagrangeSubgridV2::stats` --- pineappl/src/empty_subgrid.rs | 5 ++++ pineappl/src/grid.rs | 18 +++++++++++++- pineappl/src/lagrange_subgrid.rs | 16 +++++++++---- pineappl/src/packed_subgrid.rs | 15 ++++++++++++ pineappl/src/pids.rs | 1 + pineappl/src/subgrid.rs | 40 ++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 5 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 7a543d25..8e43d766 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -1,5 +1,6 @@ //! TODO +use super::grid::Grid; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -10,6 +11,10 @@ use std::iter; pub struct EmptySubgridV1; impl Subgrid for EmptySubgridV1 { + fn nodes(&self, _: &Grid) -> Cow<[Vec]> { + Cow::Borrowed(&[]) + } + fn fill(&mut self, _: &[f64], _: f64) { panic!("EmptySubgridV1 doesn't support the fill operation"); } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 83ff711b..81f77ec0 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -10,7 +10,7 @@ use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; -use super::subgrid::{ExtraSubgridParams, Mu2, Subgrid, SubgridEnum, SubgridParams}; +use super::subgrid::{ExtraSubgridParams, Kinematics, Mu2, Subgrid, SubgridEnum, SubgridParams}; use bitflags::bitflags; use float_cmp::approx_eq; use git_version::git_version; @@ -149,6 +149,7 @@ pub struct Grid { metadata: BTreeMap, convolutions: Vec, pid_basis: PidBasis, + kinematics: Kinematics, more_members: MoreMembers, } @@ -178,6 +179,8 @@ impl Grid { convolutions, // TODO: make this a new parameter pid_basis: PidBasis::Pdg, + // TODO: make this a new parameter + kinematics: Kinematics::Q2rfX1X2, channels, subgrid_params, } @@ -199,6 +202,8 @@ impl Grid { extra: ExtraSubgridParams, subgrid_type: &str, ) -> Result { + // TODO: remove this constructor + let subgrid_template: SubgridEnum = match subgrid_type { "LagrangeSubgrid" | "LagrangeSubgridV2" => { LagrangeSubgridV2::new(&subgrid_params, &extra).into() @@ -217,11 +222,18 @@ impl Grid { metadata: default_metadata(), convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], pid_basis: PidBasis::Pdg, + kinematics: Kinematics::Q2rfX1X2, channels, more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), }) } + /// Return the meaning of kinematic variables which each subgrid interpolates. + #[must_use] + pub const fn kinematics(&self) -> Kinematics { + self.kinematics + } + /// Return the convention by which the channels' PIDs are encoded. #[must_use] pub const fn pid_basis(&self) -> &PidBasis { @@ -558,6 +570,8 @@ impl Grid { _ => panic!("unknown PID basis '{lumi_id_types}'"), } }), + // TODO: when reading DIS grids, change this value + kinematics: Kinematics::Q2rfX1X2, // TODO: make these proper members more_members: MoreMembers::V3(Mmv3 { remapper: grid.remapper().map(|r| { @@ -1434,6 +1448,7 @@ impl Grid { metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), pid_basis: info.pid_basis, + kinematics: self.kinematics, more_members: self.more_members.clone(), }; @@ -1570,6 +1585,7 @@ impl Grid { metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), pid_basis: infos[0].pid_basis, + kinematics: self.kinematics, more_members: self.more_members.clone(), }; diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 7eab3565..ce2a4dbf 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -1,6 +1,7 @@ //! Module containing the Lagrange-interpolation subgrid. use super::convert::{f64_from_usize, usize_from_f64}; +use super::grid::Grid; use super::subgrid::{ ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, }; @@ -162,6 +163,14 @@ impl LagrangeSubgridV2 { } impl Subgrid for LagrangeSubgridV2 { + fn nodes(&self, _: &Grid) -> Cow<[Vec]> { + Cow::Owned(vec![ + (0..self.ny1).map(|iy| fx(self.gety1(iy))).collect(), + (0..self.ny2).map(|iy| fx(self.gety2(iy))).collect(), + (0..self.ntau).map(|itau| fq2(self.gettau(itau))).collect(), + ]) + } + fn fill(&mut self, ntuple: &[f64], weight: f64) { if weight == 0.0 { return; @@ -400,13 +409,12 @@ impl Subgrid for LagrangeSubgridV2 { fn stats(&self) -> Stats { let (non_zeros, zeros) = self.grid.as_ref().map_or((0, 0), |array| { - array.iter().fold((0, 0), |mut result, value| { + array.iter().fold((0, 0), |(non_zeros, zeros), value| { if *value == 0.0 { - result.0 += 1; + (non_zeros, zeros + 1) } else { - result.1 += 1; + (non_zeros + 1, zeros) } - result }) }); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 6904c65e..5df59b10 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -1,5 +1,6 @@ //! TODO +use super::grid::Grid; use super::packed_array::PackedArray; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; @@ -39,6 +40,20 @@ impl PackedQ1X2SubgridV1 { } impl Subgrid for PackedQ1X2SubgridV1 { + fn nodes(&self, _: &Grid) -> Cow<[Vec]> { + Cow::Owned(vec![ + self.x1_grid.clone(), + self.x2_grid.clone(), + self.mu2_grid + .iter() + .map(|&Mu2 { ren, fac, frg: _ }| { + assert_eq!(ren, fac); + ren + }) + .collect(), + ]) + } + fn fill(&mut self, _: &[f64], _: f64) { panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); } diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index 75eeb732..828b4051 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -44,6 +44,7 @@ impl PidBasis { // TODO: in the general case we should allow to return a vector of tuples (Self::Evol, 100 | 103 | 108 | 115 | 124 | 135) => (pid, 1.0), (Self::Evol, 200 | 203 | 208 | 215 | 224 | 235) => (pid, -1.0), + (Self::Evol | Self::Pdg, 21 | 22) => (pid, 1.0), (Self::Evol | Self::Pdg, _) => (charge_conjugate_pdg_pid(pid), 1.0), } } diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 4330b9ba..e5da0543 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,6 +1,7 @@ //! Module containing the trait `Subgrid` and supporting structs. use super::empty_subgrid::EmptySubgridV1; +use super::grid::Grid; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; @@ -32,6 +33,41 @@ pub struct Mu2 { pub frg: f64, } +/// Enumeration describing the kinematic variables that are interpolated in a [`Subgrid`]. +#[derive(Clone, Copy, Deserialize, Serialize)] +pub enum Kinematics { + /// Denotes a [`Subgrid`] with three kinematic variables: 1) one scale, which is the value of + /// both the renormalization and factorization scale, 2) a momentum fraction for the first + /// convolution and 3) a momentum fraction for the second convolution. + Q2rfX1X2, + // /// Denotes a [`Subgrid`] with four kinematic variables: two scales, 1) the renormalization and + // /// 2) factorization scale, 3) a momentum fraction for the first convolution and 3) a momentum + // /// fraction for the second convolution. + // Q2rQ2fX1X2, + // /// FK-table. + // Q2fX1X2, +} + +// /// Return the renormalization scales for the `subgrid` contained in `grid`. +// pub fn ren<'a, 'b>(grid: &'a Grid, subgrid: &'b SubgridEnum) -> Cow<'b, [f64]> { +// match grid.kinematics() { +// Kinematics::Q2rfX1X2 => match subgrid.nodes(grid) { +// Cow::Borrowed(vec) => Cow::Borrowed(&vec[0]), +// Cow::Owned(mut vec) => Cow::Owned(vec.remove(0)), +// }, +// } +// } +// +// /// Return the factorization scales for the `subgrid` contained in `grid`. +// pub fn fac<'a, 'b>(grid: &'a Grid, subgrid: &'b SubgridEnum) -> Cow<'b, [f64]> { +// match grid.kinematics() { +// Kinematics::Q2rfX1X2 => match subgrid.nodes(grid) { +// Cow::Borrowed(vec) => Cow::Borrowed(&vec[0]), +// Cow::Owned(mut vec) => Cow::Owned(vec.remove(0)), +// }, +// } +// } + /// Size-related statistics for a subgrid. #[derive(Debug, Eq, PartialEq)] pub struct Stats { @@ -55,6 +91,10 @@ pub struct Stats { /// Trait each subgrid must implement. #[enum_dispatch] pub trait Subgrid { + /// Return the values of node points for each kinematical variable interpolated in this + /// subgrid. + fn nodes(&self, grid: &Grid) -> Cow<[Vec]>; + /// Return a slice of [`Mu2`] values corresponding to the (squared) renormalization and /// factorization values of the grid. If the subgrid does not use a grid, this method should /// return an empty slice. From c9b737c89a07b6bd7171832e74bdef820829e143 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:18:43 +0200 Subject: [PATCH 050/213] Remove `factor == 0` optimization --- pineappl/src/lagrange_subgrid.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index ce2a4dbf..4734c4cb 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -335,9 +335,7 @@ impl Subgrid for LagrangeSubgridV2 { } fn scale(&mut self, factor: f64) { - if factor == 0.0 { - self.grid = None; - } else if let Some(self_grid) = &mut self.grid { + if let Some(self_grid) = &mut self.grid { self_grid.iter_mut().for_each(|x| *x *= factor); } } From 7409f3cb246e90612700d4aba677eefcaaf2c655 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:19:05 +0200 Subject: [PATCH 051/213] Increase doctest coverage of `LagrangeSubgridV2` --- pineappl/src/lagrange_subgrid.rs | 78 +++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 4734c4cb..151148b2 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -439,7 +439,7 @@ mod tests { use super::*; #[test] - fn fill_zero_v2() { + fn fill_zero() { let mut subgrid = LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); @@ -447,5 +447,81 @@ mod tests { assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); + assert_eq!( + subgrid.stats(), + Stats { + total: 0, + allocated: 0, + zeros: 0, + overhead: 0, + bytes_per_value: mem::size_of::() + } + ); + } + + #[test] + fn fill_outside_range() { + let mut subgrid = + LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); + + subgrid.fill(&[1e-10, 0.5, 1000.0], 0.0); + + assert!(subgrid.is_empty()); + assert_eq!(subgrid.indexed_iter().count(), 0); + assert_eq!( + subgrid.stats(), + Stats { + total: 0, + allocated: 0, + zeros: 0, + overhead: 0, + bytes_per_value: mem::size_of::() + } + ); + } + + #[test] + fn fill() { + let mut subgrid = + LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); + + subgrid.fill(&[0.5, 0.5, 1000.0], 1.0); + + assert!(!subgrid.is_empty()); + assert_eq!(subgrid.indexed_iter().count(), 4 * 4 * 4); + assert_eq!( + subgrid.static_scale(), + Some(Mu2 { + ren: 1000.0, + fac: 1000.0, + frg: -1.0 + }) + ); + assert_eq!( + subgrid.stats(), + Stats { + total: 50 * 50 * 4, + allocated: 50 * 50 * 4, + zeros: 50 * 50 * 4 - 4 * 4 * 4, + overhead: 0, + bytes_per_value: mem::size_of::() + } + ); + + subgrid.fill(&[0.5, 0.5, 1000000.0], 1.0); + + assert!(!subgrid.is_empty()); + assert_eq!(subgrid.indexed_iter().count(), 2 * 4 * 4 * 4); + assert_eq!(subgrid.static_scale(), None); + assert_eq!( + subgrid.stats(), + Stats { + total: 50 * 50 * 23, + allocated: 50 * 50 * 23, + zeros: 50 * 50 * 23 - 4 * 4 * 4 * 2, + overhead: 0, + bytes_per_value: mem::size_of::() + } + ); } } From e5b7b345536443fa5000f6e67f139cfe4a6afc70 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:19:27 +0200 Subject: [PATCH 052/213] Remove unneeded derivations for `ExtraSubgridParams` --- pineappl/src/subgrid.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index e5da0543..d6bf1562 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -292,7 +292,6 @@ impl SubgridParams { } /// Extra grid creation parameters when the limits for `x1` and `x2` are different. -#[derive(Deserialize, Serialize)] pub struct ExtraSubgridParams { reweight2: bool, x2_bins: usize, From 75355e3afc8d71cf4e9d558665a187b06a824e84 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:34:32 +0200 Subject: [PATCH 053/213] Use assignment instead of `swap` --- pineappl/src/lagrange_subgrid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 151148b2..216c2c73 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -158,7 +158,7 @@ impl LagrangeSubgridV2 { self.itaumin = new_itaumin; self.itaumax = new_itaumax; - mem::swap(&mut self.grid, &mut Some(new_grid)); + self.grid = Some(new_grid); } } From 67265c967c12d9bad10802c2137b5c9aea5a8e5a Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:34:52 +0200 Subject: [PATCH 054/213] Temporarily remove trait implementation --- pineappl/src/subgrid.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index d6bf1562..c12ae5d9 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -5,7 +5,7 @@ use super::grid::Grid; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; -use ndarray::Array3; +// use ndarray::Array3; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -139,22 +139,22 @@ pub trait Subgrid { fn static_scale(&self) -> Option; } -// this is needed in the Python interface -impl From<&SubgridEnum> for Array3 { - fn from(subgrid: &SubgridEnum) -> Self { - let mut result = Self::zeros(( - subgrid.mu2_grid().len(), - subgrid.x1_grid().len(), - subgrid.x2_grid().len(), - )); - - for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { - result[[imu2, ix1, ix2]] = value; - } - - result - } -} +// // this is needed in the Python interface +// impl From<&SubgridEnum> for Array3 { +// fn from(subgrid: &SubgridEnum) -> Self { +// let mut result = Self::zeros(( +// subgrid.mu2_grid().len(), +// subgrid.x1_grid().len(), +// subgrid.x2_grid().len(), +// )); +// +// for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { +// result[[imu2, ix1, ix2]] = value; +// } +// +// result +// } +// } /// Type to iterate over the non-zero contents of a subgrid. The tuple contains the indices of the /// `mu2_grid`, the `x1_grid` and finally the `x2_grid`. From efdabd4a91701f5c0d111cd2c18bd3b411108d4c Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 09:57:26 +0200 Subject: [PATCH 055/213] Revert unintended changes from commit cb937f --- pineappl/src/empty_subgrid.rs | 5 ---- pineappl/src/grid.rs | 18 +------------- pineappl/src/lagrange_subgrid.rs | 9 ------- pineappl/src/packed_subgrid.rs | 15 ------------ pineappl/src/pids.rs | 1 - pineappl/src/subgrid.rs | 40 -------------------------------- 6 files changed, 1 insertion(+), 87 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 8e43d766..7a543d25 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -1,6 +1,5 @@ //! TODO -use super::grid::Grid; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -11,10 +10,6 @@ use std::iter; pub struct EmptySubgridV1; impl Subgrid for EmptySubgridV1 { - fn nodes(&self, _: &Grid) -> Cow<[Vec]> { - Cow::Borrowed(&[]) - } - fn fill(&mut self, _: &[f64], _: f64) { panic!("EmptySubgridV1 doesn't support the fill operation"); } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 81f77ec0..83ff711b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -10,7 +10,7 @@ use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; -use super::subgrid::{ExtraSubgridParams, Kinematics, Mu2, Subgrid, SubgridEnum, SubgridParams}; +use super::subgrid::{ExtraSubgridParams, Mu2, Subgrid, SubgridEnum, SubgridParams}; use bitflags::bitflags; use float_cmp::approx_eq; use git_version::git_version; @@ -149,7 +149,6 @@ pub struct Grid { metadata: BTreeMap, convolutions: Vec, pid_basis: PidBasis, - kinematics: Kinematics, more_members: MoreMembers, } @@ -179,8 +178,6 @@ impl Grid { convolutions, // TODO: make this a new parameter pid_basis: PidBasis::Pdg, - // TODO: make this a new parameter - kinematics: Kinematics::Q2rfX1X2, channels, subgrid_params, } @@ -202,8 +199,6 @@ impl Grid { extra: ExtraSubgridParams, subgrid_type: &str, ) -> Result { - // TODO: remove this constructor - let subgrid_template: SubgridEnum = match subgrid_type { "LagrangeSubgrid" | "LagrangeSubgridV2" => { LagrangeSubgridV2::new(&subgrid_params, &extra).into() @@ -222,18 +217,11 @@ impl Grid { metadata: default_metadata(), convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], pid_basis: PidBasis::Pdg, - kinematics: Kinematics::Q2rfX1X2, channels, more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), }) } - /// Return the meaning of kinematic variables which each subgrid interpolates. - #[must_use] - pub const fn kinematics(&self) -> Kinematics { - self.kinematics - } - /// Return the convention by which the channels' PIDs are encoded. #[must_use] pub const fn pid_basis(&self) -> &PidBasis { @@ -570,8 +558,6 @@ impl Grid { _ => panic!("unknown PID basis '{lumi_id_types}'"), } }), - // TODO: when reading DIS grids, change this value - kinematics: Kinematics::Q2rfX1X2, // TODO: make these proper members more_members: MoreMembers::V3(Mmv3 { remapper: grid.remapper().map(|r| { @@ -1448,7 +1434,6 @@ impl Grid { metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), pid_basis: info.pid_basis, - kinematics: self.kinematics, more_members: self.more_members.clone(), }; @@ -1585,7 +1570,6 @@ impl Grid { metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), pid_basis: infos[0].pid_basis, - kinematics: self.kinematics, more_members: self.more_members.clone(), }; diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 216c2c73..28e28a1e 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -1,7 +1,6 @@ //! Module containing the Lagrange-interpolation subgrid. use super::convert::{f64_from_usize, usize_from_f64}; -use super::grid::Grid; use super::subgrid::{ ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, }; @@ -163,14 +162,6 @@ impl LagrangeSubgridV2 { } impl Subgrid for LagrangeSubgridV2 { - fn nodes(&self, _: &Grid) -> Cow<[Vec]> { - Cow::Owned(vec![ - (0..self.ny1).map(|iy| fx(self.gety1(iy))).collect(), - (0..self.ny2).map(|iy| fx(self.gety2(iy))).collect(), - (0..self.ntau).map(|itau| fq2(self.gettau(itau))).collect(), - ]) - } - fn fill(&mut self, ntuple: &[f64], weight: f64) { if weight == 0.0 { return; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 5df59b10..6904c65e 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -1,6 +1,5 @@ //! TODO -use super::grid::Grid; use super::packed_array::PackedArray; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; @@ -40,20 +39,6 @@ impl PackedQ1X2SubgridV1 { } impl Subgrid for PackedQ1X2SubgridV1 { - fn nodes(&self, _: &Grid) -> Cow<[Vec]> { - Cow::Owned(vec![ - self.x1_grid.clone(), - self.x2_grid.clone(), - self.mu2_grid - .iter() - .map(|&Mu2 { ren, fac, frg: _ }| { - assert_eq!(ren, fac); - ren - }) - .collect(), - ]) - } - fn fill(&mut self, _: &[f64], _: f64) { panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); } diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index 828b4051..75eeb732 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -44,7 +44,6 @@ impl PidBasis { // TODO: in the general case we should allow to return a vector of tuples (Self::Evol, 100 | 103 | 108 | 115 | 124 | 135) => (pid, 1.0), (Self::Evol, 200 | 203 | 208 | 215 | 224 | 235) => (pid, -1.0), - (Self::Evol | Self::Pdg, 21 | 22) => (pid, 1.0), (Self::Evol | Self::Pdg, _) => (charge_conjugate_pdg_pid(pid), 1.0), } } diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index c12ae5d9..6c62e3b2 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,7 +1,6 @@ //! Module containing the trait `Subgrid` and supporting structs. use super::empty_subgrid::EmptySubgridV1; -use super::grid::Grid; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; @@ -33,41 +32,6 @@ pub struct Mu2 { pub frg: f64, } -/// Enumeration describing the kinematic variables that are interpolated in a [`Subgrid`]. -#[derive(Clone, Copy, Deserialize, Serialize)] -pub enum Kinematics { - /// Denotes a [`Subgrid`] with three kinematic variables: 1) one scale, which is the value of - /// both the renormalization and factorization scale, 2) a momentum fraction for the first - /// convolution and 3) a momentum fraction for the second convolution. - Q2rfX1X2, - // /// Denotes a [`Subgrid`] with four kinematic variables: two scales, 1) the renormalization and - // /// 2) factorization scale, 3) a momentum fraction for the first convolution and 3) a momentum - // /// fraction for the second convolution. - // Q2rQ2fX1X2, - // /// FK-table. - // Q2fX1X2, -} - -// /// Return the renormalization scales for the `subgrid` contained in `grid`. -// pub fn ren<'a, 'b>(grid: &'a Grid, subgrid: &'b SubgridEnum) -> Cow<'b, [f64]> { -// match grid.kinematics() { -// Kinematics::Q2rfX1X2 => match subgrid.nodes(grid) { -// Cow::Borrowed(vec) => Cow::Borrowed(&vec[0]), -// Cow::Owned(mut vec) => Cow::Owned(vec.remove(0)), -// }, -// } -// } -// -// /// Return the factorization scales for the `subgrid` contained in `grid`. -// pub fn fac<'a, 'b>(grid: &'a Grid, subgrid: &'b SubgridEnum) -> Cow<'b, [f64]> { -// match grid.kinematics() { -// Kinematics::Q2rfX1X2 => match subgrid.nodes(grid) { -// Cow::Borrowed(vec) => Cow::Borrowed(&vec[0]), -// Cow::Owned(mut vec) => Cow::Owned(vec.remove(0)), -// }, -// } -// } - /// Size-related statistics for a subgrid. #[derive(Debug, Eq, PartialEq)] pub struct Stats { @@ -91,10 +55,6 @@ pub struct Stats { /// Trait each subgrid must implement. #[enum_dispatch] pub trait Subgrid { - /// Return the values of node points for each kinematical variable interpolated in this - /// subgrid. - fn nodes(&self, grid: &Grid) -> Cow<[Vec]>; - /// Return a slice of [`Mu2`] values corresponding to the (squared) renormalization and /// factorization values of the grid. If the subgrid does not use a grid, this method should /// return an empty slice. From 7bbde3ad663673e43124ee01d786b844b9c1ce14 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 10:59:24 +0200 Subject: [PATCH 056/213] Remove obsolete `FkTable` methods --- pineappl/src/fk_table.rs | 66 +++--------------------------------- pineappl_cli/tests/import.rs | 10 +++--- 2 files changed, 9 insertions(+), 67 deletions(-) diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 5d146746..0b8bd935 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -2,12 +2,11 @@ use super::boc::Order; use super::convolutions::{Convolution, LumiCache}; -use super::grid::{Grid, GridError}; +use super::grid::Grid; use super::subgrid::Subgrid; use float_cmp::approx_eq; use ndarray::Array4; use std::fmt::{self, Display, Formatter}; -use std::io::Write; use std::str::FromStr; use thiserror::Error; @@ -151,7 +150,7 @@ impl FkTable { let x_grid = self.x_grid(); let mut result = Array4::zeros(( - self.bins(), + self.grid.bin_info().bins(), self.grid.channels().len(), if has_pdf1 { x_grid.len() } else { 1 }, if has_pdf2 { x_grid.len() } else { 1 }, @@ -187,36 +186,6 @@ impl FkTable { result } - /// Returns the number of bins for this `FkTable`. - #[must_use] - pub fn bins(&self) -> usize { - self.grid.bin_info().bins() - } - - /// Extract the normalizations for each bin. - #[must_use] - pub fn bin_normalizations(&self) -> Vec { - self.grid.bin_info().normalizations() - } - - /// Extract the number of dimensions for bins. - #[must_use] - pub fn bin_dimensions(&self) -> usize { - self.grid.bin_info().dimensions() - } - - /// Extract the left edges of a specific bin dimension. - #[must_use] - pub fn bin_left(&self, dimension: usize) -> Vec { - self.grid.bin_info().left(dimension) - } - - /// Extract the right edges of a specific bin dimension. - #[must_use] - pub fn bin_right(&self, dimension: usize) -> Vec { - self.grid.bin_info().right(dimension) - } - /// Return the channel definition for this `FkTable`. All factors are `1.0`. #[must_use] pub fn channels(&self) -> Vec> { @@ -244,24 +213,6 @@ impl FkTable { self.grid.evolve_info(&[true]).x1 } - /// Propagate write to grid - /// - /// # Errors - /// - /// TODO - pub fn write(&self, writer: impl Write) -> Result<(), GridError> { - self.grid.write(writer) - } - - /// Propagate `write_lz4` to `Grid`. - /// - /// # Errors - /// - /// See [`Grid::write_lz4`]. - pub fn write_lz4(&self, writer: impl Write) -> Result<(), GridError> { - self.grid.write_lz4(writer) - } - /// Convolve the FK-table. This method has fewer arguments than [`Grid::convolve`], because /// FK-tables have all orders merged together and do not support scale variations. pub fn convolve( @@ -279,13 +230,6 @@ impl FkTable { ) } - /// Set a metadata key-value pair. - pub fn set_key_value(&mut self, key: &str, value: &str) { - self.grid - .metadata_mut() - .insert(key.to_owned(), value.to_owned()); - } - /// Optimizes the storage of FK tables based of assumptions of the PDFs at the FK table's /// scale. /// @@ -422,10 +366,8 @@ mod tests { assert_eq!(FkAssumptions::from_str("Nf3Ind"), Ok(FkAssumptions::Nf3Ind)); assert_eq!(FkAssumptions::from_str("Nf3Sym"), Ok(FkAssumptions::Nf3Sym)); assert_eq!( - FkAssumptions::from_str("XXXXXX"), - Err(UnknownFkAssumption { - variant: "XXXXXX".to_owned() - }) + FkAssumptions::from_str("XXXXXX").unwrap().to_string(), + "unknown variant for FkAssumptions: XXXXXX" ); } diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index b3f48156..ef9a7459 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -902,11 +902,11 @@ fn import_hadronic_fktable() { ] ); - assert_eq!(fk_table.bins(), 1); - assert_eq!(fk_table.bin_normalizations(), [1.0]); - assert_eq!(fk_table.bin_dimensions(), 1); - assert_eq!(fk_table.bin_left(0), [0.0]); - assert_eq!(fk_table.bin_right(0), [1.0]); + assert_eq!(fk_table.grid().bin_info().bins(), 1); + assert_eq!(fk_table.grid().bin_info().normalizations(), [1.0]); + assert_eq!(fk_table.grid().bin_info().dimensions(), 1); + assert_eq!(fk_table.grid().bin_info().left(0), [0.0]); + assert_eq!(fk_table.grid().bin_info().right(0), [1.0]); assert_eq!( fk_table.grid().convolutions(), [Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)] From 1fab0ea77bb88f90ffa2f61c063769c74ce660ec Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 19 Aug 2024 11:40:59 +0200 Subject: [PATCH 057/213] Increase code coverage --- pineappl/src/boc.rs | 53 ++++++++++++++++++++++++++-------------- pineappl/src/fk_table.rs | 2 +- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 342e9339..2c54b996 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -28,7 +28,8 @@ pub struct Order { pub logxir: u32, /// Exponent of the logarithm of the scale factor of the initial state factorization scale. pub logxif: u32, - /// Exponent of the logarithm of the scale factor of the final state factorization scale (fragmentation scale). + /// Exponent of the logarithm of the scale factor of the final state factorization scale + /// (fragmentation scale). pub logxia: u32, } @@ -261,6 +262,7 @@ impl Order { && match max_as.cmp(&max_al) { Ordering::Greater => lo_as + pto == alphas, Ordering::Less => lo_al + pto == alpha, + // TODO: when do we hit this condition? Ordering::Equal => false, }) }, @@ -415,16 +417,22 @@ impl Channel { /// # Examples /// /// ```rust - /// use pineappl::boc::Channel; - /// - /// let entry1 = Channel::new(vec![(vec![2, 2], 2.0), (vec![4, 4], 2.0)]); - /// let entry2 = Channel::new(vec![(vec![4, 4], 1.0), (vec![2, 2], 1.0)]); - /// let entry3 = Channel::new(vec![(vec![3, 4], 1.0), (vec![2, 2], 1.0)]); - /// let entry4 = Channel::new(vec![(vec![4, 3], 1.0), (vec![2, 3], 2.0)]); + /// use pineappl::channel; /// - /// assert_eq!(entry1.common_factor(&entry2), Some(2.0)); - /// assert_eq!(entry1.common_factor(&entry3), None); - /// assert_eq!(entry1.common_factor(&entry4), None); + /// let ch1 = channel![2, 2, 2.0; 4, 4, 2.0]; + /// let ch2 = channel![4, 4, 1.0; 2, 2, 1.0]; + /// let ch3 = channel![3, 4, 1.0; 2, 2, 1.0]; + /// let ch4 = channel![4, 3, 1.0; 2, 3, 2.0]; + /// let ch5 = channel![2, 2, 1.0; 4, 4, 2.0]; + /// + /// // ch1 is ch2 multiplied by two + /// assert_eq!(ch1.common_factor(&ch2), Some(2.0)); + /// // ch1 isn't similar to ch3 + /// assert_eq!(ch1.common_factor(&ch3), None); + /// // ch1 isn't similar to ch4 either + /// assert_eq!(ch1.common_factor(&ch4), None); + /// // ch1 is similar to ch5, but they don't share a common factor + /// assert_eq!(ch1.common_factor(&ch5), None); /// ``` #[must_use] pub fn common_factor(&self, other: &Self) -> Option { @@ -519,6 +527,7 @@ impl FromStr for Channel { /// ``` #[macro_export] macro_rules! channel { + // TODO: generalize this to accept an arbitrary number of PIDs ($a:expr, $b:expr, $factor:expr $(; $c:expr, $d:expr, $fac:expr)*) => { $crate::boc::Channel::new(vec![(vec![$a, $b], $factor), $((vec![$c, $d], $fac)),*]) }; @@ -526,7 +535,7 @@ macro_rules! channel { #[cfg(test)] mod tests { - use super::{Channel, Order, ParseOrderError}; + use super::*; use crate::pids; #[test] @@ -535,16 +544,17 @@ mod tests { assert_eq!("a1".parse(), Ok(Order::new(0, 1, 0, 0, 0))); assert_eq!("as1lr1".parse(), Ok(Order::new(1, 0, 1, 0, 0))); assert_eq!("as1lf1".parse(), Ok(Order::new(1, 0, 0, 1, 0))); + assert_eq!("as1la1".parse(), Ok(Order::new(1, 0, 0, 0, 1))); assert_eq!( - "ab12".parse::(), - Err(ParseOrderError("unknown coupling: 'ab'".to_owned())) + "ab12".parse::().unwrap_err().to_string(), + "unknown coupling: 'ab'" ); assert_eq!( - "ab123456789000000".parse::(), - Err(ParseOrderError( - "error while parsing exponent of 'ab': number too large to fit in target type" - .to_owned() - )) + "ab123456789000000" + .parse::() + .unwrap_err() + .to_string(), + "error while parsing exponent of 'ab': number too large to fit in target type" ); } @@ -784,5 +794,12 @@ mod tests { .to_string(), "missing ')' in ' ( 2, -2 '" ); + + assert_eq!( + str::parse::("1 * (2, 2, 2) + 2 * (4, 4)") + .unwrap_err() + .to_string(), + "PID tuples have different lengths" + ); } } diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 0b8bd935..a3e969ea 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -366,7 +366,7 @@ mod tests { assert_eq!(FkAssumptions::from_str("Nf3Ind"), Ok(FkAssumptions::Nf3Ind)); assert_eq!(FkAssumptions::from_str("Nf3Sym"), Ok(FkAssumptions::Nf3Sym)); assert_eq!( - FkAssumptions::from_str("XXXXXX").unwrap().to_string(), + FkAssumptions::from_str("XXXXXX").unwrap_err().to_string(), "unknown variant for FkAssumptions: XXXXXX" ); } From 2f06916f6b31b8d3495b1cbfff0f516a0b1b2a3b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 20 Aug 2024 13:33:59 +0200 Subject: [PATCH 058/213] Increase code coverage --- pineappl_cli/src/helpers.rs | 9 ++++++-- pineappl_cli/tests/convolve.rs | 39 +++++++++++++++++++++++++++++++++- pineappl_cli/tests/pull.rs | 19 +++++++++++++++++ pineappl_cli/tests/uncert.rs | 24 ++++++++++----------- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index bd8c10c8..9d24e32a 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -295,8 +295,9 @@ pub fn convolve_scales( let mut alphas = |q2| match cfg.use_alphas_from { 0 => fun1.alphas_q2(q2), 1 => fun2.alphas_q2(q2), + // TODO: convert this into an error _ => panic!( - "expected `use_alphas_from` to be `0` or `1`, is {}", + "expected `use_alphas_from` to be `0` or `1`, is `{}`", cfg.use_alphas_from ), }; @@ -321,7 +322,11 @@ pub fn convolve_scales( grid.convolve(&mut cache, &orders, bins, channels, scales) } - _ => unimplemented!(), + // TODO: convert this into an error + _ => panic!( + "convolutions with {} convolution functions is not supported", + conv_funs.len() + ), }; match mode { diff --git a/pineappl_cli/tests/convolve.rs b/pineappl_cli/tests/convolve.rs index 36902e63..9fad280b 100644 --- a/pineappl_cli/tests/convolve.rs +++ b/pineappl_cli/tests/convolve.rs @@ -1,4 +1,5 @@ use assert_cmd::Command; +use predicates::str; const HELP_STR: &str = "Convolutes a PineAPPL grid with a PDF set @@ -30,6 +31,12 @@ const DEFAULT_STR: &str = "b etal dsig/detal 7 4 4.5 2.7517266e1 "; +const USE_ALPHAS_FROM_ERROR_STR: &str = "expected `use_alphas_from` to be `0` or `1`, is `2` +"; + +const THREE_PDF_ERROR_STR: &str = "convolutions with 3 convolution functions is not supported +"; + const FORCE_POSITIVE_STR: &str = "b etal dsig/detal [] [pb] -+----+----+----------- @@ -179,15 +186,45 @@ fn default() { Command::cargo_bin("pineappl") .unwrap() .args([ + "--use-alphas-from=1", "convolve", "../test-data/LHCB_WP_7TEV.pineappl.lz4", - "NNPDF31_nlo_as_0118_luxqed", + "NNPDF31_nlo_as_0118_luxqed,NNPDF31_nlo_as_0118_luxqed", ]) .assert() .success() .stdout(DEFAULT_STR); } +#[test] +fn use_alphas_from_error() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "--use-alphas-from=2", + "convolve", + "../test-data/LHCB_WP_7TEV.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed,NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .failure() + .stderr(str::contains(USE_ALPHAS_FROM_ERROR_STR)); +} + +#[test] +fn three_pdf_error() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "convolve", + "../test-data/LHCB_WP_7TEV.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed,NNPDF31_nlo_as_0118_luxqed,NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .failure() + .stderr(str::contains(THREE_PDF_ERROR_STR)); +} + #[test] fn force_positive() { Command::cargo_bin("pineappl") diff --git a/pineappl_cli/tests/pull.rs b/pineappl_cli/tests/pull.rs index 5668a8f0..632021d1 100644 --- a/pineappl_cli/tests/pull.rs +++ b/pineappl_cli/tests/pull.rs @@ -86,6 +86,9 @@ const REPLICA0_STR: &str = "b etal total c pull c pull c pull c pull 7 4 4.5 1.215 0 1.426 1 -0.353 3 0.147 4 -0.003 2 -0.002 "; +const LHAID_ERROR_STR: &str = "Error: no convolution function for LHAID = `0` found +"; + #[test] fn help() { Command::cargo_bin("pineappl") @@ -185,3 +188,19 @@ fn replica0() { .success() .stdout(REPLICA0_STR); } + +#[test] +fn conv_fun_lhaid_error() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "pull", + "--threads=1", + "../test-data/LHCB_WP_7TEV.pineappl.lz4", + "0", + "NNPDF40_nnlo_as_01180", + ]) + .assert() + .failure() + .stderr(LHAID_ERROR_STR); +} diff --git a/pineappl_cli/tests/uncert.rs b/pineappl_cli/tests/uncert.rs index 93c482c4..7258630c 100644 --- a/pineappl_cli/tests/uncert.rs +++ b/pineappl_cli/tests/uncert.rs @@ -25,17 +25,17 @@ Options: "; const DEFAULT_STR: &str = - "b etal dsig/detal NNPDF31_nlo_as_0118_luxqed NNPDF40_nnlo_as_01180 - [] [pb] [pb] [%] [%] [pb] [%] [%] --+----+----+-----------+-----------+---------+---------+-----------+--------+-------- -0 2 2.25 7.7302788e2 7.7302788e2 -0.67 0.67 7.7302788e2 -0.62 0.62 -1 2.25 2.5 7.0634852e2 7.0634851e2 -0.72 0.72 7.0634851e2 -0.62 0.62 -2 2.5 2.75 6.1354750e2 6.1354750e2 -0.78 0.78 6.1354750e2 -0.64 0.64 -3 2.75 3 4.9584391e2 4.9584391e2 -0.86 0.86 4.9584391e2 -0.68 0.68 -4 3 3.25 3.6957893e2 3.6957893e2 -0.97 0.97 3.6957893e2 -0.76 0.76 -5 3.25 3.5 2.5143057e2 2.5143057e2 -1.14 1.14 2.5143057e2 -0.89 0.89 -6 3.5 4 1.1962468e2 1.1962468e2 -1.55 1.55 1.1962468e2 -1.34 1.34 -7 4 4.5 2.9665790e1 2.9665790e1 -2.56 2.56 2.9665789e1 -3.51 3.51 + "b etal dsig/detal 324900 NNPDF40_nnlo_as_01180 + [] [pb] [pb] [%] [%] [pb] [%] [%] +-+----+----+-----------+-----------+--------+--------+-----------+--------+-------- +0 2 2.25 7.7302788e2 7.7302788e2 -0.67 0.67 7.7302788e2 -0.62 0.62 +1 2.25 2.5 7.0634852e2 7.0634851e2 -0.72 0.72 7.0634851e2 -0.62 0.62 +2 2.5 2.75 6.1354750e2 6.1354750e2 -0.78 0.78 6.1354750e2 -0.64 0.64 +3 2.75 3 4.9584391e2 4.9584391e2 -0.86 0.86 4.9584391e2 -0.68 0.68 +4 3 3.25 3.6957893e2 3.6957893e2 -0.97 0.97 3.6957893e2 -0.76 0.76 +5 3.25 3.5 2.5143057e2 2.5143057e2 -1.14 1.14 2.5143057e2 -0.89 0.89 +6 3.5 4 1.1962468e2 1.1962468e2 -1.55 1.55 1.1962468e2 -1.34 1.34 +7 4 4.5 2.9665790e1 2.9665790e1 -2.56 2.56 2.9665789e1 -3.51 3.51 "; const CL_90_STR: &str = "b etal dsig/detal NNPDF31_nlo_as_0118_luxqed @@ -170,7 +170,7 @@ fn conv_fun_default() { "--conv-fun=0,1", "--threads=1", "../test-data/LHCB_WP_7TEV.pineappl.lz4", - "NNPDF31_nlo_as_0118_luxqed,NNPDF40_nnlo_as_01180=NNPDF3.1+NNPDF4.0", + "324900,NNPDF40_nnlo_as_01180=NNPDF3.1+NNPDF4.0", ]) .assert() .success() From 7b255aaca63a3dd04454576a669c3e7581d20695 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 20 Aug 2024 15:28:52 +0200 Subject: [PATCH 059/213] Generalize convolution helpers and increase test coverage --- pineappl_cli/src/helpers.rs | 223 +++++++++++++++--------------------- pineappl_cli/tests/plot.rs | 21 ++++ 2 files changed, 112 insertions(+), 132 deletions(-) diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 9d24e32a..97864a36 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -247,81 +247,56 @@ pub fn convolve_scales( } } - let mut results = match conv_funs { - [fun] => { - // there's only one convolution function from which we can use the strong coupling - assert_eq!(cfg.use_alphas_from, 0); - - // if the field 'Particle' is missing we assume it's a proton PDF - let pdg_id = fun - .set() - .entry("Particle") - .map_or(Ok(2212), |string| string.parse::()) - .unwrap(); + // TODO: promote this to an error + assert!( + cfg.use_alphas_from < conv_funs.len(), + "expected `use_alphas_from` to be `0` or `1`, is `{}`", + cfg.use_alphas_from + ); - let x_max = fun.x_max(); - let x_min = fun.x_min(); - let mut alphas = |q2| fun.alphas_q2(q2); - let mut fun = |id, x, q2| { + let x_min_max: Vec<_> = conv_funs + .iter_mut() + .map(|fun| (fun.x_min(), fun.x_max())) + .collect(); + let mut funs: Vec<_> = conv_funs + .iter() + .zip(x_min_max) + .map(|(fun, (x_min, x_max))| { + move |id, x, q2| { if !cfg.allow_extrapolation && (x < x_min || x > x_max) { 0.0 } else { fun.xfx_q2(id, x, q2) } - }; - - let mut cache = LumiCache::with_one(pdg_id, &mut fun, &mut alphas); - - grid.convolve(&mut cache, &orders, bins, channels, scales) - } - [fun1, fun2] => { - let pdg_id1 = fun1 - .set() - .entry("Particle") - .map_or(Ok(2212), |string| string.parse::()) - .unwrap(); - - let pdg_id2 = fun2 - .set() + } + }) + .collect(); + let mut alphas_funs: Vec<_> = conv_funs + .iter() + .map(|fun| move |q2| fun.alphas_q2(q2)) + .collect(); + let pdg_ids: Vec<_> = conv_funs + .iter() + .map(|fun| { + // if the field 'Particle' is missing we assume it's a proton PDF + fun.set() .entry("Particle") .map_or(Ok(2212), |string| string.parse::()) - .unwrap(); - - let x_max1 = fun1.x_max(); - let x_min1 = fun1.x_min(); - let x_max2 = fun2.x_max(); - let x_min2 = fun2.x_min(); - - let mut alphas = |q2| match cfg.use_alphas_from { - 0 => fun1.alphas_q2(q2), - 1 => fun2.alphas_q2(q2), - // TODO: convert this into an error - _ => panic!( - "expected `use_alphas_from` to be `0` or `1`, is `{}`", - cfg.use_alphas_from - ), - }; - let mut fun1 = |id, x, q2| { - if !cfg.allow_extrapolation && (x < x_min1 || x > x_max1) { - 0.0 - } else { - fun1.xfx_q2(id, x, q2) - } - }; - - let mut fun2 = |id, x, q2| { - if !cfg.allow_extrapolation && (x < x_min2 || x > x_max2) { - 0.0 - } else { - fun2.xfx_q2(id, x, q2) - } - }; - - let mut cache = - LumiCache::with_two(pdg_id1, &mut fun1, pdg_id2, &mut fun2, &mut alphas); + // UNWRAP: if this fails, there's a non-integer string in the LHAPDF info file + .unwrap() + }) + .collect(); - grid.convolve(&mut cache, &orders, bins, channels, scales) - } + // TODO: write a new constructor of `LumiCache` that accepts a vector of all the arguments + let mut cache = match funs.as_mut_slice() { + [funs0] => LumiCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]), + [funs0, funs1] => LumiCache::with_two( + pdg_ids[0], + funs0, + pdg_ids[1], + funs1, + &mut alphas_funs[cfg.use_alphas_from], + ), // TODO: convert this into an error _ => panic!( "convolutions with {} convolution functions is not supported", @@ -329,6 +304,8 @@ pub fn convolve_scales( ), }; + let mut results = grid.convolve(&mut cache, &orders, bins, channels, scales); + match mode { ConvoluteMode::Asymmetry => { let bin_count = grid.bin_info().bins(); @@ -433,82 +410,64 @@ pub fn convolve_subgrid( } } - match conv_funs { - [fun] => { - // there's only one convolution function from which we can use the strong coupling - assert_eq!(cfg.use_alphas_from, 0); - - // if the field 'Particle' is missing we assume it's a proton PDF - let pdg_id = fun - .set() - .entry("Particle") - .map_or(Ok(2212), |string| string.parse::()) - .unwrap(); + // TODO: promote this to an error + assert!( + cfg.use_alphas_from < conv_funs.len(), + "expected `use_alphas_from` to be `0` or `1`, is `{}`", + cfg.use_alphas_from + ); - let x_max = fun.x_max(); - let x_min = fun.x_min(); - let mut alphas = |q2| fun.alphas_q2(q2); - let mut fun = |id, x, q2| { + let x_min_max: Vec<_> = conv_funs + .iter_mut() + .map(|fun| (fun.x_min(), fun.x_max())) + .collect(); + let mut funs: Vec<_> = conv_funs + .iter() + .zip(x_min_max) + .map(|(fun, (x_min, x_max))| { + move |id, x, q2| { if !cfg.allow_extrapolation && (x < x_min || x > x_max) { 0.0 } else { fun.xfx_q2(id, x, q2) } - }; - - let mut cache = LumiCache::with_one(pdg_id, &mut fun, &mut alphas); - - grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)) - } - [fun1, fun2] => { - let pdg_id1 = fun1 - .set() - .entry("Particle") - .map_or(Ok(2212), |string| string.parse::()) - .unwrap(); - - let pdg_id2 = fun2 - .set() + } + }) + .collect(); + let mut alphas_funs: Vec<_> = conv_funs + .iter() + .map(|fun| move |q2| fun.alphas_q2(q2)) + .collect(); + let pdg_ids: Vec<_> = conv_funs + .iter() + .map(|fun| { + // if the field 'Particle' is missing we assume it's a proton PDF + fun.set() .entry("Particle") .map_or(Ok(2212), |string| string.parse::()) - .unwrap(); - - let x_max1 = fun1.x_max(); - let x_min1 = fun1.x_min(); - let x_max2 = fun2.x_max(); - let x_min2 = fun2.x_min(); - - let mut alphas = |q2| match cfg.use_alphas_from { - 0 => fun1.alphas_q2(q2), - 1 => fun2.alphas_q2(q2), - _ => panic!( - "expected `use_alphas_from` to be `0` or `1`, is {}", - cfg.use_alphas_from - ), - }; - let mut fun1 = |id, x, q2| { - if !cfg.allow_extrapolation && (x < x_min1 || x > x_max1) { - 0.0 - } else { - fun1.xfx_q2(id, x, q2) - } - }; - - let mut fun2 = |id, x, q2| { - if !cfg.allow_extrapolation && (x < x_min2 || x > x_max2) { - 0.0 - } else { - fun2.xfx_q2(id, x, q2) - } - }; + // UNWRAP: if this fails, there's a non-integer string in the LHAPDF info file + .unwrap() + }) + .collect(); - let mut cache = - LumiCache::with_two(pdg_id1, &mut fun1, pdg_id2, &mut fun2, &mut alphas); + // TODO: write a new constructor of `LumiCache` that accepts a vector of all the arguments + let mut cache = match funs.as_mut_slice() { + [funs0] => LumiCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]), + [funs0, funs1] => LumiCache::with_two( + pdg_ids[0], + funs0, + pdg_ids[1], + funs1, + &mut alphas_funs[cfg.use_alphas_from], + ), + // TODO: convert this into an error + _ => panic!( + "convolutions with {} convolution functions is not supported", + conv_funs.len() + ), + }; - grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)) - } - _ => unimplemented!(), - } + grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)) } pub fn parse_integer_range(range: &str) -> Result> { diff --git a/pineappl_cli/tests/plot.rs b/pineappl_cli/tests/plot.rs index a93ac23c..37b784c8 100644 --- a/pineappl_cli/tests/plot.rs +++ b/pineappl_cli/tests/plot.rs @@ -1,4 +1,5 @@ use assert_cmd::Command; +use predicates::str; use std::num::NonZeroUsize; use std::thread; @@ -1400,6 +1401,9 @@ if __name__ == "__main__": main() "#; +const THREE_PDF_ERROR_STR: &str = "convolutions with 3 convolution functions is not supported +"; + #[test] fn help() { Command::cargo_bin("pineappl") @@ -1450,6 +1454,23 @@ fn subgrid_pull() { .stdout(SUBGRID_PULL_STR); } +#[test] +fn three_pdf_error() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "plot", + "--subgrid-pull=0,0,0", + "--threads=1", + "../test-data/LHCB_WP_7TEV.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed", + "NNPDF40_nnlo_as_01180,NNPDF40_nnlo_as_01180,NNPDF40_nnlo_as_01180", + ]) + .assert() + .failure() + .stderr(str::contains(THREE_PDF_ERROR_STR)); +} + #[test] fn drell_yan_afb() { Command::cargo_bin("pineappl") From 6935d9ae58c5526f4c2f91014cce81654bc9686b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 22 Aug 2024 17:49:25 +0200 Subject: [PATCH 060/213] Move v0 file conversion into separate module --- pineappl/src/grid.rs | 196 ++++--------------------------------------- pineappl/src/lib.rs | 1 + pineappl/src/v0.rs | 179 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 181 deletions(-) create mode 100644 pineappl/src/v0.rs diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 83ff711b..7e0c752f 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -7,16 +7,15 @@ use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorInfo, OperatorSliceInfo}; use super::fk_table::FkTable; use super::lagrange_subgrid::LagrangeSubgridV2; -use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; use super::subgrid::{ExtraSubgridParams, Mu2, Subgrid, SubgridEnum, SubgridParams}; +use super::v0; use bitflags::bitflags; use float_cmp::approx_eq; use git_version::git_version; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; use ndarray::{s, Array3, ArrayView3, ArrayView5, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; -use pineappl_v0::grid::Grid as GridV0; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::collections::BTreeMap; @@ -71,9 +70,9 @@ pub enum GridError { } #[derive(Clone, Deserialize, Serialize)] -struct Mmv3 { - remapper: Option, - subgrid_template: SubgridEnum, +pub(crate) struct Mmv3 { + pub(crate) remapper: Option, + pub(crate) subgrid_template: SubgridEnum, } impl Mmv3 { @@ -101,7 +100,7 @@ fn default_metadata() -> BTreeMap { // ALLOW: fixing the warning will break the file format #[allow(clippy::large_enum_variant)] #[derive(Clone, Deserialize, Serialize)] -enum MoreMembers { +pub(crate) enum MoreMembers { V3(Mmv3), } @@ -141,15 +140,15 @@ bitflags! { /// bin, and coupling order it was created with. #[derive(Clone, Deserialize, Serialize)] pub struct Grid { - subgrids: Array3, - channels: Vec, - bin_limits: BinLimits, - orders: Vec, - subgrid_params: SubgridParams, - metadata: BTreeMap, - convolutions: Vec, - pid_basis: PidBasis, - more_members: MoreMembers, + pub(crate) subgrids: Array3, + pub(crate) channels: Vec, + pub(crate) bin_limits: BinLimits, + pub(crate) orders: Vec, + pub(crate) subgrid_params: SubgridParams, + pub(crate) metadata: BTreeMap, + pub(crate) convolutions: Vec, + pub(crate) pid_basis: PidBasis, + pub(crate) more_members: MoreMembers, } impl Grid { @@ -467,177 +466,12 @@ impl Grid { }; match file_version { - 0 => Self::read_uncompressed_v0(reader), + 0 => v0::read_uncompressed_v0(reader), 1 => bincode::deserialize_from(reader).map_err(GridError::ReadFailure), _ => Err(GridError::FileVersionUnsupported { file_version }), } } - fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { - use pineappl_v0::subgrid::Subgrid as _; - - let grid = GridV0::read(&mut reader).map_err(|err| GridError::Other(err.into()))?; - - // TODO: convert differently if grid only has one convolution - let result = Self { - subgrids: Array3::from_shape_vec( - grid.subgrids().dim(), - grid.subgrids() - .into_iter() - .map(|subgrid| { - if subgrid.is_empty() { - EmptySubgridV1.into() - } else { - let mu2_grid: Vec<_> = subgrid - .mu2_grid() - .iter() - .map(|mu2v0| Mu2 { - ren: mu2v0.ren, - fac: mu2v0.fac, - frg: -1.0, - }) - .collect(); - let x1_grid = subgrid.x1_grid().into_owned(); - let x2_grid = subgrid.x2_grid().into_owned(); - let mut array = - PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - for ((o, b, c), v) in subgrid.indexed_iter() { - array[[o, b, c]] = v; - } - PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() - } - }) - .collect(), - ) - // UNWRAP: the dimensions must be the same as in the v0 grid - .unwrap(), - channels: grid - .channels() - .iter() - .map(|c| Channel::new(c.entry().iter().map(|&(a, b, f)| (vec![a, b], f)).collect())) - .collect(), - // TODO: change this member to something much easier to handle - bin_limits: BinLimits::new(if grid.remapper().is_none() { - let limits = &grid.bin_info().limits(); - iter::once(limits[0][0].0) - .chain(limits.iter().map(|v| v[0].1)) - .collect() - } else { - // if there is a BinRemapper this member will likely have no impact - (0..=grid.bin_info().bins()) - .map(|i| f64::from(u16::try_from(i).unwrap())) - .collect() - }), - orders: grid - .orders() - .iter() - .map(|o| Order { - alphas: o.alphas, - alpha: o.alpha, - logxir: o.logxir, - logxif: o.logxif, - logxia: 0, - }) - .collect(), - // TODO: remove this member - subgrid_params: SubgridParams::default(), - metadata: grid - .key_values() - .cloned() - .unwrap_or_default() - .into_iter() - .collect(), - convolutions: Self::read_convolutions_from_metadata(&grid), - pid_basis: grid - .key_values() - .and_then(|kv| kv.get("lumi_id_types")) - .map_or(PidBasis::Pdg, |lumi_id_types| { - match lumi_id_types.as_str() { - "pdg_mc_ids" => PidBasis::Pdg, - "evol" => PidBasis::Evol, - _ => panic!("unknown PID basis '{lumi_id_types}'"), - } - }), - // TODO: make these proper members - more_members: MoreMembers::V3(Mmv3 { - remapper: grid.remapper().map(|r| { - // UNWRAP: if the old grid could be constructed with the given normalizations - // and limits we should be able to do the same without error - BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() - }), - // TODO: remove this member - subgrid_template: EmptySubgridV1.into(), - }), - }; - - assert_eq!(result.bin_info().bins(), grid.bin_info().bins()); - - Ok(result) - } - - fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { - grid.key_values().map_or_else( - // if there isn't any metadata, we assume two unpolarized proton-PDFs are used - || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], - |kv| { - // file format v0 only supports exactly two convolutions - (1..=2) - .map(|index| { - // if there are key-value pairs `convolution_particle_1` and - // `convolution_type_1` and the same with a higher index, we convert this - // metadata into `Convolution` - match ( - kv.get(&format!("convolution_particle_{index}")) - .map(|s| s.parse::()), - kv.get(&format!("convolution_type_{index}")) - .map(String::as_str), - ) { - (_, Some("None")) => Convolution::None, - (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), - (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), - (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), - (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), - (None, None) => { - // if these key-value pairs are missing use the old metadata - match kv - .get(&format!("initial_state_{index}")) - .map(|s| s.parse::()) - { - Some(Ok(pid)) => { - let condition = !grid.channels().iter().all(|entry| { - entry.entry().iter().all(|&(a, b, _)| match index {1 => a, 2 => b, _ => unreachable!()} == pid) - }); - - if condition { - Convolution::UnpolPDF(pid) - } else { - Convolution::None - } - } - None => Convolution::UnpolPDF(2212), - Some(Err(err)) => panic!( - "metadata 'initial_state_{index}' could not be parsed: {err}" - ), - } - } - (None, Some(_)) => { - panic!("metadata 'convolution_type_{index}' is missing") - } - (Some(_), None) => { - panic!("metadata 'convolution_particle_{index}' is missing") - } - (Some(Ok(_)), Some(type_)) => { - panic!("metadata 'convolution_type_{index} = {type_}' is unknown") - } - (Some(Err(err)), Some(_)) => { - panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") - } - } - }) - .collect() - }, - ) - } /// Serializes `self` into `writer`. Writing is buffered. /// /// # Errors diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index 4ffebba4..a6c579b7 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -34,6 +34,7 @@ //! [CLI tutorial]: https://nnpdf.github.io/pineappl/docs/cli-tutorial.html mod convert; +mod v0; pub mod bin; pub mod boc; diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs new file mode 100644 index 00000000..183f524a --- /dev/null +++ b/pineappl/src/v0.rs @@ -0,0 +1,179 @@ +use super::bin::{BinLimits, BinRemapper}; +use super::boc::{Channel, Order}; +use super::convolutions::Convolution; +use super::empty_subgrid::EmptySubgridV1; +use super::grid::{Grid, GridError, Mmv3, MoreMembers}; +use super::packed_array::PackedArray; +use super::packed_subgrid::PackedQ1X2SubgridV1; +use super::pids::PidBasis; +use super::subgrid::{Mu2, SubgridParams}; +use ndarray::Array3; +use pineappl_v0::grid::Grid as GridV0; +use std::io::BufRead; +use std::iter; + +pub(crate) fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { + use pineappl_v0::subgrid::Subgrid as _; + + let grid = GridV0::read(&mut reader).map_err(|err| GridError::Other(err.into()))?; + + // TODO: convert differently if grid only has one convolution + let result = Grid { + subgrids: Array3::from_shape_vec( + grid.subgrids().dim(), + grid.subgrids() + .into_iter() + .map(|subgrid| { + if subgrid.is_empty() { + EmptySubgridV1.into() + } else { + let mu2_grid: Vec<_> = subgrid + .mu2_grid() + .iter() + .map(|mu2v0| Mu2 { + ren: mu2v0.ren, + fac: mu2v0.fac, + frg: -1.0, + }) + .collect(); + let x1_grid = subgrid.x1_grid().into_owned(); + let x2_grid = subgrid.x2_grid().into_owned(); + let mut array = + PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + for ((o, b, c), v) in subgrid.indexed_iter() { + array[[o, b, c]] = v; + } + PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() + } + }) + .collect(), + ) + // UNWRAP: the dimensions must be the same as in the v0 grid + .unwrap(), + channels: grid + .channels() + .iter() + .map(|c| Channel::new(c.entry().iter().map(|&(a, b, f)| (vec![a, b], f)).collect())) + .collect(), + // TODO: change this member to something much easier to handle + bin_limits: BinLimits::new(if grid.remapper().is_none() { + let limits = &grid.bin_info().limits(); + iter::once(limits[0][0].0) + .chain(limits.iter().map(|v| v[0].1)) + .collect() + } else { + // if there is a BinRemapper this member will likely have no impact + (0..=grid.bin_info().bins()) + .map(|i| f64::from(u16::try_from(i).unwrap())) + .collect() + }), + orders: grid + .orders() + .iter() + .map(|o| Order { + alphas: o.alphas, + alpha: o.alpha, + logxir: o.logxir, + logxif: o.logxif, + logxia: 0, + }) + .collect(), + // TODO: remove this member + subgrid_params: SubgridParams::default(), + metadata: grid + .key_values() + .cloned() + .unwrap_or_default() + .into_iter() + .collect(), + convolutions: read_convolutions_from_metadata(&grid), + pid_basis: grid + .key_values() + .and_then(|kv| kv.get("lumi_id_types")) + .map_or(PidBasis::Pdg, |lumi_id_types| { + match lumi_id_types.as_str() { + "pdg_mc_ids" => PidBasis::Pdg, + "evol" => PidBasis::Evol, + _ => panic!("unknown PID basis '{lumi_id_types}'"), + } + }), + // TODO: make these proper members + more_members: MoreMembers::V3(Mmv3 { + remapper: grid.remapper().map(|r| { + // UNWRAP: if the old grid could be constructed with the given normalizations + // and limits we should be able to do the same without error + BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() + }), + // TODO: remove this member + subgrid_template: EmptySubgridV1.into(), + }), + }; + + assert_eq!(result.bin_info().bins(), grid.bin_info().bins()); + + Ok(result) +} + +fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { + grid.key_values().map_or_else( + // if there isn't any metadata, we assume two unpolarized proton-PDFs are used + || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + |kv| { + // file format v0 only supports exactly two convolutions + (1..=2) + .map(|index| { + // if there are key-value pairs `convolution_particle_1` and + // `convolution_type_1` and the same with a higher index, we convert this + // metadata into `Convolution` + match ( + kv.get(&format!("convolution_particle_{index}")) + .map(|s| s.parse::()), + kv.get(&format!("convolution_type_{index}")) + .map(String::as_str), + ) { + (_, Some("None")) => Convolution::None, + (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), + (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), + (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), + (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), + (None, None) => { + // if these key-value pairs are missing use the old metadata + match kv + .get(&format!("initial_state_{index}")) + .map(|s| s.parse::()) + { + Some(Ok(pid)) => { + let condition = !grid.channels().iter().all(|entry| { + entry.entry().iter().all(|&(a, b, _)| match index {1 => a, 2 => b, _ => unreachable!()} == pid) + }); + + if condition { + Convolution::UnpolPDF(pid) + } else { + Convolution::None + } + } + None => Convolution::UnpolPDF(2212), + Some(Err(err)) => panic!( + "metadata 'initial_state_{index}' could not be parsed: {err}" + ), + } + } + (None, Some(_)) => { + panic!("metadata 'convolution_type_{index}' is missing") + } + (Some(_), None) => { + panic!("metadata 'convolution_particle_{index}' is missing") + } + (Some(Ok(_)), Some(type_)) => { + panic!("metadata 'convolution_type_{index} = {type_}' is unknown") + } + (Some(Err(err)), Some(_)) => { + panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") + } + } + }) + .collect() + }, + ) +} From 501a29af11bf63b91f66c083430a2daed8d5d41b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 22 Aug 2024 17:57:04 +0200 Subject: [PATCH 061/213] Rename `pdg_channels` to `channels_pdg` --- pineappl/src/grid.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 7e0c752f..20fe01ef 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -232,17 +232,6 @@ impl Grid { &mut self.pid_basis } - fn pdg_channels(&self) -> Cow<[Channel]> { - match self.pid_basis() { - PidBasis::Evol => self - .channels - .iter() - .map(|entry| Channel::translate(entry, &pids::evol_to_pdg_mc_ids)) - .collect(), - PidBasis::Pdg => Cow::Borrowed(self.channels()), - } - } - /// Perform a convolution using the PDFs and strong coupling in `lumi_cache`, and only /// selecting only the orders, bins and channels corresponding to `order_mask`, `bin_indices` /// and `channel_mask`. A variation of the scales is performed using the factors in `xi`; the @@ -276,7 +265,7 @@ impl Grid { }; let mut bins = vec![0.0; bin_indices.len() * xi.len()]; let normalizations = self.bin_info().normalizations(); - let pdg_channels = self.pdg_channels(); + let pdg_channels = self.channels_pdg(); for (xi_index, &(xir, xif)) in xi.iter().enumerate() { for ((ord, bin, chan), subgrid) in self.subgrids.indexed_iter() { @@ -363,7 +352,7 @@ impl Grid { lumi_cache.setup(self, &[(xir, xif)]).unwrap(); let normalizations = self.bin_info().normalizations(); - let pdg_channels = self.pdg_channels(); + let pdg_channels = self.channels_pdg(); let subgrid = &self.subgrids[[ord, bin, channel]]; let order = &self.orders[ord]; @@ -512,6 +501,17 @@ impl Grid { &self.channels } + fn channels_pdg(&self) -> Cow<[Channel]> { + match self.pid_basis() { + PidBasis::Evol => self + .channels + .iter() + .map(|entry| Channel::translate(entry, &pids::evol_to_pdg_mc_ids)) + .collect(), + PidBasis::Pdg => Cow::Borrowed(self.channels()), + } + } + /// Merges the bins for the corresponding range together in a single one. /// /// # Errors From 2c9ade2500854c268c4a649dc68b9f75398779fc Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sat, 24 Aug 2024 10:53:19 +0200 Subject: [PATCH 062/213] Update `managed-lhapdf` from 0.3.3 to 0.3.4 --- Cargo.lock | 597 ++--------------------------------- pineappl/Cargo.toml | 2 +- pineappl_applgrid/Cargo.toml | 2 +- pineappl_cli/Cargo.toml | 4 +- 4 files changed, 32 insertions(+), 573 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 426c3a49..eaf5c705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "addr2line" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" -dependencies = [ - "gimli", -] - [[package]] name = "adler" version = "1.0.2" @@ -122,26 +113,11 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" -[[package]] -name = "backtrace" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - [[package]] name = "base64" -version = "0.21.7" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "bincode" @@ -184,24 +160,12 @@ dependencies = [ "serde", ] -[[package]] -name = "bumpalo" -version = "3.15.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" - [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" -[[package]] -name = "bytes" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" - [[package]] name = "cc" version = "1.0.92" @@ -521,12 +485,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "form_urlencoded" version = "1.2.1" @@ -546,56 +504,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "futures-channel" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" -dependencies = [ - "futures-core", - "futures-sink", -] - -[[package]] -name = "futures-core" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" - -[[package]] -name = "futures-io" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" - -[[package]] -name = "futures-sink" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" - -[[package]] -name = "futures-task" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" - -[[package]] -name = "futures-util" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" -dependencies = [ - "futures-core", - "futures-io", - "futures-sink", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - [[package]] name = "generic-array" version = "0.14.7" @@ -617,12 +525,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "gimli" -version = "0.28.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" - [[package]] name = "git-version" version = "0.3.9" @@ -685,102 +587,6 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" -[[package]] -name = "http" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" -dependencies = [ - "bytes", - "http", -] - -[[package]] -name = "http-body-util" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" -dependencies = [ - "bytes", - "futures-core", - "http", - "http-body", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" - -[[package]] -name = "hyper" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "http", - "http-body", - "httparse", - "itoa", - "pin-project-lite", - "smallvec", - "tokio", - "want", -] - -[[package]] -name = "hyper-rustls" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" -dependencies = [ - "futures-util", - "http", - "hyper", - "hyper-util", - "rustls", - "rustls-pki-types", - "tokio", - "tokio-rustls", - "tower-service", -] - -[[package]] -name = "hyper-util" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "http", - "http-body", - "hyper", - "pin-project-lite", - "socket2", - "tokio", - "tower", - "tower-service", - "tracing", -] - [[package]] name = "idna" version = "0.5.0" @@ -823,12 +629,6 @@ version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8" -[[package]] -name = "ipnet" -version = "2.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" - [[package]] name = "is-terminal" version = "0.4.10" @@ -855,15 +655,6 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" -[[package]] -name = "js-sys" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -929,9 +720,9 @@ dependencies = [ [[package]] name = "managed-lhapdf" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f930f0fd8c87a299efd84e910c2c0db3cd04fcbfe2952ccdced47dd830dc017c" +checksum = "aab1b15ac56f2746106430a2cc6e8ae1d92d6342bd363bb7973be29df75ae6f4" dependencies = [ "anyhow", "cxx", @@ -940,11 +731,11 @@ dependencies = [ "flate2", "fs2", "pkg-config", - "reqwest", "serde", "tar", "thiserror", "toml", + "ureq", ] [[package]] @@ -972,12 +763,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - [[package]] name = "miniz_oxide" version = "0.7.1" @@ -987,17 +772,6 @@ dependencies = [ "adler", ] -[[package]] -name = "mio" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" -dependencies = [ - "libc", - "wasi", - "windows-sys 0.48.0", -] - [[package]] name = "ndarray" version = "0.15.6" @@ -1071,16 +845,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "numpy" version = "0.21.0" @@ -1096,15 +860,6 @@ dependencies = [ "rustc-hash", ] -[[package]] -name = "object" -version = "0.32.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.19.0" @@ -1191,38 +946,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "pin-project" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "pin-project-lite" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "pineappl" version = "0.8.2" @@ -1589,48 +1312,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" -[[package]] -name = "reqwest" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d66674f2b6fb864665eea7a3c1ac4e3dfacd2fda83cf6f935a612e01b0e3338" -dependencies = [ - "base64", - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-rustls", - "hyper-util", - "ipnet", - "js-sys", - "log", - "mime", - "once_cell", - "percent-encoding", - "pin-project-lite", - "rustls", - "rustls-pemfile", - "rustls-pki-types", - "serde", - "serde_json", - "serde_urlencoded", - "sync_wrapper", - "tokio", - "tokio-rustls", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "webpki-roots", - "winreg", -] - [[package]] name = "ring" version = "0.17.8" @@ -1652,12 +1333,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" -[[package]] -name = "rustc-demangle" -version = "0.1.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" - [[package]] name = "rustc-hash" version = "1.1.0" @@ -1679,11 +1354,12 @@ dependencies = [ [[package]] name = "rustls" -version = "0.22.4" +version = "0.23.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432" +checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" dependencies = [ "log", + "once_cell", "ring", "rustls-pki-types", "rustls-webpki", @@ -1691,26 +1367,17 @@ dependencies = [ "zeroize", ] -[[package]] -name = "rustls-pemfile" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" -dependencies = [ - "base64", -] - [[package]] name = "rustls-pki-types" -version = "1.4.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "868e20fada228fefaf6b652e00cc73623d54f8171e7352c18bb281571f2d92da" +checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" [[package]] name = "rustls-webpki" -version = "0.102.2" +version = "0.102.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "faaa0a62740bedb9b2ef5afa303da42764c012f743917351dc9a237ea1663610" +checksum = "8e6b52d4fda176fd835fdc55a835d4a89b8499cad995885a21149d5ad62f852e" dependencies = [ "ring", "rustls-pki-types", @@ -1770,17 +1437,6 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_json" -version = "1.0.114" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" -dependencies = [ - "itoa", - "ryu", - "serde", -] - [[package]] name = "serde_spanned" version = "0.6.5" @@ -1790,18 +1446,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - [[package]] name = "serde_yaml" version = "0.9.30" @@ -1826,31 +1470,12 @@ dependencies = [ "digest", ] -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - [[package]] name = "smallvec" version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" -[[package]] -name = "socket2" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "spin" version = "0.9.8" @@ -1886,12 +1511,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_wrapper" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" - [[package]] name = "tar" version = "0.4.40" @@ -1983,33 +1602,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" -[[package]] -name = "tokio" -version = "1.36.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" -dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "num_cpus", - "pin-project-lite", - "socket2", - "windows-sys 0.48.0", -] - -[[package]] -name = "tokio-rustls" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" -dependencies = [ - "rustls", - "rustls-pki-types", - "tokio", -] - [[package]] name = "toml" version = "0.8.12" @@ -2044,60 +1636,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "tower" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" -dependencies = [ - "futures-core", - "futures-util", - "pin-project", - "pin-project-lite", - "tokio", - "tower-layer", - "tower-service", - "tracing", -] - -[[package]] -name = "tower-layer" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" - -[[package]] -name = "tower-service" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" - -[[package]] -name = "tracing" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" -dependencies = [ - "log", - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" -dependencies = [ - "once_cell", -] - -[[package]] -name = "try-lock" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" - [[package]] name = "twox-hash" version = "1.6.3" @@ -2165,6 +1703,22 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "ureq" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74fc6b57825be3373f7054754755f03ac3a8f5d70015ccad699ba2029956f4a" +dependencies = [ + "base64", + "flate2", + "log", + "once_cell", + "rustls", + "rustls-pki-types", + "url", + "webpki-roots", +] + [[package]] name = "url" version = "2.5.0" @@ -2207,97 +1761,12 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "want" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" -dependencies = [ - "try-lock", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" -dependencies = [ - "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" - -[[package]] -name = "web-sys" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "webpki-roots" version = "0.26.1" @@ -2479,16 +1948,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "winreg" -version = "0.50.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" -dependencies = [ - "cfg-if", - "windows-sys 0.48.0", -] - [[package]] name = "xattr" version = "1.3.1" diff --git a/pineappl/Cargo.toml b/pineappl/Cargo.toml index 9c124be2..e028d92e 100644 --- a/pineappl/Cargo.toml +++ b/pineappl/Cargo.toml @@ -33,7 +33,7 @@ thiserror = "1.0.30" [dev-dependencies] anyhow = "1.0.48" -lhapdf = { package = "managed-lhapdf", version = "0.3.2" } +lhapdf = { package = "managed-lhapdf", version = "0.3.4" } num-complex = "0.4.4" rand = { default-features = false, version = "0.8.4" } rand_pcg = { default-features = false, version = "0.3.1" } diff --git a/pineappl_applgrid/Cargo.toml b/pineappl_applgrid/Cargo.toml index fde6ef3a..f00b3db5 100644 --- a/pineappl_applgrid/Cargo.toml +++ b/pineappl_applgrid/Cargo.toml @@ -17,7 +17,7 @@ workspace = true [dependencies] cxx = "1.0.65" -lhapdf = { package = "managed-lhapdf", version = "0.3.2" } +lhapdf = { package = "managed-lhapdf", version = "0.3.4" } [build-dependencies] cc = "1.0.49" diff --git a/pineappl_cli/Cargo.toml b/pineappl_cli/Cargo.toml index 0e2ebe82..6c74ccf6 100644 --- a/pineappl_cli/Cargo.toml +++ b/pineappl_cli/Cargo.toml @@ -17,7 +17,7 @@ workspace = true [dependencies] anyhow = "1.0.48" -base64 = { optional = true, version = "0.21.0" } +base64 = { optional = true, version = "0.22.1" } clap = { features = ["derive"], version = "4.4.18" } cxx = { optional = true, version = "1.0.65" } either = { features = ["serde"], optional = true, version = "1.8.0" } @@ -26,7 +26,7 @@ flate2 = { optional = true, version = "1.0.22" } float-cmp = "0.9.0" git-version = "0.3.5" itertools = "0.10.1" -lhapdf = { package = "managed-lhapdf", version = "0.3.2" } +lhapdf = { package = "managed-lhapdf", version = "0.3.4" } lz4_flex = { optional = true, version = "0.9.2" } ndarray = "0.15.4" ndarray-npy = { optional = true, version = "0.8.1" } From ebf677086533bb54d7a54b2932a33ba2bf992b4c Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 1 Sep 2024 09:18:39 +0200 Subject: [PATCH 063/213] Remove deprecated `Grid::evolve` method --- pineappl/src/grid.rs | 48 ++------------------------ pineappl_cli/src/evolve.rs | 65 ++++++++---------------------------- pineappl_cli/tests/evolve.rs | 20 ----------- 3 files changed, 16 insertions(+), 117 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 4ef19bf2..9b8afeb6 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -4,7 +4,7 @@ use super::bin::{BinInfo, BinLimits, BinRemapper}; use super::boc::{Channel, Order}; use super::convolutions::{Convolution, LumiCache}; use super::empty_subgrid::EmptySubgridV1; -use super::evolution::{self, AlphasTable, EvolveInfo, OperatorInfo, OperatorSliceInfo}; +use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; @@ -15,7 +15,7 @@ use bitflags::bitflags; use float_cmp::approx_eq; use git_version::git_version; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; -use ndarray::{s, Array3, ArrayView3, ArrayView5, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; +use ndarray::{s, Array3, ArrayView3, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::collections::BTreeMap; @@ -1137,50 +1137,6 @@ impl Grid { } } - /// Converts this `Grid` into an [`FkTable`] using an evolution kernel operator (EKO) given as - /// `operator`. The dimensions and properties of this operator must be described using `info`. - /// The parameter `order_mask` can be used to include or exclude orders from this operation, - /// and must correspond to the ordering given by [`Grid::orders`]. Orders that are not given - /// are enabled, and in particular if `order_mask` is empty all orders are activated. - /// - /// # Errors - /// - /// Returns a [`GridError::EvolutionFailure`] if either the `operator` or its `info` is - /// incompatible with this `Grid`. - #[deprecated(since = "0.7.4", note = "use evolve_with_slice_iter instead")] - pub fn evolve( - &self, - operator: ArrayView5, - info: &OperatorInfo, - order_mask: &[bool], - ) -> Result { - self.evolve_with_slice_iter( - info.fac1 - .iter() - .zip(operator.axis_iter(Axis(0))) - .map(|(&fac1, op)| { - Ok::<_, GridError>(( - OperatorSliceInfo { - fac0: info.fac0, - pids0: info.pids0.clone(), - x0: info.x0.clone(), - fac1, - pids1: info.pids1.clone(), - x1: info.x1.clone(), - pid_basis: info.pid_basis, - }, - CowArray::from(op), - )) - }), - order_mask, - (info.xir, info.xif), - &AlphasTable { - ren1: info.ren1.clone(), - alphas: info.alphas.clone(), - }, - ) - } - // TODO: // - try to find a better solution than to require that E must be convertible into // anyhow::Error diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index 57933744..fb168626 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -429,11 +429,9 @@ fn evolve_grid( orders: &[(u32, u32)], xir: f64, xif: f64, - use_old_evolve: bool, ) -> Result { - use anyhow::bail; use eko::EkoSlices; - use pineappl::evolution::{AlphasTable, OperatorInfo}; + use pineappl::evolution::AlphasTable; let order_mask: Vec<_> = grid .orders() @@ -452,51 +450,19 @@ fn evolve_grid( .collect::>()?; let alphas_table = AlphasTable::from_grid(grid, xir, &|q2| use_alphas_from.alphas_q2(q2)); - if use_old_evolve { - assert_eq!(eko_slices.len(), 1); - - if let EkoSlices::V0 { - fac1, - info, - operator, - } = eko_slices.remove(0) - { - let op_info = OperatorInfo { - fac0: info.fac0, - pids0: info.pids0.clone(), - x0: info.x0.clone(), - fac1, - pids1: info.pids1.clone(), - x1: info.x1.clone(), - ren1: alphas_table.ren1, - alphas: alphas_table.alphas, - xir, - xif, - pid_basis: info.pid_basis, - }; - - #[allow(deprecated)] - Ok(grid.evolve(operator.view(), &op_info, &order_mask)?) - } else { - bail!("`--use-old-evolve` can only be used with the old EKO format (`V0`)") - } - } else { - match eko_slices.as_mut_slice() { - [eko] => { - Ok(grid.evolve_with_slice_iter(eko, &order_mask, (xir, xif), &alphas_table)?) - } - [eko_a, eko_b] => Ok(grid.evolve_with_slice_iter2( - eko_a, - eko_b, - &order_mask, - (xir, xif), - &alphas_table, - )?), - _ => unimplemented!( - "evolution with {} EKOs is not implemented", - eko_slices.len() - ), - } + match eko_slices.as_mut_slice() { + [eko] => Ok(grid.evolve_with_slice_iter(eko, &order_mask, (xir, xif), &alphas_table)?), + [eko_a, eko_b] => Ok(grid.evolve_with_slice_iter2( + eko_a, + eko_b, + &order_mask, + (xir, xif), + &alphas_table, + )?), + _ => unimplemented!( + "evolution with {} EKOs is not implemented", + eko_slices.len() + ), } } @@ -559,8 +525,6 @@ pub struct Opts { /// Rescale the fragmentation scale with this factor. #[arg(default_value_t = 1.0, long)] xia: f64, - #[arg(hide = true, long)] - use_old_evolve: bool, } impl Subcommand for Opts { @@ -590,7 +554,6 @@ impl Subcommand for Opts { &self.orders, self.xir, self.xif, - self.use_old_evolve, )?; let evolved_results = helpers::convolve_scales( diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index 7bcf0ab3..2fda4e27 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -238,26 +238,6 @@ fn lhcb_wp_7tev() { .stdout(LHCB_WP_7TEV_OPTIMIZED_STR); } -#[test] -fn lhcb_wp_7tev_use_old_evolve() { - let output = NamedTempFile::new("fktable1c.lz4").unwrap(); - - Command::cargo_bin("pineappl") - .unwrap() - .args([ - "evolve", - "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", - "../test-data/LHCB_WP_7TEV.tar", - output.path().to_str().unwrap(), - "NNPDF40_nlo_as_01180", - "--orders=a2,as1a2", - "--use-old-evolve", - ]) - .assert() - .success() - .stdout(LHCB_WP_7TEV_STR); -} - #[test] fn lhcb_wp_7tev_v2() { let output = NamedTempFile::new("fktable2a.lz4").unwrap(); From a1365f2b878f59c3cd50f874ed3ae87024f444de Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 1 Sep 2024 09:19:49 +0200 Subject: [PATCH 064/213] Remove struct `OperatorInfo` --- pineappl/src/evolution.rs | 56 --------------------------------------- 1 file changed, 56 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index e8ac23f9..48d4c890 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -36,62 +36,6 @@ pub struct EvolveInfo { pub ren1: Vec, } -/// Information about the evolution kernel operator (EKO) passed to [`Grid::evolve`] as `operator`, -/// which is used to convert a [`Grid`] into an [`FkTable`]. The dimensions of the EKO must -/// correspond to the values given in [`fac1`], [`pids0`], [`x0`], [`pids1`] and [`x1`], exactly in -/// this order. Members with a `1` are defined at the squared factorization scales given in -/// [`fac1`] (often called process scales) and are found in the [`Grid`] that [`Grid::evolve`] is -/// called with. Members with a `0` are defined at the squared factorization scale [`fac0`] (often -/// called fitting scale or starting scale) and are found in the [`FkTable`] resulting from -/// [`Grid::evolve`]. -/// -/// The EKO may convert a `Grid` from a basis given by the particle identifiers [`pids1`] to a -/// possibly different basis given by [`pids0`]. This basis must also be identified using -/// [`pid_basis`], which tells [`FkTable::convolve`] how to perform a convolution. The members -/// [`ren1`] and [`alphas`] must be the strong couplings given at the respective renormalization -/// scales. Finally, [`xir`] and [`xif`] can be used to vary the renormalization and factorization -/// scales, respectively, around their central values. -/// -/// [`FkTable::convolve`]: super::fk_table::FkTable::convolve -/// [`FkTable`]: super::fk_table::FkTable -/// [`alphas`]: Self::alphas -/// [`fac0`]: Self::fac0 -/// [`fac1`]: Self::fac1 -/// [`pid_basis`]: Self::pid_basis -/// [`pids0`]: Self::pids0 -/// [`pids1`]: Self::pids1 -/// [`ren1`]: Self::ren1 -/// [`x0`]: Self::x0 -/// [`x1`]: Self::x1 -/// [`xif`]: Self::xif -/// [`xir`]: Self::xir -pub struct OperatorInfo { - /// Squared factorization scale of the `FkTable`. - pub fac0: f64, - /// Particle identifiers of the `FkTable`. - pub pids0: Vec, - /// `x`-grid coordinates of the `FkTable` - pub x0: Vec, - /// Squared factorization scales of the `Grid`. - pub fac1: Vec, - /// Particle identifiers of the `Grid`. If the `Grid` contains more particle identifiers than - /// given here, the contributions of them are silently ignored. - pub pids1: Vec, - /// `x`-grid coordinates of the `Grid`. - pub x1: Vec, - - /// Renormalization scales of the `Grid`. - pub ren1: Vec, - /// Strong couplings corresponding to the order given in [`ren1`](Self::ren1). - pub alphas: Vec, - /// Multiplicative factor for the central renormalization scale. - pub xir: f64, - /// Multiplicative factor for the central factorization scale. - pub xif: f64, - /// Particle ID basis for `FkTable`. - pub pid_basis: PidBasis, -} - /// Information about the evolution kernel operator slice (EKO) passed to /// [`Grid::evolve_with_slice_iter`](super::grid::Grid::evolve_with_slice_iter) as `operator`, /// which is used to convert a [`Grid`] into an [`FkTable`](super::fk_table::FkTable). The From 2b21919c46265530bc349951f28fb60e4572309d Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 1 Sep 2024 09:20:57 +0200 Subject: [PATCH 065/213] Fix one clippy warning --- pineappl/src/v0.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 183f524a..14dea165 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -12,7 +12,7 @@ use pineappl_v0::grid::Grid as GridV0; use std::io::BufRead; use std::iter; -pub(crate) fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { +pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { use pineappl_v0::subgrid::Subgrid as _; let grid = GridV0::read(&mut reader).map_err(|err| GridError::Other(err.into()))?; From 89e3daacb0d266b54a193fcef8179eb08e4d9112 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 1 Sep 2024 12:26:11 +0200 Subject: [PATCH 066/213] Add preliminary interpolation module --- pineappl/src/interpolation.rs | 242 ++++++++++++++++++++++++++++++++++ pineappl/src/lib.rs | 1 + 2 files changed, 243 insertions(+) create mode 100644 pineappl/src/interpolation.rs diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs new file mode 100644 index 00000000..c73802f2 --- /dev/null +++ b/pineappl/src/interpolation.rs @@ -0,0 +1,242 @@ +//! TODO + +use super::convert; +use arrayvec::ArrayVec; +use std::ops::IndexMut; + +mod applgrid { + pub fn reweight_x(x: f64) -> f64 { + (x.sqrt() / (1.0 - 0.99 * x)).powi(3) + } + + pub fn fx2(y: f64) -> f64 { + let mut yp = y; + + for _ in 0..100 { + let x = (-yp).exp(); + let delta = y - yp - 5.0 * (1.0 - x); + if (delta).abs() < 1e-12 { + return x; + } + let deriv = -1.0 - 5.0 * x; + yp -= delta / deriv; + } + + unreachable!(); + } + + pub fn fy2(x: f64) -> f64 { + (1.0 - x).mul_add(5.0, -x.ln()) + } + + pub fn ftau0(q2: f64) -> f64 { + (q2 / 0.0625).ln().ln() + } + + pub fn fq20(tau: f64) -> f64 { + 0.0625 * tau.exp().exp() + } +} + +fn no_reweight(_: f64) -> f64 { + 1.0 +} + +fn lagrange_weights(i: usize, n: usize, u: f64) -> f64 { + let mut factorials = 1; + let mut product = 1.0; + for z in 0..i { + product *= u - convert::f64_from_usize(z); + factorials *= i - z; + } + for z in i + 1..=n { + product *= convert::f64_from_usize(z) - u; + factorials *= z - i; + } + product / convert::f64_from_usize(factorials) +} + +/// TODO +pub enum ReweightMeth { + /// TODO + ApplGridX, + /// TODO + NoReweight, +} + +/// TODO +pub enum Map { + /// TODO + ApplGridF2, + /// TODO + ApplGridH0, +} + +/// TODO +pub enum InterpMeth { + /// TODO + Lagrange, +} + +/// TODO +pub struct Interp { + min: f64, + max: f64, + nodes: usize, + order: usize, + reweight_x: fn(f64) -> f64, + map_x_to_y: fn(f64) -> f64, + map_y_to_x: fn(f64) -> f64, + node_weights: fn(usize, usize, f64) -> f64, +} + +impl Interp { + /// TODO + /// + /// # Panics + /// + /// Panics if `nodes` is `0`, or if `nodes` is smaller or equal to `order`. + pub fn new( + min: f64, + max: f64, + nodes: usize, + order: usize, + reweight: ReweightMeth, + map: Map, + interp_meth: InterpMeth, + ) -> Self { + assert!(nodes > 0); + assert!(nodes > order); + + let mut result = Self { + min: 0.0, + max: 0.0, + nodes, + order, + reweight_x: match reweight { + ReweightMeth::ApplGridX => applgrid::reweight_x, + ReweightMeth::NoReweight => no_reweight, + }, + map_x_to_y: match map { + Map::ApplGridF2 => applgrid::fx2, + Map::ApplGridH0 => applgrid::fq20, + }, + map_y_to_x: match map { + Map::ApplGridF2 => applgrid::fy2, + Map::ApplGridH0 => applgrid::ftau0, + }, + node_weights: match interp_meth { + InterpMeth::Lagrange => lagrange_weights, + }, + }; + + result.min = (result.map_x_to_y)(min); + result.max = (result.map_x_to_y)(max); + + result + } + + fn deltay(&self) -> f64 { + (self.max - self.min) / convert::f64_from_usize(self.nodes - 1) + } + + fn gety(&self, index: usize) -> f64 { + convert::f64_from_usize(index).mul_add(self.deltay(), self.min) + } + + /// TODO + pub fn reweight(&self, x: f64) -> f64 { + (self.reweight_x)(x) + } + + /// TODO + pub fn interpolate(&self, x: f64) -> Option<(usize, f64)> { + let y = (self.map_x_to_y)(x); + + // points falling outside the interpolation range shouldn't happen very often, because when + // it does it degrades the interpolation quality + if (self.min > y) || (y > self.max) { + return None; + } + + if self.nodes == 1 { + // TODO: is the `y_fraction` correct? + Some((0, 0.0)) + } else { + let index = convert::usize_from_f64( + (y - self.min) / self.deltay() - convert::f64_from_usize(self.order / 2), + ) + .min(self.nodes - self.order - 1); + let y_fraction = (y - self.gety(index)) / self.deltay(); + + Some((index, y_fraction)) + } + } + + /// TODO + pub fn node_weights(&self, fraction: f64) -> ArrayVec { + (0..=self.order) + .map(|i| (self.node_weights)(i, self.order, fraction)) + .collect() + } +} + +/// TODO +pub fn interpolate( + interps: &[Interp], + ntuple: &[f64], + weight: f64, + _array: &impl IndexMut, +) { + if weight == 0.0 { + return; + } + + // we must have as many variables as we want to interpolate + debug_assert_eq!(interps.len(), ntuple.len()); + debug_assert_eq!(interps.len(), D); + + let Some(result): Option> = interps + .iter() + .zip(ntuple) + .map(|(interp, &x)| interp.interpolate(x)) + .collect() + else { + return; + }; + + //if self.static_q2 == 0.0 { + // self.static_q2 = q2; + //} else if (self.static_q2 != -1.0) && (self.static_q2 != q2) { + // self.static_q2 = -1.0; + //} + + let weight = weight + / interps + .iter() + .zip(ntuple) + .map(|(interp, &x)| interp.reweight(x)) + .product::(); + + let node_weights: ArrayVec<_, D> = interps + .iter() + .zip(result) + .map(|(interp, (_, fraction))| interp.node_weights(fraction)) + .collect(); + + //for i3 in 0..=self.tauorder { + // let fi3i3 = fi(i3, self.tauorder, u_tau); + + // for (i1, fi1i1) in fi1.iter().enumerate() { + // for (i2, fi2i2) in fi2.iter().enumerate() { + // let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * weight; + + // let grid = self + // .grid + // .get_or_insert_with(|| Array3::zeros((size, ny1, ny2))); + + // grid[[k3 + i3 - self.itaumin, k1 + i1, k2 + i2]] += fillweight; + // } + // } + //} +} diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index a6c579b7..33b7add8 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -43,6 +43,7 @@ pub mod empty_subgrid; pub mod evolution; pub mod fk_table; pub mod grid; +pub mod interpolation; pub mod lagrange_subgrid; pub mod packed_array; pub mod packed_subgrid; From 9179d061f8b3813bf5c9e089b4656ea20c11eaa8 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 3 Sep 2024 09:00:06 +0200 Subject: [PATCH 067/213] Fix documentation links --- pineappl/src/boc.rs | 10 +++++----- pineappl/src/evolution.rs | 4 ++-- pineappl/src/grid.rs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 2c54b996..a8d9aed0 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -121,13 +121,13 @@ impl Order { } /// Return a mask suitable to pass as the `order_mask` parameter of [`Grid::convolve`], - /// [`Grid::evolve`] or [`Grid::evolve_info`]. The selection of `orders` is controlled using - /// the `max_as` and `max_al` parameters, for instance setting `max_as = 1` and `max_al = 0` - /// selects the LO QCD only, `max_as = 2` and `max_al = 0` the NLO QCD; setting `max_as = 3` - /// and `max_al = 2` would select all NLOs, and the NNLO QCD. + /// [`Grid::evolve_with_slice_iter`] or [`Grid::evolve_info`]. The selection of `orders` is + /// controlled using the `max_as` and `max_al` parameters, for instance setting `max_as = 1` + /// and `max_al = 0` selects the LO QCD only, `max_as = 2` and `max_al = 0` the NLO QCD; + /// setting `max_as = 3` and `max_al = 2` would select all NLOs, and the NNLO QCD. /// /// [`Grid::convolve`]: super::grid::Grid::convolve - /// [`Grid::evolve`]: super::grid::Grid::evolve + /// [`Grid::evolve_with_slice_iter`]: super::grid::Grid::evolve_with_slice_iter /// [`Grid::evolve_info`]: super::grid::Grid::evolve_info /// /// # Example diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 48d4c890..47e282d3 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -1,4 +1,4 @@ -//! Supporting classes and functions for [`Grid::evolve`]. +//! Supporting classes and functions for [`Grid::evolve_with_slice_iter`]. use super::boc::{Channel, Order}; use super::channel; @@ -45,7 +45,7 @@ pub struct EvolveInfo { /// `fac1` (often called process scale) and are found in the [`Grid`] that /// `Grid::evolve_with_slice_iter` is called with. Members with a `0` are defined at the squared /// factorization scale [`fac0`](Self::fac0) (often called fitting scale or starting scale) and are -/// found in the `FkTable` resulting from [`Grid::evolve`]. +/// found in the `FkTable` resulting from [`Grid::evolve_with_slice_iter`]. /// /// The EKO slice may convert a `Grid` from a basis given by the particle identifiers `pids1` to a /// possibly different basis given by `pids0`. This basis must also be identified using diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 9b8afeb6..11ea0620 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -61,7 +61,7 @@ pub enum GridError { /// File format version of the file read. file_version: u64, }, - /// Returned from [`Grid::evolve`] if the evolution failed. + /// Returned from [`Grid::evolve_with_slice_iter`] if the evolution failed. #[error("failed to evolve grid: {0}")] EvolutionFailure(String), /// Errors that do no originate from this crate itself. @@ -1066,7 +1066,7 @@ impl Grid { } /// Returns information for the generation of evolution operators that are being used in - /// [`Grid::evolve`] with the parameter `order_mask`. + /// [`Grid::convolve`] with the parameter `order_mask`. #[must_use] pub fn evolve_info(&self, order_mask: &[bool]) -> EvolveInfo { use super::evolution::EVOLVE_INFO_TOL_ULPS; From f6a8088b6f55e324b6afbcf031e95db3756f39c5 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 3 Sep 2024 09:00:37 +0200 Subject: [PATCH 068/213] Remove additional argument --- pineappl_cli/src/evolve.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index fb168626..1c61922e 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -474,7 +474,6 @@ fn evolve_grid( _: &[(u32, u32)], _: f64, _: f64, - _: bool, ) -> Result { Err(anyhow!( "you need to install `pineappl` with feature `evolve`" From 4a7cd59c2c38ce0b4c7e6dcce8b65a5163824528 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 3 Sep 2024 09:01:46 +0200 Subject: [PATCH 069/213] Add new method `Grid::charge_conjugate` --- pineappl/src/grid.rs | 24 +++++++++++ pineappl_cli/src/write.rs | 82 +++++++++++++------------------------ pineappl_cli/tests/write.rs | 12 +++--- 3 files changed, 58 insertions(+), 60 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 11ea0620..3296bad6 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -689,6 +689,30 @@ impl Grid { &mut self.convolutions } + /// Charge conjugate both the convolution function with index `convolution` and the PIDs in the + /// channel definition corresponding to it. This leaves the the results returned by + /// [`Grid::convolve`] invariant. + pub fn charge_conjugate(&mut self, convolution: usize) { + let pid_basis = *self.pid_basis(); + + for channel in self.channels_mut() { + *channel = Channel::new( + channel + .entry() + .iter() + .cloned() + .map(|(mut pids, f)| { + let (cc_pid, f1) = pid_basis.charge_conjugate(pids[convolution]); + pids[convolution] = cc_pid; + (pids, f * f1) + }) + .collect(), + ); + } + + self.convolutions_mut()[convolution] = self.convolutions()[convolution].charge_conjugate(); + } + fn increase_shape(&mut self, new_dim: &(usize, usize, usize)) { let old_dim = self.subgrids.raw_dim().into_pattern(); let mut new_subgrids = Array3::from_shape_simple_fn( diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index 8538b0ca..3c264b38 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -30,8 +30,7 @@ pub struct Opts { #[derive(Clone)] enum OpsArg { - Cc1(bool), - Cc2(bool), + Cc(usize), DedupChannels(i64), DeleteBins(Vec>), DeleteChannels(Vec>), @@ -73,7 +72,23 @@ impl FromArgMatches for MoreArgs { args.resize(indices.iter().max().unwrap() + 1, None); match id.as_str() { - "cc1" | "cc2" | "optimize" | "split_channels" | "upgrade" => { + "cc" => { + let arguments: Vec> = matches + .remove_occurrences(&id) + .unwrap() + .map(Iterator::collect) + .collect(); + assert_eq!(arguments.len(), indices.len()); + + for (index, arg) in indices.into_iter().zip(arguments.into_iter()) { + assert_eq!(arg.len(), 1); + args[index] = Some(match id.as_str() { + "cc" => OpsArg::Cc(arg[0]), + _ => unreachable!(), + }); + } + } + "optimize" | "split_channels" | "upgrade" => { let arguments: Vec> = matches .remove_occurrences(&id) .unwrap() @@ -84,8 +99,6 @@ impl FromArgMatches for MoreArgs { for (index, arg) in indices.into_iter().zip(arguments.into_iter()) { assert_eq!(arg.len(), 1); args[index] = Some(match id.as_str() { - "cc1" => OpsArg::Cc1(arg[0]), - "cc2" => OpsArg::Cc2(arg[0]), "optimize" => OpsArg::Optimize(arg[0]), "split_channels" => OpsArg::SplitChannels(arg[0]), "upgrade" => OpsArg::Upgrade(arg[0]), @@ -265,26 +278,13 @@ impl FromArgMatches for MoreArgs { impl Args for MoreArgs { fn augment_args(cmd: Command) -> Command { cmd.arg( - Arg::new("cc1") + Arg::new("cc") .action(ArgAction::Append) - .default_missing_value("true") - .help("Charge conjugate the first initial state") - .long("cc1") - .num_args(0..=1) - .require_equals(true) - .value_name("ENABLE") - .value_parser(clap::value_parser!(bool)), - ) - .arg( - Arg::new("cc2") - .action(ArgAction::Append) - .default_missing_value("true") - .help("Charge conjugate the second initial state") - .long("cc2") - .num_args(0..=1) - .require_equals(true) - .value_name("ENABLE") - .value_parser(clap::value_parser!(bool)), + .help("Charge conjugate the convolution with the specified index") + .long("cc") + .num_args(1) + .value_name("IDX") + .value_parser(clap::value_parser!(usize)), ) .arg( Arg::new("dedup_channels") @@ -508,31 +508,8 @@ impl Subcommand for Opts { for arg in &self.more_args.args { match arg { // TODO: generalize to arbitrary convolutions - OpsArg::Cc1(true) | OpsArg::Cc2(true) => { - let index = match arg { - OpsArg::Cc1(true) => 0, - OpsArg::Cc2(true) => 1, - _ => unreachable!(), - }; - - let pid_basis = *grid.pid_basis(); - - for channel in grid.channels_mut() { - *channel = Channel::new( - channel - .entry() - .iter() - .map(|(pids, f)| { - let mut cc_pids = pids.clone(); - let (cc_pid, f1) = pid_basis.charge_conjugate(pids[index]); - cc_pids[index] = cc_pid; - (cc_pids, f * f1) - }) - .collect(), - ); - } - - grid.convolutions_mut()[index] = grid.convolutions()[index].charge_conjugate(); + OpsArg::Cc(index) => { + grid.charge_conjugate(*index); } OpsArg::DedupChannels(ulps) => { grid.dedup_channels(*ulps); @@ -623,11 +600,8 @@ impl Subcommand for Opts { } OpsArg::SplitChannels(true) => grid.split_channels(), OpsArg::Upgrade(true) => grid.upgrade(), - OpsArg::Cc1(false) - | OpsArg::Cc2(false) - | OpsArg::Optimize(false) - | OpsArg::SplitChannels(false) - | OpsArg::Upgrade(false) => {} + OpsArg::Optimize(false) | OpsArg::SplitChannels(false) | OpsArg::Upgrade(false) => { + } } } diff --git a/pineappl_cli/tests/write.rs b/pineappl_cli/tests/write.rs index ddf8c63d..a3762731 100644 --- a/pineappl_cli/tests/write.rs +++ b/pineappl_cli/tests/write.rs @@ -10,8 +10,7 @@ Arguments: Path of the modified PineAPPL file Options: - --cc1[=] Charge conjugate the first initial state [possible values: true, false] - --cc2[=] Charge conjugate the second initial state [possible values: true, false] + --cc Charge conjugate the convolution with the specified index --dedup-channels[=] Deduplicate channels assuming numbers differing by ULPS are the same --delete-bins Delete bins with the specified indices --delete-channels Delete channels with the specified indices @@ -455,14 +454,14 @@ fn help() { } #[test] -fn cc1() { +fn cc_0() { let output = NamedTempFile::new("cc1.pineappl.lz4").unwrap(); Command::cargo_bin("pineappl") .unwrap() .args([ "write", - "--cc1", + "--cc=0", "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", output.path().to_str().unwrap(), ]) @@ -483,14 +482,15 @@ fn cc1() { } #[test] -fn cc2() { +fn cc_1() { let output = NamedTempFile::new("cc2.pineappl.lz4").unwrap(); Command::cargo_bin("pineappl") .unwrap() .args([ "write", - "--cc2", + "--cc", + "1", "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", output.path().to_str().unwrap(), ]) From 31449884cb347c2605aa215ea4dec4b31e6a53e5 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 3 Sep 2024 09:21:31 +0200 Subject: [PATCH 070/213] Polish interpolation a bit --- pineappl/src/interpolation.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index c73802f2..ba827dd2 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -4,6 +4,8 @@ use super::convert; use arrayvec::ArrayVec; use std::ops::IndexMut; +const INTERP_ORDER_MAX_PLUS_ONE: usize = 8; + mod applgrid { pub fn reweight_x(x: f64) -> f64 { (x.sqrt() / (1.0 - 0.99 * x)).powi(3) @@ -86,7 +88,7 @@ pub struct Interp { order: usize, reweight_x: fn(f64) -> f64, map_x_to_y: fn(f64) -> f64, - map_y_to_x: fn(f64) -> f64, + _map_y_to_x: fn(f64) -> f64, node_weights: fn(usize, usize, f64) -> f64, } @@ -95,7 +97,8 @@ impl Interp { /// /// # Panics /// - /// Panics if `nodes` is `0`, or if `nodes` is smaller or equal to `order`. + /// Panics if `nodes` is `0`, or if `nodes` is smaller or equal to `order` or if `order` is + /// larger than some internally specified maximum. pub fn new( min: f64, max: f64, @@ -105,8 +108,12 @@ impl Interp { map: Map, interp_meth: InterpMeth, ) -> Self { + // for interpolation to work `nodes` has to be at least `1` assert!(nodes > 0); + // for each interpolated point `order + 1` nodes are updated assert!(nodes > order); + // for `order` + assert!(order < INTERP_ORDER_MAX_PLUS_ONE); let mut result = Self { min: 0.0, @@ -121,7 +128,7 @@ impl Interp { Map::ApplGridF2 => applgrid::fx2, Map::ApplGridH0 => applgrid::fq20, }, - map_y_to_x: match map { + _map_y_to_x: match map { Map::ApplGridF2 => applgrid::fy2, Map::ApplGridH0 => applgrid::ftau0, }, @@ -174,7 +181,7 @@ impl Interp { } /// TODO - pub fn node_weights(&self, fraction: f64) -> ArrayVec { + pub fn node_weights(&self, fraction: f64) -> ArrayVec { (0..=self.order) .map(|i| (self.node_weights)(i, self.order, fraction)) .collect() @@ -211,14 +218,14 @@ pub fn interpolate( // self.static_q2 = -1.0; //} - let weight = weight + let _weight = weight / interps .iter() .zip(ntuple) .map(|(interp, &x)| interp.reweight(x)) .product::(); - let node_weights: ArrayVec<_, D> = interps + let _node_weights: ArrayVec<_, D> = interps .iter() .zip(result) .map(|(interp, (_, fraction))| interp.node_weights(fraction)) From f39219f793eb61f4ce2c3dea55bc55509ca66dbf Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 3 Sep 2024 14:21:42 +0200 Subject: [PATCH 071/213] Prevent 'functions have mismatched data' warnings --- .github/workflows/capi.yaml | 4 +++- .github/workflows/python.yml | 3 ++- .github/workflows/rust.yml | 3 ++- maintainer/generate-coverage.sh | 3 ++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/capi.yaml b/.github/workflows/capi.yaml index 90f3d021..57034008 100644 --- a/.github/workflows/capi.yaml +++ b/.github/workflows/capi.yaml @@ -15,8 +15,10 @@ jobs: - uses: actions/checkout@v4 - name: Install PineAPPL's C API + env: + # `-C link-dead-code` is needed to prevent 'warning: XX functions have mismatched data' warnings + RUSTFLAGS: '-Cinstrument-coverage -Clink-dead-code' run: | - export RUSTFLAGS="-Cinstrument-coverage" cargo cinstall --verbose --prefix=/usr/local/ --manifest-path pineappl_capi/Cargo.toml ldconfig diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 3210db34..c2bf990d 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -25,7 +25,8 @@ jobs: rustup component add llvm-tools - name: Test env: - RUSTFLAGS: '-Cinstrument-coverage' + # `-C link-dead-code` is needed to prevent 'warning: XX functions have mismatched data' warnings + RUSTFLAGS: '-Cinstrument-coverage -Clink-dead-code' run: | cd pineappl_py python -m venv env diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a76a9828..1023a4aa 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -66,7 +66,8 @@ jobs: - name: Run tests env: - RUSTFLAGS: '-Cinstrument-coverage' + # `-C link-dead-code` is needed to prevent 'warning: XX functions have mismatched data' warnings + RUSTFLAGS: '-Cinstrument-coverage -Clink-dead-code' run: | # we need stderr, but we can't run test twice because it'll regenerate/modify the binaries which interferes with `llvm-cov` cargo test --features=applgrid,evolve,fastnlo,fktable --no-fail-fast 2> >(tee stderr 1>&2) diff --git a/maintainer/generate-coverage.sh b/maintainer/generate-coverage.sh index a0c1820d..a025a6c1 100755 --- a/maintainer/generate-coverage.sh +++ b/maintainer/generate-coverage.sh @@ -37,7 +37,8 @@ export CARGO_TARGET_DIR="$(mktemp -d)" # relative paths sometimes don't work, so use an absolute path dir="${CARGO_TARGET_DIR}"/debug/doctestbins -export RUSTFLAGS="-Cinstrument-coverage" +# `-C link-dead-code` is needed to prevent 'warning: XX functions have mismatched data' warnings +export RUSTFLAGS="-Cinstrument-coverage -Clink-dead-code" export RUSTDOCFLAGS="-Cinstrument-coverage -Z unstable-options --persist-doctests ${dir}" # -Z doctest-in-workspace is enabled by default starting from 1.72.0 From d454d5fd6cc8b61598bd4fdf4a330b20abff732e Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 3 Sep 2024 16:35:08 +0200 Subject: [PATCH 072/213] Debug new module `interpolation` --- pineappl/src/interpolation.rs | 66 ++++++++++++++++++++++++++++++----- pineappl/src/packed_array.rs | 2 +- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index ba827dd2..0584ea4b 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -125,13 +125,13 @@ impl Interp { ReweightMeth::NoReweight => no_reweight, }, map_x_to_y: match map { - Map::ApplGridF2 => applgrid::fx2, - Map::ApplGridH0 => applgrid::fq20, - }, - _map_y_to_x: match map { Map::ApplGridF2 => applgrid::fy2, Map::ApplGridH0 => applgrid::ftau0, }, + _map_y_to_x: match map { + Map::ApplGridF2 => applgrid::fx2, + Map::ApplGridH0 => applgrid::fq20, + }, node_weights: match interp_meth { InterpMeth::Lagrange => lagrange_weights, }, @@ -140,6 +140,10 @@ impl Interp { result.min = (result.map_x_to_y)(min); result.max = (result.map_x_to_y)(max); + if result.min > result.max { + std::mem::swap(&mut result.min, &mut result.max); + } + result } @@ -158,7 +162,7 @@ impl Interp { /// TODO pub fn interpolate(&self, x: f64) -> Option<(usize, f64)> { - let y = (self.map_x_to_y)(x); + let y = dbg!((self.map_x_to_y)(x)); // points falling outside the interpolation range shouldn't happen very often, because when // it does it degrades the interpolation quality @@ -193,8 +197,10 @@ pub fn interpolate( interps: &[Interp], ntuple: &[f64], weight: f64, - _array: &impl IndexMut, + array: &mut impl IndexMut<[usize; D]>, ) { + use itertools::Itertools; + if weight == 0.0 { return; } @@ -206,7 +212,7 @@ pub fn interpolate( let Some(result): Option> = interps .iter() .zip(ntuple) - .map(|(interp, &x)| interp.interpolate(x)) + .map(|(interp, &x)| dbg!(interp.interpolate(x))) .collect() else { return; @@ -218,19 +224,26 @@ pub fn interpolate( // self.static_q2 = -1.0; //} - let _weight = weight + let weight = weight / interps .iter() .zip(ntuple) .map(|(interp, &x)| interp.reweight(x)) .product::(); - let _node_weights: ArrayVec<_, D> = interps + let node_weights: ArrayVec<_, D> = interps .iter() .zip(result) .map(|(interp, (_, fraction))| interp.node_weights(fraction)) .collect(); + for (i, node_weights) in node_weights.iter().multi_cartesian_product().enumerate() { + let mut index = crate::packed_array::unravel_index::(i, &[4; D]); + println!("{index:?}"); + println!("{node_weights:?}"); + array[index] += weight * node_weights.iter().product(); + } + //for i3 in 0..=self.tauorder { // let fi3i3 = fi(i3, self.tauorder, u_tau); @@ -247,3 +260,38 @@ pub fn interpolate( // } //} } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_interpolation() { + let interps = vec![ + Interp::new( + 10.0, + 10000.0, + 50, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ]; + let ntuple = [100.0, 0.5]; + let weight = 1.0; + let mut array = crate::packed_array::PackedArray::::new([50, 50]); + println!("anything!"); + + interpolate(&interps, &ntuple, weight, &mut array); + } +} diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index ca6561f0..a3a1b3e8 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -140,7 +140,7 @@ fn ravel_multi_index(multi_index: &[usize; D], shape: &[usize]) } /// Converts a flat `index` into a `multi_index`. -fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D] { +pub fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D] { assert!(index < shape.iter().product()); let mut indices = [0; D]; for (i, d) in indices.iter_mut().zip(shape).rev() { From 5ae6466ed46e8e2b6c45f6cd9fcd6284b51a8528 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 3 Sep 2024 17:13:39 +0200 Subject: [PATCH 073/213] Fix problems in previous commit --- pineappl/src/interpolation.rs | 65 +++++++++++++++++------------------ pineappl/src/packed_array.rs | 2 +- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 0584ea4b..6640b664 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -2,6 +2,7 @@ use super::convert; use arrayvec::ArrayVec; +use std::mem; use std::ops::IndexMut; const INTERP_ORDER_MAX_PLUS_ONE: usize = 8; @@ -140,8 +141,9 @@ impl Interp { result.min = (result.map_x_to_y)(min); result.max = (result.map_x_to_y)(max); + // for some maps the minimum in x is mapped to the maximum in y if result.min > result.max { - std::mem::swap(&mut result.min, &mut result.max); + mem::swap(&mut result.min, &mut result.max); } result @@ -162,7 +164,7 @@ impl Interp { /// TODO pub fn interpolate(&self, x: f64) -> Option<(usize, f64)> { - let y = dbg!((self.map_x_to_y)(x)); + let y = (self.map_x_to_y)(x); // points falling outside the interpolation range shouldn't happen very often, because when // it does it degrades the interpolation quality @@ -190,6 +192,11 @@ impl Interp { .map(|i| (self.node_weights)(i, self.order, fraction)) .collect() } + + /// TODO + pub fn order(&self) -> usize { + self.order + } } /// TODO @@ -197,8 +204,9 @@ pub fn interpolate( interps: &[Interp], ntuple: &[f64], weight: f64, - array: &mut impl IndexMut<[usize; D]>, + array: &mut impl IndexMut<[usize; D], Output = f64>, ) { + use super::packed_array; use itertools::Itertools; if weight == 0.0 { @@ -212,17 +220,13 @@ pub fn interpolate( let Some(result): Option> = interps .iter() .zip(ntuple) - .map(|(interp, &x)| dbg!(interp.interpolate(x))) + .map(|(interp, &x)| interp.interpolate(x)) .collect() else { return; }; - //if self.static_q2 == 0.0 { - // self.static_q2 = q2; - //} else if (self.static_q2 != -1.0) && (self.static_q2 != q2) { - // self.static_q2 = -1.0; - //} + // TODO: add static value detection let weight = weight / interps @@ -233,32 +237,23 @@ pub fn interpolate( let node_weights: ArrayVec<_, D> = interps .iter() - .zip(result) - .map(|(interp, (_, fraction))| interp.node_weights(fraction)) + .zip(&result) + .map(|(interp, &(_, fraction))| interp.node_weights(fraction)) .collect(); - for (i, node_weights) in node_weights.iter().multi_cartesian_product().enumerate() { - let mut index = crate::packed_array::unravel_index::(i, &[4; D]); - println!("{index:?}"); - println!("{node_weights:?}"); - array[index] += weight * node_weights.iter().product(); - } - - //for i3 in 0..=self.tauorder { - // let fi3i3 = fi(i3, self.tauorder, u_tau); - - // for (i1, fi1i1) in fi1.iter().enumerate() { - // for (i2, fi2i2) in fi2.iter().enumerate() { - // let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * weight; + let shape: ArrayVec<_, D> = interps.iter().map(|interp| interp.order() + 1).collect(); - // let grid = self - // .grid - // .get_or_insert_with(|| Array3::zeros((size, ny1, ny2))); - - // grid[[k3 + i3 - self.itaumin, k1 + i1, k2 + i2]] += fillweight; - // } - // } - //} + for (i, node_weights) in node_weights + .into_iter() + .multi_cartesian_product() + .enumerate() + { + let mut index = packed_array::unravel_index::(i, &shape); + for (entry, (start_index, _)) in index.iter_mut().zip(&result) { + *entry += start_index; + } + array[index] += weight * node_weights.iter().product::(); + } } #[cfg(test)] @@ -290,8 +285,12 @@ mod tests { let ntuple = [100.0, 0.5]; let weight = 1.0; let mut array = crate::packed_array::PackedArray::::new([50, 50]); - println!("anything!"); + + //println!("{array:?}"); + //println!("anything!"); interpolate(&interps, &ntuple, weight, &mut array); + + //println!("{array:?}"); } } diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index a3a1b3e8..1dd6e07d 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -9,7 +9,7 @@ use std::ops::{Index, IndexMut, MulAssign}; /// `D`-dimensional array similar to [`ndarray::ArrayBase`], except that `T::default()` is not /// stored to save space. Instead, adjacent non-default elements are grouped together and the index /// of their first element (`start_index`) and the length of the group (`lengths`) is stored. -#[derive(Clone, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct PackedArray { /// The actual values stored in the array. The length of `entries` is always the sum of the /// elements in `lengths`. From 18f84ecfd1969c92c41bf6946b7bd698100f0e81 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 08:53:39 +0200 Subject: [PATCH 074/213] Make `Channel::translate` a method --- pineappl/src/boc.rs | 8 ++++---- pineappl/src/grid.rs | 6 +++--- pineappl/src/pids.rs | 8 +++----- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index a8d9aed0..a826f210 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -352,17 +352,17 @@ impl Channel { /// use pineappl::boc::Channel; /// use pineappl::channel; /// - /// let entry = Channel::translate(&channel![103, 11, 10.0], &|evol_id| match evol_id { + /// let entry = channel![103, 11, 10.0].translate(&|evol_id| match evol_id { /// 103 => vec![(2, 1.0), (-2, -1.0), (1, -1.0), (-1, 1.0)], /// _ => vec![(evol_id, 1.0)], /// }); /// /// assert_eq!(entry, channel![2, 11, 10.0; -2, 11, -10.0; 1, 11, -10.0; -1, 11, 10.0]); /// ``` - pub fn translate(entry: &Self, translator: &dyn Fn(i32) -> Vec<(i32, f64)>) -> Self { + pub fn translate(&self, translator: &dyn Fn(i32) -> Vec<(i32, f64)>) -> Self { let mut result = Vec::new(); - for (pids, factor) in &entry.entry { + for (pids, factor) in &self.entry { for tuples in pids .iter() .map(|&pid| translator(pid)) @@ -742,7 +742,7 @@ mod tests { #[test] fn channel_translate() { - let channel = Channel::translate(&channel![103, 203, 2.0], &pids::evol_to_pdg_mc_ids); + let channel = channel![103, 203, 2.0].translate(&pids::evol_to_pdg_mc_ids); assert_eq!( channel, diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 3296bad6..f1cd768d 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -506,7 +506,7 @@ impl Grid { PidBasis::Evol => self .channels .iter() - .map(|entry| Channel::translate(entry, &pids::evol_to_pdg_mc_ids)) + .map(|entry| entry.translate(&pids::evol_to_pdg_mc_ids)) .collect(), PidBasis::Pdg => Cow::Borrowed(self.channels()), } @@ -1485,7 +1485,7 @@ impl Grid { self.channels = self .channels() .iter() - .map(|channel| Channel::translate(channel, &pids::pdg_mc_pids_to_evol)) + .map(|channel| channel.translate(&pids::pdg_mc_pids_to_evol)) .collect(); *self.pid_basis_mut() = PidBasis::Evol; @@ -1494,7 +1494,7 @@ impl Grid { self.channels = self .channels() .iter() - .map(|channel| Channel::translate(channel, &pids::evol_to_pdg_mc_ids)) + .map(|channel| channel.translate(&pids::evol_to_pdg_mc_ids)) .collect(); *self.pid_basis_mut() = PidBasis::Pdg; diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index 949ebfa1..0a27c1f8 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -417,7 +417,6 @@ pub fn pdg_mc_ids_to_evol(tuples: &[(i32, f64)]) -> Option { #[cfg(test)] mod tests { use super::*; - use crate::boc::Channel; use crate::channel; use float_cmp::assert_approx_eq; @@ -925,10 +924,9 @@ mod tests { #[test] fn inverse_inverse_evol() { for pid in [-6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6] { - let result = Channel::translate( - &Channel::translate(&channel![pid, pid, 1.0], &pdg_mc_pids_to_evol), - &evol_to_pdg_mc_ids, - ); + let result = &channel![pid, pid, 1.0] + .translate(&pdg_mc_pids_to_evol) + .translate(&evol_to_pdg_mc_ids); assert_eq!(result.entry().len(), 1); assert_eq!(result.entry()[0].0[0], pid); From add7baa6820de44ddce384cc7172f706c39569a1 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 11:17:26 +0200 Subject: [PATCH 075/213] Add test for new interpolation module --- pineappl/src/interpolation.rs | 169 +++++++++++++++++++++++++++++++--- 1 file changed, 156 insertions(+), 13 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 6640b664..078af62f 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -217,7 +217,7 @@ pub fn interpolate( debug_assert_eq!(interps.len(), ntuple.len()); debug_assert_eq!(interps.len(), D); - let Some(result): Option> = interps + let Some((indices, fractions)): Option<(ArrayVec<_, D>, ArrayVec<_, D>)> = interps .iter() .zip(ntuple) .map(|(interp, &x)| interp.interpolate(x)) @@ -237,8 +237,8 @@ pub fn interpolate( let node_weights: ArrayVec<_, D> = interps .iter() - .zip(&result) - .map(|(interp, &(_, fraction))| interp.node_weights(fraction)) + .zip(fractions) + .map(|(interp, fraction)| interp.node_weights(fraction)) .collect(); let shape: ArrayVec<_, D> = interps.iter().map(|interp| interp.order() + 1).collect(); @@ -249,7 +249,7 @@ pub fn interpolate( .enumerate() { let mut index = packed_array::unravel_index::(i, &shape); - for (entry, (start_index, _)) in index.iter_mut().zip(&result) { + for (entry, start_index) in index.iter_mut().zip(&indices) { *entry += start_index; } array[index] += weight * node_weights.iter().product::(); @@ -259,14 +259,15 @@ pub fn interpolate( #[cfg(test)] mod tests { use super::*; + use float_cmp::assert_approx_eq; #[test] fn test_interpolation() { let interps = vec![ Interp::new( - 10.0, - 10000.0, - 50, + 1e2, + 1e8, + 40, 3, ReweightMeth::NoReweight, Map::ApplGridH0, @@ -281,16 +282,158 @@ mod tests { Map::ApplGridF2, InterpMeth::Lagrange, ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), ]; - let ntuple = [100.0, 0.5]; + let mut array = crate::packed_array::PackedArray::::new([40, 50, 50]); + let ntuples = [[100000.0, 0.25, 0.5], [1000.0, 0.5, 0.5]]; let weight = 1.0; - let mut array = crate::packed_array::PackedArray::::new([50, 50]); - //println!("{array:?}"); - //println!("anything!"); + for ntuple in &ntuples { + interpolate(&interps, ntuple, weight, &mut array); + } - interpolate(&interps, &ntuple, weight, &mut array); + let reference = [ + ([9, 6, 6], -4.0913584971505212e-6), + ([9, 6, 7], 3.0858594463668783e-5), + ([9, 6, 8], 6.0021251939206686e-5), + ([9, 6, 9], -5.0714506160633226e-6), + ([9, 7, 6], 3.0858594463668783e-5), + ([9, 7, 7], -2.3274735101712016e-4), + ([9, 7, 8], -4.5270329502624643e-4), + ([9, 7, 9], 3.8250825004119329e-5), + ([9, 8, 6], 6.0021251939206680e-5), + ([9, 8, 7], -4.5270329502624637e-4), + ([9, 8, 8], -8.8052677047459023e-4), + ([9, 8, 9], 7.4399448333843429e-5), + ([9, 9, 6], -5.0714506160633217e-6), + ([9, 9, 7], 3.8250825004119329e-5), + ([9, 9, 8], 7.4399448333843443e-5), + ([9, 9, 9], -6.2863255246593026e-6), + ([10, 6, 6], 3.2560454032038003e-4), + ([10, 6, 7], -2.4558342839606324e-3), + ([10, 6, 8], -4.7767000033681270e-3), + ([10, 6, 9], 4.0360368023258439e-4), + ([10, 7, 6], -2.4558342839606324e-3), + ([10, 7, 7], 1.8522843767295388e-2), + ([10, 7, 8], 3.6027702872090658e-2), + ([10, 7, 9], -3.0441337030269453e-3), + ([10, 8, 6], -4.7767000033681270e-3), + ([10, 8, 7], 3.6027702872090658e-2), + ([10, 8, 8], 7.0075383161814372e-2), + ([10, 8, 9], -5.9209668846429931e-3), + ([10, 9, 6], 4.0360368023258439e-4), + ([10, 9, 7], -3.0441337030269453e-3), + ([10, 9, 8], -5.9209668846429931e-3), + ([10, 9, 9], 5.0028765120106755e-4), + ([11, 6, 6], 1.3274904136884986e-5), + ([11, 6, 7], -1.0012441676511976e-4), + ([11, 6, 8], -1.9474616224017421e-4), + ([11, 6, 9], 1.6454930754680843e-5), + ([11, 7, 6], -1.0012441676511976e-4), + ([11, 7, 7], 7.5517674019963063e-4), + ([11, 7, 8], 1.4688502237364042e-3), + ([11, 7, 9], -1.2410939677862364e-4), + ([11, 8, 6], -1.9474616224017418e-4), + ([11, 8, 7], 1.4688502237364042e-3), + ([11, 8, 8], 2.8569748840518382e-3), + ([11, 8, 9], -2.4139794768822075e-4), + ([11, 9, 6], 1.6454930754680843e-5), + ([11, 9, 7], -1.2410939677862364e-4), + ([11, 9, 8], -2.4139794768822075e-4), + ([11, 9, 9], 2.0396738337944602e-5), + ([12, 6, 6], -2.1682835394615433e-6), + ([12, 6, 7], 1.6354025801721504e-5), + ([12, 6, 8], 3.1809261566371142e-5), + ([12, 6, 9], -2.6876996722875166e-6), + ([12, 7, 6], 1.6354025801721504e-5), + ([12, 7, 7], -1.2334833293517984e-4), + ([12, 7, 8], -2.3991764680339134e-4), + ([12, 7, 9], 2.0271661426154572e-5), + ([12, 8, 6], 3.1809261566371142e-5), + ([12, 8, 7], -2.3991764680339134e-4), + ([12, 8, 8], -4.6664981907720756e-4), + ([12, 8, 9], 3.9429226082154630e-5), + ([12, 9, 6], -2.6876996722875166e-6), + ([12, 9, 7], 2.0271661426154572e-5), + ([12, 9, 8], 3.9429226082154623e-5), + ([12, 9, 9], -3.3315428526512343e-6), + ([23, 11, 6], -2.4353100307613186e-4), + ([23, 11, 7], 1.8368041980410083e-3), + ([23, 11, 8], 3.5726606946862392e-3), + ([23, 11, 9], -3.0186928289005667e-4), + ([23, 12, 6], 2.9987494527093064e-3), + ([23, 12, 7], -2.2617718130482554e-2), + ([23, 12, 8], -4.3992404119311192e-2), + ([23, 12, 9], 3.7171051546702580e-3), + ([23, 13, 6], 1.4248943085993610e-3), + ([23, 13, 7], -1.0747099197804599e-2), + ([23, 13, 8], -2.0903555712057060e-2), + ([23, 13, 9], 1.7662302446007081e-3), + ([23, 14, 6], -1.9189233197773798e-4), + ([23, 14, 7], 1.4473255417027965e-3), + ([23, 14, 8], 2.8151084806817320e-3), + ([23, 14, 9], -2.3786047737056168e-4), + ([24, 11, 6], 2.4624842908465045e-3), + ([24, 11, 7], -1.8573000668924675e-2), + ([24, 11, 8], -3.6125260135520983e-2), + ([24, 11, 9], 3.0523767307502974e-3), + ([24, 12, 6], -3.0322108175987520e-2), + ([24, 12, 7], 2.2870096573985707e-1), + ([24, 12, 8], 4.4483290707142076e-1), + ([24, 12, 9], -3.7585822483302452e-2), + ([24, 13, 6], -1.4407939057950724e-2), + ([24, 13, 7], 1.0867020055959625e-1), + ([24, 13, 8], 2.1136806777609088e-1), + ([24, 13, 9], -1.7859386182495846e-2), + ([24, 14, 6], 1.9403355098954720e-3), + ([24, 14, 7], -1.4634754364600849e-2), + ([24, 14, 8], -2.8465206988616668e-2), + ([24, 14, 9], 2.4051462916002946e-3), + ([25, 11, 6], 1.7967411488022474e-3), + ([25, 11, 7], -1.3551710637356816e-2), + ([25, 11, 8], -2.6358641814670535e-2), + ([25, 11, 9], 2.2271536489275397e-3), + ([25, 12, 6], -2.2124396764984615e-2), + ([25, 12, 7], 1.6687068317270662e-1), + ([25, 12, 8], 3.2457043135158342e-1), + ([25, 12, 9], -2.7424334895599013e-2), + ([25, 13, 6], -1.0512691216379747e-2), + ([25, 13, 7], 7.9290747851593249e-2), + ([25, 13, 8], 1.5422380817933001e-1), + ([25, 13, 9], -1.3031024874237778e-2), + ([25, 14, 6], 1.4157575201882570e-3), + ([25, 14, 7], -1.0678186036448784e-2), + ([25, 14, 8], -2.0769516741988788e-2), + ([25, 14, 9], 1.7549047224670190e-3), + ([26, 11, 6], -2.1941078639412583e-4), + ([26, 11, 7], 1.6548802758317395e-3), + ([26, 11, 8], 3.2188110862231270e-3), + ([26, 11, 9], -2.7197102590848567e-4), + ([26, 12, 6], 2.7017421490774826e-3), + ([26, 12, 7], -2.0377575170166206e-2), + ([26, 12, 8], -3.9635232727098596e-2), + ([26, 12, 9], 3.3489492294371172e-3), + ([26, 13, 6], 1.2837674744868705e-3), + ([26, 13, 7], -9.6826665051300553e-3), + ([26, 13, 8], -1.8833189775767707e-2), + ([26, 13, 9], 1.5912962293338163e-3), + ([26, 14, 6], -1.7288660142000884e-4), + ([26, 14, 7], 1.3039770348009471e-3), + ([26, 14, 8], 2.5362896622162690e-3), + ([26, 14, 9], -2.1430189065349477e-4), + ]; - //println!("{array:?}"); + for ((index, value), (ref_index, ref_value)) in array.indexed_iter().zip(reference) { + assert_eq!(index, ref_index); + assert_approx_eq!(f64, value, ref_value, ulps = 4); + } } } From 7d92ea0b7ac9f331c539d0234e958b9ca12882d6 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 12:46:46 +0200 Subject: [PATCH 076/213] Increase MSRV from 1.70.0 to 1.80.1 --- .github/workflows/msrv.yml | 2 +- .readthedocs.yml | 5 ++++- CHANGELOG.md | 1 + Cargo.toml | 2 +- README.md | 2 +- docs/installation.md | 2 +- maintainer/make-release.sh | 2 +- maintainer/pineappl-ci/Containerfile | 2 +- 8 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/msrv.yml b/.github/workflows/msrv.yml index 6a56e06f..a36a9383 100644 --- a/.github/workflows/msrv.yml +++ b/.github/workflows/msrv.yml @@ -19,5 +19,5 @@ jobs: - name: Run check run: | # enable the MSRV - rustup default 1.70.0 + rustup default 1.80.1 cargo check --all-features --all-targets diff --git a/.readthedocs.yml b/.readthedocs.yml index d8317c14..1cc0a04a 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -8,7 +8,10 @@ build: os: ubuntu-22.04 tools: python: "3.10" - rust: "1.70" + commands: + - export RUST_WITHOUT=rust-docs,rustfmt + - asdf install rust latest + - asdf global rust latest python: install: diff --git a/CHANGELOG.md b/CHANGELOG.md index 23d71533..b570a937 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - by default `pineappl plot` no longer shows a channel breakdown in the panel with absolute PDF predictions. However, this feature can be enabled with via a new array added at the start of the script +- raised MSRV to 1.80.1 ### Removed diff --git a/Cargo.toml b/Cargo.toml index d239b76d..82786bf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ edition = "2021" keywords = ["high-energy-physics", "physics"] license = "GPL-3.0-or-later" repository = "https://github.com/NNPDF/pineappl" -rust-version = "1.70.0" +rust-version = "1.80.1" version = "1.0.0-alpha1" [workspace.lints.clippy] diff --git a/README.md b/README.md index ff54d9ba..e1874473 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![codecov](https://codecov.io/gh/NNPDF/pineappl/branch/master/graph/badge.svg)](https://codecov.io/gh/NNPDF/pineappl) [![Documentation](https://docs.rs/pineappl/badge.svg)](https://docs.rs/pineappl) [![crates.io](https://img.shields.io/crates/v/pineappl.svg)](https://crates.io/crates/pineappl) -[![MSRV](https://img.shields.io/badge/Rust-1.70+-lightgray.svg)](docs/installation.md) +[![MSRV](https://img.shields.io/badge/Rust-1.80+-lightgray.svg)](docs/installation.md) # What is PineAPPL? diff --git a/docs/installation.md b/docs/installation.md index d01aa21f..74707a66 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -244,7 +244,7 @@ already installed, make sure it is recent enough: cargo --version -This should show a version that is at least 1.70.0. If you do not have `cargo` +This should show a version that is at least 1.80.1. If you do not have `cargo` or if it is too old, go to and follow the instructions there. diff --git a/maintainer/make-release.sh b/maintainer/make-release.sh index 74ce4942..409a5633 100755 --- a/maintainer/make-release.sh +++ b/maintainer/make-release.sh @@ -57,7 +57,7 @@ if ! cargo msrv --help >/dev/null; then exit 1 fi -if ! cargo msrv --min 1.70.0 --max 1.70.0 >/dev/null; then +if ! cargo msrv --min 1.80.1 --max 1.80.1 >/dev/null; then echo "Minimum supported Rust version doesn't match avertised one." exit 1 fi diff --git a/maintainer/pineappl-ci/Containerfile b/maintainer/pineappl-ci/Containerfile index 5b317c1b..952cf163 100644 --- a/maintainer/pineappl-ci/Containerfile +++ b/maintainer/pineappl-ci/Containerfile @@ -11,7 +11,7 @@ ARG ZLIB_V=1.3.1 # the last version is the default Rust version used in the container # as long as we're using `persist-doctests` in the `Rust` workflow we need nightly as default -ARG RUST_V="1.70.0 nightly-2024-01-25" +ARG RUST_V="1.80.1 nightly-2024-09-04 1.70.0" ENV APPL_IGRID_DIR="/usr/local/src/applgrid-${APPLGRID_V}/src" ENV CARGO_HOME="/usr/local/cargo" From e3acdd5ae1abc91ee93165fe9141b26932f0ad74 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 13:03:23 +0200 Subject: [PATCH 077/213] Use nightly as default --- maintainer/pineappl-ci/Containerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maintainer/pineappl-ci/Containerfile b/maintainer/pineappl-ci/Containerfile index 952cf163..3ce63ed9 100644 --- a/maintainer/pineappl-ci/Containerfile +++ b/maintainer/pineappl-ci/Containerfile @@ -11,7 +11,7 @@ ARG ZLIB_V=1.3.1 # the last version is the default Rust version used in the container # as long as we're using `persist-doctests` in the `Rust` workflow we need nightly as default -ARG RUST_V="1.80.1 nightly-2024-09-04 1.70.0" +ARG RUST_V="1.70.0 1.80.1 nightly-2024-09-04" ENV APPL_IGRID_DIR="/usr/local/src/applgrid-${APPLGRID_V}/src" ENV CARGO_HOME="/usr/local/cargo" From 0c56545fbf5f3cae69e29810535fec6999029309 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 13:18:15 +0200 Subject: [PATCH 078/213] Raise version of `cargo-c` --- maintainer/pineappl-ci/Containerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maintainer/pineappl-ci/Containerfile b/maintainer/pineappl-ci/Containerfile index 3ce63ed9..e1d8fea8 100644 --- a/maintainer/pineappl-ci/Containerfile +++ b/maintainer/pineappl-ci/Containerfile @@ -3,8 +3,7 @@ FROM debian:11-slim ARG APPLGRID_V=1.6.36 -# `0.9.27+cargo-0.74.0` is the last version that support Rust 1.70 -ARG CARGOC_V=0.9.27+cargo-0.74.0 +ARG CARGOC_V=0.10.3 ARG FASTNLO_V=2.5.0-2826 ARG LHAPDF_V=6.5.4 ARG ZLIB_V=1.3.1 From dec24a78accbde50248c7c0d317d0b265857019f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 14:11:29 +0200 Subject: [PATCH 079/213] Fix library installation directory --- .github/workflows/capi.yaml | 2 +- docs/installation.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/capi.yaml b/.github/workflows/capi.yaml index 57034008..c89ca0e5 100644 --- a/.github/workflows/capi.yaml +++ b/.github/workflows/capi.yaml @@ -19,7 +19,7 @@ jobs: # `-C link-dead-code` is needed to prevent 'warning: XX functions have mismatched data' warnings RUSTFLAGS: '-Cinstrument-coverage -Clink-dead-code' run: | - cargo cinstall --verbose --prefix=/usr/local/ --manifest-path pineappl_capi/Cargo.toml + cargo cinstall --verbose --prefix=/usr/local/ --libdir=lib --manifest-path pineappl_capi/Cargo.toml ldconfig - name: Test C++ example diff --git a/docs/installation.md b/docs/installation.md index 74707a66..8ac9c97e 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -75,7 +75,7 @@ If you want to build the CAPI from its sources instead, you first need to 3. Now install `pineappl_capi`, PineAPPL's C API: cd pineappl_capi - cargo cinstall --release --prefix=${prefix} + cargo cinstall --release --prefix=${prefix} --libdir=lib cd .. where `${prefix}` points to the desired installation directory. From 48c3e9cd13856f62ba5e3fd1ae3cbc0b733bf265 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 4 Sep 2024 15:34:16 +0200 Subject: [PATCH 080/213] Improve test coverage of interpolation module --- pineappl/src/interpolation.rs | 172 +++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 3 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 078af62f..2763bef1 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -89,7 +89,7 @@ pub struct Interp { order: usize, reweight_x: fn(f64) -> f64, map_x_to_y: fn(f64) -> f64, - _map_y_to_x: fn(f64) -> f64, + map_y_to_x: fn(f64) -> f64, node_weights: fn(usize, usize, f64) -> f64, } @@ -129,7 +129,7 @@ impl Interp { Map::ApplGridF2 => applgrid::fy2, Map::ApplGridH0 => applgrid::ftau0, }, - _map_y_to_x: match map { + map_y_to_x: match map { Map::ApplGridF2 => applgrid::fx2, Map::ApplGridH0 => applgrid::fq20, }, @@ -197,6 +197,13 @@ impl Interp { pub fn order(&self) -> usize { self.order } + + /// TODO + pub fn nodes(&self) -> Vec { + (0..self.nodes) + .map(|node| (self.map_y_to_x)(self.gety(node))) + .collect() + } } /// TODO @@ -262,7 +269,7 @@ mod tests { use float_cmp::assert_approx_eq; #[test] - fn test_interpolation() { + fn interpolate_two_points() { let interps = vec![ Interp::new( 1e2, @@ -292,6 +299,117 @@ mod tests { InterpMeth::Lagrange, ), ]; + + let nodes: Vec<_> = interps.iter().map(Interp::nodes).collect(); + + let q2_reference = [ + 9.9999999999999986e1, + 1.2242682307575689e2, + 1.5071735829758390e2, + 1.8660624792652183e2, + 2.3239844323901826e2, + 2.9117504454783159e2, + 3.6707996194452909e2, + 4.6572167648697109e2, + 5.9473999989302229e2, + 7.6461095796663312e2, + 9.8979770734783131e2, + 1.2904078604330668e3, + 1.6945973073289490e3, + 2.2420826491130997e3, + 2.9893125907295248e3, + 4.0171412997902630e3, + 5.4423054291935287e3, + 7.4347313816879214e3, + 1.0243854670019169e4, + 1.4238990475802799e4, + 1.9971806922234402e4, + 2.8273883344269376e4, + 4.0410482328443621e4, + 5.8325253189217328e4, + 8.5033475340946548e4, + 1.2526040013230646e5, + 1.8648821332147921e5, + 2.8069149021747953e5, + 4.2724538080621109e5, + 6.5785374312992941e5, + 1.0249965523865514e6, + 1.6165812577807596e6, + 2.5816634211063879e6, + 4.1761634755570055e6, + 6.8451673415389210e6, + 1.1373037585359517e7, + 1.9160909972020049e7, + 3.2746801715531096e7, + 5.6794352823474184e7, + 9.9999999999999493e7, + ]; + + for (&node, ref_node) in nodes[0].iter().zip(q2_reference) { + assert_approx_eq!(f64, node, ref_node, ulps = 4); + } + + let x_reference = [ + 1.0000000000000000e0, + 9.3094408087175440e-1, + 8.6278393239061080e-1, + 7.9562425229227562e-1, + 7.2958684424143116e-1, + 6.6481394824738227e-1, + 6.0147219796733498e-1, + 5.3975723378804452e-1, + 4.7989890296102550e-1, + 4.2216677535896480e-1, + 3.6687531864822420e-1, + 3.1438740076927585e-1, + 2.6511370415828228e-1, + 2.1950412650038861e-1, + 1.7802566042569432e-1, + 1.4112080644440345e-1, + 1.0914375746330703e-1, + 8.2281221262048926e-2, + 6.0480028754447364e-2, + 4.3414917417022691e-2, + 3.0521584007828916e-2, + 2.1089186683787169e-2, + 1.4375068581090129e-2, + 9.6991595740433985e-3, + 6.4962061946337987e-3, + 4.3285006388208112e-3, + 2.8738675812817515e-3, + 1.9034634022867384e-3, + 1.2586797144272762e-3, + 8.3140688364881441e-4, + 5.4877953236707956e-4, + 3.6205449638139736e-4, + 2.3878782918561914e-4, + 1.5745605600841445e-4, + 1.0381172986576898e-4, + 6.8437449189678965e-5, + 4.5114383949640441e-5, + 2.9738495372244901e-5, + 1.9602505002391748e-5, + 1.2921015690747310e-5, + 8.5168066775733548e-6, + 5.6137577169301513e-6, + 3.7002272069854957e-6, + 2.4389432928916821e-6, + 1.6075854984708080e-6, + 1.0596094959101024e-6, + 6.9842085307003639e-7, + 4.6035014748963906e-7, + 3.0343047658679519e-7, + 1.9999999999999954e-7, + ]; + + for (&node, ref_node) in nodes[1].iter().zip(x_reference) { + assert_approx_eq!(f64, node, ref_node, ulps = 4); + } + + for (&node, ref_node) in nodes[2].iter().zip(x_reference) { + assert_approx_eq!(f64, node, ref_node, ulps = 4); + } + let mut array = crate::packed_array::PackedArray::::new([40, 50, 50]); let ntuples = [[100000.0, 0.25, 0.5], [1000.0, 0.5, 0.5]]; let weight = 1.0; @@ -436,4 +554,52 @@ mod tests { assert_approx_eq!(f64, value, ref_value, ulps = 4); } } + + #[test] + fn interpolate_zero_and_outside() { + let interps = vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ]; + let mut array = crate::packed_array::PackedArray::::new([40, 50, 50]); + + let ntuple = [1000.0, 0.5, 0.5]; + let weight = 0.0; + interpolate(&interps, &ntuple, weight, &mut array); + + assert_eq!(array.non_zeros(), 0); + assert_eq!(array.explicit_zeros(), 0); + + let ntuple = [10.0, 0.5, 0.5]; + let weight = 1.0; + interpolate(&interps, &ntuple, weight, &mut array); + + assert_eq!(array.non_zeros(), 0); + assert_eq!(array.explicit_zeros(), 0); + } } From 41b294aa37f1c4455180f44aa85692ed0e82e71b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 5 Sep 2024 13:31:26 +0200 Subject: [PATCH 081/213] Test interpolation with one node --- pineappl/src/interpolation.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 2763bef1..366d05bf 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -602,4 +602,33 @@ mod tests { assert_eq!(array.non_zeros(), 0); assert_eq!(array.explicit_zeros(), 0); } + + #[test] + fn interpolate_with_one_node() { + // TODO: does it make sense for an interpolation to have `min = max`? There will be + // numerical problems if the `x` value doesn't exactly hit the limits + let interps = vec![Interp::new( + 90.0_f64.powi(2), + 90.0_f64.powi(2), + 1, + 0, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + )]; + let mut array = crate::packed_array::PackedArray::::new([1]); + + let ntuple = [90.0_f64.powi(2)]; + let weight = 1.0; + interpolate(&interps, &ntuple, weight, &mut array); + + assert_eq!(array[[0]], 1.0); + + let nodes = interps[0].nodes(); + + assert_eq!(nodes.len(), 1); + + // TODO: the return value is not the one expected (90^2), because `deltay` is zero + assert!(nodes[0].is_nan()); + } } From 3c5223c1a3c27a2b351023b3902655a10111a9a7 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 5 Sep 2024 13:39:09 +0200 Subject: [PATCH 082/213] Add more documentation and comments --- CHANGELOG.md | 4 ++-- pineappl/src/boc.rs | 1 + pineappl/src/grid.rs | 2 +- pineappl/src/interpolation.rs | 17 +++++++++++------ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b570a937..8520827f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,8 +78,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - added `PidBasis::charge_conjugate` and `PidBasis::guess` - added `Grid::set_pid_basis` method - added `Grid::subgrids` and `Grid::subgrids_mut` methods -- added new switch `conv_fun_uncert_from` to subcommand `plot` to allow - choosing with convolution function uncertainty should be plotted +- added new switch `--conv-fun-uncert-from` to subcommand `plot` to allow + choosing which convolution function uncertainty should be plotted ### Changed diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index a826f210..2046c34c 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -473,6 +473,7 @@ impl FromStr for Channel { .split('+') .map(|sub| { sub.split_once('*').map_or_else( + // TODO: allow a missing numerical factor which then is assumed to be `1` || Err(ParseChannelError(format!("missing '*' in '{sub}'"))), |(factor, pids)| { let vector: Vec<_> = pids diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index f1cd768d..47b3e7a4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -117,7 +117,7 @@ bitflags! { #[derive(Clone, Copy)] #[repr(transparent)] pub struct GridOptFlags: u32 { - /// Change the [`Subgrid`] type to optimize storage effeciency. + /// Change the [`Subgrid`] type to optimize storage efficiency. const OPTIMIZE_SUBGRID_TYPE = 0b1; /// Recognize whether a subgrid was filled with events with a static scale and if this is /// the case, optimize it by undoing the interpolation in the scale. This flag requires diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 366d05bf..ed54ddca 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -1,11 +1,11 @@ -//! TODO +//! Interpolation module. use super::convert; use arrayvec::ArrayVec; use std::mem; use std::ops::IndexMut; -const INTERP_ORDER_MAX_PLUS_ONE: usize = 8; +const MAX_INTERP_ORDER_PLUS_ONE: usize = 8; mod applgrid { pub fn reweight_x(x: f64) -> f64 { @@ -18,7 +18,7 @@ mod applgrid { for _ in 0..100 { let x = (-yp).exp(); let delta = y - yp - 5.0 * (1.0 - x); - if (delta).abs() < 1e-12 { + if delta.abs() < 1e-12 { return x; } let deriv = -1.0 - 5.0 * x; @@ -109,12 +109,14 @@ impl Interp { map: Map, interp_meth: InterpMeth, ) -> Self { + // minimum must be larger or equal to the maximum + assert!(min <= max); // for interpolation to work `nodes` has to be at least `1` assert!(nodes > 0); // for each interpolated point `order + 1` nodes are updated assert!(nodes > order); - // for `order` - assert!(order < INTERP_ORDER_MAX_PLUS_ONE); + // using arrays with fixed size limit the possible max value of `order` + assert!(order < MAX_INTERP_ORDER_PLUS_ONE); let mut result = Self { min: 0.0, @@ -143,6 +145,8 @@ impl Interp { // for some maps the minimum in x is mapped to the maximum in y if result.min > result.max { + // TODO: alternatively we have to modify our range check in `Self::interpolate`, which + // has the advantage that we don't swap min and max in `x` space mem::swap(&mut result.min, &mut result.max); } @@ -187,7 +191,7 @@ impl Interp { } /// TODO - pub fn node_weights(&self, fraction: f64) -> ArrayVec { + pub fn node_weights(&self, fraction: f64) -> ArrayVec { (0..=self.order) .map(|i| (self.node_weights)(i, self.order, fraction)) .collect() @@ -252,6 +256,7 @@ pub fn interpolate( for (i, node_weights) in node_weights .into_iter() + // TODO: replace this with something else to avoid allocating memory .multi_cartesian_product() .enumerate() { From 9ccb176dd93b8dd25b7310b625d0874602a7385c Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 5 Sep 2024 13:39:39 +0200 Subject: [PATCH 083/213] Fix CAPI library installation directory This is necessary to counteract a recent change in `cargo-c` --- .github/workflows/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5f36c1e4..3af3e288 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,7 +34,7 @@ jobs: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --target=${{ matrix.target }} cargo install --locked cargo-c cd pineappl_capi - cargo cinstall --destdir=prefix --library-type=cdylib --locked --prefix=/ --target=${{ matrix.target }} --verbose + cargo cinstall --destdir=prefix --libdir=lib --library-type=cdylib --locked --prefix=/ --target=${{ matrix.target }} --verbose cd prefix tar czf ../../pineappl_capi-${{ matrix.target }}.tar.gz . - name: Upload artifact @@ -163,7 +163,7 @@ jobs: - name: Compile library run: | cd pineappl_capi - cargo cinstall --destdir=prefix --library-type=cdylib --locked --prefix=/ --target=${{ matrix.target }} --verbose + cargo cinstall --destdir=prefix --libdir=lib --library-type=cdylib --locked --prefix=/ --target=${{ matrix.target }} --verbose cd prefix tar czf ../../pineappl_capi-${{ matrix.target }}.tar.gz . # print the glibc version requirement From 719a45d016afb5cec45c6b59702a09356a7eaabe Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 5 Sep 2024 13:41:55 +0200 Subject: [PATCH 084/213] Fix `generate-coverage.sh` script The feature `static` should not be used when APPLgrid has been compiled with ROOT support, because ROOT doesn't support static linking --- maintainer/generate-coverage.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maintainer/generate-coverage.sh b/maintainer/generate-coverage.sh index a025a6c1..76d02b1b 100755 --- a/maintainer/generate-coverage.sh +++ b/maintainer/generate-coverage.sh @@ -42,7 +42,7 @@ export RUSTFLAGS="-Cinstrument-coverage -Clink-dead-code" export RUSTDOCFLAGS="-Cinstrument-coverage -Z unstable-options --persist-doctests ${dir}" # -Z doctest-in-workspace is enabled by default starting from 1.72.0 -cargo test -Z doctest-in-workspace --all-features 2> >(tee stderr 1>&2) +cargo test -Z doctest-in-workspace --features=applgrid,evolve,fastnlo,fktable 2> >(tee stderr 1>&2) # from https://stackoverflow.com/a/51141872/812178 sed -i 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' stderr From 9f303cec4a96df8eff00639323491e30d8c013b6 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 6 Sep 2024 16:31:00 +0200 Subject: [PATCH 085/213] Prepare replacement of `LagrangeSubgridV2` --- pineappl/src/grid.rs | 17 +- pineappl/src/interpolation.rs | 65 ++-- pineappl/src/lagrange_subgrid.rs | 527 ++++++++++--------------------- 3 files changed, 214 insertions(+), 395 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 47b3e7a4..442d2337 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -891,14 +891,15 @@ impl Grid { *subgrid = EmptySubgridV1.into(); } _ => { - // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should - // replace this with a method - if !static_scale_detection { - if let SubgridEnum::LagrangeSubgridV2(subgrid) = subgrid { - // disable static-scale detection - subgrid.static_q2 = -1.0; - } - } + todo!(); + // // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should + // // replace this with a method + // if !static_scale_detection { + // if let SubgridEnum::LagrangeSubgridV2(subgrid) = subgrid { + // // disable static-scale detection + // subgrid.static_q2 = -1.0; + // } + // } *subgrid = PackedQ1X2SubgridV1::from(&*subgrid).into(); } diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index ed54ddca..57bb349e 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -2,6 +2,7 @@ use super::convert; use arrayvec::ArrayVec; +use serde::{Deserialize, Serialize}; use std::mem; use std::ops::IndexMut; @@ -41,10 +42,6 @@ mod applgrid { } } -fn no_reweight(_: f64) -> f64 { - 1.0 -} - fn lagrange_weights(i: usize, n: usize, u: f64) -> f64 { let mut factorials = 1; let mut product = 1.0; @@ -60,6 +57,7 @@ fn lagrange_weights(i: usize, n: usize, u: f64) -> f64 { } /// TODO +#[derive(Clone, Deserialize, Serialize)] pub enum ReweightMeth { /// TODO ApplGridX, @@ -68,6 +66,7 @@ pub enum ReweightMeth { } /// TODO +#[derive(Clone, Deserialize, Serialize)] pub enum Map { /// TODO ApplGridF2, @@ -76,21 +75,22 @@ pub enum Map { } /// TODO +#[derive(Clone, Deserialize, Serialize)] pub enum InterpMeth { /// TODO Lagrange, } /// TODO +#[derive(Clone, Deserialize, Serialize)] pub struct Interp { min: f64, max: f64, nodes: usize, order: usize, - reweight_x: fn(f64) -> f64, - map_x_to_y: fn(f64) -> f64, - map_y_to_x: fn(f64) -> f64, - node_weights: fn(usize, usize, f64) -> f64, + reweight: ReweightMeth, + map: Map, + interp_meth: InterpMeth, } impl Interp { @@ -123,25 +123,13 @@ impl Interp { max: 0.0, nodes, order, - reweight_x: match reweight { - ReweightMeth::ApplGridX => applgrid::reweight_x, - ReweightMeth::NoReweight => no_reweight, - }, - map_x_to_y: match map { - Map::ApplGridF2 => applgrid::fy2, - Map::ApplGridH0 => applgrid::ftau0, - }, - map_y_to_x: match map { - Map::ApplGridF2 => applgrid::fx2, - Map::ApplGridH0 => applgrid::fq20, - }, - node_weights: match interp_meth { - InterpMeth::Lagrange => lagrange_weights, - }, + reweight, + map, + interp_meth, }; - result.min = (result.map_x_to_y)(min); - result.max = (result.map_x_to_y)(max); + result.min = result.map_x_to_y(min); + result.max = result.map_x_to_y(max); // for some maps the minimum in x is mapped to the maximum in y if result.min > result.max { @@ -163,12 +151,15 @@ impl Interp { /// TODO pub fn reweight(&self, x: f64) -> f64 { - (self.reweight_x)(x) + match self.reweight { + ReweightMeth::ApplGridX => applgrid::reweight_x(x), + ReweightMeth::NoReweight => return 1.0, + } } /// TODO pub fn interpolate(&self, x: f64) -> Option<(usize, f64)> { - let y = (self.map_x_to_y)(x); + let y = self.map_x_to_y(x); // points falling outside the interpolation range shouldn't happen very often, because when // it does it degrades the interpolation quality @@ -193,7 +184,9 @@ impl Interp { /// TODO pub fn node_weights(&self, fraction: f64) -> ArrayVec { (0..=self.order) - .map(|i| (self.node_weights)(i, self.order, fraction)) + .map(|i| match self.interp_meth { + InterpMeth::Lagrange => lagrange_weights(i, self.order, fraction), + }) .collect() } @@ -205,9 +198,23 @@ impl Interp { /// TODO pub fn nodes(&self) -> Vec { (0..self.nodes) - .map(|node| (self.map_y_to_x)(self.gety(node))) + .map(|node| self.map_y_to_x(self.gety(node))) .collect() } + + fn map_y_to_x(&self, y: f64) -> f64 { + match self.map { + Map::ApplGridF2 => applgrid::fx2(y), + Map::ApplGridH0 => applgrid::fq20(y), + } + } + + fn map_x_to_y(&self, x: f64) -> f64 { + match self.map { + Map::ApplGridF2 => applgrid::fy2(x), + Map::ApplGridH0 => applgrid::ftau0(x), + } + } } /// TODO diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 28e28a1e..8d520c05 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -1,83 +1,19 @@ //! Module containing the Lagrange-interpolation subgrid. -use super::convert::{f64_from_usize, usize_from_f64}; +use super::interpolation::{self, Interp, InterpMeth, Map, ReweightMeth}; +use super::packed_array::PackedArray; use super::subgrid::{ ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, }; -use arrayvec::ArrayVec; -use ndarray::Array3; use serde::{Deserialize, Serialize}; use std::borrow::Cow; -use std::iter; use std::mem; -fn weightfun(x: f64) -> f64 { - (x.sqrt() / (1.0 - 0.99 * x)).powi(3) -} - -fn fx(y: f64) -> f64 { - let mut yp = y; - - for _ in 0..100 { - let x = (-yp).exp(); - let delta = y - yp - 5.0 * (1.0 - x); - if (delta).abs() < 1e-12 { - return x; - } - let deriv = -1.0 - 5.0 * x; - yp -= delta / deriv; - } - - unreachable!(); -} - -fn fy(x: f64) -> f64 { - (1.0 - x).mul_add(5.0, -x.ln()) -} - -fn ftau(q2: f64) -> f64 { - (q2 / 0.0625).ln().ln() -} - -fn fq2(tau: f64) -> f64 { - 0.0625 * tau.exp().exp() -} - -fn fi(i: usize, n: usize, u: f64) -> f64 { - let mut factorials = 1; - let mut product = 1.0; - for z in 0..i { - product *= u - f64_from_usize(z); - factorials *= i - z; - } - for z in i + 1..=n { - product *= f64_from_usize(z) - u; - factorials *= z - i; - } - product / f64_from_usize(factorials) -} - /// Subgrid which uses Lagrange-interpolation. #[derive(Clone, Deserialize, Serialize)] pub struct LagrangeSubgridV2 { - grid: Option>, - ntau: usize, - ny1: usize, - ny2: usize, - y1order: usize, - y2order: usize, - tauorder: usize, - itaumin: usize, - itaumax: usize, - reweight1: bool, - reweight2: bool, - y1min: f64, - y1max: f64, - y2min: f64, - y2max: f64, - taumin: f64, - taumax: f64, - pub(crate) static_q2: f64, + grid: PackedArray, + interps: [Interp; 3], } impl LagrangeSubgridV2 { @@ -85,343 +21,218 @@ impl LagrangeSubgridV2 { #[must_use] pub fn new(subgrid_params: &SubgridParams, extra_params: &ExtraSubgridParams) -> Self { Self { - grid: None, - ntau: subgrid_params.q2_bins(), - ny1: subgrid_params.x_bins(), - ny2: extra_params.x2_bins(), - y1order: subgrid_params.x_order(), - y2order: extra_params.x2_order(), - tauorder: subgrid_params.q2_order(), - itaumin: 0, - itaumax: 0, - reweight1: subgrid_params.reweight(), - reweight2: extra_params.reweight2(), - y1min: fy(subgrid_params.x_max()), - y1max: fy(subgrid_params.x_min()), - y2min: fy(extra_params.x2_max()), - y2max: fy(extra_params.x2_min()), - taumin: ftau(subgrid_params.q2_min()), - taumax: ftau(subgrid_params.q2_max()), - static_q2: 0.0, - } - } - - fn deltay1(&self) -> f64 { - (self.y1max - self.y1min) / f64_from_usize(self.ny1 - 1) - } - - fn deltay2(&self) -> f64 { - (self.y1max - self.y2min) / f64_from_usize(self.ny2 - 1) - } - - fn deltatau(&self) -> f64 { - (self.taumax - self.taumin) / f64_from_usize(self.ntau - 1) - } - - fn gety1(&self, iy: usize) -> f64 { - if self.y1min == self.y1max { - debug_assert_eq!(iy, 0); - self.y1min - } else { - f64_from_usize(iy).mul_add(self.deltay1(), self.y1min) - } - } - - fn gety2(&self, iy: usize) -> f64 { - if self.y2min == self.y2max { - debug_assert_eq!(iy, 0); - self.y2min - } else { - f64_from_usize(iy).mul_add(self.deltay2(), self.y2min) - } - } - - fn gettau(&self, iy: usize) -> f64 { - if self.taumin == self.taumax { - debug_assert_eq!(iy, 0); - self.taumin - } else { - f64_from_usize(iy).mul_add(self.deltatau(), self.taumin) - } - } - - fn increase_tau(&mut self, new_itaumin: usize, new_itaumax: usize) { - let min_diff = self.itaumin - new_itaumin; - - let mut new_grid = Array3::zeros((new_itaumax - new_itaumin, self.ny1, self.ny2)); - - for ((i, j, k), value) in self.grid.as_ref().unwrap().indexed_iter() { - new_grid[[i + min_diff, j, k]] = *value; + grid: PackedArray::new([ + subgrid_params.q2_bins(), + subgrid_params.x_bins(), + extra_params.x2_bins(), + ]), + interps: [ + Interp::new( + subgrid_params.q2_min(), + subgrid_params.q2_max(), + subgrid_params.q2_bins(), + subgrid_params.q2_order(), + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + subgrid_params.x_min(), + subgrid_params.x_max(), + subgrid_params.x_bins(), + subgrid_params.x_order(), + if subgrid_params.reweight() { + ReweightMeth::ApplGridX + } else { + ReweightMeth::NoReweight + }, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + extra_params.x2_min(), + extra_params.x2_max(), + extra_params.x2_bins(), + extra_params.x2_order(), + if extra_params.reweight2() { + ReweightMeth::ApplGridX + } else { + ReweightMeth::NoReweight + }, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ], } - - self.itaumin = new_itaumin; - self.itaumax = new_itaumax; - - self.grid = Some(new_grid); } } impl Subgrid for LagrangeSubgridV2 { fn fill(&mut self, ntuple: &[f64], weight: f64) { - if weight == 0.0 { - return; - } - - let &[x1, x2, q2] = ntuple else { - unreachable!(); - }; - let y1 = fy(x1); - let y2 = fy(x2); - let tau = ftau(q2); - - if self.static_q2 == 0.0 { - self.static_q2 = q2; - } else if (self.static_q2 != -1.0) && (self.static_q2 != q2) { - self.static_q2 = -1.0; - } - - if (y2 < self.y2min) - || (y2 > self.y2max) - || (y1 < self.y1min) - || (y1 > self.y1max) - || (tau < self.taumin) - || (tau > self.taumax) - { - return; - } - - let k1 = - usize_from_f64((y1 - self.y1min) / self.deltay1() - f64_from_usize(self.y1order / 2)) - .min(self.ny1 - 1 - self.y1order); - let k2 = - usize_from_f64((y2 - self.y2min) / self.deltay2() - f64_from_usize(self.y2order / 2)) - .min(self.ny2 - 1 - self.y2order); - - let u_y1 = (y1 - self.gety1(k1)) / self.deltay1(); - let u_y2 = (y2 - self.gety2(k2)) / self.deltay2(); - - let fi1: ArrayVec<_, 8> = (0..=self.y1order) - .map(|i| fi(i, self.y1order, u_y1)) - .collect(); - let fi2: ArrayVec<_, 8> = (0..=self.y2order) - .map(|i| fi(i, self.y2order, u_y2)) - .collect(); - - let k3 = usize_from_f64( - (tau - self.taumin) / self.deltatau() - f64_from_usize(self.tauorder / 2), - ) - .min(self.ntau - 1 - self.tauorder); - - let u_tau = (tau - self.gettau(k3)) / self.deltatau(); - - let factor = 1.0 - / (if self.reweight1 { weightfun(x1) } else { 1.0 } - * if self.reweight2 { weightfun(x2) } else { 1.0 }); - - let size = self.tauorder + 1; - let ny1 = self.ny1; - let ny2 = self.ny2; - - if self.grid.is_none() { - self.itaumin = k3; - self.itaumax = k3 + size; - } else if k3 < self.itaumin || k3 + size > self.itaumax { - self.increase_tau(self.itaumin.min(k3), self.itaumax.max(k3 + size)); - } - - for i3 in 0..=self.tauorder { - let fi3i3 = fi(i3, self.tauorder, u_tau); - - for (i1, fi1i1) in fi1.iter().enumerate() { - for (i2, fi2i2) in fi2.iter().enumerate() { - let fillweight = factor * fi1i1 * fi2i2 * fi3i3 * weight; - - let grid = self - .grid - .get_or_insert_with(|| Array3::zeros((size, ny1, ny2))); - - grid[[k3 + i3 - self.itaumin, k1 + i1, k2 + i2]] += fillweight; - } - } - } + // TODO: change the order of ntuple higher up in the code + let mut ntuple = ntuple.to_vec(); + ntuple.rotate_right(1); + interpolation::interpolate(&self.interps, &ntuple, weight, &mut self.grid); } fn mu2_grid(&self) -> Cow<[Mu2]> { - (0..self.ntau) - .map(|itau| { - let q2 = fq2(self.gettau(itau)); - Mu2 { - ren: q2, - fac: q2, - frg: -1.0, - } + self.interps[0] + .nodes() + .iter() + .map(|&q2| Mu2 { + ren: q2, + fac: q2, + frg: -1.0, }) .collect() } fn x1_grid(&self) -> Cow<[f64]> { - (0..self.ny1).map(|iy| fx(self.gety1(iy))).collect() + self.interps[1].nodes().into() } fn x2_grid(&self) -> Cow<[f64]> { - (0..self.ny2).map(|iy| fx(self.gety2(iy))).collect() + self.interps[2].nodes().into() } fn is_empty(&self) -> bool { - self.grid.is_none() + self.grid.is_empty() } fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - let x1_equal = self.x1_grid() == other.x1_grid(); - let x2_equal = self.x2_grid() == other.x2_grid(); - - if let SubgridEnum::LagrangeSubgridV2(other_grid) = other { - if let Some(other_grid_grid) = &mut other_grid.grid { - if self.grid.is_some() { - // TODO: the general case isn't implemented - assert!(x1_equal); - assert!(x2_equal); - - let new_itaumin = self.itaumin.min(other_grid.itaumin); - let new_itaumax = self.itaumax.max(other_grid.itaumax); - let offset = other_grid.itaumin.saturating_sub(self.itaumin); - - // TODO: we need much more checks here if there subgrids are compatible at all - - if (self.itaumin != new_itaumin) || (self.itaumax != new_itaumax) { - self.increase_tau(new_itaumin, new_itaumax); - } + // if self.is_empty() && !transpose { + // if let SubgridEnum::LagrangeSubgridV2(other) = other { + // *self = *other; + // return; + // } + // } + + let rhs_mu2 = other.mu2_grid().into_owned(); + let rhs_x1 = if transpose { + other.x2_grid() + } else { + other.x1_grid() + }; + let rhs_x2 = if transpose { + other.x1_grid() + } else { + other.x2_grid() + }; - if (other_grid.static_q2 == -1.0) || (self.static_q2 != other_grid.static_q2) { - self.static_q2 = -1.0; - } + if (self.mu2_grid() != rhs_mu2) || (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) + { + let mut mu2_grid = self.mu2_grid().into_owned(); + let mut x1_grid = self.x1_grid().into_owned(); + let mut x2_grid = self.x2_grid().into_owned(); + + mu2_grid.extend_from_slice(&rhs_mu2); + mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + mu2_grid.dedup(); + x1_grid.extend_from_slice(&rhs_x1); + x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x1_grid.dedup(); + x2_grid.extend_from_slice(&rhs_x2); + x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x2_grid.dedup(); + + let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + + for ([i, j, k], value) in self.grid.indexed_iter() { + let target_i = mu2_grid + .iter() + .position(|mu2| *mu2 == self.mu2_grid()[i]) + .unwrap_or_else(|| unreachable!()); + let target_j = x1_grid + .iter() + .position(|&x| x == self.x1_grid()[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = x2_grid + .iter() + .position(|&x| x == self.x2_grid()[k]) + .unwrap_or_else(|| unreachable!()); + + array[[target_i, target_j, target_k]] = value; + } - let self_grid = self.grid.as_mut().unwrap(); + self.grid = array; + // TODO: make sure that the interpolation of both grids are the same + } - if transpose { - for ((i, k, j), value) in other_grid_grid.indexed_iter() { - self_grid[[i + offset, j, k]] += value; - } - } else { - for ((i, j, k), value) in other_grid_grid.indexed_iter() { - self_grid[[i + offset, j, k]] += value; - } - } - } else { - self.grid = other_grid.grid.take(); - self.itaumin = other_grid.itaumin; - self.itaumax = other_grid.itaumax; - self.static_q2 = other_grid.static_q2; - - if transpose { - if let Some(grid) = &mut self.grid { - grid.swap_axes(1, 2); - } - } - } - } - } else { - todo!(); + for ((i, j, k), value) in other.indexed_iter() { + let (j, k) = if transpose { (k, j) } else { (j, k) }; + let target_i = self + .mu2_grid() + .iter() + .position(|x| *x == rhs_mu2[i]) + .unwrap_or_else(|| unreachable!()); + let target_j = self + .x1_grid() + .iter() + .position(|&x| x == rhs_x1[j]) + .unwrap_or_else(|| unreachable!()); + let target_k = self + .x2_grid() + .iter() + .position(|&x| x == rhs_x2[k]) + .unwrap_or_else(|| unreachable!()); + + self.grid[[target_i, target_j, target_k]] += value; } } fn scale(&mut self, factor: f64) { - if let Some(self_grid) = &mut self.grid { - self_grid.iter_mut().for_each(|x| *x *= factor); - } + self.grid *= factor; } fn symmetrize(&mut self) { - if let Some(grid) = self.grid.as_mut() { - let (i_size, j_size, k_size) = grid.dim(); - - for i in 0..i_size { - for j in 0..j_size { - for k in j + 1..k_size { - grid[[i, j, k]] += grid[[i, k, j]]; - grid[[i, k, j]] = 0.0; - } - } - } + let mut new_array = PackedArray::new([ + self.mu2_grid().len(), + self.x1_grid().len(), + self.x2_grid().len(), + ]); + + for ([i, j, k], sigma) in self.grid.indexed_iter().filter(|([_, j, k], _)| k >= j) { + new_array[[i, j, k]] = sigma; } + // do not change the diagonal entries (k==j) + for ([i, j, k], sigma) in self.grid.indexed_iter().filter(|([_, j, k], _)| k < j) { + new_array[[i, k, j]] += sigma; + } + + self.grid = new_array; } fn clone_empty(&self) -> SubgridEnum { - Self { - grid: None, - ntau: self.ntau, - ny1: self.ny1, - ny2: self.ny2, - y1order: self.y1order, - y2order: self.y2order, - tauorder: self.tauorder, - itaumin: 0, - itaumax: 0, - reweight1: self.reweight1, - reweight2: self.reweight2, - y1min: self.y1min, - y1max: self.y1max, - y2min: self.y2min, - y2max: self.y2max, - taumin: self.taumin, - taumax: self.taumax, - static_q2: 0.0, - } - .into() + self.clone().into() } fn indexed_iter(&self) -> SubgridIndexedIter { - self.grid.as_ref().map_or_else( - || Box::new(iter::empty()) as Box>, - |grid| { - Box::new(grid.indexed_iter().filter(|(_, &value)| value != 0.0).map( - |(tuple, &value)| { - ( - (self.itaumin + tuple.0, tuple.1, tuple.2), - value - * if self.reweight1 { - weightfun(fx(self.gety1(tuple.1))) - } else { - 1.0 - } - * if self.reweight2 { - weightfun(fx(self.gety2(tuple.2))) - } else { - 1.0 - }, - ) - }, - )) - }, - ) + let nodes: Vec<_> = self.interps.iter().map(|interp| interp.nodes()).collect(); + Box::new(self.grid.indexed_iter().map(move |(index, v)| { + ( + (index[0], index[1], index[2]), + v * self + .interps + .iter() + .enumerate() + .map(|(i, interp)| interp.reweight(nodes[i][index[i]])) + .product::(), + ) + })) } fn stats(&self) -> Stats { - let (non_zeros, zeros) = self.grid.as_ref().map_or((0, 0), |array| { - array.iter().fold((0, 0), |(non_zeros, zeros), value| { - if *value == 0.0 { - (non_zeros, zeros + 1) - } else { - (non_zeros + 1, zeros) - } - }) - }); - Stats { - total: non_zeros + zeros, - allocated: non_zeros + zeros, - zeros, - overhead: 0, + total: self.mu2_grid().len() * self.x1_grid().len() * self.x2_grid().len(), + allocated: self.grid.non_zeros() + self.grid.explicit_zeros(), + zeros: self.grid.explicit_zeros(), + overhead: self.grid.overhead(), bytes_per_value: mem::size_of::(), } } fn static_scale(&self) -> Option { - (self.static_q2 > 0.0).then_some(Mu2 { - ren: self.static_q2, - fac: self.static_q2, - frg: -1.0, - }) + None } } @@ -441,7 +252,7 @@ mod tests { assert_eq!( subgrid.stats(), Stats { - total: 0, + total: 100000, allocated: 0, zeros: 0, overhead: 0, @@ -462,7 +273,7 @@ mod tests { assert_eq!( subgrid.stats(), Stats { - total: 0, + total: 100000, allocated: 0, zeros: 0, overhead: 0, From cd3dd32b9010018487726f30c12707dc36d93cc7 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 8 Sep 2024 09:40:32 +0200 Subject: [PATCH 086/213] Fix bug in `LagrangeSubgridV2::merge` --- pineappl/src/grid.rs | 18 +++---- pineappl/src/lagrange_subgrid.rs | 86 ++++---------------------------- 2 files changed, 19 insertions(+), 85 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 442d2337..0c0866fb 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -891,15 +891,15 @@ impl Grid { *subgrid = EmptySubgridV1.into(); } _ => { - todo!(); - // // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should - // // replace this with a method - // if !static_scale_detection { - // if let SubgridEnum::LagrangeSubgridV2(subgrid) = subgrid { - // // disable static-scale detection - // subgrid.static_q2 = -1.0; - // } - // } + // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should + // replace this with a method + if !static_scale_detection { + if let SubgridEnum::LagrangeSubgridV2(_subgrid) = subgrid { + // disable static-scale detection + //subgrid.static_q2 = -1.0; + todo!(); + } + } *subgrid = PackedQ1X2SubgridV1::from(&*subgrid).into(); } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 8d520c05..8ada067e 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -100,83 +100,17 @@ impl Subgrid for LagrangeSubgridV2 { } fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - // if self.is_empty() && !transpose { - // if let SubgridEnum::LagrangeSubgridV2(other) = other { - // *self = *other; - // return; - // } - // } - - let rhs_mu2 = other.mu2_grid().into_owned(); - let rhs_x1 = if transpose { - other.x2_grid() - } else { - other.x1_grid() - }; - let rhs_x2 = if transpose { - other.x1_grid() - } else { - other.x2_grid() - }; - - if (self.mu2_grid() != rhs_mu2) || (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) - { - let mut mu2_grid = self.mu2_grid().into_owned(); - let mut x1_grid = self.x1_grid().into_owned(); - let mut x2_grid = self.x2_grid().into_owned(); - - mu2_grid.extend_from_slice(&rhs_mu2); - mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - mu2_grid.dedup(); - x1_grid.extend_from_slice(&rhs_x1); - x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x1_grid.dedup(); - x2_grid.extend_from_slice(&rhs_x2); - x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x2_grid.dedup(); - - let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - - for ([i, j, k], value) in self.grid.indexed_iter() { - let target_i = mu2_grid - .iter() - .position(|mu2| *mu2 == self.mu2_grid()[i]) - .unwrap_or_else(|| unreachable!()); - let target_j = x1_grid - .iter() - .position(|&x| x == self.x1_grid()[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = x2_grid - .iter() - .position(|&x| x == self.x2_grid()[k]) - .unwrap_or_else(|| unreachable!()); - - array[[target_i, target_j, target_k]] = value; + // we cannot use `Self::indexed_iter` because it multiplies with `reweight` + if let SubgridEnum::LagrangeSubgridV2(other) = other { + // TODO: make sure `other` has the same interpolation as `self` + for (mut index, value) in other.grid.indexed_iter() { + if transpose { + index.swap(1, 2); + } + self.grid[index] += value; } - - self.grid = array; - // TODO: make sure that the interpolation of both grids are the same - } - - for ((i, j, k), value) in other.indexed_iter() { - let (j, k) = if transpose { (k, j) } else { (j, k) }; - let target_i = self - .mu2_grid() - .iter() - .position(|x| *x == rhs_mu2[i]) - .unwrap_or_else(|| unreachable!()); - let target_j = self - .x1_grid() - .iter() - .position(|&x| x == rhs_x1[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = self - .x2_grid() - .iter() - .position(|&x| x == rhs_x2[k]) - .unwrap_or_else(|| unreachable!()); - - self.grid[[target_i, target_j, target_k]] += value; + } else { + unimplemented!(); } } From cb1ce331918e13cb0a2a3414ec1892238c90ba70 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sun, 8 Sep 2024 11:09:14 +0200 Subject: [PATCH 087/213] Return support for static scale detection --- pineappl/src/grid.rs | 5 ++--- pineappl/src/interpolation.rs | 8 +++++--- pineappl/src/lagrange_subgrid.rs | 34 ++++++++++++++++++++++---------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 0c0866fb..47b3e7a4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -894,10 +894,9 @@ impl Grid { // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should // replace this with a method if !static_scale_detection { - if let SubgridEnum::LagrangeSubgridV2(_subgrid) = subgrid { + if let SubgridEnum::LagrangeSubgridV2(subgrid) = subgrid { // disable static-scale detection - //subgrid.static_q2 = -1.0; - todo!(); + subgrid.static_q2 = -1.0; } } diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 57bb349e..017a0e09 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -223,12 +223,12 @@ pub fn interpolate( ntuple: &[f64], weight: f64, array: &mut impl IndexMut<[usize; D], Output = f64>, -) { +) -> bool { use super::packed_array; use itertools::Itertools; if weight == 0.0 { - return; + return false; } // we must have as many variables as we want to interpolate @@ -241,7 +241,7 @@ pub fn interpolate( .map(|(interp, &x)| interp.interpolate(x)) .collect() else { - return; + return false; }; // TODO: add static value detection @@ -273,6 +273,8 @@ pub fn interpolate( } array[index] += weight * node_weights.iter().product::(); } + + true } #[cfg(test)] diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 8ada067e..3aac82ad 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -5,6 +5,7 @@ use super::packed_array::PackedArray; use super::subgrid::{ ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, }; +use float_cmp::approx_eq; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::mem; @@ -14,6 +15,7 @@ use std::mem; pub struct LagrangeSubgridV2 { grid: PackedArray, interps: [Interp; 3], + pub(crate) static_q2: f64, } impl LagrangeSubgridV2 { @@ -63,6 +65,7 @@ impl LagrangeSubgridV2 { InterpMeth::Lagrange, ), ], + static_q2: 0.0, } } } @@ -72,7 +75,14 @@ impl Subgrid for LagrangeSubgridV2 { // TODO: change the order of ntuple higher up in the code let mut ntuple = ntuple.to_vec(); ntuple.rotate_right(1); - interpolation::interpolate(&self.interps, &ntuple, weight, &mut self.grid); + if interpolation::interpolate(&self.interps, &ntuple, weight, &mut self.grid) { + let q2 = ntuple[0]; + if self.static_q2 == 0.0 { + self.static_q2 = q2; + } else if (self.static_q2 != -1.0) && !approx_eq!(f64, self.static_q2, q2, ulps = 4) { + self.static_q2 = -1.0; + } + } } fn mu2_grid(&self) -> Cow<[Mu2]> { @@ -166,7 +176,11 @@ impl Subgrid for LagrangeSubgridV2 { } fn static_scale(&self) -> Option { - None + (self.static_q2 > 0.0).then_some(Mu2 { + ren: self.static_q2, + fac: self.static_q2, + frg: -1.0, + }) } } @@ -236,10 +250,10 @@ mod tests { assert_eq!( subgrid.stats(), Stats { - total: 50 * 50 * 4, - allocated: 50 * 50 * 4, - zeros: 50 * 50 * 4 - 4 * 4 * 4, - overhead: 0, + total: 100000, + allocated: 64, + zeros: 0, + overhead: 32, bytes_per_value: mem::size_of::() } ); @@ -252,10 +266,10 @@ mod tests { assert_eq!( subgrid.stats(), Stats { - total: 50 * 50 * 23, - allocated: 50 * 50 * 23, - zeros: 50 * 50 * 23 - 4 * 4 * 4 * 2, - overhead: 0, + total: 100000, + allocated: 128, + zeros: 0, + overhead: 64, bytes_per_value: mem::size_of::() } ); From 7d6c9f8633b342480fe2d851dbc5f17b104c34ed Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 10 Sep 2024 11:12:32 +0200 Subject: [PATCH 088/213] Check number of channel PIDs --- pineappl/src/boc.rs | 13 ++++++++++++- pineappl/src/grid.rs | 26 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 2046c34c..90c82431 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -318,9 +318,20 @@ impl Channel { /// /// let _ = Channel::new(vec![]); /// ``` + /// + /// Creating a channel with entries that have a different number of PIDs panics: + /// ```rust,should_panic + /// use pineappl::boc::Channel; + /// + /// let _ = Channel::new(vec![(vec![1, 1, 1], 1.0), (vec![1, 1], 1.0)]); + /// ``` #[must_use] pub fn new(mut entry: Vec<(Vec, f64)>) -> Self { - assert!(!entry.is_empty()); + assert!(!entry.is_empty(), "can not create empty channel"); + assert!( + entry.iter().map(|(pids, _)| pids.len()).all_equal(), + "can not create channel with a different number of PIDs" + ); // sort `entry` because the ordering doesn't matter and because it makes it easier to // compare `Channel` objects with each other diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 47b3e7a4..ec51ac0b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -161,7 +161,17 @@ impl Grid { convolutions: Vec, subgrid_params: SubgridParams, ) -> Self { - // TODO: check that channels has same number of PIDs everywhere + for (channel_idx, channel) in channels.iter().enumerate() { + let offending_entry = channel + .entry() + .iter() + .find_map(|(pids, _)| (pids.len() != convolutions.len()).then_some(pids.len())); + + if let Some(pids_len) = offending_entry { + panic!("channel #{channel_idx} has wrong number of PIDs: expected {}, found {pids_len}", convolutions.len()); + } + } + Self { subgrids: Array3::from_shape_simple_fn( (orders.len(), bin_limits.len() - 1, channels.len()), @@ -1627,6 +1637,20 @@ mod tests { use float_cmp::assert_approx_eq; use std::fs::File; + #[test] + #[should_panic(expected = "channel #0 has wrong number of PIDs: expected 2, found 3")] + fn grid_new_panic() { + let channel = vec![(vec![1, -1, 1], 1.0), (vec![2, -2, 2], 1.0)]; + + let _ = Grid::new( + vec![Channel::new(channel)], + vec![Order::new(0, 2, 0, 0, 0)], + vec![0.0, 1.0], + vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + SubgridParams::default(), + ); + } + #[test] fn grid_with_subgrid_type() { let subgrid_type = String::from("Idontexist"); From 36a46b4f6cd227bd61ee5c96a96a5bac343a7e2f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 10 Sep 2024 11:55:52 +0200 Subject: [PATCH 089/213] Add enum `Kinematics` --- Cargo.lock | 3 +++ pineappl/Cargo.toml | 2 +- pineappl/src/boc.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index eaf5c705..2dc983db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,6 +139,9 @@ name = "bitflags" version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +dependencies = [ + "serde", +] [[package]] name = "block-buffer" diff --git a/pineappl/Cargo.toml b/pineappl/Cargo.toml index e028d92e..9cc1254c 100644 --- a/pineappl/Cargo.toml +++ b/pineappl/Cargo.toml @@ -19,7 +19,7 @@ workspace = true anyhow = "1.0.48" arrayvec = "0.7.2" bincode = "1.3.3" -bitflags = "2.4.2" +bitflags = { features = ["serde"], version = "2.4.2" } enum_dispatch = "0.3.7" float-cmp = "0.9.0" git-version = "0.3.5" diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 90c82431..a5fc7a5c 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -3,6 +3,7 @@ //! //! [`Grid`]: super::grid::Grid +use bitflags::bitflags; use float_cmp::approx_eq; use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -10,6 +11,55 @@ use std::cmp::Ordering; use std::str::FromStr; use thiserror::Error; +bitflags! { + /// TODO + #[derive(Clone, Copy, Deserialize, PartialEq, Serialize)] + #[repr(transparent)] + pub struct Scale: u32 { + /// TODO + const REN = 0b001; + /// TODO + const FAC = 0b010; + /// TODO + const FRG = 0b100; + } +} + +/// TODO +#[derive(Clone, Copy, Deserialize, PartialEq, Serialize)] +pub enum Kinematics { + /// TODO + Mu2(Scale), + /// TODO + X(usize), +} + +impl Kinematics { + /// TODO + pub const MU2_R: Self = Self::Mu2(Scale::REN); + + /// TODO + pub const MU2_F: Self = Self::Mu2(Scale::FAC); + + /// TODO + pub const MU2_A: Self = Self::Mu2(Scale::FRG); + + /// TODO + pub const MU2_RFA: Self = Self::Mu2(Scale::REN.union(Scale::FAC).union(Scale::FRG)); + + /// TODO + pub const MU2_RF: Self = Self::Mu2(Scale::REN.union(Scale::FAC)); + + /// TODO + pub const X1: Self = Self::X(0); + + /// TODO + pub const X2: Self = Self::X(1); + + /// TODO + pub const X3: Self = Self::X(2); +} + /// Error type keeping information if [`Order::from_str`] went wrong. #[derive(Debug, Error, Eq, PartialEq)] #[error("{0}")] From 885ef075a18ed820654c5e7961380d1f5f09de0f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 10 Sep 2024 13:42:49 +0200 Subject: [PATCH 090/213] Introduce `kinematics` into `Grid` --- pineappl/src/grid.rs | 10 +++++++++- pineappl/src/v0.rs | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index ec51ac0b..0d7b4798 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1,7 +1,7 @@ //! Module containing all traits and supporting structures for grids. use super::bin::{BinInfo, BinLimits, BinRemapper}; -use super::boc::{Channel, Order}; +use super::boc::{Channel, Kinematics, Order}; use super::convolutions::{Convolution, LumiCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; @@ -149,6 +149,7 @@ pub struct Grid { pub(crate) convolutions: Vec, pub(crate) pid_basis: PidBasis, pub(crate) more_members: MoreMembers, + pub(crate) kinematics: Vec, } impl Grid { @@ -189,9 +190,13 @@ impl Grid { pid_basis: PidBasis::Pdg, channels, subgrid_params, + // TODO: make this a new parameter + kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], } } + // TODO: get rid of this constructor + /// Constructor. This function can be used like `new`, but the additional parameter /// `subgrid_type` selects the underlying `Subgrid` type. Supported values are: /// - `LagrangeSubgrid` @@ -228,6 +233,7 @@ impl Grid { pid_basis: PidBasis::Pdg, channels, more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), + kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], }) } @@ -1236,6 +1242,7 @@ impl Grid { convolutions: self.convolutions.clone(), pid_basis: info.pid_basis, more_members: self.more_members.clone(), + kinematics: self.kinematics.clone(), }; if let Some(lhs) = &mut lhs { @@ -1372,6 +1379,7 @@ impl Grid { convolutions: self.convolutions.clone(), pid_basis: infos[0].pid_basis, more_members: self.more_members.clone(), + kinematics: self.kinematics.clone(), }; assert_eq!(infos[0].pid_basis, infos[1].pid_basis); diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 14dea165..8ccb01b0 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -1,5 +1,5 @@ use super::bin::{BinLimits, BinRemapper}; -use super::boc::{Channel, Order}; +use super::boc::{Channel, Kinematics, Order}; use super::convolutions::Convolution; use super::empty_subgrid::EmptySubgridV1; use super::grid::{Grid, GridError, Mmv3, MoreMembers}; @@ -107,6 +107,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result // TODO: remove this member subgrid_template: EmptySubgridV1.into(), }), + kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], }; assert_eq!(result.bin_info().bins(), grid.bin_info().bins()); From 2203f22a4611880e7dabdf4501f26372210e097f Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 10 Sep 2024 14:44:22 +0200 Subject: [PATCH 091/213] Rename `Interp::nodes` to `Interp::node_values` --- pineappl/src/interpolation.rs | 16 ++++++++-------- pineappl/src/lagrange_subgrid.rs | 12 ++++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 017a0e09..716bdae0 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -196,7 +196,7 @@ impl Interp { } /// TODO - pub fn nodes(&self) -> Vec { + pub fn node_values(&self) -> Vec { (0..self.nodes) .map(|node| self.map_y_to_x(self.gety(node))) .collect() @@ -314,7 +314,7 @@ mod tests { ), ]; - let nodes: Vec<_> = interps.iter().map(Interp::nodes).collect(); + let node_values: Vec<_> = interps.iter().map(Interp::node_values).collect(); let q2_reference = [ 9.9999999999999986e1, @@ -359,7 +359,7 @@ mod tests { 9.9999999999999493e7, ]; - for (&node, ref_node) in nodes[0].iter().zip(q2_reference) { + for (&node, ref_node) in node_values[0].iter().zip(q2_reference) { assert_approx_eq!(f64, node, ref_node, ulps = 4); } @@ -416,11 +416,11 @@ mod tests { 1.9999999999999954e-7, ]; - for (&node, ref_node) in nodes[1].iter().zip(x_reference) { + for (&node, ref_node) in node_values[1].iter().zip(x_reference) { assert_approx_eq!(f64, node, ref_node, ulps = 4); } - for (&node, ref_node) in nodes[2].iter().zip(x_reference) { + for (&node, ref_node) in node_values[2].iter().zip(x_reference) { assert_approx_eq!(f64, node, ref_node, ulps = 4); } @@ -638,11 +638,11 @@ mod tests { assert_eq!(array[[0]], 1.0); - let nodes = interps[0].nodes(); + let node_values = interps[0].node_values(); - assert_eq!(nodes.len(), 1); + assert_eq!(node_values.len(), 1); // TODO: the return value is not the one expected (90^2), because `deltay` is zero - assert!(nodes[0].is_nan()); + assert!(node_values[0].is_nan()); } } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 3aac82ad..a5fdbd7e 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -87,7 +87,7 @@ impl Subgrid for LagrangeSubgridV2 { fn mu2_grid(&self) -> Cow<[Mu2]> { self.interps[0] - .nodes() + .node_values() .iter() .map(|&q2| Mu2 { ren: q2, @@ -98,11 +98,11 @@ impl Subgrid for LagrangeSubgridV2 { } fn x1_grid(&self) -> Cow<[f64]> { - self.interps[1].nodes().into() + self.interps[1].node_values().into() } fn x2_grid(&self) -> Cow<[f64]> { - self.interps[2].nodes().into() + self.interps[2].node_values().into() } fn is_empty(&self) -> bool { @@ -151,7 +151,11 @@ impl Subgrid for LagrangeSubgridV2 { } fn indexed_iter(&self) -> SubgridIndexedIter { - let nodes: Vec<_> = self.interps.iter().map(|interp| interp.nodes()).collect(); + let nodes: Vec<_> = self + .interps + .iter() + .map(|interp| interp.node_values()) + .collect(); Box::new(self.grid.indexed_iter().map(move |(index, v)| { ( (index[0], index[1], index[2]), From d2f8397edfd4621cde1458409f7f5bf685d7b89e Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 10 Sep 2024 16:55:51 +0200 Subject: [PATCH 092/213] Replace `SubgridParams` and `ExtraSubgridParams` with `Interp` --- pineappl/src/grid.rs | 77 +++++----- pineappl/src/interpolation.rs | 29 +++- pineappl/src/lagrange_subgrid.rs | 65 ++------ pineappl/src/subgrid.rs | 221 ---------------------------- pineappl/src/v0.rs | 38 ++++- pineappl/tests/drell_yan_lo.rs | 64 ++++---- pineappl_capi/src/lib.rs | 101 +++++++++---- pineappl_cli/src/export/applgrid.rs | 94 +++++++++--- pineappl_cli/src/import/applgrid.rs | 38 ++++- pineappl_cli/src/import/fastnlo.rs | 71 ++++++++- pineappl_cli/src/import/fktable.rs | 38 ++++- 11 files changed, 433 insertions(+), 403 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 0d7b4798..ba7a9d01 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -6,10 +6,11 @@ use super::convolutions::{Convolution, LumiCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; +use super::interpolation::Interp; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; -use super::subgrid::{ExtraSubgridParams, Mu2, Subgrid, SubgridEnum, SubgridParams}; +use super::subgrid::{Mu2, Subgrid, SubgridEnum}; use super::v0; use bitflags::bitflags; use float_cmp::approx_eq; @@ -144,12 +145,12 @@ pub struct Grid { pub(crate) channels: Vec, pub(crate) bin_limits: BinLimits, pub(crate) orders: Vec, - pub(crate) subgrid_params: SubgridParams, pub(crate) metadata: BTreeMap, pub(crate) convolutions: Vec, pub(crate) pid_basis: PidBasis, pub(crate) more_members: MoreMembers, pub(crate) kinematics: Vec, + pub(crate) interps: Vec, } impl Grid { @@ -160,7 +161,8 @@ impl Grid { orders: Vec, bin_limits: Vec, convolutions: Vec, - subgrid_params: SubgridParams, + interps: Vec, + kinematics: Vec, ) -> Self { for (channel_idx, channel) in channels.iter().enumerate() { let offending_entry = channel @@ -173,6 +175,12 @@ impl Grid { } } + assert_eq!( + interps.len(), + kinematics.len(), + "interps and kinematics have different lengths" + ); + Self { subgrids: Array3::from_shape_simple_fn( (orders.len(), bin_limits.len() - 1, channels.len()), @@ -181,17 +189,13 @@ impl Grid { orders, bin_limits: BinLimits::new(bin_limits), metadata: default_metadata(), - more_members: MoreMembers::V3(Mmv3::new( - LagrangeSubgridV2::new(&subgrid_params, &ExtraSubgridParams::from(&subgrid_params)) - .into(), - )), + more_members: MoreMembers::V3(Mmv3::new(LagrangeSubgridV2::new(&interps).into())), convolutions, // TODO: make this a new parameter pid_basis: PidBasis::Pdg, channels, - subgrid_params, - // TODO: make this a new parameter - kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + interps, + kinematics, } } @@ -209,14 +213,11 @@ impl Grid { channels: Vec, orders: Vec, bin_limits: Vec, - subgrid_params: SubgridParams, - extra: ExtraSubgridParams, + interps: Vec, subgrid_type: &str, ) -> Result { let subgrid_template: SubgridEnum = match subgrid_type { - "LagrangeSubgrid" | "LagrangeSubgridV2" => { - LagrangeSubgridV2::new(&subgrid_params, &extra).into() - } + "LagrangeSubgrid" | "LagrangeSubgridV2" => LagrangeSubgridV2::new(&interps).into(), _ => return Err(GridError::UnknownSubgridType(subgrid_type.to_owned())), }; @@ -227,12 +228,12 @@ impl Grid { ), orders, bin_limits: BinLimits::new(bin_limits), - subgrid_params, metadata: default_metadata(), convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], pid_basis: PidBasis::Pdg, channels, more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), + interps, kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], }) } @@ -1237,7 +1238,7 @@ impl Grid { channels, bin_limits: self.bin_limits.clone(), orders: vec![Order::new(0, 0, 0, 0, 0)], - subgrid_params: SubgridParams::default(), + interps: self.interps.clone(), metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), pid_basis: info.pid_basis, @@ -1374,7 +1375,7 @@ impl Grid { channels, bin_limits: self.bin_limits.clone(), orders: vec![Order::new(0, 0, 0, 0, 0)], - subgrid_params: SubgridParams::default(), + interps: self.interps.clone(), metadata: self.metadata.clone(), convolutions: self.convolutions.clone(), pid_basis: infos[0].pid_basis, @@ -1655,21 +1656,16 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 1.0], vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); } #[test] fn grid_with_subgrid_type() { let subgrid_type = String::from("Idontexist"); - let result = Grid::with_subgrid_type( - vec![], - vec![], - vec![], - SubgridParams::default(), - ExtraSubgridParams::default(), - &subgrid_type, - ); + let result = + Grid::with_subgrid_type(vec![], vec![], vec![], v0::default_interps(), &subgrid_type); matches!(result, Err(GridError::UnknownSubgridType(x)) if x == subgrid_type); } @@ -1684,7 +1680,8 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); assert_eq!(grid.bin_info().bins(), 4); @@ -1700,7 +1697,8 @@ mod tests { vec![Order::new(1, 2, 0, 0, 0), Order::new(1, 2, 0, 1, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); // merging with empty subgrids should not change the grid @@ -1721,7 +1719,8 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); assert_eq!(grid.bin_info().bins(), 4); @@ -1740,7 +1739,8 @@ mod tests { ], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 1.0); @@ -1766,7 +1766,8 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); assert_eq!(grid.bin_info().bins(), 4); @@ -1778,7 +1779,8 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); // fill the photon-photon entry @@ -1801,7 +1803,8 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); assert_eq!(grid.bin_info().bins(), 2); @@ -1817,7 +1820,8 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); @@ -1845,7 +1849,8 @@ mod tests { }], vec![0.0, 1.0], vec![Convolution::UnpolPDF(2212); 2], - SubgridParams::default(), + v0::default_interps(), + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); // by default we assume unpolarized proton PDFs are used diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 716bdae0..2d7d9dc0 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -57,7 +57,7 @@ fn lagrange_weights(i: usize, n: usize, u: f64) -> f64 { } /// TODO -#[derive(Clone, Deserialize, Serialize)] +#[derive(Clone, Copy, Deserialize, Serialize)] pub enum ReweightMeth { /// TODO ApplGridX, @@ -66,7 +66,7 @@ pub enum ReweightMeth { } /// TODO -#[derive(Clone, Deserialize, Serialize)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub enum Map { /// TODO ApplGridF2, @@ -215,6 +215,26 @@ impl Interp { Map::ApplGridH0 => applgrid::ftau0(x), } } + + /// TODO + pub fn nodes(&self) -> usize { + self.nodes + } + + /// TODO + pub fn min(&self) -> f64 { + self.min + } + + /// TODO + pub fn max(&self) -> f64 { + self.max + } + + /// TODO + pub fn map(&self) -> Map { + self.map + } } /// TODO @@ -359,6 +379,8 @@ mod tests { 9.9999999999999493e7, ]; + assert_eq!(node_values[0].len(), interps[0].nodes()); + for (&node, ref_node) in node_values[0].iter().zip(q2_reference) { assert_approx_eq!(f64, node, ref_node, ulps = 4); } @@ -416,6 +438,9 @@ mod tests { 1.9999999999999954e-7, ]; + assert_eq!(node_values[1].len(), interps[1].nodes()); + assert_eq!(node_values[2].len(), interps[2].nodes()); + for (&node, ref_node) in node_values[1].iter().zip(x_reference) { assert_approx_eq!(f64, node, ref_node, ulps = 4); } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index a5fdbd7e..5ef873b2 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -1,10 +1,8 @@ //! Module containing the Lagrange-interpolation subgrid. -use super::interpolation::{self, Interp, InterpMeth, Map, ReweightMeth}; +use super::interpolation::{self, Interp}; use super::packed_array::PackedArray; -use super::subgrid::{ - ExtraSubgridParams, Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter, SubgridParams, -}; +use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use float_cmp::approx_eq; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -14,57 +12,18 @@ use std::mem; #[derive(Clone, Deserialize, Serialize)] pub struct LagrangeSubgridV2 { grid: PackedArray, - interps: [Interp; 3], + interps: Vec, pub(crate) static_q2: f64, } impl LagrangeSubgridV2 { /// Constructor. #[must_use] - pub fn new(subgrid_params: &SubgridParams, extra_params: &ExtraSubgridParams) -> Self { + pub fn new(interps: &[Interp]) -> Self { + debug_assert_eq!(interps.len(), 3); Self { - grid: PackedArray::new([ - subgrid_params.q2_bins(), - subgrid_params.x_bins(), - extra_params.x2_bins(), - ]), - interps: [ - Interp::new( - subgrid_params.q2_min(), - subgrid_params.q2_max(), - subgrid_params.q2_bins(), - subgrid_params.q2_order(), - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - subgrid_params.x_min(), - subgrid_params.x_max(), - subgrid_params.x_bins(), - subgrid_params.x_order(), - if subgrid_params.reweight() { - ReweightMeth::ApplGridX - } else { - ReweightMeth::NoReweight - }, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( - extra_params.x2_min(), - extra_params.x2_max(), - extra_params.x2_bins(), - extra_params.x2_order(), - if extra_params.reweight2() { - ReweightMeth::ApplGridX - } else { - ReweightMeth::NoReweight - }, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - ], + grid: PackedArray::new([interps[0].nodes(), interps[1].nodes(), interps[2].nodes()]), + interps: interps.to_vec(), static_q2: 0.0, } } @@ -191,11 +150,11 @@ impl Subgrid for LagrangeSubgridV2 { #[cfg(test)] mod tests { use super::*; + use crate::v0; #[test] fn fill_zero() { - let mut subgrid = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); + let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); subgrid.fill(&[0.5, 0.5, 1000.0], 0.0); @@ -215,8 +174,7 @@ mod tests { #[test] fn fill_outside_range() { - let mut subgrid = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); + let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); subgrid.fill(&[1e-10, 0.5, 1000.0], 0.0); @@ -236,8 +194,7 @@ mod tests { #[test] fn fill() { - let mut subgrid = - LagrangeSubgridV2::new(&SubgridParams::default(), &ExtraSubgridParams::default()); + let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); subgrid.fill(&[0.5, 0.5, 1000.0], 1.0); diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 6c62e3b2..4ad28995 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -119,224 +119,3 @@ pub trait Subgrid { /// Type to iterate over the non-zero contents of a subgrid. The tuple contains the indices of the /// `mu2_grid`, the `x1_grid` and finally the `x2_grid`. pub type SubgridIndexedIter<'a> = Box + 'a>; - -/// Subgrid creation parameters for subgrids that perform interpolation. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct SubgridParams { - q2_bins: usize, - q2_max: f64, - q2_min: f64, - q2_order: usize, - reweight: bool, - x_bins: usize, - x_max: f64, - x_min: f64, - x_order: usize, -} - -impl Default for SubgridParams { - fn default() -> Self { - Self { - q2_bins: 40, - q2_max: 1e8, - q2_min: 1e2, - q2_order: 3, - reweight: true, - x_bins: 50, - x_max: 1.0, - x_min: 2e-7, - x_order: 3, - } - } -} - -impl SubgridParams { - /// Returns the number of bins for the $Q^2$ axis. - #[must_use] - pub const fn q2_bins(&self) -> usize { - self.q2_bins - } - - /// Returns the upper limit of the $Q^2$ axis. - #[must_use] - pub const fn q2_max(&self) -> f64 { - self.q2_max - } - - /// Returns the lower limit of the $Q^2$ axis. - #[must_use] - pub const fn q2_min(&self) -> f64 { - self.q2_min - } - - /// Returns the interpolation order for the $Q^2$ axis. - #[must_use] - pub const fn q2_order(&self) -> usize { - self.q2_order - } - - /// Returns whether reweighting is enabled or not. - #[must_use] - pub const fn reweight(&self) -> bool { - self.reweight - } - - /// Sets the number of bins for the $Q^2$ axis. - pub fn set_q2_bins(&mut self, q2_bins: usize) { - self.q2_bins = q2_bins; - } - - /// Sets the upper limit of the $Q^2$ axis. - pub fn set_q2_max(&mut self, q2_max: f64) { - self.q2_max = q2_max; - } - - /// Sets the lower limit of the $Q^2$ axis. - pub fn set_q2_min(&mut self, q2_min: f64) { - self.q2_min = q2_min; - } - - /// Sets the interpolation order for the $Q^2$ axis. - pub fn set_q2_order(&mut self, q2_order: usize) { - self.q2_order = q2_order; - } - - /// Sets the reweighting parameter. - pub fn set_reweight(&mut self, reweight: bool) { - self.reweight = reweight; - } - - /// Sets the number of bins for the $x$ axes. - pub fn set_x_bins(&mut self, x_bins: usize) { - self.x_bins = x_bins; - } - - /// Sets the upper limit of the $x$ axes. - pub fn set_x_max(&mut self, x_max: f64) { - self.x_max = x_max; - } - - /// Sets the lower limit of the $x$ axes. - pub fn set_x_min(&mut self, x_min: f64) { - self.x_min = x_min; - } - - /// Sets the interpolation order for the $x$ axes. - pub fn set_x_order(&mut self, x_order: usize) { - self.x_order = x_order; - } - - /// Returns the number of bins for the $x$ axes. - #[must_use] - pub const fn x_bins(&self) -> usize { - self.x_bins - } - - /// Returns the upper limit of the $x$ axes. - #[must_use] - pub const fn x_max(&self) -> f64 { - self.x_max - } - - /// Returns the lower limit of the $x$ axes. - #[must_use] - pub const fn x_min(&self) -> f64 { - self.x_min - } - - /// Returns the interpolation order for the $x$ axes. - #[must_use] - pub const fn x_order(&self) -> usize { - self.x_order - } -} - -/// Extra grid creation parameters when the limits for `x1` and `x2` are different. -pub struct ExtraSubgridParams { - reweight2: bool, - x2_bins: usize, - x2_max: f64, - x2_min: f64, - x2_order: usize, -} - -impl Default for ExtraSubgridParams { - fn default() -> Self { - Self { - reweight2: true, - x2_bins: 50, - x2_max: 1.0, - x2_min: 2e-7, - x2_order: 3, - } - } -} - -impl From<&SubgridParams> for ExtraSubgridParams { - fn from(subgrid_params: &SubgridParams) -> Self { - Self { - reweight2: subgrid_params.reweight(), - x2_bins: subgrid_params.x_bins(), - x2_max: subgrid_params.x_max(), - x2_min: subgrid_params.x_min(), - x2_order: subgrid_params.x_order(), - } - } -} - -impl ExtraSubgridParams { - /// Returns whether reweighting is enabled for the `x2` axis or not. - #[must_use] - pub const fn reweight2(&self) -> bool { - self.reweight2 - } - - /// Sets the reweighting parameter for the `x2` axis. - pub fn set_reweight2(&mut self, reweight2: bool) { - self.reweight2 = reweight2; - } - - /// Sets the number of bins for the `x2` axes. - pub fn set_x2_bins(&mut self, x_bins: usize) { - self.x2_bins = x_bins; - } - - /// Sets the upper limit of the `x2` axes. - pub fn set_x2_max(&mut self, x_max: f64) { - self.x2_max = x_max; - } - - /// Sets the lower limit of the `x2` axes. - pub fn set_x2_min(&mut self, x_min: f64) { - self.x2_min = x_min; - } - - /// Sets the interpolation order for the `x2` axes. - pub fn set_x2_order(&mut self, x_order: usize) { - self.x2_order = x_order; - } - - /// Returns the number of bins for the `x2` axes. - #[must_use] - pub const fn x2_bins(&self) -> usize { - self.x2_bins - } - - /// Returns the upper limit of the `x2` axes. - #[must_use] - pub const fn x2_max(&self) -> f64 { - self.x2_max - } - - /// Returns the lower limit of the `x2` axes. - #[must_use] - pub const fn x2_min(&self) -> f64 { - self.x2_min - } - - /// Returns the interpolation order for the `x2` axes. - #[must_use] - pub const fn x2_order(&self) -> usize { - self.x2_order - } -} diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 8ccb01b0..ab873b29 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -3,15 +3,48 @@ use super::boc::{Channel, Kinematics, Order}; use super::convolutions::Convolution; use super::empty_subgrid::EmptySubgridV1; use super::grid::{Grid, GridError, Mmv3, MoreMembers}; +use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::PidBasis; -use super::subgrid::{Mu2, SubgridParams}; +use super::subgrid::Mu2; use ndarray::Array3; use pineappl_v0::grid::Grid as GridV0; use std::io::BufRead; use std::iter; +pub fn default_interps() -> Vec { + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ] +} + pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { use pineappl_v0::subgrid::Subgrid as _; @@ -78,8 +111,6 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result logxia: 0, }) .collect(), - // TODO: remove this member - subgrid_params: SubgridParams::default(), metadata: grid .key_values() .cloned() @@ -108,6 +139,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result subgrid_template: EmptySubgridV1.into(), }), kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + interps: default_interps(), }; assert_eq!(result.bin_info().bins(), grid.bin_info().bins()); diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index f939d144..413416ee 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -7,7 +7,8 @@ use pineappl::boc::Order; use pineappl::channel; use pineappl::convolutions::LumiCache; use pineappl::grid::{Grid, GridOptFlags}; -use pineappl::subgrid::{ExtraSubgridParams, Subgrid, SubgridEnum, SubgridParams}; +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; +use pineappl::subgrid::{Subgrid, SubgridEnum}; use rand::Rng; use rand_pcg::Pcg64; use std::f64::consts::PI; @@ -162,33 +163,44 @@ fn fill_drell_yan_lo_grid( // we bin in rapidity from 0 to 2.4 in steps of 0.1 let bin_limits: Vec<_> = (0..=24).map(|x: u32| f64::from(x) / 10.0).collect(); - let mut subgrid_params = SubgridParams::default(); - let mut extra = ExtraSubgridParams::default(); - - subgrid_params.set_q2_bins(30); - subgrid_params.set_q2_max(1e6); - subgrid_params.set_q2_min(1e2); - subgrid_params.set_q2_order(3); - subgrid_params.set_reweight(reweight); - subgrid_params.set_x_bins(50); - subgrid_params.set_x_max(1.0); - subgrid_params.set_x_min(2e-7); - subgrid_params.set_x_order(3); - extra.set_x2_bins(50); - extra.set_x2_max(1.0); - extra.set_x2_min(2e-7); - extra.set_x2_order(3); - extra.set_reweight2(reweight); + let reweight = if reweight { + ReweightMeth::ApplGridX + } else { + ReweightMeth::NoReweight + }; + + let interps = vec![ + Interp::new( + 1e2, + 1e6, + 30, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + reweight, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + reweight, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ]; // create the PineAPPL grid - let mut grid = Grid::with_subgrid_type( - channels, - orders, - bin_limits, - subgrid_params, - extra, - subgrid_type, - )?; + let mut grid = Grid::with_subgrid_type(channels, orders, bin_limits, interps, subgrid_type)?; // in GeV^2 pbarn let hbarc2 = 3.893793721e8; diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 5392905a..95acb9c3 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -60,7 +60,7 @@ use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Order}; use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; -use pineappl::subgrid::{ExtraSubgridParams, SubgridParams}; +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use std::collections::HashMap; use std::ffi::{CStr, CString}; use std::fs::File; @@ -71,10 +71,21 @@ use std::slice; // TODO: make sure no `panic` calls leave functions marked as `extern "C"` -fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgridParams) { +fn grid_params(key_vals: Option<&KeyVal>) -> (String, Vec) { let mut subgrid_type = "LagrangeSubgrid".to_owned(); - let mut subgrid_params = SubgridParams::default(); - let mut extra = ExtraSubgridParams::default(); + let mut q2_min = 1e2; + let mut q2_max = 1e8; + let mut q2_nodes = 40; + let mut q2_order = 3; + let mut x1_min = 2e-7; + let mut x1_max = 1.0; + let mut x1_nodes = 50; + let mut x1_order = 3; + let mut x2_min = 2e-7; + let mut x2_max = 1.0; + let mut x2_nodes = 50; + let mut x2_order = 3; + let mut reweight = ReweightMeth::ApplGridX; if let Some(keyval) = key_vals { if let Some(value) = keyval @@ -82,7 +93,7 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .get("q2_bins") .or_else(|| keyval.ints.get("nq2")) { - subgrid_params.set_q2_bins(usize::try_from(*value).unwrap()); + q2_nodes = usize::try_from(*value).unwrap(); } if let Some(value) = keyval @@ -90,7 +101,7 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .get("q2_max") .or_else(|| keyval.doubles.get("q2max")) { - subgrid_params.set_q2_max(*value); + q2_max = *value; } if let Some(value) = keyval @@ -98,7 +109,7 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .get("q2_min") .or_else(|| keyval.doubles.get("q2min")) { - subgrid_params.set_q2_min(*value); + q2_min = *value; } if let Some(value) = keyval @@ -106,17 +117,19 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .get("q2_order") .or_else(|| keyval.ints.get("q2order")) { - subgrid_params.set_q2_order(usize::try_from(*value).unwrap()); + q2_order = usize::try_from(*value).unwrap(); } if let Some(value) = keyval.bools.get("reweight") { - subgrid_params.set_reweight(*value); + if !value { + reweight = ReweightMeth::NoReweight; + } } if let Some(value) = keyval.ints.get("x_bins").or_else(|| keyval.ints.get("nx")) { let value = usize::try_from(*value).unwrap(); - subgrid_params.set_x_bins(value); - extra.set_x2_bins(value); + x1_nodes = value; + x2_nodes = value; } if let Some(value) = keyval @@ -124,8 +137,8 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .get("x_max") .or_else(|| keyval.doubles.get("xmax")) { - subgrid_params.set_x_max(*value); - extra.set_x2_max(*value); + x1_max = *value; + x2_max = *value; } if let Some(value) = keyval @@ -133,8 +146,8 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .get("x_min") .or_else(|| keyval.doubles.get("xmin")) { - subgrid_params.set_x_min(*value); - extra.set_x2_min(*value); + x1_min = *value; + x2_min = *value; } if let Some(value) = keyval @@ -143,40 +156,40 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri .or_else(|| keyval.ints.get("xorder")) { let value = usize::try_from(*value).unwrap(); - subgrid_params.set_x_order(value); - extra.set_x2_order(value); + x1_order = value; + x2_order = value; } if let Some(value) = keyval.ints.get("x1_bins") { - subgrid_params.set_x_bins(usize::try_from(*value).unwrap()); + x1_nodes = usize::try_from(*value).unwrap(); } if let Some(value) = keyval.doubles.get("x1_max") { - subgrid_params.set_x_max(*value); + x1_max = *value; } if let Some(value) = keyval.doubles.get("x1_min") { - subgrid_params.set_x_min(*value); + x1_min = *value; } if let Some(value) = keyval.ints.get("x1_order") { - subgrid_params.set_x_order(usize::try_from(*value).unwrap()); + x1_order = usize::try_from(*value).unwrap(); } if let Some(value) = keyval.ints.get("x2_bins") { - extra.set_x2_bins(usize::try_from(*value).unwrap()); + x2_nodes = usize::try_from(*value).unwrap(); } if let Some(value) = keyval.doubles.get("x2_max") { - extra.set_x2_max(*value); + x2_max = *value; } if let Some(value) = keyval.doubles.get("x2_min") { - extra.set_x2_min(*value); + x2_min = *value; } if let Some(value) = keyval.ints.get("x2_order") { - extra.set_x2_order(usize::try_from(*value).unwrap()); + x2_order = usize::try_from(*value).unwrap(); } if let Some(value) = keyval.strings.get("subgrid_type") { @@ -184,7 +197,38 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, SubgridParams, ExtraSubgri } } - (subgrid_type, subgrid_params, extra) + ( + subgrid_type, + vec![ + Interp::new( + q2_min, + q2_max, + q2_nodes, + q2_order, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + x1_min, + x1_max, + x1_nodes, + x1_order, + reweight, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + x2_min, + x2_max, + x2_nodes, + x2_order, + reweight, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ], + ) } /// Type for defining a luminosity function. @@ -693,7 +737,7 @@ pub unsafe extern "C" fn pineappl_grid_new( .collect(); let key_vals = unsafe { key_vals.as_ref() }; - let (subgrid_type, subgrid_params, extra) = grid_params(key_vals); + let (subgrid_type, interps) = grid_params(key_vals); let lumi = unsafe { &*lumi }; @@ -714,8 +758,7 @@ pub unsafe extern "C" fn pineappl_grid_new( lumi.0.clone(), orders, unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), - subgrid_params, - extra, + interps, &subgrid_type, ) .unwrap(), diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 74e6b38b..d75fc8c7 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -6,7 +6,8 @@ use ndarray::{s, Axis}; use pineappl::boc::Order; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; -use pineappl::subgrid::{Mu2, Subgrid, SubgridParams}; +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; +use pineappl::subgrid::{Mu2, Subgrid}; use pineappl_applgrid::ffi::{self, grid}; use std::borrow::Cow; use std::f64::consts::TAU; @@ -14,9 +15,7 @@ use std::iter; use std::path::Path; use std::pin::Pin; -fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result { - let mut result = SubgridParams::default(); - +fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result> { let mu2_grid: Vec<_> = grid .subgrids() .slice(s![order, bin, ..]) @@ -41,16 +40,57 @@ fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result>()?; let mut mu2_grid: Vec<_> = mu2_grid.into_iter().flatten().collect(); mu2_grid.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = 128)); - let mu2_grid = mu2_grid.as_slice(); - if let &[fac] = mu2_grid { - result.set_q2_bins(1); - result.set_q2_max(fac); - result.set_q2_min(fac); - result.set_q2_order(0); + // TODO: implement the general case + let mut result = vec![ + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ]; + + if let &[fac] = mu2_grid.as_slice() { + result.insert( + 0, + Interp::new( + fac, + fac, + 1, + 0, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + ); + } else { + result.insert( + 0, + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + ); } - // TODO: implement the general case Ok(result) } @@ -165,19 +205,29 @@ pub fn convert_into_applgrid( .axis_iter(Axis(0)) .enumerate() { - let p = reconstruct_subgrid_params(grid, order, bin)?; + let interps = reconstruct_subgrid_params(grid, order, bin)?; + // TODO: support DIS case + assert_eq!(interps.len(), 3); + + // TODO: make sure interps[1] is the same as interps[2] let mut igrid = ffi::make_igrid( - p.q2_bins().try_into().unwrap(), - p.q2_min(), - p.q2_max(), - p.q2_order().try_into().unwrap(), - p.x_bins().try_into().unwrap(), - p.x_min(), - p.x_max(), - p.x_order().try_into().unwrap(), - "f2", - "h0", + interps[0].nodes().try_into().unwrap(), + interps[0].min(), + interps[0].max(), + interps[0].order().try_into().unwrap(), + interps[1].nodes().try_into().unwrap(), + interps[1].min(), + interps[1].max(), + interps[1].order().try_into().unwrap(), + match interps[1].map() { + Map::ApplGridF2 => "f2", + map => panic!("export does not support {map:?}"), + }, + match interps[0].map() { + Map::ApplGridH0 => "h0", + map => panic!("export does not support {map:?}"), + }, grid.channels().len().try_into().unwrap(), has_pdf1 != has_pdf2, ); diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index e775b55e..e5b97c93 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -1,11 +1,12 @@ use anyhow::Result; use lhapdf::Pdf; -use pineappl::boc::{Channel, Order}; +use pineappl::boc::{Channel, Kinematics, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; -use pineappl::subgrid::{Mu2, SubgridParams}; +use pineappl::subgrid::Mu2; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::pin::Pin; @@ -137,7 +138,38 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul vec![order], bin_limits.clone(), convolutions.clone(), - SubgridParams::default(), + // TODO: read out interpolation parameters from APPLgrid + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ], + // TODO: change kinematics for DIS + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); for bin in 0..grid.Nobs_internal() { diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 2c937060..09b7b5d4 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -2,12 +2,13 @@ use anyhow::Result; use itertools::Itertools; use ndarray::s; use pineappl::bin::BinRemapper; -use pineappl::boc::{Channel, Order}; +use pineappl::boc::{Channel, Kinematics, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; -use pineappl::subgrid::{Mu2, SubgridParams}; +use pineappl::subgrid::Mu2; use pineappl_fastnlo::ffi::{ self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF, fastNLOPDFLinearCombinations, EScaleFunctionalForm, @@ -126,7 +127,38 @@ fn convert_coeff_add_fix( .map(|limit| u16::try_from(limit).unwrap().into()) .collect(), convolutions, - SubgridParams::default(), + // TODO: read out interpolation parameters from fastNLO + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ], + // TODO: change kinematics for DIS + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); let total_scalenodes: usize = table.GetTotalScalenodes().try_into().unwrap(); @@ -274,7 +306,38 @@ fn convert_coeff_add_flex( .map(|limit| u16::try_from(limit).unwrap().into()) .collect(), convolutions, - SubgridParams::default(), + // TODO: read out interpolation parameters from fastNLO + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ], + // TODO: change kinematics for DIS + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); let rescale = 0.1_f64.powi(table.GetIXsectUnits() - ipub_units); diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 70731b3b..f7d34dac 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -1,14 +1,15 @@ use anyhow::{anyhow, Context, Result}; use flate2::read::GzDecoder; use ndarray::s; -use pineappl::boc::Order; +use pineappl::boc::{Kinematics, Order}; use pineappl::channel; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::pids::PidBasis; -use pineappl::subgrid::{Mu2, SubgridParams}; +use pineappl::subgrid::Mu2; use std::fs::File; use std::io::{BufRead, BufReader}; use std::iter; @@ -111,7 +112,38 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { Convolution::None }, ], - SubgridParams::default(), + // TODO: what are sensible parameters for FK-tables? + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ], + // TODO: change kinematics for DIS + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); // explicitly set the evolution basis From b2cd8a76d12e489fa5d367787db61c6aca04a9e6 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 10 Sep 2024 17:55:52 +0200 Subject: [PATCH 093/213] Fix bug returning variables from y-space instead of x --- pineappl/src/interpolation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 2d7d9dc0..75a35afc 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -223,12 +223,12 @@ impl Interp { /// TODO pub fn min(&self) -> f64 { - self.min + self.map_y_to_x(self.min).min(self.map_y_to_x(self.max)) } /// TODO pub fn max(&self) -> f64 { - self.max + self.map_y_to_x(self.min).max(self.map_y_to_x(self.max)) } /// TODO From 88f9151b96327c59760abc8c5ec7ff8cdf020be2 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 11:36:45 +0200 Subject: [PATCH 094/213] Fix a few clippy lints --- clippy.toml | 1 + pineappl/src/boc.rs | 5 +++-- pineappl/src/grid.rs | 23 +++++++++++++++++++---- pineappl/src/interpolation.rs | 18 ++++++++++++++---- pineappl/src/lagrange_subgrid.rs | 7 ++----- pineappl/src/packed_array.rs | 5 +++++ pineappl/src/packed_subgrid.rs | 4 ++-- pineappl/src/v0.rs | 4 ++-- pineappl_applgrid/src/lib.rs | 3 ++- pineappl_cli/src/export/applgrid.rs | 27 ++++++++------------------- pineappl_cli/src/plot.rs | 10 +++++----- 11 files changed, 63 insertions(+), 44 deletions(-) create mode 100644 clippy.toml diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 00000000..ca61e3bf --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +doc-valid-idents = ["APPLgrid", "PineAPPL", ".."] diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index a5fc7a5c..5afdb2e0 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -13,7 +13,7 @@ use thiserror::Error; bitflags! { /// TODO - #[derive(Clone, Copy, Deserialize, PartialEq, Serialize)] + #[derive(Clone, Copy, Deserialize, Eq, PartialEq, Serialize)] #[repr(transparent)] pub struct Scale: u32 { /// TODO @@ -26,7 +26,7 @@ bitflags! { } /// TODO -#[derive(Clone, Copy, Deserialize, PartialEq, Serialize)] +#[derive(Clone, Copy, Deserialize, Eq, PartialEq, Serialize)] pub enum Kinematics { /// TODO Mu2(Scale), @@ -420,6 +420,7 @@ impl Channel { /// /// assert_eq!(entry, channel![2, 11, 10.0; -2, 11, -10.0; 1, 11, -10.0; -1, 11, 10.0]); /// ``` + #[must_use] pub fn translate(&self, translator: &dyn Fn(i32) -> Vec<(i32, f64)>) -> Self { let mut result = Vec::new(); diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 1dec1924..4a304d65 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -155,6 +155,11 @@ pub struct Grid { impl Grid { /// Constructor. + /// + /// # Panics + /// + /// Panics when the number of PIDs in `channels` is not equal to `convolutions.len()`, or if + /// `interps` and `kinematics` have different lengths. #[must_use] pub fn new( channels: Vec, @@ -383,7 +388,7 @@ impl Grid { let mut array = Array3::zeros((mu2_grid.len(), x1_grid.len(), x2_grid.len())); - for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { + for (index @ (imu2, ix1, ix2), value) in subgrid.indexed_iter() { let x1 = x1_grid[ix1]; let x2 = x2_grid[ix2]; let mut lumi = 0.0; @@ -399,7 +404,7 @@ impl Grid { lumi *= alphas.powi(order.alphas.try_into().unwrap()); - array[[imu2, ix1, ix2]] = lumi * value; + array[<[usize; 3]>::from(index)] = lumi * value; } if order.logxir > 0 { @@ -741,8 +746,8 @@ impl Grid { || EmptySubgridV1.into(), ); - for ((i, j, k), subgrid) in self.subgrids.indexed_iter_mut() { - mem::swap(&mut new_subgrids[[i, j, k]], subgrid); + for (index, subgrid) in self.subgrids.indexed_iter_mut() { + mem::swap(&mut new_subgrids[<[usize; 3]>::from(index)], subgrid); } self.subgrids = new_subgrids; @@ -1193,6 +1198,11 @@ impl Grid { /// Returns a [`GridError::EvolutionFailure`] if either the `operator` or its `info` is /// incompatible with this `Grid`. Returns a [`GridError::Other`] if the iterator from `slices` /// return an error. + /// + /// # Panics + /// + /// Panics when the operator returned by `slices` has different dimensions than promised by the + /// corresponding [`OperatorSliceInfo`]. pub fn evolve_with_slice_iter<'a, E: Into>( &self, slices: impl IntoIterator), E>>, @@ -1316,6 +1326,11 @@ impl Grid { /// Returns a [`GridError::EvolutionFailure`] if either the `operator` or its `info` is /// incompatible with this `Grid`. Returns a [`GridError::Other`] if the iterator from `slices` /// return an error. + /// + /// # Panics + /// + /// Panics when the operators returned by `slices_a` and `slices_b` have different dimensions + /// than promised by the corresponding [`OperatorSliceInfo`]. pub fn evolve_with_slice_iter2<'a, E: Into>( &self, slices_a: impl IntoIterator), E>>, diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 75a35afc..e71b08b7 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -100,6 +100,7 @@ impl Interp { /// /// Panics if `nodes` is `0`, or if `nodes` is smaller or equal to `order` or if `order` is /// larger than some internally specified maximum. + #[must_use] pub fn new( min: f64, max: f64, @@ -150,14 +151,16 @@ impl Interp { } /// TODO + #[must_use] pub fn reweight(&self, x: f64) -> f64 { match self.reweight { ReweightMeth::ApplGridX => applgrid::reweight_x(x), - ReweightMeth::NoReweight => return 1.0, + ReweightMeth::NoReweight => 1.0, } } /// TODO + #[must_use] pub fn interpolate(&self, x: f64) -> Option<(usize, f64)> { let y = self.map_x_to_y(x); @@ -182,6 +185,7 @@ impl Interp { } /// TODO + #[must_use] pub fn node_weights(&self, fraction: f64) -> ArrayVec { (0..=self.order) .map(|i| match self.interp_meth { @@ -191,11 +195,13 @@ impl Interp { } /// TODO - pub fn order(&self) -> usize { + #[must_use] + pub const fn order(&self) -> usize { self.order } /// TODO + #[must_use] pub fn node_values(&self) -> Vec { (0..self.nodes) .map(|node| self.map_y_to_x(self.gety(node))) @@ -217,22 +223,26 @@ impl Interp { } /// TODO - pub fn nodes(&self) -> usize { + #[must_use] + pub const fn nodes(&self) -> usize { self.nodes } /// TODO + #[must_use] pub fn min(&self) -> f64 { self.map_y_to_x(self.min).min(self.map_y_to_x(self.max)) } /// TODO + #[must_use] pub fn max(&self) -> f64 { self.map_y_to_x(self.min).max(self.map_y_to_x(self.max)) } /// TODO - pub fn map(&self) -> Map { + #[must_use] + pub const fn map(&self) -> Map { self.map } } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 5ef873b2..8816b655 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -110,11 +110,8 @@ impl Subgrid for LagrangeSubgridV2 { } fn indexed_iter(&self) -> SubgridIndexedIter { - let nodes: Vec<_> = self - .interps - .iter() - .map(|interp| interp.node_values()) - .collect(); + let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); + Box::new(self.grid.indexed_iter().map(move |(index, v)| { ( (index[0], index[1], index[2]), diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 1dd6e07d..2c6d409b 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -140,6 +140,11 @@ fn ravel_multi_index(multi_index: &[usize; D], shape: &[usize]) } /// Converts a flat `index` into a `multi_index`. +/// +/// # Panics +/// +/// Panics when `index` is out of range. +#[must_use] pub fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D] { assert!(index < shape.iter().product()); let mut indices = [0; D]; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 6904c65e..8c807689 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -174,7 +174,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { Box::new( self.array .indexed_iter() - .map(|([o, b, c], v)| ((o, b, c), v)), + .map(|(index, v)| (index.into(), v)), ) } @@ -313,7 +313,7 @@ mod tests { frg: 1.0, }], x.clone(), - x.clone(), + x, ) .into(); if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid2 { diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index ab873b29..3c8cd6c9 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -73,8 +73,8 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result let x2_grid = subgrid.x2_grid().into_owned(); let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - for ((o, b, c), v) in subgrid.indexed_iter() { - array[[o, b, c]] = v; + for (index, v) in subgrid.indexed_iter() { + array[index.into()] = v; } PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() } diff --git a/pineappl_applgrid/src/lib.rs b/pineappl_applgrid/src/lib.rs index e476ec28..a4dd9966 100644 --- a/pineappl_applgrid/src/lib.rs +++ b/pineappl_applgrid/src/lib.rs @@ -13,6 +13,7 @@ use lhapdf::Pdf; use std::mem; use std::pin::Pin; +use std::ptr; use std::slice; use std::sync::{Mutex, OnceLock}; @@ -185,7 +186,7 @@ pub fn grid_convolve_with_one( grid, xfx, alphas, - (pdf as *mut Pdf).cast::(), + ptr::from_mut(pdf).cast::(), nloops, rscale, fscale, diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index d75fc8c7..72cc0147 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, bail, Result}; +use anyhow::{bail, Result}; use cxx::{let_cxx_string, UniquePtr}; use float_cmp::approx_eq; use lhapdf::Pdf; @@ -222,11 +222,11 @@ pub fn convert_into_applgrid( interps[1].order().try_into().unwrap(), match interps[1].map() { Map::ApplGridF2 => "f2", - map => panic!("export does not support {map:?}"), + map @ Map::ApplGridH0 => panic!("export does not support {map:?}"), }, match interps[0].map() { Map::ApplGridH0 => "h0", - map => panic!("export does not support {map:?}"), + map @ Map::ApplGridF2 => panic!("export does not support {map:?}"), }, grid.channels().len().try_into().unwrap(), has_pdf1 != has_pdf2, @@ -255,10 +255,9 @@ pub fn convert_into_applgrid( if discard_non_matching_scales { Ok(-1) } else { - Err(anyhow!( - "factorization scale muf2 = {} not found in APPLgrid", - fac - )) + bail!( + "factorization scale muf2 = {fac} not found in APPLgrid", + ) } }, |idx| Ok(idx.try_into().unwrap()), @@ -282,12 +281,7 @@ pub fn convert_into_applgrid( .iter() .position(|&x| approx_eq!(f64, x, x1, ulps = 128)) .map_or_else( - || { - Err(anyhow!( - "momentum fraction x1 = {} not found in APPLgrid", - x1 - )) - }, + || bail!("momentum fraction x1 = {x1} not found in APPLgrid"), |idx| Ok(idx.try_into().unwrap()), ) }) @@ -299,12 +293,7 @@ pub fn convert_into_applgrid( .iter() .position(|&x| approx_eq!(f64, x, x2, ulps = 128)) .map_or_else( - || { - Err(anyhow!( - "momentum fraction x2 = {} not found in APPLgrid", - x2 - )) - }, + || bail!("momentum fraction x2 = {x2} not found in APPLgrid"), |idx| Ok(idx.try_into().unwrap()), ) }) diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index 2ebd7c1b..b7ef7783 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -81,10 +81,10 @@ fn map_format_channel(channel: &Channel, grid: &Grid) -> String { .iter() .zip(pids) .map(|(convolution, &pid)| { - if *convolution != Convolution::None { - grid.pid_basis().to_latex_str(pid) - } else { + if *convolution == Convolution::None { "" + } else { + grid.pid_basis().to_latex_str(pid) } }) .collect::>() @@ -526,7 +526,7 @@ impl Subcommand for Opts { helpers::create_conv_funs_for_set(&self.conv_funs[0], self.conv_fun_uncert_from)?; let (set2, mut conv_funs2) = helpers::create_conv_funs_for_set(&self.conv_funs[1], self.conv_fun_uncert_from)?; - let (order, bin, channel) = self + let index @ (order, bin, channel) = self .subgrid_pull .iter() .map(|num| num.parse::().unwrap()) @@ -616,7 +616,7 @@ impl Subcommand for Opts { ) .sum_axis(Axis(0)); - let subgrid = &grid.subgrids()[[order, bin, channel]]; + let subgrid = &grid.subgrids()[<[usize; 3]>::from(index)]; //let q2 = subgrid.q2_grid(); let x1 = subgrid.x1_grid(); let x2 = subgrid.x2_grid(); From f13095d1af0ca2bcff65214d002a915c9f675a6f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 11:40:31 +0200 Subject: [PATCH 095/213] Remove `MoreMembers::upgrade` method --- pineappl/src/grid.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 4a304d65..623f9a95 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -105,14 +105,6 @@ pub(crate) enum MoreMembers { V3(Mmv3), } -impl MoreMembers { - fn upgrade(&mut self) { - match self { - Self::V3(_) => {} - } - } -} - bitflags! { /// Bitflags for optimizing a [`Grid`]. See [`Grid::optimize_using`]. #[derive(Clone, Copy)] @@ -849,8 +841,6 @@ impl Grid { }); } - self.more_members.upgrade(); - match &mut self.more_members { MoreMembers::V3(mmv3) => mmv3.remapper = Some(remapper), } @@ -1091,9 +1081,7 @@ impl Grid { } /// Upgrades the internal data structures to their latest versions. - pub fn upgrade(&mut self) { - self.more_members.upgrade(); - } + pub fn upgrade(&mut self) {} /// Return the metadata of this grid. #[must_use] From 0dcd1186b6b654c5e3509fcddfe0b850cd0d2694 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 13:04:15 +0200 Subject: [PATCH 096/213] Move members from `MoreMembers` into `Grid` --- pineappl/src/grid.rs | 46 +++++++++++++++----------------------------- pineappl/src/v0.rs | 16 ++++++--------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 623f9a95..785d3a4f 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -71,19 +71,7 @@ pub enum GridError { } #[derive(Clone, Deserialize, Serialize)] -pub(crate) struct Mmv3 { - pub(crate) remapper: Option, - pub(crate) subgrid_template: SubgridEnum, -} - -impl Mmv3 { - const fn new(subgrid_template: SubgridEnum) -> Self { - Self { - remapper: None, - subgrid_template, - } - } -} +pub(crate) struct Mmv4; fn default_metadata() -> BTreeMap { iter::once(( @@ -98,11 +86,9 @@ fn default_metadata() -> BTreeMap { .collect() } -// ALLOW: fixing the warning will break the file format -#[allow(clippy::large_enum_variant)] #[derive(Clone, Deserialize, Serialize)] pub(crate) enum MoreMembers { - V3(Mmv3), + V4(Mmv4), } bitflags! { @@ -143,6 +129,7 @@ pub struct Grid { pub(crate) more_members: MoreMembers, pub(crate) kinematics: Vec, pub(crate) interps: Vec, + pub(crate) remapper: Option, } impl Grid { @@ -186,13 +173,14 @@ impl Grid { orders, bin_limits: BinLimits::new(bin_limits), metadata: default_metadata(), - more_members: MoreMembers::V3(Mmv3::new(LagrangeSubgridV2::new(&interps).into())), + more_members: MoreMembers::V4(Mmv4), convolutions, // TODO: make this a new parameter pid_basis: PidBasis::Pdg, channels, interps, kinematics, + remapper: None, } } @@ -213,8 +201,8 @@ impl Grid { interps: Vec, subgrid_type: &str, ) -> Result { - let subgrid_template: SubgridEnum = match subgrid_type { - "LagrangeSubgrid" | "LagrangeSubgridV2" => LagrangeSubgridV2::new(&interps).into(), + match subgrid_type { + "LagrangeSubgrid" | "LagrangeSubgridV2" => {} _ => return Err(GridError::UnknownSubgridType(subgrid_type.to_owned())), }; @@ -229,9 +217,10 @@ impl Grid { convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], pid_basis: PidBasis::Pdg, channels, - more_members: MoreMembers::V3(Mmv3::new(subgrid_template)), + more_members: MoreMembers::V4(Mmv4), interps, kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + remapper: None, }) } @@ -427,8 +416,7 @@ impl Grid { if let Some(bin) = self.bin_limits.index(observable) { let subgrid = &mut self.subgrids[[order, bin, channel]]; if let SubgridEnum::EmptySubgridV1(_) = subgrid { - let MoreMembers::V3(mmv3) = &self.more_members; - *subgrid = mmv3.subgrid_template.clone_empty(); + *subgrid = LagrangeSubgridV2::new(&self.interps).into(); } subgrid.fill(ntuple, weight); @@ -841,9 +829,7 @@ impl Grid { }); } - match &mut self.more_members { - MoreMembers::V3(mmv3) => mmv3.remapper = Some(remapper), - } + self.remapper = Some(remapper); Ok(()) } @@ -851,15 +837,11 @@ impl Grid { /// Return the currently set remapper, if there is any. #[must_use] pub const fn remapper(&self) -> Option<&BinRemapper> { - match &self.more_members { - MoreMembers::V3(mmv3) => mmv3.remapper.as_ref(), - } + self.remapper.as_ref() } fn remapper_mut(&mut self) -> Option<&mut BinRemapper> { - match &mut self.more_members { - MoreMembers::V3(mmv3) => mmv3.remapper.as_mut(), - } + self.remapper.as_mut() } /// Returns all information about the bins in this grid. @@ -1271,6 +1253,7 @@ impl Grid { pid_basis: info.pid_basis, more_members: self.more_members.clone(), kinematics: self.kinematics.clone(), + remapper: self.remapper.clone(), }; if let Some(lhs) = &mut lhs { @@ -1434,6 +1417,7 @@ impl Grid { pid_basis: infos[0].pid_basis, more_members: self.more_members.clone(), kinematics: self.kinematics.clone(), + remapper: self.remapper.clone(), }; assert_eq!(infos[0].pid_basis, infos[1].pid_basis); diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 3c8cd6c9..b94e566f 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -2,7 +2,7 @@ use super::bin::{BinLimits, BinRemapper}; use super::boc::{Channel, Kinematics, Order}; use super::convolutions::Convolution; use super::empty_subgrid::EmptySubgridV1; -use super::grid::{Grid, GridError, Mmv3, MoreMembers}; +use super::grid::{Grid, GridError, Mmv4, MoreMembers}; use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; @@ -128,15 +128,11 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result _ => panic!("unknown PID basis '{lumi_id_types}'"), } }), - // TODO: make these proper members - more_members: MoreMembers::V3(Mmv3 { - remapper: grid.remapper().map(|r| { - // UNWRAP: if the old grid could be constructed with the given normalizations - // and limits we should be able to do the same without error - BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() - }), - // TODO: remove this member - subgrid_template: EmptySubgridV1.into(), + more_members: MoreMembers::V4(Mmv4), + remapper: grid.remapper().map(|r| { + // UNWRAP: if the old grid could be constructed with the given normalizations + // and limits we should be able to do the same without error + BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() }), kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], interps: default_interps(), From c677a63b5aeaf6bd77ad6094e49bfda1b975c949 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 15:32:59 +0200 Subject: [PATCH 097/213] Remove `Grid::with_subgrid_type` constructor --- pineappl/src/grid.rs | 49 ------------------ pineappl/tests/drell_yan_lo.rs | 92 ++++++++++++-------------------- pineappl_capi/src/lib.rs | 95 ++++++++++++++-------------------- 3 files changed, 74 insertions(+), 162 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 785d3a4f..e8b16d0b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -184,46 +184,6 @@ impl Grid { } } - // TODO: get rid of this constructor - - /// Constructor. This function can be used like `new`, but the additional parameter - /// `subgrid_type` selects the underlying `Subgrid` type. Supported values are: - /// - `LagrangeSubgrid` - /// - `LagrangeSubgridV2` - /// - /// # Errors - /// - /// If `subgrid_type` is none of the values listed above, an error is returned. - pub fn with_subgrid_type( - channels: Vec, - orders: Vec, - bin_limits: Vec, - interps: Vec, - subgrid_type: &str, - ) -> Result { - match subgrid_type { - "LagrangeSubgrid" | "LagrangeSubgridV2" => {} - _ => return Err(GridError::UnknownSubgridType(subgrid_type.to_owned())), - }; - - Ok(Self { - subgrids: Array3::from_shape_simple_fn( - (orders.len(), bin_limits.len() - 1, channels.len()), - || EmptySubgridV1.into(), - ), - orders, - bin_limits: BinLimits::new(bin_limits), - metadata: default_metadata(), - convolutions: vec![Convolution::UnpolPDF(2212); channels[0].entry()[0].0.len()], - pid_basis: PidBasis::Pdg, - channels, - more_members: MoreMembers::V4(Mmv4), - interps, - kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], - remapper: None, - }) - } - /// Return the convention by which the channels' PIDs are encoded. #[must_use] pub const fn pid_basis(&self) -> &PidBasis { @@ -1690,15 +1650,6 @@ mod tests { ); } - #[test] - fn grid_with_subgrid_type() { - let subgrid_type = String::from("Idontexist"); - let result = - Grid::with_subgrid_type(vec![], vec![], vec![], v0::default_interps(), &subgrid_type); - - matches!(result, Err(GridError::UnknownSubgridType(x)) if x == subgrid_type); - } - #[test] fn grid_merge_empty_subgrids() { let mut grid = Grid::new( diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 413416ee..e67a047f 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -3,9 +3,9 @@ use float_cmp::assert_approx_eq; use lhapdf::Pdf; use num_complex::Complex; use pineappl::bin::BinRemapper; -use pineappl::boc::Order; +use pineappl::boc::{Kinematics, Order}; use pineappl::channel; -use pineappl::convolutions::LumiCache; +use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::subgrid::{Subgrid, SubgridEnum}; @@ -117,7 +117,6 @@ fn hadronic_pspgen(rng: &mut impl Rng, mmin: f64, mmax: f64) -> Psp2to2 { fn fill_drell_yan_lo_grid( rng: &mut impl Rng, calls: u32, - subgrid_type: &str, dynamic: bool, reweight: bool, ) -> Result { @@ -163,13 +162,18 @@ fn fill_drell_yan_lo_grid( // we bin in rapidity from 0 to 2.4 in steps of 0.1 let bin_limits: Vec<_> = (0..=24).map(|x: u32| f64::from(x) / 10.0).collect(); + // the grid represents data with two unpolarized proton PDFs + let convolutions = vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)]; + let reweight = if reweight { ReweightMeth::ApplGridX } else { ReweightMeth::NoReweight }; + // define how `Grid::fill` interpolates let interps = vec![ + // 1st dimension interpolation parameters Interp::new( 1e2, 1e6, @@ -179,6 +183,7 @@ fn fill_drell_yan_lo_grid( Map::ApplGridH0, InterpMeth::Lagrange, ), + // 2nd dimension interpolation parameters Interp::new( 2e-7, 1.0, @@ -188,6 +193,7 @@ fn fill_drell_yan_lo_grid( Map::ApplGridF2, InterpMeth::Lagrange, ), + // 3rd dimension interpolation parameters Interp::new( 2e-7, 1.0, @@ -199,8 +205,24 @@ fn fill_drell_yan_lo_grid( ), ]; + let kinematics = vec![ + // 1st dimension is factorization and at the same time also the renormalization scale + Kinematics::MU2_RF, + // 2nd dimension is the parton momentum fraction of the first convolution + Kinematics::X1, + // 3rd dimension is the parton momentum fraction of the second convolution + Kinematics::X2, + ]; + // create the PineAPPL grid - let mut grid = Grid::with_subgrid_type(channels, orders, bin_limits, interps, subgrid_type)?; + let mut grid = Grid::new( + channels, + orders, + bin_limits, + convolutions, + interps, + kinematics, + ); // in GeV^2 pbarn let hbarc2 = 3.893793721e8; @@ -276,7 +298,6 @@ fn fill_drell_yan_lo_grid( } fn perform_grid_tests( - subgrid_type: &str, dynamic: bool, reference: &[f64], reference_after_ssd: &[f64], @@ -284,15 +305,11 @@ fn perform_grid_tests( reweight: bool, ) -> Result<()> { let mut rng = Pcg64::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7ac28fa16a64abf96); - let mut grid = fill_drell_yan_lo_grid(&mut rng, INT_STATS, subgrid_type, dynamic, reweight)?; + let mut grid = fill_drell_yan_lo_grid(&mut rng, INT_STATS, dynamic, reweight)?; // TEST 1: `merge` and `scale` grid.merge(fill_drell_yan_lo_grid( - &mut rng, - INT_STATS, - subgrid_type, - dynamic, - reweight, + &mut rng, INT_STATS, dynamic, reweight, )?)?; grid.scale(0.5); @@ -467,9 +484,9 @@ fn perform_grid_tests( Ok(()) } -fn generate_grid(subgrid_type: &str, dynamic: bool, reweight: bool) -> Result { +fn generate_grid(dynamic: bool, reweight: bool) -> Result { let mut rng = Pcg64::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7ac28fa16a64abf96); - fill_drell_yan_lo_grid(&mut rng, 500_000, subgrid_type, dynamic, reweight) + fill_drell_yan_lo_grid(&mut rng, 500_000, dynamic, reweight) } // number is small enough for the tests to be quick and meaningful @@ -586,9 +603,8 @@ const DYNAMIC_REFERENCE_NO_REWEIGHT: [f64; 24] = [ ]; #[test] -fn drell_yan_lagrange_static() -> Result<()> { +fn drell_yan_static() -> Result<()> { perform_grid_tests( - "LagrangeSubgrid", false, &STATIC_REFERENCE, &STATIC_REFERENCE_AFTER_SSD, @@ -605,47 +621,8 @@ fn drell_yan_lagrange_static() -> Result<()> { } #[test] -fn drell_yan_lagrange_v2_static() -> Result<()> { - perform_grid_tests( - "LagrangeSubgridV2", - false, - &STATIC_REFERENCE, - &STATIC_REFERENCE_AFTER_SSD, - &[ - 0.030521584007828916, - 0.02108918668378717, - 0.014375068581090129, - 0.009699159574043398, - 0.006496206194633799, - 0.004328500638820811, - ], - true, - ) -} - -#[test] -fn drell_yan_lagrange_dynamic() -> Result<()> { - perform_grid_tests( - "LagrangeSubgrid", - true, - &DYNAMIC_REFERENCE, - &DYNAMIC_REFERENCE, - &[ - 0.030521584007828916, - 0.02108918668378717, - 0.014375068581090129, - 0.009699159574043398, - 0.006496206194633799, - 0.004328500638820811, - ], - true, - ) -} - -#[test] -fn drell_yan_lagrange_v2_dynamic() -> Result<()> { +fn drell_yan_dynamic() -> Result<()> { perform_grid_tests( - "LagrangeSubgridV2", true, &DYNAMIC_REFERENCE, &DYNAMIC_REFERENCE, @@ -662,9 +639,8 @@ fn drell_yan_lagrange_v2_dynamic() -> Result<()> { } #[test] -fn drell_yan_lagrange_v2_dynamic_no_reweight() -> Result<()> { +fn drell_yan_dynamic_no_reweight() -> Result<()> { perform_grid_tests( - "LagrangeSubgridV2", true, &DYNAMIC_REFERENCE_NO_REWEIGHT, &DYNAMIC_REFERENCE_NO_REWEIGHT, @@ -682,7 +658,7 @@ fn drell_yan_lagrange_v2_dynamic_no_reweight() -> Result<()> { #[test] fn grid_optimize() -> Result<()> { - let mut grid = generate_grid("LagrangeSubgridV2", false, false)?; + let mut grid = generate_grid(false, false)?; assert_eq!(grid.orders().len(), 3); assert_eq!(grid.channels().len(), 5); diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 95acb9c3..761ef9d1 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -57,7 +57,7 @@ use itertools::izip; use pineappl::bin::BinRemapper; -use pineappl::boc::{Channel, Order}; +use pineappl::boc::{Channel, Kinematics, Order}; use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; @@ -71,8 +71,7 @@ use std::slice; // TODO: make sure no `panic` calls leave functions marked as `extern "C"` -fn grid_params(key_vals: Option<&KeyVal>) -> (String, Vec) { - let mut subgrid_type = "LagrangeSubgrid".to_owned(); +fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec { let mut q2_min = 1e2; let mut q2_max = 1e8; let mut q2_nodes = 40; @@ -191,44 +190,37 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, Vec) { if let Some(value) = keyval.ints.get("x2_order") { x2_order = usize::try_from(*value).unwrap(); } - - if let Some(value) = keyval.strings.get("subgrid_type") { - value.to_str().unwrap().clone_into(&mut subgrid_type); - } } - ( - subgrid_type, - vec![ - Interp::new( - q2_min, - q2_max, - q2_nodes, - q2_order, - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - x1_min, - x1_max, - x1_nodes, - x1_order, - reweight, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( - x2_min, - x2_max, - x2_nodes, - x2_order, - reweight, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - ], - ) + vec![ + Interp::new( + q2_min, + q2_max, + q2_nodes, + q2_order, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + x1_min, + x1_max, + x1_nodes, + x1_order, + reweight, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + x2_min, + x2_max, + x2_nodes, + x2_order, + reweight, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ] } /// Type for defining a luminosity function. @@ -737,7 +729,7 @@ pub unsafe extern "C" fn pineappl_grid_new( .collect(); let key_vals = unsafe { key_vals.as_ref() }; - let (subgrid_type, interps) = grid_params(key_vals); + let interps = grid_interpolation_params(key_vals); let lumi = unsafe { &*lumi }; @@ -753,21 +745,14 @@ pub unsafe extern "C" fn pineappl_grid_new( } } - let mut grid = Box::new( - Grid::with_subgrid_type( - lumi.0.clone(), - orders, - unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), - interps, - &subgrid_type, - ) - .unwrap(), - ); - - // TODO: set the convolutions using a new constructor - grid.convolutions_mut().clone_from_slice(&convolutions); - - grid + Box::new(Grid::new( + lumi.0.clone(), + orders, + unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), + convolutions, + interps, + vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + )) } /// Read a `PineAPPL` grid from a file with name `filename`. From 46fe945d6abae7b6039042dcbf576bf9d42bf42d Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 16:30:48 +0200 Subject: [PATCH 098/213] Change ordering of `ntuples` in `Grid::fill` --- CHANGELOG.md | 2 ++ pineappl/src/grid.rs | 18 ++++++++++-------- pineappl/src/lagrange_subgrid.rs | 11 ++++------- pineappl/src/packed_subgrid.rs | 2 +- pineappl/src/subgrid.rs | 5 ++--- pineappl/tests/drell_yan_lo.rs | 10 +++++----- pineappl_capi/src/lib.rs | 6 +++--- pineappl_py/src/grid.rs | 4 ++-- 8 files changed, 29 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 168bc4a7..b95acd95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 with absolute PDF predictions. However, this feature can be enabled with via a new array added at the start of the script - raised MSRV to 1.80.1 +- changed the order of elements in `Grid::fill` of the parameter `ntuple` to + reflect the ordering of `kinematics` given to `Grid::new` ### Removed diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index e8b16d0b..ddd15e2b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -360,7 +360,9 @@ impl Grid { array } - /// Fills the grid with an ntuple for the given `order`, `observable`, and `channel`. + /// Fills the grid with an ntuple for the given `order`, `observable`, and `channel`. The + /// parameter `ntuple` must contain the variables specified by the `kinematics` parameter in + /// the constructor [`Grid::new`] in the same order. /// /// # Panics /// @@ -1723,10 +1725,10 @@ mod tests { vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); - other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 1.0); - other.fill(0, 0.1, 1, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); - other.fill(1, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 1.0); - other.fill(1, 0.1, 1, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); + other.fill(0, 0.1, 0, &[90.0_f64.powi(2), 0.1, 0.2], 1.0); + other.fill(0, 0.1, 1, &[90.0_f64.powi(2), 0.1, 0.2], 2.0); + other.fill(1, 0.1, 0, &[90.0_f64.powi(2), 0.1, 0.2], 1.0); + other.fill(1, 0.1, 1, &[90.0_f64.powi(2), 0.1, 0.2], 2.0); // merge with four non-empty subgrids grid.merge(other).unwrap(); @@ -1764,7 +1766,7 @@ mod tests { ); // fill the photon-photon entry - other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 3.0); + other.fill(0, 0.1, 0, &[90.0_f64.powi(2), 0.1, 0.2], 3.0); grid.merge(other).unwrap(); @@ -1804,8 +1806,8 @@ mod tests { vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); - other.fill(0, 0.1, 0, &[0.1, 0.2, 90.0_f64.powi(2)], 2.0); - other.fill(0, 0.1, 1, &[0.1, 0.2, 90.0_f64.powi(2)], 3.0); + other.fill(0, 0.1, 0, &[90.0_f64.powi(2), 0.1, 0.2], 2.0); + other.fill(0, 0.1, 1, &[90.0_f64.powi(2), 0.1, 0.2], 3.0); grid.merge(other).unwrap(); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 8816b655..ffe8ecc1 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -31,9 +31,6 @@ impl LagrangeSubgridV2 { impl Subgrid for LagrangeSubgridV2 { fn fill(&mut self, ntuple: &[f64], weight: f64) { - // TODO: change the order of ntuple higher up in the code - let mut ntuple = ntuple.to_vec(); - ntuple.rotate_right(1); if interpolation::interpolate(&self.interps, &ntuple, weight, &mut self.grid) { let q2 = ntuple[0]; if self.static_q2 == 0.0 { @@ -153,7 +150,7 @@ mod tests { fn fill_zero() { let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); - subgrid.fill(&[0.5, 0.5, 1000.0], 0.0); + subgrid.fill(&[1000.0, 0.5, 0.5], 0.0); assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); @@ -173,7 +170,7 @@ mod tests { fn fill_outside_range() { let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); - subgrid.fill(&[1e-10, 0.5, 1000.0], 0.0); + subgrid.fill(&[1000.0, 1e-10, 0.5], 0.0); assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); @@ -193,7 +190,7 @@ mod tests { fn fill() { let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); - subgrid.fill(&[0.5, 0.5, 1000.0], 1.0); + subgrid.fill(&[1000.0, 0.5, 0.5], 1.0); assert!(!subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 4 * 4 * 4); @@ -216,7 +213,7 @@ mod tests { } ); - subgrid.fill(&[0.5, 0.5, 1000000.0], 1.0); + subgrid.fill(&[1000000.0, 0.5, 0.5], 1.0); assert!(!subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 2 * 4 * 4 * 4); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 8c807689..63c6c440 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -257,7 +257,7 @@ mod tests { Vec::new(), Vec::new(), ); - subgrid.fill(&[0.0; 4], 0.0); + subgrid.fill(&[0.0; 3], 0.0); } #[test] diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 4ad28995..150943ff 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -68,9 +68,8 @@ pub trait Subgrid { /// return an empty slice. fn x2_grid(&self) -> Cow<[f64]>; - /// Fills the subgrid with `weight` for the parton momentum fractions `x1` and `x2`, and the - /// scale `q2`. Filling is currently only support where both renormalization and factorization - /// scale have the same value. + /// Fills the subgrid with `weight` and the kinematic information in `ntuple` according to the + /// same ordering given by `kinematics` in [`Grid::new`] that was used to create the grid. fn fill(&mut self, ntuple: &[f64], weight: f64); /// Returns true if `fill` was never called for this grid. diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index e67a047f..093a92f4 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -263,35 +263,35 @@ fn fill_drell_yan_lo_grid( let pto = 0; let channel = 0; - grid.fill(pto, yll.abs(), channel, &[x1, x2, q2], weight); + grid.fill(pto, yll.abs(), channel, &[q2, x1, x2], weight); // LO up-antiup-type channel let weight = jacobian * int_quark(s, t, u, 2.0 / 3.0, 0.5); let pto = 0; let channel = 1; - grid.fill(pto, yll.abs(), channel, &[x1, x2, q2], weight); + grid.fill(pto, yll.abs(), channel, &[q2, x1, x2], weight); // LO antiup-up-type channel - swap (x1 <-> x2) and (t <-> u) let weight = jacobian * int_quark(s, u, t, 2.0 / 3.0, 0.5); let pto = 0; let channel = 2; - grid.fill(pto, yll.abs(), channel, &[x2, x1, q2], weight); + grid.fill(pto, yll.abs(), channel, &[q2, x2, x1], weight); // LO down-antidown-type channel let weight = jacobian * int_quark(s, t, u, -1.0 / 3.0, -0.5); let pto = 0; let channel = 3; - grid.fill(pto, yll.abs(), channel, &[x1, x2, q2], weight); + grid.fill(pto, yll.abs(), channel, &[q2, x1, x2], weight); // LO antidown-down-type channel - swap (x1 <-> x2) and (t <-> u) let weight = jacobian * int_quark(s, u, t, -1.0 / 3.0, -0.5); let pto = 0; let channel = 4; - grid.fill(pto, yll.abs(), channel, &[x2, x1, q2], weight); + grid.fill(pto, yll.abs(), channel, &[q2, x2, x1], weight); } Ok(grid) diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 761ef9d1..a709aed0 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -567,7 +567,7 @@ pub unsafe extern "C" fn pineappl_grid_fill( ) { let grid = unsafe { &mut *grid }; - grid.fill(order, observable, lumi, &[x1, x2, q2], weight); + grid.fill(order, observable, lumi, &[q2, x1, x2], weight); } /// Fill `grid` for the given momentum fractions `x1` and `x2`, at the scale `q2` for the given @@ -592,7 +592,7 @@ pub unsafe extern "C" fn pineappl_grid_fill_all( let weights = unsafe { slice::from_raw_parts(weights, grid.channels().len()) }; for (channel, &weight) in weights.iter().enumerate() { - grid.fill(order, observable, channel, &[x1, x2, q2], weight); + grid.fill(order, observable, channel, &[q2, x1, x2], weight); } } @@ -627,7 +627,7 @@ pub unsafe extern "C" fn pineappl_grid_fill_array( for (&x1, &x2, &q2, &order, &observable, &lumi, &weight) in izip!(x1, x2, q2, orders, observables, lumis, weights) { - grid.fill(order, observable, lumi, &[x1, x2, q2], weight); + grid.fill(order, observable, lumi, &[q2, x1, x2], weight); } } diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 6eda73c1..8ac7328f 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -90,7 +90,7 @@ impl PyGrid { order, observable, channel, - &Ntuple:: { x1, x2, q2, weight }, + &Ntuple:: { q2, x1, x2, weight }, ); } @@ -135,7 +135,7 @@ impl PyGrid { order, observable, channel, - &Ntuple:: { x1, x2, q2, weight }, + &Ntuple:: { q2, x1, x2, weight }, ); } } From fd68be744dd6cbb1535a9ba1f55c39c7314c8f0c Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 16:44:06 +0200 Subject: [PATCH 099/213] Add missing changelog item --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b95acd95..2fc31781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Python API: removed `pineappl.grid.Grid.create()` and `pineappl.fk_table.FkTable.from_grid()` methods; use the constructors of the respective class instead +- removed the constructor `Grid::with_subgrid_type` ### Fixed From bcae6a58cd8227f182a3d193fb3c6030d1f092d3 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 16:46:15 +0200 Subject: [PATCH 100/213] Add the interpolation as a parameter to `Subgrid::fill` --- pineappl/src/empty_subgrid.rs | 6 ++++-- pineappl/src/grid.rs | 2 +- pineappl/src/lagrange_subgrid.rs | 26 +++++++++++++++++--------- pineappl/src/packed_subgrid.rs | 6 ++++-- pineappl/src/subgrid.rs | 8 +++++--- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 7a543d25..79ad25bc 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -1,5 +1,6 @@ //! TODO +use super::interpolation::Interp; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -10,7 +11,7 @@ use std::iter; pub struct EmptySubgridV1; impl Subgrid for EmptySubgridV1 { - fn fill(&mut self, _: &[f64], _: f64) { + fn fill(&mut self, _: &[Interp], _: &[f64], _: f64) { panic!("EmptySubgridV1 doesn't support the fill operation"); } @@ -67,6 +68,7 @@ impl Subgrid for EmptySubgridV1 { #[cfg(test)] mod tests { use super::*; + use crate::v0; #[test] fn create_empty() { @@ -93,7 +95,7 @@ mod tests { #[should_panic(expected = "EmptySubgridV1 doesn't support the fill operation")] fn fill() { let mut subgrid = EmptySubgridV1; - subgrid.fill(&[0.0; 4], 0.0); + subgrid.fill(&v0::default_interps(), &[0.0; 3], 0.0); } #[test] diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index ddd15e2b..69e82e63 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -381,7 +381,7 @@ impl Grid { *subgrid = LagrangeSubgridV2::new(&self.interps).into(); } - subgrid.fill(ntuple, weight); + subgrid.fill(&self.interps, ntuple, weight); } } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index ffe8ecc1..0beda0b9 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -30,8 +30,13 @@ impl LagrangeSubgridV2 { } impl Subgrid for LagrangeSubgridV2 { - fn fill(&mut self, ntuple: &[f64], weight: f64) { - if interpolation::interpolate(&self.interps, &ntuple, weight, &mut self.grid) { + fn fill(&mut self, interps: &[Interp], ntuple: &[f64], weight: f64) { + debug_assert_eq!(interps.len(), ntuple.len()); + + // TODO: lift the restriction to exactly three kinematics variables + debug_assert_eq!(interps.len(), 3); + + if interpolation::interpolate(interps, &ntuple, weight, &mut self.grid) { let q2 = ntuple[0]; if self.static_q2 == 0.0 { self.static_q2 = q2; @@ -148,9 +153,10 @@ mod tests { #[test] fn fill_zero() { - let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); + let interps = v0::default_interps(); + let mut subgrid = LagrangeSubgridV2::new(&interps); - subgrid.fill(&[1000.0, 0.5, 0.5], 0.0); + subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 0.0); assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); @@ -168,9 +174,10 @@ mod tests { #[test] fn fill_outside_range() { - let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); + let interps = v0::default_interps(); + let mut subgrid = LagrangeSubgridV2::new(&interps); - subgrid.fill(&[1000.0, 1e-10, 0.5], 0.0); + subgrid.fill(&interps, &[1000.0, 1e-10, 0.5], 0.0); assert!(subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 0); @@ -188,9 +195,10 @@ mod tests { #[test] fn fill() { - let mut subgrid = LagrangeSubgridV2::new(&v0::default_interps()); + let interps = v0::default_interps(); + let mut subgrid = LagrangeSubgridV2::new(&interps); - subgrid.fill(&[1000.0, 0.5, 0.5], 1.0); + subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 1.0); assert!(!subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 4 * 4 * 4); @@ -213,7 +221,7 @@ mod tests { } ); - subgrid.fill(&[1000000.0, 0.5, 0.5], 1.0); + subgrid.fill(&interps, &[1000000.0, 0.5, 0.5], 1.0); assert!(!subgrid.is_empty()); assert_eq!(subgrid.indexed_iter().count(), 2 * 4 * 4 * 4); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 63c6c440..b9d7790f 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -1,5 +1,6 @@ //! TODO +use super::interpolation::Interp; use super::packed_array::PackedArray; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; @@ -39,7 +40,7 @@ impl PackedQ1X2SubgridV1 { } impl Subgrid for PackedQ1X2SubgridV1 { - fn fill(&mut self, _: &[f64], _: f64) { + fn fill(&mut self, _: &[Interp], _: &[f64], _: f64) { panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); } @@ -247,6 +248,7 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { #[cfg(test)] mod tests { use super::*; + use crate::v0; #[test] #[should_panic(expected = "PackedQ1X2SubgridV1 doesn't support the fill operation")] @@ -257,7 +259,7 @@ mod tests { Vec::new(), Vec::new(), ); - subgrid.fill(&[0.0; 3], 0.0); + subgrid.fill(&v0::default_interps(), &[0.0; 3], 0.0); } #[test] diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 150943ff..d05ea332 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -5,6 +5,7 @@ use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; // use ndarray::Array3; +use super::interpolation::Interp; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -68,9 +69,10 @@ pub trait Subgrid { /// return an empty slice. fn x2_grid(&self) -> Cow<[f64]>; - /// Fills the subgrid with `weight` and the kinematic information in `ntuple` according to the - /// same ordering given by `kinematics` in [`Grid::new`] that was used to create the grid. - fn fill(&mut self, ntuple: &[f64], weight: f64); + /// Fill the subgrid with `weight` that is being interpolated with `interps` using the + /// kinematic information in `ntuple`. The parameter `ntuple` assumes the same ordering given + /// by `kinematics` in [`Grid::new`] that was used to create the grid. + fn fill(&mut self, interps: &[Interp], ntuple: &[f64], weight: f64); /// Returns true if `fill` was never called for this grid. fn is_empty(&self) -> bool; From 9aa79eac27dc693ff7ce810b242823b85d39e592 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 17:20:44 +0200 Subject: [PATCH 101/213] Make `pid_basis` an argument of `Grid::new` --- pineappl/src/grid.rs | 14 ++++++++++++-- pineappl/tests/drell_yan_lo.rs | 3 +++ pineappl_capi/src/lib.rs | 2 ++ pineappl_cli/src/import/applgrid.rs | 2 ++ pineappl_cli/src/import/fastnlo.rs | 3 +++ pineappl_cli/src/import/fktable.rs | 8 +++----- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 69e82e63..3caaac2b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -141,6 +141,7 @@ impl Grid { /// `interps` and `kinematics` have different lengths. #[must_use] pub fn new( + pid_basis: PidBasis, channels: Vec, orders: Vec, bin_limits: Vec, @@ -175,8 +176,7 @@ impl Grid { metadata: default_metadata(), more_members: MoreMembers::V4(Mmv4), convolutions, - // TODO: make this a new parameter - pid_basis: PidBasis::Pdg, + pid_basis, channels, interps, kinematics, @@ -1643,6 +1643,7 @@ mod tests { let channel = vec![(vec![1, -1, 1], 1.0), (vec![2, -2, 2], 1.0)]; let _ = Grid::new( + PidBasis::Pdg, vec![Channel::new(channel)], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 1.0], @@ -1655,6 +1656,7 @@ mod tests { #[test] fn grid_merge_empty_subgrids() { let mut grid = Grid::new( + PidBasis::Pdg, vec![ channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], @@ -1671,6 +1673,7 @@ mod tests { assert_eq!(grid.orders().len(), 1); let other = Grid::new( + PidBasis::Pdg, vec![ // differently ordered than `grid` channel![1, 1, 1.0; 3, 3, 1.0], @@ -1694,6 +1697,7 @@ mod tests { #[test] fn grid_merge_orders() { let mut grid = Grid::new( + PidBasis::Pdg, vec![ channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], @@ -1710,6 +1714,7 @@ mod tests { assert_eq!(grid.orders().len(), 1); let mut other = Grid::new( + PidBasis::Pdg, vec![ channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], @@ -1741,6 +1746,7 @@ mod tests { #[test] fn grid_merge_channels_entries() { let mut grid = Grid::new( + PidBasis::Pdg, vec![ channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], @@ -1757,6 +1763,7 @@ mod tests { assert_eq!(grid.orders().len(), 1); let mut other = Grid::new( + PidBasis::Pdg, vec![channel![22, 22, 1.0], channel![2, 2, 1.0; 4, 4, 1.0]], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], @@ -1778,6 +1785,7 @@ mod tests { #[test] fn grid_merge_bins() { let mut grid = Grid::new( + PidBasis::Pdg, vec![ channel![2, 2, 1.0; 4, 4, 1.0], channel![1, 1, 1.0; 3, 3, 1.0], @@ -1794,6 +1802,7 @@ mod tests { assert_eq!(grid.orders().len(), 1); let mut other = Grid::new( + PidBasis::Pdg, vec![ // channels are differently sorted channel![1, 1, 1.0; 3, 3, 1.0], @@ -1821,6 +1830,7 @@ mod tests { #[test] fn grid_convolutions() { let mut grid = Grid::new( + PidBasis::Pdg, vec![channel![21, 21, 1.0]], vec![Order { alphas: 0, diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 093a92f4..6ebb2f82 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -8,6 +8,7 @@ use pineappl::channel; use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; +use pineappl::pids::PidBasis; use pineappl::subgrid::{Subgrid, SubgridEnum}; use rand::Rng; use rand_pcg::Pcg64; @@ -216,6 +217,8 @@ fn fill_drell_yan_lo_grid( // create the PineAPPL grid let mut grid = Grid::new( + // the integers in the channel definition are PDG Monte Carlo IDs + PidBasis::Pdg, channels, orders, bin_limits, diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index a709aed0..c6bba9b6 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -61,6 +61,7 @@ use pineappl::boc::{Channel, Kinematics, Order}; use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; +use pineappl::pids::PidBasis; use std::collections::HashMap; use std::ffi::{CStr, CString}; use std::fs::File; @@ -746,6 +747,7 @@ pub unsafe extern "C" fn pineappl_grid_new( } Box::new(Grid::new( + PidBasis::Pdg, lumi.0.clone(), orders, unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index e5b97c93..dc199ff4 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -6,6 +6,7 @@ use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::pids::PidBasis; use pineappl::subgrid::Mu2; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; @@ -134,6 +135,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let channels = reconstruct_channels(&grid, i.try_into().unwrap(), dis_pid); let lumis_len = channels.len(); let mut pgrid = Grid::new( + PidBasis::Pdg, channels, vec![order], bin_limits.clone(), diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 09b7b5d4..fb90968f 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -8,6 +8,7 @@ use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::pids::PidBasis; use pineappl::subgrid::Mu2; use pineappl_fastnlo::ffi::{ self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF, @@ -115,6 +116,7 @@ fn convert_coeff_add_fix( .collect(); let mut grid = Grid::new( + PidBasis::Pdg, reconstruct_channels(table_as_add_base, comb, dis_pid), vec![Order { alphas: table_as_add_base.GetNpow().try_into().unwrap(), @@ -300,6 +302,7 @@ fn convert_coeff_add_flex( .collect(); let mut grid = Grid::new( + PidBasis::Pdg, reconstruct_channels(table_as_add_base, comb, dis_pid), orders, (0..=bins) diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index f7d34dac..d29dfd22 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -78,7 +78,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { nx2 = if hadronic { nx1 } else { 1 }; - // FK tables are always in the flavor basis + // TODO: are FK tables always in the evolution basis? let basis = [ 22, 100, 21, 200, 203, 208, 215, 224, 235, 103, 108, 115, 124, 135, ]; @@ -99,7 +99,8 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { }; // construct `Grid` - let mut fktable = Grid::new( + let fktable = Grid::new( + PidBasis::Evol, lumis, vec![Order::new(0, 0, 0, 0, 0)], (0..=ndata).map(Into::into).collect(), @@ -146,9 +147,6 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], ); - // explicitly set the evolution basis - *fktable.pid_basis_mut() = PidBasis::Evol; - grid = Some(fktable); arrays = iter::repeat(PackedArray::new([1, nx1, nx2])) From 29f48d0d62f668713fa40ceb7504957121b7b69d Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 11 Sep 2024 17:56:17 +0200 Subject: [PATCH 102/213] Remove `PackedQ1X2SubgridV1::array_mut` --- pineappl/src/packed_subgrid.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index b9d7790f..1024d50a 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -32,11 +32,6 @@ impl PackedQ1X2SubgridV1 { x2_grid, } } - - /// Return the array containing the numerical values of the grid. - pub fn array_mut(&mut self) -> &mut PackedArray { - &mut self.array - } } impl Subgrid for PackedQ1X2SubgridV1 { @@ -293,10 +288,10 @@ mod tests { // only use exactly representable numbers here so that we can avoid using approx_eq if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid1 { - x.array_mut()[[0, 1, 2]] = 1.0; - x.array_mut()[[0, 1, 3]] = 2.0; - x.array_mut()[[0, 4, 3]] = 4.0; - x.array_mut()[[0, 7, 1]] = 8.0; + x.array[[0, 1, 2]] = 1.0; + x.array[[0, 1, 3]] = 2.0; + x.array[[0, 4, 3]] = 4.0; + x.array[[0, 7, 1]] = 8.0; } assert!(!grid1.is_empty()); @@ -319,10 +314,10 @@ mod tests { ) .into(); if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid2 { - x.array_mut()[[0, 2, 1]] = 1.0; - x.array_mut()[[0, 3, 1]] = 2.0; - x.array_mut()[[0, 3, 4]] = 4.0; - x.array_mut()[[0, 1, 7]] = 8.0; + x.array[[0, 2, 1]] = 1.0; + x.array[[0, 3, 1]] = 2.0; + x.array[[0, 3, 4]] = 4.0; + x.array[[0, 1, 7]] = 8.0; } assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); From f67a3418560485d441804806630c8db52207f606 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 12 Sep 2024 09:36:27 +0200 Subject: [PATCH 103/213] Remove `Subgrid::clone_empty` --- pineappl/src/empty_subgrid.rs | 5 ----- pineappl/src/grid.rs | 18 ++++++++++-------- pineappl/src/lagrange_subgrid.rs | 4 ---- pineappl/src/packed_subgrid.rs | 16 ---------------- pineappl/src/subgrid.rs | 3 --- 5 files changed, 10 insertions(+), 36 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 79ad25bc..dd9df9a2 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -42,10 +42,6 @@ impl Subgrid for EmptySubgridV1 { fn symmetrize(&mut self) {} - fn clone_empty(&self) -> SubgridEnum { - Self.into() - } - fn indexed_iter(&self) -> SubgridIndexedIter { Box::new(iter::empty()) } @@ -77,7 +73,6 @@ mod tests { subgrid.merge(&mut EmptySubgridV1.into(), false); subgrid.scale(2.0); subgrid.symmetrize(); - assert!(subgrid.clone_empty().is_empty()); assert_eq!( subgrid.stats(), Stats { diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 3caaac2b..2d5c348c 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -931,11 +931,11 @@ impl Grid { rhs.scale(1.0 / factor); if lhs.is_empty() { // we can't merge into an EmptySubgridV1 - *lhs = rhs.clone_empty(); + *lhs = mem::replace(rhs, EmptySubgridV1.into()); + } else { + lhs.merge(rhs, false); + *rhs = EmptySubgridV1.into(); } - lhs.merge(rhs, false); - - *rhs = EmptySubgridV1.into(); } } } @@ -1013,11 +1013,13 @@ impl Grid { if !rhs.is_empty() { if lhs.is_empty() { // we can't merge into an EmptySubgridV1 - *lhs = rhs.clone_empty(); + *lhs = mem::replace(rhs, EmptySubgridV1.into()); + // transpose `lhs` + todo!(); + } else { + lhs.merge(rhs, true); + *rhs = EmptySubgridV1.into(); } - - lhs.merge(rhs, true); - *rhs = EmptySubgridV1.into(); } } } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 0beda0b9..4b95a87d 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -107,10 +107,6 @@ impl Subgrid for LagrangeSubgridV2 { self.grid = new_array; } - fn clone_empty(&self) -> SubgridEnum { - self.clone().into() - } - fn indexed_iter(&self) -> SubgridIndexedIter { let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 1024d50a..8fef0077 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -156,16 +156,6 @@ impl Subgrid for PackedQ1X2SubgridV1 { mem::swap(&mut self.array, &mut new_array); } - fn clone_empty(&self) -> SubgridEnum { - Self { - array: PackedArray::new([self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()]), - mu2_grid: self.mu2_grid.clone(), - x1_grid: self.x1_grid.clone(), - x2_grid: self.x2_grid.clone(), - } - .into() - } - fn indexed_iter(&self) -> SubgridIndexedIter { Box::new( self.array @@ -327,12 +317,6 @@ mod tests { grid1.merge(&mut grid2, false); - let mut grid1 = { - let mut g = grid1.clone_empty(); - g.merge(&mut grid1, false); - g - }; - // the luminosity function is symmetric, so after symmetrization the result must be // unchanged grid1.symmetrize(); diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index d05ea332..f27784f6 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -87,9 +87,6 @@ pub trait Subgrid { /// grid by getting rid of almost half of the entries. fn symmetrize(&mut self); - /// Returns an empty copy of the current subgrid. - fn clone_empty(&self) -> SubgridEnum; - /// Return an iterator over all non-zero elements of the subgrid. fn indexed_iter(&self) -> SubgridIndexedIter; From 90ab92c814ee5e2f94e1e49890b69286ab9c3ddf Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 12 Sep 2024 12:39:04 +0200 Subject: [PATCH 104/213] Rename `grid` to `array` --- pineappl/src/lagrange_subgrid.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 4b95a87d..b2eb7f92 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -11,7 +11,7 @@ use std::mem; /// Subgrid which uses Lagrange-interpolation. #[derive(Clone, Deserialize, Serialize)] pub struct LagrangeSubgridV2 { - grid: PackedArray, + array: PackedArray, interps: Vec, pub(crate) static_q2: f64, } @@ -22,7 +22,7 @@ impl LagrangeSubgridV2 { pub fn new(interps: &[Interp]) -> Self { debug_assert_eq!(interps.len(), 3); Self { - grid: PackedArray::new([interps[0].nodes(), interps[1].nodes(), interps[2].nodes()]), + array: PackedArray::new([interps[0].nodes(), interps[1].nodes(), interps[2].nodes()]), interps: interps.to_vec(), static_q2: 0.0, } @@ -36,7 +36,7 @@ impl Subgrid for LagrangeSubgridV2 { // TODO: lift the restriction to exactly three kinematics variables debug_assert_eq!(interps.len(), 3); - if interpolation::interpolate(interps, &ntuple, weight, &mut self.grid) { + if interpolation::interpolate(interps, &ntuple, weight, &mut self.array) { let q2 = ntuple[0]; if self.static_q2 == 0.0 { self.static_q2 = q2; @@ -67,18 +67,18 @@ impl Subgrid for LagrangeSubgridV2 { } fn is_empty(&self) -> bool { - self.grid.is_empty() + self.array.is_empty() } fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { // we cannot use `Self::indexed_iter` because it multiplies with `reweight` if let SubgridEnum::LagrangeSubgridV2(other) = other { // TODO: make sure `other` has the same interpolation as `self` - for (mut index, value) in other.grid.indexed_iter() { + for (mut index, value) in other.array.indexed_iter() { if transpose { index.swap(1, 2); } - self.grid[index] += value; + self.array[index] += value; } } else { unimplemented!(); @@ -86,7 +86,7 @@ impl Subgrid for LagrangeSubgridV2 { } fn scale(&mut self, factor: f64) { - self.grid *= factor; + self.array *= factor; } fn symmetrize(&mut self) { @@ -96,21 +96,21 @@ impl Subgrid for LagrangeSubgridV2 { self.x2_grid().len(), ]); - for ([i, j, k], sigma) in self.grid.indexed_iter().filter(|([_, j, k], _)| k >= j) { + for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k >= j) { new_array[[i, j, k]] = sigma; } // do not change the diagonal entries (k==j) - for ([i, j, k], sigma) in self.grid.indexed_iter().filter(|([_, j, k], _)| k < j) { + for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k < j) { new_array[[i, k, j]] += sigma; } - self.grid = new_array; + self.array = new_array; } fn indexed_iter(&self) -> SubgridIndexedIter { let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); - Box::new(self.grid.indexed_iter().map(move |(index, v)| { + Box::new(self.array.indexed_iter().map(move |(index, v)| { ( (index[0], index[1], index[2]), v * self @@ -126,9 +126,9 @@ impl Subgrid for LagrangeSubgridV2 { fn stats(&self) -> Stats { Stats { total: self.mu2_grid().len() * self.x1_grid().len() * self.x2_grid().len(), - allocated: self.grid.non_zeros() + self.grid.explicit_zeros(), - zeros: self.grid.explicit_zeros(), - overhead: self.grid.overhead(), + allocated: self.array.non_zeros() + self.array.explicit_zeros(), + zeros: self.array.explicit_zeros(), + overhead: self.array.overhead(), bytes_per_value: mem::size_of::(), } } From 751f92c99927b93c949f083737ee1e0ffde0d5a2 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 12 Sep 2024 14:42:47 +0200 Subject: [PATCH 105/213] Unify the methods of `LagrangeSubgridV2` and `PackedQ1X2SubgridV1` --- pineappl/src/lagrange_subgrid.rs | 27 ++++++++++++++------------- pineappl/src/packed_subgrid.rs | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index b2eb7f92..4d950c2a 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -90,18 +90,19 @@ impl Subgrid for LagrangeSubgridV2 { } fn symmetrize(&mut self) { - let mut new_array = PackedArray::new([ - self.mu2_grid().len(), - self.x1_grid().len(), - self.x2_grid().len(), - ]); - - for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k >= j) { - new_array[[i, j, k]] = sigma; - } - // do not change the diagonal entries (k==j) - for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k < j) { - new_array[[i, k, j]] += sigma; + let mut new_array = PackedArray::new( + <[usize; 3]>::try_from(self.array.shape()) + // UNWRAP: this must succeed since the array is 3-dimensional + .unwrap(), + ); + + for (mut index, sigma) in self.array.indexed_iter() { + // TODO: why not the other way around? + if index[2] < index[1] { + index.swap(1, 2); + } + + new_array[index] += sigma; } self.array = new_array; @@ -125,7 +126,7 @@ impl Subgrid for LagrangeSubgridV2 { fn stats(&self) -> Stats { Stats { - total: self.mu2_grid().len() * self.x1_grid().len() * self.x2_grid().len(), + total: self.array.shape().iter().product(), allocated: self.array.non_zeros() + self.array.explicit_zeros(), zeros: self.array.explicit_zeros(), overhead: self.array.overhead(), diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 8fef0077..0121de8c 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -142,18 +142,22 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn symmetrize(&mut self) { - let mut new_array = - PackedArray::new([self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()]); + let mut new_array = PackedArray::new( + <[usize; 3]>::try_from(self.array.shape()) + // UNWRAP: this must succeed since the array is 3-dimensional + .unwrap(), + ); - for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k >= j) { - new_array[[i, j, k]] = sigma; - } - // do not change the diagonal entries (k==j) - for ([i, j, k], sigma) in self.array.indexed_iter().filter(|([_, j, k], _)| k < j) { - new_array[[i, k, j]] += sigma; + for (mut index, sigma) in self.array.indexed_iter() { + // TODO: why not the other way around? + if index[2] < index[1] { + index.swap(1, 2); + } + + new_array[index] += sigma; } - mem::swap(&mut self.array, &mut new_array); + self.array = new_array; } fn indexed_iter(&self) -> SubgridIndexedIter { @@ -166,7 +170,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { fn stats(&self) -> Stats { Stats { - total: self.mu2_grid.len() * self.x1_grid.len() * self.x2_grid.len(), + total: self.array.shape().iter().product(), allocated: self.array.non_zeros() + self.array.explicit_zeros(), zeros: self.array.explicit_zeros(), overhead: self.array.overhead(), From f3ae04fbee177dd7663938440d0208822cc47451 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 12 Sep 2024 14:47:04 +0200 Subject: [PATCH 106/213] Add 'test-opt' profile for fast tests --- Cargo.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 82786bf9..9fecccb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,3 +44,8 @@ unsafe-op-in-unsafe-fn = "deny" codegen-units = 1 lto = true strip = "debuginfo" + +[profile.test-opt] +inherits = "test" +opt-level = 1 +debug = false From b16ac2e2be6db1ad8a29a7e5091ab5005de740a7 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 13 Sep 2024 11:10:59 +0200 Subject: [PATCH 107/213] Remove `PackedArray::default` method --- pineappl/src/packed_array.rs | 11 ----------- pineappl/src/packed_subgrid.rs | 9 +-------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 2c6d409b..706a4d31 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -23,17 +23,6 @@ pub struct PackedArray { shape: Vec, } -impl Default for PackedArray { - fn default() -> Self { - Self { - entries: Vec::new(), - start_indices: Vec::new(), - lengths: Vec::new(), - shape: vec![0; D], - } - } -} - impl PackedArray { /// Constructs a new and empty `PackedArray` of shape `shape`. #[must_use] diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 0121de8c..f4ad544b 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -8,7 +8,7 @@ use std::borrow::Cow; use std::mem; /// TODO -#[derive(Clone, Default, Deserialize, Serialize)] +#[derive(Clone, Deserialize, Serialize)] pub struct PackedQ1X2SubgridV1 { array: PackedArray, mu2_grid: Vec, @@ -56,13 +56,6 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { - if self.is_empty() && !transpose { - if let SubgridEnum::PackedQ1X2SubgridV1(other) = other { - *self = mem::take(other); - return; - } - } - let rhs_mu2 = other.mu2_grid().into_owned(); let rhs_x1 = if transpose { other.x2_grid() From 4f05cb8bc29030c2d4944439b2b9e88035e2abf9 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 13 Sep 2024 13:40:08 +0200 Subject: [PATCH 108/213] Remove `mut` from `Subgrid::merge` --- pineappl/src/empty_subgrid.rs | 2 +- pineappl/src/lagrange_subgrid.rs | 2 +- pineappl/src/packed_subgrid.rs | 2 +- pineappl/src/subgrid.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index dd9df9a2..1dbeb23b 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -31,7 +31,7 @@ impl Subgrid for EmptySubgridV1 { true } - fn merge(&mut self, subgrid: &mut SubgridEnum, _: bool) { + fn merge(&mut self, subgrid: &SubgridEnum, _: bool) { assert!( subgrid.is_empty(), "EmptySubgridV1 doesn't support the merge operation for non-empty subgrids" diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 4d950c2a..9ce4c183 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -70,7 +70,7 @@ impl Subgrid for LagrangeSubgridV2 { self.array.is_empty() } - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { + fn merge(&mut self, other: &SubgridEnum, transpose: bool) { // we cannot use `Self::indexed_iter` because it multiplies with `reweight` if let SubgridEnum::LagrangeSubgridV2(other) = other { // TODO: make sure `other` has the same interpolation as `self` diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index f4ad544b..0b8f1285 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -55,7 +55,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { self.array.is_empty() } - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { + fn merge(&mut self, other: &SubgridEnum, transpose: bool) { let rhs_mu2 = other.mu2_grid().into_owned(); let rhs_x1 = if transpose { other.x2_grid() diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index f27784f6..8a9ce375 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -78,7 +78,7 @@ pub trait Subgrid { fn is_empty(&self) -> bool; /// Merges `other` into this subgrid. - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool); + fn merge(&mut self, other: &SubgridEnum, transpose: bool); /// Scale the subgrid by `factor`. fn scale(&mut self, factor: f64); From dc62e5a53782afb7dff1512b7e2478111371d8c2 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 13 Sep 2024 14:36:06 +0200 Subject: [PATCH 109/213] Remove specification of dimension in `PackedArray` --- pineappl/src/interpolation.rs | 14 ++++---- pineappl/src/lagrange_subgrid.rs | 18 ++++------- pineappl/src/packed_array.rs | 50 +++++++++++++++-------------- pineappl/src/packed_subgrid.rs | 22 ++++++------- pineappl/src/v0.rs | 2 +- pineappl_cli/src/import/applgrid.rs | 2 +- pineappl_cli/src/import/fastnlo.rs | 4 +-- pineappl_cli/src/import/fktable.rs | 4 +-- 8 files changed, 55 insertions(+), 61 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index e71b08b7..bc3cf5eb 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -459,12 +459,12 @@ mod tests { assert_approx_eq!(f64, node, ref_node, ulps = 4); } - let mut array = crate::packed_array::PackedArray::::new([40, 50, 50]); + let mut array = crate::packed_array::PackedArray::::new(&[40, 50, 50]); let ntuples = [[100000.0, 0.25, 0.5], [1000.0, 0.5, 0.5]]; let weight = 1.0; for ntuple in &ntuples { - interpolate(&interps, ntuple, weight, &mut array); + interpolate::<3>(&interps, ntuple, weight, &mut array); } let reference = [ @@ -635,18 +635,18 @@ mod tests { InterpMeth::Lagrange, ), ]; - let mut array = crate::packed_array::PackedArray::::new([40, 50, 50]); + let mut array = crate::packed_array::PackedArray::::new(&[40, 50, 50]); let ntuple = [1000.0, 0.5, 0.5]; let weight = 0.0; - interpolate(&interps, &ntuple, weight, &mut array); + interpolate::<3>(&interps, &ntuple, weight, &mut array); assert_eq!(array.non_zeros(), 0); assert_eq!(array.explicit_zeros(), 0); let ntuple = [10.0, 0.5, 0.5]; let weight = 1.0; - interpolate(&interps, &ntuple, weight, &mut array); + interpolate::<3>(&interps, &ntuple, weight, &mut array); assert_eq!(array.non_zeros(), 0); assert_eq!(array.explicit_zeros(), 0); @@ -665,11 +665,11 @@ mod tests { Map::ApplGridH0, InterpMeth::Lagrange, )]; - let mut array = crate::packed_array::PackedArray::::new([1]); + let mut array = crate::packed_array::PackedArray::::new(&[1]); let ntuple = [90.0_f64.powi(2)]; let weight = 1.0; - interpolate(&interps, &ntuple, weight, &mut array); + interpolate::<1>(&interps, &ntuple, weight, &mut array); assert_eq!(array[[0]], 1.0); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 9ce4c183..b47393bf 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -11,7 +11,7 @@ use std::mem; /// Subgrid which uses Lagrange-interpolation. #[derive(Clone, Deserialize, Serialize)] pub struct LagrangeSubgridV2 { - array: PackedArray, + array: PackedArray, interps: Vec, pub(crate) static_q2: f64, } @@ -22,7 +22,7 @@ impl LagrangeSubgridV2 { pub fn new(interps: &[Interp]) -> Self { debug_assert_eq!(interps.len(), 3); Self { - array: PackedArray::new([interps[0].nodes(), interps[1].nodes(), interps[2].nodes()]), + array: PackedArray::new(&[interps[0].nodes(), interps[1].nodes(), interps[2].nodes()]), interps: interps.to_vec(), static_q2: 0.0, } @@ -36,7 +36,7 @@ impl Subgrid for LagrangeSubgridV2 { // TODO: lift the restriction to exactly three kinematics variables debug_assert_eq!(interps.len(), 3); - if interpolation::interpolate(interps, &ntuple, weight, &mut self.array) { + if interpolation::interpolate::<3>(interps, &ntuple, weight, &mut self.array) { let q2 = ntuple[0]; if self.static_q2 == 0.0 { self.static_q2 = q2; @@ -74,7 +74,7 @@ impl Subgrid for LagrangeSubgridV2 { // we cannot use `Self::indexed_iter` because it multiplies with `reweight` if let SubgridEnum::LagrangeSubgridV2(other) = other { // TODO: make sure `other` has the same interpolation as `self` - for (mut index, value) in other.array.indexed_iter() { + for (mut index, value) in other.array.indexed_iter::<3>() { if transpose { index.swap(1, 2); } @@ -90,13 +90,9 @@ impl Subgrid for LagrangeSubgridV2 { } fn symmetrize(&mut self) { - let mut new_array = PackedArray::new( - <[usize; 3]>::try_from(self.array.shape()) - // UNWRAP: this must succeed since the array is 3-dimensional - .unwrap(), - ); + let mut new_array = PackedArray::new(self.array.shape()); - for (mut index, sigma) in self.array.indexed_iter() { + for (mut index, sigma) in self.array.indexed_iter::<3>() { // TODO: why not the other way around? if index[2] < index[1] { index.swap(1, 2); @@ -111,7 +107,7 @@ impl Subgrid for LagrangeSubgridV2 { fn indexed_iter(&self) -> SubgridIndexedIter { let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); - Box::new(self.array.indexed_iter().map(move |(index, v)| { + Box::new(self.array.indexed_iter::<3>().map(move |(index, v)| { ( (index[0], index[1], index[2]), v * self diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 706a4d31..f9f43458 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -10,7 +10,7 @@ use std::ops::{Index, IndexMut, MulAssign}; /// stored to save space. Instead, adjacent non-default elements are grouped together and the index /// of their first element (`start_index`) and the length of the group (`lengths`) is stored. #[derive(Clone, Debug, Deserialize, Serialize)] -pub struct PackedArray { +pub struct PackedArray { /// The actual values stored in the array. The length of `entries` is always the sum of the /// elements in `lengths`. entries: Vec, @@ -23,10 +23,10 @@ pub struct PackedArray { shape: Vec, } -impl PackedArray { +impl PackedArray { /// Constructs a new and empty `PackedArray` of shape `shape`. #[must_use] - pub fn new(shape: [usize; D]) -> Self { + pub fn new(shape: &[usize]) -> Self { Self { entries: vec![], start_indices: vec![], @@ -80,7 +80,9 @@ impl PackedArray { /// Returns an `Iterator` over the non-default (non-zero) elements of this array. The type of /// an iterator element is `([usize; D], T)` where the first element of the tuple is the index /// and the second element is the value. - pub fn indexed_iter(&self) -> impl Iterator + '_ { + pub fn indexed_iter(&self) -> impl Iterator + '_ { + assert_eq!(self.shape.len(), D); + self.start_indices .iter() .zip(&self.lengths) @@ -93,19 +95,19 @@ impl PackedArray { } } -impl, const D: usize> MulAssign for PackedArray { +impl> MulAssign for PackedArray { fn mul_assign(&mut self, rhs: T) { self.entries.iter_mut().for_each(|x| *x *= rhs); } } -impl PackedArray { - /// Converts `array` into a `PackedArray`. +impl PackedArray { + /// Converts `array` into a `PackedArray`. #[must_use] pub fn from_ndarray(array: ArrayView3, xstart: usize, xsize: usize) -> Self { let shape = array.shape(); - let mut result = Self::new([xsize, shape[1], shape[2]]); + let mut result = Self::new(&[xsize, shape[1], shape[2]]); for ((i, j, k), &entry) in array .indexed_iter() @@ -144,7 +146,7 @@ pub fn unravel_index(mut index: usize, shape: &[usize]) -> [usiz indices } -impl Index<[usize; D]> for PackedArray { +impl Index<[usize; D]> for PackedArray { type Output = T; fn index(&self, index: [usize; D]) -> &Self::Output { @@ -180,7 +182,7 @@ impl Index<[usize; D]> for Packed } impl IndexMut<[usize; D]> - for PackedArray + for PackedArray { fn index_mut(&mut self, index: [usize; D]) -> &mut Self::Output { assert_eq!(index.len(), self.shape.len()); @@ -325,7 +327,7 @@ mod tests { #[test] fn index() { - let mut a = PackedArray::new([4, 2]); + let mut a = PackedArray::new(&[4, 2]); a[[0, 0]] = 1; assert_eq!(a[[0, 0]], 1); @@ -379,7 +381,7 @@ mod tests { #[test] fn iter() { - let mut a = PackedArray::new([6, 5]); + let mut a = PackedArray::new(&[6, 5]); a[[2, 2]] = 1; a[[2, 4]] = 2; a[[4, 1]] = 3; @@ -399,7 +401,7 @@ mod tests { #[test] fn index_access() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); // after creation the array must be empty assert_eq!(array.overhead(), 0); @@ -544,7 +546,7 @@ mod tests { #[test] #[should_panic(expected = "index [40, 0, 50] is out of bounds for array of shape [40, 50, 50]")] fn index_mut_panic_dim0() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[40, 0, 50]] = 1.0; } @@ -552,7 +554,7 @@ mod tests { #[test] #[should_panic(expected = "index [0, 50, 0] is out of bounds for array of shape [40, 50, 50]")] fn index_mut_panic_dim1() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[0, 50, 0]] = 1.0; } @@ -560,7 +562,7 @@ mod tests { #[test] #[should_panic(expected = "index [0, 0, 50] is out of bounds for array of shape [40, 50, 50]")] fn index_mut_panic_dim2() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[0, 0, 50]] = 1.0; } @@ -568,7 +570,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [0, 0, 0] is implicitly set to the default value")] fn index_panic_dim0_0() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[1, 0, 0]] = 1; @@ -578,7 +580,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [2, 0, 0] is implicitly set to the default value")] fn index_panic_dim0_1() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[1, 0, 0]] = 1; @@ -588,7 +590,7 @@ mod tests { #[test] #[should_panic(expected = "index [1, 50, 0] is out of bounds for array of shape [40, 50, 50]")] fn index_panic_dim1() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[1, 0, 0]] = 1; @@ -598,7 +600,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [0, 0, 0] is implicitly set to the default value")] fn index_panic_dim2_0() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[0, 0, 1]] = 1; @@ -608,7 +610,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [0, 0, 2] is implicitly set to the default value")] fn index_panic_dim2_1() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[0, 0, 1]] = 1; @@ -617,13 +619,13 @@ mod tests { #[test] fn indexed_iter() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); // check shape assert_eq!(array.shape(), [40, 50, 50]); // check empty iterator - assert_eq!(array.indexed_iter().next(), None); + assert_eq!(array.indexed_iter::<3>().next(), None); // insert an element array[[2, 3, 4]] = 1; @@ -673,7 +675,7 @@ mod tests { #[test] fn clear() { - let mut array = PackedArray::new([40, 50, 50]); + let mut array = PackedArray::new(&[40, 50, 50]); array[[3, 5, 1]] = 1; array[[7, 8, 9]] = 2; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 0b8f1285..222d8a59 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -10,7 +10,7 @@ use std::mem; /// TODO #[derive(Clone, Deserialize, Serialize)] pub struct PackedQ1X2SubgridV1 { - array: PackedArray, + array: PackedArray, mu2_grid: Vec, x1_grid: Vec, x2_grid: Vec, @@ -20,7 +20,7 @@ impl PackedQ1X2SubgridV1 { /// Constructor. #[must_use] pub const fn new( - array: PackedArray, + array: PackedArray, mu2_grid: Vec, x1_grid: Vec, x2_grid: Vec, @@ -83,7 +83,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); x2_grid.dedup(); - let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + let mut array = PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for ([i, j, k], value) in self.array.indexed_iter() { let target_i = mu2_grid @@ -135,13 +135,9 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn symmetrize(&mut self) { - let mut new_array = PackedArray::new( - <[usize; 3]>::try_from(self.array.shape()) - // UNWRAP: this must succeed since the array is 3-dimensional - .unwrap(), - ); + let mut new_array = PackedArray::new(self.array.shape()); - for (mut index, sigma) in self.array.indexed_iter() { + for (mut index, sigma) in self.array.indexed_iter::<3>() { // TODO: why not the other way around? if index[2] < index[1] { index.swap(1, 2); @@ -205,7 +201,7 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { let x1_grid = subgrid.x1_grid()[x1_range.clone()].to_vec(); let x2_grid = subgrid.x2_grid()[x2_range.clone()].to_vec(); - let mut array = PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + let mut array = PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { // if there's a static scale we want every value to be added to same grid point @@ -236,7 +232,7 @@ mod tests { #[should_panic(expected = "PackedQ1X2SubgridV1 doesn't support the fill operation")] fn fill_packed_q1x2_subgrid_v1() { let mut subgrid = PackedQ1X2SubgridV1::new( - PackedArray::new([0, 0, 0]), + PackedArray::new(&[0, 0, 0]), Vec::new(), Vec::new(), Vec::new(), @@ -250,7 +246,7 @@ mod tests { 0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0, ]; let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( - PackedArray::new([1, 10, 10]), + PackedArray::new(&[1, 10, 10]), vec![Mu2 { ren: 0.0, fac: 0.0, @@ -290,7 +286,7 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( - PackedArray::new([1, 10, 10]), + PackedArray::new(&[1, 10, 10]), vec![Mu2 { ren: 1.0, fac: 1.0, diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index b94e566f..045f19c8 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -72,7 +72,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result let x1_grid = subgrid.x1_grid().into_owned(); let x2_grid = subgrid.x2_grid().into_owned(); let mut array = - PackedArray::new([mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for (index, v) in subgrid.indexed_iter() { array[index.into()] = v; } diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index dc199ff4..815f036b 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -220,7 +220,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let matrix = unsafe { &*matrix }; let mut array = - PackedArray::new([mu2_values.len(), x1_values.len(), x2_values.len()]); + PackedArray::new(&[mu2_values.len(), x1_values.len(), x2_values.len()]); for itau in 0..mu2_values.len() { for ix1 in 0..x1_values.len() { diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index fb90968f..3ca5ed01 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -195,7 +195,7 @@ fn convert_coeff_add_fix( .collect(); let mut array = - PackedArray::new([mu2_values.len(), x1_values.len(), x2_values.len()]); + PackedArray::new(&[mu2_values.len(), x1_values.len(), x2_values.len()]); // TODO: figure out what the general case is supposed to be assert_eq!(j, 0); @@ -370,7 +370,7 @@ fn convert_coeff_add_flex( let factor = rescale / table_as_add_base.GetNevt(obs.try_into().unwrap(), subproc); let mut arrays = vec![ - PackedArray::new([mu2_values.len(), x1_values.len(), x2_values.len()]); + PackedArray::new(&[mu2_values.len(), x1_values.len(), x2_values.len()]); orders_len ]; diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index d29dfd22..c631205e 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -149,7 +149,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { grid = Some(fktable); - arrays = iter::repeat(PackedArray::new([1, nx1, nx2])) + arrays = iter::repeat(PackedArray::new(&[1, nx1, nx2])) .take(flavor_mask.iter().filter(|&&value| value).count()) .collect(); } @@ -225,7 +225,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .into(); } - arrays = iter::repeat(PackedArray::new([1, nx1, nx2])) + arrays = iter::repeat(PackedArray::new(&[1, nx1, nx2])) .take(flavor_mask.iter().filter(|&&value| value).count()) .collect(); last_bin = bin; From c437e0c19d9b4cfe7105d1fbf42c4db2821bbf74 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 13 Sep 2024 16:16:40 +0200 Subject: [PATCH 110/213] Add more index methods to `PackedArray` --- pineappl/src/lagrange_subgrid.rs | 3 +- pineappl/src/packed_array.rs | 71 +++++++++++++++++++++++++++++--- pineappl/src/v0.rs | 2 +- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index b47393bf..10af838f 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -20,9 +20,8 @@ impl LagrangeSubgridV2 { /// Constructor. #[must_use] pub fn new(interps: &[Interp]) -> Self { - debug_assert_eq!(interps.len(), 3); Self { - array: PackedArray::new(&[interps[0].nodes(), interps[1].nodes(), interps[2].nodes()]), + array: PackedArray::new(&interps.iter().map(Interp::nodes).collect::>()), interps: interps.to_vec(), static_q2: 0.0, } diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index f9f43458..faa41516 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -77,6 +77,17 @@ impl PackedArray { self.entries.iter().filter(|x| **x != T::default()).count() } + /// TODO + pub fn indexed_iter2(&self) -> impl Iterator + '_ { + self.start_indices + .iter() + .zip(&self.lengths) + .flat_map(|(&start_index, &length)| start_index..(start_index + length)) + .zip(&self.entries) + .filter(|&(_, entry)| *entry != Default::default()) + .map(|(indices, entry)| (indices, *entry)) + } + /// Returns an `Iterator` over the non-default (non-zero) elements of this array. The type of /// an iterator element is `([usize; D], T)` where the first element of the tuple is the index /// and the second element is the value. @@ -121,7 +132,7 @@ impl PackedArray { } /// Converts a `multi_index` into a flat index. -fn ravel_multi_index(multi_index: &[usize; D], shape: &[usize]) -> usize { +fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { assert_eq!(multi_index.len(), shape.len()); multi_index @@ -150,6 +161,14 @@ impl Index<[usize; D]> for Packed type Output = T; fn index(&self, index: [usize; D]) -> &Self::Output { + &self[index.as_slice()] + } +} + +impl Index<&[usize]> for PackedArray { + type Output = T; + + fn index(&self, index: &[usize]) -> &Self::Output { assert_eq!(index.len(), self.shape.len()); assert!( index.iter().zip(self.shape.iter()).all(|(&i, &d)| i < d), @@ -181,10 +200,44 @@ impl Index<[usize; D]> for Packed } } -impl IndexMut<[usize; D]> - for PackedArray -{ - fn index_mut(&mut self, index: [usize; D]) -> &mut Self::Output { +impl Index for PackedArray { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + // assert_eq!(index.len(), self.shape.len()); + // assert!( + // index.iter().zip(self.shape.iter()).all(|(&i, &d)| i < d), + // "index {:?} is out of bounds for array of shape {:?}", + // index, + // self.shape + // ); + + let raveled_index = index; + // let raveled_index = ravel_multi_index(&index, &self.shape); + let point = self.start_indices.partition_point(|&i| i <= raveled_index); + + assert!( + point > 0, + "entry at index {index:?} is implicitly set to the default value" + ); + + let start_index = self.start_indices[point - 1]; + let length = self.lengths[point - 1]; + + let point_entries = + self.lengths.iter().take(point - 1).sum::() + raveled_index - start_index; + + assert!( + raveled_index < (start_index + length), + "entry at index {index:?} is implicitly set to the default value" + ); + + &self.entries[point_entries] + } +} + +impl IndexMut<&[usize]> for PackedArray { + fn index_mut(&mut self, index: &[usize]) -> &mut Self::Output { assert_eq!(index.len(), self.shape.len()); // Panic if the index value for any dimension is greater or equal than the length of this @@ -299,6 +352,14 @@ impl IndexMut<[usize; D]> } } +impl IndexMut<[usize; D]> + for PackedArray +{ + fn index_mut(&mut self, index: [usize; D]) -> &mut Self::Output { + &mut self[index.as_slice()] + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 045f19c8..090f10e9 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -74,7 +74,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result let mut array = PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for (index, v) in subgrid.indexed_iter() { - array[index.into()] = v; + array[<[usize; 3]>::from(index)] = v; } PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() } From 315e2854dec3d9f5a6d95085b1e5a0f130520d23 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 17 Sep 2024 09:59:04 +0200 Subject: [PATCH 111/213] Remove more static specifications of the dimension --- pineappl/src/interpolation.rs | 15 ++-- pineappl/src/packed_array.rs | 146 +++++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 10 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index bc3cf5eb..140390bd 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -1,10 +1,10 @@ //! Interpolation module. use super::convert; +use super::packed_array::PackedArray; use arrayvec::ArrayVec; use serde::{Deserialize, Serialize}; use std::mem; -use std::ops::IndexMut; const MAX_INTERP_ORDER_PLUS_ONE: usize = 8; @@ -252,9 +252,8 @@ pub fn interpolate( interps: &[Interp], ntuple: &[f64], weight: f64, - array: &mut impl IndexMut<[usize; D], Output = f64>, + array: &mut PackedArray, ) -> bool { - use super::packed_array; use itertools::Itertools; if weight == 0.0 { @@ -291,17 +290,15 @@ pub fn interpolate( let shape: ArrayVec<_, D> = interps.iter().map(|interp| interp.order() + 1).collect(); - for (i, node_weights) in node_weights + for (i, node_weight) in node_weights .into_iter() // TODO: replace this with something else to avoid allocating memory .multi_cartesian_product() + .map(|weights| weights.iter().product::()) .enumerate() { - let mut index = packed_array::unravel_index::(i, &shape); - for (entry, start_index) in index.iter_mut().zip(&indices) { - *entry += start_index; - } - array[index] += weight * node_weights.iter().product::(); + let idx = array.sub_block_idx(&indices, i, &shape); + array[idx] += weight * node_weight; } true diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index faa41516..4b5ec6fb 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -104,6 +104,33 @@ impl PackedArray { .filter(|&(_, entry)| *entry != Default::default()) .map(|(indices, entry)| (indices, *entry)) } + + /// TODO + // TODO: rewrite this method into `sub_block_iter_mut() -> impl Iterator` + pub fn sub_block_idx( + &self, + start_index: &[usize], + mut i: usize, + fill_shape: &[usize], + ) -> usize { + use super::packed_array; + + assert_eq!(start_index.len(), fill_shape.len()); + + let mut index = { + assert!(i < fill_shape.iter().product()); + let mut indices = vec![0; start_index.len()]; + for (j, d) in indices.iter_mut().zip(fill_shape).rev() { + *j = i % d; + i /= d; + } + indices + }; + for (entry, start_index) in index.iter_mut().zip(start_index) { + *entry += start_index; + } + packed_array::ravel_multi_index(&index, &self.shape) + } } impl> MulAssign for PackedArray { @@ -132,7 +159,7 @@ impl PackedArray { } /// Converts a `multi_index` into a flat index. -fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { +pub fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { assert_eq!(multi_index.len(), shape.len()); multi_index @@ -236,6 +263,123 @@ impl Index for PackedArray { } } +impl IndexMut for PackedArray { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + // assert_eq!(index.len(), self.shape.len()); + + // // Panic if the index value for any dimension is greater or equal than the length of this + // // dimension. + // assert!( + // index.iter().zip(self.shape.iter()).all(|(&i, &d)| i < d), + // "index {:?} is out of bounds for array of shape {:?}", + // index, + // self.shape + // ); + + // // The insertion cases are: + // // 1. this array already stores an element at `index`: + // // -> we just have to update this element + // // 2. this array does not store an element at `index`: + // // a. the distance of the (raveled) `index` is `threshold_distance` away from the next + // // or previous element that is already stored: + // // -> we can merge the new element into already stored groups, potentially padding + // // with `T::default()` elements + // // b. the distance of the (raveled) `index` from the existing elements is greater than + // // `threshold_distance`: + // // -> we insert the element as a new group + + // let raveled_index = ravel_multi_index(&index, &self.shape); + let raveled_index = index; + + // To determine which groups the new element is close to, `point` is the index of the + // start_index of the first group after the new element. `point` is 0 if no elements before + // the new element are stored, and point is `self.start_indices.len()` if no elements after + // the new element are stored. + let point = self.start_indices.partition_point(|&i| i <= raveled_index); + + // `point_entries` is the index of the first element of the next group, given in + // `self.entries`, i.e. the element at index `self.start_indices[point]`. + let point_entries = self.lengths.iter().take(point).sum::(); + + // Maximum distance for merging groups. If the new element is within `threshold_distance` + // of an existing group (i.e. there are `threshold_distance - 1` implicit elements + // between them), we merge the new element into the existing group. We choose 2 as the + // `threshold_distance` based on memory: in the case of `T` = `f64`, it is more economical + // to store one zero explicitly than to store the start_index and length of a new group. + let threshold_distance = 2; + + // If `point > 0`, there is at least one group preceding the new element. Thus, in the + // following we determine if we can insert the new element into this group. + if point > 0 { + // start_index and length of the group before the new element, i.e. the group + // (potentially) getting the new element + let start_index = self.start_indices[point - 1]; + let length = self.lengths[point - 1]; + + // Case 1: an element is already stored at this `index` + if raveled_index < start_index + length { + return &mut self.entries[point_entries - length + raveled_index - start_index]; + // Case 2a: the new element can be merged into the preceding group + } else if raveled_index < start_index + length + threshold_distance { + let distance = raveled_index - (start_index + length) + 1; + // Merging happens by increasing the length of the group + self.lengths[point - 1] += distance; + // and inserting the necessary number of default elements. + self.entries.splice( + point_entries..point_entries, + iter::repeat(Default::default()).take(distance), + ); + + // If the new element is within `threshold_distance` of the *next* group, we merge + // the next group into this group. + if let Some(start_index_next) = self.start_indices.get(point) { + if raveled_index + threshold_distance >= *start_index_next { + let distance_next = start_index_next - raveled_index; + + // Increase the length of this group + self.lengths[point - 1] += distance_next - 1 + self.lengths[point]; + // and remove the next group. we don't have to manipulate `self.entries`, + // since the grouping of the elements is handled only by + // `self.start_indices` and `self.lengths` + self.lengths.remove(point); + self.start_indices.remove(point); + // Insert the default elements between the groups. + self.entries.splice( + point_entries..point_entries, + iter::repeat(Default::default()).take(distance_next - 1), + ); + } + } + + return &mut self.entries[point_entries - 1 + distance]; + } + } + + // Case 2a: the new element can be merged into the next group. No `self.lengths.remove` and + // `self.start_indices.remove` here, since we are not merging two groups. + if let Some(start_index_next) = self.start_indices.get(point) { + if raveled_index + threshold_distance >= *start_index_next { + let distance = start_index_next - raveled_index; + + self.start_indices[point] = raveled_index; + self.lengths[point] += distance; + self.entries.splice( + point_entries..point_entries, + iter::repeat(Default::default()).take(distance), + ); + return &mut self.entries[point_entries]; + } + } + + // Case 2b: we insert a new group of length 1 + self.start_indices.insert(point, raveled_index); + self.lengths.insert(point, 1); + self.entries.insert(point_entries, Default::default()); + + &mut self.entries[point_entries] + } +} + impl IndexMut<&[usize]> for PackedArray { fn index_mut(&mut self, index: &[usize]) -> &mut Self::Output { assert_eq!(index.len(), self.shape.len()); From 72beb7ad074426dd20b742476894064a77041619 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 17 Sep 2024 10:02:22 +0200 Subject: [PATCH 112/213] Make ravel functions private again --- pineappl/src/packed_array.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 4b5ec6fb..ba28f4dd 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -159,7 +159,7 @@ impl PackedArray { } /// Converts a `multi_index` into a flat index. -pub fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { +fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { assert_eq!(multi_index.len(), shape.len()); multi_index @@ -174,7 +174,7 @@ pub fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { /// /// Panics when `index` is out of range. #[must_use] -pub fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D] { +fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D] { assert!(index < shape.iter().product()); let mut indices = [0; D]; for (i, d) in indices.iter_mut().zip(shape).rev() { From 44dc9f0f41b68d5d9d861aaac162c3c5e9c4f714 Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 17 Sep 2024 10:18:42 +0200 Subject: [PATCH 113/213] Remove `D` from `interpolate` --- pineappl/src/interpolation.rs | 23 ++++++++++++++--------- pineappl/src/lagrange_subgrid.rs | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 140390bd..93f27a4e 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; use std::mem; const MAX_INTERP_ORDER_PLUS_ONE: usize = 8; +const MAX_DIMENSIONS: usize = 8; mod applgrid { pub fn reweight_x(x: f64) -> f64 { @@ -248,7 +249,7 @@ impl Interp { } /// TODO -pub fn interpolate( +pub fn interpolate( interps: &[Interp], ntuple: &[f64], weight: f64, @@ -262,9 +263,12 @@ pub fn interpolate( // we must have as many variables as we want to interpolate debug_assert_eq!(interps.len(), ntuple.len()); - debug_assert_eq!(interps.len(), D); + debug_assert!(interps.len() <= MAX_DIMENSIONS); - let Some((indices, fractions)): Option<(ArrayVec<_, D>, ArrayVec<_, D>)> = interps + let Some((indices, fractions)): Option<( + ArrayVec<_, MAX_DIMENSIONS>, + ArrayVec<_, MAX_DIMENSIONS>, + )> = interps .iter() .zip(ntuple) .map(|(interp, &x)| interp.interpolate(x)) @@ -282,13 +286,14 @@ pub fn interpolate( .map(|(interp, &x)| interp.reweight(x)) .product::(); - let node_weights: ArrayVec<_, D> = interps + let node_weights: ArrayVec<_, MAX_DIMENSIONS> = interps .iter() .zip(fractions) .map(|(interp, fraction)| interp.node_weights(fraction)) .collect(); - let shape: ArrayVec<_, D> = interps.iter().map(|interp| interp.order() + 1).collect(); + let shape: ArrayVec<_, MAX_DIMENSIONS> = + interps.iter().map(|interp| interp.order() + 1).collect(); for (i, node_weight) in node_weights .into_iter() @@ -461,7 +466,7 @@ mod tests { let weight = 1.0; for ntuple in &ntuples { - interpolate::<3>(&interps, ntuple, weight, &mut array); + interpolate(&interps, ntuple, weight, &mut array); } let reference = [ @@ -636,14 +641,14 @@ mod tests { let ntuple = [1000.0, 0.5, 0.5]; let weight = 0.0; - interpolate::<3>(&interps, &ntuple, weight, &mut array); + interpolate(&interps, &ntuple, weight, &mut array); assert_eq!(array.non_zeros(), 0); assert_eq!(array.explicit_zeros(), 0); let ntuple = [10.0, 0.5, 0.5]; let weight = 1.0; - interpolate::<3>(&interps, &ntuple, weight, &mut array); + interpolate(&interps, &ntuple, weight, &mut array); assert_eq!(array.non_zeros(), 0); assert_eq!(array.explicit_zeros(), 0); @@ -666,7 +671,7 @@ mod tests { let ntuple = [90.0_f64.powi(2)]; let weight = 1.0; - interpolate::<1>(&interps, &ntuple, weight, &mut array); + interpolate(&interps, &ntuple, weight, &mut array); assert_eq!(array[[0]], 1.0); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 10af838f..e3a0ec33 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -35,7 +35,7 @@ impl Subgrid for LagrangeSubgridV2 { // TODO: lift the restriction to exactly three kinematics variables debug_assert_eq!(interps.len(), 3); - if interpolation::interpolate::<3>(interps, &ntuple, weight, &mut self.array) { + if interpolation::interpolate(interps, &ntuple, weight, &mut self.array) { let q2 = ntuple[0]; if self.static_q2 == 0.0 { self.static_q2 = q2; From f5d3e07e3f28b68bde1d94ef091128278da3533d Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 17 Sep 2024 14:17:30 +0200 Subject: [PATCH 114/213] Make `Subgrid::indexed_iter` independent of dimensions --- pineappl/src/evolution.rs | 5 ++- pineappl/src/fk_table.rs | 6 ++-- pineappl/src/grid.rs | 35 +++++++++++--------- pineappl/src/lagrange_subgrid.rs | 38 ++++++++++++++-------- pineappl/src/packed_array.rs | 24 ++++++++++++++ pineappl/src/packed_subgrid.rs | 50 ++++++++++++++++------------- pineappl/src/subgrid.rs | 2 +- pineappl_cli/src/export/applgrid.rs | 5 ++- pineappl_cli/src/helpers.rs | 8 +++-- 9 files changed, 115 insertions(+), 58 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 1cbc01ea..f2240227 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -295,7 +295,10 @@ fn ndarray_from_subgrid_orders_slice( }) .collect::>()?; - for ((ifac1, ix1, ix2), value) in subgrid.indexed_iter() { + for (indices, value) in subgrid.indexed_iter() { + let &[ifac1, ix1, ix2] = indices.as_slice() else { + unimplemented!() + }; let Mu2 { ren, fac, frg } = subgrid.mu2_grid()[ifac1]; // TODO: implement evolution for non-zero fragmentation scales diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index a3e969ea..f58ce084 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -178,8 +178,10 @@ impl FkTable { vec![0] }; - for ((_, ix1, ix2), value) in subgrid.indexed_iter() { - result[[bin, channel, indices1[ix1], indices2[ix2]]] = value; + for (index, value) in subgrid.indexed_iter() { + assert_eq!(index.len(), 3); + assert_eq!(index[0], 0); + result[[bin, channel, indices1[index[1]], indices2[index[2]]]] = value; } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 2d5c348c..cb0c2dc0 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -16,7 +16,7 @@ use bitflags::bitflags; use float_cmp::{approx_eq, assert_approx_eq}; use git_version::git_version; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; -use ndarray::{s, Array3, ArrayView3, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; +use ndarray::{s, Array3, ArrayD, ArrayView3, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::collections::BTreeMap; @@ -261,19 +261,20 @@ impl Grid { let mut value = 0.0; - for ((imu2, ix1, ix2), v) in subgrid.indexed_iter() { - let x1 = x1_grid[ix1]; - let x2 = x2_grid[ix2]; + for (idx, v) in subgrid.indexed_iter() { + assert_eq!(idx.len(), 3); + let x1 = x1_grid[idx[1]]; + let x2 = x2_grid[idx[2]]; let mut lumi = 0.0; for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); - let xfx1 = lumi_cache.xfx1(entry.0[0], ix1, imu2); - let xfx2 = lumi_cache.xfx2(entry.0[1], ix2, imu2); + let xfx1 = lumi_cache.xfx1(entry.0[0], idx[1], idx[0]); + let xfx2 = lumi_cache.xfx2(entry.0[1], idx[2], idx[0]); lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); } - let alphas = lumi_cache.alphas(imu2); + let alphas = lumi_cache.alphas(idx[0]); lumi *= alphas.powi(order.alphas.try_into().unwrap()); @@ -310,7 +311,7 @@ impl Grid { bin: usize, channel: usize, (xir, xif, xia): (f64, f64, f64), - ) -> Array3 { + ) -> ArrayD { assert_eq!(xia, 1.0); lumi_cache.setup(self, &[(xir, xif)]).unwrap(); @@ -327,25 +328,27 @@ impl Grid { lumi_cache.set_grids(&mu2_grid, &x1_grid, &x2_grid, xir, xif); - let mut array = Array3::zeros((mu2_grid.len(), x1_grid.len(), x2_grid.len())); + let dim = vec![mu2_grid.len(), x1_grid.len(), x2_grid.len()]; + let mut array = ArrayD::zeros(dim); - for (index @ (imu2, ix1, ix2), value) in subgrid.indexed_iter() { - let x1 = x1_grid[ix1]; - let x2 = x2_grid[ix2]; + for (idx, value) in subgrid.indexed_iter() { + assert_eq!(idx.len(), 3); + let x1 = x1_grid[idx[1]]; + let x2 = x2_grid[idx[2]]; let mut lumi = 0.0; for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); - let xfx1 = lumi_cache.xfx1(entry.0[0], ix1, imu2); - let xfx2 = lumi_cache.xfx2(entry.0[1], ix2, imu2); + let xfx1 = lumi_cache.xfx1(entry.0[0], idx[1], idx[0]); + let xfx2 = lumi_cache.xfx2(entry.0[1], idx[2], idx[0]); lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); } - let alphas = lumi_cache.alphas(imu2); + let alphas = lumi_cache.alphas(idx[0]); lumi *= alphas.powi(order.alphas.try_into().unwrap()); - array[<[usize; 3]>::from(index)] = lumi * value; + array[idx.as_slice()] = lumi * value; } if order.logxir > 0 { diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index e3a0ec33..ea3e30b8 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -32,10 +32,8 @@ impl Subgrid for LagrangeSubgridV2 { fn fill(&mut self, interps: &[Interp], ntuple: &[f64], weight: f64) { debug_assert_eq!(interps.len(), ntuple.len()); - // TODO: lift the restriction to exactly three kinematics variables - debug_assert_eq!(interps.len(), 3); - if interpolation::interpolate(interps, &ntuple, weight, &mut self.array) { + // TODO: make this more general let q2 = ntuple[0]; if self.static_q2 == 0.0 { self.static_q2 = q2; @@ -103,19 +101,33 @@ impl Subgrid for LagrangeSubgridV2 { self.array = new_array; } + // fn indexed_iter(&self) -> SubgridIndexedIter { + // let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); + + // Box::new(self.array.indexed_iter::<3>().map(move |(index, v)| { + // ( + // (index[0], index[1], index[2]), + // v * self + // .interps + // .iter() + // .enumerate() + // .map(|(i, interp)| interp.reweight(nodes[i][index[i]])) + // .product::(), + // ) + // })) + // } + fn indexed_iter(&self) -> SubgridIndexedIter { let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); - Box::new(self.array.indexed_iter::<3>().map(move |(index, v)| { - ( - (index[0], index[1], index[2]), - v * self - .interps - .iter() - .enumerate() - .map(|(i, interp)| interp.reweight(nodes[i][index[i]])) - .product::(), - ) + Box::new(self.array.indexed_iter3().map(move |(indices, weight)| { + let reweight = self + .interps + .iter() + .enumerate() + .map(|(i, interp)| interp.reweight(nodes[i][indices[i]])) + .product::(); + (indices, weight * reweight) })) } diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index ba28f4dd..12db6f63 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -105,6 +105,19 @@ impl PackedArray { .map(|(indices, entry)| (indices, *entry)) } + /// TODO + pub fn indexed_iter3(&self) -> impl Iterator, T)> + '_ { + self.start_indices + .iter() + .zip(&self.lengths) + .flat_map(|(&start_index, &length)| { + (start_index..(start_index + length)).map(|i| unravel_index2(i, &self.shape)) + }) + .zip(&self.entries) + .filter(|&(_, entry)| *entry != Default::default()) + .map(|(indices, entry)| (indices, *entry)) + } + /// TODO // TODO: rewrite this method into `sub_block_iter_mut() -> impl Iterator` pub fn sub_block_idx( @@ -184,6 +197,17 @@ fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D indices } +/// TODO +pub fn unravel_index2(mut index: usize, shape: &[usize]) -> Vec { + assert!(index < shape.iter().product()); + let mut indices = vec![0; shape.len()]; + for (i, d) in indices.iter_mut().zip(shape).rev() { + *i = index % d; + index /= d; + } + indices +} + impl Index<[usize; D]> for PackedArray { type Output = T; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 222d8a59..736937d7 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -108,8 +108,14 @@ impl Subgrid for PackedQ1X2SubgridV1 { self.x2_grid = x2_grid; } - for ((i, j, k), value) in other.indexed_iter() { - let (j, k) = if transpose { (k, j) } else { (j, k) }; + for (indices, value) in other.indexed_iter() { + assert_eq!(indices.len(), 3); + let (j, k) = if transpose { + (indices[2], indices[1]) + } else { + (indices[1], indices[2]) + }; + let i = indices[0]; let target_i = self .mu2_grid .iter() @@ -150,11 +156,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn indexed_iter(&self) -> SubgridIndexedIter { - Box::new( - self.array - .indexed_iter() - .map(|(index, v)| (index.into(), v)), - ) + Box::new(self.array.indexed_iter3()) } fn stats(&self) -> Stats { @@ -185,11 +187,11 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { subgrid.x1_grid().len()..0, subgrid.x2_grid().len()..0, ), - |prev, ((imu2, ix1, ix2), _)| { + |prev, (indices, _)| { ( - prev.0.start.min(imu2)..prev.0.end.max(imu2 + 1), - prev.1.start.min(ix1)..prev.1.end.max(ix1 + 1), - prev.2.start.min(ix2)..prev.2.end.max(ix2 + 1), + prev.0.start.min(indices[0])..prev.0.end.max(indices[0] + 1), + prev.1.start.min(indices[1])..prev.1.end.max(indices[1] + 1), + prev.2.start.min(indices[2])..prev.2.end.max(indices[2] + 1), ) }, ); @@ -203,15 +205,19 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { let mut array = PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { + for (indices, value) in subgrid.indexed_iter() { // if there's a static scale we want every value to be added to same grid point let index = if static_scale { 0 } else { - imu2 - mu2_range.start + indices[0] - mu2_range.start }; - array[[index, ix1 - x1_range.start, ix2 - x2_range.start]] += value; + array[[ + index, + indices[1] - x1_range.start, + indices[2] - x2_range.start, + ]] += value; } Self { @@ -279,10 +285,10 @@ mod tests { assert!(!grid1.is_empty()); - assert_eq!(grid1.indexed_iter().next(), Some(((0, 1, 2), 1.0))); - assert_eq!(grid1.indexed_iter().nth(1), Some(((0, 1, 3), 2.0))); - assert_eq!(grid1.indexed_iter().nth(2), Some(((0, 4, 3), 4.0))); - assert_eq!(grid1.indexed_iter().nth(3), Some(((0, 7, 1), 8.0))); + assert_eq!(grid1.indexed_iter().next(), Some((vec![0, 1, 2], 1.0))); + assert_eq!(grid1.indexed_iter().nth(1), Some((vec![0, 1, 3], 2.0))); + assert_eq!(grid1.indexed_iter().nth(2), Some((vec![0, 4, 3], 4.0))); + assert_eq!(grid1.indexed_iter().nth(3), Some((vec![0, 7, 1], 8.0))); // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( @@ -303,10 +309,10 @@ mod tests { x.array[[0, 1, 7]] = 8.0; } - assert_eq!(grid2.indexed_iter().next(), Some(((0, 1, 7), 8.0))); - assert_eq!(grid2.indexed_iter().nth(1), Some(((0, 2, 1), 1.0))); - assert_eq!(grid2.indexed_iter().nth(2), Some(((0, 3, 1), 2.0))); - assert_eq!(grid2.indexed_iter().nth(3), Some(((0, 3, 4), 4.0))); + assert_eq!(grid2.indexed_iter().next(), Some((vec![0, 1, 7], 8.0))); + assert_eq!(grid2.indexed_iter().nth(1), Some((vec![0, 2, 1], 1.0))); + assert_eq!(grid2.indexed_iter().nth(2), Some((vec![0, 3, 1], 2.0))); + assert_eq!(grid2.indexed_iter().nth(3), Some((vec![0, 3, 4], 4.0))); grid1.merge(&mut grid2, false); diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 8a9ce375..83417e9c 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -116,4 +116,4 @@ pub trait Subgrid { /// Type to iterate over the non-zero contents of a subgrid. The tuple contains the indices of the /// `mu2_grid`, the `x1_grid` and finally the `x2_grid`. -pub type SubgridIndexedIter<'a> = Box + 'a>; +pub type SubgridIndexedIter<'a> = Box, f64)> + 'a>; diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 72cc0147..b377a3ac 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -301,7 +301,10 @@ pub fn convert_into_applgrid( let mut weightgrid = ffi::igrid_weightgrid(igrid.pin_mut(), lumi); - for ((iq2, ix1, ix2), value) in subgrid.indexed_iter() { + for (indices, value) in subgrid.indexed_iter() { + let &[iq2, ix1, ix2] = indices.as_slice() else { + unimplemented!() + }; let appl_q2_idx = appl_q2_idx[iq2]; if appl_q2_idx == -1 { diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 97864a36..97f0362c 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -1,7 +1,7 @@ use super::GlobalConfiguration; use anyhow::{anyhow, ensure, Context, Error, Result}; use lhapdf::{Pdf, PdfSet}; -use ndarray::Array3; +use ndarray::{Array3, Ix3}; use pineappl::convolutions::LumiCache; use pineappl::grid::Grid; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; @@ -467,7 +467,11 @@ pub fn convolve_subgrid( ), }; - grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)) + let subgrid = grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)); + subgrid + .into_dimensionality::() + .map_err(|_| anyhow!("Only 3-dimensional subgrids are supported",)) + .unwrap() } pub fn parse_integer_range(range: &str) -> Result> { From 2a3dc7371278733349a293138cc94563d0be0802 Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 17 Sep 2024 15:05:28 +0200 Subject: [PATCH 115/213] Generalize `Subgrid::symmetrize` --- pineappl/src/empty_subgrid.rs | 4 ++-- pineappl/src/grid.rs | 2 +- pineappl/src/lagrange_subgrid.rs | 10 +++++----- pineappl/src/packed_subgrid.rs | 12 ++++++------ pineappl/src/subgrid.rs | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 1dbeb23b..0093b7bc 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -40,7 +40,7 @@ impl Subgrid for EmptySubgridV1 { fn scale(&mut self, _: f64) {} - fn symmetrize(&mut self) {} + fn symmetrize(&mut self, _: usize, _: usize) {} fn indexed_iter(&self) -> SubgridIndexedIter { Box::new(iter::empty()) @@ -72,7 +72,7 @@ mod tests { assert!(subgrid.is_empty()); subgrid.merge(&mut EmptySubgridV1.into(), false); subgrid.scale(2.0); - subgrid.symmetrize(); + subgrid.symmetrize(1, 2); assert_eq!( subgrid.stats(), Stats { diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index cb0c2dc0..9c721257 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -997,7 +997,7 @@ impl Grid { .iter_mut() .for_each(|subgrid| { if !subgrid.is_empty() && (subgrid.x1_grid() == subgrid.x2_grid()) { - subgrid.symmetrize(); + subgrid.symmetrize(1, 2); } }); } else if let Some((j, &other_index)) = indices diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index ea3e30b8..229858cd 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -86,16 +86,16 @@ impl Subgrid for LagrangeSubgridV2 { self.array *= factor; } - fn symmetrize(&mut self) { + fn symmetrize(&mut self, a: usize, b: usize) { let mut new_array = PackedArray::new(self.array.shape()); - for (mut index, sigma) in self.array.indexed_iter::<3>() { + for (mut index, sigma) in self.array.indexed_iter3() { // TODO: why not the other way around? - if index[2] < index[1] { - index.swap(1, 2); + if index[b] < index[a] { + index.swap(a, b); } - new_array[index] += sigma; + new_array[index.as_slice()] += sigma; } self.array = new_array; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 736937d7..ac297cc7 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -140,16 +140,16 @@ impl Subgrid for PackedQ1X2SubgridV1 { self.array *= factor; } - fn symmetrize(&mut self) { + fn symmetrize(&mut self, a: usize, b: usize) { let mut new_array = PackedArray::new(self.array.shape()); - for (mut index, sigma) in self.array.indexed_iter::<3>() { + for (mut index, sigma) in self.array.indexed_iter3() { // TODO: why not the other way around? - if index[2] < index[1] { - index.swap(1, 2); + if index[b] < index[a] { + index.swap(a, b); } - new_array[index] += sigma; + new_array[index.as_slice()] += sigma; } self.array = new_array; @@ -318,7 +318,7 @@ mod tests { // the luminosity function is symmetric, so after symmetrization the result must be // unchanged - grid1.symmetrize(); + grid1.symmetrize(1, 2); grid1.scale(2.0); diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 83417e9c..ca14a454 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -83,9 +83,9 @@ pub trait Subgrid { /// Scale the subgrid by `factor`. fn scale(&mut self, factor: f64); - /// Assumes that the initial states for this grid are the same and uses this to optimize the - /// grid by getting rid of almost half of the entries. - fn symmetrize(&mut self); + /// Assume that the convolution functions for indices `a` and `b` for this grid are the same + /// and use this to optimize the size of the grid. + fn symmetrize(&mut self, a: usize, b: usize); /// Return an iterator over all non-zero elements of the subgrid. fn indexed_iter(&self) -> SubgridIndexedIter; From 9135e119ae944a0796e7b4f3839b16b58030130c Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 17 Sep 2024 16:28:37 +0200 Subject: [PATCH 116/213] Add new type `NodeValues` and use it --- pineappl/src/empty_subgrid.rs | 8 +- pineappl/src/grid.rs | 8 +- pineappl/src/lagrange_subgrid.rs | 16 +-- pineappl/src/packed_subgrid.rs | 170 ++++++++++++++++++------------- pineappl/src/subgrid.rs | 66 +++++++++++- 5 files changed, 183 insertions(+), 85 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 0093b7bc..95a7cdb8 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -1,7 +1,7 @@ //! TODO use super::interpolation::Interp; -use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::iter; @@ -27,11 +27,15 @@ impl Subgrid for EmptySubgridV1 { Cow::Borrowed(&[]) } + fn node_values(&self) -> Vec { + Vec::new() + } + fn is_empty(&self) -> bool { true } - fn merge(&mut self, subgrid: &SubgridEnum, _: bool) { + fn merge(&mut self, subgrid: &SubgridEnum, _: Option<(usize, usize)>) { assert!( subgrid.is_empty(), "EmptySubgridV1 doesn't support the merge operation for non-empty subgrids" diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 9c721257..89e9736e 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -515,7 +515,7 @@ impl Grid { if new_subgrid.is_empty() { mem::swap(new_subgrid, subgrid); } else { - new_subgrid.merge(subgrid, false); + new_subgrid.merge(subgrid, None); } } else { let new_bin = if bin > bins.start { @@ -633,7 +633,7 @@ impl Grid { if self.subgrids[[self_i, self_j, self_k]].is_empty() { mem::swap(&mut self.subgrids[[self_i, self_j, self_k]], subgrid); } else { - self.subgrids[[self_i, self_j, self_k]].merge(&mut *subgrid, false); + self.subgrids[[self_i, self_j, self_k]].merge(&mut *subgrid, None); } } @@ -936,7 +936,7 @@ impl Grid { // we can't merge into an EmptySubgridV1 *lhs = mem::replace(rhs, EmptySubgridV1.into()); } else { - lhs.merge(rhs, false); + lhs.merge(rhs, None); *rhs = EmptySubgridV1.into(); } } @@ -1020,7 +1020,7 @@ impl Grid { // transpose `lhs` todo!(); } else { - lhs.merge(rhs, true); + lhs.merge(rhs, Some((1, 2))); *rhs = EmptySubgridV1.into(); } } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 229858cd..5b9a2d6d 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -2,7 +2,7 @@ use super::interpolation::{self, Interp}; use super::packed_array::PackedArray; -use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use float_cmp::approx_eq; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -63,19 +63,23 @@ impl Subgrid for LagrangeSubgridV2 { self.interps[2].node_values().into() } + fn node_values(&self) -> Vec { + vec![NodeValues::UseFromGrid; self.interps.len()] + } + fn is_empty(&self) -> bool { self.array.is_empty() } - fn merge(&mut self, other: &SubgridEnum, transpose: bool) { + fn merge(&mut self, other: &SubgridEnum, transpose: Option<(usize, usize)>) { // we cannot use `Self::indexed_iter` because it multiplies with `reweight` if let SubgridEnum::LagrangeSubgridV2(other) = other { // TODO: make sure `other` has the same interpolation as `self` - for (mut index, value) in other.array.indexed_iter::<3>() { - if transpose { - index.swap(1, 2); + for (mut index, value) in other.array.indexed_iter3() { + if let Some((a, b)) = transpose { + index.swap(a, b); } - self.array[index] += value; + self.array[index.as_slice()] += value; } } else { unimplemented!(); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index ac297cc7..1e9fd955 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -2,7 +2,8 @@ use super::interpolation::Interp; use super::packed_array::PackedArray; -use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use float_cmp::assert_approx_eq; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::mem; @@ -51,88 +52,115 @@ impl Subgrid for PackedQ1X2SubgridV1 { Cow::Borrowed(&self.x2_grid) } + fn node_values(&self) -> Vec { + vec![ + NodeValues::UseThese( + self.mu2_grid + .iter() + .map(|&Mu2 { ren, fac, frg }| { + assert_approx_eq!(f64, ren, fac, ulps = 4); + assert_approx_eq!(f64, frg, -1.0, ulps = 4); + fac + }) + .collect(), + ), + NodeValues::UseThese(self.x1_grid.clone()), + NodeValues::UseThese(self.x2_grid.clone()), + ] + } + fn is_empty(&self) -> bool { self.array.is_empty() } - fn merge(&mut self, other: &SubgridEnum, transpose: bool) { - let rhs_mu2 = other.mu2_grid().into_owned(); - let rhs_x1 = if transpose { - other.x2_grid() - } else { - other.x1_grid() - }; - let rhs_x2 = if transpose { - other.x1_grid() - } else { - other.x2_grid() - }; - - if (self.mu2_grid != rhs_mu2) || (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { - let mut mu2_grid = self.mu2_grid.clone(); - let mut x1_grid = self.x1_grid.clone(); - let mut x2_grid = self.x2_grid.clone(); - - mu2_grid.extend_from_slice(&rhs_mu2); - mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - mu2_grid.dedup(); - x1_grid.extend_from_slice(&rhs_x1); - x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x1_grid.dedup(); - x2_grid.extend_from_slice(&rhs_x2); - x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x2_grid.dedup(); - - let mut array = PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - - for ([i, j, k], value) in self.array.indexed_iter() { - let target_i = mu2_grid - .iter() - .position(|mu2| *mu2 == self.mu2_grid[i]) - .unwrap_or_else(|| unreachable!()); - let target_j = x1_grid + fn merge(&mut self, other: &SubgridEnum, transpose: Option<(usize, usize)>) { + // let rhs_mu2 = other.mu2_grid().into_owned(); + // let rhs_x1 = if transpose { + // other.x2_grid() + // } else { + // other.x1_grid() + // }; + // let rhs_x2 = if transpose { + // other.x1_grid() + // } else { + // other.x2_grid() + // }; + let lhs_node_values = self.node_values(); + let mut rhs_node_values = other.node_values(); + let mut new_node_values = lhs_node_values.clone(); + if let Some((a, b)) = transpose { + rhs_node_values.swap(a, b); + } + + if lhs_node_values != rhs_node_values { + for (lhs, rhs) in new_node_values.iter_mut().zip(rhs_node_values) { + lhs.extend(&rhs); + } + + let mut array = PackedArray::new( + &new_node_values .iter() - .position(|&x| x == self.x1_grid[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = x2_grid + .map(NodeValues::len) + .collect::>(), + ); + + for (indices, value) in self.array.indexed_iter3() { + // let target_i = mu2_grid + // .iter() + // .position(|mu2| *mu2 == self.mu2_grid[i]) + // .unwrap_or_else(|| unreachable!()); + // let target_j = x1_grid + // .iter() + // .position(|&x| x == self.x1_grid[j]) + // .unwrap_or_else(|| unreachable!()); + // let target_k = x2_grid + // .iter() + // .position(|&x| x == self.x2_grid[k]) + // .unwrap_or_else(|| unreachable!()); + let target: Vec<_> = indices .iter() - .position(|&x| x == self.x2_grid[k]) - .unwrap_or_else(|| unreachable!()); + .zip(&new_node_values) + .zip(&lhs_node_values) + .map(|((&i, new_values), old_values)| new_values.find(old_values.get(i)).unwrap()) + .collect(); - array[[target_i, target_j, target_k]] = value; + array[target.as_slice()] = value; } self.array = array; - self.mu2_grid = mu2_grid; - self.x1_grid = x1_grid; - self.x2_grid = x2_grid; + todo!(); } - for (indices, value) in other.indexed_iter() { - assert_eq!(indices.len(), 3); - let (j, k) = if transpose { - (indices[2], indices[1]) - } else { - (indices[1], indices[2]) - }; - let i = indices[0]; - let target_i = self - .mu2_grid - .iter() - .position(|x| *x == rhs_mu2[i]) - .unwrap_or_else(|| unreachable!()); - let target_j = self - .x1_grid - .iter() - .position(|&x| x == rhs_x1[j]) - .unwrap_or_else(|| unreachable!()); - let target_k = self - .x2_grid - .iter() - .position(|&x| x == rhs_x2[k]) - .unwrap_or_else(|| unreachable!()); - - self.array[[target_i, target_j, target_k]] += value; + for (mut indices, value) in other.indexed_iter() { + if let Some((a, b)) = transpose { + indices.swap(a, b); + } + // let i = indices[0]; + // let target_i = self + // .mu2_grid + // .iter() + // .position(|x| *x == rhs_mu2[i]) + // .unwrap_or_else(|| unreachable!()); + // let target_j = self + // .x1_grid + // .iter() + // .position(|&x| x == rhs_x1[j]) + // .unwrap_or_else(|| unreachable!()); + // let target_k = self + // .x2_grid + // .iter() + // .position(|&x| x == rhs_x2[k]) + // .unwrap_or_else(|| unreachable!()); + + // self.array[[target_i, target_j, target_k]] += value; + let target: Vec<_> = indices + .iter() + .zip(&new_node_values) + .zip(&rhs_node_values) + .map(|((&i, new_values), old_values)| new_values.find(old_values.get(i)).unwrap()) + .collect(); + + self.array[target.as_slice()] += value; } } diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index ca14a454..74049562 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,14 +1,72 @@ //! Module containing the trait `Subgrid` and supporting structs. +use float_cmp::approx_eq; use super::empty_subgrid::EmptySubgridV1; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; // use ndarray::Array3; +use super::evolution::EVOLVE_INFO_TOL_ULPS; use super::interpolation::Interp; use serde::{Deserialize, Serialize}; use std::borrow::Cow; +/// TODO +#[derive(Clone)] +pub enum NodeValues { + /// TODO + UseFromGrid, + /// TODO + UseThese(Vec), +} + +impl NodeValues { + pub fn extend(&mut self, other: &Self) { + match (self, other) { + (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (), + (NodeValues::UseThese(a), NodeValues::UseThese(b)) => a.extend_from_slice(b), + _ => unimplemented!(), + } + } + + pub fn len(&self) -> usize { + match self { + NodeValues::UseFromGrid => unimplemented!(), + NodeValues::UseThese(a) => a.len(), + } + } + + pub fn find(&self, value: f64) -> Option { + match self { + NodeValues::UseFromGrid => unimplemented!(), + NodeValues::UseThese(a) => a + .iter() + .position(|&x| approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS)), + } + } + + pub fn get(&self, index: usize) -> f64 { + match self { + NodeValues::UseFromGrid => unimplemented!(), + NodeValues::UseThese(a) => a[index], + } + } +} + +impl PartialEq for NodeValues { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, + (NodeValues::UseThese(a), NodeValues::UseThese(b)) => a + .iter() + .zip(b) + .all(|(&a, &b)| approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS)), + // TODO: the remaining cases could still be the same, but we don't know the values from `UseFromGrid`. + _ => false, + } + } +} + /// Enum which lists all possible `Subgrid` variants possible. #[enum_dispatch(Subgrid)] #[derive(Clone, Deserialize, Serialize)] @@ -69,6 +127,9 @@ pub trait Subgrid { /// return an empty slice. fn x2_grid(&self) -> Cow<[f64]>; + /// TODO + fn node_values(&self) -> Vec; + /// Fill the subgrid with `weight` that is being interpolated with `interps` using the /// kinematic information in `ntuple`. The parameter `ntuple` assumes the same ordering given /// by `kinematics` in [`Grid::new`] that was used to create the grid. @@ -77,8 +138,9 @@ pub trait Subgrid { /// Returns true if `fill` was never called for this grid. fn is_empty(&self) -> bool; - /// Merges `other` into this subgrid. - fn merge(&mut self, other: &SubgridEnum, transpose: bool); + /// Merge `other` into this subgrid, possibly transposing the two dimensions given by + /// `transpose`. + fn merge(&mut self, other: &SubgridEnum, transpose: Option<(usize, usize)>); /// Scale the subgrid by `factor`. fn scale(&mut self, factor: f64); From 3fac121d3b44e50e6832b92529d95e34dcb1fa86 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 18 Sep 2024 13:21:48 +0200 Subject: [PATCH 117/213] Fix compilation problems and some bugs --- pineappl/src/empty_subgrid.rs | 2 +- pineappl/src/packed_subgrid.rs | 2 +- pineappl/src/subgrid.rs | 30 ++++++++++++++++++++---------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 95a7cdb8..4efb917a 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -74,7 +74,7 @@ mod tests { fn create_empty() { let mut subgrid = EmptySubgridV1; assert!(subgrid.is_empty()); - subgrid.merge(&mut EmptySubgridV1.into(), false); + subgrid.merge(&mut EmptySubgridV1.into(), None); subgrid.scale(2.0); subgrid.symmetrize(1, 2); assert_eq!( diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 1e9fd955..6ffb633a 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -342,7 +342,7 @@ mod tests { assert_eq!(grid2.indexed_iter().nth(2), Some((vec![0, 3, 1], 2.0))); assert_eq!(grid2.indexed_iter().nth(3), Some((vec![0, 3, 4], 4.0))); - grid1.merge(&mut grid2, false); + grid1.merge(&mut grid2, None); // the luminosity function is symmetric, so after symmetrization the result must be // unchanged diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 74049562..bf408543 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,12 +1,12 @@ //! Module containing the trait `Subgrid` and supporting structs. -use float_cmp::approx_eq; use super::empty_subgrid::EmptySubgridV1; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; +// use float_cmp::approx_eq; // use ndarray::Array3; -use super::evolution::EVOLVE_INFO_TOL_ULPS; +// use super::evolution::EVOLVE_INFO_TOL_ULPS; use super::interpolation::Interp; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -21,14 +21,21 @@ pub enum NodeValues { } impl NodeValues { + /// TODO pub fn extend(&mut self, other: &Self) { match (self, other) { (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (), - (NodeValues::UseThese(a), NodeValues::UseThese(b)) => a.extend_from_slice(b), + (NodeValues::UseThese(a), NodeValues::UseThese(b)) => { + a.extend_from_slice(b); + a.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap()); + // TODO: use some tolerance + a.dedup(); + } _ => unimplemented!(), } } + /// TODO pub fn len(&self) -> usize { match self { NodeValues::UseFromGrid => unimplemented!(), @@ -36,15 +43,17 @@ impl NodeValues { } } + /// TODO pub fn find(&self, value: f64) -> Option { match self { NodeValues::UseFromGrid => unimplemented!(), - NodeValues::UseThese(a) => a - .iter() - .position(|&x| approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS)), + NodeValues::UseThese(a) => a.iter().position(|&x| + // approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS) + x == value), } } + /// TODO pub fn get(&self, index: usize) -> f64 { match self { NodeValues::UseFromGrid => unimplemented!(), @@ -57,10 +66,11 @@ impl PartialEq for NodeValues { fn eq(&self, other: &Self) -> bool { match (self, other) { (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, - (NodeValues::UseThese(a), NodeValues::UseThese(b)) => a - .iter() - .zip(b) - .all(|(&a, &b)| approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS)), + (NodeValues::UseThese(a), NodeValues::UseThese(b)) => a.iter().zip(b).all( + // TODO: use some tolerance + |(&a, &b)| a == b, + //approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS) + ), // TODO: the remaining cases could still be the same, but we don't know the values from `UseFromGrid`. _ => false, } From 3efc12b3b0ab4d3a27df7e2ca6259a9d0d09a3e7 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 18 Sep 2024 13:25:59 +0200 Subject: [PATCH 118/213] Change argument type of `PackedArray`'s constructor --- pineappl/src/interpolation.rs | 6 +++--- pineappl/src/lagrange_subgrid.rs | 4 ++-- pineappl/src/packed_array.rs | 32 ++++++++++++++--------------- pineappl/src/packed_subgrid.rs | 10 ++++----- pineappl/src/v0.rs | 2 +- pineappl_cli/src/import/applgrid.rs | 2 +- pineappl_cli/src/import/fastnlo.rs | 4 ++-- pineappl_cli/src/import/fktable.rs | 4 ++-- 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 93f27a4e..6f7d3f63 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -461,7 +461,7 @@ mod tests { assert_approx_eq!(f64, node, ref_node, ulps = 4); } - let mut array = crate::packed_array::PackedArray::::new(&[40, 50, 50]); + let mut array = crate::packed_array::PackedArray::::new(vec![40, 50, 50]); let ntuples = [[100000.0, 0.25, 0.5], [1000.0, 0.5, 0.5]]; let weight = 1.0; @@ -637,7 +637,7 @@ mod tests { InterpMeth::Lagrange, ), ]; - let mut array = crate::packed_array::PackedArray::::new(&[40, 50, 50]); + let mut array = crate::packed_array::PackedArray::::new(vec![40, 50, 50]); let ntuple = [1000.0, 0.5, 0.5]; let weight = 0.0; @@ -667,7 +667,7 @@ mod tests { Map::ApplGridH0, InterpMeth::Lagrange, )]; - let mut array = crate::packed_array::PackedArray::::new(&[1]); + let mut array = crate::packed_array::PackedArray::::new(vec![1]); let ntuple = [90.0_f64.powi(2)]; let weight = 1.0; diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 5b9a2d6d..c4be39df 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -21,7 +21,7 @@ impl LagrangeSubgridV2 { #[must_use] pub fn new(interps: &[Interp]) -> Self { Self { - array: PackedArray::new(&interps.iter().map(Interp::nodes).collect::>()), + array: PackedArray::new(interps.iter().map(Interp::nodes).collect()), interps: interps.to_vec(), static_q2: 0.0, } @@ -91,7 +91,7 @@ impl Subgrid for LagrangeSubgridV2 { } fn symmetrize(&mut self, a: usize, b: usize) { - let mut new_array = PackedArray::new(self.array.shape()); + let mut new_array = PackedArray::new(self.array.shape().to_vec()); for (mut index, sigma) in self.array.indexed_iter3() { // TODO: why not the other way around? diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 12db6f63..ef63663a 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -26,12 +26,12 @@ pub struct PackedArray { impl PackedArray { /// Constructs a new and empty `PackedArray` of shape `shape`. #[must_use] - pub fn new(shape: &[usize]) -> Self { + pub fn new(shape: Vec) -> Self { Self { entries: vec![], start_indices: vec![], lengths: vec![], - shape: shape.to_vec(), + shape, } } @@ -158,7 +158,7 @@ impl PackedArray { pub fn from_ndarray(array: ArrayView3, xstart: usize, xsize: usize) -> Self { let shape = array.shape(); - let mut result = Self::new(&[xsize, shape[1], shape[2]]); + let mut result = Self::new(vec![xsize, shape[1], shape[2]]); for ((i, j, k), &entry) in array .indexed_iter() @@ -556,7 +556,7 @@ mod tests { #[test] fn index() { - let mut a = PackedArray::new(&[4, 2]); + let mut a = PackedArray::new(vec![4, 2]); a[[0, 0]] = 1; assert_eq!(a[[0, 0]], 1); @@ -610,7 +610,7 @@ mod tests { #[test] fn iter() { - let mut a = PackedArray::new(&[6, 5]); + let mut a = PackedArray::new(vec![6, 5]); a[[2, 2]] = 1; a[[2, 4]] = 2; a[[4, 1]] = 3; @@ -630,7 +630,7 @@ mod tests { #[test] fn index_access() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); // after creation the array must be empty assert_eq!(array.overhead(), 0); @@ -775,7 +775,7 @@ mod tests { #[test] #[should_panic(expected = "index [40, 0, 50] is out of bounds for array of shape [40, 50, 50]")] fn index_mut_panic_dim0() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[40, 0, 50]] = 1.0; } @@ -783,7 +783,7 @@ mod tests { #[test] #[should_panic(expected = "index [0, 50, 0] is out of bounds for array of shape [40, 50, 50]")] fn index_mut_panic_dim1() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[0, 50, 0]] = 1.0; } @@ -791,7 +791,7 @@ mod tests { #[test] #[should_panic(expected = "index [0, 0, 50] is out of bounds for array of shape [40, 50, 50]")] fn index_mut_panic_dim2() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[0, 0, 50]] = 1.0; } @@ -799,7 +799,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [0, 0, 0] is implicitly set to the default value")] fn index_panic_dim0_0() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[1, 0, 0]] = 1; @@ -809,7 +809,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [2, 0, 0] is implicitly set to the default value")] fn index_panic_dim0_1() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[1, 0, 0]] = 1; @@ -819,7 +819,7 @@ mod tests { #[test] #[should_panic(expected = "index [1, 50, 0] is out of bounds for array of shape [40, 50, 50]")] fn index_panic_dim1() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[1, 0, 0]] = 1; @@ -829,7 +829,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [0, 0, 0] is implicitly set to the default value")] fn index_panic_dim2_0() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[0, 0, 1]] = 1; @@ -839,7 +839,7 @@ mod tests { #[test] #[should_panic(expected = "entry at index [0, 0, 2] is implicitly set to the default value")] fn index_panic_dim2_1() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[0, 0, 1]] = 1; @@ -848,7 +848,7 @@ mod tests { #[test] fn indexed_iter() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); // check shape assert_eq!(array.shape(), [40, 50, 50]); @@ -904,7 +904,7 @@ mod tests { #[test] fn clear() { - let mut array = PackedArray::new(&[40, 50, 50]); + let mut array = PackedArray::new(vec![40, 50, 50]); array[[3, 5, 1]] = 1; array[[7, 8, 9]] = 2; diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 6ffb633a..18b04d1b 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -169,7 +169,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn symmetrize(&mut self, a: usize, b: usize) { - let mut new_array = PackedArray::new(self.array.shape()); + let mut new_array = PackedArray::new(self.array.shape().to_vec()); for (mut index, sigma) in self.array.indexed_iter3() { // TODO: why not the other way around? @@ -231,7 +231,7 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { let x1_grid = subgrid.x1_grid()[x1_range.clone()].to_vec(); let x2_grid = subgrid.x2_grid()[x2_range.clone()].to_vec(); - let mut array = PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + let mut array = PackedArray::new(vec![mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for (indices, value) in subgrid.indexed_iter() { // if there's a static scale we want every value to be added to same grid point @@ -266,7 +266,7 @@ mod tests { #[should_panic(expected = "PackedQ1X2SubgridV1 doesn't support the fill operation")] fn fill_packed_q1x2_subgrid_v1() { let mut subgrid = PackedQ1X2SubgridV1::new( - PackedArray::new(&[0, 0, 0]), + PackedArray::new(vec![0, 0, 0]), Vec::new(), Vec::new(), Vec::new(), @@ -280,7 +280,7 @@ mod tests { 0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0, ]; let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( - PackedArray::new(&[1, 10, 10]), + PackedArray::new(vec![1, 10, 10]), vec![Mu2 { ren: 0.0, fac: 0.0, @@ -320,7 +320,7 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( - PackedArray::new(&[1, 10, 10]), + PackedArray::new(vec![1, 10, 10]), vec![Mu2 { ren: 1.0, fac: 1.0, diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 090f10e9..15632d32 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -72,7 +72,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result let x1_grid = subgrid.x1_grid().into_owned(); let x2_grid = subgrid.x2_grid().into_owned(); let mut array = - PackedArray::new(&[mu2_grid.len(), x1_grid.len(), x2_grid.len()]); + PackedArray::new(vec![mu2_grid.len(), x1_grid.len(), x2_grid.len()]); for (index, v) in subgrid.indexed_iter() { array[<[usize; 3]>::from(index)] = v; } diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 815f036b..3c56cef4 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -220,7 +220,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let matrix = unsafe { &*matrix }; let mut array = - PackedArray::new(&[mu2_values.len(), x1_values.len(), x2_values.len()]); + PackedArray::new(vec![mu2_values.len(), x1_values.len(), x2_values.len()]); for itau in 0..mu2_values.len() { for ix1 in 0..x1_values.len() { diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 3ca5ed01..ba234a2a 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -195,7 +195,7 @@ fn convert_coeff_add_fix( .collect(); let mut array = - PackedArray::new(&[mu2_values.len(), x1_values.len(), x2_values.len()]); + PackedArray::new(vec![mu2_values.len(), x1_values.len(), x2_values.len()]); // TODO: figure out what the general case is supposed to be assert_eq!(j, 0); @@ -370,7 +370,7 @@ fn convert_coeff_add_flex( let factor = rescale / table_as_add_base.GetNevt(obs.try_into().unwrap(), subproc); let mut arrays = vec![ - PackedArray::new(&[mu2_values.len(), x1_values.len(), x2_values.len()]); + PackedArray::new(vec![mu2_values.len(), x1_values.len(), x2_values.len()]); orders_len ]; diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index c631205e..32fd2e98 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -149,7 +149,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { grid = Some(fktable); - arrays = iter::repeat(PackedArray::new(&[1, nx1, nx2])) + arrays = iter::repeat(PackedArray::new(vec![1, nx1, nx2])) .take(flavor_mask.iter().filter(|&&value| value).count()) .collect(); } @@ -225,7 +225,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .into(); } - arrays = iter::repeat(PackedArray::new(&[1, nx1, nx2])) + arrays = iter::repeat(PackedArray::new(vec![1, nx1, nx2])) .take(flavor_mask.iter().filter(|&&value| value).count()) .collect(); last_bin = bin; From 74d9ae7ea108d743a2f1549084732159af2888d3 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 18 Sep 2024 13:52:52 +0200 Subject: [PATCH 119/213] Fix bugs in `PackedQ1X2SubgridV1::merge` --- pineappl/src/packed_subgrid.rs | 116 ++++++++++++++------------------- 1 file changed, 50 insertions(+), 66 deletions(-) diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 18b04d1b..c01c6efb 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -4,6 +4,7 @@ use super::interpolation::Interp; use super::packed_array::PackedArray; use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use float_cmp::assert_approx_eq; +use itertools::izip; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::mem; @@ -74,17 +75,6 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn merge(&mut self, other: &SubgridEnum, transpose: Option<(usize, usize)>) { - // let rhs_mu2 = other.mu2_grid().into_owned(); - // let rhs_x1 = if transpose { - // other.x2_grid() - // } else { - // other.x1_grid() - // }; - // let rhs_x2 = if transpose { - // other.x1_grid() - // } else { - // other.x2_grid() - // }; let lhs_node_values = self.node_values(); let mut rhs_node_values = other.node_values(); let mut new_node_values = lhs_node_values.clone(); @@ -92,75 +82,69 @@ impl Subgrid for PackedQ1X2SubgridV1 { rhs_node_values.swap(a, b); } - if lhs_node_values != rhs_node_values { - for (lhs, rhs) in new_node_values.iter_mut().zip(rhs_node_values) { - lhs.extend(&rhs); + // TODO: remove this block + assert!(matches!(transpose, None | Some((1, 2)))); + let rhs_mu2 = other.mu2_grid().into_owned(); + let rhs_x1 = if transpose.is_some() { + other.x2_grid() + } else { + other.x1_grid() + }; + let rhs_x2 = if transpose.is_some() { + other.x1_grid() + } else { + other.x2_grid() + }; + + if (self.mu2_grid != rhs_mu2) || (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { + // end block + for (lhs, rhs) in new_node_values.iter_mut().zip(&rhs_node_values) { + lhs.extend(rhs); } - let mut array = PackedArray::new( - &new_node_values - .iter() - .map(NodeValues::len) - .collect::>(), - ); + let mut array = PackedArray::new(new_node_values.iter().map(NodeValues::len).collect()); for (indices, value) in self.array.indexed_iter3() { - // let target_i = mu2_grid - // .iter() - // .position(|mu2| *mu2 == self.mu2_grid[i]) - // .unwrap_or_else(|| unreachable!()); - // let target_j = x1_grid - // .iter() - // .position(|&x| x == self.x1_grid[j]) - // .unwrap_or_else(|| unreachable!()); - // let target_k = x2_grid - // .iter() - // .position(|&x| x == self.x2_grid[k]) - // .unwrap_or_else(|| unreachable!()); - let target: Vec<_> = indices - .iter() - .zip(&new_node_values) - .zip(&lhs_node_values) - .map(|((&i, new_values), old_values)| new_values.find(old_values.get(i)).unwrap()) + let target: Vec<_> = izip!(indices, &new_node_values, &lhs_node_values) + .map(|(index, new, lhs)| new.find(lhs.get(index)).unwrap()) .collect(); array[target.as_slice()] = value; } self.array = array; - todo!(); + + // TODO: remove this block + let mut mu2_grid = self.mu2_grid.clone(); + let mut x1_grid = self.x1_grid.clone(); + let mut x2_grid = self.x2_grid.clone(); + + mu2_grid.extend_from_slice(&rhs_mu2); + mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + mu2_grid.dedup(); + x1_grid.extend_from_slice(&rhs_x1); + x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x1_grid.dedup(); + x2_grid.extend_from_slice(&rhs_x2); + x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); + x2_grid.dedup(); + // end block + + self.mu2_grid = mu2_grid; + self.x1_grid = x1_grid; + self.x2_grid = x2_grid; } for (mut indices, value) in other.indexed_iter() { if let Some((a, b)) = transpose { indices.swap(a, b); } - // let i = indices[0]; - // let target_i = self - // .mu2_grid - // .iter() - // .position(|x| *x == rhs_mu2[i]) - // .unwrap_or_else(|| unreachable!()); - // let target_j = self - // .x1_grid - // .iter() - // .position(|&x| x == rhs_x1[j]) - // .unwrap_or_else(|| unreachable!()); - // let target_k = self - // .x2_grid - // .iter() - // .position(|&x| x == rhs_x2[k]) - // .unwrap_or_else(|| unreachable!()); - - // self.array[[target_i, target_j, target_k]] += value; - let target: Vec<_> = indices - .iter() - .zip(&new_node_values) - .zip(&rhs_node_values) - .map(|((&i, new_values), old_values)| new_values.find(old_values.get(i)).unwrap()) - .collect(); - self.array[target.as_slice()] += value; + let target: Vec<_> = izip!(indices, &new_node_values, &rhs_node_values) + .map(|(index, new, rhs)| new.find(rhs.get(index)).unwrap()) + .collect(); + + self.array[target.as_slice()] += value; } } @@ -284,7 +268,7 @@ mod tests { vec![Mu2 { ren: 0.0, fac: 0.0, - frg: 0.0, + frg: -1.0, }], x.clone(), x.clone(), @@ -294,7 +278,7 @@ mod tests { let mu2 = vec![Mu2 { ren: 0.0, fac: 0.0, - frg: 0.0, + frg: -1.0, }]; assert_eq!(grid1.mu2_grid().as_ref(), mu2); @@ -324,7 +308,7 @@ mod tests { vec![Mu2 { ren: 1.0, fac: 1.0, - frg: 1.0, + frg: -1.0, }], x.clone(), x, From 5429a9f54d0eb4229c8b54fe23990cc05b419573 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 19 Sep 2024 14:43:39 +0200 Subject: [PATCH 120/213] Add initial support for flexible-scale grids --- pineappl/src/boc.rs | 45 ++++++---------- pineappl/src/grid.rs | 80 +++++++++++++++++++++++++---- pineappl/src/v0.rs | 11 +++- pineappl/tests/drell_yan_lo.rs | 11 +++- pineappl_capi/src/lib.rs | 9 +++- pineappl_cli/src/import/applgrid.rs | 9 +++- pineappl_cli/src/import/fastnlo.rs | 21 ++++++-- pineappl_cli/src/import/fktable.rs | 10 +++- 8 files changed, 144 insertions(+), 52 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 5afdb2e0..7e77df06 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -3,7 +3,6 @@ //! //! [`Grid`]: super::grid::Grid -use bitflags::bitflags; use float_cmp::approx_eq; use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -11,53 +10,43 @@ use std::cmp::Ordering; use std::str::FromStr; use thiserror::Error; -bitflags! { - /// TODO - #[derive(Clone, Copy, Deserialize, Eq, PartialEq, Serialize)] - #[repr(transparent)] - pub struct Scale: u32 { - /// TODO - const REN = 0b001; - /// TODO - const FAC = 0b010; - /// TODO - const FRG = 0b100; - } -} - /// TODO #[derive(Clone, Copy, Deserialize, Eq, PartialEq, Serialize)] pub enum Kinematics { /// TODO - Mu2(Scale), + Scale(usize), /// TODO X(usize), } impl Kinematics { /// TODO - pub const MU2_R: Self = Self::Mu2(Scale::REN); + pub const X1: Self = Self::X(0); /// TODO - pub const MU2_F: Self = Self::Mu2(Scale::FAC); + pub const X2: Self = Self::X(1); +} +/// TODO +#[derive(Clone, Deserialize, Serialize)] +pub enum ScaleFuncForm { /// TODO - pub const MU2_A: Self = Self::Mu2(Scale::FRG); - + NoScale, /// TODO - pub const MU2_RFA: Self = Self::Mu2(Scale::REN.union(Scale::FAC).union(Scale::FRG)); - + Scale(usize), /// TODO - pub const MU2_RF: Self = Self::Mu2(Scale::REN.union(Scale::FAC)); + QuadraticSum(usize, usize), +} +/// TODO +#[derive(Clone, Deserialize, Serialize)] +pub struct Scales { /// TODO - pub const X1: Self = Self::X(0); - + pub ren: ScaleFuncForm, /// TODO - pub const X2: Self = Self::X(1); - + pub fac: ScaleFuncForm, /// TODO - pub const X3: Self = Self::X(2); + pub frg: ScaleFuncForm, } /// Error type keeping information if [`Order::from_str`] went wrong. diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 89e9736e..4ebf414e 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1,7 +1,7 @@ //! Module containing all traits and supporting structures for grids. use super::bin::{BinInfo, BinLimits, BinRemapper}; -use super::boc::{Channel, Kinematics, Order}; +use super::boc::{Channel, Kinematics, Order, Scales}; use super::convolutions::{Convolution, LumiCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; @@ -130,6 +130,7 @@ pub struct Grid { pub(crate) kinematics: Vec, pub(crate) interps: Vec, pub(crate) remapper: Option, + pub(crate) scales: Scales, } impl Grid { @@ -148,6 +149,7 @@ impl Grid { convolutions: Vec, interps: Vec, kinematics: Vec, + scales: Scales, ) -> Self { for (channel_idx, channel) in channels.iter().enumerate() { let offending_entry = channel @@ -181,6 +183,7 @@ impl Grid { interps, kinematics, remapper: None, + scales, } } @@ -1221,6 +1224,8 @@ impl Grid { more_members: self.more_members.clone(), kinematics: self.kinematics.clone(), remapper: self.remapper.clone(), + // TODO: is this correct? + scales: self.scales.clone(), }; if let Some(lhs) = &mut lhs { @@ -1385,6 +1390,8 @@ impl Grid { more_members: self.more_members.clone(), kinematics: self.kinematics.clone(), remapper: self.remapper.clone(), + // TODO: is this correct? + scales: self.scales.clone(), }; assert_eq!(infos[0].pid_basis, infos[1].pid_basis); @@ -1639,6 +1646,7 @@ impl Grid { #[cfg(test)] mod tests { use super::*; + use crate::boc::ScaleFuncForm; use crate::channel; use std::fs::File; @@ -1654,7 +1662,12 @@ mod tests { vec![0.0, 1.0], vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); } @@ -1670,7 +1683,12 @@ mod tests { vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); assert_eq!(grid.bin_info().bins(), 4); @@ -1688,7 +1706,12 @@ mod tests { vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); // merging with empty subgrids should not change the grid @@ -1711,7 +1734,12 @@ mod tests { vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); assert_eq!(grid.bin_info().bins(), 4); @@ -1732,7 +1760,12 @@ mod tests { vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); other.fill(0, 0.1, 0, &[90.0_f64.powi(2), 0.1, 0.2], 1.0); @@ -1760,7 +1793,12 @@ mod tests { vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); assert_eq!(grid.bin_info().bins(), 4); @@ -1774,7 +1812,12 @@ mod tests { vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); // fill the photon-photon entry @@ -1799,7 +1842,12 @@ mod tests { vec![0.0, 0.25, 0.5], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); assert_eq!(grid.bin_info().bins(), 2); @@ -1817,7 +1865,12 @@ mod tests { vec![0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); other.fill(0, 0.1, 0, &[90.0_f64.powi(2), 0.1, 0.2], 2.0); @@ -1847,7 +1900,12 @@ mod tests { vec![0.0, 1.0], vec![Convolution::UnpolPDF(2212); 2], v0::default_interps(), - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); // by default we assume unpolarized proton PDFs are used diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 15632d32..287b1ee4 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -1,5 +1,5 @@ use super::bin::{BinLimits, BinRemapper}; -use super::boc::{Channel, Kinematics, Order}; +use super::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use super::convolutions::Convolution; use super::empty_subgrid::EmptySubgridV1; use super::grid::{Grid, GridError, Mmv4, MoreMembers}; @@ -134,8 +134,15 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result // and limits we should be able to do the same without error BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() }), - kinematics: vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + // TODO: read in flexible-scale grids properly + kinematics: vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], interps: default_interps(), + scales: Scales { + // TODO: read in flexible-scale grids properly + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, }; assert_eq!(result.bin_info().bins(), grid.bin_info().bins()); diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 6ebb2f82..43edaa49 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -3,7 +3,7 @@ use float_cmp::assert_approx_eq; use lhapdf::Pdf; use num_complex::Complex; use pineappl::bin::BinRemapper; -use pineappl::boc::{Kinematics, Order}; +use pineappl::boc::{Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; @@ -208,13 +208,19 @@ fn fill_drell_yan_lo_grid( let kinematics = vec![ // 1st dimension is factorization and at the same time also the renormalization scale - Kinematics::MU2_RF, + Kinematics::Scale(0), // 2nd dimension is the parton momentum fraction of the first convolution Kinematics::X1, // 3rd dimension is the parton momentum fraction of the second convolution Kinematics::X2, ]; + let scales = Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(1), + frg: ScaleFuncForm::NoScale, + }; + // create the PineAPPL grid let mut grid = Grid::new( // the integers in the channel definition are PDG Monte Carlo IDs @@ -225,6 +231,7 @@ fn fill_drell_yan_lo_grid( convolutions, interps, kinematics, + scales, ); // in GeV^2 pbarn diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index c6bba9b6..4a96c8a1 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -57,7 +57,7 @@ use itertools::izip; use pineappl::bin::BinRemapper; -use pineappl::boc::{Channel, Kinematics, Order}; +use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::{Convolution, LumiCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; @@ -753,7 +753,12 @@ pub unsafe extern "C" fn pineappl_grid_new( unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), convolutions, interps, - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, )) } diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 3c56cef4..24aaf9f9 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -1,6 +1,6 @@ use anyhow::Result; use lhapdf::Pdf; -use pineappl::boc::{Channel, Kinematics, Order}; +use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; @@ -171,7 +171,12 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul ), ], // TODO: change kinematics for DIS - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); for bin in 0..grid.Nobs_internal() { diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index ba234a2a..698e8d6b 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -2,7 +2,7 @@ use anyhow::Result; use itertools::Itertools; use ndarray::s; use pineappl::bin::BinRemapper; -use pineappl::boc::{Channel, Kinematics, Order}; +use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; @@ -160,7 +160,12 @@ fn convert_coeff_add_fix( ), ], // TODO: change kinematics for DIS - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); let total_scalenodes: usize = table.GetTotalScalenodes().try_into().unwrap(); @@ -340,7 +345,17 @@ fn convert_coeff_add_flex( ), ], // TODO: change kinematics for DIS - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![ + Kinematics::Scale(0), + Kinematics::Scale(1), + Kinematics::X1, + Kinematics::X2, + ], + Scales { + ren: todo!(), + fac: todo!(), + frg: ScaleFuncForm::NoScale, + }, ); let rescale = 0.1_f64.powi(table.GetIXsectUnits() - ipub_units); diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 32fd2e98..cc1dc7c9 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Context, Result}; use flate2::read::GzDecoder; use ndarray::s; -use pineappl::boc::{Kinematics, Order}; +use pineappl::boc::{Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; @@ -144,7 +144,13 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { ), ], // TODO: change kinematics for DIS - vec![Kinematics::MU2_RF, Kinematics::X1, Kinematics::X2], + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + // TODO: is this correct? + Scales { + ren: ScaleFuncForm::NoScale, + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ); grid = Some(fktable); From 876e23b13e3f4f6bc9c4b79f202627fcc00559ce Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 19 Sep 2024 14:44:00 +0200 Subject: [PATCH 121/213] Ignore flexible-scale grid tests for the time being --- pineappl_cli/tests/import.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index ef9a7459..dee894b8 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -335,6 +335,7 @@ fn import_fix_grid() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -353,6 +354,7 @@ fn import_flex_grid() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_scale_1() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -373,6 +375,7 @@ fn import_flex_grid_scale_1() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_scale_2() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -393,6 +396,7 @@ fn import_flex_grid_scale_2() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_quadratic_sum() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -415,6 +419,7 @@ fn import_flex_grid_quadratic_sum() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_quadratic_mean() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -437,6 +442,7 @@ fn import_flex_grid_quadratic_mean() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_5() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -457,6 +463,7 @@ fn import_flex_grid_5() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_6() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -477,6 +484,7 @@ fn import_flex_grid_6() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_7() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -497,6 +505,7 @@ fn import_flex_grid_7() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_8() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -517,6 +526,7 @@ fn import_flex_grid_8() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_9() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -537,6 +547,7 @@ fn import_flex_grid_9() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_10() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -557,6 +568,7 @@ fn import_flex_grid_10() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_11() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -577,6 +589,7 @@ fn import_flex_grid_11() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_12() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -597,6 +610,7 @@ fn import_flex_grid_12() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_13() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -617,6 +631,7 @@ fn import_flex_grid_13() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_14() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -637,6 +652,7 @@ fn import_flex_grid_14() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_flex_grid_15() { let output = NamedTempFile::new("converted2.pineappl.lz4").unwrap(); @@ -1250,6 +1266,7 @@ fn import_dis_applgrid() { } #[test] +#[ignore] #[cfg(feature = "fastnlo")] fn import_double_hadronic_fastnlo() { let output = NamedTempFile::new("converted9.pineappl.lz4").unwrap(); From 726ca36bc5e8b58d8293d8ed9552062874ee7cfb Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 19 Sep 2024 17:24:40 +0200 Subject: [PATCH 122/213] Remove `{mu2,x1,x2}_grid` members in `packed_subgrid.rs` --- pineappl/src/evolution.rs | 63 +++++-------- pineappl/src/packed_subgrid.rs | 137 +++++++++------------------- pineappl/src/subgrid.rs | 23 +++-- pineappl/src/v0.rs | 14 ++- pineappl_cli/src/import/applgrid.rs | 12 ++- pineappl_cli/src/import/fastnlo.rs | 20 ++-- pineappl_cli/src/import/fktable.rs | 32 +++---- 7 files changed, 129 insertions(+), 172 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index f2240227..ee0a384e 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -7,7 +7,7 @@ use super::grid::{Grid, GridError}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::PidBasis; -use super::subgrid::{Mu2, Subgrid, SubgridEnum}; +use super::subgrid::{Mu2, NodeValues, Subgrid, SubgridEnum}; use float_cmp::approx_eq; use itertools::izip; use itertools::Itertools; @@ -429,24 +429,19 @@ pub(crate) fn evolve_slice_with_one( 0, 1, ), - vec![Mu2 { - // TODO: FK tables don't depend on the renormalization scale - //ren: -1.0, - ren: info.fac0, - fac: info.fac0, - // TODO: implement evolution for non-zero fragmentation scales - frg: -1.0, - }], - if index == 0 { - info.x0.clone() - } else { - vec![1.0] - }, - if index == 0 { - vec![1.0] - } else { - info.x0.clone() - }, + vec![ + NodeValues::UseThese(vec![info.fac0]), + NodeValues::UseThese(if index == 0 { + info.x0.clone() + } else { + vec![1.0] + }), + NodeValues::UseThese(if index == 0 { + vec![1.0] + } else { + info.x0.clone() + }), + ], ) .into() })); @@ -574,16 +569,11 @@ pub(crate) fn evolve_slice_with_two( sub_fk_tables.extend(tables.into_iter().map(|table| { PackedQ1X2SubgridV1::new( PackedArray::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), - vec![Mu2 { - // TODO: FK tables don't depend on the renormalization scale - //ren: -1.0, - ren: info.fac0, - fac: info.fac0, - // TODO: implement evolution for non-zero fragmentation scales - frg: -1.0, - }], - info.x0.clone(), - info.x0.clone(), + vec![ + NodeValues::UseThese(vec![info.fac0]), + NodeValues::UseThese(info.x0.clone()), + NodeValues::UseThese(info.x0.clone()), + ], ) .into() })); @@ -724,16 +714,11 @@ pub(crate) fn evolve_slice_with_two2( sub_fk_tables.extend(tables.into_iter().map(|table| { PackedQ1X2SubgridV1::new( PackedArray::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), - vec![Mu2 { - // TODO: FK tables don't depend on the renormalization scale - //ren: -1.0, - ren: infos[0].fac0, - fac: infos[0].fac0, - // TODO: implement evolution for non-zero fragmentation scales - frg: -1.0, - }], - infos[0].x0.clone(), - infos[1].x0.clone(), + vec![ + NodeValues::UseThese(vec![infos[0].fac0]), + NodeValues::UseThese(infos[0].x0.clone()), + NodeValues::UseThese(infos[1].x0.clone()), + ], ) .into() })); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index c01c6efb..15ed154a 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -3,7 +3,6 @@ use super::interpolation::Interp; use super::packed_array::PackedArray; use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; -use float_cmp::assert_approx_eq; use itertools::izip; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -13,26 +12,14 @@ use std::mem; #[derive(Clone, Deserialize, Serialize)] pub struct PackedQ1X2SubgridV1 { array: PackedArray, - mu2_grid: Vec, - x1_grid: Vec, - x2_grid: Vec, + node_values: Vec, } impl PackedQ1X2SubgridV1 { /// Constructor. #[must_use] - pub const fn new( - array: PackedArray, - mu2_grid: Vec, - x1_grid: Vec, - x2_grid: Vec, - ) -> Self { - Self { - array, - mu2_grid, - x1_grid, - x2_grid, - } + pub const fn new(array: PackedArray, node_values: Vec) -> Self { + Self { array, node_values } } } @@ -42,32 +29,30 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn mu2_grid(&self) -> Cow<[Mu2]> { - Cow::Borrowed(&self.mu2_grid) + // Cow::Borrowed(&self.mu2_grid) + Cow::Owned( + self.node_values[0] + .values() + .into_iter() + .map(|ren| Mu2 { + ren, + fac: ren, + frg: -1.0, + }) + .collect(), + ) } fn x1_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&self.x1_grid) + Cow::Owned(self.node_values[1].values()) } fn x2_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&self.x2_grid) + Cow::Owned(self.node_values[2].values()) } fn node_values(&self) -> Vec { - vec![ - NodeValues::UseThese( - self.mu2_grid - .iter() - .map(|&Mu2 { ren, fac, frg }| { - assert_approx_eq!(f64, ren, fac, ulps = 4); - assert_approx_eq!(f64, frg, -1.0, ulps = 4); - fac - }) - .collect(), - ), - NodeValues::UseThese(self.x1_grid.clone()), - NodeValues::UseThese(self.x2_grid.clone()), - ] + self.node_values.clone() } fn is_empty(&self) -> bool { @@ -82,22 +67,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { rhs_node_values.swap(a, b); } - // TODO: remove this block - assert!(matches!(transpose, None | Some((1, 2)))); - let rhs_mu2 = other.mu2_grid().into_owned(); - let rhs_x1 = if transpose.is_some() { - other.x2_grid() - } else { - other.x1_grid() - }; - let rhs_x2 = if transpose.is_some() { - other.x1_grid() - } else { - other.x2_grid() - }; - - if (self.mu2_grid != rhs_mu2) || (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { - // end block + if self.node_values() != rhs_node_values { for (lhs, rhs) in new_node_values.iter_mut().zip(&rhs_node_values) { lhs.extend(rhs); } @@ -113,26 +83,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } self.array = array; - - // TODO: remove this block - let mut mu2_grid = self.mu2_grid.clone(); - let mut x1_grid = self.x1_grid.clone(); - let mut x2_grid = self.x2_grid.clone(); - - mu2_grid.extend_from_slice(&rhs_mu2); - mu2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - mu2_grid.dedup(); - x1_grid.extend_from_slice(&rhs_x1); - x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x1_grid.dedup(); - x2_grid.extend_from_slice(&rhs_x2); - x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); - x2_grid.dedup(); - // end block - - self.mu2_grid = mu2_grid; - self.x1_grid = x1_grid; - self.x2_grid = x2_grid; + self.node_values = new_node_values.clone(); } for (mut indices, value) in other.indexed_iter() { @@ -182,8 +133,12 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn static_scale(&self) -> Option { - if let [static_scale] = self.mu2_grid.as_slice() { - Some(static_scale.clone()) + if let &[static_scale] = self.node_values()[0].values().as_slice() { + Some(Mu2 { + ren: static_scale, + fac: static_scale, + frg: -1.0, + }) } else { None } @@ -232,12 +187,14 @@ impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { ]] += value; } - Self { + Self::new( array, - mu2_grid, - x1_grid, - x2_grid, - } + vec![ + NodeValues::UseThese(mu2_grid.iter().map(|mu2| mu2.ren).collect()), + NodeValues::UseThese(x1_grid), + NodeValues::UseThese(x2_grid), + ], + ) } } @@ -251,9 +208,7 @@ mod tests { fn fill_packed_q1x2_subgrid_v1() { let mut subgrid = PackedQ1X2SubgridV1::new( PackedArray::new(vec![0, 0, 0]), - Vec::new(), - Vec::new(), - Vec::new(), + vec![NodeValues::UseThese(Vec::new()); 3], ); subgrid.fill(&v0::default_interps(), &[0.0; 3], 0.0); } @@ -265,13 +220,11 @@ mod tests { ]; let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new(vec![1, 10, 10]), - vec![Mu2 { - ren: 0.0, - fac: 0.0, - frg: -1.0, - }], - x.clone(), - x.clone(), + vec![ + NodeValues::UseThese(vec![0.0]), + NodeValues::UseThese(x.clone()), + NodeValues::UseThese(x.clone()), + ], ) .into(); @@ -305,13 +258,11 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( PackedArray::new(vec![1, 10, 10]), - vec![Mu2 { - ren: 1.0, - fac: 1.0, - frg: -1.0, - }], - x.clone(), - x, + vec![ + NodeValues::UseThese(vec![1.0]), + NodeValues::UseThese(x.clone()), + NodeValues::UseThese(x.clone()), + ], ) .into(); if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid2 { diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index bf408543..4ff6c548 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; /// TODO -#[derive(Clone)] +#[derive(Clone, Deserialize, Serialize)] pub enum NodeValues { /// TODO UseFromGrid, @@ -60,17 +60,28 @@ impl NodeValues { NodeValues::UseThese(a) => a[index], } } + + /// TODO + pub fn values(&self) -> Vec { + match self { + NodeValues::UseFromGrid => unimplemented!(), + NodeValues::UseThese(a) => a.clone(), + } + } } impl PartialEq for NodeValues { fn eq(&self, other: &Self) -> bool { match (self, other) { (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, - (NodeValues::UseThese(a), NodeValues::UseThese(b)) => a.iter().zip(b).all( - // TODO: use some tolerance - |(&a, &b)| a == b, - //approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS) - ), + (NodeValues::UseThese(a), NodeValues::UseThese(b)) => { + (a.len() == b.len()) + && a.iter().zip(b).all( + // TODO: use some tolerance + |(&a, &b)| a == b, + //approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS) + ) + } // TODO: the remaining cases could still be the same, but we don't know the values from `UseFromGrid`. _ => false, } diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 287b1ee4..98931ee3 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -7,7 +7,7 @@ use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::PidBasis; -use super::subgrid::Mu2; +use super::subgrid::{Mu2, NodeValues}; use ndarray::Array3; use pineappl_v0::grid::Grid as GridV0; use std::io::BufRead; @@ -76,7 +76,17 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result for (index, v) in subgrid.indexed_iter() { array[<[usize; 3]>::from(index)] = v; } - PackedQ1X2SubgridV1::new(array, mu2_grid, x1_grid, x2_grid).into() + PackedQ1X2SubgridV1::new( + array, + vec![ + NodeValues::UseThese( + mu2_grid.iter().map(|&Mu2 { ren, .. }| ren).collect(), + ), + NodeValues::UseThese(x1_grid), + NodeValues::UseThese(x2_grid), + ], + ) + .into() } }) .collect(), diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 24aaf9f9..53b5a076 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -7,7 +7,7 @@ use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::pids::PidBasis; -use pineappl::subgrid::Mu2; +use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::pin::Pin; @@ -248,9 +248,13 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul pgrid.subgrids_mut()[[0, bin.try_into().unwrap(), lumi]] = PackedQ1X2SubgridV1::new( array, - mu2_values.clone(), - x1_values.clone(), - x2_values.clone(), + vec![ + NodeValues::UseThese( + mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), + ), + NodeValues::UseThese(x1_values.clone()), + NodeValues::UseThese(x2_values.clone()), + ], ) .into(); } diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 698e8d6b..9badc975 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -9,7 +9,7 @@ use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::pids::PidBasis; -use pineappl::subgrid::Mu2; +use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_fastnlo::ffi::{ self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF, fastNLOPDFLinearCombinations, EScaleFunctionalForm, @@ -248,9 +248,13 @@ fn convert_coeff_add_fix( [[0, obs.try_into().unwrap(), subproc.try_into().unwrap()]] = PackedQ1X2SubgridV1::new( array, - mu2_values, - x1_values.clone(), - x2_values.clone(), + vec![ + NodeValues::UseThese( + mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), + ), + NodeValues::UseThese(x1_values.clone()), + NodeValues::UseThese(x2_values.clone()), + ], ) .into(); } @@ -453,13 +457,7 @@ fn convert_coeff_add_flex( continue; } - *subgrid = PackedQ1X2SubgridV1::new( - array, - mu2_values.clone(), - x1_values.clone(), - x2_values.clone(), - ) - .into(); + *subgrid = PackedQ1X2SubgridV1::new(array, todo!()).into(); } } } diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index cc1dc7c9..c1b2d747 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -9,7 +9,7 @@ use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pineappl::pids::PidBasis; -use pineappl::subgrid::Mu2; +use pineappl::subgrid::NodeValues; use std::fs::File; use std::io::{BufRead, BufReader}; use std::iter; @@ -219,14 +219,15 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { { *subgrid = PackedQ1X2SubgridV1::new( array, - // TODO: remove the renormalization scale - vec![Mu2 { - ren: q0 * q0, - fac: q0 * q0, - frg: -1.0, - }], - x_grid.clone(), - if hadronic { x_grid.clone() } else { vec![1.0] }, + vec![ + NodeValues::UseThese(vec![q0 * q0]), + NodeValues::UseThese(x_grid.clone()), + NodeValues::UseThese(if hadronic { + x_grid.clone() + } else { + vec![1.0] + }), + ], ) .into(); } @@ -280,14 +281,11 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { { *subgrid = PackedQ1X2SubgridV1::new( array, - // TODO: remove the renormalization scale - vec![Mu2 { - ren: q0 * q0, - fac: q0 * q0, - frg: -1.0, - }], - x_grid.clone(), - if hadronic { x_grid.clone() } else { vec![1.0] }, + vec![ + NodeValues::UseThese(vec![q0 * q0]), + NodeValues::UseThese(x_grid.clone()), + NodeValues::UseThese(if hadronic { x_grid.clone() } else { vec![1.0] }), + ], ) .into(); } From aa4a097fb7f465d662a1ab8ee8a1d240dd2da59b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 20 Sep 2024 10:00:07 +0200 Subject: [PATCH 123/213] Silence warnings --- pineappl_cli/src/import/fastnlo.rs | 398 +++++++++++++++-------------- 1 file changed, 200 insertions(+), 198 deletions(-) diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 9badc975..708efefc 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -1,6 +1,6 @@ use anyhow::Result; -use itertools::Itertools; -use ndarray::s; +// use itertools::Itertools; +// use ndarray::s; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::Convolution; @@ -266,203 +266,205 @@ fn convert_coeff_add_fix( } fn convert_coeff_add_flex( - table: &fastNLOCoeffAddFlex, - comb: &fastNLOPDFLinearCombinations, - mur_ff: EScaleFunctionalForm, - muf_ff: EScaleFunctionalForm, - bins: usize, - alpha: u32, - ipub_units: i32, - dis_pid: i32, + _table: &fastNLOCoeffAddFlex, + _comb: &fastNLOPDFLinearCombinations, + _mur_ff: EScaleFunctionalForm, + _muf_ff: EScaleFunctionalForm, + _bins: usize, + _alpha: u32, + _ipub_units: i32, + _dis_pid: i32, ) -> Grid { - let table_as_add_base = ffi::downcast_coeff_add_flex_to_base(table); - - let alphas = table_as_add_base.GetNpow().try_into().unwrap(); - let orders: Vec<_> = [ - Order::new(alphas, alpha, 0, 0, 0), - Order::new(alphas, alpha, 1, 0, 0), - Order::new(alphas, alpha, 0, 1, 0), - Order::new(alphas, alpha, 2, 0, 0), - Order::new(alphas, alpha, 0, 2, 0), - Order::new(alphas, alpha, 1, 1, 0), - ] - .into_iter() - .take(match table.GetNScaleDep() { - 0..=4 => 1, - 5 => 3, - 6 => 4, - 7 => 6, - _ => unimplemented!(), - }) - .collect(); - let orders_len = orders.len(); - - let npdf = table_as_add_base.GetNPDF(); - assert!(npdf <= 2); - - let convolutions = (0..2) - .map(|index| { - if index < npdf { - Convolution::UnpolPDF(table.GetPDFPDG(index)) - } else { - Convolution::None - } - }) - .collect(); - - let mut grid = Grid::new( - PidBasis::Pdg, - reconstruct_channels(table_as_add_base, comb, dis_pid), - orders, - (0..=bins) - .map(|limit| u16::try_from(limit).unwrap().into()) - .collect(), - convolutions, - // TODO: read out interpolation parameters from fastNLO - vec![ - Interp::new( - 1e2, - 1e8, - 40, - 3, - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - ], - // TODO: change kinematics for DIS - vec![ - Kinematics::Scale(0), - Kinematics::Scale(1), - Kinematics::X1, - Kinematics::X2, - ], - Scales { - ren: todo!(), - fac: todo!(), - frg: ScaleFuncForm::NoScale, - }, - ); - - let rescale = 0.1_f64.powi(table.GetIXsectUnits() - ipub_units); - - for obs in 0..bins { - let scale_nodes1 = ffi::GetScaleNodes1(table, obs.try_into().unwrap()); - let scale_nodes2 = ffi::GetScaleNodes2(table, obs.try_into().unwrap()); - let x1_values = ffi::GetXNodes1(table_as_add_base, obs.try_into().unwrap()); - let x2_values = if npdf > 1 { - ffi::GetXNodes2(table_as_add_base, obs.try_into().unwrap()) - } else { - vec![1.0] - }; - - let mu2_values: Vec<_> = scale_nodes1 - .iter() - .cartesian_product(scale_nodes2.iter()) - .map(|(&s1, &s2)| Mu2 { - ren: mur_ff.compute_scale(s1, s2), - fac: muf_ff.compute_scale(s1, s2), - frg: -1.0, - }) - .collect(); - let nx = ffi::GetNx(table, obs); - - for subproc in 0..table_as_add_base.GetNSubproc() { - let factor = rescale / table_as_add_base.GetNevt(obs.try_into().unwrap(), subproc); - let mut arrays = - vec![ - PackedArray::new(vec![mu2_values.len(), x1_values.len(), x2_values.len()]); - orders_len - ]; - - for (mu2_slice, (is1, is2)) in (0..scale_nodes1.len()) - .cartesian_product(0..scale_nodes2.len()) - .enumerate() - { - let logmur2 = mu2_values[mu2_slice].ren.ln(); - let logmuf2 = mu2_values[mu2_slice].fac.ln(); - let logs00 = [ - logmur2, - logmuf2, - logmur2 * logmur2, - logmuf2 * logmuf2, - logmur2 * logmuf2, - ]; - let logs10 = [2.0 * logmur2, 0.0, logmuf2]; - let logs01 = [0.0, 2.0 * logmuf2, logmur2]; - - for ix in 0..nx { - // TODO: is this always correct? Isn't there a member function for it? - let ix1 = ix % x1_values.len(); - let ix2 = ix / x1_values.len(); - let mut values = [0.0; 6]; - - for (index, value) in values.iter_mut().enumerate().take(orders_len) { - *value = ffi::GetSigmaTilde(table, index, obs, ix, is1, is2, subproc); - } - - values[0] += values[1..] - .iter() - .zip(logs00.iter()) - .map(|(value, log)| value * log) - .sum::(); - values[1] += values[3..] - .iter() - .zip(logs10.iter()) - .map(|(value, log)| value * log) - .sum::(); - values[2] += values[3..] - .iter() - .zip(logs01.iter()) - .map(|(value, log)| value * log) - .sum::(); - - for (value, array) in values - .iter() - .copied() - .zip(arrays.iter_mut()) - .filter(|(value, _)| *value != 0.0) - { - array[[mu2_slice, ix1, ix2]] = - value * factor * x1_values[ix1] * x2_values[ix2]; - } - } - } - - for (subgrid, array) in grid - .subgrids_mut() - .slice_mut(s![.., obs, usize::try_from(subproc).unwrap()]) - .iter_mut() - .zip(arrays.into_iter()) - { - if array.is_empty() { - continue; - } - - *subgrid = PackedQ1X2SubgridV1::new(array, todo!()).into(); - } - } - } - - grid + todo!() + + // let table_as_add_base = ffi::downcast_coeff_add_flex_to_base(table); + + // let alphas = table_as_add_base.GetNpow().try_into().unwrap(); + // let orders: Vec<_> = [ + // Order::new(alphas, alpha, 0, 0, 0), + // Order::new(alphas, alpha, 1, 0, 0), + // Order::new(alphas, alpha, 0, 1, 0), + // Order::new(alphas, alpha, 2, 0, 0), + // Order::new(alphas, alpha, 0, 2, 0), + // Order::new(alphas, alpha, 1, 1, 0), + // ] + // .into_iter() + // .take(match table.GetNScaleDep() { + // 0..=4 => 1, + // 5 => 3, + // 6 => 4, + // 7 => 6, + // _ => unimplemented!(), + // }) + // .collect(); + // let orders_len = orders.len(); + + // let npdf = table_as_add_base.GetNPDF(); + // assert!(npdf <= 2); + + // let convolutions = (0..2) + // .map(|index| { + // if index < npdf { + // Convolution::UnpolPDF(table.GetPDFPDG(index)) + // } else { + // Convolution::None + // } + // }) + // .collect(); + + // let mut grid = Grid::new( + // PidBasis::Pdg, + // reconstruct_channels(table_as_add_base, comb, dis_pid), + // orders, + // (0..=bins) + // .map(|limit| u16::try_from(limit).unwrap().into()) + // .collect(), + // convolutions, + // // TODO: read out interpolation parameters from fastNLO + // vec![ + // Interp::new( + // 1e2, + // 1e8, + // 40, + // 3, + // ReweightMeth::NoReweight, + // Map::ApplGridH0, + // InterpMeth::Lagrange, + // ), + // Interp::new( + // 2e-7, + // 1.0, + // 50, + // 3, + // ReweightMeth::ApplGridX, + // Map::ApplGridF2, + // InterpMeth::Lagrange, + // ), + // Interp::new( + // 2e-7, + // 1.0, + // 50, + // 3, + // ReweightMeth::ApplGridX, + // Map::ApplGridF2, + // InterpMeth::Lagrange, + // ), + // ], + // // TODO: change kinematics for DIS + // vec![ + // Kinematics::Scale(0), + // Kinematics::Scale(1), + // Kinematics::X1, + // Kinematics::X2, + // ], + // Scales { + // ren: todo!(), + // fac: todo!(), + // frg: ScaleFuncForm::NoScale, + // }, + // ); + + // let rescale = 0.1_f64.powi(table.GetIXsectUnits() - ipub_units); + + // for obs in 0..bins { + // let scale_nodes1 = ffi::GetScaleNodes1(table, obs.try_into().unwrap()); + // let scale_nodes2 = ffi::GetScaleNodes2(table, obs.try_into().unwrap()); + // let x1_values = ffi::GetXNodes1(table_as_add_base, obs.try_into().unwrap()); + // let x2_values = if npdf > 1 { + // ffi::GetXNodes2(table_as_add_base, obs.try_into().unwrap()) + // } else { + // vec![1.0] + // }; + + // let mu2_values: Vec<_> = scale_nodes1 + // .iter() + // .cartesian_product(scale_nodes2.iter()) + // .map(|(&s1, &s2)| Mu2 { + // ren: mur_ff.compute_scale(s1, s2), + // fac: muf_ff.compute_scale(s1, s2), + // frg: -1.0, + // }) + // .collect(); + // let nx = ffi::GetNx(table, obs); + + // for subproc in 0..table_as_add_base.GetNSubproc() { + // let factor = rescale / table_as_add_base.GetNevt(obs.try_into().unwrap(), subproc); + // let mut arrays = + // vec![ + // PackedArray::new(vec![mu2_values.len(), x1_values.len(), x2_values.len()]); + // orders_len + // ]; + + // for (mu2_slice, (is1, is2)) in (0..scale_nodes1.len()) + // .cartesian_product(0..scale_nodes2.len()) + // .enumerate() + // { + // let logmur2 = mu2_values[mu2_slice].ren.ln(); + // let logmuf2 = mu2_values[mu2_slice].fac.ln(); + // let logs00 = [ + // logmur2, + // logmuf2, + // logmur2 * logmur2, + // logmuf2 * logmuf2, + // logmur2 * logmuf2, + // ]; + // let logs10 = [2.0 * logmur2, 0.0, logmuf2]; + // let logs01 = [0.0, 2.0 * logmuf2, logmur2]; + + // for ix in 0..nx { + // // TODO: is this always correct? Isn't there a member function for it? + // let ix1 = ix % x1_values.len(); + // let ix2 = ix / x1_values.len(); + // let mut values = [0.0; 6]; + + // for (index, value) in values.iter_mut().enumerate().take(orders_len) { + // *value = ffi::GetSigmaTilde(table, index, obs, ix, is1, is2, subproc); + // } + + // values[0] += values[1..] + // .iter() + // .zip(logs00.iter()) + // .map(|(value, log)| value * log) + // .sum::(); + // values[1] += values[3..] + // .iter() + // .zip(logs10.iter()) + // .map(|(value, log)| value * log) + // .sum::(); + // values[2] += values[3..] + // .iter() + // .zip(logs01.iter()) + // .map(|(value, log)| value * log) + // .sum::(); + + // for (value, array) in values + // .iter() + // .copied() + // .zip(arrays.iter_mut()) + // .filter(|(value, _)| *value != 0.0) + // { + // array[[mu2_slice, ix1, ix2]] = + // value * factor * x1_values[ix1] * x2_values[ix2]; + // } + // } + // } + + // for (subgrid, array) in grid + // .subgrids_mut() + // .slice_mut(s![.., obs, usize::try_from(subproc).unwrap()]) + // .iter_mut() + // .zip(arrays.into_iter()) + // { + // if array.is_empty() { + // continue; + // } + + // *subgrid = PackedQ1X2SubgridV1::new(array, todo!()).into(); + // } + // } + // } + + // grid } pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> Result { From 763906a4927eedf2ebad1aee32386572bb70d864 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 20 Sep 2024 10:23:14 +0200 Subject: [PATCH 124/213] Add new methods `ScaleFuncForm::calc` and `Scales::compatible_with` --- pineappl/src/boc.rs | 40 ++++++++++++++++++++++++++++++++++ pineappl/src/grid.rs | 10 +++++++-- pineappl/tests/drell_yan_lo.rs | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 7e77df06..701650a1 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -3,6 +3,7 @@ //! //! [`Grid`]: super::grid::Grid +use super::subgrid::NodeValues; use float_cmp::approx_eq; use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -38,6 +39,24 @@ pub enum ScaleFuncForm { QuadraticSum(usize, usize), } +impl ScaleFuncForm { + /// TODO + pub fn calc(&self, node_values: &[NodeValues], kinematics: &[Kinematics]) -> Option> { + match self { + ScaleFuncForm::NoScale => None, + &ScaleFuncForm::Scale(index) => Some( + node_values[kinematics + .iter() + .position(|&kin| kin == Kinematics::Scale(index)) + // UNWRAP: this should be guaranteed by `Grid::new` + .unwrap()] + .values(), + ), + ScaleFuncForm::QuadraticSum(_, _) => todo!(), + } + } +} + /// TODO #[derive(Clone, Deserialize, Serialize)] pub struct Scales { @@ -49,6 +68,27 @@ pub struct Scales { pub frg: ScaleFuncForm, } +impl Scales { + /// TODO + pub fn compatible_with(&self, kinematics: &[Kinematics]) -> bool { + for scale in [&self.ren, &self.fac, &self.frg].map(Clone::clone) { + match scale { + ScaleFuncForm::NoScale => {} + ScaleFuncForm::Scale(index) + if kinematics + .iter() + .any(|&kin| kin == Kinematics::Scale(index)) => {} + ScaleFuncForm::QuadraticSum(idx1, idx2) + if kinematics.iter().any(|&kin| kin == Kinematics::Scale(idx1)) + && kinematics.iter().any(|&kin| kin == Kinematics::Scale(idx2)) => {} + _ => return false, + } + } + + true + } +} + /// Error type keeping information if [`Order::from_str`] went wrong. #[derive(Debug, Error, Eq, PartialEq)] #[error("{0}")] diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 4ebf414e..b00677fc 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -138,8 +138,9 @@ impl Grid { /// /// # Panics /// - /// Panics when the number of PIDs in `channels` is not equal to `convolutions.len()`, or if - /// `interps` and `kinematics` have different lengths. + /// Panics when the number of PIDs in `channels` is not equal to `convolutions.len()`, or + /// `interps` and `kinematics` have different lengths or if `kinematics` are not compatible + /// with `scales`. #[must_use] pub fn new( pid_basis: PidBasis, @@ -168,6 +169,11 @@ impl Grid { "interps and kinematics have different lengths" ); + assert!( + scales.compatible_with(&kinematics), + "scales and kinematics are not compatible" + ); + Self { subgrids: Array3::from_shape_simple_fn( (orders.len(), bin_limits.len() - 1, channels.len()), diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 43edaa49..7d44f5c9 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -217,7 +217,7 @@ fn fill_drell_yan_lo_grid( let scales = Scales { ren: ScaleFuncForm::Scale(0), - fac: ScaleFuncForm::Scale(1), + fac: ScaleFuncForm::Scale(0), frg: ScaleFuncForm::NoScale, }; From b225b0c41b28391d9b5019a77efc7c974cfd810e Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 20 Sep 2024 11:24:39 +0200 Subject: [PATCH 125/213] Remove last calls to `{mu2,x1,x2}_grid` in subgrid types --- pineappl/src/lagrange_subgrid.rs | 5 +- pineappl/src/packed_subgrid.rs | 112 +++++++++++++++++-------------- pineappl/src/subgrid.rs | 24 +++---- 3 files changed, 78 insertions(+), 63 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index c4be39df..8a311dd8 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -64,7 +64,10 @@ impl Subgrid for LagrangeSubgridV2 { } fn node_values(&self) -> Vec { - vec![NodeValues::UseFromGrid; self.interps.len()] + self.interps + .iter() + .map(|interp| NodeValues::UseThese(interp.node_values().to_vec())) + .collect() } fn is_empty(&self) -> bool { diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 15ed154a..05edaf74 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -148,53 +148,58 @@ impl Subgrid for PackedQ1X2SubgridV1 { impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { fn from(subgrid: &SubgridEnum) -> Self { // find smallest ranges - let (mu2_range, x1_range, x2_range) = subgrid.indexed_iter().fold( - ( - subgrid.mu2_grid().len()..0, - subgrid.x1_grid().len()..0, - subgrid.x2_grid().len()..0, - ), - |prev, (indices, _)| { - ( - prev.0.start.min(indices[0])..prev.0.end.max(indices[0] + 1), - prev.1.start.min(indices[1])..prev.1.end.max(indices[1] + 1), - prev.2.start.min(indices[2])..prev.2.end.max(indices[2] + 1), - ) + let ranges: Vec<_> = subgrid.indexed_iter().fold( + subgrid + .node_values() + .iter() + .map(|values| values.values().len()..0) + .collect(), + |mut prev, (indices, _)| { + for (i, index) in indices.iter().enumerate() { + prev[i].start = prev[i].start.min(*index); + prev[i].end = prev[i].end.max(*index + 1); + } + prev }, ); - let (mu2_grid, static_scale) = subgrid.static_scale().map_or_else( - || (subgrid.mu2_grid()[mu2_range.clone()].to_vec(), false), - |scale| (vec![scale], true), + let mut new_node_values: Vec<_> = subgrid + .node_values() + .iter() + .zip(&ranges) + .map(|(values, range)| NodeValues::UseThese(values.values()[range.clone()].to_vec())) + .collect(); + let static_scale = if let Some(Mu2 { ren, fac, frg }) = subgrid.static_scale() { + assert_eq!(ren, fac); + assert_eq!(frg, -1.0); + new_node_values[0] = NodeValues::UseThese(vec![fac]); + true + } else { + false + }; + + let mut array = PackedArray::new( + new_node_values + .iter() + .map(|values| values.values().len()) + .collect(), ); - let x1_grid = subgrid.x1_grid()[x1_range.clone()].to_vec(); - let x2_grid = subgrid.x2_grid()[x2_range.clone()].to_vec(); - - let mut array = PackedArray::new(vec![mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - - for (indices, value) in subgrid.indexed_iter() { - // if there's a static scale we want every value to be added to same grid point - let index = if static_scale { - 0 - } else { - indices[0] - mu2_range.start - }; - - array[[ - index, - indices[1] - x1_range.start, - indices[2] - x2_range.start, - ]] += value; + + for (mut indices, value) in subgrid.indexed_iter() { + for (idx, (index, range)) in indices.iter_mut().zip(&ranges).enumerate() { + // TODO: generalize static scale detection + *index = if static_scale && idx == 0 { + // if there's a static scale we want every value to be added to same grid point + 0 + } else { + *index - range.start + }; + } + + array[indices.as_slice()] += value; } - Self::new( - array, - vec![ - NodeValues::UseThese(mu2_grid.iter().map(|mu2| mu2.ren).collect()), - NodeValues::UseThese(x1_grid), - NodeValues::UseThese(x2_grid), - ], - ) + Self::new(array, new_node_values) } } @@ -228,15 +233,24 @@ mod tests { ) .into(); - let mu2 = vec![Mu2 { - ren: 0.0, - fac: 0.0, - frg: -1.0, - }]; + // let mu2 = vec![Mu2 { + // ren: 0.0, + // fac: 0.0, + // frg: -1.0, + // }]; - assert_eq!(grid1.mu2_grid().as_ref(), mu2); - assert_eq!(grid1.x1_grid().as_ref(), x); - assert_eq!(grid1.x2_grid(), grid1.x1_grid()); + // assert_eq!(grid1.mu2_grid().as_ref(), mu2); + // assert_eq!(grid1.x1_grid().as_ref(), x); + // assert_eq!(grid1.x2_grid(), grid1.x1_grid()); + + assert_eq!( + grid1.node_values(), + vec![ + NodeValues::UseThese(vec![0.0]), + NodeValues::UseThese(x.clone()), + NodeValues::UseThese(x.clone()) + ] + ); assert!(grid1.is_empty()); diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 4ff6c548..23d770bf 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -12,10 +12,10 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; /// TODO -#[derive(Clone, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub enum NodeValues { /// TODO - UseFromGrid, + // UseFromGrid, /// TODO UseThese(Vec), } @@ -24,21 +24,20 @@ impl NodeValues { /// TODO pub fn extend(&mut self, other: &Self) { match (self, other) { - (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (), + // (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (), (NodeValues::UseThese(a), NodeValues::UseThese(b)) => { a.extend_from_slice(b); a.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap()); // TODO: use some tolerance a.dedup(); - } - _ => unimplemented!(), + } // _ => unimplemented!(), } } /// TODO pub fn len(&self) -> usize { match self { - NodeValues::UseFromGrid => unimplemented!(), + // NodeValues::UseFromGrid => unimplemented!(), NodeValues::UseThese(a) => a.len(), } } @@ -46,7 +45,7 @@ impl NodeValues { /// TODO pub fn find(&self, value: f64) -> Option { match self { - NodeValues::UseFromGrid => unimplemented!(), + // NodeValues::UseFromGrid => unimplemented!(), NodeValues::UseThese(a) => a.iter().position(|&x| // approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS) x == value), @@ -56,7 +55,7 @@ impl NodeValues { /// TODO pub fn get(&self, index: usize) -> f64 { match self { - NodeValues::UseFromGrid => unimplemented!(), + // NodeValues::UseFromGrid => unimplemented!(), NodeValues::UseThese(a) => a[index], } } @@ -64,7 +63,7 @@ impl NodeValues { /// TODO pub fn values(&self) -> Vec { match self { - NodeValues::UseFromGrid => unimplemented!(), + // NodeValues::UseFromGrid => unimplemented!(), NodeValues::UseThese(a) => a.clone(), } } @@ -73,7 +72,7 @@ impl NodeValues { impl PartialEq for NodeValues { fn eq(&self, other: &Self) -> bool { match (self, other) { - (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, + // (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, (NodeValues::UseThese(a), NodeValues::UseThese(b)) => { (a.len() == b.len()) && a.iter().zip(b).all( @@ -81,9 +80,8 @@ impl PartialEq for NodeValues { |(&a, &b)| a == b, //approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS) ) - } - // TODO: the remaining cases could still be the same, but we don't know the values from `UseFromGrid`. - _ => false, + } // TODO: the remaining cases could still be the same, but we don't know the values from `UseFromGrid`. + // _ => false, } } } From e51c1d1a82f8a1df1a4315bf1f8562f9bff0ddf0 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 20 Sep 2024 12:00:28 +0200 Subject: [PATCH 126/213] Migrate `AlphasTable::from_grid` to `NodeValues` --- pineappl/src/boc.rs | 9 ++++++--- pineappl/src/evolution.rs | 12 +++++++----- pineappl/src/grid.rs | 10 ++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 701650a1..362e075d 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -44,14 +44,17 @@ impl ScaleFuncForm { pub fn calc(&self, node_values: &[NodeValues], kinematics: &[Kinematics]) -> Option> { match self { ScaleFuncForm::NoScale => None, - &ScaleFuncForm::Scale(index) => Some( + &ScaleFuncForm::Scale(index) => Some(if node_values.is_empty() { + // TODO: empty subgrid should have as many node values as dimensions + Vec::new() + } else { node_values[kinematics .iter() .position(|&kin| kin == Kinematics::Scale(index)) // UNWRAP: this should be guaranteed by `Grid::new` .unwrap()] - .values(), - ), + .values() + }), ScaleFuncForm::QuadraticSum(_, _) => todo!(), } } diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index ee0a384e..6cf282b7 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -89,11 +89,13 @@ impl AlphasTable { .subgrids() .iter() .flat_map(|subgrid| { - subgrid - .mu2_grid() - .iter() - .map(|Mu2 { ren, .. }| xir * xir * ren) - .collect::>() + grid.scales() + .ren + .calc(&subgrid.node_values(), grid.kinematics()) + // UNWRAP: grids with no renormalization scales should not call this function + .unwrap() + .into_iter() + .map(|ren| xir * xir * ren) }) .collect(); // UNWRAP: if we can't sort numbers the grid is fishy diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index b00677fc..60b95ee6 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -204,6 +204,16 @@ impl Grid { &mut self.pid_basis } + /// TODO + pub fn kinematics(&self) -> &[Kinematics] { + &self.kinematics + } + + /// TODO + pub fn scales(&self) -> &Scales { + &self.scales + } + /// Perform a convolution using the PDFs and strong coupling in `lumi_cache`, and /// selecting only the orders, bins and channels corresponding to `order_mask`, `bin_indices` /// and `channel_mask`. A variation of the scales is performed using the factors in `xi`; the From 7ad6515bb08963e11404a202dce7512f99eb1607 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 20 Sep 2024 13:42:38 +0200 Subject: [PATCH 127/213] Remove two references to `x{1,2}_grid` in the evolution --- pineappl/src/evolution.rs | 87 +++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 6cf282b7..8121603b 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -1,6 +1,6 @@ //! Supporting classes and functions for [`Grid::evolve_with_slice_iter`]. -use super::boc::{Channel, Order}; +use super::boc::{Channel, Kinematics, Order}; use super::channel; use super::convolutions::Convolution; use super::grid::{Grid, GridError}; @@ -216,37 +216,44 @@ type X1aX1bOp2Tuple = (Vec>, Option>); fn ndarray_from_subgrid_orders_slice( fac1: f64, + kinematics: &[Kinematics], subgrids: &ArrayView1, orders: &[Order], order_mask: &[bool], (xir, xif): (f64, f64), alphas_table: &AlphasTable, ) -> Result { - // TODO: skip empty subgrids - - let mut x1_a: Vec<_> = subgrids - .iter() - .enumerate() - .filter(|(index, _)| order_mask.get(*index).copied().unwrap_or(true)) - .flat_map(|(_, subgrid)| subgrid.x1_grid().into_owned()) - .collect(); - let mut x1_b: Vec<_> = subgrids + // create a Vec of all x values for each dimension + let mut x1n: Vec<_> = kinematics .iter() .enumerate() - .filter(|(index, _)| order_mask.get(*index).copied().unwrap_or(true)) - .flat_map(|(_, subgrid)| subgrid.x2_grid().into_owned()) + .filter_map(|(idx, kin)| matches!(kin, Kinematics::X(_)).then_some(idx)) + .map(|kin_idx| { + subgrids + .iter() + .enumerate() + .filter(|&(ord_idx, subgrid)| { + order_mask.get(ord_idx).copied().unwrap_or(true) + // TODO: empty subgrids don't have node values + && !subgrid.is_empty() + }) + .flat_map(|(_, subgrid)| subgrid.node_values()[kin_idx].values()) + .collect::>() + }) .collect(); - x1_a.sort_by(f64::total_cmp); - x1_a.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLUTION_TOL_ULPS)); - x1_b.sort_by(f64::total_cmp); - x1_b.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLUTION_TOL_ULPS)); + for x1 in &mut x1n { + x1.sort_by(f64::total_cmp); + x1.dedup_by(|&mut a, &mut b| approx_eq!(f64, a, b, ulps = EVOLUTION_TOL_ULPS)); + } - let mut array = Array2::::zeros((x1_a.len(), x1_b.len())); + // TODO: lift this restriction + assert_eq!(x1n.len(), 2); + + let mut array = Array2::::zeros((x1n[0].len(), x1n[1].len())); let mut zero = true; - // add subgrids for different orders, but the same bin and lumi, using the right - // couplings + // for the same bin and channel, sum subgrids of different orders, using the right couplings for (subgrid, order) in subgrids .iter() .zip(orders.iter()) @@ -273,29 +280,24 @@ fn ndarray_from_subgrid_orders_slice( logs *= (xif * xif).ln(); } - // TODO: use `try_collect` once stabilized - let xa_indices: Vec<_> = subgrid - .x1_grid() + let x1_indices: Vec> = kinematics .iter() - .map(|&xa| { - x1_a.iter() - .position(|&x1a| approx_eq!(f64, x1a, xa, ulps = EVOLUTION_TOL_ULPS)) - .ok_or_else(|| { - GridError::EvolutionFailure(format!("no operator for x1 = {xa} found")) - }) - }) - .collect::>()?; - let xb_indices: Vec<_> = subgrid - .x2_grid() - .iter() - .map(|&xb| { - x1_b.iter() - .position(|&x1b| approx_eq!(f64, x1b, xb, ulps = EVOLUTION_TOL_ULPS)) - .ok_or_else(|| { - GridError::EvolutionFailure(format!("no operator for x1 = {xb} found")) + .enumerate() + .filter_map(|(idx, kin)| matches!(kin, Kinematics::X(_)).then_some(idx)) + .zip(&x1n) + .map(|(kin_idx, x1)| { + subgrid.node_values()[kin_idx] + .values() + .into_iter() + .map(|xs| { + x1.iter() + .position(|&x| approx_eq!(f64, x, xs, ulps = EVOLUTION_TOL_ULPS)) + // UNWRAP: `x1n` contains all x-values, so we must find each `x` + .unwrap() }) + .collect() }) - .collect::>()?; + .collect(); for (indices, value) in subgrid.indexed_iter() { let &[ifac1, ix1, ix2] = indices.as_slice() else { @@ -331,11 +333,11 @@ fn ndarray_from_subgrid_orders_slice( zero = false; - array[[xa_indices[ix1], xb_indices[ix2]]] += als * logs * value; + array[[x1_indices[0][ix1], x1_indices[1][ix2]]] += als * logs * value; } } - Ok((vec![x1_a, x1_b], (!zero).then_some(array))) + Ok((x1n, (!zero).then_some(array))) } // TODO: merge this method into evolve_slice_with_two @@ -374,6 +376,7 @@ pub(crate) fn evolve_slice_with_one( for (subgrids_o, channel1) in subgrids_ol.axis_iter(Axis(1)).zip(grid.channels()) { let (mut x1, array) = ndarray_from_subgrid_orders_slice( info.fac1, + grid.kinematics(), &subgrids_o, grid.orders(), order_mask, @@ -513,6 +516,7 @@ pub(crate) fn evolve_slice_with_two( for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { let (x1, array) = ndarray_from_subgrid_orders_slice( info.fac1, + grid.kinematics(), &subgrids_o, grid.orders(), order_mask, @@ -651,6 +655,7 @@ pub(crate) fn evolve_slice_with_two2( for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { let (x1, array) = ndarray_from_subgrid_orders_slice( fac1, + grid.kinematics(), &subgrids_o, grid.orders(), order_mask, From a8e02ed7935f8c13cd5ff06d8850a5eeedc86381 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 20 Sep 2024 16:04:26 +0200 Subject: [PATCH 128/213] Migrate `grid.rs` to `NodeValues` --- pineappl/src/grid.rs | 141 +++++++++++++++++++++++++++---------------- 1 file changed, 90 insertions(+), 51 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 60b95ee6..4b338aee 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -15,6 +15,7 @@ use super::v0; use bitflags::bitflags; use float_cmp::{approx_eq, assert_approx_eq}; use git_version::git_version; +use itertools::Itertools; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; use ndarray::{s, Array3, ArrayD, ArrayView3, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; use serde::{Deserialize, Serialize}; @@ -273,17 +274,25 @@ impl Grid { let channel = &pdg_channels[chan]; let mu2_grid = subgrid.mu2_grid(); - let x1_grid = subgrid.x1_grid(); - let x2_grid = subgrid.x2_grid(); + let x_grid: Vec<_> = subgrid + .node_values() + .iter() + .zip(self.kinematics()) + .filter_map(|(node_values, kin)| { + matches!(kin, Kinematics::X(_)).then(|| node_values.values()) + }) + .collect(); + // TODO: generalize this to N dimensions + assert_eq!(x_grid.len(), 2); - lumi_cache.set_grids(&mu2_grid, &x1_grid, &x2_grid, xir, xif); + lumi_cache.set_grids(&mu2_grid, &x_grid[0], &x_grid[1], xir, xif); let mut value = 0.0; for (idx, v) in subgrid.indexed_iter() { assert_eq!(idx.len(), 3); - let x1 = x1_grid[idx[1]]; - let x2 = x2_grid[idx[2]]; + let x1 = x_grid[0][idx[1]]; + let x2 = x_grid[1][idx[2]]; let mut lumi = 0.0; for entry in channel.entry() { @@ -341,19 +350,36 @@ impl Grid { let order = &self.orders[ord]; let channel = &pdg_channels[channel]; - let mu2_grid = subgrid.mu2_grid(); - let x1_grid = subgrid.x1_grid(); - let x2_grid = subgrid.x2_grid(); + let node_values: Vec<_> = subgrid + .node_values() + .iter() + .map(|node_values| node_values.values()) + .collect(); + // TODO: generalize this to N dimensions + assert_eq!(node_values.len(), 3); - lumi_cache.set_grids(&mu2_grid, &x1_grid, &x2_grid, xir, xif); + lumi_cache.set_grids( + &node_values[0] + .iter() + .map(|&scale| Mu2 { + ren: scale, + fac: scale, + frg: -1.0, + }) + .collect::>(), + &node_values[1], + &node_values[2], + xir, + xif, + ); - let dim = vec![mu2_grid.len(), x1_grid.len(), x2_grid.len()]; + let dim: Vec<_> = node_values.iter().map(Vec::len).collect(); let mut array = ArrayD::zeros(dim); for (idx, value) in subgrid.indexed_iter() { assert_eq!(idx.len(), 3); - let x1 = x1_grid[idx[1]]; - let x2 = x2_grid[idx[2]]; + let x1 = node_values[1][idx[1]]; + let x2 = node_values[2][idx[2]]; let mut lumi = 0.0; for entry in channel.entry() { @@ -997,32 +1023,56 @@ impl Grid { } fn symmetrize_channels(&mut self) { - let convolutions = self.convolutions(); - // TODO: generalize this method to n convolutions - assert_eq!(convolutions.len(), 2); - if convolutions[0] != convolutions[1] { - return; - } + let pairs: Vec<_> = self + .convolutions() + .iter() + .enumerate() + .tuple_combinations() + .filter_map(|((idx_a, conv_a), (idx_b, conv_b))| { + (conv_a == conv_b).then(|| (idx_a, idx_b)) + }) + .collect(); + + let (idx_a, idx_b) = match pairs.as_slice() { + &[] => return, + &[pair] => pair, + _ => panic!("more than two equal convolutions found"), + }; + let a_subgrid = self + .kinematics() + .iter() + .position(|&kin| kin == Kinematics::X(idx_a)) + // UNWRAP: should be guaranteed by the constructor + .unwrap(); + let b_subgrid = self + .kinematics() + .iter() + .position(|&kin| kin == Kinematics::X(idx_b)) + // UNWRAP: should be guaranteed by the constructor + .unwrap(); let mut indices: Vec = (0..self.channels.len()).rev().collect(); while let Some(index) = indices.pop() { let channel_entry = &self.channels[index]; - if *channel_entry == channel_entry.transpose(0, 1) { + if *channel_entry == channel_entry.transpose(idx_a, idx_b) { // check if in all cases the limits are compatible with merging self.subgrids .slice_mut(s![.., .., index]) .iter_mut() .for_each(|subgrid| { - if !subgrid.is_empty() && (subgrid.x1_grid() == subgrid.x2_grid()) { - subgrid.symmetrize(1, 2); + if !subgrid.is_empty() + && (subgrid.node_values()[a_subgrid] + == subgrid.node_values()[b_subgrid]) + { + subgrid.symmetrize(a_subgrid, b_subgrid); } }); } else if let Some((j, &other_index)) = indices .iter() .enumerate() - .find(|(_, i)| self.channels[**i] == channel_entry.transpose(0, 1)) + .find(|(_, i)| self.channels[**i] == channel_entry.transpose(idx_a, idx_b)) { indices.remove(j); @@ -1039,7 +1089,7 @@ impl Grid { // transpose `lhs` todo!(); } else { - lhs.merge(rhs, Some((1, 2))); + lhs.merge(rhs, Some((a_subgrid, b_subgrid))); *rhs = EmptySubgridV1.into(); } } @@ -1073,12 +1123,6 @@ impl Grid { pub fn evolve_info(&self, order_mask: &[bool]) -> EvolveInfo { use super::evolution::EVOLVE_INFO_TOL_ULPS; - // TODO: generalize this method to n convolutions and different EKOs - assert_eq!(self.convolutions().len(), 2); - - let has_pdf1 = self.convolutions()[0] != Convolution::None; - let has_pdf2 = self.convolutions()[1] != Convolution::None; - let mut ren1 = Vec::new(); let mut fac1 = Vec::new(); let mut x1 = Vec::new(); @@ -1089,7 +1133,7 @@ impl Grid { .indexed_iter() .filter_map(|(tuple, subgrid)| { (!subgrid.is_empty() && (order_mask.is_empty() || order_mask[tuple.0])) - .then_some((tuple.2, subgrid)) + .then_some((&self.channels()[tuple.2], subgrid)) }) { ren1.extend(subgrid.mu2_grid().iter().map(|Mu2 { ren, .. }| *ren)); @@ -1100,31 +1144,26 @@ impl Grid { fac1.sort_by(f64::total_cmp); fac1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); - if has_pdf1 { - x1.extend(subgrid.x1_grid().iter().copied()); - } - if has_pdf2 { - x1.extend(subgrid.x2_grid().iter()); - } + x1.extend( + subgrid + .node_values() + .iter() + .zip(self.kinematics()) + .filter(|(_, kin)| matches!(kin, Kinematics::X(_))) + .zip(self.convolutions()) + .filter_map(|((node_values, _), convolution)| { + (*convolution != Convolution::None).then_some(node_values.values()) + }) + .flatten(), + ); x1.sort_by(f64::total_cmp); x1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); - if has_pdf1 { - pids1.extend( - self.channels()[channel] - .entry() - .iter() - .map(|(pids, _)| pids[0]), - ); - } - if has_pdf2 { - pids1.extend( - self.channels()[channel] - .entry() - .iter() - .map(|(pids, _)| pids[1]), - ); + for (index, convolution) in self.convolutions().iter().enumerate() { + if *convolution != Convolution::None { + pids1.extend(channel.entry().iter().map(|(pids, _)| pids[index])); + } } pids1.sort_unstable(); From 91238e4efc220055555c7546e54c8f6596682b5f Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 20 Sep 2024 17:17:46 +0200 Subject: [PATCH 129/213] Migrate more call sites to `NodeValues` --- pineappl/src/empty_subgrid.rs | 14 +------ pineappl/src/fk_table.rs | 79 +++++++++++++++++++++-------------- pineappl_cli/tests/import.rs | 6 ++- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 4efb917a..66770cf5 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -98,17 +98,7 @@ mod tests { } #[test] - fn q2_grid() { - assert!(EmptySubgridV1.mu2_grid().is_empty()); - } - - #[test] - fn x1_grid() { - assert!(EmptySubgridV1.x1_grid().is_empty()); - } - - #[test] - fn x2_grid() { - assert!(EmptySubgridV1.x2_grid().is_empty()); + fn node_values() { + assert!(EmptySubgridV1.node_values().is_empty()); } } diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index f58ce084..0e3ff396 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -1,11 +1,11 @@ //! Provides the [`FkTable`] type. -use super::boc::Order; +use super::boc::{Kinematics, Order}; use super::convolutions::{Convolution, LumiCache}; use super::grid::Grid; use super::subgrid::Subgrid; use float_cmp::approx_eq; -use ndarray::Array4; +use ndarray::ArrayD; use std::fmt::{self, Display, Formatter}; use std::str::FromStr; use thiserror::Error; @@ -144,44 +144,59 @@ impl FkTable { /// /// TODO #[must_use] - pub fn table(&self) -> Array4 { - let has_pdf1 = self.grid.convolutions()[0] != Convolution::None; - let has_pdf2 = self.grid.convolutions()[1] != Convolution::None; + pub fn table(&self) -> ArrayD { let x_grid = self.x_grid(); - let mut result = Array4::zeros(( - self.grid.bin_info().bins(), - self.grid.channels().len(), - if has_pdf1 { x_grid.len() } else { 1 }, - if has_pdf2 { x_grid.len() } else { 1 }, - )); + let mut dim = vec![self.grid.bin_info().bins(), self.grid.channels().len()]; + dim.extend(self.grid.convolutions().iter().map(|convolution| { + if *convolution == Convolution::None { + 1 + } else { + x_grid.len() + } + })); + let mut result = ArrayD::zeros(dim); for ((_, bin, channel), subgrid) in self.grid().subgrids().indexed_iter() { - let indices1 = if has_pdf1 { - subgrid - .x1_grid() - .iter() - .map(|&s| x_grid.iter().position(|&x| approx_eq!(f64, s, x, ulps = 2))) - .collect::>() - .unwrap() - } else { - vec![0] - }; - let indices2 = if has_pdf2 { - subgrid - .x2_grid() - .iter() - .map(|&s| x_grid.iter().position(|&x| approx_eq!(f64, s, x, ulps = 2))) - .collect::>() - .unwrap() - } else { - vec![0] - }; + let indices: Vec<_> = self + .grid + .convolutions() + .iter() + .enumerate() + .map(|(index, convolution)| { + subgrid + .node_values() + .iter() + .zip(self.grid.kinematics()) + .find_map(|(node_values, kin)| { + matches!(kin, Kinematics::X(i) if *i == index).then(|| { + if *convolution == Convolution::None { + vec![0] + } else { + node_values + .values() + .iter() + .map(|&s| { + x_grid + .iter() + .position(|&x| approx_eq!(f64, s, x, ulps = 2)) + // UNWRAP: must be guaranteed by the grid constructor + .unwrap() + }) + .collect() + } + }) + }) + // UNWRAP: must be guaranteed by the grid constructor + .unwrap() + }) + .collect(); for (index, value) in subgrid.indexed_iter() { assert_eq!(index.len(), 3); assert_eq!(index[0], 0); - result[[bin, channel, indices1[index[1]], indices2[index[2]]]] = value; + // result[[bin, channel, indices1[index[1]], indices2[index[2]]]] = value; + result[[bin, channel, indices[0][index[1]], indices[1][index[2]]]] = value; } } diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index dee894b8..8625e9ea 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -675,6 +675,7 @@ fn import_flex_grid_15() { #[test] #[cfg(feature = "fktable")] fn import_dis_fktable() { + use ndarray::Array4; use pineappl::fk_table::FkTable; use pineappl::grid::Grid; use std::fs::File; @@ -823,7 +824,7 @@ fn import_dis_fktable() { ] ); - let table = fk_table.table(); + let table: Array4 = fk_table.table().into_dimensionality().unwrap(); assert_eq!(table.dim(), (20, 9, 100, 1)); assert_eq!( @@ -852,6 +853,7 @@ fn import_dis_fktable() { fn import_hadronic_fktable() { use float_cmp::assert_approx_eq; use lhapdf::Pdf; + use ndarray::Array4; use pineappl::convolutions::Convolution; use pineappl::convolutions::LumiCache; use pineappl::fk_table::{FkAssumptions, FkTable}; @@ -895,7 +897,7 @@ fn import_hadronic_fktable() { let results = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); let mut fk_table = FkTable::try_from(grid).unwrap(); - let table = fk_table.table(); + let table: Array4 = fk_table.table().into_dimensionality().unwrap(); assert_eq!(table.dim(), (1, 45, 30, 30)); assert_eq!( From e010ffa0200cbb6fe5c486cf8a776ba904d43e35 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 24 Sep 2024 09:14:10 +0200 Subject: [PATCH 130/213] Migrate DY test to `Subgrid::node_values` --- pineappl/tests/drell_yan_lo.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 7d44f5c9..26388e43 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -394,8 +394,10 @@ fn perform_grid_tests( // TEST 7b: `optimize` grid.optimize(); - assert_eq!(grid.subgrids()[[0, 0, 0]].x1_grid().as_ref(), x_grid); - assert_eq!(grid.subgrids()[[0, 0, 0]].x2_grid().as_ref(), x_grid); + let node_values = grid.subgrids()[[0, 0, 0]].node_values(); + + assert_eq!(node_values[1].values(), x_grid); + assert_eq!(node_values[2].values(), x_grid); // TEST 8: `convolve_subgrid` for the optimized subgrids let bins: Vec<_> = (0..grid.bin_info().bins()) @@ -676,9 +678,11 @@ fn grid_optimize() -> Result<()> { grid.subgrids()[[0, 0, 0]], SubgridEnum::LagrangeSubgridV2 { .. } )); - assert_eq!(grid.subgrids()[[0, 0, 0]].x1_grid().len(), 50); - assert_eq!(grid.subgrids()[[0, 0, 0]].x2_grid().len(), 50); - assert_eq!(grid.subgrids()[[0, 0, 0]].mu2_grid().len(), 30); + + let node_values = grid.subgrids()[[0, 0, 0]].node_values(); + assert_eq!(node_values[0].len(), 30); + assert_eq!(node_values[1].len(), 50); + assert_eq!(node_values[2].len(), 50); let mut grid2 = grid.clone(); grid2.optimize_using(GridOptFlags::OPTIMIZE_SUBGRID_TYPE); @@ -689,9 +693,10 @@ fn grid_optimize() -> Result<()> { SubgridEnum::PackedQ1X2SubgridV1 { .. } )); // and the dimensions of the subgrid - assert_eq!(grid2.subgrids()[[0, 0, 0]].x1_grid().len(), 6); - assert_eq!(grid2.subgrids()[[0, 0, 0]].x2_grid().len(), 6); - assert_eq!(grid2.subgrids()[[0, 0, 0]].mu2_grid().len(), 4); + let node_values = grid2.subgrids()[[0, 0, 0]].node_values(); + assert_eq!(node_values[0].len(), 4); + assert_eq!(node_values[1].len(), 6); + assert_eq!(node_values[2].len(), 6); grid.optimize_using(GridOptFlags::OPTIMIZE_SUBGRID_TYPE | GridOptFlags::STATIC_SCALE_DETECTION); @@ -699,10 +704,11 @@ fn grid_optimize() -> Result<()> { grid.subgrids()[[0, 0, 0]], SubgridEnum::PackedQ1X2SubgridV1 { .. } )); - // if `STATIC_SCALE_DETECTION` is present the `mu2_grid` dimension are better optimized - assert_eq!(grid.subgrids()[[0, 0, 0]].x1_grid().len(), 6); - assert_eq!(grid.subgrids()[[0, 0, 0]].x2_grid().len(), 6); - assert_eq!(grid.subgrids()[[0, 0, 0]].mu2_grid().len(), 1); + // if `STATIC_SCALE_DETECTION` is present the scale dimension is better optimized + let node_values = grid.subgrids()[[0, 0, 0]].node_values(); + assert_eq!(node_values[0].len(), 1); + assert_eq!(node_values[1].len(), 6); + assert_eq!(node_values[2].len(), 6); // has no effect for this test grid.optimize_using(GridOptFlags::SYMMETRIZE_CHANNELS); From 8012a94754a5925ffae1eff75312d7a6a6ac3928 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 24 Sep 2024 09:14:36 +0200 Subject: [PATCH 131/213] Remove commented-out code --- pineappl/src/lagrange_subgrid.rs | 16 ---------------- pineappl/src/packed_subgrid.rs | 11 ----------- 2 files changed, 27 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 8a311dd8..d5454ec8 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -108,22 +108,6 @@ impl Subgrid for LagrangeSubgridV2 { self.array = new_array; } - // fn indexed_iter(&self) -> SubgridIndexedIter { - // let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); - - // Box::new(self.array.indexed_iter::<3>().map(move |(index, v)| { - // ( - // (index[0], index[1], index[2]), - // v * self - // .interps - // .iter() - // .enumerate() - // .map(|(i, interp)| interp.reweight(nodes[i][index[i]])) - // .product::(), - // ) - // })) - // } - fn indexed_iter(&self) -> SubgridIndexedIter { let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 05edaf74..c99654b4 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -29,7 +29,6 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn mu2_grid(&self) -> Cow<[Mu2]> { - // Cow::Borrowed(&self.mu2_grid) Cow::Owned( self.node_values[0] .values() @@ -233,16 +232,6 @@ mod tests { ) .into(); - // let mu2 = vec![Mu2 { - // ren: 0.0, - // fac: 0.0, - // frg: -1.0, - // }]; - - // assert_eq!(grid1.mu2_grid().as_ref(), mu2); - // assert_eq!(grid1.x1_grid().as_ref(), x); - // assert_eq!(grid1.x2_grid(), grid1.x1_grid()); - assert_eq!( grid1.node_values(), vec![ From ac95f2695792f82c729be6d8a3fcb97ae0775408 Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 24 Sep 2024 11:58:02 +0200 Subject: [PATCH 132/213] Removee more `x{1,2}_grid` calls --- pineappl/src/convolutions.rs | 42 +++++++++++--- pineappl/src/grid.rs | 13 +++-- pineappl_cli/src/export/applgrid.rs | 88 +++++++++++++++++++++++++---- pineappl_cli/src/plot.rs | 28 ++++++++- pineappl_cli/src/subgrids.rs | 38 ++++++------- pineappl_cli/tests/subgrids.rs | 21 ++++--- 6 files changed, 170 insertions(+), 60 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 2c635d89..f0f94824 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -1,8 +1,9 @@ //! Module for everything related to luminosity functions. +use super::boc::Kinematics; use super::grid::Grid; use super::pids; -use super::subgrid::{Mu2, Subgrid}; +use super::subgrid::{Mu2, NodeValues, Subgrid}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; @@ -165,8 +166,13 @@ impl<'a> LumiCache<'a> { if subgrid.is_empty() { None } else { - let mut vec = subgrid.x1_grid().into_owned(); - vec.extend_from_slice(&subgrid.x2_grid()); + let vec: Vec<_> = grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .filter(|(kin, _)| matches!(kin, Kinematics::X(_))) + .flat_map(|(_, node_values)| node_values.values()) + .collect(); Some(vec) } }) @@ -297,12 +303,16 @@ impl<'a> LumiCache<'a> { /// Set the grids. pub fn set_grids( &mut self, + grid: &Grid, + node_values: &[NodeValues], mu2_grid: &[Mu2], - x1_grid: &[f64], - x2_grid: &[f64], xir: f64, xif: f64, + xia: f64, ) { + // TODO: generalize this for fragmentation functions + assert_eq!(xia, 1.0); + self.imur2 = mu2_grid .iter() .map(|Mu2 { ren, .. }| { @@ -321,7 +331,16 @@ impl<'a> LumiCache<'a> { .unwrap_or_else(|| unreachable!()) }) .collect(); - self.ix1 = x1_grid + self.ix1 = grid + .kinematics() + .iter() + .zip(node_values) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(index) if index == 0).then_some(node_values) + }) + // UNWRAP: guaranteed by the grid constructor + .unwrap() + .values() .iter() .map(|x1| { self.x_grid @@ -331,7 +350,16 @@ impl<'a> LumiCache<'a> { }) .collect(); - self.ix2 = x2_grid + self.ix2 = grid + .kinematics() + .iter() + .zip(node_values) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(index) if index == 1).then_some(node_values) + }) + // UNWRAP: guaranteed by the grid constructor + .unwrap() + .values() .iter() .map(|x2| { self.x_grid diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 4b338aee..a361ec6a 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -274,8 +274,9 @@ impl Grid { let channel = &pdg_channels[chan]; let mu2_grid = subgrid.mu2_grid(); - let x_grid: Vec<_> = subgrid - .node_values() + let node_values = subgrid.node_values(); + + let x_grid: Vec<_> = node_values .iter() .zip(self.kinematics()) .filter_map(|(node_values, kin)| { @@ -285,7 +286,8 @@ impl Grid { // TODO: generalize this to N dimensions assert_eq!(x_grid.len(), 2); - lumi_cache.set_grids(&mu2_grid, &x_grid[0], &x_grid[1], xir, xif); + // TODO: generalize this for fragmentation functions + lumi_cache.set_grids(self, &node_values, &mu2_grid, xir, xif, 1.0); let mut value = 0.0; @@ -359,6 +361,8 @@ impl Grid { assert_eq!(node_values.len(), 3); lumi_cache.set_grids( + self, + &subgrid.node_values(), &node_values[0] .iter() .map(|&scale| Mu2 { @@ -367,10 +371,9 @@ impl Grid { frg: -1.0, }) .collect::>(), - &node_values[1], - &node_values[2], xir, xif, + xia, ); let dim: Vec<_> = node_values.iter().map(Vec::len).collect(); diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index b377a3ac..0e22ebff 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -3,13 +3,12 @@ use cxx::{let_cxx_string, UniquePtr}; use float_cmp::approx_eq; use lhapdf::Pdf; use ndarray::{s, Axis}; -use pineappl::boc::Order; +use pineappl::boc::{Order, Kinematics}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::subgrid::{Mu2, Subgrid}; use pineappl_applgrid::ffi::{self, grid}; -use std::borrow::Cow; use std::f64::consts::TAU; use std::iter; use std::path::Path; @@ -120,6 +119,12 @@ pub fn convert_into_applgrid( let lumis = grid.channels().len(); let has_pdf1 = grid.convolutions()[0] != Convolution::None; let has_pdf2 = grid.convolutions()[1] != Convolution::None; + // let (has_pdf1, has_pdf2) = match (grid.convolutions()[0].clone(), grid.convolutions()[1].clone()) { + // (Convolution::None, Convolution::None) => unreachable!(), + // (Convolution::None, _) => (false, true), + // (_, Convolution::None) => (true, false), + // _ => (true, true), + // }; // TODO: check that PDG MC IDs are used @@ -140,12 +145,14 @@ pub fn convert_into_applgrid( // propagate them assert_eq!(factor, 1.0); - match (has_pdf1, has_pdf2) { - (true, true) => [pids[0], pids[1]], - (true, false) => [pids[0], 0], - (false, true) => [pids[1], 0], - (false, false) => unreachable!(), - } + pids.iter() + .zip(grid.convolutions()) + .filter_map(|(&pid, convolution)| { + (*convolution != Convolution::None).then_some(pid) + }) + .chain(iter::repeat(0)) + .take(2) + .collect::>() })) }), ) @@ -229,7 +236,11 @@ pub fn convert_into_applgrid( map @ Map::ApplGridF2 => panic!("export does not support {map:?}"), }, grid.channels().len().try_into().unwrap(), - has_pdf1 != has_pdf2, + grid.convolutions() + .iter() + .filter(|&conv| *conv != Convolution::None) + .count() + == 1, ); let appl_q2: Vec<_> = (0..igrid.Ntau()).map(|i| igrid.getQ2(i)).collect(); let appl_x1: Vec<_> = (0..igrid.Ny1()).map(|i| igrid.getx1(i)).collect(); @@ -266,13 +277,66 @@ pub fn convert_into_applgrid( .collect::>()?; // in the DIS case APPLgrid always uses the first x dimension + let (x1_grid, x2_grid) = if has_pdf1 && has_pdf2 { - (subgrid.x1_grid(), subgrid.x2_grid()) + ( + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == 0) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == 1) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + ) } else if has_pdf1 { - (subgrid.x1_grid(), Cow::Owned(vec![])) + ( + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == 0) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + Vec::new(), + ) } else { - (subgrid.x2_grid(), Cow::Owned(vec![])) + ( + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == 1) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + Vec::new(), + ) }; + // let (x1_grid, x2_grid) = if has_pdf1 && has_pdf2 { + // (subgrid.x1_grid(), subgrid.x2_grid()) + // } else if has_pdf1 { + // (subgrid.x1_grid(), Cow::Owned(vec![])) + // } else { + // (subgrid.x2_grid(), Cow::Owned(vec![])) + // }; let appl_x1_idx: Vec<_> = x1_grid .iter() diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index b7ef7783..82fb44de 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -5,7 +5,7 @@ use clap::builder::{PossibleValuesParser, TypedValueParser}; use clap::{Parser, ValueHint}; use itertools::Itertools; use ndarray::Axis; -use pineappl::boc::Channel; +use pineappl::boc::{Channel, Kinematics}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::subgrid::Subgrid; @@ -536,6 +536,9 @@ impl Subcommand for Opts { let cl = lhapdf::CL_1_SIGMA; let grid = helpers::read_grid(&self.input)?; + // TODO: convert this into an error + assert_eq!(grid.convolutions().len(), 2); + let member1 = self.conv_funs[0].members[self.conv_fun_uncert_from]; let member2 = self.conv_funs[1].members[self.conv_fun_uncert_from]; @@ -618,8 +621,27 @@ impl Subcommand for Opts { let subgrid = &grid.subgrids()[<[usize; 3]>::from(index)]; //let q2 = subgrid.q2_grid(); - let x1 = subgrid.x1_grid(); - let x2 = subgrid.x2_grid(); + let x1 = grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(); + + let x2 = grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == 1).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(); let mut x1_vals = vec![]; let mut x2_vals = vec![]; diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index 64753a37..124e9d5a 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -2,6 +2,7 @@ use super::helpers; use super::{GlobalConfiguration, Subcommand}; use anyhow::Result; use clap::{Args, Parser, ValueHint}; +use pineappl::boc::Kinematics; use pineappl::subgrid::Mu2; use pineappl::subgrid::{Subgrid, SubgridEnum}; use prettytable::{cell, row}; @@ -26,12 +27,9 @@ struct Group { /// Show the squared factorization grid values. #[arg(long)] muf2: bool, - /// Show the x1 grid values. - #[arg(long)] - x1: bool, - /// Show the x2 grid values. - #[arg(long)] - x2: bool, + /// Show the x-node values for the given indices. + #[arg(long, require_equals = true, value_delimiter = ',', value_name = "IDX")] + x: Vec, /// Show grid statistics (figures are the number of entries). #[arg(long)] stats: bool, @@ -74,11 +72,8 @@ impl Subcommand for Opts { if self.group.muf2 { titles.add_cell(cell!(c->"muf2")); } - if self.group.x1 { - titles.add_cell(cell!(c->"x1")); - } - if self.group.x2 { - titles.add_cell(cell!(c->"x2")); + for index in &self.group.x { + titles.add_cell(cell!(c->format!("x{index}"))); } if self.group.stats { titles.add_cell(cell!(c->"total")); @@ -144,18 +139,17 @@ impl Subcommand for Opts { row.add_cell(cell!(l->values.join(", "))); } - if self.group.x1 { - let values: Vec<_> = subgrid - .x1_grid() + for &index in &self.group.x { + let values: Vec<_> = grid + .kinematics() .iter() - .map(|x| format!("{:.*e}", self.digits, x)) - .collect(); - - row.add_cell(cell!(l->values.join(", "))); - } - if self.group.x2 { - let values: Vec<_> = subgrid - .x2_grid() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(idx) if idx == index).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values() .iter() .map(|x| format!("{:.*e}", self.digits, x)) .collect(); diff --git a/pineappl_cli/tests/subgrids.rs b/pineappl_cli/tests/subgrids.rs index 8b649828..0fb151c3 100644 --- a/pineappl_cli/tests/subgrids.rs +++ b/pineappl_cli/tests/subgrids.rs @@ -2,7 +2,7 @@ use assert_cmd::Command; const HELP_STR: &str = "Print information about the internal subgrid types -Usage: pineappl subgrids [OPTIONS] <--type|--mur|--mur2|--muf|--muf2|--x1|--x2|--stats> +Usage: pineappl subgrids [OPTIONS] <--type|--mur|--mur2|--muf|--muf2|--x=|--stats> Arguments: Path to the input grid @@ -14,8 +14,7 @@ Options: --mur2 Show the squared renormalization grid values --muf Show the factorization grid values --muf2 Show the squared factorization grid values - --x1 Show the x1 grid values - --x2 Show the x2 grid values + --x= Show the x-node values for the given indices --stats Show grid statistics (figures are the number of entries) --digits Set the number of digits shown for numerical values [default: 3] -h, --help Print help @@ -873,7 +872,7 @@ const TYPE_SHOW_EMPTY_STR: &str = "o b c type 4 7 4 PackedQ1X2SubgridV1 "; -const X1_STR: &str = "o b c x1 +const X0_STR: &str = "o b c x0 -+-+-+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0 0 0 1.000e0, 9.309e-1, 8.628e-1, 7.956e-1, 7.296e-1, 6.648e-1, 6.015e-1, 5.398e-1, 4.799e-1, 4.222e-1, 3.669e-1, 3.144e-1, 2.651e-1, 2.195e-1, 1.780e-1, 1.411e-1, 1.091e-1, 8.228e-2, 6.048e-2, 4.341e-2, 3.052e-2, 2.109e-2, 1.438e-2, 9.699e-3, 6.496e-3, 4.329e-3, 2.874e-3, 1.903e-3, 1.259e-3, 8.314e-4, 5.488e-4, 3.621e-4, 2.388e-4, 1.575e-4 0 1 0 1.000e0, 9.309e-1, 8.628e-1, 7.956e-1, 7.296e-1, 6.648e-1, 6.015e-1, 5.398e-1, 4.799e-1, 4.222e-1, 3.669e-1, 3.144e-1, 2.651e-1, 2.195e-1, 1.780e-1, 1.411e-1, 1.091e-1, 8.228e-2, 6.048e-2, 4.341e-2, 3.052e-2, 2.109e-2, 1.438e-2, 9.699e-3, 6.496e-3, 4.329e-3, 2.874e-3, 1.903e-3, 1.259e-3, 8.314e-4, 5.488e-4, 3.621e-4, 2.388e-4, 1.575e-4 @@ -981,7 +980,7 @@ const X1_STR: &str = "o b c 4 7 4 1.000e0, 9.309e-1, 8.628e-1, 7.956e-1, 7.296e-1, 6.648e-1, 6.015e-1, 5.398e-1, 4.799e-1, 4.222e-1, 3.669e-1, 3.144e-1, 2.651e-1, 2.195e-1, 1.780e-1, 1.411e-1, 1.091e-1, 8.228e-2, 6.048e-2, 4.341e-2, 3.052e-2, 2.109e-2, 1.438e-2, 9.699e-3, 6.496e-3, 4.329e-3, 2.874e-3, 1.903e-3, 1.259e-3, 8.314e-4, 5.488e-4, 3.621e-4, 2.388e-4, 1.575e-4, 1.038e-4, 6.844e-5, 4.511e-5, 2.974e-5 "; -const X2_STR: &str = "o b c x2 +const X1_STR: &str = "o b c x1 -+-+-+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0 0 0 1.000e0, 9.309e-1, 8.628e-1, 7.956e-1, 7.296e-1, 6.648e-1, 6.015e-1, 5.398e-1, 4.799e-1, 4.222e-1, 3.669e-1, 3.144e-1, 2.651e-1, 2.195e-1, 1.780e-1, 1.411e-1, 1.091e-1, 8.228e-2, 6.048e-2, 4.341e-2, 3.052e-2, 2.109e-2, 1.438e-2, 9.699e-3, 6.496e-3, 4.329e-3, 2.874e-3, 1.903e-3, 1.259e-3, 8.314e-4, 5.488e-4, 3.621e-4, 2.388e-4, 1.575e-4 0 1 0 1.000e0, 9.309e-1, 8.628e-1, 7.956e-1, 7.296e-1, 6.648e-1, 6.015e-1, 5.398e-1, 4.799e-1, 4.222e-1, 3.669e-1, 3.144e-1, 2.651e-1, 2.195e-1, 1.780e-1, 1.411e-1, 1.091e-1, 8.228e-2, 6.048e-2, 4.341e-2, 3.052e-2, 2.109e-2, 1.438e-2, 9.699e-3, 6.496e-3, 4.329e-3, 2.874e-3, 1.903e-3, 1.259e-3, 8.314e-4, 5.488e-4, 3.621e-4, 2.388e-4, 1.575e-4 @@ -1199,29 +1198,29 @@ fn type_show_empty() { } #[test] -fn x1() { +fn x0() { Command::cargo_bin("pineappl") .unwrap() .args([ "subgrids", - "--x1", + "--x=0", "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", ]) .assert() .success() - .stdout(X1_STR); + .stdout(X0_STR); } #[test] -fn x2() { +fn x1() { Command::cargo_bin("pineappl") .unwrap() .args([ "subgrids", - "--x2", + "--x=1", "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", ]) .assert() .success() - .stdout(X2_STR); + .stdout(X1_STR); } From 399403140c8e1d9b2e6403b406ec7ef333ae7b76 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 1 Oct 2024 11:38:20 +0200 Subject: [PATCH 133/213] Fix integration test --- pineappl_cli/src/export/applgrid.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 0e22ebff..da8ea938 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -246,7 +246,11 @@ pub fn convert_into_applgrid( let appl_x1: Vec<_> = (0..igrid.Ny1()).map(|i| igrid.getx1(i)).collect(); let appl_x2: Vec<_> = (0..igrid.Ny2()).map(|i| igrid.getx2(i)).collect(); - for (lumi, subgrid) in subgrids.iter().enumerate() { + for (lumi, subgrid) in subgrids + .iter() + .enumerate() + .filter(|(_, subgrid)| !subgrid.is_empty()) + { let appl_q2_idx: Vec<_> = subgrid .mu2_grid() .iter() From 751623a2cd234cda18aa8f0100eda1b06877e7a9 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 1 Oct 2024 11:41:41 +0200 Subject: [PATCH 134/213] Add some cosmetic fixes --- pineappl_cli/src/export/applgrid.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index da8ea938..c5032121 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -3,7 +3,7 @@ use cxx::{let_cxx_string, UniquePtr}; use float_cmp::approx_eq; use lhapdf::Pdf; use ndarray::{s, Axis}; -use pineappl::boc::{Order, Kinematics}; +use pineappl::boc::{Kinematics, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; @@ -246,7 +246,7 @@ pub fn convert_into_applgrid( let appl_x1: Vec<_> = (0..igrid.Ny1()).map(|i| igrid.getx1(i)).collect(); let appl_x2: Vec<_> = (0..igrid.Ny2()).map(|i| igrid.getx2(i)).collect(); - for (lumi, subgrid) in subgrids + for (channel, subgrid) in subgrids .iter() .enumerate() .filter(|(_, subgrid)| !subgrid.is_empty()) @@ -367,7 +367,7 @@ pub fn convert_into_applgrid( }) .collect::>()?; - let mut weightgrid = ffi::igrid_weightgrid(igrid.pin_mut(), lumi); + let mut weightgrid = ffi::igrid_weightgrid(igrid.pin_mut(), channel); for (indices, value) in subgrid.indexed_iter() { let &[iq2, ix1, ix2] = indices.as_slice() else { From 6f13cbd9251e178410f6a98bcfcf58da97f99c6d Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 1 Oct 2024 14:41:47 +0200 Subject: [PATCH 135/213] Remove `x{1,2}_grid` methods --- pineappl/src/empty_subgrid.rs | 8 -------- pineappl/src/lagrange_subgrid.rs | 8 -------- pineappl/src/packed_subgrid.rs | 8 -------- pineappl/src/subgrid.rs | 8 -------- 4 files changed, 32 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 66770cf5..8aafe369 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -19,14 +19,6 @@ impl Subgrid for EmptySubgridV1 { Cow::Borrowed(&[]) } - fn x1_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&[]) - } - - fn x2_grid(&self) -> Cow<[f64]> { - Cow::Borrowed(&[]) - } - fn node_values(&self) -> Vec { Vec::new() } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index d5454ec8..db5458dc 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -55,14 +55,6 @@ impl Subgrid for LagrangeSubgridV2 { .collect() } - fn x1_grid(&self) -> Cow<[f64]> { - self.interps[1].node_values().into() - } - - fn x2_grid(&self) -> Cow<[f64]> { - self.interps[2].node_values().into() - } - fn node_values(&self) -> Vec { self.interps .iter() diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index c99654b4..8c201204 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -42,14 +42,6 @@ impl Subgrid for PackedQ1X2SubgridV1 { ) } - fn x1_grid(&self) -> Cow<[f64]> { - Cow::Owned(self.node_values[1].values()) - } - - fn x2_grid(&self) -> Cow<[f64]> { - Cow::Owned(self.node_values[2].values()) - } - fn node_values(&self) -> Vec { self.node_values.clone() } diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 23d770bf..bc5fbf66 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -138,14 +138,6 @@ pub trait Subgrid { /// return an empty slice. fn mu2_grid(&self) -> Cow<[Mu2]>; - /// Return a slice of values of `x1`. If the subgrid does not use a grid, this method should - /// return an empty slice. - fn x1_grid(&self) -> Cow<[f64]>; - - /// Return a slice of values of `x2`. If the subgrid does not use a grid, this method should - /// return an empty slice. - fn x2_grid(&self) -> Cow<[f64]>; - /// TODO fn node_values(&self) -> Vec; From ab84b0fdb1205bcbb5575486144768c8e370f22b Mon Sep 17 00:00:00 2001 From: t7phy Date: Tue, 1 Oct 2024 16:27:28 +0200 Subject: [PATCH 136/213] Remove `Subgrid::mu2_grid` --- pineappl/src/convolutions.rs | 40 ++- pineappl/src/empty_subgrid.rs | 5 - pineappl/src/evolution.rs | 20 +- pineappl/src/fk_table.rs | 21 +- pineappl/src/grid.rs | 47 +++- pineappl/src/lagrange_subgrid.rs | 13 - pineappl/src/packed_subgrid.rs | 15 -- pineappl/src/subgrid.rs | 6 - pineappl_cli/src/export/applgrid.rs | 73 ++++-- pineappl_cli/src/subgrids.rs | 70 ++--- pineappl_cli/tests/subgrids.rs | 381 +--------------------------- 11 files changed, 166 insertions(+), 525 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index f0f94824..566cebde 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -3,7 +3,7 @@ use super::boc::Kinematics; use super::grid::Grid; use super::pids; -use super::subgrid::{Mu2, NodeValues, Subgrid}; +use super::subgrid::{NodeValues, Subgrid}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; @@ -188,11 +188,23 @@ impl<'a> LumiCache<'a> { if subgrid.is_empty() { None } else { - Some(subgrid.mu2_grid().into_owned()) + Some( + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + // TODO: generalize this for arbitrary scales + matches!(kin, &Kinematics::Scale(idx) if idx == 0) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + ) } }) .flatten() - .flat_map(|Mu2 { ren, .. }| { + .flat_map(|ren| { xi.iter() .map(|(xir, _)| xir * xir * ren) .collect::>() @@ -208,11 +220,23 @@ impl<'a> LumiCache<'a> { if subgrid.is_empty() { None } else { - Some(subgrid.mu2_grid().into_owned()) + Some( + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + // TODO: generalize this for arbitrary scales + matches!(kin, &Kinematics::Scale(idx) if idx == 0) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + ) } }) .flatten() - .flat_map(|Mu2 { fac, .. }| { + .flat_map(|fac| { xi.iter() .map(|(_, xif)| xif * xif * fac) .collect::>() @@ -305,7 +329,7 @@ impl<'a> LumiCache<'a> { &mut self, grid: &Grid, node_values: &[NodeValues], - mu2_grid: &[Mu2], + mu2_grid: &[f64], xir: f64, xif: f64, xia: f64, @@ -315,7 +339,7 @@ impl<'a> LumiCache<'a> { self.imur2 = mu2_grid .iter() - .map(|Mu2 { ren, .. }| { + .map(|ren| { self.mur2_grid .iter() .position(|&mur2| mur2 == xir * xir * ren) @@ -324,7 +348,7 @@ impl<'a> LumiCache<'a> { .collect(); self.imuf2 = mu2_grid .iter() - .map(|Mu2 { fac, .. }| { + .map(|fac| { self.muf2_grid .iter() .position(|&muf2| muf2 == xif * xif * fac) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 8aafe369..191aadd1 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -3,7 +3,6 @@ use super::interpolation::Interp; use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; -use std::borrow::Cow; use std::iter; /// A subgrid type that is always empty. @@ -15,10 +14,6 @@ impl Subgrid for EmptySubgridV1 { panic!("EmptySubgridV1 doesn't support the fill operation"); } - fn mu2_grid(&self) -> Cow<[Mu2]> { - Cow::Borrowed(&[]) - } - fn node_values(&self) -> Vec { Vec::new() } diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 8121603b..0880bf97 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -7,7 +7,7 @@ use super::grid::{Grid, GridError}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::PidBasis; -use super::subgrid::{Mu2, NodeValues, Subgrid, SubgridEnum}; +use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use float_cmp::approx_eq; use itertools::izip; use itertools::Itertools; @@ -215,6 +215,7 @@ fn operator_slices( type X1aX1bOp2Tuple = (Vec>, Option>); fn ndarray_from_subgrid_orders_slice( + grid: &Grid, fac1: f64, kinematics: &[Kinematics], subgrids: &ArrayView1, @@ -303,10 +304,20 @@ fn ndarray_from_subgrid_orders_slice( let &[ifac1, ix1, ix2] = indices.as_slice() else { unimplemented!() }; - let Mu2 { ren, fac, frg } = subgrid.mu2_grid()[ifac1]; + let fac = grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values()[ifac1]; + // TODO: generalize this for multiple scales + let ren = fac; // TODO: implement evolution for non-zero fragmentation scales - assert_eq!(frg, -1.0); if !approx_eq!(f64, xif * xif * fac, fac1, ulps = EVOLUTION_TOL_ULPS) { continue; @@ -375,6 +386,7 @@ pub(crate) fn evolve_slice_with_one( for (subgrids_o, channel1) in subgrids_ol.axis_iter(Axis(1)).zip(grid.channels()) { let (mut x1, array) = ndarray_from_subgrid_orders_slice( + grid, info.fac1, grid.kinematics(), &subgrids_o, @@ -515,6 +527,7 @@ pub(crate) fn evolve_slice_with_two( for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { let (x1, array) = ndarray_from_subgrid_orders_slice( + grid, info.fac1, grid.kinematics(), &subgrids_o, @@ -654,6 +667,7 @@ pub(crate) fn evolve_slice_with_two2( for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { let (x1, array) = ndarray_from_subgrid_orders_slice( + grid, fac1, grid.kinematics(), &subgrids_o, diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 0e3ff396..83233a2f 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -337,15 +337,24 @@ impl TryFrom for FkTable { continue; } - let mu2_grid = subgrid.mu2_grid(); - - if mu2_grid.len() > 1 { + let [fac] = grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + // TODO: generalize this for arbitrary scales + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values()[..] + else { return Err(TryFromGridError::MultipleScales); - } + }; if muf2 < 0.0 { - muf2 = mu2_grid[0].fac; - } else if muf2 != mu2_grid[0].fac { + muf2 = fac; + } else if !approx_eq!(f64, muf2, fac, ulps = 4) { return Err(TryFromGridError::MultipleScales); } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index a361ec6a..941bcb22 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -10,7 +10,7 @@ use super::interpolation::Interp; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; -use super::subgrid::{Mu2, Subgrid, SubgridEnum}; +use super::subgrid::{Subgrid, SubgridEnum}; use super::v0; use bitflags::bitflags; use float_cmp::{approx_eq, assert_approx_eq}; @@ -273,7 +273,17 @@ impl Grid { } let channel = &pdg_channels[chan]; - let mu2_grid = subgrid.mu2_grid(); + let mu2_grid = self + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + // TODO: generalize this for arbitrary scales + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(); let node_values = subgrid.node_values(); let x_grid: Vec<_> = node_values @@ -363,14 +373,7 @@ impl Grid { lumi_cache.set_grids( self, &subgrid.node_values(), - &node_values[0] - .iter() - .map(|&scale| Mu2 { - ren: scale, - fac: scale, - frg: -1.0, - }) - .collect::>(), + &node_values[0].iter().copied().collect::>(), xir, xif, xia, @@ -1139,11 +1142,31 @@ impl Grid { .then_some((&self.channels()[tuple.2], subgrid)) }) { - ren1.extend(subgrid.mu2_grid().iter().map(|Mu2 { ren, .. }| *ren)); + // ren1.extend(subgrid.mu2_grid().iter().map(|Mu2 { ren, .. }| *ren)); + ren1.extend(self + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values().into_iter()); ren1.sort_by(f64::total_cmp); ren1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); - fac1.extend(subgrid.mu2_grid().iter().map(|Mu2 { fac, .. }| *fac)); + // fac1.extend(subgrid.mu2_grid().iter().map(|Mu2 { fac, .. }| *fac)); + fac1.extend(self + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values().into_iter()); fac1.sort_by(f64::total_cmp); fac1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index db5458dc..2b3c3bc4 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -5,7 +5,6 @@ use super::packed_array::PackedArray; use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use float_cmp::approx_eq; use serde::{Deserialize, Serialize}; -use std::borrow::Cow; use std::mem; /// Subgrid which uses Lagrange-interpolation. @@ -43,18 +42,6 @@ impl Subgrid for LagrangeSubgridV2 { } } - fn mu2_grid(&self) -> Cow<[Mu2]> { - self.interps[0] - .node_values() - .iter() - .map(|&q2| Mu2 { - ren: q2, - fac: q2, - frg: -1.0, - }) - .collect() - } - fn node_values(&self) -> Vec { self.interps .iter() diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 8c201204..875d3403 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -5,7 +5,6 @@ use super::packed_array::PackedArray; use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use itertools::izip; use serde::{Deserialize, Serialize}; -use std::borrow::Cow; use std::mem; /// TODO @@ -28,20 +27,6 @@ impl Subgrid for PackedQ1X2SubgridV1 { panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); } - fn mu2_grid(&self) -> Cow<[Mu2]> { - Cow::Owned( - self.node_values[0] - .values() - .into_iter() - .map(|ren| Mu2 { - ren, - fac: ren, - frg: -1.0, - }) - .collect(), - ) - } - fn node_values(&self) -> Vec { self.node_values.clone() } diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index bc5fbf66..da06711d 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -9,7 +9,6 @@ use enum_dispatch::enum_dispatch; // use super::evolution::EVOLVE_INFO_TOL_ULPS; use super::interpolation::Interp; use serde::{Deserialize, Serialize}; -use std::borrow::Cow; /// TODO #[derive(Clone, Debug, Deserialize, Serialize)] @@ -133,11 +132,6 @@ pub struct Stats { /// Trait each subgrid must implement. #[enum_dispatch] pub trait Subgrid { - /// Return a slice of [`Mu2`] values corresponding to the (squared) renormalization and - /// factorization values of the grid. If the subgrid does not use a grid, this method should - /// return an empty slice. - fn mu2_grid(&self) -> Cow<[Mu2]>; - /// TODO fn node_values(&self) -> Vec; diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index c5032121..731f568b 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -7,7 +7,7 @@ use pineappl::boc::{Kinematics, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; -use pineappl::subgrid::{Mu2, Subgrid}; +use pineappl::subgrid::{Subgrid}; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::iter; @@ -15,26 +15,33 @@ use std::path::Path; use std::pin::Pin; fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result> { + if grid + .kinematics() + .iter() + .filter(|kin| matches!(kin, Kinematics::Scale(_))) + .count() + > 1 + { + bail!("APPLgrid does not support grids with more than one scale"); + } + let mu2_grid: Vec<_> = grid .subgrids() .slice(s![order, bin, ..]) .iter() - .map(|subgrid| { - subgrid - .mu2_grid() - .iter() - .map(|&Mu2 { ren, fac, frg }| { - if frg > 0.0 { - bail!("subgrid has mua2 > 0.0, which APPLgrid does not support"); - } - - if !approx_eq!(f64, ren, fac, ulps = 128) { - bail!("subgrid has mur2 != muf2, which APPLgrid does not support"); - } - - Ok(fac) - }) - .collect::>>() + .filter_map(|subgrid| { + (!subgrid.is_empty()).then(|| { + Ok(grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values()) + }) }) .collect::>()?; let mut mu2_grid: Vec<_> = mu2_grid.into_iter().flatten().collect(); @@ -251,17 +258,18 @@ pub fn convert_into_applgrid( .enumerate() .filter(|(_, subgrid)| !subgrid.is_empty()) { - let appl_q2_idx: Vec<_> = subgrid - .mu2_grid() + let appl_q2_idx: Vec<_> = grid + .kinematics() .iter() - .map(|&Mu2 { ren, fac, frg }| { - if frg > 0.0 { - bail!("subgrid has mua2 > 0.0, which APPLgrid does not support"); - } - - if !approx_eq!(f64, ren, fac, ulps = 128) { - bail!("subgrid has mur2 != muf2, which APPLgrid does not support"); - } + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values() + .into_iter() + .map(|fac| { appl_q2 .iter() .position(|&x| approx_eq!(f64, x, fac, ulps = 128)) @@ -379,7 +387,16 @@ pub fn convert_into_applgrid( if value != 0.0 { println!( "WARNING: discarding non-matching scale muf2 = {}", - subgrid.mu2_grid()[iq2].fac + grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values()[iq2] ); } diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index 124e9d5a..ab470c4b 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -3,7 +3,6 @@ use super::{GlobalConfiguration, Subcommand}; use anyhow::Result; use clap::{Args, Parser, ValueHint}; use pineappl::boc::Kinematics; -use pineappl::subgrid::Mu2; use pineappl::subgrid::{Subgrid, SubgridEnum}; use prettytable::{cell, row}; use std::path::PathBuf; @@ -15,18 +14,9 @@ struct Group { /// Show the subgrid type. #[arg(long = "type")] type_: bool, - /// Show the renormalization grid values. - #[arg(long)] - mur: bool, - /// Show the squared renormalization grid values. - #[arg(long)] - mur2: bool, - /// Show the factorization grid values. - #[arg(long)] - muf: bool, - /// Show the squared factorization grid values. - #[arg(long)] - muf2: bool, + /// Show the scale node values for the given indices. + #[arg(long, require_equals = true, value_delimiter = ',', value_name = "IDX")] + scale: Vec, /// Show the x-node values for the given indices. #[arg(long, require_equals = true, value_delimiter = ',', value_name = "IDX")] x: Vec, @@ -60,17 +50,8 @@ impl Subcommand for Opts { if self.group.type_ { titles.add_cell(cell!(c->"type")); } - if self.group.mur { - titles.add_cell(cell!(c->"mur")); - } - if self.group.mur2 { - titles.add_cell(cell!(c->"mur2")); - } - if self.group.muf { - titles.add_cell(cell!(c->"muf")); - } - if self.group.muf2 { - titles.add_cell(cell!(c->"muf2")); + for index in &self.group.scale { + titles.add_cell(cell!(c->format!("scale{index}"))); } for index in &self.group.x { titles.add_cell(cell!(c->format!("x{index}"))); @@ -103,38 +84,19 @@ impl Subcommand for Opts { } )); } - if self.group.mur { - let values: Vec<_> = subgrid - .mu2_grid() - .iter() - .map(|Mu2 { ren, .. }| format!("{:.*}", self.digits, ren.sqrt())) - .collect(); - - row.add_cell(cell!(l->values.join(", "))); - } - if self.group.mur2 { - let values: Vec<_> = subgrid - .mu2_grid() - .iter() - .map(|Mu2 { ren, .. }| format!("{:.*}", self.digits, ren)) - .collect(); - - row.add_cell(cell!(l->values.join(", "))); - } - if self.group.muf { - let values: Vec<_> = subgrid - .mu2_grid() + for &index in &self.group.scale { + let values: Vec<_> = grid + .kinematics() .iter() - .map(|Mu2 { fac, .. }| format!("{:.*}", self.digits, fac.sqrt())) - .collect(); - - row.add_cell(cell!(l->values.join(", "))); - } - if self.group.muf2 { - let values: Vec<_> = subgrid - .mu2_grid() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == index).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values() .iter() - .map(|Mu2 { fac, .. }| format!("{:.*}", self.digits, fac)) + .map(|x| format!("{:.*}", self.digits, x)) .collect(); row.add_cell(cell!(l->values.join(", "))); diff --git a/pineappl_cli/tests/subgrids.rs b/pineappl_cli/tests/subgrids.rs index 0fb151c3..988072cf 100644 --- a/pineappl_cli/tests/subgrids.rs +++ b/pineappl_cli/tests/subgrids.rs @@ -2,7 +2,7 @@ use assert_cmd::Command; const HELP_STR: &str = "Print information about the internal subgrid types -Usage: pineappl subgrids [OPTIONS] <--type|--mur|--mur2|--muf|--muf2|--x=|--stats> +Usage: pineappl subgrids [OPTIONS] <--type|--scale=|--x=|--stats> Arguments: Path to the input grid @@ -10,341 +10,14 @@ Arguments: Options: --show-empty Show empty subgrids --type Show the subgrid type - --mur Show the renormalization grid values - --mur2 Show the squared renormalization grid values - --muf Show the factorization grid values - --muf2 Show the squared factorization grid values + --scale= Show the scale node values for the given indices --x= Show the x-node values for the given indices --stats Show grid statistics (figures are the number of entries) --digits Set the number of digits shown for numerical values [default: 3] -h, --help Print help "; -const MUF_STR: &str = "o b c muf --+-+-+------ -0 0 0 80.352 -0 1 0 80.352 -0 2 0 80.352 -0 3 0 80.352 -0 4 0 80.352 -0 5 0 80.352 -0 6 0 80.352 -0 7 0 80.352 -1 0 0 80.352 -1 0 1 80.352 -1 0 3 80.352 -1 1 0 80.352 -1 1 1 80.352 -1 1 3 80.352 -1 2 0 80.352 -1 2 1 80.352 -1 2 3 80.352 -1 3 0 80.352 -1 3 1 80.352 -1 3 3 80.352 -1 4 0 80.352 -1 4 1 80.352 -1 4 3 80.352 -1 5 0 80.352 -1 5 1 80.352 -1 5 3 80.352 -1 6 0 80.352 -1 6 1 80.352 -1 6 3 80.352 -1 7 0 80.352 -1 7 1 80.352 -1 7 3 80.352 -2 0 0 80.352 -2 0 1 80.352 -2 0 3 80.352 -2 1 0 80.352 -2 1 1 80.352 -2 1 3 80.352 -2 2 0 80.352 -2 2 1 80.352 -2 2 3 80.352 -2 3 0 80.352 -2 3 1 80.352 -2 3 3 80.352 -2 4 0 80.352 -2 4 1 80.352 -2 4 3 80.352 -2 5 0 80.352 -2 5 1 80.352 -2 5 3 80.352 -2 6 0 80.352 -2 6 1 80.352 -2 6 3 80.352 -2 7 0 80.352 -2 7 1 80.352 -2 7 3 80.352 -3 0 0 80.352 -3 0 2 80.352 -3 0 4 80.352 -3 1 0 80.352 -3 1 2 80.352 -3 1 4 80.352 -3 2 0 80.352 -3 2 2 80.352 -3 2 4 80.352 -3 3 0 80.352 -3 3 2 80.352 -3 3 4 80.352 -3 4 0 80.352 -3 4 2 80.352 -3 4 4 80.352 -3 5 0 80.352 -3 5 2 80.352 -3 5 4 80.352 -3 6 0 80.352 -3 6 2 80.352 -3 6 4 80.352 -3 7 0 80.352 -3 7 2 80.352 -3 7 4 80.352 -4 0 0 80.352 -4 0 2 80.352 -4 0 4 80.352 -4 1 0 80.352 -4 1 2 80.352 -4 1 4 80.352 -4 2 0 80.352 -4 2 2 80.352 -4 2 4 80.352 -4 3 0 80.352 -4 3 2 80.352 -4 3 4 80.352 -4 4 0 80.352 -4 4 2 80.352 -4 4 4 80.352 -4 5 0 80.352 -4 5 2 80.352 -4 5 4 80.352 -4 6 0 80.352 -4 6 2 80.352 -4 6 4 80.352 -4 7 0 80.352 -4 7 2 80.352 -4 7 4 80.352 -"; - -const MUF2_STR: &str = "o b c muf2 --+-+-+-------- -0 0 0 6456.444 -0 1 0 6456.444 -0 2 0 6456.444 -0 3 0 6456.444 -0 4 0 6456.444 -0 5 0 6456.444 -0 6 0 6456.444 -0 7 0 6456.444 -1 0 0 6456.444 -1 0 1 6456.444 -1 0 3 6456.444 -1 1 0 6456.444 -1 1 1 6456.444 -1 1 3 6456.444 -1 2 0 6456.444 -1 2 1 6456.444 -1 2 3 6456.444 -1 3 0 6456.444 -1 3 1 6456.444 -1 3 3 6456.444 -1 4 0 6456.444 -1 4 1 6456.444 -1 4 3 6456.444 -1 5 0 6456.444 -1 5 1 6456.444 -1 5 3 6456.444 -1 6 0 6456.444 -1 6 1 6456.444 -1 6 3 6456.444 -1 7 0 6456.444 -1 7 1 6456.444 -1 7 3 6456.444 -2 0 0 6456.444 -2 0 1 6456.444 -2 0 3 6456.444 -2 1 0 6456.444 -2 1 1 6456.444 -2 1 3 6456.444 -2 2 0 6456.444 -2 2 1 6456.444 -2 2 3 6456.444 -2 3 0 6456.444 -2 3 1 6456.444 -2 3 3 6456.444 -2 4 0 6456.444 -2 4 1 6456.444 -2 4 3 6456.444 -2 5 0 6456.444 -2 5 1 6456.444 -2 5 3 6456.444 -2 6 0 6456.444 -2 6 1 6456.444 -2 6 3 6456.444 -2 7 0 6456.444 -2 7 1 6456.444 -2 7 3 6456.444 -3 0 0 6456.444 -3 0 2 6456.444 -3 0 4 6456.444 -3 1 0 6456.444 -3 1 2 6456.444 -3 1 4 6456.444 -3 2 0 6456.444 -3 2 2 6456.444 -3 2 4 6456.444 -3 3 0 6456.444 -3 3 2 6456.444 -3 3 4 6456.444 -3 4 0 6456.444 -3 4 2 6456.444 -3 4 4 6456.444 -3 5 0 6456.444 -3 5 2 6456.444 -3 5 4 6456.444 -3 6 0 6456.444 -3 6 2 6456.444 -3 6 4 6456.444 -3 7 0 6456.444 -3 7 2 6456.444 -3 7 4 6456.444 -4 0 0 6456.444 -4 0 2 6456.444 -4 0 4 6456.444 -4 1 0 6456.444 -4 1 2 6456.444 -4 1 4 6456.444 -4 2 0 6456.444 -4 2 2 6456.444 -4 2 4 6456.444 -4 3 0 6456.444 -4 3 2 6456.444 -4 3 4 6456.444 -4 4 0 6456.444 -4 4 2 6456.444 -4 4 4 6456.444 -4 5 0 6456.444 -4 5 2 6456.444 -4 5 4 6456.444 -4 6 0 6456.444 -4 6 2 6456.444 -4 6 4 6456.444 -4 7 0 6456.444 -4 7 2 6456.444 -4 7 4 6456.444 -"; - -const MUR_STR: &str = "o b c mur --+-+-+------ -0 0 0 80.352 -0 1 0 80.352 -0 2 0 80.352 -0 3 0 80.352 -0 4 0 80.352 -0 5 0 80.352 -0 6 0 80.352 -0 7 0 80.352 -1 0 0 80.352 -1 0 1 80.352 -1 0 3 80.352 -1 1 0 80.352 -1 1 1 80.352 -1 1 3 80.352 -1 2 0 80.352 -1 2 1 80.352 -1 2 3 80.352 -1 3 0 80.352 -1 3 1 80.352 -1 3 3 80.352 -1 4 0 80.352 -1 4 1 80.352 -1 4 3 80.352 -1 5 0 80.352 -1 5 1 80.352 -1 5 3 80.352 -1 6 0 80.352 -1 6 1 80.352 -1 6 3 80.352 -1 7 0 80.352 -1 7 1 80.352 -1 7 3 80.352 -2 0 0 80.352 -2 0 1 80.352 -2 0 3 80.352 -2 1 0 80.352 -2 1 1 80.352 -2 1 3 80.352 -2 2 0 80.352 -2 2 1 80.352 -2 2 3 80.352 -2 3 0 80.352 -2 3 1 80.352 -2 3 3 80.352 -2 4 0 80.352 -2 4 1 80.352 -2 4 3 80.352 -2 5 0 80.352 -2 5 1 80.352 -2 5 3 80.352 -2 6 0 80.352 -2 6 1 80.352 -2 6 3 80.352 -2 7 0 80.352 -2 7 1 80.352 -2 7 3 80.352 -3 0 0 80.352 -3 0 2 80.352 -3 0 4 80.352 -3 1 0 80.352 -3 1 2 80.352 -3 1 4 80.352 -3 2 0 80.352 -3 2 2 80.352 -3 2 4 80.352 -3 3 0 80.352 -3 3 2 80.352 -3 3 4 80.352 -3 4 0 80.352 -3 4 2 80.352 -3 4 4 80.352 -3 5 0 80.352 -3 5 2 80.352 -3 5 4 80.352 -3 6 0 80.352 -3 6 2 80.352 -3 6 4 80.352 -3 7 0 80.352 -3 7 2 80.352 -3 7 4 80.352 -4 0 0 80.352 -4 0 2 80.352 -4 0 4 80.352 -4 1 0 80.352 -4 1 2 80.352 -4 1 4 80.352 -4 2 0 80.352 -4 2 2 80.352 -4 2 4 80.352 -4 3 0 80.352 -4 3 2 80.352 -4 3 4 80.352 -4 4 0 80.352 -4 4 2 80.352 -4 4 4 80.352 -4 5 0 80.352 -4 5 2 80.352 -4 5 4 80.352 -4 6 0 80.352 -4 6 2 80.352 -4 6 4 80.352 -4 7 0 80.352 -4 7 2 80.352 -4 7 4 80.352 -"; - -const MUR2_STR: &str = "o b c mur2 +const SCALE0_STR: &str = "o b c scale0 -+-+-+-------- 0 0 0 6456.444 0 1 0 6456.444 @@ -1099,59 +772,17 @@ fn help() { } #[test] -fn muf() { - Command::cargo_bin("pineappl") - .unwrap() - .args([ - "subgrids", - "--muf", - "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", - ]) - .assert() - .success() - .stdout(MUF_STR); -} - -#[test] -fn muf2() { - Command::cargo_bin("pineappl") - .unwrap() - .args([ - "subgrids", - "--muf2", - "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", - ]) - .assert() - .success() - .stdout(MUF2_STR); -} - -#[test] -fn mur() { - Command::cargo_bin("pineappl") - .unwrap() - .args([ - "subgrids", - "--mur", - "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", - ]) - .assert() - .success() - .stdout(MUR_STR); -} - -#[test] -fn mur2() { +fn scale0() { Command::cargo_bin("pineappl") .unwrap() .args([ "subgrids", - "--mur2", + "--scale=0", "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", ]) .assert() .success() - .stdout(MUR2_STR); + .stdout(SCALE0_STR); } #[test] From 4cc80945b28a23e677c156e00dcdac8d01ce2a18 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 3 Oct 2024 16:25:43 +0200 Subject: [PATCH 137/213] Rename `LumiCache` to `ConvolutionCache` --- pineappl/src/convolutions.rs | 4 ++-- pineappl/src/fk_table.rs | 6 +++--- pineappl/src/grid.rs | 26 +++++++++++++------------- pineappl/tests/drell_yan_lo.rs | 27 ++++++++++++++------------- pineappl_capi/src/lib.rs | 11 ++++++----- pineappl_cli/src/helpers.rs | 14 +++++++++----- pineappl_cli/tests/import.rs | 18 +++++++++--------- 7 files changed, 56 insertions(+), 50 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 566cebde..91754879 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -38,7 +38,7 @@ impl<'a> Pdfs<'a> { /// A cache for evaluating PDFs. Methods like [`Grid::convolve`] accept instances of this `struct` /// instead of the PDFs themselves. -pub struct LumiCache<'a> { +pub struct ConvolutionCache<'a> { pdfs: Pdfs<'a>, alphas: &'a mut dyn FnMut(f64) -> f64, alphas_cache: Vec, @@ -55,7 +55,7 @@ pub struct LumiCache<'a> { cc2: i32, } -impl<'a> LumiCache<'a> { +impl<'a> ConvolutionCache<'a> { /// Construct a luminosity cache with two PDFs, `xfx1` and `xfx2`. The types of hadrons the /// PDFs correspond to must be given as `pdg1` and `pdg2`. The function to evaluate the /// strong coupling must be given as `alphas`. The grid that the cache will be used with must diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 83233a2f..ac7e6faa 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -1,7 +1,7 @@ //! Provides the [`FkTable`] type. use super::boc::{Kinematics, Order}; -use super::convolutions::{Convolution, LumiCache}; +use super::convolutions::{Convolution, ConvolutionCache}; use super::grid::Grid; use super::subgrid::Subgrid; use float_cmp::approx_eq; @@ -234,12 +234,12 @@ impl FkTable { /// FK-tables have all orders merged together and do not support scale variations. pub fn convolve( &self, - lumi_cache: &mut LumiCache, + convolution_cache: &mut ConvolutionCache, bin_indices: &[usize], channel_mask: &[bool], ) -> Vec { self.grid.convolve( - lumi_cache, + convolution_cache, &[], bin_indices, channel_mask, diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 941bcb22..4f4955cc 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -2,7 +2,7 @@ use super::bin::{BinInfo, BinLimits, BinRemapper}; use super::boc::{Channel, Kinematics, Order, Scales}; -use super::convolutions::{Convolution, LumiCache}; +use super::convolutions::{Convolution, ConvolutionCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; @@ -226,7 +226,7 @@ impl Grid { /// TODO pub fn convolve( &self, - lumi_cache: &mut LumiCache, + convolution_cache: &mut ConvolutionCache, order_mask: &[bool], bin_indices: &[usize], channel_mask: &[bool], @@ -239,7 +239,7 @@ impl Grid { .iter() .map(|&(xir, xif, _)| (xir, xif)) .collect::>(); - lumi_cache.setup(self, &xi).unwrap(); + convolution_cache.setup(self, &xi).unwrap(); let bin_indices = if bin_indices.is_empty() { (0..self.bin_info().bins()).collect() @@ -297,7 +297,7 @@ impl Grid { assert_eq!(x_grid.len(), 2); // TODO: generalize this for fragmentation functions - lumi_cache.set_grids(self, &node_values, &mu2_grid, xir, xif, 1.0); + convolution_cache.set_grids(self, &node_values, &mu2_grid, xir, xif, 1.0); let mut value = 0.0; @@ -309,12 +309,12 @@ impl Grid { for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); - let xfx1 = lumi_cache.xfx1(entry.0[0], idx[1], idx[0]); - let xfx2 = lumi_cache.xfx2(entry.0[1], idx[2], idx[0]); + let xfx1 = convolution_cache.xfx1(entry.0[0], idx[1], idx[0]); + let xfx2 = convolution_cache.xfx2(entry.0[1], idx[2], idx[0]); lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); } - let alphas = lumi_cache.alphas(idx[0]); + let alphas = convolution_cache.alphas(idx[0]); lumi *= alphas.powi(order.alphas.try_into().unwrap()); @@ -346,14 +346,14 @@ impl Grid { /// TODO pub fn convolve_subgrid( &self, - lumi_cache: &mut LumiCache, + convolution_cache: &mut ConvolutionCache, ord: usize, bin: usize, channel: usize, (xir, xif, xia): (f64, f64, f64), ) -> ArrayD { assert_eq!(xia, 1.0); - lumi_cache.setup(self, &[(xir, xif)]).unwrap(); + convolution_cache.setup(self, &[(xir, xif)]).unwrap(); let normalizations = self.bin_info().normalizations(); let pdg_channels = self.channels_pdg(); @@ -370,7 +370,7 @@ impl Grid { // TODO: generalize this to N dimensions assert_eq!(node_values.len(), 3); - lumi_cache.set_grids( + convolution_cache.set_grids( self, &subgrid.node_values(), &node_values[0].iter().copied().collect::>(), @@ -390,12 +390,12 @@ impl Grid { for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); - let xfx1 = lumi_cache.xfx1(entry.0[0], idx[1], idx[0]); - let xfx2 = lumi_cache.xfx2(entry.0[1], idx[2], idx[0]); + let xfx1 = convolution_cache.xfx1(entry.0[0], idx[1], idx[0]); + let xfx2 = convolution_cache.xfx2(entry.0[1], idx[2], idx[0]); lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); } - let alphas = lumi_cache.alphas(idx[0]); + let alphas = convolution_cache.alphas(idx[0]); lumi *= alphas.powi(order.alphas.try_into().unwrap()); diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 26388e43..a32596f6 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -5,7 +5,7 @@ use num_complex::Complex; use pineappl::bin::BinRemapper; use pineappl::boc::{Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; -use pineappl::convolutions::{Convolution, LumiCache}; +use pineappl::convolutions::{Convolution, ConvolutionCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::pids::PidBasis; @@ -351,25 +351,26 @@ fn perform_grid_tests( grid.scale_by_order(10.0, 1.0, 10.0, 10.0, 4.0); // TEST 5: `convolve` - let mut lumi_cache = LumiCache::with_one(2212, &mut xfx, &mut alphas); - let bins = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let mut convolution_cache = ConvolutionCache::with_one(2212, &mut xfx, &mut alphas); + let bins = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins.iter().zip(reference.iter()) { assert_approx_eq!(f64, *result, *reference, ulps = 16); } - // TEST 5b: `convolve` with `LumiCache::with_two` + // TEST 5b: `convolve` with `ConvolutionCache::with_two` let mut xfx1 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut xfx2 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas2 = |_| 0.0; - let mut lumi_cache2 = LumiCache::with_two(2212, &mut xfx1, 2212, &mut xfx2, &mut alphas2); - let bins2 = grid.convolve(&mut lumi_cache2, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let mut convolution_cache2 = + ConvolutionCache::with_two(2212, &mut xfx1, 2212, &mut xfx2, &mut alphas2); + let bins2 = grid.convolve(&mut convolution_cache2, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins2.iter().zip(reference.iter()) { assert_approx_eq!(f64, *result, *reference, ulps = 16); } - mem::drop(lumi_cache2); + mem::drop(convolution_cache2); mem::drop(bins2); // TEST 6: `convolve_subgrid` @@ -377,7 +378,7 @@ fn perform_grid_tests( .map(|bin| { (0..grid.channels().len()) .map(|channel| { - grid.convolve_subgrid(&mut lumi_cache, 0, bin, channel, (1.0, 1.0, 1.0)) + grid.convolve_subgrid(&mut convolution_cache, 0, bin, channel, (1.0, 1.0, 1.0)) .sum() }) .sum() @@ -404,7 +405,7 @@ fn perform_grid_tests( .map(|bin| { (0..grid.channels().len()) .map(|channel| { - grid.convolve_subgrid(&mut lumi_cache, 0, bin, channel, (1.0, 1.0, 1.0)) + grid.convolve_subgrid(&mut convolution_cache, 0, bin, channel, (1.0, 1.0, 1.0)) .sum() }) .sum() @@ -415,7 +416,7 @@ fn perform_grid_tests( assert_approx_eq!(f64, *result, *reference_after_ssd, ulps = 24); } - let bins = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let bins = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference_after_ssd) in bins.iter().zip(reference_after_ssd.iter()) { assert_approx_eq!(f64, *result, *reference_after_ssd, ulps = 24); @@ -448,7 +449,7 @@ fn perform_grid_tests( grid.merge_bins(bin..bin + 2)?; } - let merged2 = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let merged2 = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference_after_ssd) in merged2.iter().zip( reference_after_ssd @@ -463,7 +464,7 @@ fn perform_grid_tests( // delete a few bins from the start grid.delete_bins(&[0, 1]); - let deleted = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let deleted = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); assert_eq!(deleted.len(), 10); @@ -479,7 +480,7 @@ fn perform_grid_tests( // delete a few bins from the ending grid.delete_bins(&[8, 9]); - let deleted2 = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let deleted2 = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); assert_eq!(deleted2.len(), 8); diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 4a96c8a1..f4deb897 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -58,7 +58,7 @@ use itertools::izip; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; -use pineappl::convolutions::{Convolution, LumiCache}; +use pineappl::convolutions::{Convolution, ConvolutionCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::pids::PidBasis; @@ -455,10 +455,10 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_one( unsafe { slice::from_raw_parts(channel_mask, grid.channels().len()) }.to_vec() }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; - let mut lumi_cache = LumiCache::with_one(pdg_id, &mut pdf, &mut als); + let mut convolution_cache = ConvolutionCache::with_one(pdg_id, &mut pdf, &mut als); results.copy_from_slice(&grid.convolve( - &mut lumi_cache, + &mut convolution_cache, &order_mask, &[], &channel_mask, @@ -517,10 +517,11 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_two( unsafe { slice::from_raw_parts(channel_mask, grid.channels().len()) }.to_vec() }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; - let mut lumi_cache = LumiCache::with_two(pdg_id1, &mut pdf1, pdg_id2, &mut pdf2, &mut als); + let mut convolution_cache = + ConvolutionCache::with_two(pdg_id1, &mut pdf1, pdg_id2, &mut pdf2, &mut als); results.copy_from_slice(&grid.convolve( - &mut lumi_cache, + &mut convolution_cache, &order_mask, &[], &channel_mask, diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 97f0362c..f7a2be4d 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -2,7 +2,7 @@ use super::GlobalConfiguration; use anyhow::{anyhow, ensure, Context, Error, Result}; use lhapdf::{Pdf, PdfSet}; use ndarray::{Array3, Ix3}; -use pineappl::convolutions::LumiCache; +use pineappl::convolutions::ConvolutionCache; use pineappl::grid::Grid; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; use prettytable::Table; @@ -289,8 +289,10 @@ pub fn convolve_scales( // TODO: write a new constructor of `LumiCache` that accepts a vector of all the arguments let mut cache = match funs.as_mut_slice() { - [funs0] => LumiCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]), - [funs0, funs1] => LumiCache::with_two( + [funs0] => { + ConvolutionCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]) + } + [funs0, funs1] => ConvolutionCache::with_two( pdg_ids[0], funs0, pdg_ids[1], @@ -452,8 +454,10 @@ pub fn convolve_subgrid( // TODO: write a new constructor of `LumiCache` that accepts a vector of all the arguments let mut cache = match funs.as_mut_slice() { - [funs0] => LumiCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]), - [funs0, funs1] => LumiCache::with_two( + [funs0] => { + ConvolutionCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]) + } + [funs0, funs1] => ConvolutionCache::with_two( pdg_ids[0], funs0, pdg_ids[1], diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 8625e9ea..1889b644 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -855,7 +855,7 @@ fn import_hadronic_fktable() { use lhapdf::Pdf; use ndarray::Array4; use pineappl::convolutions::Convolution; - use pineappl::convolutions::LumiCache; + use pineappl::convolutions::ConvolutionCache; use pineappl::fk_table::{FkAssumptions, FkTable}; use pineappl::grid::Grid; use std::fs::File; @@ -893,8 +893,8 @@ fn import_hadronic_fktable() { let pdf = Pdf::with_setname_and_member("NNPDF31_nlo_as_0118_luxqed", 0).unwrap(); let mut xfx = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas = |_| 0.0; - let mut lumi_cache = LumiCache::with_one(2212, &mut xfx, &mut alphas); - let results = grid.convolve(&mut lumi_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); + let mut convolution_cache = ConvolutionCache::with_one(2212, &mut xfx, &mut alphas); + let results = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); let mut fk_table = FkTable::try_from(grid).unwrap(); let table: Array4 = fk_table.table().into_dimensionality().unwrap(); @@ -1017,14 +1017,14 @@ fn import_hadronic_fktable() { ] ); - assert_eq!(results, fk_table.convolve(&mut lumi_cache, &[], &[])); + assert_eq!(results, fk_table.convolve(&mut convolution_cache, &[], &[])); fk_table.optimize(FkAssumptions::Nf6Ind); assert_eq!(fk_table.channels(), channels); assert_approx_eq!( f64, results[0], - fk_table.convolve(&mut lumi_cache, &[], &[])[0], + fk_table.convolve(&mut convolution_cache, &[], &[])[0], ulps = 4 ); fk_table.optimize(FkAssumptions::Nf6Sym); @@ -1032,7 +1032,7 @@ fn import_hadronic_fktable() { assert_approx_eq!( f64, results[0], - fk_table.convolve(&mut lumi_cache, &[], &[])[0], + fk_table.convolve(&mut convolution_cache, &[], &[])[0], ulps = 4 ); fk_table.optimize(FkAssumptions::Nf5Ind); @@ -1040,21 +1040,21 @@ fn import_hadronic_fktable() { assert_approx_eq!( f64, results[0], - fk_table.convolve(&mut lumi_cache, &[], &[])[0] + fk_table.convolve(&mut convolution_cache, &[], &[])[0] ); fk_table.optimize(FkAssumptions::Nf5Sym); assert_eq!(fk_table.channels(), channels); assert_approx_eq!( f64, results[0], - fk_table.convolve(&mut lumi_cache, &[], &[])[0] + fk_table.convolve(&mut convolution_cache, &[], &[])[0] ); fk_table.optimize(FkAssumptions::Nf4Ind); assert_eq!(fk_table.channels(), channels); assert_approx_eq!( f64, results[0], - fk_table.convolve(&mut lumi_cache, &[], &[])[0] + fk_table.convolve(&mut convolution_cache, &[], &[])[0] ); fk_table.optimize(FkAssumptions::Nf4Sym); From f259f63b51513e5f4f39a5c86093130b0aff15de Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 3 Oct 2024 16:26:41 +0200 Subject: [PATCH 138/213] Run `cargo fmt` --- pineappl/src/grid.rs | 44 ++++++++++++++++------------- pineappl_cli/src/export/applgrid.rs | 8 +++--- pineappl_cli/src/subgrids.rs | 3 +- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 4f4955cc..d4aafa0f 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1143,30 +1143,34 @@ impl Grid { }) { // ren1.extend(subgrid.mu2_grid().iter().map(|Mu2 { ren, .. }| *ren)); - ren1.extend(self - .kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values().into_iter()); + ren1.extend( + self.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values() + .into_iter(), + ); ren1.sort_by(f64::total_cmp); ren1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); // fac1.extend(subgrid.mu2_grid().iter().map(|Mu2 { fac, .. }| *fac)); - fac1.extend(self - .kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values().into_iter()); + fac1.extend( + self.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values() + .into_iter(), + ); fac1.sort_by(f64::total_cmp); fac1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 731f568b..fbe86470 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -7,7 +7,7 @@ use pineappl::boc::{Kinematics, Order}; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; -use pineappl::subgrid::{Subgrid}; +use pineappl::subgrid::Subgrid; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::iter; @@ -387,12 +387,12 @@ pub fn convert_into_applgrid( if value != 0.0 { println!( "WARNING: discarding non-matching scale muf2 = {}", - grid - .kinematics() + grid.kinematics() .iter() .zip(subgrid.node_values()) .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + matches!(kin, &Kinematics::Scale(idx) if idx == 0) + .then_some(node_values) }) // TODO: convert this into an error .unwrap() diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index ab470c4b..63505a24 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -90,7 +90,8 @@ impl Subcommand for Opts { .iter() .zip(subgrid.node_values()) .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::Scale(idx) if idx == index).then_some(node_values) + matches!(kin, &Kinematics::Scale(idx) if idx == index) + .then_some(node_values) }) // TODO: convert this into an error .unwrap() From cbdb528dfeedc6358d7ab7cc89312a6e4c6e1cb1 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 3 Oct 2024 16:27:30 +0200 Subject: [PATCH 139/213] Start generalizing `ConvolutionCache` to arbitrary dimensions --- pineappl/src/convolutions.rs | 7 +++++++ pineappl/src/grid.rs | 21 ++++----------------- pineappl_cli/tests/evolve.rs | 2 +- pineappl_cli/tests/import.rs | 8 ++++---- pineappl_cli/tests/write.rs | 4 ++-- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 91754879..115400e9 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -308,6 +308,13 @@ impl<'a> ConvolutionCache<'a> { } } + /// TODO + pub fn fx_prod(&mut self, pdg_ids: &[i32], indices: &[usize]) -> f64 { + self.xfx1(pdg_ids[0], indices[1], indices[0]) + * self.xfx2(pdg_ids[1], indices[2], indices[0]) + / (self.x_grid[self.ix1[indices[1]]] * self.x_grid[self.ix2[indices[2]]]) + } + /// Return the strong coupling for the renormalization scale set with [`LumiCache::set_grids`], /// in the grid `mu2_grid` at the index `imu2`. #[must_use] diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index d4aafa0f..f1a2e68c 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -286,34 +286,21 @@ impl Grid { .values(); let node_values = subgrid.node_values(); - let x_grid: Vec<_> = node_values - .iter() - .zip(self.kinematics()) - .filter_map(|(node_values, kin)| { - matches!(kin, Kinematics::X(_)).then(|| node_values.values()) - }) - .collect(); - // TODO: generalize this to N dimensions - assert_eq!(x_grid.len(), 2); - // TODO: generalize this for fragmentation functions convolution_cache.set_grids(self, &node_values, &mu2_grid, xir, xif, 1.0); let mut value = 0.0; for (idx, v) in subgrid.indexed_iter() { - assert_eq!(idx.len(), 3); - let x1 = x_grid[0][idx[1]]; - let x2 = x_grid[1][idx[2]]; let mut lumi = 0.0; for entry in channel.entry() { - debug_assert_eq!(entry.0.len(), 2); - let xfx1 = convolution_cache.xfx1(entry.0[0], idx[1], idx[0]); - let xfx2 = convolution_cache.xfx2(entry.0[1], idx[2], idx[0]); - lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); + // TODO: we assume `idx` to be ordered as scale, x1, x2 + let fx_prod = convolution_cache.fx_prod(&entry.0, &idx); + lumi += fx_prod * entry.1; } + // TODO: we assume `idx` to be ordered as scale, x1, x2 let alphas = convolution_cache.alphas(idx[0]); lumi *= alphas.powi(order.alphas.try_into().unwrap()); diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index 2fda4e27..e93a83a3 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -86,7 +86,7 @@ const LHCB_WP_7TEV_V2_XIR2_STR: &str = "b Grid FkTable 3 4.9482819982783724e2 4.9469964081143053e2 -2.5980535557890150e-4 4 3.6756257449354945e2 3.6746967569489709e2 -2.5274281196974169e-4 5 2.4912642701834142e2 2.4906651029915440e2 -2.4050727939273209e-4 -6 1.1776254040032327e2 1.1773772039493417e2 -2.1076316207790935e-4 +6 1.1776254040032327e2 1.1773772039493414e2 -2.1076316207813139e-4 7 2.8749891297668260e1 2.8746299479656258e1 -1.2493327278395583e-4 "; diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 1889b644..7f20aca5 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -255,12 +255,12 @@ const IMPORT_NEW_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff 2 6.3163323e2 6.3163323e2 2.2204460e-16 3 6.3586556e2 6.3586556e2 2.2204460e-16 4 6.4139163e2 6.4139163e2 1.7763568e-15 -5 6.4848088e2 6.4848088e2 -2.6645353e-15 -6 6.5354150e2 6.5354150e2 -3.6637360e-15 -7 6.5377566e2 6.5377566e2 -1.7763568e-15 +5 6.4848088e2 6.4848088e2 -2.4424907e-15 +6 6.5354150e2 6.5354150e2 -3.4416914e-15 +7 6.5377566e2 6.5377566e2 -1.4432899e-15 8 6.5094729e2 6.5094729e2 1.7763568e-15 9 6.3588760e2 6.3588760e2 2.2204460e-15 -10 5.9810718e2 5.9810718e2 2.6645353e-15 +10 5.9810718e2 5.9810718e2 2.2204460e-15 "; const IMPORT_FILE_FORMAT_FAILURE_STR: &str = "Error: could not detect file format diff --git a/pineappl_cli/tests/write.rs b/pineappl_cli/tests/write.rs index a3762731..b3f50637 100644 --- a/pineappl_cli/tests/write.rs +++ b/pineappl_cli/tests/write.rs @@ -248,9 +248,9 @@ const ROTATE_PID_BASIS_DIFF_STR: &str = "b x1 O(as^0 a^2) 2 2.5 2.75 5.1561247e2 5.1561247e2 -8.882e-16 -5.2348261e0 -5.2348261e0 -6.661e-16 8.9874343e1 8.9874343e1 -1.221e-15 3 2.75 3 4.1534629e2 4.1534629e2 -4.441e-16 -3.7590420e0 -3.7590420e0 -5.551e-16 7.3935106e1 7.3935106e1 -1.554e-15 4 3 3.25 3.0812719e2 3.0812719e2 -3.331e-16 -2.5871885e0 -2.5871885e0 -5.551e-16 5.6414554e1 5.6414554e1 -2.220e-16 -5 3.25 3.5 2.0807482e2 2.0807482e2 -6.661e-16 -1.6762487e0 -1.6762487e0 -1.110e-16 3.9468336e1 3.9468336e1 -3.331e-16 +5 3.25 3.5 2.0807482e2 2.0807482e2 -6.661e-16 -1.6762487e0 -1.6762487e0 -1.110e-16 3.9468336e1 3.9468336e1 -6.661e-16 6 3.5 4 9.6856769e1 9.6856769e1 -3.331e-16 -8.1027456e-1 -8.1027456e-1 -1.110e-16 1.9822014e1 1.9822014e1 -1.110e-15 -7 4 4.5 2.2383492e1 2.2383492e1 -4.441e-16 -2.2022770e-1 -2.2022770e-1 -5.551e-16 5.3540011e0 5.3540011e0 -3.331e-16 +7 4 4.5 2.2383492e1 2.2383492e1 -3.331e-16 -2.2022770e-1 -2.2022770e-1 -5.551e-16 5.3540011e0 5.3540011e0 0.000e0 "; const ROTATE_PID_BASIS_READ_CHANNELS_STR: &str = " c entry From 76cabc390f8a1e9166aa5b9f2ce5774cc131e579 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 3 Oct 2024 17:59:41 +0200 Subject: [PATCH 140/213] Generalize `ConvolutionCache` even further --- pineappl/src/convolutions.rs | 198 +++++++++++++---------------------- 1 file changed, 73 insertions(+), 125 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 115400e9..1ab76143 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -7,31 +7,15 @@ use super::subgrid::{NodeValues, Subgrid}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; -enum Pdfs<'a> { - Two { - xfx1: &'a mut dyn FnMut(i32, f64, f64) -> f64, - xfx1_cache: FxHashMap<(i32, usize, usize), f64>, - xfx2: &'a mut dyn FnMut(i32, f64, f64) -> f64, - xfx2_cache: FxHashMap<(i32, usize, usize), f64>, - }, - One { - xfx: &'a mut dyn FnMut(i32, f64, f64) -> f64, - xfx_cache: FxHashMap<(i32, usize, usize), f64>, - }, +struct Pdfs<'a> { + xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, + xfx_cache: Vec>, } impl<'a> Pdfs<'a> { pub fn clear(&mut self) { - match self { - Self::One { xfx_cache, .. } => xfx_cache.clear(), - Self::Two { - xfx1_cache, - xfx2_cache, - .. - } => { - xfx1_cache.clear(); - xfx2_cache.clear(); - } + for xfx_cache in &mut self.xfx_cache { + xfx_cache.clear(); } } } @@ -47,12 +31,9 @@ pub struct ConvolutionCache<'a> { x_grid: Vec, imur2: Vec, imuf2: Vec, - ix1: Vec, - ix2: Vec, - pdg1: i32, - pdg2: i32, - cc1: i32, - cc2: i32, + ix: Vec>, + pdg: Vec, + cc: Vec, } impl<'a> ConvolutionCache<'a> { @@ -70,11 +51,9 @@ impl<'a> ConvolutionCache<'a> { alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { Self { - pdfs: Pdfs::Two { - xfx1, - xfx1_cache: FxHashMap::default(), - xfx2, - xfx2_cache: FxHashMap::default(), + pdfs: Pdfs { + xfx: vec![xfx1, xfx2], + xfx_cache: vec![FxHashMap::default(); 2], }, alphas, alphas_cache: vec![], @@ -83,12 +62,9 @@ impl<'a> ConvolutionCache<'a> { x_grid: vec![], imur2: Vec::new(), imuf2: Vec::new(), - ix1: Vec::new(), - ix2: Vec::new(), - pdg1, - pdg2, - cc1: 0, - cc2: 0, + ix: Vec::new(), + pdg: vec![pdg1, pdg2], + cc: vec![0, 0], } } @@ -104,9 +80,9 @@ impl<'a> ConvolutionCache<'a> { alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { Self { - pdfs: Pdfs::One { - xfx, - xfx_cache: FxHashMap::default(), + pdfs: Pdfs { + xfx: vec![xfx], + xfx_cache: vec![FxHashMap::default()], }, alphas, alphas_cache: vec![], @@ -115,12 +91,9 @@ impl<'a> ConvolutionCache<'a> { x_grid: vec![], imur2: Vec::new(), imuf2: Vec::new(), - ix1: Vec::new(), - ix2: Vec::new(), - pdg1: pdg, - pdg2: pdg, - cc1: 0, - cc2: 0, + ix: Vec::new(), + pdg: vec![pdg, pdg], + cc: vec![0, 0], } } @@ -131,30 +104,24 @@ impl<'a> ConvolutionCache<'a> { assert_eq!(convolutions.len(), 2); // do we have to charge-conjugate the initial states? - let cc1 = if let Some(pid) = convolutions[0].pid() { - if self.pdg1 == pid { - 1 - } else if self.pdg1 == pids::charge_conjugate_pdg_pid(pid) { - -1 - } else { - // TODO: return a proper error - return Err(()); - } - } else { - 0 - }; - let cc2 = if let Some(pid) = convolutions[1].pid() { - if self.pdg2 == pid { - 1 - } else if self.pdg2 == pids::charge_conjugate_pdg_pid(pid) { - -1 - } else { - // TODO: return a proper error - return Err(()); - } - } else { - 0 - }; + self.cc = convolutions + .iter() + .zip(&self.pdg) + .map(|(convolution, &pdg)| { + if let Some(pid) = convolution.pid() { + if pdg == pid { + 1 + } else if pdg == pids::charge_conjugate_pdg_pid(pid) { + -1 + } else { + // TODO: return a proper error + panic!("wrong convolution function"); + } + } else { + 0 + } + }) + .collect(); // TODO: try to avoid calling clear self.clear(); @@ -250,32 +217,26 @@ impl<'a> ConvolutionCache<'a> { self.mur2_grid = mur2_grid; self.muf2_grid = muf2_grid; self.x_grid = x_grid; - self.cc1 = cc1; - self.cc2 = cc2; Ok(()) } /// Return the PDF (multiplied with `x`) for the first initial state. pub fn xfx1(&mut self, pdg_id: i32, ix1: usize, imu2: usize) -> f64 { - let ix1 = self.ix1[ix1]; + let ix1 = self.ix[0][ix1]; let x = self.x_grid[ix1]; - if self.cc1 == 0 { + if self.cc[0] == 0 { x } else { let imuf2 = self.imuf2[imu2]; let muf2 = self.muf2_grid[imuf2]; - let pid = if self.cc1 == 1 { + let pid = if self.cc[0] == 1 { pdg_id } else { pids::charge_conjugate_pdg_pid(pdg_id) }; - let (xfx, xfx_cache) = match &mut self.pdfs { - Pdfs::One { xfx, xfx_cache, .. } => (xfx, xfx_cache), - Pdfs::Two { - xfx1, xfx1_cache, .. - } => (xfx1, xfx1_cache), - }; + let xfx = &mut self.pdfs.xfx[0]; + let xfx_cache = &mut self.pdfs.xfx_cache[0]; *xfx_cache .entry((pid, ix1, imuf2)) .or_insert_with(|| xfx(pid, x, muf2)) @@ -284,24 +245,25 @@ impl<'a> ConvolutionCache<'a> { /// Return the PDF (multiplied with `x`) for the second initial state. pub fn xfx2(&mut self, pdg_id: i32, ix2: usize, imu2: usize) -> f64 { - let ix2 = self.ix2[ix2]; + let ix2 = self.ix[1][ix2]; let x = self.x_grid[ix2]; - if self.cc2 == 0 { + if self.cc[1] == 0 { x } else { let imuf2 = self.imuf2[imu2]; let muf2 = self.muf2_grid[imuf2]; - let pid = if self.cc2 == 1 { + let pid = if self.cc[1] == 1 { pdg_id } else { pids::charge_conjugate_pdg_pid(pdg_id) }; - let (xfx, xfx_cache) = match &mut self.pdfs { - Pdfs::One { xfx, xfx_cache, .. } => (xfx, xfx_cache), - Pdfs::Two { - xfx2, xfx2_cache, .. - } => (xfx2, xfx2_cache), - }; + let len = self.pdfs.xfx.len(); + let xfx = &mut self + .pdfs + .xfx[if len == 1 { 0 } else { 1 }]; + let xfx_cache = &mut self + .pdfs + .xfx_cache[if len == 1 { 0 } else { 1 }]; *xfx_cache .entry((pid, ix2, imuf2)) .or_insert_with(|| xfx(pid, x, muf2)) @@ -312,7 +274,7 @@ impl<'a> ConvolutionCache<'a> { pub fn fx_prod(&mut self, pdg_ids: &[i32], indices: &[usize]) -> f64 { self.xfx1(pdg_ids[0], indices[1], indices[0]) * self.xfx2(pdg_ids[1], indices[2], indices[0]) - / (self.x_grid[self.ix1[indices[1]]] * self.x_grid[self.ix2[indices[2]]]) + / (self.x_grid[self.ix[0][indices[1]]] * self.x_grid[self.ix[1][indices[2]]]) } /// Return the strong coupling for the renormalization scale set with [`LumiCache::set_grids`], @@ -362,41 +324,27 @@ impl<'a> ConvolutionCache<'a> { .unwrap_or_else(|| unreachable!()) }) .collect(); - self.ix1 = grid - .kinematics() - .iter() - .zip(node_values) - .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::X(index) if index == 0).then_some(node_values) - }) - // UNWRAP: guaranteed by the grid constructor - .unwrap() - .values() - .iter() - .map(|x1| { - self.x_grid - .iter() - .position(|x| x1 == x) - .unwrap_or_else(|| unreachable!()) - }) - .collect(); - self.ix2 = grid - .kinematics() - .iter() - .zip(node_values) - .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::X(index) if index == 1).then_some(node_values) - }) - // UNWRAP: guaranteed by the grid constructor - .unwrap() - .values() - .iter() - .map(|x2| { - self.x_grid + // TODO: generalize this for arbitrary orderings of x + self.ix = (0..grid.convolutions().len()) + .map(|idx| { + grid.kinematics() .iter() - .position(|x| x2 == x) - .unwrap_or_else(|| unreachable!()) + .zip(node_values) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::X(index) if index == idx).then_some(node_values) + }) + // UNWRAP: guaranteed by the grid constructor + .unwrap() + .values() + .iter() + .map(|xd| { + self.x_grid + .iter() + .position(|x| xd == x) + .unwrap_or_else(|| unreachable!()) + }) + .collect() }) .collect(); } From cc798a838fea14410a0289f68abffe570fe0b7d5 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 4 Oct 2024 08:56:29 +0200 Subject: [PATCH 141/213] Merge `Pdfs` into `ConvolutionCache` --- pineappl/src/convolutions.rs | 46 +++++++++++------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 1ab76143..76d6e219 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -7,23 +7,11 @@ use super::subgrid::{NodeValues, Subgrid}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; -struct Pdfs<'a> { - xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, - xfx_cache: Vec>, -} - -impl<'a> Pdfs<'a> { - pub fn clear(&mut self) { - for xfx_cache in &mut self.xfx_cache { - xfx_cache.clear(); - } - } -} - /// A cache for evaluating PDFs. Methods like [`Grid::convolve`] accept instances of this `struct` /// instead of the PDFs themselves. pub struct ConvolutionCache<'a> { - pdfs: Pdfs<'a>, + xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, + xfx_cache: Vec>, alphas: &'a mut dyn FnMut(f64) -> f64, alphas_cache: Vec, mur2_grid: Vec, @@ -51,10 +39,8 @@ impl<'a> ConvolutionCache<'a> { alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { Self { - pdfs: Pdfs { - xfx: vec![xfx1, xfx2], - xfx_cache: vec![FxHashMap::default(); 2], - }, + xfx: vec![xfx1, xfx2], + xfx_cache: vec![FxHashMap::default(); 2], alphas, alphas_cache: vec![], mur2_grid: vec![], @@ -80,10 +66,8 @@ impl<'a> ConvolutionCache<'a> { alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { Self { - pdfs: Pdfs { - xfx: vec![xfx], - xfx_cache: vec![FxHashMap::default()], - }, + xfx: vec![xfx], + xfx_cache: vec![FxHashMap::default()], alphas, alphas_cache: vec![], mur2_grid: vec![], @@ -235,8 +219,8 @@ impl<'a> ConvolutionCache<'a> { } else { pids::charge_conjugate_pdg_pid(pdg_id) }; - let xfx = &mut self.pdfs.xfx[0]; - let xfx_cache = &mut self.pdfs.xfx_cache[0]; + let xfx = &mut self.xfx[0]; + let xfx_cache = &mut self.xfx_cache[0]; *xfx_cache .entry((pid, ix1, imuf2)) .or_insert_with(|| xfx(pid, x, muf2)) @@ -257,13 +241,9 @@ impl<'a> ConvolutionCache<'a> { } else { pids::charge_conjugate_pdg_pid(pdg_id) }; - let len = self.pdfs.xfx.len(); - let xfx = &mut self - .pdfs - .xfx[if len == 1 { 0 } else { 1 }]; - let xfx_cache = &mut self - .pdfs - .xfx_cache[if len == 1 { 0 } else { 1 }]; + let len = self.xfx.len(); + let xfx = &mut self.xfx[if len == 1 { 0 } else { 1 }]; + let xfx_cache = &mut self.xfx_cache[if len == 1 { 0 } else { 1 }]; *xfx_cache .entry((pid, ix2, imuf2)) .or_insert_with(|| xfx(pid, x, muf2)) @@ -287,7 +267,9 @@ impl<'a> ConvolutionCache<'a> { /// Clears the cache. pub fn clear(&mut self) { self.alphas_cache.clear(); - self.pdfs.clear(); + for xfx_cache in &mut self.xfx_cache { + xfx_cache.clear(); + } self.mur2_grid.clear(); self.muf2_grid.clear(); self.x_grid.clear(); From 59d52a97185ff7b563a75bf3d3804cc73679e0bb Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 4 Oct 2024 10:10:16 +0200 Subject: [PATCH 142/213] Match convolution functions with convolutions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's possible that a single convolution function suffices for a grid that needs two convolutions, the most prominent example is proton–proton scattering: one PDF is used for both convolutions. These changes match the given convolution functions to the convolutions needed by the grid. --- pineappl/src/convolutions.rs | 79 +++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 76d6e219..250ce57b 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -21,7 +21,7 @@ pub struct ConvolutionCache<'a> { imuf2: Vec, ix: Vec>, pdg: Vec, - cc: Vec, + perm: Vec>, } impl<'a> ConvolutionCache<'a> { @@ -50,7 +50,7 @@ impl<'a> ConvolutionCache<'a> { imuf2: Vec::new(), ix: Vec::new(), pdg: vec![pdg1, pdg2], - cc: vec![0, 0], + perm: Vec::new(), } } @@ -76,8 +76,8 @@ impl<'a> ConvolutionCache<'a> { imur2: Vec::new(), imuf2: Vec::new(), ix: Vec::new(), - pdg: vec![pdg, pdg], - cc: vec![0, 0], + pdg: vec![pdg], + perm: Vec::new(), } } @@ -87,23 +87,29 @@ impl<'a> ConvolutionCache<'a> { // TODO: the following code only works with exactly two convolutions assert_eq!(convolutions.len(), 2); - // do we have to charge-conjugate the initial states? - self.cc = convolutions + self.perm = grid + .convolutions() .iter() - .zip(&self.pdg) - .map(|(convolution, &pdg)| { - if let Some(pid) = convolution.pid() { - if pdg == pid { - 1 - } else if pdg == pids::charge_conjugate_pdg_pid(pid) { - -1 - } else { - // TODO: return a proper error - panic!("wrong convolution function"); - } - } else { - 0 - } + .enumerate() + .map(|(max_idx, conv)| { + conv.pid().map(|pid| { + self.pdg + .iter() + .take(max_idx + 1) + .enumerate() + .rev() + .find_map(|(idx, &pdg)| { + if pid == pdg { + Some((idx, false)) + } else if pid == pids::charge_conjugate_pdg_pid(pdg) { + Some((idx, true)) + } else { + None + } + }) + // TODO: convert `unwrap` to `Err` + .unwrap() + }) }) .collect(); @@ -209,21 +215,21 @@ impl<'a> ConvolutionCache<'a> { pub fn xfx1(&mut self, pdg_id: i32, ix1: usize, imu2: usize) -> f64 { let ix1 = self.ix[0][ix1]; let x = self.x_grid[ix1]; - if self.cc[0] == 0 { - x - } else { + if let Some((idx, cc)) = self.perm[0] { let imuf2 = self.imuf2[imu2]; let muf2 = self.muf2_grid[imuf2]; - let pid = if self.cc[0] == 1 { - pdg_id - } else { + let pid = if cc { pids::charge_conjugate_pdg_pid(pdg_id) + } else { + pdg_id }; - let xfx = &mut self.xfx[0]; - let xfx_cache = &mut self.xfx_cache[0]; + let xfx = &mut self.xfx[idx]; + let xfx_cache = &mut self.xfx_cache[idx]; *xfx_cache .entry((pid, ix1, imuf2)) .or_insert_with(|| xfx(pid, x, muf2)) + } else { + x } } @@ -231,22 +237,21 @@ impl<'a> ConvolutionCache<'a> { pub fn xfx2(&mut self, pdg_id: i32, ix2: usize, imu2: usize) -> f64 { let ix2 = self.ix[1][ix2]; let x = self.x_grid[ix2]; - if self.cc[1] == 0 { - x - } else { + if let Some((idx, cc)) = self.perm[1] { let imuf2 = self.imuf2[imu2]; let muf2 = self.muf2_grid[imuf2]; - let pid = if self.cc[1] == 1 { - pdg_id - } else { + let pid = if cc { pids::charge_conjugate_pdg_pid(pdg_id) + } else { + pdg_id }; - let len = self.xfx.len(); - let xfx = &mut self.xfx[if len == 1 { 0 } else { 1 }]; - let xfx_cache = &mut self.xfx_cache[if len == 1 { 0 } else { 1 }]; + let xfx = &mut self.xfx[idx]; + let xfx_cache = &mut self.xfx_cache[idx]; *xfx_cache .entry((pid, ix2, imuf2)) .or_insert_with(|| xfx(pid, x, muf2)) + } else { + x } } From bf0208430ed653f5ebadcceb17323e56f074fe98 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 4 Oct 2024 10:44:06 +0200 Subject: [PATCH 143/213] Generalize `ConvolutionCache::fx_prod` --- pineappl/src/convolutions.rs | 75 ++++++++++++++---------------------- pineappl/src/grid.rs | 8 ++-- pineappl_cli/tests/evolve.rs | 26 ++++++------- pineappl_cli/tests/export.rs | 14 +++---- pineappl_cli/tests/import.rs | 14 +++---- pineappl_cli/tests/write.rs | 16 ++++---- 6 files changed, 66 insertions(+), 87 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 250ce57b..db8d1ce8 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -211,55 +211,36 @@ impl<'a> ConvolutionCache<'a> { Ok(()) } - /// Return the PDF (multiplied with `x`) for the first initial state. - pub fn xfx1(&mut self, pdg_id: i32, ix1: usize, imu2: usize) -> f64 { - let ix1 = self.ix[0][ix1]; - let x = self.x_grid[ix1]; - if let Some((idx, cc)) = self.perm[0] { - let imuf2 = self.imuf2[imu2]; - let muf2 = self.muf2_grid[imuf2]; - let pid = if cc { - pids::charge_conjugate_pdg_pid(pdg_id) - } else { - pdg_id - }; - let xfx = &mut self.xfx[idx]; - let xfx_cache = &mut self.xfx_cache[idx]; - *xfx_cache - .entry((pid, ix1, imuf2)) - .or_insert_with(|| xfx(pid, x, muf2)) - } else { - x - } - } - - /// Return the PDF (multiplied with `x`) for the second initial state. - pub fn xfx2(&mut self, pdg_id: i32, ix2: usize, imu2: usize) -> f64 { - let ix2 = self.ix[1][ix2]; - let x = self.x_grid[ix2]; - if let Some((idx, cc)) = self.perm[1] { - let imuf2 = self.imuf2[imu2]; - let muf2 = self.muf2_grid[imuf2]; - let pid = if cc { - pids::charge_conjugate_pdg_pid(pdg_id) - } else { - pdg_id - }; - let xfx = &mut self.xfx[idx]; - let xfx_cache = &mut self.xfx_cache[idx]; - *xfx_cache - .entry((pid, ix2, imuf2)) - .or_insert_with(|| xfx(pid, x, muf2)) - } else { - x - } - } - /// TODO pub fn fx_prod(&mut self, pdg_ids: &[i32], indices: &[usize]) -> f64 { - self.xfx1(pdg_ids[0], indices[1], indices[0]) - * self.xfx2(pdg_ids[1], indices[2], indices[0]) - / (self.x_grid[self.ix[0][indices[1]]] * self.x_grid[self.ix[1][indices[2]]]) + // TODO: here we assume that + // - indices[0] is the (squared) factorization scale, + // - indices[1] is x1 and + // - indices[2] is x2. + // Lift this restriction! + self.perm + .iter() + .zip(pdg_ids) + .enumerate() + .filter_map(|(index, (perm, &pdg_id))| { + perm.map(|(idx, cc)| { + let ix = self.ix[index][indices[index + 1]]; + let imuf2 = self.imuf2[indices[0]]; + let muf2 = self.muf2_grid[imuf2]; + let pid = if cc { + pids::charge_conjugate_pdg_pid(pdg_id) + } else { + pdg_id + }; + let xfx = &mut self.xfx[idx]; + let xfx_cache = &mut self.xfx_cache[idx]; + *xfx_cache.entry((pid, ix, imuf2)).or_insert_with(|| { + let x = self.x_grid[ix]; + xfx(pid, x, muf2) / x + }) + }) + }) + .product() } /// Return the strong coupling for the renormalization scale set with [`LumiCache::set_grids`], diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index f1a2e68c..e4a0493c 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -371,15 +371,13 @@ impl Grid { for (idx, value) in subgrid.indexed_iter() { assert_eq!(idx.len(), 3); - let x1 = node_values[1][idx[1]]; - let x2 = node_values[2][idx[2]]; let mut lumi = 0.0; for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); - let xfx1 = convolution_cache.xfx1(entry.0[0], idx[1], idx[0]); - let xfx2 = convolution_cache.xfx2(entry.0[1], idx[2], idx[0]); - lumi += xfx1 * xfx2 * entry.1 / (x1 * x2); + // TODO: we assume `idx` to be ordered as scale, x1, x2 + let fx_prod = convolution_cache.fx_prod(&entry.0, &idx); + lumi += fx_prod * entry.1; } let alphas = convolution_cache.alphas(idx[0]); diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index e93a83a3..e6b57616 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -70,37 +70,37 @@ const LHCB_WP_7TEV_V2_STR: &str = "b Grid FkTable -+--------------------+--------------------+---------------------- 0 7.8752126798068639e2 7.8731064380928558e2 -2.6745204220435248e-4 1 7.1872113080347663e2 7.1853123147848032e2 -2.6421836906898033e-4 -2 6.2322357391848550e2 6.2306009928459093e2 -2.6230495882362259e-4 -3 5.0216762988872915e2 5.0203737363369049e2 -2.5938799573266280e-4 -4 3.7314505699003126e2 3.7305089832847733e2 -2.5233795755852384e-4 +2 6.2322357391848550e2 6.2306009928459105e2 -2.6230495882340055e-4 +3 5.0216762988872927e2 5.0203737363369049e2 -2.5938799573288485e-4 +4 3.7314505699003132e2 3.7305089832847727e2 -2.5233795755885691e-4 5 2.5302044227292129e2 2.5295968261889854e2 -2.4013733229188983e-4 -6 1.1971045984774410e2 1.1968525412249538e2 -2.1055574659711862e-4 -7 2.9272102213930090e1 2.9268443366651141e1 -1.2499434622803562e-4 +6 1.1971045984774410e2 1.1968525412249534e2 -2.1055574659745169e-4 +7 2.9272102213930090e1 2.9268443366651130e1 -1.2499434622836869e-4 "; const LHCB_WP_7TEV_V2_XIR2_STR: &str = "b Grid FkTable rel. diff -+--------------------+--------------------+---------------------- 0 7.7634833292737017e2 7.7614037816519419e2 -2.6786270203205120e-4 -1 7.0866199875124983e2 7.0847444839781781e2 -2.6465417048249229e-4 -2 6.1427556024981789e2 6.1411417374531106e2 -2.6272655946324441e-4 -3 4.9482819982783724e2 4.9469964081143053e2 -2.5980535557890150e-4 +1 7.0866199875124983e2 7.0847444839781758e2 -2.6465417048282536e-4 +2 6.1427556024981789e2 6.1411417374531095e2 -2.6272655946346646e-4 +3 4.9482819982783735e2 4.9469964081143053e2 -2.5980535557912354e-4 4 3.6756257449354945e2 3.6746967569489709e2 -2.5274281196974169e-4 5 2.4912642701834142e2 2.4906651029915440e2 -2.4050727939273209e-4 6 1.1776254040032327e2 1.1773772039493414e2 -2.1076316207813139e-4 -7 2.8749891297668260e1 2.8746299479656258e1 -1.2493327278395583e-4 +7 2.8749891297668267e1 2.8746299479656273e1 -1.2493327278373378e-4 "; const LHCB_WP_7TEV_V2_XIF_2_STR: &str = "b Grid FkTable rel. diff -+--------------------+--------------------+---------------------- -0 8.0902449713533758e2 8.0880109089579207e2 -2.7614273774967391e-4 -1 7.3869242569893402e2 7.3849113100483919e2 -2.7250136469769703e-4 +0 8.0902449713533770e2 8.0880109089579207e2 -2.7614273774978493e-4 +1 7.3869242569893413e2 7.3849113100483896e2 -2.7250136469814112e-4 2 6.4102495904778243e2 6.4085178025871448e2 -2.7015919836448354e-4 3 5.1668563837653949e2 5.1654786167667771e2 -2.6665478896348294e-4 4 3.8405066991124284e2 3.8395127677619655e2 -2.5880213949180941e-4 -5 2.6047697125229388e2 2.6041295913273854e2 -2.4574963094659008e-4 +5 2.6047697125229377e2 2.6041295913273854e2 -2.4574963094614599e-4 6 1.2324364745022301e2 1.2321715784184289e2 -2.1493690691698486e-4 -7 3.0134629982656573e1 3.0130872371345841e1 -1.2469412476256991e-4 +7 3.0134629982656573e1 3.0130872371345848e1 -1.2469412476234787e-4 "; const LHCB_WP_7TEV_V2_XIF_2_ERROR_STR: &str = "Error: failed to evolve grid: no operator for muf2 = 25825.775616000003 found in [6456.443904000001] diff --git a/pineappl_cli/tests/export.rs b/pineappl_cli/tests/export.rs index 33b748f2..80a1cdab 100644 --- a/pineappl_cli/tests/export.rs +++ b/pineappl_cli/tests/export.rs @@ -34,16 +34,16 @@ b APPLgrid PineAPPL rel. diff 1 2.3289219e1 2.3289219e1 -1.9984014e-15 2 3.7442697e1 3.7442697e1 -2.1094237e-15 3 5.0087316e1 5.0087316e1 -3.1086245e-15 -4 6.0873237e1 6.0873237e1 -2.8865799e-15 -5 6.8944378e1 6.8944378e1 -3.8857806e-15 +4 6.0873237e1 6.0873237e1 -2.5535130e-15 +5 6.8944378e1 6.8944378e1 -3.5527137e-15 6 7.4277783e1 7.4277783e1 -2.8865799e-15 -7 7.6356931e1 7.6356931e1 -3.7747583e-15 -8 7.5009607e1 7.5009607e1 -1.5543122e-15 -9 7.0045787e1 7.0045787e1 -1.2212453e-15 +7 7.6356931e1 7.6356931e1 -3.3306691e-15 +8 7.5009607e1 7.5009607e1 -1.8873791e-15 +9 7.0045787e1 7.0045787e1 -7.7715612e-16 10 6.0009803e1 6.0009803e1 -7.7715612e-16 -11 4.6770515e1 4.6770515e1 4.4408921e-16 +11 4.6770515e1 4.6770515e1 2.2204460e-16 12 3.3569217e1 3.3569217e1 1.5543122e-15 -13 2.1820341e1 2.1820341e1 1.3322676e-15 +13 2.1820341e1 2.1820341e1 1.1102230e-15 14 1.2542026e1 1.2542026e1 2.2204460e-16 15 6.0879666e0 6.0879666e0 -1.3322676e-15 16 1.5789361e0 1.5789361e0 -1.5543122e-15 diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 7f20aca5..c7b92c5a 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -52,7 +52,7 @@ Options: #[cfg(feature = "fastnlo")] const IMPORT_FIX_GRID_STR: &str = "b PineAPPL fastNLO rel. diff -+------------+------------+-------------- -0 2.9158424e-4 2.9158424e-4 -2.7755576e-15 +0 2.9158424e-4 2.9158424e-4 -2.5535130e-15 1 2.4657895e-4 2.4657895e-4 -2.6645353e-15 "; @@ -251,13 +251,13 @@ const IMPORT_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff const IMPORT_NEW_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff --+-----------+-----------+-------------- 0 6.2634897e2 6.2634897e2 1.5543122e-15 -1 6.2847078e2 6.2847078e2 0.0000000e0 -2 6.3163323e2 6.3163323e2 2.2204460e-16 +1 6.2847078e2 6.2847078e2 -2.2204460e-16 +2 6.3163323e2 6.3163323e2 0.0000000e0 3 6.3586556e2 6.3586556e2 2.2204460e-16 4 6.4139163e2 6.4139163e2 1.7763568e-15 -5 6.4848088e2 6.4848088e2 -2.4424907e-15 -6 6.5354150e2 6.5354150e2 -3.4416914e-15 -7 6.5377566e2 6.5377566e2 -1.4432899e-15 +5 6.4848088e2 6.4848088e2 -2.3314684e-15 +6 6.5354150e2 6.5354150e2 -3.6637360e-15 +7 6.5377566e2 6.5377566e2 -1.5543122e-15 8 6.5094729e2 6.5094729e2 1.7763568e-15 9 6.3588760e2 6.3588760e2 2.2204460e-15 10 5.9810718e2 5.9810718e2 2.2204460e-15 @@ -275,7 +275,7 @@ const IMPORT_DIS_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff -+------------+------------+-------------- 0 9.3514881e-2 9.3514881e-2 -3.3306691e-16 1 3.9993061e-2 3.9993061e-2 2.2204460e-16 -2 1.3593440e-2 1.3593440e-2 -2.2204460e-16 +2 1.3593440e-2 1.3593440e-2 0.0000000e0 3 2.0825199e-3 2.0825199e-3 -4.4408921e-16 "; diff --git a/pineappl_cli/tests/write.rs b/pineappl_cli/tests/write.rs index b3f50637..7d4bdd53 100644 --- a/pineappl_cli/tests/write.rs +++ b/pineappl_cli/tests/write.rs @@ -243,14 +243,14 @@ const ROTATE_PID_BASIS_NO_DIFF_STR: &str = "b x1 O(as^0 a^2) const ROTATE_PID_BASIS_DIFF_STR: &str = "b x1 O(as^0 a^2) O(as^0 a^3) O(as^1 a^2) -+----+----+-----------+-----------+----------+-------------+-------------+----------+-----------+-----------+---------- -0 2 2.25 6.5070305e2 6.5070305e2 -2.220e-16 -7.8692484e0 -7.8692484e0 -4.441e-16 1.1175729e2 1.1175729e2 -1.221e-15 -1 2.25 2.5 5.9601236e2 5.9601236e2 -7.772e-16 -6.5623495e0 -6.5623495e0 -2.220e-16 1.0083341e2 1.0083341e2 -5.551e-16 -2 2.5 2.75 5.1561247e2 5.1561247e2 -8.882e-16 -5.2348261e0 -5.2348261e0 -6.661e-16 8.9874343e1 8.9874343e1 -1.221e-15 -3 2.75 3 4.1534629e2 4.1534629e2 -4.441e-16 -3.7590420e0 -3.7590420e0 -5.551e-16 7.3935106e1 7.3935106e1 -1.554e-15 -4 3 3.25 3.0812719e2 3.0812719e2 -3.331e-16 -2.5871885e0 -2.5871885e0 -5.551e-16 5.6414554e1 5.6414554e1 -2.220e-16 -5 3.25 3.5 2.0807482e2 2.0807482e2 -6.661e-16 -1.6762487e0 -1.6762487e0 -1.110e-16 3.9468336e1 3.9468336e1 -6.661e-16 -6 3.5 4 9.6856769e1 9.6856769e1 -3.331e-16 -8.1027456e-1 -8.1027456e-1 -1.110e-16 1.9822014e1 1.9822014e1 -1.110e-15 -7 4 4.5 2.2383492e1 2.2383492e1 -3.331e-16 -2.2022770e-1 -2.2022770e-1 -5.551e-16 5.3540011e0 5.3540011e0 0.000e0 +0 2 2.25 6.5070305e2 6.5070305e2 -5.551e-16 -7.8692484e0 -7.8692484e0 -4.441e-16 1.1175729e2 1.1175729e2 -1.221e-15 +1 2.25 2.5 5.9601236e2 5.9601236e2 -7.772e-16 -6.5623495e0 -6.5623495e0 -4.441e-16 1.0083341e2 1.0083341e2 0.000e0 +2 2.5 2.75 5.1561247e2 5.1561247e2 -8.882e-16 -5.2348261e0 -5.2348261e0 -8.882e-16 8.9874343e1 8.9874343e1 -1.221e-15 +3 2.75 3 4.1534629e2 4.1534629e2 -4.441e-16 -3.7590420e0 -3.7590420e0 -6.661e-16 7.3935106e1 7.3935106e1 -1.554e-15 +4 3 3.25 3.0812719e2 3.0812719e2 -5.551e-16 -2.5871885e0 -2.5871885e0 -5.551e-16 5.6414554e1 5.6414554e1 0.000e0 +5 3.25 3.5 2.0807482e2 2.0807482e2 -5.551e-16 -1.6762487e0 -1.6762487e0 -2.220e-16 3.9468336e1 3.9468336e1 -6.661e-16 +6 3.5 4 9.6856769e1 9.6856769e1 -4.441e-16 -8.1027456e-1 -8.1027456e-1 -4.441e-16 1.9822014e1 1.9822014e1 -1.110e-15 +7 4 4.5 2.2383492e1 2.2383492e1 -6.661e-16 -2.2022770e-1 -2.2022770e-1 -5.551e-16 5.3540011e0 5.3540011e0 -6.661e-16 "; const ROTATE_PID_BASIS_READ_CHANNELS_STR: &str = " c entry From c196936dab6cff3aab088d1c301ca4379cc4d923 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 4 Oct 2024 11:02:36 +0200 Subject: [PATCH 144/213] Add generalized constructor for `ConvolutionCache` --- pineappl/src/convolutions.rs | 56 +++++++--------------------------- pineappl/tests/drell_yan_lo.rs | 4 +-- pineappl_capi/src/lib.rs | 10 +++--- pineappl_cli/src/helpers.rs | 49 +++++++---------------------- pineappl_cli/tests/convolve.rs | 17 ----------- pineappl_cli/tests/import.rs | 2 +- pineappl_cli/tests/plot.rs | 20 ------------ 7 files changed, 30 insertions(+), 128 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index db8d1ce8..2f88034d 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -25,58 +25,24 @@ pub struct ConvolutionCache<'a> { } impl<'a> ConvolutionCache<'a> { - /// Construct a luminosity cache with two PDFs, `xfx1` and `xfx2`. The types of hadrons the - /// PDFs correspond to must be given as `pdg1` and `pdg2`. The function to evaluate the - /// strong coupling must be given as `alphas`. The grid that the cache will be used with must - /// be given as `grid`; this parameter determines which of the initial states are hadronic, and - /// if an initial states is not hadronic the corresponding 'PDF' is set to `xfx = x`. If some - /// of the PDFs must be charge-conjugated, this is automatically done in this function. - pub fn with_two( - pdg1: i32, - xfx1: &'a mut dyn FnMut(i32, f64, f64) -> f64, - pdg2: i32, - xfx2: &'a mut dyn FnMut(i32, f64, f64) -> f64, - alphas: &'a mut dyn FnMut(f64) -> f64, - ) -> Self { - Self { - xfx: vec![xfx1, xfx2], - xfx_cache: vec![FxHashMap::default(); 2], - alphas, - alphas_cache: vec![], - mur2_grid: vec![], - muf2_grid: vec![], - x_grid: vec![], - imur2: Vec::new(), - imuf2: Vec::new(), - ix: Vec::new(), - pdg: vec![pdg1, pdg2], - perm: Vec::new(), - } - } - - /// Construct a luminosity cache with a single PDF `xfx`. The type of hadron the PDF - /// corresponds to must be given as `pdg`. The function to evaluate the strong coupling must be - /// given as `alphas`. The grid that the cache should be used with must be given as `grid`; - /// this parameter determines which of the initial states are hadronic, and if an initial - /// states is not hadronic the corresponding 'PDF' is set to `xfx = x`. If some of the PDFs - /// must be charge-conjugated, this is automatically done in this function. - pub fn with_one( - pdg: i32, - xfx: &'a mut dyn FnMut(i32, f64, f64) -> f64, + /// TODO + pub fn new( + pdg: Vec, + xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { Self { - xfx: vec![xfx], - xfx_cache: vec![FxHashMap::default()], + xfx_cache: vec![FxHashMap::default(); xfx.len()], + xfx, alphas, - alphas_cache: vec![], - mur2_grid: vec![], - muf2_grid: vec![], - x_grid: vec![], + alphas_cache: Vec::new(), + mur2_grid: Vec::new(), + muf2_grid: Vec::new(), + x_grid: Vec::new(), imur2: Vec::new(), imuf2: Vec::new(), ix: Vec::new(), - pdg: vec![pdg], + pdg, perm: Vec::new(), } } diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index a32596f6..1dd30879 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -351,7 +351,7 @@ fn perform_grid_tests( grid.scale_by_order(10.0, 1.0, 10.0, 10.0, 4.0); // TEST 5: `convolve` - let mut convolution_cache = ConvolutionCache::with_one(2212, &mut xfx, &mut alphas); + let mut convolution_cache = ConvolutionCache::new(vec![2212], vec![&mut xfx], &mut alphas); let bins = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins.iter().zip(reference.iter()) { @@ -363,7 +363,7 @@ fn perform_grid_tests( let mut xfx2 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas2 = |_| 0.0; let mut convolution_cache2 = - ConvolutionCache::with_two(2212, &mut xfx1, 2212, &mut xfx2, &mut alphas2); + ConvolutionCache::new(vec![2212, 2212], vec![&mut xfx1, &mut xfx2], &mut alphas2); let bins2 = grid.convolve(&mut convolution_cache2, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins2.iter().zip(reference.iter()) { diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index f4deb897..290c7d04 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -442,7 +442,7 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_one( results: *mut f64, ) { let grid = unsafe { &*grid }; - let mut pdf = |id, x, q2| xfx(id, x, q2, state); + let mut xfx = |id, x, q2| xfx(id, x, q2, state); let mut als = |q2| alphas(q2, state); let order_mask = if order_mask.is_null() { vec![] @@ -455,7 +455,7 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_one( unsafe { slice::from_raw_parts(channel_mask, grid.channels().len()) }.to_vec() }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; - let mut convolution_cache = ConvolutionCache::with_one(pdg_id, &mut pdf, &mut als); + let mut convolution_cache = ConvolutionCache::new(vec![pdg_id], vec![&mut xfx], &mut als); results.copy_from_slice(&grid.convolve( &mut convolution_cache, @@ -503,8 +503,8 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_two( results: *mut f64, ) { let grid = unsafe { &*grid }; - let mut pdf1 = |id, x, q2| xfx1(id, x, q2, state); - let mut pdf2 = |id, x, q2| xfx2(id, x, q2, state); + let mut xfx1 = |id, x, q2| xfx1(id, x, q2, state); + let mut xfx2 = |id, x, q2| xfx2(id, x, q2, state); let mut als = |q2| alphas(q2, state); let order_mask = if order_mask.is_null() { vec![] @@ -518,7 +518,7 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_two( }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; let mut convolution_cache = - ConvolutionCache::with_two(pdg_id1, &mut pdf1, pdg_id2, &mut pdf2, &mut als); + ConvolutionCache::new(vec![pdg_id1, pdg_id2], vec![&mut xfx1, &mut xfx2], &mut als); results.copy_from_slice(&grid.convolve( &mut convolution_cache, diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index f7a2be4d..30f6acf4 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -271,6 +271,10 @@ pub fn convolve_scales( } }) .collect(); + let xfx: Vec<_> = funs + .iter_mut() + .map(|fun| fun as &mut dyn FnMut(i32, f64, f64) -> f64) + .collect(); let mut alphas_funs: Vec<_> = conv_funs .iter() .map(|fun| move |q2| fun.alphas_q2(q2)) @@ -287,25 +291,7 @@ pub fn convolve_scales( }) .collect(); - // TODO: write a new constructor of `LumiCache` that accepts a vector of all the arguments - let mut cache = match funs.as_mut_slice() { - [funs0] => { - ConvolutionCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]) - } - [funs0, funs1] => ConvolutionCache::with_two( - pdg_ids[0], - funs0, - pdg_ids[1], - funs1, - &mut alphas_funs[cfg.use_alphas_from], - ), - // TODO: convert this into an error - _ => panic!( - "convolutions with {} convolution functions is not supported", - conv_funs.len() - ), - }; - + let mut cache = ConvolutionCache::new(pdg_ids, xfx, &mut alphas_funs[cfg.use_alphas_from]); let mut results = grid.convolve(&mut cache, &orders, bins, channels, scales); match mode { @@ -436,6 +422,10 @@ pub fn convolve_subgrid( } }) .collect(); + let xfx: Vec<_> = funs + .iter_mut() + .map(|fun| fun as &mut dyn FnMut(i32, f64, f64) -> f64) + .collect(); let mut alphas_funs: Vec<_> = conv_funs .iter() .map(|fun| move |q2| fun.alphas_q2(q2)) @@ -452,26 +442,9 @@ pub fn convolve_subgrid( }) .collect(); - // TODO: write a new constructor of `LumiCache` that accepts a vector of all the arguments - let mut cache = match funs.as_mut_slice() { - [funs0] => { - ConvolutionCache::with_one(pdg_ids[0], funs0, &mut alphas_funs[cfg.use_alphas_from]) - } - [funs0, funs1] => ConvolutionCache::with_two( - pdg_ids[0], - funs0, - pdg_ids[1], - funs1, - &mut alphas_funs[cfg.use_alphas_from], - ), - // TODO: convert this into an error - _ => panic!( - "convolutions with {} convolution functions is not supported", - conv_funs.len() - ), - }; - + let mut cache = ConvolutionCache::new(pdg_ids, xfx, &mut alphas_funs[cfg.use_alphas_from]); let subgrid = grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)); + subgrid .into_dimensionality::() .map_err(|_| anyhow!("Only 3-dimensional subgrids are supported",)) diff --git a/pineappl_cli/tests/convolve.rs b/pineappl_cli/tests/convolve.rs index dbf0d107..f7130c13 100644 --- a/pineappl_cli/tests/convolve.rs +++ b/pineappl_cli/tests/convolve.rs @@ -34,9 +34,6 @@ const DEFAULT_STR: &str = "b etal dsig/detal const USE_ALPHAS_FROM_ERROR_STR: &str = "expected `use_alphas_from` to be `0` or `1`, is `2` "; -const THREE_PDF_ERROR_STR: &str = "convolutions with 3 convolution functions is not supported -"; - const FORCE_POSITIVE_STR: &str = "b etal dsig/detal [] [pb] -+----+----+----------- @@ -211,20 +208,6 @@ fn use_alphas_from_error() { .stderr(str::contains(USE_ALPHAS_FROM_ERROR_STR)); } -#[test] -fn three_pdf_error() { - Command::cargo_bin("pineappl") - .unwrap() - .args([ - "convolve", - "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", - "NNPDF31_nlo_as_0118_luxqed,NNPDF31_nlo_as_0118_luxqed,NNPDF31_nlo_as_0118_luxqed", - ]) - .assert() - .failure() - .stderr(str::contains(THREE_PDF_ERROR_STR)); -} - #[test] fn force_positive() { Command::cargo_bin("pineappl") diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index c7b92c5a..6501c9c0 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -893,7 +893,7 @@ fn import_hadronic_fktable() { let pdf = Pdf::with_setname_and_member("NNPDF31_nlo_as_0118_luxqed", 0).unwrap(); let mut xfx = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas = |_| 0.0; - let mut convolution_cache = ConvolutionCache::with_one(2212, &mut xfx, &mut alphas); + let mut convolution_cache = ConvolutionCache::new(vec![2212], vec![&mut xfx], &mut alphas); let results = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); let mut fk_table = FkTable::try_from(grid).unwrap(); diff --git a/pineappl_cli/tests/plot.rs b/pineappl_cli/tests/plot.rs index 7309ec2f..57894a72 100644 --- a/pineappl_cli/tests/plot.rs +++ b/pineappl_cli/tests/plot.rs @@ -1416,9 +1416,6 @@ if __name__ == "__main__": main() "#; -const THREE_PDF_ERROR_STR: &str = "convolutions with 3 convolution functions is not supported -"; - #[test] fn help() { Command::cargo_bin("pineappl") @@ -1469,23 +1466,6 @@ fn subgrid_pull() { .stdout(SUBGRID_PULL_STR); } -#[test] -fn three_pdf_error() { - Command::cargo_bin("pineappl") - .unwrap() - .args([ - "plot", - "--subgrid-pull=0,0,0", - "--threads=1", - "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", - "NNPDF31_nlo_as_0118_luxqed", - "NNPDF40_nnlo_as_01180,NNPDF40_nnlo_as_01180,NNPDF40_nnlo_as_01180", - ]) - .assert() - .failure() - .stderr(str::contains(THREE_PDF_ERROR_STR)); -} - #[test] fn drell_yan_afb() { Command::cargo_bin("pineappl") From 9d63ba7f50d97c95697abbfbef3897687bab0e19 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 4 Oct 2024 11:49:47 +0200 Subject: [PATCH 145/213] Polish `PackedArray` a bit --- pineappl/src/lagrange_subgrid.rs | 6 +- pineappl/src/packed_array.rs | 179 +++++++++++++++++-------------- pineappl/src/packed_subgrid.rs | 6 +- 3 files changed, 106 insertions(+), 85 deletions(-) diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 2b3c3bc4..feb5e137 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -57,7 +57,7 @@ impl Subgrid for LagrangeSubgridV2 { // we cannot use `Self::indexed_iter` because it multiplies with `reweight` if let SubgridEnum::LagrangeSubgridV2(other) = other { // TODO: make sure `other` has the same interpolation as `self` - for (mut index, value) in other.array.indexed_iter3() { + for (mut index, value) in other.array.indexed_iter() { if let Some((a, b)) = transpose { index.swap(a, b); } @@ -75,7 +75,7 @@ impl Subgrid for LagrangeSubgridV2 { fn symmetrize(&mut self, a: usize, b: usize) { let mut new_array = PackedArray::new(self.array.shape().to_vec()); - for (mut index, sigma) in self.array.indexed_iter3() { + for (mut index, sigma) in self.array.indexed_iter() { // TODO: why not the other way around? if index[b] < index[a] { index.swap(a, b); @@ -90,7 +90,7 @@ impl Subgrid for LagrangeSubgridV2 { fn indexed_iter(&self) -> SubgridIndexedIter { let nodes: Vec<_> = self.interps.iter().map(Interp::node_values).collect(); - Box::new(self.array.indexed_iter3().map(move |(indices, weight)| { + Box::new(self.array.indexed_iter().map(move |(indices, weight)| { let reweight = self .interps .iter() diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index ef63663a..14e40057 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -78,22 +78,7 @@ impl PackedArray { } /// TODO - pub fn indexed_iter2(&self) -> impl Iterator + '_ { - self.start_indices - .iter() - .zip(&self.lengths) - .flat_map(|(&start_index, &length)| start_index..(start_index + length)) - .zip(&self.entries) - .filter(|&(_, entry)| *entry != Default::default()) - .map(|(indices, entry)| (indices, *entry)) - } - - /// Returns an `Iterator` over the non-default (non-zero) elements of this array. The type of - /// an iterator element is `([usize; D], T)` where the first element of the tuple is the index - /// and the second element is the value. - pub fn indexed_iter(&self) -> impl Iterator + '_ { - assert_eq!(self.shape.len(), D); - + pub fn indexed_iter(&self) -> impl Iterator, T)> + '_ { self.start_indices .iter() .zip(&self.lengths) @@ -105,19 +90,6 @@ impl PackedArray { .map(|(indices, entry)| (indices, *entry)) } - /// TODO - pub fn indexed_iter3(&self) -> impl Iterator, T)> + '_ { - self.start_indices - .iter() - .zip(&self.lengths) - .flat_map(|(&start_index, &length)| { - (start_index..(start_index + length)).map(|i| unravel_index2(i, &self.shape)) - }) - .zip(&self.entries) - .filter(|&(_, entry)| *entry != Default::default()) - .map(|(indices, entry)| (indices, *entry)) - } - /// TODO // TODO: rewrite this method into `sub_block_iter_mut() -> impl Iterator` pub fn sub_block_idx( @@ -181,24 +153,8 @@ fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { .fold(0, |acc, (i, d)| acc * d + i) } -/// Converts a flat `index` into a `multi_index`. -/// -/// # Panics -/// -/// Panics when `index` is out of range. -#[must_use] -fn unravel_index(mut index: usize, shape: &[usize]) -> [usize; D] { - assert!(index < shape.iter().product()); - let mut indices = [0; D]; - for (i, d) in indices.iter_mut().zip(shape).rev() { - *i = index % d; - index /= d; - } - indices -} - /// TODO -pub fn unravel_index2(mut index: usize, shape: &[usize]) -> Vec { +pub fn unravel_index(mut index: usize, shape: &[usize]) -> Vec { assert!(index < shape.iter().product()); let mut indices = vec![0; shape.len()]; for (i, d) in indices.iter_mut().zip(shape).rev() { @@ -255,13 +211,11 @@ impl Index for PackedArray { type Output = T; fn index(&self, index: usize) -> &Self::Output { - // assert_eq!(index.len(), self.shape.len()); - // assert!( - // index.iter().zip(self.shape.iter()).all(|(&i, &d)| i < d), - // "index {:?} is out of bounds for array of shape {:?}", - // index, - // self.shape - // ); + assert!( + index < self.shape.iter().product(), + "index {index} is out of bounds for array of shape {:?}", + self.shape + ); let raveled_index = index; // let raveled_index = ravel_multi_index(&index, &self.shape); @@ -534,16 +488,6 @@ mod tests { use ndarray::Array3; use std::mem; - #[test] - fn unravel_index() { - assert_eq!(super::unravel_index(0, &[3, 2]), [0, 0]); - assert_eq!(super::unravel_index(1, &[3, 2]), [0, 1]); - assert_eq!(super::unravel_index(2, &[3, 2]), [1, 0]); - assert_eq!(super::unravel_index(3, &[3, 2]), [1, 1]); - assert_eq!(super::unravel_index(4, &[3, 2]), [2, 0]); - assert_eq!(super::unravel_index(5, &[3, 2]), [2, 1]); - } - #[test] fn ravel_multi_index() { assert_eq!(super::ravel_multi_index(&[0, 0], &[3, 2]), 0); @@ -608,6 +552,61 @@ mod tests { assert_eq!(a.lengths, vec![8]); } + #[test] + fn flat_index() { + let shape = vec![4, 2]; + let mut a = PackedArray::new(shape.clone()); + + a[[0, 0]] = 1; + assert_eq!(a[super::ravel_multi_index(&[0, 0], &shape)], 1); + assert_eq!(a.entries, vec![1]); + assert_eq!(a.start_indices, vec![0]); + assert_eq!(a.lengths, vec![1]); + + a[[3, 0]] = 2; + assert_eq!(a[super::ravel_multi_index(&[0, 0], &shape)], 1); + assert_eq!(a[super::ravel_multi_index(&[3, 0], &shape)], 2); + assert_eq!(a.entries, vec![1, 2]); + assert_eq!(a.start_indices, vec![0, 6]); + assert_eq!(a.lengths, vec![1, 1]); + + a[[3, 1]] = 3; + assert_eq!(a[super::ravel_multi_index(&[0, 0], &shape)], 1); + assert_eq!(a[super::ravel_multi_index(&[3, 0], &shape)], 2); + assert_eq!(a[super::ravel_multi_index(&[3, 1], &shape)], 3); + assert_eq!(a.entries, vec![1, 2, 3]); + assert_eq!(a.start_indices, vec![0, 6]); + assert_eq!(a.lengths, vec![1, 2]); + + a[[2, 0]] = 9; + assert_eq!(a[super::ravel_multi_index(&[0, 0], &shape)], 1); + assert_eq!(a[super::ravel_multi_index(&[3, 0], &shape)], 2); + assert_eq!(a[super::ravel_multi_index(&[3, 1], &shape)], 3); + assert_eq!(a[super::ravel_multi_index(&[2, 0], &shape)], 9); + assert_eq!(a.entries, vec![1, 9, 0, 2, 3]); + assert_eq!(a.start_indices, vec![0, 4]); + assert_eq!(a.lengths, vec![1, 4]); + + a[[2, 0]] = 4; + assert_eq!(a[super::ravel_multi_index(&[0, 0], &shape)], 1); + assert_eq!(a[super::ravel_multi_index(&[3, 0], &shape)], 2); + assert_eq!(a[super::ravel_multi_index(&[3, 1], &shape)], 3); + assert_eq!(a[super::ravel_multi_index(&[2, 0], &shape)], 4); + assert_eq!(a.entries, vec![1, 4, 0, 2, 3]); + assert_eq!(a.start_indices, vec![0, 4]); + assert_eq!(a.lengths, vec![1, 4]); + + a[[1, 0]] = 5; + assert_eq!(a[super::ravel_multi_index(&[0, 0], &shape)], 1); + assert_eq!(a[super::ravel_multi_index(&[3, 0], &shape)], 2); + assert_eq!(a[super::ravel_multi_index(&[3, 1], &shape)], 3); + assert_eq!(a[super::ravel_multi_index(&[2, 0], &shape)], 4); + assert_eq!(a[super::ravel_multi_index(&[1, 0], &shape)], 5); + assert_eq!(a.entries, vec![1, 0, 5, 0, 4, 0, 2, 3]); + assert_eq!(a.start_indices, vec![0]); + assert_eq!(a.lengths, vec![8]); + } + #[test] fn iter() { let mut a = PackedArray::new(vec![6, 5]); @@ -619,11 +618,11 @@ mod tests { assert_eq!( a.indexed_iter().collect::>(), &[ - ([2, 2], 1), - ([2, 4], 2), - ([4, 1], 3), - ([4, 4], 4), - ([5, 0], 5), + (vec![2, 2], 1), + (vec![2, 4], 2), + (vec![4, 1], 3), + (vec![4, 4], 4), + (vec![5, 0], 5), ] ); } @@ -846,6 +845,28 @@ mod tests { assert_eq!(array[[0, 0, 2]], 0); } + #[test] + #[should_panic(expected = "entry at index 0 is implicitly set to the default value")] + fn flat_index_panic_0() { + let shape = vec![40, 50, 50]; + let mut array = PackedArray::new(shape.clone()); + + array[[1, 0, 0]] = 1; + + let _ = array[super::ravel_multi_index(&[0, 0, 0], &shape)]; + } + + #[test] + #[should_panic(expected = "index 102550 is out of bounds for array of shape [40, 50, 50]")] + fn flat_index_panic_dim1() { + let shape = vec![40, 50, 50]; + let mut array = PackedArray::new(shape.clone()); + + array[[1, 0, 0]] = 1; + + let _ = array[super::ravel_multi_index(&[40, 50, 50], &shape)]; + } + #[test] fn indexed_iter() { let mut array = PackedArray::new(vec![40, 50, 50]); @@ -854,7 +875,7 @@ mod tests { assert_eq!(array.shape(), [40, 50, 50]); // check empty iterator - assert_eq!(array.indexed_iter::<3>().next(), None); + assert_eq!(array.indexed_iter().next(), None); // insert an element array[[2, 3, 4]] = 1; @@ -862,7 +883,7 @@ mod tests { let mut iter = array.indexed_iter(); // check iterator with one element - assert_eq!(iter.next(), Some(([2, 3, 4], 1))); + assert_eq!(iter.next(), Some((vec![2, 3, 4], 1))); assert_eq!(iter.next(), None); mem::drop(iter); @@ -872,8 +893,8 @@ mod tests { let mut iter = array.indexed_iter(); - assert_eq!(iter.next(), Some(([2, 3, 4], 1))); - assert_eq!(iter.next(), Some(([2, 3, 6], 2))); + assert_eq!(iter.next(), Some((vec![2, 3, 4], 1))); + assert_eq!(iter.next(), Some((vec![2, 3, 6], 2))); assert_eq!(iter.next(), None); mem::drop(iter); @@ -883,9 +904,9 @@ mod tests { let mut iter = array.indexed_iter(); - assert_eq!(iter.next(), Some(([2, 3, 4], 1))); - assert_eq!(iter.next(), Some(([2, 3, 6], 2))); - assert_eq!(iter.next(), Some(([4, 5, 7], 3))); + assert_eq!(iter.next(), Some((vec![2, 3, 4], 1))); + assert_eq!(iter.next(), Some((vec![2, 3, 6], 2))); + assert_eq!(iter.next(), Some((vec![4, 5, 7], 3))); assert_eq!(iter.next(), None); mem::drop(iter); @@ -895,10 +916,10 @@ mod tests { let mut iter = array.indexed_iter(); - assert_eq!(iter.next(), Some(([2, 0, 0], 4))); - assert_eq!(iter.next(), Some(([2, 3, 4], 1))); - assert_eq!(iter.next(), Some(([2, 3, 6], 2))); - assert_eq!(iter.next(), Some(([4, 5, 7], 3))); + assert_eq!(iter.next(), Some((vec![2, 0, 0], 4))); + assert_eq!(iter.next(), Some((vec![2, 3, 4], 1))); + assert_eq!(iter.next(), Some((vec![2, 3, 6], 2))); + assert_eq!(iter.next(), Some((vec![4, 5, 7], 3))); assert_eq!(iter.next(), None); } diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 875d3403..4b604580 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -50,7 +50,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { let mut array = PackedArray::new(new_node_values.iter().map(NodeValues::len).collect()); - for (indices, value) in self.array.indexed_iter3() { + for (indices, value) in self.array.indexed_iter() { let target: Vec<_> = izip!(indices, &new_node_values, &lhs_node_values) .map(|(index, new, lhs)| new.find(lhs.get(index)).unwrap()) .collect(); @@ -82,7 +82,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { fn symmetrize(&mut self, a: usize, b: usize) { let mut new_array = PackedArray::new(self.array.shape().to_vec()); - for (mut index, sigma) in self.array.indexed_iter3() { + for (mut index, sigma) in self.array.indexed_iter() { // TODO: why not the other way around? if index[b] < index[a] { index.swap(a, b); @@ -95,7 +95,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } fn indexed_iter(&self) -> SubgridIndexedIter { - Box::new(self.array.indexed_iter3()) + Box::new(self.array.indexed_iter()) } fn stats(&self) -> Stats { From 6e3a578b220f23fec88a3e39ddf702daef3088ba Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 4 Oct 2024 12:06:59 +0200 Subject: [PATCH 146/213] Increase test coverage of `packed_array.rs` --- pineappl/src/packed_array.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 14e40057..8cba4dd7 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -802,7 +802,7 @@ mod tests { array[[1, 0, 0]] = 1; - assert_eq!(array[[0, 0, 0]], 0); + let _ = array[[0, 0, 0]]; } #[test] @@ -812,7 +812,7 @@ mod tests { array[[1, 0, 0]] = 1; - assert_eq!(array[[2, 0, 0]], 0); + let _ = array[[2, 0, 0]]; } #[test] @@ -822,7 +822,7 @@ mod tests { array[[1, 0, 0]] = 1; - assert_eq!(array[[1, 50, 0]], 0); + let _ = array[[1, 50, 0]]; } #[test] @@ -832,7 +832,7 @@ mod tests { array[[0, 0, 1]] = 1; - assert_eq!(array[[0, 0, 0]], 0); + let _ = array[[0, 0, 0]]; } #[test] @@ -842,7 +842,7 @@ mod tests { array[[0, 0, 1]] = 1; - assert_eq!(array[[0, 0, 2]], 0); + let _ = array[[0, 0, 2]]; } #[test] @@ -856,9 +856,20 @@ mod tests { let _ = array[super::ravel_multi_index(&[0, 0, 0], &shape)]; } + #[test] + #[should_panic(expected = "entry at index 2 is implicitly set to the default value")] + fn flat_index_panic_2() { + let shape = vec![40, 50, 50]; + let mut array = PackedArray::new(shape.clone()); + + array[[0, 0, 1]] = 1; + + let _ = array[super::ravel_multi_index(&[0, 0, 2], &shape)]; + } + #[test] #[should_panic(expected = "index 102550 is out of bounds for array of shape [40, 50, 50]")] - fn flat_index_panic_dim1() { + fn flat_index_panic_102550() { let shape = vec![40, 50, 50]; let mut array = PackedArray::new(shape.clone()); From 2a4f79c87d13b31010e7125b7d72fcf9a8c11cb0 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 4 Oct 2024 15:47:05 +0200 Subject: [PATCH 147/213] Save convolution type in `ConvolutionCache` --- pineappl/src/convolutions.rs | 12 +++---- pineappl/tests/drell_yan_lo.rs | 13 +++++-- pineappl_capi/src/lib.rs | 16 +++++++-- pineappl_cli/src/helpers.rs | 66 +++++++++++++++++++++++++++------- pineappl_cli/tests/import.rs | 6 +++- 5 files changed, 87 insertions(+), 26 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 2f88034d..f4a7f5b6 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -20,14 +20,14 @@ pub struct ConvolutionCache<'a> { imur2: Vec, imuf2: Vec, ix: Vec>, - pdg: Vec, + pdg: Vec, perm: Vec>, } impl<'a> ConvolutionCache<'a> { /// TODO pub fn new( - pdg: Vec, + pdg: Vec, xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { @@ -58,16 +58,16 @@ impl<'a> ConvolutionCache<'a> { .iter() .enumerate() .map(|(max_idx, conv)| { - conv.pid().map(|pid| { + conv.pid().map(|_| { self.pdg .iter() .take(max_idx + 1) .enumerate() .rev() - .find_map(|(idx, &pdg)| { - if pid == pdg { + .find_map(|(idx, pdg)| { + if conv == pdg { Some((idx, false)) - } else if pid == pids::charge_conjugate_pdg_pid(pdg) { + } else if *conv == pdg.charge_conjugate() { Some((idx, true)) } else { None diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 1dd30879..3b411ebd 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -351,7 +351,11 @@ fn perform_grid_tests( grid.scale_by_order(10.0, 1.0, 10.0, 10.0, 4.0); // TEST 5: `convolve` - let mut convolution_cache = ConvolutionCache::new(vec![2212], vec![&mut xfx], &mut alphas); + let mut convolution_cache = ConvolutionCache::new( + vec![Convolution::UnpolPDF(2212)], + vec![&mut xfx], + &mut alphas, + ); let bins = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins.iter().zip(reference.iter()) { @@ -362,8 +366,11 @@ fn perform_grid_tests( let mut xfx1 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut xfx2 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas2 = |_| 0.0; - let mut convolution_cache2 = - ConvolutionCache::new(vec![2212, 2212], vec![&mut xfx1, &mut xfx2], &mut alphas2); + let mut convolution_cache2 = ConvolutionCache::new( + vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + vec![&mut xfx1, &mut xfx2], + &mut alphas2, + ); let bins2 = grid.convolve(&mut convolution_cache2, &[], &[], &[], &[(1.0, 1.0, 1.0)]); for (result, reference) in bins2.iter().zip(reference.iter()) { diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 290c7d04..7343e8b6 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -455,7 +455,11 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_one( unsafe { slice::from_raw_parts(channel_mask, grid.channels().len()) }.to_vec() }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; - let mut convolution_cache = ConvolutionCache::new(vec![pdg_id], vec![&mut xfx], &mut als); + let mut convolution_cache = ConvolutionCache::new( + vec![Convolution::UnpolPDF(pdg_id)], + vec![&mut xfx], + &mut als, + ); results.copy_from_slice(&grid.convolve( &mut convolution_cache, @@ -517,8 +521,14 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_two( unsafe { slice::from_raw_parts(channel_mask, grid.channels().len()) }.to_vec() }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; - let mut convolution_cache = - ConvolutionCache::new(vec![pdg_id1, pdg_id2], vec![&mut xfx1, &mut xfx2], &mut als); + let mut convolution_cache = ConvolutionCache::new( + vec![ + Convolution::UnpolPDF(pdg_id1), + Convolution::UnpolPDF(pdg_id2), + ], + vec![&mut xfx1, &mut xfx2], + &mut als, + ); results.copy_from_slice(&grid.convolve( &mut convolution_cache, diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 30f6acf4..9dbcf19e 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -2,7 +2,7 @@ use super::GlobalConfiguration; use anyhow::{anyhow, ensure, Context, Error, Result}; use lhapdf::{Pdf, PdfSet}; use ndarray::{Array3, Ix3}; -use pineappl::convolutions::ConvolutionCache; +use pineappl::convolutions::{Convolution, ConvolutionCache}; use pineappl::grid::Grid; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; use prettytable::Table; @@ -279,19 +279,39 @@ pub fn convolve_scales( .iter() .map(|fun| move |q2| fun.alphas_q2(q2)) .collect(); - let pdg_ids: Vec<_> = conv_funs + let convolutions: Vec<_> = conv_funs .iter() - .map(|fun| { - // if the field 'Particle' is missing we assume it's a proton PDF - fun.set() + .zip(grid.convolutions()) + .map(|(fun, convolution)| { + let pid = fun + .set() .entry("Particle") + // if the field 'Particle' is missing we assume it's a proton PDF .map_or(Ok(2212), |string| string.parse::()) // UNWRAP: if this fails, there's a non-integer string in the LHAPDF info file - .unwrap() + .unwrap(); + + match fun.set().entry("SetType").unwrap_or_default().as_str() { + "fragfn" => Convolution::UnpolFF(pid), + "" => { + // if we can not figure out the type of the convolution from the PDF set, we + // assume it from the grid convolution at the same index + match convolution { + // if the grid convolution is None, we assume it's a proton PDF + Convolution::None | Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), + Convolution::PolPDF(_) => Convolution::PolPDF(pid), + Convolution::UnpolFF(_) => Convolution::UnpolFF(pid), + Convolution::PolFF(_) => Convolution::PolFF(pid), + } + } + // TODO: convince the LHAPDF maintainers to make SetType necessary for polarized + // PDFs and all FFs + _ => unimplemented!(), + } }) .collect(); - let mut cache = ConvolutionCache::new(pdg_ids, xfx, &mut alphas_funs[cfg.use_alphas_from]); + let mut cache = ConvolutionCache::new(convolutions, xfx, &mut alphas_funs[cfg.use_alphas_from]); let mut results = grid.convolve(&mut cache, &orders, bins, channels, scales); match mode { @@ -430,19 +450,39 @@ pub fn convolve_subgrid( .iter() .map(|fun| move |q2| fun.alphas_q2(q2)) .collect(); - let pdg_ids: Vec<_> = conv_funs + let convolutions: Vec<_> = conv_funs .iter() - .map(|fun| { - // if the field 'Particle' is missing we assume it's a proton PDF - fun.set() + .zip(grid.convolutions()) + .map(|(fun, convolution)| { + let pid = fun + .set() .entry("Particle") + // if the field 'Particle' is missing we assume it's a proton PDF .map_or(Ok(2212), |string| string.parse::()) // UNWRAP: if this fails, there's a non-integer string in the LHAPDF info file - .unwrap() + .unwrap(); + + match fun.set().entry("SetType").unwrap_or_default().as_str() { + "fragfn" => Convolution::UnpolFF(pid), + "" => { + // if we can not figure out the type of the convolution from the PDF set, we + // assume it from the grid convolution at the same index + match convolution { + // if the grid convolution is None, we assume it's a proton PDF + Convolution::None | Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), + Convolution::PolPDF(_) => Convolution::PolPDF(pid), + Convolution::UnpolFF(_) => Convolution::UnpolFF(pid), + Convolution::PolFF(_) => Convolution::PolFF(pid), + } + } + // TODO: convince the LHAPDF maintainers to make SetType necessary for polarized + // PDFs and all FFs + _ => unimplemented!(), + } }) .collect(); - let mut cache = ConvolutionCache::new(pdg_ids, xfx, &mut alphas_funs[cfg.use_alphas_from]); + let mut cache = ConvolutionCache::new(convolutions, xfx, &mut alphas_funs[cfg.use_alphas_from]); let subgrid = grid.convolve_subgrid(&mut cache, order, bin, lumi, (1.0, 1.0, 1.0)); subgrid diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 6501c9c0..871b951b 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -893,7 +893,11 @@ fn import_hadronic_fktable() { let pdf = Pdf::with_setname_and_member("NNPDF31_nlo_as_0118_luxqed", 0).unwrap(); let mut xfx = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas = |_| 0.0; - let mut convolution_cache = ConvolutionCache::new(vec![2212], vec![&mut xfx], &mut alphas); + let mut convolution_cache = ConvolutionCache::new( + vec![Convolution::UnpolPDF(2212)], + vec![&mut xfx], + &mut alphas, + ); let results = grid.convolve(&mut convolution_cache, &[], &[], &[], &[(1.0, 1.0, 1.0)]); let mut fk_table = FkTable::try_from(grid).unwrap(); From 7569391cbe775957acc7f95131f0037c149d869e Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 4 Oct 2024 16:26:24 +0200 Subject: [PATCH 148/213] Add support for a fragmentation scale --- pineappl/src/convolutions.rs | 74 +++++++++++++++++++++++++++++++----- pineappl/src/grid.rs | 25 ++++++------ 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index f4a7f5b6..98a2b594 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -16,9 +16,11 @@ pub struct ConvolutionCache<'a> { alphas_cache: Vec, mur2_grid: Vec, muf2_grid: Vec, + mua2_grid: Vec, x_grid: Vec, imur2: Vec, imuf2: Vec, + imua2: Vec, ix: Vec>, pdg: Vec, perm: Vec>, @@ -38,16 +40,18 @@ impl<'a> ConvolutionCache<'a> { alphas_cache: Vec::new(), mur2_grid: Vec::new(), muf2_grid: Vec::new(), + mua2_grid: Vec::new(), x_grid: Vec::new(), imur2: Vec::new(), imuf2: Vec::new(), + imua2: Vec::new(), ix: Vec::new(), pdg, perm: Vec::new(), } } - pub(crate) fn setup(&mut self, grid: &Grid, xi: &[(f64, f64)]) -> Result<(), ()> { + pub(crate) fn setup(&mut self, grid: &Grid, xi: &[(f64, f64, f64)]) -> Result<(), ()> { let convolutions = grid.convolutions(); // TODO: the following code only works with exactly two convolutions @@ -129,7 +133,7 @@ impl<'a> ConvolutionCache<'a> { .flatten() .flat_map(|ren| { xi.iter() - .map(|(xir, _)| xir * xir * ren) + .map(|(xir, _, _)| xir * xir * ren) .collect::>() }) .collect(); @@ -161,17 +165,50 @@ impl<'a> ConvolutionCache<'a> { .flatten() .flat_map(|fac| { xi.iter() - .map(|(_, xif)| xif * xif * fac) + .map(|(_, xif, _)| xif * xif * fac) .collect::>() }) .collect(); muf2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); muf2_grid.dedup(); + let mut mua2_grid: Vec<_> = grid + .subgrids() + .iter() + .filter_map(|subgrid| { + if subgrid.is_empty() { + None + } else { + Some( + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + // TODO: generalize this for arbitrary scales + matches!(kin, &Kinematics::Scale(idx) if idx == 0) + .then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values(), + ) + } + }) + .flatten() + .flat_map(|frg| { + xi.iter() + .map(|(_, _, xia)| xia * xia * frg) + .collect::>() + }) + .collect(); + mua2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); + mua2_grid.dedup(); + self.alphas_cache = mur2_grid.iter().map(|&mur2| (self.alphas)(mur2)).collect(); self.mur2_grid = mur2_grid; self.muf2_grid = muf2_grid; + self.mua2_grid = mua2_grid; self.x_grid = x_grid; Ok(()) @@ -191,8 +228,7 @@ impl<'a> ConvolutionCache<'a> { .filter_map(|(index, (perm, &pdg_id))| { perm.map(|(idx, cc)| { let ix = self.ix[index][indices[index + 1]]; - let imuf2 = self.imuf2[indices[0]]; - let muf2 = self.muf2_grid[imuf2]; + let pid = if cc { pids::charge_conjugate_pdg_pid(pdg_id) } else { @@ -200,9 +236,20 @@ impl<'a> ConvolutionCache<'a> { }; let xfx = &mut self.xfx[idx]; let xfx_cache = &mut self.xfx_cache[idx]; - *xfx_cache.entry((pid, ix, imuf2)).or_insert_with(|| { + let (imu2, mu2) = match self.pdg[idx] { + Convolution::UnpolPDF(_) | Convolution::PolPDF(_) => { + let imuf2 = self.imuf2[indices[0]]; + (imuf2, self.muf2_grid[imuf2]) + } + Convolution::UnpolFF(_) | Convolution::PolFF(_) => { + let imua2 = self.imua2[indices[0]]; + (imua2, self.mua2_grid[imua2]) + } + Convolution::None => unreachable!(), + }; + *xfx_cache.entry((pid, ix, imu2)).or_insert_with(|| { let x = self.x_grid[ix]; - xfx(pid, x, muf2) / x + xfx(pid, x, mu2) / x }) }) }) @@ -224,6 +271,7 @@ impl<'a> ConvolutionCache<'a> { } self.mur2_grid.clear(); self.muf2_grid.clear(); + self.mua2_grid.clear(); self.x_grid.clear(); } @@ -237,9 +285,6 @@ impl<'a> ConvolutionCache<'a> { xif: f64, xia: f64, ) { - // TODO: generalize this for fragmentation functions - assert_eq!(xia, 1.0); - self.imur2 = mu2_grid .iter() .map(|ren| { @@ -258,6 +303,15 @@ impl<'a> ConvolutionCache<'a> { .unwrap_or_else(|| unreachable!()) }) .collect(); + self.imua2 = mu2_grid + .iter() + .map(|frg| { + self.mua2_grid + .iter() + .position(|&mua2| mua2 == xia * xia * frg) + .unwrap_or_else(|| unreachable!()) + }) + .collect(); // TODO: generalize this for arbitrary orderings of x self.ix = (0..grid.convolutions().len()) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index e4a0493c..2f176cc4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -232,13 +232,6 @@ impl Grid { channel_mask: &[bool], xi: &[(f64, f64, f64)], ) -> Vec { - assert!(xi - .iter() - .all(|&(_, _, xia)| approx_eq!(f64, xia, 1.0, ulps = 2))); - let xi = xi - .iter() - .map(|&(xir, xif, _)| (xir, xif)) - .collect::>(); convolution_cache.setup(self, &xi).unwrap(); let bin_indices = if bin_indices.is_empty() { @@ -250,11 +243,14 @@ impl Grid { let normalizations = self.bin_info().normalizations(); let pdg_channels = self.channels_pdg(); - for (xi_index, &(xir, xif)) in xi.iter().enumerate() { + for (xi_index, &(xir, xif, xia)) in xi.iter().enumerate() { for ((ord, bin, chan), subgrid) in self.subgrids.indexed_iter() { let order = &self.orders[ord]; - if ((order.logxir > 0) && (xir == 1.0)) || ((order.logxif > 0) && (xif == 1.0)) { + if ((order.logxir > 0) && (xir == 1.0)) + || ((order.logxif > 0) && (xif == 1.0)) + || ((order.logxia > 0) && (xia == 1.0)) + { continue; } @@ -316,6 +312,10 @@ impl Grid { value *= (xif * xif).ln().powi(order.logxif.try_into().unwrap()); } + if order.logxia > 0 { + value *= (xia * xia).ln().powi(order.logxia.try_into().unwrap()); + } + bins[xi_index + xi.len() * bin_index] += value / normalizations[bin]; } } @@ -339,8 +339,7 @@ impl Grid { channel: usize, (xir, xif, xia): (f64, f64, f64), ) -> ArrayD { - assert_eq!(xia, 1.0); - convolution_cache.setup(self, &[(xir, xif)]).unwrap(); + convolution_cache.setup(self, &[(xir, xif, xia)]).unwrap(); let normalizations = self.bin_info().normalizations(); let pdg_channels = self.channels_pdg(); @@ -395,6 +394,10 @@ impl Grid { array *= (xif * xif).ln().powi(order.logxif.try_into().unwrap()); } + if order.logxia > 0 { + array *= (xia * xia).ln().powi(order.logxia.try_into().unwrap()); + } + array /= normalizations[bin]; array } From a9c57848bcf7b12df87c1bcda4602d3e72d54a91 Mon Sep 17 00:00:00 2001 From: t7phy Date: Mon, 7 Oct 2024 11:22:10 +0200 Subject: [PATCH 149/213] Start generalizing evolution methods --- pineappl/src/evolution.rs | 332 ++++++++++++++++++++++++++++++++++- pineappl/src/grid.rs | 4 +- pineappl/src/packed_array.rs | 18 +- 3 files changed, 344 insertions(+), 10 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 0880bf97..b0ebbdac 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -12,7 +12,7 @@ use float_cmp::approx_eq; use itertools::izip; use itertools::Itertools; use ndarray::linalg; -use ndarray::{s, Array1, Array2, Array3, ArrayView1, ArrayView4, Axis}; +use ndarray::{s, Array1, Array2, Array3, ArrayD, ArrayView1, ArrayView4, Axis, Ix2}; use std::iter; /// Number of ULPS used to de-duplicate grid values in [`Grid::evolve_info`]. @@ -351,6 +351,151 @@ fn ndarray_from_subgrid_orders_slice( Ok((x1n, (!zero).then_some(array))) } +type X1aX1bOpDTuple = (Vec>, Option>); + +fn ndarray_from_subgrid_orders_slice_many( + grid: &Grid, + fac1: f64, + kinematics: &[Kinematics], + subgrids: &ArrayView1, + orders: &[Order], + order_mask: &[bool], + (xir, xif, xia): (f64, f64, f64), + alphas_table: &AlphasTable, +) -> Result { + // create a Vec of all x values for each dimension + let mut x1n: Vec<_> = kinematics + .iter() + .enumerate() + .filter_map(|(idx, kin)| matches!(kin, Kinematics::X(_)).then_some(idx)) + .map(|kin_idx| { + subgrids + .iter() + .enumerate() + .filter(|&(ord_idx, subgrid)| { + order_mask.get(ord_idx).copied().unwrap_or(true) + // TODO: empty subgrids don't have node values + && !subgrid.is_empty() + }) + .flat_map(|(_, subgrid)| subgrid.node_values()[kin_idx].values()) + .collect::>() + }) + .collect(); + + for x1 in &mut x1n { + x1.sort_by(f64::total_cmp); + x1.dedup_by(|&mut a, &mut b| approx_eq!(f64, a, b, ulps = EVOLUTION_TOL_ULPS)); + } + + let dim: Vec<_> = x1n.iter().map(Vec::len).collect(); + let mut array = ArrayD::::zeros(dim); + let mut zero = true; + + // for the same bin and channel, sum subgrids of different orders, using the right couplings + for (subgrid, order) in subgrids + .iter() + .zip(orders.iter()) + .zip(order_mask.iter().chain(iter::repeat(&true))) + .filter_map(|((subgrid, order), &enabled)| { + (enabled && !subgrid.is_empty()).then_some((subgrid, order)) + }) + { + let mut logs = 1.0; + + if order.logxir > 0 { + if approx_eq!(f64, xir, 1.0, ulps = 4) { + continue; + } + + logs *= (xir * xir).ln(); + } + + if order.logxif > 0 { + if approx_eq!(f64, xif, 1.0, ulps = 4) { + continue; + } + + logs *= (xif * xif).ln(); + } + + if order.logxia > 0 { + if approx_eq!(f64, xia, 1.0, ulps = 4) { + continue; + } + + logs *= (xia * xia).ln(); + } + + let x1_indices: Vec> = kinematics + .iter() + .enumerate() + .filter_map(|(idx, kin)| matches!(kin, Kinematics::X(_)).then_some(idx)) + .zip(&x1n) + .map(|(kin_idx, x1)| { + subgrid.node_values()[kin_idx] + .values() + .into_iter() + .map(|xs| { + x1.iter() + .position(|&x| approx_eq!(f64, x, xs, ulps = EVOLUTION_TOL_ULPS)) + // UNWRAP: `x1n` contains all x-values, so we must find each `x` + .unwrap() + }) + .collect() + }) + .collect(); + + for (indices, value) in subgrid.indexed_iter() { + let &[ifac1, ix1, ix2] = indices.as_slice() else { + unimplemented!() + }; + let fac = grid + .kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values()[ifac1]; + // TODO: generalize this for multiple scales + let ren = fac; + + // TODO: implement evolution for non-zero fragmentation scales + + if !approx_eq!(f64, xif * xif * fac, fac1, ulps = EVOLUTION_TOL_ULPS) { + continue; + } + + let mur2 = xir * xir * ren; + + let als = if order.alphas == 0 { + 1.0 + } else if let Some(alphas) = alphas_table + .ren1 + .iter() + .zip(alphas_table.alphas.iter()) + .find_map(|(&ren1, &alphas)| { + approx_eq!(f64, ren1, mur2, ulps = EVOLUTION_TOL_ULPS).then(|| alphas) + }) + { + alphas.powi(order.alphas.try_into().unwrap()) + } else { + return Err(GridError::EvolutionFailure(format!( + "no alphas for mur2 = {mur2} found" + ))); + }; + + zero = false; + + array[[x1_indices[0][ix1], x1_indices[1][ix2]]] += als * logs * value; + } + } + + Ok((x1n, (!zero).then_some(array))) +} + // TODO: merge this method into evolve_slice_with_two pub(crate) fn evolve_slice_with_one( grid: &Grid, @@ -438,13 +583,12 @@ pub(crate) fn evolve_slice_with_one( sub_fk_tables.extend(tables.into_iter().map(|table| { PackedQ1X2SubgridV1::new( - PackedArray::from_ndarray( + PackedArray::from( table .insert_axis(Axis(0)) .insert_axis(Axis(new_axis)) - .view(), - 0, - 1, + .view() + .into_dyn(), ), vec![ NodeValues::UseThese(vec![info.fac0]), @@ -526,14 +670,14 @@ pub(crate) fn evolve_slice_with_two( let mut tables = vec![Array2::zeros((info.x0.len(), info.x0.len())); channels0.len()]; for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { - let (x1, array) = ndarray_from_subgrid_orders_slice( + let (x1, array) = ndarray_from_subgrid_orders_slice_many( grid, info.fac1, grid.kinematics(), &subgrids_o, grid.orders(), order_mask, - xi, + (xi.0, xi.1, 1.0), alphas_table, )?; @@ -542,6 +686,8 @@ pub(crate) fn evolve_slice_with_two( continue; }; + let array = array.into_dimensionality::().unwrap(); + for (last_x1, x1, pid_indices, operators) in izip!(&mut last_x1, x1, &pid_indices, &mut operators) { @@ -755,3 +901,175 @@ pub(crate) fn evolve_slice_with_two2( .collect(), )) } + +pub(crate) fn evolve_slice_with_many( + grid: &Grid, + operators: &[ArrayView4], + infos: &[OperatorSliceInfo], + order_mask: &[bool], + xi: (f64, f64, f64), + alphas_table: &AlphasTable, +) -> Result<(Array3, Vec), GridError> { + let gluon_has_pid_zero = gluon_has_pid_zero(grid); + + // TODO: implement matching of different scales for different EKOs + let mut fac1_scales: Vec<_> = infos.iter().map(|info| info.fac1).collect(); + fac1_scales.sort_by(f64::total_cmp); + assert!(fac1_scales.windows(2).all(|scales| approx_eq!( + f64, + scales[0], + scales[1], + ulps = EVOLUTION_TOL_ULPS + ))); + let fac1 = fac1_scales[0]; + + assert_eq!(operators.len(), infos.len()); + assert_eq!(operators.len(), grid.convolutions().len()); + assert_eq!( + grid.convolutions() + .iter() + .filter(|&conv| *conv == Convolution::None) + .count(), + 0 + ); + + let (pid_indices, pids01): (Vec<_>, Vec<_>) = izip!(0..infos.len(), operators, infos) + .map(|(d, operator, info)| { + pid_slices(operator, info, gluon_has_pid_zero, &|pid1| { + grid.channels() + .iter() + .flat_map(Channel::entry) + .any(|(pids, _)| pids[d] == pid1) + }) + }) + .collect::, _>>()? + .into_iter() + .unzip(); + + let mut channels0: Vec<_> = pids01 + .iter() + .map(|pids| pids.iter().map(|&(pid0, _)| pid0)) + .multi_cartesian_product() + .collect(); + channels0.sort_unstable(); + channels0.dedup(); + let channels0 = channels0; + + let mut sub_fk_tables = Vec::with_capacity(grid.bin_info().bins() * channels0.len()); + + // TODO: generalize to `n` + let mut last_x1 = vec![Vec::new(); infos.len()]; + let mut eko_slices = vec![Vec::new(); infos.len()]; + let dim: Vec<_> = infos.iter().map(|info| info.x0.len()).collect(); + + for subgrids_oc in grid.subgrids().axis_iter(Axis(1)) { + assert_eq!(infos[0].x0.len(), infos[1].x0.len()); + + let mut tables = vec![ArrayD::zeros(dim.clone()); channels0.len()]; + + for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { + let (x1, array) = ndarray_from_subgrid_orders_slice_many( + grid, + fac1, + grid.kinematics(), + &subgrids_o, + grid.orders(), + order_mask, + xi, + alphas_table, + )?; + + // skip over zero arrays to speed up evolution and avoid problems with NaNs + let Some(array) = array else { + continue; + }; + + for (last_x1, x1, pid_indices, slices, operator, info) in izip!( + &mut last_x1, + x1, + &pid_indices, + &mut eko_slices, + operators, + infos + ) { + if (last_x1.len() != x1.len()) + || last_x1 + .iter() + .zip(x1.iter()) + .any(|(&lhs, &rhs)| !approx_eq!(f64, lhs, rhs, ulps = EVOLUTION_TOL_ULPS)) + { + *slices = operator_slices(operator, info, pid_indices, &x1)?; + *last_x1 = x1; + } + } + + let mut tmp = Array2::zeros((last_x1[0].len(), infos[1].x0.len())); + + for (pids1, factor) in channel1 + .entry() + .iter() + .map(|(pids1, factor)| ([pids1[0], pids1[1]], factor)) + { + for (fk_table, ops) in + channels0 + .iter() + .zip(tables.iter_mut()) + .filter_map(|(pids0, fk_table)| { + izip!(pids0, &pids1, &pids01, &eko_slices) + .map(|(&pid0, &pid1, pids, slices)| { + pids.iter().zip(slices).find_map(|(&(p0, p1), op)| { + ((p0 == pid0) && (p1 == pid1)).then_some(op) + }) + }) + // TODO: avoid using `collect` + .collect::>>() + .map(|ops| (fk_table, ops)) + }) + { + general_tensor_mul(*factor, &array, &ops, &mut tmp, fk_table); + } + } + } + + sub_fk_tables.extend(tables.into_iter().map(|table| { + PackedQ1X2SubgridV1::new( + PackedArray::from(table.insert_axis(Axis(0)).view()), + // TODO: generalize this for arbitrary scales and x values + vec![ + NodeValues::UseThese(vec![infos[0].fac0]), + NodeValues::UseThese(infos[0].x0.clone()), + NodeValues::UseThese(infos[1].x0.clone()), + ], + ) + .into() + })); + } + + Ok(( + Array1::from_iter(sub_fk_tables) + .into_shape((1, grid.bin_info().bins(), channels0.len())) + .unwrap(), + channels0 + .iter() + .map(|c| channel![c[0], c[1], 1.0]) + .collect(), + )) +} + +fn general_tensor_mul( + factor: f64, + array: &ArrayD, + ops: &[&Array2], + tmp: &mut Array2, + fk_table: &mut ArrayD, +) { + // TODO: generalize this to n dimensions + assert_eq!(array.shape().len(), 2); + let array = array.view().into_dimensionality::().unwrap(); + let mut fk_table = fk_table.view_mut().into_dimensionality::().unwrap(); + + // tmp = array * ops[1]^T + linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, tmp); + // fk_table += factor * ops[0] * tmp + linalg::general_mat_mul(factor, ops[0], &tmp, 1.0, &mut fk_table); +} diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 2f176cc4..bae8becf 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1442,12 +1442,12 @@ impl Grid { "only one convolution found, use `Grid::evolve_with_slice_iter` instead" ); - let (subgrids, channels) = evolution::evolve_slice_with_two2( + let (subgrids, channels) = evolution::evolve_slice_with_many( self, &views, &infos, order_mask, - xi, + (xi.0, xi.1, 1.0), alphas_table, )?; diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 8cba4dd7..7b6ea33d 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -1,6 +1,6 @@ //! Provides the [`PackedArray`] struct. -use ndarray::ArrayView3; +use ndarray::{ArrayView3, ArrayViewD}; use serde::{Deserialize, Serialize}; use std::iter; use std::mem; @@ -143,6 +143,22 @@ impl PackedArray { } } +impl From> for PackedArray { + fn from(array: ArrayViewD) -> Self { + let mut result = Self::new(array.shape().to_vec()); + + for (i, &entry) in array + .iter() + .enumerate() + .filter(|(_, &entry)| entry != Default::default()) + { + result[i] = entry; + } + + result + } +} + /// Converts a `multi_index` into a flat index. fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { assert_eq!(multi_index.len(), shape.len()); From f6587e934fb39c2b07be642e0cfc2199eae6f64c Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 9 Oct 2024 11:10:31 +0200 Subject: [PATCH 150/213] Reformat `read_convolutions_from_metadata` --- pineappl/src/v0.rs | 116 ++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 55 deletions(-) diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 98931ee3..6c65ab43 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -162,64 +162,70 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { grid.key_values().map_or_else( - // if there isn't any metadata, we assume two unpolarized proton-PDFs are used - || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], - |kv| { - // file format v0 only supports exactly two convolutions - (1..=2) - .map(|index| { - // if there are key-value pairs `convolution_particle_1` and - // `convolution_type_1` and the same with a higher index, we convert this - // metadata into `Convolution` - match ( - kv.get(&format!("convolution_particle_{index}")) - .map(|s| s.parse::()), - kv.get(&format!("convolution_type_{index}")) - .map(String::as_str), - ) { - (_, Some("None")) => Convolution::None, - (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), - (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), - (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), - (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), - (None, None) => { - // if these key-value pairs are missing use the old metadata - match kv - .get(&format!("initial_state_{index}")) - .map(|s| s.parse::()) - { - Some(Ok(pid)) => { - let condition = !grid.channels().iter().all(|entry| { - entry.entry().iter().all(|&(a, b, _)| match index {1 => a, 2 => b, _ => unreachable!()} == pid) - }); + // if there isn't any metadata, we assume two unpolarized proton-PDFs are used + || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + |kv| { + // file format v0 only supports exactly two convolutions + (1..=2) + .map(|index| { + // if there are key-value pairs `convolution_particle_1` and + // `convolution_type_1` and the same with a higher index, we convert this + // metadata into `Convolution` + match ( + kv.get(&format!("convolution_particle_{index}")) + .map(|s| s.parse::()), + kv.get(&format!("convolution_type_{index}")) + .map(String::as_str), + ) { + (_, Some("None")) => Convolution::None, + (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), + (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), + (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), + (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), + (None, None) => { + // if these key-value pairs are missing use the old metadata + match kv + .get(&format!("initial_state_{index}")) + .map(|s| s.parse::()) + { + Some(Ok(pid)) => { + let condition = !grid.channels().iter().all(|entry| { + entry.entry().iter().all(|&(a, b, _)| + match index { + 1 => a, + 2 => b, + _ => unreachable!() + } == pid + ) + }); - if condition { - Convolution::UnpolPDF(pid) - } else { - Convolution::None + if condition { + Convolution::UnpolPDF(pid) + } else { + Convolution::None + } } + None => Convolution::UnpolPDF(2212), + Some(Err(err)) => panic!( + "metadata 'initial_state_{index}' could not be parsed: {err}" + ), } - None => Convolution::UnpolPDF(2212), - Some(Err(err)) => panic!( - "metadata 'initial_state_{index}' could not be parsed: {err}" - ), + } + (None, Some(_)) => { + panic!("metadata 'convolution_type_{index}' is missing") + } + (Some(_), None) => { + panic!("metadata 'convolution_particle_{index}' is missing") + } + (Some(Ok(_)), Some(type_)) => { + panic!("metadata 'convolution_type_{index} = {type_}' is unknown") + } + (Some(Err(err)), Some(_)) => { + panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") } } - (None, Some(_)) => { - panic!("metadata 'convolution_type_{index}' is missing") - } - (Some(_), None) => { - panic!("metadata 'convolution_particle_{index}' is missing") - } - (Some(Ok(_)), Some(type_)) => { - panic!("metadata 'convolution_type_{index} = {type_}' is unknown") - } - (Some(Err(err)), Some(_)) => { - panic!("metadata 'convolution_particle_{index}' could not be parsed: {err}") - } - } - }) - .collect() - }, - ) + }) + .collect() + }, + ) } From 0632b0389d77dc6e052abbdffbdaab0b5b283205 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Wed, 9 Oct 2024 16:22:21 +0200 Subject: [PATCH 151/213] Generalize `ndarray_from_subgrid_orders_slice_many` a bit --- pineappl/src/boc.rs | 2 +- pineappl/src/evolution.rs | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 362e075d..52e0ead7 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -12,7 +12,7 @@ use std::str::FromStr; use thiserror::Error; /// TODO -#[derive(Clone, Copy, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum Kinematics { /// TODO Scale(usize), diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index b0ebbdac..71cec721 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -363,6 +363,19 @@ fn ndarray_from_subgrid_orders_slice_many( (xir, xif, xia): (f64, f64, f64), alphas_table: &AlphasTable, ) -> Result { + // TODO: remove these assumptions from the following code + assert_eq!(grid.kinematics()[0], Kinematics::Scale(0)); + assert_eq!( + grid.kinematics()[1..] + .iter() + .map(|kin| match kin { + &Kinematics::X(idx) => idx, + _ => unreachable!(), + }) + .collect::>(), + (0..(grid.kinematics().len() - 1)).collect::>() + ); + // create a Vec of all x values for each dimension let mut x1n: Vec<_> = kinematics .iter() @@ -390,6 +403,7 @@ fn ndarray_from_subgrid_orders_slice_many( let dim: Vec<_> = x1n.iter().map(Vec::len).collect(); let mut array = ArrayD::::zeros(dim); let mut zero = true; + let mut x1_idx = vec![0; grid.convolutions().len()]; // for the same bin and channel, sum subgrids of different orders, using the right couplings for (subgrid, order) in subgrids @@ -446,9 +460,6 @@ fn ndarray_from_subgrid_orders_slice_many( .collect(); for (indices, value) in subgrid.indexed_iter() { - let &[ifac1, ix1, ix2] = indices.as_slice() else { - unimplemented!() - }; let fac = grid .kinematics() .iter() @@ -458,7 +469,7 @@ fn ndarray_from_subgrid_orders_slice_many( }) // TODO: convert this into an error .unwrap() - .values()[ifac1]; + .values()[indices[0]]; // TODO: generalize this for multiple scales let ren = fac; @@ -489,7 +500,13 @@ fn ndarray_from_subgrid_orders_slice_many( zero = false; - array[[x1_indices[0][ix1], x1_indices[1][ix2]]] += als * logs * value; + // TODO: here we assume that all X are consecutive starting from the second element and + // are in ascending order + for (i, &index) in indices.iter().skip(1).enumerate() { + x1_idx[i] = x1_indices[i][index]; + } + + array[x1_idx.as_slice()] += als * logs * value; } } From ae4b9a11e5aceb5a73a80302249a53c6971849d4 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 10 Oct 2024 11:04:07 +0200 Subject: [PATCH 152/213] Start getting rid of `Convolution::None` --- pineappl/src/convolutions.rs | 5 - pineappl/src/empty_subgrid.rs | 2 +- pineappl/src/evolution.rs | 63 ++++++------ pineappl/src/grid.rs | 39 +++---- pineappl/src/lagrange_subgrid.rs | 6 +- pineappl/src/packed_subgrid.rs | 2 +- pineappl/src/v0.rs | 151 +++++++++++++++++----------- pineappl_cli/src/export/applgrid.rs | 20 ++-- 8 files changed, 157 insertions(+), 131 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 98a2b594..235c8d6e 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -52,11 +52,6 @@ impl<'a> ConvolutionCache<'a> { } pub(crate) fn setup(&mut self, grid: &Grid, xi: &[(f64, f64, f64)]) -> Result<(), ()> { - let convolutions = grid.convolutions(); - - // TODO: the following code only works with exactly two convolutions - assert_eq!(convolutions.len(), 2); - self.perm = grid .convolutions() .iter() diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 191aadd1..0238d262 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -81,7 +81,7 @@ mod tests { #[should_panic(expected = "EmptySubgridV1 doesn't support the fill operation")] fn fill() { let mut subgrid = EmptySubgridV1; - subgrid.fill(&v0::default_interps(), &[0.0; 3], 0.0); + subgrid.fill(&v0::default_interps(2), &[0.0; 3], 0.0); } #[test] diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 71cec721..1bd41827 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -12,7 +12,7 @@ use float_cmp::approx_eq; use itertools::izip; use itertools::Itertools; use ndarray::linalg; -use ndarray::{s, Array1, Array2, Array3, ArrayD, ArrayView1, ArrayView4, Axis, Ix2}; +use ndarray::{s, Array1, Array2, Array3, ArrayD, ArrayView1, ArrayView4, Axis, Ix1, Ix2}; use std::iter; /// Number of ULPS used to de-duplicate grid values in [`Grid::evolve_info`]. @@ -980,8 +980,6 @@ pub(crate) fn evolve_slice_with_many( let dim: Vec<_> = infos.iter().map(|info| info.x0.len()).collect(); for subgrids_oc in grid.subgrids().axis_iter(Axis(1)) { - assert_eq!(infos[0].x0.len(), infos[1].x0.len()); - let mut tables = vec![ArrayD::zeros(dim.clone()); channels0.len()]; for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { @@ -1020,19 +1018,13 @@ pub(crate) fn evolve_slice_with_many( } } - let mut tmp = Array2::zeros((last_x1[0].len(), infos[1].x0.len())); - - for (pids1, factor) in channel1 - .entry() - .iter() - .map(|(pids1, factor)| ([pids1[0], pids1[1]], factor)) - { + for (pids1, factor) in channel1.entry() { for (fk_table, ops) in channels0 .iter() .zip(tables.iter_mut()) .filter_map(|(pids0, fk_table)| { - izip!(pids0, &pids1, &pids01, &eko_slices) + izip!(pids0, pids1, &pids01, &eko_slices) .map(|(&pid0, &pid1, pids, slices)| { pids.iter().zip(slices).find_map(|(&(p0, p1), op)| { ((p0 == pid0) && (p1 == pid1)).then_some(op) @@ -1043,20 +1035,22 @@ pub(crate) fn evolve_slice_with_many( .map(|ops| (fk_table, ops)) }) { - general_tensor_mul(*factor, &array, &ops, &mut tmp, fk_table); + general_tensor_mul(*factor, &array, &ops, fk_table); } } } + // TODO: generalize this for arbitrary scales and x values + let mut node_values = vec![NodeValues::UseThese(vec![infos[0].fac0])]; + + for info in infos { + node_values.push(NodeValues::UseThese(info.x0.clone())); + } + sub_fk_tables.extend(tables.into_iter().map(|table| { PackedQ1X2SubgridV1::new( PackedArray::from(table.insert_axis(Axis(0)).view()), - // TODO: generalize this for arbitrary scales and x values - vec![ - NodeValues::UseThese(vec![infos[0].fac0]), - NodeValues::UseThese(infos[0].x0.clone()), - NodeValues::UseThese(infos[1].x0.clone()), - ], + node_values.clone(), ) .into() })); @@ -1067,8 +1061,8 @@ pub(crate) fn evolve_slice_with_many( .into_shape((1, grid.bin_info().bins(), channels0.len())) .unwrap(), channels0 - .iter() - .map(|c| channel![c[0], c[1], 1.0]) + .into_iter() + .map(|c| Channel::new(vec![(c, 1.0)])) .collect(), )) } @@ -1077,16 +1071,25 @@ fn general_tensor_mul( factor: f64, array: &ArrayD, ops: &[&Array2], - tmp: &mut Array2, fk_table: &mut ArrayD, ) { - // TODO: generalize this to n dimensions - assert_eq!(array.shape().len(), 2); - let array = array.view().into_dimensionality::().unwrap(); - let mut fk_table = fk_table.view_mut().into_dimensionality::().unwrap(); - - // tmp = array * ops[1]^T - linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, tmp); - // fk_table += factor * ops[0] * tmp - linalg::general_mat_mul(factor, ops[0], &tmp, 1.0, &mut fk_table); + match array.shape().len() { + 1 => { + let array = array.view().into_dimensionality::().unwrap(); + let mut fk_table = fk_table.view_mut().into_dimensionality::().unwrap(); + fk_table.scaled_add(factor, &ops[0].dot(&array)); + } + 2 => { + let array = array.view().into_dimensionality::().unwrap(); + let mut fk_table = fk_table.view_mut().into_dimensionality::().unwrap(); + + let mut tmp = Array2::zeros((array.shape()[0], ops[1].shape()[0])); + // tmp = array * ops[1]^T + linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, &mut tmp); + // fk_table += factor * ops[0] * tmp + linalg::general_mat_mul(factor, ops[0], &tmp, 1.0, &mut fk_table); + } + // TODO: generalize this to n dimensions + _ => unimplemented!(), + } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index bae8becf..3c8e881b 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1275,15 +1275,16 @@ impl Grid { operator.dim(), ); - let view = operator.view(); - - let (subgrids, channels) = if self.convolutions()[0] != Convolution::None - && self.convolutions()[1] != Convolution::None - { - evolution::evolve_slice_with_two(self, &view, &info, order_mask, xi, alphas_table) - } else { - evolution::evolve_slice_with_one(self, &view, &info, order_mask, xi, alphas_table) - }?; + let views = vec![operator.view(); self.convolutions().len()]; + let infos = vec![info.clone(); self.convolutions().len()]; + let (subgrids, channels) = evolution::evolve_slice_with_many( + self, + &views, + &infos, + order_mask, + (xi.0, xi.1, 1.0), + alphas_table, + )?; let rhs = Self { subgrids, @@ -1734,7 +1735,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 1.0], vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1755,7 +1756,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1778,7 +1779,7 @@ mod tests { vec![Order::new(1, 2, 0, 0, 0), Order::new(1, 2, 0, 1, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1806,7 +1807,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1832,7 +1833,7 @@ mod tests { ], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1865,7 +1866,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1884,7 +1885,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1914,7 +1915,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1937,7 +1938,7 @@ mod tests { vec![Order::new(0, 2, 0, 0, 0)], vec![0.5, 0.75, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), @@ -1972,7 +1973,7 @@ mod tests { }], vec![0.0, 1.0], vec![Convolution::UnpolPDF(2212); 2], - v0::default_interps(), + v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { ren: ScaleFuncForm::Scale(0), diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index feb5e137..3abbfeac 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -127,7 +127,7 @@ mod tests { #[test] fn fill_zero() { - let interps = v0::default_interps(); + let interps = v0::default_interps(2); let mut subgrid = LagrangeSubgridV2::new(&interps); subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 0.0); @@ -148,7 +148,7 @@ mod tests { #[test] fn fill_outside_range() { - let interps = v0::default_interps(); + let interps = v0::default_interps(2); let mut subgrid = LagrangeSubgridV2::new(&interps); subgrid.fill(&interps, &[1000.0, 1e-10, 0.5], 0.0); @@ -169,7 +169,7 @@ mod tests { #[test] fn fill() { - let interps = v0::default_interps(); + let interps = v0::default_interps(2); let mut subgrid = LagrangeSubgridV2::new(&interps); subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 1.0); diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 4b604580..1a0a4acb 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -191,7 +191,7 @@ mod tests { PackedArray::new(vec![0, 0, 0]), vec![NodeValues::UseThese(Vec::new()); 3], ); - subgrid.fill(&v0::default_interps(), &[0.0; 3], 0.0); + subgrid.fill(&v0::default_interps(2), &[0.0; 3], 0.0); } #[test] diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 6c65ab43..1bf6e0e4 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -13,27 +13,19 @@ use pineappl_v0::grid::Grid as GridV0; use std::io::BufRead; use std::iter; -pub fn default_interps() -> Vec { - vec![ - Interp::new( - 1e2, - 1e8, - 40, - 3, - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( +pub fn default_interps(convolutions: usize) -> Vec { + let mut interps = vec![Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + )]; + + for _ in 0..convolutions { + interps.push(Interp::new( 2e-7, 1.0, 50, @@ -41,16 +33,26 @@ pub fn default_interps() -> Vec { ReweightMeth::ApplGridX, Map::ApplGridF2, InterpMeth::Lagrange, - ), - ] + )); + } + + interps } pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result { use pineappl_v0::subgrid::Subgrid as _; let grid = GridV0::read(&mut reader).map_err(|err| GridError::Other(err.into()))?; + let convolutions = read_convolutions_from_metadata(&grid); + + // TODO: read in flexible-scale grids properly + let mut kinematics = vec![Kinematics::Scale(0), Kinematics::X(0)]; + if convolutions[0].is_some() && convolutions[1].is_some() { + kinematics.push(Kinematics::X(1)); + } + + assert_eq!(convolutions.len(), 2); - // TODO: convert differently if grid only has one convolution let result = Grid { subgrids: Array3::from_shape_vec( grid.subgrids().dim(), @@ -69,24 +71,40 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result frg: -1.0, }) .collect(); - let x1_grid = subgrid.x1_grid().into_owned(); - let x2_grid = subgrid.x2_grid().into_owned(); - let mut array = - PackedArray::new(vec![mu2_grid.len(), x1_grid.len(), x2_grid.len()]); - for (index, v) in subgrid.indexed_iter() { - array[<[usize; 3]>::from(index)] = v; + + let mut dim = vec![mu2_grid.len()]; + if convolutions[0].is_some() { + dim.push(subgrid.x1_grid().len()); } - PackedQ1X2SubgridV1::new( - array, - vec![ - NodeValues::UseThese( - mu2_grid.iter().map(|&Mu2 { ren, .. }| ren).collect(), - ), - NodeValues::UseThese(x1_grid), - NodeValues::UseThese(x2_grid), - ], - ) - .into() + if convolutions[1].is_some() { + dim.push(subgrid.x2_grid().len()); + } + let mut array = PackedArray::new(dim); + + if convolutions[0].is_none() { + for (index, v) in subgrid.indexed_iter() { + array[[index.0, index.2]] = v; + } + } else if convolutions[1].is_none() { + for (index, v) in subgrid.indexed_iter() { + array[[index.0, index.1]] = v; + } + } else { + for (index, v) in subgrid.indexed_iter() { + array[<[usize; 3]>::from(index)] = v; + } + } + + let mut node_values = vec![NodeValues::UseThese( + mu2_grid.iter().map(|&Mu2 { ren, .. }| ren).collect(), + )]; + if convolutions[0].is_some() { + node_values.push(NodeValues::UseThese(subgrid.x1_grid().into_owned())); + } + if convolutions[1].is_some() { + node_values.push(NodeValues::UseThese(subgrid.x2_grid().into_owned())); + } + PackedQ1X2SubgridV1::new(array, node_values).into() } }) .collect(), @@ -96,7 +114,23 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result channels: grid .channels() .iter() - .map(|c| Channel::new(c.entry().iter().map(|&(a, b, f)| (vec![a, b], f)).collect())) + .map(|c| { + Channel::new( + c.entry() + .iter() + .map(|&(a, b, f)| { + let mut pids = Vec::new(); + if convolutions[0].is_some() { + pids.push(a); + } + if convolutions[1].is_some() { + pids.push(b); + }; + (pids, f) + }) + .collect(), + ) + }) .collect(), // TODO: change this member to something much easier to handle bin_limits: BinLimits::new(if grid.remapper().is_none() { @@ -127,7 +161,12 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result .unwrap_or_default() .into_iter() .collect(), - convolutions: read_convolutions_from_metadata(&grid), + interps: default_interps(convolutions.len()), + convolutions: convolutions + .into_iter() + //.map(|conv| conv.unwrap_or(Convolution::None)) + .filter_map(|conv| conv) + .collect(), pid_basis: grid .key_values() .and_then(|kv| kv.get("lumi_id_types")) @@ -144,9 +183,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result // and limits we should be able to do the same without error BinRemapper::new(r.normalizations().to_vec(), r.limits().to_vec()).unwrap() }), - // TODO: read in flexible-scale grids properly - kinematics: vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], - interps: default_interps(), + kinematics, scales: Scales { // TODO: read in flexible-scale grids properly ren: ScaleFuncForm::Scale(0), @@ -160,10 +197,10 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result Ok(result) } -fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { +fn read_convolutions_from_metadata(grid: &GridV0) -> Vec> { grid.key_values().map_or_else( // if there isn't any metadata, we assume two unpolarized proton-PDFs are used - || vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + || vec![Some(Convolution::UnpolPDF(2212)); 2], |kv| { // file format v0 only supports exactly two convolutions (1..=2) @@ -177,11 +214,11 @@ fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { kv.get(&format!("convolution_type_{index}")) .map(String::as_str), ) { - (_, Some("None")) => Convolution::None, - (Some(Ok(pid)), Some("UnpolPDF")) => Convolution::UnpolPDF(pid), - (Some(Ok(pid)), Some("PolPDF")) => Convolution::PolPDF(pid), - (Some(Ok(pid)), Some("UnpolFF")) => Convolution::UnpolFF(pid), - (Some(Ok(pid)), Some("PolFF")) => Convolution::PolFF(pid), + (_, Some("None")) => None, + (Some(Ok(pid)), Some("UnpolPDF")) => Some(Convolution::UnpolPDF(pid)), + (Some(Ok(pid)), Some("PolPDF")) => Some(Convolution::PolPDF(pid)), + (Some(Ok(pid)), Some("UnpolFF")) => Some(Convolution::UnpolFF(pid)), + (Some(Ok(pid)), Some("PolFF")) => Some(Convolution::PolFF(pid)), (None, None) => { // if these key-value pairs are missing use the old metadata match kv @@ -199,13 +236,9 @@ fn read_convolutions_from_metadata(grid: &GridV0) -> Vec { ) }); - if condition { - Convolution::UnpolPDF(pid) - } else { - Convolution::None - } + condition.then_some(Convolution::UnpolPDF(pid)) } - None => Convolution::UnpolPDF(2212), + None => Some(Convolution::UnpolPDF(2212)), Some(Err(err)) => panic!( "metadata 'initial_state_{index}' could not be parsed: {err}" ), diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index fbe86470..9554470e 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -124,14 +124,8 @@ pub fn convert_into_applgrid( } let lumis = grid.channels().len(); - let has_pdf1 = grid.convolutions()[0] != Convolution::None; - let has_pdf2 = grid.convolutions()[1] != Convolution::None; - // let (has_pdf1, has_pdf2) = match (grid.convolutions()[0].clone(), grid.convolutions()[1].clone()) { - // (Convolution::None, Convolution::None) => unreachable!(), - // (Convolution::None, _) => (false, true), - // (_, Convolution::None) => (true, false), - // _ => (true, true), - // }; + let has_pdf1 = grid.convolutions().get(0).is_some(); + let has_pdf2 = grid.convolutions().get(1).is_some(); // TODO: check that PDG MC IDs are used @@ -378,9 +372,9 @@ pub fn convert_into_applgrid( let mut weightgrid = ffi::igrid_weightgrid(igrid.pin_mut(), channel); for (indices, value) in subgrid.indexed_iter() { - let &[iq2, ix1, ix2] = indices.as_slice() else { - unimplemented!() - }; + // TODO: here we assume that all X are consecutive starting from the second + // element and are in ascending order + let iq2 = indices[0]; let appl_q2_idx = appl_q2_idx[iq2]; if appl_q2_idx == -1 { @@ -406,9 +400,9 @@ pub fn convert_into_applgrid( ffi::sparse_matrix_set( weightgrid.as_mut(), appl_q2_idx, - appl_x1_idx[ix1], + appl_x1_idx[indices[1]], if has_pdf1 && has_pdf2 { - appl_x2_idx[ix2] + appl_x2_idx[indices[2]] } else { 0 }, From defd91e7cf6117b5b01275fe3aed5094349b5cd7 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 10 Oct 2024 11:21:18 +0200 Subject: [PATCH 153/213] Delete unused functions --- pineappl/src/evolution.rs | 554 -------------------------------------- 1 file changed, 554 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 1bd41827..0b849e1a 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -1,7 +1,6 @@ //! Supporting classes and functions for [`Grid::evolve_with_slice_iter`]. use super::boc::{Channel, Kinematics, Order}; -use super::channel; use super::convolutions::Convolution; use super::grid::{Grid, GridError}; use super::packed_array::PackedArray; @@ -168,14 +167,6 @@ fn pid_slices( Ok((pid_indices, pids)) } -fn channels0_with_one(pids: &[(i32, i32)]) -> Vec { - let mut pids0: Vec<_> = pids.iter().map(|&(pid0, _)| pid0).collect(); - pids0.sort_unstable(); - pids0.dedup(); - - pids0 -} - fn operator_slices( operator: &ArrayView4, info: &OperatorSliceInfo, @@ -212,145 +203,6 @@ fn operator_slices( Ok(operators) } -type X1aX1bOp2Tuple = (Vec>, Option>); - -fn ndarray_from_subgrid_orders_slice( - grid: &Grid, - fac1: f64, - kinematics: &[Kinematics], - subgrids: &ArrayView1, - orders: &[Order], - order_mask: &[bool], - (xir, xif): (f64, f64), - alphas_table: &AlphasTable, -) -> Result { - // create a Vec of all x values for each dimension - let mut x1n: Vec<_> = kinematics - .iter() - .enumerate() - .filter_map(|(idx, kin)| matches!(kin, Kinematics::X(_)).then_some(idx)) - .map(|kin_idx| { - subgrids - .iter() - .enumerate() - .filter(|&(ord_idx, subgrid)| { - order_mask.get(ord_idx).copied().unwrap_or(true) - // TODO: empty subgrids don't have node values - && !subgrid.is_empty() - }) - .flat_map(|(_, subgrid)| subgrid.node_values()[kin_idx].values()) - .collect::>() - }) - .collect(); - - for x1 in &mut x1n { - x1.sort_by(f64::total_cmp); - x1.dedup_by(|&mut a, &mut b| approx_eq!(f64, a, b, ulps = EVOLUTION_TOL_ULPS)); - } - - // TODO: lift this restriction - assert_eq!(x1n.len(), 2); - - let mut array = Array2::::zeros((x1n[0].len(), x1n[1].len())); - let mut zero = true; - - // for the same bin and channel, sum subgrids of different orders, using the right couplings - for (subgrid, order) in subgrids - .iter() - .zip(orders.iter()) - .zip(order_mask.iter().chain(iter::repeat(&true))) - .filter_map(|((subgrid, order), &enabled)| { - (enabled && !subgrid.is_empty()).then_some((subgrid, order)) - }) - { - let mut logs = 1.0; - - if order.logxir > 0 { - if approx_eq!(f64, xir, 1.0, ulps = 4) { - continue; - } - - logs *= (xir * xir).ln(); - } - - if order.logxif > 0 { - if approx_eq!(f64, xif, 1.0, ulps = 4) { - continue; - } - - logs *= (xif * xif).ln(); - } - - let x1_indices: Vec> = kinematics - .iter() - .enumerate() - .filter_map(|(idx, kin)| matches!(kin, Kinematics::X(_)).then_some(idx)) - .zip(&x1n) - .map(|(kin_idx, x1)| { - subgrid.node_values()[kin_idx] - .values() - .into_iter() - .map(|xs| { - x1.iter() - .position(|&x| approx_eq!(f64, x, xs, ulps = EVOLUTION_TOL_ULPS)) - // UNWRAP: `x1n` contains all x-values, so we must find each `x` - .unwrap() - }) - .collect() - }) - .collect(); - - for (indices, value) in subgrid.indexed_iter() { - let &[ifac1, ix1, ix2] = indices.as_slice() else { - unimplemented!() - }; - let fac = grid - .kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values()[ifac1]; - // TODO: generalize this for multiple scales - let ren = fac; - - // TODO: implement evolution for non-zero fragmentation scales - - if !approx_eq!(f64, xif * xif * fac, fac1, ulps = EVOLUTION_TOL_ULPS) { - continue; - } - - let mur2 = xir * xir * ren; - - let als = if order.alphas == 0 { - 1.0 - } else if let Some(alphas) = alphas_table - .ren1 - .iter() - .zip(alphas_table.alphas.iter()) - .find_map(|(&ren1, &alphas)| { - approx_eq!(f64, ren1, mur2, ulps = EVOLUTION_TOL_ULPS).then(|| alphas) - }) - { - alphas.powi(order.alphas.try_into().unwrap()) - } else { - return Err(GridError::EvolutionFailure(format!( - "no alphas for mur2 = {mur2} found" - ))); - }; - - zero = false; - - array[[x1_indices[0][ix1], x1_indices[1][ix2]]] += als * logs * value; - } - } - - Ok((x1n, (!zero).then_some(array))) -} - type X1aX1bOpDTuple = (Vec>, Option>); fn ndarray_from_subgrid_orders_slice_many( @@ -513,412 +365,6 @@ fn ndarray_from_subgrid_orders_slice_many( Ok((x1n, (!zero).then_some(array))) } -// TODO: merge this method into evolve_slice_with_two -pub(crate) fn evolve_slice_with_one( - grid: &Grid, - operator: &ArrayView4, - info: &OperatorSliceInfo, - order_mask: &[bool], - xi: (f64, f64), - alphas_table: &AlphasTable, -) -> Result<(Array3, Vec), GridError> { - let gluon_has_pid_zero = gluon_has_pid_zero(grid); - // UNWRAP: there must be exactly one convolution that's not None - let index = grid - .convolutions() - .iter() - .position(|c| *c != Convolution::None) - .unwrap(); - let (pid_indices, pids) = pid_slices(operator, info, gluon_has_pid_zero, &|pid| { - grid.channels() - .iter() - .flat_map(Channel::entry) - .any(|(pids, _)| pids[index] == pid) - })?; - - let channels0 = channels0_with_one(&pids); - let mut sub_fk_tables = Vec::with_capacity(grid.bin_info().bins() * channels0.len()); - let new_axis = 2 - index; - - let mut last_x1 = Vec::new(); - let mut ops = Vec::new(); - - for subgrids_ol in grid.subgrids().axis_iter(Axis(1)) { - let mut tables = vec![Array1::zeros(info.x0.len()); channels0.len()]; - - for (subgrids_o, channel1) in subgrids_ol.axis_iter(Axis(1)).zip(grid.channels()) { - let (mut x1, array) = ndarray_from_subgrid_orders_slice( - grid, - info.fac1, - grid.kinematics(), - &subgrids_o, - grid.orders(), - order_mask, - xi, - alphas_table, - )?; - - // skip over zero arrays to speed up evolution and avoid problems with NaNs - let Some(array) = array else { - continue; - }; - - let x1 = x1.remove(index); - - if x1.is_empty() { - continue; - } - - if (last_x1.len() != x1.len()) - || last_x1 - .iter() - .zip(x1.iter()) - .any(|(&lhs, &rhs)| !approx_eq!(f64, lhs, rhs, ulps = EVOLUTION_TOL_ULPS)) - { - ops = operator_slices(operator, info, &pid_indices, &x1)?; - last_x1 = x1; - } - - for (pid1, &factor) in channel1.entry().iter().map(|(pids, f)| (pids[index], f)) { - for (fk_table, op) in - channels0 - .iter() - .zip(tables.iter_mut()) - .filter_map(|(&pid0, fk_table)| { - pids.iter() - .zip(ops.iter()) - .find_map(|(&(p0, p1), op)| { - (p0 == pid0 && p1 == pid1).then_some(op) - }) - .map(|op| (fk_table, op)) - }) - { - fk_table.scaled_add(factor, &op.dot(&array.index_axis(Axis(new_axis - 1), 0))); - } - } - } - - sub_fk_tables.extend(tables.into_iter().map(|table| { - PackedQ1X2SubgridV1::new( - PackedArray::from( - table - .insert_axis(Axis(0)) - .insert_axis(Axis(new_axis)) - .view() - .into_dyn(), - ), - vec![ - NodeValues::UseThese(vec![info.fac0]), - NodeValues::UseThese(if index == 0 { - info.x0.clone() - } else { - vec![1.0] - }), - NodeValues::UseThese(if index == 0 { - vec![1.0] - } else { - info.x0.clone() - }), - ], - ) - .into() - })); - } - - let pid = grid.channels()[0].entry()[0].0[1 - index]; - - Ok(( - Array1::from_iter(sub_fk_tables) - .into_shape((1, grid.bin_info().bins(), channels0.len())) - .unwrap(), - channels0 - .iter() - .map(|&a| { - channel![ - if index == 0 { a } else { pid }, - if index == 0 { pid } else { a }, - 1.0 - ] - }) - .collect(), - )) -} - -pub(crate) fn evolve_slice_with_two( - grid: &Grid, - operator: &ArrayView4, - info: &OperatorSliceInfo, - order_mask: &[bool], - xi: (f64, f64), - alphas_table: &AlphasTable, -) -> Result<(Array3, Vec), GridError> { - let gluon_has_pid_zero = gluon_has_pid_zero(grid); - - // TODO: generalize by iterating up to `n` - let (pid_indices, pids01): (Vec<_>, Vec<_>) = (0..2) - .map(|d| { - pid_slices(operator, info, gluon_has_pid_zero, &|pid1| { - grid.channels() - .iter() - .flat_map(Channel::entry) - .any(|(pids, _)| pids[d] == pid1) - }) - }) - .collect::, _>>()? - .into_iter() - .unzip(); - - let mut channels0: Vec<_> = pids01 - .iter() - .map(|pids| pids.iter().map(|&(pid0, _)| pid0)) - .multi_cartesian_product() - .collect(); - channels0.sort_unstable(); - channels0.dedup(); - let channels0 = channels0; - - let mut sub_fk_tables = Vec::with_capacity(grid.bin_info().bins() * channels0.len()); - - // TODO: generalize to `n` - let mut last_x1 = vec![Vec::new(); 2]; - let mut operators = vec![Vec::new(); 2]; - - for subgrids_oc in grid.subgrids().axis_iter(Axis(1)) { - let mut tables = vec![Array2::zeros((info.x0.len(), info.x0.len())); channels0.len()]; - - for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { - let (x1, array) = ndarray_from_subgrid_orders_slice_many( - grid, - info.fac1, - grid.kinematics(), - &subgrids_o, - grid.orders(), - order_mask, - (xi.0, xi.1, 1.0), - alphas_table, - )?; - - // skip over zero arrays to speed up evolution and avoid problems with NaNs - let Some(array) = array else { - continue; - }; - - let array = array.into_dimensionality::().unwrap(); - - for (last_x1, x1, pid_indices, operators) in - izip!(&mut last_x1, x1, &pid_indices, &mut operators) - { - if (last_x1.len() != x1.len()) - || last_x1 - .iter() - .zip(x1.iter()) - .any(|(&lhs, &rhs)| !approx_eq!(f64, lhs, rhs, ulps = EVOLUTION_TOL_ULPS)) - { - *operators = operator_slices(operator, info, pid_indices, &x1)?; - *last_x1 = x1; - } - } - - let mut tmp = Array2::zeros((last_x1[0].len(), info.x0.len())); - - for (pids1, factor) in channel1 - .entry() - .iter() - .map(|(pids1, factor)| ([pids1[0], pids1[1]], factor)) - { - for (fk_table, ops) in - channels0 - .iter() - .zip(tables.iter_mut()) - .filter_map(|(pids0, fk_table)| { - izip!(pids0, &pids1, &pids01, &operators) - .map(|(&pid0, &pid1, pids, operators)| { - pids.iter().zip(operators).find_map(|(&(p0, p1), op)| { - ((p0 == pid0) && (p1 == pid1)).then_some(op) - }) - }) - // TODO: avoid using `collect` - .collect::>>() - .map(|ops| (fk_table, ops)) - }) - { - linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, &mut tmp); - linalg::general_mat_mul(*factor, ops[0], &tmp, 1.0, fk_table); - } - } - } - - sub_fk_tables.extend(tables.into_iter().map(|table| { - PackedQ1X2SubgridV1::new( - PackedArray::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), - vec![ - NodeValues::UseThese(vec![info.fac0]), - NodeValues::UseThese(info.x0.clone()), - NodeValues::UseThese(info.x0.clone()), - ], - ) - .into() - })); - } - - Ok(( - Array1::from_iter(sub_fk_tables) - .into_shape((1, grid.bin_info().bins(), channels0.len())) - .unwrap(), - channels0 - .iter() - .map(|c| channel![c[0], c[1], 1.0]) - .collect(), - )) -} - -pub(crate) fn evolve_slice_with_two2( - grid: &Grid, - operators: &[ArrayView4], - infos: &[OperatorSliceInfo], - order_mask: &[bool], - xi: (f64, f64), - alphas_table: &AlphasTable, -) -> Result<(Array3, Vec), GridError> { - let gluon_has_pid_zero = gluon_has_pid_zero(grid); - - // TODO: implement matching of different scales for different EKOs - let mut fac1_scales: Vec<_> = infos.iter().map(|info| info.fac1).collect(); - fac1_scales.sort_by(f64::total_cmp); - assert!(fac1_scales.windows(2).all(|scales| approx_eq!( - f64, - scales[0], - scales[1], - ulps = EVOLUTION_TOL_ULPS - ))); - let fac1 = fac1_scales[0]; - - // TODO: generalize by iterating up to `n` - let (pid_indices, pids01): (Vec<_>, Vec<_>) = izip!(0..2, operators, infos) - .map(|(d, operator, info)| { - pid_slices(operator, info, gluon_has_pid_zero, &|pid1| { - grid.channels() - .iter() - .flat_map(Channel::entry) - .any(|(pids, _)| pids[d] == pid1) - }) - }) - .collect::, _>>()? - .into_iter() - .unzip(); - - let mut channels0: Vec<_> = pids01 - .iter() - .map(|pids| pids.iter().map(|&(pid0, _)| pid0)) - .multi_cartesian_product() - .collect(); - channels0.sort_unstable(); - channels0.dedup(); - let channels0 = channels0; - - let mut sub_fk_tables = Vec::with_capacity(grid.bin_info().bins() * channels0.len()); - - // TODO: generalize to `n` - let mut last_x1 = vec![Vec::new(); 2]; - let mut eko_slices = vec![Vec::new(); 2]; - - for subgrids_oc in grid.subgrids().axis_iter(Axis(1)) { - assert_eq!(infos[0].x0.len(), infos[1].x0.len()); - - let mut tables = - vec![Array2::zeros((infos[0].x0.len(), infos[1].x0.len())); channels0.len()]; - - for (subgrids_o, channel1) in subgrids_oc.axis_iter(Axis(1)).zip(grid.channels()) { - let (x1, array) = ndarray_from_subgrid_orders_slice( - grid, - fac1, - grid.kinematics(), - &subgrids_o, - grid.orders(), - order_mask, - xi, - alphas_table, - )?; - - // skip over zero arrays to speed up evolution and avoid problems with NaNs - let Some(array) = array else { - continue; - }; - - for (last_x1, x1, pid_indices, slices, operator, info) in izip!( - &mut last_x1, - x1, - &pid_indices, - &mut eko_slices, - operators, - infos - ) { - if (last_x1.len() != x1.len()) - || last_x1 - .iter() - .zip(x1.iter()) - .any(|(&lhs, &rhs)| !approx_eq!(f64, lhs, rhs, ulps = EVOLUTION_TOL_ULPS)) - { - *slices = operator_slices(operator, info, pid_indices, &x1)?; - *last_x1 = x1; - } - } - - let mut tmp = Array2::zeros((last_x1[0].len(), infos[1].x0.len())); - - for (pids1, factor) in channel1 - .entry() - .iter() - .map(|(pids1, factor)| ([pids1[0], pids1[1]], factor)) - { - for (fk_table, ops) in - channels0 - .iter() - .zip(tables.iter_mut()) - .filter_map(|(pids0, fk_table)| { - izip!(pids0, &pids1, &pids01, &eko_slices) - .map(|(&pid0, &pid1, pids, slices)| { - pids.iter().zip(slices).find_map(|(&(p0, p1), op)| { - ((p0 == pid0) && (p1 == pid1)).then_some(op) - }) - }) - // TODO: avoid using `collect` - .collect::>>() - .map(|ops| (fk_table, ops)) - }) - { - // tmp = array * ops[1]^T - linalg::general_mat_mul(1.0, &array, &ops[1].t(), 0.0, &mut tmp); - // fk_table += factor * ops[0] * tmp - linalg::general_mat_mul(*factor, ops[0], &tmp, 1.0, fk_table); - } - } - } - - sub_fk_tables.extend(tables.into_iter().map(|table| { - PackedQ1X2SubgridV1::new( - PackedArray::from_ndarray(table.insert_axis(Axis(0)).view(), 0, 1), - vec![ - NodeValues::UseThese(vec![infos[0].fac0]), - NodeValues::UseThese(infos[0].x0.clone()), - NodeValues::UseThese(infos[1].x0.clone()), - ], - ) - .into() - })); - } - - Ok(( - Array1::from_iter(sub_fk_tables) - .into_shape((1, grid.bin_info().bins(), channels0.len())) - .unwrap(), - channels0 - .iter() - .map(|c| channel![c[0], c[1], 1.0]) - .collect(), - )) -} - pub(crate) fn evolve_slice_with_many( grid: &Grid, operators: &[ArrayView4], From 461d53662d3a33f2231b5381e3545e3b118d2c3b Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 10 Oct 2024 14:21:36 +0200 Subject: [PATCH 154/213] Remove `Convolution::None` and start fixing the importing of DIS grids --- pineappl/src/convolutions.rs | 8 ---- pineappl/src/evolution.rs | 8 ---- pineappl/src/fk_table.rs | 51 ++++++++++----------- pineappl/src/grid.rs | 21 +++------ pineappl_cli/src/export/applgrid.rs | 12 +---- pineappl_cli/src/helpers.rs | 6 +-- pineappl_cli/src/import.rs | 14 +++--- pineappl_cli/src/import/applgrid.rs | 71 ++++++++++++----------------- pineappl_cli/src/import/fastnlo.rs | 12 +---- pineappl_cli/src/import/fktable.rs | 40 ++++++++-------- pineappl_cli/src/plot.rs | 13 +----- pineappl_cli/tests/import.rs | 30 ++++++------ 12 files changed, 111 insertions(+), 175 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 235c8d6e..6edd30ae 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -240,7 +240,6 @@ impl<'a> ConvolutionCache<'a> { let imua2 = self.imua2[indices[0]]; (imua2, self.mua2_grid[imua2]) } - Convolution::None => unreachable!(), }; *xfx_cache.entry((pid, ix, imu2)).or_insert_with(|| { let x = self.x_grid[ix]; @@ -336,9 +335,6 @@ impl<'a> ConvolutionCache<'a> { /// Data type that indentifies different types of convolutions. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum Convolution { - // TODO: eventually get rid of this value - /// No convolution. - None, /// Unpolarized parton distribution function. The integer denotes the type of hadron with a PDG /// MC ID. UnpolPDF(i32), @@ -357,7 +353,6 @@ impl Convolution { #[must_use] pub const fn charge_conjugate(&self) -> Self { match *self { - Self::None => Self::None, Self::UnpolPDF(pid) => Self::UnpolPDF(pids::charge_conjugate_pdg_pid(pid)), Self::PolPDF(pid) => Self::PolPDF(pids::charge_conjugate_pdg_pid(pid)), Self::UnpolFF(pid) => Self::UnpolFF(pids::charge_conjugate_pdg_pid(pid)), @@ -369,7 +364,6 @@ impl Convolution { #[must_use] pub const fn pid(&self) -> Option { match *self { - Self::None => None, Self::UnpolPDF(pid) | Self::PolPDF(pid) | Self::UnpolFF(pid) | Self::PolFF(pid) => { Some(pid) } @@ -383,7 +377,6 @@ mod tests { #[test] fn convolution_charge_conjugate() { - assert_eq!(Convolution::None.charge_conjugate(), Convolution::None); assert_eq!( Convolution::UnpolPDF(2212).charge_conjugate(), Convolution::UnpolPDF(-2212) @@ -404,7 +397,6 @@ mod tests { #[test] fn convolution_pid() { - assert_eq!(Convolution::None.pid(), None); assert_eq!(Convolution::UnpolPDF(2212).pid(), Some(2212)); assert_eq!(Convolution::PolPDF(2212).pid(), Some(2212)); assert_eq!(Convolution::UnpolFF(2212).pid(), Some(2212)); diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 0b849e1a..0283d6f5 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -1,7 +1,6 @@ //! Supporting classes and functions for [`Grid::evolve_with_slice_iter`]. use super::boc::{Channel, Kinematics, Order}; -use super::convolutions::Convolution; use super::grid::{Grid, GridError}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; @@ -388,13 +387,6 @@ pub(crate) fn evolve_slice_with_many( assert_eq!(operators.len(), infos.len()); assert_eq!(operators.len(), grid.convolutions().len()); - assert_eq!( - grid.convolutions() - .iter() - .filter(|&conv| *conv == Convolution::None) - .count(), - 0 - ); let (pid_indices, pids01): (Vec<_>, Vec<_>) = izip!(0..infos.len(), operators, infos) .map(|(d, operator, info)| { diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index ac7e6faa..f5ec3e76 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -1,12 +1,13 @@ //! Provides the [`FkTable`] type. use super::boc::{Kinematics, Order}; -use super::convolutions::{Convolution, ConvolutionCache}; +use super::convolutions::ConvolutionCache; use super::grid::Grid; use super::subgrid::Subgrid; use float_cmp::approx_eq; use ndarray::ArrayD; use std::fmt::{self, Display, Formatter}; +use std::iter; use std::str::FromStr; use thiserror::Error; @@ -148,43 +149,34 @@ impl FkTable { let x_grid = self.x_grid(); let mut dim = vec![self.grid.bin_info().bins(), self.grid.channels().len()]; - dim.extend(self.grid.convolutions().iter().map(|convolution| { - if *convolution == Convolution::None { - 1 - } else { - x_grid.len() - } - })); + dim.extend(iter::repeat(x_grid.len()).take(self.grid.convolutions().len())); + let mut idx = vec![0; dim.len()]; let mut result = ArrayD::zeros(dim); for ((_, bin, channel), subgrid) in self.grid().subgrids().indexed_iter() { - let indices: Vec<_> = self + let indices: Vec> = self .grid .convolutions() .iter() .enumerate() - .map(|(index, convolution)| { + .map(|(index, _)| { subgrid .node_values() .iter() .zip(self.grid.kinematics()) .find_map(|(node_values, kin)| { matches!(kin, Kinematics::X(i) if *i == index).then(|| { - if *convolution == Convolution::None { - vec![0] - } else { - node_values - .values() - .iter() - .map(|&s| { - x_grid - .iter() - .position(|&x| approx_eq!(f64, s, x, ulps = 2)) - // UNWRAP: must be guaranteed by the grid constructor - .unwrap() - }) - .collect() - } + node_values + .values() + .iter() + .map(|&s| { + x_grid + .iter() + .position(|&x| approx_eq!(f64, s, x, ulps = 2)) + // UNWRAP: must be guaranteed by the grid constructor + .unwrap() + }) + .collect() }) }) // UNWRAP: must be guaranteed by the grid constructor @@ -193,10 +185,13 @@ impl FkTable { .collect(); for (index, value) in subgrid.indexed_iter() { - assert_eq!(index.len(), 3); assert_eq!(index[0], 0); - // result[[bin, channel, indices1[index[1]], indices2[index[2]]]] = value; - result[[bin, channel, indices[0][index[1]], indices[1][index[2]]]] = value; + idx[0] = bin; + idx[1] = channel; + for i in 2..result.shape().len() { + idx[i] = indices[i - 2][index[i - 1]]; + } + result[idx.as_slice()] = value; } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 3c8e881b..ada5f6b4 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1167,21 +1167,15 @@ impl Grid { .node_values() .iter() .zip(self.kinematics()) - .filter(|(_, kin)| matches!(kin, Kinematics::X(_))) - .zip(self.convolutions()) - .filter_map(|((node_values, _), convolution)| { - (*convolution != Convolution::None).then_some(node_values.values()) - }) + .filter_map(|(nv, kin)| matches!(kin, Kinematics::X(_)).then(|| nv.values())) .flatten(), ); x1.sort_by(f64::total_cmp); x1.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = EVOLVE_INFO_TOL_ULPS)); - for (index, convolution) in self.convolutions().iter().enumerate() { - if *convolution != Convolution::None { - pids1.extend(channel.entry().iter().map(|(pids, _)| pids[index])); - } + for (index, _) in self.convolutions().iter().enumerate() { + pids1.extend(channel.entry().iter().map(|(pids, _)| pids[index])); } pids1.sort_unstable(); @@ -1359,6 +1353,9 @@ impl Grid { use super::evolution::EVOLVE_INFO_TOL_ULPS; use itertools::izip; + // TODO: generalize this function for an arbitrary number of convolutions + assert_eq!(self.convolutions().len(), 2); + let mut lhs: Option = None; // Q2 slices we use let mut used_op_fac1 = Vec::new(); @@ -1437,12 +1434,6 @@ impl Grid { let views = [operator_a.view(), operator_b.view()]; let infos = [info_a, info_b]; - assert!( - (self.convolutions()[0] != Convolution::None) - && (self.convolutions()[1] != Convolution::None), - "only one convolution found, use `Grid::evolve_with_slice_iter` instead" - ); - let (subgrids, channels) = evolution::evolve_slice_with_many( self, &views, diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 9554470e..40c3fd45 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -4,7 +4,6 @@ use float_cmp::approx_eq; use lhapdf::Pdf; use ndarray::{s, Axis}; use pineappl::boc::{Kinematics, Order}; -use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::subgrid::Subgrid; @@ -147,10 +146,7 @@ pub fn convert_into_applgrid( assert_eq!(factor, 1.0); pids.iter() - .zip(grid.convolutions()) - .filter_map(|(&pid, convolution)| { - (*convolution != Convolution::None).then_some(pid) - }) + .copied() .chain(iter::repeat(0)) .take(2) .collect::>() @@ -237,11 +233,7 @@ pub fn convert_into_applgrid( map @ Map::ApplGridF2 => panic!("export does not support {map:?}"), }, grid.channels().len().try_into().unwrap(), - grid.convolutions() - .iter() - .filter(|&conv| *conv != Convolution::None) - .count() - == 1, + grid.convolutions().len() == 1, ); let appl_q2: Vec<_> = (0..igrid.Ntau()).map(|i| igrid.getQ2(i)).collect(); let appl_x1: Vec<_> = (0..igrid.Ny1()).map(|i| igrid.getx1(i)).collect(); diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 9dbcf19e..bf29f920 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -297,8 +297,7 @@ pub fn convolve_scales( // if we can not figure out the type of the convolution from the PDF set, we // assume it from the grid convolution at the same index match convolution { - // if the grid convolution is None, we assume it's a proton PDF - Convolution::None | Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), + Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), Convolution::PolPDF(_) => Convolution::PolPDF(pid), Convolution::UnpolFF(_) => Convolution::UnpolFF(pid), Convolution::PolFF(_) => Convolution::PolFF(pid), @@ -468,8 +467,7 @@ pub fn convolve_subgrid( // if we can not figure out the type of the convolution from the PDF set, we // assume it from the grid convolution at the same index match convolution { - // if the grid convolution is None, we assume it's a proton PDF - Convolution::None | Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), + Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), Convolution::PolPDF(_) => Convolution::PolPDF(pid), Convolution::UnpolFF(_) => Convolution::UnpolFF(pid), Convolution::PolFF(_) => Convolution::PolFF(pid), diff --git a/pineappl_cli/src/import.rs b/pineappl_cli/src/import.rs index e16bcafe..cf6a6ff0 100644 --- a/pineappl_cli/src/import.rs +++ b/pineappl_cli/src/import.rs @@ -20,7 +20,6 @@ fn convert_applgrid( input: &Path, alpha: u32, conv_funs: &mut [Pdf], - dis_pid: i32, _: usize, ) -> Result<(&'static str, Grid, Vec, usize)> { use pineappl_applgrid::ffi; @@ -28,7 +27,7 @@ fn convert_applgrid( // TODO: check AMCATNLO scale variations let mut grid = ffi::make_grid(input.to_str().unwrap())?; - let pgrid = applgrid::convert_applgrid(grid.pin_mut(), alpha, dis_pid)?; + let pgrid = applgrid::convert_applgrid(grid.pin_mut(), alpha)?; let results = applgrid::convolve_applgrid(grid.pin_mut(), conv_funs); Ok(("APPLgrid", pgrid, results, 1)) @@ -39,7 +38,6 @@ fn convert_applgrid( _: &Path, _: u32, _: &mut [Pdf], - _: i32, _: usize, ) -> Result<(&'static str, Grid, Vec, usize)> { Err(anyhow!( @@ -134,14 +132,14 @@ fn convert_fastnlo( } #[cfg(feature = "fktable")] -fn convert_fktable(input: &Path, dis_pid: i32) -> Result<(&'static str, Grid, Vec, usize)> { - let fktable = fktable::convert_fktable(input, dis_pid)?; +fn convert_fktable(input: &Path) -> Result<(&'static str, Grid, Vec, usize)> { + let fktable = fktable::convert_fktable(input)?; Ok(("fktable", fktable, vec![], 1)) } #[cfg(not(feature = "fktable"))] -fn convert_fktable(_: &Path, _: i32) -> Result<(&'static str, Grid, Vec, usize)> { +fn convert_fktable(_: &Path) -> Result<(&'static str, Grid, Vec, usize)> { Err(anyhow!( "you need to install `pineappl` with feature `fktable`" )) @@ -170,9 +168,9 @@ fn convert_grid( input, alpha, fun_names, member, dis_pid, scales, fnlo_mur, fnlo_muf, ); } else if extension == "dat" { - return convert_fktable(input, dis_pid); + return convert_fktable(input); } else if extension == "appl" || extension == "root" { - return convert_applgrid(input, alpha, conv_funs, dis_pid, scales); + return convert_applgrid(input, alpha, conv_funs, scales); } } diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 53b5a076..a876775c 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -11,7 +11,7 @@ use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_applgrid::ffi::{self, grid}; use std::f64::consts::TAU; use std::pin::Pin; -use std::ptr; +use std::{iter, ptr}; fn convert_to_pdg_id(pid: usize) -> i32 { let pid = i32::try_from(pid).unwrap() - 6; @@ -24,7 +24,7 @@ fn convert_to_pdg_id(pid: usize) -> i32 { } } -fn reconstruct_channels(grid: &grid, order: i32, dis_pid: i32) -> Vec { +fn reconstruct_channels(grid: &grid, order: i32) -> Vec { let pdf = unsafe { &*grid.genpdf(order, false) }; let nproc: usize = pdf.Nproc().try_into().unwrap(); @@ -43,7 +43,7 @@ fn reconstruct_channels(grid: &grid, order: i32, dis_pid: i32) -> Vec { for i in 0..nproc { if results[i] != 0.0 { - channels[i].push((vec![convert_to_pdg_id(a), dis_pid], results[i])); + channels[i].push((vec![convert_to_pdg_id(a)], results[i])); } } } else { @@ -71,7 +71,7 @@ fn reconstruct_channels(grid: &grid, order: i32, dis_pid: i32) -> Vec { channels.into_iter().map(Channel::new).collect() } -pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Result { +pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { let bin_limits: Vec<_> = (0..=grid.Nobs_internal()) .map(|i| grid.obslow_internal(i)) .collect(); @@ -125,14 +125,31 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul let mut grids = Vec::with_capacity(orders.len()); // from APPLgrid alone we don't know what type of convolution we have - let mut convolutions = vec![Convolution::UnpolPDF(2212); 2]; - - if grid.isDIS() { - convolutions[1] = Convolution::None; + let convolutions = vec![Convolution::UnpolPDF(2212); if grid.isDIS() { 1 } else { 2 }]; + // TODO: read out interpolation parameters from APPLgrid + let mut interps = vec![Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + )]; + for _ in 0..convolutions.len() { + interps.push(Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + )); } for (i, order) in orders.into_iter().enumerate() { - let channels = reconstruct_channels(&grid, i.try_into().unwrap(), dis_pid); + let channels = reconstruct_channels(&grid, i.try_into().unwrap()); let lumis_len = channels.len(); let mut pgrid = Grid::new( PidBasis::Pdg, @@ -140,38 +157,10 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32, dis_pid: i32) -> Resul vec![order], bin_limits.clone(), convolutions.clone(), - // TODO: read out interpolation parameters from APPLgrid - vec![ - Interp::new( - 1e2, - 1e8, - 40, - 3, - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - ], - // TODO: change kinematics for DIS - vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + interps.clone(), + iter::once(Kinematics::Scale(0)) + .chain((0..convolutions.len()).map(|idx| Kinematics::X(idx))) + .collect(), Scales { ren: ScaleFuncForm::Scale(0), fac: ScaleFuncForm::Scale(0), diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 708efefc..34d14fab 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -104,16 +104,8 @@ fn convert_coeff_add_fix( let npdf = usize::try_from(table_as_add_base.GetNPDF()).unwrap(); assert!(npdf <= 2); - let convolutions = (0..2) - .map(|index| { - if index < npdf { - // TODO: how do we determined the PID/type of the convolution for fixed tables? - Convolution::UnpolPDF(2212) - } else { - Convolution::None - } - }) - .collect(); + // TODO: extract the proper convolution PIDs + let convolutions = vec![Convolution::UnpolPDF(2212); npdf]; let mut grid = Grid::new( PidBasis::Pdg, diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index c1b2d747..46cf6b98 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Context, Result}; use flate2::read::GzDecoder; use ndarray::s; -use pineappl::boc::{Kinematics, Order, ScaleFuncForm, Scales}; +use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; use pineappl::convolutions::Convolution; use pineappl::grid::Grid; @@ -28,7 +28,7 @@ enum FkTableSection { FastKernel, } -fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { +fn read_fktable(reader: impl BufRead) -> Result { let mut section = FkTableSection::Sof; let mut flavor_mask = Vec::::new(); let mut x_grid = Vec::new(); @@ -94,7 +94,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { .iter() .enumerate() .filter(|&(_, &value)| value) - .map(|(index, _)| channel![basis[index], dis_pid, 1.0]) + .map(|(index, _)| Channel::new(vec![(vec![basis[index]], 1.0)])) .collect() }; @@ -105,14 +105,11 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { vec![Order::new(0, 0, 0, 0, 0)], (0..=ndata).map(Into::into).collect(), // legacy FK-tables only support unpolarized proton PDFs - vec![ - Convolution::UnpolPDF(2212), - if hadronic { - Convolution::UnpolPDF(2212) - } else { - Convolution::None - }, - ], + if hadronic { + vec![Convolution::UnpolPDF(2212); 2] + } else { + vec![Convolution::UnpolPDF(2212)] + }, // TODO: what are sensible parameters for FK-tables? vec![ Interp::new( @@ -281,11 +278,18 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { { *subgrid = PackedQ1X2SubgridV1::new( array, - vec![ - NodeValues::UseThese(vec![q0 * q0]), - NodeValues::UseThese(x_grid.clone()), - NodeValues::UseThese(if hadronic { x_grid.clone() } else { vec![1.0] }), - ], + if hadronic { + vec![ + NodeValues::UseThese(vec![q0 * q0]), + NodeValues::UseThese(x_grid.clone()), + NodeValues::UseThese(x_grid.clone()), + ] + } else { + vec![ + NodeValues::UseThese(vec![q0 * q0]), + NodeValues::UseThese(x_grid.clone()), + ] + }, ) .into(); } @@ -293,7 +297,7 @@ fn read_fktable(reader: impl BufRead, dis_pid: i32) -> Result { Ok(grid) } -pub fn convert_fktable(input: &Path, dis_pid: i32) -> Result { +pub fn convert_fktable(input: &Path) -> Result { let reader = GzDecoder::new(File::open(input)?); let mut archive = Archive::new(reader); @@ -304,7 +308,7 @@ pub fn convert_fktable(input: &Path, dis_pid: i32) -> Result { if let Some(extension) = path.extension() { if extension == "dat" { - return read_fktable(BufReader::new(file), dis_pid); + return read_fktable(BufReader::new(file)); } } } diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index 82fb44de..35ad37bc 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -6,7 +6,6 @@ use clap::{Parser, ValueHint}; use itertools::Itertools; use ndarray::Axis; use pineappl::boc::{Channel, Kinematics}; -use pineappl::convolutions::Convolution; use pineappl::grid::Grid; use pineappl::subgrid::Subgrid; use rayon::{prelude::*, ThreadPoolBuilder}; @@ -77,16 +76,8 @@ fn map_format_channel(channel: &Channel, grid: &Grid) -> String { .entry() .iter() .map(|(pids, _)| { - grid.convolutions() - .iter() - .zip(pids) - .map(|(convolution, &pid)| { - if *convolution == Convolution::None { - "" - } else { - grid.pid_basis().to_latex_str(pid) - } - }) + pids.iter() + .map(|&pid| grid.pid_basis().to_latex_str(pid)) .collect::>() .join("") }) diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 871b951b..83a8d23f 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -675,7 +675,7 @@ fn import_flex_grid_15() { #[test] #[cfg(feature = "fktable")] fn import_dis_fktable() { - use ndarray::Array4; + use ndarray::Array3; use pineappl::fk_table::FkTable; use pineappl::grid::Grid; use std::fs::File; @@ -717,6 +717,7 @@ fn import_dis_fktable() { // TODO: this should ideally be a unit test, but we need an FK table that we don't convert + assert_eq!(fk_table.grid().kinematics().len(), 2); assert_eq!(fk_table.muf2(), 1.65 * 1.65); assert_eq!( fk_table.x_grid(), @@ -820,13 +821,14 @@ fn import_dis_fktable() { 0.8837966741980419, 0.9126417795942889, 0.9416284084927907, - 0.9707498946430192 + 0.9707498946430192, + 1.0 ] ); - let table: Array4 = fk_table.table().into_dimensionality().unwrap(); + let table: Array3 = fk_table.table().into_dimensionality().unwrap(); - assert_eq!(table.dim(), (20, 9, 100, 1)); + assert_eq!(table.dim(), (20, 9, 101)); assert_eq!( table .indexed_iter() @@ -834,16 +836,16 @@ fn import_dis_fktable() { .take(10) .collect::>(), [ - ((0, 0, 0, 0), &4.506605409085538e-8), - ((0, 0, 1, 0), &1.8561090273141668e-8), - ((0, 0, 2, 0), &-3.3821015317570252e-9), - ((0, 0, 3, 0), &1.980084314325426e-9), - ((0, 0, 4, 0), &2.187815687938248e-9), - ((0, 0, 5, 0), &1.3280152778522626e-9), - ((0, 0, 6, 0), &1.3848470515483116e-9), - ((0, 0, 7, 0), &1.5145898293299224e-9), - ((0, 0, 8, 0), &1.6942313031679552e-9), - ((0, 0, 9, 0), &1.9734220063025288e-9), + ((0, 0, 0), &4.506605409085538e-8), + ((0, 0, 1), &1.8561090273141668e-8), + ((0, 0, 2), &-3.3821015317570252e-9), + ((0, 0, 3), &1.980084314325426e-9), + ((0, 0, 4), &2.187815687938248e-9), + ((0, 0, 5), &1.3280152778522626e-9), + ((0, 0, 6), &1.3848470515483116e-9), + ((0, 0, 7), &1.5145898293299224e-9), + ((0, 0, 8), &1.6942313031679552e-9), + ((0, 0, 9), &1.9734220063025288e-9), ] ); } From 0a66c20bc4ff1007fd329d6926aa568ec7cfaf24 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 10 Oct 2024 15:12:17 +0200 Subject: [PATCH 155/213] Fix failing doctest and make DIS FK-table subgrids 2-dimensional --- pineappl_cli/src/import/fktable.rs | 144 +++++++++++++++++++---------- pineappl_cli/tests/import.rs | 3 +- 2 files changed, 94 insertions(+), 53 deletions(-) diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 46cf6b98..8b58bc6b 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -98,6 +98,12 @@ fn read_fktable(reader: impl BufRead) -> Result { .collect() }; + let convolutions = if hadronic { + vec![Convolution::UnpolPDF(2212); 2] + } else { + vec![Convolution::UnpolPDF(2212)] + }; + // construct `Grid` let fktable = Grid::new( PidBasis::Evol, @@ -105,43 +111,65 @@ fn read_fktable(reader: impl BufRead) -> Result { vec![Order::new(0, 0, 0, 0, 0)], (0..=ndata).map(Into::into).collect(), // legacy FK-tables only support unpolarized proton PDFs + convolutions.clone(), + // TODO: what are sensible parameters for FK-tables? if hadronic { - vec![Convolution::UnpolPDF(2212); 2] + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ] } else { - vec![Convolution::UnpolPDF(2212)] + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ] + }, + if hadronic { + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2] + } else { + vec![Kinematics::Scale(0), Kinematics::X1] }, - // TODO: what are sensible parameters for FK-tables? - vec![ - Interp::new( - 1e2, - 1e8, - 40, - 3, - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - ], - // TODO: change kinematics for DIS - vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], // TODO: is this correct? Scales { ren: ScaleFuncForm::NoScale, @@ -152,9 +180,13 @@ fn read_fktable(reader: impl BufRead) -> Result { grid = Some(fktable); - arrays = iter::repeat(PackedArray::new(vec![1, nx1, nx2])) - .take(flavor_mask.iter().filter(|&&value| value).count()) - .collect(); + arrays = iter::repeat(PackedArray::new(if hadronic { + vec![1, nx1, nx2] + } else { + vec![1, nx1] + })) + .take(flavor_mask.iter().filter(|&&value| value).count()) + .collect(); } _ => match section { FkTableSection::GridInfo => { @@ -216,22 +248,29 @@ fn read_fktable(reader: impl BufRead) -> Result { { *subgrid = PackedQ1X2SubgridV1::new( array, - vec![ - NodeValues::UseThese(vec![q0 * q0]), - NodeValues::UseThese(x_grid.clone()), - NodeValues::UseThese(if hadronic { - x_grid.clone() - } else { - vec![1.0] - }), - ], + if hadronic { + vec![ + NodeValues::UseThese(vec![q0 * q0]), + NodeValues::UseThese(x_grid.clone()), + NodeValues::UseThese(x_grid.clone()), + ] + } else { + vec![ + NodeValues::UseThese(vec![q0 * q0]), + NodeValues::UseThese(x_grid.clone()), + ] + }, ) .into(); } - arrays = iter::repeat(PackedArray::new(vec![1, nx1, nx2])) - .take(flavor_mask.iter().filter(|&&value| value).count()) - .collect(); + arrays = iter::repeat(PackedArray::new(if hadronic { + vec![1, nx1, nx2] + } else { + vec![1, nx1] + })) + .take(flavor_mask.iter().filter(|&&value| value).count()) + .collect(); last_bin = bin; } @@ -257,8 +296,11 @@ fn read_fktable(reader: impl BufRead) -> Result { .zip(grid_values.iter()) .filter(|(_, value)| **value != 0.0) { - array[[0, x1, x2]] = - x_grid[x1] * if hadronic { x_grid[x2] } else { 1.0 } * value; + if hadronic { + array[[0, x1, x2]] = x_grid[x1] * x_grid[x2] * value; + } else { + array[[0, x1]] = x_grid[x1] * value; + } } } _ => {} diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 83a8d23f..25b0d1a0 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -822,13 +822,12 @@ fn import_dis_fktable() { 0.9126417795942889, 0.9416284084927907, 0.9707498946430192, - 1.0 ] ); let table: Array3 = fk_table.table().into_dimensionality().unwrap(); - assert_eq!(table.dim(), (20, 9, 101)); + assert_eq!(table.dim(), (20, 9, 100)); assert_eq!( table .indexed_iter() From 1e1715514eb8d04cc04a205640bc49d735d448a2 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 10 Oct 2024 15:29:34 +0200 Subject: [PATCH 156/213] Fix dimensionality of imported DIS fastNLO tables --- pineappl_cli/src/import.rs | 13 +--- pineappl_cli/src/import/fastnlo.rs | 114 +++++++++++++++++------------ pineappl_cli/tests/import.rs | 2 - 3 files changed, 70 insertions(+), 59 deletions(-) diff --git a/pineappl_cli/src/import.rs b/pineappl_cli/src/import.rs index cf6a6ff0..7fb77871 100644 --- a/pineappl_cli/src/import.rs +++ b/pineappl_cli/src/import.rs @@ -51,7 +51,6 @@ fn convert_fastnlo( alpha: u32, conv_funs: &ConvFuns, member: usize, - dis_pid: i32, scales: usize, fnlo_mur: Option<&str>, fnlo_muf: Option<&str>, @@ -79,7 +78,7 @@ fn convert_fastnlo( } } - let grid = fastnlo::convert_fastnlo_table(&file, alpha, dis_pid)?; + let grid = fastnlo::convert_fastnlo_table(&file, alpha)?; let mut reader = ffi::downcast_lhapdf_to_reader_mut(file.as_mut().unwrap()); // TODO: scale-variation log conversion is only enabled for flex grids @@ -121,7 +120,6 @@ fn convert_fastnlo( _: u32, _: &ConvFuns, _: usize, - _: i32, _: usize, _: Option<&str>, _: Option<&str>, @@ -151,7 +149,6 @@ fn convert_grid( conv_funs: &mut [Pdf], fun_names: &ConvFuns, member: usize, - dis_pid: i32, scales: usize, fnlo_mur: Option<&str>, fnlo_muf: Option<&str>, @@ -164,9 +161,7 @@ fn convert_grid( .extension() .map_or(false, |ext| ext == "tab")) { - return convert_fastnlo( - input, alpha, fun_names, member, dis_pid, scales, fnlo_mur, fnlo_muf, - ); + return convert_fastnlo(input, alpha, fun_names, member, scales, fnlo_mur, fnlo_muf); } else if extension == "dat" { return convert_fktable(input); } else if extension == "appl" || extension == "root" { @@ -245,9 +240,6 @@ pub struct Opts { /// Do not optimize converted grid. #[arg(long)] no_optimize: bool, - /// Particle ID for the non-hadronic initial states if it cannot be determined from the grid. - #[arg(long, default_value_t = 11)] - dis_pid: i32, } impl Subcommand for Opts { @@ -263,7 +255,6 @@ impl Subcommand for Opts { &mut conv_funs, &self.conv_funs, 0, - self.dis_pid, self.scales, self.fnlo_mur.as_deref(), self.fnlo_muf.as_deref(), diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 34d14fab..e4a219af 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -27,10 +27,9 @@ fn pid_to_pdg_id(pid: i32) -> i32 { fn reconstruct_channels( table: &fastNLOCoeffAddBase, comb: &fastNLOPDFLinearCombinations, - dis_pid: i32, ) -> Vec { - let dis_pid = if table.GetNPDF() == 2 { 0 } else { dis_pid }; let mut channels = Vec::new(); + let npdf = table.GetNPDF(); // if there's a (non-empty) PDF coefficient vector reconstruct the channels; the advantage is // that we preserve the order of the channels in the PineAPPL grid @@ -38,15 +37,13 @@ fn reconstruct_channels( let mut entries = Vec::new(); for entry in ffi::GetPDFCoeff(table, pdf_entry) { - let a = pid_to_pdg_id(entry.first); - let b = if dis_pid == 0 { - pid_to_pdg_id(entry.second) - } else { - dis_pid - }; - let f = 1.0; + let mut pids = vec![pid_to_pdg_id(entry.first)]; + + if npdf == 2 { + pids.push(pid_to_pdg_id(entry.second)); + } - entries.push((vec![a, b], f)); + entries.push((pids, 1.0)); } channels.push(Channel::new(entries)); @@ -54,6 +51,8 @@ fn reconstruct_channels( // if the PDF coefficient vector was empty, we must reconstruct the channels in a different way if channels.is_empty() { + assert_eq!(npdf, 2); + let nsubproc = table.GetNSubproc().try_into().unwrap(); let mut xfx1 = [0.0; 13]; @@ -96,7 +95,6 @@ fn convert_coeff_add_fix( comb: &fastNLOPDFLinearCombinations, bins: usize, alpha: u32, - dis_pid: i32, ) -> Grid { let table_as_add_base = ffi::downcast_coeff_add_fix_to_base(table); @@ -109,7 +107,7 @@ fn convert_coeff_add_fix( let mut grid = Grid::new( PidBasis::Pdg, - reconstruct_channels(table_as_add_base, comb, dis_pid), + reconstruct_channels(table_as_add_base, comb), vec![Order { alphas: table_as_add_base.GetNpow().try_into().unwrap(), alpha, @@ -122,37 +120,64 @@ fn convert_coeff_add_fix( .collect(), convolutions, // TODO: read out interpolation parameters from fastNLO - vec![ - Interp::new( - 1e2, - 1e8, - 40, - 3, - ReweightMeth::NoReweight, - Map::ApplGridH0, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - Interp::new( - 2e-7, - 1.0, - 50, - 3, - ReweightMeth::ApplGridX, - Map::ApplGridF2, - InterpMeth::Lagrange, - ), - ], + if npdf == 2 { + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ] + } else { + vec![ + Interp::new( + 1e2, + 1e8, + 40, + 3, + ReweightMeth::NoReweight, + Map::ApplGridH0, + InterpMeth::Lagrange, + ), + Interp::new( + 2e-7, + 1.0, + 50, + 3, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + ), + ] + }, // TODO: change kinematics for DIS - vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + if npdf == 2 { + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2] + } else { + vec![Kinematics::Scale(0), Kinematics::X1] + }, Scales { ren: ScaleFuncForm::Scale(0), fac: ScaleFuncForm::Scale(0), @@ -265,7 +290,6 @@ fn convert_coeff_add_flex( _bins: usize, _alpha: u32, _ipub_units: i32, - _dis_pid: i32, ) -> Grid { todo!() @@ -459,7 +483,7 @@ fn convert_coeff_add_flex( // grid } -pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> Result { +pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32) -> Result { let file_as_reader = ffi::downcast_lhapdf_to_reader(file); let file_as_table = ffi::downcast_lhapdf_to_table(file); @@ -500,7 +524,6 @@ pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> bins, alpha, file_as_table.GetIpublunits(), - dis_pid, )); } } else { @@ -509,7 +532,6 @@ pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32, dis_pid: i32) -> linear_combinations, bins, alpha, - dis_pid, )); } } diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 25b0d1a0..1e0d6d94 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -22,7 +22,6 @@ Options: --digits-abs Set the number of fractional digits shown for absolute numbers [default: 7] --digits-rel Set the number of fractional digits shown for relative numbers [default: 7] --no-optimize Do not optimize converted grid - --dis-pid Particle ID for the non-hadronic initial states if it cannot be determined from the grid [default: 11] -h, --help Print help "; @@ -45,7 +44,6 @@ Options: --digits-abs Set the number of fractional digits shown for absolute numbers [default: 7] --digits-rel Set the number of fractional digits shown for relative numbers [default: 7] --no-optimize Do not optimize converted grid - --dis-pid Particle ID for the non-hadronic initial states if it cannot be determined from the grid [default: 11] -h, --help Print help "; From 1affcbb9f14f743259c83845177440f05f23cd51 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 11 Oct 2024 09:15:04 +0200 Subject: [PATCH 157/213] Add support for `polarized` and `time_like` fields --- pineappl_cli/src/evolve.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index 1c61922e..bdaf04e2 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -73,9 +73,19 @@ mod eko { const BASES_V1_DEFAULT_PIDS: [i32; 14] = [22, -6, -5, -4, -3, -2, -1, 21, 1, 2, 3, 4, 5, 6]; + #[derive(Deserialize)] + struct OperatorConfigsV1 { + #[serde(rename = "polarized")] + _polarized: bool, + #[serde(rename = "time_like")] + _time_like: bool, + } + #[derive(Deserialize)] struct OperatorV1 { mu0: f64, + #[serde(rename = "configs")] + _configs: OperatorConfigsV1, } #[derive(Deserialize)] From a06503b48a8fdcbd32515ef6bb23be7ef54fbc22 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 11 Oct 2024 10:22:50 +0200 Subject: [PATCH 158/213] Rename `Convolution` to `Conv` and introduce `ConvType` --- pineappl/src/convolutions.rs | 126 +++++++++++++++++----------- pineappl/src/grid.rs | 50 ++++++----- pineappl/src/v0.rs | 18 ++-- pineappl/tests/drell_yan_lo.rs | 14 +++- pineappl_capi/src/lib.rs | 20 +++-- pineappl_cli/src/helpers.rs | 20 ++--- pineappl_cli/src/import/applgrid.rs | 4 +- pineappl_cli/src/import/fastnlo.rs | 4 +- pineappl_cli/src/import/fktable.rs | 6 +- pineappl_cli/tests/import.rs | 9 +- 10 files changed, 155 insertions(+), 116 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 6edd30ae..1381593d 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -22,14 +22,14 @@ pub struct ConvolutionCache<'a> { imuf2: Vec, imua2: Vec, ix: Vec>, - pdg: Vec, + pdg: Vec, perm: Vec>, } impl<'a> ConvolutionCache<'a> { /// TODO pub fn new( - pdg: Vec, + pdg: Vec, xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { @@ -57,7 +57,7 @@ impl<'a> ConvolutionCache<'a> { .iter() .enumerate() .map(|(max_idx, conv)| { - conv.pid().map(|_| { + Some( self.pdg .iter() .take(max_idx + 1) @@ -66,15 +66,15 @@ impl<'a> ConvolutionCache<'a> { .find_map(|(idx, pdg)| { if conv == pdg { Some((idx, false)) - } else if *conv == pdg.charge_conjugate() { + } else if *conv == pdg.cc() { Some((idx, true)) } else { None } }) // TODO: convert `unwrap` to `Err` - .unwrap() - }) + .unwrap(), + ) }) .collect(); @@ -231,12 +231,12 @@ impl<'a> ConvolutionCache<'a> { }; let xfx = &mut self.xfx[idx]; let xfx_cache = &mut self.xfx_cache[idx]; - let (imu2, mu2) = match self.pdg[idx] { - Convolution::UnpolPDF(_) | Convolution::PolPDF(_) => { + let (imu2, mu2) = match self.pdg[idx].conv_type() { + ConvType::UnpolPDF | ConvType::PolPDF => { let imuf2 = self.imuf2[indices[0]]; (imuf2, self.muf2_grid[imuf2]) } - Convolution::UnpolFF(_) | Convolution::PolFF(_) => { + ConvType::UnpolFF | ConvType::PolFF => { let imua2 = self.imua2[indices[0]]; (imua2, self.mua2_grid[imua2]) } @@ -332,42 +332,70 @@ impl<'a> ConvolutionCache<'a> { } } +/// TODO +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub enum ConvType { + /// Unpolarized parton distribution function. + UnpolPDF, + /// Polarized parton distribution function. + PolPDF, + /// Unpolarized fragmentation function. + UnpolFF, + /// Polarized fragmentation function. + PolFF, +} + +impl ConvType { + /// TODO + pub fn new(polarized: bool, time_like: bool) -> Self { + match (polarized, time_like) { + (false, false) => Self::UnpolPDF, + (false, true) => Self::UnpolFF, + (true, false) => Self::PolPDF, + (true, true) => Self::PolFF, + } + } +} + /// Data type that indentifies different types of convolutions. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] -pub enum Convolution { - /// Unpolarized parton distribution function. The integer denotes the type of hadron with a PDG - /// MC ID. - UnpolPDF(i32), - /// Polarized parton distribution function. The integer denotes the type of hadron with a PDG - /// MC ID. - PolPDF(i32), - /// Unpolarized fragmentation function. The integer denotes the type of hadron with a PDG MC - /// ID. - UnpolFF(i32), - /// Polarized fragmentation function. The integer denotes the type of hadron with a PDG MC ID. - PolFF(i32), +pub struct Conv { + conv_type: ConvType, + pid: i32, } -impl Convolution { +impl Conv { + /// Constructor. + pub fn new(conv_type: ConvType, pid: i32) -> Self { + Self { conv_type, pid } + } + + /// TODO + pub fn with_pid(&self, pid: i32) -> Self { + Self { + conv_type: self.conv_type, + pid, + } + } + /// Return the convolution if the PID is charged conjugated. #[must_use] - pub const fn charge_conjugate(&self) -> Self { - match *self { - Self::UnpolPDF(pid) => Self::UnpolPDF(pids::charge_conjugate_pdg_pid(pid)), - Self::PolPDF(pid) => Self::PolPDF(pids::charge_conjugate_pdg_pid(pid)), - Self::UnpolFF(pid) => Self::UnpolFF(pids::charge_conjugate_pdg_pid(pid)), - Self::PolFF(pid) => Self::PolFF(pids::charge_conjugate_pdg_pid(pid)), + pub const fn cc(&self) -> Self { + Self { + conv_type: self.conv_type, + pid: pids::charge_conjugate_pdg_pid(self.pid), } } - /// Return the PID of the convolution if it has any. + /// Return the PID of the convolution. #[must_use] - pub const fn pid(&self) -> Option { - match *self { - Self::UnpolPDF(pid) | Self::PolPDF(pid) | Self::UnpolFF(pid) | Self::PolFF(pid) => { - Some(pid) - } - } + pub const fn pid(&self) -> i32 { + self.pid + } + + /// Return the convolution type of this convolution. + pub fn conv_type(&self) -> ConvType { + self.conv_type } } @@ -376,30 +404,30 @@ mod tests { use super::*; #[test] - fn convolution_charge_conjugate() { + fn conv_cc() { assert_eq!( - Convolution::UnpolPDF(2212).charge_conjugate(), - Convolution::UnpolPDF(-2212) + Conv::new(ConvType::UnpolPDF, 2212).cc(), + Conv::new(ConvType::UnpolPDF, -2212) ); assert_eq!( - Convolution::PolPDF(2212).charge_conjugate(), - Convolution::PolPDF(-2212) + Conv::new(ConvType::PolPDF, 2212).cc(), + Conv::new(ConvType::PolPDF, -2212) ); assert_eq!( - Convolution::UnpolFF(2212).charge_conjugate(), - Convolution::UnpolFF(-2212) + Conv::new(ConvType::UnpolFF, 2212).cc(), + Conv::new(ConvType::UnpolFF, -2212) ); assert_eq!( - Convolution::PolFF(2212).charge_conjugate(), - Convolution::PolFF(-2212) + Conv::new(ConvType::PolFF, 2212).cc(), + Conv::new(ConvType::PolFF, -2212) ); } #[test] - fn convolution_pid() { - assert_eq!(Convolution::UnpolPDF(2212).pid(), Some(2212)); - assert_eq!(Convolution::PolPDF(2212).pid(), Some(2212)); - assert_eq!(Convolution::UnpolFF(2212).pid(), Some(2212)); - assert_eq!(Convolution::PolFF(2212).pid(), Some(2212)); + fn conv_pid() { + assert_eq!(Conv::new(ConvType::UnpolPDF, 2212).pid(), 2212); + assert_eq!(Conv::new(ConvType::PolPDF, 2212).pid(), 2212); + assert_eq!(Conv::new(ConvType::UnpolFF, 2212).pid(), 2212); + assert_eq!(Conv::new(ConvType::PolFF, 2212).pid(), 2212); } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index ada5f6b4..49c44a90 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -2,7 +2,7 @@ use super::bin::{BinInfo, BinLimits, BinRemapper}; use super::boc::{Channel, Kinematics, Order, Scales}; -use super::convolutions::{Convolution, ConvolutionCache}; +use super::convolutions::{Conv, ConvolutionCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; @@ -125,7 +125,7 @@ pub struct Grid { pub(crate) bin_limits: BinLimits, pub(crate) orders: Vec, pub(crate) metadata: BTreeMap, - pub(crate) convolutions: Vec, + pub(crate) convolutions: Vec, pub(crate) pid_basis: PidBasis, pub(crate) more_members: MoreMembers, pub(crate) kinematics: Vec, @@ -148,7 +148,7 @@ impl Grid { channels: Vec, orders: Vec, bin_limits: Vec, - convolutions: Vec, + convolutions: Vec, interps: Vec, kinematics: Vec, scales: Scales, @@ -686,12 +686,12 @@ impl Grid { /// Panics if the metadata key--value pairs `convolution_particle_1` and `convolution_type_1`, /// or `convolution_particle_2` and `convolution_type_2` are not correctly set. #[must_use] - pub fn convolutions(&self) -> &[Convolution] { + pub fn convolutions(&self) -> &[Conv] { &self.convolutions } /// Return the convolution types. - pub fn convolutions_mut(&mut self) -> &mut [Convolution] { + pub fn convolutions_mut(&mut self) -> &mut [Conv] { &mut self.convolutions } @@ -716,7 +716,7 @@ impl Grid { ); } - self.convolutions_mut()[convolution] = self.convolutions()[convolution].charge_conjugate(); + self.convolutions_mut()[convolution] = self.convolutions()[convolution].cc(); } fn increase_shape(&mut self, new_dim: &(usize, usize, usize)) { @@ -1713,6 +1713,7 @@ mod tests { use super::*; use crate::boc::ScaleFuncForm; use crate::channel; + use crate::convolutions::ConvType; use std::fs::File; #[test] @@ -1725,7 +1726,10 @@ mod tests { vec![Channel::new(channel)], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 1.0], - vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + vec![ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212), + ], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1746,7 +1750,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1769,7 +1773,7 @@ mod tests { ], vec![Order::new(1, 2, 0, 0, 0), Order::new(1, 2, 0, 1, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1797,7 +1801,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1823,7 +1827,7 @@ mod tests { Order::new(0, 2, 0, 0, 0), ], vec![0.0, 0.25, 0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1856,7 +1860,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1875,7 +1879,7 @@ mod tests { vec![channel![22, 22, 1.0], channel![2, 2, 1.0; 4, 4, 1.0]], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1905,7 +1909,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.0, 0.25, 0.5], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1928,7 +1932,7 @@ mod tests { ], vec![Order::new(0, 2, 0, 0, 0)], vec![0.5, 0.75, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1963,7 +1967,7 @@ mod tests { logxia: 0, }], vec![0.0, 1.0], - vec![Convolution::UnpolPDF(2212); 2], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], v0::default_interps(2), vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], Scales { @@ -1976,15 +1980,21 @@ mod tests { // by default we assume unpolarized proton PDFs are used assert_eq!( grid.convolutions(), - [Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)] + [ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212) + ] ); - grid.convolutions_mut()[0] = Convolution::UnpolPDF(-2212); - grid.convolutions_mut()[1] = Convolution::UnpolPDF(-2212); + grid.convolutions_mut()[0] = Conv::new(ConvType::UnpolPDF, -2212); + grid.convolutions_mut()[1] = Conv::new(ConvType::UnpolPDF, -2212); assert_eq!( grid.convolutions(), - [Convolution::UnpolPDF(-2212), Convolution::UnpolPDF(-2212)] + [ + Conv::new(ConvType::UnpolPDF, -2212), + Conv::new(ConvType::UnpolPDF, -2212) + ] ); } diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 1bf6e0e4..8c1b5766 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -1,6 +1,6 @@ use super::bin::{BinLimits, BinRemapper}; use super::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; -use super::convolutions::Convolution; +use super::convolutions::{Conv, ConvType}; use super::empty_subgrid::EmptySubgridV1; use super::grid::{Grid, GridError, Mmv4, MoreMembers}; use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; @@ -197,10 +197,10 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result Ok(result) } -fn read_convolutions_from_metadata(grid: &GridV0) -> Vec> { +fn read_convolutions_from_metadata(grid: &GridV0) -> Vec> { grid.key_values().map_or_else( // if there isn't any metadata, we assume two unpolarized proton-PDFs are used - || vec![Some(Convolution::UnpolPDF(2212)); 2], + || vec![Some(Conv::new(ConvType::UnpolPDF, 2212)); 2], |kv| { // file format v0 only supports exactly two convolutions (1..=2) @@ -215,10 +215,10 @@ fn read_convolutions_from_metadata(grid: &GridV0) -> Vec> { .map(String::as_str), ) { (_, Some("None")) => None, - (Some(Ok(pid)), Some("UnpolPDF")) => Some(Convolution::UnpolPDF(pid)), - (Some(Ok(pid)), Some("PolPDF")) => Some(Convolution::PolPDF(pid)), - (Some(Ok(pid)), Some("UnpolFF")) => Some(Convolution::UnpolFF(pid)), - (Some(Ok(pid)), Some("PolFF")) => Some(Convolution::PolFF(pid)), + (Some(Ok(pid)), Some("UnpolPDF")) => Some(Conv::new(ConvType::UnpolPDF, pid)), + (Some(Ok(pid)), Some("PolPDF")) => Some(Conv::new(ConvType::PolPDF, pid)), + (Some(Ok(pid)), Some("UnpolFF")) => Some(Conv::new(ConvType::UnpolFF, pid)), + (Some(Ok(pid)), Some("PolFF")) => Some(Conv::new(ConvType::PolFF, pid)), (None, None) => { // if these key-value pairs are missing use the old metadata match kv @@ -236,9 +236,9 @@ fn read_convolutions_from_metadata(grid: &GridV0) -> Vec> { ) }); - condition.then_some(Convolution::UnpolPDF(pid)) + condition.then_some(Conv::new(ConvType::UnpolPDF, pid)) } - None => Some(Convolution::UnpolPDF(2212)), + None => Some(Conv::new(ConvType::UnpolPDF, 2212)), Some(Err(err)) => panic!( "metadata 'initial_state_{index}' could not be parsed: {err}" ), diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 3b411ebd..059b9a06 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -5,7 +5,7 @@ use num_complex::Complex; use pineappl::bin::BinRemapper; use pineappl::boc::{Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; -use pineappl::convolutions::{Convolution, ConvolutionCache}; +use pineappl::convolutions::{Conv, ConvType, ConvolutionCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::pids::PidBasis; @@ -164,7 +164,10 @@ fn fill_drell_yan_lo_grid( let bin_limits: Vec<_> = (0..=24).map(|x: u32| f64::from(x) / 10.0).collect(); // the grid represents data with two unpolarized proton PDFs - let convolutions = vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)]; + let convolutions = vec![ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212), + ]; let reweight = if reweight { ReweightMeth::ApplGridX @@ -352,7 +355,7 @@ fn perform_grid_tests( // TEST 5: `convolve` let mut convolution_cache = ConvolutionCache::new( - vec![Convolution::UnpolPDF(2212)], + vec![Conv::new(ConvType::UnpolPDF, 2212)], vec![&mut xfx], &mut alphas, ); @@ -367,7 +370,10 @@ fn perform_grid_tests( let mut xfx2 = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas2 = |_| 0.0; let mut convolution_cache2 = ConvolutionCache::new( - vec![Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)], + vec![ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212), + ], vec![&mut xfx1, &mut xfx2], &mut alphas2, ); diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 7343e8b6..86c1f97e 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -58,7 +58,7 @@ use itertools::izip; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; -use pineappl::convolutions::{Convolution, ConvolutionCache}; +use pineappl::convolutions::{Conv, ConvType, ConvolutionCache}; use pineappl::grid::{Grid, GridOptFlags}; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::pids::PidBasis; @@ -456,7 +456,7 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_one( }; let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; let mut convolution_cache = ConvolutionCache::new( - vec![Convolution::UnpolPDF(pdg_id)], + vec![Conv::new(ConvType::UnpolPDF, pdg_id)], vec![&mut xfx], &mut als, ); @@ -523,8 +523,8 @@ pub unsafe extern "C" fn pineappl_grid_convolve_with_two( let results = unsafe { slice::from_raw_parts_mut(results, grid.bin_info().bins()) }; let mut convolution_cache = ConvolutionCache::new( vec![ - Convolution::UnpolPDF(pdg_id1), - Convolution::UnpolPDF(pdg_id2), + Conv::new(ConvType::UnpolPDF, pdg_id1), + Conv::new(ConvType::UnpolPDF, pdg_id2), ], vec![&mut xfx1, &mut xfx2], &mut als, @@ -745,15 +745,17 @@ pub unsafe extern "C" fn pineappl_grid_new( let lumi = unsafe { &*lumi }; - let mut convolutions = vec![Convolution::UnpolPDF(2212); 2]; + let mut convolutions = vec![Conv::new(ConvType::UnpolPDF, 2212); 2]; if let Some(keyval) = key_vals { if let Some(value) = keyval.strings.get("initial_state_1") { - convolutions[0] = Convolution::UnpolPDF(value.to_string_lossy().parse().unwrap()); + convolutions[0] = + Conv::new(ConvType::UnpolPDF, value.to_string_lossy().parse().unwrap()); } if let Some(value) = keyval.strings.get("initial_state_2") { - convolutions[1] = Convolution::UnpolPDF(value.to_string_lossy().parse().unwrap()); + convolutions[1] = + Conv::new(ConvType::UnpolPDF, value.to_string_lossy().parse().unwrap()); } } @@ -955,7 +957,7 @@ pub unsafe extern "C" fn pineappl_grid_key_value( }; if let Some(index) = index { - return CString::new(grid.convolutions()[index].pid().unwrap().to_string()) + return CString::new(grid.convolutions()[index].pid().to_string()) .unwrap() .into_raw(); } @@ -998,7 +1000,7 @@ pub unsafe extern "C" fn pineappl_grid_set_key_value( }; if let Some(index) = index { - grid.convolutions_mut()[index] = Convolution::UnpolPDF(value.parse().unwrap()); + grid.convolutions_mut()[index] = Conv::new(ConvType::UnpolPDF, value.parse().unwrap()); } grid.metadata_mut().insert(key, value); diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index bf29f920..747d5aaa 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -2,7 +2,7 @@ use super::GlobalConfiguration; use anyhow::{anyhow, ensure, Context, Error, Result}; use lhapdf::{Pdf, PdfSet}; use ndarray::{Array3, Ix3}; -use pineappl::convolutions::{Convolution, ConvolutionCache}; +use pineappl::convolutions::{Conv, ConvType, ConvolutionCache}; use pineappl::grid::Grid; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; use prettytable::Table; @@ -292,16 +292,11 @@ pub fn convolve_scales( .unwrap(); match fun.set().entry("SetType").unwrap_or_default().as_str() { - "fragfn" => Convolution::UnpolFF(pid), + "fragfn" => Conv::new(ConvType::UnpolFF, pid), "" => { // if we can not figure out the type of the convolution from the PDF set, we // assume it from the grid convolution at the same index - match convolution { - Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), - Convolution::PolPDF(_) => Convolution::PolPDF(pid), - Convolution::UnpolFF(_) => Convolution::UnpolFF(pid), - Convolution::PolFF(_) => Convolution::PolFF(pid), - } + convolution.with_pid(pid) } // TODO: convince the LHAPDF maintainers to make SetType necessary for polarized // PDFs and all FFs @@ -462,16 +457,11 @@ pub fn convolve_subgrid( .unwrap(); match fun.set().entry("SetType").unwrap_or_default().as_str() { - "fragfn" => Convolution::UnpolFF(pid), + "fragfn" => Conv::new(ConvType::UnpolFF, pid), "" => { // if we can not figure out the type of the convolution from the PDF set, we // assume it from the grid convolution at the same index - match convolution { - Convolution::UnpolPDF(_) => Convolution::UnpolPDF(pid), - Convolution::PolPDF(_) => Convolution::PolPDF(pid), - Convolution::UnpolFF(_) => Convolution::UnpolFF(pid), - Convolution::PolFF(_) => Convolution::PolFF(pid), - } + convolution.with_pid(pid) } // TODO: convince the LHAPDF maintainers to make SetType necessary for polarized // PDFs and all FFs diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index a876775c..b9865226 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -1,7 +1,7 @@ use anyhow::Result; use lhapdf::Pdf; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; -use pineappl::convolutions::Convolution; +use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; @@ -125,7 +125,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { let mut grids = Vec::with_capacity(orders.len()); // from APPLgrid alone we don't know what type of convolution we have - let convolutions = vec![Convolution::UnpolPDF(2212); if grid.isDIS() { 1 } else { 2 }]; + let convolutions = vec![Conv::new(ConvType::UnpolPDF, 2212); if grid.isDIS() { 1 } else { 2 }]; // TODO: read out interpolation parameters from APPLgrid let mut interps = vec![Interp::new( 1e2, diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index e4a219af..6c42322b 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -3,7 +3,7 @@ use anyhow::Result; // use ndarray::s; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; -use pineappl::convolutions::Convolution; +use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; @@ -103,7 +103,7 @@ fn convert_coeff_add_fix( assert!(npdf <= 2); // TODO: extract the proper convolution PIDs - let convolutions = vec![Convolution::UnpolPDF(2212); npdf]; + let convolutions = vec![Conv::new(ConvType::UnpolPDF, 2212); npdf]; let mut grid = Grid::new( PidBasis::Pdg, diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 8b58bc6b..fb8491e6 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -3,7 +3,7 @@ use flate2::read::GzDecoder; use ndarray::s; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; -use pineappl::convolutions::Convolution; +use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; @@ -99,9 +99,9 @@ fn read_fktable(reader: impl BufRead) -> Result { }; let convolutions = if hadronic { - vec![Convolution::UnpolPDF(2212); 2] + vec![Conv::new(ConvType::UnpolPDF, 2212); 2] } else { - vec![Convolution::UnpolPDF(2212)] + vec![Conv::new(ConvType::UnpolPDF, 2212)] }; // construct `Grid` diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 1e0d6d94..9a16882a 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -853,8 +853,8 @@ fn import_hadronic_fktable() { use float_cmp::assert_approx_eq; use lhapdf::Pdf; use ndarray::Array4; - use pineappl::convolutions::Convolution; use pineappl::convolutions::ConvolutionCache; + use pineappl::convolutions::{Conv, ConvType}; use pineappl::fk_table::{FkAssumptions, FkTable}; use pineappl::grid::Grid; use std::fs::File; @@ -893,7 +893,7 @@ fn import_hadronic_fktable() { let mut xfx = |id, x, q2| pdf.xfx_q2(id, x, q2); let mut alphas = |_| 0.0; let mut convolution_cache = ConvolutionCache::new( - vec![Convolution::UnpolPDF(2212)], + vec![Conv::new(ConvType::UnpolPDF, 2212)], vec![&mut xfx], &mut alphas, ); @@ -930,7 +930,10 @@ fn import_hadronic_fktable() { assert_eq!(fk_table.grid().bin_info().right(0), [1.0]); assert_eq!( fk_table.grid().convolutions(), - [Convolution::UnpolPDF(2212), Convolution::UnpolPDF(2212)] + [ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212) + ] ); let channels = fk_table.channels(); assert_eq!( From 870eb26c3cdfe5f1b5c7cf3aacc798220051df49 Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 11 Oct 2024 11:09:10 +0200 Subject: [PATCH 159/213] Remove some traces of `Convolution::None` --- pineappl/src/convolutions.rs | 82 +++++++++++++++++------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 1381593d..10ba3f68 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -23,7 +23,7 @@ pub struct ConvolutionCache<'a> { imua2: Vec, ix: Vec>, pdg: Vec, - perm: Vec>, + perm: Vec<(usize, bool)>, } impl<'a> ConvolutionCache<'a> { @@ -57,24 +57,22 @@ impl<'a> ConvolutionCache<'a> { .iter() .enumerate() .map(|(max_idx, conv)| { - Some( - self.pdg - .iter() - .take(max_idx + 1) - .enumerate() - .rev() - .find_map(|(idx, pdg)| { - if conv == pdg { - Some((idx, false)) - } else if *conv == pdg.cc() { - Some((idx, true)) - } else { - None - } - }) - // TODO: convert `unwrap` to `Err` - .unwrap(), - ) + self.pdg + .iter() + .take(max_idx + 1) + .enumerate() + .rev() + .find_map(|(idx, pdg)| { + if conv == pdg { + Some((idx, false)) + } else if *conv == pdg.cc() { + Some((idx, true)) + } else { + None + } + }) + // TODO: convert `unwrap` to `Err` + .unwrap() }) .collect(); @@ -220,31 +218,29 @@ impl<'a> ConvolutionCache<'a> { .iter() .zip(pdg_ids) .enumerate() - .filter_map(|(index, (perm, &pdg_id))| { - perm.map(|(idx, cc)| { - let ix = self.ix[index][indices[index + 1]]; + .map(|(index, (&(idx, cc), &pdg_id))| { + let ix = self.ix[index][indices[index + 1]]; - let pid = if cc { - pids::charge_conjugate_pdg_pid(pdg_id) - } else { - pdg_id - }; - let xfx = &mut self.xfx[idx]; - let xfx_cache = &mut self.xfx_cache[idx]; - let (imu2, mu2) = match self.pdg[idx].conv_type() { - ConvType::UnpolPDF | ConvType::PolPDF => { - let imuf2 = self.imuf2[indices[0]]; - (imuf2, self.muf2_grid[imuf2]) - } - ConvType::UnpolFF | ConvType::PolFF => { - let imua2 = self.imua2[indices[0]]; - (imua2, self.mua2_grid[imua2]) - } - }; - *xfx_cache.entry((pid, ix, imu2)).or_insert_with(|| { - let x = self.x_grid[ix]; - xfx(pid, x, mu2) / x - }) + let pid = if cc { + pids::charge_conjugate_pdg_pid(pdg_id) + } else { + pdg_id + }; + let xfx = &mut self.xfx[idx]; + let xfx_cache = &mut self.xfx_cache[idx]; + let (imu2, mu2) = match self.pdg[idx].conv_type() { + ConvType::UnpolPDF | ConvType::PolPDF => { + let imuf2 = self.imuf2[indices[0]]; + (imuf2, self.muf2_grid[imuf2]) + } + ConvType::UnpolFF | ConvType::PolFF => { + let imua2 = self.imua2[indices[0]]; + (imua2, self.mua2_grid[imua2]) + } + }; + *xfx_cache.entry((pid, ix, imu2)).or_insert_with(|| { + let x = self.x_grid[ix]; + xfx(pid, x, mu2) / x }) }) .product() From 5067abfc4ee9e6cb131ff00f974f5081ab65c04f Mon Sep 17 00:00:00 2001 From: t7phy Date: Fri, 11 Oct 2024 14:51:08 +0200 Subject: [PATCH 160/213] Add new method `Grid::evolve` --- pineappl/src/evolution.rs | 7 +- pineappl/src/grid.rs | 293 ++++++++++++++----------------------- pineappl_cli/src/evolve.rs | 35 ++--- 3 files changed, 129 insertions(+), 206 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 0283d6f5..a73e9b52 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -1,6 +1,7 @@ //! Supporting classes and functions for [`Grid::evolve_with_slice_iter`]. use super::boc::{Channel, Kinematics, Order}; +use super::convolutions::ConvType; use super::grid::{Grid, GridError}; use super::packed_array::PackedArray; use super::packed_subgrid::PackedQ1X2SubgridV1; @@ -51,13 +52,13 @@ pub struct EvolveInfo { /// [`FkTable::convolve`](super::fk_table::FkTable::convolve) how to perform a convolution. #[derive(Clone)] pub struct OperatorSliceInfo { - /// Squared factorization scale of the `FkTable`. + /// Squared factorization/fragmentation scale of the `FkTable`. pub fac0: f64, /// Particle identifiers of the `FkTable`. pub pids0: Vec, /// `x`-grid coordinates of the `FkTable` pub x0: Vec, - /// Squared factorization scale of the slice of `Grid` that should be evolved. + /// Squared factorization/fragmentation scale of the slice of `Grid` that should be evolved. pub fac1: f64, /// Particle identifiers of the `Grid`. If the `Grid` contains more particle identifiers than /// given here, the contributions of them are silently ignored. @@ -67,6 +68,8 @@ pub struct OperatorSliceInfo { /// Particle ID basis for `FkTable`. pub pid_basis: PidBasis, + /// TODO + pub conv_type: ConvType, } /// A mapping of squared renormalization scales in `ren1` to strong couplings in `alphas`. The diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 49c44a90..0dcf229e 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -2,7 +2,7 @@ use super::bin::{BinInfo, BinLimits, BinRemapper}; use super::boc::{Channel, Kinematics, Order, Scales}; -use super::convolutions::{Conv, ConvolutionCache}; +use super::convolutions::{Conv, ConvType, ConvolutionCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; @@ -1194,7 +1194,8 @@ impl Grid { // - try to find a better solution than to require that E must be convertible into // anyhow::Error - /// Converts this `Grid` into an [`FkTable`] using `slices` that must iterate over a [`Result`] + /// Convert this `Grid` into an [`FkTable`] using `slices.len()` evolution operators, which + /// for each entry must iterate over a [`Result`] /// of tuples of an [`OperatorSliceInfo`] and the corresponding sliced operator. The parameter /// `order_mask` can be used to include or exclude orders from this operation, and must /// correspond to the ordering given by [`Grid::orders`]. Orders that are not given are @@ -1208,13 +1209,17 @@ impl Grid { /// /// # Panics /// - /// Panics when the operator returned by `slices` has different dimensions than promised by the - /// corresponding [`OperatorSliceInfo`]. - pub fn evolve_with_slice_iter<'a, E: Into>( + /// Panics when the operators returned by either slice have different dimensions + /// than promised by the corresponding [`OperatorSliceInfo`]. + pub fn evolve< + 'a, + E: Into, + S: IntoIterator), E>>, + >( &self, - slices: impl IntoIterator), E>>, + slices: Vec, order_mask: &[bool], - xi: (f64, f64), + xi: (f64, f64, f64), alphas_table: &AlphasTable, ) -> Result { use super::evolution::EVOLVE_INFO_TOL_ULPS; @@ -1229,166 +1234,118 @@ impl Grid { .evolve_info(order_mask) .fac1 .into_iter() + // TODO: also take care of the fragmentation scale .map(|fac| xi.1 * xi.1 * fac) .collect(); - for result in slices { - let (info, operator) = result.map_err(|err| GridError::Other(err.into()))?; - - op_fac1.push(info.fac1); - - // it's possible that due to small numerical differences we get two slices which are - // almost the same. We have to skip those in order not to evolve the 'same' slice twice - if used_op_fac1 - .iter() - .any(|&fac| approx_eq!(f64, fac, info.fac1, ulps = EVOLVE_INFO_TOL_ULPS)) - { - continue; - } - - // skip slices that the grid doesn't use - if !grid_fac1 - .iter() - .any(|&fac| approx_eq!(f64, fac, info.fac1, ulps = EVOLVE_INFO_TOL_ULPS)) - { - continue; - } + let mut perm = Vec::new(); + let mut eko_conv_types: Vec; - let op_info_dim = ( - info.pids1.len(), - info.x1.len(), - info.pids0.len(), - info.x0.len(), - ); - - assert_eq!( - operator.dim(), - op_info_dim, - "operator information {:?} does not match the operator's dimensions: {:?}", - op_info_dim, - operator.dim(), - ); + struct Iter { + iters: Vec, + } - let views = vec![operator.view(); self.convolutions().len()]; - let infos = vec![info.clone(); self.convolutions().len()]; - let (subgrids, channels) = evolution::evolve_slice_with_many( - self, - &views, - &infos, - order_mask, - (xi.0, xi.1, 1.0), - alphas_table, - )?; + impl Iterator for Iter { + type Item = Vec; - let rhs = Self { - subgrids, - channels, - bin_limits: self.bin_limits.clone(), - orders: vec![Order::new(0, 0, 0, 0, 0)], - interps: self.interps.clone(), - metadata: self.metadata.clone(), - convolutions: self.convolutions.clone(), - pid_basis: info.pid_basis, - more_members: self.more_members.clone(), - kinematics: self.kinematics.clone(), - remapper: self.remapper.clone(), - // TODO: is this correct? - scales: self.scales.clone(), - }; + fn next(&mut self) -> Option { + let v: Vec<_> = self.iters.iter_mut().filter_map(Iterator::next).collect(); - if let Some(lhs) = &mut lhs { - lhs.merge(rhs)?; - } else { - lhs = Some(rhs); + if v.len() == self.iters.len() { + Some(v) + } else { + None + } } - - used_op_fac1.push(info.fac1); } - // UNWRAP: if we can't compare two numbers there's a bug - op_fac1.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); - - // make sure we've evolved all slices - if let Some(muf2) = grid_fac1.into_iter().find(|&grid_mu2| { - !used_op_fac1 - .iter() - .any(|&eko_mu2| approx_eq!(f64, grid_mu2, eko_mu2, ulps = EVOLVE_INFO_TOL_ULPS)) - }) { - return Err(GridError::EvolutionFailure(format!( - "no operator for muf2 = {muf2} found in {op_fac1:?}" - ))); + fn zip_n(iters: O) -> impl Iterator> + where + O: IntoIterator, + T: IntoIterator, + { + Iter { + iters: iters.into_iter().map(IntoIterator::into_iter).collect(), + } } - // TODO: convert this unwrap into error - let grid = lhs.unwrap(); - - // UNWRAP: merging evolved slices should be a proper FkTable again - Ok(FkTable::try_from(grid).unwrap_or_else(|_| unreachable!())) - } - - /// Converts this `Grid` into an [`FkTable`] using `slices` that must iterate over a [`Result`] - /// of tuples of an [`OperatorSliceInfo`] and the corresponding sliced operator. The parameter - /// `order_mask` can be used to include or exclude orders from this operation, and must - /// correspond to the ordering given by [`Grid::orders`]. Orders that are not given are - /// enabled, and in particular if `order_mask` is empty all orders are activated. - /// - /// # Errors - /// - /// Returns a [`GridError::EvolutionFailure`] if either the `operator` or its `info` is - /// incompatible with this `Grid`. Returns a [`GridError::Other`] if the iterator from `slices` - /// return an error. - /// - /// # Panics - /// - /// Panics when the operators returned by `slices_a` and `slices_b` have different dimensions - /// than promised by the corresponding [`OperatorSliceInfo`]. - pub fn evolve_with_slice_iter2<'a, E: Into>( - &self, - slices_a: impl IntoIterator), E>>, - slices_b: impl IntoIterator), E>>, - order_mask: &[bool], - xi: (f64, f64), - alphas_table: &AlphasTable, - ) -> Result { - use super::evolution::EVOLVE_INFO_TOL_ULPS; - use itertools::izip; - - // TODO: generalize this function for an arbitrary number of convolutions - assert_eq!(self.convolutions().len(), 2); - - let mut lhs: Option = None; - // Q2 slices we use - let mut used_op_fac1 = Vec::new(); - // Q2 slices we encounter, but possibly don't use - let mut op_fac1 = Vec::new(); - // Q2 slices needed by the grid - let grid_fac1: Vec<_> = self - .evolve_info(order_mask) - .fac1 - .into_iter() - .map(|fac| xi.1 * xi.1 * fac) - .collect(); - - // TODO: simplify the ugly repetition below by offloading some ops into fn - for (result_a, result_b) in izip!(slices_a, slices_b) { - // Operate on `slices_a` - let (info_a, operator_a) = result_a.map_err(|err| GridError::Other(err.into()))?; - // Operate on `slices_b` - let (info_b, operator_b) = result_b.map_err(|err| GridError::Other(err.into()))?; + for result in zip_n(slices) { + let (infos, operators): (Vec, Vec>) = result + .into_iter() + .map(|res| res.map_err(|err| GridError::Other(err.into()))) + .collect::>()?; // TODO: what if the scales of the EKOs don't agree? Is there an ordering problem? - assert_approx_eq!(f64, info_a.fac1, info_b.fac1, ulps = EVOLVE_INFO_TOL_ULPS); - // also the PID bases must be the same - assert_eq!(info_a.pid_basis, info_b.pid_basis); + let (info_0, infos_rest) = infos + .split_first() + // UNWRAP: TODO + .unwrap(); + let dim_op_info_0 = ( + info_0.pids1.len(), + info_0.x1.len(), + info_0.pids0.len(), + info_0.x0.len(), + ); + assert_eq!( + operators[0].dim(), + dim_op_info_0, + "operator information {:?} does not match the operator's dimensions: {:?}", + dim_op_info_0, + operators[0].dim(), + ); + for (index, info) in infos_rest.iter().enumerate() { + assert_approx_eq!(f64, info_0.fac1, info.fac1, ulps = EVOLVE_INFO_TOL_ULPS); + assert_eq!(info_0.pid_basis, info.pid_basis); + + let dim_op_info = ( + info.pids1.len(), + info.x1.len(), + info.pids0.len(), + info.x0.len(), + ); + + assert_eq!( + operators[index + 1].dim(), + dim_op_info, + "operator information {:?} does not match the operator's dimensions: {:?}", + dim_op_info, + operators[index + 1].dim(), + ); + } + + if perm.is_empty() { + eko_conv_types = infos.iter().map(|info| info.conv_type).collect(); + perm = self + .convolutions() + .iter() + .enumerate() + .map(|(max_idx, conv)| { + eko_conv_types + .iter() + .take(max_idx + 1) + .enumerate() + .rev() + .find_map(|(idx, &eko_conv_type)| { + if conv.conv_type() == eko_conv_type { + Some(idx) + } else { + None + } + }) + // TODO: convert `unwrap` to `Err` + .unwrap() + }) + .collect(); + } - op_fac1.push(info_a.fac1); + op_fac1.push(info_0.fac1); // it's possible that due to small numerical differences we get two slices which are // almost the same. We have to skip those in order not to evolve the 'same' slice twice if used_op_fac1 .iter() - .any(|&fac| approx_eq!(f64, fac, info_a.fac1, ulps = EVOLVE_INFO_TOL_ULPS)) + .any(|&fac| approx_eq!(f64, fac, info_0.fac1, ulps = EVOLVE_INFO_TOL_ULPS)) { continue; } @@ -1396,50 +1353,20 @@ impl Grid { // skip slices that the grid doesn't use if !grid_fac1 .iter() - .any(|&fac| approx_eq!(f64, fac, info_a.fac1, ulps = EVOLVE_INFO_TOL_ULPS)) + .any(|&fac| approx_eq!(f64, fac, info_0.fac1, ulps = EVOLVE_INFO_TOL_ULPS)) { continue; } - let op_info_dim_a = ( - info_a.pids1.len(), - info_a.x1.len(), - info_a.pids0.len(), - info_a.x0.len(), - ); - - assert_eq!( - operator_a.dim(), - op_info_dim_a, - "operator information {:?} does not match the operator's dimensions: {:?}", - op_info_dim_a, - operator_a.dim(), - ); - - let op_info_dim_b = ( - info_b.pids1.len(), - info_b.x1.len(), - info_b.pids0.len(), - info_b.x0.len(), - ); - - assert_eq!( - operator_b.dim(), - op_info_dim_b, - "operator information {:?} does not match the operator's dimensions: {:?}", - op_info_dim_b, - operator_b.dim(), - ); - - let views = [operator_a.view(), operator_b.view()]; - let infos = [info_a, info_b]; + let operators: Vec<_> = perm.iter().map(|&idx| operators[idx].view()).collect(); + let infos: Vec<_> = perm.iter().map(|&idx| infos[idx].clone()).collect(); let (subgrids, channels) = evolution::evolve_slice_with_many( self, - &views, + &operators, &infos, order_mask, - (xi.0, xi.1, 1.0), + xi, alphas_table, )?; @@ -1459,15 +1386,13 @@ impl Grid { scales: self.scales.clone(), }; - assert_eq!(infos[0].pid_basis, infos[1].pid_basis); - if let Some(lhs) = &mut lhs { lhs.merge(rhs)?; } else { lhs = Some(rhs); } - used_op_fac1.push(infos[0].fac1); + used_op_fac1.push(info_0.fac1); } // UNWRAP: if we can't compare two numbers there's a bug diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index bdaf04e2..6047657e 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -20,6 +20,7 @@ mod eko { use ndarray::iter::AxisIter; use ndarray::{Array4, Array5, Axis, CowArray, Ix4}; use ndarray_npy::{NpzReader, ReadNpyExt}; + use pineappl::convolutions::ConvType; use pineappl::evolution::OperatorSliceInfo; use pineappl::pids::{self, PidBasis}; use serde::Deserialize; @@ -75,17 +76,14 @@ mod eko { #[derive(Deserialize)] struct OperatorConfigsV1 { - #[serde(rename = "polarized")] - _polarized: bool, - #[serde(rename = "time_like")] - _time_like: bool, + polarized: bool, + time_like: bool, } #[derive(Deserialize)] struct OperatorV1 { mu0: f64, - #[serde(rename = "configs")] - _configs: OperatorConfigsV1, + configs: OperatorConfigsV1, } #[derive(Deserialize)] @@ -172,6 +170,7 @@ mod eko { fac1: 0.0, pids1: metadata.targetpids, x1: metadata.targetgrid, + conv_type: ConvType::UnpolPDF, }, operator, }) @@ -250,6 +249,7 @@ mod eko { .rotations .targetgrid .unwrap_or(metadata.rotations.xgrid), + conv_type: ConvType::UnpolPDF, }, archive: Archive::new(File::open(eko_path)?), }) @@ -316,6 +316,10 @@ mod eko { .bases .targetgrid .unwrap_or_else(|| metadata.bases.xgrid.clone()), + conv_type: ConvType::new( + operator.configs.polarized, + operator.configs.time_like, + ), }, archive: Archive::new(File::open(eko_path)?), }) @@ -439,6 +443,7 @@ fn evolve_grid( orders: &[(u32, u32)], xir: f64, xif: f64, + xia: f64, ) -> Result { use eko::EkoSlices; use pineappl::evolution::AlphasTable; @@ -458,22 +463,10 @@ fn evolve_grid( .iter() .map(|eko| EkoSlices::new(eko)) .collect::>()?; + let eko_slices: Vec<_> = eko_slices.iter_mut().collect(); let alphas_table = AlphasTable::from_grid(grid, xir, &|q2| use_alphas_from.alphas_q2(q2)); - match eko_slices.as_mut_slice() { - [eko] => Ok(grid.evolve_with_slice_iter(eko, &order_mask, (xir, xif), &alphas_table)?), - [eko_a, eko_b] => Ok(grid.evolve_with_slice_iter2( - eko_a, - eko_b, - &order_mask, - (xir, xif), - &alphas_table, - )?), - _ => unimplemented!( - "evolution with {} EKOs is not implemented", - eko_slices.len() - ), - } + Ok(grid.evolve(eko_slices, &order_mask, (xir, xif, xia), &alphas_table)?) } #[cfg(not(feature = "evolve"))] @@ -484,6 +477,7 @@ fn evolve_grid( _: &[(u32, u32)], _: f64, _: f64, + _: f64, ) -> Result { Err(anyhow!( "you need to install `pineappl` with feature `evolve`" @@ -563,6 +557,7 @@ impl Subcommand for Opts { &self.orders, self.xir, self.xif, + self.xia, )?; let evolved_results = helpers::convolve_scales( From a401ca0650e20177cbab1f2ea496b13724b462a5 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sat, 12 Oct 2024 10:29:50 +0200 Subject: [PATCH 161/213] Reformat `Grid::evolve` --- pineappl/src/grid.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 0dcf229e..2df509a2 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -1194,12 +1194,12 @@ impl Grid { // - try to find a better solution than to require that E must be convertible into // anyhow::Error - /// Convert this `Grid` into an [`FkTable`] using `slices.len()` evolution operators, which - /// for each entry must iterate over a [`Result`] - /// of tuples of an [`OperatorSliceInfo`] and the corresponding sliced operator. The parameter - /// `order_mask` can be used to include or exclude orders from this operation, and must - /// correspond to the ordering given by [`Grid::orders`]. Orders that are not given are - /// enabled, and in particular if `order_mask` is empty all orders are activated. + /// Convert this `Grid` into an [`FkTable`] using `slices.len()` evolution operators, which for + /// each entry must iterate over a [`Result`] of tuples of an [`OperatorSliceInfo`] and the + /// corresponding sliced operator. The parameter `order_mask` can be used to include or exclude + /// orders from this operation, and must correspond to the ordering given by [`Grid::orders`]. + /// Orders that are not given are enabled, and in particular if `order_mask` is empty all + /// orders are activated. /// /// # Errors /// @@ -1209,8 +1209,8 @@ impl Grid { /// /// # Panics /// - /// Panics when the operators returned by either slice have different dimensions - /// than promised by the corresponding [`OperatorSliceInfo`]. + /// Panics when the operators returned by either slice have different dimensions than promised + /// by the corresponding [`OperatorSliceInfo`]. pub fn evolve< 'a, E: Into, @@ -1239,7 +1239,6 @@ impl Grid { .collect(); let mut perm = Vec::new(); - let mut eko_conv_types: Vec; struct Iter { iters: Vec, @@ -1275,18 +1274,18 @@ impl Grid { .map(|res| res.map_err(|err| GridError::Other(err.into()))) .collect::>()?; - // TODO: what if the scales of the EKOs don't agree? Is there an ordering problem? - let (info_0, infos_rest) = infos .split_first() // UNWRAP: TODO .unwrap(); + let dim_op_info_0 = ( info_0.pids1.len(), info_0.x1.len(), info_0.pids0.len(), info_0.x0.len(), ); + assert_eq!( operators[0].dim(), dim_op_info_0, @@ -1294,7 +1293,9 @@ impl Grid { dim_op_info_0, operators[0].dim(), ); + for (index, info) in infos_rest.iter().enumerate() { + // TODO: what if the scales of the EKOs don't agree? Is there an ordering problem? assert_approx_eq!(f64, info_0.fac1, info.fac1, ulps = EVOLVE_INFO_TOL_ULPS); assert_eq!(info_0.pid_basis, info.pid_basis); @@ -1315,7 +1316,9 @@ impl Grid { } if perm.is_empty() { - eko_conv_types = infos.iter().map(|info| info.conv_type).collect(); + let eko_conv_types: Vec = + infos.iter().map(|info| info.conv_type).collect(); + perm = self .convolutions() .iter() From aeaefd9f763d16512eeefc0578c5d6068c6e1fd6 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sat, 12 Oct 2024 10:30:17 +0200 Subject: [PATCH 162/213] Allow missing docs for tests --- pineappl/tests/drell_yan_lo.rs | 2 ++ pineappl_cli/tests/analyze.rs | 2 ++ pineappl_cli/tests/channels.rs | 2 ++ pineappl_cli/tests/convolve.rs | 2 ++ pineappl_cli/tests/diff.rs | 2 ++ pineappl_cli/tests/evolve.rs | 1 + pineappl_cli/tests/export.rs | 2 ++ pineappl_cli/tests/help.rs | 2 ++ pineappl_cli/tests/import.rs | 2 ++ pineappl_cli/tests/main.rs | 2 ++ pineappl_cli/tests/merge.rs | 2 ++ pineappl_cli/tests/orders.rs | 2 ++ pineappl_cli/tests/plot.rs | 2 ++ pineappl_cli/tests/pull.rs | 2 ++ pineappl_cli/tests/read.rs | 2 ++ pineappl_cli/tests/subgrids.rs | 2 ++ pineappl_cli/tests/uncert.rs | 2 ++ pineappl_cli/tests/write.rs | 2 ++ 18 files changed, 35 insertions(+) diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 059b9a06..9253c0ba 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use anyhow::Result; use float_cmp::assert_approx_eq; use lhapdf::Pdf; diff --git a/pineappl_cli/tests/analyze.rs b/pineappl_cli/tests/analyze.rs index 2b2f71fa..c8a225c7 100644 --- a/pineappl_cli/tests/analyze.rs +++ b/pineappl_cli/tests/analyze.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Perform various analyses with grids diff --git a/pineappl_cli/tests/channels.rs b/pineappl_cli/tests/channels.rs index a7556af1..2bf018d9 100644 --- a/pineappl_cli/tests/channels.rs +++ b/pineappl_cli/tests/channels.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Shows the contribution for each partonic channel diff --git a/pineappl_cli/tests/convolve.rs b/pineappl_cli/tests/convolve.rs index f7130c13..54be75c0 100644 --- a/pineappl_cli/tests/convolve.rs +++ b/pineappl_cli/tests/convolve.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use predicates::str; diff --git a/pineappl_cli/tests/diff.rs b/pineappl_cli/tests/diff.rs index 6b3c59ab..ef7d810d 100644 --- a/pineappl_cli/tests/diff.rs +++ b/pineappl_cli/tests/diff.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use assert_fs::NamedTempFile; diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index e6b57616..7e862ada 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -1,3 +1,4 @@ +#![allow(missing_docs)] #![cfg(feature = "evolve")] use assert_cmd::Command; diff --git a/pineappl_cli/tests/export.rs b/pineappl_cli/tests/export.rs index 80a1cdab..872c8f3a 100644 --- a/pineappl_cli/tests/export.rs +++ b/pineappl_cli/tests/export.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; #[cfg(feature = "applgrid")] diff --git a/pineappl_cli/tests/help.rs b/pineappl_cli/tests/help.rs index f620d84e..a6b3ada0 100644 --- a/pineappl_cli/tests/help.rs +++ b/pineappl_cli/tests/help.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Display a manpage for selected subcommands diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 9a16882a..c98e4b65 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; #[cfg(any(feature = "applgrid", feature = "fastnlo", feature = "fktable"))] diff --git a/pineappl_cli/tests/main.rs b/pineappl_cli/tests/main.rs index 0ba04dce..8f6b2e34 100644 --- a/pineappl_cli/tests/main.rs +++ b/pineappl_cli/tests/main.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Read, write, and query PineAPPL grids diff --git a/pineappl_cli/tests/merge.rs b/pineappl_cli/tests/merge.rs index ae57dc5a..30fb4a30 100644 --- a/pineappl_cli/tests/merge.rs +++ b/pineappl_cli/tests/merge.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use assert_fs::NamedTempFile; diff --git a/pineappl_cli/tests/orders.rs b/pineappl_cli/tests/orders.rs index f6321064..bb0336e4 100644 --- a/pineappl_cli/tests/orders.rs +++ b/pineappl_cli/tests/orders.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Shows the predictions for all bin for each order separately diff --git a/pineappl_cli/tests/plot.rs b/pineappl_cli/tests/plot.rs index 57894a72..d689fb43 100644 --- a/pineappl_cli/tests/plot.rs +++ b/pineappl_cli/tests/plot.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use predicates::str; use std::num::NonZeroUsize; diff --git a/pineappl_cli/tests/pull.rs b/pineappl_cli/tests/pull.rs index 6b6f6e86..9891cfda 100644 --- a/pineappl_cli/tests/pull.rs +++ b/pineappl_cli/tests/pull.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use std::num::NonZeroUsize; use std::thread; diff --git a/pineappl_cli/tests/read.rs b/pineappl_cli/tests/read.rs index 1d9abe08..2e9120a3 100644 --- a/pineappl_cli/tests/read.rs +++ b/pineappl_cli/tests/read.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Read out information of a grid diff --git a/pineappl_cli/tests/subgrids.rs b/pineappl_cli/tests/subgrids.rs index 988072cf..f9a2b5b9 100644 --- a/pineappl_cli/tests/subgrids.rs +++ b/pineappl_cli/tests/subgrids.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; const HELP_STR: &str = "Print information about the internal subgrid types diff --git a/pineappl_cli/tests/uncert.rs b/pineappl_cli/tests/uncert.rs index 6a1dae44..a2923f5d 100644 --- a/pineappl_cli/tests/uncert.rs +++ b/pineappl_cli/tests/uncert.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use std::num::NonZeroUsize; use std::thread; diff --git a/pineappl_cli/tests/write.rs b/pineappl_cli/tests/write.rs index 7d4bdd53..eacb08f6 100644 --- a/pineappl_cli/tests/write.rs +++ b/pineappl_cli/tests/write.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use assert_cmd::Command; use assert_fs::{fixture::FileWriteStr, NamedTempFile}; From f09956d98dfc8e166b80efc2810198f95f3d03c3 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sat, 12 Oct 2024 11:04:36 +0200 Subject: [PATCH 163/213] Fix a few clippy warnings --- pineappl/src/boc.rs | 7 +-- pineappl/src/convolutions.rs | 14 +++--- pineappl/src/empty_subgrid.rs | 2 +- pineappl/src/grid.rs | 68 +++++++++++++---------------- pineappl/src/lagrange_subgrid.rs | 4 +- pineappl/src/packed_array.rs | 16 +++++-- pineappl/src/packed_subgrid.rs | 6 +-- pineappl/src/subgrid.rs | 16 ++++--- pineappl/src/v0.rs | 2 +- pineappl/tests/drell_yan_lo.rs | 21 +++------ pineappl_cli/src/export/applgrid.rs | 31 ++++++------- pineappl_cli/src/import/applgrid.rs | 2 +- 12 files changed, 96 insertions(+), 93 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 52e0ead7..1d5e8e73 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -41,10 +41,11 @@ pub enum ScaleFuncForm { impl ScaleFuncForm { /// TODO + #[must_use] pub fn calc(&self, node_values: &[NodeValues], kinematics: &[Kinematics]) -> Option> { match self { - ScaleFuncForm::NoScale => None, - &ScaleFuncForm::Scale(index) => Some(if node_values.is_empty() { + Self::NoScale => None, + &Self::Scale(index) => Some(if node_values.is_empty() { // TODO: empty subgrid should have as many node values as dimensions Vec::new() } else { @@ -55,7 +56,7 @@ impl ScaleFuncForm { .unwrap()] .values() }), - ScaleFuncForm::QuadraticSum(_, _) => todo!(), + Self::QuadraticSum(_, _) => todo!(), } } } diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 10ba3f68..1b1f6810 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -313,7 +313,7 @@ impl<'a> ConvolutionCache<'a> { matches!(kin, &Kinematics::X(index) if index == idx).then_some(node_values) }) // UNWRAP: guaranteed by the grid constructor - .unwrap() + .unwrap_or_else(|| unreachable!()) .values() .iter() .map(|xd| { @@ -343,7 +343,8 @@ pub enum ConvType { impl ConvType { /// TODO - pub fn new(polarized: bool, time_like: bool) -> Self { + #[must_use] + pub const fn new(polarized: bool, time_like: bool) -> Self { match (polarized, time_like) { (false, false) => Self::UnpolPDF, (false, true) => Self::UnpolFF, @@ -362,12 +363,14 @@ pub struct Conv { impl Conv { /// Constructor. - pub fn new(conv_type: ConvType, pid: i32) -> Self { + #[must_use] + pub const fn new(conv_type: ConvType, pid: i32) -> Self { Self { conv_type, pid } } /// TODO - pub fn with_pid(&self, pid: i32) -> Self { + #[must_use] + pub const fn with_pid(&self, pid: i32) -> Self { Self { conv_type: self.conv_type, pid, @@ -390,7 +393,8 @@ impl Conv { } /// Return the convolution type of this convolution. - pub fn conv_type(&self) -> ConvType { + #[must_use] + pub const fn conv_type(&self) -> ConvType { self.conv_type } } diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 0238d262..5f2acfea 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -61,7 +61,7 @@ mod tests { fn create_empty() { let mut subgrid = EmptySubgridV1; assert!(subgrid.is_empty()); - subgrid.merge(&mut EmptySubgridV1.into(), None); + subgrid.merge(&EmptySubgridV1.into(), None); subgrid.scale(2.0); subgrid.symmetrize(1, 2); assert_eq!( diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 2df509a2..599b6ec0 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -10,7 +10,7 @@ use super::interpolation::Interp; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::{self, PidBasis}; -use super::subgrid::{Subgrid, SubgridEnum}; +use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use super::v0; use bitflags::bitflags; use float_cmp::{approx_eq, assert_approx_eq}; @@ -206,12 +206,14 @@ impl Grid { } /// TODO + #[must_use] pub fn kinematics(&self) -> &[Kinematics] { &self.kinematics } /// TODO - pub fn scales(&self) -> &Scales { + #[must_use] + pub const fn scales(&self) -> &Scales { &self.scales } @@ -232,7 +234,7 @@ impl Grid { channel_mask: &[bool], xi: &[(f64, f64, f64)], ) -> Vec { - convolution_cache.setup(self, &xi).unwrap(); + convolution_cache.setup(self, xi).unwrap(); let bin_indices = if bin_indices.is_empty() { (0..self.bin_info().bins()).collect() @@ -351,19 +353,12 @@ impl Grid { let node_values: Vec<_> = subgrid .node_values() .iter() - .map(|node_values| node_values.values()) + .map(NodeValues::values) .collect(); // TODO: generalize this to N dimensions assert_eq!(node_values.len(), 3); - convolution_cache.set_grids( - self, - &subgrid.node_values(), - &node_values[0].iter().copied().collect::>(), - xir, - xif, - xia, - ); + convolution_cache.set_grids(self, &subgrid.node_values(), &node_values[0], xir, xif, xia); let dim: Vec<_> = node_values.iter().map(Vec::len).collect(); let mut array = ArrayD::zeros(dim); @@ -672,7 +667,7 @@ impl Grid { if self.subgrids[[self_i, self_j, self_k]].is_empty() { mem::swap(&mut self.subgrids[[self_i, self_j, self_k]], subgrid); } else { - self.subgrids[[self_i, self_j, self_k]].merge(&mut *subgrid, None); + self.subgrids[[self_i, self_j, self_k]].merge(subgrid, None); } } @@ -1022,14 +1017,13 @@ impl Grid { .iter() .enumerate() .tuple_combinations() - .filter_map(|((idx_a, conv_a), (idx_b, conv_b))| { - (conv_a == conv_b).then(|| (idx_a, idx_b)) - }) + .filter(|((_, conv_a), (_, conv_b))| conv_a == conv_b) + .map(|((idx_a, _), (idx_b, _))| (idx_a, idx_b)) .collect(); - let (idx_a, idx_b) = match pairs.as_slice() { - &[] => return, - &[pair] => pair, + let (idx_a, idx_b) = match *pairs.as_slice() { + [] => return, + [pair] => pair, _ => panic!("more than two equal convolutions found"), }; let a_subgrid = self @@ -1222,24 +1216,6 @@ impl Grid { xi: (f64, f64, f64), alphas_table: &AlphasTable, ) -> Result { - use super::evolution::EVOLVE_INFO_TOL_ULPS; - - let mut lhs: Option = None; - // Q2 slices we use - let mut used_op_fac1 = Vec::new(); - // Q2 slices we encounter, but possibly don't use - let mut op_fac1 = Vec::new(); - // Q2 slices needed by the grid - let grid_fac1: Vec<_> = self - .evolve_info(order_mask) - .fac1 - .into_iter() - // TODO: also take care of the fragmentation scale - .map(|fac| xi.1 * xi.1 * fac) - .collect(); - - let mut perm = Vec::new(); - struct Iter { iters: Vec, } @@ -1268,6 +1244,24 @@ impl Grid { } } + use super::evolution::EVOLVE_INFO_TOL_ULPS; + + let mut lhs: Option = None; + // Q2 slices we use + let mut used_op_fac1 = Vec::new(); + // Q2 slices we encounter, but possibly don't use + let mut op_fac1 = Vec::new(); + // Q2 slices needed by the grid + let grid_fac1: Vec<_> = self + .evolve_info(order_mask) + .fac1 + .into_iter() + // TODO: also take care of the fragmentation scale + .map(|fac| xi.1 * xi.1 * fac) + .collect(); + + let mut perm = Vec::new(); + for result in zip_n(slices) { let (infos, operators): (Vec, Vec>) = result .into_iter() diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/lagrange_subgrid.rs index 3abbfeac..b4c90b1b 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/lagrange_subgrid.rs @@ -31,7 +31,7 @@ impl Subgrid for LagrangeSubgridV2 { fn fill(&mut self, interps: &[Interp], ntuple: &[f64], weight: f64) { debug_assert_eq!(interps.len(), ntuple.len()); - if interpolation::interpolate(interps, &ntuple, weight, &mut self.array) { + if interpolation::interpolate(interps, ntuple, weight, &mut self.array) { // TODO: make this more general let q2 = ntuple[0]; if self.static_q2 == 0.0 { @@ -45,7 +45,7 @@ impl Subgrid for LagrangeSubgridV2 { fn node_values(&self) -> Vec { self.interps .iter() - .map(|interp| NodeValues::UseThese(interp.node_values().to_vec())) + .map(|interp| NodeValues::UseThese(interp.node_values())) .collect() } diff --git a/pineappl/src/packed_array.rs b/pineappl/src/packed_array.rs index 7b6ea33d..fbd20c68 100644 --- a/pineappl/src/packed_array.rs +++ b/pineappl/src/packed_array.rs @@ -26,7 +26,7 @@ pub struct PackedArray { impl PackedArray { /// Constructs a new and empty `PackedArray` of shape `shape`. #[must_use] - pub fn new(shape: Vec) -> Self { + pub const fn new(shape: Vec) -> Self { Self { entries: vec![], start_indices: vec![], @@ -90,8 +90,13 @@ impl PackedArray { .map(|(indices, entry)| (indices, *entry)) } + /// TODO + /// + /// # Panics + /// /// TODO // TODO: rewrite this method into `sub_block_iter_mut() -> impl Iterator` + #[must_use] pub fn sub_block_idx( &self, start_index: &[usize], @@ -170,6 +175,11 @@ fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize { } /// TODO +/// +/// # Panics +/// +/// TODO +#[must_use] pub fn unravel_index(mut index: usize, shape: &[usize]) -> Vec { assert!(index < shape.iter().product()); let mut indices = vec![0; shape.len()]; @@ -200,7 +210,7 @@ impl Index<&[usize]> for PackedArray { self.shape ); - let raveled_index = ravel_multi_index(&index, &self.shape); + let raveled_index = ravel_multi_index(index, &self.shape); let point = self.start_indices.partition_point(|&i| i <= raveled_index); assert!( @@ -399,7 +409,7 @@ impl IndexMut<&[usize]> for PackedArray we insert the element as a new group - let raveled_index = ravel_multi_index(&index, &self.shape); + let raveled_index = ravel_multi_index(index, &self.shape); // To determine which groups the new element is close to, `point` is the index of the // start_index of the first group after the new element. `point` is 0 if no elements before diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/packed_subgrid.rs index 1a0a4acb..0b317bb9 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/packed_subgrid.rs @@ -59,7 +59,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } self.array = array; - self.node_values = new_node_values.clone(); + self.node_values.clone_from(&new_node_values); } for (mut indices, value) in other.indexed_iter() { @@ -241,7 +241,7 @@ mod tests { vec![ NodeValues::UseThese(vec![1.0]), NodeValues::UseThese(x.clone()), - NodeValues::UseThese(x.clone()), + NodeValues::UseThese(x), ], ) .into(); @@ -257,7 +257,7 @@ mod tests { assert_eq!(grid2.indexed_iter().nth(2), Some((vec![0, 3, 1], 2.0))); assert_eq!(grid2.indexed_iter().nth(3), Some((vec![0, 3, 4], 4.0))); - grid1.merge(&mut grid2, None); + grid1.merge(&grid2, None); // the luminosity function is symmetric, so after symmetrization the result must be // unchanged diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index da06711d..0b5c3481 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -24,7 +24,7 @@ impl NodeValues { pub fn extend(&mut self, other: &Self) { match (self, other) { // (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (), - (NodeValues::UseThese(a), NodeValues::UseThese(b)) => { + (Self::UseThese(a), Self::UseThese(b)) => { a.extend_from_slice(b); a.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap()); // TODO: use some tolerance @@ -34,36 +34,40 @@ impl NodeValues { } /// TODO + #[must_use] pub fn len(&self) -> usize { match self { // NodeValues::UseFromGrid => unimplemented!(), - NodeValues::UseThese(a) => a.len(), + Self::UseThese(a) => a.len(), } } /// TODO + #[must_use] pub fn find(&self, value: f64) -> Option { match self { // NodeValues::UseFromGrid => unimplemented!(), - NodeValues::UseThese(a) => a.iter().position(|&x| + Self::UseThese(a) => a.iter().position(|&x| // approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS) x == value), } } /// TODO + #[must_use] pub fn get(&self, index: usize) -> f64 { match self { // NodeValues::UseFromGrid => unimplemented!(), - NodeValues::UseThese(a) => a[index], + Self::UseThese(a) => a[index], } } /// TODO + #[must_use] pub fn values(&self) -> Vec { match self { // NodeValues::UseFromGrid => unimplemented!(), - NodeValues::UseThese(a) => a.clone(), + Self::UseThese(a) => a.clone(), } } } @@ -72,7 +76,7 @@ impl PartialEq for NodeValues { fn eq(&self, other: &Self) -> bool { match (self, other) { // (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, - (NodeValues::UseThese(a), NodeValues::UseThese(b)) => { + (Self::UseThese(a), Self::UseThese(b)) => { (a.len() == b.len()) && a.iter().zip(b).all( // TODO: use some tolerance diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 8c1b5766..47ca1a7c 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -165,7 +165,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result convolutions: convolutions .into_iter() //.map(|conv| conv.unwrap_or(Convolution::None)) - .filter_map(|conv| conv) + .flatten() .collect(), pid_basis: grid .key_values() diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 9253c0ba..aea040b8 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -117,12 +117,7 @@ fn hadronic_pspgen(rng: &mut impl Rng, mmin: f64, mmax: f64) -> Psp2to2 { } } -fn fill_drell_yan_lo_grid( - rng: &mut impl Rng, - calls: u32, - dynamic: bool, - reweight: bool, -) -> Result { +fn fill_drell_yan_lo_grid(rng: &mut impl Rng, calls: u32, dynamic: bool, reweight: bool) -> Grid { let channels = vec![ // photons channel![22, 22, 1.0], @@ -309,7 +304,7 @@ fn fill_drell_yan_lo_grid( grid.fill(pto, yll.abs(), channel, &[q2, x2, x1], weight); } - Ok(grid) + grid } fn perform_grid_tests( @@ -320,12 +315,12 @@ fn perform_grid_tests( reweight: bool, ) -> Result<()> { let mut rng = Pcg64::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7ac28fa16a64abf96); - let mut grid = fill_drell_yan_lo_grid(&mut rng, INT_STATS, dynamic, reweight)?; + let mut grid = fill_drell_yan_lo_grid(&mut rng, INT_STATS, dynamic, reweight); // TEST 1: `merge` and `scale` grid.merge(fill_drell_yan_lo_grid( &mut rng, INT_STATS, dynamic, reweight, - )?)?; + ))?; grid.scale(0.5); // suppress LHAPDF banners @@ -512,7 +507,7 @@ fn perform_grid_tests( Ok(()) } -fn generate_grid(dynamic: bool, reweight: bool) -> Result { +fn generate_grid(dynamic: bool, reweight: bool) -> Grid { let mut rng = Pcg64::new(0xcafef00dd15ea5e5, 0xa02bdbf7bb3c0a7ac28fa16a64abf96); fill_drell_yan_lo_grid(&mut rng, 500_000, dynamic, reweight) } @@ -685,8 +680,8 @@ fn drell_yan_dynamic_no_reweight() -> Result<()> { } #[test] -fn grid_optimize() -> Result<()> { - let mut grid = generate_grid(false, false)?; +fn grid_optimize() { + let mut grid = generate_grid(false, false); assert_eq!(grid.orders().len(), 3); assert_eq!(grid.channels().len(), 5); @@ -747,6 +742,4 @@ fn grid_optimize() -> Result<()> { assert_eq!(grid.orders().len(), 1); assert_eq!(grid.channels().len(), 3); - - Ok(()) } diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 40c3fd45..ccc8b893 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -24,26 +24,23 @@ fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result = grid + let mut mu2_grid: Vec<_> = grid .subgrids() .slice(s![order, bin, ..]) .iter() - .filter_map(|subgrid| { - (!subgrid.is_empty()).then(|| { - Ok(grid - .kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values()) - }) + .filter(|subgrid| !subgrid.is_empty()) + .flat_map(|subgrid| { + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // TODO: convert this into an error + .unwrap() + .values() }) - .collect::>()?; - let mut mu2_grid: Vec<_> = mu2_grid.into_iter().flatten().collect(); + .collect(); mu2_grid.dedup_by(|a, b| approx_eq!(f64, *a, *b, ulps = 128)); // TODO: implement the general case @@ -123,7 +120,7 @@ pub fn convert_into_applgrid( } let lumis = grid.channels().len(); - let has_pdf1 = grid.convolutions().get(0).is_some(); + let has_pdf1 = grid.convolutions().first().is_some(); let has_pdf2 = grid.convolutions().get(1).is_some(); // TODO: check that PDG MC IDs are used diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index b9865226..4be31148 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -159,7 +159,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { convolutions.clone(), interps.clone(), iter::once(Kinematics::Scale(0)) - .chain((0..convolutions.len()).map(|idx| Kinematics::X(idx))) + .chain((0..convolutions.len()).map(Kinematics::X)) .collect(), Scales { ren: ScaleFuncForm::Scale(0), From 823b20f76941cbec60ecf81bd9e7e53559f8bb95 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sat, 12 Oct 2024 11:58:28 +0200 Subject: [PATCH 164/213] Add new method `PidBasis::translate` --- pineappl/src/boc.rs | 14 -------------- pineappl/src/grid.rs | 44 +++++++++++--------------------------------- pineappl/src/pids.rs | 28 ++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 49 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 1d5e8e73..17ce36b3 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -632,7 +632,6 @@ macro_rules! channel { #[cfg(test)] mod tests { use super::*; - use crate::pids; #[test] fn order_from_str() { @@ -836,19 +835,6 @@ mod tests { ); } - #[test] - fn channel_translate() { - let channel = channel![103, 203, 2.0].translate(&pids::evol_to_pdg_mc_ids); - - assert_eq!( - channel, - channel![ 2, 2, 2.0; 2, -2, -2.0; 2, 1, -2.0; 2, -1, 2.0; - -2, 2, 2.0; -2, -2, -2.0; -2, 1, -2.0; -2, -1, 2.0; - 1, 2, -2.0; 1, -2, 2.0; 1, 1, 2.0; 1, -1, -2.0; - -1, 2, -2.0; -1, -2, 2.0; -1, 1, 2.0; -1, -1, -2.0] - ); - } - #[test] fn channel_from_str() { assert_eq!( diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 599b6ec0..02dac604 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -9,7 +9,7 @@ use super::fk_table::FkTable; use super::interpolation::Interp; use super::lagrange_subgrid::LagrangeSubgridV2; use super::packed_subgrid::PackedQ1X2SubgridV1; -use super::pids::{self, PidBasis}; +use super::pids::PidBasis; use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use super::v0; use bitflags::bitflags; @@ -19,7 +19,6 @@ use itertools::Itertools; use lz4_flex::frame::{FrameDecoder, FrameEncoder}; use ndarray::{s, Array3, ArrayD, ArrayView3, ArrayViewMut3, Axis, CowArray, Dimension, Ix4}; use serde::{Deserialize, Serialize}; -use std::borrow::Cow; use std::collections::BTreeMap; use std::io::{self, BufRead, BufReader, BufWriter, Read, Write}; use std::iter; @@ -502,15 +501,12 @@ impl Grid { &self.channels } - fn channels_pdg(&self) -> Cow<[Channel]> { - match self.pid_basis() { - PidBasis::Evol => self - .channels - .iter() - .map(|entry| entry.translate(&pids::evol_to_pdg_mc_ids)) - .collect(), - PidBasis::Pdg => Cow::Borrowed(self.channels()), - } + fn channels_pdg(&self) -> Vec { + self.channels() + .iter() + .cloned() + .map(|channel| self.pid_basis().translate(PidBasis::Pdg, channel)) + .collect() } /// Merges the bins for the corresponding range together in a single one. @@ -1490,29 +1486,11 @@ impl Grid { /// Change the particle ID convention. pub fn rotate_pid_basis(&mut self, pid_basis: PidBasis) { - match (self.pid_basis(), pid_basis) { - (PidBasis::Pdg, PidBasis::Evol) => { - self.channels = self - .channels() - .iter() - .map(|channel| channel.translate(&pids::pdg_mc_pids_to_evol)) - .collect(); - - *self.pid_basis_mut() = PidBasis::Evol; - } - (PidBasis::Evol, PidBasis::Pdg) => { - self.channels = self - .channels() - .iter() - .map(|channel| channel.translate(&pids::evol_to_pdg_mc_ids)) - .collect(); - - *self.pid_basis_mut() = PidBasis::Pdg; - } - (PidBasis::Evol, PidBasis::Evol) | (PidBasis::Pdg, PidBasis::Pdg) => { - // here's nothing to do - } + let self_pid_basis = *self.pid_basis(); + for channel in &mut self.channels { + *channel = self_pid_basis.translate(pid_basis, channel.clone()); } + self.pid_basis = pid_basis; } /// Deletes channels with the corresponding `channel_indices`. Repeated indices and indices diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index 100864f7..42c8d889 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -1,5 +1,6 @@ //! TODO +use super::boc::Channel; use serde::{Deserialize, Serialize}; use std::str::FromStr; use thiserror::Error; @@ -101,6 +102,16 @@ impl PidBasis { ), } } + + /// TODO + #[must_use] + pub fn translate(&self, to: Self, channel: Channel) -> Channel { + match (self, to) { + (&Self::Pdg, Self::Pdg) | (&Self::Evol, Self::Evol) => channel, + (&Self::Pdg, Self::Evol) => channel.translate(&pdg_mc_pids_to_evol), + (&Self::Evol, Self::Pdg) => channel.translate(&evol_to_pdg_mc_ids), + } + } } /// Error returned by [`PidBasis::from_str`] when passed with an unknown argument. @@ -112,7 +123,7 @@ pub struct UnknownPidBasis { /// Translates IDs from the evolution basis into IDs using PDG Monte Carlo IDs. #[must_use] -pub fn evol_to_pdg_mc_ids(id: i32) -> Vec<(i32, f64)> { +fn evol_to_pdg_mc_ids(id: i32) -> Vec<(i32, f64)> { match id { 100 => vec![ (2, 1.0), @@ -238,7 +249,7 @@ pub fn evol_to_pdg_mc_ids(id: i32) -> Vec<(i32, f64)> { /// Translates PDG Monte Carlo IDs to particle IDs from the evolution basis. #[must_use] -pub fn pdg_mc_pids_to_evol(pid: i32) -> Vec<(i32, f64)> { +fn pdg_mc_pids_to_evol(pid: i32) -> Vec<(i32, f64)> { match pid { -6 => vec![ (100, 1.0 / 12.0), @@ -1015,4 +1026,17 @@ mod tests { fn to_latex_str_error() { let _ = PidBasis::Pdg.to_latex_str(999); } + + #[test] + fn translate() { + let channel = PidBasis::Evol.translate(PidBasis::Pdg, channel![103, 203, 2.0]); + + assert_eq!( + channel, + channel![ 2, 2, 2.0; 2, -2, -2.0; 2, 1, -2.0; 2, -1, 2.0; + -2, 2, 2.0; -2, -2, -2.0; -2, 1, -2.0; -2, -1, 2.0; + 1, 2, -2.0; 1, -2, 2.0; 1, 1, 2.0; 1, -1, -2.0; + -1, 2, -2.0; -1, -2, 2.0; -1, 1, 2.0; -1, -1, -2.0] + ); + } } From d4d9ec1397b385692ce5b9d9ac0343029d3b7f5b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Sat, 12 Oct 2024 13:14:49 +0200 Subject: [PATCH 165/213] Increase code coverage a bit --- pineappl/src/empty_subgrid.rs | 17 +++++++ pineappl/src/fk_table.rs | 12 ++--- pineappl/src/grid.rs | 93 ++++++++++++++++++++++++++++++++++- pineappl_cli/tests/evolve.rs | 3 +- 4 files changed, 116 insertions(+), 9 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 5f2acfea..4977d082 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -55,6 +55,8 @@ impl Subgrid for EmptySubgridV1 { #[cfg(test)] mod tests { use super::*; + use crate::packed_array::PackedArray; + use crate::packed_subgrid::PackedQ1X2SubgridV1; use crate::v0; #[test] @@ -84,6 +86,21 @@ mod tests { subgrid.fill(&v0::default_interps(2), &[0.0; 3], 0.0); } + #[test] + #[should_panic( + expected = "EmptySubgridV1 doesn't support the merge operation for non-empty subgrids" + )] + fn merge_non_empty() { + let mut subgrid_lhs = EmptySubgridV1; + + let mut array = PackedArray::new(vec![1, 1]); + array[0] = 1.0; + let node_values = vec![NodeValues::UseThese(vec![1.0]); 2]; + let subgrid_rhs = PackedQ1X2SubgridV1::new(array, node_values).into(); + + subgrid_lhs.merge(&subgrid_rhs, None); + } + #[test] fn node_values() { assert!(EmptySubgridV1.node_values().is_empty()); diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index f5ec3e76..5c3e117b 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -211,12 +211,12 @@ impl FkTable { /// Returns the single `muf2` scale of this `FkTable`. #[must_use] pub fn muf2(&self) -> f64 { - if let &[muf2] = &self.grid.evolve_info(&[true]).fac1[..] { - muf2 - } else { - // every `FkTable` has only a single factorization scale - unreachable!() - } + let [muf2] = self.grid.evolve_info(&[true]).fac1[..] + .try_into() + // UNWRAP: every `FkTable` has only a single factorization scale + .unwrap_or_else(|_| unreachable!()); + + muf2 } /// Returns the x grid that all subgrids for all hadronic initial states share. diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 02dac604..19ddcb06 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -166,7 +166,9 @@ impl Grid { assert_eq!( interps.len(), kinematics.len(), - "interps and kinematics have different lengths" + "interps and kinematics have different lengths: {} vs. {}", + interps.len(), + kinematics.len(), ); assert!( @@ -1618,7 +1620,7 @@ mod tests { #[test] #[should_panic(expected = "channel #0 has wrong number of PIDs: expected 2, found 3")] - fn grid_new_panic() { + fn grid_new_panic0() { let channel = vec![(vec![1, -1, 1], 1.0), (vec![2, -2, 2], 1.0)]; let _ = Grid::new( @@ -1640,6 +1642,64 @@ mod tests { ); } + #[test] + #[should_panic(expected = "interps and kinematics have different lengths: 2 vs. 3")] + fn grid_new_panic1() { + let channel = vec![(vec![1, -1], 1.0), (vec![2, -2], 1.0)]; + + let _ = Grid::new( + PidBasis::Pdg, + vec![Channel::new(channel)], + vec![Order::new(0, 2, 0, 0, 0)], + vec![0.0, 1.0], + vec![ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212), + ], + v0::default_interps(1), + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, + ); + } + + #[test] + #[should_panic(expected = "scales and kinematics are not compatible")] + fn grid_new_panic2() { + let channel = vec![(vec![1, -1], 1.0), (vec![2, -2], 1.0)]; + + let _ = Grid::new( + PidBasis::Pdg, + vec![Channel::new(channel)], + vec![Order::new(0, 2, 0, 0, 0)], + vec![0.0, 1.0], + vec![ + Conv::new(ConvType::UnpolPDF, 2212), + Conv::new(ConvType::UnpolPDF, 2212), + ], + v0::default_interps(2), + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(1), + frg: ScaleFuncForm::NoScale, + }, + ); + } + + #[test] + fn grid_read_file_version_unsupported() { + assert!(matches!( + Grid::read( + &[b'P', b'i', b'n', b'e', b'A', b'P', b'P', b'L', 99, 0, 0, 0, 0, 0, 0, 0][..] + ), + Err(GridError::FileVersionUnsupported { file_version: 99 }) + )); + } + #[test] fn grid_merge_empty_subgrids() { let mut grid = Grid::new( @@ -1898,6 +1958,35 @@ mod tests { ); } + #[test] + fn grid_set_remapper_bin_number_mismatch() { + let mut grid = Grid::new( + PidBasis::Pdg, + vec![ + channel![2, 2, 1.0; 4, 4, 1.0], + channel![1, 1, 1.0; 3, 3, 1.0], + ], + vec![Order::new(0, 2, 0, 0, 0)], + vec![0.0, 0.25, 0.5, 0.75, 1.0], + vec![Conv::new(ConvType::UnpolPDF, 2212); 2], + v0::default_interps(2), + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, + ); + + assert!(matches!( + grid.set_remapper(BinRemapper::new(vec![1.0], vec![(0.0, 1.0)]).unwrap()), + Err(GridError::BinNumberMismatch { + grid_bins: 4, + remapper_bins: 1 + }) + )); + } + #[test] fn evolve_info() { let grid = diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index 7e862ada..4f50125c 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -171,7 +171,7 @@ const NUTEV_CC_NU_FE_SIGMARED_STR: &str = "b Grid FkTable rel. dif const CMS_TTB_8TEV_2D_TTM_TRAP_TOT_STR: &str = "b Grid FkTable rel. diff -+-----------+-----------+------------- -0 2.1596192e2 2.1590144e2 -2.8005486e-4 +0 2.0680644e2 2.0666857e2 -6.6663644e-4 "; const STAR_WMWP_510GEV_WM_AL_POL: &str = "b Grid FkTable rel. diff @@ -406,6 +406,7 @@ fn cms_ttb_8tev_2d_ttm_trap_tot() { .args([ "evolve", "--orders=as2,as3,as4", + "--xir=2", "../test-data/CMS_TTB_8TEV_2D_TTM_TRAP_TOT-opt.pineappl.lz4", "../test-data/CMS_TTB_8TEV_2D_TTM_TRAP_TOT.tar", output.path().to_str().unwrap(), From db0fafd50478f33d35a39be45296dc8daf5a2131 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Mon, 14 Oct 2024 10:13:40 +0200 Subject: [PATCH 166/213] Disable carryforward to remove Python test coverage --- .github/codecov.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/codecov.yml b/.github/codecov.yml index 7e551e67..e00ce3d6 100644 --- a/.github/codecov.yml +++ b/.github/codecov.yml @@ -1,6 +1,2 @@ -flag_management: - default_rules: - carryforward: true - github_checks: annotations: false From 24223924fa1b020a395891ef210643fabbc7607f Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Wed, 16 Oct 2024 11:44:06 +0200 Subject: [PATCH 167/213] Start fixing Python interface --- clippy.toml | 1 + pineappl_py/src/bin.rs | 9 + pineappl_py/src/boc.rs | 60 ++-- pineappl_py/src/convolutions.rs | 71 +++++ pineappl_py/src/evolution.rs | 8 + pineappl_py/src/fk_table.rs | 96 +++--- pineappl_py/src/grid.rs | 391 ++++++++++--------------- pineappl_py/src/import_only_subgrid.rs | 169 ----------- pineappl_py/src/interpolation.rs | 51 ++++ pineappl_py/src/lib.rs | 12 +- pineappl_py/src/pids.rs | 3 + pineappl_py/src/subgrid.rs | 165 ++--------- 12 files changed, 422 insertions(+), 614 deletions(-) create mode 100644 pineappl_py/src/convolutions.rs delete mode 100644 pineappl_py/src/import_only_subgrid.rs create mode 100644 pineappl_py/src/interpolation.rs diff --git a/clippy.toml b/clippy.toml index ca61e3bf..e117ecbe 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1,2 @@ doc-valid-idents = ["APPLgrid", "PineAPPL", ".."] +too-many-arguments-threshold = 12 diff --git a/pineappl_py/src/bin.rs b/pineappl_py/src/bin.rs index 17398e17..2d043909 100644 --- a/pineappl_py/src/bin.rs +++ b/pineappl_py/src/bin.rs @@ -16,6 +16,10 @@ pub struct PyBinRemapper { impl PyBinRemapper { /// Constructor. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// normalizations : list(float) @@ -23,6 +27,8 @@ impl PyBinRemapper { /// limits : list(tuple(float, float)) /// bin limits #[new] + #[must_use] + #[allow(clippy::needless_pass_by_value)] pub fn new(normalizations: PyReadonlyArray1, limits: Vec<(f64, f64)>) -> Self { Self { bin_remapper: BinRemapper::new(normalizations.to_vec().unwrap(), limits).unwrap(), @@ -31,6 +37,9 @@ impl PyBinRemapper { } /// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let m = PyModule::new_bound(parent_module.py(), "bin")?; m.setattr(pyo3::intern!(m.py(), "__doc__"), "Binning interface.")?; diff --git a/pineappl_py/src/boc.rs b/pineappl_py/src/boc.rs index 343aa71b..e3336d98 100644 --- a/pineappl_py/src/boc.rs +++ b/pineappl_py/src/boc.rs @@ -23,10 +23,11 @@ impl PyChannel { /// /// Parameters /// ---------- - /// entry: list(tuple(int, int, float)) + /// entry: list(tuple(list(int),float)) /// channel configuration #[new] - pub fn new(entry: Vec<(i32, i32, f64)>) -> Self { + #[must_use] + pub fn new(entry: Vec<(Vec, f64)>) -> Self { Self { entry: Channel::new(entry), } @@ -36,29 +37,14 @@ impl PyChannel { /// /// Returns /// ------- - /// list(tuple(int,int,float)) : + /// list(tuple(list(int),float)) : /// list representation - pub fn into_array(&self) -> Vec<(i32, i32, f64)> { + #[must_use] + pub fn into_array(&self) -> Vec<(Vec, f64)> { self.entry.entry().to_vec() } } -/// Register submodule in parent. -pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { - let m = PyModule::new_bound(parent_module.py(), "boc")?; - m.setattr( - pyo3::intern!(m.py(), "__doc__"), - "Interface for bins, orders and channels.", - )?; - pyo3::py_run!( - parent_module.py(), - m, - "import sys; sys.modules['pineappl.channel'] = m" - ); - m.add_class::()?; - parent_module.add_submodule(&m) -} - /// PyO3 wrapper to :rustdoc:`pineappl::boc::Order `. #[pyclass(name = "Order")] #[repr(transparent)] @@ -67,7 +53,7 @@ pub struct PyOrder { } impl PyOrder { - pub(crate) fn new(order: Order) -> Self { + pub(crate) const fn new(order: Order) -> Self { Self { order } } } @@ -86,9 +72,12 @@ impl PyOrder { /// power of :math:`\ln(\xi_r)` /// logxif : int /// power of :math:`\ln(\xi_f)` + /// logxia : int + /// power of :math:`\ln(\xi_a)` #[new] - pub fn new_order(alphas: u32, alpha: u32, logxir: u32, logxif: u32) -> Self { - Self::new(Order::new(alphas, alpha, logxir, logxif)) + #[must_use] + pub const fn new_order(alphas: u32, alpha: u32, logxir: u32, logxif: u32, logxia: u32) -> Self { + Self::new(Order::new(alphas, alpha, logxir, logxif, logxia)) } /// Tuple representation. @@ -103,7 +92,8 @@ impl PyOrder { /// power of :math:`\ln(\xi_r)` /// logxif : int /// power of :math:`\ln(\xi_f)` - pub fn as_tuple(&self) -> (u32, u32, u32, u32) { + #[must_use] + pub const fn as_tuple(&self) -> (u32, u32, u32, u32) { ( self.order.alphas, self.order.alpha, @@ -126,6 +116,8 @@ impl PyOrder { /// numpy.ndarray(bool) /// boolean array, to be used as orders' mask #[staticmethod] + #[must_use] + #[allow(clippy::needless_pass_by_value)] pub fn create_mask<'py>( orders: Vec>, max_as: u32, @@ -142,3 +134,23 @@ impl PyOrder { .into_pyarray_bound(py) } } + +/// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. +pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { + let m = PyModule::new_bound(parent_module.py(), "boc")?; + m.setattr( + pyo3::intern!(m.py(), "__doc__"), + "Interface for bins, orders and channels.", + )?; + pyo3::py_run!( + parent_module.py(), + m, + "import sys; sys.modules['pineappl.channel'] = m" + ); + m.add_class::()?; + m.add_class::()?; + parent_module.add_submodule(&m) +} diff --git a/pineappl_py/src/convolutions.rs b/pineappl_py/src/convolutions.rs new file mode 100644 index 00000000..71e7e77a --- /dev/null +++ b/pineappl_py/src/convolutions.rs @@ -0,0 +1,71 @@ +//! Convolution interface. + +use pineappl::convolutions::{Conv, ConvType}; +use pyo3::prelude::*; + +/// PyO3 wrapper to :rustdoc:`pineappl::`. +#[pyclass(name = "ConvType")] +#[repr(transparent)] +pub struct PyConvType { + pub(crate) convtype: ConvType, +} + +impl PyConvType { + pub(crate) const fn new(convtype: ConvType) -> Self { + Self { convtype } + } +} + +#[pymethods] +impl PyConvType { + /// Constructor. + #[new] + #[must_use] + pub const fn new_convtype(polarized: bool, time_like: bool) -> Self { + Self::new(ConvType::new(polarized, time_like)) + } +} + +/// PyO3 wrapper to :rustdoc:`pineappl::`. +#[pyclass(name = "Conv")] +#[repr(transparent)] +pub struct PyConv { + pub(crate) conv: Conv, +} + +impl PyConv { + pub(crate) const fn new(conv: Conv) -> Self { + Self { conv } + } +} + +#[pymethods] +impl PyConv { + /// Constructor. + #[new] + #[must_use] + #[allow(clippy::needless_pass_by_value)] + pub fn new_conv(conv_type: PyRef, pid: i32) -> Self { + Self::new(Conv::new(conv_type.convtype, pid)) + } +} + +/// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. +pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { + let m = PyModule::new_bound(parent_module.py(), "convolutions")?; + m.setattr( + pyo3::intern!(m.py(), "__doc__"), + "Define the type of convolutions.", + )?; + pyo3::py_run!( + parent_module.py(), + m, + "import sys; sys.modules['vector_length_module.convolutions'] = m" + ); + m.add_class::()?; + m.add_class::()?; + parent_module.add_submodule(&m) +} diff --git a/pineappl_py/src/evolution.rs b/pineappl_py/src/evolution.rs index 4a28a3de..344fdced 100644 --- a/pineappl_py/src/evolution.rs +++ b/pineappl_py/src/evolution.rs @@ -1,5 +1,6 @@ //! Evolution interface. +use super::convolutions::PyConvType; use super::pids::PyPidBasis; use numpy::{IntoPyArray, PyArray1}; use pineappl::evolution::{EvolveInfo, OperatorSliceInfo}; @@ -34,6 +35,8 @@ impl PyOperatorSliceInfo { /// pid_basis : PyPidBasis /// flavor basis reprentation at the initial scale #[new] + #[must_use] + #[allow(clippy::needless_pass_by_value)] pub fn new( fac0: f64, pids0: Vec, @@ -42,6 +45,7 @@ impl PyOperatorSliceInfo { pids1: Vec, x1: Vec, pid_basis: PyPidBasis, + conv_type: PyRef, ) -> Self { Self { info: OperatorSliceInfo { @@ -52,6 +56,7 @@ impl PyOperatorSliceInfo { pids1, x1, pid_basis: pid_basis.into(), + conv_type: conv_type.convtype, }, } } @@ -92,6 +97,9 @@ impl PyEvolveInfo { } /// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let m = PyModule::new_bound(parent_module.py(), "evolution")?; m.setattr(pyo3::intern!(m.py(), "__doc__"), "Evolution interface.")?; diff --git a/pineappl_py/src/fk_table.rs b/pineappl_py/src/fk_table.rs index cafedc2d..dcbf1a67 100644 --- a/pineappl_py/src/fk_table.rs +++ b/pineappl_py/src/fk_table.rs @@ -1,12 +1,12 @@ //! FK table interface. +use super::convolutions::PyConv; use super::grid::PyGrid; -use numpy::{IntoPyArray, PyArray1, PyArray4, PyArrayMethods, PyReadonlyArray1}; -use pineappl::convolutions::LumiCache; +use numpy::{IntoPyArray, PyArray1, PyArrayDyn, PyArrayMethods, PyReadonlyArray1}; +use pineappl::convolutions::ConvolutionCache; use pineappl::fk_table::{FkAssumptions, FkTable}; use pineappl::grid::Grid; use pyo3::prelude::*; -use std::collections::HashMap; use std::fs::File; use std::io::BufReader; use std::path::PathBuf; @@ -29,9 +29,12 @@ pub struct PyFkAssumptions { #[pymethods] impl PyFkAssumptions { /// Constructor. + /// # Panics + /// TODO #[new] + #[must_use] pub fn new(assumption: &str) -> Self { - PyFkAssumptions { + Self { fk_assumptions: FkAssumptions::from_str(assumption).unwrap(), } } @@ -40,7 +43,10 @@ impl PyFkAssumptions { #[pymethods] impl PyFkTable { /// Constructor from an existing grid. + /// # Panics + /// TODO #[new] + #[must_use] pub fn new(grid: PyGrid) -> Self { Self { fk_table: FkTable::try_from(grid.grid).unwrap(), @@ -48,6 +54,9 @@ impl PyFkTable { } /// Read from given path. + /// # Panics + /// TODO + #[must_use] #[staticmethod] pub fn read(path: PathBuf) -> Self { Self { @@ -60,11 +69,14 @@ impl PyFkTable { /// Get cross section tensor. /// + /// # Errors + /// TODO + /// /// Returns /// ------- /// numpy.ndarray : /// 4-dimensional tensor with indixes: bin, channel, x1, x2 - pub fn table<'py>(&self, py: Python<'py>) -> PyResult>> { + pub fn table<'py>(&self, py: Python<'py>) -> PyResult>> { Ok(self.fk_table.table().into_pyarray_bound(py)) } @@ -74,6 +86,7 @@ impl PyFkTable { /// ------- /// int : /// number of bins + #[must_use] pub fn bins(&self) -> usize { self.fk_table.grid().bin_info().bins() } @@ -84,6 +97,7 @@ impl PyFkTable { /// ------- /// numpy.ndarray /// bin normalizations + #[must_use] pub fn bin_normalizations<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1> { self.fk_table .grid() @@ -100,6 +114,7 @@ impl PyFkTable { /// ------- /// int : /// bin dimension + #[must_use] pub fn bin_dimensions(&self) -> usize { self.fk_table.grid().bin_info().dimensions() } @@ -115,6 +130,7 @@ impl PyFkTable { /// ------- /// numpy.ndarray(float) : /// left edges of bins + #[must_use] pub fn bin_left<'py>(&self, dimension: usize, py: Python<'py>) -> Bound<'py, PyArray1> { self.fk_table .grid() @@ -134,6 +150,7 @@ impl PyFkTable { /// ------- /// numpy.ndarray(float) : /// right edges of bins + #[must_use] pub fn bin_right<'py>(&self, dimension: usize, py: Python<'py>) -> Bound<'py, PyArray1> { self.fk_table .grid() @@ -142,35 +159,14 @@ impl PyFkTable { .into_pyarray_bound(py) } - /// Get metadata values. - /// - /// Returns - /// ------- - /// dict : - /// key, value map - pub fn key_values(&self) -> HashMap { - self.fk_table.grid().key_values().unwrap().clone() - } - - /// Set a metadata key-value pair. - /// - /// Parameters - /// ---------- - /// key : str - /// key - /// value : str - /// value - pub fn set_key_value(&mut self, key: &str, value: &str) { - self.fk_table.set_key_value(key, value); - } - /// Get channels. /// /// Returns /// ------- /// list(tuple(float,float)) : /// channel functions as pid tuples - pub fn channels(&self) -> Vec<(i32, i32)> { + #[must_use] + pub fn channels(&self) -> Vec> { self.fk_table.channels() } @@ -180,6 +176,7 @@ impl PyFkTable { /// ------- /// float : /// reference scale + #[must_use] pub fn muf2(&self) -> f64 { self.fk_table.muf2() } @@ -190,12 +187,16 @@ impl PyFkTable { /// ------- /// x_grid : numpy.ndarray(float) /// interpolation grid + #[must_use] pub fn x_grid<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1> { self.fk_table.x_grid().into_pyarray_bound(py) } /// Write to file. /// + /// # Panics + /// TODO + /// /// Parameters /// ---------- /// path : str @@ -209,6 +210,9 @@ impl PyFkTable { /// Write to file using lz4. /// + /// # Panics + /// TODO + /// /// Parameters /// ---------- /// path : str @@ -222,6 +226,9 @@ impl PyFkTable { /// Convolve with a single distribution. /// + /// # Panics + /// TODO + /// /// Parameters /// ---------- /// pdg_id : integer @@ -233,10 +240,12 @@ impl PyFkTable { /// ------- /// numpy.ndarray(float) : /// cross sections for all bins - #[pyo3(signature = (pdg_id, xfx, bin_indices = None, channel_mask= None))] + #[must_use] + #[allow(clippy::needless_pass_by_value)] + #[pyo3(signature = (pdg_conv, xfx, bin_indices = None, channel_mask= None))] pub fn convolve_with_one<'py>( &self, - pdg_id: i32, + pdg_conv: PyRef, xfx: &Bound<'py, PyAny>, bin_indices: Option>, channel_mask: Option>, @@ -244,7 +253,8 @@ impl PyFkTable { ) -> Bound<'py, PyArray1> { let mut xfx = |id, x, q2| xfx.call1((id, x, q2)).unwrap().extract().unwrap(); let mut alphas = |_| 1.0; - let mut lumi_cache = LumiCache::with_one(pdg_id, &mut xfx, &mut alphas); + let mut lumi_cache = + ConvolutionCache::new(vec![pdg_conv.conv.clone()], vec![&mut xfx], &mut alphas); self.fk_table .convolve( &mut lumi_cache, @@ -256,6 +266,9 @@ impl PyFkTable { /// Convoluve grid with two different distribution. /// + /// # Panics + /// TODO + /// /// Parameters /// ---------- /// pdg_id1 : integer @@ -271,12 +284,14 @@ impl PyFkTable { /// ------- /// numpy.ndarray(float) : /// cross sections for all bins - #[pyo3(signature = (pdg_id1, xfx1, pdg_id2, xfx2, bin_indices = None, channel_mask= None))] + #[must_use] + #[allow(clippy::needless_pass_by_value)] + #[pyo3(signature = (pdg_conv1, xfx1, pdg_conv2, xfx2, bin_indices = None, channel_mask= None))] pub fn convolve_with_two<'py>( &self, - pdg_id1: i32, + pdg_conv1: PyRef, xfx1: &Bound<'py, PyAny>, - pdg_id2: i32, + pdg_conv2: PyRef, xfx2: &Bound<'py, PyAny>, bin_indices: Option>, channel_mask: Option>, @@ -285,8 +300,11 @@ impl PyFkTable { let mut xfx1 = |id, x, q2| xfx1.call1((id, x, q2)).unwrap().extract().unwrap(); let mut xfx2 = |id, x, q2| xfx2.call1((id, x, q2)).unwrap().extract().unwrap(); let mut alphas = |_| 1.0; - let mut lumi_cache = - LumiCache::with_two(pdg_id1, &mut xfx1, pdg_id2, &mut xfx2, &mut alphas); + let mut lumi_cache = ConvolutionCache::new( + vec![pdg_conv1.conv.clone(), pdg_conv2.conv.clone()], + vec![&mut xfx1, &mut xfx2], + &mut alphas, + ); self.fk_table .convolve( &mut lumi_cache, @@ -306,12 +324,16 @@ impl PyFkTable { /// assumptions : PyFkAssumptions /// assumptions about the FkTable properties, declared by the user, deciding which /// optimizations are possible + #[allow(clippy::needless_pass_by_value)] pub fn optimize(&mut self, assumptions: PyRef) { - self.fk_table.optimize(assumptions.fk_assumptions) + self.fk_table.optimize(assumptions.fk_assumptions); } } /// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let m = PyModule::new_bound(parent_module.py(), "fk_table")?; m.setattr(pyo3::intern!(m.py(), "__doc__"), "FK table interface.")?; diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 8ac7328f..8eec0bbe 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -2,19 +2,20 @@ use super::bin::PyBinRemapper; use super::boc::{PyChannel, PyOrder}; +use super::convolutions::PyConv; use super::evolution::{PyEvolveInfo, PyOperatorSliceInfo}; use super::fk_table::PyFkTable; -use super::subgrid::{PySubgridEnum, PySubgridParams}; -use itertools::izip; +use super::interpolation::PyInterp; +use super::pids::PyPidBasis; use ndarray::CowArray; use numpy::{IntoPyArray, PyArray1, PyArrayMethods, PyReadonlyArray1, PyReadonlyArray4}; -use pineappl::convolutions::LumiCache; +use pineappl::boc::{Kinematics, ScaleFuncForm, Scales}; +use pineappl::convolutions::ConvolutionCache; use pineappl::evolution::AlphasTable; -use pineappl::grid::{Grid, Ntuple}; +use pineappl::grid::Grid; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::types::PyIterator; -use std::collections::HashMap; use std::fs::File; use std::io::BufReader; use std::path::PathBuf; @@ -31,6 +32,12 @@ pub struct PyGrid { impl PyGrid { /// Constructor. /// + /// TODO Exposes all the arguments + /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// channels : list(PyChannel) @@ -42,18 +49,33 @@ impl PyGrid { /// subgrid_params : PySubgridParams /// subgrid parameters #[new] + #[must_use] + #[allow(clippy::needless_pass_by_value)] pub fn new_grid( + pid_basis: PyPidBasis, channels: Vec>, orders: Vec>, bin_limits: PyReadonlyArray1, - subgrid_params: PySubgridParams, + convolutions: Vec>, + interpolations: Vec>, ) -> Self { Self { grid: Grid::new( + pid_basis.into(), channels.iter().map(|pyc| pyc.entry.clone()).collect(), orders.iter().map(|pyo| pyo.order.clone()).collect(), bin_limits.to_vec().unwrap(), - subgrid_params.subgrid_params, + convolutions.iter().map(|pyx| pyx.conv.clone()).collect(), + interpolations + .iter() + .map(|pyi| pyi.interp.clone()) + .collect(), + vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + Scales { + ren: ScaleFuncForm::Scale(0), + fac: ScaleFuncForm::Scale(0), + frg: ScaleFuncForm::NoScale, + }, ), } } @@ -62,164 +84,33 @@ impl PyGrid { /// /// Parameters /// ---------- - /// x1 : float - /// first momentum fraction - /// x2 : float - /// second momentum fraction - /// q2 : float - /// process scale /// order : int /// order index /// observable : float /// reference point (to be binned) /// channel : int /// channel index + /// ntuple: list(float) + /// list containing information on kinematics /// weight : float /// cross section weight + #[allow(clippy::needless_pass_by_value)] pub fn fill( &mut self, - x1: f64, - x2: f64, - q2: f64, order: usize, observable: f64, channel: usize, + ntuple: Vec, weight: f64, ) { - self.grid.fill( - order, - observable, - channel, - &Ntuple:: { q2, x1, x2, weight }, - ); - } - - /// Add an array to the grid. - /// - /// Useful to avoid multiple python calls, leading to performance improvement. - /// - /// Parameters - /// ---------- - /// x1s : np.array(float) - /// first momentum fraction of all events - /// x2s : np.array(float) - /// second momentum fraction of all events - /// x1s : np.array(float) - /// process scale of all events - /// order : int - /// order index - /// observable : float - /// reference point (to be binned) - /// channel : int - /// channel index - /// weights : np.array(float) - /// cross section weight for all events - pub fn fill_array( - &mut self, - x1s: PyReadonlyArray1, - x2s: PyReadonlyArray1, - q2s: PyReadonlyArray1, - order: usize, - observables: PyReadonlyArray1, - channel: usize, - weights: PyReadonlyArray1, - ) { - for (&x1, &x2, &q2, &observable, &weight) in izip!( - x1s.as_array().iter(), - x2s.as_array().iter(), - q2s.as_array().iter(), - observables.as_array().iter(), - weights.as_array().iter(), - ) { - self.grid.fill( - order, - observable, - channel, - &Ntuple:: { q2, x1, x2, weight }, - ); - } - } - - /// Add a point to the grid for all channels. - /// - /// Parameters - /// ---------- - /// x1 : float - /// first momentum fraction - /// x2 : float - /// second momentum fraction - /// q2 : float - /// process scale - /// order : int - /// order index - /// observable : float - /// reference point (to be binned) - /// weights : np.array(float) - /// cross section weights, one for each channels - pub fn fill_all( - &mut self, - x1: f64, - x2: f64, - q2: f64, - order: usize, - observable: f64, - weights: PyReadonlyArray1, - ) { - self.grid.fill_all( - order, - observable, - &Ntuple::<()> { - x1, - x2, - q2, - weight: (), - }, - &weights.to_vec().unwrap(), - ); + self.grid.fill(order, observable, channel, &ntuple, weight); } - /// Get metadata values stored in the grid. + /// Set the bin normalizations. /// + /// # Panics /// - /// Returns - /// ------- - /// dict : - /// key, value map - pub fn key_values(&self) -> HashMap { - self.grid.key_values().unwrap().clone() - } - - /// Set a metadata key-value pair in the grid. - /// - /// Parameters - /// ---------- - /// key : str - /// key - /// value : str - /// value - pub fn set_key_value(&mut self, key: &str, value: &str) { - self.grid.set_key_value(key, value); - } - - /// Retrieve a subgrid. - pub fn subgrid(&self, order: usize, bin: usize, channel: usize) -> PySubgridEnum { - PySubgridEnum { - subgrid_enum: self.grid.subgrids()[[order, bin, channel]].clone(), - } - } - - /// Set a subgrid. - pub fn set_subgrid( - &mut self, - order: usize, - bin: usize, - channel: usize, - subgrid: PySubgridEnum, - ) { - self.grid.subgrids_mut()[[order, bin, channel]] = subgrid.subgrid_enum; - } - - /// Set the bin normalizations. + /// TODO /// /// Parameters /// ---------- @@ -231,6 +122,10 @@ impl PyGrid { /// Convolve with a single distribution. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// pdg_id : int @@ -261,35 +156,42 @@ impl PyGrid { /// numpy.ndarray(float) : /// cross sections for all bins, for each scale-variation tuple (first all bins, then /// the scale variation) - #[pyo3(signature = (pdg_id, xfx, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] + #[must_use] + #[allow(clippy::needless_pass_by_value)] + #[pyo3(signature = (pdg_conv, xfx, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] pub fn convolve_with_one<'py>( &self, - pdg_id: i32, + pdg_conv: PyRef, xfx: &Bound<'py, PyAny>, alphas: &Bound<'py, PyAny>, order_mask: Option>, bin_indices: Option>, channel_mask: Option>, - xi: Option>, + xi: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { let mut xfx = |id, x, q2| xfx.call1((id, x, q2)).unwrap().extract().unwrap(); // `(q2, )` must have the comma to make it a Rust tuple let mut alphas = |q2| alphas.call1((q2,)).unwrap().extract().unwrap(); - let mut lumi_cache = LumiCache::with_one(pdg_id, &mut xfx, &mut alphas); + let mut lumi_cache = + ConvolutionCache::new(vec![pdg_conv.conv.clone()], vec![&mut xfx], &mut alphas); self.grid .convolve( &mut lumi_cache, &order_mask.map_or(vec![], |b| b.to_vec().unwrap()), &bin_indices.map_or(vec![], |c| c.to_vec().unwrap()), &channel_mask.map_or(vec![], |d| d.to_vec().unwrap()), - &xi.map_or(vec![(1.0, 1.0)], |m| m), + &xi.map_or_else(|| vec![(1.0, 1.0, 1.0)], |m| m), ) .into_pyarray_bound(py) } /// Convolve with two distributions. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// pdg_id1 : int @@ -324,39 +226,66 @@ impl PyGrid { /// numpy.ndarray(float) : /// cross sections for all bins, for each scale-variation tuple (first all bins, then /// the scale variation) - #[pyo3(signature = (pdg_id1, xfx1, pdg_id2, xfx2, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] + #[must_use] + #[allow(clippy::needless_pass_by_value)] + #[pyo3(signature = (pdg_conv1, xfx1, pdg_conv2, xfx2, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] pub fn convolve_with_two<'py>( &self, - pdg_id1: i32, + pdg_conv1: PyRef, xfx1: &Bound<'py, PyAny>, - pdg_id2: i32, + pdg_conv2: PyRef, xfx2: &Bound<'py, PyAny>, alphas: &Bound<'py, PyAny>, order_mask: Option>, bin_indices: Option>, channel_mask: Option>, - xi: Option>, + xi: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { let mut xfx1 = |id, x, q2| xfx1.call1((id, x, q2)).unwrap().extract().unwrap(); let mut xfx2 = |id, x, q2| xfx2.call1((id, x, q2)).unwrap().extract().unwrap(); // `(q2, )` must have the comma to make it a Rust tuple let mut alphas = |q2| alphas.call1((q2,)).unwrap().extract().unwrap(); - let mut lumi_cache = - LumiCache::with_two(pdg_id1, &mut xfx1, pdg_id2, &mut xfx2, &mut alphas); + let mut lumi_cache = ConvolutionCache::new( + vec![pdg_conv1.conv.clone(), pdg_conv2.conv.clone()], + vec![&mut xfx1, &mut xfx2], + &mut alphas, + ); self.grid .convolve( &mut lumi_cache, &order_mask.map_or(vec![], |b| b.to_vec().unwrap()), &bin_indices.map_or(vec![], |c| c.to_vec().unwrap()), &channel_mask.map_or(vec![], |d| d.to_vec().unwrap()), - &xi.map_or(vec![(1.0, 1.0)], |m| m), + &xi.map_or_else(|| vec![(1.0, 1.0, 1.0)], |m| m), ) .into_pyarray_bound(py) } + /// TODO + // #[pyo3(signature = (pdg_convs, xfxs, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] + #[must_use] + #[allow(clippy::needless_pass_by_value)] + pub fn convolve<'py>( + &self, + _pdg_convs: Vec>, + _xfxs: &Bound<'py, PyIterator>, + _alphas: &Bound<'py, PyAny>, + _order_mask: Option>, + _bin_indices: Option>, + _channel_mask: Option>, + _xi: Option>, + _py: Python<'py>, + ) -> Bound<'py, PyArray1> { + todo!() + } + /// Collect information for convolution with an evolution operator. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// order_mask : numpy.ndarray(bool) @@ -366,13 +295,21 @@ impl PyGrid { /// ------- /// PyEvolveInfo : /// evolution informations + #[must_use] + #[allow(clippy::needless_pass_by_value)] pub fn evolve_info(&self, order_mask: PyReadonlyArray1) -> PyEvolveInfo { PyEvolveInfo { evolve_info: self.grid.evolve_info(order_mask.as_slice().unwrap()), } } - /// Convolve with uniform evolution operator slices. + /// Evolve grid using sliced operators. + /// + /// # Panics + /// TODO + /// + /// # Errors + /// TODO /// /// Parameters /// ---------- @@ -391,18 +328,20 @@ impl PyGrid { /// ------- /// PyFkTable : /// produced FK table - pub fn evolve_with_slice_iter<'py>( + #[allow(clippy::needless_lifetimes)] + #[allow(clippy::needless_pass_by_value)] + pub fn evolve<'py>( &self, slices: &Bound<'py, PyIterator>, order_mask: PyReadonlyArray1, - xi: (f64, f64), + xi: (f64, f64, f64), ren1: Vec, alphas: Vec, ) -> PyResult { Ok(self .grid - .evolve_with_slice_iter( - slices.into_iter().map(|slice| { + .evolve( + vec![slices.into_iter().map(|slice| { let (info, op) = slice .unwrap() .extract::<(PyOperatorSliceInfo, PyReadonlyArray4)>() @@ -412,7 +351,7 @@ impl PyGrid { // TODO: avoid copying CowArray::from(op.as_array().to_owned()), )) - }), + })], // TODO: make `order_mask` a `Vec` &order_mask.to_vec().unwrap(), xi, @@ -423,72 +362,11 @@ impl PyGrid { .unwrap()) } - /// Convolve with two different evolution operator slices. + /// Load from file. /// - /// Parameters - /// ---------- - /// slices_a : Iterable - /// list of (PyOperatorSliceInfo, 5D array) describing the first convolution - /// slices_b : Iterable - /// list of (PyOperatorSliceInfo, 5D array) describing the second convolution - /// order_mask : numpy.ndarray(bool) - /// boolean mask to activate orders - /// xi : (float, float) - /// factorization and renormalization variation - /// ren1 : numpy.ndarray(float) - /// list of renormalization scales - /// alphas : numpy.ndarray(float) - /// list with :math:`\alpha_s(Q2)` for the process scales + /// # Panics /// - /// Returns - /// ------- - /// PyFkTable : - /// produced FK table - pub fn evolve_with_slice_iter2<'py>( - &self, - slices_a: &Bound<'py, PyIterator>, - slices_b: &Bound<'py, PyIterator>, - order_mask: PyReadonlyArray1, - xi: (f64, f64), - ren1: Vec, - alphas: Vec, - ) -> PyResult { - Ok(self - .grid - .evolve_with_slice_iter2( - slices_a.into_iter().map(|slice| { - let (info, op) = slice - .unwrap() - .extract::<(PyOperatorSliceInfo, PyReadonlyArray4)>() - .unwrap(); - Ok::<_, std::io::Error>(( - info.info, - // TODO: avoid copying - CowArray::from(op.as_array().to_owned()), - )) - }), - slices_b.into_iter().map(|slice| { - let (info, op) = slice - .unwrap() - .extract::<(PyOperatorSliceInfo, PyReadonlyArray4)>() - .unwrap(); - Ok::<_, std::io::Error>(( - info.info, - // TODO: avoid copying - CowArray::from(op.as_array().to_owned()), - )) - }), - // TODO: make `order_mask` a `Vec` - &order_mask.to_vec().unwrap(), - xi, - &AlphasTable { ren1, alphas }, - ) - .map(|fk_table| PyFkTable { fk_table }) - // TODO: avoid unwrap and convert `Result` into `PyResult` - .unwrap()) - } - - /// Load from file. + /// TODO /// /// Parameters /// ---------- @@ -499,6 +377,7 @@ impl PyGrid { /// ------- /// PyGrid : /// grid + #[must_use] #[staticmethod] pub fn read(path: PathBuf) -> Self { Self { @@ -508,6 +387,10 @@ impl PyGrid { /// Write to file. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// path : str @@ -518,6 +401,10 @@ impl PyGrid { /// Write to compressed file. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// path : str @@ -532,10 +419,18 @@ impl PyGrid { } /// Merge with another grid. + /// + /// # Panics + /// + /// TODO + /// + /// # Errors + /// + /// TODO pub fn merge(&mut self, other: Self) -> PyResult<()> { match self.grid.merge(other.grid) { Ok(()) => Ok(()), - Err(x) => Err(PyValueError::new_err(format!("{:?}", x))), + Err(x) => Err(PyValueError::new_err(format!("{x:?}"))), } } @@ -547,6 +442,7 @@ impl PyGrid { /// ------- /// int : /// bin dimension + #[must_use] pub fn bin_dimensions(&self) -> usize { self.grid.bin_info().dimensions() } @@ -557,6 +453,7 @@ impl PyGrid { /// ------- /// np.ndarray /// bin normalizations + #[must_use] pub fn bin_normalizations<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1> { self.grid.bin_info().normalizations().into_pyarray_bound(py) } @@ -572,6 +469,7 @@ impl PyGrid { /// ------- /// numpy.ndarray(float) : /// left edges of bins + #[must_use] pub fn bin_left<'py>(&self, dimension: usize, py: Python<'py>) -> Bound<'py, PyArray1> { self.grid.bin_info().left(dimension).into_pyarray_bound(py) } @@ -587,6 +485,7 @@ impl PyGrid { /// ------- /// numpy.ndarray(float) : /// right edges of bins + #[must_use] pub fn bin_right<'py>(&self, dimension: usize, py: Python<'py>) -> Bound<'py, PyArray1> { self.grid.bin_info().right(dimension).into_pyarray_bound(py) } @@ -597,6 +496,7 @@ impl PyGrid { /// ------- /// int : /// Number of bins + #[must_use] pub fn bins(&self) -> usize { self.grid.bin_info().bins() } @@ -607,6 +507,7 @@ impl PyGrid { /// ------- /// list(PyOrder) : /// list with perturbative orders and scale variations + #[must_use] pub fn orders(&self) -> Vec { self.grid .orders() @@ -617,6 +518,20 @@ impl PyGrid { .collect() } + /// Get the type(s) of convolution(s) for the current Grid. + /// + /// Returns + /// list(PyConv): + /// list of convolution type with the corresponding PIDs + #[must_use] + pub fn convolutions(&self) -> Vec { + self.grid + .convolutions() + .iter() + .map(|conv| PyConv { conv: conv.clone() }) + .collect() + } + /// Extract channels. /// /// Returns @@ -624,7 +539,8 @@ impl PyGrid { /// list(list(tuple(float,float,int))) : /// channels as tuples (pid, pid, factor) (multiple tuples can be associated to the same /// contribution) - pub fn channels(&self) -> Vec> { + #[must_use] + pub fn channels(&self) -> Vec, f64)>> { self.grid .channels() .iter() @@ -644,28 +560,41 @@ impl PyGrid { /// Scale subgrids bin by bin. /// + /// # Panics + /// + /// TODO + /// /// Parameters /// ---------- /// factors : numpy.ndarray[float] /// bin-dependent factors by which to scale + #[allow(clippy::needless_pass_by_value)] pub fn scale_by_bin(&mut self, factors: PyReadonlyArray1) { self.grid.scale_by_bin(&factors.to_vec().unwrap()); } /// Delete bins. /// + /// # Panics + /// + /// TODO + /// /// Repeated bins and those exceeding the length are ignored. /// /// Parameters /// ---------- /// bin_indices : numpy.ndarray[int] /// list of indices of bins to removed + #[allow(clippy::needless_pass_by_value)] pub fn delete_bins(&mut self, bin_indices: PyReadonlyArray1) { - self.grid.delete_bins(&bin_indices.to_vec().unwrap()) + self.grid.delete_bins(&bin_indices.to_vec().unwrap()); } } /// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let m = PyModule::new_bound(parent_module.py(), "grid")?; m.setattr(pyo3::intern!(m.py(), "__doc__"), "Grid interface.")?; diff --git a/pineappl_py/src/import_only_subgrid.rs b/pineappl_py/src/import_only_subgrid.rs deleted file mode 100644 index b58a006c..00000000 --- a/pineappl_py/src/import_only_subgrid.rs +++ /dev/null @@ -1,169 +0,0 @@ -//! PyImportOnlySubgrid* interface. - -use super::subgrid::PySubgridEnum; -use numpy::{PyArrayMethods, PyReadonlyArray1, PyReadonlyArray3}; -use pineappl::import_only_subgrid::ImportOnlySubgridV1; -use pineappl::import_only_subgrid::ImportOnlySubgridV2; -use pineappl::sparse_array3::SparseArray3; -use pineappl::subgrid::Mu2; -use pyo3::prelude::*; - -/// PyO3 wrapper to :rustdoc:`pineappl::import_only_subgrid::ImportOnlySubgridV2 `. -#[pyclass(name = "ImportOnlySubgridV2")] -#[derive(Clone)] -#[repr(transparent)] -pub struct PyImportOnlySubgridV2 { - pub(crate) import_only_subgrid: ImportOnlySubgridV2, -} - -#[pymethods] -impl PyImportOnlySubgridV2 { - /// Constructor. - /// - /// Parameters - /// ---------- - /// array : numpy.ndarray(float) - /// 3D array with all weights - /// mu2_grid : list(tuple(float, float)) - /// scales grid - /// x1_grid : list(float) - /// first momentum fraction grid - /// x2_grid : list(float) - /// second momentum fraction grid - #[new] - pub fn new( - array: PyReadonlyArray3, - mu2_grid: Vec<(f64, f64)>, - x1_grid: PyReadonlyArray1, - x2_grid: PyReadonlyArray1, - ) -> Self { - let mut sparse_array = SparseArray3::new( - mu2_grid.len(), - x1_grid.as_slice().unwrap().len(), - x2_grid.as_slice().unwrap().len(), - ); - - for ((imu2, ix1, ix2), value) in array - .as_array() - .indexed_iter() - .filter(|((_, _, _), value)| **value != 0.0) - { - sparse_array[[imu2, ix1, ix2]] = *value; - } - Self { - import_only_subgrid: ImportOnlySubgridV2::new( - sparse_array, - mu2_grid - .iter() - .map(|(ren, fac)| Mu2 { - ren: *ren, - fac: *fac, - }) - .collect(), - x1_grid.to_vec().unwrap(), - x2_grid.to_vec().unwrap(), - ), - } - } - - /// Wrapper to match :meth:`pineappl.pineappl.PyGrid.set_subgrid()`. - /// - /// Returns - /// ------- - /// PySubgridEnum : - /// casted object - pub fn into(&self) -> PySubgridEnum { - PySubgridEnum { - subgrid_enum: self.import_only_subgrid.clone().into(), - } - } -} - -/// PyO3 wrapper to :rustdoc:`pineappl::import_only_subgrid::ImportOnlySubgridV1 `. -#[pyclass(name = "ImportOnlySubgridV1")] -#[derive(Clone)] -#[repr(transparent)] -pub struct PyImportOnlySubgridV1 { - pub(crate) import_only_subgrid: ImportOnlySubgridV1, -} - -impl PyImportOnlySubgridV1 { - pub(crate) fn new(import_only_subgrid: ImportOnlySubgridV1) -> Self { - Self { - import_only_subgrid, - } - } -} - -#[pymethods] -impl PyImportOnlySubgridV1 { - /// Constructor. - /// - /// Parameters - /// ---------- - /// array : numpy.ndarray(float) - /// 3D array with all weights - /// mu2_grid : list(tuple(float, float)) - /// scales grid - /// x1_grid : list(float) - /// first momentum fraction grid - /// x2_grid : list(float) - /// second momentum fraction grid - #[new] - pub fn new_import_only_subgrid( - array: PyReadonlyArray3, - q2_grid: PyReadonlyArray1, - x1_grid: PyReadonlyArray1, - x2_grid: PyReadonlyArray1, - ) -> Self { - let mut sparse_array = SparseArray3::new( - q2_grid.as_slice().unwrap().len(), - x1_grid.as_slice().unwrap().len(), - x2_grid.as_slice().unwrap().len(), - ); - - for ((iq2, ix1, ix2), value) in array - .as_array() - .indexed_iter() - .filter(|((_, _, _), value)| **value != 0.0) - { - sparse_array[[iq2, ix1, ix2]] = *value; - } - - Self::new(ImportOnlySubgridV1::new( - sparse_array, - q2_grid.to_vec().unwrap(), - x1_grid.to_vec().unwrap(), - x2_grid.to_vec().unwrap(), - )) - } - - /// Wrapper to match :meth:`pineappl.pineappl.PyGrid.set_subgrid()`. - /// - /// Returns - /// ------- - /// PySubgridEnum : - /// casted object - pub fn into(&self) -> PySubgridEnum { - PySubgridEnum { - subgrid_enum: self.import_only_subgrid.clone().into(), - } - } -} - -/// Register submodule in parent. -pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { - let m = PyModule::new_bound(parent_module.py(), "import_only_subgrid")?; - m.setattr( - pyo3::intern!(m.py(), "__doc__"), - "ImportOnlySubgrid* interface.", - )?; - pyo3::py_run!( - parent_module.py(), - m, - "import sys; sys.modules['pineappl.import_only_subgrid'] = m" - ); - m.add_class::()?; - m.add_class::()?; - parent_module.add_submodule(&m) -} diff --git a/pineappl_py/src/interpolation.rs b/pineappl_py/src/interpolation.rs new file mode 100644 index 00000000..e1e9a53e --- /dev/null +++ b/pineappl_py/src/interpolation.rs @@ -0,0 +1,51 @@ +//! Interpolation interface. + +use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; +use pyo3::{prelude::*, pyclass}; + +/// PyO3 wrapper to :rustdoc:`pineappl::`. +#[pyclass(name = "Interp")] +#[repr(transparent)] +pub struct PyInterp { + pub(crate) interp: Interp, +} + +impl PyInterp { + pub(crate) const fn new(interp: Interp) -> Self { + Self { interp } + } +} + +#[pymethods] +impl PyInterp { + /// Constructor. + #[new] + #[must_use] + pub fn new_interp(min: f64, max: f64, nodes: usize, order: usize) -> Self { + Self::new(Interp::new( + min, + max, + nodes, + order, + ReweightMeth::ApplGridX, + Map::ApplGridF2, + InterpMeth::Lagrange, + )) + } +} + +/// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. +pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { + let m = PyModule::new_bound(parent_module.py(), "interpolation")?; + m.setattr(pyo3::intern!(m.py(), "__doc__"), "Interpolation submodule.")?; + pyo3::py_run!( + parent_module.py(), + m, + "import sys; sys.modules['vector_length_module.interpolation'] = m" + ); + m.add_class::()?; + parent_module.add_submodule(&m) +} diff --git a/pineappl_py/src/lib.rs b/pineappl_py/src/lib.rs index 5c0c5e1d..f3198d0a 100644 --- a/pineappl_py/src/lib.rs +++ b/pineappl_py/src/lib.rs @@ -6,23 +6,25 @@ use pyo3::prelude::*; pub mod bin; pub mod boc; +pub mod convolutions; pub mod evolution; pub mod fk_table; pub mod grid; -pub mod import_only_subgrid; +pub mod interpolation; pub mod pids; pub mod subgrid; /// PyO3 Python module that contains all exposed classes from Rust. #[pymodule] -fn pineappl(m: &Bound<'_, PyModule>) -> PyResult<()> { +fn vector_length_module(m: &Bound<'_, PyModule>) -> PyResult<()> { bin::register(m)?; boc::register(m)?; - grid::register(m)?; - import_only_subgrid::register(m)?; evolution::register(m)?; - fk_table::register(m)?; + convolutions::register(m)?; + grid::register(m)?; + interpolation::register(m)?; pids::register(m)?; + fk_table::register(m)?; subgrid::register(m)?; m.add("version", env!("CARGO_PKG_VERSION"))?; diff --git a/pineappl_py/src/pids.rs b/pineappl_py/src/pids.rs index 81b3b67e..e8fdfadd 100644 --- a/pineappl_py/src/pids.rs +++ b/pineappl_py/src/pids.rs @@ -23,6 +23,9 @@ impl From for PidBasis { } /// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let m = PyModule::new_bound(parent_module.py(), "pids")?; m.setattr(pyo3::intern!(m.py(), "__doc__"), "PIDs interface.")?; diff --git a/pineappl_py/src/subgrid.rs b/pineappl_py/src/subgrid.rs index 5eb89a78..7a15ecc1 100644 --- a/pineappl_py/src/subgrid.rs +++ b/pineappl_py/src/subgrid.rs @@ -1,120 +1,9 @@ //! Subgrid interface. -use ndarray::Array3; -use numpy::{IntoPyArray, PyArray1, PyArray3}; use pineappl::subgrid::Mu2; -use pineappl::subgrid::{Subgrid, SubgridEnum, SubgridParams}; +use pineappl::subgrid::{Subgrid, SubgridEnum}; use pyo3::prelude::*; -/// PyO3 wrapper to :rustdoc:`pineappl::subgrid::SubgridParams ` -#[pyclass(name = "SubgridParams")] -#[derive(Clone)] -#[repr(transparent)] -pub struct PySubgridParams { - pub(crate) subgrid_params: SubgridParams, -} - -#[pymethods] -impl PySubgridParams { - /// Constructor using the defaults. - #[new] - pub fn default() -> Self { - Self { - subgrid_params: SubgridParams::default(), - } - } - - /// Set number of :math:`Q^2` bins. - /// - /// Parameters - /// ---------- - /// q2_bins : int - /// number of bins - pub fn set_q2_bins(&mut self, q2_bins: usize) { - self.subgrid_params.set_q2_bins(q2_bins); - } - - /// Set the upper limit for :math:`Q^2`. - /// - /// Parameters - /// ---------- - /// q2_max: float - /// new `q2_max` - pub fn set_q2_max(&mut self, q2_max: f64) { - self.subgrid_params.set_q2_max(q2_max); - } - - /// Set the lower limit for :math:`Q^2`. - /// - /// Parameters - /// ---------- - /// q2_min: float - /// new `q2_min` - pub fn set_q2_min(&mut self, q2_min: f64) { - self.subgrid_params.set_q2_min(q2_min); - } - - /// Set interpolation order for :math:`Q^2_{grid}`. - /// - /// Parameters - /// ---------- - /// q2_order : float - /// new `q2_order` - pub fn set_q2_order(&mut self, q2_order: usize) { - self.subgrid_params.set_q2_order(q2_order); - } - - /// Set reweighting. - /// - /// Parameters - /// ---------- - /// reweight : bool - /// apply reweighting? - pub fn set_reweight(&mut self, reweight: bool) { - self.subgrid_params.set_reweight(reweight); - } - - /// Set number of x bins. - /// - /// Parameters - /// ---------- - /// x_bins : int - /// number of bins - pub fn set_x_bins(&mut self, x_bins: usize) { - self.subgrid_params.set_x_bins(x_bins); - } - - /// Set :math:`x_{max}`. - /// - /// Parameters - /// ---------- - /// x_max : float - /// new `x_max` - pub fn set_x_max(&mut self, x_max: f64) { - self.subgrid_params.set_x_max(x_max); - } - - /// Set :math:`x_{min}`. - /// - /// Parameters - /// ---------- - /// x_min : float - /// new `x_min` - pub fn set_x_min(&mut self, x_min: f64) { - self.subgrid_params.set_x_min(x_min); - } - - /// Set interpolation order for :math:`x_{grid}`. - /// - /// Parameters - /// ---------- - /// x_order : float - /// new `x_order` - pub fn set_x_order(&mut self, x_order: usize) { - self.subgrid_params.set_x_order(x_order); - } -} - /// PyO3 wrapper to :rustdoc:`pineappl::subgrid::Mu2 ` #[pyclass(name = "Mu2")] #[repr(transparent)] @@ -132,33 +21,34 @@ impl PyMu2 { /// renormalization scale /// fac : float /// factorization scale + /// frg : float + /// fragmentation scale #[new] - pub fn new(ren: f64, fac: f64) -> Self { + #[must_use] + pub const fn new(ren: f64, fac: f64, frg: f64) -> Self { Self { - mu2: Mu2 { ren, fac }, + mu2: Mu2 { ren, fac, frg }, } } #[getter] - fn ren(&self) -> PyResult { - Ok(self.mu2.ren) + const fn ren(&self) -> f64 { + self.mu2.ren } #[setter] - fn set_ren(&mut self, value: f64) -> PyResult<()> { + fn set_ren(&mut self, value: f64) { self.mu2.ren = value; - Ok(()) } #[getter] - fn fac(&self) -> PyResult { - Ok(self.mu2.fac) + const fn fac(&self) -> f64 { + self.mu2.fac } #[setter] - fn set_fac(&mut self, value: f64) -> PyResult<()> { + fn set_fac(&mut self, value: f64) { self.mu2.fac = value; - Ok(()) } } @@ -182,38 +72,18 @@ impl PySubgridEnum { self.subgrid_enum.scale(factor); } - /// Return the dense array of the subgrid. - pub fn to_array3<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray3> { - Array3::from(&self.subgrid_enum).into_pyarray_bound(py) - } - /// Clone. + #[must_use] pub fn into(&self) -> Self { self.clone() } - - /// Return the array of mu2 objects. - pub fn mu2_grid(&self) -> Vec { - self.subgrid_enum - .mu2_grid() - .iter() - .cloned() - .map(|mu2| PyMu2 { mu2 }) - .collect() - } - - /// Return the array of x1. - pub fn x1_grid<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1> { - PyArray1::from_slice_bound(py, &self.subgrid_enum.x1_grid()) - } - - /// Return the array of x2. - pub fn x2_grid<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1> { - PyArray1::from_slice_bound(py, &self.subgrid_enum.x2_grid()) - } } /// Register submodule in parent. +/// +/// # Errors +/// +/// Raises Errors if (sub-)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let m = PyModule::new_bound(parent_module.py(), "subgrid")?; m.setattr(pyo3::intern!(m.py(), "__doc__"), "Subgrid interface.")?; @@ -223,7 +93,6 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { "import sys; sys.modules['pineappl.subgrid'] = m" ); m.add_class::()?; - m.add_class::()?; m.add_class::()?; parent_module.add_submodule(&m) } From c3968457b621b49c03ce9a0d2afc85c7069d031b Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Wed, 16 Oct 2024 20:45:36 +0200 Subject: [PATCH 168/213] Fix `pineappl` Python module --- pineappl_py/src/boc.rs | 32 +++++++++++++++++++++++++++++++- pineappl_py/src/grid.rs | 7 ++++--- pineappl_py/src/lib.rs | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pineappl_py/src/boc.rs b/pineappl_py/src/boc.rs index e3336d98..bec26ca6 100644 --- a/pineappl_py/src/boc.rs +++ b/pineappl_py/src/boc.rs @@ -1,7 +1,7 @@ //! Interface for bins, orders and channels. use numpy::{IntoPyArray, PyArray1}; -use pineappl::boc::{Channel, Order}; +use pineappl::boc::{Channel, Kinematics, Order}; use pyo3::prelude::*; /// PyO3 wrapper to :rustdoc:`pineappl::boc::Channel `. @@ -45,6 +45,35 @@ impl PyChannel { } } +/// PyO3 wrapper to :rustdoc:`pineappl`. +#[pyclass(name = "Kinematics")] +#[repr(transparent)] +pub struct PyKinematics { + pub(crate) kinematics: Kinematics, +} + +impl PyKinematics { + pub(crate) const fn new(kinematics: Kinematics) -> Self { + Self { kinematics } + } +} + +#[pymethods] +impl PyKinematics { + /// Constructor. + #[new] + #[must_use] + pub const fn new_kin(kinematic: usize) -> Self { + let kins = match kinematic { + 0 => Kinematics::Scale(0), + 1 => Kinematics::X(0), + 2 => Kinematics::X(1), + _ => todo!(), + }; + Self::new(kins) + } +} + /// PyO3 wrapper to :rustdoc:`pineappl::boc::Order `. #[pyclass(name = "Order")] #[repr(transparent)] @@ -152,5 +181,6 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { ); m.add_class::()?; m.add_class::()?; + m.add_class::()?; parent_module.add_submodule(&m) } diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 8eec0bbe..96bde71a 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -1,7 +1,7 @@ //! Grid interface. use super::bin::PyBinRemapper; -use super::boc::{PyChannel, PyOrder}; +use super::boc::{PyChannel, PyKinematics, PyOrder}; use super::convolutions::PyConv; use super::evolution::{PyEvolveInfo, PyOperatorSliceInfo}; use super::fk_table::PyFkTable; @@ -9,7 +9,7 @@ use super::interpolation::PyInterp; use super::pids::PyPidBasis; use ndarray::CowArray; use numpy::{IntoPyArray, PyArray1, PyArrayMethods, PyReadonlyArray1, PyReadonlyArray4}; -use pineappl::boc::{Kinematics, ScaleFuncForm, Scales}; +use pineappl::boc::{ScaleFuncForm, Scales}; use pineappl::convolutions::ConvolutionCache; use pineappl::evolution::AlphasTable; use pineappl::grid::Grid; @@ -58,6 +58,7 @@ impl PyGrid { bin_limits: PyReadonlyArray1, convolutions: Vec>, interpolations: Vec>, + kinematics: Vec>, ) -> Self { Self { grid: Grid::new( @@ -70,7 +71,7 @@ impl PyGrid { .iter() .map(|pyi| pyi.interp.clone()) .collect(), - vec![Kinematics::Scale(0), Kinematics::X1, Kinematics::X2], + kinematics.iter().map(|pyk| pyk.kinematics).collect(), Scales { ren: ScaleFuncForm::Scale(0), fac: ScaleFuncForm::Scale(0), diff --git a/pineappl_py/src/lib.rs b/pineappl_py/src/lib.rs index f3198d0a..1b0d5ab2 100644 --- a/pineappl_py/src/lib.rs +++ b/pineappl_py/src/lib.rs @@ -16,7 +16,7 @@ pub mod subgrid; /// PyO3 Python module that contains all exposed classes from Rust. #[pymodule] -fn vector_length_module(m: &Bound<'_, PyModule>) -> PyResult<()> { +fn pineappl(m: &Bound<'_, PyModule>) -> PyResult<()> { bin::register(m)?; boc::register(m)?; evolution::register(m)?; From b140e6598d4a3b6970753feb302311e07f6d784b Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Wed, 16 Oct 2024 22:03:45 +0200 Subject: [PATCH 169/213] Start fixing Python tests --- pineappl_py/src/boc.rs | 7 ++- pineappl_py/src/convolutions.rs | 2 +- pineappl_py/src/interpolation.rs | 2 +- pineappl_py/tests/test_grid.py | 104 ++++++++++++++++++++++--------- 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/pineappl_py/src/boc.rs b/pineappl_py/src/boc.rs index bec26ca6..83f47eea 100644 --- a/pineappl_py/src/boc.rs +++ b/pineappl_py/src/boc.rs @@ -121,13 +121,16 @@ impl PyOrder { /// power of :math:`\ln(\xi_r)` /// logxif : int /// power of :math:`\ln(\xi_f)` + /// logxia : int + /// power of :math:`\ln(\xi_a)` #[must_use] - pub const fn as_tuple(&self) -> (u32, u32, u32, u32) { + pub const fn as_tuple(&self) -> (u32, u32, u32, u32, u32) { ( self.order.alphas, self.order.alpha, self.order.logxir, self.order.logxif, + self.order.logxia, ) } @@ -177,7 +180,7 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { pyo3::py_run!( parent_module.py(), m, - "import sys; sys.modules['pineappl.channel'] = m" + "import sys; sys.modules['pineappl.boc'] = m" ); m.add_class::()?; m.add_class::()?; diff --git a/pineappl_py/src/convolutions.rs b/pineappl_py/src/convolutions.rs index 71e7e77a..2f91d30a 100644 --- a/pineappl_py/src/convolutions.rs +++ b/pineappl_py/src/convolutions.rs @@ -63,7 +63,7 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { pyo3::py_run!( parent_module.py(), m, - "import sys; sys.modules['vector_length_module.convolutions'] = m" + "import sys; sys.modules['pineappl.convolutions'] = m" ); m.add_class::()?; m.add_class::()?; diff --git a/pineappl_py/src/interpolation.rs b/pineappl_py/src/interpolation.rs index e1e9a53e..1ff5f246 100644 --- a/pineappl_py/src/interpolation.rs +++ b/pineappl_py/src/interpolation.rs @@ -44,7 +44,7 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { pyo3::py_run!( parent_module.py(), m, - "import sys; sys.modules['vector_length_module.interpolation'] = m" + "import sys; sys.modules['pineappl.interpolation'] = m" ); m.add_class::()?; parent_module.add_submodule(&m) diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index f0678db9..cd00f34e 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -2,32 +2,78 @@ import pytest import pineappl +from pineappl.pids import PidBasis +from pineappl.boc import Channel, Kinematics +from pineappl.grid import Order, Grid +from pineappl.convolutions import Conv, ConvType +from pineappl.interpolation import Interp class TestOrder: def test_init(self): - args = (2, 1, 0, 1) + args = (2, 1, 0, 1, 0) o = pineappl.grid.Order(*args) - assert isinstance(o, pineappl.grid.Order) + assert isinstance(o, Order) assert o.as_tuple() == args class TestGrid: def fake_grid(self, bins=None): - channels = [pineappl.boc.Channel([(1, 21, 0.1)])] - orders = [pineappl.grid.Order(3, 0, 0, 0)] + channels = [Channel([([1, 21], 0.1)])] + orders = [Order(3, 0, 0, 0, 0)] + # Construct the type of convolutions and the convolution object + convtype = ConvType(polarized=False, time_like=False) + conv = Conv(conv_type=convtype, pid=2212) + # We assume symmetrical proton-proton in the initial state + convolutions = [conv, conv] + # Define the kinematics. Kinematics are defined as a list of items. + # 1st item: factorization and renormalization scale + # 2nd item: parton momentum fraction of the 1st convolution + # 3rd tiem: parton momentum fraction of the 2nd convolution + kinematics = [ + Kinematics(0), # Scale + Kinematics(1), # x1 momentum fraction + Kinematics(2), # x2 momentum fraction + ] + # Define the interpolation specs for each item of the Kinematics + interpolations = [ + Interp( + min=1e2, + max=1e8, + nodes=40, + order=3, + ), # Interpolation on the Scale + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x1 momentum fraction + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x2 momentum fraction + ] bin_limits = np.array([1e-7, 1e-3, 1] if bins is None else bins, dtype=float) - subgrid_params = pineappl.subgrid.SubgridParams() - g = pineappl.grid.Grid(channels, orders, bin_limits, subgrid_params) + g = pineappl.grid.Grid( + pid_basis=PidBasis.Evol, + channels=channels, + orders=orders, + bin_limits=bin_limits, + convolutions=convolutions, + interpolations=interpolations, + kinematics=kinematics, + ) return g def test_init(self): g = self.fake_grid() - assert isinstance(g, pineappl.grid.Grid) - # orders + assert isinstance(g, Grid) assert len(g.orders()) == 1 - assert g.orders()[0].as_tuple() == (3, 0, 0, 0) + assert g.orders()[0].as_tuple() == (3, 0, 0, 0, 0) def test_set_subgrid(self): g = self.fake_grid() @@ -116,28 +162,26 @@ def test_io(self, tmp_path): def test_fill(self): g = self.fake_grid() - g.fill(0.5, 0.5, 10.0, 0, 0.01, 0, 10.0) - res = g.convolve_with_one(2212, lambda pid, x, q2: x, lambda q2: 1.0) - pytest.approx(res) == 0.0 - - def test_fill_array(self): - g = self.fake_grid() - g.fill_array( - np.array([0.5, 1.0]), - np.array([0.5, 1.0]), - np.array([0.5, 1.0]), - 0, - np.array([1e-3, 1e-2]), - 0, - np.array([10.0, 100.0]), + # Fill the Grid with some values + n_tuple = [0.5, 0.5, 10.0] + g.fill( + order=0, + observable=0.01, + channel=0, + ntuple=n_tuple, + weight=10, + ) + # Construct the type of convolutions and the convolution object + convtype = ConvType(polarized=False, time_like=False) + conv = Conv(conv_type=convtype, pid=2212) + # Peform convolutions using Toy LHPDF & AlphasQ2 functions + res = g.convolve_with_two( + pdg_conv1=conv, + xfx1=lambda pid, x, q2: x, + pdg_conv2=conv, + xfx2=lambda pid, x, q2: x, + alphas=lambda q2: 1.0, ) - res = g.convolve_with_one(2212, lambda pid, x, q2: x, lambda q2: 1.0) - pytest.approx(res) == 0.0 - - def test_fill_all(self): - g = self.fake_grid() - g.fill_all(1.0, 1.0, 1.0, 0, 1e-2, np.array([10.0])) - res = g.convolve_with_one(2212, lambda pid, x, q2: x, lambda q2: 1.0) pytest.approx(res) == 0.0 def test_merge(self): From e32f543058dd9cb2df319cc926068bfc2361ec7a Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Wed, 16 Oct 2024 23:42:44 +0200 Subject: [PATCH 170/213] Add preliminary Python interface to `packed_subgrid` --- pineappl_py/src/grid.rs | 20 +++++++ pineappl_py/src/lib.rs | 1 + pineappl_py/src/packed_subgrid.rs | 87 +++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 pineappl_py/src/packed_subgrid.rs diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 96bde71a..21f6cd81 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -7,6 +7,7 @@ use super::evolution::{PyEvolveInfo, PyOperatorSliceInfo}; use super::fk_table::PyFkTable; use super::interpolation::PyInterp; use super::pids::PyPidBasis; +use super::subgrid::PySubgridEnum; use ndarray::CowArray; use numpy::{IntoPyArray, PyArray1, PyArrayMethods, PyReadonlyArray1, PyReadonlyArray4}; use pineappl::boc::{ScaleFuncForm, Scales}; @@ -107,6 +108,25 @@ impl PyGrid { self.grid.fill(order, observable, channel, &ntuple, weight); } + /// Retrieve a subgrid. + #[must_use] + pub fn subgrid(&self, order: usize, bin: usize, channel: usize) -> PySubgridEnum { + PySubgridEnum { + subgrid_enum: self.grid.subgrids()[[order, bin, channel]].clone(), + } + } + + /// Set a subgrid. + pub fn set_subgrid( + &mut self, + order: usize, + bin: usize, + channel: usize, + subgrid: PySubgridEnum, + ) { + self.grid.subgrids_mut()[[order, bin, channel]] = subgrid.subgrid_enum; + } + /// Set the bin normalizations. /// /// # Panics diff --git a/pineappl_py/src/lib.rs b/pineappl_py/src/lib.rs index 1b0d5ab2..4874f073 100644 --- a/pineappl_py/src/lib.rs +++ b/pineappl_py/src/lib.rs @@ -11,6 +11,7 @@ pub mod evolution; pub mod fk_table; pub mod grid; pub mod interpolation; +pub mod packed_subgrid; pub mod pids; pub mod subgrid; diff --git a/pineappl_py/src/packed_subgrid.rs b/pineappl_py/src/packed_subgrid.rs new file mode 100644 index 00000000..1bf8fc8a --- /dev/null +++ b/pineappl_py/src/packed_subgrid.rs @@ -0,0 +1,87 @@ +//! PyPackedSubgrid* interface. + +use super::subgrid::PySubgridEnum; +use numpy::PyReadonlyArray3; +use pineappl::packed_array::PackedArray; +use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::subgrid::NodeValues; +use pyo3::prelude::*; + +/// PyO3 wrapper to :rustdoc:`pineappl`. +#[pyclass(name = "PyPackedSubgrid")] +#[derive(Clone)] +#[repr(transparent)] +pub struct PyPackedSubgrid { + pub(crate) packed_subgrid: PackedQ1X2SubgridV1, +} + +#[pymethods] +impl PyPackedSubgrid { + /// Constructor. + /// Constructor. + /// + /// Parameters + /// ---------- + /// array : numpy.ndarray(float) + /// 3D array with all weights + /// scales : list(float) + /// scales grid + /// x1_grid : list(float) + /// first momentum fraction grid + /// x2_grid : list(float) + /// second momentum fraction grid + #[new] + pub fn new( + array: PyReadonlyArray3, + scales: Vec, + x1_grid: Vec, + x2_grid: Vec, + ) -> Self { + let node_values: Vec = vec![ + NodeValues::UseThese(scales), + NodeValues::UseThese(x1_grid), + NodeValues::UseThese(x2_grid), + ]; + let mut sparse_array: PackedArray = + PackedArray::new(node_values.iter().map(NodeValues::len).collect()); + + for ((iscale, ix1, ix2), value) in array + .as_array() + .indexed_iter() + .filter(|((_, _, _), value)| **value != 0.0) + { + sparse_array[[iscale, ix1, ix2]] = *value; + } + + Self { + packed_subgrid: PackedQ1X2SubgridV1::new(sparse_array, node_values), + } + } + + /// TODO + #[must_use] + pub fn into(&self) -> PySubgridEnum { + PySubgridEnum { + subgrid_enum: self.packed_subgrid.clone().into(), + } + } +} + +/// Register submodule in parent. +/// # Errors +/// +/// Raises an error if (sub)module is not found. +pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { + let m = PyModule::new_bound(parent_module.py(), "packed_subgrid")?; + m.setattr( + pyo3::intern!(m.py(), "__doc__"), + "Interface for packed subgrid specs.", + )?; + pyo3::py_run!( + parent_module.py(), + m, + "import sys; sys.modules['pineappl.packed_subgrid'] = m" + ); + m.add_class::()?; + parent_module.add_submodule(&m) +} From d13e3254c991637cb76c7d1a956c91fae4a4863b Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Oct 2024 10:29:33 +0200 Subject: [PATCH 171/213] Simplify implementation of `ConvolutionCache::setup` --- pineappl/src/convolutions.rs | 124 +++++++---------------------------- 1 file changed, 25 insertions(+), 99 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 1b1f6810..28d4cc06 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -4,6 +4,7 @@ use super::boc::Kinematics; use super::grid::Grid; use super::pids; use super::subgrid::{NodeValues, Subgrid}; +use itertools::Itertools; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; @@ -82,123 +83,48 @@ impl<'a> ConvolutionCache<'a> { let mut x_grid: Vec<_> = grid .subgrids() .iter() - .filter_map(|subgrid| { - if subgrid.is_empty() { - None - } else { - let vec: Vec<_> = grid - .kinematics() - .iter() - .zip(subgrid.node_values()) - .filter(|(kin, _)| matches!(kin, Kinematics::X(_))) - .flat_map(|(_, node_values)| node_values.values()) - .collect(); - Some(vec) - } + .filter(|subgrid| !subgrid.is_empty()) + .flat_map(|subgrid| { + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .filter(|(kin, _)| matches!(kin, Kinematics::X(_))) + .flat_map(|(_, node_values)| node_values.values()) }) - .flatten() .collect(); x_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); x_grid.dedup(); - let mut mur2_grid: Vec<_> = grid + let (mut mur2_grid, mut muf2_grid, mut mua2_grid): (Vec<_>, Vec<_>, Vec<_>) = grid .subgrids() .iter() - .filter_map(|subgrid| { - if subgrid.is_empty() { - None - } else { - Some( - grid.kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - // TODO: generalize this for arbitrary scales - matches!(kin, &Kinematics::Scale(idx) if idx == 0) - .then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values(), - ) - } + .filter(|subgrid| !subgrid.is_empty()) + .flat_map(|subgrid| { + grid.kinematics() + .iter() + .zip(subgrid.node_values()) + .find_map(|(kin, node_values)| { + // TODO: generalize this for arbitrary scales + matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) + }) + // UNWRAP: the `Grid` constructor should guarantee all scales are available + .unwrap_or_else(|| unreachable!()) + .values() }) - .flatten() - .flat_map(|ren| { + .flat_map(|val| { xi.iter() - .map(|(xir, _, _)| xir * xir * ren) - .collect::>() + .map(move |(xir, xif, xia)| (xir * xir * val, xif * xif * val, xia * xia * val)) }) - .collect(); + .multiunzip(); + mur2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); mur2_grid.dedup(); - - let mut muf2_grid: Vec<_> = grid - .subgrids() - .iter() - .filter_map(|subgrid| { - if subgrid.is_empty() { - None - } else { - Some( - grid.kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - // TODO: generalize this for arbitrary scales - matches!(kin, &Kinematics::Scale(idx) if idx == 0) - .then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values(), - ) - } - }) - .flatten() - .flat_map(|fac| { - xi.iter() - .map(|(_, xif, _)| xif * xif * fac) - .collect::>() - }) - .collect(); muf2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); muf2_grid.dedup(); - - let mut mua2_grid: Vec<_> = grid - .subgrids() - .iter() - .filter_map(|subgrid| { - if subgrid.is_empty() { - None - } else { - Some( - grid.kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - // TODO: generalize this for arbitrary scales - matches!(kin, &Kinematics::Scale(idx) if idx == 0) - .then_some(node_values) - }) - // TODO: convert this into an error - .unwrap() - .values(), - ) - } - }) - .flatten() - .flat_map(|frg| { - xi.iter() - .map(|(_, _, xia)| xia * xia * frg) - .collect::>() - }) - .collect(); mua2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); mua2_grid.dedup(); self.alphas_cache = mur2_grid.iter().map(|&mur2| (self.alphas)(mur2)).collect(); - self.mur2_grid = mur2_grid; self.muf2_grid = muf2_grid; self.mua2_grid = mua2_grid; From d72820256245468d25478100db0bbaeb01ecd70f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Oct 2024 11:32:45 +0200 Subject: [PATCH 172/213] Merge alphas evaluation into other function --- pineappl/src/convolutions.rs | 12 +++--------- pineappl/src/grid.rs | 13 ++----------- pineappl_cli/tests/evolve.rs | 12 ++++++------ pineappl_cli/tests/export.rs | 10 +++++----- pineappl_cli/tests/import.rs | 12 ++++++------ pineappl_cli/tests/write.rs | 8 ++++---- 6 files changed, 26 insertions(+), 41 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 28d4cc06..b8947160 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -134,7 +134,7 @@ impl<'a> ConvolutionCache<'a> { } /// TODO - pub fn fx_prod(&mut self, pdg_ids: &[i32], indices: &[usize]) -> f64 { + pub fn as_fx_prod(&mut self, pdg_ids: &[i32], as_order: u32, indices: &[usize]) -> f64 { // TODO: here we assume that // - indices[0] is the (squared) factorization scale, // - indices[1] is x1 and @@ -169,14 +169,8 @@ impl<'a> ConvolutionCache<'a> { xfx(pid, x, mu2) / x }) }) - .product() - } - - /// Return the strong coupling for the renormalization scale set with [`LumiCache::set_grids`], - /// in the grid `mu2_grid` at the index `imu2`. - #[must_use] - pub fn alphas(&self, imu2: usize) -> f64 { - self.alphas_cache[self.imur2[imu2]] + .product::() + * self.alphas_cache[self.imur2[indices[0]]].powi(as_order.try_into().unwrap()) } /// Clears the cache. diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 19ddcb06..599f32b9 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -295,15 +295,10 @@ impl Grid { for entry in channel.entry() { // TODO: we assume `idx` to be ordered as scale, x1, x2 - let fx_prod = convolution_cache.fx_prod(&entry.0, &idx); + let fx_prod = convolution_cache.as_fx_prod(&entry.0, order.alphas, &idx); lumi += fx_prod * entry.1; } - // TODO: we assume `idx` to be ordered as scale, x1, x2 - let alphas = convolution_cache.alphas(idx[0]); - - lumi *= alphas.powi(order.alphas.try_into().unwrap()); - value += lumi * v; } @@ -371,14 +366,10 @@ impl Grid { for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); // TODO: we assume `idx` to be ordered as scale, x1, x2 - let fx_prod = convolution_cache.fx_prod(&entry.0, &idx); + let fx_prod = convolution_cache.as_fx_prod(&entry.0, order.alphas, &idx); lumi += fx_prod * entry.1; } - let alphas = convolution_cache.alphas(idx[0]); - - lumi *= alphas.powi(order.alphas.try_into().unwrap()); - array[idx.as_slice()] = lumi * value; } diff --git a/pineappl_cli/tests/evolve.rs b/pineappl_cli/tests/evolve.rs index 4f50125c..b56e4868 100644 --- a/pineappl_cli/tests/evolve.rs +++ b/pineappl_cli/tests/evolve.rs @@ -74,32 +74,32 @@ const LHCB_WP_7TEV_V2_STR: &str = "b Grid FkTable 2 6.2322357391848550e2 6.2306009928459105e2 -2.6230495882340055e-4 3 5.0216762988872927e2 5.0203737363369049e2 -2.5938799573288485e-4 4 3.7314505699003132e2 3.7305089832847727e2 -2.5233795755885691e-4 -5 2.5302044227292129e2 2.5295968261889854e2 -2.4013733229188983e-4 +5 2.5302044227292134e2 2.5295968261889854e2 -2.4013733229211187e-4 6 1.1971045984774410e2 1.1968525412249534e2 -2.1055574659745169e-4 -7 2.9272102213930090e1 2.9268443366651130e1 -1.2499434622836869e-4 +7 2.9272102213930097e1 2.9268443366651130e1 -1.2499434622859074e-4 "; const LHCB_WP_7TEV_V2_XIR2_STR: &str = "b Grid FkTable rel. diff -+--------------------+--------------------+---------------------- 0 7.7634833292737017e2 7.7614037816519419e2 -2.6786270203205120e-4 -1 7.0866199875124983e2 7.0847444839781758e2 -2.6465417048282536e-4 +1 7.0866199875124971e2 7.0847444839781758e2 -2.6465417048271433e-4 2 6.1427556024981789e2 6.1411417374531095e2 -2.6272655946346646e-4 3 4.9482819982783735e2 4.9469964081143053e2 -2.5980535557912354e-4 4 3.6756257449354945e2 3.6746967569489709e2 -2.5274281196974169e-4 5 2.4912642701834142e2 2.4906651029915440e2 -2.4050727939273209e-4 6 1.1776254040032327e2 1.1773772039493414e2 -2.1076316207813139e-4 -7 2.8749891297668267e1 2.8746299479656273e1 -1.2493327278373378e-4 +7 2.8749891297668260e1 2.8746299479656273e1 -1.2493327278351174e-4 "; const LHCB_WP_7TEV_V2_XIF_2_STR: &str = "b Grid FkTable rel. diff -+--------------------+--------------------+---------------------- 0 8.0902449713533770e2 8.0880109089579207e2 -2.7614273774978493e-4 -1 7.3869242569893413e2 7.3849113100483896e2 -2.7250136469814112e-4 +1 7.3869242569893402e2 7.3849113100483896e2 -2.7250136469803010e-4 2 6.4102495904778243e2 6.4085178025871448e2 -2.7015919836448354e-4 3 5.1668563837653949e2 5.1654786167667771e2 -2.6665478896348294e-4 4 3.8405066991124284e2 3.8395127677619655e2 -2.5880213949180941e-4 -5 2.6047697125229377e2 2.6041295913273854e2 -2.4574963094614599e-4 +5 2.6047697125229382e2 2.6041295913273854e2 -2.4574963094636804e-4 6 1.2324364745022301e2 1.2321715784184289e2 -2.1493690691698486e-4 7 3.0134629982656573e1 3.0130872371345848e1 -1.2469412476234787e-4 "; diff --git a/pineappl_cli/tests/export.rs b/pineappl_cli/tests/export.rs index 872c8f3a..7a4ec596 100644 --- a/pineappl_cli/tests/export.rs +++ b/pineappl_cli/tests/export.rs @@ -33,23 +33,23 @@ WARNING: the order O(as^0 a^3 lr^0 lf^1 la^0) isn't supported by APPLgrid and wi b APPLgrid PineAPPL rel. diff --+------------+------------+-------------- 0 7.9566291e0 7.9566291e0 -2.5535130e-15 -1 2.3289219e1 2.3289219e1 -1.9984014e-15 +1 2.3289219e1 2.3289219e1 -1.7763568e-15 2 3.7442697e1 3.7442697e1 -2.1094237e-15 -3 5.0087316e1 5.0087316e1 -3.1086245e-15 +3 5.0087316e1 5.0087316e1 -2.9976022e-15 4 6.0873237e1 6.0873237e1 -2.5535130e-15 5 6.8944378e1 6.8944378e1 -3.5527137e-15 6 7.4277783e1 7.4277783e1 -2.8865799e-15 7 7.6356931e1 7.6356931e1 -3.3306691e-15 8 7.5009607e1 7.5009607e1 -1.8873791e-15 -9 7.0045787e1 7.0045787e1 -7.7715612e-16 +9 7.0045787e1 7.0045787e1 -9.9920072e-16 10 6.0009803e1 6.0009803e1 -7.7715612e-16 -11 4.6770515e1 4.6770515e1 2.2204460e-16 +11 4.6770515e1 4.6770515e1 4.4408921e-16 12 3.3569217e1 3.3569217e1 1.5543122e-15 13 2.1820341e1 2.1820341e1 1.1102230e-15 14 1.2542026e1 1.2542026e1 2.2204460e-16 15 6.0879666e0 6.0879666e0 -1.3322676e-15 16 1.5789361e0 1.5789361e0 -1.5543122e-15 -17 7.4959880e-2 7.4959880e-2 -1.3322676e-15 +17 7.4959880e-2 7.4959880e-2 -1.1102230e-15 "; #[cfg(feature = "applgrid")] diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index c98e4b65..a8cded1e 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -52,7 +52,7 @@ Options: #[cfg(feature = "fastnlo")] const IMPORT_FIX_GRID_STR: &str = "b PineAPPL fastNLO rel. diff -+------------+------------+-------------- -0 2.9158424e-4 2.9158424e-4 -2.5535130e-15 +0 2.9158424e-4 2.9158424e-4 -2.7755576e-15 1 2.4657895e-4 2.4657895e-4 -2.6645353e-15 "; @@ -244,20 +244,20 @@ const IMPORT_PHOTON_GRID_STR: &str = "b PineAPPL APPLgrid rel. diff #[cfg(feature = "applgrid")] const IMPORT_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff -+-----------+-----------+-------------- -0 2.9884537e6 2.9884537e6 -6.6613381e-16 +0 2.9884537e6 2.9884537e6 -7.7715612e-16 "; #[cfg(feature = "applgrid")] const IMPORT_NEW_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff --+-----------+-----------+-------------- -0 6.2634897e2 6.2634897e2 1.5543122e-15 +0 6.2634897e2 6.2634897e2 1.7763568e-15 1 6.2847078e2 6.2847078e2 -2.2204460e-16 2 6.3163323e2 6.3163323e2 0.0000000e0 3 6.3586556e2 6.3586556e2 2.2204460e-16 4 6.4139163e2 6.4139163e2 1.7763568e-15 5 6.4848088e2 6.4848088e2 -2.3314684e-15 6 6.5354150e2 6.5354150e2 -3.6637360e-15 -7 6.5377566e2 6.5377566e2 -1.5543122e-15 +7 6.5377566e2 6.5377566e2 -1.7763568e-15 8 6.5094729e2 6.5094729e2 1.7763568e-15 9 6.3588760e2 6.3588760e2 2.2204460e-15 10 5.9810718e2 5.9810718e2 2.2204460e-15 @@ -273,9 +273,9 @@ const IMPORT_GRID_COMPARISON_FAILURE_STR: &str = "Error: grids are different #[cfg(feature = "applgrid")] const IMPORT_DIS_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff -+------------+------------+-------------- -0 9.3514881e-2 9.3514881e-2 -3.3306691e-16 +0 9.3514881e-2 9.3514881e-2 -4.4408921e-16 1 3.9993061e-2 3.9993061e-2 2.2204460e-16 -2 1.3593440e-2 1.3593440e-2 0.0000000e0 +2 1.3593440e-2 1.3593440e-2 -2.2204460e-16 3 2.0825199e-3 2.0825199e-3 -4.4408921e-16 "; diff --git a/pineappl_cli/tests/write.rs b/pineappl_cli/tests/write.rs index eacb08f6..a9a130d6 100644 --- a/pineappl_cli/tests/write.rs +++ b/pineappl_cli/tests/write.rs @@ -246,12 +246,12 @@ const ROTATE_PID_BASIS_NO_DIFF_STR: &str = "b x1 O(as^0 a^2) const ROTATE_PID_BASIS_DIFF_STR: &str = "b x1 O(as^0 a^2) O(as^0 a^3) O(as^1 a^2) -+----+----+-----------+-----------+----------+-------------+-------------+----------+-----------+-----------+---------- 0 2 2.25 6.5070305e2 6.5070305e2 -5.551e-16 -7.8692484e0 -7.8692484e0 -4.441e-16 1.1175729e2 1.1175729e2 -1.221e-15 -1 2.25 2.5 5.9601236e2 5.9601236e2 -7.772e-16 -6.5623495e0 -6.5623495e0 -4.441e-16 1.0083341e2 1.0083341e2 0.000e0 +1 2.25 2.5 5.9601236e2 5.9601236e2 -7.772e-16 -6.5623495e0 -6.5623495e0 -4.441e-16 1.0083341e2 1.0083341e2 -8.882e-16 2 2.5 2.75 5.1561247e2 5.1561247e2 -8.882e-16 -5.2348261e0 -5.2348261e0 -8.882e-16 8.9874343e1 8.9874343e1 -1.221e-15 -3 2.75 3 4.1534629e2 4.1534629e2 -4.441e-16 -3.7590420e0 -3.7590420e0 -6.661e-16 7.3935106e1 7.3935106e1 -1.554e-15 -4 3 3.25 3.0812719e2 3.0812719e2 -5.551e-16 -2.5871885e0 -2.5871885e0 -5.551e-16 5.6414554e1 5.6414554e1 0.000e0 +3 2.75 3 4.1534629e2 4.1534629e2 -4.441e-16 -3.7590420e0 -3.7590420e0 -6.661e-16 7.3935106e1 7.3935106e1 -1.110e-15 +4 3 3.25 3.0812719e2 3.0812719e2 -5.551e-16 -2.5871885e0 -2.5871885e0 -5.551e-16 5.6414554e1 5.6414554e1 2.220e-16 5 3.25 3.5 2.0807482e2 2.0807482e2 -5.551e-16 -1.6762487e0 -1.6762487e0 -2.220e-16 3.9468336e1 3.9468336e1 -6.661e-16 -6 3.5 4 9.6856769e1 9.6856769e1 -4.441e-16 -8.1027456e-1 -8.1027456e-1 -4.441e-16 1.9822014e1 1.9822014e1 -1.110e-15 +6 3.5 4 9.6856769e1 9.6856769e1 -4.441e-16 -8.1027456e-1 -8.1027456e-1 -4.441e-16 1.9822014e1 1.9822014e1 -1.443e-15 7 4 4.5 2.2383492e1 2.2383492e1 -6.661e-16 -2.2022770e-1 -2.2022770e-1 -5.551e-16 5.3540011e0 5.3540011e0 -6.661e-16 "; From 17e608e00f3e7aba878885f1046fba7c17164927 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Oct 2024 11:56:27 +0200 Subject: [PATCH 173/213] Prepare `ConvolutionCache::setup` for multiple scales --- pineappl/src/convolutions.rs | 82 ++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index b8947160..b6b9378d 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -4,7 +4,6 @@ use super::boc::Kinematics; use super::grid::Grid; use super::pids; use super::subgrid::{NodeValues, Subgrid}; -use itertools::Itertools; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; @@ -95,32 +94,48 @@ impl<'a> ConvolutionCache<'a> { x_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); x_grid.dedup(); - let (mut mur2_grid, mut muf2_grid, mut mua2_grid): (Vec<_>, Vec<_>, Vec<_>) = grid + let mut mur2_grid: Vec<_> = grid .subgrids() .iter() .filter(|subgrid| !subgrid.is_empty()) .flat_map(|subgrid| { - grid.kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - // TODO: generalize this for arbitrary scales - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) - }) - // UNWRAP: the `Grid` constructor should guarantee all scales are available - .unwrap_or_else(|| unreachable!()) - .values() - }) - .flat_map(|val| { - xi.iter() - .map(move |(xir, xif, xia)| (xir * xir * val, xif * xif * val, xia * xia * val)) + grid.scales() + .ren + .calc(&subgrid.node_values(), grid.kinematics()) + .unwrap_or_default() }) - .multiunzip(); - + .flat_map(|ren| xi.iter().map(move |(xir, _, _)| xir * xir * ren)) + .collect(); mur2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); mur2_grid.dedup(); + + let mut muf2_grid: Vec<_> = grid + .subgrids() + .iter() + .filter(|subgrid| !subgrid.is_empty()) + .flat_map(|subgrid| { + grid.scales() + .fac + .calc(&subgrid.node_values(), grid.kinematics()) + .unwrap_or_default() + }) + .flat_map(|fac| xi.iter().map(move |(_, xif, _)| xif * xif * fac)) + .collect(); muf2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); muf2_grid.dedup(); + + let mut mua2_grid: Vec<_> = grid + .subgrids() + .iter() + .filter(|subgrid| !subgrid.is_empty()) + .flat_map(|subgrid| { + grid.scales() + .frg + .calc(&subgrid.node_values(), grid.kinematics()) + .unwrap_or_default() + }) + .flat_map(|frg| xi.iter().map(move |(_, _, xia)| xia * xia * frg)) + .collect(); mua2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); mua2_grid.dedup(); @@ -140,7 +155,8 @@ impl<'a> ConvolutionCache<'a> { // - indices[1] is x1 and // - indices[2] is x2. // Lift this restriction! - self.perm + let fx_prod: f64 = self + .perm .iter() .zip(pdg_ids) .enumerate() @@ -169,8 +185,14 @@ impl<'a> ConvolutionCache<'a> { xfx(pid, x, mu2) / x }) }) - .product::() - * self.alphas_cache[self.imur2[indices[0]]].powi(as_order.try_into().unwrap()) + .product(); + let alphas_powers = if as_order != 0 { + self.alphas_cache[self.imur2[indices[0]]].powi(as_order.try_into().unwrap()) + } else { + 1.0 + }; + + fx_prod * alphas_powers } /// Clears the cache. @@ -201,27 +223,33 @@ impl<'a> ConvolutionCache<'a> { self.mur2_grid .iter() .position(|&mur2| mur2 == xir * xir * ren) - .unwrap_or_else(|| unreachable!()) }) - .collect(); + .collect::>() + // if we didn't find a single renormalization scale, we assume we don't need any + // renormalization scale + .unwrap_or_default(); self.imuf2 = mu2_grid .iter() .map(|fac| { self.muf2_grid .iter() .position(|&muf2| muf2 == xif * xif * fac) - .unwrap_or_else(|| unreachable!()) }) - .collect(); + .collect::>() + // if we didn't find a single renormalization scale, we assume we don't need any + // factorization scale + .unwrap_or_default(); self.imua2 = mu2_grid .iter() .map(|frg| { self.mua2_grid .iter() .position(|&mua2| mua2 == xia * xia * frg) - .unwrap_or_else(|| unreachable!()) }) - .collect(); + .collect::>() + // if we didn't find a single renormalization scale, we assume we don't need any + // fragmentation scale + .unwrap_or_default(); // TODO: generalize this for arbitrary orderings of x self.ix = (0..grid.convolutions().len()) From 35849fd515c8fa61ef9c36bd3937db955b289d19 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 17 Oct 2024 09:33:37 +0200 Subject: [PATCH 174/213] Add some trivial fixes to Python API tests --- pineappl_py/tests/test_boc.py | 2 +- pineappl_py/tests/test_fk_table.py | 4 ++-- pineappl_py/tests/test_sugrid.py | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pineappl_py/tests/test_boc.py b/pineappl_py/tests/test_boc.py index 9b00171d..f52091ee 100644 --- a/pineappl_py/tests/test_boc.py +++ b/pineappl_py/tests/test_boc.py @@ -3,5 +3,5 @@ class TestChannel: def test_init(self): - le = pineappl.boc.Channel([(2, 2, 0.5)]) + le = pineappl.boc.Channel([([2, 2], 0.5)]) assert isinstance(le, pineappl.boc.Channel) diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index af047ed8..b696e3cc 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -5,8 +5,8 @@ class TestFkTable: def fake_grid(self, bins=None): - channels = [pineappl.boc.Channel([(1, 21, 1.0)])] - orders = [pineappl.grid.Order(0, 0, 0, 0)] + channels = [pineappl.boc.Channel([([1, 21], 1.0)])] + orders = [pineappl.grid.Order(0, 0, 0, 0, 0)] bin_limits = np.array([1e-7, 1e-3, 1] if bins is None else bins, dtype=float) subgrid_params = pineappl.subgrid.SubgridParams() g = pineappl.grid.Grid(channels, orders, bin_limits, subgrid_params) diff --git a/pineappl_py/tests/test_sugrid.py b/pineappl_py/tests/test_sugrid.py index 91a0d423..4ad873c3 100644 --- a/pineappl_py/tests/test_sugrid.py +++ b/pineappl_py/tests/test_sugrid.py @@ -11,8 +11,8 @@ def test_init(self): def test_issue_164(pdf): - channels = [pineappl.boc.Channel([(1, 2, 1.0)])] - orders = [pineappl.grid.Order(0, 0, 0, 0)] + channels = [pineappl.boc.Channel([([1, 2], 1.0)])] + orders = [pineappl.grid.Order(0, 0, 0, 0, 0)] params = pineappl.subgrid.SubgridParams() def convolve_grid(): @@ -33,8 +33,8 @@ def convolve_grid(): class TestSubgrid: def fake_grid(self): - channels = [pineappl.boc.Channel([(1, 2, 1.0)])] - orders = [pineappl.grid.Order(0, 0, 0, 0)] + channels = [pineappl.boc.Channel([([1, 2], 1.0)])] + orders = [pineappl.grid.Order(0, 0, 0, 0, 0)] params = pineappl.subgrid.SubgridParams() bin_limits = np.array([0.0, 1.0]) grid = pineappl.grid.Grid(channels, orders, bin_limits, params) From c795a24af56d9fa5fb1cd33e3de17398bfe5c4f7 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 17 Oct 2024 09:34:23 +0200 Subject: [PATCH 175/213] Re-enable Python tests in CI --- .github/workflows/python.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index c2bf990d..6d8fe1fa 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -4,7 +4,6 @@ on: push: branches-ignore: - pycli - - v1-file-format jobs: test: From e4604f366ccc94495cd22eff12fb58a7b77b5cc3 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 17 Oct 2024 11:12:57 +0200 Subject: [PATCH 176/213] Add some small fixes --- pineappl/src/convolutions.rs | 7 +++---- pineappl/src/v0.rs | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index b6b9378d..6f0a6bc0 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -1,4 +1,4 @@ -//! Module for everything related to luminosity functions. +//! Module for everything related to convolution functions. use super::boc::Kinematics; use super::grid::Grid; @@ -236,7 +236,7 @@ impl<'a> ConvolutionCache<'a> { .position(|&muf2| muf2 == xif * xif * fac) }) .collect::>() - // if we didn't find a single renormalization scale, we assume we don't need any + // if we didn't find a single factorization scale, we assume we don't need any // factorization scale .unwrap_or_default(); self.imua2 = mu2_grid @@ -247,11 +247,10 @@ impl<'a> ConvolutionCache<'a> { .position(|&mua2| mua2 == xia * xia * frg) }) .collect::>() - // if we didn't find a single renormalization scale, we assume we don't need any + // if we didn't find a single fragmentation scale, we assume we don't need any // fragmentation scale .unwrap_or_default(); - // TODO: generalize this for arbitrary orderings of x self.ix = (0..grid.convolutions().len()) .map(|idx| { grid.kinematics() diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 47ca1a7c..ffe3c16a 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -164,7 +164,6 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result interps: default_interps(convolutions.len()), convolutions: convolutions .into_iter() - //.map(|conv| conv.unwrap_or(Convolution::None)) .flatten() .collect(), pid_basis: grid From 4e0214c7ac109b9af0b5353f5c33bb9f98967fc8 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 17 Oct 2024 11:13:53 +0200 Subject: [PATCH 177/213] Add fragmentation scaling factor to `Grid::scale_by_order` --- pineappl/src/grid.rs | 4 ++- pineappl/tests/drell_yan_lo.rs | 4 +-- pineappl_capi/src/lib.rs | 2 +- pineappl_cli/src/import/applgrid.rs | 2 +- pineappl_cli/src/import/fastnlo.rs | 2 +- pineappl_cli/src/write.rs | 6 ++-- pineappl_cli/tests/write.rs | 48 ++++++++++++++--------------- 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 599f32b9..cb58cf00 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -741,6 +741,7 @@ impl Grid { alpha: f64, logxir: f64, logxif: f64, + logxia: f64, global: f64, ) { for ((i, _, _), subgrid) in self.subgrids.indexed_iter_mut() { @@ -749,7 +750,8 @@ impl Grid { * alphas.powi(order.alphas.try_into().unwrap()) * alpha.powi(order.alpha.try_into().unwrap()) * logxir.powi(order.logxir.try_into().unwrap()) - * logxif.powi(order.logxif.try_into().unwrap()); + * logxif.powi(order.logxif.try_into().unwrap()) + * logxia.powi(order.logxia.try_into().unwrap()); subgrid.scale(factor); } diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index aea040b8..7118d1a8 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -347,8 +347,8 @@ fn perform_grid_tests( let mut grid = Grid::read(&mut file)?; // TEST 4: `scale_by_order` - grid.scale_by_order(10.0, 0.5, 10.0, 10.0, 1.0); - grid.scale_by_order(10.0, 1.0, 10.0, 10.0, 4.0); + grid.scale_by_order(10.0, 0.5, 10.0, 10.0, 1.0, 1.0); + grid.scale_by_order(10.0, 1.0, 10.0, 10.0, 1.0, 4.0); // TEST 5: `convolve` let mut convolution_cache = ConvolutionCache::new( diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 86c1f97e..cea323de 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -925,7 +925,7 @@ pub unsafe extern "C" fn pineappl_grid_scale_by_order( ) { let grid = unsafe { &mut *grid }; - grid.scale_by_order(alphas, alpha, logxir, logxif, global); + grid.scale_by_order(alphas, alpha, logxir, logxif, 1.0, global); } /// Return the value for `key` stored in `grid`. If `key` isn't found, `NULL` will be returned. diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 4be31148..ad58402c 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -287,7 +287,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { } } - grid0.scale_by_order(alphas_factor, 1.0, 1.0, 1.0, global); + grid0.scale_by_order(alphas_factor, 1.0, 1.0, 1.0, 1.0, global); Ok(grid0) } diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 6c42322b..b8426ded 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -541,7 +541,7 @@ pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32) -> Result { result.merge(grid)?; } - result.scale_by_order(1.0 / TAU, 1.0, 1.0, 1.0, 1.0); + result.scale_by_order(1.0 / TAU, 1.0, 1.0, 1.0, 1.0, 1.0); let dimensions: usize = file_as_table.GetNumDiffBin().try_into().unwrap(); let mut limits = Vec::new(); diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index 586210ae..20af0a85 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -439,11 +439,11 @@ impl Args for MoreArgs { .arg( Arg::new("scale_by_order") .action(ArgAction::Append) - .help("Scales all grids with order-dependent factors") + .help("Scale subgrids with order-dependent factors") .long("scale-by-order") .num_args(1) .value_delimiter(',') - .value_name("AS,AL,LR,LF") + .value_name("FAC1,FAC2,...") .value_parser(value_parser!(f64)), ) .arg( @@ -581,7 +581,7 @@ impl Subcommand for Opts { } OpsArg::ScaleByBin(factors) => grid.scale_by_bin(factors), OpsArg::ScaleByOrder(factors) => { - grid.scale_by_order(factors[0], factors[1], factors[2], factors[3], 1.0); + grid.scale_by_order(factors[0], factors[1], factors[2], factors[3], factors[4], 1.0); } OpsArg::SetKeyValue(key_value) => { grid.metadata_mut() diff --git a/pineappl_cli/tests/write.rs b/pineappl_cli/tests/write.rs index a9a130d6..947838df 100644 --- a/pineappl_cli/tests/write.rs +++ b/pineappl_cli/tests/write.rs @@ -12,29 +12,29 @@ Arguments: Path of the modified PineAPPL file Options: - --cc Charge conjugate the convolution with the specified index - --dedup-channels[=] Deduplicate channels assuming numbers differing by ULPS are the same - --delete-bins Delete bins with the specified indices - --delete-channels Delete channels with the specified indices - --delete-orders Delete orders with the specified indices - --delete-key Delete an internal key-value pair - --merge-bins Merge specific bins together - --optimize[=] Optimize internal data structure to minimize memory and disk usage [possible values: true, false] - --optimize-fk-table Optimize internal data structure of an FkTable to minimize memory and disk usage [possible values: Nf6Ind, Nf6Sym, Nf5Ind, Nf5Sym, Nf4Ind, Nf4Sym, Nf3Ind, Nf3Sym] - --remap Modify the bin dimensions and widths - --remap-norm Modify the bin normalizations with a common factor - --remap-norm-ignore Modify the bin normalizations by multiplying with the bin lengths for the given dimensions - --rewrite-channel Rewrite the definition of the channel with index IDX - --rewrite-order Rewrite the definition of the order with index IDX - --rotate-pid-basis Rotate the PID basis for this grid [possible values: PDG, EVOL] - -s, --scale Scales all grids with the given factor - --scale-by-bin Scale each bin with a different factor - --scale-by-order Scales all grids with order-dependent factors - --set-key-value Set an internal key-value pair - --set-key-file Set an internal key-value pair, with value being read from a file - --split-channels[=] Split the grid such that each channel contains only a single PID combination [possible values: true, false] - --upgrade[=] Convert the file format to the most recent version [possible values: true, false] - -h, --help Print help + --cc Charge conjugate the convolution with the specified index + --dedup-channels[=] Deduplicate channels assuming numbers differing by ULPS are the same + --delete-bins Delete bins with the specified indices + --delete-channels Delete channels with the specified indices + --delete-orders Delete orders with the specified indices + --delete-key Delete an internal key-value pair + --merge-bins Merge specific bins together + --optimize[=] Optimize internal data structure to minimize memory and disk usage [possible values: true, false] + --optimize-fk-table Optimize internal data structure of an FkTable to minimize memory and disk usage [possible values: Nf6Ind, Nf6Sym, Nf5Ind, Nf5Sym, Nf4Ind, Nf4Sym, Nf3Ind, Nf3Sym] + --remap Modify the bin dimensions and widths + --remap-norm Modify the bin normalizations with a common factor + --remap-norm-ignore Modify the bin normalizations by multiplying with the bin lengths for the given dimensions + --rewrite-channel Rewrite the definition of the channel with index IDX + --rewrite-order Rewrite the definition of the order with index IDX + --rotate-pid-basis Rotate the PID basis for this grid [possible values: PDG, EVOL] + -s, --scale Scales all grids with the given factor + --scale-by-bin Scale each bin with a different factor + --scale-by-order Scale subgrids with order-dependent factors + --set-key-value Set an internal key-value pair + --set-key-file Set an internal key-value pair, with value being read from a file + --split-channels[=] Split the grid such that each channel contains only a single PID combination [possible values: true, false] + --upgrade[=] Convert the file format to the most recent version [possible values: true, false] + -h, --help Print help "; const CHANNEL_STR: &str = "c entry entry @@ -771,7 +771,7 @@ fn scale_by_order() { .unwrap() .args([ "write", - "--scale-by-order=2,1,0.5,0.5", + "--scale-by-order=2,1,0.5,0.5,1.0", "--scale=0.5", "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", output.path().to_str().unwrap(), From c134c7ea3bc8e47e8e0e00d2f2e0ec516d8bff93 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 17 Oct 2024 11:26:12 +0200 Subject: [PATCH 178/213] Rename `LagrangeSubgridV2` to `InterpSubgridV1` --- pineappl/src/grid.rs | 8 ++++---- .../src/{lagrange_subgrid.rs => interp_subgrid.rs} | 14 +++++++------- pineappl/src/lib.rs | 2 +- pineappl/src/subgrid.rs | 6 +++--- pineappl/tests/drell_yan_lo.rs | 2 +- pineappl_cli/src/subgrids.rs | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) rename pineappl/src/{lagrange_subgrid.rs => interp_subgrid.rs} (94%) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index cb58cf00..5a9d62f5 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -7,7 +7,7 @@ use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; use super::interpolation::Interp; -use super::lagrange_subgrid::LagrangeSubgridV2; +use super::interp_subgrid::InterpSubgridV1; use super::packed_subgrid::PackedQ1X2SubgridV1; use super::pids::PidBasis; use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; @@ -407,7 +407,7 @@ impl Grid { if let Some(bin) = self.bin_limits.index(observable) { let subgrid = &mut self.subgrids[[order, bin, channel]]; if let SubgridEnum::EmptySubgridV1(_) = subgrid { - *subgrid = LagrangeSubgridV2::new(&self.interps).into(); + *subgrid = InterpSubgridV1::new(&self.interps).into(); } subgrid.fill(&self.interps, ntuple, weight); @@ -875,10 +875,10 @@ impl Grid { *subgrid = EmptySubgridV1.into(); } _ => { - // TODO: this requires a `pub(crate)` in `LagrangeSubgridV2`; we should + // TODO: this requires a `pub(crate)` in `InterpSubgridV1`; we should // replace this with a method if !static_scale_detection { - if let SubgridEnum::LagrangeSubgridV2(subgrid) = subgrid { + if let SubgridEnum::InterpSubgridV1(subgrid) = subgrid { // disable static-scale detection subgrid.static_q2 = -1.0; } diff --git a/pineappl/src/lagrange_subgrid.rs b/pineappl/src/interp_subgrid.rs similarity index 94% rename from pineappl/src/lagrange_subgrid.rs rename to pineappl/src/interp_subgrid.rs index b4c90b1b..7d38a7ce 100644 --- a/pineappl/src/lagrange_subgrid.rs +++ b/pineappl/src/interp_subgrid.rs @@ -9,13 +9,13 @@ use std::mem; /// Subgrid which uses Lagrange-interpolation. #[derive(Clone, Deserialize, Serialize)] -pub struct LagrangeSubgridV2 { +pub struct InterpSubgridV1 { array: PackedArray, interps: Vec, pub(crate) static_q2: f64, } -impl LagrangeSubgridV2 { +impl InterpSubgridV1 { /// Constructor. #[must_use] pub fn new(interps: &[Interp]) -> Self { @@ -27,7 +27,7 @@ impl LagrangeSubgridV2 { } } -impl Subgrid for LagrangeSubgridV2 { +impl Subgrid for InterpSubgridV1 { fn fill(&mut self, interps: &[Interp], ntuple: &[f64], weight: f64) { debug_assert_eq!(interps.len(), ntuple.len()); @@ -55,7 +55,7 @@ impl Subgrid for LagrangeSubgridV2 { fn merge(&mut self, other: &SubgridEnum, transpose: Option<(usize, usize)>) { // we cannot use `Self::indexed_iter` because it multiplies with `reweight` - if let SubgridEnum::LagrangeSubgridV2(other) = other { + if let SubgridEnum::InterpSubgridV1(other) = other { // TODO: make sure `other` has the same interpolation as `self` for (mut index, value) in other.array.indexed_iter() { if let Some((a, b)) = transpose { @@ -128,7 +128,7 @@ mod tests { #[test] fn fill_zero() { let interps = v0::default_interps(2); - let mut subgrid = LagrangeSubgridV2::new(&interps); + let mut subgrid = InterpSubgridV1::new(&interps); subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 0.0); @@ -149,7 +149,7 @@ mod tests { #[test] fn fill_outside_range() { let interps = v0::default_interps(2); - let mut subgrid = LagrangeSubgridV2::new(&interps); + let mut subgrid = InterpSubgridV1::new(&interps); subgrid.fill(&interps, &[1000.0, 1e-10, 0.5], 0.0); @@ -170,7 +170,7 @@ mod tests { #[test] fn fill() { let interps = v0::default_interps(2); - let mut subgrid = LagrangeSubgridV2::new(&interps); + let mut subgrid = InterpSubgridV1::new(&interps); subgrid.fill(&interps, &[1000.0, 0.5, 0.5], 1.0); diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index 33b7add8..c85ce418 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -44,7 +44,7 @@ pub mod evolution; pub mod fk_table; pub mod grid; pub mod interpolation; -pub mod lagrange_subgrid; +pub mod interp_subgrid; pub mod packed_array; pub mod packed_subgrid; pub mod pids; diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 0b5c3481..a049a6c0 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,7 +1,7 @@ //! Module containing the trait `Subgrid` and supporting structs. use super::empty_subgrid::EmptySubgridV1; -use super::lagrange_subgrid::LagrangeSubgridV2; +use super::interp_subgrid::InterpSubgridV1; use super::packed_subgrid::PackedQ1X2SubgridV1; use enum_dispatch::enum_dispatch; // use float_cmp::approx_eq; @@ -94,8 +94,8 @@ impl PartialEq for NodeValues { #[derive(Clone, Deserialize, Serialize)] pub enum SubgridEnum { // WARNING: never change the order or content of this enum, only add to the end of it - /// Lagrange-interpolation subgrid with possibly different x1 and x2 bins. - LagrangeSubgridV2, + /// Subgrid type that supports filling. + InterpSubgridV1, /// Empty subgrid. EmptySubgridV1, /// TODO diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 7118d1a8..53d5bedd 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -687,7 +687,7 @@ fn grid_optimize() { assert_eq!(grid.channels().len(), 5); assert!(matches!( grid.subgrids()[[0, 0, 0]], - SubgridEnum::LagrangeSubgridV2 { .. } + SubgridEnum::InterpSubgridV1 { .. } )); let node_values = grid.subgrids()[[0, 0, 0]].node_values(); diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index 63505a24..e8964d87 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -78,7 +78,7 @@ impl Subcommand for Opts { if self.group.type_ { row.add_cell(cell!(l-> match subgrid { - SubgridEnum::LagrangeSubgridV2(_) => "LagrangeSubgridV2", + SubgridEnum::InterpSubgridV1(_) => "InterpSubgridV1", SubgridEnum::EmptySubgridV1(_) => "EmptySubgridV1", SubgridEnum::PackedQ1X2SubgridV1(_) => "PackedQ1X2SubgridV1", } From 9182bdb4dadc480180b7796af219f8c81edcecb0 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 17 Oct 2024 11:40:54 +0200 Subject: [PATCH 179/213] Rename `PackedQ1X2SubgridV1` to `ImportSubgridV1` --- pineappl/src/empty_subgrid.rs | 4 +- pineappl/src/evolution.rs | 4 +- pineappl/src/grid.rs | 4 +- .../{packed_subgrid.rs => import_subgrid.rs} | 22 +- pineappl/src/lib.rs | 2 +- pineappl/src/subgrid.rs | 4 +- pineappl/src/v0.rs | 4 +- pineappl/tests/drell_yan_lo.rs | 4 +- pineappl_cli/src/import/applgrid.rs | 4 +- pineappl_cli/src/import/fastnlo.rs | 6 +- pineappl_cli/src/import/fktable.rs | 6 +- pineappl_cli/src/subgrids.rs | 2 +- pineappl_cli/tests/subgrids.rs | 424 +++++++++--------- 13 files changed, 245 insertions(+), 245 deletions(-) rename pineappl/src/{packed_subgrid.rs => import_subgrid.rs} (92%) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 4977d082..0f7db160 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -56,7 +56,7 @@ impl Subgrid for EmptySubgridV1 { mod tests { use super::*; use crate::packed_array::PackedArray; - use crate::packed_subgrid::PackedQ1X2SubgridV1; + use crate::import_subgrid::ImportSubgridV1; use crate::v0; #[test] @@ -96,7 +96,7 @@ mod tests { let mut array = PackedArray::new(vec![1, 1]); array[0] = 1.0; let node_values = vec![NodeValues::UseThese(vec![1.0]); 2]; - let subgrid_rhs = PackedQ1X2SubgridV1::new(array, node_values).into(); + let subgrid_rhs = ImportSubgridV1::new(array, node_values).into(); subgrid_lhs.merge(&subgrid_rhs, None); } diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index a73e9b52..7a865894 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -4,7 +4,7 @@ use super::boc::{Channel, Kinematics, Order}; use super::convolutions::ConvType; use super::grid::{Grid, GridError}; use super::packed_array::PackedArray; -use super::packed_subgrid::PackedQ1X2SubgridV1; +use super::import_subgrid::ImportSubgridV1; use super::pids::PidBasis; use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use float_cmp::approx_eq; @@ -489,7 +489,7 @@ pub(crate) fn evolve_slice_with_many( } sub_fk_tables.extend(tables.into_iter().map(|table| { - PackedQ1X2SubgridV1::new( + ImportSubgridV1::new( PackedArray::from(table.insert_axis(Axis(0)).view()), node_values.clone(), ) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 5a9d62f5..1bc73a81 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -8,7 +8,7 @@ use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; use super::interpolation::Interp; use super::interp_subgrid::InterpSubgridV1; -use super::packed_subgrid::PackedQ1X2SubgridV1; +use super::import_subgrid::ImportSubgridV1; use super::pids::PidBasis; use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use super::v0; @@ -884,7 +884,7 @@ impl Grid { } } - *subgrid = PackedQ1X2SubgridV1::from(&*subgrid).into(); + *subgrid = ImportSubgridV1::from(&*subgrid).into(); } } } diff --git a/pineappl/src/packed_subgrid.rs b/pineappl/src/import_subgrid.rs similarity index 92% rename from pineappl/src/packed_subgrid.rs rename to pineappl/src/import_subgrid.rs index 0b317bb9..02681416 100644 --- a/pineappl/src/packed_subgrid.rs +++ b/pineappl/src/import_subgrid.rs @@ -9,12 +9,12 @@ use std::mem; /// TODO #[derive(Clone, Deserialize, Serialize)] -pub struct PackedQ1X2SubgridV1 { +pub struct ImportSubgridV1 { array: PackedArray, node_values: Vec, } -impl PackedQ1X2SubgridV1 { +impl ImportSubgridV1 { /// Constructor. #[must_use] pub const fn new(array: PackedArray, node_values: Vec) -> Self { @@ -22,9 +22,9 @@ impl PackedQ1X2SubgridV1 { } } -impl Subgrid for PackedQ1X2SubgridV1 { +impl Subgrid for ImportSubgridV1 { fn fill(&mut self, _: &[Interp], _: &[f64], _: f64) { - panic!("PackedQ1X2SubgridV1 doesn't support the fill operation"); + panic!("ImportSubgridV1 doesn't support the fill operation"); } fn node_values(&self) -> Vec { @@ -121,7 +121,7 @@ impl Subgrid for PackedQ1X2SubgridV1 { } } -impl From<&SubgridEnum> for PackedQ1X2SubgridV1 { +impl From<&SubgridEnum> for ImportSubgridV1 { fn from(subgrid: &SubgridEnum) -> Self { // find smallest ranges let ranges: Vec<_> = subgrid.indexed_iter().fold( @@ -185,9 +185,9 @@ mod tests { use crate::v0; #[test] - #[should_panic(expected = "PackedQ1X2SubgridV1 doesn't support the fill operation")] + #[should_panic(expected = "ImportSubgridV1 doesn't support the fill operation")] fn fill_packed_q1x2_subgrid_v1() { - let mut subgrid = PackedQ1X2SubgridV1::new( + let mut subgrid = ImportSubgridV1::new( PackedArray::new(vec![0, 0, 0]), vec![NodeValues::UseThese(Vec::new()); 3], ); @@ -199,7 +199,7 @@ mod tests { let x = vec![ 0.015625, 0.03125, 0.0625, 0.125, 0.1875, 0.25, 0.375, 0.5, 0.75, 1.0, ]; - let mut grid1: SubgridEnum = PackedQ1X2SubgridV1::new( + let mut grid1: SubgridEnum = ImportSubgridV1::new( PackedArray::new(vec![1, 10, 10]), vec![ NodeValues::UseThese(vec![0.0]), @@ -221,7 +221,7 @@ mod tests { assert!(grid1.is_empty()); // only use exactly representable numbers here so that we can avoid using approx_eq - if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid1 { + if let SubgridEnum::ImportSubgridV1(ref mut x) = grid1 { x.array[[0, 1, 2]] = 1.0; x.array[[0, 1, 3]] = 2.0; x.array[[0, 4, 3]] = 4.0; @@ -236,7 +236,7 @@ mod tests { assert_eq!(grid1.indexed_iter().nth(3), Some((vec![0, 7, 1], 8.0))); // create grid with transposed entries, but different q2 - let mut grid2: SubgridEnum = PackedQ1X2SubgridV1::new( + let mut grid2: SubgridEnum = ImportSubgridV1::new( PackedArray::new(vec![1, 10, 10]), vec![ NodeValues::UseThese(vec![1.0]), @@ -245,7 +245,7 @@ mod tests { ], ) .into(); - if let SubgridEnum::PackedQ1X2SubgridV1(ref mut x) = grid2 { + if let SubgridEnum::ImportSubgridV1(ref mut x) = grid2 { x.array[[0, 2, 1]] = 1.0; x.array[[0, 3, 1]] = 2.0; x.array[[0, 3, 4]] = 4.0; diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index c85ce418..ac05684c 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -46,6 +46,6 @@ pub mod grid; pub mod interpolation; pub mod interp_subgrid; pub mod packed_array; -pub mod packed_subgrid; +pub mod import_subgrid; pub mod pids; pub mod subgrid; diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index a049a6c0..cb3bc91c 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -2,7 +2,7 @@ use super::empty_subgrid::EmptySubgridV1; use super::interp_subgrid::InterpSubgridV1; -use super::packed_subgrid::PackedQ1X2SubgridV1; +use super::import_subgrid::ImportSubgridV1; use enum_dispatch::enum_dispatch; // use float_cmp::approx_eq; // use ndarray::Array3; @@ -99,7 +99,7 @@ pub enum SubgridEnum { /// Empty subgrid. EmptySubgridV1, /// TODO - PackedQ1X2SubgridV1, + ImportSubgridV1, } /// Structure denoting renormalization and factorization scale values. diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index ffe3c16a..0d45d643 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -5,7 +5,7 @@ use super::empty_subgrid::EmptySubgridV1; use super::grid::{Grid, GridError, Mmv4, MoreMembers}; use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use super::packed_array::PackedArray; -use super::packed_subgrid::PackedQ1X2SubgridV1; +use super::import_subgrid::ImportSubgridV1; use super::pids::PidBasis; use super::subgrid::{Mu2, NodeValues}; use ndarray::Array3; @@ -104,7 +104,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result if convolutions[1].is_some() { node_values.push(NodeValues::UseThese(subgrid.x2_grid().into_owned())); } - PackedQ1X2SubgridV1::new(array, node_values).into() + ImportSubgridV1::new(array, node_values).into() } }) .collect(), diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index 53d5bedd..a94fbeb7 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -701,7 +701,7 @@ fn grid_optimize() { // `OPTIMIZE_SUBGRID_TYPE` changes the subgrid type ... assert!(matches!( grid2.subgrids()[[0, 0, 0]], - SubgridEnum::PackedQ1X2SubgridV1 { .. } + SubgridEnum::ImportSubgridV1 { .. } )); // and the dimensions of the subgrid let node_values = grid2.subgrids()[[0, 0, 0]].node_values(); @@ -713,7 +713,7 @@ fn grid_optimize() { assert!(matches!( grid.subgrids()[[0, 0, 0]], - SubgridEnum::PackedQ1X2SubgridV1 { .. } + SubgridEnum::ImportSubgridV1 { .. } )); // if `STATIC_SCALE_DETECTION` is present the scale dimension is better optimized let node_values = grid.subgrids()[[0, 0, 0]].node_values(); diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index ad58402c..66fc2842 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -5,7 +5,7 @@ use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; -use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::pids::PidBasis; use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_applgrid::ffi::{self, grid}; @@ -235,7 +235,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { if !array.is_empty() { pgrid.subgrids_mut()[[0, bin.try_into().unwrap(), lumi]] = - PackedQ1X2SubgridV1::new( + ImportSubgridV1::new( array, vec![ NodeValues::UseThese( diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index b8426ded..82691d3f 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -7,7 +7,7 @@ use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; -use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::pids::PidBasis; use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_fastnlo::ffi::{ @@ -263,7 +263,7 @@ fn convert_coeff_add_fix( if !array.is_empty() { grid.subgrids_mut() [[0, obs.try_into().unwrap(), subproc.try_into().unwrap()]] = - PackedQ1X2SubgridV1::new( + ImportSubgridV1::new( array, vec![ NodeValues::UseThese( @@ -475,7 +475,7 @@ fn convert_coeff_add_flex( // continue; // } - // *subgrid = PackedQ1X2SubgridV1::new(array, todo!()).into(); + // *subgrid = ImportSubgridV1::new(array, todo!()).into(); // } // } // } diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index fb8491e6..5517bc1a 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -7,7 +7,7 @@ use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; -use pineappl::packed_subgrid::PackedQ1X2SubgridV1; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::pids::PidBasis; use pineappl::subgrid::NodeValues; use std::fs::File; @@ -246,7 +246,7 @@ fn read_fktable(reader: impl BufRead) -> Result { .iter_mut() .zip(arrays.into_iter()) { - *subgrid = PackedQ1X2SubgridV1::new( + *subgrid = ImportSubgridV1::new( array, if hadronic { vec![ @@ -318,7 +318,7 @@ fn read_fktable(reader: impl BufRead) -> Result { .iter_mut() .zip(arrays.into_iter()) { - *subgrid = PackedQ1X2SubgridV1::new( + *subgrid = ImportSubgridV1::new( array, if hadronic { vec![ diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index e8964d87..e0a76d51 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -80,7 +80,7 @@ impl Subcommand for Opts { match subgrid { SubgridEnum::InterpSubgridV1(_) => "InterpSubgridV1", SubgridEnum::EmptySubgridV1(_) => "EmptySubgridV1", - SubgridEnum::PackedQ1X2SubgridV1(_) => "PackedQ1X2SubgridV1", + SubgridEnum::ImportSubgridV1(_) => "ImportSubgridV1", } )); } diff --git a/pineappl_cli/tests/subgrids.rs b/pineappl_cli/tests/subgrids.rs index f9a2b5b9..77f76020 100644 --- a/pineappl_cli/tests/subgrids.rs +++ b/pineappl_cli/tests/subgrids.rs @@ -235,316 +235,316 @@ const STATS_STR: &str = "o b c total allocated zeros overhead 4 7 4 1444 709 0 76 "; -const TYPE_STR: &str = "o b c type --+-+-+------------------- -0 0 0 PackedQ1X2SubgridV1 -0 1 0 PackedQ1X2SubgridV1 -0 2 0 PackedQ1X2SubgridV1 -0 3 0 PackedQ1X2SubgridV1 -0 4 0 PackedQ1X2SubgridV1 -0 5 0 PackedQ1X2SubgridV1 -0 6 0 PackedQ1X2SubgridV1 -0 7 0 PackedQ1X2SubgridV1 -1 0 0 PackedQ1X2SubgridV1 -1 0 1 PackedQ1X2SubgridV1 -1 0 3 PackedQ1X2SubgridV1 -1 1 0 PackedQ1X2SubgridV1 -1 1 1 PackedQ1X2SubgridV1 -1 1 3 PackedQ1X2SubgridV1 -1 2 0 PackedQ1X2SubgridV1 -1 2 1 PackedQ1X2SubgridV1 -1 2 3 PackedQ1X2SubgridV1 -1 3 0 PackedQ1X2SubgridV1 -1 3 1 PackedQ1X2SubgridV1 -1 3 3 PackedQ1X2SubgridV1 -1 4 0 PackedQ1X2SubgridV1 -1 4 1 PackedQ1X2SubgridV1 -1 4 3 PackedQ1X2SubgridV1 -1 5 0 PackedQ1X2SubgridV1 -1 5 1 PackedQ1X2SubgridV1 -1 5 3 PackedQ1X2SubgridV1 -1 6 0 PackedQ1X2SubgridV1 -1 6 1 PackedQ1X2SubgridV1 -1 6 3 PackedQ1X2SubgridV1 -1 7 0 PackedQ1X2SubgridV1 -1 7 1 PackedQ1X2SubgridV1 -1 7 3 PackedQ1X2SubgridV1 -2 0 0 PackedQ1X2SubgridV1 -2 0 1 PackedQ1X2SubgridV1 -2 0 3 PackedQ1X2SubgridV1 -2 1 0 PackedQ1X2SubgridV1 -2 1 1 PackedQ1X2SubgridV1 -2 1 3 PackedQ1X2SubgridV1 -2 2 0 PackedQ1X2SubgridV1 -2 2 1 PackedQ1X2SubgridV1 -2 2 3 PackedQ1X2SubgridV1 -2 3 0 PackedQ1X2SubgridV1 -2 3 1 PackedQ1X2SubgridV1 -2 3 3 PackedQ1X2SubgridV1 -2 4 0 PackedQ1X2SubgridV1 -2 4 1 PackedQ1X2SubgridV1 -2 4 3 PackedQ1X2SubgridV1 -2 5 0 PackedQ1X2SubgridV1 -2 5 1 PackedQ1X2SubgridV1 -2 5 3 PackedQ1X2SubgridV1 -2 6 0 PackedQ1X2SubgridV1 -2 6 1 PackedQ1X2SubgridV1 -2 6 3 PackedQ1X2SubgridV1 -2 7 0 PackedQ1X2SubgridV1 -2 7 1 PackedQ1X2SubgridV1 -2 7 3 PackedQ1X2SubgridV1 -3 0 0 PackedQ1X2SubgridV1 -3 0 2 PackedQ1X2SubgridV1 -3 0 4 PackedQ1X2SubgridV1 -3 1 0 PackedQ1X2SubgridV1 -3 1 2 PackedQ1X2SubgridV1 -3 1 4 PackedQ1X2SubgridV1 -3 2 0 PackedQ1X2SubgridV1 -3 2 2 PackedQ1X2SubgridV1 -3 2 4 PackedQ1X2SubgridV1 -3 3 0 PackedQ1X2SubgridV1 -3 3 2 PackedQ1X2SubgridV1 -3 3 4 PackedQ1X2SubgridV1 -3 4 0 PackedQ1X2SubgridV1 -3 4 2 PackedQ1X2SubgridV1 -3 4 4 PackedQ1X2SubgridV1 -3 5 0 PackedQ1X2SubgridV1 -3 5 2 PackedQ1X2SubgridV1 -3 5 4 PackedQ1X2SubgridV1 -3 6 0 PackedQ1X2SubgridV1 -3 6 2 PackedQ1X2SubgridV1 -3 6 4 PackedQ1X2SubgridV1 -3 7 0 PackedQ1X2SubgridV1 -3 7 2 PackedQ1X2SubgridV1 -3 7 4 PackedQ1X2SubgridV1 -4 0 0 PackedQ1X2SubgridV1 -4 0 2 PackedQ1X2SubgridV1 -4 0 4 PackedQ1X2SubgridV1 -4 1 0 PackedQ1X2SubgridV1 -4 1 2 PackedQ1X2SubgridV1 -4 1 4 PackedQ1X2SubgridV1 -4 2 0 PackedQ1X2SubgridV1 -4 2 2 PackedQ1X2SubgridV1 -4 2 4 PackedQ1X2SubgridV1 -4 3 0 PackedQ1X2SubgridV1 -4 3 2 PackedQ1X2SubgridV1 -4 3 4 PackedQ1X2SubgridV1 -4 4 0 PackedQ1X2SubgridV1 -4 4 2 PackedQ1X2SubgridV1 -4 4 4 PackedQ1X2SubgridV1 -4 5 0 PackedQ1X2SubgridV1 -4 5 2 PackedQ1X2SubgridV1 -4 5 4 PackedQ1X2SubgridV1 -4 6 0 PackedQ1X2SubgridV1 -4 6 2 PackedQ1X2SubgridV1 -4 6 4 PackedQ1X2SubgridV1 -4 7 0 PackedQ1X2SubgridV1 -4 7 2 PackedQ1X2SubgridV1 -4 7 4 PackedQ1X2SubgridV1 +const TYPE_STR: &str = "o b c type +-+-+-+--------------- +0 0 0 ImportSubgridV1 +0 1 0 ImportSubgridV1 +0 2 0 ImportSubgridV1 +0 3 0 ImportSubgridV1 +0 4 0 ImportSubgridV1 +0 5 0 ImportSubgridV1 +0 6 0 ImportSubgridV1 +0 7 0 ImportSubgridV1 +1 0 0 ImportSubgridV1 +1 0 1 ImportSubgridV1 +1 0 3 ImportSubgridV1 +1 1 0 ImportSubgridV1 +1 1 1 ImportSubgridV1 +1 1 3 ImportSubgridV1 +1 2 0 ImportSubgridV1 +1 2 1 ImportSubgridV1 +1 2 3 ImportSubgridV1 +1 3 0 ImportSubgridV1 +1 3 1 ImportSubgridV1 +1 3 3 ImportSubgridV1 +1 4 0 ImportSubgridV1 +1 4 1 ImportSubgridV1 +1 4 3 ImportSubgridV1 +1 5 0 ImportSubgridV1 +1 5 1 ImportSubgridV1 +1 5 3 ImportSubgridV1 +1 6 0 ImportSubgridV1 +1 6 1 ImportSubgridV1 +1 6 3 ImportSubgridV1 +1 7 0 ImportSubgridV1 +1 7 1 ImportSubgridV1 +1 7 3 ImportSubgridV1 +2 0 0 ImportSubgridV1 +2 0 1 ImportSubgridV1 +2 0 3 ImportSubgridV1 +2 1 0 ImportSubgridV1 +2 1 1 ImportSubgridV1 +2 1 3 ImportSubgridV1 +2 2 0 ImportSubgridV1 +2 2 1 ImportSubgridV1 +2 2 3 ImportSubgridV1 +2 3 0 ImportSubgridV1 +2 3 1 ImportSubgridV1 +2 3 3 ImportSubgridV1 +2 4 0 ImportSubgridV1 +2 4 1 ImportSubgridV1 +2 4 3 ImportSubgridV1 +2 5 0 ImportSubgridV1 +2 5 1 ImportSubgridV1 +2 5 3 ImportSubgridV1 +2 6 0 ImportSubgridV1 +2 6 1 ImportSubgridV1 +2 6 3 ImportSubgridV1 +2 7 0 ImportSubgridV1 +2 7 1 ImportSubgridV1 +2 7 3 ImportSubgridV1 +3 0 0 ImportSubgridV1 +3 0 2 ImportSubgridV1 +3 0 4 ImportSubgridV1 +3 1 0 ImportSubgridV1 +3 1 2 ImportSubgridV1 +3 1 4 ImportSubgridV1 +3 2 0 ImportSubgridV1 +3 2 2 ImportSubgridV1 +3 2 4 ImportSubgridV1 +3 3 0 ImportSubgridV1 +3 3 2 ImportSubgridV1 +3 3 4 ImportSubgridV1 +3 4 0 ImportSubgridV1 +3 4 2 ImportSubgridV1 +3 4 4 ImportSubgridV1 +3 5 0 ImportSubgridV1 +3 5 2 ImportSubgridV1 +3 5 4 ImportSubgridV1 +3 6 0 ImportSubgridV1 +3 6 2 ImportSubgridV1 +3 6 4 ImportSubgridV1 +3 7 0 ImportSubgridV1 +3 7 2 ImportSubgridV1 +3 7 4 ImportSubgridV1 +4 0 0 ImportSubgridV1 +4 0 2 ImportSubgridV1 +4 0 4 ImportSubgridV1 +4 1 0 ImportSubgridV1 +4 1 2 ImportSubgridV1 +4 1 4 ImportSubgridV1 +4 2 0 ImportSubgridV1 +4 2 2 ImportSubgridV1 +4 2 4 ImportSubgridV1 +4 3 0 ImportSubgridV1 +4 3 2 ImportSubgridV1 +4 3 4 ImportSubgridV1 +4 4 0 ImportSubgridV1 +4 4 2 ImportSubgridV1 +4 4 4 ImportSubgridV1 +4 5 0 ImportSubgridV1 +4 5 2 ImportSubgridV1 +4 5 4 ImportSubgridV1 +4 6 0 ImportSubgridV1 +4 6 2 ImportSubgridV1 +4 6 4 ImportSubgridV1 +4 7 0 ImportSubgridV1 +4 7 2 ImportSubgridV1 +4 7 4 ImportSubgridV1 "; -const TYPE_SHOW_EMPTY_STR: &str = "o b c type --+-+-+------------------- -0 0 0 PackedQ1X2SubgridV1 +const TYPE_SHOW_EMPTY_STR: &str = "o b c type +-+-+-+--------------- +0 0 0 ImportSubgridV1 0 0 1 EmptySubgridV1 0 0 2 EmptySubgridV1 0 0 3 EmptySubgridV1 0 0 4 EmptySubgridV1 -0 1 0 PackedQ1X2SubgridV1 +0 1 0 ImportSubgridV1 0 1 1 EmptySubgridV1 0 1 2 EmptySubgridV1 0 1 3 EmptySubgridV1 0 1 4 EmptySubgridV1 -0 2 0 PackedQ1X2SubgridV1 +0 2 0 ImportSubgridV1 0 2 1 EmptySubgridV1 0 2 2 EmptySubgridV1 0 2 3 EmptySubgridV1 0 2 4 EmptySubgridV1 -0 3 0 PackedQ1X2SubgridV1 +0 3 0 ImportSubgridV1 0 3 1 EmptySubgridV1 0 3 2 EmptySubgridV1 0 3 3 EmptySubgridV1 0 3 4 EmptySubgridV1 -0 4 0 PackedQ1X2SubgridV1 +0 4 0 ImportSubgridV1 0 4 1 EmptySubgridV1 0 4 2 EmptySubgridV1 0 4 3 EmptySubgridV1 0 4 4 EmptySubgridV1 -0 5 0 PackedQ1X2SubgridV1 +0 5 0 ImportSubgridV1 0 5 1 EmptySubgridV1 0 5 2 EmptySubgridV1 0 5 3 EmptySubgridV1 0 5 4 EmptySubgridV1 -0 6 0 PackedQ1X2SubgridV1 +0 6 0 ImportSubgridV1 0 6 1 EmptySubgridV1 0 6 2 EmptySubgridV1 0 6 3 EmptySubgridV1 0 6 4 EmptySubgridV1 -0 7 0 PackedQ1X2SubgridV1 +0 7 0 ImportSubgridV1 0 7 1 EmptySubgridV1 0 7 2 EmptySubgridV1 0 7 3 EmptySubgridV1 0 7 4 EmptySubgridV1 -1 0 0 PackedQ1X2SubgridV1 -1 0 1 PackedQ1X2SubgridV1 +1 0 0 ImportSubgridV1 +1 0 1 ImportSubgridV1 1 0 2 EmptySubgridV1 -1 0 3 PackedQ1X2SubgridV1 +1 0 3 ImportSubgridV1 1 0 4 EmptySubgridV1 -1 1 0 PackedQ1X2SubgridV1 -1 1 1 PackedQ1X2SubgridV1 +1 1 0 ImportSubgridV1 +1 1 1 ImportSubgridV1 1 1 2 EmptySubgridV1 -1 1 3 PackedQ1X2SubgridV1 +1 1 3 ImportSubgridV1 1 1 4 EmptySubgridV1 -1 2 0 PackedQ1X2SubgridV1 -1 2 1 PackedQ1X2SubgridV1 +1 2 0 ImportSubgridV1 +1 2 1 ImportSubgridV1 1 2 2 EmptySubgridV1 -1 2 3 PackedQ1X2SubgridV1 +1 2 3 ImportSubgridV1 1 2 4 EmptySubgridV1 -1 3 0 PackedQ1X2SubgridV1 -1 3 1 PackedQ1X2SubgridV1 +1 3 0 ImportSubgridV1 +1 3 1 ImportSubgridV1 1 3 2 EmptySubgridV1 -1 3 3 PackedQ1X2SubgridV1 +1 3 3 ImportSubgridV1 1 3 4 EmptySubgridV1 -1 4 0 PackedQ1X2SubgridV1 -1 4 1 PackedQ1X2SubgridV1 +1 4 0 ImportSubgridV1 +1 4 1 ImportSubgridV1 1 4 2 EmptySubgridV1 -1 4 3 PackedQ1X2SubgridV1 +1 4 3 ImportSubgridV1 1 4 4 EmptySubgridV1 -1 5 0 PackedQ1X2SubgridV1 -1 5 1 PackedQ1X2SubgridV1 +1 5 0 ImportSubgridV1 +1 5 1 ImportSubgridV1 1 5 2 EmptySubgridV1 -1 5 3 PackedQ1X2SubgridV1 +1 5 3 ImportSubgridV1 1 5 4 EmptySubgridV1 -1 6 0 PackedQ1X2SubgridV1 -1 6 1 PackedQ1X2SubgridV1 +1 6 0 ImportSubgridV1 +1 6 1 ImportSubgridV1 1 6 2 EmptySubgridV1 -1 6 3 PackedQ1X2SubgridV1 +1 6 3 ImportSubgridV1 1 6 4 EmptySubgridV1 -1 7 0 PackedQ1X2SubgridV1 -1 7 1 PackedQ1X2SubgridV1 +1 7 0 ImportSubgridV1 +1 7 1 ImportSubgridV1 1 7 2 EmptySubgridV1 -1 7 3 PackedQ1X2SubgridV1 +1 7 3 ImportSubgridV1 1 7 4 EmptySubgridV1 -2 0 0 PackedQ1X2SubgridV1 -2 0 1 PackedQ1X2SubgridV1 +2 0 0 ImportSubgridV1 +2 0 1 ImportSubgridV1 2 0 2 EmptySubgridV1 -2 0 3 PackedQ1X2SubgridV1 +2 0 3 ImportSubgridV1 2 0 4 EmptySubgridV1 -2 1 0 PackedQ1X2SubgridV1 -2 1 1 PackedQ1X2SubgridV1 +2 1 0 ImportSubgridV1 +2 1 1 ImportSubgridV1 2 1 2 EmptySubgridV1 -2 1 3 PackedQ1X2SubgridV1 +2 1 3 ImportSubgridV1 2 1 4 EmptySubgridV1 -2 2 0 PackedQ1X2SubgridV1 -2 2 1 PackedQ1X2SubgridV1 +2 2 0 ImportSubgridV1 +2 2 1 ImportSubgridV1 2 2 2 EmptySubgridV1 -2 2 3 PackedQ1X2SubgridV1 +2 2 3 ImportSubgridV1 2 2 4 EmptySubgridV1 -2 3 0 PackedQ1X2SubgridV1 -2 3 1 PackedQ1X2SubgridV1 +2 3 0 ImportSubgridV1 +2 3 1 ImportSubgridV1 2 3 2 EmptySubgridV1 -2 3 3 PackedQ1X2SubgridV1 +2 3 3 ImportSubgridV1 2 3 4 EmptySubgridV1 -2 4 0 PackedQ1X2SubgridV1 -2 4 1 PackedQ1X2SubgridV1 +2 4 0 ImportSubgridV1 +2 4 1 ImportSubgridV1 2 4 2 EmptySubgridV1 -2 4 3 PackedQ1X2SubgridV1 +2 4 3 ImportSubgridV1 2 4 4 EmptySubgridV1 -2 5 0 PackedQ1X2SubgridV1 -2 5 1 PackedQ1X2SubgridV1 +2 5 0 ImportSubgridV1 +2 5 1 ImportSubgridV1 2 5 2 EmptySubgridV1 -2 5 3 PackedQ1X2SubgridV1 +2 5 3 ImportSubgridV1 2 5 4 EmptySubgridV1 -2 6 0 PackedQ1X2SubgridV1 -2 6 1 PackedQ1X2SubgridV1 +2 6 0 ImportSubgridV1 +2 6 1 ImportSubgridV1 2 6 2 EmptySubgridV1 -2 6 3 PackedQ1X2SubgridV1 +2 6 3 ImportSubgridV1 2 6 4 EmptySubgridV1 -2 7 0 PackedQ1X2SubgridV1 -2 7 1 PackedQ1X2SubgridV1 +2 7 0 ImportSubgridV1 +2 7 1 ImportSubgridV1 2 7 2 EmptySubgridV1 -2 7 3 PackedQ1X2SubgridV1 +2 7 3 ImportSubgridV1 2 7 4 EmptySubgridV1 -3 0 0 PackedQ1X2SubgridV1 +3 0 0 ImportSubgridV1 3 0 1 EmptySubgridV1 -3 0 2 PackedQ1X2SubgridV1 +3 0 2 ImportSubgridV1 3 0 3 EmptySubgridV1 -3 0 4 PackedQ1X2SubgridV1 -3 1 0 PackedQ1X2SubgridV1 +3 0 4 ImportSubgridV1 +3 1 0 ImportSubgridV1 3 1 1 EmptySubgridV1 -3 1 2 PackedQ1X2SubgridV1 +3 1 2 ImportSubgridV1 3 1 3 EmptySubgridV1 -3 1 4 PackedQ1X2SubgridV1 -3 2 0 PackedQ1X2SubgridV1 +3 1 4 ImportSubgridV1 +3 2 0 ImportSubgridV1 3 2 1 EmptySubgridV1 -3 2 2 PackedQ1X2SubgridV1 +3 2 2 ImportSubgridV1 3 2 3 EmptySubgridV1 -3 2 4 PackedQ1X2SubgridV1 -3 3 0 PackedQ1X2SubgridV1 +3 2 4 ImportSubgridV1 +3 3 0 ImportSubgridV1 3 3 1 EmptySubgridV1 -3 3 2 PackedQ1X2SubgridV1 +3 3 2 ImportSubgridV1 3 3 3 EmptySubgridV1 -3 3 4 PackedQ1X2SubgridV1 -3 4 0 PackedQ1X2SubgridV1 +3 3 4 ImportSubgridV1 +3 4 0 ImportSubgridV1 3 4 1 EmptySubgridV1 -3 4 2 PackedQ1X2SubgridV1 +3 4 2 ImportSubgridV1 3 4 3 EmptySubgridV1 -3 4 4 PackedQ1X2SubgridV1 -3 5 0 PackedQ1X2SubgridV1 +3 4 4 ImportSubgridV1 +3 5 0 ImportSubgridV1 3 5 1 EmptySubgridV1 -3 5 2 PackedQ1X2SubgridV1 +3 5 2 ImportSubgridV1 3 5 3 EmptySubgridV1 -3 5 4 PackedQ1X2SubgridV1 -3 6 0 PackedQ1X2SubgridV1 +3 5 4 ImportSubgridV1 +3 6 0 ImportSubgridV1 3 6 1 EmptySubgridV1 -3 6 2 PackedQ1X2SubgridV1 +3 6 2 ImportSubgridV1 3 6 3 EmptySubgridV1 -3 6 4 PackedQ1X2SubgridV1 -3 7 0 PackedQ1X2SubgridV1 +3 6 4 ImportSubgridV1 +3 7 0 ImportSubgridV1 3 7 1 EmptySubgridV1 -3 7 2 PackedQ1X2SubgridV1 +3 7 2 ImportSubgridV1 3 7 3 EmptySubgridV1 -3 7 4 PackedQ1X2SubgridV1 -4 0 0 PackedQ1X2SubgridV1 +3 7 4 ImportSubgridV1 +4 0 0 ImportSubgridV1 4 0 1 EmptySubgridV1 -4 0 2 PackedQ1X2SubgridV1 +4 0 2 ImportSubgridV1 4 0 3 EmptySubgridV1 -4 0 4 PackedQ1X2SubgridV1 -4 1 0 PackedQ1X2SubgridV1 +4 0 4 ImportSubgridV1 +4 1 0 ImportSubgridV1 4 1 1 EmptySubgridV1 -4 1 2 PackedQ1X2SubgridV1 +4 1 2 ImportSubgridV1 4 1 3 EmptySubgridV1 -4 1 4 PackedQ1X2SubgridV1 -4 2 0 PackedQ1X2SubgridV1 +4 1 4 ImportSubgridV1 +4 2 0 ImportSubgridV1 4 2 1 EmptySubgridV1 -4 2 2 PackedQ1X2SubgridV1 +4 2 2 ImportSubgridV1 4 2 3 EmptySubgridV1 -4 2 4 PackedQ1X2SubgridV1 -4 3 0 PackedQ1X2SubgridV1 +4 2 4 ImportSubgridV1 +4 3 0 ImportSubgridV1 4 3 1 EmptySubgridV1 -4 3 2 PackedQ1X2SubgridV1 +4 3 2 ImportSubgridV1 4 3 3 EmptySubgridV1 -4 3 4 PackedQ1X2SubgridV1 -4 4 0 PackedQ1X2SubgridV1 +4 3 4 ImportSubgridV1 +4 4 0 ImportSubgridV1 4 4 1 EmptySubgridV1 -4 4 2 PackedQ1X2SubgridV1 +4 4 2 ImportSubgridV1 4 4 3 EmptySubgridV1 -4 4 4 PackedQ1X2SubgridV1 -4 5 0 PackedQ1X2SubgridV1 +4 4 4 ImportSubgridV1 +4 5 0 ImportSubgridV1 4 5 1 EmptySubgridV1 -4 5 2 PackedQ1X2SubgridV1 +4 5 2 ImportSubgridV1 4 5 3 EmptySubgridV1 -4 5 4 PackedQ1X2SubgridV1 -4 6 0 PackedQ1X2SubgridV1 +4 5 4 ImportSubgridV1 +4 6 0 ImportSubgridV1 4 6 1 EmptySubgridV1 -4 6 2 PackedQ1X2SubgridV1 +4 6 2 ImportSubgridV1 4 6 3 EmptySubgridV1 -4 6 4 PackedQ1X2SubgridV1 -4 7 0 PackedQ1X2SubgridV1 +4 6 4 ImportSubgridV1 +4 7 0 ImportSubgridV1 4 7 1 EmptySubgridV1 -4 7 2 PackedQ1X2SubgridV1 +4 7 2 ImportSubgridV1 4 7 3 EmptySubgridV1 -4 7 4 PackedQ1X2SubgridV1 +4 7 4 ImportSubgridV1 "; const X0_STR: &str = "o b c x0 From aeab8d2e2612b8d3675f8c864ada6a8421fbb709 Mon Sep 17 00:00:00 2001 From: t7phy Date: Thu, 17 Oct 2024 12:05:36 +0200 Subject: [PATCH 180/213] Make 7 and 9-pt variation dependent on scales in grid --- pineappl/src/empty_subgrid.rs | 2 +- pineappl/src/evolution.rs | 2 +- pineappl/src/grid.rs | 4 +-- pineappl/src/lib.rs | 4 +-- pineappl/src/subgrid.rs | 2 +- pineappl/src/v0.rs | 7 ++--- pineappl_cli/src/helpers.rs | 44 ++++++++++++++++------------- pineappl_cli/src/import/applgrid.rs | 2 +- pineappl_cli/src/import/fastnlo.rs | 2 +- pineappl_cli/src/import/fktable.rs | 2 +- pineappl_cli/src/write.rs | 4 ++- 11 files changed, 39 insertions(+), 36 deletions(-) diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 0f7db160..907d10cb 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -55,8 +55,8 @@ impl Subgrid for EmptySubgridV1 { #[cfg(test)] mod tests { use super::*; - use crate::packed_array::PackedArray; use crate::import_subgrid::ImportSubgridV1; + use crate::packed_array::PackedArray; use crate::v0; #[test] diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 7a865894..0669dc8a 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -3,8 +3,8 @@ use super::boc::{Channel, Kinematics, Order}; use super::convolutions::ConvType; use super::grid::{Grid, GridError}; -use super::packed_array::PackedArray; use super::import_subgrid::ImportSubgridV1; +use super::packed_array::PackedArray; use super::pids::PidBasis; use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use float_cmp::approx_eq; diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 1bc73a81..bc3f1bd9 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -6,9 +6,9 @@ use super::convolutions::{Conv, ConvType, ConvolutionCache}; use super::empty_subgrid::EmptySubgridV1; use super::evolution::{self, AlphasTable, EvolveInfo, OperatorSliceInfo}; use super::fk_table::FkTable; -use super::interpolation::Interp; -use super::interp_subgrid::InterpSubgridV1; use super::import_subgrid::ImportSubgridV1; +use super::interp_subgrid::InterpSubgridV1; +use super::interpolation::Interp; use super::pids::PidBasis; use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; use super::v0; diff --git a/pineappl/src/lib.rs b/pineappl/src/lib.rs index ac05684c..f225dff0 100644 --- a/pineappl/src/lib.rs +++ b/pineappl/src/lib.rs @@ -43,9 +43,9 @@ pub mod empty_subgrid; pub mod evolution; pub mod fk_table; pub mod grid; -pub mod interpolation; +pub mod import_subgrid; pub mod interp_subgrid; +pub mod interpolation; pub mod packed_array; -pub mod import_subgrid; pub mod pids; pub mod subgrid; diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index cb3bc91c..aefad922 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -1,8 +1,8 @@ //! Module containing the trait `Subgrid` and supporting structs. use super::empty_subgrid::EmptySubgridV1; -use super::interp_subgrid::InterpSubgridV1; use super::import_subgrid::ImportSubgridV1; +use super::interp_subgrid::InterpSubgridV1; use enum_dispatch::enum_dispatch; // use float_cmp::approx_eq; // use ndarray::Array3; diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index 0d45d643..b601ecf9 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -3,9 +3,9 @@ use super::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use super::convolutions::{Conv, ConvType}; use super::empty_subgrid::EmptySubgridV1; use super::grid::{Grid, GridError, Mmv4, MoreMembers}; +use super::import_subgrid::ImportSubgridV1; use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use super::packed_array::PackedArray; -use super::import_subgrid::ImportSubgridV1; use super::pids::PidBasis; use super::subgrid::{Mu2, NodeValues}; use ndarray::Array3; @@ -162,10 +162,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result .into_iter() .collect(), interps: default_interps(convolutions.len()), - convolutions: convolutions - .into_iter() - .flatten() - .collect(), + convolutions: convolutions.into_iter().flatten().collect(), pid_basis: grid .key_values() .and_then(|kv| kv.get("lumi_id_types")) diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 747d5aaa..14a82383 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -2,6 +2,7 @@ use super::GlobalConfiguration; use anyhow::{anyhow, ensure, Context, Error, Result}; use lhapdf::{Pdf, PdfSet}; use ndarray::{Array3, Ix3}; +use pineappl::boc::{ScaleFuncForm, Scales}; use pineappl::convolutions::{Conv, ConvType, ConvolutionCache}; use pineappl::grid::Grid; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; @@ -141,17 +142,17 @@ pub const SCALES_VECTOR_REN_FAC: [(f64, f64, f64); 9] = [ (0.5, 2.0, 1.0), ]; -// const SCALES_VECTOR_REN_FRG: [(f64, f64, f64); 9] = [ -// (1.0, 1.0, 1.0), -// (2.0, 1.0, 2.0), -// (0.5, 1.0, 0.5), -// (2.0, 1.0, 1.0), -// (1.0, 1.0, 2.0), -// (0.5, 1.0, 1.0), -// (1.0, 1.0, 0.5), -// (2.0, 1.0, 0.5), -// (0.5, 1.0, 2.0), -// ]; +const SCALES_VECTOR_REN_FRG: [(f64, f64, f64); 9] = [ + (1.0, 1.0, 1.0), + (2.0, 1.0, 2.0), + (0.5, 1.0, 0.5), + (2.0, 1.0, 1.0), + (1.0, 1.0, 2.0), + (0.5, 1.0, 1.0), + (1.0, 1.0, 0.5), + (2.0, 1.0, 0.5), + (0.5, 1.0, 2.0), +]; const SCALES_VECTOR_27: [(f64, f64, f64); 27] = [ (1.0, 1.0, 1.0), @@ -348,15 +349,18 @@ pub fn convolve_scales( } } -pub fn scales_vector(_grid: &Grid, scales: usize) -> &[(f64, f64, f64)] { - match scales { - 1 => &SCALES_VECTOR_27[0..1], - 3 => &SCALES_VECTOR_27[0..3], - // TODO: fix 7 and 9 for cases where there is a fragmentation scale - 7 => &SCALES_VECTOR_REN_FAC[0..7], - 9 => &SCALES_VECTOR_REN_FAC[..], - 17 => &SCALES_VECTOR_27[0..17], - 27 => &SCALES_VECTOR_27[..], +pub fn scales_vector(grid: &Grid, scales: usize) -> &[(f64, f64, f64)] { + let Scales { fac, frg, .. } = grid.scales(); + + match (fac, frg, scales) { + (_, _, 1) => &SCALES_VECTOR_27[0..1], + (_, _, 3) => &SCALES_VECTOR_27[0..3], + (_, ScaleFuncForm::NoScale, 7) => &SCALES_VECTOR_REN_FAC[0..7], + (_, ScaleFuncForm::NoScale, 9) => &SCALES_VECTOR_REN_FAC[..], + (ScaleFuncForm::NoScale, _, 7) => &SCALES_VECTOR_REN_FRG[0..7], + (ScaleFuncForm::NoScale, _, 9) => &SCALES_VECTOR_REN_FRG[..], + (_, _, 17) => &SCALES_VECTOR_27[0..17], + (_, _, 27) => &SCALES_VECTOR_27[..], _ => unreachable!(), } } diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 66fc2842..a3c03224 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -3,9 +3,9 @@ use lhapdf::Pdf; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; -use pineappl::import_subgrid::ImportSubgridV1; use pineappl::pids::PidBasis; use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_applgrid::ffi::{self, grid}; diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 82691d3f..0b3a260f 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -5,9 +5,9 @@ use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; -use pineappl::import_subgrid::ImportSubgridV1; use pineappl::pids::PidBasis; use pineappl::subgrid::{Mu2, NodeValues}; use pineappl_fastnlo::ffi::{ diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index 5517bc1a..d4bce61f 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -5,9 +5,9 @@ use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::channel; use pineappl::convolutions::{Conv, ConvType}; use pineappl::grid::Grid; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; -use pineappl::import_subgrid::ImportSubgridV1; use pineappl::pids::PidBasis; use pineappl::subgrid::NodeValues; use std::fs::File; diff --git a/pineappl_cli/src/write.rs b/pineappl_cli/src/write.rs index 20af0a85..8435a551 100644 --- a/pineappl_cli/src/write.rs +++ b/pineappl_cli/src/write.rs @@ -581,7 +581,9 @@ impl Subcommand for Opts { } OpsArg::ScaleByBin(factors) => grid.scale_by_bin(factors), OpsArg::ScaleByOrder(factors) => { - grid.scale_by_order(factors[0], factors[1], factors[2], factors[3], factors[4], 1.0); + grid.scale_by_order( + factors[0], factors[1], factors[2], factors[3], factors[4], 1.0, + ); } OpsArg::SetKeyValue(key_value) => { grid.metadata_mut() From d050130ce079ef8780eb407431babc043e257ab2 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 17 Oct 2024 14:13:45 +0200 Subject: [PATCH 181/213] Test more scale variations --- pineappl_cli/tests/uncert.rs | 137 ++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 2 deletions(-) diff --git a/pineappl_cli/tests/uncert.rs b/pineappl_cli/tests/uncert.rs index a2923f5d..b0bfcd63 100644 --- a/pineappl_cli/tests/uncert.rs +++ b/pineappl_cli/tests/uncert.rs @@ -79,7 +79,21 @@ const ORDERS_A2_AS1A2_STR: &str = "b etal dsig/detal NNPDF31_nlo_as_0118 7 4 4.5 2.7737493e1 2.7724826e1 -2.77 2.77 "; -const SCALE_ABS_STR: &str = +const SCALE_ABS_3_STR: &str = "b etal dsig/detal 1,1,1 2,2,2 0.5,0.5,0.5 + [] [pb] (r,f,a) (r,f,a) (r,f,a) + [pb] [pb] [pb] +-+----+----+-----------+-----------+-----------+----------- +0 2 2.25 7.5459110e2 7.5459110e2 7.6745431e2 7.4296019e2 +1 2.25 2.5 6.9028342e2 6.9028342e2 7.0221920e2 6.7923774e2 +2 2.5 2.75 6.0025198e2 6.0025198e2 6.1056383e2 5.9046454e2 +3 2.75 3 4.8552235e2 4.8552235e2 4.9366919e2 4.7761552e2 +4 3 3.25 3.6195456e2 3.6195456e2 3.6783089e2 3.5611822e2 +5 3.25 3.5 2.4586691e2 2.4586691e2 2.4967698e2 2.4198770e2 +6 3.5 4 1.1586851e2 1.1586851e2 1.1746280e2 1.1418227e2 +7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 +"; + +const SCALE_ABS_7_STR: &str = "b etal dsig/detal 1,1,1 2,2,1 0.5,0.5,1 2,1,1 1,2,1 0.5,1,1 1,0.5,1 [] [pb] (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) [pb] [pb] [pb] [pb] [pb] [pb] [pb] @@ -94,6 +108,50 @@ const SCALE_ABS_STR: &str = 7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 2.7002241e1 2.8306905e1 2.8157972e1 2.6562471e1 "; +const SCALE_ABS_9_STR: &str = +"b etal dsig/detal 1,1,1 2,2,1 0.5,0.5,1 2,1,1 1,2,1 0.5,1,1 1,0.5,1 2,0.5,1 0.5,2,1 + [] [pb] (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) + [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] +-+----+----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+----------- +0 2 2.25 7.5459110e2 7.5459110e2 7.6745431e2 7.4296019e2 7.4384068e2 7.7529764e2 7.6796494e2 7.2595107e2 7.1227848e2 7.8505497e2 +1 2.25 2.5 6.9028342e2 6.9028342e2 7.0221920e2 6.7923774e2 6.8058382e2 7.0957480e2 7.0235002e2 6.6417441e2 6.5206593e2 7.1872540e2 +2 2.5 2.75 6.0025198e2 6.0025198e2 6.1056383e2 5.9046454e2 5.9160658e2 6.1750966e2 6.1100712e2 5.7747295e2 5.6702981e2 6.2615050e2 +3 2.75 3 4.8552235e2 4.8552235e2 4.9366919e2 4.7761552e2 4.7841022e2 4.9966237e2 4.9437007e2 4.6723687e2 4.5889411e2 5.0711808e2 +4 3 3.25 3.6195456e2 3.6195456e2 3.6783089e2 3.5611822e2 3.5652780e2 3.7261436e2 3.6870561e2 3.4843600e2 3.4226073e2 3.7856515e2 +5 3.25 3.5 2.4586691e2 2.4586691e2 2.4967698e2 2.4198770e2 2.4207028e2 2.5316566e2 2.5059003e2 2.3677625e2 2.3258708e2 2.5750568e2 +6 3.5 4 1.1586851e2 1.1586851e2 1.1746280e2 1.1418227e2 1.1396174e2 1.1930157e2 1.1824058e2 1.1166942e2 1.0964950e2 1.2158905e2 +7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 2.7002241e1 2.8306905e1 2.8157972e1 2.6562471e1 2.6041156e1 2.8953268e1 +"; + +const SCALE_ABS_17_STR: &str = "b etal dsig/detal 1,1,1 2,2,2 0.5,0.5,0.5 0.5,0.5,1 0.5,1,0.5 0.5,1,1 0.5,1,2 1,0.5,0.5 1,0.5,1 1,1,0.5 1,1,2 1,2,1 1,2,2 2,1,0.5 2,1,1 2,1,2 2,2,1 + [] [pb] (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) + [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] +-+----+----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+----------- +0 2 2.25 7.5459110e2 7.5459110e2 7.6745431e2 7.4296019e2 7.4296019e2 7.6796494e2 7.6796494e2 7.6796494e2 7.2595107e2 7.2595107e2 7.5459110e2 7.5459110e2 7.7529764e2 7.7529764e2 7.4384068e2 7.4384068e2 7.4384068e2 7.6745431e2 +1 2.25 2.5 6.9028342e2 6.9028342e2 7.0221920e2 6.7923774e2 6.7923774e2 7.0235002e2 7.0235002e2 7.0235002e2 6.6417441e2 6.6417441e2 6.9028342e2 6.9028342e2 7.0957480e2 7.0957480e2 6.8058382e2 6.8058382e2 6.8058382e2 7.0221920e2 +2 2.5 2.75 6.0025198e2 6.0025198e2 6.1056383e2 5.9046454e2 5.9046454e2 6.1100712e2 6.1100712e2 6.1100712e2 5.7747295e2 5.7747295e2 6.0025198e2 6.0025198e2 6.1750966e2 6.1750966e2 5.9160658e2 5.9160658e2 5.9160658e2 6.1056383e2 +3 2.75 3 4.8552235e2 4.8552235e2 4.9366919e2 4.7761552e2 4.7761552e2 4.9437007e2 4.9437007e2 4.9437007e2 4.6723687e2 4.6723687e2 4.8552235e2 4.8552235e2 4.9966237e2 4.9966237e2 4.7841022e2 4.7841022e2 4.7841022e2 4.9366919e2 +4 3 3.25 3.6195456e2 3.6195456e2 3.6783089e2 3.5611822e2 3.5611822e2 3.6870561e2 3.6870561e2 3.6870561e2 3.4843600e2 3.4843600e2 3.6195456e2 3.6195456e2 3.7261436e2 3.7261436e2 3.5652780e2 3.5652780e2 3.5652780e2 3.6783089e2 +5 3.25 3.5 2.4586691e2 2.4586691e2 2.4967698e2 2.4198770e2 2.4198770e2 2.5059003e2 2.5059003e2 2.5059003e2 2.3677625e2 2.3677625e2 2.4586691e2 2.4586691e2 2.5316566e2 2.5316566e2 2.4207028e2 2.4207028e2 2.4207028e2 2.4967698e2 +6 3.5 4 1.1586851e2 1.1586851e2 1.1746280e2 1.1418227e2 1.1418227e2 1.1824058e2 1.1824058e2 1.1824058e2 1.1166942e2 1.1166942e2 1.1586851e2 1.1586851e2 1.1930157e2 1.1930157e2 1.1396174e2 1.1396174e2 1.1396174e2 1.1746280e2 +7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 2.7211003e1 2.8157972e1 2.8157972e1 2.8157972e1 2.6562471e1 2.6562471e1 2.7517266e1 2.7517266e1 2.8306905e1 2.8306905e1 2.7002241e1 2.7002241e1 2.7002241e1 2.7787333e1 +"; + +const SCALE_ABS_27_STR: &str = +"b etal dsig/detal 1,1,1 2,2,2 0.5,0.5,0.5 0.5,0.5,1 0.5,1,0.5 0.5,1,1 0.5,1,2 1,0.5,0.5 1,0.5,1 1,1,0.5 1,1,2 1,2,1 1,2,2 2,1,0.5 2,1,1 2,1,2 2,2,1 2,0.5,0.5 0.5,2,0.5 1,2,0.5 2,2,0.5 2,0.5,1 0.5,2,1 0.5,0.5,2 1,0.5,2 2,0.5,2 0.5,2,2 + [] [pb] (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) (r,f,a) + [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] [pb] +-+----+----+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+----------- +0 2 2.25 7.5459110e2 7.5459110e2 7.6745431e2 7.4296019e2 7.4296019e2 7.6796494e2 7.6796494e2 7.6796494e2 7.2595107e2 7.2595107e2 7.5459110e2 7.5459110e2 7.7529764e2 7.7529764e2 7.4384068e2 7.4384068e2 7.4384068e2 7.6745431e2 7.1227848e2 7.8505497e2 7.7529764e2 7.6745431e2 7.1227848e2 7.8505497e2 7.4296019e2 7.2595107e2 7.1227848e2 7.8505497e2 +1 2.25 2.5 6.9028342e2 6.9028342e2 7.0221920e2 6.7923774e2 6.7923774e2 7.0235002e2 7.0235002e2 7.0235002e2 6.6417441e2 6.6417441e2 6.9028342e2 6.9028342e2 7.0957480e2 7.0957480e2 6.8058382e2 6.8058382e2 6.8058382e2 7.0221920e2 6.5206593e2 7.1872540e2 7.0957480e2 7.0221920e2 6.5206593e2 7.1872540e2 6.7923774e2 6.6417441e2 6.5206593e2 7.1872540e2 +2 2.5 2.75 6.0025198e2 6.0025198e2 6.1056383e2 5.9046454e2 5.9046454e2 6.1100712e2 6.1100712e2 6.1100712e2 5.7747295e2 5.7747295e2 6.0025198e2 6.0025198e2 6.1750966e2 6.1750966e2 5.9160658e2 5.9160658e2 5.9160658e2 6.1056383e2 5.6702981e2 6.2615050e2 6.1750966e2 6.1056383e2 5.6702981e2 6.2615050e2 5.9046454e2 5.7747295e2 5.6702981e2 6.2615050e2 +3 2.75 3 4.8552235e2 4.8552235e2 4.9366919e2 4.7761552e2 4.7761552e2 4.9437007e2 4.9437007e2 4.9437007e2 4.6723687e2 4.6723687e2 4.8552235e2 4.8552235e2 4.9966237e2 4.9966237e2 4.7841022e2 4.7841022e2 4.7841022e2 4.9366919e2 4.5889411e2 5.0711808e2 4.9966237e2 4.9366919e2 4.5889411e2 5.0711808e2 4.7761552e2 4.6723687e2 4.5889411e2 5.0711808e2 +4 3 3.25 3.6195456e2 3.6195456e2 3.6783089e2 3.5611822e2 3.5611822e2 3.6870561e2 3.6870561e2 3.6870561e2 3.4843600e2 3.4843600e2 3.6195456e2 3.6195456e2 3.7261436e2 3.7261436e2 3.5652780e2 3.5652780e2 3.5652780e2 3.6783089e2 3.4226073e2 3.7856515e2 3.7261436e2 3.6783089e2 3.4226073e2 3.7856515e2 3.5611822e2 3.4843600e2 3.4226073e2 3.7856515e2 +5 3.25 3.5 2.4586691e2 2.4586691e2 2.4967698e2 2.4198770e2 2.4198770e2 2.5059003e2 2.5059003e2 2.5059003e2 2.3677625e2 2.3677625e2 2.4586691e2 2.4586691e2 2.5316566e2 2.5316566e2 2.4207028e2 2.4207028e2 2.4207028e2 2.4967698e2 2.3258708e2 2.5750568e2 2.5316566e2 2.4967698e2 2.3258708e2 2.5750568e2 2.4198770e2 2.3677625e2 2.3258708e2 2.5750568e2 +6 3.5 4 1.1586851e2 1.1586851e2 1.1746280e2 1.1418227e2 1.1418227e2 1.1824058e2 1.1824058e2 1.1824058e2 1.1166942e2 1.1166942e2 1.1586851e2 1.1586851e2 1.1930157e2 1.1930157e2 1.1396174e2 1.1396174e2 1.1396174e2 1.1746280e2 1.0964950e2 1.2158905e2 1.1930157e2 1.1746280e2 1.0964950e2 1.2158905e2 1.1418227e2 1.1166942e2 1.0964950e2 1.2158905e2 +7 4 4.5 2.7517266e1 2.7517266e1 2.7787333e1 2.7211003e1 2.7211003e1 2.8157972e1 2.8157972e1 2.8157972e1 2.6562471e1 2.6562471e1 2.7517266e1 2.7517266e1 2.8306905e1 2.8306905e1 2.7002241e1 2.7002241e1 2.7002241e1 2.7787333e1 2.6041156e1 2.8953268e1 2.8306905e1 2.7787333e1 2.6041156e1 2.8953268e1 2.7211003e1 2.6562471e1 2.6041156e1 2.8953268e1 +"; + const SCALE_COV_STR: &str = "b etal dsig/detal 7pt scale (cov) [] [pb] [%] -+----+----+-----------+-------+------- @@ -242,7 +300,82 @@ fn scale_abs() { ]) .assert() .success() - .stdout(SCALE_ABS_STR); + .stdout(SCALE_ABS_7_STR); +} + +#[test] +fn scale_abs_3() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "uncert", + "--scale-abs=3", + "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .success() + .stdout(SCALE_ABS_3_STR); +} + +#[test] +fn scale_abs_7() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "uncert", + "--scale-abs=7", + "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .success() + .stdout(SCALE_ABS_7_STR); +} + +#[test] +fn scale_abs_9() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "uncert", + "--scale-abs=9", + "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .success() + .stdout(SCALE_ABS_9_STR); +} + +#[test] +fn scale_abs_17() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "uncert", + "--scale-abs=17", + "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .success() + .stdout(SCALE_ABS_17_STR); +} + +#[test] +fn scale_abs_27() { + Command::cargo_bin("pineappl") + .unwrap() + .args([ + "uncert", + "--scale-abs=27", + "../test-data/LHCB_WP_7TEV_opt.pineappl.lz4", + "NNPDF31_nlo_as_0118_luxqed", + ]) + .assert() + .success() + .stdout(SCALE_ABS_27_STR); } #[test] From 11dcd8ce814768c9b2e407304fbeafa3e229cc20 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 17 Oct 2024 14:37:37 +0200 Subject: [PATCH 182/213] Change type of `Order` members from `u32` to `u8` --- pineappl/src/boc.rs | 16 +++++++++------- pineappl/src/convolutions.rs | 4 ++-- pineappl/src/v0.rs | 9 +++++---- pineappl_capi/src/lib.rs | 17 +++++++++-------- pineappl_cli/src/analyze.rs | 4 ++-- pineappl_cli/src/channels.rs | 2 +- pineappl_cli/src/convolve.rs | 2 +- pineappl_cli/src/diff.rs | 4 ++-- pineappl_cli/src/evolve.rs | 4 ++-- pineappl_cli/src/helpers.rs | 10 +++++----- pineappl_cli/src/import.rs | 12 ++++++------ pineappl_cli/src/import/applgrid.rs | 13 ++++++++++--- pineappl_cli/src/import/fastnlo.rs | 6 +++--- pineappl_cli/src/orders.rs | 2 +- pineappl_cli/src/pull.rs | 2 +- pineappl_cli/src/uncert.rs | 2 +- pineappl_py/src/boc.rs | 8 ++++---- 17 files changed, 64 insertions(+), 53 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 17ce36b3..f69c5758 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -104,16 +104,18 @@ pub struct ParseOrderError(String); #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub struct Order { /// Exponent of the strong coupling. - pub alphas: u32, + pub alphas: u8, /// Exponent of the electromagnetic coupling. - pub alpha: u32, + pub alpha: u8, /// Exponent of the logarithm of the scale factor of the renomalization scale. - pub logxir: u32, + pub logxir: u8, /// Exponent of the logarithm of the scale factor of the initial state factorization scale. - pub logxif: u32, + pub logxif: u8, /// Exponent of the logarithm of the scale factor of the final state factorization scale /// (fragmentation scale). - pub logxia: u32, + pub logxia: u8, + // /// Reserved for future usage. + // pub other: [u8; 3], } impl FromStr for Order { @@ -193,7 +195,7 @@ impl Order { /// Constructor. This function mainly exists to have a way of constructing `Order` that is less /// verbose. #[must_use] - pub const fn new(alphas: u32, alpha: u32, logxir: u32, logxif: u32, logxia: u32) -> Self { + pub const fn new(alphas: u8, alpha: u8, logxir: u8, logxif: u8, logxia: u8) -> Self { Self { alphas, alpha, @@ -295,7 +297,7 @@ impl Order { /// assert_eq!(Order::create_mask(&orders, 1, 1, false), [true, true, true, false, false, false, false]); /// ``` #[must_use] - pub fn create_mask(orders: &[Self], max_as: u32, max_al: u32, logs: bool) -> Vec { + pub fn create_mask(orders: &[Self], max_as: u8, max_al: u8, logs: bool) -> Vec { // smallest sum of alphas and alpha let lo = orders .iter() diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 6f0a6bc0..ad8fa551 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -149,7 +149,7 @@ impl<'a> ConvolutionCache<'a> { } /// TODO - pub fn as_fx_prod(&mut self, pdg_ids: &[i32], as_order: u32, indices: &[usize]) -> f64 { + pub fn as_fx_prod(&mut self, pdg_ids: &[i32], as_order: u8, indices: &[usize]) -> f64 { // TODO: here we assume that // - indices[0] is the (squared) factorization scale, // - indices[1] is x1 and @@ -187,7 +187,7 @@ impl<'a> ConvolutionCache<'a> { }) .product(); let alphas_powers = if as_order != 0 { - self.alphas_cache[self.imur2[indices[0]]].powi(as_order.try_into().unwrap()) + self.alphas_cache[self.imur2[indices[0]]].powi(as_order.into()) } else { 1.0 }; diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index b601ecf9..afdfd374 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -148,10 +148,11 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result .orders() .iter() .map(|o| Order { - alphas: o.alphas, - alpha: o.alpha, - logxir: o.logxir, - logxif: o.logxif, + // UNWRAP: there shouldn't be orders with exponents larger than 255 + alphas: o.alphas.try_into().unwrap(), + alpha: o.alpha.try_into().unwrap(), + logxir: o.logxir.try_into().unwrap(), + logxif: o.logxif.try_into().unwrap(), logxia: 0, }) .collect(), diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index cea323de..f95e1eb6 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -670,10 +670,10 @@ pub unsafe extern "C" fn pineappl_grid_order_params(grid: *const Grid, order_par let order_params = unsafe { slice::from_raw_parts_mut(order_params, 4 * orders.len()) }; for (i, order) in orders.iter().enumerate() { - order_params[4 * i] = order.alphas; - order_params[4 * i + 1] = order.alpha; - order_params[4 * i + 2] = order.logxir; - order_params[4 * i + 3] = order.logxif; + order_params[4 * i] = order.alphas.into(); + order_params[4 * i + 1] = order.alpha.into(); + order_params[4 * i + 2] = order.logxir.into(); + order_params[4 * i + 3] = order.logxif.into(); } } @@ -731,10 +731,11 @@ pub unsafe extern "C" fn pineappl_grid_new( let orders: Vec<_> = order_params .chunks(4) .map(|s| Order { - alphas: s[0], - alpha: s[1], - logxir: s[2], - logxif: s[3], + // UNWRAP: there shouldn't be orders with exponents larger than 255 + alphas: s[0].try_into().unwrap(), + alpha: s[1].try_into().unwrap(), + logxir: s[2].try_into().unwrap(), + logxif: s[3].try_into().unwrap(), // this function doesn't support fragmentation scale logs logxia: 0, }) diff --git a/pineappl_cli/src/analyze.rs b/pineappl_cli/src/analyze.rs index 3c644c48..ac77eeaa 100644 --- a/pineappl_cli/src/analyze.rs +++ b/pineappl_cli/src/analyze.rs @@ -43,10 +43,10 @@ pub struct CkfOpts { conv_funs: ConvFuns, /// Order defining the K factors. #[arg(value_parser = helpers::parse_order)] - order: (u32, u32), + order: (u8, u8), /// Normalizing orders of the K factors. #[arg(value_delimiter = ',', value_parser = helpers::parse_order)] - orders_den: Vec<(u32, u32)>, + orders_den: Vec<(u8, u8)>, /// The maximum number of channels displayed. #[arg( default_value_t = 10, diff --git a/pineappl_cli/src/channels.rs b/pineappl_cli/src/channels.rs index 53173a1e..f0bee66c 100644 --- a/pineappl_cli/src/channels.rs +++ b/pineappl_cli/src/channels.rs @@ -49,7 +49,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders: Vec<(u32, u32)>, + orders: Vec<(u8, u8)>, /// Do not sort the channels according to their size. #[arg(long)] dont_sort: bool, diff --git a/pineappl_cli/src/convolve.rs b/pineappl_cli/src/convolve.rs index 5377abc5..466e286e 100644 --- a/pineappl_cli/src/convolve.rs +++ b/pineappl_cli/src/convolve.rs @@ -37,7 +37,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders: Vec<(u32, u32)>, + orders: Vec<(u8, u8)>, /// Set the number of fractional digits shown for absolute numbers. #[arg(default_value_t = 7, long, value_name = "ABS")] digits_abs: usize, diff --git a/pineappl_cli/src/diff.rs b/pineappl_cli/src/diff.rs index 4e6e4858..a202778c 100644 --- a/pineappl_cli/src/diff.rs +++ b/pineappl_cli/src/diff.rs @@ -34,7 +34,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders1: Vec<(u32, u32)>, + orders1: Vec<(u8, u8)>, /// Select orders of the second grid. #[arg( long, @@ -42,7 +42,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders2: Vec<(u32, u32)>, + orders2: Vec<(u8, u8)>, /// Scale all results of the first grid. #[arg(long, default_value = "1.0")] scale1: f64, diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index 6047657e..73b4adef 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -440,7 +440,7 @@ fn evolve_grid( grid: &Grid, ekos: &[&Path], use_alphas_from: &Pdf, - orders: &[(u32, u32)], + orders: &[(u8, u8)], xir: f64, xif: f64, xia: f64, @@ -518,7 +518,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders: Vec<(u32, u32)>, + orders: Vec<(u8, u8)>, /// Rescale the renormalization scale with this factor. #[arg(default_value_t = 1.0, long)] xir: f64, diff --git a/pineappl_cli/src/helpers.rs b/pineappl_cli/src/helpers.rs index 14a82383..c25b8033 100644 --- a/pineappl_cli/src/helpers.rs +++ b/pineappl_cli/src/helpers.rs @@ -224,7 +224,7 @@ pub enum ConvoluteMode { pub fn convolve_scales( grid: &Grid, conv_funs: &mut [Pdf], - orders: &[(u32, u32)], + orders: &[(u8, u8)], bins: &[usize], channels: &[bool], scales: &[(f64, f64, f64)], @@ -368,7 +368,7 @@ pub fn scales_vector(grid: &Grid, scales: usize) -> &[(f64, f64, f64)] { pub fn convolve( grid: &Grid, conv_funs: &mut [Pdf], - orders: &[(u32, u32)], + orders: &[(u8, u8)], bins: &[usize], lumis: &[bool], scales: usize, @@ -502,7 +502,7 @@ pub fn parse_integer_range(range: &str) -> Result> { } } -pub fn parse_order(order: &str) -> Result<(u32, u32)> { +pub fn parse_order(order: &str) -> Result<(u8, u8)> { let mut alphas = 0; let mut alpha = 0; @@ -520,14 +520,14 @@ pub fn parse_order(order: &str) -> Result<(u32, u32)> { .chars() .take_while(|c| c.is_numeric()) .count(); - alphas = str::parse::(&order[index + 2..index + 2 + len]) + alphas = str::parse::(&order[index + 2..index + 2 + len]) .context(format!("unable to parse order '{order}'"))?; } else { let len = order[index + 1..] .chars() .take_while(|c| c.is_numeric()) .count(); - alpha = str::parse::(&order[index + 1..index + 1 + len]) + alpha = str::parse::(&order[index + 1..index + 1 + len]) .context(format!("unable to parse order '{order}'"))?; } } diff --git a/pineappl_cli/src/import.rs b/pineappl_cli/src/import.rs index 7fb77871..c6ae680e 100644 --- a/pineappl_cli/src/import.rs +++ b/pineappl_cli/src/import.rs @@ -18,7 +18,7 @@ mod fktable; #[cfg(feature = "applgrid")] fn convert_applgrid( input: &Path, - alpha: u32, + alpha: u8, conv_funs: &mut [Pdf], _: usize, ) -> Result<(&'static str, Grid, Vec, usize)> { @@ -36,7 +36,7 @@ fn convert_applgrid( #[cfg(not(feature = "applgrid"))] fn convert_applgrid( _: &Path, - _: u32, + _: u8, _: &mut [Pdf], _: usize, ) -> Result<(&'static str, Grid, Vec, usize)> { @@ -48,7 +48,7 @@ fn convert_applgrid( #[cfg(feature = "fastnlo")] fn convert_fastnlo( input: &Path, - alpha: u32, + alpha: u8, conv_funs: &ConvFuns, member: usize, scales: usize, @@ -117,7 +117,7 @@ fn convert_fastnlo( #[cfg(not(feature = "fastnlo"))] fn convert_fastnlo( _: &Path, - _: u32, + _: u8, _: &ConvFuns, _: usize, _: usize, @@ -145,7 +145,7 @@ fn convert_fktable(_: &Path) -> Result<(&'static str, Grid, Vec, usize)> { fn convert_grid( input: &Path, - alpha: u32, + alpha: u8, conv_funs: &mut [Pdf], fun_names: &ConvFuns, member: usize, @@ -211,7 +211,7 @@ pub struct Opts { conv_funs: ConvFuns, /// LO coupling power in alpha. #[arg(default_value_t = 0, long)] - alpha: u32, + alpha: u8, /// Relative threshold between the table and the converted grid when comparison fails. #[arg(default_value = "1e-10", long)] accuracy: f64, diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index a3c03224..f9161426 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -71,12 +71,16 @@ fn reconstruct_channels(grid: &grid, order: i32) -> Vec { channels.into_iter().map(Channel::new).collect() } -pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { +pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u8) -> Result { let bin_limits: Vec<_> = (0..=grid.Nobs_internal()) .map(|i| grid.obslow_internal(i)) .collect(); - let leading_order: u32 = grid.leadingOrder().try_into().unwrap(); + let leading_order: u8 = grid + .leadingOrder() + .try_into() + // UNWRAP: exponents of orders shouldn't be larger than 255 + .unwrap(); let orders; let alphas_factor; @@ -99,7 +103,10 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u32) -> Result { orders = (0..=grid.nloops()) .map(|power| { Order::new( - leading_order + u32::try_from(power).unwrap(), + leading_order + + u8::try_from(power) + // UNWRAP: exponents of orders shouldn't be larger than 255 + .unwrap(), alpha, 0, 0, diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 0b3a260f..03f4b77a 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -94,7 +94,7 @@ fn convert_coeff_add_fix( table: &fastNLOCoeffAddFix, comb: &fastNLOPDFLinearCombinations, bins: usize, - alpha: u32, + alpha: u8, ) -> Grid { let table_as_add_base = ffi::downcast_coeff_add_fix_to_base(table); @@ -288,7 +288,7 @@ fn convert_coeff_add_flex( _mur_ff: EScaleFunctionalForm, _muf_ff: EScaleFunctionalForm, _bins: usize, - _alpha: u32, + _alpha: u8, _ipub_units: i32, ) -> Grid { todo!() @@ -483,7 +483,7 @@ fn convert_coeff_add_flex( // grid } -pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u32) -> Result { +pub fn convert_fastnlo_table(file: &fastNLOLHAPDF, alpha: u8) -> Result { let file_as_reader = ffi::downcast_lhapdf_to_reader(file); let file_as_table = ffi::downcast_lhapdf_to_table(file); diff --git a/pineappl_cli/src/orders.rs b/pineappl_cli/src/orders.rs index 8a229251..a9b37156 100644 --- a/pineappl_cli/src/orders.rs +++ b/pineappl_cli/src/orders.rs @@ -29,7 +29,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - normalize: Vec<(u32, u32)>, + normalize: Vec<(u8, u8)>, /// Set the number of fractional digits shown for absolute numbers. #[arg(default_value_t = 7, long, value_name = "ABS")] digits_abs: usize, diff --git a/pineappl_cli/src/pull.rs b/pineappl_cli/src/pull.rs index 0ec6fe65..8707e9e3 100644 --- a/pineappl_cli/src/pull.rs +++ b/pineappl_cli/src/pull.rs @@ -39,7 +39,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders: Vec<(u32, u32)>, + orders: Vec<(u8, u8)>, /// Number of threads to utilize. #[arg(default_value_t = thread::available_parallelism().map_or(1, NonZeroUsize::get), long)] threads: usize, diff --git a/pineappl_cli/src/uncert.rs b/pineappl_cli/src/uncert.rs index 149faeec..8f94c389 100644 --- a/pineappl_cli/src/uncert.rs +++ b/pineappl_cli/src/uncert.rs @@ -79,7 +79,7 @@ pub struct Opts { value_delimiter = ',', value_parser = helpers::parse_order )] - orders: Vec<(u32, u32)>, + orders: Vec<(u8, u8)>, /// Number of threads to utilize. #[arg(default_value_t = thread::available_parallelism().map_or(1, NonZeroUsize::get), long)] threads: usize, diff --git a/pineappl_py/src/boc.rs b/pineappl_py/src/boc.rs index 83f47eea..1440adb5 100644 --- a/pineappl_py/src/boc.rs +++ b/pineappl_py/src/boc.rs @@ -105,7 +105,7 @@ impl PyOrder { /// power of :math:`\ln(\xi_a)` #[new] #[must_use] - pub const fn new_order(alphas: u32, alpha: u32, logxir: u32, logxif: u32, logxia: u32) -> Self { + pub const fn new_order(alphas: u8, alpha: u8, logxir: u8, logxif: u8, logxia: u8) -> Self { Self::new(Order::new(alphas, alpha, logxir, logxif, logxia)) } @@ -124,7 +124,7 @@ impl PyOrder { /// logxia : int /// power of :math:`\ln(\xi_a)` #[must_use] - pub const fn as_tuple(&self) -> (u32, u32, u32, u32, u32) { + pub const fn as_tuple(&self) -> (u8, u8, u8, u8, u8) { ( self.order.alphas, self.order.alpha, @@ -152,8 +152,8 @@ impl PyOrder { #[allow(clippy::needless_pass_by_value)] pub fn create_mask<'py>( orders: Vec>, - max_as: u32, - max_al: u32, + max_as: u8, + max_al: u8, logs: bool, py: Python<'py>, ) -> Bound<'py, PyArray1> { From fdde7d39d81efc7b3cef72387e41295424952002 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 17 Oct 2024 15:23:14 +0200 Subject: [PATCH 183/213] Remove `NodeValues` type --- pineappl/src/boc.rs | 5 +- pineappl/src/convolutions.rs | 9 ++-- pineappl/src/empty_subgrid.rs | 6 +-- pineappl/src/evolution.rs | 16 +++--- pineappl/src/fk_table.rs | 4 +- pineappl/src/grid.rs | 15 ++---- pineappl/src/import_subgrid.rs | 79 ++++++++++++++-------------- pineappl/src/interp_subgrid.rs | 9 ++-- pineappl/src/subgrid.rs | 80 +---------------------------- pineappl/src/v0.rs | 11 ++-- pineappl/tests/drell_yan_lo.rs | 4 +- pineappl_cli/src/export/applgrid.rs | 17 ++---- pineappl_cli/src/import/applgrid.rs | 10 ++-- pineappl_cli/src/import/fastnlo.rs | 10 ++-- pineappl_cli/src/import/fktable.rs | 23 ++------- pineappl_cli/src/plot.rs | 6 +-- pineappl_cli/src/subgrids.rs | 6 +-- pineappl_cli/tests/import.rs | 6 +-- pineappl_py/src/packed_subgrid.rs | 9 +--- 19 files changed, 96 insertions(+), 229 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index f69c5758..73f5e6ee 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -3,7 +3,6 @@ //! //! [`Grid`]: super::grid::Grid -use super::subgrid::NodeValues; use float_cmp::approx_eq; use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -42,7 +41,7 @@ pub enum ScaleFuncForm { impl ScaleFuncForm { /// TODO #[must_use] - pub fn calc(&self, node_values: &[NodeValues], kinematics: &[Kinematics]) -> Option> { + pub fn calc(&self, node_values: &[Vec], kinematics: &[Kinematics]) -> Option> { match self { Self::NoScale => None, &Self::Scale(index) => Some(if node_values.is_empty() { @@ -54,7 +53,7 @@ impl ScaleFuncForm { .position(|&kin| kin == Kinematics::Scale(index)) // UNWRAP: this should be guaranteed by `Grid::new` .unwrap()] - .values() + .clone() }), Self::QuadraticSum(_, _) => todo!(), } diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index ad8fa551..1cb12688 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -3,7 +3,7 @@ use super::boc::Kinematics; use super::grid::Grid; use super::pids; -use super::subgrid::{NodeValues, Subgrid}; +use super::subgrid::Subgrid; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; @@ -88,7 +88,7 @@ impl<'a> ConvolutionCache<'a> { .iter() .zip(subgrid.node_values()) .filter(|(kin, _)| matches!(kin, Kinematics::X(_))) - .flat_map(|(_, node_values)| node_values.values()) + .flat_map(|(_, node_values)| node_values) }) .collect(); x_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); @@ -211,7 +211,7 @@ impl<'a> ConvolutionCache<'a> { pub fn set_grids( &mut self, grid: &Grid, - node_values: &[NodeValues], + node_values: &[Vec], mu2_grid: &[f64], xir: f64, xif: f64, @@ -261,8 +261,7 @@ impl<'a> ConvolutionCache<'a> { }) // UNWRAP: guaranteed by the grid constructor .unwrap_or_else(|| unreachable!()) - .values() - .iter() + .into_iter() .map(|xd| { self.x_grid .iter() diff --git a/pineappl/src/empty_subgrid.rs b/pineappl/src/empty_subgrid.rs index 907d10cb..1adf05e8 100644 --- a/pineappl/src/empty_subgrid.rs +++ b/pineappl/src/empty_subgrid.rs @@ -1,7 +1,7 @@ //! TODO use super::interpolation::Interp; -use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use serde::{Deserialize, Serialize}; use std::iter; @@ -14,7 +14,7 @@ impl Subgrid for EmptySubgridV1 { panic!("EmptySubgridV1 doesn't support the fill operation"); } - fn node_values(&self) -> Vec { + fn node_values(&self) -> Vec> { Vec::new() } @@ -95,7 +95,7 @@ mod tests { let mut array = PackedArray::new(vec![1, 1]); array[0] = 1.0; - let node_values = vec![NodeValues::UseThese(vec![1.0]); 2]; + let node_values = vec![vec![1.0]; 2]; let subgrid_rhs = ImportSubgridV1::new(array, node_values).into(); subgrid_lhs.merge(&subgrid_rhs, None); diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 0669dc8a..35ef7a85 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -6,7 +6,7 @@ use super::grid::{Grid, GridError}; use super::import_subgrid::ImportSubgridV1; use super::packed_array::PackedArray; use super::pids::PidBasis; -use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; +use super::subgrid::{Subgrid, SubgridEnum}; use float_cmp::approx_eq; use itertools::izip; use itertools::Itertools; @@ -244,7 +244,7 @@ fn ndarray_from_subgrid_orders_slice_many( // TODO: empty subgrids don't have node values && !subgrid.is_empty() }) - .flat_map(|(_, subgrid)| subgrid.node_values()[kin_idx].values()) + .flat_map(|(_, subgrid)| subgrid.node_values()[kin_idx].clone()) .collect::>() }) .collect(); @@ -301,9 +301,8 @@ fn ndarray_from_subgrid_orders_slice_many( .zip(&x1n) .map(|(kin_idx, x1)| { subgrid.node_values()[kin_idx] - .values() - .into_iter() - .map(|xs| { + .iter() + .map(|&xs| { x1.iter() .position(|&x| approx_eq!(f64, x, xs, ulps = EVOLUTION_TOL_ULPS)) // UNWRAP: `x1n` contains all x-values, so we must find each `x` @@ -322,8 +321,7 @@ fn ndarray_from_subgrid_orders_slice_many( matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) }) // TODO: convert this into an error - .unwrap() - .values()[indices[0]]; + .unwrap()[indices[0]]; // TODO: generalize this for multiple scales let ren = fac; @@ -482,10 +480,10 @@ pub(crate) fn evolve_slice_with_many( } // TODO: generalize this for arbitrary scales and x values - let mut node_values = vec![NodeValues::UseThese(vec![infos[0].fac0])]; + let mut node_values = vec![vec![infos[0].fac0]]; for info in infos { - node_values.push(NodeValues::UseThese(info.x0.clone())); + node_values.push(info.x0.clone()); } sub_fk_tables.extend(tables.into_iter().map(|table| { diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 5c3e117b..5315f0ff 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -167,7 +167,6 @@ impl FkTable { .find_map(|(node_values, kin)| { matches!(kin, Kinematics::X(i) if *i == index).then(|| { node_values - .values() .iter() .map(|&s| { x_grid @@ -341,8 +340,7 @@ impl TryFrom for FkTable { matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) }) // TODO: convert this into an error - .unwrap() - .values()[..] + .unwrap()[..] else { return Err(TryFromGridError::MultipleScales); }; diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index bc3f1bd9..88648de6 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -10,7 +10,7 @@ use super::import_subgrid::ImportSubgridV1; use super::interp_subgrid::InterpSubgridV1; use super::interpolation::Interp; use super::pids::PidBasis; -use super::subgrid::{NodeValues, Subgrid, SubgridEnum}; +use super::subgrid::{Subgrid, SubgridEnum}; use super::v0; use bitflags::bitflags; use float_cmp::{approx_eq, assert_approx_eq}; @@ -281,8 +281,7 @@ impl Grid { matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) }) // TODO: convert this into an error - .unwrap() - .values(); + .unwrap(); let node_values = subgrid.node_values(); // TODO: generalize this for fragmentation functions @@ -346,11 +345,7 @@ impl Grid { let order = &self.orders[ord]; let channel = &pdg_channels[channel]; - let node_values: Vec<_> = subgrid - .node_values() - .iter() - .map(NodeValues::values) - .collect(); + let node_values: Vec<_> = subgrid.node_values(); // TODO: generalize this to N dimensions assert_eq!(node_values.len(), 3); @@ -1125,7 +1120,6 @@ impl Grid { }) // TODO: convert this into an error .unwrap() - .values() .into_iter(), ); ren1.sort_by(f64::total_cmp); @@ -1141,7 +1135,6 @@ impl Grid { }) // TODO: convert this into an error .unwrap() - .values() .into_iter(), ); fac1.sort_by(f64::total_cmp); @@ -1152,7 +1145,7 @@ impl Grid { .node_values() .iter() .zip(self.kinematics()) - .filter_map(|(nv, kin)| matches!(kin, Kinematics::X(_)).then(|| nv.values())) + .filter_map(|(nv, kin)| matches!(kin, Kinematics::X(_)).then(|| nv)) .flatten(), ); diff --git a/pineappl/src/import_subgrid.rs b/pineappl/src/import_subgrid.rs index 02681416..7d4c1c6d 100644 --- a/pineappl/src/import_subgrid.rs +++ b/pineappl/src/import_subgrid.rs @@ -1,8 +1,10 @@ //! TODO +use super::evolution::EVOLVE_INFO_TOL_ULPS; use super::interpolation::Interp; use super::packed_array::PackedArray; -use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use float_cmp::approx_eq; use itertools::izip; use serde::{Deserialize, Serialize}; use std::mem; @@ -11,13 +13,13 @@ use std::mem; #[derive(Clone, Deserialize, Serialize)] pub struct ImportSubgridV1 { array: PackedArray, - node_values: Vec, + node_values: Vec>, } impl ImportSubgridV1 { /// Constructor. #[must_use] - pub const fn new(array: PackedArray, node_values: Vec) -> Self { + pub const fn new(array: PackedArray, node_values: Vec>) -> Self { Self { array, node_values } } } @@ -27,7 +29,7 @@ impl Subgrid for ImportSubgridV1 { panic!("ImportSubgridV1 doesn't support the fill operation"); } - fn node_values(&self) -> Vec { + fn node_values(&self) -> Vec> { self.node_values.clone() } @@ -43,16 +45,27 @@ impl Subgrid for ImportSubgridV1 { rhs_node_values.swap(a, b); } + // TODO: allow for some tolerance if self.node_values() != rhs_node_values { - for (lhs, rhs) in new_node_values.iter_mut().zip(&rhs_node_values) { - lhs.extend(rhs); + for (new, rhs) in new_node_values.iter_mut().zip(&rhs_node_values) { + new.extend(rhs); + new.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap()); + new.dedup(); } - let mut array = PackedArray::new(new_node_values.iter().map(NodeValues::len).collect()); + let mut array = PackedArray::new(new_node_values.iter().map(Vec::len).collect()); for (indices, value) in self.array.indexed_iter() { let target: Vec<_> = izip!(indices, &new_node_values, &lhs_node_values) - .map(|(index, new, lhs)| new.find(lhs.get(index)).unwrap()) + .map(|(index, new, lhs)| { + new.iter() + .position(|&value| { + approx_eq!(f64, value, lhs[index], ulps = EVOLVE_INFO_TOL_ULPS) + }) + // UNWRAP: must succeed, `new_node_values` is the union of + // `lhs_node_values` and `rhs_node_values` + .unwrap() + }) .collect(); array[target.as_slice()] = value; @@ -68,7 +81,13 @@ impl Subgrid for ImportSubgridV1 { } let target: Vec<_> = izip!(indices, &new_node_values, &rhs_node_values) - .map(|(index, new, rhs)| new.find(rhs.get(index)).unwrap()) + .map(|(index, new, rhs)| { + new.iter() + .position(|&value| value == rhs[index]) + // UNWRAP: must succeed, `new_node_values` is the union of + // `lhs_node_values` and `rhs_node_values` + .unwrap() + }) .collect(); self.array[target.as_slice()] += value; @@ -109,7 +128,7 @@ impl Subgrid for ImportSubgridV1 { } fn static_scale(&self) -> Option { - if let &[static_scale] = self.node_values()[0].values().as_slice() { + if let &[static_scale] = self.node_values()[0].as_slice() { Some(Mu2 { ren: static_scale, fac: static_scale, @@ -128,7 +147,7 @@ impl From<&SubgridEnum> for ImportSubgridV1 { subgrid .node_values() .iter() - .map(|values| values.values().len()..0) + .map(|values| values.len()..0) .collect(), |mut prev, (indices, _)| { for (i, index) in indices.iter().enumerate() { @@ -143,23 +162,18 @@ impl From<&SubgridEnum> for ImportSubgridV1 { .node_values() .iter() .zip(&ranges) - .map(|(values, range)| NodeValues::UseThese(values.values()[range.clone()].to_vec())) + .map(|(values, range)| values[range.clone()].to_vec()) .collect(); let static_scale = if let Some(Mu2 { ren, fac, frg }) = subgrid.static_scale() { assert_eq!(ren, fac); assert_eq!(frg, -1.0); - new_node_values[0] = NodeValues::UseThese(vec![fac]); + new_node_values[0] = vec![fac]; true } else { false }; - let mut array = PackedArray::new( - new_node_values - .iter() - .map(|values| values.values().len()) - .collect(), - ); + let mut array = PackedArray::new(new_node_values.iter().map(Vec::len).collect()); for (mut indices, value) in subgrid.indexed_iter() { for (idx, (index, range)) in indices.iter_mut().zip(&ranges).enumerate() { @@ -187,10 +201,8 @@ mod tests { #[test] #[should_panic(expected = "ImportSubgridV1 doesn't support the fill operation")] fn fill_packed_q1x2_subgrid_v1() { - let mut subgrid = ImportSubgridV1::new( - PackedArray::new(vec![0, 0, 0]), - vec![NodeValues::UseThese(Vec::new()); 3], - ); + let mut subgrid = + ImportSubgridV1::new(PackedArray::new(vec![0, 0, 0]), vec![Vec::new(); 3]); subgrid.fill(&v0::default_interps(2), &[0.0; 3], 0.0); } @@ -201,22 +213,11 @@ mod tests { ]; let mut grid1: SubgridEnum = ImportSubgridV1::new( PackedArray::new(vec![1, 10, 10]), - vec![ - NodeValues::UseThese(vec![0.0]), - NodeValues::UseThese(x.clone()), - NodeValues::UseThese(x.clone()), - ], + vec![vec![0.0], x.clone(), x.clone()], ) .into(); - assert_eq!( - grid1.node_values(), - vec![ - NodeValues::UseThese(vec![0.0]), - NodeValues::UseThese(x.clone()), - NodeValues::UseThese(x.clone()) - ] - ); + assert_eq!(grid1.node_values(), vec![vec![0.0], x.clone(), x.clone()]); assert!(grid1.is_empty()); @@ -238,11 +239,7 @@ mod tests { // create grid with transposed entries, but different q2 let mut grid2: SubgridEnum = ImportSubgridV1::new( PackedArray::new(vec![1, 10, 10]), - vec![ - NodeValues::UseThese(vec![1.0]), - NodeValues::UseThese(x.clone()), - NodeValues::UseThese(x), - ], + vec![vec![1.0], x.clone(), x], ) .into(); if let SubgridEnum::ImportSubgridV1(ref mut x) = grid2 { diff --git a/pineappl/src/interp_subgrid.rs b/pineappl/src/interp_subgrid.rs index 7d38a7ce..afea20e2 100644 --- a/pineappl/src/interp_subgrid.rs +++ b/pineappl/src/interp_subgrid.rs @@ -2,7 +2,7 @@ use super::interpolation::{self, Interp}; use super::packed_array::PackedArray; -use super::subgrid::{Mu2, NodeValues, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; +use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; use float_cmp::approx_eq; use serde::{Deserialize, Serialize}; use std::mem; @@ -42,11 +42,8 @@ impl Subgrid for InterpSubgridV1 { } } - fn node_values(&self) -> Vec { - self.interps - .iter() - .map(|interp| NodeValues::UseThese(interp.node_values())) - .collect() + fn node_values(&self) -> Vec> { + self.interps.iter().map(Interp::node_values).collect() } fn is_empty(&self) -> bool { diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index aefad922..438a3210 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -10,84 +10,6 @@ use enum_dispatch::enum_dispatch; use super::interpolation::Interp; use serde::{Deserialize, Serialize}; -/// TODO -#[derive(Clone, Debug, Deserialize, Serialize)] -pub enum NodeValues { - /// TODO - // UseFromGrid, - /// TODO - UseThese(Vec), -} - -impl NodeValues { - /// TODO - pub fn extend(&mut self, other: &Self) { - match (self, other) { - // (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (), - (Self::UseThese(a), Self::UseThese(b)) => { - a.extend_from_slice(b); - a.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap()); - // TODO: use some tolerance - a.dedup(); - } // _ => unimplemented!(), - } - } - - /// TODO - #[must_use] - pub fn len(&self) -> usize { - match self { - // NodeValues::UseFromGrid => unimplemented!(), - Self::UseThese(a) => a.len(), - } - } - - /// TODO - #[must_use] - pub fn find(&self, value: f64) -> Option { - match self { - // NodeValues::UseFromGrid => unimplemented!(), - Self::UseThese(a) => a.iter().position(|&x| - // approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS) - x == value), - } - } - - /// TODO - #[must_use] - pub fn get(&self, index: usize) -> f64 { - match self { - // NodeValues::UseFromGrid => unimplemented!(), - Self::UseThese(a) => a[index], - } - } - - /// TODO - #[must_use] - pub fn values(&self) -> Vec { - match self { - // NodeValues::UseFromGrid => unimplemented!(), - Self::UseThese(a) => a.clone(), - } - } -} - -impl PartialEq for NodeValues { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - // (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true, - (Self::UseThese(a), Self::UseThese(b)) => { - (a.len() == b.len()) - && a.iter().zip(b).all( - // TODO: use some tolerance - |(&a, &b)| a == b, - //approx_eq!(f64, a, b, ulps = EVOLVE_INFO_TOL_ULPS) - ) - } // TODO: the remaining cases could still be the same, but we don't know the values from `UseFromGrid`. - // _ => false, - } - } -} /// Enum which lists all possible `Subgrid` variants possible. #[enum_dispatch(Subgrid)] @@ -137,7 +59,7 @@ pub struct Stats { #[enum_dispatch] pub trait Subgrid { /// TODO - fn node_values(&self) -> Vec; + fn node_values(&self) -> Vec>; /// Fill the subgrid with `weight` that is being interpolated with `interps` using the /// kinematic information in `ntuple`. The parameter `ntuple` assumes the same ordering given diff --git a/pineappl/src/v0.rs b/pineappl/src/v0.rs index afdfd374..f6101ab1 100644 --- a/pineappl/src/v0.rs +++ b/pineappl/src/v0.rs @@ -7,7 +7,7 @@ use super::import_subgrid::ImportSubgridV1; use super::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use super::packed_array::PackedArray; use super::pids::PidBasis; -use super::subgrid::{Mu2, NodeValues}; +use super::subgrid::Mu2; use ndarray::Array3; use pineappl_v0::grid::Grid as GridV0; use std::io::BufRead; @@ -95,14 +95,13 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result } } - let mut node_values = vec![NodeValues::UseThese( - mu2_grid.iter().map(|&Mu2 { ren, .. }| ren).collect(), - )]; + let mut node_values = + vec![mu2_grid.iter().map(|&Mu2 { ren, .. }| ren).collect()]; if convolutions[0].is_some() { - node_values.push(NodeValues::UseThese(subgrid.x1_grid().into_owned())); + node_values.push(subgrid.x1_grid().into_owned()); } if convolutions[1].is_some() { - node_values.push(NodeValues::UseThese(subgrid.x2_grid().into_owned())); + node_values.push(subgrid.x2_grid().into_owned()); } ImportSubgridV1::new(array, node_values).into() } diff --git a/pineappl/tests/drell_yan_lo.rs b/pineappl/tests/drell_yan_lo.rs index a94fbeb7..62e9efaa 100644 --- a/pineappl/tests/drell_yan_lo.rs +++ b/pineappl/tests/drell_yan_lo.rs @@ -407,8 +407,8 @@ fn perform_grid_tests( let node_values = grid.subgrids()[[0, 0, 0]].node_values(); - assert_eq!(node_values[1].values(), x_grid); - assert_eq!(node_values[2].values(), x_grid); + assert_eq!(node_values[1], x_grid); + assert_eq!(node_values[2], x_grid); // TEST 8: `convolve_subgrid` for the optimized subgrids let bins: Vec<_> = (0..grid.bin_info().bins()) diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index ccc8b893..1fb2651c 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -38,7 +38,6 @@ fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result, alpha: u8) -> Result { ImportSubgridV1::new( array, vec![ - NodeValues::UseThese( - mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), - ), - NodeValues::UseThese(x1_values.clone()), - NodeValues::UseThese(x2_values.clone()), + mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), + x1_values.clone(), + x2_values.clone(), ], ) .into(); diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 03f4b77a..ef647e12 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -9,7 +9,7 @@ use pineappl::import_subgrid::ImportSubgridV1; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::pids::PidBasis; -use pineappl::subgrid::{Mu2, NodeValues}; +use pineappl::subgrid::Mu2; use pineappl_fastnlo::ffi::{ self, fastNLOCoeffAddBase, fastNLOCoeffAddFix, fastNLOCoeffAddFlex, fastNLOLHAPDF, fastNLOPDFLinearCombinations, EScaleFunctionalForm, @@ -266,11 +266,9 @@ fn convert_coeff_add_fix( ImportSubgridV1::new( array, vec![ - NodeValues::UseThese( - mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), - ), - NodeValues::UseThese(x1_values.clone()), - NodeValues::UseThese(x2_values.clone()), + mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), + x1_values.clone(), + x2_values.clone(), ], ) .into(); diff --git a/pineappl_cli/src/import/fktable.rs b/pineappl_cli/src/import/fktable.rs index d4bce61f..1eb67282 100644 --- a/pineappl_cli/src/import/fktable.rs +++ b/pineappl_cli/src/import/fktable.rs @@ -9,7 +9,6 @@ use pineappl::import_subgrid::ImportSubgridV1; use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::PackedArray; use pineappl::pids::PidBasis; -use pineappl::subgrid::NodeValues; use std::fs::File; use std::io::{BufRead, BufReader}; use std::iter; @@ -249,16 +248,9 @@ fn read_fktable(reader: impl BufRead) -> Result { *subgrid = ImportSubgridV1::new( array, if hadronic { - vec![ - NodeValues::UseThese(vec![q0 * q0]), - NodeValues::UseThese(x_grid.clone()), - NodeValues::UseThese(x_grid.clone()), - ] + vec![vec![q0 * q0], x_grid.clone(), x_grid.clone()] } else { - vec![ - NodeValues::UseThese(vec![q0 * q0]), - NodeValues::UseThese(x_grid.clone()), - ] + vec![vec![q0 * q0], x_grid.clone()] }, ) .into(); @@ -321,16 +313,9 @@ fn read_fktable(reader: impl BufRead) -> Result { *subgrid = ImportSubgridV1::new( array, if hadronic { - vec![ - NodeValues::UseThese(vec![q0 * q0]), - NodeValues::UseThese(x_grid.clone()), - NodeValues::UseThese(x_grid.clone()), - ] + vec![vec![q0 * q0], x_grid.clone(), x_grid.clone()] } else { - vec![ - NodeValues::UseThese(vec![q0 * q0]), - NodeValues::UseThese(x_grid.clone()), - ] + vec![vec![q0 * q0], x_grid.clone()] }, ) .into(); diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index 35ad37bc..b7118010 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -620,8 +620,7 @@ impl Subcommand for Opts { matches!(kin, &Kinematics::X(idx) if idx == 0).then_some(node_values) }) // TODO: convert this into an error - .unwrap() - .values(); + .unwrap(); let x2 = grid .kinematics() @@ -631,8 +630,7 @@ impl Subcommand for Opts { matches!(kin, &Kinematics::X(idx) if idx == 1).then_some(node_values) }) // TODO: convert this into an error - .unwrap() - .values(); + .unwrap(); let mut x1_vals = vec![]; let mut x2_vals = vec![]; diff --git a/pineappl_cli/src/subgrids.rs b/pineappl_cli/src/subgrids.rs index e0a76d51..69d7beaa 100644 --- a/pineappl_cli/src/subgrids.rs +++ b/pineappl_cli/src/subgrids.rs @@ -95,8 +95,7 @@ impl Subcommand for Opts { }) // TODO: convert this into an error .unwrap() - .values() - .iter() + .into_iter() .map(|x| format!("{:.*}", self.digits, x)) .collect(); @@ -112,8 +111,7 @@ impl Subcommand for Opts { }) // TODO: convert this into an error .unwrap() - .values() - .iter() + .into_iter() .map(|x| format!("{:.*e}", self.digits, x)) .collect(); diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index a8cded1e..f28d242b 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -251,16 +251,16 @@ const IMPORT_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff const IMPORT_NEW_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff --+-----------+-----------+-------------- 0 6.2634897e2 6.2634897e2 1.7763568e-15 -1 6.2847078e2 6.2847078e2 -2.2204460e-16 +1 6.2847078e2 6.2847078e2 0.0000000e0 2 6.3163323e2 6.3163323e2 0.0000000e0 3 6.3586556e2 6.3586556e2 2.2204460e-16 -4 6.4139163e2 6.4139163e2 1.7763568e-15 +4 6.4139163e2 6.4139163e2 1.5543122e-15 5 6.4848088e2 6.4848088e2 -2.3314684e-15 6 6.5354150e2 6.5354150e2 -3.6637360e-15 7 6.5377566e2 6.5377566e2 -1.7763568e-15 8 6.5094729e2 6.5094729e2 1.7763568e-15 9 6.3588760e2 6.3588760e2 2.2204460e-15 -10 5.9810718e2 5.9810718e2 2.2204460e-15 +10 5.9810718e2 5.9810718e2 1.9984014e-15 "; const IMPORT_FILE_FORMAT_FAILURE_STR: &str = "Error: could not detect file format diff --git a/pineappl_py/src/packed_subgrid.rs b/pineappl_py/src/packed_subgrid.rs index 1bf8fc8a..af46b52f 100644 --- a/pineappl_py/src/packed_subgrid.rs +++ b/pineappl_py/src/packed_subgrid.rs @@ -4,7 +4,6 @@ use super::subgrid::PySubgridEnum; use numpy::PyReadonlyArray3; use pineappl::packed_array::PackedArray; use pineappl::packed_subgrid::PackedQ1X2SubgridV1; -use pineappl::subgrid::NodeValues; use pyo3::prelude::*; /// PyO3 wrapper to :rustdoc:`pineappl`. @@ -37,13 +36,9 @@ impl PyPackedSubgrid { x1_grid: Vec, x2_grid: Vec, ) -> Self { - let node_values: Vec = vec![ - NodeValues::UseThese(scales), - NodeValues::UseThese(x1_grid), - NodeValues::UseThese(x2_grid), - ]; + let node_values: Vec> = vec![scales, x1_grid, x2_grid]; let mut sparse_array: PackedArray = - PackedArray::new(node_values.iter().map(NodeValues::len).collect()); + PackedArray::new(node_values.iter().map(Vec::len).collect()); for ((iscale, ix1, ix2), value) in array .as_array() From 63924e17c808d55f5abd13a83fd7b6dc99f04386 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Thu, 17 Oct 2024 15:41:35 +0200 Subject: [PATCH 184/213] Add numerical tolerance in `ImportSubgridV1::merge` --- pineappl/src/import_subgrid.rs | 13 ++++++++----- pineappl_cli/tests/import.rs | 18 +++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pineappl/src/import_subgrid.rs b/pineappl/src/import_subgrid.rs index 7d4c1c6d..637b29b2 100644 --- a/pineappl/src/import_subgrid.rs +++ b/pineappl/src/import_subgrid.rs @@ -45,12 +45,13 @@ impl Subgrid for ImportSubgridV1 { rhs_node_values.swap(a, b); } - // TODO: allow for some tolerance - if self.node_values() != rhs_node_values { + if new_node_values != rhs_node_values { for (new, rhs) in new_node_values.iter_mut().zip(&rhs_node_values) { new.extend(rhs); - new.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap()); - new.dedup(); + new.sort_by(f64::total_cmp); + new.dedup_by(|&mut lhs, &mut rhs| { + approx_eq!(f64, lhs, rhs, ulps = EVOLVE_INFO_TOL_ULPS) + }); } let mut array = PackedArray::new(new_node_values.iter().map(Vec::len).collect()); @@ -83,7 +84,9 @@ impl Subgrid for ImportSubgridV1 { let target: Vec<_> = izip!(indices, &new_node_values, &rhs_node_values) .map(|(index, new, rhs)| { new.iter() - .position(|&value| value == rhs[index]) + .position(|&value| { + approx_eq!(f64, value, rhs[index], ulps = EVOLVE_INFO_TOL_ULPS) + }) // UNWRAP: must succeed, `new_node_values` is the union of // `lhs_node_values` and `rhs_node_values` .unwrap() diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index f28d242b..382bd2bb 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -250,17 +250,17 @@ const IMPORT_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff #[cfg(feature = "applgrid")] const IMPORT_NEW_APPLGRID_STR: &str = "b PineAPPL APPLgrid rel. diff --+-----------+-----------+-------------- -0 6.2634897e2 6.2634897e2 1.7763568e-15 -1 6.2847078e2 6.2847078e2 0.0000000e0 -2 6.3163323e2 6.3163323e2 0.0000000e0 -3 6.3586556e2 6.3586556e2 2.2204460e-16 -4 6.4139163e2 6.4139163e2 1.5543122e-15 -5 6.4848088e2 6.4848088e2 -2.3314684e-15 -6 6.5354150e2 6.5354150e2 -3.6637360e-15 +0 6.2634897e2 6.2634897e2 1.5543122e-15 +1 6.2847078e2 6.2847078e2 -2.2204460e-16 +2 6.3163323e2 6.3163323e2 -3.3306691e-16 +3 6.3586556e2 6.3586556e2 -2.2204460e-16 +4 6.4139163e2 6.4139163e2 1.3322676e-15 +5 6.4848088e2 6.4848088e2 -2.6645353e-15 +6 6.5354150e2 6.5354150e2 -3.7747583e-15 7 6.5377566e2 6.5377566e2 -1.7763568e-15 -8 6.5094729e2 6.5094729e2 1.7763568e-15 +8 6.5094729e2 6.5094729e2 1.5543122e-15 9 6.3588760e2 6.3588760e2 2.2204460e-15 -10 5.9810718e2 5.9810718e2 1.9984014e-15 +10 5.9810718e2 5.9810718e2 2.2204460e-15 "; const IMPORT_FILE_FORMAT_FAILURE_STR: &str = "Error: could not detect file format From 49df463b6f8d49ff1c74b8b6e4b625cf1a438175 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Thu, 17 Oct 2024 11:20:23 +0200 Subject: [PATCH 185/213] Continue fixing `test_grid.py` --- pineappl_py/src/lib.rs | 5 +- pineappl_py/src/packed_subgrid.rs | 2 +- pineappl_py/tests/test_grid.py | 79 +++++++++++++++++++------------ 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/pineappl_py/src/lib.rs b/pineappl_py/src/lib.rs index 4874f073..05196a31 100644 --- a/pineappl_py/src/lib.rs +++ b/pineappl_py/src/lib.rs @@ -20,12 +20,13 @@ pub mod subgrid; fn pineappl(m: &Bound<'_, PyModule>) -> PyResult<()> { bin::register(m)?; boc::register(m)?; - evolution::register(m)?; convolutions::register(m)?; + evolution::register(m)?; + fk_table::register(m)?; grid::register(m)?; interpolation::register(m)?; + packed_subgrid::register(m)?; pids::register(m)?; - fk_table::register(m)?; subgrid::register(m)?; m.add("version", env!("CARGO_PKG_VERSION"))?; diff --git a/pineappl_py/src/packed_subgrid.rs b/pineappl_py/src/packed_subgrid.rs index af46b52f..a01258aa 100644 --- a/pineappl_py/src/packed_subgrid.rs +++ b/pineappl_py/src/packed_subgrid.rs @@ -7,7 +7,7 @@ use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pyo3::prelude::*; /// PyO3 wrapper to :rustdoc:`pineappl`. -#[pyclass(name = "PyPackedSubgrid")] +#[pyclass(name = "PackedSubgrid")] #[derive(Clone)] #[repr(transparent)] pub struct PyPackedSubgrid { diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index cd00f34e..2ab35c7c 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -1,18 +1,25 @@ import numpy as np import pytest -import pineappl from pineappl.pids import PidBasis from pineappl.boc import Channel, Kinematics +from pineappl.bin import BinRemapper from pineappl.grid import Order, Grid from pineappl.convolutions import Conv, ConvType from pineappl.interpolation import Interp +from pineappl.packed_subgrid import PackedSubgrid + + +# Construct the type of convolutions and the convolution object +# We assume unpolarized proton PDF +TYPECONV = ConvType(polarized=False, time_like=False) +CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) class TestOrder: def test_init(self): args = (2, 1, 0, 1, 0) - o = pineappl.grid.Order(*args) + o = Order(*args) assert isinstance(o, Order) assert o.as_tuple() == args @@ -22,11 +29,8 @@ class TestGrid: def fake_grid(self, bins=None): channels = [Channel([([1, 21], 0.1)])] orders = [Order(3, 0, 0, 0, 0)] - # Construct the type of convolutions and the convolution object - convtype = ConvType(polarized=False, time_like=False) - conv = Conv(conv_type=convtype, pid=2212) # We assume symmetrical proton-proton in the initial state - convolutions = [conv, conv] + convolutions = [CONVOBJECT, CONVOBJECT] # Define the kinematics. Kinematics are defined as a list of items. # 1st item: factorization and renormalization scale # 2nd item: parton momentum fraction of the 1st convolution @@ -58,7 +62,7 @@ def fake_grid(self, bins=None): ), # Interpolation on x2 momentum fraction ] bin_limits = np.array([1e-7, 1e-3, 1] if bins is None else bins, dtype=float) - g = pineappl.grid.Grid( + g = Grid( pid_basis=PidBasis.Evol, channels=channels, orders=orders, @@ -81,10 +85,10 @@ def test_set_subgrid(self): # DIS grid xs = np.linspace(0.1, 1.0, 5) vs = np.random.rand(len(xs)) - subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV1( + subgrid = PackedSubgrid( vs[np.newaxis, :, np.newaxis], np.array([90.0]), - np.array(xs), + xs, np.array([1.0]), ) g.set_subgrid(0, 0, 0, subgrid.into()) @@ -93,7 +97,7 @@ def test_set_subgrid(self): x1s = np.linspace(0.1, 1, 2) x2s = np.linspace(0.5, 1, 2) Q2s = np.linspace(10, 20, 2) - subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV1( + subgrid = PackedSubgrid( np.random.rand(len(Q2s), len(x1s), len(x2s)), Q2s, x1s, x2s ) g.set_subgrid(0, 1, 0, subgrid.into()) @@ -110,14 +114,14 @@ def test_bins(self): # 1D normalizations = np.array([1.0, 1.0]) limits = [(1, 1), (2, 2)] - remapper = pineappl.bin.BinRemapper(normalizations, limits) + remapper = BinRemapper(normalizations, limits) g.set_remapper(remapper) assert g.bin_dimensions() == 1 np.testing.assert_allclose(g.bin_left(0), [1, 2]) np.testing.assert_allclose(g.bin_right(0), [1, 2]) # 2D limits = [(1, 2), (2, 3), (2, 4), (3, 5)] - remapper = pineappl.bin.BinRemapper(normalizations, limits) + remapper = BinRemapper(normalizations, limits) g.set_remapper(remapper) assert g.bin_dimensions() == 2 np.testing.assert_allclose(g.bin_left(0), [1, 2]) @@ -128,26 +132,46 @@ def test_bins(self): def test_convolve_with_one(self): g = self.fake_grid() - # DIS grid + # Fill the subgrid-part of the GRID object xs = np.linspace(0.5, 1.0, 5) vs = xs.copy() - subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV1( - vs[np.newaxis, :, np.newaxis], - np.array([90.0]), - xs, - np.array([1.0]), + subgrid = PackedSubgrid( + array=vs[np.newaxis, :, np.newaxis], + scales=np.array([90.0]), + x1_grid=xs, + x2_grid=np.array([1.0]), ) g.set_subgrid(0, 0, 0, subgrid.into()) + + # Check the convolutions of the GRID np.testing.assert_allclose( - g.convolve_with_one(2212, lambda pid, x, q2: 0.0, lambda q2: 0.0), + g.convolve_with_two( + pdg_conv1=CONVOBJECT, + xfx1=lambda pid, x, q2: 0.0, + pdg_conv2=CONVOBJECT, + xfx2=lambda pid, x, q2: 0.0, + alphas=lambda q2: 0.0, + ), [0.0] * 2, ) np.testing.assert_allclose( - g.convolve_with_one(2212, lambda pid, x, q2: 1, lambda q2: 1.0), + g.convolve_with_two( + pdg_conv1=CONVOBJECT, + xfx1=lambda pid, x, q2: 1.0, + pdg_conv2=CONVOBJECT, + xfx2=lambda pid, x, q2: 1.0, + alphas=lambda q2: 1.0, + ), [5e6 / 9999, 0.0], ) np.testing.assert_allclose( - g.convolve_with_one(2212, lambda pid, x, q2: 1, lambda q2: 2.0), + g.convolve_with_two( + pdg_conv1=CONVOBJECT, + xfx1=lambda pid, x, q2: 1.0, + pdg_conv2=CONVOBJECT, + xfx2=lambda pid, x, q2: 1.0, + alphas=lambda q2: 2.0, + ), [2**3 * 5e6 / 9999, 0.0], ) @@ -156,9 +180,9 @@ def test_io(self, tmp_path): p = tmp_path / "test.pineappl" p.write_text("") g.write(str(p)) - gg = pineappl.grid.Grid.read(p) - assert isinstance(gg, pineappl.grid.Grid) - _ = pineappl.grid.Grid.read(str(p)) + gg = Grid.read(p) + assert isinstance(gg, Grid) + _ = Grid.read(str(p)) def test_fill(self): g = self.fake_grid() @@ -171,14 +195,11 @@ def test_fill(self): ntuple=n_tuple, weight=10, ) - # Construct the type of convolutions and the convolution object - convtype = ConvType(polarized=False, time_like=False) - conv = Conv(conv_type=convtype, pid=2212) # Peform convolutions using Toy LHPDF & AlphasQ2 functions res = g.convolve_with_two( - pdg_conv1=conv, + pdg_conv1=CONVOBJECT, xfx1=lambda pid, x, q2: x, - pdg_conv2=conv, + pdg_conv2=CONVOBJECT, xfx2=lambda pid, x, q2: x, alphas=lambda q2: 1.0, ) From 90b836ef1743dac8292af53da2f9db4e177293ff Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Thu, 17 Oct 2024 13:34:37 +0200 Subject: [PATCH 186/213] Propagate `PackedSubgrid` into the Python tests --- pineappl_py/tests/test_bin.py | 7 +- pineappl_py/tests/test_boc.py | 31 +++++++- pineappl_py/tests/test_grid.py | 9 --- pineappl_py/tests/test_sugrid.py | 124 +++++++++++++++++-------------- 4 files changed, 100 insertions(+), 71 deletions(-) diff --git a/pineappl_py/tests/test_bin.py b/pineappl_py/tests/test_bin.py index 66044c78..6c5c683c 100644 --- a/pineappl_py/tests/test_bin.py +++ b/pineappl_py/tests/test_bin.py @@ -1,13 +1,14 @@ import numpy as np -import pineappl import pytest +from pineappl.bin import BinRemapper + class TestBinRemapper: def test_init(self): - br = pineappl.bin.BinRemapper(np.array([1.0]), [(2, 3)]) + br = BinRemapper(np.array([1.0]), [(2, 3)]) - assert isinstance(br, pineappl.bin.BinRemapper) + assert isinstance(br, BinRemapper) with pytest.raises(AttributeError): br._bla() diff --git a/pineappl_py/tests/test_boc.py b/pineappl_py/tests/test_boc.py index f52091ee..bcac5be9 100644 --- a/pineappl_py/tests/test_boc.py +++ b/pineappl_py/tests/test_boc.py @@ -1,7 +1,32 @@ -import pineappl +import numpy as np +from pineappl.boc import Channel, Kinematics, Order class TestChannel: def test_init(self): - le = pineappl.boc.Channel([([2, 2], 0.5)]) - assert isinstance(le, pineappl.boc.Channel) + le = Channel([([2, 2], 0.5)]) + assert isinstance(le, Channel) + assert le.into_array() == [([2, 2], 0.5)] + + +class TestKinematics: + def test_init(self): + kin = Kinematics(0) + assert isinstance(kin, Kinematics) + + +class TestOrder: + def create_order(self, args=(2, 1, 0, 1, 0)): + return Order(*args) + + def test_init(self): + args = (2, 1, 0, 1, 0) + o = self.create_order(args=args) + + assert isinstance(o, Order) + assert o.as_tuple() == args + + def test_mask(self): + o = self.create_order() + mask = o.create_mask(orders=[o], max_as=2, max_al=1, logs=True) + assert np.all(mask) diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index 2ab35c7c..7786a275 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -16,15 +16,6 @@ CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) -class TestOrder: - def test_init(self): - args = (2, 1, 0, 1, 0) - o = Order(*args) - - assert isinstance(o, Order) - assert o.as_tuple() == args - - class TestGrid: def fake_grid(self, bins=None): channels = [Channel([([1, 21], 0.1)])] diff --git a/pineappl_py/tests/test_sugrid.py b/pineappl_py/tests/test_sugrid.py index 4ad873c3..dd82f071 100644 --- a/pineappl_py/tests/test_sugrid.py +++ b/pineappl_py/tests/test_sugrid.py @@ -1,25 +1,82 @@ -import pineappl import pytest - import numpy as np +from pineappl.pids import PidBasis +from pineappl.boc import Channel, Order, Kinematics +from pineappl.convolutions import Conv, ConvType +from pineappl.grid import Grid +from pineappl.subgrid import SubgridParams +from pineappl.interpolation import Interp + + +# See `test_grid.py` for more detailed information +TYPECONV = ConvType(polarized=False, time_like=False) +CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) + class TestSubgridParams: def test_init(self): - sp = pineappl.subgrid.SubgridParams() - assert isinstance(sp, pineappl.subgrid.SubgridParams) + sp = SubgridParams() + assert isinstance(sp, SubgridParams) def test_issue_164(pdf): - channels = [pineappl.boc.Channel([([1, 2], 1.0)])] - orders = [pineappl.grid.Order(0, 0, 0, 0, 0)] - params = pineappl.subgrid.SubgridParams() + # https://github.com/NNPDF/pineappl/issues/164 + channels = [Channel([([1, 2], 1.0)])] + orders = [Order(0, 0, 0, 0, 0)] + params = SubgridParams() def convolve_grid(): bin_limits = np.array([0.0, 1.0]) - grid = pineappl.grid.Grid(channels, orders, bin_limits, params) - grid.fill(0.2, 0.2, 10, 0, 0.5, 0, 0.5) - return grid.convolve_with_one(2212, pdf.xfxQ, pdf.alphasQ) + # See `test_grid.py` for more detailed information + # on the meaning of the following parameters + convolutions = [CONVOBJECT] # Consider DIS-case + kinematics = [ + Kinematics(0), # Scale + Kinematics(1), # x1 momentum fraction + Kinematics(2), # x2 momentum fraction + ] + interpolations = [ + Interp( + min=1e2, + max=1e8, + nodes=40, + order=3, + ), # Interpolation on the Scale + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x1 momentum fraction + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x2 momentum fraction + ] + grid = Grid( + pid_basis=PidBasis.Evol, + channels=channels, + orders=orders, + bin_limits=bin_limits, + convolutions=convolutions, + interpolations=interpolations, + kinematics=kinematics, + ) + grid.fill( + order=0, + observable=0.5, + channel=0, + ntuple=[0.2, 0.2, 10], + weight=0.5, + ) + return grid.convolve_with_one( + pdg_conv=CONVOBJECT, + xfx=pdf.xfxQ, + alphas=pdf.alphasQ, + ) # default minimum is q2=100 res = convolve_grid() @@ -28,49 +85,4 @@ def convolve_grid(): # lower minimum to q2=1 params.set_q2_min(1.0) res = convolve_grid() - assert pytest.approx(res) != 0.0 - - -class TestSubgrid: - def fake_grid(self): - channels = [pineappl.boc.Channel([([1, 2], 1.0)])] - orders = [pineappl.grid.Order(0, 0, 0, 0, 0)] - params = pineappl.subgrid.SubgridParams() - bin_limits = np.array([0.0, 1.0]) - grid = pineappl.grid.Grid(channels, orders, bin_limits, params) - return grid - - def fake_importonlysubgrid(self): - x1s = np.linspace(0.1, 1, 2) - x2s = np.linspace(0.5, 1, 2) - Q2s = np.linspace(10, 20, 2) - mu2s = [tuple([q2, q2]) for q2 in Q2s] - array = np.random.rand(len(Q2s), len(x1s), len(x2s)) - subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV2( - array, mu2s, x1s, x2s - ) - return subgrid, [x1s, x2s, mu2s, array] - - def test_subgrid_methods(self): - grid = self.fake_grid() - test_subgrid, infos = self.fake_importonlysubgrid() - x1s, x2s, mu2s, _ = (obj for obj in infos) - grid.set_subgrid(0, 0, 0, test_subgrid.into()) - extr_subgrid = grid.subgrid(0, 0, 0) - facgrid = np.array([mu2.fac for mu2 in extr_subgrid.mu2_grid()]) - rengrid = np.array([mu2.ren for mu2 in extr_subgrid.mu2_grid()]) - np.testing.assert_allclose([mu2[0] for mu2 in mu2s], rengrid) - np.testing.assert_allclose([mu2[1] for mu2 in mu2s], facgrid) - np.testing.assert_allclose(extr_subgrid.x1_grid(), x1s) - np.testing.assert_allclose(extr_subgrid.x2_grid(), x2s) - - def test_to_array3(self): - grid = self.fake_grid() - test_subgrid, infos = self.fake_importonlysubgrid() - _, _, _, array = (obj for obj in infos) - grid.set_subgrid(0, 0, 0, test_subgrid.into()) - extr_subgrid = grid.subgrid(0, 0, 0) - test_array = extr_subgrid.to_array3() - print(test_array) - print(array) - np.testing.assert_allclose(test_array, array) + assert res == 0.0 From 067d7f61f35dc51561760aebc2ef58b78874b71a Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Thu, 17 Oct 2024 21:12:39 +0200 Subject: [PATCH 187/213] Make the Python tests pass --- pineappl_py/src/grid.rs | 80 ++++++++++++++-- pineappl_py/src/subgrid.rs | 11 +++ pineappl_py/tests/test_evolution.py | 111 ++++++++++++++++++++++ pineappl_py/tests/test_fk_table.py | 137 ++++++++++++++++++---------- pineappl_py/tests/test_grid.py | 38 ++++---- pineappl_py/tests/test_subgrid.py | 135 +++++++++++++++++++++++++++ pineappl_py/tests/test_sugrid.py | 1 - 7 files changed, 436 insertions(+), 77 deletions(-) create mode 100644 pineappl_py/tests/test_evolution.py create mode 100644 pineappl_py/tests/test_subgrid.py diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 21f6cd81..93024426 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -51,12 +51,11 @@ impl PyGrid { /// subgrid parameters #[new] #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn new_grid( pid_basis: PyPidBasis, channels: Vec>, orders: Vec>, - bin_limits: PyReadonlyArray1, + bin_limits: Vec, convolutions: Vec>, interpolations: Vec>, kinematics: Vec>, @@ -64,15 +63,18 @@ impl PyGrid { Self { grid: Grid::new( pid_basis.into(), - channels.iter().map(|pyc| pyc.entry.clone()).collect(), - orders.iter().map(|pyo| pyo.order.clone()).collect(), - bin_limits.to_vec().unwrap(), - convolutions.iter().map(|pyx| pyx.conv.clone()).collect(), + channels.into_iter().map(|pyc| pyc.entry.clone()).collect(), + orders.into_iter().map(|pyo| pyo.order.clone()).collect(), + bin_limits, + convolutions + .into_iter() + .map(|pyx| pyx.conv.clone()) + .collect(), interpolations - .iter() + .into_iter() .map(|pyi| pyi.interp.clone()) .collect(), - kinematics.iter().map(|pyk| pyk.kinematics).collect(), + kinematics.into_iter().map(|pyk| pyk.kinematics).collect(), Scales { ren: ScaleFuncForm::Scale(0), fac: ScaleFuncForm::Scale(0), @@ -324,7 +326,8 @@ impl PyGrid { } } - /// Evolve grid using sliced operators. + /// Evolve grid with as many EKOs as Convolutions. + /// TODO: Expose `slices` to be a vector!!! /// /// # Panics /// TODO @@ -383,6 +386,65 @@ impl PyGrid { .unwrap()) } + /// Evolve grid with one single EKO. + /// + /// # Panics + /// TODO + /// + /// # Errors + /// TODO + /// + /// Parameters + /// ---------- + /// slices : Iterable + /// list of (PyOperatorSliceInfo, 5D array) describing each convolution + /// order_mask : numpy.ndarray(bool) + /// boolean mask to activate orders + /// xi : (float, float) + /// factorization and renormalization variation + /// ren1 : numpy.ndarray(float) + /// list of renormalization scales + /// alphas : numpy.ndarray(float) + /// list with :math:`\alpha_s(Q2)` for the process scales + /// + /// Returns + /// ------- + /// PyFkTable : + /// produced FK table + #[allow(clippy::needless_lifetimes)] + #[allow(clippy::needless_pass_by_value)] + pub fn evolve_with_slice_iter<'py>( + &self, + slices: &Bound<'py, PyIterator>, + order_mask: PyReadonlyArray1, + xi: (f64, f64, f64), + ren1: Vec, + alphas: Vec, + ) -> PyResult { + Ok(self + .grid + .evolve( + vec![slices.into_iter().map(|slice| { + let (info, op) = slice + .unwrap() + .extract::<(PyOperatorSliceInfo, PyReadonlyArray4)>() + .unwrap(); + Ok::<_, std::io::Error>(( + info.info, + // TODO: avoid copying + CowArray::from(op.as_array().to_owned()), + )) + })], + // TODO: make `order_mask` a `Vec` + &order_mask.to_vec().unwrap(), + xi, + &AlphasTable { ren1, alphas }, + ) + .map(|fk_table| PyFkTable { fk_table }) + // TODO: avoid unwrap and convert `Result` into `PyResult` + .unwrap()) + } + /// Load from file. /// /// # Panics diff --git a/pineappl_py/src/subgrid.rs b/pineappl_py/src/subgrid.rs index 7a15ecc1..f66aabba 100644 --- a/pineappl_py/src/subgrid.rs +++ b/pineappl_py/src/subgrid.rs @@ -77,6 +77,17 @@ impl PySubgridEnum { pub fn into(&self) -> Self { self.clone() } + + /// Return the array of mu2 objects. + #[must_use] + pub fn static_scale(&self) -> Vec { + self.subgrid_enum + .static_scale() + .iter() + .cloned() + .map(|mu2| PyMu2 { mu2 }) + .collect() + } } /// Register submodule in parent. diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py new file mode 100644 index 00000000..51435bb0 --- /dev/null +++ b/pineappl_py/tests/test_evolution.py @@ -0,0 +1,111 @@ +"""Test module for the interface of the `evolution`. + +It checks the cases in which we have evolve with one, +two, and three (general) EKOs. +""" + +import itertools +import numpy as np + +from pineappl.pids import PidBasis +from pineappl.boc import Channel, Kinematics +from pineappl.grid import Order, Grid +from pineappl.convolutions import Conv, ConvType +from pineappl.interpolation import Interp +from pineappl.evolution import OperatorSliceInfo + + +class TestFkTable: + def fake_grid( + self, + channels: list[Channel], + orders: list[Order], + convolutions: list[Conv], + bins=None, + ) -> Grid: + kinematics = [ + Kinematics(0), # Scale + Kinematics(1), # x1 momentum fraction + Kinematics(2), # x2 momentum fraction + ] + # Define the interpolation specs for each item of the Kinematics + interpolations = [ + Interp( + min=1.0, + max=1e8, + nodes=40, + order=3, + ), # Interpolation on the Scale + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x1 momentum fraction + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x2 momentum fraction + ] + bin_limits = np.array( + [1e-7, 1e-3, 1] if bins is None else bins, dtype=float + ) + return Grid( + pid_basis=PidBasis.Evol, + channels=channels, + orders=orders, + bin_limits=bin_limits, + convolutions=convolutions, + interpolations=interpolations, + kinematics=kinematics, + ) + + def test_with_one_eko(self): + # Define convolution types and the initial state hadrons + # We consider an initial state Polarized Proton + h = ConvType(polarized=True, time_like=False) + h_conv = Conv(conv_type=h, pid=2212) + + # The length of the convolutions has to match the nb of hadrons + convolutions = [h_conv] + + # We define the PIDs of the partons out of the Proton + down_channel = [([1], 1.0)] # DIS-case + up_channel = [([2], 1.0)] # DIS-case + channels = [Channel(down_channel), Channel(up_channel)] + + # Now we define the perturbative orders + orders = [Order(0, 0, 0, 0, 0)] + + # Construct the Grid and fill with some values + grid = self.fake_grid(channels, orders, convolutions) + + x1g = np.linspace(0.5, 1.0, 5) + x2g = x1g.copy() + q2g = np.array([10, 90, 100]) + + for x1, x2, q2 in itertools.product(x1g, x2g, q2g): + grid.fill( + order=0, + observable=0.01, + channel=0, + ntuple=[x1, x2, q2], + weight=10, + ) + + # Check the Evolution of the Grid + info = OperatorSliceInfo( + fac0=1.0, + pids0=[], + x0=[], + fac1=1.0, + pids1=[], + x1=[], + pid_basis=PidBasis.Pdg, + conv_type=h, + ) + + # TODO: check a Toy evolution + assert isinstance(info, OperatorSliceInfo) diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index b696e3cc..7e963ec9 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -1,75 +1,112 @@ +"""Test module for the interface of the `fk_table`. + +It checks the cases in which we have one, two, and +three (general) convolutions. +""" + import numpy as np -import pineappl +from pineappl.pids import PidBasis +from pineappl.boc import Channel, Kinematics +from pineappl.grid import Order, Grid +from pineappl.convolutions import Conv, ConvType +from pineappl.interpolation import Interp +from pineappl.packed_subgrid import PackedSubgrid +from pineappl.fk_table import FkTable class TestFkTable: - def fake_grid(self, bins=None): - channels = [pineappl.boc.Channel([([1, 21], 1.0)])] - orders = [pineappl.grid.Order(0, 0, 0, 0, 0)] - bin_limits = np.array([1e-7, 1e-3, 1] if bins is None else bins, dtype=float) - subgrid_params = pineappl.subgrid.SubgridParams() - g = pineappl.grid.Grid(channels, orders, bin_limits, subgrid_params) - return g + def fake_grid( + self, + channels: list[Channel], + orders: list[Order], + convolutions: list[Conv], + bins=None, + ) -> Grid: + kinematics = [ + Kinematics(0), # Scale + Kinematics(1), # x1 momentum fraction + Kinematics(2), # x2 momentum fraction + ] + # Define the interpolation specs for each item of the Kinematics + interpolations = [ + Interp( + min=1e2, + max=1e8, + nodes=40, + order=3, + ), # Interpolation on the Scale + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x1 momentum fraction + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x2 momentum fraction + ] + bin_limits = np.array( + [1e-7, 1e-3, 1] if bins is None else bins, dtype=float + ) + return Grid( + pid_basis=PidBasis.Evol, + channels=channels, + orders=orders, + bin_limits=bin_limits, + convolutions=convolutions, + interpolations=interpolations, + kinematics=kinematics, + ) def test_convolve_with_one(self): - g = self.fake_grid() + # Define convolution types and the initial state hadrons + # We consider an initial state Polarized Proton + h = ConvType(polarized=True, time_like=False) + h_conv = Conv(conv_type=h, pid=2212) + # The length of the convolutions has to match the nb of hadrons + convolutions = [h_conv] + # We define the PIDs of the partons out of the Proton + down_channel = [([1], 1.0)] # DIS-case + up_channel = [([2], 1.0)] # DIS-case + channels = [Channel(down_channel), Channel(up_channel)] + # Now we define the perturbative orders + orders = [Order(0, 0, 0, 0, 0)] + g = self.fake_grid(channels, orders, convolutions) # DIS grid xs = np.linspace(0.5, 1.0, 5) vs = xs.copy() - subgrid = pineappl.import_only_subgrid.ImportOnlySubgridV1( + subgrid = PackedSubgrid( vs[np.newaxis, :, np.newaxis], np.array([90.0]), xs, np.array([1.0]), ) g.set_subgrid(0, 0, 0, subgrid.into()) - fk = pineappl.fk_table.FkTable(g) + fk = FkTable(g) # Convert Grid -> FkTable np.testing.assert_allclose( - fk.convolve_with_one(2212, lambda pid, x, q2: 0.0), + fk.convolve_with_one( + pdg_conv=h_conv, + xfx=lambda pid, x, q2: 0.0, + ), [0.0] * 2, ) np.testing.assert_allclose( - fk.convolve_with_one(2212, lambda pid, x, q2: 1), + fk.convolve_with_one( + pdg_conv=h_conv, + xfx=lambda pid, x, q2: 1.0, + ), [5e7 / 9999, 0.0], ) - info = pineappl.evolution.OperatorSliceInfo( - 1.0, [], [], 1.0, [], [], pineappl.pids.PidBasis.Pdg - ) - - # TODO: write a better test - try: - g.evolve_with_slice_iter( - iter( - [(info, np.ndarray([0, 0, 0, 0])), (info, np.ndarray([0, 0, 0, 0]))] - ), - np.array([], dtype=bool), - (1.0, 1.0), - [], - [], - ) - - assert False - except: - assert True - - # TODO: write a better test - try: - g.evolve_with_slice_iter2( - iter( - [(info, np.ndarray([0, 0, 0, 0])), (info, np.ndarray([0, 0, 0, 0]))] - ), - iter( - [(info, np.ndarray([0, 0, 0, 0])), (info, np.ndarray([0, 0, 0, 0]))] - ), - np.array([], dtype=bool), - (1.0, 1.0), - [], - [], - ) + def test_convolve_with_two(self): + # TODO + pass - assert False - except: - assert True + def test_convolve_with_many(self): + # TODO + pass diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index 7786a275..c964b85c 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -15,11 +15,14 @@ TYPECONV = ConvType(polarized=False, time_like=False) CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) +# Construct the Channel and Order objetcs +UP_ANTIUP_CHANNEL = [([2, -2], 0.1)] +CHANNELS = [Channel(UP_ANTIUP_CHANNEL)] +ORDERS = [Order(3, 0, 0, 0, 0)] + class TestGrid: - def fake_grid(self, bins=None): - channels = [Channel([([1, 21], 0.1)])] - orders = [Order(3, 0, 0, 0, 0)] + def fake_grid(self, channels=CHANNELS, orders=ORDERS, bins=None): # We assume symmetrical proton-proton in the initial state convolutions = [CONVOBJECT, CONVOBJECT] # Define the kinematics. Kinematics are defined as a list of items. @@ -52,7 +55,9 @@ def fake_grid(self, bins=None): order=3, ), # Interpolation on x2 momentum fraction ] - bin_limits = np.array([1e-7, 1e-3, 1] if bins is None else bins, dtype=float) + bin_limits = np.array( + [1e-7, 1e-3, 1] if bins is None else bins, dtype=float + ) g = Grid( pid_basis=PidBasis.Evol, channels=channels, @@ -70,6 +75,11 @@ def test_init(self): assert len(g.orders()) == 1 assert g.orders()[0].as_tuple() == (3, 0, 0, 0, 0) + def test_channels(self): + g = self.fake_grid() + assert len(g.channels()) == 1 + assert g.channels()[0] == UP_ANTIUP_CHANNEL + def test_set_subgrid(self): g = self.fake_grid() @@ -94,12 +104,6 @@ def test_set_subgrid(self): g.set_subgrid(0, 1, 0, subgrid.into()) g.optimize() - def test_set_key_value(self): - g = self.fake_grid() - g.set_key_value("bla", "blub") - g.set_key_value('"', "'") - g.set_key_value("äöü", "ß\\") - def test_bins(self): g = self.fake_grid() # 1D @@ -120,7 +124,7 @@ def test_bins(self): np.testing.assert_allclose(g.bin_left(1), [2, 3]) np.testing.assert_allclose(g.bin_right(1), [3, 5]) - def test_convolve_with_one(self): + def test_convolve_with_two(self): g = self.fake_grid() # Fill the subgrid-part of the GRID object @@ -197,24 +201,24 @@ def test_fill(self): pytest.approx(res) == 0.0 def test_merge(self): - g = self.fake_grid([1, 2, 3]) - g1 = self.fake_grid([3, 4, 5]) + g = self.fake_grid(bins=[1, 2, 3]) + g1 = self.fake_grid(bins=[3, 4, 5]) assert g.bins() == 2 assert g1.bins() == 2 g.merge(g1) assert g.bins() == 4 - g2 = self.fake_grid([1, 2, 3]) - g3 = self.fake_grid([1, 2, 3]) + g2 = self.fake_grid(bins=[1, 2, 3]) + g3 = self.fake_grid(bins=[1, 2, 3]) assert g2.bins() == 2 assert g3.bins() == 2 g2.merge(g3) assert g2.bins() == 2 - g4 = self.fake_grid([2, 3, 4]) - g5 = self.fake_grid([4, 5, 6]) + g4 = self.fake_grid(bins=[2, 3, 4]) + g5 = self.fake_grid(bins=[4, 5, 6]) assert g4.bins() == 2 assert g5.bins() == 2 diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py new file mode 100644 index 00000000..277aa418 --- /dev/null +++ b/pineappl_py/tests/test_subgrid.py @@ -0,0 +1,135 @@ +import pytest +import numpy as np + +from pineappl.pids import PidBasis +from pineappl.boc import Channel, Order, Kinematics +from pineappl.convolutions import Conv, ConvType +from pineappl.grid import Grid +from pineappl.interpolation import Interp +from pineappl.subgrid import SubgridEnum +from pineappl.packed_subgrid import PackedSubgrid + + +# Define some default for the minimum value of `Q2` +Q2_MIN = 1e2 + +# See `test_grid.py` for more detailed information +TYPECONV = ConvType(polarized=False, time_like=False) +CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) + + +def fake_dis_grid( + orders: Order, channels: Channel, q2_min: float = Q2_MIN +) -> Grid: + bin_limits = np.array([0.0, 1.0]) + # See `test_grid.py` for more detailed information + # on the meaning of the following parameters + convolutions = [CONVOBJECT] # Consider DIS-case + kinematics = [ + Kinematics(0), # Scale + Kinematics(1), # x1 momentum fraction + Kinematics(2), # x2 momentum fraction + ] + interpolations = [ + Interp( + min=q2_min, + max=1e8, + nodes=40, + order=3, + ), # Interpolation on the Scale + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x1 momentum fraction + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x2 momentum fraction + ] + return Grid( + pid_basis=PidBasis.Evol, + channels=channels, + orders=orders, + bin_limits=bin_limits, + convolutions=convolutions, + interpolations=interpolations, + kinematics=kinematics, + ) + + +def test_issue_164(pdf): + # https://github.com/NNPDF/pineappl/issues/164 + # DIS-like convolution now ONLY requires one entry of `PID` + channels = [Channel([([2], 1.0)])] # DIS-case + orders = [Order(0, 0, 0, 0, 0)] + + def convolve_grid(q2_min=Q2_MIN): + grid = fake_dis_grid(orders, channels, q2_min) + # Fill the Grid with some values + grid.fill( + order=0, + observable=0.5, + channel=0, + ntuple=[0.2, 0.2, 10], + weight=0.5, + ) + return grid.convolve_with_one( + pdg_conv=CONVOBJECT, + xfx=pdf.xfxQ, + alphas=pdf.alphasQ, + ) + + # Using default minimum + res = convolve_grid() + assert res == 0.0 + # lower minimum to q2=1 + res = convolve_grid(q2_min=1.0) + assert res == 0.0 + + +class TestSubgrid: + def fake_grid(sefl): + channels = [Channel([([2], 1.0)]), Channel([([3], 0.5)])] + orders = [Order(0, 0, 0, 0, 0)] + return fake_dis_grid(orders, channels) + + def fake_importonlysubgrid(self): + x1s = np.linspace(0.1, 1, 2) + x2s = np.linspace(0.5, 1, 2) + Q2s = np.linspace(10, 20, 2) + scale = [q2 for q2 in Q2s] + array = np.random.rand(len(Q2s), len(x1s), len(x2s)) + subgrid = PackedSubgrid( + array=array, + scales=scale, + x1_grid=x1s, + x2_grid=x2s, + ) + return subgrid, [x1s, x2s, scale, array] + + def test_subgrid_methods(self): + # TODO: extract the values of the scales and x grids + grid = self.fake_grid() + test_subgrid, infos = self.fake_importonlysubgrid() + x1s, x2s, mu2s, _ = (obj for obj in infos) + grid.set_subgrid(0, 0, 0, test_subgrid.into()) + extr_subgrid = grid.subgrid(0, 0, 0) + assert isinstance(extr_subgrid, SubgridEnum) + + @pytest.mark.skip(reason="No implementation of Array3 for subgrid.") + def test_to_array3(self): + # TODO: extract and check the dense array of the subgrid + # requires `impl From<&SubgridEnum> for Array3` + grid = self.fake_grid() + test_subgrid, infos = self.fake_importonlysubgrid() + _, _, _, array = (obj for obj in infos) + grid.set_subgrid(0, 0, 0, test_subgrid.into()) + extr_subgrid = grid.subgrid(0, 0, 0) + test_array = extr_subgrid.to_array3() + print(test_array) + print(array) + np.testing.assert_allclose(test_array, array) diff --git a/pineappl_py/tests/test_sugrid.py b/pineappl_py/tests/test_sugrid.py index dd82f071..9af17e57 100644 --- a/pineappl_py/tests/test_sugrid.py +++ b/pineappl_py/tests/test_sugrid.py @@ -1,4 +1,3 @@ -import pytest import numpy as np from pineappl.pids import PidBasis From bdfca60c7891d7209e27375b7093100264450b57 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 10:47:26 +0200 Subject: [PATCH 188/213] Replace `PyPackedArray` -> `PyImportSubgridV1` --- .../{packed_subgrid.rs => import_subgrid.rs} | 20 ++--- pineappl_py/src/lib.rs | 4 +- pineappl_py/tests/test_fk_table.py | 4 +- pineappl_py/tests/test_grid.py | 8 +- pineappl_py/tests/test_subgrid.py | 4 +- pineappl_py/tests/test_sugrid.py | 87 ------------------- 6 files changed, 20 insertions(+), 107 deletions(-) rename pineappl_py/src/{packed_subgrid.rs => import_subgrid.rs} (77%) delete mode 100644 pineappl_py/tests/test_sugrid.py diff --git a/pineappl_py/src/packed_subgrid.rs b/pineappl_py/src/import_subgrid.rs similarity index 77% rename from pineappl_py/src/packed_subgrid.rs rename to pineappl_py/src/import_subgrid.rs index a01258aa..e94bd80e 100644 --- a/pineappl_py/src/packed_subgrid.rs +++ b/pineappl_py/src/import_subgrid.rs @@ -2,20 +2,20 @@ use super::subgrid::PySubgridEnum; use numpy::PyReadonlyArray3; +use pineappl::import_subgrid::ImportSubgridV1; use pineappl::packed_array::PackedArray; -use pineappl::packed_subgrid::PackedQ1X2SubgridV1; use pyo3::prelude::*; /// PyO3 wrapper to :rustdoc:`pineappl`. -#[pyclass(name = "PackedSubgrid")] +#[pyclass(name = "ImportSubgridV1")] #[derive(Clone)] #[repr(transparent)] -pub struct PyPackedSubgrid { - pub(crate) packed_subgrid: PackedQ1X2SubgridV1, +pub struct PyImportSubgridV1 { + pub(crate) import_subgrid: ImportSubgridV1, } #[pymethods] -impl PyPackedSubgrid { +impl PyImportSubgridV1 { /// Constructor. /// Constructor. /// @@ -49,7 +49,7 @@ impl PyPackedSubgrid { } Self { - packed_subgrid: PackedQ1X2SubgridV1::new(sparse_array, node_values), + import_subgrid: ImportSubgridV1::new(sparse_array, node_values), } } @@ -57,7 +57,7 @@ impl PyPackedSubgrid { #[must_use] pub fn into(&self) -> PySubgridEnum { PySubgridEnum { - subgrid_enum: self.packed_subgrid.clone().into(), + subgrid_enum: self.import_subgrid.clone().into(), } } } @@ -67,7 +67,7 @@ impl PyPackedSubgrid { /// /// Raises an error if (sub)module is not found. pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { - let m = PyModule::new_bound(parent_module.py(), "packed_subgrid")?; + let m = PyModule::new_bound(parent_module.py(), "import_subgrid")?; m.setattr( pyo3::intern!(m.py(), "__doc__"), "Interface for packed subgrid specs.", @@ -75,8 +75,8 @@ pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { pyo3::py_run!( parent_module.py(), m, - "import sys; sys.modules['pineappl.packed_subgrid'] = m" + "import sys; sys.modules['pineappl.import_subgrid'] = m" ); - m.add_class::()?; + m.add_class::()?; parent_module.add_submodule(&m) } diff --git a/pineappl_py/src/lib.rs b/pineappl_py/src/lib.rs index 05196a31..fac23ad4 100644 --- a/pineappl_py/src/lib.rs +++ b/pineappl_py/src/lib.rs @@ -10,8 +10,8 @@ pub mod convolutions; pub mod evolution; pub mod fk_table; pub mod grid; +pub mod import_subgrid; pub mod interpolation; -pub mod packed_subgrid; pub mod pids; pub mod subgrid; @@ -25,7 +25,7 @@ fn pineappl(m: &Bound<'_, PyModule>) -> PyResult<()> { fk_table::register(m)?; grid::register(m)?; interpolation::register(m)?; - packed_subgrid::register(m)?; + import_subgrid::register(m)?; pids::register(m)?; subgrid::register(m)?; m.add("version", env!("CARGO_PKG_VERSION"))?; diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index 7e963ec9..5508c522 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -11,8 +11,8 @@ from pineappl.grid import Order, Grid from pineappl.convolutions import Conv, ConvType from pineappl.interpolation import Interp -from pineappl.packed_subgrid import PackedSubgrid from pineappl.fk_table import FkTable +from pineappl.import_subgrid import ImportSubgridV1 class TestFkTable: @@ -80,7 +80,7 @@ def test_convolve_with_one(self): # DIS grid xs = np.linspace(0.5, 1.0, 5) vs = xs.copy() - subgrid = PackedSubgrid( + subgrid = ImportSubgridV1( vs[np.newaxis, :, np.newaxis], np.array([90.0]), xs, diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index c964b85c..f44e4379 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -7,7 +7,7 @@ from pineappl.grid import Order, Grid from pineappl.convolutions import Conv, ConvType from pineappl.interpolation import Interp -from pineappl.packed_subgrid import PackedSubgrid +from pineappl.import_subgrid import ImportSubgridV1 # Construct the type of convolutions and the convolution object @@ -86,7 +86,7 @@ def test_set_subgrid(self): # DIS grid xs = np.linspace(0.1, 1.0, 5) vs = np.random.rand(len(xs)) - subgrid = PackedSubgrid( + subgrid = ImportSubgridV1( vs[np.newaxis, :, np.newaxis], np.array([90.0]), xs, @@ -98,7 +98,7 @@ def test_set_subgrid(self): x1s = np.linspace(0.1, 1, 2) x2s = np.linspace(0.5, 1, 2) Q2s = np.linspace(10, 20, 2) - subgrid = PackedSubgrid( + subgrid = ImportSubgridV1( np.random.rand(len(Q2s), len(x1s), len(x2s)), Q2s, x1s, x2s ) g.set_subgrid(0, 1, 0, subgrid.into()) @@ -130,7 +130,7 @@ def test_convolve_with_two(self): # Fill the subgrid-part of the GRID object xs = np.linspace(0.5, 1.0, 5) vs = xs.copy() - subgrid = PackedSubgrid( + subgrid = ImportSubgridV1( array=vs[np.newaxis, :, np.newaxis], scales=np.array([90.0]), x1_grid=xs, diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py index 277aa418..fe44e190 100644 --- a/pineappl_py/tests/test_subgrid.py +++ b/pineappl_py/tests/test_subgrid.py @@ -7,7 +7,7 @@ from pineappl.grid import Grid from pineappl.interpolation import Interp from pineappl.subgrid import SubgridEnum -from pineappl.packed_subgrid import PackedSubgrid +from pineappl.import_subgrid import ImportSubgridV1 # Define some default for the minimum value of `Q2` @@ -103,7 +103,7 @@ def fake_importonlysubgrid(self): Q2s = np.linspace(10, 20, 2) scale = [q2 for q2 in Q2s] array = np.random.rand(len(Q2s), len(x1s), len(x2s)) - subgrid = PackedSubgrid( + subgrid = ImportSubgridV1( array=array, scales=scale, x1_grid=x1s, diff --git a/pineappl_py/tests/test_sugrid.py b/pineappl_py/tests/test_sugrid.py deleted file mode 100644 index 9af17e57..00000000 --- a/pineappl_py/tests/test_sugrid.py +++ /dev/null @@ -1,87 +0,0 @@ -import numpy as np - -from pineappl.pids import PidBasis -from pineappl.boc import Channel, Order, Kinematics -from pineappl.convolutions import Conv, ConvType -from pineappl.grid import Grid -from pineappl.subgrid import SubgridParams -from pineappl.interpolation import Interp - - -# See `test_grid.py` for more detailed information -TYPECONV = ConvType(polarized=False, time_like=False) -CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) - - -class TestSubgridParams: - def test_init(self): - sp = SubgridParams() - assert isinstance(sp, SubgridParams) - - -def test_issue_164(pdf): - # https://github.com/NNPDF/pineappl/issues/164 - channels = [Channel([([1, 2], 1.0)])] - orders = [Order(0, 0, 0, 0, 0)] - params = SubgridParams() - - def convolve_grid(): - bin_limits = np.array([0.0, 1.0]) - # See `test_grid.py` for more detailed information - # on the meaning of the following parameters - convolutions = [CONVOBJECT] # Consider DIS-case - kinematics = [ - Kinematics(0), # Scale - Kinematics(1), # x1 momentum fraction - Kinematics(2), # x2 momentum fraction - ] - interpolations = [ - Interp( - min=1e2, - max=1e8, - nodes=40, - order=3, - ), # Interpolation on the Scale - Interp( - min=2e-7, - max=1.0, - nodes=50, - order=3, - ), # Interpolation on x1 momentum fraction - Interp( - min=2e-7, - max=1.0, - nodes=50, - order=3, - ), # Interpolation on x2 momentum fraction - ] - grid = Grid( - pid_basis=PidBasis.Evol, - channels=channels, - orders=orders, - bin_limits=bin_limits, - convolutions=convolutions, - interpolations=interpolations, - kinematics=kinematics, - ) - grid.fill( - order=0, - observable=0.5, - channel=0, - ntuple=[0.2, 0.2, 10], - weight=0.5, - ) - return grid.convolve_with_one( - pdg_conv=CONVOBJECT, - xfx=pdf.xfxQ, - alphas=pdf.alphasQ, - ) - - # default minimum is q2=100 - res = convolve_grid() - assert res == 0.0 - - # lower minimum to q2=1 - params.set_q2_min(1.0) - res = convolve_grid() - assert res == 0.0 From 5f68dcf81d9f82c3696224dd69f88fdff4b53442 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 13:25:13 +0200 Subject: [PATCH 189/213] Add docstrings to Python Interface and clean tests --- pineappl_py/src/bin.rs | 1 - pineappl_py/src/boc.rs | 15 ++++++---- pineappl_py/src/convolutions.rs | 5 ++-- pineappl_py/src/evolution.rs | 3 +- pineappl_py/src/fk_table.rs | 25 ++++++++-------- pineappl_py/src/grid.rs | 47 ++++++++++++++++++------------- pineappl_py/src/import_subgrid.rs | 3 +- pineappl_py/src/interpolation.rs | 15 +++++++++- pineappl_py/tests/test_boc.py | 2 +- pineappl_py/tests/test_grid.py | 10 +++++-- pineappl_py/tests/test_subgrid.py | 6 ++-- 11 files changed, 81 insertions(+), 51 deletions(-) diff --git a/pineappl_py/src/bin.rs b/pineappl_py/src/bin.rs index 2d043909..3bdbe4fb 100644 --- a/pineappl_py/src/bin.rs +++ b/pineappl_py/src/bin.rs @@ -28,7 +28,6 @@ impl PyBinRemapper { /// bin limits #[new] #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn new(normalizations: PyReadonlyArray1, limits: Vec<(f64, f64)>) -> Self { Self { bin_remapper: BinRemapper::new(normalizations.to_vec().unwrap(), limits).unwrap(), diff --git a/pineappl_py/src/boc.rs b/pineappl_py/src/boc.rs index 1440adb5..d2c7cbcb 100644 --- a/pineappl_py/src/boc.rs +++ b/pineappl_py/src/boc.rs @@ -8,9 +8,8 @@ use pyo3::prelude::*; /// /// Each entry consists of a tuple, which contains, in the following order: /// -/// 1. the PDG id of the first incoming parton -/// 2. the PDG id of the second parton -/// 3. a numerical factor that will multiply the result for this specific combination. +/// 1. a list containing the PDG value of the 1st, 2nd, and etc. of the incoming parton +/// 2. a numerical factor that will multiply the result for this specific combination. #[pyclass(name = "Channel")] #[repr(transparent)] pub struct PyChannel { @@ -45,7 +44,7 @@ impl PyChannel { } } -/// PyO3 wrapper to :rustdoc:`pineappl`. +/// PyO3 wrapper to :rustdoc:`pineappl::boc::Kinematics `. #[pyclass(name = "Kinematics")] #[repr(transparent)] pub struct PyKinematics { @@ -61,6 +60,13 @@ impl PyKinematics { #[pymethods] impl PyKinematics { /// Constructor. + /// + /// Parameters + /// ---------- + /// kinematic: int + /// an integer representing the kinematic. 0 represents the scale, + /// 1 represents the momentum fraction of the first parton, and 2 + /// represents the momentum fraction of the second parton. #[new] #[must_use] pub const fn new_kin(kinematic: usize) -> Self { @@ -149,7 +155,6 @@ impl PyOrder { /// boolean array, to be used as orders' mask #[staticmethod] #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn create_mask<'py>( orders: Vec>, max_as: u8, diff --git a/pineappl_py/src/convolutions.rs b/pineappl_py/src/convolutions.rs index 2f91d30a..02263ae3 100644 --- a/pineappl_py/src/convolutions.rs +++ b/pineappl_py/src/convolutions.rs @@ -3,7 +3,7 @@ use pineappl::convolutions::{Conv, ConvType}; use pyo3::prelude::*; -/// PyO3 wrapper to :rustdoc:`pineappl::`. +/// PyO3 wrapper to :rustdoc:`pineappl::convolutions::ConvType `. #[pyclass(name = "ConvType")] #[repr(transparent)] pub struct PyConvType { @@ -26,7 +26,7 @@ impl PyConvType { } } -/// PyO3 wrapper to :rustdoc:`pineappl::`. +/// PyO3 wrapper to :rustdoc:`pineappl::convolutions::Conv `. #[pyclass(name = "Conv")] #[repr(transparent)] pub struct PyConv { @@ -44,7 +44,6 @@ impl PyConv { /// Constructor. #[new] #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn new_conv(conv_type: PyRef, pid: i32) -> Self { Self::new(Conv::new(conv_type.convtype, pid)) } diff --git a/pineappl_py/src/evolution.rs b/pineappl_py/src/evolution.rs index 344fdced..e4c444e4 100644 --- a/pineappl_py/src/evolution.rs +++ b/pineappl_py/src/evolution.rs @@ -34,9 +34,10 @@ impl PyOperatorSliceInfo { /// x-grid at the final scale /// pid_basis : PyPidBasis /// flavor basis reprentation at the initial scale + /// conv_type : PyConvType + /// the type of convolution required #[new] #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn new( fac0: f64, pids0: Vec, diff --git a/pineappl_py/src/fk_table.rs b/pineappl_py/src/fk_table.rs index dcbf1a67..8b1f53d2 100644 --- a/pineappl_py/src/fk_table.rs +++ b/pineappl_py/src/fk_table.rs @@ -12,13 +12,6 @@ use std::io::BufReader; use std::path::PathBuf; use std::str::FromStr; -/// PyO3 wrapper to :rustdoc:`pineappl::fk_table::FkTable `. -#[pyclass(name = "FkTable")] -#[repr(transparent)] -pub struct PyFkTable { - pub(crate) fk_table: FkTable, -} - /// PyO3 wrapper to :rustdoc:`pineappl::fk_table::FkAssumptions `. #[pyclass(name = "FkAssumptions")] #[repr(transparent)] @@ -40,6 +33,13 @@ impl PyFkAssumptions { } } +/// PyO3 wrapper to :rustdoc:`pineappl::fk_table::FkTable `. +#[pyclass(name = "FkTable")] +#[repr(transparent)] +pub struct PyFkTable { + pub(crate) fk_table: FkTable, +} + #[pymethods] impl PyFkTable { /// Constructor from an existing grid. @@ -53,7 +53,13 @@ impl PyFkTable { } } - /// Read from given path. + /// Read an FK Table from given path. + /// + /// Parameteters + /// ------------ + /// path : str + /// path to the FK table + /// /// # Panics /// TODO #[must_use] @@ -241,7 +247,6 @@ impl PyFkTable { /// numpy.ndarray(float) : /// cross sections for all bins #[must_use] - #[allow(clippy::needless_pass_by_value)] #[pyo3(signature = (pdg_conv, xfx, bin_indices = None, channel_mask= None))] pub fn convolve_with_one<'py>( &self, @@ -285,7 +290,6 @@ impl PyFkTable { /// numpy.ndarray(float) : /// cross sections for all bins #[must_use] - #[allow(clippy::needless_pass_by_value)] #[pyo3(signature = (pdg_conv1, xfx1, pdg_conv2, xfx2, bin_indices = None, channel_mask= None))] pub fn convolve_with_two<'py>( &self, @@ -324,7 +328,6 @@ impl PyFkTable { /// assumptions : PyFkAssumptions /// assumptions about the FkTable properties, declared by the user, deciding which /// optimizations are possible - #[allow(clippy::needless_pass_by_value)] pub fn optimize(&mut self, assumptions: PyRef) { self.fk_table.optimize(assumptions.fk_assumptions); } diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 93024426..396196d0 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -31,9 +31,7 @@ pub struct PyGrid { #[pymethods] impl PyGrid { - /// Constructor. - /// - /// TODO Exposes all the arguments + /// Constructor to instantiate a new Grid. /// /// # Panics /// @@ -41,14 +39,20 @@ impl PyGrid { /// /// Parameters /// ---------- + /// pid_basis : PidBasis + /// choice of basis which can be `Evol` or `Pdg` /// channels : list(PyChannel) /// channels /// orders : list(PyOrder) /// orders /// bin_limits : list(float) /// bin configurations - /// subgrid_params : PySubgridParams - /// subgrid parameters + /// convolutions : list(PyConv) + /// contains the types of convolution + /// interpolations : list(PyInterp) + /// types of interpolations required by each kinematic + /// kinematics : list(PyKinematics) + /// list of kinematics #[new] #[must_use] pub fn new_grid( @@ -98,7 +102,6 @@ impl PyGrid { /// list containing information on kinematics /// weight : float /// cross section weight - #[allow(clippy::needless_pass_by_value)] pub fn fill( &mut self, order: usize, @@ -119,6 +122,17 @@ impl PyGrid { } /// Set a subgrid. + /// + /// Parameters + /// ---------- + /// order : int + /// order index + /// bin : int + /// bin index + /// channel : int + /// channel index + /// subgrid : PySubgridEnum + /// subgrid object pub fn set_subgrid( &mut self, order: usize, @@ -151,8 +165,8 @@ impl PyGrid { /// /// Parameters /// ---------- - /// pdg_id : int - /// PDG Monte Carlo ID of the hadronic particle + /// pdg_conv : PyConv + /// contains the types of convolutions and PID /// xfx : callable /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid /// alphas : callable @@ -180,7 +194,6 @@ impl PyGrid { /// cross sections for all bins, for each scale-variation tuple (first all bins, then /// the scale variation) #[must_use] - #[allow(clippy::needless_pass_by_value)] #[pyo3(signature = (pdg_conv, xfx, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] pub fn convolve_with_one<'py>( &self, @@ -217,12 +230,12 @@ impl PyGrid { /// /// Parameters /// ---------- - /// pdg_id1 : int - /// PDG Monte Carlo ID of the first hadronic particle + /// pdg_conv1 : PyConv + /// contains the types of convolutions and PID /// xfx1 : callable /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid - /// pdg_id2 : int - /// PDG Monte Carlo ID of the second hadronic particle + /// pdg_conv2 : PyConv + /// contains the types of convolutions and PID /// xfx2 : callable /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid /// alphas : callable @@ -250,7 +263,6 @@ impl PyGrid { /// cross sections for all bins, for each scale-variation tuple (first all bins, then /// the scale variation) #[must_use] - #[allow(clippy::needless_pass_by_value)] #[pyo3(signature = (pdg_conv1, xfx1, pdg_conv2, xfx2, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] pub fn convolve_with_two<'py>( &self, @@ -288,7 +300,6 @@ impl PyGrid { /// TODO // #[pyo3(signature = (pdg_convs, xfxs, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn convolve<'py>( &self, _pdg_convs: Vec>, @@ -319,7 +330,6 @@ impl PyGrid { /// PyEvolveInfo : /// evolution informations #[must_use] - #[allow(clippy::needless_pass_by_value)] pub fn evolve_info(&self, order_mask: PyReadonlyArray1) -> PyEvolveInfo { PyEvolveInfo { evolve_info: self.grid.evolve_info(order_mask.as_slice().unwrap()), @@ -327,6 +337,7 @@ impl PyGrid { } /// Evolve grid with as many EKOs as Convolutions. + /// /// TODO: Expose `slices` to be a vector!!! /// /// # Panics @@ -353,7 +364,6 @@ impl PyGrid { /// PyFkTable : /// produced FK table #[allow(clippy::needless_lifetimes)] - #[allow(clippy::needless_pass_by_value)] pub fn evolve<'py>( &self, slices: &Bound<'py, PyIterator>, @@ -412,7 +422,6 @@ impl PyGrid { /// PyFkTable : /// produced FK table #[allow(clippy::needless_lifetimes)] - #[allow(clippy::needless_pass_by_value)] pub fn evolve_with_slice_iter<'py>( &self, slices: &Bound<'py, PyIterator>, @@ -651,7 +660,6 @@ impl PyGrid { /// ---------- /// factors : numpy.ndarray[float] /// bin-dependent factors by which to scale - #[allow(clippy::needless_pass_by_value)] pub fn scale_by_bin(&mut self, factors: PyReadonlyArray1) { self.grid.scale_by_bin(&factors.to_vec().unwrap()); } @@ -668,7 +676,6 @@ impl PyGrid { /// ---------- /// bin_indices : numpy.ndarray[int] /// list of indices of bins to removed - #[allow(clippy::needless_pass_by_value)] pub fn delete_bins(&mut self, bin_indices: PyReadonlyArray1) { self.grid.delete_bins(&bin_indices.to_vec().unwrap()); } diff --git a/pineappl_py/src/import_subgrid.rs b/pineappl_py/src/import_subgrid.rs index e94bd80e..6cef84e3 100644 --- a/pineappl_py/src/import_subgrid.rs +++ b/pineappl_py/src/import_subgrid.rs @@ -6,7 +6,7 @@ use pineappl::import_subgrid::ImportSubgridV1; use pineappl::packed_array::PackedArray; use pyo3::prelude::*; -/// PyO3 wrapper to :rustdoc:`pineappl`. +/// PyO3 wrapper to :rustdoc:`pineappl::import_subgrid::ImportSubgridV1 `. #[pyclass(name = "ImportSubgridV1")] #[derive(Clone)] #[repr(transparent)] @@ -16,7 +16,6 @@ pub struct PyImportSubgridV1 { #[pymethods] impl PyImportSubgridV1 { - /// Constructor. /// Constructor. /// /// Parameters diff --git a/pineappl_py/src/interpolation.rs b/pineappl_py/src/interpolation.rs index 1ff5f246..fc1fd93a 100644 --- a/pineappl_py/src/interpolation.rs +++ b/pineappl_py/src/interpolation.rs @@ -3,7 +3,7 @@ use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; use pyo3::{prelude::*, pyclass}; -/// PyO3 wrapper to :rustdoc:`pineappl::`. +/// PyO3 wrapper to :rustdoc:`pineappl::interpolation::Interp `. #[pyclass(name = "Interp")] #[repr(transparent)] pub struct PyInterp { @@ -19,6 +19,19 @@ impl PyInterp { #[pymethods] impl PyInterp { /// Constructor. + /// + /// TODO: Exposes ReweightMeth,reweight Map, and InterpMeth + /// + /// Parameteters + /// ------------ + /// min : float + /// minimum value of the node + /// max : float + /// maximum value of the node + /// nodes : int + /// number of nodes + /// order : int + /// order of the interpolation #[new] #[must_use] pub fn new_interp(min: f64, max: f64, nodes: usize, order: usize) -> Self { diff --git a/pineappl_py/tests/test_boc.py b/pineappl_py/tests/test_boc.py index bcac5be9..a321c3d0 100644 --- a/pineappl_py/tests/test_boc.py +++ b/pineappl_py/tests/test_boc.py @@ -16,7 +16,7 @@ def test_init(self): class TestOrder: - def create_order(self, args=(2, 1, 0, 1, 0)): + def create_order(self, args: tuple = (2, 1, 0, 1, 0)) -> Order: return Order(*args) def test_init(self): diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index f44e4379..4132cccf 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -22,7 +22,12 @@ class TestGrid: - def fake_grid(self, channels=CHANNELS, orders=ORDERS, bins=None): + def fake_grid( + self, + channels: list[Channel] = CHANNELS, + orders: list[Order] = ORDERS, + bins: list | None = None, + ) -> Grid: # We assume symmetrical proton-proton in the initial state convolutions = [CONVOBJECT, CONVOBJECT] # Define the kinematics. Kinematics are defined as a list of items. @@ -58,7 +63,7 @@ def fake_grid(self, channels=CHANNELS, orders=ORDERS, bins=None): bin_limits = np.array( [1e-7, 1e-3, 1] if bins is None else bins, dtype=float ) - g = Grid( + return Grid( pid_basis=PidBasis.Evol, channels=channels, orders=orders, @@ -67,7 +72,6 @@ def fake_grid(self, channels=CHANNELS, orders=ORDERS, bins=None): interpolations=interpolations, kinematics=kinematics, ) - return g def test_init(self): g = self.fake_grid() diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py index fe44e190..09e17f6a 100644 --- a/pineappl_py/tests/test_subgrid.py +++ b/pineappl_py/tests/test_subgrid.py @@ -67,7 +67,7 @@ def test_issue_164(pdf): channels = [Channel([([2], 1.0)])] # DIS-case orders = [Order(0, 0, 0, 0, 0)] - def convolve_grid(q2_min=Q2_MIN): + def convolve_grid(q2_min: float = Q2_MIN) -> Grid: grid = fake_dis_grid(orders, channels, q2_min) # Fill the Grid with some values grid.fill( @@ -92,12 +92,12 @@ def convolve_grid(q2_min=Q2_MIN): class TestSubgrid: - def fake_grid(sefl): + def fake_grid(self): channels = [Channel([([2], 1.0)]), Channel([([3], 0.5)])] orders = [Order(0, 0, 0, 0, 0)] return fake_dis_grid(orders, channels) - def fake_importonlysubgrid(self): + def fake_importonlysubgrid(self) -> tuple: x1s = np.linspace(0.1, 1, 2) x2s = np.linspace(0.5, 1, 2) Q2s = np.linspace(10, 20, 2) From f354849fe684d9cd9e4ce0004c32c3916297fc36 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 13:29:51 +0200 Subject: [PATCH 190/213] Sort import modules for tests --- pineappl_py/tests/test_bin.py | 1 - pineappl_py/tests/test_evolution.py | 8 ++++---- pineappl_py/tests/test_fk_table.py | 7 +++---- pineappl_py/tests/test_grid.py | 10 ++++------ pineappl_py/tests/test_subgrid.py | 10 ++++------ 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/pineappl_py/tests/test_bin.py b/pineappl_py/tests/test_bin.py index 6c5c683c..5f32a993 100644 --- a/pineappl_py/tests/test_bin.py +++ b/pineappl_py/tests/test_bin.py @@ -1,6 +1,5 @@ import numpy as np import pytest - from pineappl.bin import BinRemapper diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py index 51435bb0..4a296280 100644 --- a/pineappl_py/tests/test_evolution.py +++ b/pineappl_py/tests/test_evolution.py @@ -5,14 +5,14 @@ """ import itertools -import numpy as np -from pineappl.pids import PidBasis +import numpy as np from pineappl.boc import Channel, Kinematics -from pineappl.grid import Order, Grid from pineappl.convolutions import Conv, ConvType -from pineappl.interpolation import Interp from pineappl.evolution import OperatorSliceInfo +from pineappl.grid import Grid, Order +from pineappl.interpolation import Interp +from pineappl.pids import PidBasis class TestFkTable: diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index 5508c522..1f9781df 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -5,14 +5,13 @@ """ import numpy as np - -from pineappl.pids import PidBasis from pineappl.boc import Channel, Kinematics -from pineappl.grid import Order, Grid from pineappl.convolutions import Conv, ConvType -from pineappl.interpolation import Interp from pineappl.fk_table import FkTable +from pineappl.grid import Grid, Order from pineappl.import_subgrid import ImportSubgridV1 +from pineappl.interpolation import Interp +from pineappl.pids import PidBasis class TestFkTable: diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index 4132cccf..ee42b758 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -1,14 +1,12 @@ import numpy as np import pytest - -from pineappl.pids import PidBasis -from pineappl.boc import Channel, Kinematics from pineappl.bin import BinRemapper -from pineappl.grid import Order, Grid +from pineappl.boc import Channel, Kinematics from pineappl.convolutions import Conv, ConvType -from pineappl.interpolation import Interp +from pineappl.grid import Grid, Order from pineappl.import_subgrid import ImportSubgridV1 - +from pineappl.interpolation import Interp +from pineappl.pids import PidBasis # Construct the type of convolutions and the convolution object # We assume unpolarized proton PDF diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py index 09e17f6a..c560499a 100644 --- a/pineappl_py/tests/test_subgrid.py +++ b/pineappl_py/tests/test_subgrid.py @@ -1,14 +1,12 @@ -import pytest import numpy as np - -from pineappl.pids import PidBasis -from pineappl.boc import Channel, Order, Kinematics +import pytest +from pineappl.boc import Channel, Kinematics, Order from pineappl.convolutions import Conv, ConvType from pineappl.grid import Grid +from pineappl.import_subgrid import ImportSubgridV1 from pineappl.interpolation import Interp +from pineappl.pids import PidBasis from pineappl.subgrid import SubgridEnum -from pineappl.import_subgrid import ImportSubgridV1 - # Define some default for the minimum value of `Q2` Q2_MIN = 1e2 From f5f46d65c5cb27582125075fa164b5b1bb0fa1eb Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 16:27:58 +0200 Subject: [PATCH 191/213] Init general convolution for PyAPI --- pineappl_py/src/grid.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 396196d0..b64c4d60 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -297,20 +297,29 @@ impl PyGrid { .into_pyarray_bound(py) } + /// Convolve a grid with as many convolutions. + /// + /// # Panics /// TODO // #[pyo3(signature = (pdg_convs, xfxs, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] #[must_use] pub fn convolve<'py>( &self, - _pdg_convs: Vec>, - _xfxs: &Bound<'py, PyIterator>, - _alphas: &Bound<'py, PyAny>, - _order_mask: Option>, - _bin_indices: Option>, - _channel_mask: Option>, - _xi: Option>, - _py: Python<'py>, + pdg_convs: Vec>, + xfxs: Vec, + alphas: PyObject, + order_mask: Option>, + bin_indices: Option>, + channel_mask: Option>, + xi: Option>, + py: Python<'py>, ) -> Bound<'py, PyArray1> { + // Closure for alphas function + let mut alphas = |q2: f64| { + let result: f64 = alphas.call1(py, (q2,)).unwrap().extract(py).unwrap(); + result + }; + todo!() } From cfc1edf44eda083efa07754a83b43269c4f16f1a Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 16:34:01 +0200 Subject: [PATCH 192/213] Fix type annotations for python tests --- pineappl_py/tests/test_evolution.py | 6 +- pineappl_py/tests/test_fk_table.py | 6 +- pineappl_py/tests/test_grid.py | 4 +- pineappl_py/tests/test_subgrid.py | 133 ---------------------------- 4 files changed, 8 insertions(+), 141 deletions(-) delete mode 100644 pineappl_py/tests/test_subgrid.py diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py index 4a296280..9e867e39 100644 --- a/pineappl_py/tests/test_evolution.py +++ b/pineappl_py/tests/test_evolution.py @@ -18,9 +18,9 @@ class TestFkTable: def fake_grid( self, - channels: list[Channel], - orders: list[Order], - convolutions: list[Conv], + channels: list(Channel), + orders: list(Order), + convolutions: list(Conv), bins=None, ) -> Grid: kinematics = [ diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index 1f9781df..1055a985 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -17,9 +17,9 @@ class TestFkTable: def fake_grid( self, - channels: list[Channel], - orders: list[Order], - convolutions: list[Conv], + channels: list(Channel), + orders: list(Order), + convolutions: list(Conv), bins=None, ) -> Grid: kinematics = [ diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index ee42b758..860ec486 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -22,8 +22,8 @@ class TestGrid: def fake_grid( self, - channels: list[Channel] = CHANNELS, - orders: list[Order] = ORDERS, + channels: list(Channel) = CHANNELS, + orders: list(Order) = ORDERS, bins: list | None = None, ) -> Grid: # We assume symmetrical proton-proton in the initial state diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py deleted file mode 100644 index c560499a..00000000 --- a/pineappl_py/tests/test_subgrid.py +++ /dev/null @@ -1,133 +0,0 @@ -import numpy as np -import pytest -from pineappl.boc import Channel, Kinematics, Order -from pineappl.convolutions import Conv, ConvType -from pineappl.grid import Grid -from pineappl.import_subgrid import ImportSubgridV1 -from pineappl.interpolation import Interp -from pineappl.pids import PidBasis -from pineappl.subgrid import SubgridEnum - -# Define some default for the minimum value of `Q2` -Q2_MIN = 1e2 - -# See `test_grid.py` for more detailed information -TYPECONV = ConvType(polarized=False, time_like=False) -CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) - - -def fake_dis_grid( - orders: Order, channels: Channel, q2_min: float = Q2_MIN -) -> Grid: - bin_limits = np.array([0.0, 1.0]) - # See `test_grid.py` for more detailed information - # on the meaning of the following parameters - convolutions = [CONVOBJECT] # Consider DIS-case - kinematics = [ - Kinematics(0), # Scale - Kinematics(1), # x1 momentum fraction - Kinematics(2), # x2 momentum fraction - ] - interpolations = [ - Interp( - min=q2_min, - max=1e8, - nodes=40, - order=3, - ), # Interpolation on the Scale - Interp( - min=2e-7, - max=1.0, - nodes=50, - order=3, - ), # Interpolation on x1 momentum fraction - Interp( - min=2e-7, - max=1.0, - nodes=50, - order=3, - ), # Interpolation on x2 momentum fraction - ] - return Grid( - pid_basis=PidBasis.Evol, - channels=channels, - orders=orders, - bin_limits=bin_limits, - convolutions=convolutions, - interpolations=interpolations, - kinematics=kinematics, - ) - - -def test_issue_164(pdf): - # https://github.com/NNPDF/pineappl/issues/164 - # DIS-like convolution now ONLY requires one entry of `PID` - channels = [Channel([([2], 1.0)])] # DIS-case - orders = [Order(0, 0, 0, 0, 0)] - - def convolve_grid(q2_min: float = Q2_MIN) -> Grid: - grid = fake_dis_grid(orders, channels, q2_min) - # Fill the Grid with some values - grid.fill( - order=0, - observable=0.5, - channel=0, - ntuple=[0.2, 0.2, 10], - weight=0.5, - ) - return grid.convolve_with_one( - pdg_conv=CONVOBJECT, - xfx=pdf.xfxQ, - alphas=pdf.alphasQ, - ) - - # Using default minimum - res = convolve_grid() - assert res == 0.0 - # lower minimum to q2=1 - res = convolve_grid(q2_min=1.0) - assert res == 0.0 - - -class TestSubgrid: - def fake_grid(self): - channels = [Channel([([2], 1.0)]), Channel([([3], 0.5)])] - orders = [Order(0, 0, 0, 0, 0)] - return fake_dis_grid(orders, channels) - - def fake_importonlysubgrid(self) -> tuple: - x1s = np.linspace(0.1, 1, 2) - x2s = np.linspace(0.5, 1, 2) - Q2s = np.linspace(10, 20, 2) - scale = [q2 for q2 in Q2s] - array = np.random.rand(len(Q2s), len(x1s), len(x2s)) - subgrid = ImportSubgridV1( - array=array, - scales=scale, - x1_grid=x1s, - x2_grid=x2s, - ) - return subgrid, [x1s, x2s, scale, array] - - def test_subgrid_methods(self): - # TODO: extract the values of the scales and x grids - grid = self.fake_grid() - test_subgrid, infos = self.fake_importonlysubgrid() - x1s, x2s, mu2s, _ = (obj for obj in infos) - grid.set_subgrid(0, 0, 0, test_subgrid.into()) - extr_subgrid = grid.subgrid(0, 0, 0) - assert isinstance(extr_subgrid, SubgridEnum) - - @pytest.mark.skip(reason="No implementation of Array3 for subgrid.") - def test_to_array3(self): - # TODO: extract and check the dense array of the subgrid - # requires `impl From<&SubgridEnum> for Array3` - grid = self.fake_grid() - test_subgrid, infos = self.fake_importonlysubgrid() - _, _, _, array = (obj for obj in infos) - grid.set_subgrid(0, 0, 0, test_subgrid.into()) - extr_subgrid = grid.subgrid(0, 0, 0) - test_array = extr_subgrid.to_array3() - print(test_array) - print(array) - np.testing.assert_allclose(test_array, array) From bd3438a300a472f24b0b769ac94ff4f842666b24 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 16:50:48 +0200 Subject: [PATCH 193/213] Revert "Fix type annotations for python tests" This reverts commit cfc1edf44eda083efa07754a83b43269c4f16f1a. --- pineappl_py/tests/test_evolution.py | 6 +- pineappl_py/tests/test_fk_table.py | 6 +- pineappl_py/tests/test_grid.py | 4 +- pineappl_py/tests/test_subgrid.py | 133 ++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 pineappl_py/tests/test_subgrid.py diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py index 9e867e39..4a296280 100644 --- a/pineappl_py/tests/test_evolution.py +++ b/pineappl_py/tests/test_evolution.py @@ -18,9 +18,9 @@ class TestFkTable: def fake_grid( self, - channels: list(Channel), - orders: list(Order), - convolutions: list(Conv), + channels: list[Channel], + orders: list[Order], + convolutions: list[Conv], bins=None, ) -> Grid: kinematics = [ diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index 1055a985..1f9781df 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -17,9 +17,9 @@ class TestFkTable: def fake_grid( self, - channels: list(Channel), - orders: list(Order), - convolutions: list(Conv), + channels: list[Channel], + orders: list[Order], + convolutions: list[Conv], bins=None, ) -> Grid: kinematics = [ diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index 860ec486..ee42b758 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -22,8 +22,8 @@ class TestGrid: def fake_grid( self, - channels: list(Channel) = CHANNELS, - orders: list(Order) = ORDERS, + channels: list[Channel] = CHANNELS, + orders: list[Order] = ORDERS, bins: list | None = None, ) -> Grid: # We assume symmetrical proton-proton in the initial state diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py new file mode 100644 index 00000000..c560499a --- /dev/null +++ b/pineappl_py/tests/test_subgrid.py @@ -0,0 +1,133 @@ +import numpy as np +import pytest +from pineappl.boc import Channel, Kinematics, Order +from pineappl.convolutions import Conv, ConvType +from pineappl.grid import Grid +from pineappl.import_subgrid import ImportSubgridV1 +from pineappl.interpolation import Interp +from pineappl.pids import PidBasis +from pineappl.subgrid import SubgridEnum + +# Define some default for the minimum value of `Q2` +Q2_MIN = 1e2 + +# See `test_grid.py` for more detailed information +TYPECONV = ConvType(polarized=False, time_like=False) +CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) + + +def fake_dis_grid( + orders: Order, channels: Channel, q2_min: float = Q2_MIN +) -> Grid: + bin_limits = np.array([0.0, 1.0]) + # See `test_grid.py` for more detailed information + # on the meaning of the following parameters + convolutions = [CONVOBJECT] # Consider DIS-case + kinematics = [ + Kinematics(0), # Scale + Kinematics(1), # x1 momentum fraction + Kinematics(2), # x2 momentum fraction + ] + interpolations = [ + Interp( + min=q2_min, + max=1e8, + nodes=40, + order=3, + ), # Interpolation on the Scale + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x1 momentum fraction + Interp( + min=2e-7, + max=1.0, + nodes=50, + order=3, + ), # Interpolation on x2 momentum fraction + ] + return Grid( + pid_basis=PidBasis.Evol, + channels=channels, + orders=orders, + bin_limits=bin_limits, + convolutions=convolutions, + interpolations=interpolations, + kinematics=kinematics, + ) + + +def test_issue_164(pdf): + # https://github.com/NNPDF/pineappl/issues/164 + # DIS-like convolution now ONLY requires one entry of `PID` + channels = [Channel([([2], 1.0)])] # DIS-case + orders = [Order(0, 0, 0, 0, 0)] + + def convolve_grid(q2_min: float = Q2_MIN) -> Grid: + grid = fake_dis_grid(orders, channels, q2_min) + # Fill the Grid with some values + grid.fill( + order=0, + observable=0.5, + channel=0, + ntuple=[0.2, 0.2, 10], + weight=0.5, + ) + return grid.convolve_with_one( + pdg_conv=CONVOBJECT, + xfx=pdf.xfxQ, + alphas=pdf.alphasQ, + ) + + # Using default minimum + res = convolve_grid() + assert res == 0.0 + # lower minimum to q2=1 + res = convolve_grid(q2_min=1.0) + assert res == 0.0 + + +class TestSubgrid: + def fake_grid(self): + channels = [Channel([([2], 1.0)]), Channel([([3], 0.5)])] + orders = [Order(0, 0, 0, 0, 0)] + return fake_dis_grid(orders, channels) + + def fake_importonlysubgrid(self) -> tuple: + x1s = np.linspace(0.1, 1, 2) + x2s = np.linspace(0.5, 1, 2) + Q2s = np.linspace(10, 20, 2) + scale = [q2 for q2 in Q2s] + array = np.random.rand(len(Q2s), len(x1s), len(x2s)) + subgrid = ImportSubgridV1( + array=array, + scales=scale, + x1_grid=x1s, + x2_grid=x2s, + ) + return subgrid, [x1s, x2s, scale, array] + + def test_subgrid_methods(self): + # TODO: extract the values of the scales and x grids + grid = self.fake_grid() + test_subgrid, infos = self.fake_importonlysubgrid() + x1s, x2s, mu2s, _ = (obj for obj in infos) + grid.set_subgrid(0, 0, 0, test_subgrid.into()) + extr_subgrid = grid.subgrid(0, 0, 0) + assert isinstance(extr_subgrid, SubgridEnum) + + @pytest.mark.skip(reason="No implementation of Array3 for subgrid.") + def test_to_array3(self): + # TODO: extract and check the dense array of the subgrid + # requires `impl From<&SubgridEnum> for Array3` + grid = self.fake_grid() + test_subgrid, infos = self.fake_importonlysubgrid() + _, _, _, array = (obj for obj in infos) + grid.set_subgrid(0, 0, 0, test_subgrid.into()) + extr_subgrid = grid.subgrid(0, 0, 0) + test_array = extr_subgrid.to_array3() + print(test_array) + print(array) + np.testing.assert_allclose(test_array, array) From c91c47584ff0a9c66ad5e8a00970d28cfe8303ba Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 17:03:45 +0200 Subject: [PATCH 194/213] Fix object is not iterable in all `bins` argument --- pineappl_py/tests/test_evolution.py | 8 +++----- pineappl_py/tests/test_fk_table.py | 6 ++---- pineappl_py/tests/test_grid.py | 6 ++---- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py index 4a296280..cf25086e 100644 --- a/pineappl_py/tests/test_evolution.py +++ b/pineappl_py/tests/test_evolution.py @@ -15,13 +15,13 @@ from pineappl.pids import PidBasis -class TestFkTable: +class TestEvolution: def fake_grid( self, channels: list[Channel], orders: list[Order], convolutions: list[Conv], - bins=None, + bins: list[float] = [1e-7, 1e-3, 1], ) -> Grid: kinematics = [ Kinematics(0), # Scale @@ -49,9 +49,7 @@ def fake_grid( order=3, ), # Interpolation on x2 momentum fraction ] - bin_limits = np.array( - [1e-7, 1e-3, 1] if bins is None else bins, dtype=float - ) + bin_limits = np.array(bins) return Grid( pid_basis=PidBasis.Evol, channels=channels, diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index 1f9781df..4be4e278 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -20,7 +20,7 @@ def fake_grid( channels: list[Channel], orders: list[Order], convolutions: list[Conv], - bins=None, + bins: list[float] = [1e-7, 1e-3, 1], ) -> Grid: kinematics = [ Kinematics(0), # Scale @@ -48,9 +48,7 @@ def fake_grid( order=3, ), # Interpolation on x2 momentum fraction ] - bin_limits = np.array( - [1e-7, 1e-3, 1] if bins is None else bins, dtype=float - ) + bin_limits = np.array(bins) return Grid( pid_basis=PidBasis.Evol, channels=channels, diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index ee42b758..f56cbfe3 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -24,7 +24,7 @@ def fake_grid( self, channels: list[Channel] = CHANNELS, orders: list[Order] = ORDERS, - bins: list | None = None, + bins: list[float] = [1e-7, 1e-3, 1], ) -> Grid: # We assume symmetrical proton-proton in the initial state convolutions = [CONVOBJECT, CONVOBJECT] @@ -58,9 +58,7 @@ def fake_grid( order=3, ), # Interpolation on x2 momentum fraction ] - bin_limits = np.array( - [1e-7, 1e-3, 1] if bins is None else bins, dtype=float - ) + bin_limits = np.array(bins) return Grid( pid_basis=PidBasis.Evol, channels=channels, From cbc74ae377f0db93aaa073fdc249eb3633f62732 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 17:19:41 +0200 Subject: [PATCH 195/213] Rely on `typing` for type annotations (needed for old Python versions) --- pineappl_py/tests/test_evolution.py | 9 +++++---- pineappl_py/tests/test_fk_table.py | 9 +++++---- pineappl_py/tests/test_grid.py | 8 +++++--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py index cf25086e..f5627ad3 100644 --- a/pineappl_py/tests/test_evolution.py +++ b/pineappl_py/tests/test_evolution.py @@ -13,15 +13,16 @@ from pineappl.grid import Grid, Order from pineappl.interpolation import Interp from pineappl.pids import PidBasis +from typing import List class TestEvolution: def fake_grid( self, - channels: list[Channel], - orders: list[Order], - convolutions: list[Conv], - bins: list[float] = [1e-7, 1e-3, 1], + channels: List[Channel], + orders: List[Order], + convolutions: List[Conv], + bins: List[float] = [1e-7, 1e-3, 1], ) -> Grid: kinematics = [ Kinematics(0), # Scale diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index 4be4e278..cec07f7e 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -12,15 +12,16 @@ from pineappl.import_subgrid import ImportSubgridV1 from pineappl.interpolation import Interp from pineappl.pids import PidBasis +from typing import List class TestFkTable: def fake_grid( self, - channels: list[Channel], - orders: list[Order], - convolutions: list[Conv], - bins: list[float] = [1e-7, 1e-3, 1], + channels: List[Channel], + orders: List[Order], + convolutions: List[Conv], + bins: List[float] = [1e-7, 1e-3, 1], ) -> Grid: kinematics = [ Kinematics(0), # Scale diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index f56cbfe3..49a4a26f 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -1,5 +1,6 @@ import numpy as np import pytest + from pineappl.bin import BinRemapper from pineappl.boc import Channel, Kinematics from pineappl.convolutions import Conv, ConvType @@ -7,6 +8,7 @@ from pineappl.import_subgrid import ImportSubgridV1 from pineappl.interpolation import Interp from pineappl.pids import PidBasis +from typing import List # Construct the type of convolutions and the convolution object # We assume unpolarized proton PDF @@ -22,9 +24,9 @@ class TestGrid: def fake_grid( self, - channels: list[Channel] = CHANNELS, - orders: list[Order] = ORDERS, - bins: list[float] = [1e-7, 1e-3, 1], + channels: List[Channel] = CHANNELS, + orders: List[Order] = ORDERS, + bins: List[float] = [1e-7, 1e-3, 1], ) -> Grid: # We assume symmetrical proton-proton in the initial state convolutions = [CONVOBJECT, CONVOBJECT] From 48be5a46715a95fb68de061f7e2b03fca3bd8a35 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 18 Oct 2024 18:36:04 +0200 Subject: [PATCH 196/213] Use `into` instead of `try_into` when possible --- pineappl/src/evolution.rs | 2 +- pineappl/src/grid.rs | 22 +++++++++++----------- pineappl_cli/src/export/applgrid.rs | 12 +++--------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 35ef7a85..25615a1d 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -343,7 +343,7 @@ fn ndarray_from_subgrid_orders_slice_many( approx_eq!(f64, ren1, mur2, ulps = EVOLUTION_TOL_ULPS).then(|| alphas) }) { - alphas.powi(order.alphas.try_into().unwrap()) + alphas.powi(order.alphas.into()) } else { return Err(GridError::EvolutionFailure(format!( "no alphas for mur2 = {mur2} found" diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 88648de6..5487befd 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -302,15 +302,15 @@ impl Grid { } if order.logxir > 0 { - value *= (xir * xir).ln().powi(order.logxir.try_into().unwrap()); + value *= (xir * xir).ln().powi(order.logxir.into()); } if order.logxif > 0 { - value *= (xif * xif).ln().powi(order.logxif.try_into().unwrap()); + value *= (xif * xif).ln().powi(order.logxif.into()); } if order.logxia > 0 { - value *= (xia * xia).ln().powi(order.logxia.try_into().unwrap()); + value *= (xia * xia).ln().powi(order.logxia.into()); } bins[xi_index + xi.len() * bin_index] += value / normalizations[bin]; @@ -369,15 +369,15 @@ impl Grid { } if order.logxir > 0 { - array *= (xir * xir).ln().powi(order.logxir.try_into().unwrap()); + array *= (xir * xir).ln().powi(order.logxir.into()); } if order.logxif > 0 { - array *= (xif * xif).ln().powi(order.logxif.try_into().unwrap()); + array *= (xif * xif).ln().powi(order.logxif.into()); } if order.logxia > 0 { - array *= (xia * xia).ln().powi(order.logxia.try_into().unwrap()); + array *= (xia * xia).ln().powi(order.logxia.into()); } array /= normalizations[bin]; @@ -742,11 +742,11 @@ impl Grid { for ((i, _, _), subgrid) in self.subgrids.indexed_iter_mut() { let order = &self.orders[i]; let factor = global - * alphas.powi(order.alphas.try_into().unwrap()) - * alpha.powi(order.alpha.try_into().unwrap()) - * logxir.powi(order.logxir.try_into().unwrap()) - * logxif.powi(order.logxif.try_into().unwrap()) - * logxia.powi(order.logxia.try_into().unwrap()); + * alphas.powi(order.alphas.into()) + * alpha.powi(order.alpha.into()) + * logxir.powi(order.logxir.into()) + * logxif.powi(order.logxif.into()) + * logxia.powi(order.logxia.into()); subgrid.scale(factor); } diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 1fb2651c..281e8457 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -182,14 +182,8 @@ pub fn convert_into_applgrid( .unwrap() - lo_alphas; - let mut applgrid = ffi::make_empty_grid( - &limits, - id, - lo_alphas.try_into().unwrap(), - loops.try_into().unwrap(), - "f2", - "h0", - ); + let mut applgrid = + ffi::make_empty_grid(&limits, id, lo_alphas.into(), loops.into(), "f2", "h0"); for (appl_order, order) in order_mask .iter() @@ -197,7 +191,7 @@ pub fn convert_into_applgrid( .filter_map(|(index, keep)| keep.then_some(index)) .enumerate() { - let factor = TAU.powi(grid.orders()[order].alphas.try_into().unwrap()); + let factor = TAU.powi(grid.orders()[order].alphas.into()); for (bin, subgrids) in grid .subgrids() From f62412ef8778b2f900867ad42ecff1ee5892da99 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 18 Oct 2024 18:43:44 +0200 Subject: [PATCH 197/213] Fix numerical noise --- pineappl_cli/tests/import.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index 650d1712..f6b3b93e 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -307,11 +307,13 @@ const IMPORT_DOUBLE_HADRONIC_FASTNLO_STR: &str = "; #[cfg(feature = "fastnlo")] -const IMPORT_NPDFDIM_2_TABLE_STR: &str = "0 1.0824021e0 1.0824021e0 1.4654944e-14 +const IMPORT_NPDFDIM_2_TABLE_STR: &str = "b PineAPPL fastNLO rel. diff +-+------------+------------+-------------- +0 1.0824021e0 1.0824021e0 1.4654944e-14 1 1.0680553e0 1.0680553e0 -1.4432899e-15 2 6.4959982e-1 6.4959982e-1 4.4408921e-15 3 3.3033872e-1 3.3033872e-1 2.0872193e-14 -4 1.3360159e-1 1.3360159e-1 -2.3092639e-14 +4 1.3360159e-1 1.3360159e-1 -2.2870594e-14 5 3.2728146e-2 3.2728146e-2 -5.7731597e-15 6 3.8508907e-3 3.8508907e-3 2.2870594e-14 "; From a884201c5e4bfa09d44f7fdfd15b886801db4261 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Fri, 18 Oct 2024 18:48:56 +0200 Subject: [PATCH 198/213] Get rid of a few `unwrap` calls --- pineappl/src/convolutions.rs | 12 +++++------- pineappl/src/grid.rs | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 1cb12688..9294bff5 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -51,7 +51,7 @@ impl<'a> ConvolutionCache<'a> { } } - pub(crate) fn setup(&mut self, grid: &Grid, xi: &[(f64, f64, f64)]) -> Result<(), ()> { + pub(crate) fn setup(&mut self, grid: &Grid, xi: &[(f64, f64, f64)]) { self.perm = grid .convolutions() .iter() @@ -91,7 +91,7 @@ impl<'a> ConvolutionCache<'a> { .flat_map(|(_, node_values)| node_values) }) .collect(); - x_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); + x_grid.sort_by(f64::total_cmp); x_grid.dedup(); let mut mur2_grid: Vec<_> = grid @@ -106,7 +106,7 @@ impl<'a> ConvolutionCache<'a> { }) .flat_map(|ren| xi.iter().map(move |(xir, _, _)| xir * xir * ren)) .collect(); - mur2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); + mur2_grid.sort_by(f64::total_cmp); mur2_grid.dedup(); let mut muf2_grid: Vec<_> = grid @@ -121,7 +121,7 @@ impl<'a> ConvolutionCache<'a> { }) .flat_map(|fac| xi.iter().map(move |(_, xif, _)| xif * xif * fac)) .collect(); - muf2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); + muf2_grid.sort_by(f64::total_cmp); muf2_grid.dedup(); let mut mua2_grid: Vec<_> = grid @@ -136,7 +136,7 @@ impl<'a> ConvolutionCache<'a> { }) .flat_map(|frg| xi.iter().map(move |(_, _, xia)| xia * xia * frg)) .collect(); - mua2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap_or_else(|| unreachable!())); + mua2_grid.sort_by(f64::total_cmp); mua2_grid.dedup(); self.alphas_cache = mur2_grid.iter().map(|&mur2| (self.alphas)(mur2)).collect(); @@ -144,8 +144,6 @@ impl<'a> ConvolutionCache<'a> { self.muf2_grid = muf2_grid; self.mua2_grid = mua2_grid; self.x_grid = x_grid; - - Ok(()) } /// TODO diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 5487befd..8201671d 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -235,7 +235,7 @@ impl Grid { channel_mask: &[bool], xi: &[(f64, f64, f64)], ) -> Vec { - convolution_cache.setup(self, xi).unwrap(); + convolution_cache.setup(self, xi); let bin_indices = if bin_indices.is_empty() { (0..self.bin_info().bins()).collect() @@ -336,7 +336,7 @@ impl Grid { channel: usize, (xir, xif, xia): (f64, f64, f64), ) -> ArrayD { - convolution_cache.setup(self, &[(xir, xif, xia)]).unwrap(); + convolution_cache.setup(self, &[(xir, xif, xia)]); let normalizations = self.bin_info().normalizations(); let pdg_channels = self.channels_pdg(); From f1574a32370568f100bbb952745173f49b7b3d05 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Fri, 18 Oct 2024 22:58:12 +0200 Subject: [PATCH 199/213] Make Generalized convolution works --- pineappl_py/src/grid.rs | 31 ++++++++++++++++++++++++++++--- pineappl_py/tests/test_grid.py | 8 ++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 0e2aaa6c..f63d5eaf 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -314,13 +314,38 @@ impl PyGrid { xi: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { - // Closure for alphas function let mut alphas = |q2: f64| { let result: f64 = alphas.call1(py, (q2,)).unwrap().extract(py).unwrap(); result }; - todo!() + let mut xfx_funcs: Vec<_> = xfxs + .iter() + .map(|xfx| { + move |id: i32, x: f64, q2: f64| { + xfx.call1(py, (id, x, q2)).unwrap().extract(py).unwrap() + } + }) + .collect(); + + let mut lumi_cache = ConvolutionCache::new( + pdg_convs.into_iter().map(|pdg| pdg.conv.clone()).collect(), + xfx_funcs + .iter_mut() + .map(|fx| fx as &mut dyn FnMut(i32, f64, f64) -> f64) + .collect(), + &mut alphas, + ); + + self.grid + .convolve( + &mut lumi_cache, + &order_mask.unwrap_or_default(), + &bin_indices.unwrap_or_default(), + &channel_mask.unwrap_or_default(), + &xi.unwrap_or_else(|| vec![(1.0, 1.0, 0.0)]), + ) + .into_pyarray_bound(py) } /// Collect information for convolution with an evolution operator. @@ -684,7 +709,7 @@ impl PyGrid { /// bin_indices : numpy.ndarray[int] /// list of indices of bins to removed pub fn delete_bins(&mut self, bin_indices: Vec) { - self.grid.delete_bins(&bin_indices) + self.grid.delete_bins(&bin_indices); } } diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index fc4ba013..875a2ea9 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -172,6 +172,14 @@ def test_convolve_with_two(self): ), [2**3 * v, 0.0], ) + np.testing.assert_allclose( + g.convolve( + pdg_convs=[CONVOBJECT, CONVOBJECT], + xfxs=[lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + alphas=lambda q2: 2.0, + ), + [2**3 * v, 0.0], + ) # Test using the generalized convolution np.testing.assert_allclose( g.convolve_with_two( pdg_conv1=CONVOBJECT, From 92212a60a0d26b73ede740c018eb8fee2e7528c1 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Sat, 19 Oct 2024 23:16:28 +0200 Subject: [PATCH 200/213] Implement generalized `convolve` and `evolve` (removes old `_with_`) --- pineappl_py/src/fk_table.rs | 87 ++++++++------------- pineappl_py/src/grid.rs | 125 ++----------------------------- pineappl_py/src/interpolation.rs | 32 +++++++- 3 files changed, 65 insertions(+), 179 deletions(-) diff --git a/pineappl_py/src/fk_table.rs b/pineappl_py/src/fk_table.rs index 349a5c65..e08dc5ec 100644 --- a/pineappl_py/src/fk_table.rs +++ b/pineappl_py/src/fk_table.rs @@ -230,85 +230,58 @@ impl PyFkTable { .unwrap(); } - /// Convolve with a single distribution. + /// Convolve the FK table with as many distributions. /// /// # Panics /// TODO /// /// Parameters /// ---------- - /// pdg_id : integer - /// PDG Monte Carlo ID of the hadronic particle - /// xfx : callable - /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid + /// pdg_convs : list(PyConv) + /// list containing the types of convolutions and PID + /// xfxs : list(callable) + /// list of lhapdf-like callable with arguments `pid, x, Q2` returning x*pdf + /// bin_indices : numpy.ndarray(int) + /// A list with the indices of the corresponding bins that should be calculated. An + /// empty list means that all bins should be calculated. + /// channel_mask : numpy.ndarray(bool) + /// Mask for selecting specific channels. The value `True` means the + /// corresponding channel is included. An empty list corresponds to all channels being + /// enabled. /// /// Returns /// ------- /// numpy.ndarray(float) : /// cross sections for all bins #[must_use] - #[pyo3(signature = (pdg_conv, xfx, bin_indices = None, channel_mask= None))] - pub fn convolve_with_one<'py>( + #[pyo3(signature = (pdg_convs, xfxs, bin_indices = None, channel_mask= None))] + pub fn convolve<'py>( &self, - pdg_conv: PyRef, - xfx: &Bound<'py, PyAny>, + pdg_convs: Vec>, + xfxs: Vec, bin_indices: Option>, channel_mask: Option>, py: Python<'py>, ) -> Bound<'py, PyArray1> { - let mut xfx = |id, x, q2| xfx.call1((id, x, q2)).unwrap().extract().unwrap(); - let mut alphas = |_| 1.0; - let mut lumi_cache = - ConvolutionCache::new(vec![pdg_conv.conv.clone()], vec![&mut xfx], &mut alphas); - self.fk_table - .convolve( - &mut lumi_cache, - &bin_indices.unwrap_or_default(), - &channel_mask.unwrap_or_default(), - ) - .into_pyarray_bound(py) - } + let mut xfx_funcs: Vec<_> = xfxs + .iter() + .map(|xfx| { + move |id: i32, x: f64, q2: f64| { + xfx.call1(py, (id, x, q2)).unwrap().extract(py).unwrap() + } + }) + .collect(); - /// Convoluve grid with two different distribution. - /// - /// # Panics - /// TODO - /// - /// Parameters - /// ---------- - /// pdg_id1 : integer - /// PDG Monte Carlo ID of the first hadronic particle - /// xfx1 : callable - /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid - /// pdg_id2 : integer - /// PDG Monte Carlo ID of the second hadronic particle - /// xfx2 : callable - /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid - /// - /// Returns - /// ------- - /// numpy.ndarray(float) : - /// cross sections for all bins - #[must_use] - #[pyo3(signature = (pdg_conv1, xfx1, pdg_conv2, xfx2, bin_indices = None, channel_mask= None))] - pub fn convolve_with_two<'py>( - &self, - pdg_conv1: PyRef, - xfx1: &Bound<'py, PyAny>, - pdg_conv2: PyRef, - xfx2: &Bound<'py, PyAny>, - bin_indices: Option>, - channel_mask: Option>, - py: Python<'py>, - ) -> Bound<'py, PyArray1> { - let mut xfx1 = |id, x, q2| xfx1.call1((id, x, q2)).unwrap().extract().unwrap(); - let mut xfx2 = |id, x, q2| xfx2.call1((id, x, q2)).unwrap().extract().unwrap(); let mut alphas = |_| 1.0; let mut lumi_cache = ConvolutionCache::new( - vec![pdg_conv1.conv.clone(), pdg_conv2.conv.clone()], - vec![&mut xfx1, &mut xfx2], + pdg_convs.into_iter().map(|pdg| pdg.conv.clone()).collect(), + xfx_funcs + .iter_mut() + .map(|fx| fx as &mut dyn FnMut(i32, f64, f64) -> f64) + .collect(), &mut alphas, ); + self.fk_table .convolve( &mut lumi_cache, diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index f63d5eaf..5a1d6488 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -157,18 +157,17 @@ impl PyGrid { self.grid.set_remapper(remapper.bin_remapper).unwrap(); } - /// Convolve with a single distribution. + /// Convolve the grid with as many distributions. /// /// # Panics - /// /// TODO /// /// Parameters /// ---------- - /// pdg_conv : PyConv - /// contains the types of convolutions and PID - /// xfx : callable - /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid + /// pdg_convs : list(PyConv) + /// list containing the types of convolutions and PID + /// xfxs : list(callable) + /// list of lhapdf-like callable with arguments `pid, x, Q2` returning x*pdf /// alphas : callable /// lhapdf like callable with arguments `Q2` returning :math:`\alpha_s` /// order_mask : numpy.ndarray(bool) @@ -194,115 +193,7 @@ impl PyGrid { /// cross sections for all bins, for each scale-variation tuple (first all bins, then /// the scale variation) #[must_use] - #[pyo3(signature = (pdg_conv, xfx, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] - pub fn convolve_with_one<'py>( - &self, - pdg_conv: PyRef, - xfx: &Bound<'py, PyAny>, - alphas: &Bound<'py, PyAny>, - order_mask: Option>, - bin_indices: Option>, - channel_mask: Option>, - xi: Option>, - py: Python<'py>, - ) -> Bound<'py, PyArray1> { - let mut xfx = |id, x, q2| xfx.call1((id, x, q2)).unwrap().extract().unwrap(); - // `(q2, )` must have the comma to make it a Rust tuple - let mut alphas = |q2| alphas.call1((q2,)).unwrap().extract().unwrap(); - let mut lumi_cache = - ConvolutionCache::new(vec![pdg_conv.conv.clone()], vec![&mut xfx], &mut alphas); - self.grid - .convolve( - &mut lumi_cache, - &order_mask.unwrap_or_default(), - &bin_indices.unwrap_or_default(), - &channel_mask.unwrap_or_default(), - &xi.unwrap_or_else(|| vec![(1.0, 1.0, 0.0)]), - ) - .into_pyarray_bound(py) - } - - /// Convolve with two distributions. - /// - /// # Panics - /// - /// TODO - /// - /// Parameters - /// ---------- - /// pdg_conv1 : PyConv - /// contains the types of convolutions and PID - /// xfx1 : callable - /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid - /// pdg_conv2 : PyConv - /// contains the types of convolutions and PID - /// xfx2 : callable - /// lhapdf like callable with arguments `pid, x, Q2` returning x*pdf for :math:`x`-grid - /// alphas : callable - /// lhapdf like callable with arguments `Q2` returning :math:`\alpha_s` - /// order_mask : numpy.ndarray(bool) - /// Mask for selecting specific orders. The value `True` means the corresponding order - /// is included. An empty list corresponds to all orders being enabled. - /// bin_indices : numpy.ndarray(int) - /// A list with the indices of the corresponding bins that should be calculated. An - /// empty list means that all bins should be calculated. - /// channel_mask : numpy.ndarray(bool) - /// Mask for selecting specific channels. The value `True` means the - /// corresponding channel is included. An empty list corresponds to all channels being - /// enabled. - /// xi : list((float, float)) - /// A list with the scale variation factors that should be used to calculate - /// scale-varied results. The first entry of a tuple corresponds to the variation of - /// the renormalization scale, the second entry to the variation of the factorization - /// scale. If only results for the central scale are need the list should contain - /// `(1.0, 1.0)`. - /// - /// Returns - /// ------- - /// numpy.ndarray(float) : - /// cross sections for all bins, for each scale-variation tuple (first all bins, then - /// the scale variation) - #[must_use] - #[pyo3(signature = (pdg_conv1, xfx1, pdg_conv2, xfx2, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] - pub fn convolve_with_two<'py>( - &self, - pdg_conv1: PyRef, - xfx1: &Bound<'py, PyAny>, - pdg_conv2: PyRef, - xfx2: &Bound<'py, PyAny>, - alphas: &Bound<'py, PyAny>, - order_mask: Option>, - bin_indices: Option>, - channel_mask: Option>, - xi: Option>, - py: Python<'py>, - ) -> Bound<'py, PyArray1> { - let mut xfx1 = |id, x, q2| xfx1.call1((id, x, q2)).unwrap().extract().unwrap(); - let mut xfx2 = |id, x, q2| xfx2.call1((id, x, q2)).unwrap().extract().unwrap(); - // `(q2, )` must have the comma to make it a Rust tuple - let mut alphas = |q2| alphas.call1((q2,)).unwrap().extract().unwrap(); - let mut lumi_cache = ConvolutionCache::new( - vec![pdg_conv1.conv.clone(), pdg_conv2.conv.clone()], - vec![&mut xfx1, &mut xfx2], - &mut alphas, - ); - self.grid - .convolve( - &mut lumi_cache, - &order_mask.unwrap_or_default(), - &bin_indices.unwrap_or_default(), - &channel_mask.unwrap_or_default(), - &xi.unwrap_or_else(|| vec![(1.0, 1.0, 0.0)]), - ) - .into_pyarray_bound(py) - } - - /// Convolve a grid with as many convolutions. - /// - /// # Panics - /// TODO - // #[pyo3(signature = (pdg_convs, xfxs, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] - #[must_use] + #[pyo3(signature = (pdg_convs, xfxs, alphas, order_mask = None, bin_indices = None, channel_mask = None, xi = None))] pub fn convolve<'py>( &self, pdg_convs: Vec>, @@ -370,7 +261,7 @@ impl PyGrid { } } - /// Evolve grid with as many EKOs as Convolutions. + /// Evolve the grid with as many EKOs as Convolutions. /// /// TODO: Expose `slices` to be a vector!!! /// @@ -397,7 +288,6 @@ impl PyGrid { /// ------- /// PyFkTable : /// produced FK table - #[allow(clippy::needless_lifetimes)] pub fn evolve<'py>( &self, slices: &Bound<'py, PyIterator>, @@ -454,7 +344,6 @@ impl PyGrid { /// ------- /// PyFkTable : /// produced FK table - #[allow(clippy::needless_lifetimes)] pub fn evolve_with_slice_iter<'py>( &self, slices: &Bound<'py, PyIterator>, diff --git a/pineappl_py/src/interpolation.rs b/pineappl_py/src/interpolation.rs index fc1fd93a..99bd7bc9 100644 --- a/pineappl_py/src/interpolation.rs +++ b/pineappl_py/src/interpolation.rs @@ -1,7 +1,7 @@ //! Interpolation interface. use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; -use pyo3::{prelude::*, pyclass}; +use pyo3::prelude::*; /// PyO3 wrapper to :rustdoc:`pineappl::interpolation::Interp `. #[pyclass(name = "Interp")] @@ -32,16 +32,40 @@ impl PyInterp { /// number of nodes /// order : int /// order of the interpolation + /// reweght_meth : Optional[str] + /// re-weighting method to be used + /// map : Optional[str] + /// the type of mapping to be used #[new] #[must_use] - pub fn new_interp(min: f64, max: f64, nodes: usize, order: usize) -> Self { + #[pyo3(signature = (min, max, nodes, order, reweight_meth = None, map = None))] + pub fn new_interp( + min: f64, + max: f64, + nodes: usize, + order: usize, + reweight_meth: Option<&str>, + map: Option<&str>, + ) -> Self { + let reweight = match reweight_meth.unwrap_or("applgrid") { + "applgrid" => ReweightMeth::ApplGridX, + "noreweight" => ReweightMeth::NoReweight, + _ => todo!(), + }; + + let mapping = match map.unwrap_or("applgrid_f2") { + "applgrid_f2" => Map::ApplGridF2, + "applgrid_h0" => Map::ApplGridH0, + _ => todo!(), + }; + Self::new(Interp::new( min, max, nodes, order, - ReweightMeth::ApplGridX, - Map::ApplGridF2, + reweight, + mapping, InterpMeth::Lagrange, )) } From b41d30482394914e1fd164aaebb3f1e623d83d9a Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Sun, 20 Oct 2024 01:10:29 +0200 Subject: [PATCH 201/213] Revamp Python tests --- pineappl_py/tests/test_fk_table.py | 127 +++++++++++++++++--- pineappl_py/tests/test_grid.py | 182 ++++++++++++++--------------- pineappl_py/tests/test_subgrid.py | 6 +- 3 files changed, 203 insertions(+), 112 deletions(-) diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index cec07f7e..f32f4340 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -5,6 +5,8 @@ """ import numpy as np +import pytest +import subprocess from pineappl.boc import Channel, Kinematics from pineappl.convolutions import Conv, ConvType from pineappl.fk_table import FkTable @@ -15,6 +17,34 @@ from typing import List +@pytest.fixture +def download_fktable(tmp_path_factory): + def _download_fk(fkname: str) -> None: + download_dir = tmp_path_factory.mktemp("data") + file_path = download_dir / f"{fkname}" + args = [ + "wget", + "--no-verbose", + "--no-clobber", + "-P", + f"{download_dir}", + f"https://data.nnpdf.science/pineappl/test-data/{fkname}", + ] + + try: + _ = subprocess.run( + args, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + return file_path + except OSError as error: + msg = f"Failed to execute the command {args}." + raise EnvironmentError(msg) from error + + return _download_fk + + class TestFkTable: def fake_grid( self, @@ -35,18 +65,24 @@ def fake_grid( max=1e8, nodes=40, order=3, + reweight_meth="noreweight", + map="applgrid_h0", ), # Interpolation on the Scale Interp( min=2e-7, max=1.0, nodes=50, order=3, + reweight_meth="applgrid", + map="applgrid_f2", ), # Interpolation on x1 momentum fraction Interp( min=2e-7, max=1.0, nodes=50, order=3, + reweight_meth="applgrid", + map="applgrid_f2", ), # Interpolation on x2 momentum fraction ] bin_limits = np.array(bins) @@ -60,7 +96,7 @@ def fake_grid( kinematics=kinematics, ) - def test_convolve_with_one(self): + def test_convolve(self): # Define convolution types and the initial state hadrons # We consider an initial state Polarized Proton h = ConvType(polarized=True, time_like=False) @@ -87,24 +123,89 @@ def test_convolve_with_one(self): g.set_subgrid(0, 0, 0, subgrid.into()) fk = FkTable(g) # Convert Grid -> FkTable np.testing.assert_allclose( - fk.convolve_with_one( - pdg_conv=h_conv, - xfx=lambda pid, x, q2: 0.0, + fk.convolve( + pdg_convs=[h_conv], + xfxs=[lambda pid, x, q2: 0.0], ), [0.0] * 2, ) np.testing.assert_allclose( - fk.convolve_with_one( - pdg_conv=h_conv, - xfx=lambda pid, x, q2: 1.0, + fk.convolve( + pdg_convs=[h_conv], + xfxs=[lambda pid, x, q2: 1.0], ), [5e7 / 9999, 0.0], ) - def test_convolve_with_two(self): - # TODO - pass + def test_unpolarized_convolution( + self, + download_fktable, + fkname: str = "CMSTTBARTOT8TEV-TOPDIFF8TEVTOT.pineappl.lz4", + ): + """Check the convolution of an actual FK table that involves two + symmetrical unpolarized protons: + """ + expected_results = [3.72524538e04] + fk_table = download_fktable(f"{fkname}") + fk = FkTable.read(fk_table) + + # Convolution object of the 1st hadron - Polarized + h = ConvType(polarized=False, time_like=False) + h_conv = Conv(conv_type=h, pid=2212) + + # Define the Toy Unpolarized PDF set + def _unpolarized_pdf(pid, x, q2): + return 1.0 - def test_convolve_with_many(self): - # TODO - pass + np.testing.assert_allclose( + fk.convolve( + pdg_convs=[h_conv, h_conv], + xfxs=[_unpolarized_pdf, _unpolarized_pdf], + ), + expected_results, + ) + + def test_polarized_convolution( + self, + download_fktable, + fkname: str = "GRID_STAR_WMWP_510GEV_WM-AL-POL.pineappl.lz4", + ): + """Check the convolution of an actual FK table that involves two + different initial states: + - 1st hadron: polarized proton + - 2nd hadron: unpolarized proton + """ + expected_results = [ + -1.00885071e6, + -2.40862657e5, + -1.66407218e5, + -2.96098362e5, + -5.67594297e5, + +6.59245015e4, + ] + fk_table = download_fktable(f"{fkname}") + fk = FkTable.read(fk_table) + + # Convolution object of the 1st hadron - Polarized + h1 = ConvType(polarized=True, time_like=False) + h1_conv = Conv(conv_type=h1, pid=2212) + + # Convolution object of the 2nd hadron - Unpolarized + h2 = ConvType(polarized=False, time_like=False) + h2_conv = Conv(conv_type=h2, pid=2212) + + # Define the Toy Polarized PDF set + def _polarized_pdf(pid, x, q2): + return 2.0 + + # Define the Toy Unpolarized PDF set + def _unpolarized_pdf(pid, x, q2): + return 1.0 + + np.testing.assert_allclose( + fk.convolve( + pdg_convs=[h1_conv, h2_conv], + xfxs=[_polarized_pdf, _unpolarized_pdf], + ), + expected_results, + ) diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index 875a2ea9..9160cb11 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -11,7 +11,7 @@ from typing import List # Construct the type of convolutions and the convolution object -# We assume unpolarized proton PDF +# We assume unpolarized protons in the initial state TYPECONV = ConvType(polarized=False, time_like=False) CONVOBJECT = Conv(conv_type=TYPECONV, pid=2212) @@ -20,6 +20,76 @@ CHANNELS = [Channel(UP_ANTIUP_CHANNEL)] ORDERS = [Order(3, 0, 0, 0, 0)] +# Testing specs for Convolution checks. Each element of the list is +# a tuple with two elements where the first element is a dictionary +# whose keys are the arguments of the `convolve` function and the +# second element is the expected results. +REF_VALUE = 5e6 / 9999 +TESTING_SPECS = [ + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 0.0, lambda pid, x, q2: 0.0], + "alphas": lambda q2: 0.0, + }, + [0.0] * 2, + ), # fixed alphas(Q2) == 0.0 + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + "alphas": lambda q2: 1.0, + }, + [REF_VALUE, 0.0], + ), # fixed alphas(Q2) == 1.0 + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + "alphas": lambda q2: 2.0, + }, + [2**3 * REF_VALUE, 0.0], + ), # fixed alphas(Q2) == 2.0 + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + "alphas": lambda q2: 1.0, + "bin_indices": [0], + }, + [REF_VALUE], + ), # block first Bin without argument + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + "alphas": lambda q2: 1.0, + "bin_indices": [0], + "order_mask": [False], + }, + [0.0], + ), # block first Bin with order_mask + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + "alphas": lambda q2: 1.0, + "bin_indices": [0], + "channel_mask": [False], + }, + [0.0], + ), # block first Bin with channel_mask + ( + { + "pdg_convs": [CONVOBJECT, CONVOBJECT], + "xfxs": [lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], + "alphas": lambda q2: 1.0, + "bin_indices": [1], + }, + [0.0], + ), # second Bin is empty +] + class TestGrid: def fake_grid( @@ -27,9 +97,8 @@ def fake_grid( channels: List[Channel] = CHANNELS, orders: List[Order] = ORDERS, bins: List[float] = [1e-7, 1e-3, 1], + convolutions: List[Conv] = [CONVOBJECT, CONVOBJECT], ) -> Grid: - # We assume symmetrical proton-proton in the initial state - convolutions = [CONVOBJECT, CONVOBJECT] # Define the kinematics. Kinematics are defined as a list of items. # 1st item: factorization and renormalization scale # 2nd item: parton momentum fraction of the 1st convolution @@ -126,7 +195,8 @@ def test_bins(self): np.testing.assert_allclose(g.bin_left(1), [2, 3]) np.testing.assert_allclose(g.bin_right(1), [3, 5]) - def test_convolve_with_two(self): + @pytest.mark.parametrize("params,expected", TESTING_SPECS) + def test_toy_convolution(self, params, expected): g = self.fake_grid() # Fill the subgrid-part of the GRID object @@ -141,93 +211,15 @@ def test_convolve_with_two(self): g.set_subgrid(0, 0, 0, subgrid.into()) # Check the convolutions of the GRID - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 0.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 0.0, - alphas=lambda q2: 0.0, - ), - [0.0] * 2, - ) - v = 5e6 / 9999 - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 1.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 1.0, - alphas=lambda q2: 1.0, - ), - [v, 0.0], - ) - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 1.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 1.0, - alphas=lambda q2: 2.0, - ), - [2**3 * v, 0.0], - ) - np.testing.assert_allclose( - g.convolve( - pdg_convs=[CONVOBJECT, CONVOBJECT], - xfxs=[lambda pid, x, q2: 1.0, lambda pid, x, q2: 1.0], - alphas=lambda q2: 2.0, - ), - [2**3 * v, 0.0], - ) # Test using the generalized convolution - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 1.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 1.0, - alphas=lambda q2: 1.0, - bin_indices=[0], - ), - [v], - ) - # block first bins with additional args - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 1.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 1.0, - alphas=lambda q2: 1.0, - bin_indices=[0], - order_mask=[False], - ), - [0.0], - ) - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 1.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 1.0, - alphas=lambda q2: 1.0, - bin_indices=[0], - channel_mask=[False], - ), - [0.0], - ) - # second bin is empty - np.testing.assert_allclose( - g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: 1.0, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: 1.0, - alphas=lambda q2: 1.0, - bin_indices=[1], - ), - [0.0], - ) + np.testing.assert_allclose(g.convolve(**params), expected) + + def test_unpolarized_convolution(self): + # TODO + pass + + def test_polarized_convolution(self): + # TODO + pass def test_io(self, tmp_path): g = self.fake_grid() @@ -250,11 +242,9 @@ def test_fill(self): weight=10, ) # Peform convolutions using Toy LHPDF & AlphasQ2 functions - res = g.convolve_with_two( - pdg_conv1=CONVOBJECT, - xfx1=lambda pid, x, q2: x, - pdg_conv2=CONVOBJECT, - xfx2=lambda pid, x, q2: x, + res = g.convolve( + pdg_convs=[CONVOBJECT, CONVOBJECT], + xfxs=[lambda pid, x, q2: x, lambda pid, x, q2: x], alphas=lambda q2: 1.0, ) pytest.approx(res) == 0.0 diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py index c560499a..5c2dd3b2 100644 --- a/pineappl_py/tests/test_subgrid.py +++ b/pineappl_py/tests/test_subgrid.py @@ -75,9 +75,9 @@ def convolve_grid(q2_min: float = Q2_MIN) -> Grid: ntuple=[0.2, 0.2, 10], weight=0.5, ) - return grid.convolve_with_one( - pdg_conv=CONVOBJECT, - xfx=pdf.xfxQ, + return grid.convolve( + pdg_convs=[CONVOBJECT], + xfxs=[pdf.xfxQ], alphas=pdf.alphasQ, ) From a46a9fbf81431d654a9db775464880adb6280996 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Mon, 21 Oct 2024 00:22:12 +0200 Subject: [PATCH 202/213] Improve Python tests and their coverage --- pineappl_py/src/evolution.rs | 14 ++++ pineappl_py/src/grid.rs | 56 -------------- pineappl_py/src/subgrid.rs | 10 +++ pineappl_py/tests/conftest.py | 43 ++++++++++- pineappl_py/tests/test_evolution.py | 14 +++- pineappl_py/tests/test_fk_table.py | 105 ++++++++++++------------- pineappl_py/tests/test_grid.py | 115 ++++++++++++++++++++++++++-- pineappl_py/tests/test_subgrid.py | 16 +++- 8 files changed, 254 insertions(+), 119 deletions(-) diff --git a/pineappl_py/src/evolution.rs b/pineappl_py/src/evolution.rs index e4c444e4..efa0fca0 100644 --- a/pineappl_py/src/evolution.rs +++ b/pineappl_py/src/evolution.rs @@ -72,6 +72,20 @@ pub struct PyEvolveInfo { #[pymethods] impl PyEvolveInfo { + /// Constructor. + #[new] + #[must_use] + pub const fn new(fac1: Vec, pids1: Vec, x1: Vec, ren1: Vec) -> Self { + Self { + evolve_info: EvolveInfo { + fac1, + pids1, + x1, + ren1, + }, + } + } + /// Squared factorization scales of the `Grid`. #[getter] fn fac1<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1> { diff --git a/pineappl_py/src/grid.rs b/pineappl_py/src/grid.rs index 5a1d6488..225836cf 100644 --- a/pineappl_py/src/grid.rs +++ b/pineappl_py/src/grid.rs @@ -319,62 +319,6 @@ impl PyGrid { .unwrap()) } - /// Evolve grid with one single EKO. - /// - /// # Panics - /// TODO - /// - /// # Errors - /// TODO - /// - /// Parameters - /// ---------- - /// slices : Iterable - /// list of (PyOperatorSliceInfo, 5D array) describing each convolution - /// order_mask : numpy.ndarray(bool) - /// boolean mask to activate orders - /// xi : (float, float) - /// factorization and renormalization variation - /// ren1 : numpy.ndarray(float) - /// list of renormalization scales - /// alphas : numpy.ndarray(float) - /// list with :math:`\alpha_s(Q2)` for the process scales - /// - /// Returns - /// ------- - /// PyFkTable : - /// produced FK table - pub fn evolve_with_slice_iter<'py>( - &self, - slices: &Bound<'py, PyIterator>, - order_mask: Vec, - xi: (f64, f64, f64), - ren1: Vec, - alphas: Vec, - ) -> PyResult { - Ok(self - .grid - .evolve( - vec![slices.into_iter().map(|slice| { - let (info, op) = slice - .unwrap() - .extract::<(PyOperatorSliceInfo, PyReadonlyArray4)>() - .unwrap(); - Ok::<_, std::io::Error>(( - info.info, - // TODO: avoid copying - CowArray::from(op.as_array().to_owned()), - )) - })], - &order_mask, - xi, - &AlphasTable { ren1, alphas }, - ) - .map(|fk_table| PyFkTable { fk_table }) - // TODO: avoid unwrap and convert `Result` into `PyResult` - .unwrap()) - } - /// Load from file. /// /// # Panics diff --git a/pineappl_py/src/subgrid.rs b/pineappl_py/src/subgrid.rs index f66aabba..c47e5796 100644 --- a/pineappl_py/src/subgrid.rs +++ b/pineappl_py/src/subgrid.rs @@ -50,6 +50,16 @@ impl PyMu2 { fn set_fac(&mut self, value: f64) { self.mu2.fac = value; } + + #[getter] + const fn frg(&self) -> f64 { + self.mu2.frg + } + + #[setter] + fn set_frg(&mut self, value: f64) { + self.mu2.frg = value; + } } /// PyO3 wrapper to :rustdoc:`pineappl::subgrid::SubgridEnum ` diff --git a/pineappl_py/tests/conftest.py b/pineappl_py/tests/conftest.py index 12887d41..3561254d 100644 --- a/pineappl_py/tests/conftest.py +++ b/pineappl_py/tests/conftest.py @@ -1,20 +1,57 @@ import pytest +import subprocess class PDF: - def xfxQ(self, pid, x, q): - return self.xfxQ2(pid, x, q**2) - def xfxQ2(self, pid, x, q2): if pid in range(-6, 6): return x * (1 - x) else: return 0.0 + def xfxQ(self, pid, x, q): + return self.xfxQ2(pid, x, q**2) + def alphasQ(self, q): return 1.0 + # Define the Toy Polarized PDF set + def polarized_pdf(self, pid, x, q2): + return 2.0 + + # Define the Toy Unpolarized PDF set + def unpolarized_pdf(self, pid, x, q2): + return 1.0 + @pytest.fixture def pdf(): return PDF() + + +@pytest.fixture +def download_objects(tmp_path_factory): + def _download_fk(objname: str) -> None: + download_dir = tmp_path_factory.mktemp("data") + file_path = download_dir / f"{objname}" + args = [ + "wget", + "--no-verbose", + "--no-clobber", + "-P", + f"{download_dir}", + f"https://data.nnpdf.science/pineappl/test-data/{objname}", + ] + + try: + _ = subprocess.run( + args, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + return file_path + except OSError as error: + msg = f"Failed to execute the command {args}." + raise EnvironmentError(msg) from error + + return _download_fk diff --git a/pineappl_py/tests/test_evolution.py b/pineappl_py/tests/test_evolution.py index f5627ad3..bc25abec 100644 --- a/pineappl_py/tests/test_evolution.py +++ b/pineappl_py/tests/test_evolution.py @@ -9,7 +9,7 @@ import numpy as np from pineappl.boc import Channel, Kinematics from pineappl.convolutions import Conv, ConvType -from pineappl.evolution import OperatorSliceInfo +from pineappl.evolution import OperatorSliceInfo, EvolveInfo from pineappl.grid import Grid, Order from pineappl.interpolation import Interp from pineappl.pids import PidBasis @@ -61,6 +61,18 @@ def fake_grid( kinematics=kinematics, ) + def test_evolveinfo(self): + evinfo = EvolveInfo( + fac1=[0.5, 1.0, 2.0], + pids1=[-2, 0, 2], + x1=[1e-3, 0.5, 1], + ren1=[0.5, 1.0, 2.0], + ) + np.testing.assert_array_equal(evinfo.fac1, [0.5, 1.0, 2.0]) + np.testing.assert_array_equal(evinfo.pids1, [-2, 0, 2]) + np.testing.assert_array_equal(evinfo.x1, [1e-3, 0.5, 1.0]) + np.testing.assert_array_equal(evinfo.fac1, [0.5, 1.0, 2.0]) + def test_with_one_eko(self): # Define convolution types and the initial state hadrons # We consider an initial state Polarized Proton diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index f32f4340..fdde8d4f 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -5,11 +5,10 @@ """ import numpy as np -import pytest -import subprocess +import tempfile from pineappl.boc import Channel, Kinematics from pineappl.convolutions import Conv, ConvType -from pineappl.fk_table import FkTable +from pineappl.fk_table import FkTable, FkAssumptions from pineappl.grid import Grid, Order from pineappl.import_subgrid import ImportSubgridV1 from pineappl.interpolation import Interp @@ -17,34 +16,6 @@ from typing import List -@pytest.fixture -def download_fktable(tmp_path_factory): - def _download_fk(fkname: str) -> None: - download_dir = tmp_path_factory.mktemp("data") - file_path = download_dir / f"{fkname}" - args = [ - "wget", - "--no-verbose", - "--no-clobber", - "-P", - f"{download_dir}", - f"https://data.nnpdf.science/pineappl/test-data/{fkname}", - ] - - try: - _ = subprocess.run( - args, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - return file_path - except OSError as error: - msg = f"Failed to execute the command {args}." - raise EnvironmentError(msg) from error - - return _download_fk - - class TestFkTable: def fake_grid( self, @@ -121,7 +92,11 @@ def test_convolve(self): np.array([1.0]), ) g.set_subgrid(0, 0, 0, subgrid.into()) - fk = FkTable(g) # Convert Grid -> FkTable + + # Convert the Grid -> FkTable + fk = FkTable(g) + + # Test a simple convolution of the FK table np.testing.assert_allclose( fk.convolve( pdg_convs=[h_conv], @@ -137,38 +112,72 @@ def test_convolve(self): [5e7 / 9999, 0.0], ) + # Test writing/dumping the FK table into disk + with tempfile.TemporaryDirectory() as tmpdir: + fk.write(f"{tmpdir}/toy_fktable.pineappl") + fk.write_lz4(f"{tmpdir}/toy_fktable.pineappl.lz4") + + def test_fktable( + self, + download_objects, + fkname: str = "FKTABLE_CMSTTBARTOT8TEV-TOPDIFF8TEVTOT.pineappl.lz4", + ): + fk_table = download_objects(f"{fkname}") + fk = FkTable.read(fk_table) + + assert fk.table().shape == (1, 51, 34, 34) + np.testing.assert_allclose(fk.muf2(), 2.7224999999999997) + + # Check the various aspects of the Bins + assert fk.bins() == 1 + np.testing.assert_allclose(fk.bin_normalizations(), [1.0]) + np.testing.assert_allclose(fk.bin_right(dimension=0), [1.0]) + np.testing.assert_allclose(fk.bin_left(dimension=0), [0.0]) + + # Check the various aspects of the Channels + channels = fk.channels() + assert len(channels) == 51 + assert [21, 200] in channels + + # Check the contents of the x-grid + x_grid = fk.x_grid() + assert x_grid.size == 34 + np.testing.assert_allclose(x_grid[0], 1.57456056e-04) + + # Test FK optimization + assumption = FkAssumptions("Nf6Sym") + fk.optimize(assumption) + def test_unpolarized_convolution( self, - download_fktable, - fkname: str = "CMSTTBARTOT8TEV-TOPDIFF8TEVTOT.pineappl.lz4", + pdf, + download_objects, + fkname: str = "FKTABLE_CMSTTBARTOT8TEV-TOPDIFF8TEVTOT.pineappl.lz4", ): """Check the convolution of an actual FK table that involves two symmetrical unpolarized protons: """ expected_results = [3.72524538e04] - fk_table = download_fktable(f"{fkname}") + fk_table = download_objects(f"{fkname}") fk = FkTable.read(fk_table) # Convolution object of the 1st hadron - Polarized h = ConvType(polarized=False, time_like=False) h_conv = Conv(conv_type=h, pid=2212) - # Define the Toy Unpolarized PDF set - def _unpolarized_pdf(pid, x, q2): - return 1.0 - np.testing.assert_allclose( fk.convolve( pdg_convs=[h_conv, h_conv], - xfxs=[_unpolarized_pdf, _unpolarized_pdf], + xfxs=[pdf.unpolarized_pdf, pdf.unpolarized_pdf], ), expected_results, ) def test_polarized_convolution( self, - download_fktable, - fkname: str = "GRID_STAR_WMWP_510GEV_WM-AL-POL.pineappl.lz4", + pdf, + download_objects, + fkname: str = "FKTABLE_STAR_WMWP_510GEV_WM-AL-POL.pineappl.lz4", ): """Check the convolution of an actual FK table that involves two different initial states: @@ -183,7 +192,7 @@ def test_polarized_convolution( -5.67594297e5, +6.59245015e4, ] - fk_table = download_fktable(f"{fkname}") + fk_table = download_objects(f"{fkname}") fk = FkTable.read(fk_table) # Convolution object of the 1st hadron - Polarized @@ -194,18 +203,10 @@ def test_polarized_convolution( h2 = ConvType(polarized=False, time_like=False) h2_conv = Conv(conv_type=h2, pid=2212) - # Define the Toy Polarized PDF set - def _polarized_pdf(pid, x, q2): - return 2.0 - - # Define the Toy Unpolarized PDF set - def _unpolarized_pdf(pid, x, q2): - return 1.0 - np.testing.assert_allclose( fk.convolve( pdg_convs=[h1_conv, h2_conv], - xfxs=[_polarized_pdf, _unpolarized_pdf], + xfxs=[pdf.polarized_pdf, pdf.unpolarized_pdf], ), expected_results, ) diff --git a/pineappl_py/tests/test_grid.py b/pineappl_py/tests/test_grid.py index 9160cb11..10373eab 100644 --- a/pineappl_py/tests/test_grid.py +++ b/pineappl_py/tests/test_grid.py @@ -1,5 +1,6 @@ import numpy as np import pytest +import tempfile from pineappl.bin import BinRemapper from pineappl.boc import Channel, Kinematics @@ -151,6 +152,14 @@ def test_channels(self): assert len(g.channels()) == 1 assert g.channels()[0] == UP_ANTIUP_CHANNEL + def test_write(self): + g = self.fake_grid() + + # Test writing/dumping the FK table into disk + with tempfile.TemporaryDirectory() as tmpdir: + g.write(f"{tmpdir}/toy_grid.pineappl") + g.write_lz4(f"{tmpdir}/toy_grid.pineappl.lz4") + def test_set_subgrid(self): g = self.fake_grid() @@ -195,6 +204,24 @@ def test_bins(self): np.testing.assert_allclose(g.bin_left(1), [2, 3]) np.testing.assert_allclose(g.bin_right(1), [3, 5]) + def test_grid( + self, + download_objects, + gridname: str = "GRID_STAR_WMWP_510GEV_WP-AL-POL.pineappl.lz4", + ): + grid = download_objects(f"{gridname}") + g = Grid.read(grid) + + # Get the types of convolutions for this grid + for conv in g.convolutions(): + assert isinstance(conv, Conv) + + # Check that the scalings work, ie run without error + # TODO: implement method to check the actual values + g.scale(factor=10.0) + g.scale_by_bin(factors=[10.0, 20.0]) + g.delete_bins(bin_indices=[0, 1, 2]) + @pytest.mark.parametrize("params,expected", TESTING_SPECS) def test_toy_convolution(self, params, expected): g = self.fake_grid() @@ -213,13 +240,74 @@ def test_toy_convolution(self, params, expected): # Check the convolutions of the GRID np.testing.assert_allclose(g.convolve(**params), expected) - def test_unpolarized_convolution(self): - # TODO - pass + def test_unpolarized_convolution( + self, + pdf, + download_objects, + gridname: str = "GRID_DYE906R_D_bin_1.pineappl.lz4", + ): + """Tes convolution with an actual Grid. In the following example, + it is a DIS grid that involves a single unique hadron/proton. + """ + expected_results = [ + +3.71019208e4, + +3.71019208e4, + +2.13727492e4, + -1.83941398e3, + +3.22728612e3, + +5.45646897e4, + ] + + grid = download_objects(f"{gridname}") + g = Grid.read(grid) + + # Convolution object of the Unpolarized proton + h = ConvType(polarized=False, time_like=False) + h_conv = Conv(conv_type=h, pid=2212) - def test_polarized_convolution(self): - # TODO - pass + np.testing.assert_allclose( + g.convolve( + pdg_convs=[h_conv], # Requires ONE single convolutions + xfxs=[pdf.polarized_pdf], # Requires ONE single PDF + alphas=pdf.alphasQ, + ), + expected_results, + ) + + def test_polarized_convolution( + self, + pdf, + download_objects, + gridname: str = "GRID_STAR_WMWP_510GEV_WP-AL-POL.pineappl.lz4", + ): + expected_results = [ + +5.50006832e6, + +1.68117895e6, + +3.08224445e5, + -2.65602464e5, + -1.04664085e6, + -5.19002089e6, + ] + + grid = download_objects(f"{gridname}") + g = Grid.read(grid) + + # Convolution object of the 1st hadron - Polarized + h1 = ConvType(polarized=True, time_like=False) + h1_conv = Conv(conv_type=h1, pid=2212) + + # Convolution object of the 2nd hadron - Unpolarized + h2 = ConvType(polarized=False, time_like=False) + h2_conv = Conv(conv_type=h2, pid=2212) + + np.testing.assert_allclose( + g.convolve( + pdg_convs=[h1_conv, h2_conv], + xfxs=[pdf.polarized_pdf, pdf.unpolarized_pdf], + alphas=pdf.alphasQ, + ), + expected_results, + ) def test_io(self, tmp_path): g = self.fake_grid() @@ -276,3 +364,18 @@ def test_merge(self): with pytest.raises(ValueError, match="NonConsecutiveBins"): g2.merge(g5) + + def test_evolveinfo( + self, + download_objects, + gridname: str = "GRID_STAR_WMWP_510GEV_WP-AL-POL.pineappl.lz4", + ): + grid = download_objects(f"{gridname}") + g = Grid.read(grid) + g_evinfo = g.evolve_info(order_mask=[True, False, False, False]) + + np.testing.assert_allclose(g_evinfo.fac1, [6463.838404]) + np.testing.assert_allclose(g_evinfo.ren1, [6463.838404]) + np.testing.assert_allclose(g_evinfo.pids1, [-5, -3, -1, 2, 4]) + assert g_evinfo.x1.size == 23 + np.testing.assert_allclose(g_evinfo.x1[0], 0.01437507) diff --git a/pineappl_py/tests/test_subgrid.py b/pineappl_py/tests/test_subgrid.py index 5c2dd3b2..1b1700fe 100644 --- a/pineappl_py/tests/test_subgrid.py +++ b/pineappl_py/tests/test_subgrid.py @@ -6,7 +6,7 @@ from pineappl.import_subgrid import ImportSubgridV1 from pineappl.interpolation import Interp from pineappl.pids import PidBasis -from pineappl.subgrid import SubgridEnum +from pineappl.subgrid import SubgridEnum, Mu2 # Define some default for the minimum value of `Q2` Q2_MIN = 1e2 @@ -109,6 +109,20 @@ def fake_importonlysubgrid(self) -> tuple: ) return subgrid, [x1s, x2s, scale, array] + def test_mu2(self): + mu2_obj = Mu2(ren=1.0, fac=2.0, frg=0.0) + assert mu2_obj.ren == 1.0 + assert mu2_obj.fac == 2.0 + assert mu2_obj.frg == 0.0 + + # Overwrite the constructed values + mu2_obj.ren = 10.0 + mu2_obj.fac = 20.0 + mu2_obj.frg = 10.0 + assert mu2_obj.ren == 10.0 + assert mu2_obj.fac == 20.0 + assert mu2_obj.frg == 10.0 + def test_subgrid_methods(self): # TODO: extract the values of the scales and x grids grid = self.fake_grid() From a1fa114cb8d33e6dd700d58e7d7175400625c36d Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 08:51:02 +0200 Subject: [PATCH 203/213] Import DIS APPLgrids with correct dimensions --- pineappl_cli/src/import/applgrid.rs | 34 +++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 0424c414..3ed14833 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -130,9 +130,10 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u8) -> Result { assert_eq!(grid.getDynamicScale(), 0.0); let mut grids = Vec::with_capacity(orders.len()); + let dis = grid.isDIS(); // from APPLgrid alone we don't know what type of convolution we have - let convolutions = vec![Conv::new(ConvType::UnpolPDF, 2212); if grid.isDIS() { 1 } else { 2 }]; + let convolutions = vec![Conv::new(ConvType::UnpolPDF, 2212); if dis { 1 } else { 2 }]; // TODO: read out interpolation parameters from APPLgrid let mut interps = vec![Interp::new( 1e2, @@ -220,8 +221,11 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u8) -> Result { let matrix = unsafe { &*matrix }; - let mut array = - PackedArray::new(vec![mu2_values.len(), x1_values.len(), x2_values.len()]); + let mut array = PackedArray::new(if dis { + vec![mu2_values.len(), x1_values.len()] + } else { + vec![mu2_values.len(), x1_values.len(), x2_values.len()] + }); for itau in 0..mu2_values.len() { for ix1 in 0..x1_values.len() { @@ -234,7 +238,12 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u8) -> Result { ); if value != 0.0 { - array[[itau, ix1, ix2]] = value * x1_weights[ix1] * x2_weights[ix2]; + if dis { + array[[itau, ix1]] = value * x1_weights[ix1]; + } else { + array[[itau, ix1, ix2]] = + value * x1_weights[ix1] * x2_weights[ix2]; + } } } } @@ -244,11 +253,18 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u8) -> Result { pgrid.subgrids_mut()[[0, bin.try_into().unwrap(), lumi]] = ImportSubgridV1::new( array, - vec![ - mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), - x1_values.clone(), - x2_values.clone(), - ], + if dis { + vec![ + mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), + x1_values.clone(), + ] + } else { + vec![ + mu2_values.iter().map(|&Mu2 { ren, .. }| ren).collect(), + x1_values.clone(), + x2_values.clone(), + ] + }, ) .into(); } From 3a2372c2bfc8bf7fc0dc0a32fba110a152714f27 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 08:49:54 +0200 Subject: [PATCH 204/213] Refactor `ConvolutionCache` --- pineappl/src/boc.rs | 42 +++-- pineappl/src/convolutions.rs | 328 +++++++++++++++++------------------ pineappl/src/evolution.rs | 3 +- pineappl/src/grid.rs | 40 ++--- 4 files changed, 202 insertions(+), 211 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index 73f5e6ee..b4c6d31d 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -6,6 +6,7 @@ use float_cmp::approx_eq; use itertools::Itertools; use serde::{Deserialize, Serialize}; +use std::borrow::Cow; use std::cmp::Ordering; use std::str::FromStr; use thiserror::Error; @@ -41,20 +42,27 @@ pub enum ScaleFuncForm { impl ScaleFuncForm { /// TODO #[must_use] - pub fn calc(&self, node_values: &[Vec], kinematics: &[Kinematics]) -> Option> { - match self { - Self::NoScale => None, - &Self::Scale(index) => Some(if node_values.is_empty() { - // TODO: empty subgrid should have as many node values as dimensions - Vec::new() - } else { - node_values[kinematics - .iter() - .position(|&kin| kin == Kinematics::Scale(index)) - // UNWRAP: this should be guaranteed by `Grid::new` - .unwrap()] - .clone() - }), + pub fn calc<'a>( + &self, + node_values: &'a [Vec], + kinematics: &[Kinematics], + ) -> Cow<'a, [f64]> { + match self.clone() { + Self::NoScale => Cow::Borrowed(&[]), + Self::Scale(index) => { + if node_values.is_empty() { + // TODO: empty subgrid should have as many node values as dimensions + Cow::Borrowed(&[]) + } else { + Cow::Borrowed( + &node_values[kinematics + .iter() + .position(|&kin| kin == Kinematics::Scale(index)) + // UNWRAP: this should be guaranteed by `Grid::new` + .unwrap()], + ) + } + } Self::QuadraticSum(_, _) => todo!(), } } @@ -71,6 +79,12 @@ pub struct Scales { pub frg: ScaleFuncForm, } +impl<'a> From<&'a Scales> for [&'a ScaleFuncForm; 3] { + fn from(scales: &'a Scales) -> [&'a ScaleFuncForm; 3] { + [&scales.ren, &scales.fac, &scales.frg] + } +} + impl Scales { /// TODO pub fn compatible_with(&self, kinematics: &[Kinematics]) -> bool { diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 9294bff5..84db6a45 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -3,81 +3,98 @@ use super::boc::Kinematics; use super::grid::Grid; use super::pids; -use super::subgrid::Subgrid; +use super::subgrid::{Subgrid, SubgridEnum}; +use itertools::izip; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; +const REN_IDX: usize = 0; +const FAC_IDX: usize = 1; +const FRG_IDX: usize = 2; +const SCALES_CNT: usize = 3; + +struct ConvCache1d<'a> { + xfx: &'a mut dyn FnMut(i32, f64, f64) -> f64, + cache: FxHashMap<(i32, usize, usize), f64>, + conv: Conv, + scale: usize, +} + /// A cache for evaluating PDFs. Methods like [`Grid::convolve`] accept instances of this `struct` /// instead of the PDFs themselves. pub struct ConvolutionCache<'a> { - xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, - xfx_cache: Vec>, + caches: Vec>, alphas: &'a mut dyn FnMut(f64) -> f64, alphas_cache: Vec, - mur2_grid: Vec, - muf2_grid: Vec, - mua2_grid: Vec, + mu2: [Vec; SCALES_CNT], x_grid: Vec, - imur2: Vec, - imuf2: Vec, - imua2: Vec, - ix: Vec>, - pdg: Vec, - perm: Vec<(usize, bool)>, } impl<'a> ConvolutionCache<'a> { /// TODO pub fn new( - pdg: Vec, + convolutions: Vec, xfx: Vec<&'a mut dyn FnMut(i32, f64, f64) -> f64>, alphas: &'a mut dyn FnMut(f64) -> f64, ) -> Self { Self { - xfx_cache: vec![FxHashMap::default(); xfx.len()], - xfx, + caches: xfx + .into_iter() + .zip(convolutions) + .map(|(xfx, conv)| ConvCache1d { + xfx, + cache: FxHashMap::default(), + scale: match conv.conv_type() { + ConvType::UnpolPDF | ConvType::PolPDF => FAC_IDX, + ConvType::UnpolFF | ConvType::PolFF => FRG_IDX, + }, + conv, + }) + .collect(), alphas, alphas_cache: Vec::new(), - mur2_grid: Vec::new(), - muf2_grid: Vec::new(), - mua2_grid: Vec::new(), + mu2: [const { Vec::new() }; SCALES_CNT], x_grid: Vec::new(), - imur2: Vec::new(), - imuf2: Vec::new(), - imua2: Vec::new(), - ix: Vec::new(), - pdg, - perm: Vec::new(), } } - pub(crate) fn setup(&mut self, grid: &Grid, xi: &[(f64, f64, f64)]) { - self.perm = grid - .convolutions() - .iter() - .enumerate() - .map(|(max_idx, conv)| { - self.pdg + pub(crate) fn new_grid_conv_cache<'b>( + &'b mut self, + grid: &Grid, + xi: &[(f64, f64, f64)], + ) -> GridConvCache<'a, 'b> { + // TODO: try to avoid calling clear + self.clear(); + + let scales: [_; SCALES_CNT] = grid.scales().into(); + let xi: Vec<_> = (0..SCALES_CNT) + .map(|idx| { + let mut vars: Vec<_> = xi .iter() - .take(max_idx + 1) - .enumerate() - .rev() - .find_map(|(idx, pdg)| { - if conv == pdg { - Some((idx, false)) - } else if *conv == pdg.cc() { - Some((idx, true)) - } else { - None - } - }) - // TODO: convert `unwrap` to `Err` - .unwrap() + .map(|&x| <[_; SCALES_CNT]>::from(x)[idx]) + .collect(); + vars.sort_by(f64::total_cmp); + vars.dedup(); + vars }) .collect(); - // TODO: try to avoid calling clear - self.clear(); + for (result, scale, xi) in izip!(&mut self.mu2, scales, xi) { + result.clear(); + result.extend( + grid.subgrids() + .iter() + .filter(|subgrid| !subgrid.is_empty()) + .flat_map(|subgrid| { + scale + .calc(&subgrid.node_values(), grid.kinematics()) + .into_owned() + }) + .flat_map(|scale| xi.iter().map(move |&xi| xi * xi * scale)), + ); + result.sort_by(f64::total_cmp); + result.dedup(); + } let mut x_grid: Vec<_> = grid .subgrids() @@ -94,58 +111,66 @@ impl<'a> ConvolutionCache<'a> { x_grid.sort_by(f64::total_cmp); x_grid.dedup(); - let mut mur2_grid: Vec<_> = grid - .subgrids() + self.alphas_cache = self.mu2[REN_IDX] .iter() - .filter(|subgrid| !subgrid.is_empty()) - .flat_map(|subgrid| { - grid.scales() - .ren - .calc(&subgrid.node_values(), grid.kinematics()) - .unwrap_or_default() - }) - .flat_map(|ren| xi.iter().map(move |(xir, _, _)| xir * xir * ren)) + .map(|&mur2| (self.alphas)(mur2)) .collect(); - mur2_grid.sort_by(f64::total_cmp); - mur2_grid.dedup(); + self.x_grid = x_grid; - let mut muf2_grid: Vec<_> = grid - .subgrids() + let perm = grid + .convolutions() .iter() - .filter(|subgrid| !subgrid.is_empty()) - .flat_map(|subgrid| { - grid.scales() - .fac - .calc(&subgrid.node_values(), grid.kinematics()) - .unwrap_or_default() + .enumerate() + .map(|(max_idx, grid_conv)| { + self.caches + .iter() + .take(max_idx + 1) + .enumerate() + .rev() + .find_map(|(idx, ConvCache1d { conv, .. })| { + if grid_conv == conv { + Some((idx, false)) + } else if *grid_conv == conv.cc() { + Some((idx, true)) + } else { + None + } + }) + // TODO: convert `unwrap` to `Err` + .unwrap() }) - .flat_map(|fac| xi.iter().map(move |(_, xif, _)| xif * xif * fac)) .collect(); - muf2_grid.sort_by(f64::total_cmp); - muf2_grid.dedup(); - let mut mua2_grid: Vec<_> = grid - .subgrids() - .iter() - .filter(|subgrid| !subgrid.is_empty()) - .flat_map(|subgrid| { - grid.scales() - .frg - .calc(&subgrid.node_values(), grid.kinematics()) - .unwrap_or_default() - }) - .flat_map(|frg| xi.iter().map(move |(_, _, xia)| xia * xia * frg)) - .collect(); - mua2_grid.sort_by(f64::total_cmp); - mua2_grid.dedup(); + GridConvCache { + cache: self, + perm, + imu2: [const { Vec::new() }; SCALES_CNT], + ix: Vec::new(), + } + } - self.alphas_cache = mur2_grid.iter().map(|&mur2| (self.alphas)(mur2)).collect(); - self.mur2_grid = mur2_grid; - self.muf2_grid = muf2_grid; - self.mua2_grid = mua2_grid; - self.x_grid = x_grid; + /// Clears the cache. + pub fn clear(&mut self) { + self.alphas_cache.clear(); + for xfx_cache in &mut self.caches { + xfx_cache.cache.clear(); + } + for scales in &mut self.mu2 { + scales.clear(); + } + self.x_grid.clear(); } +} +/// TODO +pub struct GridConvCache<'a, 'b> { + cache: &'b mut ConvolutionCache<'a>, + perm: Vec<(usize, bool)>, + imu2: [Vec; SCALES_CNT], + ix: Vec>, +} + +impl<'a, 'b> GridConvCache<'a, 'b> { /// TODO pub fn as_fx_prod(&mut self, pdg_ids: &[i32], as_order: u8, indices: &[usize]) -> f64 { // TODO: here we assume that @@ -153,39 +178,42 @@ impl<'a> ConvolutionCache<'a> { // - indices[1] is x1 and // - indices[2] is x2. // Lift this restriction! - let fx_prod: f64 = self - .perm - .iter() - .zip(pdg_ids) - .enumerate() - .map(|(index, (&(idx, cc), &pdg_id))| { - let ix = self.ix[index][indices[index + 1]]; - let pid = if cc { + let skip_to_x = indices.len() - pdg_ids.len(); + let ix = self + .ix + .iter() + .zip(indices.iter().skip(skip_to_x)) + .map(|(ix, &index)| ix[index]); + let idx_pid = self.perm.iter().zip(pdg_ids).map(|(&(idx, cc), &pdg_id)| { + ( + idx, + if cc { pids::charge_conjugate_pdg_pid(pdg_id) } else { pdg_id - }; - let xfx = &mut self.xfx[idx]; - let xfx_cache = &mut self.xfx_cache[idx]; - let (imu2, mu2) = match self.pdg[idx].conv_type() { - ConvType::UnpolPDF | ConvType::PolPDF => { - let imuf2 = self.imuf2[indices[0]]; - (imuf2, self.muf2_grid[imuf2]) - } - ConvType::UnpolFF | ConvType::PolFF => { - let imua2 = self.imua2[indices[0]]; - (imua2, self.mua2_grid[imua2]) - } - }; - *xfx_cache.entry((pid, ix, imu2)).or_insert_with(|| { - let x = self.x_grid[ix]; + }, + ) + }); + + let fx_prod: f64 = ix + .zip(idx_pid) + .map(|(ix, (idx, pid))| { + let ConvCache1d { + xfx, cache, scale, .. + } = &mut self.cache.caches[idx]; + + let imu2 = self.imu2[*scale][indices[0]]; + let mu2 = self.cache.mu2[*scale][imu2]; + + *cache.entry((pid, ix, imu2)).or_insert_with(|| { + let x = self.cache.x_grid[ix]; xfx(pid, x, mu2) / x }) }) .product(); let alphas_powers = if as_order != 0 { - self.alphas_cache[self.imur2[indices[0]]].powi(as_order.into()) + self.cache.alphas_cache[self.imu2[REN_IDX][indices[0]]].powi(as_order.into()) } else { 1.0 }; @@ -193,67 +221,28 @@ impl<'a> ConvolutionCache<'a> { fx_prod * alphas_powers } - /// Clears the cache. - pub fn clear(&mut self) { - self.alphas_cache.clear(); - for xfx_cache in &mut self.xfx_cache { - xfx_cache.clear(); - } - self.mur2_grid.clear(); - self.muf2_grid.clear(); - self.mua2_grid.clear(); - self.x_grid.clear(); - } - /// Set the grids. - pub fn set_grids( - &mut self, - grid: &Grid, - node_values: &[Vec], - mu2_grid: &[f64], - xir: f64, - xif: f64, - xia: f64, - ) { - self.imur2 = mu2_grid - .iter() - .map(|ren| { - self.mur2_grid - .iter() - .position(|&mur2| mur2 == xir * xir * ren) - }) - .collect::>() - // if we didn't find a single renormalization scale, we assume we don't need any - // renormalization scale - .unwrap_or_default(); - self.imuf2 = mu2_grid - .iter() - .map(|fac| { - self.muf2_grid - .iter() - .position(|&muf2| muf2 == xif * xif * fac) - }) - .collect::>() - // if we didn't find a single factorization scale, we assume we don't need any - // factorization scale - .unwrap_or_default(); - self.imua2 = mu2_grid - .iter() - .map(|frg| { - self.mua2_grid + pub fn set_grids(&mut self, grid: &Grid, subgrid: &SubgridEnum, xi: (f64, f64, f64)) { + let node_values = subgrid.node_values(); + let kinematics = grid.kinematics(); + let scales: [_; SCALES_CNT] = grid.scales().into(); + let xi: [_; SCALES_CNT] = xi.into(); + + for (result, values, scale, xi) in izip!(&mut self.imu2, &self.cache.mu2, scales, xi) { + result.clear(); + result.extend(scale.calc(&node_values, kinematics).into_iter().map(|s| { + values .iter() - .position(|&mua2| mua2 == xia * xia * frg) - }) - .collect::>() - // if we didn't find a single fragmentation scale, we assume we don't need any - // fragmentation scale - .unwrap_or_default(); + .position(|&value| value == xi * xi * s) + .unwrap() + })); + } self.ix = (0..grid.convolutions().len()) .map(|idx| { - grid.kinematics() + kinematics .iter() - .zip(node_values) + .zip(&node_values) .find_map(|(kin, node_values)| { matches!(kin, &Kinematics::X(index) if index == idx).then_some(node_values) }) @@ -261,7 +250,8 @@ impl<'a> ConvolutionCache<'a> { .unwrap_or_else(|| unreachable!()) .into_iter() .map(|xd| { - self.x_grid + self.cache + .x_grid .iter() .position(|x| xd == x) .unwrap_or_else(|| unreachable!()) diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index 25615a1d..fad0f147 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -93,8 +93,7 @@ impl AlphasTable { grid.scales() .ren .calc(&subgrid.node_values(), grid.kinematics()) - // UNWRAP: grids with no renormalization scales should not call this function - .unwrap() + .into_owned() .into_iter() .map(|ren| xir * xir * ren) }) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 8201671d..e12c6b9f 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -229,13 +229,13 @@ impl Grid { /// TODO pub fn convolve( &self, - convolution_cache: &mut ConvolutionCache, + cache: &mut ConvolutionCache, order_mask: &[bool], bin_indices: &[usize], channel_mask: &[bool], xi: &[(f64, f64, f64)], ) -> Vec { - convolution_cache.setup(self, xi); + let mut cache = cache.new_grid_conv_cache(self, xi); let bin_indices = if bin_indices.is_empty() { (0..self.bin_info().bins()).collect() @@ -246,7 +246,7 @@ impl Grid { let normalizations = self.bin_info().normalizations(); let pdg_channels = self.channels_pdg(); - for (xi_index, &(xir, xif, xia)) in xi.iter().enumerate() { + for (xi_index, &xis @ (xir, xif, xia)) in xi.iter().enumerate() { for ((ord, bin, chan), subgrid) in self.subgrids.indexed_iter() { let order = &self.orders[ord]; @@ -272,29 +272,16 @@ impl Grid { } let channel = &pdg_channels[chan]; - let mu2_grid = self - .kinematics() - .iter() - .zip(subgrid.node_values()) - .find_map(|(kin, node_values)| { - // TODO: generalize this for arbitrary scales - matches!(kin, &Kinematics::Scale(idx) if idx == 0).then_some(node_values) - }) - // TODO: convert this into an error - .unwrap(); - let node_values = subgrid.node_values(); - - // TODO: generalize this for fragmentation functions - convolution_cache.set_grids(self, &node_values, &mu2_grid, xir, xif, 1.0); - let mut value = 0.0; + cache.set_grids(self, subgrid, xis); + for (idx, v) in subgrid.indexed_iter() { let mut lumi = 0.0; for entry in channel.entry() { // TODO: we assume `idx` to be ordered as scale, x1, x2 - let fx_prod = convolution_cache.as_fx_prod(&entry.0, order.alphas, &idx); + let fx_prod = cache.as_fx_prod(&entry.0, order.alphas, &idx); lumi += fx_prod * entry.1; } @@ -330,13 +317,13 @@ impl Grid { /// TODO pub fn convolve_subgrid( &self, - convolution_cache: &mut ConvolutionCache, + cache: &mut ConvolutionCache, ord: usize, bin: usize, channel: usize, - (xir, xif, xia): (f64, f64, f64), + xi @ (xir, xif, xia): (f64, f64, f64), ) -> ArrayD { - convolution_cache.setup(self, &[(xir, xif, xia)]); + let mut cache = cache.new_grid_conv_cache(self, &[(xir, xif, xia)]); let normalizations = self.bin_info().normalizations(); let pdg_channels = self.channels_pdg(); @@ -345,13 +332,14 @@ impl Grid { let order = &self.orders[ord]; let channel = &pdg_channels[channel]; + + cache.set_grids(self, subgrid, xi); + let node_values: Vec<_> = subgrid.node_values(); // TODO: generalize this to N dimensions assert_eq!(node_values.len(), 3); - - convolution_cache.set_grids(self, &subgrid.node_values(), &node_values[0], xir, xif, xia); - let dim: Vec<_> = node_values.iter().map(Vec::len).collect(); + let mut array = ArrayD::zeros(dim); for (idx, value) in subgrid.indexed_iter() { @@ -361,7 +349,7 @@ impl Grid { for entry in channel.entry() { debug_assert_eq!(entry.0.len(), 2); // TODO: we assume `idx` to be ordered as scale, x1, x2 - let fx_prod = convolution_cache.as_fx_prod(&entry.0, order.alphas, &idx); + let fx_prod = cache.as_fx_prod(&entry.0, order.alphas, &idx); lumi += fx_prod * entry.1; } From 62215369e7c6e1f4c5f4aee908b9dc739feb2824 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 10:36:55 +0200 Subject: [PATCH 205/213] Update PyO3 and Numpy from 0.21.x to 0.22.x --- Cargo.lock | 93 ++++++++++++----------------------------- pineappl_py/Cargo.toml | 4 +- pineappl_py/src/pids.rs | 4 +- 3 files changed, 31 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b9bd970c..12f49682 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,7 +209,7 @@ version = "4.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro2", "quote", "syn", @@ -584,6 +584,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.4" @@ -696,16 +702,6 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" -[[package]] -name = "lock_api" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" version = "0.4.20" @@ -850,9 +846,9 @@ dependencies = [ [[package]] name = "numpy" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec170733ca37175f5d75a5bea5911d6ff45d2cd52849ce98b685394e4f2f37f4" +checksum = "cf314fca279e6e6ac2126a4ff98f26d88aa4ad06bc68fb6ae5cf4bd706758311" dependencies = [ "libc", "ndarray", @@ -875,29 +871,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets 0.48.5", -] - [[package]] name = "percent-encoding" version = "2.3.1" @@ -1124,9 +1097,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.78" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" dependencies = [ "unicode-ident", ] @@ -1146,15 +1119,15 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.21.2" +version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5e00b96a521718e08e03b1a622f01c8a8deb50719335de3f60b3b3950f069d8" +checksum = "3d922163ba1f79c04bc49073ba7b32fd5a8d3b76a87c955921234b8e77333c51" dependencies = [ "cfg-if", "indoc", "libc", "memoffset", - "parking_lot", + "once_cell", "portable-atomic", "pyo3-build-config", "pyo3-ffi", @@ -1164,9 +1137,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.21.2" +version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7883df5835fafdad87c0d888b266c8ec0f4c9ca48a5bed6bbb592e8dedee1b50" +checksum = "bc38c5feeb496c8321091edf3d63e9a6829eab4b863b4a6a65f26f3e9cc6b179" dependencies = [ "once_cell", "target-lexicon", @@ -1174,9 +1147,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.21.2" +version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01be5843dc60b916ab4dad1dca6d20b9b4e6ddc8e15f50c47fe6d85f1fb97403" +checksum = "94845622d88ae274d2729fcefc850e63d7a3ddff5e3ce11bd88486db9f1d357d" dependencies = [ "libc", "pyo3-build-config", @@ -1184,9 +1157,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.21.2" +version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77b34069fc0682e11b31dbd10321cbf94808394c56fd996796ce45217dfac53c" +checksum = "e655aad15e09b94ffdb3ce3d217acf652e26bbc37697ef012f5e5e348c716e5e" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -1196,11 +1169,11 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.21.2" +version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08260721f32db5e1a5beae69a55553f56b99bd0e1c3e6e0a5e8851a9d0f5a85c" +checksum = "ae1e3f09eecd94618f60a455a23def79f79eba4dc561a97324bf9ac8c6df30ce" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "pyo3-build-config", "quote", @@ -1408,12 +1381,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "scratch" version = "1.0.7" @@ -1473,12 +1440,6 @@ dependencies = [ "digest", ] -[[package]] -name = "smallvec" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" - [[package]] name = "spin" version = "0.9.8" @@ -1505,9 +1466,9 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "syn" -version = "2.0.48" +version = "2.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" dependencies = [ "proc-macro2", "quote", @@ -1527,9 +1488,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.12.13" +version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] name = "tempfile" diff --git a/pineappl_py/Cargo.toml b/pineappl_py/Cargo.toml index 134ce107..7aa5af36 100644 --- a/pineappl_py/Cargo.toml +++ b/pineappl_py/Cargo.toml @@ -29,6 +29,6 @@ crate-type = ["cdylib"] [dependencies] itertools = "0.10.1" ndarray = "0.15.4" -numpy = "0.21.0" +numpy = "0.22.0" pineappl = { path = "../pineappl", version = "=1.0.0-alpha1" } -pyo3 = { features = ["extension-module"], version = "0.21.2" } +pyo3 = { features = ["extension-module"], version = "0.22.5" } diff --git a/pineappl_py/src/pids.rs b/pineappl_py/src/pids.rs index e8fdfadd..f4c7edb3 100644 --- a/pineappl_py/src/pids.rs +++ b/pineappl_py/src/pids.rs @@ -4,8 +4,8 @@ use pineappl::pids::PidBasis; use pyo3::prelude::*; /// PyO3 wrapper to :rustdoc:`pineappl::pids::PidBasis `. -#[pyclass(name = "PidBasis")] -#[derive(Clone)] +#[pyclass(eq, eq_int, name = "PidBasis")] +#[derive(Clone, PartialEq)] pub enum PyPidBasis { /// PDG Monte Carlo IDs. Pdg, From ee7297215026001a1c8b44ddaf01d4960dd03e09 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 10:39:01 +0200 Subject: [PATCH 206/213] Allow numpy-2 --- pineappl_py/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl_py/pyproject.toml b/pineappl_py/pyproject.toml index f8e19cb3..1bccab67 100644 --- a/pineappl_py/pyproject.toml +++ b/pineappl_py/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ "Topic :: Scientific/Engineering :: Physics", ] -dependencies = ["numpy>=1.16.0,<2.0.0"] +dependencies = ["numpy>=1.16.0"] [project.optional-dependencies] cli = ["pineappl-cli"] From 3fe8b62a2a2d55627e48f6c9089dae3f660e9004 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 10:39:23 +0200 Subject: [PATCH 207/213] Increase minimal Python version to 3.7 --- pineappl_py/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl_py/pyproject.toml b/pineappl_py/pyproject.toml index 1bccab67..37adb15e 100644 --- a/pineappl_py/pyproject.toml +++ b/pineappl_py/pyproject.toml @@ -7,7 +7,7 @@ name = "pineappl" # due to a bug in warehouse, https://github.com/pypi/warehouse/issues/8090, this file must be the # same across all wheels of a single version and therefore `requires-python` must give the minimum # Python version that we support -requires-python = ">=3.6" +requires-python = ">=3.7" classifiers = [ "Development Status :: 4 - Beta", "Intended Audience :: Science/Research", From 9dfdfcf68320103f3fc4dd880f5d81038977e54d Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 10:47:11 +0200 Subject: [PATCH 208/213] Test and release wheels for Python 3.13 --- .github/workflows/python.yml | 2 +- .github/workflows/release.yml | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 6d8fe1fa..ad304ee9 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: - python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3af3e288..05c6b969 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -117,6 +117,7 @@ jobs: 3.9 3.11 3.12 + 3.13 3.10 - name: Install dependencies run: | @@ -379,6 +380,7 @@ jobs: 3.9 3.11 3.12 + 3.13 3.10 - name: Build wheels uses: PyO3/maturin-action@v1 @@ -429,6 +431,7 @@ jobs: 3.9 3.11 3.12 + 3.13 3.10 architecture: ${{ matrix.target }} - name: Build wheels From 95cba1249f2b446d839645d059ec00972dac0471 Mon Sep 17 00:00:00 2001 From: Radonirinaunimi Date: Tue, 22 Oct 2024 13:09:11 +0200 Subject: [PATCH 209/213] Fixed shapes of `array` and `node_values` of `ImportSubgridV1` for DIS --- pineappl_py/src/import_subgrid.rs | 59 ++++++++++++++++++++++++------ pineappl_py/tests/test_fk_table.py | 3 +- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/pineappl_py/src/import_subgrid.rs b/pineappl_py/src/import_subgrid.rs index 6cef84e3..d3721a81 100644 --- a/pineappl_py/src/import_subgrid.rs +++ b/pineappl_py/src/import_subgrid.rs @@ -1,7 +1,7 @@ //! PyPackedSubgrid* interface. use super::subgrid::PySubgridEnum; -use numpy::PyReadonlyArray3; +use numpy::{PyReadonlyArray2, PyReadonlyArray3}; use pineappl::import_subgrid::ImportSubgridV1; use pineappl::packed_array::PackedArray; use pyo3::prelude::*; @@ -18,6 +18,8 @@ pub struct PyImportSubgridV1 { impl PyImportSubgridV1 { /// Constructor. /// + /// # Panics + /// /// Parameters /// ---------- /// array : numpy.ndarray(float) @@ -26,25 +28,58 @@ impl PyImportSubgridV1 { /// scales grid /// x1_grid : list(float) /// first momentum fraction grid - /// x2_grid : list(float) + /// x2_grid : Optional(list(float)) /// second momentum fraction grid #[new] - pub fn new( - array: PyReadonlyArray3, + #[must_use] + pub fn new<'py>( + array: PyObject, scales: Vec, x1_grid: Vec, - x2_grid: Vec, + x2_grid: Option>, + py: Python<'py>, ) -> Self { - let node_values: Vec> = vec![scales, x1_grid, x2_grid]; + // let node_values: Vec> = vec![scales, x1_grid, x2_grid]; + // let mut sparse_array: PackedArray = + // PackedArray::new(node_values.iter().map(Vec::len).collect()); + + // for ((iscale, ix1, ix2), value) in array + // .as_array() + // .indexed_iter() + // .filter(|((_, _, _), value)| **value != 0.0) + // { + // sparse_array[[iscale, ix1, ix2]] = *value; + // } + + // Self { + // import_subgrid: ImportSubgridV1::new(sparse_array, node_values), + // } + let mut node_values: Vec> = vec![scales, x1_grid]; + + if let Some(x2) = x2_grid { + node_values.push(x2); + } let mut sparse_array: PackedArray = PackedArray::new(node_values.iter().map(Vec::len).collect()); - for ((iscale, ix1, ix2), value) in array - .as_array() - .indexed_iter() - .filter(|((_, _, _), value)| **value != 0.0) - { - sparse_array[[iscale, ix1, ix2]] = *value; + if sparse_array.shape().to_vec().len() == 3 { + let array_3d: PyReadonlyArray3 = array.extract(py).unwrap(); + for ((iscale, ix1, ix2), value) in array_3d + .as_array() + .indexed_iter() + .filter(|((_, _, _), value)| **value != 0.0) + { + sparse_array[[iscale, ix1, ix2]] = *value; + } + } else { + let array_2d: PyReadonlyArray2 = array.extract(py).unwrap(); + for ((iscale, ix1), value) in array_2d + .as_array() + .indexed_iter() + .filter(|((_, _), value)| **value != 0.0) + { + sparse_array[[iscale, ix1]] = *value; + } } Self { diff --git a/pineappl_py/tests/test_fk_table.py b/pineappl_py/tests/test_fk_table.py index fdde8d4f..ac61e166 100644 --- a/pineappl_py/tests/test_fk_table.py +++ b/pineappl_py/tests/test_fk_table.py @@ -86,10 +86,9 @@ def test_convolve(self): xs = np.linspace(0.5, 1.0, 5) vs = xs.copy() subgrid = ImportSubgridV1( - vs[np.newaxis, :, np.newaxis], + vs[np.newaxis, :], # DIS shape: (len(q2), len(x_grid)) np.array([90.0]), xs, - np.array([1.0]), ) g.set_subgrid(0, 0, 0, subgrid.into()) From 05a4c3e669534b4b39ad49bbc11220fc8b4a591a Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 12:20:28 +0200 Subject: [PATCH 210/213] Fix some clippy warnings --- pineappl/src/boc.rs | 2 +- pineappl/src/convolutions.rs | 9 +- pineappl/src/evolution.rs | 2 +- pineappl/src/interpolation.rs | 214 +++++++++++++++++----------------- 4 files changed, 114 insertions(+), 113 deletions(-) diff --git a/pineappl/src/boc.rs b/pineappl/src/boc.rs index b4c6d31d..b52293b4 100644 --- a/pineappl/src/boc.rs +++ b/pineappl/src/boc.rs @@ -59,7 +59,7 @@ impl ScaleFuncForm { .iter() .position(|&kin| kin == Kinematics::Scale(index)) // UNWRAP: this should be guaranteed by `Grid::new` - .unwrap()], + .unwrap_or_else(|| unreachable!())], ) } } diff --git a/pineappl/src/convolutions.rs b/pineappl/src/convolutions.rs index 84db6a45..276eac57 100644 --- a/pineappl/src/convolutions.rs +++ b/pineappl/src/convolutions.rs @@ -170,7 +170,7 @@ pub struct GridConvCache<'a, 'b> { ix: Vec>, } -impl<'a, 'b> GridConvCache<'a, 'b> { +impl GridConvCache<'_, '_> { /// TODO pub fn as_fx_prod(&mut self, pdg_ids: &[i32], as_order: u8, indices: &[usize]) -> f64 { // TODO: here we assume that @@ -230,11 +230,12 @@ impl<'a, 'b> GridConvCache<'a, 'b> { for (result, values, scale, xi) in izip!(&mut self.imu2, &self.cache.mu2, scales, xi) { result.clear(); - result.extend(scale.calc(&node_values, kinematics).into_iter().map(|s| { + result.extend(scale.calc(&node_values, kinematics).iter().map(|s| { values .iter() .position(|&value| value == xi * xi * s) - .unwrap() + // UNWRAP: if this fails, `new_grid_conv_cache` hasn't been called properly + .unwrap_or_else(|| unreachable!()) })); } @@ -248,7 +249,7 @@ impl<'a, 'b> GridConvCache<'a, 'b> { }) // UNWRAP: guaranteed by the grid constructor .unwrap_or_else(|| unreachable!()) - .into_iter() + .iter() .map(|xd| { self.cache .x_grid diff --git a/pineappl/src/evolution.rs b/pineappl/src/evolution.rs index fad0f147..d8cd9696 100644 --- a/pineappl/src/evolution.rs +++ b/pineappl/src/evolution.rs @@ -223,7 +223,7 @@ fn ndarray_from_subgrid_orders_slice_many( .iter() .map(|kin| match kin { &Kinematics::X(idx) => idx, - _ => unreachable!(), + Kinematics::Scale(_) => unreachable!(), }) .collect::>(), (0..(grid.kinematics().len() - 1)).collect::>() diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 6f7d3f63..0e102c7e 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -349,46 +349,46 @@ mod tests { let node_values: Vec<_> = interps.iter().map(Interp::node_values).collect(); let q2_reference = [ - 9.9999999999999986e1, + 9.999999999999999e1, 1.2242682307575689e2, - 1.5071735829758390e2, + 1.507173582975839e2, 1.8660624792652183e2, 2.3239844323901826e2, - 2.9117504454783159e2, - 3.6707996194452909e2, - 4.6572167648697109e2, - 5.9473999989302229e2, - 7.6461095796663312e2, - 9.8979770734783131e2, + 2.911750445478316e2, + 3.670799619445291e2, + 4.657216764869711e2, + 5.947399998930223e2, + 7.646109579666331e2, + 9.897977073478313e2, 1.2904078604330668e3, - 1.6945973073289490e3, + 1.694597307328949e3, 2.2420826491130997e3, - 2.9893125907295248e3, - 4.0171412997902630e3, - 5.4423054291935287e3, - 7.4347313816879214e3, - 1.0243854670019169e4, + 2.989312590729525e3, + 4.017141299790263e3, + 5.442305429193529e3, + 7.434731381687921e3, + 1.024385467001917e4, 1.4238990475802799e4, 1.9971806922234402e4, 2.8273883344269376e4, - 4.0410482328443621e4, - 5.8325253189217328e4, - 8.5033475340946548e4, + 4.041048232844362e4, + 5.832525318921733e4, + 8.503347534094655e4, 1.2526040013230646e5, - 1.8648821332147921e5, - 2.8069149021747953e5, - 4.2724538080621109e5, - 6.5785374312992941e5, + 1.864882133214792e5, + 2.806914902174795e5, + 4.272453808062111e5, + 6.578537431299294e5, 1.0249965523865514e6, 1.6165812577807596e6, - 2.5816634211063879e6, + 2.581663421106388e6, 4.1761634755570055e6, - 6.8451673415389210e6, + 6.845167341538921e6, 1.1373037585359517e7, - 1.9160909972020049e7, + 1.916090997202005e7, 3.2746801715531096e7, - 5.6794352823474184e7, - 9.9999999999999493e7, + 5.679435282347418e7, + 9.99999999999995e7, ]; assert_eq!(node_values[0].len(), interps[0].nodes()); @@ -398,55 +398,55 @@ mod tests { } let x_reference = [ - 1.0000000000000000e0, - 9.3094408087175440e-1, - 8.6278393239061080e-1, - 7.9562425229227562e-1, - 7.2958684424143116e-1, - 6.6481394824738227e-1, - 6.0147219796733498e-1, - 5.3975723378804452e-1, - 4.7989890296102550e-1, - 4.2216677535896480e-1, - 3.6687531864822420e-1, + 1.0, + 9.309440808717544e-1, + 8.627839323906108e-1, + 7.956242522922756e-1, + 7.295868442414312e-1, + 6.648139482473823e-1, + 6.01472197967335e-1, + 5.397572337880445e-1, + 4.798989029610255e-1, + 4.221667753589648e-1, + 3.668753186482242e-1, 3.1438740076927585e-1, - 2.6511370415828228e-1, - 2.1950412650038861e-1, + 2.651137041582823e-1, + 2.195041265003886e-1, 1.7802566042569432e-1, 1.4112080644440345e-1, 1.0914375746330703e-1, - 8.2281221262048926e-2, + 8.228122126204893e-2, 6.0480028754447364e-2, - 4.3414917417022691e-2, + 4.341491741702269e-2, 3.0521584007828916e-2, - 2.1089186683787169e-2, + 2.108918668378717e-2, 1.4375068581090129e-2, - 9.6991595740433985e-3, - 6.4962061946337987e-3, - 4.3285006388208112e-3, + 9.699159574043399e-3, + 6.496206194633799e-3, + 4.328500638820811e-3, 2.8738675812817515e-3, 1.9034634022867384e-3, 1.2586797144272762e-3, - 8.3140688364881441e-4, - 5.4877953236707956e-4, + 8.314068836488144e-4, + 5.487795323670796e-4, 3.6205449638139736e-4, 2.3878782918561914e-4, 1.5745605600841445e-4, 1.0381172986576898e-4, - 6.8437449189678965e-5, - 4.5114383949640441e-5, - 2.9738495372244901e-5, + 6.843744918967897e-5, + 4.511438394964044e-5, + 2.97384953722449e-5, 1.9602505002391748e-5, - 1.2921015690747310e-5, - 8.5168066775733548e-6, - 5.6137577169301513e-6, + 1.292101569074731e-5, + 8.516806677573355e-6, + 5.613757716930151e-6, 3.7002272069854957e-6, - 2.4389432928916821e-6, - 1.6075854984708080e-6, + 2.438943292891682e-6, + 1.607585498470808e-6, 1.0596094959101024e-6, - 6.9842085307003639e-7, + 6.984208530700364e-7, 4.6035014748963906e-7, - 3.0343047658679519e-7, + 3.034304765867952e-7, 1.9999999999999954e-7, ]; @@ -470,44 +470,44 @@ mod tests { } let reference = [ - ([9, 6, 6], -4.0913584971505212e-6), - ([9, 6, 7], 3.0858594463668783e-5), + ([9, 6, 6], -4.091358497150521e-6), + ([9, 6, 7], 3.085859446366878e-5), ([9, 6, 8], 6.0021251939206686e-5), ([9, 6, 9], -5.0714506160633226e-6), - ([9, 7, 6], 3.0858594463668783e-5), + ([9, 7, 6], 3.085859446366878e-5), ([9, 7, 7], -2.3274735101712016e-4), - ([9, 7, 8], -4.5270329502624643e-4), - ([9, 7, 9], 3.8250825004119329e-5), - ([9, 8, 6], 6.0021251939206680e-5), + ([9, 7, 8], -4.527032950262464e-4), + ([9, 7, 9], 3.825082500411933e-5), + ([9, 8, 6], 6.002125193920668e-5), ([9, 8, 7], -4.5270329502624637e-4), - ([9, 8, 8], -8.8052677047459023e-4), - ([9, 8, 9], 7.4399448333843429e-5), - ([9, 9, 6], -5.0714506160633217e-6), - ([9, 9, 7], 3.8250825004119329e-5), - ([9, 9, 8], 7.4399448333843443e-5), - ([9, 9, 9], -6.2863255246593026e-6), - ([10, 6, 6], 3.2560454032038003e-4), + ([9, 8, 8], -8.805267704745902e-4), + ([9, 8, 9], 7.439944833384343e-5), + ([9, 9, 6], -5.071450616063322e-6), + ([9, 9, 7], 3.825082500411933e-5), + ([9, 9, 8], 7.439944833384344e-5), + ([9, 9, 9], -6.286325524659303e-6), + ([10, 6, 6], 3.25604540320380e-4), ([10, 6, 7], -2.4558342839606324e-3), - ([10, 6, 8], -4.7767000033681270e-3), - ([10, 6, 9], 4.0360368023258439e-4), + ([10, 6, 8], -4.776700003368127e-3), + ([10, 6, 9], 4.036036802325844e-4), ([10, 7, 6], -2.4558342839606324e-3), ([10, 7, 7], 1.8522843767295388e-2), - ([10, 7, 8], 3.6027702872090658e-2), + ([10, 7, 8], 3.602770287209066e-2), ([10, 7, 9], -3.0441337030269453e-3), - ([10, 8, 6], -4.7767000033681270e-3), - ([10, 8, 7], 3.6027702872090658e-2), - ([10, 8, 8], 7.0075383161814372e-2), - ([10, 8, 9], -5.9209668846429931e-3), - ([10, 9, 6], 4.0360368023258439e-4), + ([10, 8, 6], -4.776700003368127e-3), + ([10, 8, 7], 3.602770287209066e-2), + ([10, 8, 8], 7.007538316181437e-2), + ([10, 8, 9], -5.920966884642993e-3), + ([10, 9, 6], 4.036036802325844e-4), ([10, 9, 7], -3.0441337030269453e-3), - ([10, 9, 8], -5.9209668846429931e-3), - ([10, 9, 9], 5.0028765120106755e-4), + ([10, 9, 8], -5.920966884642993e-3), + ([10, 9, 9], 5.002876512010676e-4), ([11, 6, 6], 1.3274904136884986e-5), ([11, 6, 7], -1.0012441676511976e-4), - ([11, 6, 8], -1.9474616224017421e-4), + ([11, 6, 8], -1.947461622401742e-4), ([11, 6, 9], 1.6454930754680843e-5), ([11, 7, 6], -1.0012441676511976e-4), - ([11, 7, 7], 7.5517674019963063e-4), + ([11, 7, 7], 7.551767401996306e-4), ([11, 7, 8], 1.4688502237364042e-3), ([11, 7, 9], -1.2410939677862364e-4), ([11, 8, 6], -1.9474616224017418e-4), @@ -520,50 +520,50 @@ mod tests { ([11, 9, 9], 2.0396738337944602e-5), ([12, 6, 6], -2.1682835394615433e-6), ([12, 6, 7], 1.6354025801721504e-5), - ([12, 6, 8], 3.1809261566371142e-5), + ([12, 6, 8], 3.180926156637114e-5), ([12, 6, 9], -2.6876996722875166e-6), ([12, 7, 6], 1.6354025801721504e-5), ([12, 7, 7], -1.2334833293517984e-4), ([12, 7, 8], -2.3991764680339134e-4), ([12, 7, 9], 2.0271661426154572e-5), - ([12, 8, 6], 3.1809261566371142e-5), + ([12, 8, 6], 3.180926156637114e-5), ([12, 8, 7], -2.3991764680339134e-4), ([12, 8, 8], -4.6664981907720756e-4), - ([12, 8, 9], 3.9429226082154630e-5), + ([12, 8, 9], 3.942922608215463e-5), ([12, 9, 6], -2.6876996722875166e-6), ([12, 9, 7], 2.0271661426154572e-5), - ([12, 9, 8], 3.9429226082154623e-5), + ([12, 9, 8], 3.942922608215462e-5), ([12, 9, 9], -3.3315428526512343e-6), ([23, 11, 6], -2.4353100307613186e-4), ([23, 11, 7], 1.8368041980410083e-3), - ([23, 11, 8], 3.5726606946862392e-3), + ([23, 11, 8], 3.572660694686239e-3), ([23, 11, 9], -3.0186928289005667e-4), ([23, 12, 6], 2.9987494527093064e-3), ([23, 12, 7], -2.2617718130482554e-2), - ([23, 12, 8], -4.3992404119311192e-2), - ([23, 12, 9], 3.7171051546702580e-3), - ([23, 13, 6], 1.4248943085993610e-3), + ([23, 12, 8], -4.399240411931119e-2), + ([23, 12, 9], 3.717105154670258e-3), + ([23, 13, 6], 1.424894308599361e-3), ([23, 13, 7], -1.0747099197804599e-2), - ([23, 13, 8], -2.0903555712057060e-2), - ([23, 13, 9], 1.7662302446007081e-3), + ([23, 13, 8], -2.090355571205706e-2), + ([23, 13, 9], 1.766230244600708e-3), ([23, 14, 6], -1.9189233197773798e-4), ([23, 14, 7], 1.4473255417027965e-3), - ([23, 14, 8], 2.8151084806817320e-3), + ([23, 14, 8], 2.815108480681732e-3), ([23, 14, 9], -2.3786047737056168e-4), ([24, 11, 6], 2.4624842908465045e-3), ([24, 11, 7], -1.8573000668924675e-2), - ([24, 11, 8], -3.6125260135520983e-2), + ([24, 11, 8], -3.612526013552098e-2), ([24, 11, 9], 3.0523767307502974e-3), - ([24, 12, 6], -3.0322108175987520e-2), + ([24, 12, 6], -3.032210817598752e-2), ([24, 12, 7], 2.2870096573985707e-1), ([24, 12, 8], 4.4483290707142076e-1), - ([24, 12, 9], -3.7585822483302452e-2), + ([24, 12, 9], -3.758582248330245e-2), ([24, 13, 6], -1.4407939057950724e-2), ([24, 13, 7], 1.0867020055959625e-1), ([24, 13, 8], 2.1136806777609088e-1), ([24, 13, 9], -1.7859386182495846e-2), - ([24, 14, 6], 1.9403355098954720e-3), - ([24, 14, 7], -1.4634754364600849e-2), + ([24, 14, 6], 1.940335509895472e-3), + ([24, 14, 7], -1.463475436460085e-2), ([24, 14, 8], -2.8465206988616668e-2), ([24, 14, 9], 2.4051462916002946e-3), ([25, 11, 6], 1.7967411488022474e-3), @@ -572,31 +572,31 @@ mod tests { ([25, 11, 9], 2.2271536489275397e-3), ([25, 12, 6], -2.2124396764984615e-2), ([25, 12, 7], 1.6687068317270662e-1), - ([25, 12, 8], 3.2457043135158342e-1), + ([25, 12, 8], 3.245704313515834e-1), ([25, 12, 9], -2.7424334895599013e-2), ([25, 13, 6], -1.0512691216379747e-2), - ([25, 13, 7], 7.9290747851593249e-2), - ([25, 13, 8], 1.5422380817933001e-1), + ([25, 13, 7], 7.929074785159325e-2), + ([25, 13, 8], 1.54223808179330e-1), ([25, 13, 9], -1.3031024874237778e-2), - ([25, 14, 6], 1.4157575201882570e-3), + ([25, 14, 6], 1.415757520188257e-3), ([25, 14, 7], -1.0678186036448784e-2), ([25, 14, 8], -2.0769516741988788e-2), - ([25, 14, 9], 1.7549047224670190e-3), + ([25, 14, 9], 1.754904722467019e-3), ([26, 11, 6], -2.1941078639412583e-4), ([26, 11, 7], 1.6548802758317395e-3), - ([26, 11, 8], 3.2188110862231270e-3), + ([26, 11, 8], 3.218811086223127e-3), ([26, 11, 9], -2.7197102590848567e-4), ([26, 12, 6], 2.7017421490774826e-3), ([26, 12, 7], -2.0377575170166206e-2), ([26, 12, 8], -3.9635232727098596e-2), ([26, 12, 9], 3.3489492294371172e-3), ([26, 13, 6], 1.2837674744868705e-3), - ([26, 13, 7], -9.6826665051300553e-3), + ([26, 13, 7], -9.682666505130055e-3), ([26, 13, 8], -1.8833189775767707e-2), ([26, 13, 9], 1.5912962293338163e-3), ([26, 14, 6], -1.7288660142000884e-4), - ([26, 14, 7], 1.3039770348009471e-3), - ([26, 14, 8], 2.5362896622162690e-3), + ([26, 14, 7], 1.303977034800947e-3), + ([26, 14, 8], 2.536289662216269e-3), ([26, 14, 9], -2.1430189065349477e-4), ]; From 411a6ae614b8711cdf8958dcec1df85e7c5ba948 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 13:54:31 +0200 Subject: [PATCH 211/213] Fix most 'strict comparison of ...' warnings --- pineappl/src/bin.rs | 13 +++++++------ pineappl/src/fk_table.rs | 2 +- pineappl/src/grid.rs | 6 +++--- pineappl/src/import_subgrid.rs | 6 +++--- pineappl/src/interp_subgrid.rs | 4 +++- pineappl/src/interpolation.rs | 2 +- pineappl/src/pids.rs | 3 ++- pineappl_cli/src/diff.rs | 25 +++++++++++++++++++++---- pineappl_cli/src/evolve.rs | 4 +++- pineappl_cli/src/export.rs | 13 +++++++++++-- pineappl_cli/src/export/applgrid.rs | 8 ++++---- pineappl_cli/src/import.rs | 13 +++++++++++-- pineappl_cli/src/import/applgrid.rs | 3 ++- pineappl_cli/src/import/fastnlo.rs | 3 ++- pineappl_cli/src/plot.rs | 3 ++- pineappl_cli/tests/import.rs | 5 +++-- 16 files changed, 79 insertions(+), 34 deletions(-) diff --git a/pineappl/src/bin.rs b/pineappl/src/bin.rs index 3d45a597..7cec0c3e 100644 --- a/pineappl/src/bin.rs +++ b/pineappl/src/bin.rs @@ -790,6 +790,7 @@ impl BinLimits { #[cfg(test)] mod test { use super::*; + use float_cmp::assert_approx_eq; use std::iter; #[test] @@ -806,16 +807,16 @@ mod test { ])) .unwrap(); - assert_eq!(limits.left(), 0.0); - assert_eq!(limits.right(), 2.0); + assert_approx_eq!(f64, limits.left(), 0.0, ulps = 2); + assert_approx_eq!(f64, limits.right(), 2.0, ulps = 2); assert_eq!(limits.bins(), 6); let non_consecutive_bins = BinLimits::new(vec![3.0, 4.0]); assert!(limits.merge(&non_consecutive_bins).is_err()); - assert_eq!(limits.left(), 0.0); - assert_eq!(limits.right(), 2.0); + assert_approx_eq!(f64, limits.left(), 0.0, ulps = 2); + assert_approx_eq!(f64, limits.right(), 2.0, ulps = 2); assert_eq!(limits.bins(), 6); // left merge @@ -828,8 +829,8 @@ mod test { ])) .is_err()); - assert_eq!(limits.left(), 0.0); - assert_eq!(limits.right(), 2.0); + assert_approx_eq!(f64, limits.left(), 0.0, ulps = 2); + assert_approx_eq!(f64, limits.right(), 2.0, ulps = 2); assert_eq!(limits.bins(), 6); } diff --git a/pineappl/src/fk_table.rs b/pineappl/src/fk_table.rs index 5315f0ff..772f098a 100644 --- a/pineappl/src/fk_table.rs +++ b/pineappl/src/fk_table.rs @@ -355,7 +355,7 @@ impl TryFrom for FkTable { for channel in grid.channels() { let entry = channel.entry(); - if entry.len() != 1 || entry[0].1 != 1.0 { + if entry.len() != 1 || !approx_eq!(f64, entry[0].1, 1.0, ulps = 4) { return Err(TryFromGridError::InvalidChannel); } } diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index e12c6b9f..8e6f3088 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -250,9 +250,9 @@ impl Grid { for ((ord, bin, chan), subgrid) in self.subgrids.indexed_iter() { let order = &self.orders[ord]; - if ((order.logxir > 0) && (xir == 1.0)) - || ((order.logxif > 0) && (xif == 1.0)) - || ((order.logxia > 0) && (xia == 1.0)) + if ((order.logxir > 0) && approx_eq!(f64, xir, 1.0, ulps = 4)) + || ((order.logxif > 0) && approx_eq!(f64, xif, 1.0, ulps = 4)) + || ((order.logxia > 0) && approx_eq!(f64, xia, 1.0, ulps = 4)) { continue; } diff --git a/pineappl/src/import_subgrid.rs b/pineappl/src/import_subgrid.rs index 637b29b2..d181118a 100644 --- a/pineappl/src/import_subgrid.rs +++ b/pineappl/src/import_subgrid.rs @@ -4,7 +4,7 @@ use super::evolution::EVOLVE_INFO_TOL_ULPS; use super::interpolation::Interp; use super::packed_array::PackedArray; use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; -use float_cmp::approx_eq; +use float_cmp::{approx_eq, assert_approx_eq}; use itertools::izip; use serde::{Deserialize, Serialize}; use std::mem; @@ -168,8 +168,8 @@ impl From<&SubgridEnum> for ImportSubgridV1 { .map(|(values, range)| values[range.clone()].to_vec()) .collect(); let static_scale = if let Some(Mu2 { ren, fac, frg }) = subgrid.static_scale() { - assert_eq!(ren, fac); - assert_eq!(frg, -1.0); + assert_approx_eq!(f64, ren, fac, ulps = 4); + assert_approx_eq!(f64, frg, -1.0, ulps = 4); new_node_values[0] = vec![fac]; true } else { diff --git a/pineappl/src/interp_subgrid.rs b/pineappl/src/interp_subgrid.rs index afea20e2..d8b1b6a0 100644 --- a/pineappl/src/interp_subgrid.rs +++ b/pineappl/src/interp_subgrid.rs @@ -36,7 +36,9 @@ impl Subgrid for InterpSubgridV1 { let q2 = ntuple[0]; if self.static_q2 == 0.0 { self.static_q2 = q2; - } else if (self.static_q2 != -1.0) && !approx_eq!(f64, self.static_q2, q2, ulps = 4) { + } else if !approx_eq!(f64, self.static_q2, -1.0, ulps = 4) + && !approx_eq!(f64, self.static_q2, q2, ulps = 4) + { self.static_q2 = -1.0; } } diff --git a/pineappl/src/interpolation.rs b/pineappl/src/interpolation.rs index 0e102c7e..7e47bd77 100644 --- a/pineappl/src/interpolation.rs +++ b/pineappl/src/interpolation.rs @@ -673,7 +673,7 @@ mod tests { let weight = 1.0; interpolate(&interps, &ntuple, weight, &mut array); - assert_eq!(array[[0]], 1.0); + assert_approx_eq!(f64, array[[0]], 1.0, ulps = 2); let node_values = interps[0].node_values(); diff --git a/pineappl/src/pids.rs b/pineappl/src/pids.rs index 42c8d889..3c99904e 100644 --- a/pineappl/src/pids.rs +++ b/pineappl/src/pids.rs @@ -1,6 +1,7 @@ //! TODO use super::boc::Channel; +use float_cmp::approx_eq; use serde::{Deserialize, Serialize}; use std::str::FromStr; use thiserror::Error; @@ -418,7 +419,7 @@ pub fn pdg_mc_ids_to_evol(tuples: &[(i32, f64)]) -> Option { .collect(); if let &[(pid, factor)] = non_zero.as_slice() { - if factor == 1.0 { + if approx_eq!(f64, factor, 1.0, ulps = 4) { return Some(pid); } } diff --git a/pineappl_cli/src/diff.rs b/pineappl_cli/src/diff.rs index a202778c..9668d716 100644 --- a/pineappl_cli/src/diff.rs +++ b/pineappl_cli/src/diff.rs @@ -183,10 +183,18 @@ impl Subcommand for Opts { let result1 = result1 * self.scale1; let result2 = result2 * self.scale2; + // ALLOW: here we really need an exact comparison + // TODO: change allow to `expect` if MSRV >= 1.81.0 + #[allow(clippy::float_cmp)] + let diff = if result1 == result2 { + 0.0 + } else { + result2 / result1 - 1.0 + }; + row.add_cell(cell!(r->format!("{:.*e}", self.digits_abs, result1))); row.add_cell(cell!(r->format!("{:.*e}", self.digits_abs, result2))); - row.add_cell(cell!(r->format!("{:.*e}", self.digits_rel, - if result1 == result2 { 0.0 } else { result2 / result1 - 1.0 }))); + row.add_cell(cell!(r->format!("{:.*e}", self.digits_rel, diff))); } } else { let orders = orders1; @@ -242,10 +250,19 @@ impl Subcommand for Opts { for (result1, result2) in order_results1.iter().zip(order_results2.iter()) { let result1 = result1[bin] * self.scale1; let result2 = result2[bin] * self.scale2; + + // ALLOW: here we really need an exact comparison + // TODO: change allow to `expect` if MSRV >= 1.81.0 + #[allow(clippy::float_cmp)] + let diff = if result1 == result2 { + 0.0 + } else { + result2 / result1 - 1.0 + }; + row.add_cell(cell!(r->format!("{:.*e}", self.digits_abs, result1))); row.add_cell(cell!(r->format!("{:.*e}", self.digits_abs, result2))); - row.add_cell(cell!(r->format!("{:.*e}", self.digits_rel, - if result1 == result2 { 0.0 } else { result2 / result1 - 1.0 }))); + row.add_cell(cell!(r->format!("{:.*e}", self.digits_rel, diff))); } } } diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index 73b4adef..9967f5f1 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -584,7 +584,9 @@ impl Subcommand for Opts { .zip(evolved_results.into_iter()) .enumerate() { - // catches the case where both results are zero + // ALLOW: here we really need an exact comparison + // TODO: change allow to `expect` if MSRV >= 1.81.0 + #[allow(clippy::float_cmp)] let rel_diff = if one == two { 0.0 } else { two / one - 1.0 }; if rel_diff.abs() > self.accuracy { diff --git a/pineappl_cli/src/export.rs b/pineappl_cli/src/export.rs index 5fc83ef8..bcb09643 100644 --- a/pineappl_cli/src/export.rs +++ b/pineappl_cli/src/export.rs @@ -185,8 +185,17 @@ impl Subcommand for Opts { // catches the case where both results are zero let rel_diffs: Vec<_> = one .iter() - .zip(two.iter()) - .map(|(a, b)| if a == b { 0.0 } else { b / a - 1.0 }) + .zip(two) + .map(|(&a, &b)| { + // ALLOW: here we really need an exact comparison + // TODO: change allow to `expect` if MSRV >= 1.81.0 + #[allow(clippy::float_cmp)] + if a == b { + 0.0 + } else { + b / a - 1.0 + } + }) .collect(); let max_rel_diff = rel_diffs diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 281e8457..210053b9 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -1,6 +1,6 @@ use anyhow::{bail, Result}; use cxx::{let_cxx_string, UniquePtr}; -use float_cmp::approx_eq; +use float_cmp::{approx_eq, assert_approx_eq}; use lhapdf::Pdf; use ndarray::{s, Axis}; use pineappl::boc::{Kinematics, Order}; @@ -137,9 +137,9 @@ pub fn convert_into_applgrid( ] .into_iter() .chain(entry.entry().iter().flat_map(|&(ref pids, factor)| { - // TODO: if the factors aren't trivial, we have to find some other way to - // propagate them - assert_eq!(factor, 1.0); + // TODO: if the factors aren't trivial, we have to find some other way + // to propagate them + assert_approx_eq!(f64, factor, 1.0, ulps = 4); pids.iter() .copied() diff --git a/pineappl_cli/src/import.rs b/pineappl_cli/src/import.rs index c6ae680e..0ad38f35 100644 --- a/pineappl_cli/src/import.rs +++ b/pineappl_cli/src/import.rs @@ -300,8 +300,17 @@ impl Subcommand for Opts { // catches the case where both results are zero let rel_diffs: Vec<_> = one .iter() - .zip(two.iter()) - .map(|(a, b)| if a == b { 0.0 } else { b / a - 1.0 }) + .zip(two) + .map(|(&a, &b)| { + // ALLOW: here we really need an exact comparison + // TODO: change allow to `expect` if MSRV >= 1.81.0 + #[allow(clippy::float_cmp)] + if a == b { + 0.0 + } else { + b / a - 1.0 + } + }) .collect(); let max_rel_diff = rel_diffs diff --git a/pineappl_cli/src/import/applgrid.rs b/pineappl_cli/src/import/applgrid.rs index 3ed14833..873a4b41 100644 --- a/pineappl_cli/src/import/applgrid.rs +++ b/pineappl_cli/src/import/applgrid.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use float_cmp::assert_approx_eq; use lhapdf::Pdf; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::{Conv, ConvType}; @@ -127,7 +128,7 @@ pub fn convert_applgrid(grid: Pin<&mut grid>, alpha: u8) -> Result { } // this setting isn't supported - assert_eq!(grid.getDynamicScale(), 0.0); + assert_approx_eq!(f64, grid.getDynamicScale(), 0.0, ulps = 4); let mut grids = Vec::with_capacity(orders.len()); let dis = grid.isDIS(); diff --git a/pineappl_cli/src/import/fastnlo.rs b/pineappl_cli/src/import/fastnlo.rs index 7b8a9697..337fda4b 100644 --- a/pineappl_cli/src/import/fastnlo.rs +++ b/pineappl_cli/src/import/fastnlo.rs @@ -1,6 +1,7 @@ use anyhow::Result; // use itertools::Itertools; // use ndarray::s; +use float_cmp::approx_eq; use pineappl::bin::BinRemapper; use pineappl::boc::{Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::{Conv, ConvType}; @@ -205,7 +206,7 @@ fn convert_coeff_add_fix( for j in 0..table.GetTotalScalevars() { // TODO: for the time being we only extract the central scale result - if table.GetScaleFactor(j) != 1.0 { + if !approx_eq!(f64, table.GetScaleFactor(j), 1.0, ulps = 4) { continue; } diff --git a/pineappl_cli/src/plot.rs b/pineappl_cli/src/plot.rs index b7118010..528facd3 100644 --- a/pineappl_cli/src/plot.rs +++ b/pineappl_cli/src/plot.rs @@ -3,6 +3,7 @@ use super::{GlobalConfiguration, Subcommand}; use anyhow::Result; use clap::builder::{PossibleValuesParser, TypedValueParser}; use clap::{Parser, ValueHint}; +use float_cmp::assert_approx_eq; use itertools::Itertools; use ndarray::Axis; use pineappl::boc::{Channel, Kinematics}; @@ -638,7 +639,7 @@ impl Subcommand for Opts { for (((ix1, ix2), &one), &two) in res1.indexed_iter().zip(res2.iter()) { if one == 0.0 { - assert_eq!(two, 0.0); + assert_approx_eq!(f64, two, 0.0, ulps = 4); continue; } diff --git a/pineappl_cli/tests/import.rs b/pineappl_cli/tests/import.rs index f6b3b93e..2d0a169a 100644 --- a/pineappl_cli/tests/import.rs +++ b/pineappl_cli/tests/import.rs @@ -1,6 +1,7 @@ #![allow(missing_docs)] use assert_cmd::Command; +use float_cmp::assert_approx_eq; #[cfg(any(feature = "applgrid", feature = "fastnlo", feature = "fktable"))] use assert_fs::NamedTempFile; @@ -730,7 +731,7 @@ fn import_dis_fktable() { // TODO: this should ideally be a unit test, but we need an FK table that we don't convert assert_eq!(fk_table.grid().kinematics().len(), 2); - assert_eq!(fk_table.muf2(), 1.65 * 1.65); + assert_approx_eq!(f64, fk_table.muf2(), 1.65 * 1.65, ulps = 2); assert_eq!( fk_table.x_grid(), [ @@ -1000,7 +1001,7 @@ fn import_hadronic_fktable() { [115, 115] ] ); - assert_eq!(fk_table.muf2(), 1.65 * 1.65); + assert_approx_eq!(f64, fk_table.muf2(), 1.65 * 1.65, ulps = 2); assert_eq!( fk_table.x_grid(), [ From 06e16b9d55257f0ec3b32278d442900ff2097962 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 13:56:29 +0200 Subject: [PATCH 212/213] Fix compilation error with no features --- pineappl_cli/src/evolve.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pineappl_cli/src/evolve.rs b/pineappl_cli/src/evolve.rs index 9967f5f1..968bcb1e 100644 --- a/pineappl_cli/src/evolve.rs +++ b/pineappl_cli/src/evolve.rs @@ -474,7 +474,7 @@ fn evolve_grid( _: &Grid, _: &[&Path], _: &Pdf, - _: &[(u32, u32)], + _: &[(u8, u8)], _: f64, _: f64, _: f64, From 6f8311882f4628ee83cae9d064b5eb9a80457662 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 22 Oct 2024 13:57:31 +0200 Subject: [PATCH 213/213] Add some cosmetic fixes --- pineappl/src/grid.rs | 22 ++++++++++------------ pineappl/src/subgrid.rs | 18 ------------------ pineappl_cli/src/export/applgrid.rs | 2 +- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/pineappl/src/grid.rs b/pineappl/src/grid.rs index 8e6f3088..f7d40866 100644 --- a/pineappl/src/grid.rs +++ b/pineappl/src/grid.rs @@ -250,9 +250,9 @@ impl Grid { for ((ord, bin, chan), subgrid) in self.subgrids.indexed_iter() { let order = &self.orders[ord]; - if ((order.logxir > 0) && approx_eq!(f64, xir, 1.0, ulps = 4)) - || ((order.logxif > 0) && approx_eq!(f64, xif, 1.0, ulps = 4)) - || ((order.logxia > 0) && approx_eq!(f64, xia, 1.0, ulps = 4)) + if ((order.logxir != 0) && approx_eq!(f64, xir, 1.0, ulps = 4)) + || ((order.logxif != 0) && approx_eq!(f64, xif, 1.0, ulps = 4)) + || ((order.logxia != 0) && approx_eq!(f64, xia, 1.0, ulps = 4)) { continue; } @@ -288,15 +288,15 @@ impl Grid { value += lumi * v; } - if order.logxir > 0 { + if order.logxir != 0 { value *= (xir * xir).ln().powi(order.logxir.into()); } - if order.logxif > 0 { + if order.logxif != 0 { value *= (xif * xif).ln().powi(order.logxif.into()); } - if order.logxia > 0 { + if order.logxia != 0 { value *= (xia * xia).ln().powi(order.logxia.into()); } @@ -356,15 +356,15 @@ impl Grid { array[idx.as_slice()] = lumi * value; } - if order.logxir > 0 { + if order.logxir != 0 { array *= (xir * xir).ln().powi(order.logxir.into()); } - if order.logxif > 0 { + if order.logxif != 0 { array *= (xif * xif).ln().powi(order.logxif.into()); } - if order.logxia > 0 { + if order.logxia != 0 { array *= (xia * xia).ln().powi(order.logxia.into()); } @@ -1466,7 +1466,7 @@ impl Grid { for channel in &mut self.channels { *channel = self_pid_basis.translate(pid_basis, channel.clone()); } - self.pid_basis = pid_basis; + *self.pid_basis_mut() = pid_basis; } /// Deletes channels with the corresponding `channel_indices`. Repeated indices and indices @@ -1886,8 +1886,6 @@ mod tests { assert_eq!(grid.orders().len(), 1); } - // TODO: convolve_subgrid, merge_bins, subgrid, set_subgrid - #[test] fn grid_convolutions() { let mut grid = Grid::new( diff --git a/pineappl/src/subgrid.rs b/pineappl/src/subgrid.rs index 438a3210..027bbd8d 100644 --- a/pineappl/src/subgrid.rs +++ b/pineappl/src/subgrid.rs @@ -10,7 +10,6 @@ use enum_dispatch::enum_dispatch; use super::interpolation::Interp; use serde::{Deserialize, Serialize}; - /// Enum which lists all possible `Subgrid` variants possible. #[enum_dispatch(Subgrid)] #[derive(Clone, Deserialize, Serialize)] @@ -90,23 +89,6 @@ pub trait Subgrid { fn static_scale(&self) -> Option; } -// // this is needed in the Python interface -// impl From<&SubgridEnum> for Array3 { -// fn from(subgrid: &SubgridEnum) -> Self { -// let mut result = Self::zeros(( -// subgrid.mu2_grid().len(), -// subgrid.x1_grid().len(), -// subgrid.x2_grid().len(), -// )); -// -// for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { -// result[[imu2, ix1, ix2]] = value; -// } -// -// result -// } -// } - /// Type to iterate over the non-zero contents of a subgrid. The tuple contains the indices of the /// `mu2_grid`, the `x1_grid` and finally the `x2_grid`. pub type SubgridIndexedIter<'a> = Box, f64)> + 'a>; diff --git a/pineappl_cli/src/export/applgrid.rs b/pineappl_cli/src/export/applgrid.rs index 210053b9..48293582 100644 --- a/pineappl_cli/src/export/applgrid.rs +++ b/pineappl_cli/src/export/applgrid.rs @@ -119,7 +119,7 @@ pub fn convert_into_applgrid( } let lumis = grid.channels().len(); - let has_pdf1 = grid.convolutions().first().is_some(); + let has_pdf1 = !grid.convolutions().is_empty(); let has_pdf2 = grid.convolutions().get(1).is_some(); // TODO: check that PDG MC IDs are used