45#include "HBondZvol.hpp"
53#include "utils/Constants.hpp"
54#include "utils/simError.h"
58 HBondZvol::HBondZvol(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_);
89 std::fill(sliceQ_.begin(), sliceQ_.end(), 0.0);
90 std::fill(sliceCount_.begin(), sliceCount_.end(), 0);
105 setOutputName(
getPrefix(filename) +
".hbondzvol");
108 void HBondZvol::process() {
111 Molecule::HBondDonor* hbd1;
112 Molecule::HBondDonor* hbd2;
113 std::vector<Molecule::HBondDonor*>::iterator hbdi;
114 std::vector<Molecule::HBondDonor*>::iterator hbdj;
115 std::vector<Atom*>::iterator hbai;
116 std::vector<Atom*>::iterator hbaj;
124 RealType DAdist, DHdist, theta, ctheta;
129 bool usePeriodicBoundaryConditions_ =
130 info_->getSimParams()->getUsePeriodicBoundaryConditions();
132 DumpReader reader(info_, dumpFilename_);
133 int nFrames = reader.getNFrames();
136 for (
int istep = 0; istep < nFrames; istep += step_) {
137 reader.readFrame(istep);
138 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
140 Mat3x3d hmat = currentSnapshot_->getHmat();
142 RealType Lx = hmat(0, 0);
143 RealType Ly = hmat(1, 1);
144 RealType Lz = hmat(2, 2);
146 zBox_.push_back(hmat(axis_, axis_));
147 sliceVol = currentSnapshot_->getVolume() / nBins_;
149 RealType halfBoxZ_ = hmat(axis_, axis_) / 2.0;
151 if (evaluator1_.isDynamic()) {
152 seleMan1_.setSelectionSet(evaluator1_.evaluate());
155 if (evaluator2_.isDynamic()) {
156 seleMan2_.setSelectionSet(evaluator2_.evaluate());
159 for (mol1 = seleMan1_.beginSelectedMolecule(ii); mol1 != NULL;
160 mol1 = seleMan1_.nextSelectedMolecule(ii)) {
165 Vector3d mPos = mol1->getCom();
167 for (mol2 = seleMan2_.beginSelectedMolecule(jj); mol2 != NULL;
168 mol2 = seleMan2_.nextSelectedMolecule(jj)) {
170 for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL;
171 hbd1 = mol1->nextHBondDonor(hbdi)) {
172 dPos = hbd1->donorAtom->getPos();
173 hPos = hbd1->donatedHydrogen->getPos();
175 currentSnapshot_->wrapVector(DH);
179 for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL;
180 hba2 = mol2->nextHBondAcceptor(hbaj)) {
181 aPos = hba2->getPos();
183 currentSnapshot_->wrapVector(DA);
184 DAdist = DA.length();
188 if (DAdist < rCut_) {
189 ctheta =
dot(DH, DA) / (DHdist * DAdist);
190 theta = acos(ctheta) * 180.0 / Constants::PI;
193 if (theta < thetaCut_) {
197 if (usePeriodicBoundaryConditions_)
198 currentSnapshot_->wrapVector(hPos);
199 int binNo = int(nBins_ * (halfBoxZ_ + hPos[axis_]) /
201 sliceQ_[binNo] += 1 / sliceVol;
202 sliceCount_[binNo] += 1;
209 for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL;
210 hba1 = mol1->nextHBondAcceptor(hbai)) {
211 aPos = hba1->getPos();
214 for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL;
215 hbd2 = mol2->nextHBondDonor(hbdj)) {
216 dPos = hbd2->donorAtom->getPos();
219 currentSnapshot_->wrapVector(DA);
220 DAdist = DA.length();
224 if (DAdist < rCut_) {
225 hPos = hbd2->donatedHydrogen->getPos();
227 currentSnapshot_->wrapVector(DH);
228 DHdist = DH.length();
229 ctheta =
dot(DH, DA) / (DHdist * DAdist);
230 theta = acos(ctheta) * 180.0 / Constants::PI;
232 if (theta < thetaCut_) {
236 if (usePeriodicBoundaryConditions_)
237 currentSnapshot_->wrapVector(hPos);
238 int binNo = int(nBins_ * (halfBoxZ_ + hPos[axis_]) /
240 sliceQ_[binNo] += 1 / sliceVol;
241 sliceCount_[binNo] += 1;
252 void HBondZvol::writeDensity() {
256 for (std::vector<RealType>::iterator j = zBox_.begin(); j != zBox_.end();
260 RealType zAve = zSum / zBox_.size();
262 DumpReader reader(info_, dumpFilename_);
263 int nFrames = reader.getNFrames();
265 std::ofstream qZstream(outputFilename_.c_str());
266 if (qZstream.is_open()) {
267 qZstream <<
"#Hydrogen Bonds (" << axisLabel_ <<
")\n";
269 qZstream <<
"#nFrames:\t" << zBox_.size() <<
"\n";
270 qZstream <<
"#selection 1: (" << selectionScript1_ <<
")\n";
271 qZstream <<
"#selection 2: (" << selectionScript2_ <<
")\n";
272 qZstream <<
"#" << axisLabel_
273 <<
"\tHydrogen Bond Density (molecules/A^3)\n";
274 for (
unsigned int i = 0; i < sliceQ_.size(); ++i) {
275 RealType z = zAve * (i + 0.5) / sliceQ_.size();
276 if (sliceCount_[i] != 0) {
277 qZstream << z <<
"\t" << sliceQ_[i] / nFrames <<
"\n";
279 qZstream << z <<
"\t" << 0 <<
"\n";
283 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
284 "HBondZvol: unable to open %s\n", outputFilename_.c_str());
285 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)