Skip to content

Commit 1829aa5

Browse files
Merge pull request #337 from NNPDF/apfelxx-capi
Exposes more functions to C-API for APFEL++
2 parents 0555ab9 + 9d6f897 commit 1829aa5

File tree

9 files changed

+284
-13
lines changed

9 files changed

+284
-13
lines changed

examples/cpp/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PROGRAMS = \
1313
advanced-filling \
1414
convolve-grid-deprecated \
1515
convolve-grid \
16+
get-subgrids \
1617
deprecated \
1718
display-channels-deprecated \
1819
display-channels \
@@ -47,6 +48,9 @@ convolve-grid: convolve-grid.cpp
4748
deprecated: deprecated.cpp
4849
$(CXX) $(CXXFLAGS) $< $(LHAPDF_DEPS) $(PINEAPPL_DEPS) -o $@
4950

51+
get-subgrids: get-subgrids.cpp
52+
$(CXX) $(CXXFLAGS) $< $(PINEAPPL_DEPS) -o $@
53+
5054
display-channels-deprecated: display-channels-deprecated.cpp
5155
$(CXX) $(CXXFLAGS) $< $(PINEAPPL_DEPS) -o $@
5256

examples/cpp/advanced-convolution.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,14 @@ int main(int argc, char* argv[]) {
123123
<< normalizations.at(bin) << '\n';
124124
}
125125

126+
// modify the particle ID representation of the Grid
127+
pineappl_pid_basis ref_pid_repr_basis = pineappl_grid_pid_basis(grid);
128+
assert(ref_pid_repr_basis == PINEAPPL_PID_BASIS_EVOL);
129+
130+
pineappl_grid_rotate_pid_basis(grid, PINEAPPL_PID_BASIS_PDG);
131+
132+
pineappl_pid_basis mod_pid_repr_basis = pineappl_grid_pid_basis(grid);
133+
assert(mod_pid_repr_basis == PINEAPPL_PID_BASIS_PDG);
134+
126135
pineappl_grid_delete(grid);
127136
}

examples/cpp/advanced-filling.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ int main() {
158158
}
159159
//-----------------------------------------------------------------------//
160160

161+
// Check that the Grid contains an empty subgrid at (b, o, c)=(0, 0, 0)
162+
auto subgrid_dim = pineappl_grid_kinematics_len(grid);
163+
std::vector<std::size_t> subgrid_shape(subgrid_dim);
164+
std::vector<std::size_t> zero_vector(subgrid_dim, 0);
165+
pineappl_grid_subgrid_shape(grid, 0, 0, 0, subgrid_shape.data());
166+
assert(subgrid_shape == zero_vector);
167+
168+
// Check that a call to an empty subgrid does not panic
169+
std::vector<double> subgrid_array(0);
170+
pineappl_grid_subgrid_array(grid, 0, 0, 0, subgrid_array.data());
171+
161172
pineappl_grid_write(grid, "advanced-filling.pineappl.lz4");
162173

163174
// release memory

