Skip to content

Commit 0a8f597

Browse files
committed
Be more consistent with C API function return values
1 parent 30944b5 commit 0a8f597

9 files changed

Lines changed: 109 additions & 55 deletions

File tree

‎docs/source/capi/index.rst‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ Type Definitions
4646
Functions
4747
---------
4848

49-
.. c:function:: void openmc_calculate_volumes()
49+
.. c:function:: int openmc_calculate_volumes()
5050
5151
Run a stochastic volume calculation
5252
53+
:return: Return status (negative if an error occurred)
54+
:rtype: int
55+
5356
.. c:function:: int openmc_cell_get_fill(int32_t index, int* type, int32_t** indices, int32_t* n)
5457
5558
Get the fill for a cell
@@ -192,11 +195,14 @@ Functions
192195
:return: Return status (negative if an error occurred)
193196
:rtype: int
194197
195-
.. c:function:: void openmc_finalize()
198+
.. c:function:: int openmc_finalize()
196199
197200
Finalize a simulation
198201
199-
.. c:function:: void openmc_find(double* xyz, int rtype, int32_t* id, int32_t* instance)
202+
:return: Return status (negative if an error occurs)
203+
:rtype: int
204+
205+
.. c:function:: int openmc_find(double* xyz, int rtype, int32_t* id, int32_t* instance)
200206
201207
Determine the ID of the cell/material containing a given point
202208
@@ -207,6 +213,8 @@ Functions
207213
occurs, the ID is -1.
208214
:param int32_t* instance: If a cell is repeated in the geometry, the instance
209215
of the cell that was found and zero otherwise.
216+
:return: Return status (negative if an error occurs)
217+
:rtype: int
210218
211219
.. c:function:: int openmc_get_cell_index(int32_t id, int32_t* index)
212220
@@ -266,11 +274,14 @@ Functions
266274
:return: Return status (negative if an error occurs)
267275
:rtype: int
268276
269-
.. c:function:: void openmc_hard_reset()
277+
.. c:function:: int openmc_hard_reset()
270278
271279
Reset tallies, timers, and pseudo-random number generator state
272280
273-
.. c:function:: void openmc_init(int argc, char** argv, const void* intracomm)
281+
:return: Return status (negative if an error occurs)
282+
:rtype: int
283+
284+
.. c:function:: int openmc_init(int argc, char** argv, const void* intracomm)
274285
275286
Initialize OpenMC
276287
@@ -279,6 +290,8 @@ Functions
279290
:param intracomm: MPI intracommunicator. If MPI is not being used, a null
280291
pointer should be passed.
281292
:type intracomm: const void*
293+
:return: Return status (negative if an error occurs)
294+
:rtype: int
282295
283296
.. c:function:: int openmc_load_nuclide(char name[])
284297
@@ -396,26 +409,41 @@ Functions
396409
:return: Return status (negative if an error occurs)
397410
:rtype: int
398411
399-
.. c:function:: void openmc_plot_geometry()
412+
.. c:function:: int openmc_plot_geometry()
400413
401414
Run plotting mode.
402415
403-
.. c:function:: void openmc_reset()
416+
:return: Return status (negative if an error occurs)
417+
:rtype: int
418+
419+
.. c:function:: int openmc_reset()
404420
405421
Resets all tally scores
406422
407-
.. c:function:: void openmc_run()
423+
:return: Return status (negative if an error occurs)
424+
:rtype: int
425+
426+
.. c:function:: int openmc_run()
408427
409428
Run a simulation
410429
411-
.. c:function:: void openmc_simulation_finalize()
430+
:return: Return status (negative if an error occurs)
431+
:rtype: int
432+
433+
.. c:function:: int openmc_simulation_finalize()
412434
413435
Finalize a simulation.
414436
415-
.. c:function:: void openmc_simulation_init()
437+
:return: Return status (negative if an error occurs)
438+
:rtype: int
439+
440+
.. c:function:: int openmc_simulation_init()
416441
417442
Initialize a simulation. Must be called after openmc_init().
418443
444+
:return: Return status (negative if an error occurs)
445+
:rtype: int
446+
419447
.. c:function:: int openmc_source_bank(struct Bank** ptr, int64_t* n)
420448
421449
Return a pointer to the source bank array.
@@ -435,13 +463,15 @@ Functions
435463
:return: Return status (negative if an error occurred)
436464
:rtype: int
437465
438-
.. c:function:: void openmc_statepoint_write(const char filename[])
466+
.. c:function:: int openmc_statepoint_write(const char filename[])
439467
440468
Write a statepoint file
441469
442470
:param filename: Name of file to create. If a null pointer is passed, a
443471
filename is assigned automatically.
444472
:type filename: const char[]
473+
:return: Return status (negative if an error occurs)
474+
:rtype: int
445475
446476
.. c:function:: int openmc_tally_get_id(int32_t index, int32_t* id)
447477

‎include/openmc.h‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <stdint.h>
55
#include <stdbool.h>
66

