45#include "io/RestWriter.hpp"
56#include "utils/simError.h"
59 RestWriter::RestWriter(SimInfo* info,
const std::string& filename,
60 std::vector<Restraint*> restraints) :
62 std::vector<Restraint*>::const_iterator resti;
64 createRestFile_ =
false;
67 MPI_Status* istatus = NULL;
71 for (resti = restraints.begin(); resti != restraints.end(); ++resti) {
72 if ((*resti)->getPrintRestraint()) { printAny = 1; }
76 MPI_Allreduce(MPI_IN_PLACE, &printAny, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
79 if (printAny) createRestFile_ =
true;
85 if (createRestFile_) {
86 output_ =
new std::ofstream(filename.c_str());
89 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
90 "Could not open %s for restraint output.\n",
103 if (createRestFile_) (*output_) <<
"#time\t";
105 for (resti = restraints.begin(); resti != restraints.end(); ++resti) {
106 if ((*resti)->getPrintRestraint()) {
107 std::string myName = (*resti)->getRestraintName();
108 int myType = (*resti)->getRestraintType();
110 (*output_) << myName <<
":";
112 if (myType & Restraint::rtDisplacement)
113 (*output_) <<
"\tPosition(angstroms)\tEnergy(kcal/mol)";
115 if (myType & Restraint::rtAbsoluteZ)
116 (*output_) <<
"\tPosition(angstroms)\tEnergy(kcal/mol)";
118 if (myType & Restraint::rtTwist)
119 (*output_) <<
"\tTwistAngle(radians)\tEnergy(kcal/mol)";
121 if (myType & Restraint::rtSwingX)
122 (*output_) <<
"\tSwingXAngle(radians)\tEnergy(kcal/mol)";
124 if (myType & Restraint::rtSwingY)
125 (*output_) <<
"\tSwingYAngle(radians)\tEnergy(kcal/mol)";
129 if (createRestFile_) (*output_) <<
"\n";
130 if (createRestFile_) (*output_).flush();
136 for (resti = restraints.begin(); resti != restraints.end(); ++resti) {
137 if ((*resti)->getPrintRestraint()) {
138 std::string myName = (*resti)->getRestraintName();
139 int myType = (*resti)->getRestraintType();
141 buffer += (myName +
":");
143 if (myType & Restraint::rtDisplacement)
144 buffer +=
"\tPosition(angstroms)\tEnergy(kcal/mol)";
146 if (myType & Restraint::rtAbsoluteZ)
147 buffer +=
"\tPosition(angstroms)\tEnergy(kcal/mol)";
149 if (myType & Restraint::rtTwist)
150 buffer +=
"\tTwistAngle(radians)\tEnergy(kcal/mol)";
152 if (myType & Restraint::rtSwingX)
153 buffer +=
"\tSwingXAngle(radians)\tEnergy(kcal/mol)";
155 if (myType & Restraint::rtSwingY)
156 buffer +=
"\tSwingYAngle(radians)\tEnergy(kcal/mol)";
162 const int primaryNode = 0;
164 if (worldRank == primaryNode) {
165 if (createRestFile_) (*output_) <<
"#time\t";
166 if (createRestFile_) (*output_) << buffer;
169 MPI_Comm_size(MPI_COMM_WORLD, &nProc);
171 for (
int i = 1; i < nProc; ++i) {
176 MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, istatus);
177 char* recvBuffer =
new char[recvLength];
178 if (recvBuffer == NULL) {
180 MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD,
182 if (createRestFile_) (*output_) << recvBuffer;
186 if (createRestFile_) (*output_).flush();
188 int sendBufferLength = buffer.size() + 1;
189 MPI_Send(&sendBufferLength, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
190 MPI_Send((
void*)buffer.c_str(), sendBufferLength, MPI_CHAR, primaryNode,
197 void RestWriter::writeRest(
198 std::vector<std::map<int, Restraint::RealPair>> restInfo) {
200 MPI_Status* istatus = NULL;
206 << info_->getSnapshotManager()->getCurrentSnapshot()->getTime();
209 std::vector<std::map<int, Restraint::RealPair>>::const_iterator i;
210 std::map<int, Restraint::RealPair>::const_iterator j;
212 if (createRestFile_) {
213 for (i = restInfo.begin(); i != restInfo.end(); ++i) {
214 for (j = (*i).begin(); j != (*i).end(); ++j) {
215 (*output_) <<
"\t" << (j->second).first <<
"\t" << (j->second).second;
217 (*output_) << std::endl;
222 std::string buffer, first, second;
223 std::stringstream ss;
225 std::vector<std::map<int, Restraint::RealPair>>::const_iterator i;
226 std::map<int, Restraint::RealPair>::const_iterator j;
228 if (createRestFile_) {
229 for (i = restInfo.begin(); i != restInfo.end(); ++i) {
230 for (j = (*i).begin(); j != (*i).end(); ++j) {
232 ss << (j->second).first;
235 ss << (j->second).second;
237 buffer += (
"\t" + first +
"\t" + second);
243 const int primaryNode = 0;
245 if (createRestFile_) {
246 if (worldRank == primaryNode) {
248 << info_->getSnapshotManager()->getCurrentSnapshot()->getTime();
249 (*output_) << buffer;
252 MPI_Comm_size(MPI_COMM_WORLD, &nProc);
253 for (
int i = 1; i < nProc; ++i) {
258 MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, istatus);
259 char* recvBuffer =
new char[recvLength];
260 if (recvBuffer == NULL) {
262 MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD,
264 if (createRestFile_) (*output_) << recvBuffer;
271 int sendBufferLength = buffer.size() + 1;
272 MPI_Send(&sendBufferLength, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
273 MPI_Send((
void*)buffer.c_str(), sendBufferLength, MPI_CHAR, primaryNode,
280 RestWriter::~RestWriter() {
283 if (worldRank == 0) {
285 if (createRestFile_) {
286 writeClosing(*output_);
294 void RestWriter::writeClosing(std::ostream& os) { os.flush(); }
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.