| 1 | /*
 | 
|---|
| 2 |  * atom_trajectoryparticle.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Oct 19, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "atom.hpp"
 | 
|---|
| 11 | #include "atom_trajectoryparticle.hpp"
 | 
|---|
| 12 | #include "config.hpp"
 | 
|---|
| 13 | #include "element.hpp"
 | 
|---|
| 14 | #include "info.hpp"
 | 
|---|
| 15 | #include "log.hpp"
 | 
|---|
| 16 | #include "parser.hpp"
 | 
|---|
| 17 | #include "ThermoStatContainer.hpp"
 | 
|---|
| 18 | #include "verbose.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /** Constructor of class TrajectoryParticle.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | TrajectoryParticle::TrajectoryParticle()
 | 
|---|
| 23 | {
 | 
|---|
| 24 | };
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /** Destructor of class TrajectoryParticle.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | TrajectoryParticle::~TrajectoryParticle()
 | 
|---|
| 29 | {
 | 
|---|
| 30 | };
 | 
|---|
| 31 | 
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /** Adds kinetic energy of this atom to given temperature value.
 | 
|---|
| 34 |  * \param *temperature add on this value
 | 
|---|
| 35 |  * \param step given step of trajectory to add
 | 
|---|
| 36 |  */
 | 
|---|
| 37 | void TrajectoryParticle::AddKineticToTemperature(double *temperature, int step) const
 | 
|---|
| 38 | {
 | 
|---|
| 39 |   for (int i=NDIM;i--;)
 | 
|---|
| 40 |     *temperature += type->mass * Trajectory.U.at(step)[i]* Trajectory.U.at(step)[i];
 | 
|---|
| 41 | };
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /** Evaluates some constraint potential if atom moves from \a startstep at once to \endstep in trajectory.
 | 
|---|
| 44 |  * \param startstep trajectory begins at
 | 
|---|
| 45 |  * \param endstep trajectory ends at
 | 
|---|
| 46 |  * \param **PermutationMap if atom switches places with some other atom, there is no translation but a permutaton noted here (not in the trajectories of ea
 | 
|---|
| 47 |  * \param *Force Force matrix to store result in
 | 
|---|
| 48 |  */
 | 
|---|
| 49 | void TrajectoryParticle::EvaluateConstrainedForce(int startstep, int endstep, atom **PermutationMap, ForceMatrix *Force) const
 | 
|---|
| 50 | {
 | 
|---|
| 51 |   double constant = 10.;
 | 
|---|
| 52 |   TrajectoryParticle *Sprinter = PermutationMap[nr];
 | 
|---|
| 53 |   // set forces
 | 
|---|
| 54 |   for (int i=NDIM;i++;)
 | 
|---|
| 55 |     Force->Matrix[0][nr][5+i] += 2.*constant*sqrt(Trajectory.R.at(startstep).distance(Sprinter->Trajectory.R.at(endstep)));
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /** Correct velocity against the summed \a CoGVelocity for \a step.
 | 
|---|
| 59 |  * \param *ActualTemp sum up actual temperature meanwhile
 | 
|---|
| 60 |  * \param Step MD step in atom::Tracjetory
 | 
|---|
| 61 |  * \param *CoGVelocity remnant velocity (i.e. vector sum of all atom velocities)
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | void TrajectoryParticle::CorrectVelocity(double *ActualTemp, int Step, Vector *CoGVelocity)
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   for(int d=0;d<NDIM;d++) {
 | 
|---|
| 66 |     Trajectory.U.at(Step)[d] -= CoGVelocity->at(d);
 | 
|---|
| 67 |     *ActualTemp += 0.5 * type->mass * Trajectory.U.at(Step)[d] * Trajectory.U.at(Step)[d];
 | 
|---|
| 68 |   }
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /** Extends the trajectory STL vector to the new size.
 | 
|---|
| 72 |  * Does nothing if \a MaxSteps is smaller than current size.
 | 
|---|
| 73 |  * \param MaxSteps
 | 
|---|
| 74 |  */
 | 
|---|
| 75 | void TrajectoryParticle::ResizeTrajectory(int MaxSteps)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   Info FunctionInfo(__func__);
 | 
|---|
| 78 |   if (Trajectory.R.size() <= (unsigned int)(MaxSteps)) {
 | 
|---|
| 79 |     DoLog(0) && (Log() << Verbose(0) << "Increasing size for trajectory array of " << nr << " from " << Trajectory.R.size() << " to " << (MaxSteps+1) << "." << endl);
 | 
|---|
| 80 |     Trajectory.R.resize(MaxSteps+1);
 | 
|---|
| 81 |     Trajectory.U.resize(MaxSteps+1);
 | 
|---|
| 82 |     Trajectory.F.resize(MaxSteps+1);
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | };
 | 
|---|
| 85 | 
 | 
|---|
| 86 | /** Copies a given trajectory step \a src onto another \a dest
 | 
|---|
| 87 |  * \param dest index of destination step
 | 
|---|
| 88 |  * \param src index of source step
 | 
|---|
| 89 |  */
 | 
|---|
| 90 | void TrajectoryParticle::CopyStepOnStep(int dest, int src)
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   if (dest == src)  // self assignment check
 | 
|---|
| 93 |     return;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   for (int n=NDIM;n--;) {
 | 
|---|
| 96 |     Trajectory.R.at(dest)[n] = Trajectory.R.at(src)[n];
 | 
|---|
| 97 |     Trajectory.U.at(dest)[n] = Trajectory.U.at(src)[n];
 | 
|---|
| 98 |     Trajectory.F.at(dest)[n] = Trajectory.F.at(src)[n];
 | 
|---|
| 99 |   }
 | 
|---|
| 100 | };
 | 
