Skip to content

Commit

Permalink
Merge pull request #41 from davidepettinari/davide24
Browse files Browse the repository at this point in the history
Make openmc an optional dependency #24
  • Loading branch information
RemDelaporteMathurin authored Dec 20, 2024
2 parents ec00e0f + 9bc5f34 commit 4a5ec5a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,7 @@

## Installation

`libra-toolbox` relies on Python. You can install it from the official website: [python.org](https://www.python.org/downloads/).

Install OpenMC:

If you want to run OpenMC functions, [install OpenMC](https://docs.openmc.org/en/stable/quickinstall.html) with conda:

```
conda install -c conda-forge openmc>=0.14.0
```


To install `libra-toolbox`, use pip:

```bash
pip install git+https://github.com/LIBRA-project/libra-toolbox
```
You can find the [installation instructions](https://libra-toolbox.readthedocs.io/en/latest/installation.html#installation) in the documentation.

## Documentation
The documentation for `libra-toolbox` is built using Sphinx and is [available online](https://libra-toolbox.readthedocs.io/en/latest/). To build the documentation locally, you can use the provided Makefile or make.bat script.
Expand Down
14 changes: 14 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ To install a specific version of the code:
Here, ``v0.1`` is the version number. You can replace it with the version you want to install.

To install the code in editable mode (i.e. the code is installed in the current directory and any changes to the code are immediately available to the user):

.. code-block:: bash
git clone https://github.com/LIBRA-project/libra-toolbox
cd libra-toolbox
pip install -e .
To install the code alongside with the optional dependencies for the neutronics module, first [install OpenMC](https://docs.openmc.org/en/stable/quickinstall.html) with conda:

```
conda install -c conda-forge openmc>=0.14.0
```

To uninstall the package:

.. code-block:: bash
Expand Down
24 changes: 18 additions & 6 deletions libra_toolbox/neutronics/neutron_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
from collections.abc import Iterable
import pandas as pd
import numpy as np
import h5py
import openmc

try:
import h5py
import openmc
from openmc import IndependentSource
except ModuleNotFoundError:
pass

def A325_generator_diamond(center=(0, 0, 0), reference_uvw=(0, 0, 1)) -> Iterable[openmc.IndependentSource]:

def A325_generator_diamond(
center=(0, 0, 0), reference_uvw=(0, 0, 1)
) -> "Iterable[IndependentSource]":
"""
Builds the MIT-VaultLab A-325 neutron generator in OpenMC
with data tabulated from John Ball and Shon Mackie characterization
Expand All @@ -28,15 +35,20 @@ def A325_generator_diamond(center=(0, 0, 0), reference_uvw=(0, 0, 1)) -> Iterabl
Returns
-------
list of openmc neutron sources with angular and energy distribution
list of openmc neutron sources with angular and energy distribution
and total strength of 1
"""
try:
import h5py
import openmc
except ModuleNotFoundError:
raise ModuleNotFoundError("openmc and h5py are required")

filename = "A325_generator_diamond.h5"
filename = str(Path(__file__).parent) / Path(filename)

with h5py.File(filename, "r") as source:
df = pd.DataFrame(source["values/table"][()]).drop(columns='index')
df = pd.DataFrame(source["values/table"][()]).drop(columns="index")
# energy values
energies = np.array(df["Energy (MeV)"]) * 1e6
# angle column names
Expand Down Expand Up @@ -64,7 +76,7 @@ def A325_generator_diamond(center=(0, 0, 0), reference_uvw=(0, 0, 1)) -> Iterabl
)
strength = yields[i]

my_source = openmc.Source(
my_source = openmc.IndependentSource(
space=space,
angle=angle,
energy=energy,
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ description = "Design and analysis tools for LIBRA project"
license = {file = "LICENSE"}
requires-python = ">=3.6"
dynamic = ["version"]
dependencies = ["numpy", "pint", "scipy", "matplotlib", "sympy", "pandas", "openmc>=0.14.0", "h5py"]
dependencies = ["numpy", "pint", "scipy", "matplotlib", "sympy", "pandas"]

[project.optional-dependencies]
neutronics = ["openmc>=0.14.0", "h5py"]
tests = ["pytest>=5.4.3", "pytest-cov", "nbconvert", "ipykernel"]

[tool.setuptools_scm]
Expand Down
11 changes: 9 additions & 2 deletions test/neutronics/test_neutron_source.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from libra_toolbox.neutronics.neutron_source import *
from collections.abc import Iterable
from libra_toolbox.neutronics.neutron_source import A325_generator_diamond

import pytest


def test_get_avg_neutron_rate():
try:
import openmc
except ImportError:
pytest.skip("OpenMC is not installed")

source = A325_generator_diamond((0, 0, 0), (0, 0, 1))

assert isinstance(source, Iterable)
for s in source:
assert isinstance(s, openmc.IndependentSource)
assert isinstance(s, openmc.IndependentSource)

0 comments on commit 4a5ec5a

Please sign in to comment.