[1a48d2] | 1 | /*
|
---|
| 2 | * ForceAnnealing.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Aug 02, 2014
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef FORCEANNEALING_HPP_
|
---|
| 9 | #define FORCEANNEALING_HPP_
|
---|
| 10 |
|
---|
| 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
| 16 | #include "Atom/atom.hpp"
|
---|
| 17 | #include "Atom/AtomSet.hpp"
|
---|
| 18 | #include "CodePatterns/Assert.hpp"
|
---|
| 19 | #include "CodePatterns/Info.hpp"
|
---|
| 20 | #include "CodePatterns/Log.hpp"
|
---|
| 21 | #include "CodePatterns/Verbose.hpp"
|
---|
[917d11] | 22 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[1a48d2] | 23 | #include "Dynamics/AtomicForceManipulator.hpp"
|
---|
| 24 | #include "Fragmentation/ForceMatrix.hpp"
|
---|
[917d11] | 25 | #include "Graph/BoostGraphCreator.hpp"
|
---|
| 26 | #include "Graph/BoostGraphHelpers.hpp"
|
---|
| 27 | #include "Graph/BreadthFirstSearchGatherer.hpp"
|
---|
[1a48d2] | 28 | #include "Helpers/helpers.hpp"
|
---|
| 29 | #include "Helpers/defs.hpp"
|
---|
[e9fa721] | 30 | #include "LinearAlgebra/LinearSystemOfEquations.hpp"
|
---|
| 31 | #include "LinearAlgebra/MatrixContent.hpp"
|
---|
[1a48d2] | 32 | #include "LinearAlgebra/Vector.hpp"
|
---|
[e9fa721] | 33 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
[1a48d2] | 34 | #include "Thermostats/ThermoStatContainer.hpp"
|
---|
| 35 | #include "Thermostats/Thermostat.hpp"
|
---|
| 36 | #include "World.hpp"
|
---|
| 37 |
|
---|
[917d11] | 38 | /** This class is the essential build block for performing structural optimization.
|
---|
[1a48d2] | 39 | *
|
---|
| 40 | * Sadly, we have to use some static instances as so far values cannot be passed
|
---|
[7963c8] | 41 | * between actions. Hence, we need to store the current step and the adaptive-
|
---|
[917d11] | 42 | * step width (we cannot perform a line search, as we have no control over the
|
---|
[1a48d2] | 43 | * calculation of the forces).
|
---|
[917d11] | 44 | *
|
---|
| 45 | * However, we do use the bond graph, i.e. if a single atom needs to be shifted
|
---|
| 46 | * to the left, then the whole molecule left of it is shifted, too. This is
|
---|
| 47 | * controlled by the \a max_distance parameter.
|
---|
[1a48d2] | 48 | */
|
---|
| 49 | template <class T>
|
---|
| 50 | class ForceAnnealing : public AtomicForceManipulator<T>
|
---|
| 51 | {
|
---|
| 52 | public:
|
---|
| 53 | /** Constructor of class ForceAnnealing.
|
---|
[7963c8] | 54 | *
|
---|
| 55 | * \note We use a fixed delta t of 1.
|
---|
[1a48d2] | 56 | *
|
---|
| 57 | * \param _atoms set of atoms to integrate
|
---|
| 58 | * \param _Deltat time step width in atomic units
|
---|
| 59 | * \param _IsAngstroem whether length units are in angstroem or bohr radii
|
---|
| 60 | * \param _maxSteps number of optimization steps to perform
|
---|
[917d11] | 61 | * \param _max_distance up to this bond order is bond graph taken into account.
|
---|
[1a48d2] | 62 | */
|
---|
| 63 | ForceAnnealing(
|
---|
| 64 | AtomSetMixin<T> &_atoms,
|
---|
[d9632e] | 65 | const double _Deltat,
|
---|
[1a48d2] | 66 | bool _IsAngstroem,
|
---|
[917d11] | 67 | const size_t _maxSteps,
|
---|
[fdd121] | 68 | const int _max_distance,
|
---|
| 69 | const double _damping_factor) :
|
---|
[d9632e] | 70 | AtomicForceManipulator<T>(_atoms, _Deltat, _IsAngstroem),
|
---|
[917d11] | 71 | maxSteps(_maxSteps),
|
---|
| 72 | max_distance(_max_distance),
|
---|
[fdd121] | 73 | damping_factor(_damping_factor)
|
---|
[1a48d2] | 74 | {}
|
---|
[d9632e] | 75 |
|
---|
[1a48d2] | 76 | /** Destructor of class ForceAnnealing.
|
---|
| 77 | *
|
---|
| 78 | */
|
---|
| 79 | ~ForceAnnealing()
|
---|
| 80 | {}
|
---|
| 81 |
|
---|
| 82 | /** Performs Gradient optimization.
|
---|
| 83 | *
|
---|
| 84 | * We assume that forces have just been calculated.
|
---|
| 85 | *
|
---|
| 86 | *
|
---|
[93effb] | 87 | * \param CurrentTimeStep current time step (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
|
---|
[1a48d2] | 88 | * \param offset offset in matrix file to the first force component
|
---|
| 89 | * \todo This is not yet checked if it is correctly working with DoConstrainedMD set >0.
|
---|
| 90 | */
|
---|
[93effb] | 91 | void operator()(
|
---|
| 92 | const int _CurrentTimeStep,
|
---|
| 93 | const size_t _offset,
|
---|
| 94 | const bool _UseBondgraph)
|
---|
[1a48d2] | 95 | {
|
---|
| 96 | // make sum of forces equal zero
|
---|
[93effb] | 97 | AtomicForceManipulator<T>::correctForceMatrixForFixedCenterOfMass(_offset, _CurrentTimeStep);
|
---|
[1a48d2] | 98 |
|
---|
| 99 | // are we in initial step? Then set static entities
|
---|
[93effb] | 100 | Vector maxComponents(zeroVec);
|
---|
[1a48d2] | 101 | if (currentStep == 0) {
|
---|
| 102 | currentDeltat = AtomicForceManipulator<T>::Deltat;
|
---|
| 103 | currentStep = 1;
|
---|
| 104 | LOG(2, "DEBUG: Initial step, setting values, current step is #" << currentStep);
|
---|
[93effb] | 105 |
|
---|
| 106 | // always use atomic annealing on first step
|
---|
| 107 | anneal(_CurrentTimeStep, _offset, maxComponents);
|
---|
[1a48d2] | 108 | } else {
|
---|
| 109 | ++currentStep;
|
---|
| 110 | LOG(2, "DEBUG: current step is #" << currentStep);
|
---|
[93effb] | 111 |
|
---|
| 112 | if (_UseBondgraph)
|
---|
| 113 | annealWithBondGraph(_CurrentTimeStep, _offset, maxComponents);
|
---|
| 114 | else
|
---|
| 115 | anneal(_CurrentTimeStep, _offset, maxComponents);
|
---|
[1a48d2] | 116 | }
|
---|
| 117 |
|
---|
[93effb] | 118 | LOG(1, "STATUS: Largest remaining force components at step #"
|
---|
| 119 | << currentStep << " are " << maxComponents);
|
---|
| 120 |
|
---|
| 121 | // are we in final step? Remember to reset static entities
|
---|
| 122 | if (currentStep == maxSteps) {
|
---|
| 123 | LOG(2, "DEBUG: Final step, resetting values");
|
---|
| 124 | reset();
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[1ea892] | 128 | /** Helper function to calculate the Barzilai-Borwein stepwidth.
|
---|
| 129 | *
|
---|
| 130 | * \param _PositionDifference difference in position between current and last step
|
---|
| 131 | * \param _GradientDifference difference in gradient between current and last step
|
---|
| 132 | * \return step width according to Barzilai-Borwein
|
---|
| 133 | */
|
---|
| 134 | double getBarzilaiBorweinStepwidth(const Vector &_PositionDifference, const Vector &_GradientDifference)
|
---|
| 135 | {
|
---|
| 136 | double stepwidth = 0.;
|
---|
| 137 | if (_GradientDifference.NormSquared() > MYEPSILON)
|
---|
| 138 | stepwidth = fabs(_PositionDifference.ScalarProduct(_GradientDifference))/
|
---|
| 139 | _GradientDifference.NormSquared();
|
---|
| 140 | if (fabs(stepwidth) < 1e-10) {
|
---|
| 141 | // dont' warn in first step, deltat usage normal
|
---|
| 142 | if (currentStep != 1)
|
---|
| 143 | ELOG(1, "INFO: Barzilai-Borwein stepwidth is zero, using deltat " << currentDeltat << " instead.");
|
---|
| 144 | stepwidth = currentDeltat;
|
---|
| 145 | }
|
---|
| 146 | return stepwidth;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[93effb] | 149 | /** Performs Gradient optimization on the atoms.
|
---|
| 150 | *
|
---|
| 151 | * We assume that forces have just been calculated.
|
---|
| 152 | *
|
---|
| 153 | * \param CurrentTimeStep current time step (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
|
---|
| 154 | * \param offset offset in matrix file to the first force component
|
---|
| 155 | * \param maxComponents to be filled with maximum force component over all atoms
|
---|
| 156 | */
|
---|
| 157 | void anneal(
|
---|
| 158 | const int CurrentTimeStep,
|
---|
| 159 | const size_t offset,
|
---|
| 160 | Vector &maxComponents)
|
---|
| 161 | {
|
---|
| 162 | for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
---|
| 163 | iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
---|
| 164 | // atom's force vector gives steepest descent direction
|
---|
[5a289c] | 165 | const Vector oldPosition = (*iter)->getPositionAtStep(CurrentTimeStep-1 >= 0 ? CurrentTimeStep - 1 : 0);
|
---|
| 166 | const Vector currentPosition = (*iter)->getPositionAtStep(CurrentTimeStep);
|
---|
| 167 | const Vector oldGradient = (*iter)->getAtomicForceAtStep(CurrentTimeStep-1 >= 0 ? CurrentTimeStep - 1 : 0);
|
---|
| 168 | const Vector currentGradient = (*iter)->getAtomicForceAtStep(CurrentTimeStep);
|
---|
[93effb] | 169 | LOG(4, "DEBUG: oldPosition for atom " << **iter << " is " << oldPosition);
|
---|
| 170 | LOG(4, "DEBUG: currentPosition for atom " << **iter << " is " << currentPosition);
|
---|
| 171 | LOG(4, "DEBUG: oldGradient for atom " << **iter << " is " << oldGradient);
|
---|
| 172 | LOG(4, "DEBUG: currentGradient for atom " << **iter << " is " << currentGradient);
|
---|
| 173 | // LOG(4, "DEBUG: Force for atom " << **iter << " is " << currentGradient);
|
---|
| 174 |
|
---|
| 175 | // we use Barzilai-Borwein update with position reversed to get descent
|
---|
[1ea892] | 176 | const double stepwidth = getBarzilaiBorweinStepwidth(
|
---|
| 177 | currentPosition - oldPosition, currentGradient - oldGradient);
|
---|
[93effb] | 178 | Vector PositionUpdate = stepwidth * currentGradient;
|
---|
| 179 | LOG(3, "DEBUG: Update would be " << stepwidth << "*" << currentGradient << " = " << PositionUpdate);
|
---|
| 180 |
|
---|
| 181 | // extract largest components for showing progress of annealing
|
---|
| 182 | for(size_t i=0;i<NDIM;++i)
|
---|
[03d40df] | 183 | maxComponents[i] = std::max(maxComponents[i], fabs(currentGradient[i]));
|
---|
[93effb] | 184 |
|
---|
| 185 | // are we in initial step? Then don't check against velocity
|
---|
| 186 | if ((currentStep > 1) && (!(*iter)->getAtomicVelocity().IsZero()))
|
---|
| 187 | // update with currentDelta tells us how the current gradient relates to
|
---|
| 188 | // the last one: If it has become larger, reduce currentDelta
|
---|
| 189 | if ((PositionUpdate.ScalarProduct((*iter)->getAtomicVelocity()) < 0)
|
---|
| 190 | && (currentDeltat > MinimumDeltat)) {
|
---|
| 191 | currentDeltat = .5*currentDeltat;
|
---|
| 192 | LOG(2, "DEBUG: Upgrade in other direction: " << PositionUpdate.NormSquared()
|
---|
| 193 | << " > " << (*iter)->getAtomicVelocity().NormSquared()
|
---|
| 194 | << ", decreasing deltat: " << currentDeltat);
|
---|
| 195 | PositionUpdate = currentDeltat * currentGradient;
|
---|
| 196 | }
|
---|
| 197 | // finally set new values
|
---|
| 198 | (*iter)->setPosition(currentPosition + PositionUpdate);
|
---|
| 199 | (*iter)->setAtomicVelocity(PositionUpdate);
|
---|
| 200 | //std::cout << "Id of atom is " << (*iter)->getId() << std::endl;
|
---|
| 201 | // (*iter)->VelocityVerletUpdateU((*iter)->getId(), CurrentTimeStep-1, Deltat, IsAngstroem);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Performs Gradient optimization on the bonds.
|
---|
| 206 | *
|
---|
| 207 | * We assume that forces have just been calculated. These forces are projected
|
---|
| 208 | * onto the bonds and these are annealed subsequently by moving atoms in the
|
---|
| 209 | * bond neighborhood on either side conjunctively.
|
---|
| 210 | *
|
---|
| 211 | *
|
---|
[5a289c] | 212 | * \param CurrentTimeStep current time step (i.e. t where \f$ t + \Delta t \f$ is in the sense of the velocity verlet)
|
---|
[93effb] | 213 | * \param offset offset in matrix file to the first force component
|
---|
| 214 | * \param maxComponents to be filled with maximum force component over all atoms
|
---|
| 215 | */
|
---|
| 216 | void annealWithBondGraph(
|
---|
| 217 | const int CurrentTimeStep,
|
---|
| 218 | const size_t offset,
|
---|
| 219 | Vector &maxComponents)
|
---|
| 220 | {
|
---|
[917d11] | 221 | // get nodes on either side of selected bond via BFS discovery
|
---|
| 222 | // std::vector<atomId_t> atomids;
|
---|
| 223 | // for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
---|
| 224 | // iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
---|
| 225 | // atomids.push_back((*iter)->getId());
|
---|
| 226 | // }
|
---|
| 227 | // ASSERT( atomids.size() == AtomicForceManipulator<T>::atoms.size(),
|
---|
| 228 | // "ForceAnnealing() - could not gather all atomic ids?");
|
---|
| 229 | BoostGraphCreator BGcreator;
|
---|
| 230 | BGcreator.createFromRange(
|
---|
| 231 | AtomicForceManipulator<T>::atoms.begin(),
|
---|
| 232 | AtomicForceManipulator<T>::atoms.end(),
|
---|
| 233 | AtomicForceManipulator<T>::atoms.size(),
|
---|
| 234 | BreadthFirstSearchGatherer::AlwaysTruePredicate);
|
---|
| 235 | BreadthFirstSearchGatherer NodeGatherer(BGcreator);
|
---|
| 236 |
|
---|
[e9fa721] | 237 | /// We assume that a force is local, i.e. a bond is too short yet and hence
|
---|
| 238 | /// the atom needs to be moved. However, all the adjacent (bound) atoms might
|
---|
| 239 | /// already be at the perfect distance. If we just move the atom alone, we ruin
|
---|
| 240 | /// all the other bonds. Hence, it would be sensible to move every atom found
|
---|
| 241 | /// through the bond graph in the direction of the force as well by the same
|
---|
| 242 | /// PositionUpdate. This is almost what we are going to do.
|
---|
| 243 |
|
---|
| 244 | /// One more issue is: If we need to shorten bond, then we use the PositionUpdate
|
---|
| 245 | /// also on the the other bond partner already. This is because it is in the
|
---|
| 246 | /// direction of the bond. Therefore, the update is actually performed twice on
|
---|
| 247 | /// each bond partner, i.e. the step size is twice as large as it should be.
|
---|
| 248 | /// This problem only occurs when bonds need to be shortened, not when they
|
---|
| 249 | /// need to be made longer (then the force vector is facing the other
|
---|
| 250 | /// direction than the bond vector).
|
---|
| 251 | /// As a remedy we need to know the forces "per bond" and not per atom.
|
---|
| 252 | /// In effect, the gradient is the error per atom. However, we need an
|
---|
| 253 | /// error per bond. To this end, we set up a matrix A that translate the
|
---|
| 254 | /// vector of bond energies into a vector of atomic force component and
|
---|
| 255 | /// then we simply solve the linear system (inverse problem) via an SVD
|
---|
| 256 | /// and use the bond gradients to get the PositionUpdate for both bond
|
---|
| 257 | /// partners at the same time when we go through all bonds.
|
---|
| 258 |
|
---|
| 259 | // gather/enumerate all bonds
|
---|
| 260 | std::set<bond::ptr> allbonds;
|
---|
| 261 | std::vector<atomId_t> allatomids;
|
---|
[1a48d2] | 262 | for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
---|
| 263 | iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
---|
[e9fa721] | 264 | const atom &walker = *(*iter);
|
---|
| 265 | allatomids.push_back(walker.getId());
|
---|
| 266 | const BondList& ListOfBonds = walker.getListOfBonds();
|
---|
| 267 | for(BondList::const_iterator bonditer = ListOfBonds.begin();
|
---|
| 268 | bonditer != ListOfBonds.end(); ++bonditer) {
|
---|
| 269 | const bond::ptr ¤t_bond = *bonditer;
|
---|
| 270 | allbonds.insert(current_bond);
|
---|
| 271 | }
|
---|
[f49361] | 272 |
|
---|
[e9fa721] | 273 | // extract largest components for showing progress of annealing
|
---|
| 274 | const Vector currentGradient = (*iter)->getAtomicForce();
|
---|
| 275 | for(size_t i=0;i<NDIM;++i)
|
---|
| 276 | maxComponents[i] = std::max(maxComponents[i], fabs(currentGradient[i]));
|
---|
[1a48d2] | 277 |
|
---|
[e9fa721] | 278 | // reset force vector for next step except on final one
|
---|
| 279 | if (currentStep != maxSteps)
|
---|
| 280 | (*iter)->setAtomicForce(zeroVec);
|
---|
| 281 | }
|
---|
| 282 | std::sort(allatomids.begin(), allatomids.end());
|
---|
| 283 | // converting set back to vector is fastest when requiring sorted and unique,
|
---|
| 284 | // see https://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector
|
---|
| 285 | typedef std::vector<bond::ptr> bondvector_t;
|
---|
| 286 | bondvector_t bondvector( allbonds.begin(), allbonds.end() );
|
---|
[917d11] | 287 |
|
---|
[e9fa721] | 288 | // setup matrix A given the enumerated bonds
|
---|
| 289 | LinearSystemOfEquations lseq(AtomicForceManipulator<T>::atoms.size(), bondvector.size());
|
---|
| 290 | for (size_t i = 0;i<bondvector.size();++i) {
|
---|
| 291 | const atom* bondatom[2] = {
|
---|
| 292 | bondvector[i]->leftatom,
|
---|
| 293 | bondvector[i]->rightatom
|
---|
| 294 | };
|
---|
| 295 | size_t index[2];
|
---|
| 296 | for (size_t n=0;n<2;++n) {
|
---|
| 297 | const std::pair<std::vector<atomId_t>::iterator, std::vector<atomId_t>::iterator> atomiditer =
|
---|
| 298 | std::equal_range(allatomids.begin(), allatomids.end(), bondatom[n]->getId());
|
---|
| 299 | index[n] = std::distance(allatomids.begin(), atomiditer.first);
|
---|
| 300 | (*lseq.A).at(index[0],index[1]) = 1.;
|
---|
| 301 | (*lseq.A).at(index[1],index[0]) = 1.;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 | lseq.SetSymmetric(true);
|
---|
[917d11] | 305 |
|
---|
[e9fa721] | 306 | // for each component and for current and last time step
|
---|
| 307 | // solve Ax=y with given A and y being the vectorized atomic force
|
---|
| 308 | double *tmpforces = new double[bondvector.size()];
|
---|
| 309 | double *bondforces = new double[bondvector.size()];
|
---|
| 310 | double *oldbondforces = new double[bondvector.size()];
|
---|
| 311 | double *bondforceref[2] = {
|
---|
| 312 | bondforces,
|
---|
| 313 | oldbondforces
|
---|
| 314 | };
|
---|
| 315 | for (size_t n=0;n<bondvector.size();++n) {
|
---|
| 316 | bondforces[n] = 0.;
|
---|
| 317 | oldbondforces[n] = 0.;
|
---|
| 318 | }
|
---|
| 319 | for (size_t timestep = 0; timestep <= 1; ++timestep) {
|
---|
| 320 | for (size_t component = 0; component < NDIM; ++component) {
|
---|
| 321 | for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
---|
| 322 | iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
---|
| 323 | const atom &walker = *(*iter);
|
---|
| 324 | const std::pair<std::vector<atomId_t>::iterator, std::vector<atomId_t>::iterator> atomiditer =
|
---|
| 325 | std::equal_range(allatomids.begin(), allatomids.end(), walker.getId());
|
---|
| 326 | const size_t i = std::distance(allatomids.begin(), atomiditer.first);
|
---|
| 327 | (*lseq.b).at(i) = timestep == 0 ?
|
---|
| 328 | walker.getAtomicForce()[component] :
|
---|
| 329 | walker.getAtomicForceAtStep(CurrentTimeStep-2 >= 0 ? CurrentTimeStep - 2 : 0)[component];
|
---|
| 330 | }
|
---|
| 331 | lseq.GetSolutionAsArray(tmpforces);
|
---|
| 332 | for (size_t i = 0;i<bondvector.size();++i)
|
---|
| 333 | bondforceref[timestep][i] += tmpforces[i];
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
[917d11] | 336 |
|
---|
[e9fa721] | 337 | // step through each bond and shift the atoms
|
---|
| 338 | std::map<atomId_t, Vector> GatheredUpdates; //!< gathers all updates which are applied at the end
|
---|
| 339 | for (size_t i = 0;i<bondvector.size();++i) {
|
---|
| 340 | const atom* bondatom[2] = {
|
---|
| 341 | bondvector[i]->leftatom,
|
---|
| 342 | bondvector[i]->rightatom};
|
---|
| 343 | const double &bondforce = bondforces[i];
|
---|
| 344 | const double &oldbondforce = oldbondforces[i];
|
---|
| 345 | const double bondforcedifference = (bondforce - oldbondforce);
|
---|
| 346 | Vector BondVector = (bondatom[0]->getPosition() - bondatom[1]->getPosition());
|
---|
| 347 | BondVector.Normalize();
|
---|
| 348 | double stepwidth = 0.;
|
---|
| 349 | for (size_t n=0;n<2;++n) {
|
---|
| 350 | const Vector oldPosition = bondatom[n]->getPositionAtStep(CurrentTimeStep-2 >= 0 ? CurrentTimeStep - 2 : 0);
|
---|
| 351 | const Vector currentPosition = bondatom[n]->getPosition();
|
---|
| 352 | stepwidth += fabs((currentPosition - oldPosition).ScalarProduct(BondVector))/bondforcedifference;
|
---|
| 353 | }
|
---|
| 354 | stepwidth = stepwidth/2;
|
---|
| 355 | Vector PositionUpdate = stepwidth * BondVector;
|
---|
| 356 | if (fabs(stepwidth) < 1e-10) {
|
---|
| 357 | // dont' warn in first step, deltat usage normal
|
---|
| 358 | if (currentStep != 1)
|
---|
| 359 | ELOG(1, "INFO: Barzilai-Borwein stepwidth is zero, using deltat " << currentDeltat << " instead.");
|
---|
| 360 | PositionUpdate = currentDeltat * BondVector;
|
---|
| 361 | }
|
---|
| 362 | LOG(3, "DEBUG: Update would be " << PositionUpdate);
|
---|
| 363 |
|
---|
| 364 | // remove the edge
|
---|
[917d11] | 365 | #ifndef NDEBUG
|
---|
[e9fa721] | 366 | const bool status =
|
---|
[917d11] | 367 | #endif
|
---|
[e9fa721] | 368 | BGcreator.removeEdge(bondatom[0]->getId(), bondatom[1]->getId());
|
---|
| 369 | ASSERT( status, "ForceAnnealing() - edge to found bond is not present?");
|
---|
| 370 |
|
---|
| 371 | // gather nodes for either atom
|
---|
| 372 | BoostGraphHelpers::Nodeset_t bondside_set[2];
|
---|
| 373 | BreadthFirstSearchGatherer::distance_map_t distance_map[2];
|
---|
| 374 | for (size_t n=0;n<2;++n) {
|
---|
| 375 | bondside_set[n] = NodeGatherer(bondatom[n]->getId(), max_distance);
|
---|
| 376 | distance_map[n] = NodeGatherer.getDistances();
|
---|
| 377 | std::sort(bondside_set[n].begin(), bondside_set[n].end());
|
---|
[917d11] | 378 | }
|
---|
| 379 |
|
---|
[e9fa721] | 380 | // re-add edge
|
---|
| 381 | BGcreator.addEdge(bondatom[0]->getId(), bondatom[1]->getId());
|
---|
| 382 |
|
---|
| 383 | // add PositionUpdate for all nodes in the bondside_set
|
---|
| 384 | for (size_t n=0;n<2;++n) {
|
---|
| 385 | for (BoostGraphHelpers::Nodeset_t::const_iterator setiter = bondside_set[n].begin();
|
---|
| 386 | setiter != bondside_set[n].end(); ++setiter) {
|
---|
| 387 | const BreadthFirstSearchGatherer::distance_map_t::const_iterator diter
|
---|
| 388 | = distance_map[n].find(*setiter);
|
---|
| 389 | ASSERT( diter != distance_map[n].end(),
|
---|
| 390 | "ForceAnnealing() - could not find distance to an atom.");
|
---|
| 391 | const double factor = pow(damping_factor, diter->second);
|
---|
| 392 | LOG(3, "DEBUG: Update for atom #" << *setiter << " will be "
|
---|
| 393 | << factor << "*" << PositionUpdate);
|
---|
| 394 | if (GatheredUpdates.count((*setiter))) {
|
---|
| 395 | GatheredUpdates[(*setiter)] += factor*PositionUpdate;
|
---|
| 396 | } else {
|
---|
| 397 | GatheredUpdates.insert(
|
---|
| 398 | std::make_pair(
|
---|
| 399 | (*setiter),
|
---|
| 400 | factor*PositionUpdate) );
|
---|
| 401 | }
|
---|
[917d11] | 402 | }
|
---|
[e9fa721] | 403 | // invert for other atom
|
---|
| 404 | PositionUpdate *= -1;
|
---|
[917d11] | 405 | }
|
---|
[1a48d2] | 406 | }
|
---|
[e9fa721] | 407 |
|
---|
[917d11] | 408 | // apply the gathered updates
|
---|
| 409 | for (std::map<atomId_t, Vector>::const_iterator iter = GatheredUpdates.begin();
|
---|
| 410 | iter != GatheredUpdates.end(); ++iter) {
|
---|
| 411 | const atomId_t &atomid = iter->first;
|
---|
| 412 | const Vector &update = iter->second;
|
---|
| 413 | atom* const walker = World::getInstance().getAtom(AtomById(atomid));
|
---|
| 414 | ASSERT( walker != NULL,
|
---|
| 415 | "ForceAnnealing() - walker with id "+toString(atomid)+" has suddenly disappeared.");
|
---|
[14a64b] | 416 | LOG(3, "DEBUG: Applying update " << update << " to atom #" << atomid
|
---|
| 417 | << ", namely " << *walker);
|
---|
[917d11] | 418 | walker->setPosition( walker->getPosition() + update );
|
---|
| 419 | }
|
---|
[1a48d2] | 420 | }
|
---|
| 421 |
|
---|
[f49361] | 422 | /** Reset function to unset static entities and artificial velocities.
|
---|
| 423 | *
|
---|
| 424 | */
|
---|
| 425 | void reset()
|
---|
| 426 | {
|
---|
| 427 | currentDeltat = 0.;
|
---|
| 428 | currentStep = 0;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[1a48d2] | 431 | private:
|
---|
| 432 | //!> contains the current step in relation to maxsteps
|
---|
| 433 | static size_t currentStep;
|
---|
| 434 | //!> contains the maximum number of steps, determines initial and final step with currentStep
|
---|
| 435 | size_t maxSteps;
|
---|
| 436 | static double currentDeltat;
|
---|
| 437 | //!> minimum deltat for internal while loop (adaptive step width)
|
---|
| 438 | static double MinimumDeltat;
|
---|
[917d11] | 439 | //!> contains the maximum bond graph distance up to which shifts of a single atom are spread
|
---|
| 440 | const int max_distance;
|
---|
| 441 | //!> the shifted is dampened by this factor with the power of the bond graph distance to the shift causing atom
|
---|
| 442 | const double damping_factor;
|
---|
[1a48d2] | 443 | };
|
---|
| 444 |
|
---|
| 445 | template <class T>
|
---|
| 446 | double ForceAnnealing<T>::currentDeltat = 0.;
|
---|
| 447 | template <class T>
|
---|
| 448 | size_t ForceAnnealing<T>::currentStep = 0;
|
---|
| 449 | template <class T>
|
---|
| 450 | double ForceAnnealing<T>::MinimumDeltat = 1e-8;
|
---|
| 451 |
|
---|
| 452 | #endif /* FORCEANNEALING_HPP_ */
|
---|