[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 |
|
---|
[6f0841] | 16 | #include "Atom/atom.hpp"
|
---|
| 17 | #include "Atom/AtomSet.hpp"
|
---|
[435065] | 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"
|
---|
[a9b86d] | 23 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
[255829] | 24 | #include "Helpers/helpers.hpp"
|
---|
[435065] | 25 | #include "LinearAlgebra/Vector.hpp"
|
---|
[ab26c3] | 26 | #include "Thermostats/ThermoStatContainer.hpp"
|
---|
[435065] | 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))) {
|
---|
[47d041] | 67 | ELOG(0, "Could not parse Force Matrix file " << file << ".");
|
---|
[435065] | 68 | performCriticalExit();
|
---|
| 69 | return false;
|
---|
| 70 | }
|
---|
| 71 | input.close();
|
---|
| 72 | if (Force.RowCounter[0] != AtomCount) {
|
---|
[47d041] | 73 | ELOG(0, "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << atoms.size() << ".");
|
---|
[435065] | 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;
|
---|
[adb5cda] | 100 | MinimiseConstrainedPotential Minimiser(atoms, PermutationMap);
|
---|
[435065] | 101 | //double ConstrainedPotentialEnergy =
|
---|
| 102 | Minimiser(DoConstrainedMD, 0, IsAngstroem);
|
---|
| 103 | Minimiser.EvaluateConstrainedForces(&Force);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | //std::cout << "Force before velocity verlet, " << Force << std::endl;
|
---|
| 107 | // and perform Verlet integration for each atom with position, velocity and force vector
|
---|
| 108 | // check size of vectors
|
---|
| 109 | for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 110 | //std::cout << "Id of atom is " << (*iter)->getId() << std::endl;
|
---|
| 111 | (*iter)->VelocityVerletUpdate((*iter)->getId(), MDSteps+1, Deltat, IsAngstroem, &Force, (const size_t) 1);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (FixedCenterOfMass) {
|
---|
| 115 | Vector Velocity;
|
---|
| 116 | // correct velocities (rather momenta) so that center of mass remains motionless
|
---|
| 117 | Velocity = atoms.totalMomentumAtStep(MDSteps+1);
|
---|
| 118 | IonMass = atoms.totalMass();
|
---|
| 119 |
|
---|
| 120 | // correct velocities (rather momenta) so that center of mass remains motionless
|
---|
| 121 | Velocity *= 1./IonMass;
|
---|
| 122 | atoms.addVelocityAtStep(-1.*Velocity,MDSteps+1);
|
---|
| 123 |
|
---|
| 124 | LOG(3, "INFO: Velocities corrected by " << Velocity << " each.");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | // thermostat
|
---|
| 128 | ActualTemp = atoms.totalTemperatureAtStep(MDSteps+1);
|
---|
| 129 | LOG(3, "INFO: Current temperature is " << ActualTemp);
|
---|
| 130 | Berendsen berendsen = Berendsen();
|
---|
| 131 | berendsen.addToContainer(World::getInstance().getThermostats());
|
---|
| 132 | double ekin = berendsen.scaleAtoms(MDSteps,ActualTemp,atoms);
|
---|
| 133 | ActualTemp = atoms.totalTemperatureAtStep(MDSteps+1);
|
---|
| 134 | LOG(3, "INFO: New temperature after thermostat is " << ActualTemp);
|
---|
[47d041] | 135 | LOG(1, "Kinetic energy is " << ekin << ".");
|
---|
[435065] | 136 |
|
---|
| 137 | // next step
|
---|
| 138 | MDSteps++;
|
---|
| 139 |
|
---|
| 140 | // exit
|
---|
| 141 | return true;
|
---|
| 142 | };
|
---|
| 143 |
|
---|
| 144 | private:
|
---|
| 145 | double Deltat;
|
---|
| 146 | bool IsAngstroem;
|
---|
| 147 | AtomSetMixin<T> atoms;
|
---|
| 148 | int MDSteps;
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | #endif /* VERLETFORCEINTEGRATION_HPP_ */
|
---|