|---|
| 101 | 
 | 
|---|
| 102 | /** Performs a velocity verlet update of the trajectory.
 | 
|---|
| 103 |  * Parameters are according to those in configuration class.
 | 
|---|
| 104 |  * \param NextStep index of sequential step to set
 | 
|---|
| 105 |  * \param *configuration pointer to configuration with parameters
 | 
|---|
| 106 |  * \param *Force matrix with forces
 | 
|---|
| 107 |  */
 | 
|---|
| 108 | void TrajectoryParticle::VelocityVerletUpdate(int NextStep, config *configuration, ForceMatrix *Force, const size_t offset)
 | 
|---|
| 109 | {
 | 
|---|
| 110 |   //a = configuration.Deltat*0.5/walker->type->mass;        // (F+F_old)/2m = a and thus: v = (F+F_old)/2m * t = (F + F_old) * a
 | 
|---|
| 111 |   for (int d=0; d<NDIM; d++) {
 | 
|---|
| 112 |     Trajectory.F.at(NextStep)[d] = -Force->Matrix[0][nr][d+offset]*(configuration->GetIsAngstroem() ? AtomicLengthToAngstroem : 1.);
 | 
|---|
| 113 |     Trajectory.R.at(NextStep)[d] = Trajectory.R.at(NextStep-1)[d];
 | 
|---|
| 114 |     Trajectory.R.at(NextStep)[d] += configuration->Deltat*(Trajectory.U.at(NextStep-1)[d]);     // s(t) = s(0) + v * deltat + 1/2 a * deltat^2
 | 
|---|
| 115 |     Trajectory.R.at(NextStep)[d] += 0.5*configuration->Deltat*configuration->Deltat*(Trajectory.F.at(NextStep)[d]/type->mass);     // F = m * a and s =
 | 
|---|
| 116 |   }
 | 
|---|
| 117 |   // Update U
 | 
|---|
| 118 |   for (int d=0; d<NDIM; d++) {
 | 
|---|
| 119 |     Trajectory.U.at(NextStep)[d] = Trajectory.U.at(NextStep-1)[d];
 | 
|---|
| 120 |     Trajectory.U.at(NextStep)[d] += configuration->Deltat * (Trajectory.F.at(NextStep)[d]+Trajectory.F.at(NextStep-1)[d]/type->mass); // v = F/m * t
 | 
|---|
| 121 |   }
 | 
|---|
| 122 |   // Update R (and F)
 | 
|---|
| 123 | //      out << "Integrated position&velocity of step " << (NextStep) << ": (";
 | 
|---|
| 124 | //      for (int d=0;d<NDIM;d++)
 | 
|---|
| 125 | //        out << Trajectory.R.at(NextStep).x[d] << " ";          // next step
 | 
|---|
| 126 | //      out << ")\t(";
 | 
|---|
| 127 | //      for (int d=0;d<NDIM;d++)
 | 
|---|
| 128 | //        Log() << Verbose(0) << Trajectory.U.at(NextStep).x[d] << " ";          // next step
 | 
|---|
| 129 | //      out << ")" << endl;
 | 
|---|
| 130 | };
 | 
|---|
| 131 | 
 | 
|---|
| 132 | /** Sums up mass and kinetics.
 | 
|---|
| 133 |  * \param Step step to sum for
 | 
|---|
| 134 |  * \param *TotalMass pointer to total mass sum
 | 
|---|
| 135 |  * \param *TotalVelocity pointer to tota velocity sum
 | 
|---|
| 136 |  */
 | 
|---|
| 137 | void TrajectoryParticle::SumUpKineticEnergy( int Step, double *TotalMass, Vector *TotalVelocity ) const
 | 
