Skip to content

Commit

Permalink
Merge pull request #3 from paulromano/dlopen_source
Browse files Browse the repository at this point in the history
Get the source_dlopen test working
  • Loading branch information
makeclean authored Feb 24, 2020
2 parents 2300617 + 030a587 commit a03eedf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 79 deletions.
7 changes: 0 additions & 7 deletions tests/regression_tests/source_dlopen/clear_and_build.sh

This file was deleted.

6 changes: 3 additions & 3 deletions tests/regression_tests/source_dlopen/inputs_true.dat
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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>
136 changes: 67 additions & 69 deletions tests/regression_tests/source_dlopen/test.py
Original file line number Diff line number Diff line change
@@ -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()

return

# compile the external source
def compile_source():

openmc_home_dir = os.environ['OPENMC_INSTALL_DIR']
__write_cmake_file(openmc_home_dir,'build')

# copy the cmakefile
status = os.system('sh clear_and_build.sh')
assert os.WEXITSTATUS(status) == 0

return 0

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)

# 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)

# settings
sett = openmc.Settings()
batches = 10
sett.batches = batches
sett.inactive = 0
sett.particles = 1000
sett.particle = "neutron"
sett.run_mode = 'fixed source'

#source
source = openmc.Source()
source.library = 'build/libsource.so'
sett.source = source

# run
model = openmc.model.Model(geom,mats,sett)
model.export_to_xml()
return

def test_dlopen_source():
compile_source()
harness = SourceTestHarness('statepoint.10.h5')

@pytest.fixture
def compile_source(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_sources CXX)
add_library(source SHARED source_sampling.cpp)
find_package(OpenMC REQUIRED HINTS {})
target_link_libraries(source 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

# Remove local build directory when test is complete
shutil.rmtree('build')


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

# 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
model.settings.batches = 10
model.settings.inactive = 0
model.settings.particles = 1000
model.settings.run_mode = 'fixed source'

# custom source from shared library
source = openmc.Source()
source.library = 'build/libsource.so'
model.settings.source = source

return model


def test_dlopen_source(compile_source, model):
harness = PyAPITestHarness('statepoint.10.h5', model)
harness.main()

0 comments on commit a03eedf

Please sign in to comment.