Commit 95dede16 authored by Patrick Shriwise's avatar Patrick Shriwise
Browse files

Adding method for class generation from a SON file. Adding options to run for...

Adding method for class generation from a SON file. Adding options to run for compatibility with Workbench output
parent 48f2d0ec
Loading
Loading
Loading
Loading
+79 −5
Original line number Diff line number Diff line
from collections import Iterable
from numbers import Integral
import os
from pathlib import Path
import subprocess
from collections import Iterable
import warnings

from . import checkvalue as cv
@@ -25,7 +26,12 @@ class PyGriffin:
        self.mesh = mesh
        self.n_procs = n_procs

    def run(self, input=None, mesh=None, cwd=None, other_args=None, color=True):
    def run(self, input=None,
                  mesh=None,
                  cwd=None,
                  other_args=None,
                  color=True,
                  logfile=None):
        """
        Execute Griffin

@@ -42,6 +48,8 @@ class PyGriffin:
        color : bool
            Removes posix terminal color characters from Griffin output
            if False
        logfile : str or pathlib.Path object
            Write output to screen if True. Write to file if false.

        Returns
        -------
@@ -90,8 +98,13 @@ class PyGriffin:
        if not color:
            cmd += ['--color', 'off']

        p = subprocess.Popen(cmd, universal_newlines=True, cwd=str(cwd))

        if logfile is not None:
            with open(logfile, 'w') as OUT:
                p = subprocess.Popen(cmd,
                                     universal_newlines=True,
                                     cwd=str(cwd),
                                     stdout=OUT,
                                     stderr=OUT)
        return p.wait()

    def process_xs(self, overwrite=False, particle='neutron', cwd=None):
@@ -132,7 +145,6 @@ class PyGriffin:
        # run isoxml executable on the current xs file
        cmd = [str(self.config.isoxml_exec), p_flag, str(self.xs)]
        p = subprocess.Popen(cmd, universal_newlines=True, cwd=str(cwd))

        p.wait()

        # construct the name of the new xs file and return it
@@ -211,4 +223,66 @@ class PyGriffin:
        cv.check_greater_than('Griffin processes', procs, 0)
        self._n_procs = procs

    @classmethod
    def from_son(cls, son_input, sonvalidxml_path=None):
        # generate an XML from the SON input
        import subprocess
        schema = Path(__file__).parent / "../etc/pygriffin.sch"
        # if explicit path to the sonvalidxml executable is
        # provided, assume it is in the environment's PATH
        if sonvalidxml_path is None:
            sonvalidxml_path = 'sonvalidxml'
        cmd = ' '.join([sonvalidxml_path, str(schema), son_input])
        xml_file = subprocess.check_output(cmd, shell=True)

        # generate a Python object from the XML file
        from wasppy import xml2obj
        xml_obj = xml2obj.xml2obj(xml_file)

        def _wasp_attr_to_str(attr):
            return str(attr.value).replace('"','')

        griffin_block = xml_obj.griffin

        if griffin_block is None:
            raise RuntimeError("No griffin block is present in the iput file")

        mesh = None
        if griffin_block.mesh_input is not None:
            mesh = _wasp_attr_to_str(griffin_block.mesh_input)
        elif griffin_block.mesh_exodus is not None:
            mesh = _wasp_attr_to_str(griffin_block.mesh_exodus)
        elif griffin_block.shift_input is not None:
            raise NotImplementedError("Shift input is not yet supported")
        else:
            raise RuntimeError("No mesh input provided in the SON file")

        options_file = None
        if griffin_block.griffin_input is not None:
            options_file = _wasp_attr_to_str(griffin_block.griffin_input)
        elif griffin_block.griffin_solver_options is not None:
            raise NotImplementedError("Support for option specification "
                                      "is not yet supported")
        else:
            raise RuntimeError("No griffin options provided in the SON file")

        # create a new PyGriffin class instance
        pyg = cls(input=options_file, mesh=mesh)

        if griffin_block.isoxml is not None:
            pyg.xs = _wasp_attr_to_str(griffin_block.isoxml)
        if griffin_block.macro_isotxs:
            pyg.xs = _wasp_attr_to_str(griffin_block.macro_isotxs)
            pyg.xs = pyg.process_xs()
        else:
            raise RuntimeError("No cross section data provided in the SON file")

        if griffin_block.num_cpu_max_griffin is not None:
            if pyg.mpi_enabled:
                pyg.n_procs = _wasp_attr_to_str(griffin_block.num_cpu_max_griffin)
            else:
                warnings.warn("Griffin executable was not compiled with MPI enabled. "
                              "Setting number of cpus/procs to 1.")
                pyg.n_procs = 1

        return pyg