| [51cdfd] | 1 | /*
 | 
|---|
 | 2 |  * AtomicForceManipulator.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Feb 23, 2011
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef ATOMICFORCEMANIPULATOR_HPP_
 | 
|---|
 | 9 | #define ATOMICFORCEMANIPULATOR_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "Atom/atom.hpp"
 | 
|---|
 | 17 | #include "Atom/AtomSet.hpp"
 | 
|---|
 | 18 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 19 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 20 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 21 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
 | 22 | #include "Fragmentation/ForceMatrix.hpp"
 | 
|---|
 | 23 | #include "Helpers/helpers.hpp"
 | 
|---|
 | 24 | #include "Helpers/defs.hpp"
 | 
|---|
 | 25 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
 | 26 | #include "Thermostats/ThermoStatContainer.hpp"
 | 
|---|
 | 27 | #include "Thermostats/Thermostat.hpp"
 | 
|---|
 | 28 | #include "World.hpp"
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | template <class T>
 | 
|---|
 | 31 | class AtomicForceManipulator
 | 
|---|
 | 32 | {
 | 
|---|
 | 33 | public:
 | 
|---|
 | 34 |   /** Constructor of class AtomicForceManipulator.
 | 
|---|
 | 35 |    *
 | 
|---|
 | 36 |    * \param _atoms set of atoms to integrate
 | 
|---|
 | 37 |    * \param _Deltat time step width in atomic units
 | 
|---|
 | 38 |    * \param _IsAngstroem whether length units are in angstroem or bohr radii
 | 
|---|
 | 39 |    */
 | 
|---|
 | 40 |   AtomicForceManipulator(AtomSetMixin<T> &_atoms, double _Deltat, bool _IsAngstroem) :
 | 
|---|
 | 41 |     Deltat(_Deltat),
 | 
|---|
 | 42 |     IsAngstroem(_IsAngstroem),
 | 
|---|
 | 43 |     atoms(_atoms)
 | 
|---|
 | 44 |   {}
 | 
|---|
 | 45 |   /** Destructor of class AtomicForceManipulator.
 | 
|---|
 | 46 |    *
 | 
|---|
 | 47 |    */
 | 
|---|
 | 48 |   ~AtomicForceManipulator()
 | 
|---|
 | 49 |   {}
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |   /** Parses nuclear forces from file.
 | 
|---|
 | 52 |    * Forces are stored in the time step \a TimeStep in the atomicForces in \a atoms.
 | 
|---|
 | 53 |    * \param *file filename
 | 
|---|
 | 54 |    * \param TimeStep time step to parse forces file into
 | 
|---|
 | 55 |    * \return true - file parsed, false - file not found or imparsable
 | 
|---|
 | 56 |    */
 | 
|---|
 | 57 |   bool parseForcesFile(const char *file, const int TimeStep)
 | 
