| 1 | 
/* | 
| 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 | 
 *  RhoZ.cpp | 
| 43 | 
 *  OOPSE-2.0 | 
| 44 | 
 * | 
| 45 | 
 *  Created by Charles F. Vardeman II on 11/26/05. | 
| 46 | 
 *  @author  Charles F. Vardeman II  | 
| 47 | 
 *  @version $Id: RhoZ.cpp,v 1.7 2008-06-30 17:53:42 gpuliti Exp $ | 
| 48 | 
 * | 
| 49 | 
 */ | 
| 50 | 
 | 
| 51 | 
/* Calculates Rho(Z) for density profile of liquid slab. */ | 
| 52 | 
 | 
| 53 | 
#include <algorithm> | 
| 54 | 
#include <fstream> | 
| 55 | 
#include "applications/staticProps/RhoZ.hpp" | 
| 56 | 
#include "utils/simError.h" | 
| 57 | 
#include "io/DumpReader.hpp" | 
| 58 | 
#include "primitives/Molecule.hpp" | 
| 59 | 
namespace oopse { | 
| 60 | 
   | 
| 61 | 
  RhoZ::RhoZ(SimInfo* info, const std::string& filename, const std::string& sele, int nzbins) | 
| 62 | 
    : StaticAnalyser(info, filename), selectionScript_(sele),  evaluator_(info), seleMan_(info), nZBins_(nzbins){ | 
| 63 | 
 | 
| 64 | 
    evaluator_.loadScriptString(sele); | 
| 65 | 
    if (!evaluator_.isDynamic()) { | 
| 66 | 
      seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 67 | 
    }        | 
| 68 | 
     | 
| 69 | 
    // fixed number of bins | 
| 70 | 
 | 
| 71 | 
    sliceSDLists_.resize(nZBins_); | 
| 72 | 
    density_.resize(nZBins_); | 
| 73 | 
     | 
| 74 | 
    setOutputName(getPrefix(filename) + ".RhoZ"); | 
| 75 | 
  } | 
| 76 | 
 | 
| 77 | 
  void RhoZ::process() { | 
| 78 | 
    Molecule* mol; | 
| 79 | 
    RigidBody* rb; | 
| 80 | 
    StuntDouble* sd; | 
| 81 | 
    SimInfo::MoleculeIterator mi; | 
| 82 | 
    Molecule::RigidBodyIterator rbIter; | 
| 83 | 
 | 
| 84 | 
    DumpReader reader(info_, dumpFilename_);     | 
| 85 | 
    int nFrames = reader.getNFrames(); | 
| 86 | 
    nProcessed_ = nFrames/step_; | 
| 87 | 
 | 
| 88 | 
    for (int istep = 0; istep < nFrames; istep += step_) { | 
| 89 | 
      reader.readFrame(istep); | 
| 90 | 
      currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 91 | 
 | 
| 92 | 
      for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 93 | 
        //change the positions of atoms which belong to the rigidbodies | 
| 94 | 
        for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 95 | 
          rb->updateAtoms(); | 
| 96 | 
        } | 
| 97 | 
      } | 
| 98 | 
 | 
| 99 | 
      int i;     | 
| 100 | 
      for (i=0; i < nZBins_; i++) { | 
| 101 | 
        sliceSDLists_[i].clear(); | 
| 102 | 
      } | 
| 103 | 
 | 
| 104 | 
      RealType sliceVolume = currentSnapshot_->getVolume() /nZBins_; | 
| 105 | 
      Mat3x3d hmat = currentSnapshot_->getHmat(); | 
| 106 | 
      zBox_.push_back(hmat(2,2)); | 
| 107 | 
       | 
| 108 | 
      RealType halfBoxZ_ = hmat(2,2) / 2.0;       | 
| 109 | 
 | 
| 110 | 
      if (evaluator_.isDynamic()) { | 
| 111 | 
        seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 112 | 
      } | 
| 113 | 
       | 
| 114 | 
      //wrap the stuntdoubles into a cell       | 
| 115 | 
      for (sd = seleMan_.beginSelected(i); sd != NULL; sd = seleMan_.nextSelected(i)) { | 
| 116 | 
        Vector3d pos = sd->getPos(); | 
| 117 | 
        if (usePeriodicBoundaryConditions_) | 
| 118 | 
          currentSnapshot_->wrapVector(pos); | 
| 119 | 
        sd->setPos(pos); | 
| 120 | 
      } | 
| 121 | 
       | 
| 122 | 
      //determine which atom belongs to which slice | 
| 123 | 
      for (sd = seleMan_.beginSelected(i); sd != NULL; sd = seleMan_.nextSelected(i)) { | 
| 124 | 
        Vector3d pos = sd->getPos(); | 
| 125 | 
        // shift molecules by half a box to have bins start at 0 | 
| 126 | 
        int binNo = int(nZBins_ * (halfBoxZ_ + pos.z()) / hmat(2,2)); | 
| 127 | 
        sliceSDLists_[binNo].push_back(sd); | 
| 128 | 
      } | 
| 129 | 
 | 
| 130 | 
      //loop over the slices to calculate the densities | 
| 131 | 
      for (i = 0; i < nZBins_; i++) { | 
| 132 | 
        RealType totalMass = 0; | 
| 133 | 
        for (int k = 0; k < sliceSDLists_[i].size(); ++k) { | 
| 134 | 
          totalMass += sliceSDLists_[i][k]->getMass(); | 
| 135 | 
        } | 
| 136 | 
        density_[i] += totalMass/sliceVolume; | 
| 137 | 
      } | 
| 138 | 
    } | 
| 139 | 
     | 
| 140 | 
    writeDensity(); | 
| 141 | 
 | 
| 142 | 
  } | 
| 143 | 
   | 
| 144 | 
   | 
| 145 | 
   | 
| 146 | 
  void RhoZ::writeDensity() { | 
| 147 | 
 | 
| 148 | 
    // compute average box length: | 
| 149 | 
    std::vector<RealType>::iterator j; | 
| 150 | 
    RealType zSum = 0.0; | 
| 151 | 
    for (j = zBox_.begin(); j != zBox_.end(); ++j) { | 
| 152 | 
      zSum += *j;        | 
| 153 | 
    } | 
| 154 | 
    RealType zAve = zSum / zBox_.size(); | 
| 155 | 
 | 
| 156 | 
    std::ofstream rdfStream(outputFilename_.c_str()); | 
| 157 | 
    if (rdfStream.is_open()) { | 
| 158 | 
      rdfStream << "#RhoZ\n"; | 
| 159 | 
      rdfStream << "#nFrames:\t" << nProcessed_ << "\n"; | 
| 160 | 
      rdfStream << "#selection: (" << selectionScript_ << ")\n"; | 
| 161 | 
      rdfStream << "#z\tdensity\n"; | 
| 162 | 
      for (int i = 0; i < density_.size(); ++i) { | 
| 163 | 
        RealType z = zAve * (i+0.5)/density_.size(); | 
| 164 | 
        rdfStream << z << "\t" << 1.660535*density_[i]/nProcessed_ << "\n"; | 
| 165 | 
      } | 
| 166 | 
       | 
| 167 | 
    } else { | 
| 168 | 
       | 
| 169 | 
      sprintf(painCave.errMsg, "RhoZ: unable to open %s\n", outputFilename_.c_str()); | 
| 170 | 
      painCave.isFatal = 1; | 
| 171 | 
      simError();   | 
| 172 | 
    } | 
| 173 | 
     | 
| 174 | 
    rdfStream.close(); | 
| 175 | 
  } | 
| 176 | 
   | 
| 177 | 
} | 
| 178 | 
 |