Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_error.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <math.h>
5 
6 #include <gsl/gsl_errno.h>
7 
8 #include "ccl.h"
9 
10 // Error handling policy: whether to exit on error (C default) or continue
11 // (Python or other binding default)
13 
14 // Debug mode policy: whether to print error messages as they are raised. This
15 // is useful for the Python wrapper, which normally allows errors to be
16 // overwritten by the C code until control returns to Python. If debug mode is
17 // switched on, the errors are always printed by the C code when they occur.
18 // Setting the debug mode to warning allows ccl_raise_warning to print warnings
19 // but keeps the behavior of ccl_raise_exception as if debug mode is set to off.
21 
22 // Set error policy
24 {
25  _ccl_error_policy = error_policy;
26 }
27 
28 // Set debug mode policy
30 {
31  _ccl_debug_mode_policy = debug_policy;
32 }
33 
34 // Convenience function to raise exceptions in an appropriate way
35 void ccl_raise_exception(int err, const char* msg, ...)
36 {
37  char message[256];
38 
39  va_list va;
40  va_start(va, msg);
41  vsnprintf(message, 250, msg, va);
42  va_end(va);
43 
44  // Print error message and exit if fatal errors are enabled
45  if ((_ccl_error_policy == CCL_ERROR_POLICY_EXIT) && (err)) {
46  fprintf(stderr, "ERROR %d: %s\n", err, message);
47  exit(1);
48  }
49  // Print error message and exit if debug output is enabled
50  else if ((_ccl_debug_mode_policy == CCL_DEBUG_MODE_ON) && (err)){
51  fprintf(stderr, "ERROR %d: %s\n", err, message);
52  }
53 }
54 
55 // Convenience function to handle warnings
56 void ccl_raise_warning(int err, const char* msg, ...)
57 {
58  char message[256];
59 
60  va_list va;
61  va_start(va, msg);
62  vsnprintf(message, 250, msg, va);
63  va_end(va);
64 
65  // For now just print warning to stderr if debug is enabled.
66  // TODO: Implement some kind of error stack that can be passed on to, e.g.,
67  // the python binding.
70  fprintf(stderr, "WARNING: %s\n", message);
71  }
72 }
73 
74 // Convenience function to handle warnings
75 void ccl_raise_gsl_warning(int gslstatus, const char* msg, ...)
76 {
77  char message[256];
78 
79  va_list va;
80  va_start(va, msg);
81  vsnprintf(message, 250, msg, va);
82  va_end(va);
83 
84  ccl_raise_warning(gslstatus, "%s GSL error: %s", message, gsl_strerror(gslstatus));
85  return;
86 }
87 
88 void ccl_check_status(ccl_cosmology *cosmo, int * status)
89 {
90 
91  switch (*status) {
92  case 0: // all good, nothing to do
93  return;
94  case CCL_ERROR_LINSPACE: // spacing allocation error, always terminate
95  ccl_raise_exception(*status, cosmo->status_message);
96  case CCL_ERROR_SPLINE: // spline allocation error, always terminate
97  ccl_raise_exception(*status, cosmo->status_message);
98  case CCL_ERROR_COMPUTECHI: // compute_chi error //RH
99  ccl_raise_exception(*status, cosmo->status_message);
100  case CCL_ERROR_HMF_INTERP: // terminate if hmf definition not supported
101  ccl_raise_exception(*status, cosmo->status_message);
102  case CCL_ERROR_NU_INT: // error in getting the neutrino integral spline: exit. No status_message in cosmo because can't pass cosmology to the function.
103  ccl_raise_exception(*status, "Error, in ccl_neutrinos.c. ccl_calculate_nu_phasespace_spline(): Error in setting neutrino phasespace spline.");
104  case CCL_ERROR_NU_SOLVE: // error in converting Omeganuh2-> Mnu: exit. No status_message in cosmo because can't pass cosmology to the function.
105  ccl_raise_exception(*status, "Error, in ccl_neutrinos.c. Omeganuh2_to_Mnu(): Root finding did not converge.");
106  // TODO: Implement softer error handling, e.g. for integral convergence here
107  default:
108  ccl_raise_exception(*status, cosmo->status_message);
109  }
110 }
111 
112 /* ------- ROUTINE: ccl_check_status_nocosmo ------
113  INPUTS: pointer to a status integer
114  TASK: Perform a check on status for the case where it is not possible to have a cosmology object where the status check is required.
115 */
116 void ccl_check_status_nocosmo(int * status)
117 {
118  switch (*status) {
119  case 0: // Nothing to do
120  return;
121  case CCL_ERROR_LINSPACE:
122  // Spacing allocation error, always terminate
123  ccl_raise_exception(*status, "CCL_ERROR_LINSPACE: Spacing allocation error.");
124  case CCL_ERROR_SPLINE:
125  // Spline allocation error, always terminate
126  ccl_raise_exception(*status, "CCL_ERROR_SPLINE: Spline allocation error.");
128  // Compute_chi error
129  ccl_raise_exception(*status,
130  "CCL_ERROR_COMPUTECHI: Comoving distance chi computation failed.");
132  // Terminate if hmf definition not supported
133  ccl_raise_exception(*status,
134  "CCL_ERROR_HMF_INTERP: Halo mass function definition not supported.");
135  case CCL_ERROR_NU_INT:
136  // Error in getting the neutrino integral spline: exit. No status_message
137  // in cosmo because can't pass cosmology to the function.
138  ccl_raise_exception(*status,
139  "CCL_ERROR_NU_INT: Error getting the neutrino phase-space integral spline.");
140  case CCL_ERROR_NU_SOLVE:
141  // Error in converting Omeganuh2-> Mnu: exit. No status_message in cosmo
142  // because can't pass cosmology to the function.
143  ccl_raise_exception(*status,
144  "CCL_ERROR_NU_SOLVE: Error converting Omeganuh2 -> Mnu.");
146  // Error in the sum of mnu or Omeganu passed for the hierarchy requested.
147  ccl_raise_exception(*status,
148  "CCL_ERROR_MNU_UNPHYSICAL: Sum of neutrinos masses for this Omeganu value is incompatible with the requested mass hierarchy.");
150  ccl_raise_exception(*status,
151  "CCL_ERROR_NOT_IMPLEMENTED: the type of m_nu specified is not supported.");
152  default:
153  ccl_raise_exception(*status,
154  "Unrecognized error code (see gsl_errno.h for error codes 1-32).");
155  }
156 }
void ccl_raise_exception(int err, const char *msg,...)
Definition: ccl_error.c:35
void ccl_raise_gsl_warning(int gslstatus, const char *msg,...)
Definition: ccl_error.c:75
void ccl_check_status(ccl_cosmology *cosmo, int *status)
Definition: ccl_error.c:88
#define CCL_ERROR_NOT_IMPLEMENTED
Definition: ccl_error.h:25
#define CCL_ERROR_NU_SOLVE
Definition: ccl_error.h:24
CCLErrorPolicy
Definition: ccl_error.h:41
void ccl_check_status_nocosmo(int *status)
Definition: ccl_error.c:116
#define CCL_ERROR_HMF_INTERP
Definition: ccl_error.h:20
void ccl_set_error_policy(CCLErrorPolicy error_policy)
Definition: ccl_error.c:23
#define CCL_ERROR_NU_INT
Definition: ccl_error.h:22
#define CCL_ERROR_SPLINE
Definition: ccl_error.h:13
static CCLErrorPolicy _ccl_error_policy
Definition: ccl_error.c:12
#define CCL_ERROR_LINSPACE
Definition: ccl_error.h:11
#define CCL_ERROR_MNU_UNPHYSICAL
Definition: ccl_error.h:26
#define CCL_ERROR_COMPUTECHI
Definition: ccl_error.h:18
void ccl_raise_warning(int err, const char *msg,...)
Definition: ccl_error.c:56
CCLDebugModePolicy
Definition: ccl_error.h:46
static CCLDebugModePolicy _ccl_debug_mode_policy
Definition: ccl_error.c:20
char status_message[500]
Definition: ccl_core.h:136
void ccl_set_debug_policy(CCLDebugModePolicy debug_policy)
Definition: ccl_error.c:29