Commit 376fc2e1 authored by Patrick Shriwise's avatar Patrick Shriwise
Browse files

Refactor for materials reporting and Griffin simulation. Now working w/ Workbench local

parent b624395f
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
[exec]
griffin=/home/neams/opt/griffin/griffin-opt
isoxml=/home/neams/opt/griffin/isoxml/isoxml-opt
 No newline at end of file
+22 −4
Original line number Diff line number Diff line
@@ -30,6 +30,24 @@ griffin{
        ValType=String
        InputDefault="link_to_file"
    }
    report_materials {
        Description = "Show materials assigned in the mesh and available in the cross section library"
        InputTmpl = "flagtypes"
        MinOccurs = 0
        MaxOccurs = 1
        ValType = String
        ValEnums = [ true false ]
        InputDefault = false
    }
    simulate{
        Description = "Whether to run the Griffin simulation"
        InputTmpl = "flagtypes"
        MinOccurs = 0
        MaxOccurs = 1
        ValType = String
        ValEnums = [ true false ]
        InputDefault = true
    }
    shift_input{
        Description = "Link to Shift input file - if provided, Shift will be executed for XS tallying and to generate ISOXML, and Mesh will be generated"
        InputTmpl="flagtypes"
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ def parse_isoxml(isoxml_file):
    return [isotope.attrib["Name"] for isotope in isotopes]

if __name__ == '__main__':
    print('Parsing XML file {}...'.foramt(sys.argv[1]))
    print('Parsing XML file {}...'.format(sys.argv[1]))
    isoxml_materials = parse_isoxml(sys.argv[1])
    print('Found {} isotopes in the ISOXML:'.format(len(isoxml_materials)))
    print(''.join('\t{}\n'.format(m) for m in isoxml_materials))
+29 −33
Original line number Diff line number Diff line
@@ -82,6 +82,14 @@ class PyGriffin:
        # append the mesh file to the command if needed
        if str(mesh_file).endswith('.i'):
            cmd.append(str(mesh_file))
        elif str(mesh_file).endswith('.e'):
            mesh_inp_content = "[Mesh]\n"
            mesh_inp_content += "file = {}".format(mesh_file)
            mesh_inp_content += "[]"
            mesh_inp_filename = 'pygriffin_mesh_input.i'
            with open(mesh_inp_filename, 'w') as pygriffin_mesh_file:
                pygriffin_mesh_file.write(mesh_inp_content)
            cmd += [mesh_inp_filename]

        if cwd is not None:
            cwd = Path(cwd)
@@ -137,21 +145,21 @@ class PyGriffin:
            invalid_mats = []

        # determine which materials in the ISOXML are unused
        unused_mats = list(isoxml_set - exodus_materials)
        unused_mats = list(isoxml_set - exodus_set)

        return invalid_mats, unused_mats

    def get_exodus_materials(self):
        with run_in_tmpdir():
        # generate the exodus file that would be used in the Griffin run
            mesh_file = self.generate_mesh()
        self.mesh = self.generate_mesh()

        # call ncdump and capture output
            ncdump_out = subprocess.check_output(['ncdump', mesh_file]).decode()
        ncdump_exe = '/home/neams/mambaforge3/envs/moose/libmesh/bin/ncdump'
        ncdump_out = subprocess.check_output([ncdump_exe, str(self.mesh)]).decode()
        ncdump_out = ncdump_out.split('\n')

        materials = []
            for line in output:
        for line in ncdump_out:
            if 'isotope' in line:
                isotope = line.split(" ")[-1]
                isotope = isotope[:isotope.find('\\')]
@@ -207,7 +215,7 @@ class PyGriffin:

        return xs_name

    def generate_mesh(self, overwrite=False):
    def generate_mesh(self, overwrite=False, logfile=None):
        """
        Generates the mesh for the griffin-problem if needed

@@ -229,7 +237,9 @@ class PyGriffin:
            name_parts = str(self.mesh).split('.')
            name_parts[-1] = 'e'
            mesh_name = '.'.join(name_parts)
            self.run(input=self.mesh, other_args=['--mesh-only', mesh_name])
            self.run(input=self.mesh,
                     other_args=['--mesh-only', mesh_name],
                     logfile=logfile)
            return Path(mesh_name).resolve().absolute()

    def __call__(self, cwd=None):
@@ -277,27 +287,13 @@ class PyGriffin:
        self._n_procs = procs

    @classmethod
    def from_son(cls, son_input, sonvalidxml_path=None):
    def from_son_obj(cls, griffin_block, sonvalidxml_path=None):
        # generate an XML from the SON input
        import subprocess
        from wasppy import xml2obj

        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
        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")

pygriffin/util.py

0 → 100644
+38 −0
Original line number Diff line number Diff line
from contextlib import contextmanager
import os
import shutil
import tempfile


class TemporaryDirectory(object):
    """
    Context manager for tempfile.mkdtemp().
    This class is available in python +v3.2.
    """
    def __enter__(self):
        self.dir_name = tempfile.mkdtemp()
        return self.dir_name

    def __exit__(self, exc_type, exc_value, traceback):
        shutil.rmtree(self.dir_name)

TemporaryDirectory = getattr(tempfile, 'TemporaryDirectory',
                             TemporaryDirectory)


@contextmanager
def run_in_tmpdir():
    """Context manager to change to/return from a tmpdir.

    Parameters
    ----------
    files : Iterable of str or Path-like
        Set of files to copy into the temporary directory
    """
    with TemporaryDirectory() as tmpdir:
        cwd = os.getcwd()
        try:
            os.chdir(tmpdir)
            yield
        finally:
            os.chdir(cwd)
Loading