def export_to_xml(self, path='materials.xml'): """Export material collection to an XML file. Parameters ---------- path : str Path to file to write. Defaults to 'materials.xml'. """ root_element = ET.Element("materials") self._create_cross_sections_subelement(root_element) self._create_material_subelements(root_element) # Clean the indentation in the file to be user-readable clean_indentation(root_element) # Check if path is a directory p = Path(path) if p.is_dir(): p /= 'materials.xml' # Write the XML Tree to the materials.xml file tree = ET.ElementTree(root_element) tree.write(str(p), xml_declaration=True, encoding='utf-8')
def export_to_xml(self, path='geometry.xml'): """Export geometry to an XML file. Parameters ---------- path : str Path to file to write. Defaults to 'geometry.xml'. """ # Create XML representation root_element = ET.Element("geometry") self.root_universe.create_xml_subelement(root_element, memo=set()) # Sort the elements in the file root_element[:] = sorted(root_element, key=lambda x: ( x.tag, int(x.get('id')))) # Clean the indentation in the file to be user-readable xml.clean_indentation(root_element) # Check if path is a directory p = Path(path) if p.is_dir(): p /= 'geometry.xml' # Write the XML Tree to the geometry.xml file tree = ET.ElementTree(root_element) tree.write(str(p), xml_declaration=True, encoding='utf-8')
def export_to_xml(self, path='plots.xml'): """Export plot specifications to an XML file. Parameters ---------- path : str Path to file to write. Defaults to 'plots.xml'. """ # Reset xml element tree self._plots_file.clear() self._create_plot_subelements() # Clean the indentation in the file to be user-readable clean_indentation(self._plots_file) # Check if path is a directory p = Path(path) if p.is_dir(): p /= 'plots.xml' # Write the XML Tree to the plots.xml file tree = ET.ElementTree(self._plots_file) tree.write(str(p), xml_declaration=True, encoding='utf-8')
def export_to_xml(self, path='settings.xml'): """Export simulation settings to an XML file. Parameters ---------- path : str Path to file to write. Defaults to 'settings.xml'. """ # Reset xml element tree root_element = ET.Element("settings") self._create_run_mode_subelement(root_element) self._create_particles_subelement(root_element) self._create_batches_subelement(root_element) self._create_inactive_subelement(root_element) self._create_generations_per_batch_subelement(root_element) self._create_keff_trigger_subelement(root_element) self._create_source_subelement(root_element) self._create_output_subelement(root_element) self._create_statepoint_subelement(root_element) self._create_sourcepoint_subelement(root_element) self._create_confidence_intervals(root_element) self._create_electron_treatment_subelement(root_element) self._create_energy_mode_subelement(root_element) self._create_max_order_subelement(root_element) self._create_photon_transport_subelement(root_element) self._create_ptables_subelement(root_element) self._create_seed_subelement(root_element) self._create_survival_biasing_subelement(root_element) self._create_cutoff_subelement(root_element) self._create_entropy_mesh_subelement(root_element) self._create_trigger_subelement(root_element) self._create_no_reduce_subelement(root_element) self._create_verbosity_subelement(root_element) self._create_tabular_legendre_subelements(root_element) self._create_temperature_subelements(root_element) self._create_trace_subelement(root_element) self._create_track_subelement(root_element) self._create_ufs_mesh_subelement(root_element) self._create_resonance_scattering_subelement(root_element) self._create_volume_calcs_subelement(root_element) self._create_create_fission_neutrons_subelement(root_element) self._create_log_grid_bins_subelement(root_element) self._create_dagmc_subelement(root_element) # Clean the indentation in the file to be user-readable clean_indentation(root_element) # Check if path is a directory p = Path(path) if p.is_dir(): p /= 'settings.xml' # Write the XML Tree to the settings.xml file tree = ET.ElementTree(root_element) tree.write(str(p), xml_declaration=True, encoding='utf-8')
def export_to_xml(self, filename): """Writes a depletion chain XML file. Parameters ---------- filename : str The path to the depletion chain XML file. """ root_elem = ET.Element('depletion_chain') for nuclide in self.nuclides: root_elem.append(nuclide.to_xml_element()) tree = ET.ElementTree(root_elem) if _have_lxml: tree.write(str(filename), encoding='utf-8', pretty_print=True) else: clean_indentation(root_elem) tree.write(str(filename), encoding='utf-8')
def export_to_xml(self, path='cross_sections.xml'): """Export cross section data library to an XML file. Parameters ---------- path : str Path to file to write. Defaults to 'cross_sections.xml'. """ root = ET.Element('cross_sections') # Determine common directory for library paths common_dir = os.path.dirname( os.path.commonprefix([lib['path'] for lib in self.libraries])) if common_dir == '': common_dir = '.' if os.path.relpath(common_dir, os.path.dirname(str(path))) != '.': dir_element = ET.SubElement(root, "directory") dir_element.text = os.path.realpath(common_dir) for library in self.libraries: if library['type'] == "depletion_chain": lib_element = ET.SubElement(root, "depletion_chain") else: lib_element = ET.SubElement(root, "library") lib_element.set('materials', ' '.join(library['materials'])) lib_element.set('path', os.path.relpath(library['path'], common_dir)) lib_element.set('type', library['type']) # Clean the indentation to be user-readable clean_indentation(root) # Write XML file reorder_attributes(root) # TODO: Remove when support is Python 3.8+ tree = ET.ElementTree(root) tree.write(str(path), xml_declaration=True, encoding='utf-8', method='xml')
def export_to_xml(self, path='geometry.xml', remove_surfs=False): """Export geometry to an XML file. Parameters ---------- path : str Path to file to write. Defaults to 'geometry.xml'. remove_surfs : bool Whether or not to remove redundant surfaces from the geometry when exporting .. versionadded:: 0.12 """ # Find and remove redundant surfaces from the geometry if remove_surfs: self.remove_redundant_surfaces() # Create XML representation root_element = ET.Element("geometry") self.root_universe.create_xml_subelement(root_element, memo=set()) # Sort the elements in the file root_element[:] = sorted(root_element, key=lambda x: (x.tag, int(x.get('id')))) # Clean the indentation in the file to be user-readable xml.clean_indentation(root_element) # Check if path is a directory p = Path(path) if p.is_dir(): p /= 'geometry.xml' # Write the XML Tree to the geometry.xml file xml.reorder_attributes( root_element) # TODO: Remove when support is Python 3.8+ tree = ET.ElementTree(root_element) tree.write(str(p), xml_declaration=True, encoding='utf-8')
def export_to_xml(self, filename): """Writes a depletion chain XML file. Parameters ---------- filename : str The path to the depletion chain XML file. """ root_elem = ET.Element('depletion') decay_elem = ET.SubElement(root_elem, 'decay_constants') for nuclide in self.nuclides: decay_elem.append( self.output_element(data=nuclide, data_type='decay_constants')) nfy_elem = ET.SubElement(root_elem, 'neutron_fission_yields') nuc_list = [ nuclide for nuclide in self.nuclides if nuclide.yield_data is not None ] pre_elem = ET.SubElement(nfy_elem, 'precursor') pre_elem.text = str(len(nuc_list)) ergpoint_elem = ET.SubElement(nfy_elem, 'energy_points') ergpoint_elem.text = str(1) prename_elem = ET.SubElement(nfy_elem, 'precursor_name') prename_elem.text = ' '.join( self.old_name(nuc.name) for nuc in nuc_list) erg_elem = ET.SubElement(nfy_elem, 'energy') erg_elem.text = str(0.0253) num_nfy_nuc = 0 # get yield data for each nuclide for nuclide in self.nuclides: from_yield = {} nuc_name = nuclide.name dat_yld = [] for nuc_pre in nuc_list: if 0.0253 in nuc_pre.yield_data.energies \ and nuc_name in nuc_pre.yield_data.products: ind_erg = nuc_pre.yield_data.energies.index(0.0253) ind_nuc = nuc_pre.yield_data.products.index(nuc_name) if ind_nuc is not None and ind_erg is not None: dat_yld.append( nuc_pre.yield_data.yield_matrix[ind_erg][ind_nuc]) else: dat_yld.append(0.0) else: dat_yld.append(0.0) # from_yield['name'] = nuc_name from_yield['energy'] = 0.0253 from_yield['yields'] = dat_yld if any(dat_yld): num_nfy_nuc += 1 nfy_elem.append( self.output_element(data=from_yield, data_type='neutron_fission_yields')) nuc_elem = ET.SubElement(nfy_elem, 'nuclides') nuc_elem.text = num_nfy_nuc clean_indentation(root_elem) tree = ET.ElementTree(root_elem) tree.write(filename, xml_declaration=True, encoding='utf-8')
def generate_tallies_xml(self, filename_string, default_group_struc=False): """Generate tallies.xml with hybrid tallies by using the energy group structure as a filter together with the material filter of an original tallies.xml. Parameters ---------- filename_string : string string with the path to a tallies.xml file that contains the reaction rate tally of a given geometry. This method will expand on this by adding the extra flux tally method and by reducing the number of scores in the original reaction-rate tally from 7 to 2. SHEM361_only : bool In case the user wants to use only the SHEM361 library. """ #reads a tallies.xml file with a reaction rate tally and uses the #material filter to build a new tallies.xml with hybrid tallies using #the energy filter. tree = ET.parse(filename_string) root = tree.getroot() mat_filter = root[0][0].text energy_filter = self.gen_energy_struc(default_group_struc) new_data = ET.Element('tallies') #attributes for hybrid tally. attrib_mat = {'id': '1', 'type': 'material'} attrib_energy = {'id': '2', 'type': 'energy'} empty_attrib = {} filter_mat = ET.SubElement(new_data, 'filter', attrib_mat) filter_ene = ET.SubElement(new_data, 'filter', attrib_energy) ET.SubElement(filter_mat, 'bins', empty_attrib) ET.SubElement(filter_ene, 'bins', empty_attrib) new_data[0][0].text = mat_filter new_data[1][0].text = ' '.join(str(i) for i in energy_filter) #writing '<tally>' object for key, value in enumerate(self.tallies_dict.items()): tally_id = value[1]['id'] tally_name = value[1]['name'] attrib_tallies = {'id': tally_id, 'name': tally_name} tally_level = ET.SubElement(new_data, 'tally', attrib_tallies) ET.SubElement(tally_level, 'filters', empty_attrib) if (value[1]['nuclides'] != ''): ET.SubElement(tally_level, 'nuclides', empty_attrib) ET.SubElement(tally_level, 'scores', empty_attrib) #last_index could be either a 1 or a 2, depending on #whether we are doing a reaction rate tally (i.e, last_index=1) or #a flux tally (i.e., last_index = 2) last_index = 1 filter_list = value[1]['filter'] score_list = value[1]['scores'] #offset by '2' indicates we are writing the tally objects below #the material and energy filters new_data[key + 2][0].text = ' '.join(i for i in filter_list) if (value[1]['nuclides'] != ''): nuclide_list = value[1]['nuclides'] new_data[key + 2][1].text = ' '.join(nuc for nuc in nuclide_list) last_index = 2 new_data[key + 2][last_index].text = ' '.join(i for i in score_list) if (old is True): clean_xml_indentation(new_data) else: clean_indentation(new_data) #renaming original 'tallies.xml' to 'tallies-original.xml' #to distinguish it from the new hybrid 'tally.xml' file. tree = ET.ElementTree(new_data) #os.rename(filename_string,'tallies-original.xml') tree.write('./tallies-hybrid-tally.xml', xml_declaration=True, encoding='utf-8', method="xml")