| 1 | /*
 | 
|---|
| 2 |  * VerletForceIntegration.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Feb 23, 2011
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef VERLETFORCEINTEGRATION_HPP_
 | 
|---|
| 9 | #define VERLETFORCEINTEGRATION_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 "Dynamics/MinimiseConstrainedPotential.hpp"
 | 
|---|
| 23 | #include "Fragmentation/ForceMatrix.hpp"
 | 
|---|
| 24 | #include "Helpers/helpers.hpp"
 | 
|---|
| 25 | #include "Helpers/defs.hpp"
 | 
|---|
| 26 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 27 | #include "Thermostats/ThermoStatContainer.hpp"
 | 
|---|
| 28 | #include "Thermostats/Thermostat.hpp"
 | 
|---|
| 29 | #include "World.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | template <class T>
 | 
|---|
| 32 | class VerletForceIntegration
 | 
|---|
| 33 | {
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |   /** Constructor of class VerletForceIntegration.
 | 
|---|
| 36 |    *
 | 
|---|
| 37 |    * \param _atoms set of atoms to integrate
 | 
|---|
| 38 |    * \param _Deltat time step width in atomic units
 | 
|---|
| 39 |    * \param _IsAngstroem whether length units are in angstroem or bohr radii
 | 
|---|
| 40 |    */
 | 
|---|
| 41 |   VerletForceIntegration(AtomSetMixin<T> &_atoms, double _Deltat, bool _IsAngstroem) :
 | 
|---|
| 42 |     Deltat(_Deltat),
 | 
|---|
| 43 |     IsAngstroem(_IsAngstroem),
 | 
|---|
| 44 |     atoms(_atoms)
 | 
|---|
| 45 |   {}
 | 
|---|
| 46 |   /** Destructor of class VerletForceIntegration.
 | 
|---|
| 47 |    *
 | 
|---|
| 48 |    */
 | 
|---|
| 49 |   ~VerletForceIntegration()
 | 
|---|
| 50 |   {}
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   /** Parses nuclear forces from file.
 | 
|---|
| 53 |    * Forces are stored in the time step \a TimeStep in the atomicForces in \a atoms.
 | 
|---|
| 54 |    * \param *file filename
 | 
|---|
| 55 |    * \param TimeStep time step to parse forces file into
 | 
|---|
| 56 |    * \return true - file parsed, false - file not found or imparsable
 | 
|---|
| 57 |    */
 | 
|---|
| 58 |   bool parseForcesFile(const char *file, const int TimeStep)
 | 
