| [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
 | 
|---|
 | 75 |   DoLog(1) && (Log() << Verbose(1) << "Allocating, initializting and filling the distance list ... " << endl);
 | 
|---|
 | 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
 | 
|---|
 | 82 |   DoLog(1) && (Log() << Verbose(1) << "Making the PermutationMap injective ... " << endl);
 | 
|---|
 | 83 |   MakeInjectivePermutation();
 | 
|---|
| [e355762] | 84 |   DoubleList.clear();
 | 
|---|
| [9e23a3] | 85 | 
 | 
|---|
 | 86 |   // argument minimise the constrained potential in this injective PermutationMap
 | 
|---|
 | 87 |   DoLog(1) && (Log() << Verbose(1) << "Argument minimising the PermutationMap." << endl);
 | 
|---|
 | 88 |   OldPotential = 1e+10;
 | 
|---|
 | 89 |   round = 0;
 | 
|---|
 | 90 |   do {
 | 
|---|
 | 91 |     DoLog(2) && (Log() << Verbose(2) << "Starting round " << ++round << ", at current potential " << OldPotential << " ... " << endl);
 | 
|---|
 | 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 |         }
 | 
|---|
| [e355762] | 106 |         //Log() << Verbose(2) << "Current Walker: " << *(*iter) << " with old/next candidate " << *Sprinter << "/" << *DistanceIterators[(*iter)]->second << "." << endl;
 | 
|---|
| [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) {
 | 
|---|
 | 111 |             //Log() << Verbose(2) << "Found the corresponding owner " << *(*runner) << " to " << *PermutationMap[(*runner)] << "." << endl;
 | 
|---|
| [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
 | 
|---|
 | 122 |             //Log() << Verbose(2) << "Current Other: " << *(*runner) << " with old/next candidate " << *PermutationMap[(*runner)] << "/" << *Rider->second << "." << endl;
 | 
|---|
| [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
 | 
|---|
 | 129 |             //Log() << Verbose(2) << "Checking new potential ..." << endl;
 | 
|---|
 | 130 |             Potential = ConstrainedPotential();
 | 
|---|
 | 131 |             if (Potential > OldPotential) { // we made everything worse! Undo ...
 | 
|---|
 | 132 |               //Log() << Verbose(3) << "Nay, made the potential worse: " << Potential << " vs. " << OldPotential << "!" << endl;
 | 
|---|
| [e355762] | 133 |               //Log() << Verbose(3) << "Setting " << *(*runner) << "'s source to " << *DistanceIterators[(*runner)]->second << "." << endl;
 | 
|---|
| [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
 | 
|---|
 | 138 |               //Log() << Verbose(3) << "Setting " << *(*iter) << "'s source to " << *DistanceIterators[(*iter)]->second << "." << endl;
 | 
|---|
 | 139 |               PermutationMap[(*iter)] = DistanceIterators[(*iter)]->second;
 | 
|---|
| [9e23a3] | 140 |             } else {
 | 
|---|
| [e355762] | 141 |               DistanceIterators[(*runner)] = Rider;  // if successful also move the pointer in the iterator list
 | 
|---|
| [9e23a3] | 142 |               DoLog(3) && (Log() << Verbose(3) << "Found a better permutation, new potential is " << Potential << " vs." << OldPotential << "." << endl);
 | 
|---|
 | 143 |               OldPotential = Potential;
 | 
|---|
 | 144 |             }
 | 
|---|
 | 145 |             if (Potential > PenaltyConstants[2]) {
 | 
|---|
 | 146 |               DoeLog(1) && (eLog()<< Verbose(1) << "The two-step permutation procedure did not maintain injectivity!" << endl);
 | 
|---|
 | 147 |               exit(255);
 | 
|---|
 | 148 |             }
 | 
|---|
 | 149 |             //Log() << Verbose(0) << endl;
 | 
|---|
 | 150 |           } else {
 | 
|---|
 | 151 |             DoeLog(1) && (eLog()<< Verbose(1) << **runner << " was not the owner of " << *Sprinter << "!" << endl);
 | 
|---|
 | 152 |             exit(255);
 | 
|---|
 | 153 |           }
 | 
|---|
 | 154 |         } else {
 | 
|---|
| [e355762] | 155 |           PermutationMap[(*iter)] = DistanceIterators[(*iter)]->second; // new target has no source!
 | 
|---|
| [9e23a3] | 156 |         }
 | 
|---|
| [e355762] | 157 |         StepList[(*iter)]++; // take next farther distance target
 | 
|---|
| [9e23a3] | 158 |       }
 | 
|---|
 | 159 |     } while (++iter != atoms.end());
 | 
|---|
 | 160 |   } while ((OlderPotential - OldPotential) > 1e-3);
 | 
