67#include "restraints/MolecularRestraint.hpp"
68#include "restraints/ObjectRestraint.hpp"
70#include "utils/simError.h"
74 void RestReader::scanFile() {
75 std::streampos prevPos;
76 std::streampos currPos;
84 currPos = inFile_->tellg();
87 bool foundOpenSnapshotTag =
false;
89 while (!foundOpenSnapshotTag && inFile_->getline(buffer, bufferSize)) {
92 std::string line = buffer;
93 currPos = inFile_->tellg();
94 if (line.find(
"<Snapshot>") != std::string::npos) {
95 foundOpenSnapshotTag =
true;
96 framePos_ = (
long long)prevPos;
103 MPI_Bcast(&framePos_, 1, MPI_LONG_LONG, 0, MPI_COMM_WORLD);
107 void RestReader::readSet() {
113 inFile_->seekg(framePos_);
115 std::istream& inputStream = *inFile_;
119 std::stringstream sstream;
120 if (worldRank == primaryNode) {
121 std::string sendBuffer;
124 inFile_->seekg(framePos_);
126 while (inFile_->getline(buffer, bufferSize)) {
130 if (line.find(
"</Snapshot>") != std::string::npos) {
break; }
133 int sendBufferSize = sendBuffer.size();
134 MPI_Bcast(&sendBufferSize, 1, MPI_INT, primaryNode, MPI_COMM_WORLD);
135 MPI_Bcast((
void*)sendBuffer.c_str(), sendBufferSize, MPI_CHAR,
136 primaryNode, MPI_COMM_WORLD);
138 sstream.str(sendBuffer);
141 MPI_Bcast(&sendBufferSize, 1, MPI_INT, primaryNode, MPI_COMM_WORLD);
142 char* recvBuffer =
new char[sendBufferSize + 1];
144 recvBuffer[sendBufferSize] =
'\0';
145 MPI_Bcast(recvBuffer, sendBufferSize, MPI_CHAR, primaryNode,
147 sstream.str(recvBuffer);
151 std::istream& inputStream = sstream;
154 inputStream.getline(buffer, bufferSize);
157 if (line.find(
"<Snapshot>") == std::string::npos) {
158 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
159 "RestReader Error: can not find <Snapshot>\n");
160 painCave.isFatal = 1;
165 readFrameProperties(inputStream);
168 readStuntDoubles(inputStream);
170 inputStream.getline(buffer, bufferSize);
172 if (line.find(
"</Snapshot>") == std::string::npos) {
173 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
174 "RestReader Error: can not find </Snapshot>\n");
175 painCave.isFatal = 1;
180 void RestReader::readReferenceStructure() {
186 all_pos_.resize(info_->getNGlobalIntegrableObjects());
200 SimInfo::MoleculeIterator i;
201 Molecule::IntegrableObjectIterator j;
209 for (mol = info_->beginMolecule(i); mol != NULL;
210 mol = info_->nextMolecule(i)) {
212 std::shared_ptr<GenericData> data = mol->getPropertyByName(
"Restraint");
214 if (data !=
nullptr) {
217 std::shared_ptr<RestraintData> restData =
218 std::dynamic_pointer_cast<RestraintData>(data);
220 if (restData !=
nullptr) {
224 MolecularRestraint* mRest =
225 dynamic_cast<MolecularRestraint*
>(restData->getData());
228 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
229 "Can not cast RestraintData to MolecularRestraint\n");
230 painCave.severity = OPENMD_ERROR;
231 painCave.isFatal = 1;
237 std::vector<Vector3d> ref;
246 for (sd = mol->beginIntegrableObject(j); sd != NULL;
247 sd = mol->nextIntegrableObject(j)) {
252 ref.push_back(all_pos_[sd->getGlobalIntegrableObjectIndex()]);
253 mass = sd->getMass();
254 COM = COM + mass * ref[count];
259 mRest->setReferenceStructure(ref, COM);
266 void RestReader::parseDumpLine(
const std::string& line) {
267 StringTokenizer tokenizer(line);
270 nTokens = tokenizer.countTokens();
273 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
274 "RestReader Error: Not enough Tokens.\n%s\n", line.c_str());
275 painCave.isFatal = 1;
279 int index = tokenizer.nextTokenAsInt();
281 StuntDouble* sd = info_->getIOIndexToIntegrableObject(index);
283 if (sd == NULL) {
return; }
285 std::string type = tokenizer.nextToken();
286 int size = type.size();
291 for (
int i = 0; i < size; ++i) {
294 pos[0] = tokenizer.nextTokenAsDouble();
295 pos[1] = tokenizer.nextTokenAsDouble();
296 pos[2] = tokenizer.nextTokenAsDouble();
301 vel[0] = tokenizer.nextTokenAsDouble();
302 vel[1] = tokenizer.nextTokenAsDouble();
303 vel[2] = tokenizer.nextTokenAsDouble();
308 if (sd->isDirectional()) {
309 q[0] = tokenizer.nextTokenAsDouble();
310 q[1] = tokenizer.nextTokenAsDouble();
311 q[2] = tokenizer.nextTokenAsDouble();
312 q[3] = tokenizer.nextTokenAsDouble();
314 RealType qlen = q.length();
315 if (qlen < OpenMD::epsilon) {
318 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
319 "RestReader Error: initial quaternion error (q0^2 + q1^2 + "
322 painCave.isFatal = 1;
332 if (sd->isDirectional()) {
333 ji[0] = tokenizer.nextTokenAsDouble();
334 ji[1] = tokenizer.nextTokenAsDouble();
335 ji[2] = tokenizer.nextTokenAsDouble();
341 force[0] = tokenizer.nextTokenAsDouble();
342 force[1] = tokenizer.nextTokenAsDouble();
343 force[2] = tokenizer.nextTokenAsDouble();
348 torque[0] = tokenizer.nextTokenAsDouble();
349 torque[1] = tokenizer.nextTokenAsDouble();
350 torque[2] = tokenizer.nextTokenAsDouble();
354 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
355 "RestReader Error: %s is an unrecognized type\n",
357 painCave.isFatal = 1;
364 all_pos_[index] = pos;
367 std::shared_ptr<GenericData> data = sd->getPropertyByName(
"Restraint");
369 if (data !=
nullptr) {
371 std::shared_ptr<RestraintData> restData =
372 std::dynamic_pointer_cast<RestraintData>(data);
373 if (restData !=
nullptr) {
376 ObjectRestraint* oRest =
377 dynamic_cast<ObjectRestraint*
>(restData->getData());
379 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
380 "Can not cast RestraintData to ObjectRestraint\n");
381 painCave.severity = OPENMD_ERROR;
382 painCave.isFatal = 1;
385 if (sd->isDirectional()) {
386 oRest->setReferenceStructure(pos, q.toRotationMatrix3());
388 oRest->setReferenceStructure(pos);
396 void RestReader::readStuntDoubles(std::istream& inputStream) {
397 inputStream.getline(buffer, bufferSize);
398 std::string line(buffer);
400 if (line.find(
"<StuntDoubles>") == std::string::npos) {
401 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
402 "RestReader Error: Missing <StuntDoubles>\n");
403 painCave.isFatal = 1;
407 while (inputStream.getline(buffer, bufferSize)) {
410 if (line.find(
"</StuntDoubles>") != std::string::npos) {
break; }
416 void RestReader::readFrameProperties(std::istream& inputStream) {
417 inputStream.getline(buffer, bufferSize);
418 std::string line(buffer);
420 if (line.find(
"<FrameData>") == std::string::npos) {
421 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
422 "RestReader Error: Missing <FrameData>\n");
423 painCave.isFatal = 1;
431 while (inputStream.getline(buffer, bufferSize)) {
434 if (line.find(
"</FrameData>") != std::string::npos) {
break; }
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.