| 1 |  | /* | 
| 2 | < | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 2 | > | * Copyright (c) 2009 The University of Notre Dame. All Rights Reserved. | 
| 3 |  | * | 
| 4 |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  | * non-exclusive, royalty free, license to use, modify and | 
| 6 |  | * redistribute this software in source and binary code form, provided | 
| 7 |  | * that the following conditions are met: | 
| 8 |  | * | 
| 9 | < | * 1. Acknowledgement of the program authors must be made in any | 
| 10 | < | *    publication of scientific results based in part on use of the | 
| 11 | < | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | < | *    the article in which the program was described (Matthew | 
| 13 | < | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | < | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | < | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | < | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | < | * | 
| 18 | < | * 2. Redistributions of source code must retain the above copyright | 
| 9 | > | * 1. Redistributions of source code must retain the above copyright | 
| 10 |  | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  | * | 
| 12 | < | * 3. Redistributions in binary form must reproduce the above copyright | 
| 12 | > | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 |  | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  | *    documentation and/or other materials provided with the | 
| 15 |  | *    distribution. | 
| 28 |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  | * such damages. | 
| 31 | + | * | 
| 32 | + | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | + | * research, please cite the appropriate papers when you publish your | 
| 34 | + | * work.  Good starting points are: | 
| 35 | + | * | 
| 36 | + | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | + | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | + | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | + | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | + | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 |  | */ | 
| 42 |  |  | 
| 43 | + | #ifdef IS_MPI | 
| 44 | + | #include <mpi.h> | 
| 45 | + | #endif | 
| 46 | + |  | 
| 47 | + | #include <string> | 
| 48 | + | #include <sstream> | 
| 49 | + | #include <iostream> | 
| 50 | + |  | 
| 51 |  | #include "io/RestWriter.hpp" | 
| 43 | – | #include "primitives/Molecule.hpp" | 
| 52 |  | #include "utils/simError.h" | 
| 53 | < | #include "io/basic_teebuf.hpp" | 
| 53 | > | #include "brains/SnapshotManager.hpp" | 
| 54 |  |  | 
| 55 | + | namespace OpenMD { | 
| 56 | + | RestWriter::RestWriter(SimInfo* info, const std::string& filename, | 
| 57 | + | std::vector<Restraint*> restraints ) : | 
| 58 | + | info_(info){ | 
| 59 | + | createRestFile_ = true; | 
| 60 | + |  | 
| 61 |  | #ifdef IS_MPI | 
| 62 | < | #include <mpi.h> | 
| 63 | < | #define TAKE_THIS_TAG_INT 1 | 
| 50 | < | #define TAKE_THIS_TAG_REAL 2 | 
| 51 | < | #endif //is_mpi | 
| 52 | < |  | 
| 53 | < | namespace oopse { | 
| 54 | < | RestWriter::RestWriter(SimInfo* info) : | 
| 55 | < | info_(info), outName_(info_->getRestFileName()) { | 
| 56 | < | } | 
| 62 | > | if(worldRank == 0){ | 
| 63 | > | #endif | 
| 64 |  |  | 
| 65 | < | RestWriter::~RestWriter() {} | 
| 66 | < |  | 
| 67 | < | void RestWriter::writeZAngFile() { | 
| 68 | < | std::ostream* zangStream; | 
| 69 | < |  | 
| 65 | > | output_ = new std::ofstream(filename.c_str()); | 
| 66 | > |  | 
| 67 | > | if(!output_){ | 
| 68 | > | sprintf( painCave.errMsg, | 
| 69 | > | "Could not open %s for restraint output.\n", | 
| 70 | > | filename.c_str()); | 
| 71 | > | painCave.isFatal = 1; | 
| 72 | > | simError(); | 
| 73 | > | } | 
| 74 | > |  | 
| 75 |  | #ifdef IS_MPI | 
| 64 | – | if (worldRank == 0) { | 
| 65 | – | #endif // is_mpi | 
| 66 | – |  | 
| 67 | – | zangStream = new std::ofstream(outName_.c_str()); | 
| 68 | – |  | 
| 69 | – | #ifdef IS_MPI | 
| 76 |  | } | 
| 71 | – | #endif // is_mpi | 
| 72 | – |  | 
| 73 | – | writeZangle(*zangStream); | 
| 74 | – |  | 
| 75 | – | #ifdef IS_MPI | 
| 76 | – | if (worldRank == 0) { | 
| 77 |  | #endif // is_mpi | 
| 78 | < | delete zangStream; | 
| 79 | < |  | 
| 78 | > |  | 
| 79 | > |  | 
| 80 |  | #ifdef IS_MPI | 
| 81 | < | } | 
| 82 | < | #endif // is_mpi | 
| 81 | > | MPI::Status istatus; | 
| 82 | > | #endif | 
| 83 |  |  | 
| 84 | < | } | 
| 84 | > | #ifndef IS_MPI | 
| 85 | > |  | 
| 86 | > | (*output_) << "#time\t"; | 
| 87 |  |  | 
| 88 | < | void RestWriter::writeZangle(std::ostream& finalOut){ | 
| 89 | < | const int BUFFERSIZE = 2000; | 
| 90 | < | char tempBuffer[BUFFERSIZE]; | 
| 91 | < | char writeLine[BUFFERSIZE]; | 
| 88 | > | std::vector<Restraint*>::const_iterator resti; | 
| 89 | > |  | 
| 90 | > | for(resti=restraints.begin(); resti != restraints.end(); ++resti){ | 
| 91 | > | if ((*resti)->getPrintRestraint()) { | 
| 92 | > |  | 
| 93 | > | std::string myName = (*resti)->getRestraintName(); | 
| 94 | > | int myType = (*resti)->getRestraintType(); | 
| 95 | > |  | 
| 96 | > | (*output_) << myName << ":"; | 
| 97 | > |  | 
| 98 | > | if (myType & Restraint::rtDisplacement) | 
| 99 | > | (*output_) << "\tPosition(angstroms)\tEnergy(kcal/mol)"; | 
| 100 | > |  | 
| 101 | > | if (myType & Restraint::rtTwist) | 
| 102 | > | (*output_) << "\tTwistAngle(radians)\tEnergy(kcal/mol)"; | 
| 103 | > |  | 
| 104 | > | if (myType & Restraint::rtSwingX) | 
| 105 | > | (*output_) << "\tSwingXAngle(radians)\tEnergy(kcal/mol)"; | 
| 106 | > |  | 
| 107 | > | if (myType & Restraint::rtSwingY) | 
| 108 | > | (*output_) << "\tSwingYAngle(radians)\tEnergy(kcal/mol)"; | 
| 109 | > | } | 
| 110 | > | } | 
| 111 | > |  | 
| 112 | > | (*output_) << "\n"; | 
| 113 | > | (*output_).flush(); | 
| 114 |  |  | 
| 115 | < | Molecule* mol; | 
| 92 | < | StuntDouble* integrableObject; | 
| 93 | < | SimInfo::MoleculeIterator mi; | 
| 94 | < | Molecule::IntegrableObjectIterator ii; | 
| 115 | > | #else | 
| 116 |  |  | 
| 117 | < | #ifndef IS_MPI | 
| 118 | < | // first we do output for the single processor version | 
| 119 | < | finalOut | 
| 120 | < | << info_->getSnapshotManager()->getCurrentSnapshot()->getTime() | 
| 121 | < | << " : omega values at this time\n"; | 
| 122 | < |  | 
| 102 | < | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 103 | < | mol = info_->nextMolecule(mi)) { | 
| 104 | < |  | 
| 105 | < | for (integrableObject = mol->beginIntegrableObject(ii); | 
| 106 | < | integrableObject != NULL; | 
| 107 | < | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 117 | > | std::string buffer; | 
| 118 | > |  | 
| 119 | > | std::vector<Restraint*>::const_iterator resti; | 
| 120 | > |  | 
| 121 | > | for(resti=restraints.begin(); resti != restraints.end(); ++resti){ | 
| 122 | > | if ((*resti)->getPrintRestraint()) { | 
| 123 |  |  | 
| 124 | < | sprintf( tempBuffer, | 
| 125 | < | "%14.10lf\n", | 
| 111 | < | integrableObject->getZangle()); | 
| 112 | < | strcpy( writeLine, tempBuffer ); | 
| 113 | < |  | 
| 114 | < | finalOut << writeLine; | 
| 124 | > | std::string myName = (*resti)->getRestraintName(); | 
| 125 | > | int myType = (*resti)->getRestraintType(); | 
| 126 |  |  | 
| 127 | + | buffer += (myName + ":"); | 
| 128 | + |  | 
| 129 | + | if (myType & Restraint::rtDisplacement) | 
| 130 | + | buffer += "\tPosition(angstroms)\tEnergy(kcal/mol)"; | 
| 131 | + |  | 
| 132 | + | if (myType & Restraint::rtTwist) | 
| 133 | + | buffer += "\tTwistAngle(radians)\tEnergy(kcal/mol)"; | 
| 134 | + |  | 
| 135 | + | if (myType & Restraint::rtSwingX) | 
| 136 | + | buffer += "\tSwingXAngle(radians)\tEnergy(kcal/mol)"; | 
| 137 | + |  | 
| 138 | + | if (myType & Restraint::rtSwingY) | 
| 139 | + | buffer += "\tSwingYAngle(radians)\tEnergy(kcal/mol)"; | 
| 140 | + |  | 
| 141 | + | buffer += "\n"; | 
| 142 |  | } | 
| 143 |  | } | 
| 144 |  |  | 
| 119 | – | #else | 
| 120 | – | int nproc; | 
| 121 | – | MPI_Comm_size(MPI_COMM_WORLD, &nproc); | 
| 145 |  | const int masterNode = 0; | 
| 146 |  |  | 
| 124 | – | MPI_Status ierr; | 
| 125 | – | int intObIndex; | 
| 126 | – | int vecLength; | 
| 127 | – | RealType zAngle; | 
| 128 | – | std::vector<int> gIndex; | 
| 129 | – | std::vector<RealType> zValues; | 
| 130 | – |  | 
| 147 |  | if (worldRank == masterNode) { | 
| 148 | < | std::map<int, RealType> zAngData; | 
| 149 | < | for(int i = 0 ; i < nproc; ++i) { | 
| 150 | < | if (i == masterNode) { | 
| 151 | < | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 152 | < | mol = info_->nextMolecule(mi)) { | 
| 153 | < |  | 
| 154 | < | for (integrableObject = mol->beginIntegrableObject(ii); | 
| 155 | < | integrableObject != NULL; | 
| 156 | < | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 157 | < |  | 
| 158 | < | intObIndex = integrableObject->getGlobalIntegrableObjectIndex(); | 
| 159 | < |  | 
| 160 | < | zAngle = integrableObject->getZangle(); | 
| 145 | < | zAngData.insert(std::pair<int, RealType>(intObIndex, zAngle)); | 
| 146 | < | } | 
| 147 | < | } | 
| 148 | > | (*output_) << "#time\t"; | 
| 149 | > | (*output_) << buffer; | 
| 150 | > |  | 
| 151 | > | int nProc = MPI::COMM_WORLD.Get_size(); | 
| 152 | > | for (int i = 1; i < nProc; ++i) { | 
| 153 | > |  | 
| 154 | > | // receive the length of the string buffer that was | 
| 155 | > | // prepared by processor i | 
| 156 | > |  | 
| 157 | > | int recvLength; | 
| 158 | > | MPI::COMM_WORLD.Recv(&recvLength, 1, MPI::INT, i, 0, istatus); | 
| 159 | > | char* recvBuffer = new char[recvLength]; | 
| 160 | > | if (recvBuffer == NULL) { | 
| 161 |  | } else { | 
| 162 | < | MPI_Recv(&vecLength, 1, MPI_INT, i, | 
| 163 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &ierr); | 
| 164 | < | // make sure the vectors are the right size for the incoming data | 
| 165 | < | gIndex.resize(vecLength); | 
| 153 | < | zValues.resize(vecLength); | 
| 154 | < |  | 
| 155 | < | MPI_Recv(&gIndex[0], vecLength, MPI_INT, i, | 
| 156 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &ierr); | 
| 157 | < | MPI_Recv(&zValues[0], vecLength, MPI_REALTYPE, i, | 
| 158 | < | TAKE_THIS_TAG_REAL, MPI_COMM_WORLD, &ierr); | 
| 159 | < |  | 
| 160 | < | for (int k = 0; k < vecLength; k++){ | 
| 161 | < | zAngData.insert(std::pair<int, RealType>(gIndex[k], zValues[k])); | 
| 162 | < | } | 
| 163 | < | gIndex.clear(); | 
| 164 | < | zValues.clear(); | 
| 162 | > | MPI::COMM_WORLD.Recv(recvBuffer, recvLength, MPI::CHAR, i, 0, | 
| 163 | > | istatus); | 
| 164 | > | (*output_) << recvBuffer; | 
| 165 | > | delete [] recvBuffer; | 
| 166 |  | } | 
| 167 | + | } | 
| 168 | + | (*output_).flush(); | 
| 169 | + | } else { | 
| 170 | + | int sendBufferLength = buffer.size() + 1; | 
| 171 | + | MPI::COMM_WORLD.Send(&sendBufferLength, 1, MPI::INT, masterNode, 0); | 
| 172 | + | MPI::COMM_WORLD.Send((void *)buffer.c_str(), sendBufferLength, MPI::CHAR, | 
| 173 | + | masterNode, 0); | 
| 174 | + | } | 
| 175 | + |  | 
| 176 | + | #endif // is_mpi | 
| 177 | + |  | 
| 178 | + | } | 
| 179 | + |  | 
| 180 | + | void RestWriter::writeRest(std::vector<std::map<int, Restraint::RealPair> > restInfo) { | 
| 181 | + |  | 
| 182 | + | #ifdef IS_MPI | 
| 183 | + | MPI::Status istatus; | 
| 184 | + | #endif | 
| 185 | + |  | 
| 186 | + | #ifndef IS_MPI | 
| 187 | + | (*output_) << info_->getSnapshotManager()->getCurrentSnapshot()->getTime(); | 
| 188 | + |  | 
| 189 | + | // output some information about the molecules | 
| 190 | + | std::vector<std::map<int, Restraint::RealPair> >::const_iterator i; | 
| 191 | + | std::map<int, Restraint::RealPair>::const_iterator j; | 
| 192 | + |  | 
| 193 | + | for( i = restInfo.begin(); i != restInfo.end(); ++i){ | 
| 194 | + | for(j = (*i).begin(); j != (*i).end(); ++j){ | 
| 195 | + | (*output_) << "\t" << (j->second).first << "\t" << (j->second).second; | 
| 196 |  | } | 
| 197 | < |  | 
| 198 | < | finalOut << info_->getSnapshotManager()->getCurrentSnapshot()->getTime() | 
| 199 | < | << " : omega values at this time\n"; | 
| 200 | < |  | 
| 201 | < | std::map<int, RealType>::iterator l; | 
| 202 | < | for (l = zAngData.begin(); l != zAngData.end(); ++l) { | 
| 203 | < |  | 
| 204 | < | sprintf( tempBuffer, | 
| 205 | < | "%14.10lf\n", | 
| 206 | < | l->second); | 
| 207 | < | strcpy( writeLine, tempBuffer ); | 
| 208 | < |  | 
| 209 | < | finalOut << writeLine; | 
| 197 | > | (*output_) << std::endl; | 
| 198 | > | } | 
| 199 | > | (*output_).flush(); | 
| 200 | > | #else | 
| 201 | > | std::string buffer, first, second; | 
| 202 | > | std::stringstream ss; | 
| 203 | > |  | 
| 204 | > | std::vector<std::map<int, Restraint::RealPair> >::const_iterator i; | 
| 205 | > | std::map<int, Restraint::RealPair>::const_iterator j; | 
| 206 | > |  | 
| 207 | > | for( i = restInfo.begin(); i != restInfo.end(); ++i){ | 
| 208 | > | for(j = (*i).begin(); j != (*i).end(); ++j){ | 
| 209 | > | ss.clear(); | 
| 210 | > | ss << (j->second).first; | 
| 211 | > | ss >> first; | 
| 212 | > | ss.clear(); | 
| 213 | > | ss << (j->second).second; | 
| 214 | > | ss >> second; | 
| 215 | > | buffer += ("\t" + first + "\t" + second); | 
| 216 |  | } | 
| 217 | + | buffer += "\n"; | 
| 218 | + | } | 
| 219 | + |  | 
| 220 | + | const int masterNode = 0; | 
| 221 | + |  | 
| 222 | + | if (worldRank == masterNode) { | 
| 223 | + | (*output_) << info_->getSnapshotManager()->getCurrentSnapshot()->getTime(); | 
| 224 | + | (*output_) << buffer; | 
| 225 |  |  | 
| 226 | + | int nProc; | 
| 227 | + | nProc = MPI::COMM_WORLD.Get_size(); | 
| 228 | + | for (int i = 1; i < nProc; ++i) { | 
| 229 | + |  | 
| 230 | + | // receive the length of the string buffer that was | 
| 231 | + | // prepared by processor i | 
| 232 | + |  | 
| 233 | + | int recvLength; | 
| 234 | + | MPI::COMM_WORLD.Recv(&recvLength, 1, MPI::INT, i, 0, istatus); | 
| 235 | + | char* recvBuffer = new char[recvLength]; | 
| 236 | + | if (recvBuffer == NULL) { | 
| 237 | + | } else { | 
| 238 | + | MPI::COMM_WORLD.Recv(recvBuffer, recvLength, MPI::CHAR, i, 0, | 
| 239 | + | istatus); | 
| 240 | + | (*output_) << recvBuffer; | 
| 241 | + |  | 
| 242 | + | delete [] recvBuffer; | 
| 243 | + | } | 
| 244 | + | } | 
| 245 | + | (*output_).flush(); | 
| 246 |  | } else { | 
| 247 | < | // pack up and send the appropriate info to the master node | 
| 248 | < | for(int j = 1; j < nproc; ++j) { | 
| 249 | < | if (worldRank == j) { | 
| 250 | < | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 251 | < | mol = info_->nextMolecule(mi)) { | 
| 252 | < |  | 
| 253 | < | for (integrableObject = mol->beginIntegrableObject(ii); | 
| 254 | < | integrableObject != NULL; | 
| 255 | < | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 256 | < |  | 
| 257 | < | // build a vector of the indicies | 
| 258 | < | intObIndex = integrableObject->getGlobalIntegrableObjectIndex(); | 
| 259 | < | gIndex.push_back(intObIndex); | 
| 260 | < |  | 
| 261 | < | // build a vector of the zAngle values | 
| 262 | < | zAngle = integrableObject->getZangle(); | 
| 263 | < | zValues.push_back(zAngle); | 
| 264 | < |  | 
| 201 | < | } | 
| 202 | < | } | 
| 203 | < |  | 
| 204 | < | // let's send these vectors to the master node so that it | 
| 205 | < | // can sort them and write to the disk | 
| 206 | < | vecLength = gIndex.size(); | 
| 207 | < |  | 
| 208 | < | MPI_Send(&vecLength, 1, MPI_INT, masterNode, | 
| 209 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); | 
| 210 | < | MPI_Send(&gIndex[0], vecLength, MPI_INT, masterNode, | 
| 211 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); | 
| 212 | < | MPI_Send(&zValues[0], vecLength, MPI_REALTYPE, masterNode, | 
| 213 | < | TAKE_THIS_TAG_REAL, MPI_COMM_WORLD); | 
| 214 | < |  | 
| 215 | < | } | 
| 247 | > | int sendBufferLength = buffer.size() + 1; | 
| 248 | > | MPI::COMM_WORLD.Send(&sendBufferLength, 1, MPI::INT, masterNode, 0); | 
| 249 | > | MPI::COMM_WORLD.Send((void *)buffer.c_str(), sendBufferLength, | 
| 250 | > | MPI::CHAR, masterNode, 0); | 
| 251 | > | } | 
| 252 | > | #endif // is_mpi | 
| 253 | > | } | 
| 254 | > |  | 
| 255 | > |  | 
| 256 | > | RestWriter::~RestWriter() { | 
| 257 | > |  | 
| 258 | > | #ifdef IS_MPI | 
| 259 | > |  | 
| 260 | > | if (worldRank == 0) { | 
| 261 | > | #endif // is_mpi | 
| 262 | > | if (createRestFile_){ | 
| 263 | > | writeClosing(*output_); | 
| 264 | > | delete output_; | 
| 265 |  | } | 
| 266 | + | #ifdef IS_MPI | 
| 267 |  | } | 
| 268 | < |  | 
| 219 | < | #endif | 
| 268 | > | #endif // is_mpi | 
| 269 |  | } | 
| 270 |  |  | 
| 271 | < | } | 
| 271 | > | void RestWriter::writeClosing(std::ostream& os) { | 
| 272 | > | os.flush(); | 
| 273 | > | } | 
| 274 | > |  | 
| 275 | > | }// end namespace OpenMD | 
| 276 | > |  |