|---|
 | 161 |   DoLog(1) && (Log() << Verbose(1) << "done." << endl);
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 |   return ConstrainedPotential();
 | 
|---|
 | 165 | };
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 | void MinimiseConstrainedPotential::FillDistanceList()
 | 
|---|
 | 168 | {
 | 
|---|
 | 169 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 170 |     for (molecule::atomSet::const_iterator runner = atoms.begin(); runner != atoms.end(); ++runner) {
 | 
|---|
| [e355762] | 171 |       DistanceList[(*iter)].insert( DistancePair((*iter)->getPositionAtStep(startstep).distance((*runner)->getPositionAtStep(endstep)), (*runner)) );
 | 
|---|
| [9e23a3] | 172 |     }
 | 
|---|
 | 173 |   }
 | 
|---|
 | 174 | };
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 | void MinimiseConstrainedPotential::CreateInitialLists()
 | 
|---|
 | 177 | {
 | 
|---|
 | 178 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| [e355762] | 179 |     StepList[(*iter)] = DistanceList[(*iter)].begin();    // stores the step to the next iterator that could be a possible next target
 | 
|---|
 | 180 |     PermutationMap[(*iter)] = DistanceList[(*iter)].begin()->second;   // always pick target with the smallest distance
 | 
|---|
 | 181 |     DoubleList[DistanceList[(*iter)].begin()->second]++;            // increase this target's source count (>1? not injective)
 | 
|---|
 | 182 |     DistanceIterators[(*iter)] = DistanceList[(*iter)].begin();    // and remember which one we picked
 | 
|---|
 | 183 |     DoLog(2) && (Log() << Verbose(2) << **iter << " starts with distance " << DistanceList[(*iter)].begin()->first << "." << endl);
 | 
|---|
| [9e23a3] | 184 |   }
 | 
|---|
 | 185 | };
 | 
|---|
 | 186 | 
 | 
|---|
 | 187 | void MinimiseConstrainedPotential::MakeInjectivePermutation()
 | 
|---|
 | 188 | {
 | 
|---|
 | 189 |   molecule::atomSet::const_iterator iter = atoms.begin();
 | 
|---|
 | 190 |   DistanceMap::iterator NewBase;
 | 
|---|
 | 191 |   double Potential = fabs(ConstrainedPotential());
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 |   if (atoms.empty()) {
 | 
|---|
 | 194 |     eLog() << Verbose(1) << "Molecule is empty." << endl;
 | 
|---|
 | 195 |     return;
 | 
|---|
 | 196 |   }
 | 
|---|
 | 197 |   while ((Potential) > PenaltyConstants[2]) {
 | 
|---|
| [e355762] | 198 |     CalculateDoubleList();
 | 
|---|
| [9e23a3] | 199 |     PrintPermutationMap();
 | 
|---|
 | 200 |     iter++;
 | 
|---|
 | 201 |     if (iter == atoms.end()) // round-robin at the end
 | 
|---|
 | 202 |       iter = atoms.begin();
 | 
|---|
| [e355762] | 203 |     if (DoubleList[DistanceIterators[(*iter)]->second] <= 1)  // no need to make those injective that aren't
 | 
|---|
| [9e23a3] | 204 |       continue;
 | 
|---|
 | 205 |     // now, try finding a new one
 | 
|---|
 | 206 |     Potential = TryNextNearestNeighbourForInjectivePermutation((*iter), Potential);
 | 
|---|
 | 207 |   }
 | 
|---|
| [e355762] | 208 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 209 |     // now each single entry in the DoubleList should be <=1
 | 
|---|
 | 210 |     if (DoubleList[*iter] > 1) {
 | 
|---|
| [9e23a3] | 211 |       DoeLog(0) && (eLog()<< Verbose(0) << "Failed to create an injective PermutationMap!" << endl);
 | 
|---|
 | 212 |       performCriticalExit();
 | 
|---|
 | 213 |     }
 | 
|---|
| [e355762] | 214 |   }
 | 
