-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproject2_Delaunoy_Crasset_IO.c
445 lines (377 loc) · 12.1 KB
/
project2_Delaunoy_Crasset_IO.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
436
437
438
439
440
441
442
443
444
445
#include <assert.h>
#include <libgen.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <mpi.h>
#include "project2_Delaunoy_Crasset_IO.h"
#include "project2_Delaunoy_Crasset_EXPLICIT.h"
/**
* Compute the value of the map at a given point using bilinear interpolation
*
* Parameters:
* map: The map used
* x: The x coordinate at which to evaluate the map
* y: The y coordinate at which to evaluate the map
*
* Returns:
* The evaluation of map a point x, y
*/
double bilinearInterpolation(Map* map, double x, double y){
assert(0 <= x);
assert(0 <= y);
assert(x <= map->a);
assert(y <= map->b);
// Sampling coordinates
int k = trunc(x/map->dx);
int l = trunc(y/map->dy);
// Compute border values
double x_k = k * map->dx;
double x_k1 = (k+1) * map->dx;
double y_l = l * map->dy;
double y_l1 = (l+1) * map->dy;
// Compute coeficients
double prod1 = (x_k1 - x) * (y_l1 - y);
double prod2 = (x_k1 - x) * (y - y_l);
double prod3 = (x - x_k) * (y_l1 - y);
double prod4 = (x - x_k) * (y - y_l);
// Interpolate
double return_value = prod1 * map->grid[k][l];
double epsilon = 10e-6;
// Robust implementation of the statement
if(fabs(x - map->a) > epsilon)
return_value += prod3 * map->grid[k+1][l];
if(fabs(y - map->b) > epsilon)
return_value += prod2 * map->grid[k][l+1];
if(fabs(x - map->a) > epsilon && fabs(y - map->b) > epsilon)
return_value += prod4 * map->grid[k+1][l+1];
return_value /= map->dx*map->dy;
return return_value;
}
/**
* Compute the value of the map at a given point
*
* Parameters:
* map: The map used
* x: The x coordinate at which to evaluate the map
* y: The y coordinate at which to evaluate the map
*
* Returns:
* The evaluation of map a point x, y
*/
double getGridValueAtDomainCoordinates(Map* map, double x, double y){
double epsilon = 10e-6;
assert(x >= 0);
assert(y >= 0);
// Sampling step
// If value already in the grid, use that instead of interpolating
if(fmod(x, map->dx) < epsilon && fmod(y, map->dy) < epsilon){
return map->grid[(int) trunc(x/map->dx)][(int) trunc(y/map->dy)];
} else {
return bilinearInterpolation(map, x, y);
}
}
/**
* Allocate a double matrix
*
* Parameters:
* x: The first index size
* y: The second index size
*
* Returns:
* The allocated matrix
*/
double** allocateDoubleMatrix(int x, int y){
assert(x > 0);
assert(y > 0);
double** matrix = malloc(x * sizeof(double*));
if(!matrix)
return NULL;
for(int i = 0; i < x; i++){
matrix[i] = malloc(y * sizeof(double));
if(!matrix[i]){
for(int j = i-1; j >= 0; j--)
free(matrix[j]);
free(matrix);
return NULL;
}
}
return matrix;
}
/**
* Free a double matrix
*
* Parameters:
* matrix: The matrix to free
* x: The first index size
*/
void freeDoubleMatrix(double** matrix, int x){
assert(x > 0);
assert(matrix);
for(int i = 0; i < x; i++){
if(matrix[i] != NULL){
free(matrix[i]);
}
}
free(matrix);
}
/**
* Tansform a matrix to a 1d flattenned array
*
* Parameters:
* matrix: The matrix to flatten
* x: The first index size
* y: The second index size
*
* Returns:
* The flattenned array
*/
double* transformMatrixToArray(double** matrix, int x, int y){
double * array = calloc(x*y, sizeof(double));
int cnt = 0;
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
array[cnt++] = matrix[i][j];
}
}
return array;
}
/**
* Read a map file and load it in a Map structure
*
* Parameters:
* filename: The name of the file to load
*
* Returns:
* A pointer to a Map structure containing the info in the file
*/
Map* readMapFile(const char* filename) {
FILE* fp;
char buffer[8];
fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "Unable to open file %s\n", filename);
MPI_Finalize();
exit(EXIT_FAILURE);
}
// Read the parameters at the top of the map file
// Assumption : there are no spaces and no end of lines, just
// contiguous bytes in double precision (8 bytes per unit)
Map* map = malloc(sizeof(Map));
if (map == NULL) {
fprintf(stderr, "Unable to allocate memory for the map\n");
fclose(fp);
MPI_Finalize();
exit(EXIT_FAILURE);
}
// Read constants from the map file
fread(buffer, 8, 1, fp);
map->a = *((double*)buffer);
fread(buffer, 8, 1, fp);
map->b = *((double*)buffer);
fread(buffer, 4, 1, fp);
map->X = *((int*)buffer);
fread(buffer, 4, 1, fp);
map->Y = *((int*)buffer);
// Sampling step
map->dx = map->a / (map->X - 1);
map->dy = map->b / (map->Y - 1);
map->grid = allocateDoubleMatrix(map->X, map->Y);
if (map->grid == NULL) {
fprintf(stderr, "Unable to allocate memory for the grid\n");
free(map);
fclose(fp);
MPI_Finalize();
exit(EXIT_FAILURE);
}
// Read the bathymetry depth grid from the map file
long long i = 0;
fread(buffer, 8, 1, fp);
for (int row = map->Y - 1; row >= 0; row--) {
for (int col = 0; col < map->X; fread(buffer, 8, 1, fp), col++) {
map->grid[col][row] = *((double*)buffer);
}
}
fclose(fp);
return map;
}
/**
* Read a parameter file and load it in a Parameters structure
*
* Parameters:
* filename: The name of the file to load
*
* Returns:
* A pointer to a Parameters structure containing the info in the file
*/
Parameters* readParameterFile(const char* filename) {
FILE* fp;
// Open file
fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Unable to open file %s\n", filename);
MPI_Finalize();
exit(EXIT_FAILURE);
}
// Allocate structure
Parameters* params = malloc(sizeof(Parameters));
if (params == NULL) {
fprintf(stderr, "Unable to allocate memory for parameters\n");
fclose(fp);
MPI_Finalize();
exit(EXIT_FAILURE);
}
// Fille structure with info from the file
params->filename = filename;
fscanf(fp, "%lf", ¶ms->g);
fscanf(fp, "%lf", ¶ms->gamma);
fscanf(fp, "%lf", ¶ms->deltaX);
fscanf(fp, "%lf", ¶ms->deltaY);
fscanf(fp, "%lf", ¶ms->deltaT);
fscanf(fp, "%lf", ¶ms->TMax);
fscanf(fp, "%lf", ¶ms->A);
fscanf(fp, "%lf", ¶ms->f);
fscanf(fp, "%u", ¶ms->S);
fscanf(fp, "%u", ¶ms->s);
fscanf(fp, "%lf", ¶ms->r_threshold);
fclose(fp);
return params;
}
/**
* Write an array into a file
*
* Parameters:
* filename: The name of the file in which to write the array
* xsize: The size of the discretization along the x axis
* ysize: The size of the discretization along the y axis
* array: The array to write in the file
*/
void writeResultArray(char* filename, int xsize, int ysize, double* array) {
// Open file
FILE* fp = fopen(filename, "wb");
if (fp == NULL) {
fprintf(stderr, "Unable to open file %s\n", filename);
MPI_Finalize();
exit(EXIT_FAILURE);
}
// Write sizes
fwrite(&xsize, sizeof(xsize), 1, fp);
fwrite(&ysize, sizeof(ysize), 1, fp);
// Write array
for (int row = ysize - 1; row >= 0; row--) {
for (int col = 0; col < xsize; col++) {
int index = col * ysize + row;
fwrite(&array[index], 8, 1, fp);
}
}
fclose(fp);
}
/**
* Build file names for each variable to save
*
* Parameters:
* etaName: A pointer to char that will be set to the name of eta
* uName: A pointer to char that will be set to the name of u
* vName: A pointer to char that will be set to the name of v
* dir_name: A string containg the name of the directory to include in the filename
* iteration: The iteration at which to make the save
*/
void getFileNames(char* etaName, char* uName, char* vName, char* dir_name, unsigned int iteration) {
// Base prefixes
char* etaPrefix = "eta";
char* uPrefix = "u";
char* vPrefix = "v";
//Build suffix based on ieration
char file_suffix[MAX_FILENAME_SIZE];
snprintf(file_suffix, MAX_FILENAME_SIZE, "_%u", iteration);
// Build eta name
strncpy(etaName, dir_name, MAX_FILENAME_SIZE);
strncat(etaName, etaPrefix, MAX_FILENAME_SIZE);
strncat(etaName, file_suffix, MAX_FILENAME_SIZE);
strncat(etaName, ".dat", MAX_FILENAME_SIZE);
// Build u name
strncpy(uName, dir_name, MAX_FILENAME_SIZE);
strncat(uName, uPrefix, MAX_FILENAME_SIZE);
strncat(uName, file_suffix, MAX_FILENAME_SIZE);
strncat(uName, ".dat", MAX_FILENAME_SIZE);
// Build v name
strncpy(vName, dir_name, MAX_FILENAME_SIZE);
strncat(vName, vPrefix, MAX_FILENAME_SIZE);
strncat(vName, file_suffix, MAX_FILENAME_SIZE);
strncat(vName, ".dat", MAX_FILENAME_SIZE);
}
/**
* Save system state to disk
*
* Parameters:
* etaTotal: An array containing the values of eta
* uTotal: An array containing the values of u
* vTotal: An array containing the values of v
* xSize: The size of the discretization along the x axis
* ySize: The size of the discretization along the y axis
* iteration: The iteration at which the save is performed
* params: The parameters of the run
* nbproc: The number of processes
* nbthreads: The number of threads
*
* Returns:
* A pointer to a Map structure containing the info in the file
*/
void saveToDisk(double* etaTotal, double* uTotal, double* vTotal, unsigned int xSize,
unsigned int ySize, unsigned int iteration, Parameters* params, int nbproc, int nbthreads) {
static int createDirectory = 0;
static char full_path[MAX_FILENAME_SIZE];
int status = 0;
// Attempt creating a directory when calling this function for the first time
if(createDirectory == 0){
createDirectory = 1;
//Get current working directory
char current_dir[MAX_FILENAME_SIZE];
getcwd(current_dir, MAX_FILENAME_SIZE);
// Get parameter file and remove '.txt' extension
char* parameter_file = basename((char*)params->filename);
parameter_file[strlen(parameter_file)-4] = 0;
// Create output directory
char new_dir[MAX_FILENAME_SIZE];
snprintf(new_dir, MAX_FILENAME_SIZE, "/Results/matrices_of_%s_%d_%d/", parameter_file, nbproc, nbthreads);
strncpy(full_path, current_dir, MAX_FILENAME_SIZE);
strncat(full_path, new_dir, MAX_FILENAME_SIZE);
char* result_dir = "/Results";
char intermediate_dir[MAX_FILENAME_SIZE];
strncpy(intermediate_dir, current_dir, MAX_FILENAME_SIZE);
strncat(intermediate_dir, result_dir, MAX_FILENAME_SIZE);
// Check if file exists, if not, create it.
if(access(intermediate_dir, F_OK) == -1) {
status = mkdir(intermediate_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(status == -1){
fprintf(stderr, "Error in saveToDisk = %s\n" , strerror(errno));
fprintf(stderr, "Attempted to create: %s\n" , full_path);
MPI_Finalize();
exit(EXIT_FAILURE);
}
}
// Check if file exists, if not, create it.
if(access(full_path, F_OK) == -1) {
status = mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(status == -1){
fprintf(stderr, "Error in saveToDisk = %s\n" , strerror(errno));
fprintf(stderr, "Attempted to create: %s\n" , full_path);
MPI_Finalize();
exit(EXIT_FAILURE);
}
}
}
// Create file names
char etaFilename[MAX_FILENAME_SIZE];
char uFilename[MAX_FILENAME_SIZE];
char vFilename[MAX_FILENAME_SIZE];
getFileNames(etaFilename, uFilename, vFilename, full_path, iteration);
writeResultArray(etaFilename, xSize + 1, ySize + 1, etaTotal);
writeResultArray(uFilename, xSize + 2, ySize + 1, uTotal);
writeResultArray(vFilename, xSize + 1, ySize + 2, vTotal);
}