| 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 | 
  | 
  | 
 * 1. Acknowledgement of the program authors must be made in any | 
| 10 | 
  | 
  | 
 *    publication of scientific results based in part on use of the | 
| 11 | 
  | 
  | 
 *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | 
  | 
  | 
 *    the article in which the program was described (Matthew | 
| 13 | 
  | 
  | 
 *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | 
  | 
  | 
 *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | 
  | 
  | 
 *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | 
  | 
  | 
 *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | 
  | 
  | 
 * | 
| 18 | 
  | 
  | 
 * 2. Redistributions of source code must retain the above copyright | 
| 19 | 
  | 
  | 
 *    notice, this list of conditions and the following disclaimer. | 
| 20 | 
  | 
  | 
 * | 
| 21 | 
  | 
  | 
 * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 | 
  | 
  | 
 *    notice, this list of conditions and the following disclaimer in the | 
| 23 | 
  | 
  | 
 *    documentation and/or other materials provided with the | 
| 24 | 
  | 
  | 
 *    distribution. | 
| 25 | 
  | 
  | 
 * | 
| 26 | 
  | 
  | 
 * This software is provided "AS IS," without a warranty of any | 
| 27 | 
  | 
  | 
 * kind. All express or implied conditions, representations and | 
| 28 | 
  | 
  | 
 * warranties, including any implied warranty of merchantability, | 
| 29 | 
  | 
  | 
 * fitness for a particular purpose or non-infringement, are hereby | 
| 30 | 
  | 
  | 
 * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 | 
  | 
  | 
 * be liable for any damages suffered by licensee as a result of | 
| 32 | 
  | 
  | 
 * using, modifying or distributing the software or its | 
| 33 | 
  | 
  | 
 * derivatives. In no event will the University of Notre Dame or its | 
| 34 | 
  | 
  | 
 * licensors be liable for any lost revenue, profit or data, or for | 
| 35 | 
  | 
  | 
 * direct, indirect, special, consequential, incidental or punitive | 
| 36 | 
  | 
  | 
 * damages, however caused and regardless of the theory of liability, | 
| 37 | 
  | 
  | 
 * arising out of the use of or inability to use software, even if the | 
| 38 | 
  | 
  | 
 * University of Notre Dame has been advised of the possibility of | 
| 39 | 
  | 
  | 
 * such damages. | 
| 40 | 
  | 
  | 
 */ | 
| 41 | 
  | 
  | 
 | 
| 42 | 
  | 
  | 
#include <algorithm> | 
| 43 | 
tim | 
565 | 
#include <functional> | 
| 44 | 
tim | 
545 | 
#include "applications/staticProps/DensityPlot.hpp" | 
| 45 | 
  | 
  | 
#include "utils/simError.h" | 
| 46 | 
  | 
  | 
#include "io/DumpReader.hpp" | 
| 47 | 
  | 
  | 
#include "primitives/Molecule.hpp" | 
| 48 | 
  | 
  | 
#include "utils/NumericConstant.hpp" | 
| 49 | 
  | 
  | 
namespace oopse { | 
| 50 | 
  | 
  | 
 | 
| 51 | 
  | 
  | 
 | 
| 52 | 
tim | 
963 | 
DensityPlot::DensityPlot(SimInfo* info, const std::string& filename, const std::string& sele, const std::string& cmSele, RealType len, int nrbins) | 
| 53 | 
tim | 
545 | 
  : StaticAnalyser(info, filename), selectionScript_(sele), evaluator_(info), seleMan_(info),  | 
| 54 | 
tim | 
558 | 
    cmSelectionScript_(cmSele), cmEvaluator_(info), cmSeleMan_(info),      | 
| 55 | 
tim | 
545 | 
    len_(len), nRBins_(nrbins), halfLen_(len/2)     { | 
| 56 | 
  | 
  | 
 | 
| 57 | 
  | 
  | 
    setOutputName(getPrefix(filename) + ".density"); | 
| 58 | 
  | 
  | 
 | 
| 59 | 
  | 
  | 
    deltaR_ = len_ /nRBins_;   | 
| 60 | 
  | 
  | 
    histogram_.resize(nRBins_); | 
| 61 | 
  | 
  | 
    density_.resize(nRBins_); | 
| 62 | 
  | 
  | 
     | 
| 63 | 
  | 
  | 
    std::fill(histogram_.begin(), histogram_.end(), 0);   | 
| 64 | 
  | 
  | 
     | 
| 65 | 
  | 
  | 
    evaluator_.loadScriptString(sele); | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
    if (!evaluator_.isDynamic()) { | 
| 68 | 
  | 
  | 
      seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 69 | 
  | 
  | 
    } | 
| 70 | 
tim | 
558 | 
 | 
| 71 | 
  | 
  | 
    cmEvaluator_.loadScriptString(cmSele); | 
| 72 | 
  | 
  | 
    if (!cmEvaluator_.isDynamic()) { | 
| 73 | 
  | 
  | 
      cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate()); | 
| 74 | 
  | 
  | 
    } | 
| 75 | 
tim | 
545 | 
     | 
| 76 | 
tim | 
558 | 
     | 
| 77 | 
tim | 
545 | 
  } | 
| 78 | 
  | 
  | 
 | 
| 79 | 
  | 
  | 
void DensityPlot::process() { | 
| 80 | 
  | 
  | 
  Molecule* mol; | 
| 81 | 
  | 
  | 
  RigidBody* rb; | 
| 82 | 
  | 
  | 
  SimInfo::MoleculeIterator mi; | 
| 83 | 
  | 
  | 
  Molecule::RigidBodyIterator rbIter; | 
| 84 | 
  | 
  | 
   | 
| 85 | 
  | 
  | 
  DumpReader reader(info_, dumpFilename_);     | 
| 86 | 
  | 
  | 
  int nFrames = reader.getNFrames(); | 
| 87 | 
tim | 
565 | 
  for (int i = 0; i < nFrames; i += step_) { | 
| 88 | 
tim | 
545 | 
    reader.readFrame(i); | 
| 89 | 
  | 
  | 
    currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 92 | 
  | 
  | 
        //change the positions of atoms which belong to the rigidbodies | 
| 93 | 
  | 
  | 
        for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 94 | 
  | 
  | 
            rb->updateAtoms(); | 
| 95 | 
  | 
  | 
        } | 
| 96 | 
  | 
  | 
         | 
| 97 | 
  | 
  | 
    } | 
| 98 | 
  | 
  | 
     | 
| 99 | 
  | 
  | 
    if (evaluator_.isDynamic()) { | 
| 100 | 
  | 
  | 
        seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 101 | 
  | 
  | 
    } | 
| 102 | 
  | 
  | 
 | 
| 103 | 
tim | 
558 | 
    if (cmEvaluator_.isDynamic()) { | 
| 104 | 
  | 
  | 
        cmSeleMan_.setSelectionSet(cmEvaluator_.evaluate()); | 
| 105 | 
  | 
  | 
    } | 
| 106 | 
  | 
  | 
 | 
| 107 | 
  | 
  | 
    Vector3d origin = calcNewOrigin(); | 
| 108 | 
  | 
  | 
 | 
| 109 | 
  | 
  | 
    Mat3x3d hmat = currentSnapshot_->getHmat(); | 
| 110 | 
tim | 
963 | 
    RealType slabVolume = deltaR_ * hmat(0, 0) * hmat(1, 1); | 
| 111 | 
tim | 
565 | 
    int k;  | 
| 112 | 
  | 
  | 
    for (StuntDouble* sd = seleMan_.beginSelected(k); sd != NULL; sd = seleMan_.nextSelected(k)) { | 
| 113 | 
tim | 
545 | 
 | 
| 114 | 
tim | 
558 | 
 | 
| 115 | 
  | 
  | 
            if (!sd->isAtom()) { | 
| 116 | 
  | 
  | 
                sprintf( painCave.errMsg, "Can not calculate electron density if it is not atom\n"); | 
| 117 | 
  | 
  | 
                painCave.severity = OOPSE_ERROR; | 
| 118 | 
  | 
  | 
                painCave.isFatal = 1; | 
| 119 | 
  | 
  | 
                simError();  | 
| 120 | 
  | 
  | 
            } | 
| 121 | 
  | 
  | 
             | 
| 122 | 
  | 
  | 
            Atom* atom = static_cast<Atom*>(sd); | 
| 123 | 
  | 
  | 
            GenericData* data = atom->getAtomType()->getPropertyByName("nelectron"); | 
| 124 | 
  | 
  | 
            if (data == NULL) { | 
| 125 | 
  | 
  | 
                sprintf( painCave.errMsg, "Can not find Parameters for nelectron\n"); | 
| 126 | 
  | 
  | 
                painCave.severity = OOPSE_ERROR; | 
| 127 | 
  | 
  | 
                painCave.isFatal = 1; | 
| 128 | 
  | 
  | 
                simError();  | 
| 129 | 
  | 
  | 
            } | 
| 130 | 
  | 
  | 
             | 
| 131 | 
  | 
  | 
            DoubleGenericData* doubleData = dynamic_cast<DoubleGenericData*>(data); | 
| 132 | 
  | 
  | 
            if (doubleData == NULL) { | 
| 133 | 
  | 
  | 
                sprintf( painCave.errMsg, | 
| 134 | 
  | 
  | 
                     "Can not cast GenericData to DoubleGenericData\n"); | 
| 135 | 
  | 
  | 
                painCave.severity = OOPSE_ERROR; | 
| 136 | 
  | 
  | 
                painCave.isFatal = 1; | 
| 137 | 
  | 
  | 
                simError();    | 
| 138 | 
  | 
  | 
            } | 
| 139 | 
  | 
  | 
             | 
| 140 | 
tim | 
963 | 
            RealType nelectron = doubleData->getData(); | 
| 141 | 
tim | 
558 | 
 | 
| 142 | 
  | 
  | 
            data = atom->getAtomType()->getPropertyByName("LennardJones"); | 
| 143 | 
  | 
  | 
            if (data == NULL) { | 
| 144 | 
  | 
  | 
                sprintf( painCave.errMsg, "Can not find Parameters for LennardJones\n"); | 
| 145 | 
  | 
  | 
                painCave.severity = OOPSE_ERROR; | 
| 146 | 
  | 
  | 
                painCave.isFatal = 1; | 
| 147 | 
  | 
  | 
                simError();  | 
| 148 | 
  | 
  | 
            } | 
| 149 | 
  | 
  | 
 | 
| 150 | 
  | 
  | 
            LJParamGenericData* ljData = dynamic_cast<LJParamGenericData*>(data); | 
| 151 | 
  | 
  | 
            if (ljData == NULL) { | 
| 152 | 
  | 
  | 
                sprintf( painCave.errMsg, | 
| 153 | 
  | 
  | 
                     "Can not cast GenericData to LJParam\n"); | 
| 154 | 
  | 
  | 
                painCave.severity = OOPSE_ERROR; | 
| 155 | 
  | 
  | 
                painCave.isFatal = 1; | 
| 156 | 
  | 
  | 
                simError();           | 
| 157 | 
  | 
  | 
            } | 
| 158 | 
  | 
  | 
 | 
| 159 | 
  | 
  | 
            LJParam ljParam = ljData->getData(); | 
| 160 | 
tim | 
963 | 
            RealType sigma = ljParam.sigma * 0.5; | 
| 161 | 
  | 
  | 
            RealType sigma2 = sigma * sigma; | 
| 162 | 
tim | 
558 | 
 | 
| 163 | 
  | 
  | 
            Vector3d pos = sd->getPos() - origin; | 
| 164 | 
  | 
  | 
            for (int j =0; j < nRBins_; ++j) { | 
| 165 | 
  | 
  | 
                Vector3d tmp(pos); | 
| 166 | 
tim | 
963 | 
                RealType zdist =j * deltaR_ - halfLen_; | 
| 167 | 
tim | 
558 | 
                tmp[2] += zdist; | 
| 168 | 
  | 
  | 
                currentSnapshot_->wrapVector(tmp); | 
| 169 | 
  | 
  | 
 | 
| 170 | 
tim | 
963 | 
                RealType wrappedZdist = tmp.z() + halfLen_; | 
| 171 | 
tim | 
558 | 
                if (wrappedZdist < 0.0 || wrappedZdist > len_) { | 
| 172 | 
  | 
  | 
                    continue; | 
| 173 | 
  | 
  | 
                } | 
| 174 | 
  | 
  | 
                 | 
| 175 | 
  | 
  | 
                int which =wrappedZdist / deltaR_;         | 
| 176 | 
  | 
  | 
                density_[which] += nelectron * exp(-zdist*zdist/(sigma2*2.0)) /(slabVolume* sqrt(2*NumericConstant::PI*sigma*sigma)); | 
| 177 | 
  | 
  | 
                     | 
| 178 | 
  | 
  | 
            } | 
| 179 | 
  | 
  | 
             | 
| 180 | 
  | 
  | 
             | 
| 181 | 
  | 
  | 
             | 
| 182 | 
tim | 
545 | 
        }         | 
| 183 | 
  | 
  | 
    } | 
