[cee0b57] | 1 | /*
|
---|
| 2 | * molecule_dynamics.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Oct 5, 2009
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[cbc5fb] | 8 | #include "World.hpp"
|
---|
[f66195] | 9 | #include "atom.hpp"
|
---|
[cee0b57] | 10 | #include "config.hpp"
|
---|
[f66195] | 11 | #include "element.hpp"
|
---|
[e138de] | 12 | #include "log.hpp"
|
---|
[cee0b57] | 13 | #include "memoryallocator.hpp"
|
---|
| 14 | #include "molecule.hpp"
|
---|
[f66195] | 15 | #include "parser.hpp"
|
---|
[cee0b57] | 16 |
|
---|
| 17 | /************************************* Functions for class molecule *********************************/
|
---|
| 18 |
|
---|
[ccd9f5] | 19 | /** Penalizes long trajectories.
|
---|
| 20 | * \param *Walker atom to check against others
|
---|
| 21 | * \param *mol molecule with other atoms
|
---|
| 22 | * \param &Params constraint potential parameters
|
---|
| 23 | * \return penalty times each distance
|
---|
| 24 | */
|
---|
| 25 | double SumDistanceOfTrajectories(atom *Walker, molecule *mol, struct EvaluatePotential &Params)
|
---|
| 26 | {
|
---|
| 27 | gsl_matrix *A = gsl_matrix_alloc(NDIM,NDIM);
|
---|
| 28 | gsl_vector *x = gsl_vector_alloc(NDIM);
|
---|
| 29 | atom * Runner = mol->start;
|
---|
| 30 | atom *Sprinter = NULL;
|
---|
| 31 | Vector trajectory1, trajectory2, normal, TestVector;
|
---|
| 32 | double Norm1, Norm2, tmp, result = 0.;
|
---|
| 33 |
|
---|
| 34 | while (Runner->next != mol->end) {
|
---|
| 35 | Runner = Runner->next;
|
---|
| 36 | if (Runner == Walker) // hence, we only go up to the Walker, not beyond (similar to i=0; i<j; i++)
|
---|
| 37 | break;
|
---|
| 38 | // determine normalized trajectories direction vector (n1, n2)
|
---|
| 39 | Sprinter = Params.PermutationMap[Walker->nr]; // find first target point
|
---|
| 40 | trajectory1.CopyVector(&Sprinter->Trajectory.R.at(Params.endstep));
|
---|
| 41 | trajectory1.SubtractVector(&Walker->Trajectory.R.at(Params.startstep));
|
---|
| 42 | trajectory1.Normalize();
|
---|
| 43 | Norm1 = trajectory1.Norm();
|
---|
| 44 | Sprinter = Params.PermutationMap[Runner->nr]; // find second target point
|
---|
| 45 | trajectory2.CopyVector(&Sprinter->Trajectory.R.at(Params.endstep));
|
---|
| 46 | trajectory2.SubtractVector(&Runner->Trajectory.R.at(Params.startstep));
|
---|
| 47 | trajectory2.Normalize();
|
---|
| 48 | Norm2 = trajectory1.Norm();
|
---|
| 49 | // check whether either is zero()
|
---|
| 50 | if ((Norm1 < MYEPSILON) && (Norm2 < MYEPSILON)) {
|
---|
| 51 | tmp = Walker->Trajectory.R.at(Params.startstep).Distance(&Runner->Trajectory.R.at(Params.startstep));
|
---|
| 52 | } else if (Norm1 < MYEPSILON) {
|
---|
| 53 | Sprinter = Params.PermutationMap[Walker->nr]; // find first target point
|
---|
| 54 | trajectory1.CopyVector(&Sprinter->Trajectory.R.at(Params.endstep)); // copy first offset
|
---|
| 55 | trajectory1.SubtractVector(&Runner->Trajectory.R.at(Params.startstep)); // subtract second offset
|
---|
| 56 | trajectory2.Scale( trajectory1.ScalarProduct(&trajectory2) ); // trajectory2 is scaled to unity, hence we don't need to divide by anything
|
---|
| 57 | trajectory1.SubtractVector(&trajectory2); // project the part in norm direction away
|
---|
| 58 | tmp = trajectory1.Norm(); // remaining norm is distance
|
---|
| 59 | } else if (Norm2 < MYEPSILON) {
|
---|
| 60 | Sprinter = Params.PermutationMap[Runner->nr]; // find second target point
|
---|
| 61 | trajectory2.CopyVector(&Sprinter->Trajectory.R.at(Params.endstep)); // copy second offset
|
---|
| 62 | trajectory2.SubtractVector(&Walker->Trajectory.R.at(Params.startstep)); // subtract first offset
|
---|
| 63 | trajectory1.Scale( trajectory2.ScalarProduct(&trajectory1) ); // trajectory1 is scaled to unity, hence we don't need to divide by anything
|
---|
| 64 | trajectory2.SubtractVector(&trajectory1); // project the part in norm direction away
|
---|
| 65 | tmp = trajectory2.Norm(); // remaining norm is distance
|
---|
| 66 | } else if ((fabs(trajectory1.ScalarProduct(&trajectory2)/Norm1/Norm2) - 1.) < MYEPSILON) { // check whether they're linear dependent
|
---|
[e138de] | 67 | // Log() << Verbose(3) << "Both trajectories of " << *Walker << " and " << *Runner << " are linear dependent: ";
|
---|
| 68 | // Log() << Verbose(0) << trajectory1;
|
---|
| 69 | // Log() << Verbose(0) << " and ";
|
---|
| 70 | // Log() << Verbose(0) << trajectory2;
|
---|
[ccd9f5] | 71 | tmp = Walker->Trajectory.R.at(Params.startstep).Distance(&Runner->Trajectory.R.at(Params.startstep));
|
---|
[e138de] | 72 | // Log() << Verbose(0) << " with distance " << tmp << "." << endl;
|
---|
[ccd9f5] | 73 | } else { // determine distance by finding minimum distance
|
---|
[e138de] | 74 | // Log() << Verbose(3) << "Both trajectories of " << *Walker << " and " << *Runner << " are linear independent ";
|
---|
| 75 | // Log() << Verbose(0) << endl;
|
---|
| 76 | // Log() << Verbose(0) << "First Trajectory: ";
|
---|
| 77 | // Log() << Verbose(0) << trajectory1 << endl;
|
---|
| 78 | // Log() << Verbose(0) << "Second Trajectory: ";
|
---|
| 79 | // Log() << Verbose(0) << trajectory2 << endl;
|
---|
[ccd9f5] | 80 | // determine normal vector for both
|
---|
| 81 | normal.MakeNormalVector(&trajectory1, &trajectory2);
|
---|
| 82 | // print all vectors for debugging
|
---|
[e138de] | 83 | // Log() << Verbose(0) << "Normal vector in between: ";
|
---|
| 84 | // Log() << Verbose(0) << normal << endl;
|
---|
[ccd9f5] | 85 | // setup matrix
|
---|
| 86 | for (int i=NDIM;i--;) {
|
---|
| 87 | gsl_matrix_set(A, 0, i, trajectory1.x[i]);
|
---|
| 88 | gsl_matrix_set(A, 1, i, trajectory2.x[i]);
|
---|
| 89 | gsl_matrix_set(A, 2, i, normal.x[i]);
|
---|
| 90 | gsl_vector_set(x,i, (Walker->Trajectory.R.at(Params.startstep).x[i] - Runner->Trajectory.R.at(Params.startstep).x[i]));
|
---|
| 91 | }
|
---|
| 92 | // solve the linear system by Householder transformations
|
---|
| 93 | gsl_linalg_HH_svx(A, x);
|
---|
| 94 | // distance from last component
|
---|
| 95 | tmp = gsl_vector_get(x,2);
|
---|
[e138de] | 96 | // Log() << Verbose(0) << " with distance " << tmp << "." << endl;
|
---|
[ccd9f5] | 97 | // test whether we really have the intersection (by checking on c_1 and c_2)
|
---|
| 98 | TestVector.CopyVector(&Runner->Trajectory.R.at(Params.startstep));
|
---|
| 99 | trajectory2.Scale(gsl_vector_get(x,1));
|
---|
| 100 | TestVector.AddVector(&trajectory2);
|
---|
| 101 | normal.Scale(gsl_vector_get(x,2));
|
---|
| 102 | TestVector.AddVector(&normal);
|
---|
| 103 | TestVector.SubtractVector(&Walker->Trajectory.R.at(Params.startstep));
|
---|
| 104 | trajectory1.Scale(gsl_vector_get(x,0));
|
---|
| 105 | TestVector.SubtractVector(&trajectory1);
|
---|
| 106 | if (TestVector.Norm() < MYEPSILON) {
|
---|
[e138de] | 107 | // Log() << Verbose(2) << "Test: ok.\tDistance of " << tmp << " is correct." << endl;
|
---|
[ccd9f5] | 108 | } else {
|
---|
[e138de] | 109 | // Log() << Verbose(2) << "Test: failed.\tIntersection is off by ";
|
---|
| 110 | // Log() << Verbose(0) << TestVector;
|
---|
| 111 | // Log() << Verbose(0) << "." << endl;
|
---|
[ccd9f5] | 112 | }
|
---|
| 113 | }
|
---|
| 114 | // add up
|
---|
| 115 | tmp *= Params.IsAngstroem ? 1. : 1./AtomicLengthToAngstroem;
|
---|
| 116 | if (fabs(tmp) > MYEPSILON) {
|
---|
| 117 | result += Params.PenaltyConstants[1] * 1./tmp;
|
---|
[e138de] | 118 | //Log() << Verbose(4) << "Adding " << 1./tmp*constants[1] << "." << endl;
|
---|
[ccd9f5] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | return result;
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 | /** Penalizes atoms heading to same target.
|
---|
| 125 | * \param *Walker atom to check against others
|
---|
| 126 | * \param *mol molecule with other atoms
|
---|
| 127 | * \param &Params constrained potential parameters
|
---|
| 128 | * \return \a penalty times the number of equal targets
|
---|
| 129 | */
|
---|
| 130 | double PenalizeEqualTargets(atom *Walker, molecule *mol, struct EvaluatePotential &Params)
|
---|
| 131 | {
|
---|
| 132 | double result = 0.;
|
---|
| 133 | atom * Runner = mol->start;
|
---|
| 134 | while (Runner->next != mol->end) {
|
---|
| 135 | Runner = Runner->next;
|
---|
| 136 | if ((Params.PermutationMap[Walker->nr] == Params.PermutationMap[Runner->nr]) && (Walker->nr < Runner->nr)) {
|
---|
| 137 | // atom *Sprinter = PermutationMap[Walker->nr];
|
---|
[e138de] | 138 | // Log() << Verbose(0) << *Walker << " and " << *Runner << " are heading to the same target at ";
|
---|
| 139 | // Log() << Verbose(0) << Sprinter->Trajectory.R.at(endstep);
|
---|
| 140 | // Log() << Verbose(0) << ", penalting." << endl;
|
---|
[ccd9f5] | 141 | result += Params.PenaltyConstants[2];
|
---|
[e138de] | 142 | //Log() << Verbose(4) << "Adding " << constants[2] << "." << endl;
|
---|
[ccd9f5] | 143 | }
|
---|
| 144 | }
|
---|
| 145 | return result;
|
---|
| 146 | };
|
---|
[cee0b57] | 147 |
|
---|
| 148 | /** Evaluates the potential energy used for constrained molecular dynamics.
|
---|
| 149 | * \f$V_i^{con} = c^{bond} \cdot | r_{P(i)} - R_i | + sum_{i \neq j} C^{min} \cdot \frac{1}{C_{ij}} + C^{inj} \Bigl (1 - \theta \bigl (\prod_{i \neq j} (P(i) - P(j)) \bigr ) \Bigr )\f$
|
---|
| 150 | * where the first term points to the target in minimum distance, the second is a penalty for trajectories lying too close to each other (\f$C_{ij}\f$ is minimum distance between
|
---|
| 151 | * trajectories i and j) and the third term is a penalty for two atoms trying to each the same target point.
|
---|
| 152 | * Note that for the second term we have to solve the following linear system:
|
---|
| 153 | * \f$-c_1 \cdot n_1 + c_2 \cdot n_2 + C \cdot n_3 = - p_2 + p_1\f$, where \f$c_1\f$, \f$c_2\f$ and \f$C\f$ are constants,
|
---|
| 154 | * offset vector \f$p_1\f$ in direction \f$n_1\f$, offset vector \f$p_2\f$ in direction \f$n_2\f$,
|
---|
| 155 | * \f$n_3\f$ is the normal vector to both directions. \f$C\f$ would be the minimum distance between the two lines.
|
---|
| 156 | * \sa molecule::MinimiseConstrainedPotential(), molecule::VerletForceIntegration()
|
---|
| 157 | * \param *out output stream for debugging
|
---|
[ccd9f5] | 158 | * \param &Params constrained potential parameters
|
---|
[cee0b57] | 159 | * \return potential energy
|
---|
| 160 | * \note This routine is scaling quadratically which is not optimal.
|
---|
| 161 | * \todo There's a bit double counting going on for the first time, bu nothing to worry really about.
|
---|
| 162 | */
|
---|
[e138de] | 163 | double molecule::ConstrainedPotential(struct EvaluatePotential &Params)
|
---|
[cee0b57] | 164 | {
|
---|
[e3cbf9] | 165 | double tmp = 0.;
|
---|
| 166 | double result = 0.;
|
---|
[cee0b57] | 167 | // go through every atom
|
---|
[ccd9f5] | 168 | atom *Runner = NULL;
|
---|
| 169 | atom *Walker = start;
|
---|
[cee0b57] | 170 | while (Walker->next != end) {
|
---|
| 171 | Walker = Walker->next;
|
---|
| 172 | // first term: distance to target
|
---|
[ccd9f5] | 173 | Runner = Params.PermutationMap[Walker->nr]; // find target point
|
---|
| 174 | tmp = (Walker->Trajectory.R.at(Params.startstep).Distance(&Runner->Trajectory.R.at(Params.endstep)));
|
---|
| 175 | tmp *= Params.IsAngstroem ? 1. : 1./AtomicLengthToAngstroem;
|
---|
| 176 | result += Params.PenaltyConstants[0] * tmp;
|
---|
[e138de] | 177 | //Log() << Verbose(4) << "Adding " << tmp*constants[0] << "." << endl;
|
---|
[cee0b57] | 178 |
|
---|
| 179 | // second term: sum of distances to other trajectories
|
---|
[ccd9f5] | 180 | result += SumDistanceOfTrajectories(Walker, this, Params);
|
---|
[cee0b57] | 181 |
|
---|
| 182 | // third term: penalty for equal targets
|
---|
[ccd9f5] | 183 | result += PenalizeEqualTargets(Walker, this, Params);
|
---|
[cee0b57] | 184 | }
|
---|
| 185 |
|
---|
| 186 | return result;
|
---|
| 187 | };
|
---|
| 188 |
|
---|
[ccd9f5] | 189 | /** print the current permutation map.
|
---|
| 190 | * \param *out output stream for debugging
|
---|
| 191 | * \param &Params constrained potential parameters
|
---|
| 192 | * \param AtomCount number of atoms
|
---|
| 193 | */
|
---|
[e138de] | 194 | void PrintPermutationMap(int AtomCount, struct EvaluatePotential &Params)
|
---|
[cee0b57] | 195 | {
|
---|
| 196 | stringstream zeile1, zeile2;
|
---|
[7218f8] | 197 | int *DoubleList = Calloc<int>(AtomCount, "PrintPermutationMap: *DoubleList");
|
---|
[cee0b57] | 198 | int doubles = 0;
|
---|
| 199 | zeile1 << "PermutationMap: ";
|
---|
| 200 | zeile2 << " ";
|
---|
[ccd9f5] | 201 | for (int i=0;i<AtomCount;i++) {
|
---|
| 202 | Params.DoubleList[Params.PermutationMap[i]->nr]++;
|
---|
[cee0b57] | 203 | zeile1 << i << " ";
|
---|
[ccd9f5] | 204 | zeile2 << Params.PermutationMap[i]->nr << " ";
|
---|
[cee0b57] | 205 | }
|
---|
[ccd9f5] | 206 | for (int i=0;i<AtomCount;i++)
|
---|
| 207 | if (Params.DoubleList[i] > 1)
|
---|
[cee0b57] | 208 | doubles++;
|
---|
[ccd9f5] | 209 | if (doubles >0)
|
---|
[e138de] | 210 | Log() << Verbose(2) << "Found " << doubles << " Doubles." << endl;
|
---|
[cee0b57] | 211 | Free(&DoubleList);
|
---|
[e138de] | 212 | // Log() << Verbose(2) << zeile1.str() << endl << zeile2.str() << endl;
|
---|
[cee0b57] | 213 | };
|
---|
| 214 |
|
---|
[ccd9f5] | 215 | /** \f$O(N^2)\f$ operation of calculation distance between each atom pair and putting into DistanceList.
|
---|
| 216 | * \param *mol molecule to scan distances in
|
---|
| 217 | * \param &Params constrained potential parameters
|
---|
| 218 | */
|
---|
| 219 | void FillDistanceList(molecule *mol, struct EvaluatePotential &Params)
|
---|
| 220 | {
|
---|
| 221 | for (int i=mol->AtomCount; i--;) {
|
---|
| 222 | Params.DistanceList[i] = new DistanceMap; // is the distance sorted target list per atom
|
---|
| 223 | Params.DistanceList[i]->clear();
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | atom *Runner = NULL;
|
---|
| 227 | atom *Walker = mol->start;
|
---|
| 228 | while (Walker->next != mol->end) {
|
---|
| 229 | Walker = Walker->next;
|
---|
| 230 | Runner = mol->start;
|
---|
| 231 | while(Runner->next != mol->end) {
|
---|
| 232 | Runner = Runner->next;
|
---|
| 233 | Params.DistanceList[Walker->nr]->insert( DistancePair(Walker->Trajectory.R.at(Params.startstep).Distance(&Runner->Trajectory.R.at(Params.endstep)), Runner) );
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | };
|
---|
| 237 |
|
---|
| 238 | /** initialize lists.
|
---|
| 239 | * \param *out output stream for debugging
|
---|
| 240 | * \param *mol molecule to scan distances in
|
---|
| 241 | * \param &Params constrained potential parameters
|
---|
| 242 | */
|
---|
[e138de] | 243 | void CreateInitialLists(molecule *mol, struct EvaluatePotential &Params)
|
---|
[ccd9f5] | 244 | {
|
---|
| 245 | atom *Walker = mol->start;
|
---|
| 246 | while (Walker->next != mol->end) {
|
---|
| 247 | Walker = Walker->next;
|
---|
| 248 | Params.StepList[Walker->nr] = Params.DistanceList[Walker->nr]->begin(); // stores the step to the next iterator that could be a possible next target
|
---|
| 249 | Params.PermutationMap[Walker->nr] = Params.DistanceList[Walker->nr]->begin()->second; // always pick target with the smallest distance
|
---|
| 250 | Params.DoubleList[Params.DistanceList[Walker->nr]->begin()->second->nr]++; // increase this target's source count (>1? not injective)
|
---|
| 251 | Params.DistanceIterators[Walker->nr] = Params.DistanceList[Walker->nr]->begin(); // and remember which one we picked
|
---|
[e138de] | 252 | Log() << Verbose(2) << *Walker << " starts with distance " << Params.DistanceList[Walker->nr]->begin()->first << "." << endl;
|
---|
[ccd9f5] | 253 | }
|
---|
| 254 | };
|
---|
| 255 |
|
---|
| 256 | /** Try the next nearest neighbour in order to make the permutation map injective.
|
---|
| 257 | * \param *out output stream for debugging
|
---|
| 258 | * \param *mol molecule
|
---|
| 259 | * \param *Walker atom to change its target
|
---|
| 260 | * \param &OldPotential old value of constraint potential to see if we do better with new target
|
---|
| 261 | * \param &Params constrained potential parameters
|
---|
| 262 | */
|
---|
[e138de] | 263 | double TryNextNearestNeighbourForInjectivePermutation(molecule *mol, atom *Walker, double &OldPotential, struct EvaluatePotential &Params)
|
---|
[ccd9f5] | 264 | {
|
---|
| 265 | double Potential = 0;
|
---|
| 266 | DistanceMap::iterator NewBase = Params.DistanceIterators[Walker->nr]; // store old base
|
---|
| 267 | do {
|
---|
| 268 | NewBase++; // take next further distance in distance to targets list that's a target of no one
|
---|
| 269 | } while ((Params.DoubleList[NewBase->second->nr] != 0) && (NewBase != Params.DistanceList[Walker->nr]->end()));
|
---|
| 270 | if (NewBase != Params.DistanceList[Walker->nr]->end()) {
|
---|
| 271 | Params.PermutationMap[Walker->nr] = NewBase->second;
|
---|
[e138de] | 272 | Potential = fabs(mol->ConstrainedPotential(Params));
|
---|
[ccd9f5] | 273 | if (Potential > OldPotential) { // undo
|
---|
| 274 | Params.PermutationMap[Walker->nr] = Params.DistanceIterators[Walker->nr]->second;
|
---|
| 275 | } else { // do
|
---|
| 276 | Params.DoubleList[Params.DistanceIterators[Walker->nr]->second->nr]--; // decrease the old entry in the doubles list
|
---|
| 277 | Params.DoubleList[NewBase->second->nr]++; // increase the old entry in the doubles list
|
---|
| 278 | Params.DistanceIterators[Walker->nr] = NewBase;
|
---|
| 279 | OldPotential = Potential;
|
---|
[e138de] | 280 | Log() << Verbose(3) << "Found a new permutation, new potential is " << OldPotential << "." << endl;
|
---|
[ccd9f5] | 281 | }
|
---|
| 282 | }
|
---|
| 283 | return Potential;
|
---|
| 284 | };
|
---|
| 285 |
|
---|
| 286 | /** Permutes \a **&PermutationMap until the penalty is below constants[2].
|
---|
| 287 | * \param *out output stream for debugging
|
---|
| 288 | * \param *mol molecule to scan distances in
|
---|
| 289 | * \param &Params constrained potential parameters
|
---|
| 290 | */
|
---|
[e138de] | 291 | void MakeInjectivePermutation(molecule *mol, struct EvaluatePotential &Params)
|
---|
[ccd9f5] | 292 | {
|
---|
| 293 | atom *Walker = mol->start;
|
---|
| 294 | DistanceMap::iterator NewBase;
|
---|
[e138de] | 295 | double Potential = fabs(mol->ConstrainedPotential(Params));
|
---|
[ccd9f5] | 296 |
|
---|
| 297 | while ((Potential) > Params.PenaltyConstants[2]) {
|
---|
[e138de] | 298 | PrintPermutationMap(mol->AtomCount, Params);
|
---|
[ccd9f5] | 299 | Walker = Walker->next;
|
---|
| 300 | if (Walker == mol->end) // round-robin at the end
|
---|
| 301 | Walker = mol->start->next;
|
---|
| 302 | if (Params.DoubleList[Params.DistanceIterators[Walker->nr]->second->nr] <= 1) // no need to make those injective that aren't
|
---|
| 303 | continue;
|
---|
| 304 | // now, try finding a new one
|
---|
[e138de] | 305 | Potential = TryNextNearestNeighbourForInjectivePermutation(mol, Walker, Potential, Params);
|
---|
[ccd9f5] | 306 | }
|
---|
| 307 | for (int i=mol->AtomCount; i--;) // now each single entry in the DoubleList should be <=1
|
---|
| 308 | if (Params.DoubleList[i] > 1) {
|
---|
[e138de] | 309 | eLog() << Verbose(0) << "Failed to create an injective PermutationMap!" << endl;
|
---|
[e359a8] | 310 | performCriticalExit();
|
---|
[ccd9f5] | 311 | }
|
---|
[e138de] | 312 | Log() << Verbose(1) << "done." << endl;
|
---|
[ccd9f5] | 313 | };
|
---|
| 314 |
|
---|
[cee0b57] | 315 | /** Minimises the extra potential for constrained molecular dynamics and gives forces and the constrained potential energy.
|
---|
| 316 | * We do the following:
|
---|
| 317 | * -# Generate a distance list from all source to all target points
|
---|
| 318 | * -# Sort this per source point
|
---|
| 319 | * -# Take for each source point the target point with minimum distance, use this as initial permutation
|
---|
| 320 | * -# check whether molecule::ConstrainedPotential() is greater than injective penalty
|
---|
| 321 | * -# If so, we go through each source point, stepping down in the sorted target point distance list and re-checking potential.
|
---|
| 322 | * -# Next, we only apply transformations that keep the injectivity of the permutations list.
|
---|
| 323 | * -# Hence, for one source point we step down the ladder and seek the corresponding owner of this new target
|
---|
| 324 | * point and try to change it for one with lesser distance, or for the next one with greater distance, but only
|
---|
| 325 | * if this decreases the conditional potential.
|
---|
| 326 | * -# finished.
|
---|
| 327 | * -# Then, we calculate the forces by taking the spatial derivative, where we scale the potential to such a degree,
|
---|
| 328 | * that the total force is always pointing in direction of the constraint force (ensuring that we move in the
|
---|
| 329 | * right direction).
|
---|
| 330 | * -# Finally, we calculate the potential energy and return.
|
---|
| 331 | * \param *out output stream for debugging
|
---|
| 332 | * \param **PermutationMap on return: mapping between the atom label of the initial and the final configuration
|
---|
| 333 | * \param startstep current MD step giving initial position between which and \a endstep we perform the constrained MD (as further steps are always concatenated)
|
---|
| 334 | * \param endstep step giving final position in constrained MD
|
---|
| 335 | * \param IsAngstroem whether coordinates are in angstroem (true) or bohrradius (false)
|
---|
| 336 | * \sa molecule::VerletForceIntegration()
|
---|
| 337 | * \return potential energy (and allocated **PermutationMap (array of molecule::AtomCount ^2)
|
---|
| 338 | * \todo The constrained potential's constants are set to fixed values right now, but they should scale based on checks of the system in order
|
---|
| 339 | * to ensure they're properties (e.g. constants[2] always greater than the energy of the system).
|
---|
| 340 | * \bug this all is not O(N log N) but O(N^2)
|
---|
| 341 | */
|
---|
[e138de] | 342 | double molecule::MinimiseConstrainedPotential(atom **&PermutationMap, int startstep, int endstep, bool IsAngstroem)
|
---|
[cee0b57] | 343 | {
|
---|
| 344 | double Potential, OldPotential, OlderPotential;
|
---|
[ccd9f5] | 345 | struct EvaluatePotential Params;
|
---|
[7218f8] | 346 | Params.PermutationMap = Calloc<atom*>(AtomCount, "molecule::MinimiseConstrainedPotential: Params.**PermutationMap");
|
---|
[ccd9f5] | 347 | Params.DistanceList = Malloc<DistanceMap*>(AtomCount, "molecule::MinimiseConstrainedPotential: Params.**DistanceList");
|
---|
| 348 | Params.DistanceIterators = Malloc<DistanceMap::iterator>(AtomCount, "molecule::MinimiseConstrainedPotential: Params.*DistanceIterators");
|
---|
[7218f8] | 349 | Params.DoubleList = Calloc<int>(AtomCount, "molecule::MinimiseConstrainedPotential: Params.*DoubleList");
|
---|
[ccd9f5] | 350 | Params.StepList = Malloc<DistanceMap::iterator>(AtomCount, "molecule::MinimiseConstrainedPotential: Params.*StepList");
|
---|
[cee0b57] | 351 | int round;
|
---|
| 352 | atom *Walker = NULL, *Runner = NULL, *Sprinter = NULL;
|
---|
| 353 | DistanceMap::iterator Rider, Strider;
|
---|
| 354 |
|
---|
| 355 | /// Minimise the potential
|
---|
| 356 | // set Lagrange multiplier constants
|
---|
[ccd9f5] | 357 | Params.PenaltyConstants[0] = 10.;
|
---|
| 358 | Params.PenaltyConstants[1] = 1.;
|
---|
| 359 | Params.PenaltyConstants[2] = 1e+7; // just a huge penalty
|
---|
[cee0b57] | 360 | // generate the distance list
|
---|
[e138de] | 361 | Log() << Verbose(1) << "Allocating, initializting and filling the distance list ... " << endl;
|
---|
[ccd9f5] | 362 | FillDistanceList(this, Params);
|
---|
| 363 |
|
---|
[cee0b57] | 364 | // create the initial PermutationMap (source -> target)
|
---|
[e138de] | 365 | CreateInitialLists(this, Params);
|
---|
[ccd9f5] | 366 |
|
---|
[cee0b57] | 367 | // make the PermutationMap injective by checking whether we have a non-zero constants[2] term in it
|
---|
[e138de] | 368 | Log() << Verbose(1) << "Making the PermutationMap injective ... " << endl;
|
---|
| 369 | MakeInjectivePermutation(this, Params);
|
---|
[ccd9f5] | 370 | Free(&Params.DoubleList);
|
---|
| 371 |
|
---|
[cee0b57] | 372 | // argument minimise the constrained potential in this injective PermutationMap
|
---|
[ebe4d6] | 373 | Log() << Verbose(1) << "Argument minimising the PermutationMap." << endl;
|
---|
[cee0b57] | 374 | OldPotential = 1e+10;
|
---|
| 375 | round = 0;
|
---|
| 376 | do {
|
---|
[ebe4d6] | 377 | Log() << Verbose(2) << "Starting round " << ++round << ", at current potential " << OldPotential << " ... " << endl;
|
---|
[cee0b57] | 378 | OlderPotential = OldPotential;
|
---|
| 379 | do {
|
---|
| 380 | Walker = start;
|
---|
| 381 | while (Walker->next != end) { // pick one
|
---|
| 382 | Walker = Walker->next;
|
---|
[e138de] | 383 | PrintPermutationMap(AtomCount, Params);
|
---|
[ccd9f5] | 384 | Sprinter = Params.DistanceIterators[Walker->nr]->second; // store initial partner
|
---|
| 385 | Strider = Params.DistanceIterators[Walker->nr]; //remember old iterator
|
---|
| 386 | Params.DistanceIterators[Walker->nr] = Params.StepList[Walker->nr];
|
---|
| 387 | if (Params.DistanceIterators[Walker->nr] == Params.DistanceList[Walker->nr]->end()) {// stop, before we run through the list and still on
|
---|
| 388 | Params.DistanceIterators[Walker->nr] == Params.DistanceList[Walker->nr]->begin();
|
---|
[cee0b57] | 389 | break;
|
---|
| 390 | }
|
---|
[e138de] | 391 | //Log() << Verbose(2) << "Current Walker: " << *Walker << " with old/next candidate " << *Sprinter << "/" << *DistanceIterators[Walker->nr]->second << "." << endl;
|
---|
[cee0b57] | 392 | // find source of the new target
|
---|
| 393 | Runner = start->next;
|
---|
| 394 | while(Runner != end) { // find the source whose toes we might be stepping on (Walker's new target should be in use by another already)
|
---|
[ccd9f5] | 395 | if (Params.PermutationMap[Runner->nr] == Params.DistanceIterators[Walker->nr]->second) {
|
---|
[e138de] | 396 | //Log() << Verbose(2) << "Found the corresponding owner " << *Runner << " to " << *PermutationMap[Runner->nr] << "." << endl;
|
---|
[cee0b57] | 397 | break;
|
---|
| 398 | }
|
---|
| 399 | Runner = Runner->next;
|
---|
| 400 | }
|
---|
| 401 | if (Runner != end) { // we found the other source
|
---|
| 402 | // then look in its distance list for Sprinter
|
---|
[ccd9f5] | 403 | Rider = Params.DistanceList[Runner->nr]->begin();
|
---|
| 404 | for (; Rider != Params.DistanceList[Runner->nr]->end(); Rider++)
|
---|
[cee0b57] | 405 | if (Rider->second == Sprinter)
|
---|
| 406 | break;
|
---|
[ccd9f5] | 407 | if (Rider != Params.DistanceList[Runner->nr]->end()) { // if we have found one
|
---|
[e138de] | 408 | //Log() << Verbose(2) << "Current Other: " << *Runner << " with old/next candidate " << *PermutationMap[Runner->nr] << "/" << *Rider->second << "." << endl;
|
---|
[cee0b57] | 409 | // exchange both
|
---|
[ccd9f5] | 410 | Params.PermutationMap[Walker->nr] = Params.DistanceIterators[Walker->nr]->second; // put next farther distance into PermutationMap
|
---|
| 411 | Params.PermutationMap[Runner->nr] = Sprinter; // and hand the old target to its respective owner
|
---|
[e138de] | 412 | PrintPermutationMap(AtomCount, Params);
|
---|
[cee0b57] | 413 | // calculate the new potential
|
---|
[e138de] | 414 | //Log() << Verbose(2) << "Checking new potential ..." << endl;
|
---|
| 415 | Potential = ConstrainedPotential(Params);
|
---|
[cee0b57] | 416 | if (Potential > OldPotential) { // we made everything worse! Undo ...
|
---|
[e138de] | 417 | //Log() << Verbose(3) << "Nay, made the potential worse: " << Potential << " vs. " << OldPotential << "!" << endl;
|
---|
| 418 | //Log() << Verbose(3) << "Setting " << *Runner << "'s source to " << *Params.DistanceIterators[Runner->nr]->second << "." << endl;
|
---|
[cee0b57] | 419 | // Undo for Runner (note, we haven't moved the iteration yet, we may use this)
|
---|
[ccd9f5] | 420 | Params.PermutationMap[Runner->nr] = Params.DistanceIterators[Runner->nr]->second;
|
---|
[cee0b57] | 421 | // Undo for Walker
|
---|
[ccd9f5] | 422 | Params.DistanceIterators[Walker->nr] = Strider; // take next farther distance target
|
---|
[e138de] | 423 | //Log() << Verbose(3) << "Setting " << *Walker << "'s source to " << *Params.DistanceIterators[Walker->nr]->second << "." << endl;
|
---|
[ccd9f5] | 424 | Params.PermutationMap[Walker->nr] = Params.DistanceIterators[Walker->nr]->second;
|
---|
[cee0b57] | 425 | } else {
|
---|
[ccd9f5] | 426 | Params.DistanceIterators[Runner->nr] = Rider; // if successful also move the pointer in the iterator list
|
---|
[e138de] | 427 | Log() << Verbose(3) << "Found a better permutation, new potential is " << Potential << " vs." << OldPotential << "." << endl;
|
---|
[cee0b57] | 428 | OldPotential = Potential;
|
---|
| 429 | }
|
---|
[ccd9f5] | 430 | if (Potential > Params.PenaltyConstants[2]) {
|
---|
[717e0c] | 431 | eLog() << Verbose(1) << "The two-step permutation procedure did not maintain injectivity!" << endl;
|
---|
[cee0b57] | 432 | exit(255);
|
---|
| 433 | }
|
---|
[e138de] | 434 | //Log() << Verbose(0) << endl;
|
---|
[cee0b57] | 435 | } else {
|
---|
[717e0c] | 436 | eLog() << Verbose(1) << *Runner << " was not the owner of " << *Sprinter << "!" << endl;
|
---|
[cee0b57] | 437 | exit(255);
|
---|
| 438 | }
|
---|
| 439 | } else {
|
---|
[ccd9f5] | 440 | Params.PermutationMap[Walker->nr] = Params.DistanceIterators[Walker->nr]->second; // new target has no source!
|
---|
[cee0b57] | 441 | }
|
---|
[ccd9f5] | 442 | Params.StepList[Walker->nr]++; // take next farther distance target
|
---|
[cee0b57] | 443 | }
|
---|
| 444 | } while (Walker->next != end);
|
---|
| 445 | } while ((OlderPotential - OldPotential) > 1e-3);
|
---|
[e138de] | 446 | Log() << Verbose(1) << "done." << endl;
|
---|
[cee0b57] | 447 |
|
---|
| 448 |
|
---|
| 449 | /// free memory and return with evaluated potential
|
---|
| 450 | for (int i=AtomCount; i--;)
|
---|
[ccd9f5] | 451 | Params.DistanceList[i]->clear();
|
---|
| 452 | Free(&Params.DistanceList);
|
---|
| 453 | Free(&Params.DistanceIterators);
|
---|
[e138de] | 454 | return ConstrainedPotential(Params);
|
---|
[cee0b57] | 455 | };
|
---|
| 456 |
|
---|
[ccd9f5] | 457 |
|
---|
[cee0b57] | 458 | /** Evaluates the (distance-related part) of the constrained potential for the constrained forces.
|
---|
| 459 | * \param *out output stream for debugging
|
---|
| 460 | * \param startstep current MD step giving initial position between which and \a endstep we perform the constrained MD (as further steps are always concatenated)
|
---|
| 461 | * \param endstep step giving final position in constrained MD
|
---|
| 462 | * \param **PermutationMap mapping between the atom label of the initial and the final configuration
|
---|
| 463 | * \param *Force ForceMatrix containing force vectors from the external energy functional minimisation.
|
---|
| 464 | * \todo the constant for the constrained potential distance part is hard-coded independently of the hard-coded value in MinimiseConstrainedPotential()
|
---|
| 465 | */
|
---|
[e138de] | 466 | void molecule::EvaluateConstrainedForces(int startstep, int endstep, atom **PermutationMap, ForceMatrix *Force)
|
---|
[cee0b57] | 467 | {
|
---|
| 468 | /// evaluate forces (only the distance to target dependent part) with the final PermutationMap
|
---|
[e138de] | 469 | Log() << Verbose(1) << "Calculating forces and adding onto ForceMatrix ... " << endl;
|
---|
[ccd9f5] | 470 | ActOnAllAtoms( &atom::EvaluateConstrainedForce, startstep, endstep, PermutationMap, Force );
|
---|
[e138de] | 471 | Log() << Verbose(1) << "done." << endl;
|
---|
[cee0b57] | 472 | };
|
---|
| 473 |
|
---|
| 474 | /** Performs a linear interpolation between two desired atomic configurations with a given number of steps.
|
---|
| 475 | * Note, step number is config::MaxOuterStep
|
---|
| 476 | * \param *out output stream for debugging
|
---|
| 477 | * \param startstep stating initial configuration in molecule::Trajectories
|
---|
| 478 | * \param endstep stating final configuration in molecule::Trajectories
|
---|
| 479 | * \param &config configuration structure
|
---|
| 480 | * \param MapByIdentity if true we just use the identity to map atoms in start config to end config, if not we find mapping by \sa MinimiseConstrainedPotential()
|
---|
| 481 | * \return true - success in writing step files, false - error writing files or only one step in molecule::Trajectories
|
---|
| 482 | */
|
---|
[e138de] | 483 | bool molecule::LinearInterpolationBetweenConfiguration(int startstep, int endstep, const char *prefix, config &configuration, bool MapByIdentity)
|
---|
[cee0b57] | 484 | {
|
---|
| 485 | molecule *mol = NULL;
|
---|
| 486 | bool status = true;
|
---|
| 487 | int MaxSteps = configuration.MaxOuterStep;
|
---|
[cbc5fb] | 488 | MoleculeListClass *MoleculePerStep = new MoleculeListClass(World::get());
|
---|
[cee0b57] | 489 | // Get the Permutation Map by MinimiseConstrainedPotential
|
---|
| 490 | atom **PermutationMap = NULL;
|
---|
| 491 | atom *Walker = NULL, *Sprinter = NULL;
|
---|
| 492 | if (!MapByIdentity)
|
---|
[e138de] | 493 | MinimiseConstrainedPotential(PermutationMap, startstep, endstep, configuration.GetIsAngstroem());
|
---|
[cee0b57] | 494 | else {
|
---|
| 495 | PermutationMap = Malloc<atom *>(AtomCount, "molecule::LinearInterpolationBetweenConfiguration: **PermutationMap");
|
---|
[4a7776a] | 496 | SetIndexedArrayForEachAtomTo( PermutationMap, &atom::nr );
|
---|
[cee0b57] | 497 | }
|
---|
| 498 |
|
---|
| 499 | // check whether we have sufficient space in Trajectories for each atom
|
---|
[4a7776a] | 500 | ActOnAllAtoms( &atom::ResizeTrajectory, MaxSteps );
|
---|
[cee0b57] | 501 | // push endstep to last one
|
---|
[4a7776a] | 502 | ActOnAllAtoms( &atom::CopyStepOnStep, MaxSteps, endstep );
|
---|
[cee0b57] | 503 | endstep = MaxSteps;
|
---|
| 504 |
|
---|
| 505 | // go through all steps and add the molecular configuration to the list and to the Trajectories of \a this molecule
|
---|
[e138de] | 506 | Log() << Verbose(1) << "Filling intermediate " << MaxSteps << " steps with MDSteps of " << MDSteps << "." << endl;
|
---|
[cee0b57] | 507 | for (int step = 0; step <= MaxSteps; step++) {
|
---|
[cbc5fb] | 508 | mol = World::get()->createMolecule();
|
---|
[cee0b57] | 509 | MoleculePerStep->insert(mol);
|
---|
| 510 | Walker = start;
|
---|
| 511 | while (Walker->next != end) {
|
---|
| 512 | Walker = Walker->next;
|
---|
| 513 | // add to molecule list
|
---|
| 514 | Sprinter = mol->AddCopyAtom(Walker);
|
---|
| 515 | for (int n=NDIM;n--;) {
|
---|
[fcd7b6] | 516 | Sprinter->x.x[n] = Walker->Trajectory.R.at(startstep).x[n] + (PermutationMap[Walker->nr]->Trajectory.R.at(endstep).x[n] - Walker->Trajectory.R.at(startstep).x[n])*((double)step/(double)MaxSteps);
|
---|
[cee0b57] | 517 | // add to Trajectories
|
---|
[e138de] | 518 | //Log() << Verbose(3) << step << ">=" << MDSteps-1 << endl;
|
---|
[cee0b57] | 519 | if (step < MaxSteps) {
|
---|
[fcd7b6] | 520 | Walker->Trajectory.R.at(step).x[n] = Walker->Trajectory.R.at(startstep).x[n] + (PermutationMap[Walker->nr]->Trajectory.R.at(endstep).x[n] - Walker->Trajectory.R.at(startstep).x[n])*((double)step/(double)MaxSteps);
|
---|
| 521 | Walker->Trajectory.U.at(step).x[n] = 0.;
|
---|
| 522 | Walker->Trajectory.F.at(step).x[n] = 0.;
|
---|
[cee0b57] | 523 | }
|
---|
| 524 | }
|
---|
| 525 | }
|
---|
| 526 | }
|
---|
| 527 | MDSteps = MaxSteps+1; // otherwise new Trajectories' points aren't stored on save&exit
|
---|
| 528 |
|
---|
| 529 | // store the list to single step files
|
---|
| 530 | int *SortIndex = Malloc<int>(AtomCount, "molecule::LinearInterpolationBetweenConfiguration: *SortIndex");
|
---|
| 531 | for (int i=AtomCount; i--; )
|
---|
| 532 | SortIndex[i] = i;
|
---|
[e138de] | 533 | status = MoleculePerStep->OutputConfigForListOfFragments(&configuration, SortIndex);
|
---|
[cee0b57] | 534 |
|
---|
| 535 | // free and return
|
---|
| 536 | Free(&PermutationMap);
|
---|
| 537 | delete(MoleculePerStep);
|
---|
| 538 | return status;
|
---|
| 539 | };
|
---|
| 540 |
|
---|
| 541 | /** Parses nuclear forces from file and performs Verlet integration.
|
---|
| 542 | * Note that we assume the parsed forces to be in atomic units (hence, if coordinates are in angstroem, we
|
---|
| 543 | * have to transform them).
|
---|
| 544 | * This adds a new MD step to the config file.
|
---|
| 545 | * \param *out output stream for debugging
|
---|
| 546 | * \param *file filename
|
---|
| 547 | * \param config structure with config::Deltat, config::IsAngstroem, config::DoConstrained
|
---|
| 548 | * \param delta_t time step width in atomic units
|
---|
| 549 | * \param IsAngstroem whether coordinates are in angstroem (true) or bohrradius (false)
|
---|
| 550 | * \param DoConstrained whether we perform a constrained (>0, target step in molecule::trajectories) or unconstrained (0) molecular dynamics, \sa molecule::MinimiseConstrainedPotential()
|
---|
| 551 | * \return true - file found and parsed, false - file not found or imparsable
|
---|
| 552 | * \todo This is not yet checked if it is correctly working with DoConstrained set to true.
|
---|
| 553 | */
|
---|
[e138de] | 554 | bool molecule::VerletForceIntegration(char *file, config &configuration)
|
---|
[cee0b57] | 555 | {
|
---|
| 556 | ifstream input(file);
|
---|
| 557 | string token;
|
---|
| 558 | stringstream item;
|
---|
[4a7776a] | 559 | double IonMass, ConstrainedPotentialEnergy, ActualTemp;
|
---|
| 560 | Vector Velocity;
|
---|
[cee0b57] | 561 | ForceMatrix Force;
|
---|
| 562 |
|
---|
| 563 | CountElements(); // make sure ElementsInMolecule is up to date
|
---|
| 564 |
|
---|
| 565 | // check file
|
---|
| 566 | if (input == NULL) {
|
---|
| 567 | return false;
|
---|
| 568 | } else {
|
---|
| 569 | // parse file into ForceMatrix
|
---|
| 570 | if (!Force.ParseMatrix(file, 0,0,0)) {
|
---|
[e138de] | 571 | eLog() << Verbose(0) << "Could not parse Force Matrix file " << file << "." << endl;
|
---|
[e359a8] | 572 | performCriticalExit();
|
---|
[cee0b57] | 573 | return false;
|
---|
| 574 | }
|
---|
| 575 | if (Force.RowCounter[0] != AtomCount) {
|
---|
[e138de] | 576 | eLog() << Verbose(0) << "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << AtomCount << "." << endl;
|
---|
[e359a8] | 577 | performCriticalExit();
|
---|
[cee0b57] | 578 | return false;
|
---|
| 579 | }
|
---|
| 580 | // correct Forces
|
---|
[4a7776a] | 581 | Velocity.Zero();
|
---|
[cee0b57] | 582 | for(int i=0;i<AtomCount;i++)
|
---|
| 583 | for(int d=0;d<NDIM;d++) {
|
---|
[4a7776a] | 584 | Velocity.x[d] += Force.Matrix[0][i][d+5];
|
---|
[cee0b57] | 585 | }
|
---|
| 586 | for(int i=0;i<AtomCount;i++)
|
---|
| 587 | for(int d=0;d<NDIM;d++) {
|
---|
[4a7776a] | 588 | Force.Matrix[0][i][d+5] -= Velocity.x[d]/(double)AtomCount;
|
---|
[cee0b57] | 589 | }
|
---|
| 590 | // solve a constrained potential if we are meant to
|
---|
| 591 | if (configuration.DoConstrainedMD) {
|
---|
| 592 | // calculate forces and potential
|
---|
| 593 | atom **PermutationMap = NULL;
|
---|
[e138de] | 594 | ConstrainedPotentialEnergy = MinimiseConstrainedPotential(PermutationMap,configuration.DoConstrainedMD, 0, configuration.GetIsAngstroem());
|
---|
| 595 | EvaluateConstrainedForces(configuration.DoConstrainedMD, 0, PermutationMap, &Force);
|
---|
[cee0b57] | 596 | Free(&PermutationMap);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | // and perform Verlet integration for each atom with position, velocity and force vector
|
---|
[4a7776a] | 600 | // check size of vectors
|
---|
| 601 | ActOnAllAtoms( &atom::ResizeTrajectory, MDSteps+10 );
|
---|
[cee0b57] | 602 |
|
---|
[4a7776a] | 603 | ActOnAllAtoms( &atom::VelocityVerletUpdate, MDSteps, &configuration, &Force);
|
---|
[cee0b57] | 604 | }
|
---|
| 605 | // correct velocities (rather momenta) so that center of mass remains motionless
|
---|
[4a7776a] | 606 | Velocity.Zero();
|
---|
[cee0b57] | 607 | IonMass = 0.;
|
---|
[4a7776a] | 608 | ActOnAllAtoms ( &atom::SumUpKineticEnergy, MDSteps, &IonMass, &Velocity );
|
---|
| 609 |
|
---|
[cee0b57] | 610 | // correct velocities (rather momenta) so that center of mass remains motionless
|
---|
[4a7776a] | 611 | Velocity.Scale(1./IonMass);
|
---|
[cee0b57] | 612 | ActualTemp = 0.;
|
---|
[4a7776a] | 613 | ActOnAllAtoms ( &atom::CorrectVelocity, &ActualTemp, MDSteps, &Velocity );
|
---|
[cee0b57] | 614 | Thermostats(configuration, ActualTemp, Berendsen);
|
---|
| 615 | MDSteps++;
|
---|
| 616 |
|
---|
| 617 | // exit
|
---|
| 618 | return true;
|
---|
| 619 | };
|
---|
| 620 |
|
---|
| 621 | /** Implementation of various thermostats.
|
---|
| 622 | * All these thermostats apply an additional force which has the following forms:
|
---|
| 623 | * -# Woodcock
|
---|
| 624 | * \f$p_i \rightarrow \sqrt{\frac{T_0}{T}} \cdot p_i\f$
|
---|
| 625 | * -# Gaussian
|
---|
| 626 | * \f$ \frac{ \sum_i \frac{p_i}{m_i} \frac{\partial V}{\partial q_i}} {\sum_i \frac{p^2_i}{m_i}} \cdot p_i\f$
|
---|
| 627 | * -# Langevin
|
---|
| 628 | * \f$p_{i,n} \rightarrow \sqrt{1-\alpha^2} p_{i,0} + \alpha p_r\f$
|
---|
| 629 | * -# Berendsen
|
---|
| 630 | * \f$p_i \rightarrow \left [ 1+ \frac{\delta t}{\tau_T} \left ( \frac{T_0}{T} \right ) \right ]^{\frac{1}{2}} \cdot p_i\f$
|
---|
| 631 | * -# Nose-Hoover
|
---|
| 632 | * \f$\zeta p_i \f$ with \f$\frac{\partial \zeta}{\partial t} = \frac{1}{M_s} \left ( \sum^N_{i=1} \frac{p_i^2}{m_i} - g k_B T \right )\f$
|
---|
| 633 | * These Thermostats either simply rescale the velocities, thus this function should be called after ion velocities have been updated, and/or
|
---|
| 634 | * have a constraint force acting additionally on the ions. In the latter case, the ion speeds have to be modified
|
---|
| 635 | * belatedly and the constraint force set.
|
---|
| 636 | * \param *P Problem at hand
|
---|
| 637 | * \param i which of the thermostats to take: 0 - none, 1 - Woodcock, 2 - Gaussian, 3 - Langevin, 4 - Berendsen, 5 - Nose-Hoover
|
---|
| 638 | * \sa InitThermostat()
|
---|
| 639 | */
|
---|
| 640 | void molecule::Thermostats(config &configuration, double ActualTemp, int Thermostat)
|
---|
| 641 | {
|
---|
| 642 | double ekin = 0.;
|
---|
| 643 | double E = 0., G = 0.;
|
---|
| 644 | double delta_alpha = 0.;
|
---|
| 645 | double ScaleTempFactor;
|
---|
| 646 | gsl_rng * r;
|
---|
| 647 | const gsl_rng_type * T;
|
---|
| 648 |
|
---|
| 649 | // calculate scale configuration
|
---|
| 650 | ScaleTempFactor = configuration.TargetTemp/ActualTemp;
|
---|
| 651 |
|
---|
| 652 | // differentating between the various thermostats
|
---|
| 653 | switch(Thermostat) {
|
---|
| 654 | case None:
|
---|
[e138de] | 655 | Log() << Verbose(2) << "Applying no thermostat..." << endl;
|
---|
[cee0b57] | 656 | break;
|
---|
| 657 | case Woodcock:
|
---|
| 658 | if ((configuration.ScaleTempStep > 0) && ((MDSteps-1) % configuration.ScaleTempStep == 0)) {
|
---|
[e138de] | 659 | Log() << Verbose(2) << "Applying Woodcock thermostat..." << endl;
|
---|
[4a7776a] | 660 | ActOnAllAtoms( &atom::Thermostat_Woodcock, sqrt(ScaleTempFactor), MDSteps, &ekin );
|
---|
[cee0b57] | 661 | }
|
---|
| 662 | break;
|
---|
| 663 | case Gaussian:
|
---|
[e138de] | 664 | Log() << Verbose(2) << "Applying Gaussian thermostat..." << endl;
|
---|
[4a7776a] | 665 | ActOnAllAtoms( &atom::Thermostat_Gaussian_init, MDSteps, &G, &E );
|
---|
| 666 |
|
---|
[e138de] | 667 | Log() << Verbose(1) << "Gaussian Least Constraint constant is " << G/E << "." << endl;
|
---|
[4a7776a] | 668 | ActOnAllAtoms( &atom::Thermostat_Gaussian_least_constraint, MDSteps, G/E, &ekin, &configuration);
|
---|
| 669 |
|
---|
[cee0b57] | 670 | break;
|
---|
| 671 | case Langevin:
|
---|
[e138de] | 672 | Log() << Verbose(2) << "Applying Langevin thermostat..." << endl;
|
---|
[cee0b57] | 673 | // init random number generator
|
---|
| 674 | gsl_rng_env_setup();
|
---|
| 675 | T = gsl_rng_default;
|
---|
| 676 | r = gsl_rng_alloc (T);
|
---|
| 677 | // Go through each ion
|
---|
[4a7776a] | 678 | ActOnAllAtoms( &atom::Thermostat_Langevin, MDSteps, r, &ekin, &configuration );
|
---|
[cee0b57] | 679 | break;
|
---|
[4a7776a] | 680 |
|
---|
[cee0b57] | 681 | case Berendsen:
|
---|
[e138de] | 682 | Log() << Verbose(2) << "Applying Berendsen-VanGunsteren thermostat..." << endl;
|
---|
[4a7776a] | 683 | ActOnAllAtoms( &atom::Thermostat_Berendsen, MDSteps, ScaleTempFactor, &ekin, &configuration );
|
---|
[cee0b57] | 684 | break;
|
---|
[4a7776a] | 685 |
|
---|
[cee0b57] | 686 | case NoseHoover:
|
---|
[e138de] | 687 | Log() << Verbose(2) << "Applying Nose-Hoover thermostat..." << endl;
|
---|
[cee0b57] | 688 | // dynamically evolve alpha (the additional degree of freedom)
|
---|
| 689 | delta_alpha = 0.;
|
---|
[4a7776a] | 690 | ActOnAllAtoms( &atom::Thermostat_NoseHoover_init, MDSteps, &delta_alpha );
|
---|
[cee0b57] | 691 | delta_alpha = (delta_alpha - (3.*AtomCount+1.) * configuration.TargetTemp)/(configuration.HooverMass*Units2Electronmass);
|
---|
| 692 | configuration.alpha += delta_alpha*configuration.Deltat;
|
---|
[e138de] | 693 | Log() << Verbose(3) << "alpha = " << delta_alpha << " * " << configuration.Deltat << " = " << configuration.alpha << "." << endl;
|
---|
[cee0b57] | 694 | // apply updated alpha as additional force
|
---|
[4a7776a] | 695 | ActOnAllAtoms( &atom::Thermostat_NoseHoover_scale, MDSteps, &ekin, &configuration );
|
---|
[cee0b57] | 696 | break;
|
---|
| 697 | }
|
---|
[e138de] | 698 | Log() << Verbose(1) << "Kinetic energy is " << ekin << "." << endl;
|
---|
[cee0b57] | 699 | };
|
---|