-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preliminary Python interface to
packed_subgrid
- Loading branch information
1 parent
b140e65
commit e32f543
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! PyPackedSubgrid* interface. | ||
use super::subgrid::PySubgridEnum; | ||
use numpy::PyReadonlyArray3; | ||
use pineappl::packed_array::PackedArray; | ||
use pineappl::packed_subgrid::PackedQ1X2SubgridV1; | ||
use pineappl::subgrid::NodeValues; | ||
use pyo3::prelude::*; | ||
|
||
/// PyO3 wrapper to :rustdoc:`pineappl`. | ||
#[pyclass(name = "PyPackedSubgrid")] | ||
#[derive(Clone)] | ||
#[repr(transparent)] | ||
pub struct PyPackedSubgrid { | ||
pub(crate) packed_subgrid: PackedQ1X2SubgridV1, | ||
} | ||
|
||
#[pymethods] | ||
impl PyPackedSubgrid { | ||
/// Constructor. | ||
/// Constructor. | ||
/// | ||
/// Parameters | ||
/// ---------- | ||
/// array : numpy.ndarray(float) | ||
/// 3D array with all weights | ||
/// scales : list(float) | ||
/// scales grid | ||
/// x1_grid : list(float) | ||
/// first momentum fraction grid | ||
/// x2_grid : list(float) | ||
/// second momentum fraction grid | ||
#[new] | ||
pub fn new( | ||
array: PyReadonlyArray3<f64>, | ||
scales: Vec<f64>, | ||
x1_grid: Vec<f64>, | ||
x2_grid: Vec<f64>, | ||
) -> Self { | ||
let node_values: Vec<NodeValues> = vec![ | ||
NodeValues::UseThese(scales), | ||
NodeValues::UseThese(x1_grid), | ||
NodeValues::UseThese(x2_grid), | ||
]; | ||
let mut sparse_array: PackedArray<f64> = | ||
PackedArray::new(node_values.iter().map(NodeValues::len).collect()); | ||
|
||
for ((iscale, ix1, ix2), value) in array | ||
.as_array() | ||
.indexed_iter() | ||
.filter(|((_, _, _), value)| **value != 0.0) | ||
{ | ||
sparse_array[[iscale, ix1, ix2]] = *value; | ||
} | ||
|
||
Self { | ||
packed_subgrid: PackedQ1X2SubgridV1::new(sparse_array, node_values), | ||
} | ||
} | ||
|
||
/// TODO | ||
#[must_use] | ||
pub fn into(&self) -> PySubgridEnum { | ||
PySubgridEnum { | ||
subgrid_enum: self.packed_subgrid.clone().into(), | ||
} | ||
} | ||
} | ||
|
||
/// Register submodule in parent. | ||
/// # Errors | ||
/// | ||
/// Raises an error if (sub)module is not found. | ||
pub fn register(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { | ||
let m = PyModule::new_bound(parent_module.py(), "packed_subgrid")?; | ||
m.setattr( | ||
pyo3::intern!(m.py(), "__doc__"), | ||
"Interface for packed subgrid specs.", | ||
)?; | ||
pyo3::py_run!( | ||
parent_module.py(), | ||
m, | ||
"import sys; sys.modules['pineappl.packed_subgrid'] = m" | ||
); | ||
m.add_class::<PyPackedSubgrid>()?; | ||
parent_module.add_submodule(&m) | ||
} |