| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * molecule_dynamics.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Oct 5, 2009
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "World.hpp"
 | 
|---|
| 23 | #include "atom.hpp"
 | 
|---|
| 24 | #include "config.hpp"
 | 
|---|
| 25 | #include "Dynamics/MinimiseConstrainedPotential.hpp"
 | 
|---|
| 26 | //#include "element.hpp"
 | 
|---|
| 27 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 28 | //#include "CodePatterns/Verbose.hpp"
 | 
|---|
| 29 | //#include "CodePatterns/Log.hpp"
 | 
|---|
| 30 | #include "molecule.hpp"
 | 
|---|
| 31 | #include "parser.hpp"
 | 
|---|
| 32 | //#include "LinearAlgebra/Plane.hpp"
 | 
|---|
| 33 | #include "ThermoStatContainer.hpp"
 | 
|---|
| 34 | #include "Thermostats/Berendsen.hpp"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "CodePatterns/enumeration.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | /************************************* Functions for class molecule *********************************/
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | /** Parses nuclear forces from file and performs Verlet integration.
 | 
|---|
| 42 |  * Note that we assume the parsed forces to be in atomic units (hence, if coordinates are in angstroem, we
 | 
|---|
| 43 |  * have to transform them).
 | 
|---|
| 44 |  * This adds a new MD step to the config file.
 | 
|---|
| 45 |  * \param *file filename
 | 
|---|
| 46 |  * \param config structure with config::Deltat, config::IsAngstroem, config::DoConstrained
 | 
|---|
| 47 |  * \param offset offset in matrix file to the first force component
 | 
|---|
| 48 |  * \return true - file found and parsed, false - file not found or imparsable
 | 
|---|
| 49 |  * \todo This is not yet checked if it is correctly working with DoConstrained set to true.
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | bool molecule::VerletForceIntegration(char *file, config &configuration, const size_t offset)
 | 
|---|
| 52 | {
 | 
|---|
| 53 |   Info FunctionInfo(__func__);
 | 
|---|
| 54 |   string token;
 | 
|---|
| 55 |   stringstream item;
 | 
|---|
| 56 |   double IonMass, ConstrainedPotentialEnergy, ActualTemp;
 | 
|---|
| 57 |   Vector Velocity;
 | 
|---|
| 58 |   ForceMatrix Force;
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   const int AtomCount = getAtomCount();
 | 
|---|
| 61 |   // parse file into ForceMatrix
 | 
|---|
| 62 |   std::ifstream input(file);
 | 
|---|
| 63 |   if ((input.good()) && (!Force.ParseMatrix(input, 0,0,0))) {
 | 
|---|
| 64 |     DoeLog(0) && (eLog()<< Verbose(0) << "Could not parse Force Matrix file " << file << "." << endl);
 | 
|---|
| 65 |     performCriticalExit();
 | 
|---|
| 66 |     return false;
 | 
|---|
| 67 |   }
 | 
|---|
| 68 |   input.close();
 | 
|---|
| 69 |   if (Force.RowCounter[0] != AtomCount) {
 | 
|---|
| 70 |     DoeLog(0) && (eLog()<< Verbose(0) << "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << getAtomCount() << "." << endl);
 | 
|---|
| 71 |     performCriticalExit();
 | 
|---|
| 72 |     return false;
 | 
|---|
| 73 |   }
 | 
|---|
| 74 |   // correct Forces
 | 
|---|
| 75 |   Velocity.Zero();
 | 
|---|
| 76 |   for(int i=0;i<AtomCount;i++)
 | 
|---|
| 77 |     for(int d=0;d<NDIM;d++) {
 | 
|---|
| 78 |       Velocity[d] += Force.Matrix[0][i][d+offset];
 | 
|---|
| 79 |     }
 | 
|---|
| 80 |   for(int i=0;i<AtomCount;i++)
 | 
|---|
| 81 |     for(int d=0;d<NDIM;d++) {
 | 
|---|
| 82 |       Force.Matrix[0][i][d+offset] -= Velocity[d]/static_cast<double>(AtomCount);
 | 
|---|
| 83 |     }
 | 
|---|
| 84 |   // solve a constrained potential if we are meant to
 | 
|---|
| 85 |   if (configuration.DoConstrainedMD) {
 | 
|---|
| 86 |     // calculate forces and potential
 | 
|---|
| 87 |     atom **PermutationMap = NULL;
 | 
|---|
| 88 |     MinimiseConstrainedPotential Minimiser(atoms);
 | 
|---|
| 89 |     //double ConstrainedPotentialEnergy =
 | 
|---|
| 90 |     Minimiser(PermutationMap, configuration.DoConstrainedMD, 0, configuration.GetIsAngstroem());
 | 
|---|
| 91 |     Minimiser.EvaluateConstrainedForces(&Force);
 | 
|---|
| 92 |     delete[](PermutationMap);
 | 
|---|
| 93 |   }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   // and perform Verlet integration for each atom with position, velocity and force vector
 | 
|---|
| 96 |   // check size of vectors
 | 
|---|
| 97 |   BOOST_FOREACH(atom *_atom, atoms) {
 | 
|---|
| 98 |     _atom->VelocityVerletUpdate(_atom->getNr(), MDSteps+1, &configuration, &Force, (const size_t) 0);
 | 
|---|
| 99 |   }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 102 |   Velocity = atoms.totalMomentumAtStep(MDSteps+1);
 | 
|---|
| 103 |   IonMass = atoms.totalMass();
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 106 |   Velocity *= 1./IonMass;
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   atoms.addVelocityAtStep(-1*Velocity,MDSteps+1);
 | 
|---|
| 109 |   ActualTemp = atoms.totalTemperatureAtStep(MDSteps+1);
 | 
|---|
| 110 |   Berendsen berendsen = Berendsen();
 | 
|---|
| 111 |   berendsen.addToContainer(World::getInstance().getThermostats());
 | 
|---|
| 112 |   double ekin = berendsen.scaleAtoms(MDSteps,ActualTemp,atoms);
 | 
|---|
| 113 |   DoLog(1) && (Log() << Verbose(1) << "Kinetic energy is " << ekin << "." << endl);
 | 
|---|
| 114 |   MDSteps++;
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   // exit
 | 
|---|
| 117 |   return true;
 | 
|---|
| 118 | };
 | 
|---|