Throw exception if next_batch() is called before simulation is initialized

This commit is contained in:
Paul Romano 2017-12-15 11:38:28 +07:00
parent 8e1b8d6264
commit 08bf32e1ae
4 changed files with 40 additions and 11 deletions

View file

@ -7,7 +7,7 @@ import numpy as np
from numpy.ctypeslib import as_array
from . import _dll
from .error import _error_handler
from .error import _error_handler, AllocationError
import openmc.capi
@ -147,7 +147,7 @@ def iter_batches():
"""
while True:
# Run next batch
retval = _dll.openmc_next_batch()
retval = next_batch()
# Provide opportunity for user to perform action between batches
yield
@ -181,7 +181,11 @@ def keff():
def next_batch():
"""Run next batch."""
return _dll.openmc_next_batch()
retval = _dll.openmc_next_batch()
if retval == -3:
raise AllocationError('Simulation has not been initialized. You must call '
'openmc.capi.simulation_init() first.')
return retval
def plot_geometry():

View file

@ -73,6 +73,12 @@ contains
type(Particle) :: p
integer(8) :: i_work
! Make sure simulation has been initialized
if (.not. simulation_initialized) then
retval = -3
return
end if
call initialize_batch()
! Handle restart runs
@ -396,6 +402,9 @@ contains
subroutine openmc_simulation_init() bind(C)
! Skip if simulation has already been initialized
if (simulation_initialized) return
! Set up tally procedure pointers
call init_tally_routines()
@ -438,6 +447,9 @@ contains
! Reset current batch
current_batch = 0
! Set flag indicating initialization is done
simulation_initialized = .true.
end subroutine openmc_simulation_init
!===============================================================================
@ -455,6 +467,9 @@ contains
real(8) :: tempr(3) ! temporary array for communication
#endif
! Skip if simulation was never run
if (.not. simulation_initialized) return
! Stop active batch timer
call time_active % stop()
@ -511,6 +526,9 @@ contains
if (check_overlaps) call print_overlap_check()
end if
! Reset initialization flag
simulation_initialized = .false.
end subroutine openmc_simulation_finalize
!===============================================================================

View file

@ -18,11 +18,12 @@ module simulation_header
real(8) :: log_spacing ! spacing on logarithmic grid
! ============================================================================
! EIGENVALUE SIMULATION VARIABLES
! SIMULATION VARIABLES
integer :: current_batch ! current batch
integer :: current_gen ! current generation within a batch
integer :: total_gen = 0 ! total number of generations simulated
logical(C_BOOL), bind(C) :: simulation_initialized = .false.
! ============================================================================
! TALLY PRECISION TRIGGER VARIABLES
@ -33,6 +34,9 @@ module simulation_header
integer(8), allocatable :: work_index(:) ! starting index in source bank for each process
integer(8) :: current_work ! index in source bank of current history simulated
! ============================================================================
! K-EIGENVALUE SIMULATION VARIABLES
! Temporary k-effective values
type(VectorReal) :: k_generation ! single-generation estimates of k
real(C_DOUBLE), bind(C) :: keff = ONE ! average k over active batches

View file

@ -206,17 +206,20 @@ def test_source_bank(capi_run):
assert np.all(source['wgt'] == 1.0)
def test_keff(capi_run):
mean, std_dev = openmc.capi.keff()
assert 0.0 < mean < 2.5
assert std_dev > 0.0
def test_by_batch(capi_run):
openmc.capi.hard_reset()
# Running next batch before simulation is initialized should raise an
# exception
with pytest.raises(openmc.capi.AllocationError):
openmc.capi.next_batch()
openmc.capi.simulation_init()
for _ in openmc.capi.iter_batches():
pass
# Make sure we can get k-effective during inactive/active batches
mean, std_dev = openmc.capi.keff()
assert 0.0 < mean < 2.5
assert std_dev > 0.0
assert openmc.capi.num_realizations() == 5
for i in range(3):