|---|
| [9e23a3] | 215 |   DoLog(1) && (Log() << Verbose(1) << "done." << endl);
 | 
|---|
 | 216 | };
 | 
|---|
 | 217 | 
 | 
|---|
| [e355762] | 218 | unsigned int MinimiseConstrainedPotential::CalculateDoubleList()
 | 
|---|
 | 219 | {
 | 
|---|
 | 220 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
 | 221 |     DoubleList[*iter] = 0;
 | 
|---|
 | 222 |   unsigned int doubles = 0;
 | 
|---|
 | 223 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
 | 224 |       DoubleList[ PermutationMap[*iter] ]++;
 | 
|---|
 | 225 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
 | 226 |     if (DoubleList[*iter] > 1)
 | 
|---|
 | 227 |       doubles++;
 | 
|---|
 | 228 |   if (doubles >0)
 | 
|---|
 | 229 |     DoLog(2) && (Log() << Verbose(2) << "Found " << doubles << " Doubles." << endl);
 | 
|---|
 | 230 |   return doubles;
 | 
|---|
 | 231 | };
 | 
|---|
 | 232 | 
 | 
|---|
| [9e23a3] | 233 | void MinimiseConstrainedPotential::PrintPermutationMap() const
 | 
|---|
 | 234 | {
 | 
|---|
 | 235 |   stringstream zeile1, zeile2;
 | 
|---|
 | 236 |   int doubles = 0;
 | 
|---|
 | 237 |   zeile1 << "PermutationMap: ";
 | 
|---|
 | 238 |   zeile2 << "                ";
 | 
|---|
| [e355762] | 239 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 240 |     zeile1 << (*iter)->getName() << " ";
 | 
|---|
 | 241 |     zeile2 << (PermutationMap[*iter])->getName() << " ";
 | 
|---|
 | 242 |   }
 | 
|---|
 | 243 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 244 |     std::map<atom *, unsigned int>::const_iterator value_iter = DoubleList.find(*iter);
 | 
|---|
 | 245 |     if (value_iter->second > (unsigned int)1)
 | 
|---|
 | 246 |       doubles++;
 | 
|---|
| [9e23a3] | 247 |   }
 | 
|---|
 | 248 |   if (doubles >0)
 | 
|---|
 | 249 |     DoLog(2) && (Log() << Verbose(2) << "Found " << doubles << " Doubles." << endl);
 | 
|---|
 | 250 | //  Log() << Verbose(2) << zeile1.str() << endl << zeile2.str() << endl;
 | 
|---|
 | 251 | };
 | 
|---|
 | 252 | 
 | 
|---|
 | 253 | double MinimiseConstrainedPotential::ConstrainedPotential()
 | 
|---|
 | 254 | {
 | 
|---|
 | 255 |   double tmp = 0.;
 | 
|---|
 | 256 |   double result = 0.;
 | 
|---|
 | 257 |   // go through every atom
 | 
|---|
 | 258 |   atom *Runner = NULL;
 | 
|---|
 | 259 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 260 |     // first term: distance to target
 | 
|---|
| [e355762] | 261 |     Runner = PermutationMap[(*iter)];   // find target point
 | 
|---|
| [9e23a3] | 262 |     tmp = ((*iter)->getPositionAtStep(startstep).distance(Runner->getPositionAtStep(endstep)));
 | 
|---|
 | 263 |     tmp *= IsAngstroem ? 1. : 1./AtomicLengthToAngstroem;
 | 
|---|
 | 264 |     result += PenaltyConstants[0] * tmp;
 | 
|---|
 | 265 |     //Log() << Verbose(4) << "Adding " << tmp*constants[0] << "." << endl;
 | 
|---|
 | 266 | 
 | 
|---|
 | 267 |     // second term: sum of distances to other trajectories
 | 
|---|
 | 268 |     result += SumDistanceOfTrajectories((*iter));
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 |     // third term: penalty for equal targets
 | 
|---|
 | 271 |     result += PenalizeEqualTargets((*iter));
 | 
|---|
 | 272 |   }
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 |   return result;
 | 