|---|
| 59 |   {
 | 
|---|
| 60 |     Info FunctionInfo(__func__);
 | 
|---|
| 61 |     ForceMatrix Force;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |     // parse file into ForceMatrix
 | 
|---|
| 64 |     std::ifstream input(file);
 | 
|---|
| 65 |     if ((input.good()) && (!Force.ParseMatrix(input, 0,0,0))) {
 | 
|---|
| 66 |       ELOG(0, "Could not parse Force Matrix file " << file << ".");
 | 
|---|
| 67 |       return false;
 | 
|---|
| 68 |     }
 | 
|---|
| 69 |     input.close();
 | 
|---|
| 70 |     if (Force.RowCounter[0] != (int)atoms.size()) {
 | 
|---|
| 71 |       ELOG(0, "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << atoms.size() << ".");
 | 
|---|
| 72 |       return false;
 | 
|---|
| 73 |     }
 | 
|---|
| 74 | 
 | 
|---|
| 75 |     addForceMatrixToAtomicForce(Force, TimeStep, 1);
 | 
|---|
| 76 |     return true;
 | 
|---|
| 77 |   }
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   /** Performs Verlet integration.
 | 
|---|
| 80 |    * Note that we assume the parsed forces to be in atomic units (hence, if coordinates are in angstroem, we
 | 
|---|
| 81 |    * have to transform them).
 | 
|---|
| 82 |    * This adds a new MD step \f$ t + \Delta t \f$ to the config file.
 | 
|---|
| 83 |    * \param NextStep current time step (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
 | 
|---|
| 84 |    * \param offset offset in matrix file to the first force component
 | 
|---|
| 85 |    * \param DoConstrainedMD whether a constrained MD shall be done
 | 
|---|
| 86 |    * \param FixedCenterOfMass whether forces and velocities are correct to have fixed center of mass
 | 
|---|
| 87 |    * \return true - file found and parsed, false - no atoms, file not found or imparsable
 | 
|---|
| 88 |    * \todo This is not yet checked if it is correctly working with DoConstrainedMD set >0.
 | 
|---|
| 89 |    */
 | 
|---|
| 90 |   bool operator()(const int NextStep, const size_t offset, const int DoConstrainedMD, const bool FixedCenterOfMass)
 | 
|---|
| 91 |   {
 | 
|---|
| 92 |     Info FunctionInfo(__func__);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |     // check that atoms are present at all
 | 
|---|
| 95 |     if (atoms.size() == 0) {
 | 
|---|
| 96 |       ELOG(2, "VerletForceIntegration::operator() - no atoms to integrate.");
 | 
|---|
| 97 |       return false;
 | 
|---|
| 98 |     }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |     // make sum of forces equal zero
 | 
|---|
| 101 |     if (FixedCenterOfMass)
 | 
|---|
| 102 |       correctForceMatrixForFixedCenterOfMass(offset,NextStep);
 | 
|---|
| 103 | 
 | 
|---|
| 104 |     // solve a constrained potential if we are meant to
 | 
|---|
| 105 |     if (DoConstrainedMD) {
 | 
|---|
| 106 |       performConstraintMinimization(DoConstrainedMD,NextStep);
 | 
|---|
| 107 |     }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     //std::cout << "Force before velocity verlet, " << Force << std::endl;
 | 
|---|
| 110 |     // and perform Verlet integration for each atom with position, velocity and force vector
 | 
|---|
| 111 |     // check size of vectors
 | 
|---|
| 112 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 113 |       //std::cout << "Id of atom is " << (*iter)->getId() << std::endl;
 | 
|---|
| 114 |       (*iter)->VelocityVerletUpdate((*iter)->getId(), NextStep, Deltat, IsAngstroem);
 | 
|---|
| 115 |     }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |     // make sum of velocities equal zero
 | 
|---|
| 118 |     if (FixedCenterOfMass)
 | 
|---|
| 119 |       correctVelocitiesForFixedCenterOfMass(NextStep);
 | 
|---|
| 120 | 
 | 
|---|
| 121 |     // thermostat
 | 
|---|
| 122 |     performThermostatControl(NextStep);
 | 
|---|
| 123 | 
 | 
|---|
| 124 |     // exit
 | 
|---|
| 125 |     return true;
 | 
|---|
| 126 |   };
 | 
|---|
| 127 | 
 | 
|---|
| 128 | private:
 | 
|---|
| 129 |   void addForceMatrixToAtomicForce(const ForceMatrix &Force, const int &TimeStep, const int offset)
 | 
|---|
| 130 |   {
 | 
|---|
| 131 |     // place forces from matrix into atoms
 | 
|---|
| 132 |     Vector tempVector;
 | 
|---|
| 133 |     size_t i=0;
 | 
|---|
| 134 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter,++i) {
 | 
|---|
| 135 |       for(size_t d=0;d<NDIM;d++) {
 | 
|---|
| 136 |         tempVector[d] = Force.Matrix[0][i][d+offset]*(IsAngstroem ? AtomicLengthToAngstroem : 1.);
 | 
|---|
| 137 |       }
 | 
|---|
| 138 |       tempVector += (*iter)->getAtomicForceAtStep(TimeStep);
 | 
|---|
| 139 |       (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
 | 
|---|
| 140 |     }
 | 
|---|
| 141 |   }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   void correctForceMatrixForFixedCenterOfMass(const size_t offset, const int &TimeStep) {
 | 
|---|
| 144 |     Vector ForceVector;
 | 
|---|
| 145 |     // correct Forces
 | 
|---|
| 146 |     //std::cout << "Force before correction, " << Force << std::endl;
 | 
|---|
| 147 |     ForceVector.Zero();
 | 
|---|
| 148 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 149 |       ForceVector += (*iter)->getAtomicForceAtStep(TimeStep);
 | 
|---|
| 150 |     }
 | 
