Debian Codegraph @

C++/Include/utilitaires.h

raw symbols SCIP
1 /*
2 * Prototypes of various utilities for Lorene
3 *
4 */
5
6 /*
7 * Copyright (c) 1999-2001 Eric Gourgoulhon
8 *
9 * This file is part of LORENE.
10 *
11 * LORENE is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * LORENE is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with LORENE; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28 #ifndef __UTILITAIRES_H_
29 #define __UTILITAIRES_H_
30
31
32 /*
33 * $Id: utilitaires.h,v 1.16 2015/01/09 15:28:52 j_novak Exp $
34 * $Log: utilitaires.h,v $
35 * Revision 1.16 2015/01/09 15:28:52 j_novak
36 * New integration function for general non-equally-spaced grids.
37 *
38 * Revision 1.15 2014/10/13 08:52:37 j_novak
39 * Lorene classes and functions now belong to the namespace Lorene.
40 *
41 * Revision 1.14 2014/10/06 15:09:40 j_novak
42 * Modified #include directives to use c++ syntax.
43 *
44 * Revision 1.13 2014/07/04 12:09:06 j_novak
45 * New argument in zerosec(): a boolean (false by default) for aborting if the number of iteration is greater than the max.
46 *
47 * Revision 1.12 2014/04/25 10:43:50 j_novak
48 * The member 'name' is of type string now. Correction of a few const-related issues.
49 *
50 * Revision 1.11 2008/08/19 06:41:59 j_novak
51 * Minor modifications to avoid warnings with gcc 4.3. Most of them concern
52 * cast-type operations, and constant strings that must be defined as const char*
53 *
54 * Revision 1.10 2004/09/01 09:47:55 r_prix
55 * fixed/improved string-reading with read_variable(): allocates returned string
56 *
57 * Revision 1.9 2004/03/22 13:12:44 j_novak
58 * Modification of comments to use doxygen instead of doc++
59 *
60 * Revision 1.8 2003/12/17 23:12:30 r_prix
61 * replaced use of C++ <string> by standard ANSI char* to be backwards compatible
62 * with broken compilers like MIPSpro Compiler 7.2 on SGI Origin200. ;-)
63 *
64 * Revision 1.7 2003/12/05 15:05:53 r_prix
65 * added read_variable() for (C++-type) strings.
66 *
67 * Revision 1.6 2003/12/04 12:33:21 r_prix
68 * added prototypes and documentation for variable-reading functions
69 * (read_variable, load_file, load_file_buffered)
70 *
71 * Revision 1.5 2002/05/02 15:16:22 j_novak
72 * Added functions for more general bi-fluid EOS
73 *
74 * Revision 1.4 2002/04/16 08:06:44 j_novak
75 * Addition of zerosec_borne
76 *
77 * Revision 1.3 2002/04/11 09:19:46 j_novak
78 * Back to old version of zerosec
79 *
80 * Revision 1.2 2001/12/04 21:24:33 e_gourgoulhon
81 *
82 * New functions fwrite_be and fread_be for writing/reading in a
83 * binary file according to the big endian convention.
84 *
85 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
86 * LORENE
87 *
88 * Revision 1.6 2001/09/14 14:23:53 eric
89 * Ajout de la fonction zero_list.
90 *
91 * Revision 1.5 2001/05/29 16:11:21 eric
92 * Modif commentaires (mise en conformite Doc++ 3.4.7).
93 *
94 * Revision 1.4 1999/12/24 12:59:54 eric
95 * Ajout de la routine zero_premier.
96 *
97 * Revision 1.3 1999/12/15 15:39:42 eric
98 * *** empty log message ***
99 *
100 * Revision 1.2 1999/12/15 15:17:03 eric
101 * *** empty log message ***
102 *
103 * Revision 1.1 1999/12/15 09:41:47 eric
104 * Initial revision
105 *
106 *
107 * $Header: /cvsroot/Lorene/C++/Include/utilitaires.h,v 1.16 2015/01/09 15:28:52 j_novak Exp $
108 *
109 */
110
111 #include "stdio.h"
112 #include <cstring>
113
114 namespace Lorene {
115 class Param ;
116 class Tbl ;
117
118 /**
119 * \defgroup misc Miscellaneous
120 * \ingroup (util)
121 * @{
122 */
123
124
125 /** Setting a stop point in a code.
126 *
127 * Stops the execution of a code, until the 'Enter' case is hit.
128 * @param a [input] stops the run if, and only if, a=0.
129 * Default value : 0.
130 *
131 *
132 */
133 void arrete(int a = 0) ;
134
135 /** Locates the sub-interval containing the first zero of a function in
136 * a given interval.
137 *
138 * @param (*f)(double, const Param\&) [input] Function the zero of which is
139 * to be searched:
140 * f(x0, par) = 0 , where par are the parameters of the
141 * function f, stored in an object of the Lorene class
142 * \c Param .
143 * @param a [input] Lower bound of the search interval [a, b]
144 * @param b [input] Higher bound of the search interval [a, b]
145 * @param n [input] Number of subdivisions of the interval [a, b]
146 * @param a0 [output] Lower bound of the first (i.e. closest to a) interval
147 * [a0, b0] which contains a zero of f
148 * @param b0 [output] Higher bound of the first (i.e. closest to a) interval
149 * [a0, b0] which contains a zero of f
150 * @return true if the interval [a0, b0] containing a zero of f has been
151 * found, false otherwise
152 */
153 bool zero_premier(double (*f)(double, const Param&), const Param& par,
154 double a, double b, int n, double& a0, double& b0) ;
155
156
157
158 /** Finding the zero a function.
159 *
160 * This routine locates a zero by means of the secant method.
161 *
162 * @param (*f)(double, const Param\&) [input] Function the zero of which is
163 * to be searched: the routine computes x0 in a given
164 * interval [a, b] such that
165 * f(x0, par) = 0 , where par are the parameters of the
166 * function f, stored in an object of the Lorene class
167 * \c Param .
168 * @param par [input] Parameters of the function f.
169 * @param a [input] Lower bound of the search interval [a, b]
170 * @param b [input] Higher bound of the search interval [a, b]
171 * @param precis [input] Required precision in the determination of x0 :
172 * the returned solution will be x0 +/- precis
173 * @param nitermax [input] Maximum number of iterations in the secant
174 * method to compute x0.
175 * @param niter [output] Number of iterations effectively used in computing x0
176 * @param abort [input] Should the function abort if the maximal number of
177 * iterations has been reached?
178 * @return x0 (zero of function f)
179 *
180 */
181 double zerosec( double (*f)(double, const Param&), const Param& par,
182 double a, double b, double precis, int nitermax,
183 int& niter, bool abort=true) ;
184
185 /** Finding the zero a function on a bounded domain.
186 *
187 * Same as \c zerosec but insures that all calls to the function f
188 * are within [x1,x2]. Namely, it requires that f(x1)*f(x2)<0.
189 *
190 * @param (*f)(double, const Param\&) [input] Function the zero of which is
191 * to be searched: the routine computes x0 in a given
192 * interval [a, b] such that
193 * f(x0, par) = 0 , where par are the parameters of the
194 * function f, stored in an object of the Lorene class
195 * \c Param .
196 * @param par [input] Parameters of the function f.
197 * @param a [input] Lower bound of the search interval [a, b]
198 * @param b [input] Higher bound of the search interval [a, b]
199 * @param precis [input] Required precision in the determination of x0 :
200 * the returned solution will be x0 +/- precis
201 * @param nitermax [input] Maximum number of iterations in the secant
202 * method to compute x0.
203 * @param niter [output] Number of iterations effectively used in computing x0
204 * @return x0 (zero of function f)
205 *
206 */
207 double zerosec_b( double (*f)(double, const Param&),const Param& par,
208 double a, double b, double precis, int nitermax,
209 int& niter) ;
210
211 /** Locates approximatively all the zeros of a function in a given interval.
212 * The N zeros are located in N intervals [az(i), bz(i)] with
213 * \f$0\leq i \leq N-1\f$.
214 *
215 * @param (*f)(double, const Param\&) [input] Function the zeros of which are
216 * to be located: a zero x0 is defined by
217 * f(x0, par) = 0 , where par are the parameters of the
218 * function f, stored in an object of the Lorene class
219 * \c Param .
220 * @param par [input] Parameters of the function f.
221 * @param xmin [input] Lower bound of the search interval
222 * @param xmax [input] Higher bound of the search interval
223 * @param nsub [input] Number of subdivision of the interval [xmin, xmax]
224 * to locate the zeros
225 * @param az [output] 1-D array (Lorene \c Tbl ) contain the lower bounds
226 * of the intervals containing a zero. This \c Tbl \
227 * is allocated by the routine via a \c new Tbl \
228 * command (hence the pointer type).
229 * @param bz [output] 1-D array (Lorene \c Tbl ) contain the higher bounds
230 * of the intervals containing a zero. This \c Tbl \
231 * is allocated by the routine via a \c new Tbl \
232 * command (hence the pointer type).
233 *
234 */
235 void zero_list( double (*f)(double, const Param&), const Param& par,
236 double xmin, double xmax, int nsub,
237 Tbl*& az, Tbl*& bz ) ;
238
239 /** Integrates a function defined on an unequally-spaced grid, approximating
240 * it by piece parabolae.
241 *
242 * This function performs the numerical integration of a function given by its
243 * values on a unequally-spaced grid, by means of local parabolic approximation.
244 * The resulting primitive is set to 0 on the lower end of the integration
245 * interval.
246 * @param xx [input] \c Tbl containing the grid abscissas
247 * @param ff [input] \c Tbl containing the values of the function to be
248 * integrated, on the grid points
249 * @return a \c Tbl with the values of the primitive of \c ff at the grid
250 * points, such that it is zero at the first grid point.
251 */
252 Tbl integ1D(const Tbl& xx, const Tbl& ff) ;
253
254 /** Writes integer(s) into a binary file according to the
255 * big endian convention.
256 *
257 * This function has the same prototype and return value than
258 * the \c fwrite function of the \c stdio C library.
259 * The difference is that it ensures that the binary file is
260 * written in the big endian format, whatever the system is
261 * using little endian or big endian.
262 * @param aa [input] integer array to be written (in case of one
263 * element, address of this integer)
264 * @param size [input] number of bytes of one \c int (must
265 * be 4)
266 * @param nb [input] number of elements in the array \c aa
267 * @param fich [input] binary file (must have been
268 * open by \c fopen )
269 * @return number of integers effectively written in the file
270 */
271 int fwrite_be(const int* aa, int size, int nb, FILE* fich) ;
272
273 /** Writes double precision number(s) into a binary file according to the
274 * big endian convention.
275 *
276 * This function has the same prototype and return value than
277 * the \c fwrite function of the \c stdio C library.
278 * The difference is that it ensures that the binary file is
279 * written in the big endian format, whatever the system is
280 * using little endian or big endian.
281 * @param aa [input] array of \c double to be written (in case of one
282 * element, address of this \c double )
283 * @param size [input] number of bytes of one \c double (must
284 * be 8)
285 * @param nb [input] number of elements in the array \c aa
286 * @param fich [input] binary file (must have been
287 * open by \c fopen )
288 * @return number of \c double effectively written in the file
289 */
290 int fwrite_be(const double* aa, int size, int nb, FILE* fich) ;
291
292 /** Reads integer(s) from a binary file according to the
293 * big endian convention.
294 *
295 * This function has the same prototype and return value than
296 * the \c fread function of the \c stdio C library.
297 * The difference is that it assumes that the binary file is
298 * written in the big endian format, whatever the system is
299 * using little endian or big endian.
300 * @param aa [output] integer array to be read (in case of one
301 * element, address of this integer)
302 * @param size [input] number of bytes of one \c int (must
303 * be 4)
304 * @param nb [input] number of elements in the array \c aa
305 * @param fich [input] binary file (must have been
306 * open by \c fopen )
307 * @return number of integers effectively read in the file
308 */
309 int fread_be(int* aa, int size, int nb, FILE* fich) ;
310
311 /** Reads double precision number(s) from a binary file according to the
312 * big endian convention.
313 *
314 * This function has the same prototype and return value than
315 * the \c fread function of the \c stdio C library.
316 * The difference is that it assumes that the binary file is
317 * written in the big endian format, whatever the system is
318 * using little endian or big endian.
319 * @param aa [output] array of \c double to be read (in case of one
320 * element, address of this \c double )
321 * @param size [input] number of bytes of one \c double (must
322 * be 8)
323 * @param nb [input] number of elements in the array \c aa
324 * @param fich [input] binary file (must have been
325 * open by \c fopen )
326 * @return number of \c double effectively read in the file
327 */
328 int fread_be(double* aa, int size, int nb, FILE* fich) ;
329
330
331 /** Read file into memory and returns pointer to data.
332 * @return: pointer to allocated memory or NULL on error.\\
333 *
334 * NOTE: don't forget to free the memory after use !
335 *
336 */
337 char *load_file(char *fname);
338
339 /** Returns pointer to data from a file using a buffer.
340 * This function only reads from disk if the file has not been buffered yet.
341 * If a new file is read, the buffer is free'ed and the new data allocated.
342 * @param fname [input] name of file to read in. You can use NULL for previous file.
343 * @return : pointer to buffered data or NULL on error.\\
344 *
345 * NOTE: do _NEVER_ free the (buffer-)data pointer, or the next call will crash!!
346 */
347 char *load_file_buffered(char *fname);
348
349 /** Reads a variable from file.
350 * Variable definitions can be of the type
351 * "variable = value", all other lines are ignored as comments.
352 * (alternatively, you can use ":" or whitespace instead of "=")
353 *
354 * NOTE: the variable-definition has to be at the beginning of a
355 * line (modulo whitespace), or it will be considered a comment!
356 *
357 * @param fname [input] Name of config-file to read from. Use NULL to use buffered file.
358 * @param var_name [input] Variable-name to read from config-file
359 * @param fmt [input] C-style format string for reading (see \c sscanf ).
360 * @param varp [output] Pointer to C-variable to read value into (has to be big enough!!)
361 *
362 * @return 0 on success, -1 on error. \\
363 *
364 * NOTE: rather use one of the type-specific overloaded functions below whenever possible
365 * (safer due to type-checking)
366 */
367 int read_variable(const char *fname, const char *var_name, char *fmt, void *varp);
368
369 /// Read an integer-variable from file (cf \c read_variable(char *, char *, char *, void *) ).
370 int read_variable(const char *fname, const char *var_name, int &var);
371 /// Read a bool variable from file (cf \c read_variable(char *, char *, char *, void *) ).
372 int read_variable(const char *fname, const char *var_name, bool &var);
373 /// Read a double variable from file (cf \c read_variable(char *, char *, char *, void *) ).
374 int read_variable(const char *fname, const char *var_name, double &var);
375 /// Read a (ANSI C) string variable from file.
376 int read_variable (const char *fname, const char *var_name, char **str);
377
378 /// 'Improved' malloc that sets memory to 0 and also auto-terminates on error.
379 void *MyMalloc (long bytes);
380
381 /// A portable routine to determine the length of a file
382 int FS_filelength (FILE *f);
383
384 /// Helpful function to say something is not implemented yet
385 void c_est_pas_fait(const char * ) ;
386
387 /** @} */
388
389 }
390 #endif