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