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