Skip to content

Commit 949c689

Browse files
committed
print
1 parent fc0a9ed commit 949c689

File tree

5 files changed

+71
-35
lines changed

5 files changed

+71
-35
lines changed

rebound/simulation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ def process_messages(self):
256256
if self.messages:
257257
while clibrebound.reb_pop_message(self.messages, buf):
258258
msg = buf.value.decode("ascii")
259-
if msg[0]=='w':
259+
if msg[0]=='i':
260+
print(msg[1:])
261+
elif msg[0]=='w':
260262
warnings.warn(msg[1:], RuntimeWarning)
261263
elif msg[0]=='e':
262264
raise RuntimeError(msg[1:])

src/rebound.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,54 +64,50 @@ void reb_exit(const char* const msg){
6464
// This function should also kill all children.
6565
// Not implemented as pid is not easy to get to.
6666
// kill(pid, SIGKILL);
67-
fprintf(stderr,"\n\033[1mFatal error! Exiting now.\033[0m %s\n",msg);
67+
reb_message(NULL, 0, REB_MESSAGE_TYPE_ERROR, msg);
6868
exit(EXIT_FAILURE);
6969
}
7070

71-
void reb_message(struct reb_simulation* const r, char type, const char* const msg){
72-
int save_messages = 0;
73-
if (r != NULL){
74-
save_messages = r->save_messages;
75-
}
76-
if (!save_messages || strlen(msg)>=reb_messages_max_length){
77-
if (type=='w'){
78-
fprintf(stderr,"\n\033[1mWarning!\033[0m %s\n",msg);
79-
}else if (type=='e'){
80-
fprintf(stderr,"\n\033[1mError!\033[0m %s\n",msg);
71+
void reb_message(char*** messages, int save_messages, enum REB_MESSAGE_TYPE type, const char* const msg){
72+
if (!save_messages || messages==NULL){
73+
switch (type){
74+
case REB_MESSAGE_TYPE_INFO:
75+
fprintf(stderr,"\nREBOUND Message! %s\n",msg);
76+
break;
77+
case REB_MESSAGE_TYPE_WARNING:
78+
fprintf(stderr,"\n\033[1mWarning!\033[0m %s\n",msg);
79+
break;
80+
case REB_MESSAGE_TYPE_ERROR:
81+
fprintf(stderr,"\n\033[1mError!\033[0m %s\n",msg);
82+
break;
8183
}
8284
}else{
83-
// TODO: Should be protected by MUTEX
84-
if (r->messages==NULL){
85-
r->messages = calloc(reb_messages_max_N,sizeof(char*));
85+
// Note: not thread safe.
86+
if (*messages==NULL){
87+
*messages = calloc(reb_messages_max_N,sizeof(char*));
8688
}
8789
int n = 0;
8890
for (;n<reb_messages_max_N;n++){
89-
if (r->messages[n]==NULL){
91+
if ((*messages)[n]==NULL){
9092
break;
9193
}
9294
}
9395
if (n==reb_messages_max_N){
94-
free(r->messages[0]);
96+
free((*messages)[0]);
9597
for (int i=0;i<reb_messages_max_N-1;i++){
96-
r->messages[i] = r->messages[i+1];
98+
(*messages)[i] = (*messages)[i+1];
9799
}
98-
r->messages[reb_messages_max_N-1] = NULL;
100+
(*messages)[reb_messages_max_N-1] = NULL;
99101
n= reb_messages_max_N-1;
100102
}
101-
r->messages[n] = malloc(sizeof(char*)*reb_messages_max_length);
102-
r->messages[n][0] = type;
103-
strcpy(r->messages[n]+1, msg);
103+
(*messages)[n] = malloc(sizeof(char*)*reb_messages_max_length);
104+
// First character indicates type for python.
105+
(*messages)[n][0] = (char)type;
106+
strncpy((*messages)[n]+1, msg, reb_messages_max_length-2);
107+
(*messages)[n][reb_messages_max_length-1] = '\0';
104108
}
105109
}
106110

107-
void reb_simulation_warning(struct reb_simulation* const r, const char* const msg){
108-
reb_message(r, 'w', msg);
109-
}
110-
111-
void reb_simulation_error(struct reb_simulation* const r, const char* const msg){
112-
reb_message(r, 'e', msg);
113-
}
114-
115111
int reb_pop_message(char** messages, char* const buf){
116112
if (messages){
117113
char* w0 = messages[0];
@@ -149,10 +145,16 @@ void reb_mpi_init(struct reb_simulation* const r){
149145
reb_communication_mpi_init(r,0,NULL);
150146
// Make sure domain can be decomposed into equal number of root boxes per node.
151147
if ((r->N_root/r->mpi_num)*r->mpi_num != r->N_root){
152-
if (r->mpi_id==0) fprintf(stderr,"ERROR: Number of root boxes (%d) not a multiple of mpi nodes (%d).\n",r->N_root,r->mpi_num);
148+
if (r->mpi_id==0){
149+
char msg[1024];
150+
sprintf(msg, "Number of root boxes (%d) not a multiple of mpi nodes (%d).\n",r->N_root,r->mpi_num);
151+
reb_simulation_error(r,msg);
152+
}
153153
exit(-1);
154154
}
155-
printf("MPI-node: %d. Process id: %d.\n",r->mpi_id, getpid());
155+
char msg[1024];
156+
sprintf(msg,"MPI-node: %d. Process id: %d.\n",r->mpi_id, getpid());
157+
reb_simulation_info(r,msg);
156158
}
157159

158160
void reb_mpi_finalize(struct reb_simulation* const r){

src/rebound.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ DLLEXPORT void reb_simulation_stop_server(struct reb_simulation* r);
657657
DLLEXPORT void reb_exit(const char* const msg);
658658
// Stop current integration in a nice way. Can be called from within heartbeat function.
659659
DLLEXPORT void reb_simulation_stop(struct reb_simulation* const r);
660+
// Print or store an info message, then continue.
661+
DLLEXPORT void reb_simulation_info(struct reb_simulation* const r, const char* const msg);
660662
// Print or store a warning message, then continue.
661663
DLLEXPORT void reb_simulation_warning(struct reb_simulation* const r, const char* const msg);
662664
// Print or store an error message, then continue.

src/rebound_internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ DLLEXPORT extern const int reb_messages_max_N;
6969
DLLEXPORT void reb_free(void* p);
7070
// Get the next stored warning message. Used only if save_messages==1. Return value is 0 if no messages are present, 1 otherwise.
7171
DLLEXPORT int reb_pop_message(char** messages, char* const buf);
72+
// Print or save a message.
73+
enum REB_MESSAGE_TYPE {
74+
REB_MESSAGE_TYPE_INFO = 'i',
75+
REB_MESSAGE_TYPE_ERROR = 'e',
76+
REB_MESSAGE_TYPE_WARNING = 'w',
77+
};
78+
void reb_message(char*** messages, int save_messages, enum REB_MESSAGE_TYPE type, const char* const msg);
7279
// Returns 1 if floating point contraction are enabled. 0 otherwise. For unit testing.
7380
DLLEXPORT int reb_check_fp_contract();
7481

src/simulation.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,28 @@ void reb_simulation_stop(struct reb_simulation* const r){
6565
r->status = REB_STATUS_USER;
6666
}
6767

68-
// Workaround for setting a pythong callback function.
68+
// Print or save warning message. r can be NULL.
69+
void reb_simulation_warning(struct reb_simulation* const r, const char* const msg){
70+
int save_messages = 0;
71+
if (r != NULL && r->save_messages) save_messages = 1;
72+
reb_message(&r->messages, save_messages, REB_MESSAGE_TYPE_WARNING, msg);
73+
}
74+
75+
// Print or save error message. r can be NULL.
76+
void reb_simulation_error(struct reb_simulation* const r, const char* const msg){
77+
int save_messages = 0;
78+
if (r != NULL && r->save_messages) save_messages = 1;
79+
reb_message(&r->messages, save_messages, REB_MESSAGE_TYPE_ERROR, msg);
80+
}
81+
82+
// Print or save info message. r can be NULL.
83+
void reb_simulation_info(struct reb_simulation* const r, const char* const msg){
84+
int save_messages = 0;
85+
if (r != NULL && r->save_messages) save_messages = 1;
86+
reb_message(&r->messages, save_messages, REB_MESSAGE_TYPE_INFO, msg);
87+
}
88+
89+
// Workaround for setting a python callback function.
6990
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)){
7091
r->collision_resolve = resolve;
7192
}
@@ -237,7 +258,7 @@ void reb_simulation_free_pointers(struct reb_simulation* const r){
237258
usleep(100);
238259
}
239260
if (r->display_data->window){ // still running?
240-
printf("Waiting for OpenGL visualization to shut down...\n");
261+
reb_simulation_info(NULL, "Waiting for OpenGL visualization to shut down...");
241262
while(r->display_data->window){
242263
usleep(100);
243264
}
@@ -750,7 +771,9 @@ void reb_simulation_init(struct reb_simulation* r){
750771
r->N_tree_essential_recv_max = 0;
751772
#endif // MPI
752773
#ifdef OPENMP
753-
printf("Using OpenMP with %d threads per node.\n",omp_get_max_threads());
774+
char msg[1024];
775+
sprintf(msg, "Using OpenMP with %d threads per node.\n", omp_get_max_threads());
776+
reb_simulation_info(r, msg);
754777
#endif // OPENMP
755778
}
756779

0 commit comments

Comments
 (0)