-
Notifications
You must be signed in to change notification settings - Fork 0
/
phReadWrite.cc
159 lines (124 loc) · 4.09 KB
/
phReadWrite.cc
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
#include "phReadWrite.h"
#include "phastaIO.h"
#include <string.h>
#include <strings.h>
#include <iostream>
#include <stdio.h>
using namespace std;
// to read parameters from a phasta file (filename)
// parameters correspond to nshg & nvar, i.e., size of field-array
// these parameters are used as reference values
// (sometimes needed before reading the field-array)
void readParametersFromFile(char *filename,
char *fieldName,
int &nshg,
int &numVars) {
// read file (i.e., restart, error etc.)
// fileDescriptor
int restart;
// format of the file
char* iformat = "binary";
openfile_( filename, "read", &restart );
// contains: nshg,numVars,lstep
int iarray[4];
// don't know what is this for
// think it loops over token `<>'
// inside phastaIO.cc in readHeader(...)
int isize = 3;
readheader_( &restart, fieldName, iarray,
&isize, "double", iformat );
// nshg * numVars
nshg=iarray[0];
numVars=iarray[1];
closefile_(&restart, "read");
}
// to read array from a phasta file (filename)
// memory is allocated HERE for 'valueArray'
// `fieldName' tells which block to read like solution, error etc.
void readArrayFromFile( char *filename,
char *fieldName,
double *&valueArray) {
// read file (i.e., restart, error etc.)
// fileDescriptor
int restart;
// format of the file
char* iformat = "binary";
openfile_( filename, "read", &restart );
// contains: nshg,numVars,lstep
int iarray[4];
// don't know what is this for
// think it loops over token `<>'
// inside phastaIO.cc in readHeader(...)
int isize = 3;
readheader_( &restart, fieldName, iarray,
&isize, "double", iformat );
// nshg * numVars
int nshg=iarray[0];
int numVars=iarray[1];
isize = iarray[0]*iarray[1];
double* q = new double[nshg*numVars];
readdatablock_( &restart, fieldName, q, &isize,
"double" , iformat );
valueArray = new double[nshg*numVars];
for(int i = 0; i< nshg; i++){
for( int j=0; j< numVars; j++){
valueArray[i*numVars+j] = q[j*nshg+i];
}
}
delete [] q;
closefile_(&restart, "read");
}
// to write array to a phasta file (filename)
// NOTE: array should be transposed!!!
// `fieldName' tells in which block to write like solution, error etc.
// `outputFormat' tells in which format to write, i.e., binary/ascii
// `mode' : "write", "append" etc.
void writeArrayToFile( char *filename,
char *fieldName,
char *outputFormat,
char *mode,
int nshg,
int numVars,
int stepNumber,
double *valueArray) {
int restart;
char fname[256];
int iarray[10];
int size, nitems;
openfile_( filename, mode, &restart );
if(!strcmp(mode,"write")) {
writestring_( &restart,"# PHASTA Input File Version 2.0\n");
writestring_( &restart, "# Byte Order Magic Number : 362436 \n");
sprintf(fname,"# Output generated by phParAdapt version: \n");
writestring_( &restart, fname );
time_t timenow = time ( &timenow);
sprintf(fname,"# %s\n", ctime( &timenow ));
writestring_( &restart, fname );
size = 1;
nitems = 1;
iarray[0] = 1;
int magic_number = 362436;
int* mptr = &magic_number;
writeheader_( &restart, "byteorder magic number ",
(void*)iarray, &nitems, &size, "integer", outputFormat );
writedatablock_( &restart, "byteorder magic number ",
(void*)mptr, &nitems, "integer", outputFormat );
bzero( (void*)fname, 256 );
sprintf(fname,"number of modes : < 0 > %d\n", nshg);
writestring_( &restart, fname );
bzero( (void*)fname, 256 );
sprintf(fname,"number of variables : < 0 > %d\n", numVars);
writestring_( &restart, fname );
}
size = nshg*numVars;
nitems = 3; // length of array
iarray[0] = nshg;
iarray[1] = numVars;
iarray[2] = stepNumber;
writeheader_( &restart, fieldName,
( void* )iarray, &nitems, &size,"double", outputFormat );
nitems = nshg*numVars; // length of array
writedatablock_( &restart, fieldName,
( void* )(valueArray), &nitems, "double", outputFormat );
closefile_( &restart, mode);
}