Skip to content

Commit

Permalink
Remove Grid::with_subgrid_type constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Sep 11, 2024
1 parent 0dcd118 commit c677a63
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 162 deletions.
49 changes: 0 additions & 49 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel>,
orders: Vec<Order>,
bin_limits: Vec<f64>,
interps: Vec<Interp>,
subgrid_type: &str,
) -> Result<Self, GridError> {
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 {
Expand Down Expand Up @@ -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(
Expand Down
92 changes: 34 additions & 58 deletions pineappl/tests/drell_yan_lo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Grid> {
Expand Down Expand Up @@ -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,
Expand All @@ -179,6 +183,7 @@ fn fill_drell_yan_lo_grid(
Map::ApplGridH0,
InterpMeth::Lagrange,
),
// 2nd dimension interpolation parameters
Interp::new(
2e-7,
1.0,
Expand All @@ -188,6 +193,7 @@ fn fill_drell_yan_lo_grid(
Map::ApplGridF2,
InterpMeth::Lagrange,
),
// 3rd dimension interpolation parameters
Interp::new(
2e-7,
1.0,
Expand All @@ -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;
Expand Down Expand Up @@ -276,23 +298,18 @@ fn fill_drell_yan_lo_grid(
}

fn perform_grid_tests(
subgrid_type: &str,
dynamic: bool,
reference: &[f64],
reference_after_ssd: &[f64],
x_grid: &[f64],
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);

Expand Down Expand Up @@ -467,9 +484,9 @@ fn perform_grid_tests(
Ok(())
}

fn generate_grid(subgrid_type: &str, dynamic: bool, reweight: bool) -> Result<Grid> {
fn generate_grid(dynamic: bool, reweight: bool) -> Result<Grid> {
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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down
95 changes: 40 additions & 55 deletions pineappl_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<Interp>) {
let mut subgrid_type = "LagrangeSubgrid".to_owned();
fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec<Interp> {
let mut q2_min = 1e2;
let mut q2_max = 1e8;
let mut q2_nodes = 40;
Expand Down Expand Up @@ -191,44 +190,37 @@ fn grid_params(key_vals: Option<&KeyVal>) -> (String, Vec<Interp>) {
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.
Expand Down Expand Up @@ -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 };

Expand All @@ -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`.
Expand Down

0 comments on commit c677a63

Please sign in to comment.