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