| 1 | /*
 | 
|---|
| 2 |  * AtomicForceManipulator.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Feb 23, 2011
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef ATOMICFORCEMANIPULATOR_HPP_
 | 
|---|
| 9 | #define ATOMICFORCEMANIPULATOR_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 "Fragmentation/ForceMatrix.hpp"
 | 
|---|
| 23 | #include "Helpers/helpers.hpp"
 | 
|---|
| 24 | #include "Helpers/defs.hpp"
 | 
|---|
| 25 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 26 | #include "Thermostats/ThermoStatContainer.hpp"
 | 
|---|
| 27 | #include "Thermostats/Thermostat.hpp"
 | 
|---|
| 28 | #include "World.hpp"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | template <class T>
 | 
|---|
| 31 | class AtomicForceManipulator
 | 
|---|
| 32 | {
 | 
|---|
| 33 | public:
 | 
|---|
| 34 |   /** Constructor of class AtomicForceManipulator.
 | 
|---|
| 35 |    *
 | 
|---|
| 36 |    * \param _atoms set of atoms to integrate
 | 
|---|
| 37 |    * \param _Deltat time step width in atomic units
 | 
|---|
| 38 |    * \param _IsAngstroem whether length units are in angstroem or bohr radii
 | 
|---|
| 39 |    */
 | 
|---|
| 40 |   AtomicForceManipulator(AtomSetMixin<T> &_atoms, double _Deltat, bool _IsAngstroem) :
 | 
|---|
| 41 |     Deltat(_Deltat),
 | 
|---|
| 42 |     IsAngstroem(_IsAngstroem),
 | 
|---|
| 43 |     atoms(_atoms)
 | 
|---|
| 44 |   {}
 | 
|---|
| 45 |   /** Destructor of class AtomicForceManipulator.
 | 
|---|
| 46 |    *
 | 
|---|
| 47 |    */
 | 
|---|
| 48 |   ~AtomicForceManipulator()
 | 
|---|
| 49 |   {}
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   /** Parses nuclear forces from file.
 | 
|---|
| 52 |    * Forces are stored in the time step \a TimeStep in the atomicForces in \a atoms.
 | 
|---|
| 53 |    * \param *file filename
 | 
|---|
| 54 |    * \param TimeStep time step to parse forces file into
 | 
|---|
| 55 |    * \return true - file parsed, false - file not found or imparsable
 | 
|---|
| 56 |    */
 | 
|---|
| 57 |   bool parseForcesFile(const char *file, const int TimeStep)
 | 
