48#include "io/RestWriter.hpp"
59#include "utils/simError.h"
62 RestWriter::RestWriter(
SimInfo* info,
const std::string& filename,
63 std::vector<Restraint*> restraints) :
65 std::vector<Restraint*>::const_iterator resti;
67 createRestFile_ =
false;
74 for (resti = restraints.begin(); resti != restraints.end(); ++resti) {
75 if ((*resti)->getPrintRestraint()) { printAny = 1; }
79 MPI_Allreduce(MPI_IN_PLACE, &printAny, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
82 if (printAny) createRestFile_ =
true;
88 if (createRestFile_) {
89 output_ =
new std::ofstream(filename.c_str());
92 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
93 "Could not open %s for restraint output.\n",
106 if (createRestFile_) (*output_) <<
"#time\t";
108 for (resti = restraints.begin(); resti != restraints.end(); ++resti) {
109 if ((*resti)->getPrintRestraint()) {
110 std::string myName = (*resti)->getRestraintName();
111 int myType = (*resti)->getRestraintType();
113 (*output_) << myName <<
":";
115 if (myType & Restraint::rtDisplacement)
116 (*output_) <<
"\tPosition(angstroms)\tEnergy(kcal/mol)";
118 if (myType & Restraint::rtAbsoluteZ)
119 (*output_) <<
"\tPosition(angstroms)\tEnergy(kcal/mol)";
121 if (myType & Restraint::rtTwist)
122 (*output_) <<
"\tTwistAngle(radians)\tEnergy(kcal/mol)";
124 if (myType & Restraint::rtSwingX)
125 (*output_) <<
"\tSwingXAngle(radians)\tEnergy(kcal/mol)";
127 if (myType & Restraint::rtSwingY)
128 (*output_) <<
"\tSwingYAngle(radians)\tEnergy(kcal/mol)";
132 if (createRestFile_) (*output_) <<
"\n";
133 if (createRestFile_) (*output_).flush();
139 for (resti = restraints.begin(); resti != restraints.end(); ++resti) {
140 if ((*resti)->getPrintRestraint()) {
141 std::string myName = (*resti)->getRestraintName();
142 int myType = (*resti)->getRestraintType();
144 buffer += (myName +
":");
146 if (myType & Restraint::rtDisplacement)
147 buffer +=
"\tPosition(angstroms)\tEnergy(kcal/mol)";
149 if (myType & Restraint::rtAbsoluteZ)
150 buffer +=
"\tPosition(angstroms)\tEnergy(kcal/mol)";
152 if (myType & Restraint::rtTwist)
153 buffer +=
"\tTwistAngle(radians)\tEnergy(kcal/mol)";
155 if (myType & Restraint::rtSwingX)
156 buffer +=
"\tSwingXAngle(radians)\tEnergy(kcal/mol)";
158 if (myType & Restraint::rtSwingY)
159 buffer +=
"\tSwingYAngle(radians)\tEnergy(kcal/mol)";
165 const int primaryNode = 0;
167 if (worldRank == primaryNode) {
168 if (createRestFile_) (*output_) <<
"#time\t";
169 if (createRestFile_) (*output_) << buffer;
172 MPI_Comm_size(MPI_COMM_WORLD, &nProc);
174 for (
int i = 1; i < nProc; ++i) {
179 MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &istatus);
180 char* recvBuffer =
new char[recvLength];
181 if (recvBuffer == NULL) {
183 MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD,
185 if (createRestFile_) (*output_) << recvBuffer;
189 if (createRestFile_) (*output_).flush();
191 int sendBufferLength = buffer.size() + 1;
192 MPI_Send(&sendBufferLength, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
193 MPI_Send((
void*)buffer.c_str(), sendBufferLength, MPI_CHAR, primaryNode,
200 void RestWriter::writeRest(
201 std::vector<std::map<int, Restraint::RealPair>> restInfo) {
209 << info_->getSnapshotManager()->getCurrentSnapshot()->getTime();
212 std::vector<std::map<int, Restraint::RealPair>>::const_iterator i;
213 std::map<int, Restraint::RealPair>::const_iterator j;
215 if (createRestFile_) {
216 for (i = restInfo.begin(); i != restInfo.end(); ++i) {
217 for (j = (*i).begin(); j != (*i).end(); ++j) {
218 (*output_) <<
"\t" << (j->second).first <<
"\t" << (j->second).second;
220 (*output_) << std::endl;
225 std::string buffer, first, second;
226 std::stringstream ss;
228 std::vector<std::map<int, Restraint::RealPair>>::const_iterator i;
229 std::map<int, Restraint::RealPair>::const_iterator j;
231 if (createRestFile_) {
232 for (i = restInfo.begin(); i != restInfo.end(); ++i) {
233 for (j = (*i).begin(); j != (*i).end(); ++j) {
235 ss << (j->second).first;
238 ss << (j->second).second;
240 buffer += (
"\t" + first +
"\t" + second);
246 const int primaryNode = 0;
248 if (createRestFile_) {
249 if (worldRank == primaryNode) {
251 << info_->getSnapshotManager()->getCurrentSnapshot()->getTime();
252 (*output_) << buffer;
255 MPI_Comm_size(MPI_COMM_WORLD, &nProc);
256 for (
int i = 1; i < nProc; ++i) {
261 MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &istatus);
262 char* recvBuffer =
new char[recvLength];
263 if (recvBuffer == NULL) {
265 MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD,
267 if (createRestFile_) (*output_) << recvBuffer;
274 int sendBufferLength = buffer.size() + 1;
275 MPI_Send(&sendBufferLength, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
276 MPI_Send((
void*)buffer.c_str(), sendBufferLength, MPI_CHAR, primaryNode,
283 RestWriter::~RestWriter() {
286 if (worldRank == 0) {
288 if (createRestFile_) {
289 writeClosing(*output_);
297 void RestWriter::writeClosing(std::ostream& os) { os.flush(); }
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.