1 |
#include <algorithm> |
2 |
#include <iostream> |
3 |
#include <vector> |
4 |
//#include <pair> |
5 |
#include "ZConsWriter.hpp" |
6 |
#include "simError.h" |
7 |
|
8 |
using namespace std; |
9 |
|
10 |
ZConsWriter::ZConsWriter(const char* filename, vector<ZConsParaItem>* thePara) |
11 |
{ |
12 |
//use master - slave mode, only master node writes to disk |
13 |
#ifdef IS_MPI |
14 |
if(worldRank == 0){ |
15 |
#endif |
16 |
|
17 |
output.open(filename); |
18 |
|
19 |
if(!output){ |
20 |
sprintf( painCave.errMsg, |
21 |
"Could not open \"s\" for z constrain output \n", |
22 |
filename); |
23 |
painCave.isFatal = 1; |
24 |
simError(); |
25 |
} |
26 |
output << "#number of z constrain molecules" << endl; |
27 |
output << "#global Index of molecule\tzPos" << endl; |
28 |
|
29 |
parameters = thePara; |
30 |
writeZPos(); |
31 |
|
32 |
output << "#time(fs)" << endl; |
33 |
output << "#number of fixed z-constrain molecules" << endl; |
34 |
output << "#global Index of molecule\tzconstrain force\tcurrentZPos" << endl; |
35 |
|
36 |
|
37 |
#ifdef IS_MPI |
38 |
} |
39 |
#endif |
40 |
|
41 |
} |
42 |
|
43 |
ZConsWriter::~ZConsWriter() |
44 |
{ |
45 |
|
46 |
#ifdef IS_MPI |
47 |
if(worldRank == 0 ){ |
48 |
#endif |
49 |
output.close(); |
50 |
#ifdef IS_MPI |
51 |
} |
52 |
#endif |
53 |
} |
54 |
|
55 |
void ZConsWriter::writeFZ(double time, int num, int* index, double* fz, double* curZPos) |
56 |
{ |
57 |
|
58 |
#ifndef IS_MPI |
59 |
output << time << endl; |
60 |
output << num << endl; |
61 |
|
62 |
for(int i = 0; i < num; i++) |
63 |
output << index[i] <<"\t" << fz[i] << "\t" << curZPos[i] << endl; |
64 |
|
65 |
#else |
66 |
|
67 |
int whichNode; |
68 |
enum CommType { RequesPosAndForce, EndOfRequest} status; |
69 |
double pos; |
70 |
double force; |
71 |
int localIndex; |
72 |
MPI_Status ierr; |
73 |
int tag = 0; |
74 |
|
75 |
if(worldRank == 0){ |
76 |
|
77 |
int globalIndexOfCurMol; |
78 |
int *MolToProcMap; |
79 |
MolToProcMap = mpiSim->getMolToProcMap(); |
80 |
|
81 |
for(int i = 0; i < parameters->size(); i++){ |
82 |
|
83 |
globalIndexOfCurMol = (*parameters)[i].zconsIndex; |
84 |
whichNode = MolToProcMap[globalIndexOfCurMol]; |
85 |
|
86 |
if(whichNode == 0){ |
87 |
|
88 |
for(int j = 0; j < num; j++) |
89 |
if(index[j] == globalIndexOfCurMol){ |
90 |
localIndex = j; |
91 |
break; |
92 |
} |
93 |
|
94 |
force = fz[localIndex]; |
95 |
pos = curZPos[localIndex]; |
96 |
|
97 |
} |
98 |
else{ |
99 |
status = RequesPosAndForce; |
100 |
MPI_Send(&status, 1, MPI_INT, whichNode, tag, MPI_COMM_WORLD); |
101 |
MPI_Send(&globalIndexOfCurMol, 1, MPI_INT, whichNode, tag, MPI_COMM_WORLD); |
102 |
MPI_Recv(&force, 1, MPI_DOUBLE_PRECISION, whichNode, tag, MPI_COMM_WORLD, &ierr); |
103 |
MPI_Recv(&pos, 1, MPI_DOUBLE_PRECISION, whichNode, tag, MPI_COMM_WORLD, &ierr); |
104 |
} |
105 |
|
106 |
output << globalIndexOfCurMol << "\t" << force << "\t" << pos << endl; |
107 |
|
108 |
} //End of Request Loop |
109 |
|
110 |
//Send ending request message to slave nodes |
111 |
status = EndOfRequest; |
112 |
for(int i =1; i < mpiSim->getNumberProcessors(); i++) |
113 |
MPI_Send(&status, 1, MPI_INT, i, tag, MPI_COMM_WORLD); |
114 |
|
115 |
} |
116 |
else{ |
117 |
|
118 |
int whichMol; |
119 |
bool done = false; |
120 |
|
121 |
while (!done){ |
122 |
|
123 |
MPI_Recv(&status, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &ierr); |
124 |
|
125 |
switch (status){ |
126 |
|
127 |
case RequesPosAndForce : |
128 |
|
129 |
MPI_Recv(&whichMol, 1, MPI_INT, 0, tag, MPI_COMM_WORLD,&ierr); |
130 |
|
131 |
for(int i = 0; i < num; i++) |
132 |
if(index[i] == whichMol){ |
133 |
localIndex = i; |
134 |
break; |
135 |
} |
136 |
|
137 |
MPI_Send(&fz[localIndex], 1, MPI_DOUBLE_PRECISION, 0, tag, MPI_COMM_WORLD); |
138 |
MPI_Send(&curZPos[localIndex], 1, MPI_DOUBLE_PRECISION, 0, tag, MPI_COMM_WORLD); |
139 |
break; |
140 |
|
141 |
case EndOfRequest : |
142 |
|
143 |
done = true; |
144 |
break; |
145 |
} |
146 |
|
147 |
} |
148 |
|
149 |
} |
150 |
|
151 |
#endif |
152 |
|
153 |
} |
154 |
|
155 |
void ZConsWriter::writeZPos(){ |
156 |
|
157 |
#ifdef IS_MPI |
158 |
if(worldRank == 0){ |
159 |
#endif |
160 |
|
161 |
output << parameters->size() << endl; |
162 |
|
163 |
for(int i =0 ; i < parameters->size(); i++) |
164 |
output << (*parameters)[i].zconsIndex << "\t" << (*parameters)[i].zPos << endl; |
165 |
|
166 |
#ifdef IS_MPI |
167 |
} |
168 |
#endif |
169 |
} |