Make each filter type a separate class in PyAPI

This commit is contained in:
Sterling Harper 2016-08-10 21:45:32 -05:00
parent 4edede450e
commit 9ce02de39a
4 changed files with 595 additions and 542 deletions

File diff suppressed because it is too large Load diff

View file

@ -397,30 +397,10 @@ class StatePoint(object):
subbase = '{0}{1}/filter '.format(base, tally_key)
# Initialize all Filters
# Read all filters
for j in range(1, n_filters+1):
# Read the Filter type
filter_type = \
self._f['{0}{1}/type'.format(subbase, j)].value.decode()
n_bins = self._f['{0}{1}/n_bins'.format(subbase, j)].value
# Read the bin values
bins = self._f['{0}{1}/bins'.format(subbase, j)].value
# Create Filter object
new_filter = openmc.Filter(filter_type, bins)
new_filter.num_bins = n_bins
if filter_type == 'mesh':
mesh_ids = self._f['tallies/meshes/ids'].value
mesh_keys = self._f['tallies/meshes/keys'].value
key = mesh_keys[mesh_ids == bins][0]
new_filter.mesh = self.meshes[key]
# Add Filter to the Tally
subsubbase = '{0}{1}'.format(subbase, j)
new_filter = openmc.Filter.from_hdf5(self._f[subsubbase])
tally.filters.append(new_filter)
# Read Nuclide bins
@ -676,31 +656,33 @@ class StatePoint(object):
tally.with_summary = True
for tally_filter in tally.filters:
summary_filter = summary_tally.find_filter(tally_filter.type)
summary_filter = summary_tally.find_filter(
tally_filter.short_name)
if tally_filter.type == 'surface':
if isinstance(tally_filter, openmc.SurfaceFilter):
surface_ids = []
for bin in tally_filter.bins:
surface_ids.append(summary.surfaces[bin].id)
tally_filter.bins = surface_ids
if tally_filter.type in ['cell', 'distribcell']:
if isinstance(tally_filter, (openmc.CellFilter,
openmc.DistribcellFilter)):
distribcell_ids = []
for bin in tally_filter.bins:
distribcell_ids.append(summary.cells[bin].id)
tally_filter.bins = distribcell_ids
if tally_filter.type == 'distribcell':
if isinstance(tally_filter, (openmc.DistribcellFilter)):
tally_filter.distribcell_paths = \
summary_filter.distribcell_paths
if tally_filter.type == 'universe':
if isinstance(tally_filter, openmc.UniverseFilter):
universe_ids = []
for bin in tally_filter.bins:
universe_ids.append(summary.universes[bin].id)
tally_filter.bins = universe_ids
if tally_filter.type == 'material':
if isinstance(tally_filter, openmc.MaterialFilter):
material_ids = []
for bin in tally_filter.bins:
material_ids.append(summary.materials[bin].id)

View file

@ -567,28 +567,10 @@ class Summary(object):
# Read filter metadata
num_filters = self._f['{0}/n_filters'.format(subbase)].value
# Initialize all Filters
# Read all filters
for j in range(1, num_filters+1):
subsubbase = '{0}/filter {1}'.format(subbase, j)
# Read filter type (e.g., "cell", "energy", etc.)
filter_type = self._f['{0}/type'.format(subsubbase)].value.decode()
# Read the filter bins
num_bins = self._f['{0}/n_bins'.format(subsubbase)].value
bins = self._f['{0}/bins'.format(subsubbase)][...]
# Create Filter object
new_filter = openmc.Filter(filter_type, bins)
new_filter.num_bins = num_bins
# Read in distribcell paths
if filter_type == 'distribcell':
paths = self._f['{0}/paths'.format(subsubbase)][...]
paths = [str(path.decode()) for path in paths]
new_filter.distribcell_paths = paths
# Add Filter to the Tally
new_filter = openmc.Filter.from_hdf5(self._f[subsubbase])
tally.filters.append(new_filter)
# Add Tally to the global dictionary of all Tallies

View file

@ -13,10 +13,10 @@ from xml.etree import ElementTree as ET
import numpy as np
from openmc import Filter, Trigger, Nuclide
from openmc import Trigger, Nuclide
from openmc.arithmetic import CrossScore, CrossNuclide, CrossFilter, \
AggregateScore, AggregateNuclide, AggregateFilter
from openmc.filter import _FILTER_TYPES
from openmc.filter import Filter, MeshFilter
import openmc.checkvalue as cv
from openmc.clean_xml import clean_xml_indentation
@ -178,8 +178,8 @@ class Tally(object):
string += '{0: <16}{1}\n'.format('\tFilters', '=\t')
for self_filter in self.filters:
string += '{0: <16}\t\t{1}\t{2}\n'.format('', self_filter.type,
self_filter.bins)
string += '{0: <16}\t\t{1}\t{2}\n'.format('',
type(self_filter).__name__, self_filter.bins)
string += '{0: <16}{1}'.format('\tNuclides', '=\t')
@ -1021,15 +1021,7 @@ class Tally(object):
# Optional Tally filters
for self_filter in self.filters:
subelement = ET.SubElement(element, "filter")
subelement.set("type", str(self_filter.type))
if self_filter.bins is not None:
bins = ''
for bin in self_filter.bins:
bins += '{0} '.format(bin)
subelement.set("bins", bins.rstrip(' '))
element.append(self_filter.to_xml())
# Optional Nuclides
if len(self.nuclides) > 0:
@ -1119,7 +1111,7 @@ class Tally(object):
# Look through all of this Tally's Filters for the type requested
for test_filter in self.filters:
if test_filter.type == filter_type:
if test_filter.short_name.lower() == filter_type.lower():
filter_found = test_filter
break
@ -3548,13 +3540,14 @@ class Tallies(cv.CheckedList):
already_written = set()
for tally in self:
for f in tally.filters:
if f.type == 'mesh' and f.mesh not in already_written:
if len(f.mesh.name) > 0:
self._tallies_file.append(ET.Comment(f.mesh.name))
if isinstance(f, MeshFilter):
if f.mesh not in already_written:
if len(f.mesh.name) > 0:
self._tallies_file.append(ET.Comment(f.mesh.name))
xml_element = f.mesh.get_mesh_xml()
self._tallies_file.append(xml_element)
already_written.add(f.mesh)
xml_element = f.mesh.get_mesh_xml()
self._tallies_file.append(xml_element)
already_written.add(f.mesh)
def export_to_xml(self):
"""Create a tallies.xml file that can be used for a simulation.