1 |
tim |
658 |
#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 |
tim |
699 |
ZConsWriter::ZConsWriter(const char* filename, vector<ZConsParaItem>* thePara) |
11 |
tim |
658 |
{ |
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 |
tim |
699 |
output << "#global Index of molecule\tzPos" << endl; |
28 |
tim |
658 |
|
29 |
tim |
699 |
parameters = thePara; |
30 |
|
|
writeZPos(); |
31 |
|
|
|
32 |
tim |
658 |
output << "#time(fs)" << endl; |
33 |
tim |
699 |
output << "#number of fixed z-constrain molecules" << endl; |
34 |
|
|
output << "#global Index of molecule\tzconstrain force\tcurrentZPos" << endl; |
35 |
|
|
|
36 |
|
|
|
37 |
tim |
658 |
#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 |
tim |
699 |
void ZConsWriter::writeFZ(double time, int num, int* index, double* fz, double* curZPos) |
56 |
tim |
658 |
{ |
57 |
|
|
|
58 |
|
|
#ifndef IS_MPI |
59 |
|
|
output << time << endl; |
60 |
|
|
output << num << endl; |
61 |
tim |
699 |
|
62 |
|
|
for(int i = 0; i < num; i++) |
63 |
|
|
output << index[i] <<"\t" << fz[i] << "\t" << curZPos[i] << endl; |
64 |
tim |
658 |
|
65 |
tim |
699 |
#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 |
tim |
658 |
|
75 |
tim |
699 |
if(worldRank == 0){ |
76 |
tim |
658 |
|
77 |
tim |
699 |
int globalIndexOfCurMol; |
78 |
|
|
int *MolToProcMap; |
79 |
|
|
MolToProcMap = mpiSim->getMolToProcMap(); |
80 |
tim |
658 |
|
81 |
tim |
699 |
for(int i = 0; i < parameters->size(); i++){ |
82 |
tim |
658 |
|
83 |
tim |
699 |
globalIndexOfCurMol = (*parameters)[i].zconsIndex; |
84 |
|
|
whichNode = MolToProcMap[globalIndexOfCurMol]; |
85 |
tim |
658 |
|
86 |
tim |
699 |
if(whichNode == 0){ |
87 |
tim |
658 |
|
88 |
tim |
699 |
for(int j = 0; j < num; j++) |
89 |
|
|
if(index[j] == globalIndexOfCurMol){ |
90 |
|
|
localIndex = j; |
91 |
|
|
break; |
92 |
|
|
} |
93 |
tim |
658 |
|
94 |
tim |
699 |
force = fz[localIndex]; |
95 |
|
|
pos = curZPos[localIndex]; |
96 |
tim |
658 |
|
97 |
tim |
699 |
} |
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 |
tim |
658 |
|
106 |
tim |
699 |
output << globalIndexOfCurMol << "\t" << force << "\t" << pos << endl; |
107 |
|
|
|
108 |
|
|
} //End of Request Loop |
109 |
tim |
658 |
|
110 |
tim |
699 |
//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 |
tim |
658 |
|
125 |
tim |
699 |
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 |
tim |
658 |
} |
148 |
tim |
699 |
|
149 |
tim |
658 |
} |
150 |
|
|
|
151 |
|
|
#endif |
152 |
|
|
|
153 |
|
|
} |
154 |
|
|
|
155 |
tim |
699 |
void ZConsWriter::writeZPos(){ |
156 |
tim |
658 |
|
157 |
|
|
#ifdef IS_MPI |
158 |
|
|
if(worldRank == 0){ |
159 |
|
|
#endif |
160 |
|
|
|
161 |
tim |
699 |
output << parameters->size() << endl; |
162 |
tim |
658 |
|
163 |
tim |
699 |
for(int i =0 ; i < parameters->size(); i++) |
164 |
|
|
output << (*parameters)[i].zconsIndex << "\t" << (*parameters)[i].zPos << endl; |
165 |
tim |
658 |
|
166 |
|
|
#ifdef IS_MPI |
167 |
|
|
} |
168 |
|
|
#endif |
169 |
|
|
} |