|---|
 | 275 | };
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 | double MinimiseConstrainedPotential::TryNextNearestNeighbourForInjectivePermutation(atom *Walker, double &OldPotential)
 | 
|---|
 | 278 | {
 | 
|---|
 | 279 |   double Potential = 0;
 | 
|---|
| [e355762] | 280 |   DistanceMap::iterator NewBase = DistanceIterators[Walker];  // store old base
 | 
|---|
| [9e23a3] | 281 |   do {
 | 
|---|
 | 282 |     NewBase++;  // take next further distance in distance to targets list that's a target of no one
 | 
|---|
| [e355762] | 283 |   } while ((DoubleList[NewBase->second] != 0) && (NewBase != DistanceList[Walker].end()));
 | 
|---|
 | 284 |   if (NewBase != DistanceList[Walker].end()) {
 | 
|---|
 | 285 |     PermutationMap[Walker] = NewBase->second;
 | 
|---|
| [9e23a3] | 286 |     Potential = fabs(ConstrainedPotential());
 | 
|---|
 | 287 |     if (Potential > OldPotential) { // undo
 | 
|---|
| [e355762] | 288 |       PermutationMap[Walker] = DistanceIterators[Walker]->second;
 | 
|---|
| [9e23a3] | 289 |     } else {  // do
 | 
|---|
| [e355762] | 290 |       DoubleList[DistanceIterators[Walker]->second]--;  // decrease the old entry in the doubles list
 | 
|---|
 | 291 |       DoubleList[NewBase->second]++;    // increase the old entry in the doubles list
 | 
|---|
 | 292 |       DistanceIterators[Walker] = NewBase;
 | 
|---|
| [9e23a3] | 293 |       OldPotential = Potential;
 | 
|---|
 | 294 |       DoLog(3) && (Log() << Verbose(3) << "Found a new permutation, new potential is " << OldPotential << "." << endl);
 | 
|---|
 | 295 |     }
 | 
|---|
 | 296 |   }
 | 
|---|
 | 297 |   return Potential;
 | 
|---|
 | 298 | };
 | 
|---|
 | 299 | 
 | 
|---|
 | 300 | double MinimiseConstrainedPotential::PenalizeEqualTargets(atom *Walker)
 | 
|---|
 | 301 | {
 | 
|---|
 | 302 |   double result = 0.;
 | 
|---|
 | 303 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| [e355762] | 304 |     if ((PermutationMap[Walker] == PermutationMap[(*iter)]) && (Walker < (*iter))) {
 | 
|---|
| [9e23a3] | 305 |   //    atom *Sprinter = PermutationMap[Walker->nr];
 | 
|---|
 | 306 |   //        Log() << Verbose(0) << *Walker << " and " << *(*iter) << " are heading to the same target at ";
 | 
|---|
 | 307 |   //        Log() << Verbose(0) << Sprinter->getPosition(endstep);
 | 
|---|
 | 308 |   //        Log() << Verbose(0) << ", penalting." << endl;
 | 
|---|
 | 309 |       result += PenaltyConstants[2];
 | 
|---|
 | 310 |       //Log() << Verbose(4) << "Adding " << constants[2] << "." << endl;
 | 
|---|
 | 311 |     }
 | 
|---|
 | 312 |   }
 | 
|---|
 | 313 |   return result;
 | 
|---|
 | 314 | };
 | 
|---|
 | 315 | 
 | 
|---|
 | 316 | double MinimiseConstrainedPotential::SumDistanceOfTrajectories(atom *Walker)
 | 
