Skip to content

Commit

Permalink
Merge pull request #2 from fusion-energy/adding_tests
Browse files Browse the repository at this point in the history
added first test and CI
  • Loading branch information
shimwell authored Mar 21, 2023
2 parents 40f4641 + 4e1f4b5 commit a0383df
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 28 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: black

on:
push:
paths:
- '**.py'

defaults:
run:
shell: bash

jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.x
- name: Install black
run: |
python -m pip install --upgrade pip
pip install black
- name: Run black
run: |
black .
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "[skip ci] Apply formatting changes"
51 changes: 51 additions & 0 deletions .github/workflows/ci_with_install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This CI will launch a Docker image that contains all the dependencies required
# within that image the pytest test suite is run

name: CI with install

on:
pull_request:
branches:
- develop
- main
paths-ignore:
- 'docs/**'
- '.gitignore'
- '*.md'
- 'CITATION.cff'
- 'LICENSE.txt'
- 'readthedocs.yml'

jobs:
testing:
runs-on: ubuntu-latest
container:
# image: continuumio/miniconda3:4.10.3
image: openmc/openmc:latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

# - name: install dependencies
# run: |
# conda install -c conda-forge mamba
# mamba install -c conda-forge openmc

- name: install package
run: |
pip install --upgrade pip
pip install .
python -c "import cylindrical_mesh_plotter"
- name: install packages for tests
run: |
pip install .[tests]
- name: Run test_utils
run: |
pytest tests
- name: Run examples
run: |
cd examples
python plot_rz_slices.py
40 changes: 40 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This yml file will trigger a Github Actions event that builds and upload the
# Python package to PiPy. This makes use of Twine and is triggered when a push
# to the main branch occures. For more information see:
# https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
# and for details on the Autobump version section see:
# https://github.com/grst/python-ci-versioneer

name: Upload Python Package

