From 72e92e9ad05e8498de26910bc9c8ef1aa747e5b6 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 18 Jul 2019 18:14:57 -0500 Subject: [PATCH] More updates. Adding complex cell test to unit tests. --- include/openmc/capi.h | 4 +- src/cell.cpp | 5 +- src/material.cpp | 5 +- tests/regression_tests/complex_cell/test.py | 24 ------ tests/unit_tests/test_capi.py | 3 - tests/unit_tests/test_complex_cell_capi.py | 93 +++++++++++++++++++++ 6 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 tests/unit_tests/test_complex_cell_capi.py diff --git a/include/openmc/capi.h b/include/openmc/capi.h index d81e1e7226..3dd9ae6976 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -14,7 +14,7 @@ extern "C" { int openmc_cell_get_fill(int32_t index, int* type, int32_t** indices, int32_t* n); int openmc_cell_get_id(int32_t index, int32_t* id); int openmc_cell_get_temperature(int32_t index, const int32_t* instance, double* T); - int openmc_cell_get_name(int32_t index, const char*& name); + int openmc_cell_get_name(int32_t index, const char** name); int openmc_cell_set_name(int32_t index, const char* name); int openmc_cell_set_fill(int32_t index, int type, int32_t n, const int32_t* indices); int openmc_cell_set_id(int32_t index, int32_t id); @@ -60,7 +60,7 @@ extern "C" { int openmc_material_set_density(int32_t index, double density, const char* units); int openmc_material_set_densities(int32_t index, int n, const char** name, const double* density); int openmc_material_set_id(int32_t index, int32_t id); - int openmc_material_get_name(int32_t index, const char*& name); + int openmc_material_get_name(int32_t index, const char** name); int openmc_material_set_name(int32_t index, const char* name); int openmc_material_set_volume(int32_t index, double volume); int openmc_material_filter_get_bins(int32_t index, const int32_t** bins, size_t* n); diff --git a/src/cell.cpp b/src/cell.cpp index 35187fa9bb..d77e0ead79 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1085,14 +1085,13 @@ openmc_cell_get_temperature(int32_t index, const int32_t* instance, double* T) //! Get the name of a cell extern "C" int -openmc_cell_get_name(int32_t index, char** name) { +openmc_cell_get_name(int32_t index, const char** name) { if (index < 0 || index >= model::cells.size()) { strcpy(openmc_err_msg, "Index in cells array is out of bounds."); return OPENMC_E_OUT_OF_BOUNDS; } - auto name_str = model::cells[index]->name_; - strcpy(*name, name_str.c_str()); + *name = model::cells[index]->name_.c_str(); return 0; } diff --git a/src/material.cpp b/src/material.cpp index 356c2bf212..292f71b404 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -1382,14 +1382,13 @@ openmc_material_set_id(int32_t index, int32_t id) } extern "C" int -openmc_material_get_name(int32_t index, char** name) { +openmc_material_get_name(int32_t index, const char** name) { if (index < 0 || index >= model::materials.size()) { strcpy(openmc_err_msg, "Index in materials array is out of bounds."); return OPENMC_E_OUT_OF_BOUNDS; } - auto name_str = model::materials[index]->name(); - strcpy(*name, name_str.c_str()); + *name = model::materials[index]->name_.data(); return 0; } diff --git a/tests/regression_tests/complex_cell/test.py b/tests/regression_tests/complex_cell/test.py index 8074c0e05a..b43ccd72f9 100755 --- a/tests/regression_tests/complex_cell/test.py +++ b/tests/regression_tests/complex_cell/test.py @@ -7,27 +7,3 @@ import openmc.capi def test_complex_cell(): harness = TestHarness('statepoint.10.h5') harness.main() - -def test_complex_cell_capi(): - # initialize - openmc.capi.init([]) - - inf = sys.float_info.max - - expected_boxes = { 1 : (( -4., -4., -inf), ( 4., 4., inf)), - 2 : (( -7., -7., -inf), ( 7., 7., inf)), - 3 : ((-10., -10., -inf), (10., 10., inf)), - 4 : ((-10., -10., -inf), (10., 10., inf)) } - - for cell_id, cell in openmc.capi.cells.items(): - cell_box = cell.bounding_box - - assert tuple(cell_box[0]) == expected_boxes[cell_id][0] - assert tuple(cell_box[1]) == expected_boxes[cell_id][1] - - cell_box = openmc.capi.bounding_box("Cell", cell_id) - - assert tuple(cell_box[0]) == expected_boxes[cell_id][0] - assert tuple(cell_box[1]) == expected_boxes[cell_id][1] - - openmc.capi.finalize() diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 1c1da98a37..b02e43815f 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -479,9 +479,6 @@ def test_bounding_box(capi_init): llc, urc = openmc.capi.bounding_box("Surface", 5) - print(llc) - print(urc) - assert tuple(llc) == expected_llc assert tuple(urc) == expected_urc diff --git a/tests/unit_tests/test_complex_cell_capi.py b/tests/unit_tests/test_complex_cell_capi.py new file mode 100644 index 0000000000..20cda54aeb --- /dev/null +++ b/tests/unit_tests/test_complex_cell_capi.py @@ -0,0 +1,93 @@ +import sys + +import numpy as np +import openmc.capi + + +def test_complex_cell(run_in_tmpdir): + + openmc.reset_auto_ids() + + model = openmc.model.Model() + + u235 = openmc.Material() + u235.set_density('g/cc', 4.5) + u235.add_nuclide("U235", 1.0) + + u238 = openmc.Material() + u238.set_density('g/cc', 4.5) + u238.add_nuclide("U238", 1.0) + + zr90 = openmc.Material() + zr90.set_density('g/cc', 2.0) + zr90.add_nuclide("Zr90", 1.0) + + n14 = openmc.Material() + n14.set_density('g/cc', 0.1) + n14.add_nuclide("N14", 1.0) + + model.materials = (u235, u238, zr90, n14) + + s1 = openmc.XPlane(x0=-10.0, boundary_type='vacuum') + s2 = openmc.XPlane(x0=-7.0) + s3 = openmc.XPlane(x0=-4.0) + s4 = openmc.XPlane(x0=4.0) + s5 = openmc.XPlane(x0=7.0) + s6 = openmc.XPlane(x0=10.0, boundary_type='vacuum') + s7 = openmc.XPlane(x0=0.0) + + s11 = openmc.YPlane(y0=-10.0, boundary_type='vacuum') + s12 = openmc.YPlane(y0=-7.0) + s13 = openmc.YPlane(y0=-4.0) + s14 = openmc.YPlane(y0=4.0) + s15 = openmc.YPlane(y0=7.0) + s16 = openmc.YPlane(y0=10.0, boundary_type='vacuum') + s17 = openmc.YPlane(y0=0.0) + + c1 = openmc.Cell(fill=u235) + c1.region = +s3 & -s4 & +s13 & -s14 + + c2 = openmc.Cell(fill=u238) + c2.region = +s2 & -s5 & +s12 & -s15 & ~(+s3 & -s4 & +s13 & -s14) + + c3 = openmc.Cell(fill=zr90) + c3.region = ((+s1 & -s7 & +s17 & -s16) | (+s7 & -s6 & +s11 & -s17)) & (-s2 | +s5 | -s12 | +s15) + + c4 = openmc.Cell(fill=n14) + c4.region = ((+s1 & -s7 & +s11 & -s17) | (+s7 & -s6 & +s17 & -s16)) & ~(+s2 & -s5 & +s12 & -s15) + + model.geometry.root_universe = openmc.Universe() + model.geometry.root_universe.add_cells([c1, c2, c3, c4]) + + model.settings.batches = 10 + model.settings.inactive = 5 + model.settings.particles = 100 + model.settings.source = openmc.Source(space=openmc.stats.Box( + [-10., -10., -1.], [10., 10., 1.])) + + model.settings.verbosity = 1 + + model.export_to_xml() + + openmc.capi.finalize() + openmc.capi.init() + + inf = sys.float_info.max + + expected_boxes = { 1 : (( -4., -4., -inf), ( 4., 4., inf)), + 2 : (( -7., -7., -inf), ( 7., 7., inf)), + 3 : ((-10., -10., -inf), (10., 10., inf)), + 4 : ((-10., -10., -inf), (10., 10., inf)) } + + for cell_id, cell in openmc.capi.cells.items(): + cell_box = cell.bounding_box + + assert tuple(cell_box[0]) == expected_boxes[cell_id][0] + assert tuple(cell_box[1]) == expected_boxes[cell_id][1] + + cell_box = openmc.capi.bounding_box("Cell", cell_id) + + assert tuple(cell_box[0]) == expected_boxes[cell_id][0] + assert tuple(cell_box[1]) == expected_boxes[cell_id][1] + + openmc.capi.finalize()