Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions csg2csg/CellCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def __init__(self,card_string):
self.cell_material_number = 0
self.cell_importance = 1 # note any importance - we assume everything else is 0
self.cell_text_description = ""
self.cell_interpreted = ""
self.cell_fill = 0
self.cell_interpreted = "" #this it the generalised form of the cell
self.cell_fill = 0 # note fill could corresepond to a universe or a lattice fill
self.cell_universe = 0
self.cell_universe_offset = 0
self.cell_universe_rotation = 0
Expand Down
2 changes: 1 addition & 1 deletion csg2csg/MCNPCellCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def generalise(self):
self.cell_interpreted = cell_description
#print(self.cell_id)
#print(self.cell_interpreted)
#logging.debug("%s\n", "Generalised cell card " + ''.join([str(i) for i in self.cell_interpreted]))
logging.debug("%s\n", "Generalised cell card " + ''.join([str(i) for i in self.cell_interpreted]))

return

Expand Down
16 changes: 10 additions & 6 deletions csg2csg/MCNPInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,14 +838,18 @@ def __get_cell_cards(self):
# set the boundary conditions
def __apply_boundary_conditions(self):

# apply the importances to cells
if len(self.importance_list) != 0:
for particle in self.importance_list:
importances = self.importance_list[particle].split()
for idx,value in enumerate(importances):
# TODO this needs to be multi particle
self.cell_list[idx].cell_importance = float(value)
# apply the importances to cells
#if len(self.importance_list) != 0:
# TODO make this loop apply to multiple particle
# types but for now just do neutrons
if len(self.importance_list[ParticleNames["NEUTRON"]]) != 0:
importances = self.importance_list[ParticleNames["NEUTRON"]].split()
for idx,value in enumerate(importances):
self.cell_list[idx].cell_importance = float(value)
# if len(self.importance_list[ParticleNames["NEUTRON"]]) != 0:
# importances = self.importance_list[ParticleNames["NEUTRON"]].split()


# loop over the cells and if the cell has
# importance 0, all the sufaces get boundary
Expand Down
57 changes: 56 additions & 1 deletion csg2csg/OpenMCCell.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,64 @@ def write_openmc_cell(cell, geometry_tree):
universe = str(universe))


# take the xml attributes and populate the
# cell
def cell_from_attribute(xml_attribute):
cell = OpenMCCell("")
cell.cell_id = xml_attribute["id"]

# todo if name based materials are used will need
# a helper function

if "material" in xml_attribute:
if xml_attribute["material"] == "void":
cell.cell_material_number = 0
else:
cell.cell_material_number = xml_attribute["material"]
else:
cell.cell_material_number = 0

cell.cell_text_description = xml_attribute["region"]
if "universe" in xml_attribute:
cell.cell_universe = xml_attribute["universe"]
if "fill" in xml_attribute:
cell.cell_fill = xml_attribute["fill"]

cell.generalise()
return cell

#
# base constructor
class OpenMCCell(CellCard):
def __init__(self, card_string):
CellCard.__init__(self, card_string)

# turn the text representation of the cell
# into a generic description
def generalise(self):
# make an interable list of the components
cell_description = list(self.cell_text_description)
# first lets sanisise the text description - remove
# double spaces trailing and leading white space
idx = 0
while True:
# breakout condition
if idx >= len(cell_description):
break
# part of the cell we're looking at
s = cell_description[idx]
if s is "|":
cell_description[idx] = CellCard.OperationType["UNION"]
idx += 1
continue
elif s is "~":
cell_description[idx] = CellCard.OperationType["NOT"]
idx += 1
continue
elif s is " ":
cell_description[idx] = CellCard.OperationType["AND"]
idx += 1
continue
idx += 1
# set the generalised cell description
self.cell_interpreted = cell_description
return
96 changes: 90 additions & 6 deletions csg2csg/OpenMCInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import sys
import xml.etree.ElementTree as ET

from csg2csg.OpenMCSurface import write_openmc_surface
from csg2csg.OpenMCCell import write_openmc_cell
from csg2csg.OpenMCMaterial import write_openmc_material
from csg2csg.OpenMCSurface import SurfaceCard,surface_from_attribute, write_openmc_surface
from csg2csg.OpenMCCell import cell_from_attribute, write_openmc_cell
from csg2csg.OpenMCMaterial import material_from_attribute, write_openmc_material
from csg2csg.OpenMCLattice import lattice_from_attribute, write_openmc_lattice

