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.2.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 /* needed because of weird openmp bug on macosx lion... */
83 
84 void class_protect_sprintf(char* dest, char* tpl,...);
85 void class_protect_fprintf(FILE* dest, char* tpl,...);
86 void* class_protect_memcpy(void* dest, void* from, size_t sz);
87 
88 /* some general functions */
89 
90 int get_number_of_titles(char * titlestring);
91 int file_exists(const char *fname);
92 int compare_doubles(const void * a,
93  const void * b);
94 int string_begins_with(char* thestring, char beginchar);
95 
96 /* general CLASS macros */
97 
98 #define class_build_error_string(dest,tmpl,...) { \
99  ErrorMsg FMsg; \
100  class_protect_sprintf(FMsg,tmpl,__VA_ARGS__); \
101  class_protect_sprintf(dest,"%s(L:%d) :%s",__func__,__LINE__,FMsg); \
102 }
103 
104 // Error reporting macros
105 
106 // Call
107 #define class_call_message(err_out,extra,err_mess) \
108  class_build_error_string(err_out,"error in %s;\n=>%s",extra,err_mess);
109 
110 /* macro for calling function and returning error if it failed */
111 #define class_call_except(function, error_message_from_function, error_message_output,list_of_commands) { \
112  if (function == _FAILURE_) { \
113  class_call_message(error_message_output,#function,error_message_from_function); \
114  list_of_commands; \
115  return _FAILURE_; \
116  } \
117 }
118 
119 /* macro for trying to call function */
120 #define class_call_try(function, error_message_from_function, error_message_output,list_of_commands) { \
121  if (function == _FAILURE_) { \
122  class_call_message(error_message_output,#function,error_message_from_function); \
123  list_of_commands; \
124  } \
125 }
126 
127 /* macro for calling function and returning error if it failed */
128 #define class_call(function, error_message_from_function, error_message_output) \
129  class_call_except(function, error_message_from_function,error_message_output,)
130 
131 /* same in parallel region */
132 #define class_call_parallel(function, error_message_from_function, error_message_output) { \
133  if (abort == _FALSE_) { \
134  if (function == _FAILURE_) { \
135  class_call_message(error_message_output,#function,error_message_from_function); \
136  abort=_TRUE_; \
137  } \
138  } \
139 }
140 
141 
142 
143 
144 // Alloc
145 #define class_alloc_message(err_out,extra,sz) \
146  class_build_error_string(err_out,"could not allocate %s with size %d",extra,sz);
147 
148 /* macro for allocating memory and returning error if it failed */
149 #define class_alloc(pointer, size, error_message_output) { \
150  pointer=malloc(size); \
151  if (pointer == NULL) { \
152  int size_int; \
153  size_int = size; \
154  class_alloc_message(error_message_output,#pointer, size_int); \
155  return _FAILURE_; \
156  } \
157 }
158 
159 /* same inside parallel structure */
160 #define class_alloc_parallel(pointer, size, error_message_output) { \
161  pointer=NULL; \
162  if (abort == _FALSE_) { \
163  pointer=malloc(size); \
164  if (pointer == NULL) { \
165  int size_int; \
166  size_int = size; \
167  class_alloc_message(error_message_output,#pointer, size_int); \
168  abort=_TRUE_; \
169  } \
170  } \
171 }
172 
173 /* macro for allocating memory, initializing it with zeros/ and returning error if it failed */
174 #define class_calloc(pointer, init,size, error_message_output) { \
175  pointer=calloc(init,size); \
176  if (pointer == NULL) { \
177  int size_int; \
178  size_int = size; \
179  class_alloc_message(error_message_output,#pointer, size_int); \
180  return _FAILURE_; \
181  } \
182 }
183 
184 /* macro for re-allocating memory, returning error if it failed */
185 #define class_realloc(pointer, newname, size, error_message_output) { \
186  pointer=realloc(newname,size); \
187  if (pointer == NULL) { \
188  int size_int; \
189  size_int = size; \
190  class_alloc_message(error_message_output,#pointer, size_int); \
191  return _FAILURE_; \
192  } \
193 }
194 
195 // Testing
196 
197 #define class_test_message(err_out,extra,args...) { \
198  ErrorMsg Optional_arguments; \
199  class_protect_sprintf(Optional_arguments,args); \
200  class_build_error_string(err_out,"condition (%s) is true; %s",extra,Optional_arguments); \
201 }
202 
203 /* macro for testing condition and returning error if condition is true;
204  args is a variable list of optional arguments, e.g.: args="x=%d",x
205  args cannot be empty, if there is nothing to pass use args="" */
206 #define class_test_except(condition, error_message_output,list_of_commands, args...) { \
207  if (condition) { \
208  class_test_message(error_message_output,#condition, args); \
209  list_of_commands; \
210  return _FAILURE_; \
211  } \
212 }
213 
214 #define class_test(condition, error_message_output, args...) { \
215  if (condition) { \
216  class_test_message(error_message_output,#condition, args); \
217  return _FAILURE_; \
218  } \
219 }
220 
221 #define class_test_parallel(condition, error_message_output, args...) { \
222  if (abort == _FALSE_) { \
223  if (condition) { \
224  class_test_message(error_message_output,#condition, args); \
225  abort=_TRUE_; \
226  } \
227  } \
228 }
229 
230 /* macro for returning error message;
231  args is a variable list of optional arguments, e.g.: args="x=%d",x
232  args cannot be empty, if there is nothing to pass use args="" */
233 #define class_stop(error_message_output,args...) { \
234  ErrorMsg Optional_arguments; \
235  class_protect_sprintf(Optional_arguments,args); \
236  class_build_error_string(error_message_output,"error; %s",Optional_arguments); \
237  return _FAILURE_; \
238 }
239 
240 // IO
241 /* macro for opening file and returning error if it failed */
242 #define class_open(pointer, filename, mode, error_output) { \
243  pointer=fopen(filename,mode); \
244  if (pointer == NULL) { \
245  class_build_error_string(error_output,"could not open %s with name %s and mode %s",#pointer,filename,#mode); \
246  return _FAILURE_; \
247  } \
248 }
249 
250 /* macro for defining indices (usually one, sometimes a block) */
251 #define class_define_index(index, \
252  condition, \
253  running_index, \
254  number_of_indices) { \
255  if (condition) { \
256  index = running_index; \
257  running_index += number_of_indices; \
258  } \
259  }
260 
261 /* macros for writing formatted output */
262 #define class_fprintf_double(file, \
263  output, \
264  condition){ \
265  if (condition == _TRUE_) \
266  fprintf(file,"%*.*e ",_COLUMNWIDTH_,_OUTPUTPRECISION_,output); \
267  }
268 
269 #define class_fprintf_double_or_default(file, \
270  output, \
271  condition, \
272  defaultvalue){ \
273  if (condition == _TRUE_) \
274  fprintf(file,"%*.*e ",_COLUMNWIDTH_,_OUTPUTPRECISION_,output); \
275  else \
276  fprintf(file,"%*.*e ",_COLUMNWIDTH_,_OUTPUTPRECISION_,defaultvalue); \
277 }
278 
279 #define class_fprintf_int(file, \
280  output, \
281  condition){ \
282  if (condition == _TRUE_) \
283  fprintf(file,"%*d%*s ", \
284  MAX(0,_COLUMNWIDTH_-_OUTPUTPRECISION_-5), \
285  output, _OUTPUTPRECISION_+5," "); \
286  }
287 
288 #define class_fprintf_columntitle(file, \
289  title, \
290  condition, \
291  colnum){ \
292  if (condition == _TRUE_) \
293  fprintf(file,"%*s%2d:%-*s ", \
294  MAX(0,MIN(_COLUMNWIDTH_-_OUTPUTPRECISION_-6-3,_COLUMNWIDTH_-((int) strlen(title))-3)), \
295  "",colnum++,_OUTPUTPRECISION_+6,title); \
296  }
297 
298 #define class_store_columntitle(titlestring, \
299  title, \
300  condition){ \
301  if (condition == _TRUE_){ \
302  strcat(titlestring,title); \
303  strcat(titlestring,_DELIMITER_); \
304  } \
305  }
306 //,_MAXTITLESTRINGLENGTH_-strlen(titlestring)-1);
307 
308 #define class_store_double(storage, \
309  value, \
310  condition, \
311  dataindex){ \
312  if (condition == _TRUE_) \
313  storage[dataindex++] = value; \
314  }
315 
316 #define class_store_double_or_default(storage, \
317  value, \
318  condition, \
319  dataindex, \
320  defaultvalue){ \
321  if (condition == _TRUE_) \
322  storage[dataindex++] = value; \
323  else \
324  storage[dataindex++] = defaultvalue; \
325 }
326 
327 //The name for this macro can be at most 30 characters total
328 #define class_print_species(name,type) \
329 printf("-> %-30s Omega = %-15g , omega = %-15g\n",name,pba->Omega0_##type,pba->Omega0_##type*pba->h*pba->h);
330 
331 /* Forward-Declare the structs of CLASS */
332 struct background;
333 struct thermodynamics;
334 struct perturbations;
335 struct transfer;
336 struct primordial;
337 struct harmonic;
338 struct fourier;
339 struct lensing;
340 struct distortions;
341 struct output;
342 
349  rk, /* Runge-Kutta integrator */
350  ndf15 /* stiff integrator */
351 };
352 
359 enum pk_def {
364 };
369 enum file_format {class_format,camb_format};
370 
377 struct precision
378 {
385  #define __ALLOCATE_PRECISION_PARAMETER__
386  #include "precisions.h"
387  #undef __ALLOCATE_PRECISION_PARAMETER__
388 
392 
396 
400 
401  ErrorMsg error_message;
404 
405 };
406 
407 #endif
Definition: background.h:48
ErrorMsg error_message
Definition: common.h:401
evolver_type
Definition: common.h:348
double smallest_allowed_variation
Definition: common.h:393
pk_def
Definition: common.h:359
@ delta_bc_squared
Definition: common.h:362
@ delta_tot_from_poisson_squared
Definition: common.h:363
@ delta_tot_squared
Definition: common.h:361
@ delta_m_squared
Definition: common.h:360
file_format
Definition: common.h:369
Definition: common.h:378
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: thermodynamics.h:59
Definition: transfer.h:76