|---|
 | 317 | {
 | 
|---|
 | 318 |   gsl_matrix *A = gsl_matrix_alloc(NDIM,NDIM);
 | 
|---|
 | 319 |   gsl_vector *x = gsl_vector_alloc(NDIM);
 | 
|---|
 | 320 |   atom *Sprinter = NULL;
 | 
|---|
 | 321 |   Vector trajectory1, trajectory2, normal, TestVector;
 | 
|---|
 | 322 |   double Norm1, Norm2, tmp, result = 0.;
 | 
|---|
 | 323 | 
 | 
|---|
 | 324 |   for (molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 325 |     if ((*iter) == Walker) // hence, we only go up to the Walker, not beyond (similar to i=0; i<j; i++)
 | 
|---|
 | 326 |       break;
 | 
|---|
 | 327 |     // determine normalized trajectories direction vector (n1, n2)
 | 
|---|
| [e355762] | 328 |     Sprinter = PermutationMap[Walker];   // find first target point
 | 
|---|
| [9e23a3] | 329 |     trajectory1 = Sprinter->getPositionAtStep(endstep) - Walker->getPositionAtStep(startstep);
 | 
|---|
 | 330 |     trajectory1.Normalize();
 | 
|---|
 | 331 |     Norm1 = trajectory1.Norm();
 | 
|---|
| [e355762] | 332 |     Sprinter = PermutationMap[(*iter)];   // find second target point
 | 
|---|
| [9e23a3] | 333 |     trajectory2 = Sprinter->getPositionAtStep(endstep) - (*iter)->getPositionAtStep(startstep);
 | 
|---|
 | 334 |     trajectory2.Normalize();
 | 
|---|
 | 335 |     Norm2 = trajectory1.Norm();
 | 
|---|
 | 336 |     // check whether either is zero()
 | 
|---|
 | 337 |     if ((Norm1 < MYEPSILON) && (Norm2 < MYEPSILON)) {
 | 
|---|
 | 338 |       tmp = Walker->getPositionAtStep(startstep).distance((*iter)->getPositionAtStep(startstep));
 | 
|---|
 | 339 |     } else if (Norm1 < MYEPSILON) {
 | 
|---|
| [e355762] | 340 |       Sprinter = PermutationMap[Walker];   // find first target point
 | 
|---|
| [9e23a3] | 341 |       trajectory1 = Sprinter->getPositionAtStep(endstep) - (*iter)->getPositionAtStep(startstep);
 | 
|---|
 | 342 |       trajectory2 *= trajectory1.ScalarProduct(trajectory2); // trajectory2 is scaled to unity, hence we don't need to divide by anything
 | 
|---|
 | 343 |       trajectory1 -= trajectory2;   // project the part in norm direction away
 | 
|---|
 | 344 |       tmp = trajectory1.Norm();  // remaining norm is distance
 | 
|---|
 | 345 |     } else if (Norm2 < MYEPSILON) {
 | 
|---|
| [e355762] | 346 |       Sprinter = PermutationMap[(*iter)];   // find second target point
 | 
|---|
| [9e23a3] | 347 |       trajectory2 = Sprinter->getPositionAtStep(endstep) - Walker->getPositionAtStep(startstep);  // copy second offset
 | 
|---|
 | 348 |       trajectory1 *= trajectory2.ScalarProduct(trajectory1); // trajectory1 is scaled to unity, hence we don't need to divide by anything
 | 
|---|
 | 349 |       trajectory2 -= trajectory1;   // project the part in norm direction away
 | 
|---|
 | 350 |       tmp = trajectory2.Norm();  // remaining norm is distance
 | 
|---|
 | 351 |     } else if ((fabs(trajectory1.ScalarProduct(trajectory2)/Norm1/Norm2) - 1.) < MYEPSILON) { // check whether they're linear dependent
 | 
|---|
 | 352 |   //        Log() << Verbose(3) << "Both trajectories of " << *Walker << " and " << *Runner << " are linear dependent: ";
 | 
|---|
 | 353 |   //        Log() << Verbose(0) << trajectory1;
 | 
|---|
 | 354 |   //        Log() << Verbose(0) << " and ";
 | 
|---|
 | 355 |   //        Log() << Verbose(0) << trajectory2;
 | 
|---|
 | 356 |       tmp = Walker->getPositionAtStep(startstep).distance((*iter)->getPositionAtStep(startstep));
 | 
|---|
 | 357 |   //        Log() << Verbose(0) << " with distance " << tmp << "." << endl;
 | 
|---|
 | 358 |     } else { // determine distance by finding minimum distance
 | 
|---|
 | 359 |   //        Log() << Verbose(3) << "Both trajectories of " << *Walker << " and " << *(*iter) << " are linear independent ";
 | 
|---|
 | 360 |   //        Log() << Verbose(0) << endl;
 | 
|---|
 | 361 |   //        Log() << Verbose(0) << "First Trajectory: ";
 | 
|---|
 | 362 |   //        Log() << Verbose(0) << trajectory1 << endl;
 | 
|---|
 | 363 |   //        Log() << Verbose(0) << "Second Trajectory: ";
 | 
|---|
 | 364 |   //        Log() << Verbose(0) << trajectory2 << endl;
 | 
|---|
 | 365 |       // determine normal vector for both
 | 
|---|
 | 366 |       normal = Plane(trajectory1, trajectory2,0).getNormal();
 | 
|---|
 | 367 |       // print all vectors for debugging
 | 
|---|
 | 368 |   //        Log() << Verbose(0) << "Normal vector in between: ";
 | 
|---|
 | 369 |   //        Log() << Verbose(0) << normal << endl;
 | 
|---|
 | 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);
 | 
|---|
 | 381 |   //        Log() << Verbose(0) << " with distance " << tmp << "." << endl;
 | 
|---|
 | 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) {
 | 
|---|
 | 389 |   //          Log() << Verbose(2) << "Test: ok.\tDistance of " << tmp << " is correct." << endl;
 | 
|---|
 | 390 |       } else {
 | 
|---|
 | 391 |   //          Log() << Verbose(2) << "Test: failed.\tIntersection is off by ";
 | 
|---|
 | 392 |   //          Log() << Verbose(0) << TestVector;
 | 
|---|
 | 393 |   //          Log() << Verbose(0) << "." << endl;
 | 
|---|
 | 394 |       }
 | 
|---|
 | 395 |     }
 | 