| 184 | 
tim | 
558 | 
 | 
| 185 | 
  | 
  | 
  int nProcessed = nFrames /step_; | 
| 186 | 
tim | 
963 | 
  std::transform(density_.begin(), density_.end(), density_.begin(), std::bind2nd(std::divides<RealType>(), nProcessed));   | 
| 187 | 
tim | 
558 | 
  writeDensity(); | 
| 188 | 
tim | 
545 | 
         | 
| 189 | 
  | 
  | 
 | 
| 190 | 
  | 
  | 
   | 
| 191 | 
  | 
  | 
} | 
| 192 | 
  | 
  | 
 | 
| 193 | 
tim | 
558 | 
Vector3d DensityPlot::calcNewOrigin() { | 
| 194 | 
  | 
  | 
 | 
| 195 | 
  | 
  | 
    int i; | 
| 196 | 
  | 
  | 
    Vector3d newOrigin(0.0); | 
| 197 | 
tim | 
963 | 
    RealType totalMass = 0.0; | 
| 198 | 
tim | 
558 | 
    for (StuntDouble* sd = seleMan_.beginSelected(i); sd != NULL; sd = seleMan_.nextSelected(i)) { | 
| 199 | 
tim | 
963 | 
        RealType mass = sd->getMass(); | 
| 200 | 
tim | 
558 | 
        totalMass += mass; | 
| 201 | 
  | 
  | 
        newOrigin += sd->getPos() * mass;         | 
| 202 | 
  | 
  | 
    } | 
| 203 | 
  | 
  | 
    newOrigin /= totalMass; | 
| 204 | 
  | 
  | 
    return newOrigin; | 
| 205 | 
  | 
  | 
} | 
| 206 | 
  | 
  | 
 | 