on:
# allows us to run workflows manually
workflow_dispatch:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel build twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python -m build
twine check dist/*
twine upload dist/*
34 changes: 15 additions & 19 deletions examples/simple_plot.py → examples/plot_rz_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
import openmc_cylindrical_mesh_plotter # adds slice_of_data method to CylindricalMesh

mesh = openmc.CylindricalMesh()
mesh.phi_grid = np.linspace(0.,2*pi,10)
mesh.r_grid = np.linspace(0,10,4)
mesh.z_grid = np.linspace(0,5,5)
mesh.phi_grid = np.linspace(0.0, 2 * pi, 10)
mesh.r_grid = np.linspace(0, 10, 4)
mesh.z_grid = np.linspace(0, 5, 5)

tally = openmc.Tally(name='my_tally')
tally = openmc.Tally(name="my_tally")
mesh_filter = openmc.MeshFilter(mesh)
tally.filters.append(mesh_filter)
tally.scores.append("flux")
tallies = openmc.Tallies([tally])

outer_surface = openmc.Sphere(r=100, boundary_type='vacuum')
cell = openmc.Cell(region = -outer_surface)
outer_surface = openmc.Sphere(r=100, boundary_type="vacuum")
cell = openmc.Cell(region=-outer_surface)

material = openmc.Material()
material.add_nuclide('Fe56', 1)
material.set_density('g/cm3', 0.1)
material.add_nuclide("Fe56", 1)
material.set_density("g/cm3", 0.1)
my_materials = openmc.Materials([material])

universe = openmc.Universe(cells=[cell])
Expand All @@ -37,17 +37,16 @@
# the distribution of source z values is just a single value
z_values = openmc.stats.Discrete([2.5], [1])
# the distribution of source azimuthal angles values is a uniform distribution between 0 and 2 Pi
angle = openmc.stats.Uniform(a=0., b=2* 3.14159265359)
angle = openmc.stats.Uniform(a=0.0, b=2 * 3.14159265359)
# this makes the ring source using the three distributions and a radius
# could do a point source instead with my_source.space = openmc.stats.Point((5,5., 5))
my_source.space = openmc.stats.CylindricalIndependent(
r=radius, phi=angle,
z=z_values, origin=(0.0, 0.0, 0.0))
r=radius, phi=angle, z=z_values, origin=(0.0, 0.0, 0.0)
)
# sets the direction to isotropic
my_source.angle = openmc.stats.Isotropic()



my_settings = openmc.Settings()
# my_settings.inactive = 0
my_settings.run_mode = "fixed source"
Expand All @@ -60,19 +59,16 @@

statepoint = openmc.StatePoint(sp_filename)

my_tally_result = statepoint.get_tally(name='my_tally')
my_tally_result = statepoint.get_tally(name="my_tally")

mesh.write_data_to_vtk(
filename="my_tally_result.vtk",
datasets={"mean": my_tally_result.mean}
filename="my_tally_result.vtk", datasets={"mean": my_tally_result.mean}
)


for slice_index in range(len(mesh.phi_grid)-1):
for slice_index in range(len(mesh.phi_grid) - 1):
data = mesh.slice_of_data(
dataset=my_tally_result.mean,
slice_index=slice_index,
volume_normalization=True
dataset=my_tally_result.mean, slice_index=slice_index, volume_normalization=True
)
extent = mesh.get_mpl_plot_extent()
x_label, y_label = mesh.get_axis_labels()
Expand Down
43 changes: 37 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
[build-system]
requires = [
"setuptools >= 45",
"wheel",
"setuptools_scm[toml] >= 6.2",
]
requires = ["setuptools >= 65.4.0", "setuptools_scm[toml]>=7.0.5"]
build-backend = "setuptools.build_meta"

[project]
name = "cylindrical_mesh_plotter"
authors = [
{ name="Jonathan Shimwell", email="[email protected]" },
]
license = {file = "LICENSE.txt"}
description = "A Python package for creating publication quality plots of cylindrical mesh tallies"
readme = "README.md"
requires-python = ">=3.8"
keywords = ["cylindrical", "mesh", "openmc", "tally", "plot", "slice"]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"numpy>=1.21.1",
"matplotlib>=3.4.2",
]
dynamic = ["version"]


[tool.setuptools_scm]
write_to = "src/openmc_cylindrical_mesh_plotter/_version.py"
write_to = "src/cylindrical_mesh_plotter/_version.py"


[project.optional-dependencies]
tests = [
"pytest",
]

[project.urls]
"Homepage" = "https://github.com/fusion-energy/cylindrical_mesh_plotter"
"Bug Tracker" = "https://github.com/fusion-energy/cylindrical_mesh_plotter/issues"

[tool.setuptools]
package-dir = {"" = "src"}
2 changes: 1 addition & 1 deletion src/openmc_cylindrical_mesh_plotter/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .core import *
from .core import *
3 changes: 1 addition & 2 deletions src/openmc_cylindrical_mesh_plotter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ def slice_of_data(
slice_index=0,
volume_normalization: bool = True,
):

if volume_normalization:
dataset = dataset.flatten() / self.volumes.T.reshape(-1, 3).flatten()
else:
dataset = dataset.flatten()

data_slice = dataset.T.reshape(-1, 3)

data_slice = data_slice[slice_index::self.dimension[1]]
data_slice = data_slice[slice_index :: self.dimension[1]]

return np.flip(data_slice, axis=0)

Expand Down
72 changes: 72 additions & 0 deletions tests/test_slice_of_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import openmc
import numpy as np
from math import pi
import matplotlib.pyplot as plt
import openmc_cylindrical_mesh_plotter # adds slice_of_data method to CylindricalMesh


def test_slice_of_data():
mesh = openmc.CylindricalMesh()
mesh.phi_grid = np.linspace(0.0, 2 * pi, 10)
mesh.r_grid = np.linspace(0, 10, 4)
mesh.z_grid = np.linspace(0, 5, 5)

tally = openmc.Tally(name="my_tally")
mesh_filter = openmc.MeshFilter(mesh)
tally.filters.append(mesh_filter)
tally.scores.append("flux")
tallies = openmc.Tallies([tally])

outer_surface = openmc.Sphere(r=100, boundary_type="vacuum")
cell = openmc.Cell(region=-outer_surface)

material = openmc.Material()
material.add_nuclide("Fe56", 1)
material.set_density("g/cm3", 0.1)
my_materials = openmc.Materials([material])

universe = openmc.Universe(cells=[cell])
my_geometry = openmc.Geometry(universe)

my_source = openmc.Source()

# the distribution of radius is just a single value
radius = openmc.stats.Discrete([5], [1])
# the distribution of source z values is just a single value
z_values = openmc.stats.Discrete([2.5], [1])
# the distribution of source azimuthal angles values is a uniform distribution between 0 and 2 Pi
angle = openmc.stats.Uniform(a=0.0, b=2 * 3.14159265359)
# this makes the ring source using the three distributions and a radius
# could do a point source instead with my_source.space = openmc.stats.Point((5,5., 5))
my_source.space = openmc.stats.CylindricalIndependent(
r=radius, phi=angle, z=z_values, origin=(0.0, 0.0, 0.0)
)
# sets the direction to isotropic
my_source.angle = openmc.stats.Isotropic()

my_settings = openmc.Settings()
# my_settings.inactive = 0
my_settings.run_mode = "fixed source"
my_settings.batches = 10
my_settings.particles = 100000
my_settings.source = my_source

model = openmc.model.Model(my_geometry, my_materials, my_settings, tallies)
sp_filename = model.run()

statepoint = openmc.StatePoint(sp_filename)

my_tally_result = statepoint.get_tally(name="my_tally")

for slice_index in range(len(mesh.phi_grid) - 1):
data = mesh.slice_of_data(
dataset=my_tally_result.mean,
slice_index=slice_index,
volume_normalization=True,
)
extent = mesh.get_mpl_plot_extent()
x_label, y_label = mesh.get_axis_labels()
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.imshow(data, extent=extent)
assert data.shape == (4, 3)

0 comments on commit a0383df

Please sign in to comment.