From 7f66b4694cc57b728663e15e3f70148c4dfe7bed Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 15 Sep 2017 14:04:31 -0500 Subject: [PATCH] Get rid of n_active global, allow a few settings to be controlled from openmc.capi --- openmc/capi/__init__.py | 1 + openmc/capi/settings.py | 65 +++++++++++++++++++++++++++++++++++++++++ src/input_xml.F90 | 5 +--- src/output.F90 | 2 ++ src/settings.F90 | 15 +++++----- 5 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 openmc/capi/settings.py diff --git a/openmc/capi/__init__.py b/openmc/capi/__init__.py index 1480707577..88a69df341 100644 --- a/openmc/capi/__init__.py +++ b/openmc/capi/__init__.py @@ -37,3 +37,4 @@ from .material import * from .cell import * from .filter import * from .tally import * +from .settings import settings diff --git a/openmc/capi/settings.py b/openmc/capi/settings.py new file mode 100644 index 0000000000..40d1768cfd --- /dev/null +++ b/openmc/capi/settings.py @@ -0,0 +1,65 @@ +from ctypes import c_int, c_int32, c_int64, c_double, c_char_p, POINTER + +from . import _dll +from .error import _error_handler + +_RUN_MODES = {1: 'fixed source', + 2: 'eigenvalue', + 3: 'plot', + 4: 'particle restart', + 5: 'volume'} + + +class _Settings(object): + @property + def batches(self): + return c_int32.in_dll(_dll, 'n_batches').value + + @batches.setter + def batches(self, n): + _dll.openmc_set_batches(n) + + @property + def generations_per_batch(self): + return c_int32.in_dll(_dll, 'gen_per_batch').value + + @generations_per_batch.setter + def generations_per_batch(self, n): + _dll.openmc_set_generations_per_batch(n) + + @property + def inactive(self): + return c_int32.in_dll(_dll, 'n_inactive').value + + @inactive.setter + def inactive(self, n): + _dll.openmc_set_inactive_batches(n) + + @property + def particles(self): + return c_int64.in_dll(_dll, 'n_particles').value + + @particles.setter + def particles(self, n): + _dll.openmc_set_particles(n) + + @property + def run_mode(self): + i = c_int.in_dll(_dll, 'run_mode').value + try: + return _RUN_MODES[i] + except KeyError: + return None + + @run_mode.setter + def run_mode(self, mode): + current_idx = c_int.in_dll(_dll, 'run_mode') + for idx, mode_value in _RUN_MODES.items(): + if mode_value == mode: + current_idx.value = idx + break + else: + raise ValueError('Invalid run mode: {}'.format(mode)) + + +settings = _Settings() diff --git a/src/input_xml.F90 b/src/input_xml.F90 index c7e80fe989..0ef4d2b306 100644 --- a/src/input_xml.F90 +++ b/src/input_xml.F90 @@ -322,7 +322,7 @@ contains call get_run_parameters(node_mode) ! Check number of active batches, inactive batches, and particles - if (n_active <= 0) then + if (n_batches <= n_inactive) then call fatal_error("Number of active batches must be greater than zero.") elseif (n_inactive < 0) then call fatal_error("Number of inactive batches must be non-negative.") @@ -891,9 +891,6 @@ contains end if end if - ! Determine number of active batches - n_active = n_batches - n_inactive - end subroutine get_run_parameters !=============================================================================== diff --git a/src/output.F90 b/src/output.F90 index 215942bfe6..e3d2b8ac14 100644 --- a/src/output.F90 +++ b/src/output.F90 @@ -532,6 +532,7 @@ contains subroutine print_runtime() + integer :: n_active real(8) :: speed_inactive ! # of neutrons/second in inactive batches real(8) :: speed_active ! # of neutrons/second in active batches character(15) :: string @@ -564,6 +565,7 @@ contains write(ou,100) "Total time elapsed", time_total % elapsed ! Calculate particle rate in active/inactive batches + n_active = n_batches - n_inactive if (restart_run) then if (restart_batch < n_inactive) then speed_inactive = real(n_particles * (n_inactive - restart_batch) * & diff --git a/src/settings.F90 b/src/settings.F90 index d1d41a75c7..add7f5b903 100644 --- a/src/settings.F90 +++ b/src/settings.F90 @@ -1,12 +1,14 @@ module settings + use, intrinsic :: ISO_C_BINDING + use constants use set_header, only: SetInt use source_header implicit none - ! ============================================================================ + ! ============================================================================ ! ENERGY TREATMENT RELATED VARIABLES logical :: run_CE = .true. ! Run in CE mode? @@ -46,11 +48,10 @@ module settings ! ============================================================================ ! SIMULATION VARIABLES - integer(8) :: n_particles = 0 ! # of particles per generation - integer :: n_batches ! # of batches - integer :: n_inactive ! # of inactive batches - integer :: n_active ! # of active batches - integer :: gen_per_batch = 1 ! # of generations per batch + integer(C_INT64_T), bind(C) :: n_particles = 0 ! # of particles per generation + integer(C_INT32_T), bind(C) :: n_batches ! # of batches + integer(C_INT32_T), bind(C) :: n_inactive ! # of inactive batches + integer(C_INT32_T), bind(C) :: gen_per_batch = 1 ! # of generations per batch integer :: n_max_batches ! max # of batches integer :: n_batch_interval = 1 ! batch interval for triggers @@ -75,7 +76,7 @@ module settings real(8) :: weight_survive = ONE ! Mode to run in (fixed source, eigenvalue, plotting, etc) - integer :: run_mode = NONE + integer(C_INT), bind(C) :: run_mode = NONE ! Restart run logical :: restart_run = .false.