|---|
| 138 | {
 | 
|---|
| 139 |   *TotalMass += type->mass;  // sum up total mass
 | 
|---|
| 140 |   for(int d=0;d<NDIM;d++) {
 | 
|---|
| 141 |     TotalVelocity->at(d) += Trajectory.U.at(Step)[d]*type->mass;
 | 
|---|
| 142 |   }
 | 
|---|
| 143 | };
 | 
|---|
| 144 | 
 | 
|---|
| 145 | /** Scales velocity of atom according to Woodcock thermostat.
 | 
|---|
| 146 |  * \param ScaleTempFactor factor to scale the velocities with (i.e. sqrt of energy scale factor)
 | 
|---|
| 147 |  * \param Step MD step to scale
 | 
|---|
| 148 |  * \param *ekin sum of kinetic energy
 | 
|---|
| 149 |  */
 | 
|---|
| 150 | void TrajectoryParticle::Thermostat_Woodcock(double ScaleTempFactor, int Step, double *ekin)
 | 
|---|
| 151 | {
 | 
|---|
| 152 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 153 |   if (FixedIon == 0) // even FixedIon moves, only not by other's forces
 | 
|---|
| 154 |     for (int d=0; d<NDIM; d++) {
 | 
|---|
| 155 |       U[d] *= ScaleTempFactor;
 | 
|---|
| 156 |       *ekin += 0.5*type->mass * U[d]*U[d];
 | 
|---|
| 157 |     }
 | 
|---|
| 158 | };
 | 
|---|
| 159 | 
 | 
|---|
| 160 | /** Scales velocity of atom according to Gaussian thermostat.
 | 
|---|
| 161 |  * \param Step MD step to scale
 | 
|---|
| 162 |  * \param *G
 | 
|---|
| 163 |  * \param *E
 | 
|---|
| 164 |  */
 | 
|---|
| 165 | void TrajectoryParticle::Thermostat_Gaussian_init(int Step, double *G, double *E)
 | 
|---|
| 166 | {
 | 
|---|
| 167 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 168 |   Vector &F = Trajectory.F.at(Step);
 | 
|---|
| 169 |   if (FixedIon == 0) // even FixedIon moves, only not by other's forces
 | 
|---|
| 170 |     for (int d=0; d<NDIM; d++) {
 | 
|---|
| 171 |       *G += U[d] * F[d];
 | 
|---|
| 172 |       *E += U[d]*U[d]*type->mass;
 | 
|---|
| 173 |     }
 | 
|---|
| 174 | };
 | 
|---|
| 175 | 
 | 
|---|
| 176 | /** Determines scale factors according to Gaussian thermostat.
 | 
|---|
| 177 |  * \param Step MD step to scale
 | 
|---|
| 178 |  * \param GE G over E ratio
 | 
|---|
| 179 |  * \param *ekin sum of kinetic energy
 | 
|---|
| 180 |  * \param *configuration configuration class with TempFrequency and TargetTemp
 | 
|---|
| 181 |  */
 | 
|---|
| 182 | void TrajectoryParticle::Thermostat_Gaussian_least_constraint(int Step, double G_over_E, double *ekin, config *configuration)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 185 |   if (FixedIon == 0) // even FixedIon moves, only not by other's forces
 | 
|---|
| 186 |     for (int d=0; d<NDIM; d++) {
 | 
|---|
| 187 |       U[d] += configuration->Deltat/type->mass * ( (G_over_E) * (U[d]*type->mass) );
 | 
|---|
| 188 |       *ekin += type->mass * U[d]*U[d];
 | 
|---|
| 189 |     }
 | 
|---|
| 190 | };
 | 
|---|
| 191 | 
 | 
|---|
| 192 | /** Scales velocity of atom according to Langevin thermostat.
 | 
|---|
| 193 |  * \param Step MD step to scale
 | 
|---|
| 194 |  * \param *r random number generator
 | 
|---|
| 195 |  * \param *ekin sum of kinetic energy
 | 
|---|
| 196 |  * \param *configuration configuration class with TempFrequency and TargetTemp
 | 
|---|
| 197 |  */
 | 
|---|
| 198 | void TrajectoryParticle::Thermostat_Langevin(int Step, gsl_rng * r, double *ekin, config *configuration)
 | 
