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