Moved #313 to update_inputs.py

This commit is contained in:
Sterling Harper 2015-02-13 14:20:48 -05:00
parent ad9509dc15
commit 88e80715c5
3 changed files with 27 additions and 112 deletions

View file

@ -21,6 +21,7 @@ optional arguments:
from __future__ import print_function
import argparse
from itertools import chain
from random import randint
from shutil import move
import xml.etree.ElementTree as ET
@ -180,13 +181,14 @@ def update_geometry(geometry_root):
cids = get_cell_ids(root)
taken_ids = uids.union(cids)
# Update the definitions of each lattice
for lat in root.iter('lattice'):
# Replace 'outside' with 'outer' in lattices.
for lat in chain(root.iter('lattice'), root.iter('hex_lattice')):
# Get the lattice's id.
lat_id = get_lat_id(lat)
# Ignore lattices that have 'outer' specified.
if any([child.tag == 'outer' for child in lat]): continue
if 'outer' in lat.attrib: continue
# Pop the 'outside' material.
material = pop_lat_outside(lat)
@ -210,6 +212,27 @@ def update_geometry(geometry_root):
was_updated = True
# Remove 'type' from lattice definitions.
for lat in root.iter('lattice'):
elem = lat.find('type')
if elem is not None:
lat.remove(elem)
was_updated = True
if 'type' in lat.attrib:
del lat.attrib['type']
was_updated = True
# Change 'width' to 'pitch' in lattice definitions.
for lat in root.iter('lattice'):
elem = lat.find('width')
if elem is not None:
elem.tag = 'pitch'
was_updated = True
if 'width' in lat.attrib:
lat.attrib['pitch'] = lat.attrib['width']
del lat.attrib['width']
was_updated = True
return was_updated

View file

@ -1,107 +0,0 @@
#!/usr/bin/env python
"""Update lattices in geometry .xml files to the post-hex_lattice format.
Usage information can be obtained by running 'track.py --help':
usage: update_lattices.py [-h] IN [IN ...]
Update lattices in geometry.xml files to the latest format.
positional arguments:
IN Input geometry.xml file(s).
optional arguments:
-h, --help show this help message and exit
"""
from __future__ import print_function
import argparse
def parse_args():
"""Read the input files from the commandline."""
# Create argument parser.
parser = argparse.ArgumentParser(description=\
'Update lattices in geometry.xml files to the latest format.')
parser.add_argument('input', metavar='IN', type=str, nargs='+',
help='Input geometry.xml file(s).')
# Parse and return commandline arguments.
return parser.parse_args()
def read_file(fname):
"""Open a file and return a list of lines."""
fin = open(fname)
lines = fin.readlines()
fin.close()
return lines
def process_lattice(lines):
"""Delete 'type' and replace 'width' with 'pitch' in lattice string."""
assert 'type' in lines
assert 'width' in lines
# Delete the 'type' attribute or tag.
if '<type>' in lines:
# 'type' is a subelement.
start = lines.index('<type>')
end = lines.index('</type>') + 6
lines = ''.join([lines[:start], lines[end+1:]])
else:
# 'type' is an attribute.
start = lines.index('type')
end = lines.index('rectangular') + 11 # adjusted to include end quote
lines = ''.join([lines[:start], lines[end+1:]])
# Change 'width' to 'pitch'.
lines = lines.replace('width', 'pitch')
return lines
def process_xml(file_lines):
"""Update the lattices in the given geometry.xml lines."""
# Join the list of lines into one big string to simplify the indexing of
# the xml tabs.
lines = ''.join(file_lines)
# Seperate the lattice elements.
lats = lines.split('<lattice')
# Add the lattice tags back to the lattices.
for i in range(1, len(lats)):
lats[i] = ''.join(['<lattice', lats[i]])
# Update the lattices.
for i in range(1, len(lats)):
lats[i] = process_lattice(lats[i])
# Put the xml back together into one big string.
lines = ''.join(lats)
# Seperate the xml back into the original lines.
lines = lines.split('\n')
# Add the newline characters back on to the end of each line.
for i in range(len(lines) - 1):
lines[i] = ''.join([lines[i], '\n'])
return lines
if __name__ == '__main__':
args = parse_args()
for fname in args.input:
input_lines = read_file(fname)
# Process files with lattices.
if any(['<lattice' in x for x in input_lines]):
output_lines = process_xml(input_lines)
# Leave files without lattices unchanged.
else:
output_lines = input_lines
fout = open(fname, mode='w')
fout.write(''.join(output_lines))
fout.close()

View file

@ -3,14 +3,13 @@
<cell id="11" universe="11" material="1"/>
<cell id="12" universe="12" material="2" surfaces=""/>
<lattice id="21" type="rect" dimension="2 2" lower_left="-2.0 -2.0"
width="2.0 2.0" outer="22">
<lattice id="21" dimension="2 2" lower_left="-2.0 -2.0"
pitch="2.0 2.0" outer="12">
<universes>
11 12
12 11
</universes>
</lattice>
<cell id="22" universe="22" material="2"/>
<surface id="101" type="z-cylinder" coeffs="0.0 0.0 5.0" boundary="vacuum"/>