CLASS MANUAL
common.h
Go to the documentation of this file.
1 
3 #include "stdio.h"
4 #include "stdlib.h"
5 #include "math.h"
6 #include "string.h"
7 #include "float.h"
8 #include "svnversion.h"
9 #include <stdarg.h>
10 
11 #ifdef _OPENMP
12 #include "omp.h"
13 #endif
14 
15 #ifndef __COMMON__
16 #define __COMMON__
17 
18 #define _VERSION_ "v3.3.0"
19 
20 /* @cond INCLUDE_WITH_DOXYGEN */
21 
22 #define _TRUE_ 1
23 #define _FALSE_ 0
25 #define _SUCCESS_ 0
26 #define _FAILURE_ 1
28 #define _ERRORMSGSIZE_ 2048
29 typedef char ErrorMsg[_ERRORMSGSIZE_];
31 #define _FILENAMESIZE_ 256
32 typedef char FileName[_FILENAMESIZE_];
33 
34 #define _SUFFIXNAMESIZE_ 4
36 #define _PI_ 3.1415926535897932384626433832795e0
38 #define _PIHALF_ 1.57079632679489661923132169164e0
40 #define _TWOPI_ 6.283185307179586476925286766559e0
42 #define _SQRT2_ 1.41421356237309504880168872421e0
44 #define _SQRT6_ 2.4494897427831780981972840747059e0
46 #define _SQRT_PI_ 1.77245385090551602729816748334e0
48 #define _E_ 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696
50 #define _MAX_IT_ 10000
52 #define _QUADRATURE_MAX_ 250
54 #define _QUADRATURE_MAX_BG_ 800
56 #define _TOLVAR_ 100.
58 #define _HUGE_ 1.e99
59 
60 #define _EPSILON_ 1.e-10
61 
62 #define _OUTPUTPRECISION_ 12
64 #define _COLUMNWIDTH_ 24
66 #define _MAXTITLESTRINGLENGTH_ 8000
68 #define _DELIMITER_ "\t"
70 #ifndef __CLASSDIR__
71 #define __CLASSDIR__ "."
72 #endif
73 
74 #define MIN(a,b) (((a)<(b)) ? (a) : (b) )
75 #define MAX(a,b) (((a)<(b)) ? (b) : (a) )
76 #define SIGN(a) (((a)>0) ? 1. : -1. )
77 #define NRSIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
78 #define index_symmetric_matrix(i1,i2,N) (((i1)<=(i2)) ? ((i2)+N*(i1)-((i1)*((i1)+1))/2) : ((i1)+N*(i2)-((i2)*((i2)+1))/2))
80 /* @endcond */
81 
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86  /* needed because of weird openmp bug on macosx lion... */
87  void class_protect_sprintf(char* dest, char* tpl, ...);
88  void class_protect_fprintf(FILE* dest, char* tpl, ...);
89  void* class_protect_memcpy(void* dest, void* from, size_t sz);
90 
91  /* some general functions */
92  int get_number_of_titles(char * titlestring);
93  int file_exists(const char *fname);
94  int compare_doubles(const void * a,
95  const void * b);
96  int string_begins_with(char* thestring, char beginchar);
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 /* general CLASS macros */
102 
103 //This macro receives additional 'do {' and '} while(0)' to safeguard
104 //in single-line if else clauses without '{' and '}'
105 //Also, careful: Since sprintf(NULL,0,x) returns the size of characters
106 //that are inside of the string x, then the buffer needs to be
107 //actually one character longer to hold also the null character '\0'
108 #define class_sprintf(string, format...) do { \
109  int _buffer_size_sprintf = snprintf(NULL, 0, format); \
110  snprintf(string, _buffer_size_sprintf+1, format); \
111 } while (0)
112 
113 #define class_build_error_string(dest,tmpl,...) { \
114  ErrorMsg FMsg; \
115  class_protect_sprintf(FMsg,tmpl,__VA_ARGS__); \
116  class_protect_sprintf(dest,"%s(L:%d) :%s",__func__,__LINE__,FMsg); \
117 }
118 
119 // Error reporting macros
120 
121 // Call
122 #define class_call_message(err_out,extra,err_mess) \
123  class_build_error_string(err_out,"error in %s;\n=>%s",extra,err_mess);
124 
125 /* macro for calling function and returning error if it failed */
126 #define class_call_except(function, error_message_from_function, error_message_output,list_of_commands) { \
127  if (function == _FAILURE_) { \
128  class_call_message(error_message_output,#function,error_message_from_function); \
129  list_of_commands; \
130  return _FAILURE_; \
131  } \
132 }
133 
134 /* macro for trying to call function */
135 #define class_call_try(function, error_message_from_function, error_message_output,list_of_commands) { \
136  if (function == _FAILURE_) { \
137  class_call_message(error_message_output,#function,error_message_from_function); \
138  list_of_commands; \
139  } \
140 }
141 
142 /* macro for calling function and returning error if it failed */
143 #define class_call(function, error_message_from_function, error_message_output) \
144  class_call_except(function, error_message_from_function,error_message_output,)
145 
146 /* same in parallel region -- UNUSED NOW */
147 /*#define class_call_parallel(function, error_message_from_function, error_message_output) { \
148  if (abort_now == _FALSE_) { \
149  if (function == _FAILURE_) { \
150  class_call_message(error_message_output,#function,error_message_from_function); \
151  abort_now=_TRUE_; \
152  } \
153  } \
154 }*/
155 
156 
157 
158 
159 // Alloc
160 #define class_alloc_message(err_out,extra,sz) \
161  class_build_error_string(err_out,"could not allocate %s with size %d",extra,sz);
162 
163 /* macro for allocating memory and returning error if it failed */
164 #define class_alloc(pointer, size, error_message_output) { \
165  pointer=(__typeof__(pointer))malloc(size); \
166  if (pointer == NULL) { \
167  int size_int; \
168  size_int = size; \
169  class_alloc_message(error_message_output,#pointer, size_int); \
170  return _FAILURE_; \
171  } \
172 }
173 
174 
175 /* same inside parallel structure -- UNUSED NOW
176 #define class_alloc_parallel(pointer, size, error_message_output) { \
177  pointer=NULL; \
178  if (abort_now == _FALSE_) { \
179  pointer=(__typeof__(pointer))malloc(size); \
180  if (pointer == NULL) { \
181  int size_int; \
182  size_int = size; \
183  class_alloc_message(error_message_output,#pointer, size_int); \
184  abort_now=_TRUE_; \
185  } \
186  } \
187 }*/
188 
189 /* macro for allocating memory, initializing it with zeros/ and returning error if it failed */
190 #define class_calloc(pointer, init,size, error_message_output) { \
191  pointer=(__typeof__(pointer))calloc(init,size); \
192  if (pointer == NULL) { \
193  int size_int; \
194  size_int = size; \
195  class_alloc_message(error_message_output,#pointer, size_int); \
196  return _FAILURE_; \
197  } \
198 }
199 
200 /* macro for re-allocating memory, returning error if it failed */
201 #define class_realloc(pointer, size, error_message_output) { \
202  pointer=(__typeof__(pointer))realloc(pointer,size); \
203  if (pointer == NULL) { \
204  int size_int; \
205  size_int = size; \
206  class_alloc_message(error_message_output,#pointer, size_int); \
207  return _FAILURE_; \
208  } \
209 }
210 
211 // Testing
212 
213 #define class_test_message(err_out,extra,args...) { \
214  ErrorMsg Optional_arguments; \
215  class_protect_sprintf(Optional_arguments,args); \
216  class_build_error_string(err_out,"condition (%s) is true; %s",extra,Optional_arguments); \
217 }
218 
219 /* macro for testing condition and returning error if condition is true;
220  args is a variable list of optional arguments, e.g.: args="x=%d",x
221  args cannot be empty, if there is nothing to pass use args="" */
222 #define class_test_except(condition, error_message_output,list_of_commands, args...) { \
223  if (condition) { \
224  class_test_message(error_message_output,#condition, args); \
225  list_of_commands; \
226  return _FAILURE_; \
227  } \
228 }
229 
230 #define class_test(condition, error_message_output, args...) { \
231  if (condition) { \
232  class_test_message(error_message_output,#condition, args); \
233  return _FAILURE_; \
234  } \
235 }
236 
237 /* UNUSED NOW
238 #define class_test_parallel(condition, error_message_output, args...) { \
239  if (abort_now == _FALSE_) { \
240  if (condition) { \
241  class_test_message(error_message_output,#condition, args); \
242  abort_now=_TRUE_; \
243  } \
244  } \
245 }*/
246 
247 /* macro for returning error message;
248  args is a variable list of optional arguments, e.g.: args="x=%d",x
249  args cannot be empty, if there is nothing to pass use args="" */
250 #define class_stop(error_message_output,args...) { \
251  ErrorMsg Optional_arguments; \
252  class_protect_sprintf(Optional_arguments,args); \
253  class_build_error_string(error_message_output,"error; %s",Optional_arguments); \
254  return _FAILURE_; \
255 }
256 
257 // IO
258 /* macro for opening file and returning error if it failed */
259 #define class_open(pointer, filename, mode, error_output) { \
260  pointer=fopen(filename,mode); \
261  if (pointer == NULL) { \
262  class_build_error_string(error_output,"could not open %s with name %s and mode %s",#pointer,filename,#mode); \
263  return _FAILURE_; \
264  } \
265 }
266 
267 /* macro for defining indices (usually one, sometimes a block) */
268 #define class_define_index(index, \
269  condition, \
270  running_index, \
271  number_of_indices) { \
272  if (condition) { \
273  index = running_index; \
274  running_index += number_of_indices; \
275  } \
276  }
277 
278 /* macros for writing formatted output */
279 #define class_fprintf_double(file, \
280  output, \
281  condition){ \
282  if (condition == _TRUE_) \
283  fprintf(file,"%*.*e ",_COLUMNWIDTH_,_OUTPUTPRECISION_,output); \
284  }
285 
286 #define class_fprintf_double_or_default(file, \
287  output, \
288  condition, \
289  defaultvalue){ \
290  if (condition == _TRUE_) \
291  fprintf(file,"%*.*e ",_COLUMNWIDTH_,_OUTPUTPRECISION_,output); \
292  else \
293  fprintf(file,"%*.*e ",_COLUMNWIDTH_,_OUTPUTPRECISION_,defaultvalue); \
294 }
295 
296 #define class_fprintf_int(file, \
297  output, \
298  condition){ \
299  if (condition == _TRUE_) \
300  fprintf(file,"%*d%*s ", \
301  MAX(0,_COLUMNWIDTH_-_OUTPUTPRECISION_-5), \
302  output, _OUTPUTPRECISION_+5," "); \
303  }
304 
305 #define class_fprintf_columntitle(file, \
306  title, \
307  condition, \
308  colnum){ \
309  if (condition == _TRUE_) \
310  fprintf(file,"%*s%2d:%-*s ", \
311  MAX(0,MIN(_COLUMNWIDTH_-_OUTPUTPRECISION_-6-3,_COLUMNWIDTH_-((int) strlen(title))-3)), \
312  "",colnum++,_OUTPUTPRECISION_+6,title); \
313  }
314 
315 #define class_store_columntitle(titlestring, \
316  title, \
317  condition){ \
318  if (condition == _TRUE_){ \
319  strcat(titlestring,title); \
320  strcat(titlestring,_DELIMITER_); \
321  } \
322  }
323 //,_MAXTITLESTRINGLENGTH_-strlen(titlestring)-1);
324 
325 #define class_store_double(storage, \
326  value, \
327  condition, \
328  dataindex){ \
329  if (condition == _TRUE_) \
330  storage[dataindex++] = value; \
331  }
332 
333 #define class_store_double_or_default(storage, \
334  value, \
335  condition, \
336  dataindex, \
337  defaultvalue){ \
338  if (condition == _TRUE_) \
339  storage[dataindex++] = value; \
340  else \
341  storage[dataindex++] = defaultvalue; \
342 }
343 
344 //The name for this macro can be at most 30 characters total
345 #define class_print_species(name,type) \
346 printf("-> %-30s Omega = %-15g , omega = %-15g\n",name,pba->Omega0_##type,pba->Omega0_##type*pba->h*pba->h);
347 
348 //Generic evolver prototype
349 #define EVOLVER_PROTOTYPE \
350  int (*)(double, double *, double *, void *, ErrorMsg), \
351  double, double, double *, int *, \
352  int, void *, double, double, \
353  int (*)(double, void *, double *, ErrorMsg), \
354  double, double *, int, \
355  int (*)(double, double *, double *, int, void *, ErrorMsg), \
356  int (*)(double, double *, double *, void *, ErrorMsg), \
357  ErrorMsg
358 
359 /* Forward-Declare the structs of CLASS */
360 struct background;
361 struct thermodynamics;
362 struct perturbations;
363 struct transfer;
364 struct primordial;
365 struct harmonic;
366 struct fourier;
367 struct lensing;
368 struct distortions;
369 struct output;
370 
377  rk, /* Runge-Kutta integrator */
378  ndf15 /* stiff integrator */
379 };
380 
387 enum pk_def {
392 };
397 enum file_format {class_format,camb_format};
398 
405 struct precision
406 {
413  #define __ALLOCATE_PRECISION_PARAMETER__
414  #include "precisions.h"
415  #undef __ALLOCATE_PRECISION_PARAMETER__
416 
420 
424 
428 
429  ErrorMsg error_message;
432 
433 };
434 
435 
436 #endif
ErrorMsg error_message
Definition: common.h:429
evolver_type
Definition: common.h:376
double smallest_allowed_variation
Definition: common.h:421
pk_def
Definition: common.h:387
@ delta_bc_squared
Definition: common.h:390
@ delta_tot_from_poisson_squared
Definition: common.h:391
@ delta_tot_squared
Definition: common.h:389
@ delta_m_squared
Definition: common.h:388
file_format
Definition: common.h:397
Definition: common.h:406
Definition: distortions.h:35
Definition: fourier.h:33
Definition: harmonic.h:17
Definition: lensing.h:17
Definition: output.h:23
Definition: perturbations.h:98
Definition: primordial.h:79
Definition: background.h:44
Definition: thermodynamics.h:59
Definition: transfer.h:74