Allow export_to_xml methods to take a directory

This commit is contained in:
Paul Romano 2019-03-12 09:58:35 -05:00
parent 830bd4f8e0
commit 17d778e57f
6 changed files with 56 additions and 15 deletions

View file

@ -97,9 +97,14 @@ class Geometry(object):
# 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(path, xml_declaration=True, encoding='utf-8')
tree.write(str(p), xml_declaration=True, encoding='utf-8')
@classmethod
def from_xml(cls, path='geometry.xml', materials=None):

View file

@ -1,6 +1,7 @@
from collections import OrderedDict
from copy import deepcopy
from numbers import Real, Integral
from pathlib import Path
import warnings
from xml.etree import ElementTree as ET
@ -1065,9 +1066,14 @@ class Materials(cv.CheckedList):
# 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(path, xml_declaration=True, encoding='utf-8')
tree.write(str(p), xml_declaration=True, encoding='utf-8')
@classmethod
def from_xml(cls, path='materials.xml'):

View file

@ -1,4 +1,5 @@
from collections.abc import Iterable
from pathlib import Path
import openmc
from openmc.checkvalue import check_type, check_value
@ -154,27 +155,39 @@ class Model(object):
'si_celi', 'si_leqi', 'celi', 'leqi'))
getattr(dep.integrator, method)(op, timesteps, **kwargs)
def export_to_xml(self):
"""Export model to XML files."""
def export_to_xml(self, directory='.'):
"""Export model to XML files.
self.settings.export_to_xml()
Parameters
----------
directory : str
Directory to write XML files to. If it doesn't exist already, it
will be created.
"""
# Create directory if
d = Path(directory)
if not d.is_dir():
d.mkdir(parents=True)
self.settings.export_to_xml(d)
if not self.settings.dagmc:
self.geometry.export_to_xml()
self.geometry.export_to_xml(d)
# If a materials collection was specified, export it. Otherwise, look
# for all materials in the geometry and use that to automatically build
# a collection.
if self.materials:
self.materials.export_to_xml()
self.materials.export_to_xml(d)
else:
materials = openmc.Materials(self.geometry.get_all_materials()
.values())
materials.export_to_xml()
materials.export_to_xml(d)
if self.tallies:
self.tallies.export_to_xml()
self.tallies.export_to_xml(d)
if self.plots:
self.plots.export_to_xml()
self.plots.export_to_xml(d)
def run(self, **kwargs):
"""Creates the XML files, runs OpenMC, and returns k-effective

View file

@ -1,9 +1,10 @@
from collections.abc import Iterable, Mapping
from numbers import Real, Integral
from xml.etree import ElementTree as ET
from pathlib import Path
import subprocess
import sys
import warnings
from xml.etree import ElementTree as ET
import numpy as np
@ -818,6 +819,11 @@ class Plots(cv.CheckedList):
# 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(path, xml_declaration=True, encoding='utf-8', method="xml")
tree.write(str(p), xml_declaration=True, encoding='utf-8')

View file

@ -1,4 +1,5 @@
from collections.abc import Iterable, MutableSequence, Mapping
from pathlib import Path
from numbers import Real, Integral
import warnings
from xml.etree import ElementTree as ET
@ -995,6 +996,11 @@ class Settings(object):
# 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(path, xml_declaration=True, encoding='utf-8', method="xml")
tree.write(str(p), xml_declaration=True, encoding='utf-8')

View file

@ -5,6 +5,7 @@ from functools import partial, reduce
from itertools import product
from numbers import Integral, Real
import operator
from pathlib import Path
import warnings
from xml.etree import ElementTree as ET
@ -3189,7 +3190,11 @@ class Tallies(cv.CheckedList):
# 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 /= 'tallies.xml'
# Write the XML Tree to the tallies.xml file
tree = ET.ElementTree(root_element)
tree.write(path, xml_declaration=True,
encoding='utf-8', method="xml")
tree.write(str(p), xml_declaration=True, encoding='utf-8')