|---|
 | 58 |   {
 | 
|---|
 | 59 |     Info FunctionInfo(__func__);
 | 
|---|
 | 60 |     ForceMatrix Force;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |     // parse file into ForceMatrix
 | 
|---|
 | 63 |     std::ifstream input(file);
 | 
|---|
 | 64 |     if ((input.good()) && (!Force.ParseMatrix(input, 0,0,0))) {
 | 
|---|
 | 65 |       ELOG(0, "Could not parse Force Matrix file " << file << ".");
 | 
|---|
 | 66 |       return false;
 | 
|---|
 | 67 |     }
 | 
|---|
 | 68 |     input.close();
 | 
|---|
 | 69 |     if (Force.RowCounter[0] != (int)atoms.size()) {
 | 
|---|
 | 70 |       ELOG(0, "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << atoms.size() << ".");
 | 
|---|
 | 71 |       return false;
 | 
|---|
 | 72 |     }
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |     addForceMatrixToAtomicForce(Force, TimeStep, 1);
 | 
|---|
 | 75 |     return true;
 | 
|---|
 | 76 |   }
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | protected:
 | 
|---|
 | 79 |   void addForceMatrixToAtomicForce(const ForceMatrix &Force, const int &TimeStep, const int offset)
 | 
|---|
 | 80 |   {
 | 
|---|
 | 81 |     // place forces from matrix into atoms
 | 
|---|
 | 82 |     Vector tempVector;
 | 
|---|
 | 83 |     size_t i=0;
 | 
|---|
| [8ddd07] | 84 | 
 | 
|---|
| [51cdfd] | 85 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter,++i) {
 | 
|---|
 | 86 |       for(size_t d=0;d<NDIM;d++) {
 | 
|---|
 | 87 |         tempVector[d] = Force.Matrix[0][i][d+offset]*(IsAngstroem ? AtomicLengthToAngstroem : 1.);
 | 
|---|
 | 88 |       }
 | 
|---|
| [8ddd07] | 89 |       LOG(3, "DEBUG: Adding force vector " << tempVector << " to atom " << **iter);
 | 
|---|
 | 90 |       ASSERT( ((*iter)->getId()+1) == Force.Matrix[0][i][0],
 | 
|---|
 | 91 |           "AtomicForceManipulator::addForceMatrixToAtomicForce() - mismatch in ids "
 | 
|---|
 | 92 |           +toString(((*iter)->getId()+1))+" and file "+toString(Force.Matrix[0][i][0])+".");
 | 
|---|
| [51cdfd] | 93 |       tempVector += (*iter)->getAtomicForceAtStep(TimeStep);
 | 
|---|
 | 94 |       (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
 | 
|---|
 | 95 |     }
 | 
|---|
 | 96 |   }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   void correctForceMatrixForFixedCenterOfMass(const size_t offset, const int &TimeStep) {
 | 
|---|
 | 99 |     Vector ForceVector;
 | 
|---|
 | 100 |     // correct Forces
 | 
|---|
 | 101 |     //std::cout << "Force before correction, " << Force << std::endl;
 | 
|---|
 | 102 |     ForceVector.Zero();
 | 
|---|
 | 103 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 104 |       ForceVector += (*iter)->getAtomicForceAtStep(TimeStep);
 | 
|---|
 | 105 |     }
 | 
|---|
 | 106 |     ForceVector.Scale(1./(double)atoms.size());
 | 
|---|
 | 107 |     //std::cout << "Force before second correction, " << Force << std::endl;
 | 
|---|
 | 108 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 109 |       const Vector tempVector = (*iter)->getAtomicForceAtStep(TimeStep) - ForceVector;
 | 
|---|
 | 110 |       (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
 | 
|---|
 | 111 |     }
 | 
|---|
 | 112 |     LOG(3, "INFO: forces corrected by " << ForceVector << " each.");
 | 
|---|
 | 113 |   }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |   void correctVelocitiesForFixedCenterOfMass(const int &TimeStep) {
 | 
|---|
 | 116 |     Vector Velocity;
 | 
|---|
 | 117 |     double IonMass;
 | 
|---|
 | 118 |     // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
 | 119 |     Velocity = atoms.totalMomentumAtStep(TimeStep);
 | 
|---|
 | 120 |     IonMass = atoms.totalMass();
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 |     // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
 | 123 |     Velocity *= 1./IonMass;
 | 
|---|
 | 124 |     atoms.addVelocityAtStep(-1.*Velocity,TimeStep);
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 |     LOG(3, "INFO: Velocities corrected by " << Velocity << " each.");
 | 
|---|
 | 127 |   }
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |   void performThermostatControl(const int &TimeStep) {
 | 
|---|
 | 130 |     double ActualTemp;
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 |     // calculate current temperature
 | 
|---|
 | 133 |     ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
 | 
|---|
 | 134 |     LOG(3, "INFO: Current temperature is " << ActualTemp);
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |     // rescale to desired value
 | 
|---|
 | 137 |     double ekin = World::getInstance().getThermostats()->getActive()->scaleAtoms(TimeStep,ActualTemp,atoms);
 | 
|---|
 | 138 |     ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
 | 
|---|
 | 139 |     LOG(3, "INFO: New temperature after thermostat is " << ActualTemp);
 | 
|---|
 | 140 |     LOG(1, "Kinetic energy is " << ekin << ".");
 | 
|---|
 | 141 |   }
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | protected:
 | 
|---|
 | 144 |   double Deltat;
 | 
|---|
 | 145 |   bool IsAngstroem;
 | 
|---|
 | 146 |   AtomSetMixin<T> atoms;
 | 
|---|
 | 147 | };
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 | #endif /* ATOMICFORCEMANIPULATOR_HPP_ */
 | 
|---|