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