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