1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use anyhow::{Result, bail};
use cxx::{UniquePtr, let_cxx_string};
use float_cmp::approx_eq;
use itertools::izip;
use lhapdf::Pdf;
use ndarray::{Axis, s};
use pineappl::boc::{Channel, Kinematics, Order};
use pineappl::grid::Grid;
use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth};
use pineappl::pids::PidBasis;
use pineappl::subgrid::{self, Subgrid};
use pineappl_applgrid::ffi::{self, grid};
use std::f64::consts::TAU;
use std::iter;
use std::path::Path;
use std::pin::Pin;
fn reconstruct_subgrid_params(grid: &Grid, order: usize, bin: usize) -> Result<Vec<Interp>> {
if grid
.kinematics()
.iter()
.filter(|kin| matches!(kin, Kinematics::Scale(_)))
.count()
> 1
{
bail!("APPLgrid does not support grids with more than one scale");
}
let mut mu2_grid: Vec<_> = grid
.subgrids()
.slice(s![order, bin, ..])
.iter()
.filter(|subgrid| !subgrid.is_empty())
.flat_map(|subgrid| {
grid.scales()
.fac
.calc(&subgrid.node_values(), grid.kinematics())
.into_owned()
})
.collect();
mu2_grid.dedup_by(subgrid::node_value_eq_ref_mut);
// 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,
),
);
}
Ok(result)
}
pub fn convert_into_applgrid(
grid: &mut Grid,
output: &Path,
discard_non_matching_values: bool,
) -> Result<(UniquePtr<grid>, Vec<bool>)> {
let dim = grid.bwfl().dimensions();
if dim > 1 {
bail!(
"grid has {dim} dimensions, but APPLgrid only supports one-dimensional distributions"
);
}
// APPLgrid can only be used with one-dimensional consecutive bin limits
if grid.bwfl().slices().len() != 1 {
bail!("grid has non-consecutive bin limits, which APPLgrid does not support");
}
match grid.convolutions() {
[_] => {}
[a, b] => {
if (a != b) && (a.cc() == *b) {
// use charge conjugate to map hadron-anti-hadron grids to use the same single
// convolution function
let index = usize::from(a.pid() < 0);
grid.charge_conjugate(index);
}
}
_ => bail!("APPLgrid does not support grids with more than two convolutions"),
}
// APPLgrid only understands PDG PIDs
grid.rotate_pid_basis(PidBasis::Pdg);
let non_trivial_factors = grid
.channels()
.iter()
.flat_map(Channel::entry)
.any(|&(_, factor)| !approx_eq!(f64, factor, 1.0, ulps = 4));
// APPLgrid doesn't support non-trivial factors
if non_trivial_factors {
// TODO: this isn't the most efficient strategy, since we also split up channels with
// trivial factors
grid.split_channels();
grid.merge_channel_factors();
grid.optimize();
}
let combinations: Vec<_> = iter::once(grid.channels().len().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(|(pids, _)| {
pids.iter()
.copied()
.chain(iter::repeat(0))
.take(2)
.collect::<Vec<_>>()
}))
}),
)
.collect();
// `id` must end with '.config' for APPLgrid to know its type is `lumi_pdf`
let id = format!(
"{}.config",
output
.file_stem()
// UNWRAP: because we write to that file in the end, there always must be a file name
.unwrap()
.to_string_lossy()
);
// this object is managed by APPLgrid internally
ffi::make_lumi_pdf(&id, &combinations).into_raw();
let limits: Vec<_> = grid
.bwfl()
.bins()
.iter()
.map(|bin| {
// TODO: instead of `bin.limits()[0]` we should use `bin.fill_limits()`, but this
// requires changing the normalization
bin.limits()[0].0
})
.chain(Some(
grid.bwfl()
.bins()
.last()
// UNWRAP: every `grid` should have at least one bin
.unwrap()
.limits()[0]
.1,
))
.collect();
let order_mask = Order::create_mask(grid.orders(), 3, 0, false);
let orders_with_mask: Vec<_> = grid
.orders()
.iter()
.cloned()
.zip(order_mask.iter().copied())
.collect();
let lo_alphas = orders_with_mask
.iter()
.filter_map(|&(Order { alphas, .. }, keep)| keep.then_some(alphas))
.min()
.unwrap();
let loops = orders_with_mask
.iter()
.filter_map(|&(Order { alphas, .. }, keep)| keep.then_some(alphas))
.max()
.unwrap()
- lo_alphas;
let mut applgrid =
ffi::make_empty_grid(&limits, &id, lo_alphas.into(), loops.into(), "f2", "h0");
// APPLgrid has either two or one convolution(s)
let convolutions = grid.convolutions().len();
for order in order_mask
.iter()
.enumerate()
.filter_map(|(index, keep)| keep.then_some(index))
{
let appl_order = grid.orders()[order].alphas - lo_alphas;
let factor = TAU.powi(grid.orders()[order].alphas.into());
for (bin, subgrids) in grid
.subgrids()
.index_axis(Axis(0), order)
.axis_iter(Axis(0))
.enumerate()
{
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(
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 @ Map::ApplGridH0 => panic!("export does not support {map:?}"),
},
match interps[0].map() {
Map::ApplGridH0 => "h0",
map @ Map::ApplGridF2 => panic!("export does not support {map:?}"),
},
grid.channels().len().try_into().unwrap(),
grid.convolutions().len() == 1,
);
let appl_grids: Vec<Vec<_>> = vec![
(0..igrid.Ntau()).map(|i| igrid.getQ2(i)).collect(),
(0..igrid.Ny1()).map(|i| igrid.getx1(i)).collect(),
(0..igrid.Ny2()).map(|i| igrid.getx2(i)).collect(),
];
for (channel, subgrid) in subgrids
.iter()
.enumerate()
.filter(|(_, subgrid)| !subgrid.is_empty())
{
let grids = vec![
grid.scales()
.fac
.calc(&subgrid.node_values(), grid.kinematics())
.into_owned(),
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(),
if convolutions == 2 {
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()
} else {
Vec::new()
},
];
let appl_idx: Vec<Vec<_>> = izip!(&grids, &appl_grids, ["factorization scale muf2", "momentum fraction x1", "momentum fraction x2"])
.map(|(grid, appl_grid, label)| {
grid
.iter()
.map(|&value| {
appl_grid
.iter()
.position(|&appl_value| subgrid::node_value_eq(appl_value, value))
.map_or_else(
|| {
if discard_non_matching_values {
Ok(-1)
} else {
bail!("{label} = {value} not found in APPLgrid; try exporting with `--discard-non-matching-values`")
}
},
|idx| Ok(idx.try_into().unwrap()),
)
})
.collect::<Result<_>>()
}
).collect::<Result<_>>()?;
let mut weightgrid = ffi::igrid_weightgrid(igrid.pin_mut(), channel);
'looop: for (indices, value) in subgrid.indexed_iter() {
// TODO: here we assume that all X are consecutive starting from the second
// element and are in ascending order
let appl_indices = [
appl_idx[0][indices[0]],
appl_idx[1][indices[1]],
if convolutions == 2 {
appl_idx[2][indices[2]]
} else {
0
},
];
for (&appl_index, grid, &index, label) in izip!(
&appl_indices,
&grids,
&indices,
["scale muf2", "momentum fraction x1", "momentum fraction x2"]
) {
if appl_index == -1 {
if value != 0.0 {
println!(
"WARNING: discarding non-matching {label} = {} in subgrid {:?}",
grid[index],
(order, bin, channel)
);
}
continue 'looop;
}
}
ffi::sparse_matrix_set(
weightgrid.as_mut(),
appl_indices[0],
appl_indices[1],
appl_indices[2],
factor * value,
);
}
// TODO: is this call needed?
weightgrid.trim();
}
igrid.pin_mut().setlimits();
unsafe {
applgrid.pin_mut().add_igrid(
bin.try_into().unwrap(),
appl_order.into(),
igrid.into_raw(),
);
}
}
}
applgrid.pin_mut().include_photon(true);
let_cxx_string!(filename = output.to_str().unwrap());
let_cxx_string!(empty = "");
applgrid.pin_mut().Write(&filename, &empty, &empty);
Ok((applgrid, order_mask))
}
// TODO: deduplicate this function from import
pub fn convolve_applgrid(grid: Pin<&mut grid>, conv_funs: &mut [Pdf]) -> Vec<f64> {
let nloops = grid.nloops();
// TODO: add support for convolving an APPLgrid with two functions
assert_eq!(conv_funs.len(), 1);
pineappl_applgrid::grid_convolve_with_one(grid, &mut conv_funs[0], nloops, 1.0, 1.0, 1.0)
}