| 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 | < | #ifdef IS_MPI | 
| 56 | < | #include <mpi.h> | 
| 57 | < | #define TAKE_THIS_TAG_INT 1 | 
| 58 | < | #define TAKE_THIS_TAG_REAL 2 | 
| 51 | < | #endif //is_mpi | 
| 55 | > | namespace OpenMD { | 
| 56 | > | RestWriter::RestWriter(SimInfo* info, const std::string& filename, | 
| 57 | > | std::vector<Restraint*> restraints ) : | 
| 58 | > | info_(info){ | 
| 59 |  |  | 
| 60 | < | namespace oopse { | 
| 61 | < | RestWriter::RestWriter(SimInfo* info) : | 
| 62 | < | info_(info), outName_(info_->getRestFileName()) { | 
| 63 | < | } | 
| 64 | < |  | 
| 65 | < | RestWriter::~RestWriter() {} | 
| 66 | < |  | 
| 60 | < | void RestWriter::writeZAngFile() { | 
| 61 | < | std::ostream* zangStream; | 
| 60 | > | std::vector<Restraint*>::const_iterator resti; | 
| 61 | > |  | 
| 62 | > | createRestFile_ = false; | 
| 63 | > |  | 
| 64 | > | #ifdef IS_MPI | 
| 65 | > | MPI_Status* istatus; | 
| 66 | > | #endif | 
| 67 |  |  | 
| 68 | < | #ifdef IS_MPI | 
| 69 | < | if (worldRank == 0) { | 
| 70 | < | #endif // is_mpi | 
| 71 | < |  | 
| 72 | < | zangStream = new std::ofstream(outName_.c_str()); | 
| 68 | < |  | 
| 69 | < | #ifdef IS_MPI | 
| 68 | > | int printAny = 0; | 
| 69 | > | for(resti=restraints.begin(); resti != restraints.end(); ++resti){ | 
| 70 | > | if ((*resti)->getPrintRestraint()) { | 
| 71 | > | printAny = 1; | 
| 72 | > | } | 
| 73 |  | } | 
| 71 | – | #endif // is_mpi | 
| 74 |  |  | 
| 73 | – | writeZangle(*zangStream); | 
| 74 | – |  | 
| 75 |  | #ifdef IS_MPI | 
| 76 | < | if (worldRank == 0) { | 
| 77 | < | #endif // is_mpi | 
| 78 | < | delete zangStream; | 
| 79 | < |  | 
| 76 | > | MPI_Allreduce(MPI_IN_PLACE, &printAny, 1, MPI_INT, MPI_SUM, | 
| 77 | > | MPI_COMM_WORLD); | 
| 78 | > | #endif | 
| 79 | > |  | 
| 80 | > | if (printAny) createRestFile_ = true; | 
| 81 | > |  | 
| 82 |  | #ifdef IS_MPI | 
| 83 | + | if(worldRank == 0){ | 
| 84 | + | #endif | 
| 85 | + |  | 
| 86 | + | if (createRestFile_) { | 
| 87 | + | output_ = new std::ofstream(filename.c_str()); | 
| 88 | + |  | 
| 89 | + | if(!output_){ | 
| 90 | + | sprintf( painCave.errMsg, | 
| 91 | + | "Could not open %s for restraint output.\n", | 
| 92 | + | filename.c_str()); | 
| 93 | + | painCave.isFatal = 1; | 
| 94 | + | simError(); | 
| 95 | + | } | 
| 96 | + | } | 
| 97 | + |  | 
| 98 | + | #ifdef IS_MPI | 
| 99 |  | } | 
| 100 | < | #endif // is_mpi | 
| 83 | < |  | 
| 84 | < | } | 
| 100 | > | #endif // is_mpi | 
| 101 |  |  | 
| 102 | < | void RestWriter::writeZangle(std::ostream& finalOut){ | 
| 87 | < | const int BUFFERSIZE = 2000; | 
| 88 | < | char tempBuffer[BUFFERSIZE]; | 
| 89 | < | char writeLine[BUFFERSIZE]; | 
| 90 | < |  | 
| 91 | < | Molecule* mol; | 
| 92 | < | StuntDouble* integrableObject; | 
| 93 | < | SimInfo::MoleculeIterator mi; | 
| 94 | < | Molecule::IntegrableObjectIterator ii; | 
| 95 | < |  | 
| 102 | > |  | 
| 103 |  | #ifndef IS_MPI | 
| 104 | < | // first we do output for the single processor version | 
| 105 | < | finalOut | 
| 99 | < | << info_->getSnapshotManager()->getCurrentSnapshot()->getTime() | 
| 100 | < | << " : omega values at this time\n"; | 
| 101 | < |  | 
| 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)) { | 
| 108 | < |  | 
| 109 | < | sprintf( tempBuffer, | 
| 110 | < | "%14.10lf\n", | 
| 111 | < | integrableObject->getZangle()); | 
| 112 | < | strcpy( writeLine, tempBuffer ); | 
| 113 | < |  | 
| 114 | < | finalOut << writeLine; | 
| 104 | > |  | 
| 105 | > | if (createRestFile_) (*output_) << "#time\t"; | 
| 106 |  |  | 
| 107 | + | for(resti=restraints.begin(); resti != restraints.end(); ++resti){ | 
| 108 | + | if ((*resti)->getPrintRestraint()) { | 
| 109 | + | std::string myName = (*resti)->getRestraintName(); | 
| 110 | + | int myType = (*resti)->getRestraintType(); | 
| 111 | + |  | 
| 112 | + | (*output_) << myName << ":"; | 
| 113 | + |  | 
| 114 | + | if (myType & Restraint::rtDisplacement) | 
| 115 | + | (*output_) << "\tPosition(angstroms)\tEnergy(kcal/mol)"; | 
| 116 | + |  | 
| 117 | + | if (myType & Restraint::rtTwist) | 
| 118 | + | (*output_) << "\tTwistAngle(radians)\tEnergy(kcal/mol)"; | 
| 119 | + |  | 
| 120 | + | if (myType & Restraint::rtSwingX) | 
| 121 | + | (*output_) << "\tSwingXAngle(radians)\tEnergy(kcal/mol)"; | 
| 122 | + |  | 
| 123 | + | if (myType & Restraint::rtSwingY) | 
| 124 | + | (*output_) << "\tSwingYAngle(radians)\tEnergy(kcal/mol)"; | 
| 125 |  | } | 
| 126 |  | } | 
| 127 | + |  | 
| 128 | + | if (createRestFile_) (*output_) << "\n"; | 
| 129 | + | if (createRestFile_) (*output_).flush(); | 
| 130 |  |  | 
| 131 |  | #else | 
| 120 | – | int nproc; | 
| 121 | – | MPI_Comm_size(MPI_COMM_WORLD, &nproc); | 
| 122 | – | const int masterNode = 0; | 
| 132 |  |  | 
| 133 | < | MPI_Status ierr; | 
| 125 | < | int intObIndex; | 
| 126 | < | int vecLength; | 
| 127 | < | RealType zAngle; | 
| 128 | < | std::vector<int> gIndex; | 
| 129 | < | std::vector<RealType> zValues; | 
| 133 | > | std::string buffer; | 
| 134 |  |  | 
| 135 | < | if (worldRank == masterNode) { | 
| 136 | < | std::map<int, RealType> zAngData; | 
| 137 | < | for(int i = 0 ; i < nproc; ++i) { | 
| 138 | < | if (i == masterNode) { | 
| 135 | < | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 136 | < | mol = info_->nextMolecule(mi)) { | 
| 137 | < |  | 
| 138 | < | for (integrableObject = mol->beginIntegrableObject(ii); | 
| 139 | < | integrableObject != NULL; | 
| 140 | < | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 141 | < |  | 
| 142 | < | intObIndex = integrableObject->getGlobalIndex(); | 
| 135 | > | for(resti=restraints.begin(); resti != restraints.end(); ++resti){ | 
| 136 | > | if ((*resti)->getPrintRestraint()) { | 
| 137 | > | std::string myName = (*resti)->getRestraintName(); | 
| 138 | > | int myType = (*resti)->getRestraintType(); | 
| 139 |  |  | 
| 140 | < | zAngle = integrableObject->getZangle(); | 
| 141 | < | zAngData.insert(std::pair<int, RealType>(intObIndex, zAngle)); | 
| 142 | < | } | 
| 143 | < | } | 
| 144 | < | } else { | 
| 145 | < | MPI_Recv(&vecLength, 1, MPI_INT, i, | 
| 146 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &ierr); | 
| 147 | < | // make sure the vectors are the right size for the incoming data | 
| 148 | < | gIndex.resize(vecLength); | 
| 149 | < | zValues.resize(vecLength); | 
| 150 | < |  | 
| 151 | < | MPI_Recv(&gIndex[0], vecLength, MPI_INT, i, | 
| 152 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &ierr); | 
| 153 | < | MPI_Recv(&zValues[0], vecLength, MPI_REALTYPE, i, | 
| 154 | < | 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(); | 
| 165 | < | } | 
| 140 | > | buffer += (myName + ":"); | 
| 141 | > |  | 
| 142 | > | if (myType & Restraint::rtDisplacement) | 
| 143 | > | buffer += "\tPosition(angstroms)\tEnergy(kcal/mol)"; | 
| 144 | > |  | 
| 145 | > | if (myType & Restraint::rtTwist) | 
| 146 | > | buffer += "\tTwistAngle(radians)\tEnergy(kcal/mol)"; | 
| 147 | > |  | 
| 148 | > | if (myType & Restraint::rtSwingX) | 
| 149 | > | buffer += "\tSwingXAngle(radians)\tEnergy(kcal/mol)"; | 
| 150 | > |  | 
| 151 | > | if (myType & Restraint::rtSwingY) | 
| 152 | > | buffer += "\tSwingYAngle(radians)\tEnergy(kcal/mol)"; | 
| 153 | > |  | 
| 154 | > | buffer += "\n"; | 
| 155 |  | } | 
| 156 | + | } | 
| 157 | + |  | 
| 158 | + | const int masterNode = 0; | 
| 159 | + |  | 
| 160 | + | if (worldRank == masterNode) { | 
| 161 | + | if (createRestFile_) (*output_) << "#time\t"; | 
| 162 | + | if (createRestFile_) (*output_) << buffer; | 
| 163 |  |  | 
| 164 | < | finalOut << info_->getSnapshotManager()->getCurrentSnapshot()->getTime() | 
| 165 | < | << " : omega values at this time\n"; | 
| 170 | < |  | 
| 171 | < | std::map<int, RealType>::iterator l; | 
| 172 | < | for (l = zAngData.begin(); l != zAngData.end(); ++l) { | 
| 164 | > | int nProc; | 
| 165 | > | MPI_Comm_size( MPI_COMM_WORLD, &nProc); | 
| 166 |  |  | 
| 167 | < | sprintf( tempBuffer, | 
| 175 | < | "%14.10lf\n", | 
| 176 | < | l->second); | 
| 177 | < | strcpy( writeLine, tempBuffer ); | 
| 167 | > | for (int i = 1; i < nProc; ++i) { | 
| 168 |  |  | 
| 169 | < | finalOut << writeLine; | 
| 170 | < | } | 
| 171 | < |  | 
| 169 | > | // receive the length of the string buffer that was | 
| 170 | > | // prepared by processor i | 
| 171 | > |  | 
| 172 | > | int recvLength; | 
| 173 | > | MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, istatus); | 
| 174 | > | char* recvBuffer = new char[recvLength]; | 
| 175 | > | if (recvBuffer == NULL) { | 
| 176 | > | } else { | 
| 177 | > | MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD, | 
| 178 | > | istatus); | 
| 179 | > | if (createRestFile_) (*output_) << recvBuffer; | 
| 180 | > | delete [] recvBuffer; | 
| 181 | > | } | 
| 182 | > | } | 
| 183 | > | if (createRestFile_) (*output_).flush(); | 
| 184 |  | } else { | 
| 185 | < | // pack up and send the appropriate info to the master node | 
| 186 | < | for(int j = 1; j < nproc; ++j) { | 
| 187 | < | if (worldRank == j) { | 
| 188 | < | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 189 | < | mol = info_->nextMolecule(mi)) { | 
| 190 | < |  | 
| 191 | < | for (integrableObject = mol->beginIntegrableObject(ii); | 
| 192 | < | integrableObject != NULL; | 
| 193 | < | integrableObject = mol->nextIntegrableObject(ii)) { | 
| 194 | < |  | 
| 195 | < | // build a vector of the indicies | 
| 196 | < | intObIndex = integrableObject->getGlobalIndex(); | 
| 197 | < | gIndex.push_back(intObIndex); | 
| 198 | < |  | 
| 199 | < | // build a vector of the zAngle values | 
| 200 | < | zAngle = integrableObject->getZangle(); | 
| 201 | < | zValues.push_back(zAngle); | 
| 185 | > | int sendBufferLength = buffer.size() + 1; | 
| 186 | > | MPI_Send(&sendBufferLength, 1, MPI_INT, masterNode, 0, MPI_COMM_WORLD); | 
| 187 | > | MPI_Send((void *)buffer.c_str(), sendBufferLength, MPI_CHAR, | 
| 188 | > | masterNode, 0, MPI_COMM_WORLD); | 
| 189 | > | } | 
| 190 | > |  | 
| 191 | > | #endif // is_mpi | 
| 192 | > |  | 
| 193 | > | } | 
| 194 | > |  | 
| 195 | > | void RestWriter::writeRest(std::vector<std::map<int, Restraint::RealPair> > restInfo) { | 
| 196 | > |  | 
| 197 | > | #ifdef IS_MPI | 
| 198 | > | MPI_Status* istatus; | 
| 199 | > | #endif | 
| 200 | > |  | 
| 201 | > | #ifndef IS_MPI | 
| 202 | > | if (createRestFile_)  (*output_) << info_->getSnapshotManager()->getCurrentSnapshot()->getTime(); | 
| 203 | > |  | 
| 204 | > | // output some information about the molecules | 
| 205 | > | std::vector<std::map<int, Restraint::RealPair> >::const_iterator i; | 
| 206 | > | std::map<int, Restraint::RealPair>::const_iterator j; | 
| 207 | > |  | 
| 208 | > | cerr << "risize = " << restInfo.size() << "\n"; | 
| 209 |  |  | 
| 210 | < | } | 
| 211 | < | } | 
| 212 | < |  | 
| 213 | < | // let's send these vectors to the master node so that it | 
| 214 | < | // can sort them and write to the disk | 
| 215 | < | vecLength = gIndex.size(); | 
| 216 | < |  | 
| 217 | < | MPI_Send(&vecLength, 1, MPI_INT, masterNode, | 
| 218 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); | 
| 219 | < | MPI_Send(&gIndex[0], vecLength, MPI_INT, masterNode, | 
| 220 | < | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); | 
| 221 | < | MPI_Send(&zValues[0], vecLength, MPI_REALTYPE, masterNode, | 
| 222 | < | TAKE_THIS_TAG_REAL, MPI_COMM_WORLD); | 
| 223 | < |  | 
| 224 | < | } | 
| 210 | > | if ( createRestFile_ ) { | 
| 211 | > |  | 
| 212 | > | for( i = restInfo.begin(); i != restInfo.end(); ++i){ | 
| 213 | > | for(j = (*i).begin(); j != (*i).end(); ++j){ | 
| 214 | > | (*output_) << "\t" << (j->second).first << "\t" << (j->second).second; | 
| 215 | > | } | 
| 216 | > | (*output_) << std::endl; | 
| 217 | > | } | 
| 218 | > | (*output_).flush(); | 
| 219 | > | } | 
| 220 | > | #else | 
| 221 | > | std::string buffer, first, second; | 
| 222 | > | std::stringstream ss; | 
| 223 | > |  | 
| 224 | > | std::vector<std::map<int, Restraint::RealPair> >::const_iterator i; | 
| 225 | > | std::map<int, Restraint::RealPair>::const_iterator j; | 
| 226 | > |  | 
| 227 | > | if ( createRestFile_ ) { | 
| 228 | > | for( i = restInfo.begin(); i != restInfo.end(); ++i){ | 
| 229 | > |  | 
| 230 | > | for(j = (*i).begin(); j != (*i).end(); ++j){ | 
| 231 | > | ss.clear(); | 
| 232 | > | ss << (j->second).first; | 
| 233 | > | ss >> first; | 
| 234 | > | ss.clear(); | 
| 235 | > | ss << (j->second).second; | 
| 236 | > | ss >> second; | 
| 237 | > | buffer += ("\t" + first + "\t" + second); | 
| 238 | > | } | 
| 239 | > | buffer += "\n"; | 
| 240 |  | } | 
| 241 |  | } | 
| 242 | < |  | 
| 243 | < | #endif | 
| 242 | > |  | 
| 243 | > | const int masterNode = 0; | 
| 244 | > |  | 
| 245 | > | if (createRestFile_) { | 
| 246 | > | if (worldRank == masterNode) { | 
| 247 | > |  | 
| 248 | > | (*output_) << info_->getSnapshotManager()->getCurrentSnapshot()->getTime(); | 
| 249 | > | (*output_) << buffer; | 
| 250 | > |  | 
| 251 | > | int nProc; | 
| 252 | > | MPI_Comm_size( MPI_COMM_WORLD, &nProc); | 
| 253 | > | for (int i = 1; i < nProc; ++i) { | 
| 254 | > |  | 
| 255 | > | // receive the length of the string buffer that was | 
| 256 | > | // prepared by processor i | 
| 257 | > |  | 
| 258 | > | int recvLength; | 
| 259 | > | MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, istatus); | 
| 260 | > | char* recvBuffer = new char[recvLength]; | 
| 261 | > | if (recvBuffer == NULL) { | 
| 262 | > | } else { | 
| 263 | > | MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD, | 
| 264 | > | istatus); | 
| 265 | > | if (createRestFile_) (*output_) << recvBuffer; | 
| 266 | > |  | 
| 267 | > | delete [] recvBuffer; | 
| 268 | > | } | 
| 269 | > | } | 
| 270 | > | (*output_).flush(); | 
| 271 | > | } else { | 
| 272 | > | int sendBufferLength = buffer.size() + 1; | 
| 273 | > | MPI_Send(&sendBufferLength, 1, MPI_INT, masterNode, 0, MPI_COMM_WORLD); | 
| 274 | > | MPI_Send((void *)buffer.c_str(), sendBufferLength, | 
| 275 | > | MPI_CHAR, masterNode, 0, MPI_COMM_WORLD); | 
| 276 | > | } | 
| 277 | > | } | 
| 278 | > | #endif // is_mpi | 
| 279 |  | } | 
| 280 |  |  | 
| 281 | < | } | 
| 281 | > |  | 
| 282 | > | RestWriter::~RestWriter() { | 
| 283 | > |  | 
| 284 | > | #ifdef IS_MPI | 
| 285 | > |  | 
| 286 | > | if (worldRank == 0) { | 
| 287 | > | #endif // is_mpi | 
| 288 | > | if (createRestFile_){ | 
| 289 | > | writeClosing(*output_); | 
| 290 | > | delete output_; | 
| 291 | > | } | 
| 292 | > | #ifdef IS_MPI | 
| 293 | > | } | 
| 294 | > | #endif // is_mpi | 
| 295 | > | } | 
| 296 | > |  | 
| 297 | > | void RestWriter::writeClosing(std::ostream& os) { | 
| 298 | > | os.flush(); | 
| 299 | > | } | 
| 300 | > |  | 
| 301 | > | }// end namespace OpenMD | 
| 302 | > |  |