53#include "utils/Constants.hpp"
54#include "utils/simError.h"
58 HBondZ::HBondZ(SimInfo* info,
const std::string& filename,
59 const std::string& sele1,
const std::string& sele2,
60 double rCut,
double thetaCut,
int nzbins,
int axis) :
61 StaticAnalyser(info, filename, nzbins),
62 selectionScript1_(sele1), seleMan1_(info), evaluator1_(info),
63 selectionScript2_(sele2), seleMan2_(info), evaluator2_(info),
65 ff_ = info_->getForceField();
67 evaluator1_.loadScriptString(sele1);
68 if (!evaluator1_.isDynamic()) {
69 seleMan1_.setSelectionSet(evaluator1_.evaluate());
71 evaluator2_.loadScriptString(sele2);
72 if (!evaluator2_.isDynamic()) {
73 seleMan2_.setSelectionSet(evaluator2_.evaluate());
83 nHBonds_.resize(nBins_);
84 nDonor_.resize(nBins_);
85 nAcceptor_.resize(nBins_);
86 sliceQ_.resize(nBins_);
87 sliceCount_.resize(nBins_);
88 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
89 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
104 setOutputName(
getPrefix(filename) +
".hbondz");
107 void HBondZ::process() {
110 Molecule::HBondDonor* hbd1;
111 Molecule::HBondDonor* hbd2;
112 std::vector<Molecule::HBondDonor*>::iterator hbdi;
113 std::vector<Molecule::HBondDonor*>::iterator hbdj;
114 std::vector<Atom*>::iterator hbai;
115 std::vector<Atom*>::iterator hbaj;
123 RealType DAdist, DHdist, theta, ctheta;
127 bool usePeriodicBoundaryConditions_ =
128 info_->getSimParams()->getUsePeriodicBoundaryConditions();
130 DumpReader reader(info_, dumpFilename_);
131 int nFrames = reader.getNFrames();
134 for (
int istep = 0; istep < nFrames; istep += step_) {
135 reader.readFrame(istep);
136 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
138 Mat3x3d hmat = currentSnapshot_->getHmat();
139 zBox_.push_back(hmat(axis_, axis_));
141 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
143 if (evaluator1_.isDynamic()) {
144 seleMan1_.setSelectionSet(evaluator1_.evaluate());
147 if (evaluator2_.isDynamic()) {
148 seleMan2_.setSelectionSet(evaluator2_.evaluate());
151 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
152 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
157 Vector3d mPos = mol1->getCom();
159 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
160 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
162 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
163 hbd1 = mol1->nextHBondDonor(hbdi)) {
164 dPos = hbd1->donorAtom->getPos();
165 hPos = hbd1->donatedHydrogen->getPos();
167 currentSnapshot_->wrapVector(DH);
171 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
172 hba2 = mol2->nextHBondAcceptor(hbaj)) {
173 aPos = hba2->getPos();
175 currentSnapshot_->wrapVector(DA);
176 DAdist = DA.length();
180 if (DAdist < rCut_) {
181 ctheta =
dot(DH, DA) / (DHdist * DAdist);
182 theta = acos(ctheta) * 180.0 / Constants::PI;
185 if (theta < thetaCut_) {
195 for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL;
196 hba1 = mol1->nextHBondAcceptor(hbai)) {
197 aPos = hba1->getPos();
200 for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL;
201 hbd2 = mol2->nextHBondDonor(hbdj)) {
202 dPos = hbd2->donorAtom->getPos();
205 currentSnapshot_->wrapVector(DA);
206 DAdist = DA.length();
210 if (DAdist < rCut_) {
211 hPos = hbd2->donatedHydrogen->getPos();
213 currentSnapshot_->wrapVector(DH);
214 DHdist = DH.length();
215 ctheta =
dot(DH, DA) / (DHdist * DAdist);
216 theta = acos(ctheta) * 180.0 / Constants::PI;
218 if (theta < thetaCut_) {
227 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(mPos);
229 int(nBins_ * (halfBoxZ_ + mPos[axis_]) / hmat(axis_, axis_));
230 sliceQ_[binNo] += nHB;
231 sliceCount_[binNo] += 1;
237 void HBondZ::writeDensity() {
241 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
245 RealType zAve = zSum / zBox_.size();
247 std::ofstream qZstream(outputFilename_.c_str());
248 if (qZstream.is_open()) {
249 qZstream <<
"#Hydrogen Bonds (" << axisLabel_ <<
")\n";
251 qZstream <<
"#nFrames:\t" << zBox_.size() <<
"\n";
252 qZstream <<
"#selection 1: (" << selectionScript1_ <<
")\n";
253 qZstream <<
"#selection 2: (" << selectionScript2_ <<
")\n";
254 qZstream <<
"#" << axisLabel_ <<
"\tHydrogen Bonds\n";
255 for (
unsigned int i = 0; i < sliceQ_.size(); ++i) {
256 RealType z = zAve * (i + 0.5) / sliceQ_.size();
257 if (sliceCount_[i] != 0) {
258 qZstream << z <<
"\t" << sliceQ_[i] / sliceCount_[i] <<
"\n";
260 qZstream << z <<
"\t" << 0 <<
"\n";
264 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
265 "HBondZ: unable to open %s\n", outputFilename_.c_str());
266 painCave.isFatal = 1;
Real length()
Returns the length of this vector.
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)