Get the source_dlopen test working

This commit is contained in:
Paul Romano 2020-02-19 12:48:11 -06:00
parent faf454c344
commit 030a587325
3 changed files with 57 additions and 66 deletions

View file

@ -1,7 +0,0 @@
#!/bin/bash
rm -rf build
mkdir build
cd build
cmake ..
make

View file

@ -1,7 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<geometry>
<cell id="13" material="1" region="-9" universe="9" />
<surface boundary="vacuum" coeffs="0.0 0.0 0.0 100" id="9" type="sphere" />
<cell id="1" material="1" region="-1" universe="1" />
<surface boundary="vacuum" coeffs="0.0 0.0 0.0 100" id="1" type="sphere" />
</geometry>
<?xml version='1.0' encoding='utf-8'?>
<materials>
@ -19,5 +19,5 @@
<particles>1000</particles>
<batches>10</batches>
<inactive>0</inactive>
<source library="./libsource.so" strength="1.0" />
<source library="build/libsource.so" strength="1.0" />
</settings>

View file

@ -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()