Commit a25d4d9f authored by Patrick Shriwise's avatar Patrick Shriwise
Browse files

Factoring out moose parsing code

parent eb7ce5a1
Loading
Loading
Loading
Loading
+99 −0
Original line number Diff line number Diff line


def _has_moose_block_close(line):
    """
    Checks for a closed block in a line
    """
    return '[]' in line or '[../]' in line


def parse_moose_materials(input_text):
        """
        Parses materials and isotopes from a MOOSE text input

        Parameters
        ----------

        input_txt : list of str
            lines of the MOOSE input

        Returns
        -------
        dict
            A dictionary with material names as keys and lists of isotope names
            as values
        """
        mat_block = []
        isotopes = []
        accumulate_lines = False
        for line in input_text:
            # start accumulating lines at the beginning of the materials block
            if 'Materials' in line:
                accumulate_lines = True

            if accumulate_lines:
                mat_block += [line]

            # once the end of the block has been reached, exit loop
            if accumulate_lines and '[]' == line.replace('\n', ''):
                break

        # remove header and footer from material block
        mat_block = mat_block[1:-1]

        # material dictionary output
        materials = {}

        # parse out individual material blocks
        mat_lines = []
        for line in mat_block:
            if not line:
                continue

            mat_lines.append(line)

            # detect close of a block
            if _has_moose_block_close(line):
                # get material information upon close of a material block
                mat_name, isotopes = parse_moose_material(mat_lines)
                materials[mat_name] = isotopes
                mat_lines = []

        return materials


def parse_moose_material(material_txt):
    """
    Parses material information from a MOOSE material block

    Parameters
    ----------

    material_txt : list of str
        Lines of text containing the material block

    Returns
    -------
    tuple
        A tuple of material name and a list of isotope names
    """
    material_name = None
    isotopes = []
    for line in material_txt:
        if _has_moose_block_close(line):
            break

        if '[' in line:
            material_name = line.strip().lstrip('[').rstrip(']')
            material_name = material_name.split('/')[-1]

        if 'isotope' in line:
            isotope = line.split(" ")[-1]
            if '\\' in isotope:
                isotope = isotope[:isotope.find('\\')]
            isotope = isotope.replace('"', '')
            isotope = isotope.replace('\'', '')
            isotope = isotope.rstrip('\n')
            isotopes.append(isotope)

    return material_name, isotopes
+3 −128
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import warnings
from . import checkvalue as cv
from .config import PyGriffinConfig
from .isoxml_reader import parse_isoxml
from .moose_parser import parse_moose_materials
from .util import run_in_tmpdir


@@ -158,112 +159,8 @@ class PyGriffin:

        return invalid_mats, unused_mats

    def get_material_assignments(self):
        if str(self.mesh).endswith('.e'):
            return self.get_exodus_materials()
        elif str(self.mesh).endswith('.i'):
            return self.get_moose_materials()
        else:
            return ["No materials found"]

    def parse_materials(self, input_text):
        """
        Parses materials and isotopes from a MOOSE text input

        Parameters
        ----------

        input_txt : list of str
            lines of the MOOSE input

        Returns
        -------
        dict
            A dictionary with material names as keys and lists of isotope names
            as values
        """
        mat_block = []
        isotopes = []
        accumulate_lines = False
        for line in input_text:
            # start accumulating lines at the beginning of the materials block
            if 'Materials' in line:
                accumulate_lines = True

            if accumulate_lines:
                mat_block += [line]

            # once the end of the block has been reached, exit loop
            if accumulate_lines and '[]' == line.replace('\n', ''):
                break

        # remove header and footer from material block
        mat_block = mat_block[1:-1]

        # material dictionary output
        materials = {}

        # parse out individual material blocks
        mat_lines = []
        for line in mat_block:
            if not line:
                continue

            mat_lines.append(line)

            # detect close of a block
            if self.has_moose_block_close(line):
                # get material information upon close of a material block
                mat_name, isotopes = self.parse_material(mat_lines)
                materials[mat_name] = isotopes
                mat_lines = []

        return materials

    @staticmethod
    def has_moose_block_close(l):
        """
        Checks for a closed block in a line
        """
        return '[]' in l or '[../]' in l

    def parse_material(self, material_txt):
        """
        Parses material information from a MOOSE material block

        Parameters
        ----------

        material_txt : list of str
            Lines of text containing the material block

        Returns
        -------
        tuple
            A tuple of material name and a list of isotope names
        """
        material_name = None
        isotopes = []
        for line in material_txt:
            if self.has_moose_block_close(line):
                break

            if '[' in line:
                material_name = line.strip().lstrip('[').rstrip(']')
                material_name = material_name.split('/')[-1]

            if 'isotope' in line:
                isotope = line.split(" ")[-1]
                if '\\' in isotope:
                    isotope = isotope[:isotope.find('\\')]
                isotope = isotope.replace('"', '')
                isotope = isotope.replace('\'', '')
                isotope = isotope.rstrip('\n')
                isotopes.append(isotope)

        return material_name, isotopes

    def get_moose_materials(self):
    def get_material_assignments(self):
        # open the mesh and options files
        lines = []
        with open(self.input, 'r') as input:
@@ -271,29 +168,7 @@ class PyGriffin:
        with open(self.mesh, 'r') as mesh:
            lines += mesh.readlines()

        return self.parse_materials(lines)

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

        # call ncdump and capture output
        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')

        # sanitize the output a bit
        nc_line_term = '\\000'
        for i, line in enumerate(ncdump_out):
            # remove quotes that each line is wrapped in from ncdump
            line = line.replace('"', '').strip()
            # remove filler bytes at the end of lines
            if nc_line_term in line:
                line = line[:line.find(nc_line_term)]
            # replace line in the list
            ncdump_out[i] = line

        return self.parse_materials(ncdump_out)
        return parse_moose_materials(lines)

    def process_xs(self, overwrite=False, particle='neutron', cwd=None):
        """