Skip to content

Commit

Permalink
Merge pull request #7 from DanShort12/implement_ci
Browse files Browse the repository at this point in the history
Implement ci
  • Loading branch information
makeclean authored Sep 7, 2020
2 parents a9d41fa + 3353357 commit 206e4d2
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 17 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Python package

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build_and_test:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install plasma source
run: |
pip install -r requirements-develop.txt
python setup.py bdist_wheel
python -m pip install --verbose dist/*.whl
- name: Run tests
run: |
pytest tests
- name: Upload wheel artifact
uses: actions/upload-artifact@v2
with:
name: dist
path: dist
34 changes: 28 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ set(OPENMC_DIR /opt/openmc)
set(OPENMC_INC_DIR ${OPENMC_DIR}/include)
set(OPENMC_LIB_DIR ${OPENMC_DIR}/lib)

# Ensure submodules are available and up to date
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE)
message(STATUS "Submodule update")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/pybind11/CMakeLists.txt")
message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

# Build source_sampling
list(APPEND source_sampling_SOURCES
${SRC_DIR}/source_sampling.cpp
Expand All @@ -26,13 +46,15 @@ list(APPEND source_sampling_SOURCES

add_library(source_sampling SHARED ${source_sampling_SOURCES})

find_library(OPENMC_LIB openmc HINTS ${OPENMC_LIB_DIR})
find_library(OPENMC_LIB openmc HINTS ${OPENMC_LIB_DIR} OPTIONAL)

set_target_properties(source_sampling PROPERTIES PREFIX "")
set_target_properties(source_sampling PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(source_sampling PUBLIC ${OPENMC_INC_DIR})
target_include_directories(source_sampling PUBLIC ${OPENMC_DIR}/vendor/pugixml)
target_link_libraries(source_sampling ${OPENMC_LIB} gfortran)
if (OPENMC_LIB)
set_target_properties(source_sampling PROPERTIES PREFIX "")
set_target_properties(source_sampling PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(source_sampling PUBLIC ${OPENMC_INC_DIR})
target_include_directories(source_sampling PUBLIC ${OPENMC_DIR}/vendor/pugixml)
target_link_libraries(source_sampling ${OPENMC_LIB} gfortran)
endif()

# Build plasma_source Python bindings
list(APPEND plasma_source_pybind_SOURCES
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# parametric-plasma-source

![Python package](https://github.com/DanShort12/parametric-plasma-source/workflows/Python%20package/badge.svg)

Python package, C++ source and build files for parametric plasma source for use in fusion neutron transport calculations with OpenMC.

The plasma source is based on a paper by [C. Fausser et al](https://www.sciencedirect.com/science/article/pii/S0920379612000853)

# Installation
## Installation

```pip install parametric_plasma_source```

# Usage
## Usage

The parametric plasma source can be imported an used in Python 3 in the following manner.

```
```[python]
from parametric_plasma_source import Plasma
my_plasma = Plasma(major_radius=6,
minor_radius=1.5,
Expand All @@ -25,7 +27,7 @@ In the above example the major_radius, minor_radius, elongation and triangularit

There are a number of additional arguments that can be passed to the Plasma class on construction. Units are in SI (e.g. meters not cm)

```
```[python]
ion_density_pedistal = 1.09e+20
ion_density_seperatrix = 3e+19
ion_density_origin = 1.09e+20
Expand Down
2 changes: 0 additions & 2 deletions parametric_plasma_source/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from .plasma import Plasma
from .plasma_source import PlasmaSource
3 changes: 0 additions & 3 deletions parametric_plasma_source/plasma_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include <cmath>
#include "plasma_source.hpp"
#include <stdlib.h>
#include "openmc/random_lcg.h"

#define RANDOM openmc::prn()

namespace plasma_source {

Expand Down
2 changes: 2 additions & 0 deletions requirements-develop.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
wheel
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def build_extension(self, ext):
"-DPYTHON_EXECUTABLE=" + sys.executable]

cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
build_args = ["--target", "plasma_source"]
build_args += ["--config", cfg]

cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
build_args += ["--", "-j2"]
build_args += ["plasma_source"]

env = os.environ.copy()
env["CXXFLAGS"] = "{} -DVERSION_INFO=\\\"{}\\\"".format(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_Compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from parametric_plasma_source.plasma import Plasma

pytest.importorskip("openmc")

class test_object_properties(unittest.TestCase):

def test_compile(self):
Expand Down
96 changes: 96 additions & 0 deletions tests/test_plasma_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Tests for the methods in plasma_source."""

import pytest

from parametric_plasma_source.plasma_source import PlasmaSource

plasma_params = {
"elongation": 1.557,
"ion_density_origin": 1.09e20,
"ion_density_peaking_factor": 1,
"ion_density_pedistal": 1.09e20,
"ion_density_seperatrix": 3e19,
"ion_temperature_origin": 45.9,
"ion_temperature_peaking_factor": 8.06,
"ion_temperature_pedistal": 6.09,
"ion_temperature_seperatrix": 0.1,
"major_radius": 906.0,
"minor_radius": 292.258,
"pedistal_radius": 0.8 * 292.258,
"plasma_id": 1,
"shafranov_shift": 44.789,
"triangularity": 0.270,
"ion_temperature_beta": 6,
}


@pytest.fixture(scope="session")
def plasma_source():
"""Make a plasma source to use as a test fixture."""
return PlasmaSource(**plasma_params)


class TestPlasmaSource:
"""A class to run tests against the plasma source."""

def test_ion_density_magnetic_origin(self, plasma_source):
"""Test the ion density at the magnetic origin."""
ion_density = plasma_source.ion_density(0.0)

assert pytest.approx(ion_density, 1.09e20)

def test_ion_density_inside_pedestal(self, plasma_source):
"""Test the ion density inside the pedestal."""
ion_density = plasma_source.ion_density(0.2)

assert pytest.approx(ion_density, 1.09e20)

def test_ion_density_outside_pedestal(self, plasma_source):
"""Test the ion density outside the pedestal."""
ion_density = plasma_source.ion_density(2.4)

assert pytest.approx(ion_density, 1.00628584e20)

def test_ion_density_boundary(self, plasma_source):
"""Test the ion density at the boundary."""
boundary = plasma_params["minor_radius"] / 100.0
ion_density = plasma_source.ion_density(boundary)

assert pytest.approx(
ion_density, plasma_params["ion_density_seperatrix"]
)

def test_ion_temperature_magnetic_origin(self, plasma_source):
"""Test the ion temperature at the magnetic origin."""
ion_temperature = plasma_source.ion_temperature(0.0)

assert pytest.approx(
ion_temperature, plasma_params["ion_temperature_origin"]
)

def test_ion_temperature_inside_pedestal(self, plasma_source):
"""Test the ion temperature inside the pedestal."""
ion_temperature = plasma_source.ion_temperature(0.2)

assert pytest.approx(ion_temperature, 45.89987429)

def test_ion_temperature_outside_pedestal(self, plasma_source):
"""Test the ion temperature outside the pedestal."""
ion_temperature = plasma_source.ion_temperature(2.4)

assert pytest.approx(ion_temperature, 5.45525594)

def test_ion_temperature_boundary(self, plasma_source):
"""Test the ion temperature at the boundary."""
boundary = plasma_params["minor_radius"] / 100.0
ion_temperature = plasma_source.ion_temperature(boundary)

assert pytest.approx(
ion_temperature, plasma_params["ion_temperature_seperatrix"]
)

def test_dt_cross_section(self, plasma_source):
"""Test the dt cross section at a specific temperature."""
dt_cross_section = plasma_source.dt_xs(4.25e7)

assert pytest.approx(dt_cross_section, 0.0)

0 comments on commit 206e4d2

Please sign in to comment.