[9e23a3] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * MinimiseConstrainedPotential.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Feb 23, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 21 |
|
---|
| 22 | #include <gsl/gsl_matrix.h>
|
---|
| 23 | #include <gsl/gsl_vector.h>
|
---|
| 24 | #include <gsl/gsl_linalg.h>
|
---|
| 25 |
|
---|
| 26 | #include "atom.hpp"
|
---|
| 27 | #include "config.hpp"
|
---|
[3bdb6d] | 28 | #include "Element/element.hpp"
|
---|
[9e23a3] | 29 | #include "CodePatterns/enumeration.hpp"
|
---|
| 30 | #include "CodePatterns/Info.hpp"
|
---|
| 31 | #include "CodePatterns/Verbose.hpp"
|
---|
| 32 | #include "CodePatterns/Log.hpp"
|
---|
[a9b86d] | 33 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
[255829] | 34 | #include "Helpers/helpers.hpp"
|
---|
[9e23a3] | 35 | #include "molecule.hpp"
|
---|
| 36 | #include "LinearAlgebra/Plane.hpp"
|
---|
| 37 | #include "World.hpp"
|
---|
| 38 |
|
---|
| 39 | #include "Dynamics/MinimiseConstrainedPotential.hpp"
|
---|
| 40 |
|
---|
| 41 |
|
---|
[e355762] | 42 | MinimiseConstrainedPotential::MinimiseConstrainedPotential(
|
---|
| 43 | molecule::atomSet &_atoms,
|
---|
| 44 | std::map<atom*, atom *> &_PermutationMap) :
|
---|
| 45 | atoms(_atoms),
|
---|
| 46 | PermutationMap(_PermutationMap)
|
---|
[9e23a3] | 47 | {}
|
---|
| 48 |
|
---|
| 49 | MinimiseConstrainedPotential::~MinimiseConstrainedPotential()
|
---|
| 50 | {}
|
---|
| 51 |
|
---|
[e355762] | 52 | double MinimiseConstrainedPotential::operator()(int _startstep, int _endstep, bool IsAngstroem)
|
---|
[9e23a3] | 53 | {
|
---|
| 54 | double Potential, OldPotential, OlderPotential;
|
---|
| 55 | int round;
|
---|
| 56 | atom *Sprinter = NULL;
|
---|
| 57 | DistanceMap::iterator Rider, Strider;
|
---|
| 58 |
|
---|
| 59 | // set to zero
|
---|
[e355762] | 60 | PermutationMap.clear();
|
---|
| 61 | DoubleList.clear();
|
---|
| 62 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 63 | DistanceList[*iter].clear();
|
---|
[9e23a3] | 64 | }
|
---|
[e355762] | 65 | DistanceList.clear();
|
---|
| 66 | DistanceIterators.clear();
|
---|
| 67 | DistanceIterators.clear();
|
---|
[9e23a3] | 68 |
|
---|
| 69 | /// Minimise the potential
|
---|
| 70 | // set Lagrange multiplier constants
|
---|
| 71 | PenaltyConstants[0] = 10.;
|
---|
| 72 | PenaltyConstants[1] = 1.;
|
---|
| 73 | PenaltyConstants[2] = 1e+7; // just a huge penalty
|
---|
| 74 | // generate the distance list
|
---|
[47d041] | 75 | LOG(1, "Allocating, initializting and filling the distance list ... ");
|
---|
[9e23a3] | 76 | FillDistanceList();
|
---|
| 77 |
|
---|
| 78 | // create the initial PermutationMap (source -> target)
|
---|
| 79 | CreateInitialLists();
|
---|
| 80 |
|
---|
| 81 | // make the PermutationMap injective by checking whether we have a non-zero constants[2] term in it
|
---|
[47d041] | 82 | LOG(1, "Making the PermutationMap injective ... ");
|
---|
[9e23a3] | 83 | MakeInjectivePermutation();
|
---|
[e355762] | 84 | DoubleList.clear();
|
---|
[9e23a3] | 85 |
|
---|
| 86 | // argument minimise the constrained potential in this injective PermutationMap
|
---|
[47d041] | 87 | LOG(1, "Argument minimising the PermutationMap.");
|
---|
[9e23a3] | 88 | OldPotential = 1e+10;
|
---|
| 89 | round = 0;
|
---|
| 90 | do {
|
---|
[47d041] | 91 | LOG(2, "Starting round " << ++round << ", at current potential " << OldPotential << " ... ");
|
---|
[9e23a3] | 92 | OlderPotential = OldPotential;
|
---|
| 93 | molecule::atomSet::const_iterator iter;
|
---|
| 94 | do {
|
---|
| 95 | iter = atoms.begin();
|
---|
| 96 | for (; iter != atoms.end(); ++iter) {
|
---|
[e355762] | 97 | CalculateDoubleList();
|
---|
[9e23a3] | 98 | PrintPermutationMap();
|
---|
[e355762] | 99 | Sprinter = DistanceIterators[(*iter)]->second; // store initial partner
|
---|
| 100 | Strider = DistanceIterators[(*iter)]; //remember old iterator
|
---|
| 101 | DistanceIterators[(*iter)] = StepList[(*iter)];
|
---|
| 102 | if (DistanceIterators[(*iter)] == DistanceList[(*iter)].end()) {// stop, before we run through the list and still on
|
---|
| 103 | DistanceIterators[(*iter)] == DistanceList[(*iter)].begin();
|
---|
[9e23a3] | 104 | break;
|
---|
| 105 | }
|
---|
[47d041] | 106 | //LOG(2, "Current Walker: " << *(*iter) << " with old/next candidate " << *Sprinter << "/" << *DistanceIterators[(*iter)]->second << ".");
|
---|
[9e23a3] | 107 | // find source of the new target
|
---|
| 108 | molecule::atomSet::const_iterator runner = atoms.begin();
|
---|
| 109 | for (; runner != atoms.end(); ++runner) { // find the source whose toes we might be stepping on (Walker's new target should be in use by another already)
|
---|
[e355762] | 110 | if (PermutationMap[(*runner)] == DistanceIterators[(*iter)]->second) {
|
---|
[47d041] | 111 | //LOG(2, "Found the corresponding owner " << *(*runner) << " to " << *PermutationMap[(*runner)] << ".");
|
---|
[9e23a3] | 112 | break;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | if (runner != atoms.end()) { // we found the other source
|
---|
| 116 | // then look in its distance list for Sprinter
|
---|
[e355762] | 117 | Rider = DistanceList[(*runner)].begin();
|
---|
| 118 | for (; Rider != DistanceList[(*runner)].end(); Rider++)
|
---|
[9e23a3] | 119 | if (Rider->second == Sprinter)
|
---|
| 120 | break;
|
---|
[e355762] | 121 | if (Rider != DistanceList[(*runner)].end()) { // if we have found one
|
---|
[47d041] | 122 | //LOG(2, "Current Other: " << *(*runner) << " with old/next candidate " << *PermutationMap[(*runner)] << "/" << *Rider->second << ".");
|
---|
[9e23a3] | 123 | // exchange both
|
---|
[e355762] | 124 | PermutationMap[(*iter)] = DistanceIterators[(*iter)]->second; // put next farther distance into PermutationMap
|
---|
| 125 | PermutationMap[(*runner)] = Sprinter; // and hand the old target to its respective owner
|
---|
| 126 | CalculateDoubleList();
|
---|
[9e23a3] | 127 | PrintPermutationMap();
|
---|
| 128 | // calculate the new potential
|
---|
[47d041] | 129 | //LOG(2, "Checking new potential ...");
|
---|
[9e23a3] | 130 | Potential = ConstrainedPotential();
|
---|
| 131 | if (Potential > OldPotential) { // we made everything worse! Undo ...
|
---|
[47d041] | 132 | //LOG(3, "Nay, made the potential worse: " << Potential << " vs. " << OldPotential << "!");
|
---|
| 133 | //LOG(3, "Setting " << *(*runner) << "'s source to " << *DistanceIterators[(*runner)]->second << ".");
|
---|
[9e23a3] | 134 | // Undo for Runner (note, we haven't moved the iteration yet, we may use this)
|
---|
[e355762] | 135 | PermutationMap[(*runner)] = DistanceIterators[(*runner)]->second;
|
---|
[9e23a3] | 136 | // Undo for Walker
|
---|
[e355762] | 137 | DistanceIterators[(*iter)] = Strider; // take next farther distance target
|
---|
[47d041] | 138 | //LOG(3, "Setting " << *(*iter) << "'s source to " << *DistanceIterators[(*iter)]->second << ".");
|
---|
[e355762] | 139 | PermutationMap[(*iter)] = DistanceIterators[(*iter)]->second;
|
---|
[9e23a3] | 140 | } else {
|
---|
[e355762] | 141 | DistanceIterators[(*runner)] = Rider; // if successful also move the pointer in the iterator list
|
---|
[47d041] | 142 | LOG(3, "Found a better permutation, new potential is " << Potential << " vs." << OldPotential << ".");
|
---|
[9e23a3] | 143 | OldPotential = Potential;
|
---|
| 144 | }
|
---|
| 145 | if (Potential > PenaltyConstants[2]) {
|
---|
[47d041] | 146 | ELOG(1, "The two-step permutation procedure did not maintain injectivity!");
|
---|
[9e23a3] | 147 | exit(255);
|
---|
| 148 | }
|
---|
| 149 | } else {
|
---|
[47d041] | 150 | ELOG(1, **runner << " was not the owner of " << *Sprinter << "!");
|
---|
[9e23a3] | 151 | exit(255);
|
---|
| 152 | }
|
---|
| 153 | } else {
|
---|
[e355762] | 154 | PermutationMap[(*iter)] = DistanceIterators[(*iter)]->second; // new target has no source!
|
---|
[9e23a3] | 155 | }
|
---|
[e355762] | 156 | StepList[(*iter)]++; // take next farther distance target
|
---|
[9e23a3] | 157 | }
|
---|
| 158 | } while (++iter != atoms.end());
|
---|
| 159 | } while ((OlderPotential - OldPotential) > 1e-3);
|
---|
[47d041] | 160 | LOG(1, "done.");
|
---|
[9e23a3] | 161 |
|
---|
| 162 |
|
---|
| 163 | return ConstrainedPotential();
|
---|
| 164 | };
|
---|
| 165 |
|
---|
| 166 | void MinimiseConstrainedPotential::FillDistanceList()
|
---|
| 167 | {
|
---|
| 168 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 169 | for (molecule::atomSet::const_iterator runner = atoms.begin(); runner != atoms.end(); ++runner) {
|
---|
[e355762] | 170 | DistanceList[(*iter)].insert( DistancePair((*iter)->getPositionAtStep(startstep).distance((*runner)->getPositionAtStep(endstep)), (*runner)) );
|
---|
[9e23a3] | 171 | }
|
---|
| 172 | }
|
---|
| 173 | };
|
---|
| 174 |
|
---|
| 175 | void MinimiseConstrainedPotential::CreateInitialLists()
|
---|
| 176 | {
|
---|
| 177 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
[e355762] | 178 | StepList[(*iter)] = DistanceList[(*iter)].begin(); // stores the step to the next iterator that could be a possible next target
|
---|
| 179 | PermutationMap[(*iter)] = DistanceList[(*iter)].begin()->second; // always pick target with the smallest distance
|
---|
| 180 | DoubleList[DistanceList[(*iter)].begin()->second]++; // increase this target's source count (>1? not injective)
|
---|
| 181 | DistanceIterators[(*iter)] = DistanceList[(*iter)].begin(); // and remember which one we picked
|
---|
[47d041] | 182 | LOG(2, **iter << " starts with distance " << DistanceList[(*iter)].begin()->first << ".");
|
---|
[9e23a3] | 183 | }
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 | void MinimiseConstrainedPotential::MakeInjectivePermutation()
|
---|
| 187 | {
|
---|
| 188 | molecule::atomSet::const_iterator iter = atoms.begin();
|
---|
| 189 | DistanceMap::iterator NewBase;
|
---|
| 190 | double Potential = fabs(ConstrainedPotential());
|
---|
| 191 |
|
---|
| 192 | if (atoms.empty()) {
|
---|
[47d041] | 193 | ELOG(1, "Molecule is empty.");
|
---|
[9e23a3] | 194 | return;
|
---|
| 195 | }
|
---|
| 196 | while ((Potential) > PenaltyConstants[2]) {
|
---|
[e355762] | 197 | CalculateDoubleList();
|
---|
[9e23a3] | 198 | PrintPermutationMap();
|
---|
| 199 | iter++;
|
---|
| 200 | if (iter == atoms.end()) // round-robin at the end
|
---|
| 201 | iter = atoms.begin();
|
---|
[e355762] | 202 | if (DoubleList[DistanceIterators[(*iter)]->second] <= 1) // no need to make those injective that aren't
|
---|
[9e23a3] | 203 | continue;
|
---|
| 204 | // now, try finding a new one
|
---|
| 205 | Potential = TryNextNearestNeighbourForInjectivePermutation((*iter), Potential);
|
---|
| 206 | }
|
---|
[e355762] | 207 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 208 | // now each single entry in the DoubleList should be <=1
|
---|
| 209 | if (DoubleList[*iter] > 1) {
|
---|
[47d041] | 210 | ELOG(0, "Failed to create an injective PermutationMap!");
|
---|
[9e23a3] | 211 | performCriticalExit();
|
---|
| 212 | }
|
---|
[e355762] | 213 | }
|
---|
[47d041] | 214 | LOG(1, "done.");
|
---|
[9e23a3] | 215 | };
|
---|
| 216 |
|
---|
[e355762] | 217 | unsigned int MinimiseConstrainedPotential::CalculateDoubleList()
|
---|
| 218 | {
|
---|
| 219 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
|
---|
| 220 | DoubleList[*iter] = 0;
|
---|
| 221 | unsigned int doubles = 0;
|
---|
| 222 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
|
---|
| 223 | DoubleList[ PermutationMap[*iter] ]++;
|
---|
| 224 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
|
---|
| 225 | if (DoubleList[*iter] > 1)
|
---|
| 226 | doubles++;
|
---|
| 227 | if (doubles >0)
|
---|
[47d041] | 228 | LOG(2, "Found " << doubles << " Doubles.");
|
---|
[e355762] | 229 | return doubles;
|
---|
| 230 | };
|
---|
| 231 |
|
---|
[9e23a3] | 232 | void MinimiseConstrainedPotential::PrintPermutationMap() const
|
---|
| 233 | {
|
---|
| 234 | stringstream zeile1, zeile2;
|
---|
| 235 | int doubles = 0;
|
---|
| 236 | zeile1 << "PermutationMap: ";
|
---|
| 237 | zeile2 << " ";
|
---|
[e355762] | 238 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 239 | zeile1 << (*iter)->getName() << " ";
|
---|
| 240 | zeile2 << (PermutationMap[*iter])->getName() << " ";
|
---|
| 241 | }
|
---|
| 242 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 243 | std::map<atom *, unsigned int>::const_iterator value_iter = DoubleList.find(*iter);
|
---|
| 244 | if (value_iter->second > (unsigned int)1)
|
---|
| 245 | doubles++;
|
---|
[9e23a3] | 246 | }
|
---|
| 247 | if (doubles >0)
|
---|
[47d041] | 248 | LOG(2, "Found " << doubles << " Doubles.");
|
---|
| 249 | // LOG(2, zeile1.str() << endl << zeile2.str());
|
---|
[9e23a3] | 250 | };
|
---|
| 251 |
|
---|
| 252 | double MinimiseConstrainedPotential::ConstrainedPotential()
|
---|
| 253 | {
|
---|
| 254 | double tmp = 0.;
|
---|
| 255 | double result = 0.;
|
---|
| 256 | // go through every atom
|
---|
| 257 | atom *Runner = NULL;
|
---|
| 258 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 259 | // first term: distance to target
|
---|
[e355762] | 260 | Runner = PermutationMap[(*iter)]; // find target point
|
---|
[9e23a3] | 261 | tmp = ((*iter)->getPositionAtStep(startstep).distance(Runner->getPositionAtStep(endstep)));
|
---|
| 262 | tmp *= IsAngstroem ? 1. : 1./AtomicLengthToAngstroem;
|
---|
| 263 | result += PenaltyConstants[0] * tmp;
|
---|
[47d041] | 264 | //LOG(4, "Adding " << tmp*constants[0] << ".");
|
---|
[9e23a3] | 265 |
|
---|
| 266 | // second term: sum of distances to other trajectories
|
---|
| 267 | result += SumDistanceOfTrajectories((*iter));
|
---|
| 268 |
|
---|
| 269 | // third term: penalty for equal targets
|
---|
| 270 | result += PenalizeEqualTargets((*iter));
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | return result;
|
---|
| 274 | };
|
---|
| 275 |
|
---|
| 276 | double MinimiseConstrainedPotential::TryNextNearestNeighbourForInjectivePermutation(atom *Walker, double &OldPotential)
|
---|
| 277 | {
|
---|
| 278 | double Potential = 0;
|
---|
[e355762] | 279 | DistanceMap::iterator NewBase = DistanceIterators[Walker]; // store old base
|
---|
[9e23a3] | 280 | do {
|
---|
| 281 | NewBase++; // take next further distance in distance to targets list that's a target of no one
|
---|
[e355762] | 282 | } while ((DoubleList[NewBase->second] != 0) && (NewBase != DistanceList[Walker].end()));
|
---|
| 283 | if (NewBase != DistanceList[Walker].end()) {
|
---|
| 284 | PermutationMap[Walker] = NewBase->second;
|
---|
[9e23a3] | 285 | Potential = fabs(ConstrainedPotential());
|
---|
| 286 | if (Potential > OldPotential) { // undo
|
---|
[e355762] | 287 | PermutationMap[Walker] = DistanceIterators[Walker]->second;
|
---|
[9e23a3] | 288 | } else { // do
|
---|
[e355762] | 289 | DoubleList[DistanceIterators[Walker]->second]--; // decrease the old entry in the doubles list
|
---|
| 290 | DoubleList[NewBase->second]++; // increase the old entry in the doubles list
|
---|
| 291 | DistanceIterators[Walker] = NewBase;
|
---|
[9e23a3] | 292 | OldPotential = Potential;
|
---|
[47d041] | 293 | LOG(3, "Found a new permutation, new potential is " << OldPotential << ".");
|
---|
[9e23a3] | 294 | }
|
---|
| 295 | }
|
---|
| 296 | return Potential;
|
---|
| 297 | };
|
---|
| 298 |
|
---|
| 299 | double MinimiseConstrainedPotential::PenalizeEqualTargets(atom *Walker)
|
---|
| 300 | {
|
---|
| 301 | double result = 0.;
|
---|
| 302 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
[e355762] | 303 | if ((PermutationMap[Walker] == PermutationMap[(*iter)]) && (Walker < (*iter))) {
|
---|
[47d041] | 304 | // atom *Sprinter = PermutationMap[Walker->nr];
|
---|
| 305 | // if (DoLog(0)) {
|
---|
| 306 | // std::stringstream output;
|
---|
| 307 | // output << *Walker << " and " << *(*iter) << " are heading to the same target at ";
|
---|
| 308 | // output << Sprinter->getPosition(endstep);
|
---|
| 309 | // output << ", penalting.";
|
---|
| 310 | // LOG(0, output.str());
|
---|
| 311 | // }
|
---|
[9e23a3] | 312 | result += PenaltyConstants[2];
|
---|
[47d041] | 313 | //LOG(4, "INFO: Adding " << constants[2] << ".");
|
---|
[9e23a3] | 314 | }
|
---|
| 315 | }
|
---|
| 316 | return result;
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | double MinimiseConstrainedPotential::SumDistanceOfTrajectories(atom *Walker)
|
---|
| 320 | {
|
---|
| 321 | gsl_matrix *A = gsl_matrix_alloc(NDIM,NDIM);
|
---|
| 322 | gsl_vector *x = gsl_vector_alloc(NDIM);
|
---|
| 323 | atom *Sprinter = NULL;
|
---|
| 324 | Vector trajectory1, trajectory2, normal, TestVector;
|
---|
| 325 | double Norm1, Norm2, tmp, result = 0.;
|
---|
| 326 |
|
---|
| 327 | for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 328 | if ((*iter) == Walker) // hence, we only go up to the Walker, not beyond (similar to i=0; i<j; i++)
|
---|
| 329 | break;
|
---|
| 330 | // determine normalized trajectories direction vector (n1, n2)
|
---|
[e355762] | 331 | Sprinter = PermutationMap[Walker]; // find first target point
|
---|
[9e23a3] | 332 | trajectory1 = Sprinter->getPositionAtStep(endstep) - Walker->getPositionAtStep(startstep);
|
---|
| 333 | trajectory1.Normalize();
|
---|
| 334 | Norm1 = trajectory1.Norm();
|
---|
[e355762] | 335 | Sprinter = PermutationMap[(*iter)]; // find second target point
|
---|
[9e23a3] | 336 | trajectory2 = Sprinter->getPositionAtStep(endstep) - (*iter)->getPositionAtStep(startstep);
|
---|
| 337 | trajectory2.Normalize();
|
---|
| 338 | Norm2 = trajectory1.Norm();
|
---|
| 339 | // check whether either is zero()
|
---|
| 340 | if ((Norm1 < MYEPSILON) && (Norm2 < MYEPSILON)) {
|
---|
| 341 | tmp = Walker->getPositionAtStep(startstep).distance((*iter)->getPositionAtStep(startstep));
|
---|
| 342 | } else if (Norm1 < MYEPSILON) {
|
---|
[e355762] | 343 | Sprinter = PermutationMap[Walker]; // find first target point
|
---|
[9e23a3] | 344 | trajectory1 = Sprinter->getPositionAtStep(endstep) - (*iter)->getPositionAtStep(startstep);
|
---|
| 345 | trajectory2 *= trajectory1.ScalarProduct(trajectory2); // trajectory2 is scaled to unity, hence we don't need to divide by anything
|
---|
| 346 | trajectory1 -= trajectory2; // project the part in norm direction away
|
---|
| 347 | tmp = trajectory1.Norm(); // remaining norm is distance
|
---|
| 348 | } else if (Norm2 < MYEPSILON) {
|
---|
[e355762] | 349 | Sprinter = PermutationMap[(*iter)]; // find second target point
|
---|
[9e23a3] | 350 | trajectory2 = Sprinter->getPositionAtStep(endstep) - Walker->getPositionAtStep(startstep); // copy second offset
|
---|
| 351 | trajectory1 *= trajectory2.ScalarProduct(trajectory1); // trajectory1 is scaled to unity, hence we don't need to divide by anything
|
---|
| 352 | trajectory2 -= trajectory1; // project the part in norm direction away
|
---|
| 353 | tmp = trajectory2.Norm(); // remaining norm is distance
|
---|
| 354 | } else if ((fabs(trajectory1.ScalarProduct(trajectory2)/Norm1/Norm2) - 1.) < MYEPSILON) { // check whether they're linear dependent
|
---|
[47d041] | 355 | // std::stringstream output;
|
---|
| 356 | // output << "Both trajectories of " << *Walker << " and " << *Runner << " are linear dependent: ";
|
---|
| 357 | // output << trajectory1 << " and " << trajectory2;
|
---|
| 358 | // LOG(3, output.str());
|
---|
[9e23a3] | 359 | tmp = Walker->getPositionAtStep(startstep).distance((*iter)->getPositionAtStep(startstep));
|
---|
[47d041] | 360 | // LOG(0, " with distance " << tmp << ".");
|
---|
[9e23a3] | 361 | } else { // determine distance by finding minimum distance
|
---|
[47d041] | 362 | // std::stringstream output;
|
---|
| 363 | // output "Both trajectories of " << *Walker << " and " << *(*iter) << " are linear independent -- ";
|
---|
| 364 | // output "First Trajectory: " << trajectory1 << ". Second Trajectory: " << trajectory2);
|
---|
| 365 | // LOG(3, output.str());
|
---|
[9e23a3] | 366 | // determine normal vector for both
|
---|
| 367 | normal = Plane(trajectory1, trajectory2,0).getNormal();
|
---|
| 368 | // print all vectors for debugging
|
---|
[47d041] | 369 | // LOG(3, "INFO: Normal vector in between: " << normal);
|
---|
[9e23a3] | 370 | // setup matrix
|
---|
| 371 | for (int i=NDIM;i--;) {
|
---|
| 372 | gsl_matrix_set(A, 0, i, trajectory1[i]);
|
---|
| 373 | gsl_matrix_set(A, 1, i, trajectory2[i]);
|
---|
| 374 | gsl_matrix_set(A, 2, i, normal[i]);
|
---|
| 375 | gsl_vector_set(x,i, (Walker->getPositionAtStep(startstep)[i] - (*iter)->getPositionAtStep(startstep)[i]));
|
---|
| 376 | }
|
---|
| 377 | // solve the linear system by Householder transformations
|
---|
| 378 | gsl_linalg_HH_svx(A, x);
|
---|
| 379 | // distance from last component
|
---|
| 380 | tmp = gsl_vector_get(x,2);
|
---|
[47d041] | 381 | // LOG(0, " with distance " << tmp << ".");
|
---|
[9e23a3] | 382 | // test whether we really have the intersection (by checking on c_1 and c_2)
|
---|
| 383 | trajectory1.Scale(gsl_vector_get(x,0));
|
---|
| 384 | trajectory2.Scale(gsl_vector_get(x,1));
|
---|
| 385 | normal.Scale(gsl_vector_get(x,2));
|
---|
| 386 | TestVector = (*iter)->getPositionAtStep(startstep) + trajectory2 + normal
|
---|
| 387 | - (Walker->getPositionAtStep(startstep) + trajectory1);
|
---|
| 388 | if (TestVector.Norm() < MYEPSILON) {
|
---|
[47d041] | 389 | // LOG(2, "Test: ok.\tDistance of " << tmp << " is correct.");
|
---|
[9e23a3] | 390 | } else {
|
---|
[47d041] | 391 | // LOG(2, "Test: failed.\tIntersection is off by " << TestVector << ".");
|
---|
[9e23a3] | 392 | }
|
---|
| 393 | }
|
---|
| 394 | // add up
|
---|
| 395 | tmp *= IsAngstroem ? 1. : 1./AtomicLengthToAngstroem;
|
---|
| 396 | if (fabs(tmp) > MYEPSILON) {
|
---|
| 397 | result += PenaltyConstants[1] * 1./tmp;
|
---|
[47d041] | 398 | //LOG(4, "Adding " << 1./tmp*constants[1] << ".");
|
---|
[9e23a3] | 399 | }
|
---|
| 400 | }
|
---|
| 401 | return result;
|
---|
| 402 | };
|
---|
| 403 |
|
---|
| 404 | void MinimiseConstrainedPotential::EvaluateConstrainedForces(ForceMatrix *Force)
|
---|
| 405 | {
|
---|
| 406 | double constant = 10.;
|
---|
| 407 |
|
---|
| 408 | /// evaluate forces (only the distance to target dependent part) with the final PermutationMap
|
---|
[47d041] | 409 | LOG(1, "Calculating forces and adding onto ForceMatrix ... ");
|
---|
[9e23a3] | 410 | for(molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
[e355762] | 411 | atom *Sprinter = PermutationMap[(*iter)];
|
---|
[9e23a3] | 412 | // set forces
|
---|
| 413 | for (int i=NDIM;i++;)
|
---|
| 414 | Force->Matrix[0][(*iter)->getNr()][5+i] += 2.*constant*sqrt((*iter)->getPositionAtStep(startstep).distance(Sprinter->getPositionAtStep(endstep)));
|
---|
| 415 | }
|
---|
[47d041] | 416 | LOG(1, "done.");
|
---|
[9e23a3] | 417 | };
|
---|
| 418 |
|
---|