| 1 | tim | 545 | /* | 
| 2 |  |  | * Copyright (c) 2005 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 | gezelter | 1390 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | tim | 545 | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 | gezelter | 1390 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | tim | 545 | *    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 | gezelter | 1390 | * | 
| 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 | gezelter | 1879 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | gezelter | 1782 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 |  |  | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | tim | 545 | */ | 
| 42 |  |  |  | 
| 43 |  |  | #include <algorithm> | 
| 44 | tim | 565 | #include <functional> | 
| 45 | tim | 545 | #include "applications/staticProps/DensityPlot.hpp" | 
| 46 |  |  | #include "utils/simError.h" | 
| 47 |  |  | #include "io/DumpReader.hpp" | 
| 48 |  |  | #include "primitives/Molecule.hpp" | 
| 49 |  |  | #include "utils/NumericConstant.hpp" | 
| 50 | gezelter | 1782 | #include "types/LennardJonesAdapter.hpp" | 
| 51 |  |  |  | 
| 52 | gezelter | 1390 | namespace OpenMD { | 
| 53 | tim | 545 |  | 
| 54 | gezelter | 2071 |  | 
| 55 |  |  | DensityPlot::DensityPlot(SimInfo* info, const std::string& filename, | 
| 56 |  |  | const std::string& sele, const std::string& cmSele, | 
| 57 |  |  | RealType len, int nrbins) | 
| 58 |  |  | : StaticAnalyser(info, filename), | 
| 59 |  |  | len_(len), halfLen_(len/2), nRBins_(nrbins), | 
| 60 |  |  | selectionScript_(sele), seleMan_(info), evaluator_(info), | 
| 61 |  |  | cmSelectionScript_(cmSele), cmSeleMan_(info), cmEvaluator_(info) { | 
| 62 | tim | 545 |  | 
| 63 |  |  | setOutputName(getPrefix(filename) + ".density"); | 
| 64 | gezelter | 2071 |  | 
| 65 | tim | 545 | deltaR_ = len_ /nRBins_; | 
| 66 |  |  | histogram_.resize(nRBins_); | 
| 67 |  |  | density_.resize(nRBins_); | 
| 68 |  |  |  | 
| 69 |  |  | std::fill(histogram_.begin(), histogram_.end(), 0); | 
| 70 |  |  |  | 
| 71 |  |  | evaluator_.loadScriptString(sele); | 
| 72 |  |  |  | 
| 73 |  |  | if (!evaluator_.isDynamic()) { | 
| 74 |  |  | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 75 |  |  | } | 
| 76 | tim | 558 |  | 
| 77 |  |  | cmEvaluator_.loadScriptString(cmSele); | 
| 78 |  |  | if (!cmEvaluator_.isDynamic()) { | 
| 79 |  |  | cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate()); | 
| 80 | gezelter | 2071 | } | 
| 81 | tim | 545 | } | 
| 82 |  |  |  | 
| 83 | gezelter | 1782 | void DensityPlot::process() { | 
| 84 |  |  | Molecule* mol; | 
| 85 |  |  | RigidBody* rb; | 
| 86 |  |  | SimInfo::MoleculeIterator mi; | 
| 87 |  |  | Molecule::RigidBodyIterator rbIter; | 
| 88 | tim | 545 |  | 
| 89 | gezelter | 1782 | DumpReader reader(info_, dumpFilename_); | 
| 90 |  |  | int nFrames = reader.getNFrames(); | 
| 91 |  |  | for (int i = 0; i < nFrames; i += step_) { | 
| 92 |  |  | reader.readFrame(i); | 
| 93 |  |  | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 94 |  |  |  | 
| 95 | gezelter | 2071 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 96 |  |  | mol = info_->nextMolecule(mi)) { | 
| 97 | tim | 545 | //change the positions of atoms which belong to the rigidbodies | 
| 98 | gezelter | 2071 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 99 |  |  | rb = mol->nextRigidBody(rbIter)) { | 
| 100 | gezelter | 1782 | rb->updateAtoms(); | 
| 101 | tim | 545 | } | 
| 102 |  |  |  | 
| 103 | gezelter | 1782 | } | 
| 104 | tim | 545 |  | 
| 105 | gezelter | 1782 | if (evaluator_.isDynamic()) { | 
| 106 | tim | 545 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 107 | gezelter | 1782 | } | 
| 108 | tim | 545 |  | 
| 109 | gezelter | 1782 | if (cmEvaluator_.isDynamic()) { | 
| 110 | tim | 558 | cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate()); | 
| 111 | gezelter | 1782 | } | 
| 112 | tim | 558 |  | 
| 113 | gezelter | 1782 | Vector3d origin = calcNewOrigin(); | 
| 114 | tim | 558 |  | 
| 115 | gezelter | 1782 | Mat3x3d hmat = currentSnapshot_->getHmat(); | 
| 116 |  |  | RealType slabVolume = deltaR_ * hmat(0, 0) * hmat(1, 1); | 
| 117 |  |  | int k; | 
| 118 | gezelter | 2071 | for (StuntDouble* sd = seleMan_.beginSelected(k); sd != NULL; | 
| 119 |  |  | sd = seleMan_.nextSelected(k)) { | 
| 120 | tim | 545 |  | 
| 121 | tim | 558 |  | 
| 122 | gezelter | 1782 | if (!sd->isAtom()) { | 
| 123 | gezelter | 2071 | sprintf( painCave.errMsg, | 
| 124 |  |  | "Can not calculate electron density if it is not atom\n"); | 
| 125 | gezelter | 1782 | painCave.severity = OPENMD_ERROR; | 
| 126 |  |  | painCave.isFatal = 1; | 
| 127 |  |  | simError(); | 
| 128 |  |  | } | 
| 129 | tim | 558 |  | 
| 130 | gezelter | 1782 | Atom* atom = static_cast<Atom*>(sd); | 
| 131 |  |  | GenericData* data = atom->getAtomType()->getPropertyByName("nelectron"); | 
| 132 |  |  | if (data == NULL) { | 
| 133 |  |  | sprintf( painCave.errMsg, "Can not find Parameters for nelectron\n"); | 
| 134 |  |  | painCave.severity = OPENMD_ERROR; | 
| 135 |  |  | painCave.isFatal = 1; | 
| 136 |  |  | simError(); | 
| 137 |  |  | } | 
| 138 | tim | 558 |  | 
| 139 | gezelter | 1782 | DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data); | 
| 140 |  |  | if (doubleData == NULL) { | 
| 141 |  |  | sprintf( painCave.errMsg, | 
| 142 |  |  | "Can not cast GenericData to DoubleGenericData\n"); | 
| 143 |  |  | painCave.severity = OPENMD_ERROR; | 
| 144 |  |  | painCave.isFatal = 1; | 
| 145 |  |  | simError(); | 
| 146 |  |  | } | 
| 147 | tim | 558 |  | 
| 148 | gezelter | 1782 | RealType nelectron = doubleData->getData(); | 
| 149 |  |  | LennardJonesAdapter lja = LennardJonesAdapter(atom->getAtomType()); | 
| 150 |  |  | RealType sigma = lja.getSigma() * 0.5; | 
| 151 |  |  | RealType sigma2 = sigma * sigma; | 
| 152 | tim | 558 |  | 
| 153 | gezelter | 1782 | Vector3d pos = sd->getPos() - origin; | 
| 154 |  |  | for (int j =0; j < nRBins_; ++j) { | 
| 155 |  |  | Vector3d tmp(pos); | 
| 156 |  |  | RealType zdist =j * deltaR_ - halfLen_; | 
| 157 |  |  | tmp[2] += zdist; | 
| 158 |  |  | if (usePeriodicBoundaryConditions_) | 
| 159 |  |  | currentSnapshot_->wrapVector(tmp); | 
| 160 |  |  |  | 
| 161 |  |  | RealType wrappedZdist = tmp.z() + halfLen_; | 
| 162 |  |  | if (wrappedZdist < 0.0 || wrappedZdist > len_) { | 
| 163 |  |  | continue; | 
| 164 |  |  | } | 
| 165 |  |  |  | 
| 166 | gezelter | 1790 | int which = int(wrappedZdist / deltaR_); | 
| 167 | gezelter | 1782 | density_[which] += nelectron * exp(-zdist*zdist/(sigma2*2.0)) /(slabVolume* sqrt(2*NumericConstant::PI*sigma*sigma)); | 
| 168 |  |  |  | 
| 169 |  |  | } | 
| 170 |  |  | } | 
| 171 | tim | 545 | } | 
| 172 | gezelter | 1782 |  | 
| 173 |  |  | int nProcessed = nFrames /step_; | 
| 174 | gezelter | 2071 | std::transform(density_.begin(), density_.end(), density_.begin(), | 
| 175 |  |  | std::bind2nd(std::divides<RealType>(), nProcessed)); | 
| 176 | gezelter | 1782 | writeDensity(); | 
| 177 | tim | 545 |  | 
| 178 |  |  |  | 
| 179 |  |  |  | 
| 180 | gezelter | 1782 | } | 
| 181 | tim | 545 |  | 
| 182 | gezelter | 1782 | Vector3d DensityPlot::calcNewOrigin() { | 
| 183 | tim | 558 |  | 
| 184 |  |  | int i; | 
| 185 |  |  | Vector3d newOrigin(0.0); | 
| 186 | tim | 963 | RealType totalMass = 0.0; | 
| 187 | gezelter | 2071 | for (StuntDouble* sd = seleMan_.beginSelected(i); sd != NULL; | 
| 188 |  |  | sd = seleMan_.nextSelected(i)) { | 
| 189 | gezelter | 1782 | RealType mass = sd->getMass(); | 
| 190 |  |  | totalMass += mass; | 
| 191 |  |  | newOrigin += sd->getPos() * mass; | 
| 192 | tim | 558 | } | 
| 193 |  |  | newOrigin /= totalMass; | 
| 194 |  |  | return newOrigin; | 
| 195 | gezelter | 1782 | } | 
| 196 | tim | 558 |  | 
| 197 | gezelter | 1782 | void DensityPlot::writeDensity() { | 
| 198 | tim | 545 | std::ofstream ofs(outputFilename_.c_str(), std::ios::binary); | 
| 199 |  |  | if (ofs.is_open()) { | 
| 200 |  |  | ofs << "#g(x, y, z)\n"; | 
| 201 | tim | 558 | ofs << "#selection: (" << selectionScript_ << ")\n"; | 
| 202 |  |  | ofs << "#cmSelection:(" << cmSelectionScript_ << ")\n"; | 
| 203 | gezelter | 2071 | ofs << "#nRBins = " << nRBins_ << "\t maxLen = " | 
| 204 |  |  | << len_ << "\tdeltaR = " << deltaR_ <<"\n"; | 
| 205 | gezelter | 1782 | for (unsigned int i = 0; i < histogram_.size(); ++i) { | 
| 206 |  |  | ofs << i*deltaR_ - halfLen_ <<"\t" << density_[i]<< std::endl; | 
| 207 | tim | 545 | } | 
| 208 |  |  | } else { | 
| 209 |  |  |  | 
| 210 | gezelter | 2071 | sprintf(painCave.errMsg, "DensityPlot: unable to open %s\n", | 
| 211 |  |  | outputFilename_.c_str()); | 
| 212 | tim | 545 | painCave.isFatal = 1; | 
| 213 |  |  | simError(); | 
| 214 |  |  | } | 
| 215 |  |  |  | 
| 216 |  |  | ofs.close(); | 
| 217 |  |  |  | 
| 218 |  |  |  | 
| 219 | gezelter | 1782 | } | 
| 220 | tim | 545 |  | 
| 221 |  |  | } | 
| 222 |  |  |  | 
| 223 |  |  |  |