7-
87
#ifdef __cplusplus
98
extern "C" {
109
#endif
@@ -17,7 +16,7 @@ extern "C" {
1716
int delayed_group;
1817
};
1918

20-
void openmc_calculate_volumes();
19+
int openmc_calculate_volumes();
2120
int openmc_cell_get_fill(int32_t index, int* type, int32_t** indices, int32_t* n);
2221
int openmc_cell_get_id(int32_t index, int32_t* id);
2322
int openmc_cell_set_fill(int32_t index, int type, int32_t n, const int32_t* indices);
@@ -35,7 +34,7 @@ extern "C" {
3534
int openmc_filter_get_type(int32_t index, const char** type);
3635
int openmc_filter_set_id(int32_t index, int32_t id);
3736
int openmc_filter_set_type(int32_t index, const char* type);
38-
void openmc_finalize();
37+
int openmc_finalize();
3938
int openmc_find(double* xyz, int rtype, int32_t* id, int32_t* instance);
4039
int openmc_get_cell_index(int32_t id, int32_t* index);
4140
int openmc_get_filter_index(int32_t id, int32_t* index);
@@ -46,7 +45,7 @@ extern "C" {
4645
int openmc_get_nuclide_index(const char name[], int* index);
4746
int64_t openmc_get_seed();
4847
int openmc_get_tally_index(int32_t id, int32_t* index);
49-
void openmc_hard_reset();
48+
int openmc_hard_reset();
5049
int openmc_init(int argc, char* argv[], const void* intracomm);
5150
int openmc_init_f(const int* intracomm);
5251
int openmc_legendre_filter_get_order(int32_t index, int* order);
@@ -73,12 +72,12 @@ extern "C" {
7372
int openmc_next_batch(int* status);
7473
int openmc_nuclide_name(int index, char** name);
7574
int openmc_particle_restart();
76-
void openmc_plot_geometry();
77-
void openmc_reset();
75+
int openmc_plot_geometry();
76+
int openmc_reset();
7877
int openmc_run();
7978
void openmc_set_seed(int64_t new_seed);
80-
void openmc_simulation_finalize();
81-
void openmc_simulation_init();
79+
int openmc_simulation_finalize();
80+
int openmc_simulation_init();
8281
int openmc_source_bank(struct Bank** ptr, int64_t* n);
8382
int openmc_source_set_strength(int32_t index, double strength);
8483
int openmc_spatial_legendre_filter_get_order(int32_t index, int* order);
@@ -90,7 +89,7 @@ extern "C" {
9089
int openmc_sphharm_filter_get_cosine(int32_t index, char cosine[]);
9190
int openmc_sphharm_filter_set_order(int32_t index, int order);
9291
int openmc_sphharm_filter_set_cosine(int32_t index, const char cosine[]);
93-
void openmc_statepoint_write(const char filename[]);
92+
int openmc_statepoint_write(const char filename[]);
9493
int openmc_tally_get_active(int32_t index, bool* active);
9594
int openmc_tally_get_id(int32_t index, int32_t* id);
9695
int openmc_tally_get_filters(int32_t index, int32_t** indices, int* n);

‎openmc/capi/core.py‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,41 @@ class _Bank(Structure):
2020
('delayed_group', c_int)]
2121

2222

23-
_dll.openmc_calculate_volumes.restype = None
24-
_dll.openmc_finalize.restype = None
23+
_dll.openmc_calculate_volumes.restype = c_int
24+
_dll.openmc_calculate_volumes.errcheck = _error_handler
25+
_dll.openmc_finalize.restype = c_int
26+
_dll.openmc_finalize.errcheck = _error_handler
2527
_dll.openmc_find.argtypes = [POINTER(c_double*3), c_int, POINTER(c_int32),
2628
POINTER(c_int32)]
2729
_dll.openmc_find.restype = c_int
2830
_dll.openmc_find.errcheck = _error_handler
29-
_dll.openmc_hard_reset.restype = None
31+
_dll.openmc_hard_reset.restype = c_int
32+
_dll.openmc_hard_reset.errcheck = _error_handler
3033
_dll.openmc_init.argtypes = [c_int, POINTER(c_char_p), c_void_p]
31-
_dll.openmc_init.restype = None
34+
_dll.openmc_init.restype = c_int
35+
_dll.openmc_init.errcheck = _error_handler
3236
_dll.openmc_get_keff.argtypes = [POINTER(c_double*2)]
3337
_dll.openmc_get_keff.restype = c_int
3438
_dll.openmc_get_keff.errcheck = _error_handler
3539
_dll.openmc_next_batch.argtypes = [POINTER(c_int)]
3640
_dll.openmc_next_batch.restype = c_int
3741
_dll.openmc_next_batch.errcheck = _error_handler
38-
_dll.openmc_plot_geometry.restype = None
42+
_dll.openmc_plot_geometry.restype = c_int
43+
_dll.openmc_plot_geometry.restype = _error_handler
3944
_dll.openmc_run.restype = c_int
4045
_dll.openmc_run.errcheck = _error_handler
41-
_dll.openmc_reset.restype = None
46+
_dll.openmc_reset.restype = c_int
47+
_dll.openmc_reset.errcheck = _error_handler
4248
_dll.openmc_source_bank.argtypes = [POINTER(POINTER(_Bank)), POINTER(c_int64)]
4349
_dll.openmc_source_bank.restype = c_int
4450
_dll.openmc_source_bank.errcheck = _error_handler
45-
_dll.openmc_simulation_init.restype = None
46-
_dll.openmc_simulation_finalize.restype = None
51+
_dll.openmc_simulation_init.restype = c_int
52+
_dll.openmc_simulation_init.errcheck = _error_handler
53+
_dll.openmc_simulation_finalize.restype = c_int
54+
_dll.openmc_simulation_finalize.errcheck = _error_handler
4755
_dll.openmc_statepoint_write.argtypes = [POINTER(c_char_p)]
48-
_dll.openmc_statepoint_write.restype = None
56+
_dll.openmc_statepoint_write.restype = c_int
57+
_dll.openmc_statepoint_write.errcheck = _error_handler
4958

5059

5160
def calculate_volumes():

‎src/api.F90‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,11 @@ module openmc_api
102102
! variables
103103
!===============================================================================
104104

105-
subroutine openmc_finalize() bind(C)
106-
107-
#ifdef OPENMC_MPI
108-
integer :: err
109-
#endif
105+
function openmc_finalize() result(err) bind(C)
106+
integer(C_INT) :: err
110107

111108
! Clear results
112-
call openmc_reset()
109+
err = openmc_reset()
113110

114111
! Reset global variables
115112
assume_separate = .false.
@@ -169,12 +166,13 @@ subroutine openmc_finalize() bind(C)
169166
! Deallocate arrays
170167
call free_memory()
171168

169+
err = 0
172170
#ifdef OPENMC_MPI
173171
! Free all MPI types
174172
call MPI_TYPE_FREE(MPI_BANK, err)
175173
#endif
176174

177-
end subroutine openmc_finalize
175+
end function openmc_finalize
178176

179177
!===============================================================================
180178
! OPENMC_FIND determines the ID or a cell or material at a given point in space
@@ -225,23 +223,27 @@ end function openmc_find
225223
! generator state
226224
!===============================================================================
227225

228-
subroutine openmc_hard_reset() bind(C)
226+
function openmc_hard_reset() result(err) bind(C)
227+
integer(C_INT) :: err
228+
229229
! Reset all tallies and timers
230-
call openmc_reset()
230+
err = openmc_reset()
231231

232232
! Reset total generations and keff guess
233233
keff = ONE
234234
total_gen = 0
235235

236236
! Reset the random number generator state
237237
call openmc_set_seed(DEFAULT_SEED)
238-
end subroutine openmc_hard_reset
238+
end function openmc_hard_reset
239239

240240
!===============================================================================
241241
! OPENMC_RESET resets tallies and timers
242242
!===============================================================================
243243

244-
subroutine openmc_reset() bind(C)
244+
function openmc_reset() result(err) bind(C)
245+
integer(C_INT) :: err
246+
245247
integer :: i
246248

247249
if (allocated(tallies)) then
@@ -289,7 +291,8 @@ subroutine openmc_reset() bind(C)
289291
call time_transport % reset()
290292
call time_finalize % reset()
291293

292-
end subroutine openmc_reset
294+
err = 0
295+
end function openmc_reset
293296

294297
!===============================================================================
295298
! FREE_MEMORY deallocates and clears all global allocatable arrays in the

‎src/main.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ int main(int argc, char* argv[]) {
2929
err = openmc_run();
3030
break;
3131
case RUN_MODE_PLOTTING:
32-
openmc_plot_geometry();
32+
err = openmc_plot_geometry();
3333
break;
3434
case RUN_MODE_PARTICLE:
3535
if (openmc_master) err = openmc_particle_restart();
3636
break;
3737
case RUN_MODE_VOLUME:
38-
openmc_calculate_volumes();
38+
err = openmc_calculate_volumes();
3939
break;
4040
}
4141

‎src/plot.F90‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ module plot
3030
! RUN_PLOT controls the logic for making one or many plots
3131
!===============================================================================
3232

33-
subroutine openmc_plot_geometry() bind(C)
33+
function openmc_plot_geometry() result(err) bind(C)
34+
integer(C_INT) :: err
3435

3536
integer :: i ! loop index for plots
3637

@@ -50,7 +51,8 @@ subroutine openmc_plot_geometry() bind(C)
5051
end associate
5152
end do
5253

53-
end subroutine openmc_plot_geometry
54+
err = 0
55+
end function openmc_plot_geometry
5456

5557
!===============================================================================
5658
! POSITION_RGB computes the red/green/blue values for a given plot with the

0 commit comments

Comments
 (0)