|---|
| 151 |     ForceVector.Scale(1./(double)atoms.size());
 | 
|---|
| 152 |     //std::cout << "Force before second correction, " << Force << std::endl;
 | 
|---|
| 153 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 154 |       const Vector tempVector = (*iter)->getAtomicForceAtStep(TimeStep) - ForceVector;
 | 
|---|
| 155 |       (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
 | 
|---|
| 156 |     }
 | 
|---|
| 157 |     LOG(3, "INFO: forces correct by " << ForceVector << "each.");
 | 
|---|
| 158 |   }
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   void correctVelocitiesForFixedCenterOfMass(const int &TimeStep) {
 | 
|---|
| 161 |     Vector Velocity;
 | 
|---|
| 162 |     double IonMass;
 | 
|---|
| 163 |     // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 164 |     Velocity = atoms.totalMomentumAtStep(TimeStep);
 | 
|---|
| 165 |     IonMass = atoms.totalMass();
 | 
|---|
| 166 | 
 | 
|---|
| 167 |     // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 168 |     Velocity *= 1./IonMass;
 | 
|---|
| 169 |     atoms.addVelocityAtStep(-1.*Velocity,TimeStep);
 | 
|---|
| 170 | 
 | 
|---|
| 171 |     LOG(3, "INFO: Velocities corrected by " << Velocity << " each.");
 | 
|---|
| 172 |   }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   void performConstraintMinimization(const int &DoConstrainedMD, const int &TimeStep) {
 | 
|---|
| 175 |     // calculate forces and potential
 | 
|---|
| 176 |     ForceMatrix Force;
 | 
|---|
| 177 |     std::map<atom *, atom*> PermutationMap;
 | 
|---|
| 178 |     MinimiseConstrainedPotential Minimiser(atoms, PermutationMap);
 | 
|---|
| 179 |     //double ConstrainedPotentialEnergy =
 | 
|---|
| 180 |     Minimiser(DoConstrainedMD, 0, IsAngstroem);
 | 
|---|
| 181 |     Minimiser.EvaluateConstrainedForces(&Force);
 | 
|---|
| 182 |     addForceMatrixToAtomicForce(Force, TimeStep, 1);
 | 
|---|
| 183 |   }
 | 
|---|
| 184 | 
 | 
|---|
| 185 |   void performThermostatControl(const int &TimeStep) {
 | 
|---|
| 186 |     double ActualTemp;
 | 
|---|
| 187 | 
 | 
|---|
| 188 |     // calculate current temperature
 | 
|---|
| 189 |     ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
 | 
|---|
| 190 |     LOG(3, "INFO: Current temperature is " << ActualTemp);
 | 
|---|
| 191 | 
 | 
|---|
| 192 |     // rescale to desired value
 | 
|---|
| 193 |     double ekin = World::getInstance().getThermostats()->getActive()->scaleAtoms(TimeStep,ActualTemp,atoms);
 | 
|---|
| 194 |     ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
 | 
|---|
| 195 |     LOG(3, "INFO: New temperature after thermostat is " << ActualTemp);
 | 
|---|
| 196 |     LOG(1, "Kinetic energy is " << ekin << ".");
 | 
|---|
| 197 |   }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | private:
 | 
|---|
| 200 |   double Deltat;
 | 
|---|
| 201 |   bool IsAngstroem;
 | 
|---|
| 202 |   AtomSetMixin<T> atoms;
 | 
|---|
| 203 | };
 | 
|---|
| 204 | 
 | 
|---|
| 205 | #endif /* VERLETFORCEINTEGRATION_HPP_ */
 | 
|---|