From a4f99cb99c3adcda1fb5403c2a70b041d61fa456 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 7 Aug 2019 08:02:38 +0100 Subject: [PATCH] Adding unit test for dlopen source --- .../dlopen_source/source_sampling.cpp | 23 ++++++++ .../dlopen_source/test_dlopen_source.py | 57 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/unit_tests/dlopen_source/source_sampling.cpp create mode 100644 tests/unit_tests/dlopen_source/test_dlopen_source.py diff --git a/tests/unit_tests/dlopen_source/source_sampling.cpp b/tests/unit_tests/dlopen_source/source_sampling.cpp new file mode 100644 index 000000000..4b13c7db9 --- /dev/null +++ b/tests/unit_tests/dlopen_source/source_sampling.cpp @@ -0,0 +1,23 @@ +#include +#include "openmc/random_lcg.h" +#include "openmc/source.h" +#include "openmc/particle.h" + +// you must have external C linkage here otherwise +// dlopen will not find the file +extern "C" openmc::Particle::Bank sample_source() { + openmc::Particle::Bank particle; + // wgt + particle.particle = openmc::Particle::Type::neutron; + particle.wgt = 1.0; + // position + + particle.r.x = 0.; + particle.r.y = 0.; + particle.r.z = 0.; + // angle + particle.u = {1.,0,0}; + particle.E = 14.08e6; + particle.delayed_group = 0; + return particle; +} diff --git a/tests/unit_tests/dlopen_source/test_dlopen_source.py b/tests/unit_tests/dlopen_source/test_dlopen_source.py new file mode 100644 index 000000000..917f3fd4f --- /dev/null +++ b/tests/unit_tests/dlopen_source/test_dlopen_source.py @@ -0,0 +1,57 @@ +import openmc +import pytest +import os + +# compile the external source +def compile_source(): + # needs a more robust way to know where the + # Makefile is + status = os.system('cp /usr/local/share/Makefile .') + assert os.WEXITSTATUS(status) == 0 + status = os.system('make') + assert os.WEXITSTATUS(status) == 0 + + return + +# build the test geometry +def make_geometry(): + 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) + 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 = PATH+'source_sampling.so' + sett.source = source + + # run + model = openmc.model.Model(geom,mats,sett) + return model + +def test_dlopen_source(): + compile_source() + model = make_geometry() + model.run() +