From 030a5873257d926eb3e02dd51ebe1ce062eac7a0 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 19 Feb 2020 12:48:11 -0600 Subject: [PATCH] Get the source_dlopen test working --- .../source_dlopen/clear_and_build.sh | 7 -- .../source_dlopen/inputs_true.dat | 6 +- tests/regression_tests/source_dlopen/test.py | 110 +++++++++--------- 3 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 tests/regression_tests/source_dlopen/clear_and_build.sh diff --git a/tests/regression_tests/source_dlopen/clear_and_build.sh b/tests/regression_tests/source_dlopen/clear_and_build.sh deleted file mode 100644 index 3f61b2cd8..000000000 --- a/tests/regression_tests/source_dlopen/clear_and_build.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -rm -rf build -mkdir build -cd build -cmake .. -make diff --git a/tests/regression_tests/source_dlopen/inputs_true.dat b/tests/regression_tests/source_dlopen/inputs_true.dat index 23f3d8438..23878ac20 100644 --- a/tests/regression_tests/source_dlopen/inputs_true.dat +++ b/tests/regression_tests/source_dlopen/inputs_true.dat @@ -1,7 +1,7 @@ - - + + @@ -19,5 +19,5 @@ 1000 10 0 - + diff --git a/tests/regression_tests/source_dlopen/test.py b/tests/regression_tests/source_dlopen/test.py index 1c174b775..12969cf29 100644 --- a/tests/regression_tests/source_dlopen/test.py +++ b/tests/regression_tests/source_dlopen/test.py @@ -1,75 +1,73 @@ +from pathlib import Path +import os +import shutil +import subprocess +import textwrap + import openmc import pytest -import os -import glob from tests.testing_harness import PyAPITestHarness -def __write_cmake_file(openmc_dir, build_dir): - cmake_string = "cmake_minimum_required(VERSION 3.3 FATAL_ERROR)\n" + - "project(openmc_sources CXX)\n" + - "add_library(source SHARED source_ring.cpp)\n" + - "find_package(OpenMC REQUIRED HINTS {})\n" + - "target_link_libraries(source OpenMC::libopenmc)" - f = open('CMakeLists.txt','w') - f.write(cmake_string.format(openmc_dir)) - f.close() +@pytest.fixture +def compile_source(request): + """Compile the external source""" - return + # 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_sources CXX) + add_library(source SHARED source_sampling.cpp) + find_package(OpenMC REQUIRED HINTS {}) + target_link_libraries(source OpenMC::libopenmc) + """.format(openmc_dir))) -# compile the external source -def compile_source(): + # Create temporary build directory and change to there + local_builddir = Path('build') + local_builddir.mkdir(exist_ok=True) + os.chdir(str(local_builddir)) - openmc_home_dir = os.environ['OPENMC_INSTALL_DIR'] - __write_cmake_file(openmc_home_dir,'build') + # 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) - # copy the cmakefile - status = os.system('sh clear_and_build.sh') - assert os.WEXITSTATUS(status) == 0 + yield - return 0 + # Remove local build directory when test is complete + shutil.rmtree('build') -class SourceTestHarness(PyAPITestHarness): - # build the test geometry - def _build_inputs(self): - mats = openmc.Materials() - natural_lead = openmc.Material(1, "natural_lead") - natural_lead.add_element('Pb', 1,'ao') - natural_lead.set_density('g/cm3', 11.34) - mats.append(natural_lead) +@pytest.fixture +def model(): + model = openmc.model.Model() + natural_lead = openmc.Material(name="natural_lead") + natural_lead.add_element('Pb', 1.0) + natural_lead.set_density('g/cm3', 11.34) + model.materials.append(natural_lead) - # surfaces - surface_sph1 = openmc.Sphere(r=100,boundary_type='vacuum') - volume_sph1 = -surface_sph1 - - # cell - cell_1 = openmc.Cell(region=volume_sph1) - cell_1.fill = natural_lead #assigning a material to a cell - universe = openmc.Universe(cells=[cell_1]) #hint, this list will need to include the new cell - geom = openmc.Geometry(universe) + # geometry + surface_sph1 = openmc.Sphere(r=100, boundary_type='vacuum') + cell_1 = openmc.Cell(fill=natural_lead, region=-surface_sph1) + model.geometry = openmc.Geometry([cell_1]) - # settings - sett = openmc.Settings() - batches = 10 - sett.batches = batches - sett.inactive = 0 - sett.particles = 1000 - sett.particle = "neutron" - sett.run_mode = 'fixed source' + # settings + model.settings.batches = 10 + model.settings.inactive = 0 + model.settings.particles = 1000 + model.settings.run_mode = 'fixed source' - #source - source = openmc.Source() - source.library = 'build/libsource.so' - sett.source = source + # custom source from shared library + source = openmc.Source() + source.library = 'build/libsource.so' + model.settings.source = source - # run - model = openmc.model.Model(geom,mats,sett) - model.export_to_xml() - return + return model -def test_dlopen_source(): - compile_source() - harness = SourceTestHarness('statepoint.10.h5') + +def test_dlopen_source(compile_source, model): + harness = PyAPITestHarness('statepoint.10.h5', model) harness.main()