-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCC_GridFunction2dUtility.h
More file actions
591 lines (468 loc) · 14.5 KB
/
SCC_GridFunction2dUtility.h
File metadata and controls
591 lines (468 loc) · 14.5 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* SCC_GridFunction2dUtility.h
*
* Created on: Jun 28, 2015
* Author: anderson
*
*
* Utility routines - mostly routines for file input and output
* of GridFunction2d instances.
*
* Modifications: Jun 28, 2015
* Sept. 4, 2018
*
* Release : 18.09.04
*/
/*
#############################################################################
#
# Copyright 2015 Chris Anderson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For a copy of the GNU General Public License see
# <http://www.gnu.org/licenses/>.
#
#############################################################################
*/
#ifndef SCC_GRID_FUNCTION_2D_UTILITY_
#define SCC_GRID_FUNCTION_2D_UTILITY_
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <functional>
// MS compilers generate warnings if fopen is used instead of fopen_s (a Microsoft specific language
// extension, so this macro implements the appropriate equivalent to fopen that MS wants when
// MS compilers are being used. In both versions, the
// macro returns a non-zero value if the open fails (e.g. a non-zero error code).
//
#ifndef _MSC_VER
#ifndef OPENFILE
#define OPENFILE(dataFile,filename,mode) ((dataFile = fopen(filename, mode)) == NULL)
#endif
#else
#ifndef OPENFILE
#define OPENFILE(dataFile,fileName,mode) ((fopen_s(&dataFile,fileName, mode)) != 0)
#pragma warning(push)
#pragma warning(disable: 4996)
#endif
#endif
#include "../GridFunctionNd/SCC_GridFunction2d.h"
#include "../DoubleVectorNd/SCC_DoubleVector2d.h"
namespace SCC
{
class GridFunction2dUtility
{
public :
// Adds the values of F to the input gFun
void addToValues(GridFunction2d& gFun, std::function<double(double,double)>& F)
{
long i; long j;
double xMin = gFun.getXmin();
double yMin = gFun.getYmin();
long xPanels = gFun.getXpanelCount();
long yPanels = gFun.getYpanelCount();
double hx = gFun.getHx();
double hy = gFun.getHy();
double x; double y;
for(i = 0; i <= xPanels; i++)
{
x = xMin + i*hx;
for(j = 0; j <= yPanels; j++)
{
y = yMin + j*hy;
gFun.Values(i,j) += F(x,y);
}}
}
//
// Gnuplot output data structure
//
// # xPanels xMin xMax yPanels yMin yMax
// x_0 y_0 f(x_0, y_0)
// x_0 y_1 f(x_0, y_1)
// ***
// x_0 y_yPanels f(x_0, y_yPanels)
//
// x_1 y_0 f(x_1, y_0)
// x_1 y_1 f(x_1, y_1))
//
void outputToGNUplot(GridFunction2d& gF, const std::string& fileName, const std::string& formatString = "%20.15e")
{
std::ostringstream s;
s.str("");
s << formatString << " " << formatString << " " << formatString << " \n";
//
// Open and then write to a file
//
FILE* dataFile = 0;
if(OPENFILE(dataFile,fileName.c_str(), "w+" ))
{
throw std::runtime_error("\nCannot open " + fileName + " \nFile not found.\n");
}
long xPanel = gF.getXpanelCount();
double xMin = gF.getXmin();
double xMax = gF.getXmax();
long yPanel = gF.getYpanelCount();
double yMin = gF.getYmin();
double yMax = gF.getYmax();
long i; long j;
fprintf(dataFile,"# %ld %20.15e %20.15e %ld %20.15e %20.15e \n",
xPanel,xMin,xMax,yPanel, yMin, yMax);
double x; double y;
double hx = (xMax-xMin)/(double)(xPanel);
double hy = (yMax-yMin)/(double)(yPanel);
for(i = 0; i <= xPanel; i++)
{
x = xMin + double(i)*hx;
for(j = 0; j <= yPanel; j++)
{
y = yMin + double(j)*hy;
fprintf(dataFile,(s.str()).c_str(),x,y,gF(i,j));
}
fprintf(dataFile,"\n");
}
fclose(dataFile);
}
void inputFromGNUplot(GridFunction2d& gF, const std::string& fileName)
{
//
// Open and then read from file
//
FILE* dataFile = 0;
if(OPENFILE(dataFile,fileName.c_str(), "r" ))
{
throw std::runtime_error("\nCannot open " + fileName + " \nFile not found.\n");
}
bool errFlag = false;
char poundChar;
long i; long j;
long xPanels; long yPanels;
double xMin; double xMax;
double yMin; double yMax;
errFlag = (fscanf(dataFile,"%c", £Char) != 1 ) || errFlag; // remove leading #
errFlag = (fscanf(dataFile,"%ld",&xPanels) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&xMin) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&xMax) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%ld",&yPanels) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&yMin) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&yMax) != 1 ) || errFlag;
errFlag = (xMax < xMin) || errFlag;
errFlag = (yMax < yMin) || errFlag;
errFlag = (xPanels <= 0) || errFlag;
errFlag = (yPanels <= 0) || errFlag;
if(errFlag)
{
throw std::runtime_error("\nSCC::GridFunction2d could not be initialized from gnuplot data file " + fileName + " \n");
}
gF.initialize(xPanels,xMin,xMax,yPanels, yMin, yMax);
double x; double y;
for(i = 0; i <= xPanels; i++)
{
for(j = 0; j <= yPanels; j++)
{
errFlag = (fscanf(dataFile,"%lf %lf %lf",&x,&y,&gF(i,j)) != 3 ) || errFlag;
}
}
if(errFlag)
{
throw std::runtime_error("\nSCC::GridFunction2d could not be initialized from gnuplot data file " + fileName + " \n");
}
fclose(dataFile);
}
void outputDataToVTKfile(const GridFunction2d& gridFun, const std::string& fileName, const std::string& dataLabel)
{
FILE* dataFile;
double a = gridFun.getXmin();
double c = gridFun.getYmin();
double e = 0.0;
double hx = gridFun.getHx();
double hy = gridFun.getHy();
double hz = hx;
hz = (hz < hy) ? hy : hz;
long mPt = gridFun.getXpanelCount() + 1;
long nPt = gridFun.getYpanelCount() + 1;
long pPt = 2;
long dataCount = mPt*nPt*pPt;
if(OPENFILE(dataFile,fileName.c_str(), "w+" ))
{
printf( "The file %s could not be opened\n",fileName.c_str());
exit(1);
}
long i; long j; long k;
double xPos; double yPos; double zPos;
//
// output the regular positions
//
fprintf(dataFile, "# vtk DataFile Version 2.0\n");
fprintf(dataFile, "%s \n",dataLabel.c_str());
fprintf(dataFile, "ASCII\n");
fprintf(dataFile, "DATASET RECTILINEAR_GRID\n");
fprintf(dataFile, "DIMENSIONS %ld %ld %ld \n",mPt,nPt,pPt);
fprintf(dataFile, "X_COORDINATES %ld float \n",mPt);
for(i = 0; i < mPt; i++)
{
xPos = i*hx + a;
fprintf(dataFile, "%10.5e ",xPos);
}
fprintf(dataFile, "\n");
fprintf(dataFile, "Y_COORDINATES %ld float \n",nPt);
for(j = 0; j < nPt; j++)
{
yPos = j*hy + c;
fprintf(dataFile, "%10.5e ",yPos);
}
fprintf(dataFile, "\n");
fprintf(dataFile, "Z_COORDINATES %ld float \n",pPt);
for(k = 0; k < pPt; k++)
{
zPos = k*hz + e;
fprintf(dataFile, "%10.5e ",zPos);
}
fprintf(dataFile, "\n");
fprintf(dataFile, "POINT_DATA %ld\n",dataCount);
fprintf(dataFile, "SCALARS %s float\n",dataLabel.c_str());
fprintf(dataFile, "LOOKUP_TABLE default\n");
for(k = 0; k < pPt; k++)
{
for(j = 0; j < nPt; j++)
{
for(i = 0; i < mPt; i++)
{
fprintf(dataFile, "%15.8e ",gridFun(i,j));
}
fprintf(dataFile, "\n");
}}
fclose(dataFile);
}
//
// Output data structure for ASCII and Binary output
//
// xPanels
// yPanels
// xMin
// xMax
// yMin
// yMax
// F(0,0)
// F(0,1)
// F(0,2)
//
// ***
//
void outputToDataFile(const GridFunction2d& gF, const std::string& fileName, const std::string& formatString = "%20.15e")
{
//
// Create format string
//
std::ostringstream s;
s.str("");
s << formatString << " ";
//
// Open and then write to a file
//
FILE* dataFile = 0;
if(OPENFILE(dataFile,fileName.c_str(), "w+" ))
{
throw std::runtime_error("\nCannot open " + fileName + " \nFile not found.\n");
}
long xPanels = gF.getXpanelCount();
long yPanels = gF.getYpanelCount();
double xMin = gF.getXmin();
double yMin = gF.getYmin();
double xMax = gF.getXmax();
double yMax = gF.getYmax();
fprintf(dataFile,"%ld \n", xPanels);
fprintf(dataFile,"%ld \n", yPanels);
fprintf(dataFile,"%20.15e \n",xMin);
fprintf(dataFile,"%20.15e \n",xMax);
fprintf(dataFile,"%20.15e \n",yMin);
fprintf(dataFile,"%20.15e \n",yMax);
for(long i = 0; i <= xPanels; i++)
{
for(long j = 0; j <= yPanels; j++)
{
fprintf(dataFile, s.str().c_str(),gF(i,j));
}
fprintf(dataFile, "\n");
}
fclose(dataFile);
}
void inputFromDataFile(GridFunction2d& gF, FILE* dataFile, std::string fileName = "")
{
bool errFlag = false;
long xPanels;
long yPanels;
double xMin;
double yMin;
double xMax;
double yMax;
errFlag = (fscanf(dataFile,"%ld", &xPanels) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%ld", &yPanels) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&xMin) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&xMax) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&yMin) != 1 ) || errFlag;
errFlag = (fscanf(dataFile,"%lf",&yMax) != 1 ) || errFlag;
errFlag = (xPanels <= 0) || errFlag;
errFlag = (yPanels <= 0) || errFlag;
errFlag = (xMax < xMin) || errFlag;
errFlag = (yMax < yMin) || errFlag;
gF.initialize(xPanels,xMin,xMax,yPanels,yMin,yMax);
for(long i = 0; i <= xPanels; i++)
{
for(long j = 0; j <= yPanels; j++)
{
errFlag = (fscanf(dataFile,"%lf",&gF(i,j)) != 1) || errFlag;
}}
if(errFlag)
{
throw std::runtime_error("\nSCC::GridFunction2d could not be initialized from file " + fileName + " \n");
}
}
void inputFromDataFile(GridFunction2d& gF, const std::string& fileName)
{
//
// Open input file
//
FILE* dataFile = 0;
if(OPENFILE(dataFile,fileName.c_str(), "r" ))
{
throw std::runtime_error("\nCannot open " + fileName + " \nFile not found.\n");
}
inputFromDataFile(gF, dataFile,fileName);
fclose(dataFile);
}
void outputToBinaryDataFile(const GridFunction2d& gF, FILE* dataFile)
{
long dataSize;
long xPanels = gF.getXpanelCount();
long yPanels = gF.getYpanelCount();
int64_t xPanels64 = (int64_t) xPanels;
int64_t yPanels64 = (int64_t) yPanels;
double xMin = gF.getXmin();
double xMax = gF.getXmax();
double yMin = gF.getYmin();
double yMax = gF.getYmax();
//
// Write out the grid structure information. Using std:int64
// for integer values to avoid problems with machines with
// alternate storage sizes for int's and long's
//
fwrite(&xPanels64, sizeof(int64_t), 1, dataFile);
fwrite(&yPanels64, sizeof(int64_t), 1, dataFile);
fwrite(&xMin, sizeof(double), 1, dataFile);
fwrite(&xMax, sizeof(double), 1, dataFile);
fwrite(&yMin, sizeof(double), 1, dataFile);
fwrite(&yMax, sizeof(double), 1, dataFile);
//
// Write ot the function values
//
dataSize = (xPanels+1)*(yPanels+1);
fwrite(gF.getDataPointer(), sizeof(double), dataSize, dataFile);
}
void inputFromBinaryDataFile(GridFunction2d& gF, const std::string& fileName)
{
// Open input file for binary read
FILE* dataFile = 0;
if(OPENFILE(dataFile,fileName.c_str(), "rb" ))
{
throw std::runtime_error("\nCannot open " + fileName + " \nFile not found.\n");
}
inputFromBinaryDataFile(gF,dataFile,fileName);
fclose(dataFile);
}
void inputFromBinaryDataFile(GridFunction2d& gF, FILE* dataFile, std::string fileName = "")
{
bool errFlag = false;
size_t dataSize;
long xPanels;
long yPanels;
double xMin; double yMin;
double xMax; double yMax;
int64_t xPanels64;
int64_t yPanels64;
errFlag = (fread(&xPanels64, sizeof(int64_t), 1, dataFile) != 1) || errFlag;
errFlag = (fread(&yPanels64, sizeof(int64_t), 1, dataFile) != 1) || errFlag;
errFlag = (fread(&xMin, sizeof(double), 1, dataFile) != 1 ) || errFlag;
errFlag = (fread(&xMax, sizeof(double), 1, dataFile) != 1 ) || errFlag;
errFlag = (fread(&yMin, sizeof(double), 1, dataFile) != 1 ) || errFlag;
errFlag = (fread(&yMax, sizeof(double), 1, dataFile) != 1 ) || errFlag;
xPanels = (long)xPanels64;
yPanels = (long)yPanels64;
errFlag = (xPanels <= 0) || errFlag;
errFlag = (yPanels <= 0) || errFlag;
errFlag = (xMax < xMin) || errFlag;
errFlag = (yMax < yMin) || errFlag;
if(errFlag == 1)
{
throw std::runtime_error("\nSCC::GridFunction2d could not be initialized from file " + fileName + " \n");
}
// Initialize instance and then read in the data
gF.initialize(xPanels,xMin,xMax,yPanels,yMin,yMax);
dataSize = (xPanels+1)*(yPanels+1);
errFlag = (fread(gF.getDataPointer(), sizeof(double), dataSize, dataFile) != dataSize ) || errFlag;
if(errFlag)
{
throw std::runtime_error("\nSCC::GridFunction2d could not be initialized from file " + fileName + " \n");
}
}
void inputValuesFromBinaryDataFile(GridFunction2d& gF, FILE* dataFile, std::string fileName = "")
{
bool errFlag = false;
size_t dataSize;
long xPanels = gF.getXpanelCount();
long yPanels = gF.getYpanelCount();
errFlag = (xPanels <= 0) || errFlag;
errFlag = (yPanels <= 0) || errFlag;
dataSize = (xPanels+1)*(yPanels+1);
errFlag = (fread(gF.getDataPointer(), sizeof(double), dataSize, dataFile) != dataSize) || errFlag;
if(errFlag)
{
throw std::runtime_error("\nValues from SCC::GridFunction2d could not be read from file " + fileName + " \n");
}
}
void appendValuesToBinaryDataFile(const GridFunction2d& gF, FILE* dataFile)
{
long dataSize;
long xPanels = gF.getXpanelCount();
long yPanels = gF.getYpanelCount();
//
// Write ot the function values
//
dataSize = (xPanels+1)*(yPanels+1);
fwrite(gF.getDataPointer(), sizeof(double), dataSize, dataFile);
}
//
// This routine opens up a new file and write GridFunction2d structure and data
// and then closes the file
//
void outputToBinaryDataFile(const GridFunction2d& gF, const std::string& fileName)
{
//
// Open and then write to a file (remember to use the b mode to specify binary!!!!)
//
FILE* dataFile = 0;
if(OPENFILE(dataFile,fileName.c_str(), "wb" ))
{
throw std::runtime_error("\nCannot open " + fileName + " \nFile not found.\n");
}
outputToBinaryDataFile(gF, dataFile);
fclose(dataFile);
}
};
}
#undef OPENFILE
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif