Skip to content

Commit

Permalink
Remove Convolution::None and start fixing the importing of DIS grids
Browse files Browse the repository at this point in the history
  • Loading branch information
t7phy committed Oct 10, 2024
1 parent defd91e commit 461d536
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 175 deletions.
8 changes: 0 additions & 8 deletions pineappl/src/convolutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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),
Expand All @@ -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)),
Expand All @@ -369,7 +364,6 @@ impl Convolution {
#[must_use]
pub const fn pid(&self) -> Option<i32> {
match *self {
Self::None => None,
Self::UnpolPDF(pid) | Self::PolPDF(pid) | Self::UnpolFF(pid) | Self::PolFF(pid) => {
Some(pid)
}
Expand All @@ -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)
Expand All @@ -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));
Expand Down
8 changes: 0 additions & 8 deletions pineappl/src/evolution.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)| {
Expand Down
51 changes: 23 additions & 28 deletions pineappl/src/fk_table.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<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
Expand All @@ -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;
}
}

Expand Down
21 changes: 6 additions & 15 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Self> = None;
// Q2 slices we use
let mut used_op_fac1 = Vec::new();
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 2 additions & 10 deletions pineappl_cli/src/export/applgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Vec<_>>()
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions pineappl_cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
14 changes: 6 additions & 8 deletions pineappl_cli/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ fn convert_applgrid(
input: &Path,
alpha: u32,
conv_funs: &mut [Pdf],
dis_pid: i32,
_: usize,
) -> Result<(&'static str, Grid, Vec<f64>, usize)> {
use pineappl_applgrid::ffi;

// 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))
Expand All @@ -39,7 +38,6 @@ fn convert_applgrid(
_: &Path,
_: u32,
_: &mut [Pdf],
_: i32,
_: usize,
) -> Result<(&'static str, Grid, Vec<f64>, usize)> {
Err(anyhow!(
Expand Down Expand Up @@ -134,14 +132,14 @@ fn convert_fastnlo(
}

#[cfg(feature = "fktable")]
fn convert_fktable(input: &Path, dis_pid: i32) -> Result<(&'static str, Grid, Vec<f64>, usize)> {
let fktable = fktable::convert_fktable(input, dis_pid)?;
fn convert_fktable(input: &Path) -> Result<(&'static str, Grid, Vec<f64>, 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<f64>, usize)> {
fn convert_fktable(_: &Path) -> Result<(&'static str, Grid, Vec<f64>, usize)> {
Err(anyhow!(
"you need to install `pineappl` with feature `fktable`"
))
Expand Down Expand Up @@ -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);
}
}

Expand Down
Loading

0 comments on commit 461d536

Please sign in to comment.