Rust API Examples
This page provides a hands-on tutorial on how to use the native Rust API of NeoPDF
to load, inspect, and interpolate PDF sets.
Prerequisites
In order to start using the native Rust API, first add the crate dependency in your
Cargo.toml:
Example 1: Loading and Evaluating a Single PDF Member
In this first example, we load a single PDF member from an LHAPDF or NeoPDF grid and evaluate the gluon distribution \(x f(x, Q^2)\) for a range of \(x\) and \(Q^2\).
Key points:
PDF::loadautomatically detects whether the set is LHAPDF (NNPDF40_nnlo_as_01180) or NeoPDF (NNPDF40_nnlo_as_01180.neopdf.lz4) and uses the appropriate backend.xfxq2(pid, &[x, q2])evaluates \(x f(x, Q^2)\) at the requested kinematic point.
Example 2: Evaluating Multiple Flavors at Once
When evaluating PDFs for several flavors at the same kinematic point, it is more
efficient to reuse the subgrid lookup and interpolation. The xfxq2_allpids method
does exactly this.
Example 3: Batch Evaluation with xfxq2s
For various analyses, one often needs values at many kinematic points. The xfxq2s
method computes a full 2D array of values for multiple flavors and many points in
a single call.
Example 4: Lazy Loading of NeoPDF Grids
For large .neopdf.lz4 grids with many members, you may not want to load everything
into memory at once. The load_pdfs_lazy constructor exposes an iterator that yields
PDF members on demand.
Example 5: Inspecting Metadata and Subgrids
Beyond simple interpolation, the Rust API allows you to inspect the metadata and
the internal subgrid structure, similar to what the CLI read commands expose.
This is the Rust equivalent of running commands such as:
neopdf read metadata NNPDF40_nnlo_as_01180
neopdf read num_subgrids NNPDF40_nnlo_as_01180
neopdf read subgrid-info NNPDF40_nnlo_as_01180 --member=0 --subgrid-index=0
Example 6: Controlling Positivity Clipping
The Rust API also exposes a method for positivity clipping of a given grid.
Example 7: Filling and Writing a NeoPDF Grid (LHAPDF-like)
The following example illustrates how one can create a NeoPDF grid from scratch.
We do so by loading the values from an existing LHAPDF set and create a genuine
compressed NeoPDF grid with the extension .neopdf.lz4.
NOTE
As in the C/C++/Fortran tutorials, this example recomputes the values of the subgrids from an existing set to validate the writer. This makes the code verbose; the lines that actually fill the grid are highlighted.
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 | |