Skip to content

Commit

Permalink
Removee more x{1,2}_grid calls
Browse files Browse the repository at this point in the history
  • Loading branch information
t7phy committed Sep 24, 2024
1 parent 8012a94 commit ac95f26
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 60 deletions.
42 changes: 35 additions & 7 deletions pineappl/src/convolutions.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)
}
})
Expand Down Expand Up @@ -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, .. }| {
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)| {
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -367,10 +371,9 @@ impl Grid {
frg: -1.0,
})
.collect::<Vec<_>>(),
&node_values[1],
&node_values[2],
xir,
xif,
xia,
);

let dim: Vec<_> = node_values.iter().map(Vec::len).collect();
Expand Down
88 changes: 76 additions & 12 deletions pineappl_cli/src/export/applgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand All @@ -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::<Vec<_>>()
}))
}),
)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -266,13 +277,66 @@ pub fn convert_into_applgrid(
.collect::<Result<_>>()?;

// 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()
Expand Down
28 changes: 25 additions & 3 deletions pineappl_cli/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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![];
Expand Down
38 changes: 16 additions & 22 deletions pineappl_cli/src/subgrids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<usize>,
/// Show grid statistics (figures are the number of entries).
#[arg(long)]
stats: bool,
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit ac95f26

Please sign in to comment.