Allow openmc.capi.statepoint_write to pass filename

This commit is contained in:
Paul Romano 2017-12-01 11:21:30 -06:00
parent ad0283d016
commit 4fbd9d75c2
2 changed files with 32 additions and 13 deletions

View file

@ -1,5 +1,6 @@
from contextlib import contextmanager
from ctypes import CDLL, c_int, c_int32, c_int64, c_double, POINTER, Structure
from ctypes import (CDLL, c_int, c_int32, c_int64, c_double, c_char_p,
POINTER, Structure)
from warnings import warn
import numpy as np
@ -38,6 +39,7 @@ _dll.openmc_source_bank.restype = c_int
_dll.openmc_source_bank.errcheck = _error_handler
_dll.openmc_simulation_init.restype = None
_dll.openmc_simulation_finalize.restype = None
_dll.openmc_statepoint_write.argtypes = [POINTER(c_char_p)]
_dll.openmc_statepoint_write.restype = None
@ -217,9 +219,19 @@ def source_bank():
return as_array(ptr, (n.value,)).view(bank_dtype)
def statepoint_write():
"""Write a statepoint."""
_dll.openmc_statepoint_write()
def statepoint_write(filename=None):
"""Write a statepoint file.
Parameters
----------
filename : str or None
Path to the statepoint to write. If None is passed, a default name that
contains the current batch will be written.
"""
if filename is not None:
filename = c_char_p(filename.encode())
_dll.openmc_statepoint_write(filename)
@contextmanager

View file

@ -29,7 +29,7 @@ module state_point
use random_lcg, only: seed
use settings
use simulation_header
use string, only: to_str, count_digits, zero_padded
use string, only: to_str, count_digits, zero_padded, to_f_string
use tally_header
use tally_filter_header
use tally_derivative_header, only: tally_derivs
@ -43,7 +43,8 @@ contains
! OPENMC_STATEPOINT_WRITE writes an HDF5 statepoint file to disk
!===============================================================================
subroutine openmc_statepoint_write() bind(C)
subroutine openmc_statepoint_write(filename) bind(C)
type(C_PTR), intent(in), optional :: filename
integer :: i, j, k
integer :: i_xs
@ -57,19 +58,25 @@ contains
integer(C_INT) :: err
real(C_DOUBLE) :: k_combined(2)
character(MAX_WORD_LEN), allocatable :: str_array(:)
character(MAX_FILE_LEN) :: filename
character(C_CHAR), pointer :: string(:)
character(len=:, kind=C_CHAR), allocatable :: filename_
! Set filename for state point
filename = trim(path_output) // 'statepoint.' // &
& zero_padded(current_batch, count_digits(n_max_batches))
filename = trim(filename) // '.h5'
if (present(filename)) then
call c_f_pointer(filename, string, [MAX_FILE_LEN])
filename_ = to_f_string(string)
else
! Set filename for state point
filename_ = trim(path_output) // 'statepoint.' // &
& zero_padded(current_batch, count_digits(n_max_batches))
filename_ = trim(filename_) // '.h5'
end if
! Write message
call write_message("Creating state point " // trim(filename) // "...", 5)
call write_message("Creating state point " // trim(filename_) // "...", 5)
if (master) then
! Create statepoint file
file_id = file_create(filename)
file_id = file_create(filename_)
! Write file type
call write_attribute(file_id, "filetype", "statepoint")