| 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) | 
| 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\trefZ" << endl; | 
| 28 |  | 
| 29 | output << "#time(fs)" << endl; | 
| 30 | output << "#number of z constrain molecules" << endl; | 
| 31 | output << "#global Index of molecule\tzconstrain force" << endl; | 
| 32 | #ifdef IS_MPI | 
| 33 | } | 
| 34 | #endif | 
| 35 |  | 
| 36 | } | 
| 37 |  | 
| 38 | ZConsWriter::~ZConsWriter() | 
| 39 | { | 
| 40 |  | 
| 41 | #ifdef IS_MPI | 
| 42 | if(worldRank == 0 ){ | 
| 43 | #endif | 
| 44 | output.close(); | 
| 45 | #ifdef IS_MPI | 
| 46 | } | 
| 47 | #endif | 
| 48 | } | 
| 49 |  | 
| 50 | void ZConsWriter::writeFZ(double time, int num, int* index, double* fz) | 
| 51 | { | 
| 52 |  | 
| 53 | #ifndef IS_MPI | 
| 54 | vector<pair<int, double> > data; // The space between two ">" is needed. Otherwise, compileer | 
| 55 | // will take it as redirect symbol ">>" | 
| 56 |  | 
| 57 | for(int i = 0; i < num ; i++) | 
| 58 | data.push_back(pair<int, double>(index[i], fz[i])); | 
| 59 |  | 
| 60 | output << time << endl; | 
| 61 | output << num << endl; | 
| 62 |  | 
| 63 | //sort data by index | 
| 64 | sort(data.begin(), data.end()); | 
| 65 |  | 
| 66 | for(int i=0; i < data.size(); i++) | 
| 67 | output << data[i].first << "\t" << data[i].second << endl; | 
| 68 |  | 
| 69 | #else | 
| 70 |  | 
| 71 | //master node will be responsible for receiving, assembling and writing data | 
| 72 | if(worldRank == 0) | 
| 73 | { | 
| 74 |  | 
| 75 | vector<pair<int,double> > data; | 
| 76 | int numProcessors; | 
| 77 | int recvCount; | 
| 78 | int* indexBuf; | 
| 79 | double* fzBuf; | 
| 80 | MPI_Status istatus; | 
| 81 |  | 
| 82 | //process the data in master | 
| 83 | for(int i=0; i < num; i++){ | 
| 84 | data.push_back(pair<int, double>(index[i], fz[i])); | 
| 85 | } | 
| 86 |  | 
| 87 |  | 
| 88 | numProcessors = mpiSim->getNumberProcessors(); | 
| 89 |  | 
| 90 | //acquire the data from other nodes; | 
| 91 | for(int whichNode = 1; whichNode < numProcessors; whichNode++){ | 
| 92 |  | 
| 93 |  | 
| 94 | MPI_Recv(&recvCount, 1, MPI_INT, whichNode, | 
| 95 | 0, MPI_COMM_WORLD, &istatus); | 
| 96 |  | 
| 97 | if(recvCount > 0){ | 
| 98 |  | 
| 99 | indexBuf = new int[recvCount]; | 
| 100 | fzBuf = new double[recvCount]; | 
| 101 |  | 
| 102 | if(!indexBuf || !fzBuf){ | 
| 103 | sprintf(painCave.errMsg, | 
| 104 | "Memory Allocation inside class ZConsWriter\n"); | 
| 105 | painCave.isFatal = 1; | 
| 106 | simError(); | 
| 107 | } | 
| 108 |  | 
| 109 | MPI_Recv(indexBuf, recvCount, MPI_INT, whichNode, | 
| 110 | 0, MPI_COMM_WORLD, &istatus); | 
| 111 |  | 
| 112 | MPI_Recv(fzBuf, recvCount, MPI_DOUBLE_PRECISION, whichNode, | 
| 113 | 0, MPI_COMM_WORLD, &istatus); | 
| 114 |  | 
| 115 | //assemble the data | 
| 116 | for(int i = 0; i < recvCount; i++){ | 
| 117 | data.push_back(pair<int, double>(indexBuf[i], fzBuf[i])); | 
| 118 | } | 
| 119 |  | 
| 120 |  | 
| 121 | delete[] indexBuf; | 
| 122 | delete[] fzBuf; | 
| 123 |  | 
| 124 | } | 
| 125 |  | 
| 126 | } | 
| 127 |  | 
| 128 | // sort the data by index | 
| 129 | sort(data.begin(), data.end()); | 
| 130 |  | 
| 131 | output << time << endl; | 
| 132 | output << data.size() << endl; | 
| 133 |  | 
| 134 | for(int i = 0; i < data.size(); i++){ | 
| 135 |  | 
| 136 | output << data[i].first << "\t" << data[i].second << endl; | 
| 137 | } | 
| 138 |  | 
| 139 | } | 
| 140 | else | 
| 141 | { | 
| 142 | MPI_Send(&num, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); | 
| 143 |  | 
| 144 | if(num > 0){ | 
| 145 | MPI_Send(index, num, MPI_INT, 0, 0, MPI_COMM_WORLD); | 
| 146 | MPI_Send(fz, num, MPI_DOUBLE_PRECISION, 0, 0, MPI_COMM_WORLD); | 
| 147 | } | 
| 148 | } | 
| 149 |  | 
| 150 | #endif | 
| 151 |  | 
| 152 | } | 
| 153 |  | 
| 154 | void ZConsWriter::writeRefZ(const vector<int>& index, const vector<double>& refZ){ | 
| 155 |  | 
| 156 | #ifdef IS_MPI | 
| 157 | if(worldRank == 0){ | 
| 158 | #endif | 
| 159 |  | 
| 160 | output << index.size() << endl; | 
| 161 |  | 
| 162 | for(int i =0 ; i < index.size(); i++) | 
| 163 | output << index[i] << "\t" << refZ[i] << endl; | 
| 164 |  | 
| 165 | #ifdef IS_MPI | 
| 166 | } | 
| 167 | #endif | 
| 168 | } |