64#include "restraints/MolecularRestraint.hpp"
65#include "restraints/ObjectRestraint.hpp"
67#include "utils/simError.h"
71 void RestReader::scanFile() {
72 std::streampos prevPos;
73 std::streampos currPos;
81 currPos = inFile_->tellg();
84 bool foundOpenSnapshotTag =
false;
86 while (!foundOpenSnapshotTag && inFile_->getline(buffer, bufferSize)) {
89 std::string line = buffer;
90 currPos = inFile_->tellg();
91 if (line.find(
"<Snapshot>") != std::string::npos) {
92 foundOpenSnapshotTag =
true;
93 framePos_ = (
long long)prevPos;
100 MPI_Bcast(&framePos_, 1, MPI_LONG_LONG, 0, MPI_COMM_WORLD);
104 void RestReader::readSet() {
110 inFile_->seekg(framePos_);
112 std::istream& inputStream = *inFile_;
116 std::stringstream sstream;
117 if (worldRank == primaryNode) {
118 std::string sendBuffer;
121 inFile_->seekg(framePos_);
123 while (inFile_->getline(buffer, bufferSize)) {
127 if (line.find(
"</Snapshot>") != std::string::npos) {
break; }
130 int sendBufferSize = sendBuffer.size();
131 MPI_Bcast(&sendBufferSize, 1, MPI_INT, primaryNode, MPI_COMM_WORLD);
132 MPI_Bcast((
void*)sendBuffer.c_str(), sendBufferSize, MPI_CHAR,
133 primaryNode, MPI_COMM_WORLD);
135 sstream.str(sendBuffer);
138 MPI_Bcast(&sendBufferSize, 1, MPI_INT, primaryNode, MPI_COMM_WORLD);
139 char* recvBuffer =
new char[sendBufferSize + 1];
141 recvBuffer[sendBufferSize] =
'\0';
142 MPI_Bcast(recvBuffer, sendBufferSize, MPI_CHAR, primaryNode,
144 sstream.str(recvBuffer);
148 std::istream& inputStream = sstream;
151 inputStream.getline(buffer, bufferSize);
154 if (line.find(
"<Snapshot>") == std::string::npos) {
155 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
156 "RestReader Error: can not find <Snapshot>\n");
157 painCave.isFatal = 1;
162 readFrameProperties(inputStream);
165 readStuntDoubles(inputStream);
167 inputStream.getline(buffer, bufferSize);
169 if (line.find(
"</Snapshot>") == std::string::npos) {
170 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
171 "RestReader Error: can not find </Snapshot>\n");
172 painCave.isFatal = 1;
177 void RestReader::readReferenceStructure() {
197 SimInfo::MoleculeIterator i;
198 Molecule::IntegrableObjectIterator j;
209 std::shared_ptr<GenericData> data = mol->getPropertyByName(
"Restraint");
211 if (data !=
nullptr) {
214 std::shared_ptr<RestraintData> restData =
215 std::dynamic_pointer_cast<RestraintData>(data);
217 if (restData !=
nullptr) {
221 MolecularRestraint* mRest =
222 dynamic_cast<MolecularRestraint*
>(restData->getData());
225 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
226 "Can not cast RestraintData to MolecularRestraint\n");
227 painCave.severity = OPENMD_ERROR;
228 painCave.isFatal = 1;
234 std::vector<Vector3d> ref;
243 for (sd = mol->beginIntegrableObject(j); sd != NULL;
244 sd = mol->nextIntegrableObject(j)) {
249 ref.push_back(all_pos_[sd->getGlobalIntegrableObjectIndex()]);
250 mass = sd->getMass();
251 COM = COM + mass * ref[count];
256 mRest->setReferenceStructure(ref, COM);
263 void RestReader::parseDumpLine(
const std::string& line) {
264 StringTokenizer tokenizer(line);
267 nTokens = tokenizer.countTokens();
270 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
271 "RestReader Error: Not enough Tokens.\n%s\n", line.c_str());
272 painCave.isFatal = 1;
276 int index = tokenizer.nextTokenAsInt();
280 if (sd == NULL) {
return; }
282 std::string type = tokenizer.nextToken();
283 int size = type.size();
288 for (
int i = 0; i < size; ++i) {
291 pos[0] = tokenizer.nextTokenAsDouble();
292 pos[1] = tokenizer.nextTokenAsDouble();
293 pos[2] = tokenizer.nextTokenAsDouble();
298 vel[0] = tokenizer.nextTokenAsDouble();
299 vel[1] = tokenizer.nextTokenAsDouble();
300 vel[2] = tokenizer.nextTokenAsDouble();
305 if (sd->isDirectional()) {
306 q[0] = tokenizer.nextTokenAsDouble();
307 q[1] = tokenizer.nextTokenAsDouble();
308 q[2] = tokenizer.nextTokenAsDouble();
309 q[3] = tokenizer.nextTokenAsDouble();
311 RealType qlen = q.
length();
312 if (qlen < OpenMD::epsilon) {
315 painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
316 "RestReader Error: initial quaternion error (q0^2 + q1^2 + "
319 painCave.isFatal = 1;
329 if (sd->isDirectional()) {
330 ji[0] = tokenizer.nextTokenAsDouble();
331 ji[1] = tokenizer.nextTokenAsDouble();
332 ji[2] = tokenizer.nextTokenAsDouble();
338 force[0] = tokenizer.nextTokenAsDouble();
339 force[1] = tokenizer.nextTokenAsDouble();
340 force[2] = tokenizer.nextTokenAsDouble();
345 torque[0] = tokenizer.nextTokenAsDouble();
346 torque[1] = tokenizer.nextTokenAsDouble();
347 torque[2] = tokenizer.nextTokenAsDouble();
351 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
352 "RestReader Error: %s is an unrecognized type\n",
354 painCave.isFatal = 1;
361 all_pos_[index] = pos;
364 std::shared_ptr<GenericData> data = sd->getPropertyByName(
"Restraint");
366 if (data !=
nullptr) {
368 std::shared_ptr<RestraintData> restData =
369 std::dynamic_pointer_cast<RestraintData>(data);
370 if (restData !=
nullptr) {
373 ObjectRestraint* oRest =
374 dynamic_cast<ObjectRestraint*
>(restData->getData());
376 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
377 "Can not cast RestraintData to ObjectRestraint\n");
378 painCave.severity = OPENMD_ERROR;
379 painCave.isFatal = 1;
382 if (sd->isDirectional()) {
383 oRest->setReferenceStructure(pos, q.toRotationMatrix3());
385 oRest->setReferenceStructure(pos);
393 void RestReader::readStuntDoubles(std::istream& inputStream) {
394 inputStream.getline(buffer, bufferSize);
395 std::string line(buffer);
397 if (line.find(
"<StuntDoubles>") == std::string::npos) {
398 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
399 "RestReader Error: Missing <StuntDoubles>\n");
400 painCave.isFatal = 1;
404 while (inputStream.getline(buffer, bufferSize)) {
407 if (line.find(
"</StuntDoubles>") != std::string::npos) {
break; }
413 void RestReader::readFrameProperties(std::istream& inputStream) {
414 inputStream.getline(buffer, bufferSize);
415 std::string line(buffer);
417 if (line.find(
"<FrameData>") == std::string::npos) {
418 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
419 "RestReader Error: Missing <FrameData>\n");
420 painCave.isFatal = 1;
428 while (inputStream.getline(buffer, bufferSize)) {
431 if (line.find(
"</FrameData>") != std::string::npos) {
break; }
int getNGlobalIntegrableObjects()
Returns the total number of integrable objects (total number of rigid bodies plus the total number of...
Molecule * beginMolecule(MoleculeIterator &i)
Returns the first molecule in this SimInfo and intialize the iterator.
Molecule * nextMolecule(MoleculeIterator &i)
Returns the next avaliable Molecule based on the iterator.
StuntDouble * getIOIndexToIntegrableObject(int index)
return an integral objects by its global index.
Real length()
Returns the length of this vector.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.