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