| 1 | /* | 
| 2 | * Copyright (c) 2007 The University of Notre Dame. All Rights Reserved. | 
| 3 | * | 
| 4 | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | * non-exclusive, royalty free, license to use, modify and | 
| 6 | * redistribute this software in source and binary code form, provided | 
| 7 | * that the following conditions are met: | 
| 8 | * | 
| 9 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 | *    documentation and/or other materials provided with the | 
| 15 | *    distribution. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 28 | * arising out of the use of or inability to use software, even if the | 
| 29 | * University of Notre Dame has been advised of the possibility of | 
| 30 | * such damages. | 
| 31 | * | 
| 32 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | * research, please cite the appropriate papers when you publish your | 
| 34 | * work.  Good starting points are: | 
| 35 | * | 
| 36 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | * [4] Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [4] , Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). * | 
| 41 | *  Created by J. Daniel Gezelter on 07/27/07. | 
| 42 | *  @author  J. Daniel Gezelter | 
| 43 | *  @version $Id$ | 
| 44 | * | 
| 45 | */ | 
| 46 |  | 
| 47 | #include "applications/staticProps/BondAngleDistribution.hpp" | 
| 48 | #include "utils/simError.h" | 
| 49 | #include "io/DumpReader.hpp" | 
| 50 | #include "primitives/Molecule.hpp" | 
| 51 | #include "utils/NumericConstant.hpp" | 
| 52 |  | 
| 53 | using namespace std; | 
| 54 | namespace OpenMD { | 
| 55 |  | 
| 56 | BondAngleDistribution::BondAngleDistribution(SimInfo* info, | 
| 57 | const string& filename, | 
| 58 | const string& sele, | 
| 59 | double rCut, int nbins) | 
| 60 | : StaticAnalyser(info, filename), selectionScript_(sele),  evaluator_(info), | 
| 61 | seleMan_(info) { | 
| 62 |  | 
| 63 | setOutputName(getPrefix(filename) + ".bad"); | 
| 64 |  | 
| 65 | evaluator_.loadScriptString(sele); | 
| 66 | if (!evaluator_.isDynamic()) { | 
| 67 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 68 | } | 
| 69 |  | 
| 70 | // Set up cutoff radius and order of the Legendre Polynomial: | 
| 71 |  | 
| 72 | rCut_ = rCut; | 
| 73 | nBins_ = nbins; | 
| 74 |  | 
| 75 |  | 
| 76 | // Theta can take values from 0 to 180 | 
| 77 | deltaTheta_ = (180.0) / nBins_; | 
| 78 | histogram_.resize(nBins_); | 
| 79 | } | 
| 80 |  | 
| 81 | BondAngleDistribution::~BondAngleDistribution() { | 
| 82 | histogram_.clear(); | 
| 83 | } | 
| 84 |  | 
| 85 | void BondAngleDistribution::initializeHistogram() { | 
| 86 | for (int bin = 0; bin < nBins_; bin++) { | 
| 87 | histogram_[bin] = 0; | 
| 88 | } | 
| 89 | } | 
| 90 |  | 
| 91 | void BondAngleDistribution::process() { | 
| 92 | Molecule* mol; | 
| 93 | Atom* atom; | 
| 94 | RigidBody* rb; | 
| 95 | int myIndex; | 
| 96 | SimInfo::MoleculeIterator mi; | 
| 97 | Molecule::RigidBodyIterator rbIter; | 
| 98 | Molecule::AtomIterator ai; | 
| 99 | StuntDouble* sd; | 
| 100 | Vector3d vec; | 
| 101 | std::vector<Vector3d> bondvec; | 
| 102 | RealType r; | 
| 103 | int nBonds; | 
| 104 | int i; | 
| 105 |  | 
| 106 | DumpReader reader(info_, dumpFilename_); | 
| 107 | int nFrames = reader.getNFrames(); | 
| 108 | frameCounter_ = 0; | 
| 109 |  | 
| 110 | nTotBonds_ = 0; | 
| 111 |  | 
| 112 | for (int istep = 0; istep < nFrames; istep += step_) { | 
| 113 | reader.readFrame(istep); | 
| 114 | frameCounter_++; | 
| 115 | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 116 |  | 
| 117 | if (evaluator_.isDynamic()) { | 
| 118 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 119 | } | 
| 120 |  | 
| 121 | // update the positions of atoms which belong to the rigidbodies | 
| 122 |  | 
| 123 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 124 | mol = info_->nextMolecule(mi)) { | 
| 125 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 126 | rb = mol->nextRigidBody(rbIter)) { | 
| 127 | rb->updateAtoms(); | 
| 128 | } | 
| 129 | } | 
| 130 |  | 
| 131 | // outer loop is over the selected StuntDoubles: | 
| 132 |  | 
| 133 | for (sd = seleMan_.beginSelected(i); sd != NULL; | 
| 134 | sd = seleMan_.nextSelected(i)) { | 
| 135 |  | 
| 136 | myIndex = sd->getGlobalIndex(); | 
| 137 | nBonds = 0; | 
| 138 | bondvec.clear(); | 
| 139 |  | 
| 140 | // inner loop is over all other atoms in the system: | 
| 141 |  | 
| 142 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 143 | mol = info_->nextMolecule(mi)) { | 
| 144 | for (atom = mol->beginAtom(ai); atom != NULL; | 
| 145 | atom = mol->nextAtom(ai)) { | 
| 146 |  | 
| 147 | if (atom->getGlobalIndex() != myIndex) { | 
| 148 |  | 
| 149 | vec = sd->getPos() - atom->getPos(); | 
| 150 |  | 
| 151 | if (usePeriodicBoundaryConditions_) | 
| 152 | currentSnapshot_->wrapVector(vec); | 
| 153 |  | 
| 154 | // Calculate "bonds" and make a pair list | 
| 155 |  | 
| 156 | r = vec.length(); | 
| 157 |  | 
| 158 | // Check to see if neighbor is in bond cutoff | 
| 159 |  | 
| 160 | if (r < rCut_) { | 
| 161 | // Add neighbor to bond list's | 
| 162 | bondvec.push_back(vec); | 
| 163 | nBonds++; | 
| 164 | nTotBonds_++; | 
| 165 | } | 
| 166 | } | 
| 167 | } | 
| 168 |  | 
| 169 |  | 
| 170 | for (int i = 0; i < nBonds-1; i++ ){ | 
| 171 | Vector3d vec1 = bondvec[i]; | 
| 172 | vec1.normalize(); | 
| 173 | for(int j = i+1; j < nBonds; j++){ | 
| 174 | Vector3d vec2 = bondvec[j]; | 
| 175 |  | 
| 176 | vec2.normalize(); | 
| 177 |  | 
| 178 | RealType theta = acos(dot(vec1,vec2))*180.0/NumericConstant::PI; | 
| 179 |  | 
| 180 |  | 
| 181 | if (theta > 180.0){ | 
| 182 | theta = 360.0 - theta; | 
| 183 | } | 
| 184 | int whichBin = int(theta/deltaTheta_); | 
| 185 |  | 
| 186 | histogram_[whichBin] += 2; | 
| 187 | } | 
| 188 | } | 
| 189 | } | 
| 190 | } | 
| 191 | } | 
| 192 |  | 
| 193 |  | 
| 194 | writeBondAngleDistribution(); | 
| 195 | } | 
| 196 |  | 
| 197 |  | 
| 198 | void BondAngleDistribution::writeBondAngleDistribution() { | 
| 199 |  | 
| 200 | std::ofstream osbad(getOutputFileName().c_str()); | 
| 201 |  | 
| 202 |  | 
| 203 | RealType norm = (RealType)nTotBonds_*((RealType)nTotBonds_-1.0)/2.0; | 
| 204 | if (osbad.is_open()) { | 
| 205 |  | 
| 206 | // Normalize by number of frames and write it out: | 
| 207 | for (int i = 0; i < nBins_; ++i) { | 
| 208 | RealType Thetaval = i * deltaTheta_; | 
| 209 | osbad << Thetaval; | 
| 210 | osbad << "\t" << (RealType)histogram_[i]/norm/frameCounter_; | 
| 211 |  | 
| 212 | osbad << "\n"; | 
| 213 | } | 
| 214 |  | 
| 215 | osbad.close(); | 
| 216 |  | 
| 217 | } else { | 
| 218 | sprintf(painCave.errMsg, "BondAngleDistribution: unable to open %s\n", | 
| 219 | (getOutputFileName() + "q").c_str()); | 
| 220 | painCave.isFatal = 1; | 
| 221 | simError(); | 
| 222 | } | 
| 223 |  | 
| 224 | } | 
| 225 | } |