Skip to content

Commit fc0a9ed

Browse files
committed
pop message
1 parent e5ec971 commit fc0a9ed

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

rebound/simulation.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,16 @@ def simulationarchive_filename(self):
250250

251251
# Message and memory management functions
252252
def process_messages(self):
253-
clibrebound.reb_simulation_get_next_message.restype = c_int
253+
clibrebound.reb_pop_message.restype = c_int
254+
clibrebound.reb_pop_message.argtypes = [c_void_p, c_char*1024]
254255
buf = create_string_buffer(c_int.in_dll(clibrebound, "reb_messages_max_length").value)
255-
while clibrebound.reb_simulation_get_next_message(byref(self), buf):
256-
msg = buf.value.decode("ascii")
257-
if msg[0]=='w':
258-
warnings.warn(msg[1:], RuntimeWarning)
259-
elif msg[0]=='e':
260-
raise RuntimeError(msg[1:])
256+
if self.messages:
257+
while clibrebound.reb_pop_message(self.messages, buf):
258+
msg = buf.value.decode("ascii")
259+
if msg[0]=='w':
260+
warnings.warn(msg[1:], RuntimeWarning)
261+
elif msg[0]=='e':
262+
raise RuntimeError(msg[1:])
261263

262264
# Pickling methods: return Simulationarchive binary
263265
def __reduce__(self):

src/rebound.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,14 @@ void reb_simulation_error(struct reb_simulation* const r, const char* const msg)
112112
reb_message(r, 'e', msg);
113113
}
114114

115-
void reb_simulation_stop(struct reb_simulation* const r){
116-
r->status = REB_STATUS_USER;
117-
}
118-
119-
int reb_simulation_get_next_message(struct reb_simulation* const r, char* const buf){
120-
if (r->messages){
121-
char* w0 = r->messages[0];
115+
int reb_pop_message(char** messages, char* const buf){
116+
if (messages){
117+
char* w0 = messages[0];
122118
if (w0){
123119
for(int i=0;i<reb_messages_max_N-1;i++){
124-
r->messages[i] = r->messages[i+1];
120+
messages[i] = messages[i+1];
125121
}
126-
r->messages[reb_messages_max_N-1] = NULL;
122+
messages[reb_messages_max_N-1] = NULL;
127123
strcpy(buf,w0);
128124
free(w0);
129125
return 1;

src/rebound_internal.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ DLLEXPORT extern const unsigned int reb_favicon_len;
6565
DLLEXPORT extern const int reb_messages_max_length;
6666
DLLEXPORT extern const int reb_messages_max_N;
6767

68-
// Other functions. Mainly used by python.
69-
// TODO: Put in other header files depending on implementation location.
68+
// Free any pointer. Alias for free(). Used by python.
7069
DLLEXPORT void reb_free(void* p);
71-
DLLEXPORT int reb_simulation_get_next_message(struct reb_simulation* const r, char* const buf); // Get the next stored warning message. Used only if save_messages==1. Return value is 0 if no messages are present, 1 otherwise.
72-
DLLEXPORT int reb_check_fp_contract(); // Returns 1 if floating point contraction are enabled. 0 otherwise.
70+
// Get the next stored warning message. Used only if save_messages==1. Return value is 0 if no messages are present, 1 otherwise.
71+
DLLEXPORT int reb_pop_message(char** messages, char* const buf);
72+
// Returns 1 if floating point contraction are enabled. 0 otherwise. For unit testing.
73+
DLLEXPORT int reb_check_fp_contract();
7374

7475

7576
#endif // _REBOUND_INTERNAL_H

src/simulation.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ size_t reb_simulation_struct_size(){
6060
return sizeof(struct reb_simulation);
6161
}
6262

63+
// User triggered stop.
64+
void reb_simulation_stop(struct reb_simulation* const r){
65+
r->status = REB_STATUS_USER;
66+
}
67+
6368
// Workaround for setting a pythong callback function.
6469
void reb_simulation_set_collision_resolve(struct reb_simulation* r, enum REB_COLLISION_RESOLVE_OUTCOME (*resolve) (struct reb_simulation* const r, struct reb_collision c)){
6570
r->collision_resolve = resolve;

0 commit comments

Comments
 (0)