forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from paulromano/dlopen_source
Get the source_dlopen test working
- Loading branch information
Showing
3 changed files
with
70 additions
and
79 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |