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

Making logic for parsing materials common to both exodus and MOOSE input formats.

parent 57cca725
Loading
Loading
Loading
Loading
+120 −27
Original line number Diff line number Diff line
@@ -128,15 +128,22 @@ class PyGriffin:
    def report_materials(self):

        mesh_materials = self.get_material_assignments()
        print("Materials applied in the Griffin mesh:")
        print(''.join('\t{}\n'.format(m) for m in mesh_materials))
        mat_str = "Materials applied in the Griffin mesh:\n"
        for material, isotopes in mesh_materials.items():
            mat_str += "\tMaterial Name: {}\n".format(material)
            mat_str += "\tIsotopes:\n"
            mat_str += ''.join('\t\t{}\n'.format(i) for i in isotopes)
        print(mat_str)

        isoxml_materials = parse_isoxml(self.process_xs())
        print("Materials available in the ISOXML library:")
        print(''.join('\t{}\n'.format(m) for m in isoxml_materials))
        isotope_str = "Isotopes available in the ISOXML library:\n"
        isotope_str += ''.join('\t{}\n'.format(m) for m in isoxml_materials)
        print(isotope_str)

        isoxml_set = set(isoxml_materials)
        mesh_set = set(mesh_materials)
        mesh_set = set()
        for isos in mesh_materials.values():
            (mesh_set.update(i) for i in isos)

        # determine materials used in the mesh that are not
        # not present in the ISOXML file
@@ -159,28 +166,111 @@ class PyGriffin:
        else:
            return ["No materials found"]

    def get_moose_materials(self):
        # open the mesh and options files
        lines = []
        with open(self.input, 'r') as input:
            lines += input.readlines()
        with open(self.mesh, 'r') as mesh:
            lines += mesh.readlines()
    def parse_materials(self, input_text):
        """
        Parses materials and isotopes from a MOOSE text input

        mat_lines = []
        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 lines:
        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_lines += [line]
                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

            if accumulate_lines and line.rstrip('\n') == '[]':
                print("Breaking")
    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

        return mat_lines
            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('\'', '')
                isotopes.append(isotope)

        return material_name, isotopes

    def get_moose_materials(self):
        # open the mesh and options files
        lines = []
        with open(self.input, 'r') as input:
            lines += input.readlines()
        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
@@ -191,15 +281,18 @@ class PyGriffin:
        ncdump_out = subprocess.check_output([ncdump_exe, str(self.mesh)]).decode()
        ncdump_out = ncdump_out.split('\n')

        materials = []
        for line in ncdump_out:
            if 'isotope' in line:
                isotope = line.split(" ")[-1]
                isotope = isotope[:isotope.find('\\')]
                isotope = isotope.replace('"', '')
                materials.append(isotope)

        return materials
        # 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)

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