diff --git a/docs/source/pythonapi/examples/mgxs-part-i.ipynb b/docs/source/pythonapi/examples/mgxs-part-i.ipynb index c7a5b2ffa..a450af97e 100644 --- a/docs/source/pythonapi/examples/mgxs-part-i.ipynb +++ b/docs/source/pythonapi/examples/mgxs-part-i.ipynb @@ -212,10 +212,9 @@ }, "outputs": [], "source": [ - "# Instantiate a Materials object, register all Materials, and export to XML\n", - "materials_file = openmc.Materials()\n", + "# Instantiate a Materials collection and export to XML\n", + "materials_file = openmc.Materials([inf_medium])\n", "materials_file.default_xs = '71c'\n", - "materials_file.add_material(inf_medium)\n", "materials_file.export_to_xml()" ] }, @@ -466,17 +465,14 @@ "tallies_file = openmc.Tallies()\n", "\n", "# Add total tallies to the tallies file\n", - "for tally in total.tallies.values():\n", - " tallies_file.add_tally(tally)\n", + "tallies_file += total.tallies.values()\n", "\n", "# Add absorption tallies to the tallies file\n", - "for tally in absorption.tallies.values():\n", - " tallies_file.add_tally(tally)\n", + "tallies_file += absorption.tallies.values()\n", "\n", "# Add scattering tallies to the tallies file\n", - "for tally in scattering.tallies.values():\n", - " tallies_file.add_tally(tally)\n", - " \n", + "tallies_file += scattering.tallies.values()\n", + "\n", "# Export to \"tallies.xml\"\n", "tallies_file.export_to_xml()" ] diff --git a/docs/source/pythonapi/examples/mgxs-part-ii.ipynb b/docs/source/pythonapi/examples/mgxs-part-ii.ipynb index 49e301f5b..793d88436 100644 --- a/docs/source/pythonapi/examples/mgxs-part-ii.ipynb +++ b/docs/source/pythonapi/examples/mgxs-part-ii.ipynb @@ -133,11 +133,8 @@ }, "outputs": [], "source": [ - "# Instantiate a Materials object, add Materials\n", - "materials_file = openmc.Materials()\n", - "materials_file.add_material(fuel)\n", - "materials_file.add_material(water)\n", - "materials_file.add_material(zircaloy)\n", + "# Instantiate a Materials collection\n", + "materials_file = openmc.Materials((fuel, water, zircaloy))\n", "materials_file.default_xs = '71c'\n", "\n", "# Export to \"materials.xml\"\n", @@ -408,7 +405,7 @@ " \n", " # Add OpenMC tallies to the tallies file for XML generation\n", " for tally in xs_library[cell.id][rxn_type].tallies.values():\n", - " tallies_file.add_tally(tally, merge=True)\n", + " tallies_file.append(tally, merge=True)\n", "\n", "# Export to \"tallies.xml\"\n", "tallies_file.export_to_xml()" diff --git a/docs/source/pythonapi/examples/mgxs-part-iii.ipynb b/docs/source/pythonapi/examples/mgxs-part-iii.ipynb index 3a3533ffe..34190371b 100644 --- a/docs/source/pythonapi/examples/mgxs-part-iii.ipynb +++ b/docs/source/pythonapi/examples/mgxs-part-iii.ipynb @@ -133,11 +133,8 @@ }, "outputs": [], "source": [ - "# Instantiate a Materials object, add Materials\n", - "materials_file = openmc.Materials()\n", - "materials_file.add_material(fuel)\n", - "materials_file.add_material(water)\n", - "materials_file.add_material(zircaloy)\n", + "# Instantiate a Materials object\n", + "materials_file = openmc.Materials((fuel, water, zircaloy))\n", "materials_file.default_xs = '71c'\n", "\n", "# Export to \"materials.xml\"\n", @@ -419,8 +416,7 @@ "plot.color = 'mat'\n", "\n", "# Instantiate a Plots object, add Plot, and export to \"plots.xml\"\n", - "plot_file = openmc.Plots()\n", - "plot_file.add_plot(plot)\n", + "plot_file = openmc.Plots([plot])\n", "plot_file.export_to_xml()" ] }, @@ -685,9 +681,8 @@ "tally.filters = [mesh_filter]\n", "tally.scores = ['fission', 'nu-fission']\n", "\n", - "# Add mesh and tally to Tallies\n", - "tallies_file.add_mesh(mesh)\n", - "tallies_file.add_tally(tally)" + "# Add tally to collection\n", + "tallies_file.append(tally)" ] }, { diff --git a/docs/source/pythonapi/examples/pandas-dataframes.ipynb b/docs/source/pythonapi/examples/pandas-dataframes.ipynb index b0f2f6b13..d5e8b9861 100644 --- a/docs/source/pythonapi/examples/pandas-dataframes.ipynb +++ b/docs/source/pythonapi/examples/pandas-dataframes.ipynb @@ -108,11 +108,8 @@ }, "outputs": [], "source": [ - "# Instantiate a Materials object, add Materials\n", - "materials_file = openmc.Materials()\n", - "materials_file.add_material(fuel)\n", - "materials_file.add_material(water)\n", - "materials_file.add_material(zircaloy)\n", + "# Instantiate a Materials collection\n", + "materials_file = openmc.Materials((fuel, water, zircaloy))\n", "materials_file.default_xs = '71c'\n", "\n", "# Export to \"materials.xml\"\n", @@ -329,9 +326,8 @@ "plot.pixels = [250, 250]\n", "plot.color = 'mat'\n", "\n", - "# Instantiate a Plots object, add Plot, and export to \"plots.xml\"\n", - "plot_file = openmc.Plots()\n", - "plot_file.add_plot(plot)\n", + "# Instantiate a Plots collection and export to \"plots.xml\"\n", + "plot_file = openmc.Plots([plot])\n", "plot_file.export_to_xml()" ] }, @@ -449,8 +445,7 @@ "tally.scores = ['fission', 'nu-fission']\n", "\n", "# Add mesh and Tally to Tallies\n", - "tallies_file.add_mesh(mesh)\n", - "tallies_file.add_tally(tally)" + "tallies_file.append(tally)" ] }, { @@ -478,7 +473,7 @@ "tally.nuclides = [u235, u238]\n", "\n", "# Add mesh and tally to Tallies\n", - "tallies_file.add_tally(tally)" + "tallies_file.append(tally)" ] }, { @@ -510,7 +505,7 @@ "tally.triggers = [trigger]\n", "\n", "# Add mesh and tally to Tallies\n", - "tallies_file.add_tally(tally)" + "tallies_file.append(tally)" ] }, { diff --git a/docs/source/pythonapi/examples/post-processing.ipynb b/docs/source/pythonapi/examples/post-processing.ipynb index ce9209b03..36cf63c6a 100644 --- a/docs/source/pythonapi/examples/post-processing.ipynb +++ b/docs/source/pythonapi/examples/post-processing.ipynb @@ -104,11 +104,8 @@ }, "outputs": [], "source": [ - "# Instantiate a Materials object, add Materials\n", - "materials_file = openmc.Materials()\n", - "materials_file.add_material(fuel)\n", - "materials_file.add_material(water)\n", - "materials_file.add_material(zircaloy)\n", + "# Instantiate a Materials collection\n", + "materials_file = openmc.Materials((fuel, water, zircaloy))\n", "materials_file.default_xs = '71c'\n", "\n", "# Export to \"materials.xml\"\n", @@ -298,9 +295,8 @@ "plot.pixels = [250, 250]\n", "plot.color = 'mat'\n", "\n", - "# Instantiate a Plots object, add Plot, and export to \"plots.xml\"\n", - "plot_file = openmc.Plots()\n", - "plot_file.add_plot(plot)\n", + "# Instantiate a Plots collection and export to \"plots.xml\"\n", + "plot_file = openmc.Plots([plot])\n", "plot_file.export_to_xml()" ] }, @@ -393,17 +389,16 @@ "mesh.dimension = [100, 100]\n", "mesh.lower_left = [-0.63, -0.63]\n", "mesh.upper_right = [0.63, 0.63]\n", - "tallies_file.add_mesh(mesh)\n", "\n", "# Create mesh filter for tally\n", - "mesh_filter = openmc.Filter(type='mesh', bins=[1])\n", + "mesh_filter = openmc.Filter(type='mesh')\n", "mesh_filter.mesh = mesh\n", "\n", "# Create mesh tally to score flux and fission rate\n", "tally = openmc.Tally(name='flux')\n", "tally.filters = [mesh_filter]\n", "tally.scores = ['flux', 'fission']\n", - "tallies_file.add_tally(tally)" + "tallies_file.append(tally)" ] }, { diff --git a/docs/source/pythonapi/examples/tally-arithmetic.ipynb b/docs/source/pythonapi/examples/tally-arithmetic.ipynb index 81334efc2..14ca97d3f 100644 --- a/docs/source/pythonapi/examples/tally-arithmetic.ipynb +++ b/docs/source/pythonapi/examples/tally-arithmetic.ipynb @@ -126,11 +126,8 @@ }, "outputs": [], "source": [ - "# Instantiate a Materials object, add Materials\n", - "materials_file = openmc.Materials()\n", - "materials_file.add_material(fuel)\n", - "materials_file.add_material(water)\n", - "materials_file.add_material(zircaloy)\n", + "# Instantiate a Materials collection\n", + "materials_file = openmc.Materials((fuel, water, zircaloy))\n", "materials_file.default_xs = '71c'\n", "\n", "# Export to \"materials.xml\"\n", @@ -321,9 +318,8 @@ "plot.pixels = [250, 250]\n", "plot.color = 'mat'\n", "\n", - "# Instantiate a Plots object, add Plot, and export to \"plots.xml\"\n", - "plot_file = openmc.Plots()\n", - "plot_file.add_plot(plot)\n", + "# Instantiate a Plots collection and export to \"plots.xml\"\n", + "plot_file = openmc.Plots([plot])\n", "plot_file.export_to_xml()" ] }, @@ -421,7 +417,7 @@ "tally.filters = [openmc.Filter(type='cell', bins=[fuel_cell.id, moderator_cell.id])]\n", "tally.filters.append(energy_filter)\n", "tally.scores = ['flux']\n", - "tallies_file.add_tally(tally)\n", + "tallies_file.append(tally)\n", "\n", "# Instantiate reaction rate Tally in fuel\n", "tally = openmc.Tally(name='fuel rxn rates')\n", @@ -429,7 +425,7 @@ "tally.filters.append(energy_filter)\n", "tally.scores = ['nu-fission', 'scatter']\n", "tally.nuclides = [u238, u235]\n", - "tallies_file.add_tally(tally)\n", + "tallies_file.append(tally)\n", "\n", "# Instantiate reaction rate Tally in moderator\n", "tally = openmc.Tally(name='moderator rxn rates')\n", @@ -437,7 +433,7 @@ "tally.filters.append(energy_filter)\n", "tally.scores = ['absorption', 'total']\n", "tally.nuclides = [o16, h1]\n", - "tallies_file.add_tally(tally)" + "tallies_file.append(tally)" ] }, { @@ -453,8 +449,7 @@ "abs_rate = openmc.Tally(name='abs. rate')\n", "fiss_rate.scores = ['nu-fission']\n", "abs_rate.scores = ['absorption']\n", - "tallies_file.add_tally(fiss_rate)\n", - "tallies_file.add_tally(abs_rate)" + "tallies_file += (fiss_rate, abs_rate)", ] }, { @@ -469,7 +464,7 @@ "therm_abs_rate = openmc.Tally(name='therm. abs. rate')\n", "therm_abs_rate.scores = ['absorption']\n", "therm_abs_rate.filters = [openmc.Filter(type='energy', bins=[0., 0.625e-6])]\n", - "tallies_file.add_tally(therm_abs_rate)" + "tallies_file.append(therm_abs_rate)" ] }, { @@ -485,7 +480,7 @@ "fuel_therm_abs_rate.scores = ['absorption']\n", "fuel_therm_abs_rate.filters = [openmc.Filter(type='energy', bins=[0., 0.625e-6]),\n", " openmc.Filter(type='cell', bins=[fuel_cell.id])]\n", - "tallies_file.add_tally(fuel_therm_abs_rate)" + "tallies_file.append(fuel_therm_abs_rate)" ] }, { @@ -500,7 +495,7 @@ "therm_fiss_rate = openmc.Tally(name='therm. fiss. rate')\n", "therm_fiss_rate.scores = ['nu-fission']\n", "therm_fiss_rate.filters = [openmc.Filter(type='energy', bins=[0., 0.625e-6])]\n", - "tallies_file.add_tally(therm_fiss_rate)" + "tallies_file.append(therm_fiss_rate)" ] }, { @@ -520,7 +515,7 @@ "tally.filters.append(energy_filter)\n", "tally.scores = ['nu-fission', 'scatter']\n", "tally.nuclides = [h1, u238]\n", - "tallies_file.add_tally(tally)" + "tallies_file.append(tally)" ] }, { diff --git a/examples/python/basic/build-xml.py b/examples/python/basic/build-xml.py index 05accbc5e..ffff03720 100644 --- a/examples/python/basic/build-xml.py +++ b/examples/python/basic/build-xml.py @@ -31,10 +31,9 @@ fuel = openmc.Material(material_id=40, name='fuel') fuel.set_density('g/cc', 4.5) fuel.add_nuclide(u235, 1.) -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([moderator, fuel]) materials_file.default_xs = '71c' -materials_file.add_materials([moderator, fuel]) materials_file.export_to_xml() @@ -124,9 +123,6 @@ third_tally = openmc.Tally(tally_id=3, name='third tally') third_tally.filters = [cell_filter, energy_filter, energyout_filter] third_tally.scores = ['scatter', 'nu-scatter', 'nu-fission'] -# Instantiate a Tallies collection, register all Tallies, and export to XML -tallies_file = openmc.Tallies() -tallies_file.add_tally(first_tally) -tallies_file.add_tally(second_tally) -tallies_file.add_tally(third_tally) +# Instantiate a Tallies collection and export to XML +tallies_file = openmc.Tallies((first_tally, second_tally, third_tally)) tallies_file.export_to_xml() diff --git a/examples/python/boxes/build-xml.py b/examples/python/boxes/build-xml.py index 318af2265..814f60beb 100644 --- a/examples/python/boxes/build-xml.py +++ b/examples/python/boxes/build-xml.py @@ -36,10 +36,9 @@ moderator.add_nuclide(h1, 2.) moderator.add_nuclide(o16, 1.) moderator.add_s_alpha_beta('HH2O', '71t') -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([fuel1, fuel2, moderator]) materials_file.default_xs = '71c' -materials_file.add_materials([fuel1, fuel2, moderator]) materials_file.export_to_xml() @@ -129,7 +128,6 @@ plot.width = [20, 20] plot.pixels = [200, 200] plot.color = 'cell' -# Instantiate a Plots collection, add Plot, and export to XML -plot_file = openmc.Plots() -plot_file.add_plot(plot) +# Instantiate a Plots collection and export to XML +plot_file = openmc.Plots([plot]) plot_file.export_to_xml() diff --git a/examples/python/lattice/hexagonal/build-xml.py b/examples/python/lattice/hexagonal/build-xml.py index 04002faf2..ef3a12847 100644 --- a/examples/python/lattice/hexagonal/build-xml.py +++ b/examples/python/lattice/hexagonal/build-xml.py @@ -35,10 +35,9 @@ iron = openmc.Material(material_id=3, name='iron') iron.set_density('g/cc', 7.9) iron.add_nuclide(fe56, 1.) -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([moderator, fuel, iron]) materials_file.default_xs = '71c' -materials_file.add_materials([moderator, fuel, iron]) materials_file.export_to_xml() @@ -152,9 +151,7 @@ plot_yz.pixels = [400, 400] plot_yz.color = 'mat' # Instantiate a Plots collection, add plots, and export to XML -plot_file = openmc.Plots() -plot_file.add_plot(plot_xy) -plot_file.add_plot(plot_yz) +plot_file = openmc.Plots((plot_xy, plot_yz)) plot_file.export_to_xml() @@ -167,7 +164,6 @@ tally = openmc.Tally(tally_id=1) tally.filters = [openmc.Filter(type='distribcell', bins=[cell2.id])] tally.scores = ['total'] -# Instantiate a Tallies collection, register Tally/Mesh, and export to XML -tallies_file = openmc.Tallies() -tallies_file.add_tally(tally) +# Instantiate a Tallies collection and export to XML +tallies_file = openmc.Tallies([tally]) tallies_file.export_to_xml() diff --git a/examples/python/lattice/nested/build-xml.py b/examples/python/lattice/nested/build-xml.py index 0e4e459e2..b2d611d34 100644 --- a/examples/python/lattice/nested/build-xml.py +++ b/examples/python/lattice/nested/build-xml.py @@ -30,10 +30,9 @@ moderator.add_nuclide(h1, 2.) moderator.add_nuclide(o16, 1.) moderator.add_s_alpha_beta('HH2O', '71t') -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials((moderator, fuel)) materials_file.default_xs = '71c' -materials_file.add_materials([moderator, fuel]) materials_file.export_to_xml() @@ -150,9 +149,8 @@ plot.width = [4, 4] plot.pixels = [400, 400] plot.color = 'mat' -# Instantiate a Plots object, add Plot, and export to XML -plot_file = openmc.Plots() -plot_file.add_plot(plot) +# Instantiate a Plots object and export to XML +plot_file = openmc.Plots([plot]) plot_file.export_to_xml() @@ -177,7 +175,5 @@ tally.filters = [mesh_filter] tally.scores = ['total'] # Instantiate a Tallies collection, register Tally/Mesh, and export to XML -tallies_file = openmc.Tallies() -tallies_file.add_mesh(mesh) -tallies_file.add_tally(tally) +tallies_file = openmc.Tallies([tally]) tallies_file.export_to_xml() diff --git a/examples/python/lattice/simple/build-xml.py b/examples/python/lattice/simple/build-xml.py index 8d9481aaa..65c355479 100644 --- a/examples/python/lattice/simple/build-xml.py +++ b/examples/python/lattice/simple/build-xml.py @@ -30,10 +30,9 @@ moderator.add_nuclide(h1, 2.) moderator.add_nuclide(o16, 1.) moderator.add_s_alpha_beta('HH2O', '71t') -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([moderator, fuel]) materials_file.default_xs = '71c' -materials_file.add_materials([moderator, fuel]) materials_file.export_to_xml() @@ -142,9 +141,8 @@ plot.width = [4, 4] plot.pixels = [400, 400] plot.color = 'mat' -# Instantiate a Plots collection, add Plot, and export to XML -plot_file = openmc.Plots() -plot_file.add_plot(plot) +# Instantiate a Plots collection and export to XML +plot_file = openmc.Plots([plot]) plot_file.export_to_xml() @@ -173,8 +171,6 @@ tally.filters = [mesh_filter] tally.scores = ['total'] tally.triggers = [trigger] -# Instantiate a Tallies collection, register Tally/Mesh, and export to XML -tallies_file = openmc.Tallies() -tallies_file.add_mesh(mesh) -tallies_file.add_tally(tally) +# Instantiate a Tallies collection and export to XML +tallies_file = openmc.Tallies([tally]) tallies_file.export_to_xml() diff --git a/examples/python/pincell/build-xml.py b/examples/python/pincell/build-xml.py index 561df2b5a..a3be3e97e 100644 --- a/examples/python/pincell/build-xml.py +++ b/examples/python/pincell/build-xml.py @@ -100,10 +100,9 @@ borated_water.add_nuclide(o16, 2.4672e-2) borated_water.add_nuclide(o17, 6.0099e-5) borated_water.add_s_alpha_beta('HH2O', '71t') -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([uo2, helium, zircaloy, borated_water]) materials_file.default_xs = '71c' -materials_file.add_materials([uo2, helium, zircaloy, borated_water]) materials_file.export_to_xml() @@ -197,8 +196,6 @@ tally = openmc.Tally(tally_id=1, name='tally 1') tally.filters = [energy_filter, mesh_filter] tally.scores = ['flux', 'fission', 'nu-fission'] -# Instantiate a Tallies collection, register all Tallies, and export to XML -tallies_file = openmc.Tallies() -tallies_file.add_mesh(mesh) -tallies_file.add_tally(tally) +# Instantiate a Tallies collection and export to XML +tallies_file = openmc.Tallies([tally]) tallies_file.export_to_xml() diff --git a/examples/python/pincell_multigroup/build-xml.py b/examples/python/pincell_multigroup/build-xml.py index 697a596d9..c7d6dfc8b 100644 --- a/examples/python/pincell_multigroup/build-xml.py +++ b/examples/python/pincell_multigroup/build-xml.py @@ -81,10 +81,9 @@ water = openmc.Material(material_id=2, name='Water') water.set_density('macro', 1.0) water.add_macroscopic(h2o_data) -# Instantiate a Materials collection, register all Materials, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([uo2, water]) materials_file.default_xs = '300K' -materials_file.add_materials([uo2, water]) materials_file.export_to_xml() @@ -167,14 +166,9 @@ mesh_filter.mesh = mesh # Instantiate the Tally tally = openmc.Tally(tally_id=1, name='tally 1') -tally.add_filter(energy_filter) -tally.add_filter(mesh_filter) -tally.add_score('flux') -tally.add_score('fission') -tally.add_score('nu-fission') +tally.filters = [energy_filter, mesh_filter] +tally.scores = ['flux', 'fission', 'nu-fission'] # Instantiate a Tallies collection, register all Tallies, and export to XML -tallies_file = openmc.Tallies() -tallies_file.add_mesh(mesh) -tallies_file.add_tally(tally) +tallies_file = openmc.Tallies([tally]) tallies_file.export_to_xml() diff --git a/examples/python/reflective/build-xml.py b/examples/python/reflective/build-xml.py index e4776e744..4ecd0351f 100644 --- a/examples/python/reflective/build-xml.py +++ b/examples/python/reflective/build-xml.py @@ -23,10 +23,9 @@ fuel = openmc.Material(material_id=1, name='fuel') fuel.set_density('g/cc', 4.5) fuel.add_nuclide(u235, 1.) -# Instantiate a Materials collection, register Material, and export to XML -materials_file = openmc.Materials() +# Instantiate a Materials collection and export to XML +materials_file = openmc.Materials([fuel]) materials_file.default_xs = '71c' -materials_file.add_material(fuel) materials_file.export_to_xml() diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py index 53f4b8368..62b843a3a 100644 --- a/openmc/checkvalue.py +++ b/openmc/checkvalue.py @@ -1,3 +1,4 @@ +import copy from collections import Iterable from numbers import Integral, Real @@ -57,7 +58,7 @@ def check_type(name, value, expected_type, expected_iter_type=None): else: msg = 'Unable to set "{0}" to "{1}" which is not of type "{2}"'.format( name, value, expected_type.__name__) - raise ValueError(msg) + raise TypeError(msg) if expected_iter_type: for item in value: @@ -71,7 +72,7 @@ def check_type(name, value, expected_type, expected_iter_type=None): msg = 'Unable to set "{0}" to "{1}" since each item must be ' \ 'of type "{2}"'.format(name, value, expected_iter_type.__name__) - raise ValueError(msg) + raise TypeError(msg) def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1): @@ -122,7 +123,7 @@ def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1): if len(tree) < min_depth: msg = 'Error setting "{0}": The item at {1} does not meet the '\ 'minimum depth of {2}'.format(name, ind_str, min_depth) - raise ValueError(msg) + raise TypeError(msg) # This item is okay. Move on to the next item. index[-1] += 1 @@ -140,7 +141,7 @@ def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1): msg = 'Error setting {0}: Found an iterable at {1}, items '\ 'in that iterable exceed the maximum depth of {2}' \ .format(name, ind_str, max_depth) - raise ValueError(msg) + raise TypeError(msg) else: # This item is completely unexpected. @@ -148,7 +149,7 @@ def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1): "item at {2} is of type '{3}'"\ .format(name, expected_type.__name__, ind_str, type(current_item).__name__) - raise ValueError(msg) + raise TypeError(msg) def check_length(name, value, length_min, length_max=None): @@ -278,6 +279,21 @@ class CheckedList(list): for item in items: self.append(item) + def __add__(self, other): + new_instance = copy.copy(self) + new_instance += other + return new_instance + + def __radd__(self, other): + return self + other + + def __iadd__(self, other): + check_type('CheckedList add operand', other, Iterable, + self.expected_type) + for item in other: + self.append(item) + return self + def append(self, item): """Append item to list diff --git a/openmc/material.py b/openmc/material.py index 97c7cedca..b3c281341 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -8,7 +8,7 @@ if sys.version_info[0] >= 3: basestring = str import openmc -from openmc.checkvalue import check_type, check_value, check_greater_than +import openmc.checkvalue as cv from openmc.clean_xml import * @@ -202,15 +202,15 @@ class Material(object): self._id = AUTO_MATERIAL_ID AUTO_MATERIAL_ID += 1 else: - check_type('material ID', material_id, Integral) - check_greater_than('material ID', material_id, 0, equality=True) + cv.check_type('material ID', material_id, Integral) + cv.check_greater_than('material ID', material_id, 0, equality=True) self._id = material_id @name.setter def name(self, name): if name is not None: - check_type('name for Material ID="{0}"'.format(self._id), - name, basestring) + cv.check_type('name for Material ID="{0}"'.format(self._id), + name, basestring) self._name = name else: self._name = '' @@ -228,9 +228,9 @@ class Material(object): """ - check_type('the density for Material ID="{0}"'.format(self._id), - density, Real) - check_value('density units', units, DENSITY_UNITS) + cv.check_type('the density for Material ID="{0}"'.format(self._id), + density, Real) + cv.check_value('density units', units, DENSITY_UNITS) if density is None and units is not 'sum': msg = 'Unable to set the density for Material ID="{0}" ' \ @@ -642,9 +642,25 @@ class Material(object): return element -class Materials(object): - """Collection of Materials used for an OpenMC simulation. Corresponds directly - to the materials.xml input file. +class Materials(cv.CheckedList): + """Collection of Materials used for an OpenMC simulation. + + This class corresponds directly to the materials.xml input file. It can be + thought of as a normal Python list where each member is a + :class:`Material`. It behaves like a list as the following example + demonstrates: + + >>> fuel = openmc.Material() + >>> clad = openmc.Material() + >>> water = openmc.Material() + >>> m = openmc.Materials([fuel]) + >>> m.append(water) + >>> m += [clad] + + Parameters + ---------- + materials : Iterable of openmc.Material + Materials to add to the collection Attributes ---------- @@ -654,10 +670,12 @@ class Materials(object): """ - def __init__(self): - self._materials = [] + def __init__(self, materials=None): + super(Materials, self).__init__(Material, 'materials collection') self._default_xs = None self._materials_file = ET.Element("materials") + if materials is not None: + self += materials @property def default_xs(self): @@ -665,11 +683,14 @@ class Materials(object): @default_xs.setter def default_xs(self, xs): - check_type('default xs', xs, basestring) + cv.check_type('default xs', xs, basestring) self._default_xs = xs def add_material(self, material): - """Add a material to the file. + """Append material to collection + + .. deprecated:: 0.8 + Use :meth:`Materials.append` instead. Parameters ---------- @@ -677,51 +698,72 @@ class Materials(object): Material to add """ - - if not isinstance(material, Material): - msg = 'Unable to add a non-Material "{0}" to the ' \ - 'Materials instance'.format(material) - raise ValueError(msg) - - self._materials.append(material) + warnings.warn("Materials.add_material(...) has been deprecated and may be " + "removed in a future version. Use Material.append(...) " + "instead.", DeprecationWarning) + self.append(material) def add_materials(self, materials): - """Add multiple materials to the file. + """Add multiple materials to the collection + + .. deprecated:: 0.8 + Use compound assignment instead. Parameters ---------- - materials : tuple or list of openmc.Material + materials : Iterable of openmc.Material Materials to add """ - - if not isinstance(materials, Iterable): - msg = 'Unable to create OpenMC materials.xml file from "{0}" which ' \ - 'is not iterable'.format(materials) - raise ValueError(msg) - + warnings.warn("Materials.add_materials(...) has been deprecated and may be " + "removed in a future version. Use compound assignment " + "instead.", DeprecationWarning) for material in materials: - self.add_material(material) + self.append(material) + + def append(self, material): + """Append material to collection + + Parameters + ---------- + material : openmc.Material + Material to append + + """ + super(Materials, self).append(material) + + def insert(self, index, material): + """Insert material before index + + Parameters + ---------- + index : int + Index in list + material : openmc.Material + Material to insert + + """ + super(Materials, self).insert(index, material) def remove_material(self, material): """Remove a material from the file + .. deprecated:: 0.8 + Use :meth:`Materials.remove` instead. + Parameters ---------- material : openmc.Material Material to remove """ - - if not isinstance(material, Material): - msg = 'Unable to remove a non-Material "{0}" from the ' \ - 'Materials instance'.format(material) - raise ValueError(msg) - - self._materials.remove(material) + warnings.warn("Materials.remove_material(...) has been deprecated and " + "may be removed in a future version. Use " + "Materials.remove(...) instead.", DeprecationWarning) + self.remove(material) def make_isotropic_in_lab(self): - for material in self._materials: + for material in self: material.make_isotropic_in_lab() def _create_material_subelements(self): @@ -729,7 +771,7 @@ class Materials(object): subelement = ET.SubElement(self._materials_file, "default_xs") subelement.text = self._default_xs - for material in self._materials: + for material in self: xml_element = material.get_material_xml() self._materials_file.append(xml_element) diff --git a/openmc/mgxs/library.py b/openmc/mgxs/library.py index 8d5e9854e..f3bf2018d 100644 --- a/openmc/mgxs/library.py +++ b/openmc/mgxs/library.py @@ -370,7 +370,7 @@ class Library(object): for mgxs_type in self.mgxs_types: mgxs = self.get_mgxs(domain, mgxs_type) for tally_id, tally in mgxs.tallies.items(): - tallies_file.add_tally(tally, merge=merge) + tallies_file.append(tally, merge=merge) def load_from_statepoint(self, statepoint): """Extracts tallies in an OpenMC StatePoint with the data needed to diff --git a/openmc/plots.py b/openmc/plots.py index a967cb060..9167e55d5 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -2,6 +2,7 @@ from collections import Iterable from numbers import Real, Integral from xml.etree import ElementTree as ET import sys +import warnings import numpy as np @@ -401,43 +402,90 @@ class Plot(object): return element -class Plots(object): - """Collection of Plots used for an OpenMC simulation. Corresponds directly to - the plots.xml input file. +class Plots(cv.CheckedList): + """Collection of Plots used for an OpenMC simulation. + + This class corresponds directly to the plots.xml input file. It can be + thought of as a normal Python list where each member is a :class:`Plot`. It + behaves like a list as the following example demonstrates: + + >>> xz_plot = openmc.Plot() + >>> big_plot = openmc.Plot() + >>> small_plot = openmc.Plot() + >>> p = openmc.Plots((xz_plot, big_plot)) + >>> p.append(small_plot) + >>> small_plot = p.pop() + + Parameters + ---------- + plots : Iterable of openmc.Plot + Plots to add to the collection """ - def __init__(self): - self._plots = [] + def __init__(self, plots=None): + super(Plots, self).__init__(Plot, 'plots collection') self._plots_file = ET.Element("plots") + if plots is not None: + self += plots def add_plot(self, plot): """Add a plot to the file. + .. deprecated:: 0.8 + Use :meth:`Plots.append` instead. + Parameters ---------- plot : openmc.Plot Plot to add """ + warnings.warn("Plots.add_plot(...) has been deprecated and may be " + "removed in a future version. Use Plots.append(...) " + "instead.", DeprecationWarning) + self.append(plot) - if not isinstance(plot, Plot): - msg = 'Unable to add a non-Plot "{0}" to the Plots instance'.format(plot) - raise ValueError(msg) + def append(self, plot): + """Append plot to collection - self._plots.append(plot) + Parameters + ---------- + plot : openmc.Plot + Plot to append + + """ + super(Plots, self).append(plot) + + def insert(self, index, plot): + """Insert plot before index + + Parameters + ---------- + index : int + Index in list + plot : openmc.Plot + Plot to insert + + """ + super(Plots, self).insert(index, plot) def remove_plot(self, plot): """Remove a plot from the file. + .. deprecated:: 0.8 + Use :meth:`Plots.remove` instead. + Parameters ---------- plot : openmc.Plot Plot to remove """ - - self._plots.remove(plot) + warnings.warn("Plots.remove_plot(...) has been deprecated and may be " + "removed in a future version. Use Plots.remove(...) " + "instead.", DeprecationWarning) + self.remove(plot) def colorize(self, geometry, seed=1): """Generate a consistent color scheme for each domain in each plot. @@ -455,7 +503,7 @@ class Plots(object): """ - for plot in self._plots: + for plot in self: plot.colorize(geometry, seed) @@ -481,11 +529,11 @@ class Plots(object): """ - for plot in self._plots: + for plot in self: plot.highlight_domains(geometry, domains, seed, alpha, background) def _create_plot_subelements(self): - for plot in self._plots: + for plot in self: xml_element = plot.get_plot_xml() if len(plot._name) > 0: diff --git a/openmc/tallies.py b/openmc/tallies.py index 90b09f582..3a5a1f1e8 100644 --- a/openmc/tallies.py +++ b/openmc/tallies.py @@ -3419,65 +3419,108 @@ class Tally(object): return new_tally -class Tallies(object): - """Collection of Tallies used for an OpenMC simulation. Corresponds directly to - the tallies.xml input file. +class Tallies(cv.CheckedList): + """Collection of Tallies used for an OpenMC simulation. + + This class corresponds directly to the tallies.xml input file. It can be + thought of as a normal Python list where each member is a :class:`Tally`. It + behaves like a list as the following example demonstrates: + + >>> t1 = openmc.Tally() + >>> t2 = openmc.Tally() + >>> t3 = openmc.Tally() + >>> tallies = openmc.Tallies([t1]) + >>> tallies.append(t2) + >>> tallies += [t3] + + Parameters + ---------- + tallies : Iterable of openmc.Tally + Tallies to add to the collection """ - def __init__(self): - self._tallies = [] - self._meshes = [] + def __init__(self, tallies=None): + super(Tallies, self).__init__(Tally, 'tallies collection') self._tallies_file = ET.Element("tallies") - - @property - def tallies(self): - return self._tallies - - @property - def meshes(self): - return self._meshes + if tallies is not None: + self += tallies def add_tally(self, tally, merge=False): - """Add a tally to the file + """Append tally to collection + + .. deprecated:: 0.8 + Use :meth:`Tallies.append` instead. Parameters ---------- tally : openmc.Tally - Tally to add to file + Tally to add merge : bool Indicate whether the tally should be merged with an existing tally, if possible. Defaults to False. """ + warnings.warn("Tallies.add_tally(...) has been deprecated and may be " + "removed in a future version. Use Tallies.append(...) " + "instead.", DeprecationWarning) + self.append(tally, merge) + def append(self, tally, merge=False): + """Append tally to collection + + Parameters + ---------- + tally : openmc.Tally + Tally to append + merge : bool + Indicate whether the tally should be merged with an existing tally, + if possible. Defaults to False. + + """ if not isinstance(tally, Tally): msg = 'Unable to add a non-Tally "{0}" to the Tallies instance'.format(tally) - raise ValueError(msg) + raise TypeError(msg) if merge: merged = False # Look for a tally to merge with this one - for i, tally2 in enumerate(self._tallies): + for i, tally2 in enumerate(self): # If a mergeable tally is found if tally2.can_merge(tally): # Replace tally 2 with the merged tally merged_tally = tally2.merge(tally) - self._tallies[i] = merged_tally + self[i] = merged_tally merged = True break # If not mergeable tally was found, simply add this tally if not merged: - self._tallies.append(tally) + super(Tallies, self).append(tally) else: - self._tallies.append(tally) + super(Tallies, self).append(tally) + + def insert(self, index, item): + """Insert tally before index + + Parameters + ---------- + index : int + Index in list + item : openmc.Tally + Tally to insert + + """ + super(Tallies, self).insert(index, item) def remove_tally(self, tally): - """Remove a tally from the file + """Remove a tally from the collection + + .. deprecated:: 0.8 + Use :meth:`Tallies.remove` instead. Parameters ---------- @@ -3485,8 +3528,11 @@ class Tallies(object): Tally to remove """ + warnings.warn("Tallies.remove_tally(...) has been deprecated and may " + "be removed in a future version. Use Tallies.remove(...) " + "instead.", DeprecationWarning) - self._tallies.remove(tally) + self.remove(tally) def merge_tallies(self): """Merge any mergeable tallies together. Note that n-way merges are @@ -3494,8 +3540,8 @@ class Tallies(object): """ - for i, tally1 in enumerate(self._tallies): - for j, tally2 in enumerate(self._tallies): + for i, tally1 in enumerate(self): + for j, tally2 in enumerate(self): # Do not merge the same tally with itself if i == j: continue @@ -3504,10 +3550,10 @@ class Tallies(object): if tally1.can_merge(tally2): # Replace tally 1 with the merged tally merged_tally = tally1.merge(tally2) - self._tallies[i] = merged_tally + self[i] = merged_tally # Remove tally 2 since it is no longer needed - self._tallies.pop(j) + self.pop(j) # Continue iterating from the first loop break @@ -3515,6 +3561,10 @@ class Tallies(object): def add_mesh(self, mesh): """Add a mesh to the file + .. deprecated:: 0.8 + Meshes that appear in a tally are automatically added to the + collection. + Parameters ---------- mesh : openmc.Mesh @@ -3522,36 +3572,43 @@ class Tallies(object): """ - if not isinstance(mesh, Mesh): - msg = 'Unable to add a non-Mesh "{0}" to the Tallies instance'.format(mesh) - raise ValueError(msg) - - self._meshes.append(mesh) + warnings.warn("Tallies.add_mesh(...) has been deprecated and may be " + "removed in a future version. Meshes that appear in a " + "tally are automatically added to the collection.", + DeprecationWarning) def remove_mesh(self, mesh): """Remove a mesh from the file + .. deprecated:: 0.8 + Meshes do not need to be managed explicitly. + Parameters ---------- mesh : openmc.Mesh Mesh to remove from the file """ - - self._meshes.remove(mesh) + warnings.warn("Tallies.remove_mesh(...) has been deprecated and may be " + "removed in a future version. Meshes do not need to be " + "managed explicitly.", DeprecationWarning) def _create_tally_subelements(self): - for tally in self._tallies: + for tally in self: xml_element = tally.get_tally_xml() self._tallies_file.append(xml_element) def _create_mesh_subelements(self): - for mesh in self._meshes: - if len(mesh._name) > 0: - self._tallies_file.append(ET.Comment(mesh._name)) + already_written = set() + for tally in self: + for f in tally.filters: + if f.type == 'mesh' and f.mesh not in already_written: + if len(f.mesh.name) > 0: + self._tallies_file.append(ET.Comment(f.mesh.name)) - xml_element = mesh.get_mesh_xml() - self._tallies_file.append(xml_element) + xml_element = f.mesh.get_mesh_xml() + self._tallies_file.append(xml_element) + already_written.add(f.mesh) def export_to_xml(self): """Create a tallies.xml file that can be used for a simulation. diff --git a/tests/input_set.py b/tests/input_set.py index 3be6c1db4..2c6841e25 100644 --- a/tests/input_set.py +++ b/tests/input_set.py @@ -267,9 +267,9 @@ class InputSet(object): # Define the materials file. self.materials.default_xs = '71c' - self.materials.add_materials((fuel, clad, cold_water, hot_water, - rpv_steel, lower_rad_ref, upper_rad_ref, bot_plate, bot_nozzle, - top_nozzle, top_fa, bot_fa)) + self.materials += (fuel, clad, cold_water, hot_water, rpv_steel, + lower_rad_ref, upper_rad_ref, bot_plate, + bot_nozzle, top_nozzle, top_fa, bot_fa) # Define surfaces. s1 = openmc.ZCylinder(R=0.41, surface_id=1) @@ -590,7 +590,7 @@ class MGInputSet(InputSet): # Define the materials file. self.materials.default_xs = '71c' - self.materials.add_materials((uo2, clad, water)) + self.materials += (uo2, clad, water) # Define surfaces. diff --git a/tests/test_asymmetric_lattice/test_asymmetric_lattice.py b/tests/test_asymmetric_lattice/test_asymmetric_lattice.py index 94562e6d9..03e55d32f 100644 --- a/tests/test_asymmetric_lattice/test_asymmetric_lattice.py +++ b/tests/test_asymmetric_lattice/test_asymmetric_lattice.py @@ -54,12 +54,11 @@ class AsymmetricLatticeTestHarness(PyAPITestHarness): # Initialize the tallies tally = openmc.Tally(name='distribcell tally', tally_id=27) - tally.add_filter(distrib_filter) - tally.add_score('nu-fission') + tally.filters.append(distrib_filter) + tally.scores.append('nu-fission') # Initialize the tallies file - tallies_file = openmc.Tallies() - tallies_file.add_tally(tally) + tallies_file = openmc.Tallies([tally]) # Assign the tallies file to the input set self._input_set.tallies = tallies_file diff --git a/tests/test_distribmat/test_distribmat.py b/tests/test_distribmat/test_distribmat.py index ded2863bd..d8f78c5cf 100644 --- a/tests/test_distribmat/test_distribmat.py +++ b/tests/test_distribmat/test_distribmat.py @@ -28,9 +28,8 @@ class DistribmatTestHarness(PyAPITestHarness): light_fuel.set_density('g/cc', 2.0) light_fuel.add_nuclide('U-235', 1.0) - mats_file = openmc.Materials() + mats_file = openmc.Materials([moderator, dense_fuel, light_fuel]) mats_file.default_xs = '71c' - mats_file.add_materials([moderator, dense_fuel, light_fuel]) mats_file.export_to_xml() diff --git a/tests/test_mg_max_order/test_mg_max_order.py b/tests/test_mg_max_order/test_mg_max_order.py index 2c4db58df..7f59572cf 100644 --- a/tests/test_mg_max_order/test_mg_max_order.py +++ b/tests/test_mg_max_order/test_mg_max_order.py @@ -27,7 +27,7 @@ class MGNuclideInputSet(MGInputSet): # Define the materials file. self.materials.default_xs = '71c' - self.materials.add_materials((uo2, clad, water)) + self.materials += (uo2, clad, water) # Define surfaces. diff --git a/tests/test_mg_nuclide/test_mg_nuclide.py b/tests/test_mg_nuclide/test_mg_nuclide.py index 0fa7184a3..866840ddf 100644 --- a/tests/test_mg_nuclide/test_mg_nuclide.py +++ b/tests/test_mg_nuclide/test_mg_nuclide.py @@ -26,7 +26,7 @@ class MGNuclideInputSet(MGInputSet): # Define the materials file. self.materials.default_xs = '71c' - self.materials.add_materials((uo2, clad, water)) + self.materials += (uo2, clad, water) # Define surfaces. diff --git a/tests/test_mg_tallies/test_mg_tallies.py b/tests/test_mg_tallies/test_mg_tallies.py index ffc57f9e9..3048f4a39 100644 --- a/tests/test_mg_tallies/test_mg_tallies.py +++ b/tests/test_mg_tallies/test_mg_tallies.py @@ -27,24 +27,15 @@ class MGTalliesTestHarness(PyAPITestHarness): mat_filter = openmc.Filter(type='material', bins=[1,2,3]) tally1 = openmc.Tally(tally_id=1) - tally1.add_filter(mesh_filter) - tally1.add_score('total') - tally1.add_score('absorption') - tally1.add_score('flux') - tally1.add_score('fission') - tally1.add_score('nu-fission') + tally1.filters = [mesh_filter] + tally1.scores = ['total', 'absorption', 'flux', + 'fission', 'nu-fission'] tally2 = openmc.Tally(tally_id=2) - tally2.add_filter(mat_filter) - tally2.add_filter(energy_filter) - tally2.add_filter(energyout_filter) - tally2.add_score('scatter') - tally2.add_score('nu-scatter') + tally2.filters = [mat_filter, energy_filter, energyout_filter] + tally2.scores = ['scatter', 'nu-scatter'] - self._input_set.tallies = openmc.Tallies() - self._input_set.tallies.add_mesh(mesh) - self._input_set.tallies.add_tally(tally1) - self._input_set.tallies.add_tally(tally2) + self._input_set.tallies = openmc.Tallies([tally1, tally2]) super(MGTalliesTestHarness, self)._build_inputs() diff --git a/tests/test_resonance_scattering/test_resonance_scattering.py b/tests/test_resonance_scattering/test_resonance_scattering.py index 5cecfedc4..b752cf7f3 100644 --- a/tests/test_resonance_scattering/test_resonance_scattering.py +++ b/tests/test_resonance_scattering/test_resonance_scattering.py @@ -17,9 +17,8 @@ class ResonanceScatteringTestHarness(PyAPITestHarness): mat.add_nuclide('Pu-239', 0.02) mat.add_nuclide('H-1', 20.0) - mats_file = openmc.Materials() + mats_file = openmc.Materials([mat]) mats_file.default_xs = '71c' - mats_file.add_material(mat) mats_file.export_to_xml() # Geometry diff --git a/tests/test_source/test_source.py b/tests/test_source/test_source.py index 1e41bd10e..0abae4344 100644 --- a/tests/test_source/test_source.py +++ b/tests/test_source/test_source.py @@ -16,8 +16,7 @@ class SourceTestHarness(PyAPITestHarness): mat1 = openmc.Material(material_id=1) mat1.set_density('g/cm3', 4.5) mat1.add_nuclide(openmc.Nuclide('U-235', '71c'), 1.0) - materials = openmc.Materials() - materials.add_material(mat1) + materials = openmc.Materials([mat1]) materials.export_to_xml() sphere = openmc.Sphere(surface_id=1, R=10.0, boundary_type='vacuum') diff --git a/tests/test_tallies/test_tallies.py b/tests/test_tallies/test_tallies.py index bb0273589..52d4084fd 100644 --- a/tests/test_tallies/test_tallies.py +++ b/tests/test_tallies/test_tallies.py @@ -42,7 +42,8 @@ class TalliesTestHarness(PyAPITestHarness): mesh_2x2.lower_left = [-182.07, -182.07] mesh_2x2.upper_right = [182.07, 182.07] mesh_2x2.dimension = [2, 2] - mesh_filter = Filter(type='mesh', bins=(1,)) + mesh_filter = Filter(type='mesh') + mesh_filter.mesh = mesh_2x2 azimuthal_tally4 = Tally() azimuthal_tally4.filters = [azimuthal_filter2, mesh_filter] azimuthal_tally4.scores = ['flux'] @@ -171,32 +172,18 @@ class TalliesTestHarness(PyAPITestHarness): all_nuclide_tallies[0].estimator = 'collision' self._input_set.tallies = Tallies() - self._input_set.tallies.add_tally(azimuthal_tally1) - self._input_set.tallies.add_tally(azimuthal_tally2) - self._input_set.tallies.add_tally(azimuthal_tally3) - self._input_set.tallies.add_tally(azimuthal_tally4) - self._input_set.tallies.add_tally(cellborn_tally) - self._input_set.tallies.add_tally(dg_tally) - self._input_set.tallies.add_tally(energy_tally) - self._input_set.tallies.add_tally(energyout_tally) - self._input_set.tallies.add_tally(transfer_tally) - self._input_set.tallies.add_tally(material_tally) - self._input_set.tallies.add_tally(mu_tally1) - self._input_set.tallies.add_tally(mu_tally2) - self._input_set.tallies.add_tally(mu_tally3) - self._input_set.tallies.add_tally(polar_tally1) - self._input_set.tallies.add_tally(polar_tally2) - self._input_set.tallies.add_tally(polar_tally3) - self._input_set.tallies.add_tally(polar_tally4) - self._input_set.tallies.add_tally(universe_tally) - [self._input_set.tallies.add_tally(t) for t in score_tallies] - [self._input_set.tallies.add_tally(t) for t in flux_tallies] - self._input_set.tallies.add_tally(scatter_tally1) - self._input_set.tallies.add_tally(scatter_tally2) - [self._input_set.tallies.add_tally(t) for t in total_tallies] - self._input_set.tallies.add_tally(questionable_tally) - [self._input_set.tallies.add_tally(t) for t in all_nuclide_tallies] - self._input_set.tallies.add_mesh(mesh_2x2) + self._input_set.tallies += ( + [azimuthal_tally1, azimuthal_tally2, azimuthal_tally3, + azimuthal_tally4, cellborn_tally, dg_tally, energy_tally, + energyout_tally, transfer_tally, material_tally, mu_tally1, + mu_tally2, mu_tally3, polar_tally1, polar_tally2, polar_tally3, + polar_tally4, universe_tally]) + self._input_set.tallies += score_tallies + self._input_set.tallies += flux_tallies + self._input_set.tallies += (scatter_tally1, scatter_tally2) + self._input_set.tallies += total_tallies + self._input_set.tallies.append(questionable_tally) + self._input_set.tallies += all_nuclide_tallies self._input_set.export() diff --git a/tests/test_tally_aggregation/test_tally_aggregation.py b/tests/test_tally_aggregation/test_tally_aggregation.py index 009a7dc09..359afbe34 100644 --- a/tests/test_tally_aggregation/test_tally_aggregation.py +++ b/tests/test_tally_aggregation/test_tally_aggregation.py @@ -15,9 +15,6 @@ class TallyAggregationTestHarness(PyAPITestHarness): # The summary.h5 file needs to be created to read in the tallies self._input_set.settings.output = {'summary': True} - # Initialize the tallies file - tallies_file = openmc.Tallies() - # Initialize the nuclides u235 = openmc.Nuclide('U-235') u238 = openmc.Nuclide('U-238') @@ -33,7 +30,7 @@ class TallyAggregationTestHarness(PyAPITestHarness): tally.filters = [energy_filter, distrib_filter] tally.scores = ['nu-fission', 'total'] tally.nuclides = [u235, u238, pu239] - tallies_file.add_tally(tally) + tallies_file = openmc.Tallies([tally]) # Export tallies to file self._input_set.tallies = tallies_file diff --git a/tests/test_tally_arithmetic/test_tally_arithmetic.py b/tests/test_tally_arithmetic/test_tally_arithmetic.py index ffea74603..8e2d2b349 100644 --- a/tests/test_tally_arithmetic/test_tally_arithmetic.py +++ b/tests/test_tally_arithmetic/test_tally_arithmetic.py @@ -43,14 +43,13 @@ class TallyArithmeticTestHarness(PyAPITestHarness): tally.filters = [material_filter, energy_filter, distrib_filter] tally.scores = ['nu-fission', 'total'] tally.nuclides = [u235, pu239] - tallies_file.add_tally(tally) + tallies_file.append(tally) tally = openmc.Tally(name='tally 2') tally.filters = [energy_filter, mesh_filter] tally.scores = ['total', 'fission'] tally.nuclides = [u238, u235] - tallies_file.add_tally(tally) - tallies_file.add_mesh(mesh) + tallies_file.append(tally) # Export tallies to file self._input_set.tallies = tallies_file diff --git a/tests/test_tally_slice_merge/test_tally_slice_merge.py b/tests/test_tally_slice_merge/test_tally_slice_merge.py index 933fdf6fa..85dd532c6 100644 --- a/tests/test_tally_slice_merge/test_tally_slice_merge.py +++ b/tests/test_tally_slice_merge/test_tally_slice_merge.py @@ -70,9 +70,7 @@ class TallySliceMergeTestHarness(PyAPITestHarness): distribcell_tally.add_nuclide(nuclide) # Add tallies to a Tallies object - tallies_file = openmc.Tallies() - tallies_file.add_tally(tallies[0]) - tallies_file.add_tally(distribcell_tally) + tallies_file = openmc.Tallies((tallies[0], distribcell_tally)) # Export tallies to file self._input_set.tallies = tallies_file