ODT
Loading...
Searching...
No Matches
inputoutput.cc
Go to the documentation of this file.
1/*
2 * @file inputoutput.cc
3 * @brief Source file for class \ref inputoutput
4 */
5
6#include "inputoutput.h"
7#include "domain.h"
8#include "processor.h"
9#include <sys/stat.h> // for mkdir
10#include <iostream>
11#include <iomanip>
12#include <fstream>
13#include <sstream>
14#include <cstdlib>
15#include <algorithm> // max_element
16
17extern processor proc;
18
20
26 domn = p_domn;
27 LdoDump = false;
28}
29
31
37inputoutput::inputoutput(const string p_caseName, const int p_nShift){
38
39 caseName = p_caseName;
40 inputFileDir = "../data/"+caseName+"/input/";
41
42 nShift = p_nShift;
43
44 inputFile = YAML::LoadFile(inputFileDir+"input.yaml");
45 params = inputFile["params"];
46 streamProps = inputFile["streamProps"];
47 initParams = inputFile["initParams"];
48 radParams = inputFile["radParams"];
49 dvParams = inputFile["dvParams"];
50 dTimes = inputFile["dumpTimes"];
51 dumpTimesGen= inputFile["dumpTimesGen"];
52 bcCond = inputFile["bcCond"];
53
54 //--------- setup dumpTimes. Either set the dumpTimesGen parameters or the dumpTimes list directly
55 //--------- if dumpTimesGen:dTimeStart is negative, then use the dumpTimes list (if present), otherwise
56 //--------- generate the list of dumptimes from the start, stop, and step parameters
57 if(dumpTimesGen && dumpTimesGen["dTimeStart"].as<double>() >= 0.0){ // compute dumpTimes
58 double DTstart = dumpTimesGen["dTimeStart"].as<double>();
59 double DTend = dumpTimesGen["dTimeEnd"].as<double>();
60 double DTstep = dumpTimesGen["dTimeStep"].as<double>();
61 double tEnd = params["tEnd"].as<double>();
62 for(double t=DTstart; t<=tEnd; t+=DTstep){ // note tEnd not DTend
63 if(t > DTend)
64 break;
65 dumpTimes.push_back(t);
66 }
67 }
68 else
69 for(int i=0; i<dTimes.size(); i++)
70 dumpTimes.push_back(dTimes[i].as<double>());
71
72 dumpTimes.push_back(1.0E200);
73 iNextDumpTime = 0;
74
75 //----------- set the data directory and runtime file
76
77 string fname;
78 stringstream ss1;
79 string s1;
80
81 ss1.clear(); ss1 << setfill('0') << setw(5) << proc.myid + nShift;
82 s1 = ss1.str();
83 dataDir = "../data/"+caseName+"/data/data_" + s1 + "/"; // e.g., "../data_00001", etc.
84
85 int iflag = mkdir(dataDir.c_str(), 0755);
86 if(iflag != 0) {
87 cout << "\n********** Error, process " << proc.myid << "failed to create "
88 << dataDir << ", or it was already there" << endl;
89 exit(0);
90 }
91
92 fname = "../data/"+caseName+"/runtime/runtime_" + s1;
93 ostrm = new ofstream(fname.c_str());
94
95 //----------- set gnuplot file
96
97 fname = dataDir + "plot_odt.gnu";
98 gnufile.open(fname.c_str());
99 if(!gnufile) {
100 cout << endl << "ERROR OPENING FILE: " << dataDir+"plot_odt.gnu" << endl;
101 exit(0);
102 }
103
104}
105
107
111
112 delete ostrm;
113 gnufile.close();
114
115}
116
118
124void inputoutput::outputProperties(const string fname, const double time) {
125
126 string s1;
127 stringstream ss1;
128
129 //--------------------------
130
131 for(int i=0; i<domn->v.size(); i++)
132 domn->v.at(i)->setVar(); // make sure the variable is up to date
133
134 //--------------------------
135
136 ofstream ofile(fname.c_str());
137 if(!ofile) {
138 *ostrm << "\n\n***************** ERROR OPENING FILE " << fname << endl << endl;
139 exit(0);
140 }
141
142 *ostrm << endl << "# Writing outputfile: " << fname;
143 ostrm->flush();
144
145 //--------------------------
146
147 ofile << "# time = " << time;
148
149 ofile << "\n# Grid points = " << domn->ngrd;
150
151 ofile << "\n# Pressure (Pa) = " << domn->pram->pres << endl;
152
153 // HEWSON setting tecplot friendly output
154 if (domn->pram->Ltecplot)
155 ofile << "VARIABLES =";
156 else
157 ofile << "#";
158 for(int i=0,j=1; i<domn->v.size(); i++){
159 if(domn->v.at(i)->L_output){
160 if (domn->pram->Ltecplot)
161 ofile << setw(14) << "\"" << j++ << "_" << domn->v.at(i)->var_name << "\"";
162 else
163 ofile << setw(14) << j++ << "_" << domn->v.at(i)->var_name;
164 }
165 }
166
167 ofile << scientific;
168 ofile << setprecision(10);
169 for(int i=0; i<domn->ngrd; i++) {
170 ofile << endl;
171 for(int k=0; k<domn->v.size(); k++)
172 if(domn->v.at(k)->L_output)
173 ofile << setw(19) << domn->v.at(k)->d.at(i);
174 }
175
176 ofile.close();
177
178}
179
181
186
187 for(int i=0; i<dumpTimes.size(); i++)
188 if(dumpTimes[i] > time) { //set this greater-than dump at the start time
189 iNextDumpTime = i;
190 break;
191 }
192}
193
195
199
200 if(!LdoDump) return;
201
202 stringstream ss;
203 ss << setfill('0') << setw(5) << iNextDumpTime;
204 string fname = dataDir + "dmp_" + ss.str() + ".dat";
205
207
209 LdoDump = false;
210}
211
213
218void inputoutput::writeDataFile(const string fnameRaw, const double time) {
219
220 string fname = dataDir+fnameRaw;
221 outputProperties(fname, time);
222 gnufile << "plot '" << fnameRaw << "' us 1:3; pause -1;" << endl;
223
224}
225
227
230
231 *ostrm << endl << "#--------------------------------------------------"
232 << "--------------------------------------------------------------------";
233 *ostrm << endl;
234 *ostrm << setw(5) << "# EE,"
235 << setw(12) << "time,"
236 << setw(12) << "t-t0,"
237 << setw(10) << "nEtry,"
238 << setw(6) << "ngrd,"
239 << setw(12) << "edSize,"
240 << setw(12) << "edPos,"
241 << setw(12) << "edPa,"
242 << setw(12) << "nEposs,"
243 << setw(12) << "PaAvg,"
244 << setw(12) << "invTauEddy"
245 ;
246
247 *ostrm << endl << "#--------------------------------------------------"
248 << "--------------------------------------------------------------------";
249}
250
252
258
259 double dmb = 0.5*(domn->ed->leftEdge + domn->ed->rightEdge);
260 if(dmb > domn->posf->d.at(domn->ngrd))
261 dmb = dmb-domn->Ldomain();
262
263 *ostrm << scientific << setprecision(3) << endl;
264 *ostrm << setw(5) << domn->solv->neddies // 1: EE
265 << setw(12) << domn->solv->time // 2: time
266 << setw(12) << domn->solv->time-domn->solv->t0 // 3: t-t0
267 << setw(10) << domn->solv->iEtrials // 4: nEtry
268 << setw(6) << domn->ngrd // 5: ngrd
269 << setw(12) << domn->ed->eddySize // 6: edSize
270 << setw(12) << dmb // 7: edPos
271 << setw(12) << domn->ed->Pa // 8: edPa
272 << setw(12) << domn->solv->nPaSumC // 9: nEposs
273 << setw(12) << domn->solv->PaSumC/domn->solv->nPaSumC // 10: PaAvg
274 << setw(12) << domn->ed->invTauEddy // 11: invTauEddy
275 ;
276 ostrm->flush();
277}
278
280
286
287 string fname;
288 stringstream ss1;
289 string s1;
290
291 for(int k=0; k<domn->v.size(); k++) {
292 if(domn->v[k]->L_transported && !domn->v[k]->L_output) {
293 cout << endl << "ERROR: to restart, all transported variables need to be in the restart file" << endl;
294 exit(0);
295 }
296 }
297
298 if(domn->pram->rstType == "multiple") {
299 ss1.clear(); ss1 << setfill('0') << setw(5) << proc.myid;
300 fname = inputFileDir + "restart/restart_" + ss1.str() + ".dat";
301 }
302 else
303 fname = inputFileDir + "/restart.dat";
304
305 ifstream ifile(fname.c_str());
306 if(!ifile) {
307 cout << endl << "ERROR: reading restart file " << fname << endl;
308 exit(0);
309 }
310
311 //------------- Get file header information
312
313 getline(ifile, s1); // read line "# time = 1.1" (this is the restart time
314 ss1.clear();
315 ss1.str(s1);
316 ss1 >> s1 >> s1 >> s1 >> domn->pram->trst;
317
318 getline(ifile, s1); // read line "# Grid points = 100"
319 ss1.clear();
320 ss1.str(s1);
321 ss1 >> s1 >> s1 >> s1 >> s1 >> domn->ngrd;
322 domn->ngrdf = domn->ngrd+1;
323
324 getline(ifile, s1); // read line "# Domain Size = 2" (don't use)
325 getline(ifile, s1); // read line "# Pressure (Pa) = 101325
326 getline(ifile, s1); // read line "# column headers
327
328 //------------- Get file data columns
329
330 for(int k=0; k<domn->v.size(); k++)
331 domn->v[k]->d.resize(domn->ngrd);
332 domn->posf->d.resize(domn->ngrdf);
333
334 for(int i=0; i<domn->ngrd; i++) {
335 for(int k=0; k<domn->v.size(); k++) {
336 if(!domn->v[k]->L_output)
337 continue;
338 ifile >> domn->v[k]->d[i];
339 }
340 }
341
342 domn->posf->d[domn->ngrd] = domn->posf->d[0] + domn->pram->domainLength;
343
344 //------------- Set the variables
345
346 for(int k=0; k<domn->v.size(); k++)
347 domn->v[k]->setVar();
348
349}
350
int ngrd
number of grid cells
Definition domain.h:42
double Ldomain()
Definition domain.cc:156
dv * posf
access as: posf->d[i], or posf->var_name, etc.
Definition domain.h:48
int ngrdf
number of grid cell faces = ngrd+1
Definition domain.h:43
eddy * ed
pointer to object for eddy operations
Definition domain.h:75
vector< dv * > v
All domain variables are stored in here.
Definition domain.h:45
solver * solv
pointer to solver object
Definition domain.h:77
param * pram
pointer to the parameters object
Definition domain.h:73
vector< double > d
the data
Definition dv.h:30
double Pa
eddy acceptance probability
Definition eddy.h:34
double rightEdge
right edge location of eddy
Definition eddy.h:32
double invTauEddy
inverse eddy timescale
Definition eddy.h:33
double eddySize
size of eddy
Definition eddy.h:30
double leftEdge
left edge location of eddy
Definition eddy.h:31
YAML::Node bcCond
yaml sub node
Definition inputoutput.h:47
void set_iNextDumpTime(double time)
int iNextDumpTime
index of next dump time
Definition inputoutput.h:50
int nShift
for file naming
Definition inputoutput.h:55
YAML::Node params
yaml sub node
Definition inputoutput.h:40
string inputFileDir
input file directory
Definition inputoutput.h:36
YAML::Node inputFile
yaml input file object base node
Definition inputoutput.h:39
YAML::Node radParams
yaml sub node
Definition inputoutput.h:43
bool LdoDump
flag for whether we are dumping a file
Definition inputoutput.h:51
void loadVarsFromRestartFile()
YAML::Node dvParams
yaml sub node
Definition inputoutput.h:44
inputoutput(const string p_inputFileDir, const int nShift)
string dataDir
data directory (output)
Definition inputoutput.h:37
YAML::Node streamProps
yaml sub node
Definition inputoutput.h:41
void outputProgress()
output data going with the header info
domain * domn
pointer to domain object
Definition inputoutput.h:31
ostream * ostrm
Runtime: points to cout or to a file.
Definition inputoutput.h:33
void dumpDomainIfNeeded()
calls outputProperties for dumpTimes
void init(domain *p_domn)
string caseName
input file directory
Definition inputoutput.h:35
ofstream gnufile
gnuplot script file
Definition inputoutput.h:53
YAML::Node initParams
yaml sub node
Definition inputoutput.h:42
void outputHeader()
output header info during odt solution
vector< double > dumpTimes
vector of dump times
Definition inputoutput.h:49
YAML::Node dTimes
yaml sub node
Definition inputoutput.h:45
void outputProperties(const string fname, const double time)
actually write the data file
void writeDataFile(const string fnameRaw, const double time)
writes the gnuplot file and calls outputProperties
YAML::Node dumpTimesGen
yaml sub node
Definition inputoutput.h:46
double domainLength
length of domain (m)
Definition param.h:34
double trst
restart time (from restart file), default is 0.0;
Definition param.h:108
bool Ltecplot
set TRUE for tecplot friendly output
Definition param.h:89
string rstType
"single" or "multiple"
Definition param.h:107
double pres
initial pressure (Pa)
Definition param.h:40
int myid
Process ID.
Definition processor.h:32
double PaSumC
sum of Pa of eddies
Definition solver.h:44
int neddies
number of eddies accepted
Definition solver.h:43
double t0
time of last eddy event; diffusion left off here.
Definition solver.h:34
double time
odt time (during sampling)
Definition solver.h:33
int iEtrials
number of eddy trials
Definition solver.h:39
int nPaSumC
number going into PaSum
Definition solver.h:45
processor proc
Definition main.cc:28
Header file for class domain.
processor proc
Definition main.cc:28
Header file for class inputoutput.
Header file for class processor.