| 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.hpp"
 | 
|---|
| 17 | #include "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 "LinearAlgebra/Vector.hpp"
 | 
|---|
| 24 | #include "parser.hpp"
 | 
|---|
| 25 | #include "ThermoStatContainer.hpp"
 | 
|---|
| 26 | #include "Thermostats/Berendsen.hpp"
 | 
|---|
| 27 | #include "World.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | template <class T>
 | 
|---|
| 30 | class VerletForceIntegration
 | 
|---|
| 31 | {
 | 
|---|
| 32 | public:
 | 
|---|
| 33 |   VerletForceIntegration(AtomSetMixin<T> &_atoms, double _Deltat, int _startstep, bool _IsAngstroem) :
 | 
|---|
| 34 |     Deltat(_Deltat),
 | 
|---|
| 35 |     IsAngstroem(_IsAngstroem),
 | 
|---|
| 36 |     atoms(_atoms),
 | 
|---|
| 37 |     MDSteps(_startstep)
 | 
|---|
| 38 |   {}
 | 
|---|
| 39 |   ~VerletForceIntegration()
 | 
|---|
| 40 |   {}
 | 
|---|
| 41 | 
 | 
|---|
| 42 |   /** Parses nuclear forces from file and performs Verlet integration.
 | 
|---|
| 43 |    * Note that we assume the parsed forces to be in atomic units (hence, if coordinates are in angstroem, we
 | 
|---|
| 44 |    * have to transform them).
 | 
|---|
| 45 |    * This adds a new MD step to the config file.
 | 
|---|
| 46 |    * \param *file filename
 | 
|---|
| 47 |    * \param offset offset in matrix file to the first force component
 | 
|---|
| 48 |    * \param DoConstrainedMD whether a constrained MD shall be done
 | 
|---|
| 49 |    * \param FixedCenterOfMass whether forces and velocities are correct to have fixed center of mass
 | 
|---|
| 50 |    * \return true - file found and parsed, false - file not found or imparsable
 | 
|---|
| 51 |    * \todo This is not yet checked if it is correctly working with DoConstrainedMD set >0.
 | 
|---|
| 52 |    */
 | 
|---|
| 53 |   bool operator()(char *file, const size_t offset, int DoConstrainedMD, bool FixedCenterOfMass)
 | 
|---|
| 54 |   {
 | 
|---|
| 55 |     Info FunctionInfo(__func__);
 | 
|---|
| 56 |     string token;
 | 
|---|
| 57 |     stringstream item;
 | 
|---|
| 58 |     double IonMass, ActualTemp;
 | 
|---|
| 59 |     ForceMatrix Force;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |     const int AtomCount = atoms.size();
 | 
|---|
| 62 |     ASSERT(AtomCount != 0, "VerletForceIntegration::operator() - no atoms to integrate.");
 | 
|---|
| 63 |     // parse file into ForceMatrix
 | 
|---|
| 64 |     std::ifstream input(file);
 | 
|---|
| 65 |     if ((input.good()) && (!Force.ParseMatrix(input, 0,0,0))) {
 | 
|---|
| 66 |       DoeLog(0) && (eLog()<< Verbose(0) << "Could not parse Force Matrix file " << file << "." << endl);
 | 
|---|
| 67 |       performCriticalExit();
 | 
|---|
| 68 |       return false;
 | 
|---|
| 69 |     }
 | 
|---|
| 70 |     input.close();
 | 
|---|
| 71 |     if (Force.RowCounter[0] != AtomCount) {
 | 
|---|
| 72 |       DoeLog(0) && (eLog()<< Verbose(0) << "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << atoms.size() << "." << endl);
 | 
|---|
| 73 |       performCriticalExit();
 | 
|---|
| 74 |       return false;
 | 
|---|
| 75 |     }
 | 
|---|
| 76 | 
 | 
|---|
| 77 |     if (FixedCenterOfMass) {
 | 
|---|
| 78 |       Vector ForceVector;
 | 
|---|
| 79 |       // correct Forces
 | 
|---|
| 80 |       //std::cout << "Force before correction, " << Force << std::endl;
 | 
|---|
| 81 |       ForceVector.Zero();
 | 
|---|
| 82 |       for(int i=0;i<AtomCount;i++)
 | 
|---|
| 83 |         for(int d=0;d<NDIM;d++) {
 | 
|---|
| 84 |           ForceVector[d] += Force.Matrix[0][i][d+offset];
 | 
|---|
| 85 |         }
 | 
|---|
| 86 |       ForceVector.Scale(1./static_cast<double>(AtomCount));
 | 
|---|
| 87 |       //std::cout << "Force before second correction, " << Force << std::endl;
 | 
|---|
| 88 |       for(int i=0;i<AtomCount;i++)
 | 
|---|
| 89 |         for(int d=0;d<NDIM;d++) {
 | 
|---|
| 90 |           Force.Matrix[0][i][d+offset] -= ForceVector[d];
 | 
|---|
| 91 |         }
 | 
|---|
| 92 |       LOG(3, "INFO: forces correct by " << ForceVector << "each.");
 | 
|---|
| 93 |     }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |     // solve a constrained potential if we are meant to
 | 
|---|
| 96 |     if (DoConstrainedMD) {
 | 
|---|
| 97 |       // calculate forces and potential
 | 
|---|
| 98 |       std::map<atom *, atom*> PermutationMap;
 | 
|---|
| 99 |       molecule::atomSet atoms_list;
 | 
|---|
| 100 |       copy(atoms.begin(), atoms.end(), atoms_list.begin());
 | 
|---|
| 101 |       MinimiseConstrainedPotential Minimiser(atoms_list, PermutationMap);
 | 
|---|
| 102 |       //double ConstrainedPotentialEnergy =
 | 
|---|
| 103 |       Minimiser(DoConstrainedMD, 0, IsAngstroem);
 | 
|---|
| 104 |       Minimiser.EvaluateConstrainedForces(&Force);
 | 
|---|
| 105 |     }
 | 
|---|
| 106 | 
 | 
|---|
| 107 |     //std::cout << "Force before velocity verlet, " << Force << std::endl;
 | 
|---|
| 108 |     // and perform Verlet integration for each atom with position, velocity and force vector
 | 
|---|
| 109 |     // check size of vectors
 | 
|---|
| 110 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 111 |       //std::cout << "Id of atom is " << (*iter)->getId() << std::endl;
 | 
|---|
| 112 |       (*iter)->VelocityVerletUpdate((*iter)->getId(), MDSteps+1, Deltat, IsAngstroem, &Force, (const size_t) 1);
 | 
|---|
| 113 |     }
 | 
|---|
| 114 | 
 | 
|---|
| 115 |     if (FixedCenterOfMass) {
 | 
|---|
| 116 |       Vector Velocity;
 | 
|---|
| 117 |       // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 118 |       Velocity = atoms.totalMomentumAtStep(MDSteps+1);
 | 
|---|
| 119 |       IonMass = atoms.totalMass();
 | 
|---|
| 120 | 
 | 
|---|
| 121 |       // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 122 |       Velocity *= 1./IonMass;
 | 
|---|
| 123 |       atoms.addVelocityAtStep(-1.*Velocity,MDSteps+1);
 | 
|---|
| 124 | 
 | 
|---|
| 125 |       LOG(3, "INFO: Velocities corrected by " << Velocity << " each.");
 | 
|---|
| 126 |     }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |     // thermostat
 | 
|---|
| 129 |     ActualTemp = atoms.totalTemperatureAtStep(MDSteps+1);
 | 
|---|
| 130 |     LOG(3, "INFO: Current temperature is " << ActualTemp);
 | 
|---|
| 131 |     Berendsen berendsen = Berendsen();
 | 
|---|
| 132 |     berendsen.addToContainer(World::getInstance().getThermostats());
 | 
|---|
| 133 |     double ekin = berendsen.scaleAtoms(MDSteps,ActualTemp,atoms);
 | 
|---|
| 134 |     ActualTemp = atoms.totalTemperatureAtStep(MDSteps+1);
 | 
|---|
| 135 |     LOG(3, "INFO: New temperature after thermostat is " << ActualTemp);
 | 
|---|
| 136 |     DoLog(1) && (Log() << Verbose(1) << "Kinetic energy is " << ekin << "." << endl);
 | 
|---|
| 137 | 
 | 
|---|
| 138 |     // next step
 | 
|---|
| 139 |     MDSteps++;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |     // exit
 | 
|---|
| 142 |     return true;
 | 
|---|
| 143 |   };
 | 
|---|
| 144 | 
 | 
|---|
| 145 | private:
 | 
|---|
| 146 |   double Deltat;
 | 
|---|
| 147 |   bool IsAngstroem;
 | 
|---|
| 148 |   AtomSetMixin<T> atoms;
 | 
|---|
| 149 |   int MDSteps;
 | 
|---|
| 150 | };
 | 
|---|
| 151 | 
 | 
|---|
| 152 | #endif /* VERLETFORCEINTEGRATION_HPP_ */
 | 
|---|