| 207 | 
tim | 
545 | 
void DensityPlot::writeDensity() { | 
| 208 | 
  | 
  | 
    std::ofstream ofs(outputFilename_.c_str(), std::ios::binary); | 
| 209 | 
  | 
  | 
    if (ofs.is_open()) { | 
| 210 | 
  | 
  | 
      ofs << "#g(x, y, z)\n"; | 
| 211 | 
tim | 
558 | 
      ofs << "#selection: (" << selectionScript_ << ")\n"; | 
| 212 | 
  | 
  | 
      ofs << "#cmSelection:(" << cmSelectionScript_ << ")\n"; | 
| 213 | 
  | 
  | 
      ofs << "#nRBins = " << nRBins_ << "\t maxLen = " << len_ << "\tdeltaR = " << deltaR_ <<"\n"; | 
| 214 | 
tim | 
545 | 
      for (int i = 0; i < histogram_.size(); ++i) { | 
| 215 | 
  | 
  | 
          ofs << i*deltaR_ - halfLen_ <<"\t" << density_[i]<< std::endl; | 
| 216 | 
  | 
  | 
      }         | 
| 217 | 
  | 
  | 
    } else { | 
| 218 | 
  | 
  | 
 | 
| 219 | 
  | 
  | 
      sprintf(painCave.errMsg, "DensityPlot: unable to open %s\n", outputFilename_.c_str()); | 
| 220 | 
  | 
  | 
      painCave.isFatal = 1; | 
| 221 | 
  | 
  | 
      simError();   | 
| 222 | 
  | 
  | 
    } | 
| 223 | 
  | 
  | 
 | 
| 224 | 
  | 
  | 
    ofs.close(); | 
| 225 | 
  | 
  | 
 | 
| 226 | 
  | 
  | 
 | 
| 227 | 
  | 
  | 
} | 
| 228 | 
  | 
  | 
 | 
| 229 | 
  | 
  | 
} | 
| 230 | 
  | 
  | 
 | 
| 231 | 
  | 
  | 
 |