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