Skip to content

Instantly share code, notes, and snippets.

@janmalec
Last active January 2, 2021 07:11
Show Gist options
  • Save janmalec/b55d4273e9361b505077b63d110466cf to your computer and use it in GitHub Desktop.
Save janmalec/b55d4273e9361b505077b63d110466cf to your computer and use it in GitHub Desktop.
Reads a serpent input file and writes an OpenMC input file for materials. Usage: `python3 serpent_openmc_mat_converter.py -i <inputfile> -o <outputfile>`
import sys, getopt
elements = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg',
'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr',
'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br',
'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd',
'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La',
'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er',
'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au',
'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th',
'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md',
'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn',
'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og']
def iso_from_no(no):
az = int(no.split('.')[0])
a = int(az/1000)
z = az%1000
el = elements[a-1]
return el + str(z)
def both_print(string, file):
print(string)
if file:
file.write(string + '\n')
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv, "hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print('python3 serpent_openmc_mat_converter.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python3 serpent_openmc_mat_converter.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
ofile = open('output', 'w')
if inputfile == '':
print('python3 serpent_openmc_mat_converter.py -i <inputfile> -o <outputfile>')
sys.exit()
print('Input file is "', inputfile)
print('Output file is "', ofile)
reading_mat = False
for line in open(inputfile, 'r'):
if line.startswith('mat'):
reading_mat = True
ls = line.split()
matname = ls[1]
dens = float(ls[2])
both_print('{} = openmc.Material()'.format(matname), ofile)
if dens < 0:
both_print('{}.set_density(\'g/cm3\', {})'.format(matname, -dens), ofile)
if 'tmp' in ls:
tmp = float(ls[ls.index('tmp')+1])
both_print('{}.temperature = {}'.format(matname, tmp), ofile)
if reading_mat:
ls = line.split()
if len(ls) == 2:
el = iso_from_no(ls[0])
fract = float(ls[1])
if fract > 0:
both_print('{}.add_nuclide( \'{}\', {}, \'{}\')'.format(
matname, el, fract, 'ao'), ofile)
if fract < 0:
both_print('{}.add_nuclide( \'{}\', {}, \'{}\')'.format(
matname, el, -fract, 'wo'), ofile)
if len(ls) == 0:
reading_mat = False
both_print('', ofile)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment