Loading pygriffin/config.py +1 −1 Original line number Diff line number Diff line Loading @@ -142,7 +142,7 @@ class PyGriffinConfig: @property def griffin_dir(self): return self.griffin_exec.parent return Path(self.griffin_exec).parent @property def griffin_exec(self): Loading pygriffin/isoxml_reader.py +12 −2 Original line number Diff line number Diff line Loading @@ -3,15 +3,25 @@ import sys from xml.etree import ElementTree as ET def parse_isoxml(isoxml_file): def parse_isoxml(isoxml_file, library): """ Extracts names of all isotopes from the XML file """ tree = ET.parse(isoxml_file) root = tree.getroot() # find the indicated library name child_node = None for child in root: if child.attrib['Name'] == library: child_node = child if child_node is None: raise RuntimeError("Could lot locate library '{}' in " "the ISOXML file".format(library)) # find all "Isotope" elements isotopes = list(root.iter('Isotope')) isotopes = list(child_node.iter('Isotope')) return [isotope.attrib["Name"] for isotope in isotopes] if __name__ == '__main__': Loading pygriffin/moose_parser.py +43 −8 Original line number Diff line number Diff line Loading @@ -20,9 +20,10 @@ def parse_moose_materials(input_text): Returns ------- dict A dictionary with material names as keys and lists of isotope names as values A dictionary with material names as keys and values that are tuples containing a list of isotopes and the material mesh block """ library_name = None mat_block = [] isotopes = [] accumulate_lines = False Loading @@ -36,7 +37,13 @@ def parse_moose_materials(input_text): # once the end of the block has been reached, exit loop if accumulate_lines and '[]' == line.replace('\n', ''): break accumulate_lines = False if 'library_name' in line: library_name = line.split('=')[-1].strip() if library_name is None: raise ValueError("Could not detect a library name in the Griffin input files") # remove header and footer from material block mat_block = mat_block[1:-1] Loading @@ -55,11 +62,11 @@ def parse_moose_materials(input_text): # 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_name, isotopes, block = parse_moose_material(mat_lines) materials[mat_name] = (isotopes, block) mat_lines = [] return materials return library_name, materials def parse_moose_material(material_txt): Loading Loading @@ -88,7 +95,7 @@ def parse_moose_material(material_txt): material_name = material_name.split('/')[-1] if 'isotope' in line: isotope = line.split(" ")[-1] isotope = line.split("=")[-1].strip() if '\\' in isotope: isotope = isotope[:isotope.find('\\')] isotope = isotope.replace('"', '') Loading @@ -96,4 +103,32 @@ def parse_moose_material(material_txt): isotope = isotope.rstrip('\n') isotopes.append(isotope) return material_name, isotopes if 'block' in line: block = line.split("=")[-1].strip().strip("'") return material_name, isotopes, block def parse_mesh_blocks(mesh_input): """ Parses mesh blocks from a mesh input file Parameters ---------- mesh_input : list of str Lines of text containing the MOOSE mesh description Returns ------- list Sorted list of block IDs """ blocks = set() for line in mesh_input: if 'block_id' in line: block_id = line.split('=')[-1].strip() blocks.add(block_id) return sorted(list(blocks)) pygriffin/pygriffin.py +46 −6 Original line number Diff line number Diff line Loading @@ -9,7 +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 .moose_parser import parse_moose_materials, parse_mesh_blocks from .util import run_in_tmpdir Loading Loading @@ -130,15 +130,19 @@ class PyGriffin: """ Prints materials assigned and available ISOXML isotopes to screen """ mesh_materials = self.get_material_assignments() mat_str = "Materials applied in the Griffin mesh:\n" for material, isotopes in mesh_materials.items(): library, mesh_materials = self.get_material_assignments() assigned_mesh_blocks = set() mat_str = "Materials applied in the Griffin mesh:\n\n" mat_str += "\tLibrary: {}\n".format(library) for material, (isotopes, block) in mesh_materials.items(): mat_str += "\tMaterial Name: {}\n".format(material) mat_str += "\tBlock: {}\n".format(block) mat_str += "\tIsotopes:\n" mat_str += ''.join('\t\t{}\n'.format(i) for i in isotopes) assigned_mesh_blocks.add(block) print(mat_str) isoxml_materials = parse_isoxml(self.process_xs()) isoxml_materials = parse_isoxml(self.process_xs(), library) isotope_str = "Isotopes available in the ISOXML library:\n" isotope_str += ''.join('\t{}\n'.format(m) for m in isoxml_materials) print(isotope_str) Loading @@ -148,6 +152,20 @@ class PyGriffin: for isos in mesh_materials.values(): (mesh_set.update(i) for i in isos) mesh_blocks = set(self.get_mesh_blocks()) unassigned_mesh_blocks = mesh_blocks - assigned_mesh_blocks if unassigned_mesh_blocks: print('The following mesh blocks are unassigned:\n') print(', '.join(sorted(list(unassigned_mesh_blocks)))) incorrect_mesh_blocks = assigned_mesh_blocks - mesh_blocks if incorrect_mesh_blocks: print('The following material blocks are not present in the mesh:\n') print(', '.join(sorted(list(incorrect_mesh_blocks)))) # determine materials used in the mesh that are not # not present in the ISOXML file if not isoxml_set.issuperset(mesh_set): Loading @@ -161,9 +179,31 @@ class PyGriffin: return invalid_mats, unused_mats def get_mesh_blocks(self): """ Get all mesh blocks in the Griffin problem Returns ------- list A list of sorted mesh block IDs """ # open the mesh file if self.mesh.endswith('.i'): with open(self.mesh, 'r') as fh: return parse_mesh_blocks(fh.readlines()) return [] def get_material_assignments(self): """ Concatenate all MOOSE inputs and Get all assigned materials in the Griffin problem Returns ------- dict A dictionary with material names as keys and values that are tuples containing a list of isotopes and the material mesh block """ # open the mesh and options files lines = [] Loading workbench_runner.py +1 −2 Original line number Diff line number Diff line Loading @@ -48,7 +48,6 @@ if __name__ == '__main__': print("Done") sys.stdout.flush() if griffin_block.simulate is None or str(son_obj.griffin.simulate.value) == '"true"': print("Running PyGriffin...") sys.stdout.flush() Loading Loading
pygriffin/config.py +1 −1 Original line number Diff line number Diff line Loading @@ -142,7 +142,7 @@ class PyGriffinConfig: @property def griffin_dir(self): return self.griffin_exec.parent return Path(self.griffin_exec).parent @property def griffin_exec(self): Loading
pygriffin/isoxml_reader.py +12 −2 Original line number Diff line number Diff line Loading @@ -3,15 +3,25 @@ import sys from xml.etree import ElementTree as ET def parse_isoxml(isoxml_file): def parse_isoxml(isoxml_file, library): """ Extracts names of all isotopes from the XML file """ tree = ET.parse(isoxml_file) root = tree.getroot() # find the indicated library name child_node = None for child in root: if child.attrib['Name'] == library: child_node = child if child_node is None: raise RuntimeError("Could lot locate library '{}' in " "the ISOXML file".format(library)) # find all "Isotope" elements isotopes = list(root.iter('Isotope')) isotopes = list(child_node.iter('Isotope')) return [isotope.attrib["Name"] for isotope in isotopes] if __name__ == '__main__': Loading
pygriffin/moose_parser.py +43 −8 Original line number Diff line number Diff line Loading @@ -20,9 +20,10 @@ def parse_moose_materials(input_text): Returns ------- dict A dictionary with material names as keys and lists of isotope names as values A dictionary with material names as keys and values that are tuples containing a list of isotopes and the material mesh block """ library_name = None mat_block = [] isotopes = [] accumulate_lines = False Loading @@ -36,7 +37,13 @@ def parse_moose_materials(input_text): # once the end of the block has been reached, exit loop if accumulate_lines and '[]' == line.replace('\n', ''): break accumulate_lines = False if 'library_name' in line: library_name = line.split('=')[-1].strip() if library_name is None: raise ValueError("Could not detect a library name in the Griffin input files") # remove header and footer from material block mat_block = mat_block[1:-1] Loading @@ -55,11 +62,11 @@ def parse_moose_materials(input_text): # 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_name, isotopes, block = parse_moose_material(mat_lines) materials[mat_name] = (isotopes, block) mat_lines = [] return materials return library_name, materials def parse_moose_material(material_txt): Loading Loading @@ -88,7 +95,7 @@ def parse_moose_material(material_txt): material_name = material_name.split('/')[-1] if 'isotope' in line: isotope = line.split(" ")[-1] isotope = line.split("=")[-1].strip() if '\\' in isotope: isotope = isotope[:isotope.find('\\')] isotope = isotope.replace('"', '') Loading @@ -96,4 +103,32 @@ def parse_moose_material(material_txt): isotope = isotope.rstrip('\n') isotopes.append(isotope) return material_name, isotopes if 'block' in line: block = line.split("=")[-1].strip().strip("'") return material_name, isotopes, block def parse_mesh_blocks(mesh_input): """ Parses mesh blocks from a mesh input file Parameters ---------- mesh_input : list of str Lines of text containing the MOOSE mesh description Returns ------- list Sorted list of block IDs """ blocks = set() for line in mesh_input: if 'block_id' in line: block_id = line.split('=')[-1].strip() blocks.add(block_id) return sorted(list(blocks))
pygriffin/pygriffin.py +46 −6 Original line number Diff line number Diff line Loading @@ -9,7 +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 .moose_parser import parse_moose_materials, parse_mesh_blocks from .util import run_in_tmpdir Loading Loading @@ -130,15 +130,19 @@ class PyGriffin: """ Prints materials assigned and available ISOXML isotopes to screen """ mesh_materials = self.get_material_assignments() mat_str = "Materials applied in the Griffin mesh:\n" for material, isotopes in mesh_materials.items(): library, mesh_materials = self.get_material_assignments() assigned_mesh_blocks = set() mat_str = "Materials applied in the Griffin mesh:\n\n" mat_str += "\tLibrary: {}\n".format(library) for material, (isotopes, block) in mesh_materials.items(): mat_str += "\tMaterial Name: {}\n".format(material) mat_str += "\tBlock: {}\n".format(block) mat_str += "\tIsotopes:\n" mat_str += ''.join('\t\t{}\n'.format(i) for i in isotopes) assigned_mesh_blocks.add(block) print(mat_str) isoxml_materials = parse_isoxml(self.process_xs()) isoxml_materials = parse_isoxml(self.process_xs(), library) isotope_str = "Isotopes available in the ISOXML library:\n" isotope_str += ''.join('\t{}\n'.format(m) for m in isoxml_materials) print(isotope_str) Loading @@ -148,6 +152,20 @@ class PyGriffin: for isos in mesh_materials.values(): (mesh_set.update(i) for i in isos) mesh_blocks = set(self.get_mesh_blocks()) unassigned_mesh_blocks = mesh_blocks - assigned_mesh_blocks if unassigned_mesh_blocks: print('The following mesh blocks are unassigned:\n') print(', '.join(sorted(list(unassigned_mesh_blocks)))) incorrect_mesh_blocks = assigned_mesh_blocks - mesh_blocks if incorrect_mesh_blocks: print('The following material blocks are not present in the mesh:\n') print(', '.join(sorted(list(incorrect_mesh_blocks)))) # determine materials used in the mesh that are not # not present in the ISOXML file if not isoxml_set.issuperset(mesh_set): Loading @@ -161,9 +179,31 @@ class PyGriffin: return invalid_mats, unused_mats def get_mesh_blocks(self): """ Get all mesh blocks in the Griffin problem Returns ------- list A list of sorted mesh block IDs """ # open the mesh file if self.mesh.endswith('.i'): with open(self.mesh, 'r') as fh: return parse_mesh_blocks(fh.readlines()) return [] def get_material_assignments(self): """ Concatenate all MOOSE inputs and Get all assigned materials in the Griffin problem Returns ------- dict A dictionary with material names as keys and values that are tuples containing a list of isotopes and the material mesh block """ # open the mesh and options files lines = [] Loading
workbench_runner.py +1 −2 Original line number Diff line number Diff line Loading @@ -48,7 +48,6 @@ if __name__ == '__main__': print("Done") sys.stdout.flush() if griffin_block.simulate is None or str(son_obj.griffin.simulate.value) == '"true"': print("Running PyGriffin...") sys.stdout.flush() Loading