diff --git a/tests/regression_tests/cpp_driver/__init__.py b/tests/regression_tests/cpp_driver/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/regression_tests/cpp_driver/driver.cpp b/tests/regression_tests/cpp_driver/driver.cpp new file mode 100644 index 000000000..2bc771b24 --- /dev/null +++ b/tests/regression_tests/cpp_driver/driver.cpp @@ -0,0 +1,35 @@ + +#include "openmc/capi.h" +#include "openmc/cell.h" +#include "openmc/tallies/filter.h" +#include "openmc/tallies/filter_cell.h" +#include "openmc/tallies/tally.h" + +using namespace openmc; + +int main(int argc, char** argv) { + openmc_init(argc, argv, nullptr); + + // create a new cell filter + auto cell_filter = Filter::create(); + + // add all cells to the cell filter + std::vector cell_indices; + for (auto& entry : openmc::model::cell_map) { + cell_indices.push_back(entry.second); + } + // sort to make sure the cell bins appear in the same + // order as the test relying on the openmc exe + std::sort(cell_indices.begin(), cell_indices.end()); + cell_filter->set_cells(cell_indices); + + // create a new tally + auto tally = Tally::create(); + std::vector filters = {cell_filter}; + tally->set_filters(filters); + tally->set_scores({"flux"}); + + openmc_run(); + openmc_finalize(); + return 0; +} diff --git a/tests/regression_tests/cpp_driver/inputs_true.dat b/tests/regression_tests/cpp_driver/inputs_true.dat new file mode 100644 index 000000000..bcaac5af6 --- /dev/null +++ b/tests/regression_tests/cpp_driver/inputs_true.dat @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + eigenvalue + 100 + 10 + 1 + + + -0.63 -0.63 -1 0.63 0.63 1 + + + + + + + 1 2 3 + + + 2 + flux + + diff --git a/tests/regression_tests/cpp_driver/inputs_true1.dat b/tests/regression_tests/cpp_driver/inputs_true1.dat new file mode 100644 index 000000000..fae59e202 --- /dev/null +++ b/tests/regression_tests/cpp_driver/inputs_true1.dat @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + eigenvalue + 100 + 10 + 1 + + + -0.63 -0.63 -1 0.63 0.63 1 + + + diff --git a/tests/regression_tests/cpp_driver/inputs_true2.dat b/tests/regression_tests/cpp_driver/inputs_true2.dat new file mode 100644 index 000000000..728491d77 --- /dev/null +++ b/tests/regression_tests/cpp_driver/inputs_true2.dat @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + eigenvalue + 100 + 10 + 1 + + + -0.63 -0.63 -1 0.63 0.63 1 + + + + + + + 4 5 6 + + + 2 + flux + + diff --git a/tests/regression_tests/cpp_driver/results_true.dat b/tests/regression_tests/cpp_driver/results_true.dat new file mode 100644 index 000000000..32a636636 --- /dev/null +++ b/tests/regression_tests/cpp_driver/results_true.dat @@ -0,0 +1,9 @@ +k-combined: +1.141180E+00 4.513757E-02 +tally 1: +1.220313E+02 +1.659250E+03 +4.421703E+01 +2.178300E+02 +2.356492E+02 +6.182509E+03 diff --git a/tests/regression_tests/cpp_driver/test.py b/tests/regression_tests/cpp_driver/test.py new file mode 100644 index 000000000..02ddc621b --- /dev/null +++ b/tests/regression_tests/cpp_driver/test.py @@ -0,0 +1,75 @@ +from pathlib import Path +import os +import shutil +import subprocess +import textwrap + +import openmc +import pytest + +from tests.testing_harness import PyAPITestHarness + + +@pytest.fixture +def driver(request): + """Compile the external source""" + + # Get build directory and write CMakeLists.txt file + openmc_dir = Path(str(request.config.rootdir)) / 'build' + with open('CMakeLists.txt', 'w') as f: + f.write(textwrap.dedent(""" + cmake_minimum_required(VERSION 3.3 FATAL_ERROR) + project(openmc_cpp_driver CXX) + add_executable(cpp_driver driver.cpp) + find_package(OpenMC REQUIRED HINTS {}) + target_link_libraries(cpp_driver OpenMC::libopenmc) + """.format(openmc_dir))) + + # Create temporary build directory and change to there + local_builddir = Path('build') + local_builddir.mkdir(exist_ok=True) + os.chdir(str(local_builddir)) + + # Run cmake/make to build the shared libary + subprocess.run(['cmake', os.path.pardir], check=True) + subprocess.run(['make'], check=True) + os.chdir(os.path.pardir) + + yield "./build/cpp_driver" + + # Remove local build directory when test is complete + shutil.rmtree('build') + + +@pytest.fixture +def model(): + model = openmc.examples.pwr_pin_cell() + model.settings.particles = 100 + model.settings.batches = 10 + model.settings.inactive = 1 + return model + +class ExternalDriverTestHarness(PyAPITestHarness): + + def __init__(self, executable, statepoint_name, model=None, inputs_true=None): + super().__init__(statepoint_name, model, inputs_true) + self.executable = executable + + def _run_openmc(self): + print("Running openmc") + openmc.run(openmc_exec=self.executable) + + +def test_cpp_driver(driver, model): + harness = ExternalDriverTestHarness(driver, 'statepoint.10.h5', model, 'inputs_true1.dat') + harness.main() + +def test_openmc_run(driver, model): + # modify model and test again using the openmc exe + cell_filter = openmc.CellFilter(list(model.geometry.get_all_cells().values())) + tally = openmc.Tally() + tally.filters = [cell_filter] + tally.scores = ['flux'] + model.tallies = [tally] + harness = PyAPITestHarness('statepoint.10.h5', model, 'inputs_true2.dat') + harness.main()