From 7cbda9085345811aeddabe8520a11c3a5e1ec228 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 21 Mar 2023 13:43:50 +0000 Subject: [PATCH 1/4] added first test --- .github/workflows/black.yml | 32 ++++++++ .github/workflows/ci_with_install.yml | 54 ++++++++++++++ .github/workflows/python-publish.yml | 40 ++++++++++ .../{simple_plot.py => plot_rz_slices.py} | 0 pyproject.toml | 43 +++++++++-- tests/test_slice_of_data.py | 73 +++++++++++++++++++ 6 files changed, 236 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/black.yml create mode 100644 .github/workflows/ci_with_install.yml create mode 100644 .github/workflows/python-publish.yml rename examples/{simple_plot.py => plot_rz_slices.py} (100%) create mode 100644 tests/test_slice_of_data.py diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml new file mode 100644 index 0000000..4eee07f --- /dev/null +++ b/.github/workflows/black.yml @@ -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" diff --git a/.github/workflows/ci_with_install.yml b/.github/workflows/ci_with_install.yml new file mode 100644 index 0000000..0b01171 --- /dev/null +++ b/.github/workflows/ci_with_install.yml @@ -0,0 +1,54 @@ +# 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/ -v --cov=cylindrical_mesh_plotter --cov-append --cov-report term --cov-report xml + + - name: Upload to codecov + uses: codecov/codecov-action@v2 + + - name: Run examples + run: | + cd examples + python plot_rz_slices.py diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..ac5bbec --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -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/* diff --git a/examples/simple_plot.py b/examples/plot_rz_slices.py similarity index 100% rename from examples/simple_plot.py rename to examples/plot_rz_slices.py diff --git a/pyproject.toml b/pyproject.toml index 933ff72..01e3f9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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="mail@jshimwell.com" }, +] +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"} diff --git a/tests/test_slice_of_data.py b/tests/test_slice_of_data.py new file mode 100644 index 0000000..033272e --- /dev/null +++ b/tests/test_slice_of_data.py @@ -0,0 +1,73 @@ +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.,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., 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) From 7cb55ff833dbcecdd070efc2660a21aed3ca8e09 Mon Sep 17 00:00:00 2001 From: shimwell Date: Tue, 21 Mar 2023 13:45:31 +0000 Subject: [PATCH 2/4] [skip ci] Apply formatting changes --- examples/plot_rz_slices.py | 34 ++++++++----------- .../__init__.py | 2 +- src/openmc_cylindrical_mesh_plotter/core.py | 3 +- tests/test_slice_of_data.py | 31 ++++++++--------- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/examples/plot_rz_slices.py b/examples/plot_rz_slices.py index 2e8d900..e371ea1 100644 --- a/examples/plot_rz_slices.py +++ b/examples/plot_rz_slices.py @@ -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]) @@ -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" @@ -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() diff --git a/src/openmc_cylindrical_mesh_plotter/__init__.py b/src/openmc_cylindrical_mesh_plotter/__init__.py index a8ce586..bb67a43 100644 --- a/src/openmc_cylindrical_mesh_plotter/__init__.py +++ b/src/openmc_cylindrical_mesh_plotter/__init__.py @@ -1 +1 @@ -from .core import * \ No newline at end of file +from .core import * diff --git a/src/openmc_cylindrical_mesh_plotter/core.py b/src/openmc_cylindrical_mesh_plotter/core.py index d2f2674..ba51503 100644 --- a/src/openmc_cylindrical_mesh_plotter/core.py +++ b/src/openmc_cylindrical_mesh_plotter/core.py @@ -10,7 +10,6 @@ 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: @@ -18,7 +17,7 @@ def slice_of_data( 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) diff --git a/tests/test_slice_of_data.py b/tests/test_slice_of_data.py index 033272e..6eaf699 100644 --- a/tests/test_slice_of_data.py +++ b/tests/test_slice_of_data.py @@ -6,24 +6,23 @@ def test_slice_of_data(): - 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]) @@ -36,12 +35,12 @@ def test_slice_of_data(): # 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() @@ -57,17 +56,17 @@ def test_slice_of_data(): statepoint = openmc.StatePoint(sp_filename) - my_tally_result = statepoint.get_tally(name='my_tally') + my_tally_result = statepoint.get_tally(name="my_tally") - 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 + 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) + assert data.shape == (4, 3) From 20f7e7a473642caa27b274b27d7028cfbf94d171 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 21 Mar 2023 14:01:16 +0000 Subject: [PATCH 3/4] removed covage --- .github/workflows/ci_with_install.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/ci_with_install.yml b/.github/workflows/ci_with_install.yml index 0b01171..ff1dfea 100644 --- a/.github/workflows/ci_with_install.yml +++ b/.github/workflows/ci_with_install.yml @@ -43,10 +43,7 @@ jobs: - name: Run test_utils run: | - pytest tests/ -v --cov=cylindrical_mesh_plotter --cov-append --cov-report term --cov-report xml - - - name: Upload to codecov - uses: codecov/codecov-action@v2 + pytest tests - name: Run examples run: | From 4e1f4b59efa328af5a28dbff621a4b0ef526fb1c Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 21 Mar 2023 14:03:05 +0000 Subject: [PATCH 4/4] no need to conda install openmc --- .github/workflows/ci_with_install.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci_with_install.yml b/.github/workflows/ci_with_install.yml index ff1dfea..df199b8 100644 --- a/.github/workflows/ci_with_install.yml +++ b/.github/workflows/ci_with_install.yml @@ -26,10 +26,10 @@ jobs: - 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 dependencies + # run: | + # conda install -c conda-forge mamba + # mamba install -c conda-forge openmc - name: install package run: |