From d7bf830028d20bb91ac9178b4d462b7bda81b24b Mon Sep 17 00:00:00 2001 From: agnelson Date: Wed, 29 Sep 2021 08:22:16 -0500 Subject: [PATCH] Added test of Model.plot_geometry and made changes to reflect that openmc.lib.plot_geometry wont work if openmc.lib.init wasnt told that the run mode is to plot because then plots.xml isnt read in --- openmc/model/model.py | 36 +++++++++++++++---------- tests/unit_tests/conftest.py | 18 ++++++++----- tests/unit_tests/test_model.py | 49 ++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/openmc/model/model.py b/openmc/model/model.py index e32876a6c..5e0c1a2bc 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -272,7 +272,7 @@ class Model: if dep.comm.rank == 0: self.export_to_xml() dep.comm.barrier() - # TODO: Implement the output flag somewhere on the C++ side + openmc.lib.init(args=args, intracomm=dep.comm) def clear_C_api(self): @@ -636,6 +636,7 @@ class Model: if not output: openmc.lib.settings.verbosity = init_verbosity else: + self.export_to_xml() openmc.calculate_volumes(threads=threads, output=output, openmc_exec=openmc_exec, mpi_args=mpi_args) @@ -691,21 +692,28 @@ class Model: "before executing this method!") with openmc.change_directory(Path(cwd)): - if self.C_init: - # Apply the output settings - if not output: - init_verbosity = openmc.lib.settings.verbosity - if not output: - openmc.lib.settings.verbosity = 1 + # TODO: openmc_init doesnt read plots.xml unless it is in plot mode + # so the following will not work. Commented out for now and + # replacing with non-C-API code + self.export_to_xml() + openmc.plot_geometry(output=output, openmc_exec=openmc_exec) - # Compute the volumes - openmc.lib.plot_geometry() + # if self.C_init: + # # Apply the output settings + # if not output: + # init_verbosity = openmc.lib.settings.verbosity + # if not output: + # openmc.lib.settings.verbosity = 1 - # Reset the output verbosity - if not output: - openmc.lib.settings.verbosity = init_verbosity - else: - openmc.plot_geometry(output=output, openmc_exec=openmc_exec) + # # Compute the volumes + # openmc.lib.plot_geometry() + + # # Reset the output verbosity + # if not output: + # openmc.lib.settings.verbosity = init_verbosity + # else: + # self.export_to_xml() + # openmc.plot_geometry(output=output, openmc_exec=openmc_exec) if convert: for p in self.plots: diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index d6f94e82e..ff5340249 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -63,12 +63,18 @@ def pin_model_attributes(): tal.scores = ['flux'] tals.append(tal) - plot = openmc.Plot() - plot.origin = (0., 0., 0.) - plot.width = (pitch, pitch) - plot.pixels = (300, 300) - plot.color_by = 'material' - plots = openmc.Plots((plot,)) + plot1 = openmc.Plot() + plot1.origin = (0., 0., 0.) + plot1.width = (pitch, pitch) + plot1.pixels = (300, 300) + plot1.color_by = 'material' + plot1.filename = 'test' + plot2 = openmc.Plot() + plot2.origin = (0., 0., 0.) + plot2.width = (pitch, pitch) + plot2.pixels = (300, 300) + plot2.color_by = 'cell' + plots = openmc.Plots((plot1, plot2)) chain = './chain_simple.xml' fission_q = {'U235': 200e6} diff --git a/tests/unit_tests/test_model.py b/tests/unit_tests/test_model.py index 0ee8568e3..dd6242fe1 100644 --- a/tests/unit_tests/test_model.py +++ b/tests/unit_tests/test_model.py @@ -1,6 +1,7 @@ import numpy as np import pytest from pathlib import Path +from shutil import which import openmc import openmc.lib @@ -226,7 +227,7 @@ def test_run(run_in_tmpdir, pin_model_attributes, mpi_intracomm): C_flux = sp.get_tally(id=1).get_values()[0, 0, 0] # and lets compare results - assert abs(C_keff - cli_keff) < 1e-15 + assert abs(C_keff - cli_keff) < 1e-13 assert abs(C_flux - cli_flux) < 1e-13 # Now we should make sure that the flags for items which should be handled @@ -247,6 +248,51 @@ def test_run(run_in_tmpdir, pin_model_attributes, mpi_intracomm): openmc.deplete.comm = orig_comm +def test_plots(run_in_tmpdir, pin_model_attributes, mpi_intracomm): + mats, geom, settings, tals, plots, _, _, _ = pin_model_attributes + test_model = openmc.Model(geom, mats, settings, tals, plots) + + if mpi_intracomm is not None: + # Set the depletion module to use the test intracomm as the depletion + # module's intracomm is the one that init uses. We will not perturb + # system state outside of this test by re-setting the + # openmc.deplete.comm to what it was before when we exist + orig_comm = openmc.deplete.comm + openmc.deplete.comm = mpi_intracomm + + # This test cannot check the correctness of the plot, but it can + # check that a plot was made and that the expected ppm and png files are + # there + + # We will only test convert if it is on the system, so as not to add an + # extra dependency just for tests + convert = which('convert') is not None + if convert: + exts = ['ppm', 'png'] + else: + exts = ['ppm'] + + # We will run the test twice, the first time without C-API, the second with + for i in range(2): + if i == 1: + test_model.init_C_api() + test_model.plot_geometry(output=True, convert=convert) + + # Now look for the files, expect to find test.ppm, plot_2.ppm, and if + # convert is True, test.png, plot_2.png + for fname in ['test.', 'plot_2.']: + for ext in exts: + test_file = Path('./{}{}'.format(fname, ext)) + assert test_file.exists() + test_file.unlink() + + test_model.clear_C_api() + + # And before done, reset the deplete communicator + if mpi_intracomm is not None: + openmc.deplete.comm = orig_comm + + def test_py_C_attributes(run_in_tmpdir, pin_model_attributes, mpi_intracomm): mats, geom, settings, tals, plots, _, _, _ = pin_model_attributes test_model = openmc.Model(geom, mats, settings, tals, plots) @@ -314,7 +360,6 @@ def test_py_C_attributes(run_in_tmpdir, pin_model_attributes, mpi_intracomm): # Now lets do the cell temperature updates. # Check initial conditions - assert test_model._cells_by_id == \ {2: geom.root_universe.cells[2], 3: geom.root_universe.cells[3], 4: geom.root_universe.cells[4],