56#include "utils/Constants.hpp"
57#include "utils/simError.h"
61 HBondZ::HBondZ(
SimInfo* info,
const std::string& filename,
62 const std::string& sele1,
const std::string& sele2,
63 double rCut,
double thetaCut,
int nzbins,
int axis) :
65 selectionScript1_(sele1), seleMan1_(info), evaluator1_(info),
66 selectionScript2_(sele2), seleMan2_(info), evaluator2_(info),
68 ff_ = info_->getForceField();
70 evaluator1_.loadScriptString(sele1);
71 if (!evaluator1_.isDynamic()) {
72 seleMan1_.setSelectionSet(evaluator1_.evaluate());
74 evaluator2_.loadScriptString(sele2);
75 if (!evaluator2_.isDynamic()) {
76 seleMan2_.setSelectionSet(evaluator2_.evaluate());
86 nHBonds_.resize(nBins_);
87 nDonor_.resize(nBins_);
88 nAcceptor_.resize(nBins_);
89 sliceQ_.resize(nBins_);
90 sliceCount_.resize(nBins_);
91 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
92 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
107 setOutputName(
getPrefix(filename) +
".hbondz");
110 void HBondZ::process() {
113 Molecule::HBondDonor* hbd1;
114 Molecule::HBondDonor* hbd2;
115 std::vector<Molecule::HBondDonor*>::iterator hbdi;
116 std::vector<Molecule::HBondDonor*>::iterator hbdj;
117 std::vector<Atom*>::iterator hbai;
118 std::vector<Atom*>::iterator hbaj;
126 RealType DAdist, DHdist, theta, ctheta;
130 bool usePeriodicBoundaryConditions_ =
131 info_->getSimParams()->getUsePeriodicBoundaryConditions();
133 DumpReader reader(info_, dumpFilename_);
134 int nFrames = reader.getNFrames();
137 for (
int istep = 0; istep < nFrames; istep += step_) {
138 reader.readFrame(istep);
139 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
141 Mat3x3d hmat = currentSnapshot_->getHmat();
142 zBox_.push_back(hmat(axis_, axis_));
144 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
146 if (evaluator1_.isDynamic()) {
147 seleMan1_.setSelectionSet(evaluator1_.evaluate());
150 if (evaluator2_.isDynamic()) {
151 seleMan2_.setSelectionSet(evaluator2_.evaluate());
154 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
155 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
160 Vector3d mPos = mol1->getCom();
162 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
163 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
165 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
166 hbd1 = mol1->nextHBondDonor(hbdi)) {
167 dPos = hbd1->donorAtom->getPos();
168 hPos = hbd1->donatedHydrogen->getPos();
170 currentSnapshot_->wrapVector(DH);
171 DHdist = DH.length();
174 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
175 hba2 = mol2->nextHBondAcceptor(hbaj)) {
176 aPos = hba2->getPos();
178 currentSnapshot_->wrapVector(DA);
179 DAdist = DA.length();
183 if (DAdist < rCut_) {
184 ctheta =
dot(DH, DA) / (DHdist * DAdist);
185 theta = acos(ctheta) * 180.0 / Constants::PI;
188 if (theta < thetaCut_) {
198 for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL;
199 hba1 = mol1->nextHBondAcceptor(hbai)) {
200 aPos = hba1->getPos();
203 for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL;
204 hbd2 = mol2->nextHBondDonor(hbdj)) {
205 dPos = hbd2->donorAtom->getPos();
208 currentSnapshot_->wrapVector(DA);
209 DAdist = DA.length();
213 if (DAdist < rCut_) {
214 hPos = hbd2->donatedHydrogen->getPos();
216 currentSnapshot_->wrapVector(DH);
217 DHdist = DH.length();
218 ctheta =
dot(DH, DA) / (DHdist * DAdist);
219 theta = acos(ctheta) * 180.0 / Constants::PI;
221 if (theta < thetaCut_) {
230 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(mPos);
232 int(nBins_ * (halfBoxZ_ + mPos[axis_]) / hmat(axis_, axis_));
233 sliceQ_[binNo] += nHB;
234 sliceCount_[binNo] += 1;
240 void HBondZ::writeDensity() {
244 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
248 RealType zAve = zSum / zBox_.size();
250 std::ofstream qZstream(outputFilename_.c_str());
251 if (qZstream.is_open()) {
252 qZstream <<
"#Hydrogen Bonds (" << axisLabel_ <<
")\n";
254 qZstream <<
"#nFrames:\t" << zBox_.size() <<
"\n";
255 qZstream <<
"#selection 1: (" << selectionScript1_ <<
")\n";
256 qZstream <<
"#selection 2: (" << selectionScript2_ <<
")\n";
257 qZstream <<
"#" << axisLabel_ <<
"\tHydrogen Bonds\n";
258 for (
unsigned int i = 0; i < sliceQ_.size(); ++i) {
259 RealType z = zAve * (i + 0.5) / sliceQ_.size();
260 if (sliceCount_[i] != 0) {
261 qZstream << z <<
"\t" << sliceQ_[i] / sliceCount_[i] <<
"\n";
263 qZstream << z <<
"\t" << 0 <<
"\n";
267 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
268 "HBondZ: unable to open %s\n", outputFilename_.c_str());
269 painCave.isFatal = 1;
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.
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.
std::string getPrefix(const std::string &str)