Geant4  10.02
nf_utilities.cc
Go to the documentation of this file.
1 /*
2 # <<BEGIN-copyright>>
3 # <<END-copyright>>
4 */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <cmath>
9 
10 #include "nf_utilities.h"
11 
12 #ifdef WIN32
13 #include <float.h>
14 #define isnan(a) _isnan(a)
15 /*#define INFINITY (DBL_MAX+DBL_MAX)*/
16 /*#define NAN (INFINITY-INFINITY)*/
17 #endif
18 
19 #if defined __cplusplus
20 namespace GIDI {
21 using namespace GIDI;
22 #endif
23 
24 static const char Okay_message[] = "all is okay";
25 static const char mallocError_message[] = "could not allocate memory";
26 static const char insufficientMemory_message[] = "user's memory is too small to hanlde data";
27 static const char badIndex_message[] = "bad index";
28 static const char XNotAscending_message[] = "x values are not ascending";
29 static const char badIndexForX_message[] = "index not correct for x value";
30 static const char XOutsideDomain_message[] = "x value not in domain";
31 static const char invalidInterpolation_message[] = "bad x,y values for interpolation";
32 static const char badSelf_message[] = "source object has bad status value";
33 static const char divByZero_message[] = "division by zero";
34 static const char unsupportedInterpolation_message[] = "unsupported interpolation";
35 static const char unsupportedInterpolationConversion_message[] = "unsupported interpolation conversion";
36 static const char empty_message[] = "empty instance";
37 static const char tooFewPoints_message[] = "too few points in instance";
38 static const char notMutualDomian_message[] = "domains are not mutual";
39 static const char unknownStatus_message[] = "unknown (i.e., invalid) status value";
40 static const char badInput_message[] = "bad input to function";
41 static const char badNorm_message[] = "bad norm";
42 static const char badIntegrationInput_message[] = "bad integration input";
43 static const char otherInterpolation_message[] = "other integration not supported";
44 static const char failedToConverge_message[] = "failed to converge";
45 static const char oddNumberOfValues_message[] = "odd number of inputted values";
46 
47 static int nfu_debugging = 0;
48 
49 /*
50 ************************************************************
51 */
52 double nfu_getNAN( void ) {
53 
54  return( NAN );
55 }
56 /*
57 ************************************************************
58 */
59 int nfu_isNAN( double d ) {
60 
61  return( isnan( d ) );
62 }
63 /*
64 ************************************************************
65 */
66 double nfu_getInfinity( double sign ) {
67 
68  if( sign < 0 ) return( -INFINITY );
69  return( INFINITY );
70 }
71 /*
72 ************************************************************
73 */
74 const char *nfu_statusMessage( nfu_status status ) {
75 
76  switch( status ) {
77  case nfu_Okay : return( Okay_message );
78  case nfu_mallocError : return( mallocError_message );
79  case nfu_insufficientMemory : return( insufficientMemory_message );
80  case nfu_badIndex : return( badIndex_message );
81  case nfu_XNotAscending : return( XNotAscending_message );
82  case nfu_badIndexForX : return( badIndexForX_message );
83  case nfu_XOutsideDomain : return( XOutsideDomain_message );
84  case nfu_invalidInterpolation : return( invalidInterpolation_message );
85  case nfu_badSelf : return( badSelf_message );
86  case nfu_divByZero : return( divByZero_message );
87  case nfu_unsupportedInterpolation : return( unsupportedInterpolation_message );
88  case nfu_unsupportedInterpolationConversion : return( unsupportedInterpolationConversion_message );
89  case nfu_empty : return( empty_message );
90  case nfu_tooFewPoints : return( tooFewPoints_message );
91  case nfu_domainsNotMutual : return( notMutualDomian_message );
92  case nfu_badInput : return( badInput_message );
93  case nfu_badNorm : return( badNorm_message );
94  case nfu_badIntegrationInput : return( badIntegrationInput_message );
95  case nfu_otherInterpolation : return( otherInterpolation_message );
96  case nfu_failedToConverge : return( failedToConverge_message );
97  case nfu_oddNumberOfValues : return( oddNumberOfValues_message );
98  }
99  return( unknownStatus_message );
100 }
101 /*
102 ************************************************************
103 */
104 void nfu_setMemoryDebugMode( int mode ) {
105 
106  nfu_debugging = mode;
107 }
108 /*
109 ************************************************************
110 */
111 void *nfu_malloc( size_t size ) {
112 
113  void *p = malloc( size );
114 
115  if( nfu_debugging ) printf( "nfu_malloc %12p size = %8llu\n", p, (long long unsigned) size );
116  return( p );
117 }
118 /*
119 ************************************************************
120 */
121 void *nfu_calloc( size_t size, size_t n ) {
122 
123  void *p = calloc( size, n );
124 
125  if( nfu_debugging ) printf( "nfu_calloc %12p size = %8llu, n = %8llu\n", p, (long long unsigned) size, (long long unsigned) n );
126  return( p );
127 }
128 /*
129 ************************************************************
130 */
131 void *nfu_realloc( size_t size, void *old ) {
132 
133  void *p = realloc( old, size );
134 
135  if( nfu_debugging ) printf( "nfu_realloc %12p size = %8llu, old = %12p\n", p, (long long unsigned) size, old );
136  return( p );
137 }
138 /*
139 ************************************************************
140 */
141 void *nfu_free( void *p ) {
142 
143  if( p != NULL ) {
144  if( nfu_debugging ) printf( "nfu_free %12p\n", p );
145  free( p );
146  }
147  return( NULL );
148 }
149 /*
150 ********************************************************
151 */
152 void nfu_printMsg( char *fmt, ... ) {
153 
154  va_list args;
155 
156  va_start( args, fmt );
157  vfprintf( stderr, fmt, args );
158  fprintf( stderr, "\n" );
159  va_end( args );
160 }
161 /*
162 ********************************************************
163 */
164 void nfu_printErrorMsg( char *fmt, ... ) {
165 
166  va_list args;
167 
168  va_start( args, fmt );
169  vfprintf( stderr, fmt, args );
170  fprintf( stderr, "\n" );
171  va_end( args );
172 
173  exit( EXIT_FAILURE );
174 }
175 
176 #if defined __cplusplus
177 }
178 #endif
static const char empty_message[]
Definition: nf_utilities.cc:36
void * nfu_realloc(size_t size, void *old)
static const char mallocError_message[]
Definition: nf_utilities.cc:25
static const char unsupportedInterpolation_message[]
Definition: nf_utilities.cc:34
static const char XNotAscending_message[]
Definition: nf_utilities.cc:28
static const char insufficientMemory_message[]
Definition: nf_utilities.cc:26
static const char badIndex_message[]
Definition: nf_utilities.cc:27
static const char unsupportedInterpolationConversion_message[]
Definition: nf_utilities.cc:35
void nfu_setMemoryDebugMode(int mode)
static const char tooFewPoints_message[]
Definition: nf_utilities.cc:37
static const char badSelf_message[]
Definition: nf_utilities.cc:32
static const char unknownStatus_message[]
Definition: nf_utilities.cc:39
const G4int n
static const char otherInterpolation_message[]
Definition: nf_utilities.cc:43
static const char XOutsideDomain_message[]
Definition: nf_utilities.cc:30
static int nfu_debugging
Definition: nf_utilities.cc:47
static const char Okay_message[]
Definition: nf_utilities.cc:24
static const char notMutualDomian_message[]
Definition: nf_utilities.cc:38
static const char divByZero_message[]
Definition: nf_utilities.cc:33
int nfu_isNAN(double d)
Definition: nf_utilities.cc:59
static const char badIntegrationInput_message[]
Definition: nf_utilities.cc:42
static const char failedToConverge_message[]
Definition: nf_utilities.cc:44
double nfu_getInfinity(double sign)
Definition: nf_utilities.cc:66
double nfu_getNAN(void)
Definition: nf_utilities.cc:52
const char * nfu_statusMessage(nfu_status status)
Definition: nf_utilities.cc:74
static const char invalidInterpolation_message[]
Definition: nf_utilities.cc:31
static const char badIndexForX_message[]
Definition: nf_utilities.cc:29
static const char badInput_message[]
Definition: nf_utilities.cc:40
void * nfu_free(void *p)
static const char badNorm_message[]
Definition: nf_utilities.cc:41
void nfu_printMsg(char *fmt,...)
void nfu_printErrorMsg(char *fmt,...)
G4int sign(const T t)
A simple sign function that allows us to port fortran code to c++ more easily.
void * nfu_calloc(size_t size, size_t n)
static const char oddNumberOfValues_message[]
Definition: nf_utilities.cc:45
void * nfu_malloc(size_t size)