|---|
 | 396 |     // add up
 | 
|---|
 | 397 |     tmp *= IsAngstroem ? 1. : 1./AtomicLengthToAngstroem;
 | 
|---|
 | 398 |     if (fabs(tmp) > MYEPSILON) {
 | 
|---|
 | 399 |       result += PenaltyConstants[1] * 1./tmp;
 | 
|---|
 | 400 |       //Log() << Verbose(4) << "Adding " << 1./tmp*constants[1] << "." << endl;
 | 
|---|
 | 401 |     }
 | 
|---|
 | 402 |   }
 | 
|---|
 | 403 |   return result;
 | 
|---|
 | 404 | };
 | 
|---|
 | 405 | 
 | 
|---|
 | 406 | void MinimiseConstrainedPotential::EvaluateConstrainedForces(ForceMatrix *Force)
 | 
|---|
 | 407 | {
 | 
|---|
 | 408 |   double constant = 10.;
 | 
|---|
 | 409 | 
 | 
|---|
 | 410 |   /// evaluate forces (only the distance to target dependent part) with the final PermutationMap
 | 
|---|
 | 411 |   DoLog(1) && (Log() << Verbose(1) << "Calculating forces and adding onto ForceMatrix ... " << endl);
 | 
|---|
 | 412 |   for(molecule::atomSet::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
| [e355762] | 413 |     atom *Sprinter = PermutationMap[(*iter)];
 | 
|---|
| [9e23a3] | 414 |     // set forces
 | 
|---|
 | 415 |     for (int i=NDIM;i++;)
 | 
|---|
 | 416 |       Force->Matrix[0][(*iter)->getNr()][5+i] += 2.*constant*sqrt((*iter)->getPositionAtStep(startstep).distance(Sprinter->getPositionAtStep(endstep)));
 | 
|---|
 | 417 |   }
 | 
|---|
 | 418 |   DoLog(1) && (Log() << Verbose(1) << "done." << endl);
 | 
|---|
 | 419 | };
 | 
|---|
 | 420 | 
 | 
|---|