examples/cpp/display-channels.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ int main(int argc, char* argv[]) {
2222
// read the grid from a file
2323
auto* grid = pineappl_grid_read(filename.c_str());
2424

25+
// How many convolutions are there?
26+
auto n_conv = pineappl_grid_convolutions_len(grid);
27+
2528
// extract all channels
2629
auto* channels = pineappl_grid_channels(grid);
2730

@@ -36,7 +39,7 @@ int main(int argc, char* argv[]) {
3639
auto combinations = pineappl_channels_combinations(channels, channel);
3740

3841
std::vector<double> factors(combinations);
39-
std::vector<int> pids(2 * combinations);
42+
std::vector<int> pids(n_conv * combinations);
4043

4144
// read out the channel with index given by `channel`, writing the particle identifiers into
4245
// `pids` and the corresponding factors into `factors`

examples/cpp/get-subgrids.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <pineappl_capi.h>
2+
3+
#include <cassert>
4+
#include <cstddef>
5+
#include <iomanip>
6+
#include <ios>
7+
#include <iostream>
8+
#include <sstream>
9+
#include <string>
10+
#include <vector>
11+
#include <numeric>
12+
13+
14+
std::vector<std::size_t> unravel_index(std::size_t flat_index, const std::vector<std::size_t>& shape) {
15+
std::size_t ndim = shape.size();
16+
std::vector<std::size_t> coords(ndim);
17+
18+
for (int i = ndim - 1; i >= 0; --i) {
19+
coords[i] = flat_index % shape[i];
20+
flat_index /= shape[i];
21+
}
22+
23+
return coords;
24+
}
25+
26+
std::string coords_to_string(const std::vector<std::size_t>& coords) {
27+
std::ostringstream osstream;
28+
29+
osstream << "(";
30+
for (std::size_t i = 0; i < coords.size(); ++i) {
31+
osstream << coords[i];
32+
if (i != coords.size() - 1) {
33+
osstream << ", ";
34+
}
35+
}
36+
osstream << ")";
37+
38+
return osstream.str();
39+
}
40+
41+
42+
int main() {
43+
std::string filename = "drell-yan-rap-ll.pineappl.lz4";
44+
45+
// read the grid from a file
46+
auto* grid = pineappl_grid_read(filename.c_str());
47+
48+
// Determine the number of bins and the index of order and channel
49+
std::size_t n_bins = pineappl_grid_bin_count(grid);
50+
std::size_t order = 0;
51+
std::size_t channel = 0;
52+
53+
// Get the dimension of the subgrids
54+
std::size_t subgrid_dim = pineappl_grid_kinematics_len(grid);
55+
56+
std::cout << std::right << std::setw(10) << "bin" << std::setw(10) << "sg idx"
57+
<< std::setw(6 * subgrid_dim) << "sg coordinates" << std::setw(16)
58+
<< "sg value" << "\n";
59+
std::cout << std::right << std::setw(10) << "---" << std::setw(10) << "------"
60+
<< std::setw(6 * subgrid_dim) << "--------------" << std::setw(16)
61+
<< "------------" << "\n";
62+
63+
for (std::size_t b = 0; b < n_bins; ++b) {
64+
std::vector<std::size_t> subgrid_shape(subgrid_dim);
65+
pineappl_grid_subgrid_shape(grid, b, order, channel, subgrid_shape.data());
66+
67+
// Check if the subgrid is not empty
68+
if (subgrid_shape[0] != 0) {
69+
std::size_t flat_shape = std::accumulate(subgrid_shape.begin(),
70+
subgrid_shape.end(), 1, std::multiplies<std::size_t>());
71+
std::vector<double> subgrid_array(flat_shape);
72+
73+
pineappl_grid_subgrid_array(grid, b, order, channel, subgrid_array.data());
74+
75+
for (std::size_t index = 0; index < subgrid_array.size(); ++index) {
76+
if (subgrid_array[index] != 0) {
77+
// Unravel the index to recover the standard coordinates
78+
std::vector<std::size_t> coords = unravel_index(index, subgrid_shape);
79+
80+
std::cout << std::right << std::setw(10) << b << std::setw(10)
81+
<< index << std::setw(6 * subgrid_dim) << coords_to_string(coords)
82+
<< std::setw(16) << subgrid_array[index] << "\n";
83+
84+
// Compare to some reference value
85+
if (b==0 && index==41020) {
86+
assert(subgrid_array[index] == -4.936156925096015e-07);
87+
}
88+
break;
89+
}
90+
}
91+
}
92+
}
93+
94+
pineappl_grid_delete(grid);
95+
}

examples/cpp/output

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,32 @@ idx left right dsig/dx dx
146146
21 2.1 2.2 3.507294e-02 0.1
147147
22 2.2 2.3 1.976542e-02 0.1
148148
23 2.3 2.4 5.590552e-03 0.1
149+
bin sg idx sg coordinates sg value
150+
--- ------ -------------- ------------
151+
0 41020 (16, 20, 20) -4.93616e-07
152+
1 41020 (16, 20, 20) -4.5499e-08
153+
2 40971 (16, 19, 21) -4.10008e-08
154+
3 40971 (16, 19, 21) -2.78597e-07
155+
4 40971 (16, 19, 21) -2.01924e-07
156+
5 40922 (16, 18, 22) -3.39322e-10
157+
6 40922 (16, 18, 22) -8.43358e-08
158+
7 40922 (16, 18, 22) -2.99247e-07
159+
8 40922 (16, 18, 22) -1.5117e-07
160+
9 40872 (16, 17, 22) -9.95013e-11
161+
10 40873 (16, 17, 23) -1.30578e-07
162+
11 40873 (16, 17, 23) -2.55869e-07
163+
12 40823 (16, 16, 23) -1.0823e-09
164+
13 40823 (16, 16, 23) -4.22686e-09
165+
14 40824 (16, 16, 24) -1.68149e-07
166+
15 40774 (16, 15, 24) -7.58702e-10
167+
16 40774 (16, 15, 24) -2.41887e-08
168+
17 40774 (16, 15, 24) -8.83139e-09
169+
18 40725 (16, 14, 25) -1.14932e-09
170+
19 40725 (16, 14, 25) -3.03857e-08
171+
20 40725 (16, 14, 25) -2.66414e-08
172+
21 40675 (16, 13, 25) -3.60243e-10
173+
22 40676 (16, 13, 26) -1.22785e-08
174+
23 40626 (16, 12, 26) -7.04493e-11
149175
idx p-p c#0 l#0 p-p~ c#0 l# p-d c#0 l#0 p-d dx
150176
--- ------------ ----------- ------------- ------------ ------
151177
0 5.263109e-01 5.263109e-01 5.263109e-01 5.263109e-01 0.1

pineappl/src/packed_array.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ impl<T: Copy + Default + PartialEq> From<ArrayViewD<'_, T>> for PackedArray<T> {
171171
}
172172

173173
/// Converts a `multi_index` into a flat index.
174-
fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize {
174+
///
175+
/// # Panics
176+
///
177+
/// TODO
178+
#[must_use]
179+
pub fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize {
175180
assert_eq!(multi_index.len(), shape.len());
176181

177182
multi_index

pineappl/src/subgrid.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Subgrid for EmptySubgridV1 {
3333
Vec::new()
3434
}
3535

36-
fn shape(&mut self) -> &[usize] {
36+
fn shape(&self) -> &[usize] {
3737
panic!("EmptySubgridV1 doesn't have a shape");
3838
}
3939

@@ -166,7 +166,7 @@ impl Subgrid for ImportSubgridV1 {
166166
Box::new(self.array.indexed_iter())
167167
}
168168

169-
fn shape(&mut self) -> &[usize] {
169+
fn shape(&self) -> &[usize] {
170170
self.array.shape()
171171
}
172172

@@ -275,7 +275,7 @@ impl Subgrid for InterpSubgridV1 {
275275
self.array.is_empty()
276276
}
277277

278-
fn shape(&mut self) -> &[usize] {
278+
fn shape(&self) -> &[usize] {
279279
self.array.shape()
280280
}
281281

@@ -471,7 +471,7 @@ pub trait Subgrid {
471471
fn scale(&mut self, factor: f64);
472472

473473
/// Return the shape of the subgrid
474-
fn shape(&mut self) -> &[usize];
474+
fn shape(&self) -> &[usize];
475475

476476
/// Assume that the convolution functions for indices `a` and `b` for this grid are the same
477477
/// and use this to optimize the size of the grid.

0 commit comments

Comments
 (0)