|---|
| 199 | {
 | 
|---|
| 200 |   double sigma  = sqrt(configuration->Thermostats->TargetTemp/type->mass); // sigma = (k_b T)/m (Hartree/atomicmass = atomiclength/atomictime)
 | 
|---|
| 201 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 202 |   if (FixedIon == 0) { // even FixedIon moves, only not by other's forces
 | 
|---|
| 203 |     // throw a dice to determine whether it gets hit by a heat bath particle
 | 
|---|
| 204 |     if (((((rand()/(double)RAND_MAX))*configuration->Thermostats->TempFrequency) < 1.)) {
 | 
|---|
| 205 |       DoLog(3) && (Log() << Verbose(3) << "Particle " << *this << " was hit (sigma " << sigma << "): " << sqrt(U[0]*U[0]+U[1]*U[1]+U[2]*U[2]) << " -> ");
 | 
|---|
| 206 |       // pick three random numbers from a Boltzmann distribution around the desired temperature T for each momenta axis
 | 
|---|
| 207 |       for (int d=0; d<NDIM; d++) {
 | 
|---|
| 208 |         U[d] = gsl_ran_gaussian (r, sigma);
 | 
|---|
| 209 |       }
 | 
|---|
| 210 |       DoLog(2) && (Log() << Verbose(2) << sqrt(U[0]*U[0]+U[1]*U[1]+U[2]*U[2]) << endl);
 | 
|---|
| 211 |     }
 | 
|---|
| 212 |     for (int d=0; d<NDIM; d++)
 | 
|---|
| 213 |       *ekin += 0.5*type->mass * U[d]*U[d];
 | 
|---|
| 214 |   }
 | 
|---|
| 215 | };
 | 
|---|
| 216 | 
 | 
|---|
| 217 | /** Scales velocity of atom according to Berendsen thermostat.
 | 
|---|
| 218 |  * \param Step MD step to scale
 | 
|---|
| 219 |  * \param ScaleTempFactor factor to scale energy (not velocity!) with
 | 
|---|
| 220 |  * \param *ekin sum of kinetic energy
 | 
|---|
| 221 |  * \param *configuration configuration class with TempFrequency and Deltat
 | 
|---|
| 222 |  */
 | 
|---|
| 223 | void TrajectoryParticle::Thermostat_Berendsen(int Step, double ScaleTempFactor, double *ekin, config *configuration)
 | 
|---|
| 224 | {
 | 
|---|
| 225 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 226 |   if (FixedIon == 0) { // even FixedIon moves, only not by other's forces
 | 
|---|
| 227 |     for (int d=0; d<NDIM; d++) {
 | 
|---|
| 228 |       U[d] *= sqrt(1+(configuration->Deltat/configuration->Thermostats->TempFrequency)*(ScaleTempFactor-1));
 | 
|---|
| 229 |       *ekin += 0.5*type->mass * U[d]*U[d];
 | 
|---|
| 230 |     }
 | 
|---|
| 231 |   }
 | 
|---|
| 232 | };
 | 
|---|
| 233 | 
 | 
|---|
| 234 | /** Initializes current run of NoseHoover thermostat.
 | 
|---|
| 235 |  * \param Step MD step to scale
 | 
|---|
| 236 |  * \param *delta_alpha additional sum of kinetic energy on return
 | 
|---|
| 237 |  */
 | 
|---|
| 238 | void TrajectoryParticle::Thermostat_NoseHoover_init(int Step, double *delta_alpha)
 | 
|---|
| 239 | {
 | 
|---|
| 240 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 241 |   if (FixedIon == 0) { // even FixedIon moves, only not by other's forces
 | 
|---|
| 242 |     for (int d=0; d<NDIM; d++) {
 | 
|---|
| 243 |       *delta_alpha += U[d]*U[d]*type->mass;
 | 
|---|
| 244 |     }
 | 
|---|
| 245 |   }
 | 
|---|
| 246 | };
 | 
|---|
| 247 | 
 | 
|---|
| 248 | /** Initializes current run of NoseHoover thermostat.
 | 
|---|
| 249 |  * \param Step MD step to scale
 | 
|---|
| 250 |  * \param *ekin sum of kinetic energy
 | 
|---|
| 251 |  * \param *configuration configuration class with TempFrequency and Deltat
 | 
|---|
| 252 |  */
 | 
|---|
| 253 | void TrajectoryParticle::Thermostat_NoseHoover_scale(int Step, double *ekin, config *configuration)
 | 
|---|
| 254 | {
 | 
|---|
| 255 |   Vector &U = Trajectory.U.at(Step);
 | 
|---|
| 256 |   if (FixedIon == 0) { // even FixedIon moves, only not by other's forces
 | 
|---|
| 257 |     for (int d=0; d<NDIM; d++) {
 | 
|---|
| 258 |         U[d] += configuration->Deltat/type->mass * (configuration->Thermostats->alpha * (U[d] * type->mass));
 | 
|---|
| 259 |         *ekin += (0.5*type->mass) * U[d]*U[d];
 | 
|---|
| 260 |       }
 | 
|---|
| 261 |   }
 | 
|---|
| 262 | };
 | 
|---|