-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.c
executable file
·436 lines (345 loc) · 12.9 KB
/
helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include "helper.h"
/* ----------------------------------------------------------------------- */
/* auxiliary functions */
/* ----------------------------------------------------------------------- */
int min( int a, int b)
{
if( a < b ) return a;
return b;
}
int max( int a, int b)
{
if( a > b ) return a;
return b;
}
double fmin( double a, double b)
{
if( a < b ) return a;
return b;
}
double fmax( double a, double b)
{
if( a > b ) return a;
return b;
}
/* ----------------------------------------------------------------------- */
/* local auxiliary functions */
/* ----------------------------------------------------------------------- */
clock_t last_timer_reset;
int min_int( const int n1, const int n2 )
{
if( n1 < n2 ) return n1;
return n2;
}
/* ----------------------------------------------------------------------- */
/* read datafile */
/* ----------------------------------------------------------------------- */
void errhandler( int nLine, const char *szFile, const char *szString )
{
int err = errno;
fprintf( ERROUT, "%s:%d Error : %s", szFile, nLine, szString );
fprintf( ERROUT, "\n" );
/* if an error within the c-library occured, an error code can be */
/* found in the global variable err */
if( err != 0 )
{
fprintf( ERROUT, "C-Lib errno = %d\n", err);
fprintf( ERROUT, "C-Lib strerror = %s\n", strerror( err ) );
}
exit(1);
}
/* for comfort */
#define READ_ERROR(szMessage, szVarName, szFileName, nLine) \
{ char szTmp[80]; \
if( nLine ) \
sprintf( szTmp, " %s File: %s Variable: %s Line: %d", szMessage, szFileName, szVarName, nLine ); \
else \
sprintf( szTmp, " %s File: %s Variable: %s ", szMessage, szFileName, szVarName); \
ERROR( szTmp ); \
}
/* --------------------------------------------------------------------------*/
/* The function searches the datafile fh for the line defining the variable */
/* szVarName and returns the respctive string including the value of the */
/* variable. If there's no appropriate line within the datafile, the program */
/* stops with an error messsage. */
/* ATTENTION: The pointer returned refers to a static variable within the */
/* function. To maintain the string over several program calls, it has to be */
/* copied!!! */
/* */
char* find_string( const char* szFileName, const char *szVarName )
{
int nLine = 0;
int i;
FILE *fh = NULL;
static char szBuffer[MAX_LINE_LENGTH]; /* containes the line read */
/* from the datafile */
char* szLine = szBuffer;
char* szValue = NULL;
char* szName = NULL;
/* open file */
fh = fopen( szFileName, "rt" );
if( fh == 0 )
READ_ERROR("Could not open file", szVarName, szFileName, 0);
/* searching */
while( ! feof(fh) )
{
fgets( szLine, MAX_LINE_LENGTH, fh );
++nLine;
/* remove comments */
for( i = 0; i < strlen(szLine); i++)
if( szLine[i] == '#' )
{
szLine[i] = '\0'; /* Stringende setzen */
break;
}
/* remove empty lines */
while( isspace( (int)*szLine ) && *szLine) ++szLine;
if( strlen( szLine ) == 0) continue;
/* now, the name can be extracted */
szName = szLine;
szValue = szLine;
while( (isalnum( (int)*szValue ) || *szValue == '_') && *szValue) ++szValue;
/* is the value for the respective name missing? */
if( *szValue == '\n' || strlen( szValue) == 0)
READ_ERROR("wrong format", szName, szFileName, nLine);
*szValue = 0; /* complete szName! at the right place */
++szValue;
/* read next line if the correct name wasn't found */
if( strcmp( szVarName, szName)) continue;
/* remove all leading blnkets and tabs from the value string */
while( isspace( (int)*szValue) ) ++szValue;
if( *szValue == '\n' || strlen( szValue) == 0)
READ_ERROR("wrong format", szName, szFileName, nLine);
fclose(fh);
return szValue;
}
READ_ERROR("variable not found", szVarName, szFileName, nLine);
return NULL; /* dummy to satisfy the compiler */
}
void read_string( const char* szFileName, const char* szVarName, char* pVariable)
{
char* szValue = NULL; /* string containg the read variable value */
if( szVarName == 0 ) ERROR("null pointer given as variable name" );
if( szFileName == 0 ) ERROR("null pointer given as filename" );
if( pVariable == 0 ) ERROR("null pointer given as variable" );
if( szVarName[0] == '*' )
szValue = find_string( szFileName, szVarName +1 );
else
szValue = find_string( szFileName, szVarName );
if( sscanf( szValue, "%s", pVariable) == 0)
READ_ERROR("wrong format", szVarName, szFileName,0);
printf( "File: %s\t\t%s%s= %s\n", szFileName,
szVarName,
&(" "[min_int( strlen(szVarName), 15)]),
pVariable );
}
void read_int( const char* szFileName, const char* szVarName, int* pVariable)
{
char* szValue = NULL; /* string containing the read variable value */
if( szVarName == 0 ) ERROR("null pointer given as varable name" );
if( szFileName == 0 ) ERROR("null pointer given as filename" );
if( pVariable == 0 ) ERROR("null pointer given as variable" );
if( szVarName[0] == '*' )
szValue = find_string( szFileName, szVarName +1 );
else
szValue = find_string( szFileName, szVarName );
if( sscanf( szValue, "%d", pVariable) == 0)
READ_ERROR("wrong format", szVarName, szFileName, 0);
printf( "File: %s\t\t%s%s= %d\n", szFileName,
szVarName,
&(" "[min_int( strlen(szVarName), 15)]),
*pVariable );
}
void read_double( const char* szFileName, const char* szVarName, double* pVariable)
{
char* szValue = NULL; /* String mit dem eingelesenen Variablenwert */
if( szVarName == 0 ) ERROR("null pointer given as varable name" );
if( szFileName == 0 ) ERROR("null pointer given as filename" );
if( pVariable == 0 ) ERROR("null pointer given as variable" );
if( szVarName[0] == '*' )
szValue = find_string( szFileName, szVarName +1 );
else
szValue = find_string( szFileName, szVarName );
if( sscanf( szValue, "%lf", pVariable) == 0)
READ_ERROR("wrong format", szVarName, szFileName, 0);
printf( "File: %s\t\t%s%s= %f\n", szFileName,
szVarName,
&(" "[min_int( strlen(szVarName), 15)]),
*pVariable );
}
/* ----------------------------------------------------------------------- */
/* write matrices to a file */
/* ----------------------------------------------------------------------- */
void write_matrix( const char* szFileName, /* filename */
double **m, /* matrix */
int nrl, /* first column */
int nrh, /* last column */
int ncl, /* first row */
int nch, /* last row */
double xlength, /* size of the geometry in */
/* x-direction */
double ylength, /* size of the geometry in */
/* y-direction */
int fFirst, /* 0 == append, else overwrite*/
int bPart ) /* 0 for files corresponding to subdomains,
in this case xlength, ylenght, nrl, nrh, ncl, nch are written
at the beginning of the file,
1 for whole region, no additional range information is written */
{
printf("write matrix %s\n", szFileName);
int i, j;
FILE * fh = 0;
if( fFirst ) /* first call of the function ? */
{
fh = fopen( szFileName, "w"); /* overwrite file/write new file */
if( fh == NULL ) /* opening failed ? */
{
char szBuff[80];
sprintf( szBuff, "Outputfile %s cannot be created", szFileName );
ERROR( szBuff );
}
if(bPart==0)
fprintf( fh,"%f\n%f\n%d\n%d\n%d\n%d\n", xlength, ylength, nrl, nrh, ncl, nch );
}
else
{
fh = fopen( szFileName ,"a"); /* append to the file */
if( fh == NULL ) /* opening failed ? */
{
char szBuff[80];
sprintf( szBuff, "Outputfile %s cannot be opened", szFileName );
ERROR( szBuff );
}
}
fprintf( fh,"dx:%f\n%f\n%d\n%d\n%d\n%d\n", xlength, ylength, nrl, nrh, ncl, nch );
for( j = nch; j >= ncl; j--){
for( i = nrl; i <= nrh; i++){
fprintf(fh," %2.2f ", (float)m[i][j]);
}
fprintf(fh, "\n");
}
if( fclose(fh) )
{
char szBuff[80];
sprintf( szBuff, "Outputfile %s cannot be closed", szFileName );
ERROR( szBuff );
};
}
size_t read_matrix( FILE *fh, /* file handle */
double **m, /* matrix */
int nrl, /* first row */
int nrh, /* last row */
int ncl, /* first line */
int nch )
{
int i, j, k;
float *Buff;
int SizeCol;
int SizeRow;
int Size;
int nRead;
/* read matrix in the array */
SizeRow = nrh - nrl + 1;
SizeCol = nch - ncl + 1;
Size = SizeRow * SizeCol;
/* create buffer */
Buff = (float *)malloc( Size * sizeof(float) );
nRead = fread( Buff, sizeof(float), Size, fh );
/* exit the routine if EOF (end of file) */
if( feof( fh ))
return EOF;
/* some errors ? */
if( nRead != Size )
{
char szBuff[80];
sprintf( szBuff, "Error: Cann't read the file %s", "unknown" );
ERROR( szBuff );
}
/* copy the buffer into array */
k = 0;
for( j = ncl; j <= nch; j++)
for( i = nrl; i <= nrh; i++)
{
m[i][j] = Buff[k++];
}
free( Buff );
return nRead;
}
/* ----------------------------------------------------------------------- */
/* general matrix functions */
/* ----------------------------------------------------------------------- */
/* allocates storage for a matrix */
double **matrix( int nrl, int nrh, int ncl, int nch )
{
int i;
int nrow = nrh - nrl + 1; /* compute number of lines */
int ncol = nch - ncl + 1; /* compute number of columns */
double **pArray = (double **) malloc((size_t)( nrow * sizeof(double*)) );
double *pMatrix = (double *) malloc((size_t)( nrow * ncol * sizeof( double )));
if( pArray == 0) ERROR("Storage cannot be allocated");
if( pMatrix == 0) ERROR("Storage cannot be allocated");
/* first entry of the array points to the value corrected by the
beginning of the column */
pArray[0] = pMatrix - ncl;
/* compute the remaining array entries */
for( i = 1; i < nrow; i++ )
{
pArray[i] = pArray[i-1] + ncol;
}
/* return the value corrected by the beginning of a line */
return pArray - nrl;
}
/* deallocates the storage of a matrix */
void free_matrix( double **m, int nrl, int nrh, int ncl, int nch )
{
double **pArray = m + nrl;
double *pMatrix = m[nrl]+ncl;
free( pMatrix );
free( pArray );
}
void init_matrix( double **m, int nrl, int nrh, int ncl, int nch, double a)
{
int i,j;
for( i = nrl; i <= nrh; i++)
for( j = ncl; j <= nch; j++)
m[i][j] = a;
}
/* allocates storage for a matrix */
int **imatrix( int nrl, int nrh, int ncl, int nch )
{
int i;
int nrow = nrh - nrl + 1; /* compute number of rows */
int ncol = nch - ncl + 1; /* compute number of columns */
int **pArray = (int **) malloc((size_t)( nrow * sizeof( int* )) );
int *pMatrix = (int *) malloc((size_t)( nrow * ncol * sizeof( int )));
if( pArray == 0) ERROR("Storage cannot be allocated");
if( pMatrix == 0) ERROR("Storage cannot be allocated");
/* first entry of the array points to the value corrected by the
beginning of the column */
pArray[0] = pMatrix - ncl;
/* compute the remaining array entries */
for( i = 1; i < nrow; i++ )
{
pArray[i] = pArray[i-1] + ncol;
}
/* return the value corrected by the beginning of a line */
return pArray - nrl;
}
/* deallocates the storage of a matrix */
void free_imatrix( int **m, int nrl, int nrh, int ncl, int nch )
{
int **pArray = m + nrl;
int *pMatrix = m[nrl]+ncl;
free( pMatrix );
free( pArray );
}
void init_imatrix( int **m, int nrl, int nrh, int ncl, int nch, int a)
{
int i,j;
for( i = nrl; i <= nrh; i++)
for( j = ncl; j <= nch; j++)
m[i][j] = a;
}