diff --git a/openmc/geometry.py b/openmc/geometry.py index e939ecbe37..260bb9e83c 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -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): diff --git a/openmc/material.py b/openmc/material.py index f8f122308d..42c56acedb 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -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'): diff --git a/openmc/model/model.py b/openmc/model/model.py index b6a89792de..cf6603638e 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -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 diff --git a/openmc/plots.py b/openmc/plots.py index f99392def9..eb582502e4 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -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') diff --git a/openmc/settings.py b/openmc/settings.py index 23450d98d3..4be2eaaa31 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -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') diff --git a/openmc/tallies.py b/openmc/tallies.py index 5bc6494bb5..291090b196 100644 --- a/openmc/tallies.py +++ b/openmc/tallies.py @@ -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')