'''
copy and paste from http://effbot.org/zone/element-lib.htm#prettyprint
it basically walks your tree and adds spaces and newlines so the tree is
Expand All @@ -35,8 +37,83 @@ class OpenMCInput(InputDeck):
xml elements directly
"""
# constructor
def __init__(self, filename = ""):
InputDeck.__init__(self, filename)
def __init__(self,geometry_file="geometry.xml",material_file="materials.xml"):
self.geometry = geometry_file
self.material = material_file
self.xml_geom = None
self.xml_material = None
# make a new input deck
InputDeck.__init__(self,filename="")

# read the openmc files
def read(self):
self.xml_geom = self.__read_geometry_xml(self.geometry)
self.xml_material = self.__read_material_xml(self.material)

# read the geometry file
def __read_geometry_xml(self, geometry_filename):
geom = ET.parse(geometry_filename)
geom_root = geom.getroot()
return geom_root

# read the material file
def __read_material_xml(self, material_filename):
mat = ET.parse(material_filename)
mat_root = mat.getroot()
return mat_root

# process the files
def process(self):
# do materials first
self.__process_materials()
self.__process_geometry()


# process the geometry
def __process_geometry(self):

for child in self.xml_geom:
if child.tag == "surface":
surface = surface_from_attribute(child.attrib)
InputDeck.surface_list.append(surface)
elif child.tag == "cell":
cell = cell_from_attribute(child.attrib)
InputDeck.cell_list.append(cell)
elif child.tag == "lattice":
lattice = lattice_from_attribute('lattice', child.attrib, child.getchildren())
InputDeck.lattice_list.append(lattice)
elif child.tag == "hex_lattice":
lattice = lattice_from_attribute('hex_latice', child.attrib, child.getchildren())
InputDeck.lattice_list.append(lattice)


# loop over the cells and set the material
# density for each cell
for cell in InputDeck.cell_list:
cell_mat = cell.cell_material_number
if cell_mat is not 0:
density = InputDeck.material_list[cell_mat].density
cell.cell_density = density

# identify the cells that have a surface with a vacuum
# boundary condition, they mark the end of the universe
boundary_surfs = []
for surface in InputDeck.surface_list:
if surface.boundary_condition == SurfaceCard.BoundaryCondition["VACUUM"]:
boundary_surfs.append(surface.surface_id)

# now loop through the cells and mark as importance 0 where
# appropriate
for cell in InputDeck.cell_list:
if set(boundary_surfs) & set(cell.cell_interpreted) == set(boundary_surfs):
cell.cell_importance = 0

# process the materials
def __process_materials(self):
for child in self.xml_material:
if child.tag == "material":
material = material_from_attribute(child.attrib, child.getchildren())
InputDeck.material_list[material.material_number] = material

# write the collection of OpenMC surface definitions
def __write_openmc_surfaces(self, geometry_tree):
Expand All @@ -47,7 +124,13 @@ def __write_openmc_surfaces(self, geometry_tree):
def __write_openmc_cells(self, geometry_tree):
for cell in self.cell_list:
write_openmc_cell(cell, geometry_tree)


# write the collection of OpenMC lattice definitions
def __write_openmc_lattices(self, geometry_tree):
for lattice in self.lattice_list:
write_openmc_lattice(lattice, geometry_tree)


# write the collection of Material
def __write_openmc_materials(self, material_tree):
for mat in self.material_list:
Expand Down Expand Up @@ -100,6 +183,7 @@ def write_openmc(self, filename, flat = True):

self.__write_openmc_surfaces(geometry)
self.__write_openmc_cells(geometry)
self.__write_openmc_lattices(geometry)
self.__check_unused_universes(geometry)

tree = ET.ElementTree(geometry)
Expand Down
58 changes: 58 additions & 0 deletions csg2csg/OpenMCMaterial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/env/python3

import re
from csg2csg.MaterialCard import MaterialCard
import xml.etree.ElementTree as ET

Expand All @@ -24,6 +25,25 @@
109:"Mt",110:"Ds",111:"Rg",112:"Cn",113:"Nh",
114:"Fl",115:"Mc",116:"Lv",117:"Ts",118:"Og"}

# return the zz given a name
def get_zaid(name):
m = re.search('[A-Z]?[a-z]*',name)
element_name = m.group(0)
m = re.search('[0-9]?[0-9]?[0-9]',name)
nucleon_number = int(m.group(0))

for zz,el in name_zaid.items():
if el == element_name:
break

# pad the right number of zeros
if nucleon_number < 10:
nucleon_number = "00" + str(nucleon_number)
elif nucleon_number > 10 and nucleon_number < 100:
nucleon_number = "0" + str(nucleon_number)

return str(zz)+str(nucleon_number)

# convert zaid to a name for openmc
def zaid_to_name(zaid_string):
if len(zaid_string) <= 4:
Expand All @@ -39,8 +59,46 @@ def zaid_to_name(zaid_string):
# turn zz into a name

name = name_zaid[zz]

return name+str(aa)

################## input functions #########################
#
def material_from_attribute(xml_element, children):
#
material = MaterialCard()
material.material_number = xml_element["id"]
# loop over the constituents
nucs = {}
atom_fraction = False

for child in children:
# set the density
if "units" in child.attrib:
units = child.attrib["units"]
density = float(child.attrib["value"])
# set the material parameters
if "name" in child.attrib:
nucid = child.attrib["name"]
zaid = get_zaid(nucid)
if "wo" in child.attrib:
# mass fractions are -ve
nucs[zaid] = float(-1*float(child.attrib["wo"]))
if "ao" in child.attrib:
atom_fraction = True
nucs[zaid] = float(child.attrib["ao"])

if not atom_fraction: density = density*-1.0

material.density = density
material.density_units = units
material.composition_dictionary = nucs

return material


################## output functions #########################

# write the atomic fraction entry
def __write_atomic_fraction(material, nuclide, mass_frac):
ET.SubElement(material, "nuclide", name = nuclide, ao = str(abs(mass_frac)))
Expand Down
Loading