-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathioGlobals.cpp
executable file
·141 lines (121 loc) · 4.56 KB
/
ioGlobals.cpp
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
#include "ioGlobals.h"
void CioGlobals::setFilename(string _filename,CglobalVars *g) {
char str[20];
sprintf(str,"Globals%d.dat",g->mpiRank);
filename = _filename+str;
fio.open(filename.c_str());
if (!fio.is_open()) {
cout << "Could not open Global Vars file: "<<filename<<". Create it..."<<endl;
ofstream fo;
fo.open(filename.c_str());
if (!fo.is_open()) {
cout <<"Crap! Still could not open Global Vars file, exiting..."<<endl;
exit(-1);
}
fo <<"# OutStep SPHStep Time linMomX linMomY angMom KE eBForce eElast eElastExact eViscF eViscB eFF eTotal wtTotalStep wtTotalForceCalc wtTotalDddtCalc wtTotalReset wtTotalUpdateDomain wtTotalMPI maxV aveDens varDens aveDensFromMass n nSph maxF maxFF rmsFF edElastdt edViscFdt edFFdt"<<'\n';
fo.close();
}
fio.close();
}
int CioGlobals::myGetLine(CglobalVars *g) {
int currStep = -1;
if (!fio.eof()) {
string line;
getline(fio,line);
istringstream ss(line);
//TODO: 2D specific!!!!
double tmp;
ss >> currStep >>g->sphStep >> g->time >> g->linMom[0]>>g->linMom[1]>>g->angMom>>g->eKE>>g->eBForce>>g->eElast>>g->eElastExact>>g->eViscF>>g->eViscB>>g->eFF>>g->eTotal>>tmp>>tmp>>tmp>>tmp>>tmp>>tmp>>g->maxV>>g->aveDens>>g->varDens>>g->aveDensFromMass>>g->n>>g->nSph>>g->maxF>>g->maxFF>>g->rmsFF>>g->edElastdt>>g->edViscFdt>>g->edFFdt;
}
return currStep;
}
void CioGlobals::myWriteLine(int currStep,CglobalVars *g) {
//TODO: 2D specific!!!!
fio << currStep <<' '<< g->sphStep <<' '<<g->time<<' '<<g->linMom[0]<<' '<<g->linMom[1]<<' '<<g->angMom<<' '<<g->eKE<<' '<<g->eBForce<<' '<<g->eElast<<' '<<g->eElastExact<<' '<<g->eViscF<<' '<<g->eViscB<<' '<<g->eFF<<' '<<g->eTotal<<' '<<g->wtTotalOutStep.tv_sec+g->wtTotalOutStep.tv_usec/1000000.0<<' '<<g->wtTotalForceCalc.tv_sec+g->wtTotalForceCalc.tv_usec/1000000.0<<' '<<g->wtTotalDddtCalc.tv_sec+g->wtTotalDddtCalc.tv_usec/1000000.0<<' '<<g->wtTotalReset.tv_sec+g->wtTotalReset.tv_usec/1000000.0<<' '<<g->wtTotalUpdateDomain.tv_sec+g->wtTotalUpdateDomain.tv_usec/1000000.0<<' '<<g->wtTotalMPI.tv_sec+g->wtTotalMPI.tv_usec/1000000.0<<' '<<g->maxV<<' '<<g->aveDens<<' '<<g->varDens<<' '<<g->aveDensFromMass<<' '<<g->n<<' '<<g->nSph<<' '<<g->maxF<<' '<<g->maxFF<<' '<<g->rmsFF<<' '<<g->edElastdt<<' '<<g->edViscFdt<<' '<<g->edFFdt;
for (int i=0;i<GLOBAL_CUSTOM_BUFFER_SIZE;i++) {
fio <<' '<<g->custom[i];
}
fio << '\n';
}
void CioGlobals::openAndSeekToStep(int outStep,CglobalVars *g) {
fio.open(filename.c_str(),ios::in|ios::out);
if (!fio.is_open() ) {
cout <<"Error: globals file will not open!!!!"<<endl;
exit(-1);
}
string dummy;
getline(fio,dummy);
fio.seekp(fio.tellg());
fio.clear();
if (outStep > 0) {
double currStep = myGetLine(g);
while ((currStep < outStep)&&!fio.eof()) {
currStep = myGetLine(g);
}
if (currStep != outStep) {
fio.seekp(0,ios::end);
fio.clear();
} else {
fio.seekp(fio.tellg());
}
}
}
int CioGlobals::findClosestStepToTime(double time) {
fstream f;
f.open(filename.c_str(),ios::in);
if (!f.is_open() ) {
cout <<"Error: globals file will not open!!!!"<<endl;
exit(-1);
}
string line;
getline(f,line);
getline(f,line);
istringstream ss(line);
int currStep,sphStep;
double thisTime,lastTime;
ss >> currStep >> sphStep >> thisTime;
lastTime = thisTime;
while (thisTime<time) {
lastTime = thisTime;
getline(f,line);
ss.str(line);
ss >> currStep >> sphStep >> thisTime;
}
if (abs(lastTime-time)<abs(thisTime-time)) {
return currStep-1;
} else {
return currStep;
}
}
void CioGlobals::readGlobals(int outStep,CglobalVars *g) {
openAndSeekToStep(outStep,g);
fio.close();
}
void CioGlobals::writeGlobals(int outStep,CglobalVars *g) {
CglobalVars sendBuf = *g;
vector<CglobalVars> recvBuf;
int gSize = sizeof(CglobalVars);
if (g->mpiRank==0) {
recvBuf.resize(g->mpiSize);
}
if (g->mpiSize > 1) {
MPI_Gather(&sendBuf,gSize,MPI_BYTE,&(recvBuf[0]),gSize,MPI_BYTE,0,MPI_COMM_WORLD);
} else if (g->mpiRank==0) {
recvBuf[0] = *g;
}
CglobalVars tmpg;
openAndSeekToStep(outStep-1,&tmpg);
if (g->mpiRank==0) {
CglobalVars sumG;
sumG.init(recvBuf[0]);
for (int i=0;i<g->mpiSize;i++) {
sumG += recvBuf[i];
}
cout << "\tWriting Global Vars with outStep = "<<outStep <<endl;
myWriteLine(outStep,&sumG);
recvBuf.clear();
} else {
myWriteLine(outStep,g);
}
fio.close();
}