|---|
| 58 |   {
 | 
|---|
| 59 |     Info FunctionInfo(__func__);
 | 
|---|
| 60 |     ForceMatrix Force;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |     // parse file into ForceMatrix
 | 
|---|
| 63 |     std::ifstream input(file);
 | 
|---|
| 64 |     if ((input.good()) && (!Force.ParseMatrix(input, 0,0,0))) {
 | 
|---|
| 65 |       ELOG(0, "Could not parse Force Matrix file " << file << ".");
 | 
|---|
| 66 |       return false;
 | 
|---|
| 67 |     }
 | 
|---|
| 68 |     input.close();
 | 
|---|
| 69 |     if (Force.RowCounter[0] != (int)atoms.size()) {
 | 
|---|
| 70 |       ELOG(0, "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << atoms.size() << ".");
 | 
|---|
| 71 |       return false;
 | 
|---|
| 72 |     }
 | 
|---|
| 73 | 
 | 
|---|
| 74 |     addForceMatrixToAtomicForce(Force, TimeStep, 1);
 | 
|---|
| 75 |     return true;
 | 
|---|
| 76 |   }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | protected:
 | 
|---|
| 79 |   void addForceMatrixToAtomicForce(const ForceMatrix &Force, const int &TimeStep, const int offset)
 | 
|---|
| 80 |   {
 | 
|---|
| 81 |     // place forces from matrix into atoms
 | 
|---|
| 82 |     Vector tempVector;
 | 
|---|
| 83 |     size_t i=0;
 | 
|---|
| 84 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter,++i) {
 | 
|---|
| 85 |       for(size_t d=0;d<NDIM;d++) {
 | 
|---|
| 86 |         tempVector[d] = Force.Matrix[0][i][d+offset]*(IsAngstroem ? AtomicLengthToAngstroem : 1.);
 | 
|---|
| 87 |       }
 | 
|---|
| 88 |       tempVector += (*iter)->getAtomicForceAtStep(TimeStep);
 | 
|---|
| 89 |       (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |   }
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   void correctForceMatrixForFixedCenterOfMass(const size_t offset, const int &TimeStep) {
 | 
|---|
| 94 |     Vector ForceVector;
 | 
|---|
| 95 |     // correct Forces
 | 
|---|
| 96 |     //std::cout << "Force before correction, " << Force << std::endl;
 | 
|---|
| 97 |     ForceVector.Zero();
 | 
|---|
| 98 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 99 |       ForceVector += (*iter)->getAtomicForceAtStep(TimeStep);
 | 
|---|
| 100 |     }
 | 
|---|
| 101 |     ForceVector.Scale(1./(double)atoms.size());
 | 
|---|
| 102 |     //std::cout << "Force before second correction, " << Force << std::endl;
 | 
|---|
| 103 |     for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| 104 |       const Vector tempVector = (*iter)->getAtomicForceAtStep(TimeStep) - ForceVector;
 | 
|---|
| 105 |       (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
 | 
|---|
| 106 |     }
 | 
|---|
| 107 |     LOG(3, "INFO: forces corrected by " << ForceVector << " each.");
 | 
|---|
| 108 |   }
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   void correctVelocitiesForFixedCenterOfMass(const int &TimeStep) {
 | 
|---|
| 111 |     Vector Velocity;
 | 
|---|
| 112 |     double IonMass;
 | 
|---|
| 113 |     // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 114 |     Velocity = atoms.totalMomentumAtStep(TimeStep);
 | 
|---|
| 115 |     IonMass = atoms.totalMass();
 | 
|---|
| 116 | 
 | 
|---|
| 117 |     // correct velocities (rather momenta) so that center of mass remains motionless
 | 
|---|
| 118 |     Velocity *= 1./IonMass;
 | 
|---|
| 119 |     atoms.addVelocityAtStep(-1.*Velocity,TimeStep);
 | 
|---|
| 120 | 
 | 
|---|
| 121 |     LOG(3, "INFO: Velocities corrected by " << Velocity << " each.");
 | 
|---|
| 122 |   }
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   void performThermostatControl(const int &TimeStep) {
 | 
|---|
| 125 |     double ActualTemp;
 | 
|---|
| 126 | 
 | 
|---|
| 127 |     // calculate current temperature
 | 
|---|
| 128 |     ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
 | 
|---|
| 129 |     LOG(3, "INFO: Current temperature is " << ActualTemp);
 | 
|---|
| 130 | 
 | 
|---|
| 131 |     // rescale to desired value
 | 
|---|
| 132 |     double ekin = World::getInstance().getThermostats()->getActive()->scaleAtoms(TimeStep,ActualTemp,atoms);
 | 
|---|
| 133 |     ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
 | 
|---|
| 134 |     LOG(3, "INFO: New temperature after thermostat is " << ActualTemp);
 | 
|---|
| 135 |     LOG(1, "Kinetic energy is " << ekin << ".");
 | 
|---|
| 136 |   }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | protected:
 | 
|---|
| 139 |   double Deltat;
 | 
|---|
| 140 |   bool IsAngstroem;
 | 
|---|
| 141 |   AtomSetMixin<T> atoms;
 | 
|---|
| 142 | };
 | 
|---|
| 143 | 
 | 
|---|
| 144 | #endif /* ATOMICFORCEMANIPULATOR_HPP_ */
 | 
|---|