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