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