Commit d113aaf7 authored by Shriwise, Patrick's avatar Shriwise, Patrick
Browse files

Merge branch 'pygriffin_postrun' into 'development'

Move logic for extracting k-effective to PyGriffin

See merge request nstauff/PyGriffin!12
parents ca579289 3fc5f09c
Loading
Loading
Loading
Loading
+52 −1
Original line number Diff line number Diff line
from collections import Iterable
from numbers import Integral
from numbers import Integral, Real
import os
from pathlib import Path
import subprocess
@@ -25,6 +25,7 @@ class PyGriffin:
        self.input = input
        self.mesh = mesh
        self.n_procs = n_procs
        self._keff = None

    def run(self, input=None,
                  mesh=None,
@@ -184,6 +185,46 @@ class PyGriffin:
            self.run(input=self.mesh, other_args=['--mesh-only', mesh_name])
            return Path(mesh_name).resolve().absolute()

    def postrun(self, griffin_csv_file):
        """
        Extract Griffin k-effective to output file

        Parameters
        ----------
        griffin_csv_file : str or pathlib.Path object
            CSV file generated by Griffin which stores eigenvalue information

        Returns
        -------
        int
            Errorcode of the Griffin postrun (0 is success,
                                              1 is csv_file not found,
                                              2 is incorrect csv data format
                                              3 is eigenvalue not found)
        """

        if not Path(griffin_csv_file).is_file():
            # CSV file not found
            return 1
        with open(griffin_csv_file, 'r') as f:
            lines = f.readlines()
        if len(lines) < 3:
            # Insufficient row entries in csv file
            return 2

        keff_field = "eigenvalue"
        header = lines[0].split('\n')[0]
        output_fields = header.split(',')
        if keff_field not in output_fields:
            # Output eigenvalue filed not found in csv file
            return 3
        keff_index = output_fields.index(keff_field)

        result_row = lines[-1].split('\n')[0]
        self.keff = float(result_row.split(',')[keff_index])

        return 0

    def __call__(self, cwd=None):
        return self.run(cwd)

@@ -291,3 +332,13 @@ class PyGriffin:
                pyg.n_procs = 1

        return pyg

    @property
    def keff(self):
        return self._keff

    @keff.setter
    def keff(self, keff):
        cv.check_type('Griffin k-effective', keff, Real)
        cv.check_greater_than('Griffin k-effective', keff, 0)
        self._keff = keff
+2 −0
Original line number Diff line number Diff line
time,eigenvalue
0,1
+3 −0
Original line number Diff line number Diff line
time
0
1
+3 −0
Original line number Diff line number Diff line
time,eigenvalue
0,1
1,0.5

tests/test_postrun.py

0 → 100644
+40 −0
Original line number Diff line number Diff line
import os
from pygriffin.pygriffin import PyGriffin
import pytest
from pathlib import Path

from pygriffin import PyGriffinConfig

_TEST_DIR = Path(__file__).parent.absolute()
_POSTRUN_SUCCESS_PATH = _TEST_DIR / 'postrun_success.csv'
_POSTRUN_NOEXIST_PATH = _TEST_DIR / 'postrun_noexist.csv'
_POSTRUN_INSUFFROW_PATH = _TEST_DIR / 'postrun_insuffrow.csv'
_POSTRUN_NOEIGEN_PATH = _TEST_DIR / 'postrun_noeigen.csv'


def test_griffin_postrun_success():
    pyg = PyGriffin(input='noinput.i', mesh='nomesh.i')
    ret_code = pyg.postrun(_POSTRUN_SUCCESS_PATH)
    assert ret_code == 0
    assert pyg.keff == 0.5


def test_griffin_postrun_file_noexist():
    pyg = PyGriffin(input='noinput.i', mesh='nomesh.i')
    ret_code = pyg.postrun(_POSTRUN_NOEXIST_PATH)
    assert ret_code == 1


def test_griffin_postrun_insufficient_row():
    pyg = PyGriffin(input='noinput.i', mesh='nomesh.i')
    ret_code = pyg.postrun(_POSTRUN_INSUFFROW_PATH)
    assert ret_code == 2


def test_griffin_postrun_no_eigenvalue():
    pyg = PyGriffin(input='noinput.i', mesh='nomesh.i')
    ret_code = pyg.postrun(_POSTRUN_NOEIGEN_PATH)
    assert ret_code == 3

if __name__ == "__main__":
    pytest.main()