| 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 <algorithm>
|
|---|
| 17 | #include <functional>
|
|---|
| 18 | #include <iterator>
|
|---|
| 19 |
|
|---|
| 20 | #include <boost/bind.hpp>
|
|---|
| 21 |
|
|---|
| 22 | #include "Atom/atom.hpp"
|
|---|
| 23 | #include "Atom/AtomSet.hpp"
|
|---|
| 24 | #include "CodePatterns/Assert.hpp"
|
|---|
| 25 | #include "CodePatterns/Info.hpp"
|
|---|
| 26 | #include "CodePatterns/Log.hpp"
|
|---|
| 27 | #include "CodePatterns/Verbose.hpp"
|
|---|
| 28 | #include "Descriptors/AtomIdDescriptor.hpp"
|
|---|
| 29 | #include "Dynamics/AtomicForceManipulator.hpp"
|
|---|
| 30 | #include "Dynamics/BondVectors.hpp"
|
|---|
| 31 | #include "Fragmentation/ForceMatrix.hpp"
|
|---|
| 32 | #include "Graph/BoostGraphCreator.hpp"
|
|---|
| 33 | #include "Graph/BoostGraphHelpers.hpp"
|
|---|
| 34 | #include "Graph/BreadthFirstSearchGatherer.hpp"
|
|---|
| 35 | #include "Helpers/helpers.hpp"
|
|---|
| 36 | #include "Helpers/defs.hpp"
|
|---|
| 37 | #include "LinearAlgebra/LinearSystemOfEquations.hpp"
|
|---|
| 38 | #include "LinearAlgebra/MatrixContent.hpp"
|
|---|
| 39 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 40 | #include "LinearAlgebra/VectorContent.hpp"
|
|---|
| 41 | #include "Thermostats/ThermoStatContainer.hpp"
|
|---|
| 42 | #include "Thermostats/Thermostat.hpp"
|
|---|
| 43 | #include "World.hpp"
|
|---|
| 44 |
|
|---|
| 45 | /** This class is the essential build block for performing structural optimization.
|
|---|
| 46 | *
|
|---|
| 47 | * Sadly, we have to use some static instances as so far values cannot be passed
|
|---|
| 48 | * between actions. Hence, we need to store the current step and the adaptive-
|
|---|
| 49 | * step width (we cannot perform a line search, as we have no control over the
|
|---|
| 50 | * calculation of the forces).
|
|---|
| 51 | *
|
|---|
| 52 | * However, we do use the bond graph, i.e. if a single atom needs to be shifted
|
|---|
| 53 | * to the left, then the whole molecule left of it is shifted, too. This is
|
|---|
| 54 | * controlled by the \a max_distance parameter.
|
|---|
| 55 | */
|
|---|
| 56 | template <class T>
|
|---|
| 57 | class ForceAnnealing : public AtomicForceManipulator<T>
|
|---|
| 58 | {
|
|---|
| 59 | public:
|
|---|
| 60 | /** Constructor of class ForceAnnealing.
|
|---|
| 61 | *
|
|---|
| 62 | * \note We use a fixed delta t of 1.
|
|---|
| 63 | *
|
|---|
| 64 | * \param _atoms set of atoms to integrate
|
|---|
| 65 | * \param _Deltat time step width in atomic units
|
|---|
| 66 | * \param _IsAngstroem whether length units are in angstroem or bohr radii
|
|---|
| 67 | * \param _maxSteps number of optimization steps to perform
|
|---|
| 68 | * \param _max_distance up to this bond order is bond graph taken into account.
|
|---|
| 69 | */
|
|---|
| 70 | ForceAnnealing(
|
|---|
| 71 | AtomSetMixin<T> &_atoms,
|
|---|
| 72 | const double _Deltat,
|
|---|
| 73 | bool _IsAngstroem,
|
|---|
| 74 | const size_t _maxSteps,
|
|---|
| 75 | const int _max_distance,
|
|---|
| 76 | const double _damping_factor) :
|
|---|
| 77 | AtomicForceManipulator<T>(_atoms, _Deltat, _IsAngstroem),
|
|---|
| 78 | maxSteps(_maxSteps),
|
|---|
| 79 | max_distance(_max_distance),
|
|---|
| 80 | damping_factor(_damping_factor)
|
|---|
| 81 | {}
|
|---|
| 82 |
|
|---|
| 83 | /** Destructor of class ForceAnnealing.
|
|---|
| 84 | *
|
|---|
| 85 | */
|
|---|
| 86 | ~ForceAnnealing()
|
|---|
| 87 | {}
|
|---|
| 88 |
|
|---|
| 89 | /** Performs Gradient optimization.
|
|---|
| 90 | *
|
|---|
| 91 | * We assume that forces have just been calculated.
|
|---|
| 92 | *
|
|---|
| 93 | *
|
|---|
| 94 | * \param CurrentTimeStep current time step (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
|
|---|
| 95 | * \param offset offset in matrix file to the first force component
|
|---|
| 96 | * \todo This is not yet checked if it is correctly working with DoConstrainedMD set >0.
|
|---|
| 97 | */
|
|---|
| 98 | void operator()(
|
|---|
| 99 | const int _CurrentTimeStep,
|
|---|
| 100 | const size_t _offset,
|
|---|
| 101 | const bool _UseBondgraph)
|
|---|
| 102 | {
|
|---|
| 103 | // make sum of forces equal zero
|
|---|
| 104 | AtomicForceManipulator<T>::correctForceMatrixForFixedCenterOfMass(_offset, _CurrentTimeStep);
|
|---|
| 105 |
|
|---|
| 106 | // are we in initial step? Then set static entities
|
|---|
| 107 | Vector maxComponents(zeroVec);
|
|---|
| 108 | if (currentStep == 0) {
|
|---|
| 109 | currentDeltat = AtomicForceManipulator<T>::Deltat;
|
|---|
| 110 | currentStep = 1;
|
|---|
| 111 | LOG(2, "DEBUG: Initial step, setting values, current step is #" << currentStep);
|
|---|
| 112 |
|
|---|
| 113 | // always use atomic annealing on first step
|
|---|
| 114 | anneal(_CurrentTimeStep, _offset, maxComponents);
|
|---|
| 115 | } else {
|
|---|
| 116 | ++currentStep;
|
|---|
| 117 | LOG(2, "DEBUG: current step is #" << currentStep);
|
|---|
| 118 |
|
|---|
| 119 | // bond graph annealing is always followed by a normal annealing
|
|---|
| 120 | if (_UseBondgraph)
|
|---|
| 121 | annealWithBondGraph(_CurrentTimeStep, _offset, maxComponents);
|
|---|
| 122 | // cannot store RemnantGradient in Atom's Force as it ruins BB stepwidth calculation
|
|---|
| 123 | else
|
|---|
| 124 | anneal(_CurrentTimeStep, _offset, maxComponents);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | LOG(1, "STATUS: Largest remaining force components at step #"
|
|---|
| 128 | << currentStep << " are " << maxComponents);
|
|---|
| 129 |
|
|---|
| 130 | // are we in final step? Remember to reset static entities
|
|---|
| 131 | if (currentStep == maxSteps) {
|
|---|
| 132 | LOG(2, "DEBUG: Final step, resetting values");
|
|---|
| 133 | reset();
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Performs Gradient optimization on the atoms.
|
|---|
| 138 | *
|
|---|
| 139 | * We assume that forces have just been calculated.
|
|---|
| 140 | *
|
|---|
| 141 | * \param CurrentTimeStep current time step (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
|
|---|
| 142 | * \param offset offset in matrix file to the first force component
|
|---|
| 143 | * \param maxComponents to be filled with maximum force component over all atoms
|
|---|
| 144 | */
|
|---|
| 145 | void anneal(
|
|---|
| 146 | const int CurrentTimeStep,
|
|---|
| 147 | const size_t offset,
|
|---|
| 148 | Vector &maxComponents)
|
|---|
| 149 | {
|
|---|
| 150 | bool deltat_decreased = false;
|
|---|
| 151 | for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
|---|
| 152 | iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
|---|
| 153 | // atom's force vector gives steepest descent direction
|
|---|
| 154 | const Vector oldPosition = (*iter)->getPositionAtStep(CurrentTimeStep-2 >= 0 ? CurrentTimeStep - 2 : 0);
|
|---|
| 155 | const Vector currentPosition = (*iter)->getPosition();
|
|---|
| 156 | const Vector oldGradient = (*iter)->getAtomicForceAtStep(CurrentTimeStep-2 >= 0 ? CurrentTimeStep - 2 : 0);
|
|---|
| 157 | const Vector currentGradient = (*iter)->getAtomicForce();
|
|---|
| 158 | LOG(4, "DEBUG: oldPosition for atom " << **iter << " is " << oldPosition);
|
|---|
| 159 | LOG(4, "DEBUG: currentPosition for atom " << **iter << " is " << currentPosition);
|
|---|
| 160 | LOG(4, "DEBUG: oldGradient for atom " << **iter << " is " << oldGradient);
|
|---|
| 161 | LOG(4, "DEBUG: currentGradient for atom " << **iter << " is " << currentGradient);
|
|---|
| 162 | // LOG(4, "DEBUG: Force for atom " << **iter << " is " << currentGradient);
|
|---|
| 163 |
|
|---|
| 164 | // we use Barzilai-Borwein update with position reversed to get descent
|
|---|
| 165 | const Vector PositionDifference = currentPosition - oldPosition;
|
|---|
| 166 | const Vector GradientDifference = (currentGradient - oldGradient);
|
|---|
| 167 | double stepwidth = 0.;
|
|---|
| 168 | if (GradientDifference.Norm() > MYEPSILON)
|
|---|
| 169 | stepwidth = fabs(PositionDifference.ScalarProduct(GradientDifference))/
|
|---|
| 170 | GradientDifference.NormSquared();
|
|---|
| 171 | if (fabs(stepwidth) < 1e-10) {
|
|---|
| 172 | // dont' warn in first step, deltat usage normal
|
|---|
| 173 | if (currentStep != 1)
|
|---|
| 174 | ELOG(1, "INFO: Barzilai-Borwein stepwidth is zero, using deltat " << currentDeltat << " instead.");
|
|---|
| 175 | stepwidth = currentDeltat;
|
|---|
| 176 | }
|
|---|
| 177 | Vector PositionUpdate = stepwidth * currentGradient;
|
|---|
| 178 | LOG(3, "DEBUG: Update would be " << stepwidth << "*" << currentGradient << " = " << PositionUpdate);
|
|---|
| 179 |
|
|---|
| 180 | // extract largest components for showing progress of annealing
|
|---|
| 181 | for(size_t i=0;i<NDIM;++i)
|
|---|
| 182 | if (currentGradient[i] > maxComponents[i])
|
|---|
| 183 | maxComponents[i] = currentGradient[i];
|
|---|
| 184 |
|
|---|
| 185 | // steps may go back and forth again (updates are of same magnitude but
|
|---|
| 186 | // have different sign: Check whether this is the case and one step with
|
|---|
| 187 | // deltat to interrupt this sequence
|
|---|
| 188 | if ((currentStep > 1) && (!PositionDifference.IsZero()))
|
|---|
| 189 | if ((PositionUpdate.ScalarProduct(PositionDifference) < 0)
|
|---|
| 190 | && (fabs(PositionUpdate.NormSquared()-PositionDifference.NormSquared()) < 1e-3)) {
|
|---|
| 191 | // for convergence we want a null sequence here, too
|
|---|
| 192 | if (!deltat_decreased) {
|
|---|
| 193 | deltat_decreased = true;
|
|---|
| 194 | currentDeltat = .5*currentDeltat;
|
|---|
| 195 | }
|
|---|
| 196 | LOG(2, "DEBUG: Upgrade in other direction: " << PositionUpdate
|
|---|
| 197 | << " > " << PositionDifference
|
|---|
| 198 | << ", using deltat: " << currentDeltat);
|
|---|
| 199 | PositionUpdate = currentDeltat * currentGradient;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | // finally set new values
|
|---|
| 203 | (*iter)->setPosition(currentPosition + PositionUpdate);
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | // knowing the number of bonds in total, we can setup the storage for the
|
|---|
| 208 | // projected forces
|
|---|
| 209 | enum whichatom_t {
|
|---|
| 210 | leftside=0,
|
|---|
| 211 | rightside=1,
|
|---|
| 212 | MAX_sides
|
|---|
| 213 | };
|
|---|
| 214 |
|
|---|
| 215 | /** Helper function to put bond force into a container.
|
|---|
| 216 | *
|
|---|
| 217 | * \param _walker atom
|
|---|
| 218 | * \param _current_bond current bond of \a _walker
|
|---|
| 219 | * \param _timestep time step
|
|---|
| 220 | * \param _force calculated bond force
|
|---|
| 221 | * \param _bv bondvectors for obtaining the correct index
|
|---|
| 222 | * \param _projected_forces container
|
|---|
| 223 | */
|
|---|
| 224 | static void ForceStore(
|
|---|
| 225 | const atom &_walker,
|
|---|
| 226 | const bond::ptr &_current_bond,
|
|---|
| 227 | const size_t &_timestep,
|
|---|
| 228 | const double _force,
|
|---|
| 229 | const BondVectors &_bv,
|
|---|
| 230 | std::vector< // time step
|
|---|
| 231 | std::vector< // which bond side
|
|---|
| 232 | std::vector<double> > // over all bonds
|
|---|
| 233 | > &_projected_forces)
|
|---|
| 234 | {
|
|---|
| 235 | std::vector<double> &forcelist = (&_walker == _current_bond->leftatom) ?
|
|---|
| 236 | _projected_forces[_timestep][leftside] : _projected_forces[_timestep][rightside];
|
|---|
| 237 | const size_t index = _bv.getIndexForBond(_current_bond);
|
|---|
| 238 | ASSERT( index != (size_t)-1,
|
|---|
| 239 | "ForceAnnealing() - could not find bond "+toString(*_current_bond)
|
|---|
| 240 | +" in bondvectors");
|
|---|
| 241 | forcelist[index] = _force;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /** Performs Gradient optimization on the bonds.
|
|---|
| 245 | *
|
|---|
| 246 | * We assume that forces have just been calculated. These forces are projected
|
|---|
| 247 | * onto the bonds and these are annealed subsequently by moving atoms in the
|
|---|
| 248 | * bond neighborhood on either side conjunctively.
|
|---|
| 249 | *
|
|---|
| 250 | *
|
|---|
| 251 | * \param CurrentTimeStep current time step (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
|
|---|
| 252 | * \param offset offset in matrix file to the first force component
|
|---|
| 253 | * \param maxComponents to be filled with maximum force component over all atoms
|
|---|
| 254 | */
|
|---|
| 255 | void annealWithBondGraph(
|
|---|
| 256 | const int CurrentTimeStep,
|
|---|
| 257 | const size_t offset,
|
|---|
| 258 | Vector &maxComponents)
|
|---|
| 259 | {
|
|---|
| 260 | // get nodes on either side of selected bond via BFS discovery
|
|---|
| 261 | BoostGraphCreator BGcreator;
|
|---|
| 262 | BGcreator.createFromRange(
|
|---|
| 263 | AtomicForceManipulator<T>::atoms.begin(),
|
|---|
| 264 | AtomicForceManipulator<T>::atoms.end(),
|
|---|
| 265 | AtomicForceManipulator<T>::atoms.size(),
|
|---|
| 266 | BreadthFirstSearchGatherer::AlwaysTruePredicate);
|
|---|
| 267 | BreadthFirstSearchGatherer NodeGatherer(BGcreator);
|
|---|
| 268 |
|
|---|
| 269 | /// We assume that a force is local, i.e. a bond is too short yet and hence
|
|---|
| 270 | /// the atom needs to be moved. However, all the adjacent (bound) atoms might
|
|---|
| 271 | /// already be at the perfect distance. If we just move the atom alone, we ruin
|
|---|
| 272 | /// all the other bonds. Hence, it would be sensible to move every atom found
|
|---|
| 273 | /// through the bond graph in the direction of the force as well by the same
|
|---|
| 274 | /// PositionUpdate. This is almost what we are going to do.
|
|---|
| 275 |
|
|---|
| 276 | /// One issue is: If we need to shorten bond, then we use the PositionUpdate
|
|---|
| 277 | /// also on the the other bond partner already. This is because it is in the
|
|---|
| 278 | /// direction of the bond. Therefore, the update is actually performed twice on
|
|---|
| 279 | /// each bond partner, i.e. the step size is twice as large as it should be.
|
|---|
| 280 | /// This problem only occurs when bonds need to be shortened, not when they
|
|---|
| 281 | /// need to be made longer (then the force vector is facing the other
|
|---|
| 282 | /// direction than the bond vector).
|
|---|
| 283 | /// As a remedy we need to average the force on either end of the bond and
|
|---|
| 284 | /// check whether each gradient points inwards out or outwards with respect
|
|---|
| 285 | /// to the bond and then shift accordingly.
|
|---|
| 286 |
|
|---|
| 287 | /// One more issue is that the projection onto the bond directions does not
|
|---|
| 288 | /// recover the gradient but may be larger as the bond directions are a
|
|---|
| 289 | /// generating system and not a basis (e.g. 3 bonds on a plane where 2 would
|
|---|
| 290 | /// suffice to span the plane). To this end, we need to account for the
|
|---|
| 291 | /// overestimation and obtain a weighting for each bond.
|
|---|
| 292 |
|
|---|
| 293 | // initialize helper class for bond vectors using bonds from range of atoms
|
|---|
| 294 | BondVectors bv;
|
|---|
| 295 | bv.setFromAtomRange< T >(
|
|---|
| 296 | AtomicForceManipulator<T>::atoms.begin(),
|
|---|
| 297 | AtomicForceManipulator<T>::atoms.end(),
|
|---|
| 298 | currentStep);
|
|---|
| 299 | const BondVectors::container_t &sorted_bonds = bv.getSorted();
|
|---|
| 300 |
|
|---|
| 301 | std::vector< // time step
|
|---|
| 302 | std::vector< // which bond side
|
|---|
| 303 | std::vector<double> > // over all bonds
|
|---|
| 304 | > projected_forces(2); // one for leftatoms, one for rightatoms (and for both time steps)
|
|---|
| 305 | for (size_t i=0;i<2;++i) {
|
|---|
| 306 | projected_forces[i].resize(MAX_sides);
|
|---|
| 307 | for (size_t j=0;j<MAX_sides;++j)
|
|---|
| 308 | projected_forces[i][j].resize(sorted_bonds.size(), 0.);
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | // for each atom we need to gather weights and then project the gradient
|
|---|
| 312 | typedef std::map<atomId_t, BondVectors::weights_t > weights_per_atom_t;
|
|---|
| 313 | std::vector<weights_per_atom_t> weights_per_atom(2);
|
|---|
| 314 | typedef std::map<atomId_t, Vector> RemnantGradient_per_atom_t;
|
|---|
| 315 | RemnantGradient_per_atom_t RemnantGradient_per_atom;
|
|---|
| 316 | for (size_t timestep = 0; timestep <= 1; ++timestep) {
|
|---|
| 317 | // TODO: We have this extra step in between because of DoOutput copying
|
|---|
| 318 | // change this making the code easier to understand
|
|---|
| 319 | const size_t CurrentStep = CurrentTimeStep-timestep >= 0 ? CurrentTimeStep - timestep : 0;
|
|---|
| 320 | LOG(2, "DEBUG: CurrentTimeStep is " << CurrentTimeStep
|
|---|
| 321 | << ", timestep is " << timestep
|
|---|
| 322 | << ", and CurrentStep is " << CurrentStep);
|
|---|
| 323 |
|
|---|
| 324 | for(typename AtomSetMixin<T>::const_iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
|---|
| 325 | iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
|---|
| 326 | const atom &walker = *(*iter);
|
|---|
| 327 | const Vector &walkerGradient = walker.getAtomicForceAtStep(CurrentStep);
|
|---|
| 328 | LOG(3, "DEBUG: Gradient of atom #" << walker.getId() << ", namely "
|
|---|
| 329 | << walker << " is " << walkerGradient);
|
|---|
| 330 |
|
|---|
| 331 | const BondList& ListOfBonds = walker.getListOfBonds();
|
|---|
| 332 | if (walkerGradient.Norm() > MYEPSILON) {
|
|---|
| 333 |
|
|---|
| 334 | // gather subset of BondVectors for the current atom
|
|---|
| 335 | const std::vector<Vector> BondVectors =
|
|---|
| 336 | bv.getAtomsBondVectorsAtStep(walker, CurrentStep);
|
|---|
| 337 |
|
|---|
| 338 | // go through all its bonds and calculate what magnitude is represented
|
|---|
| 339 | // by the others i.e. sum of scalar products against other bonds
|
|---|
| 340 | const std::pair<weights_per_atom_t::iterator, bool> inserter =
|
|---|
| 341 | weights_per_atom[timestep].insert(
|
|---|
| 342 | std::make_pair(walker.getId(),
|
|---|
| 343 | bv.getWeightsForAtomAtStep(walker, BondVectors, CurrentStep)) );
|
|---|
| 344 | ASSERT( inserter.second,
|
|---|
| 345 | "ForceAnnealing::operator() - weight map for atom "+toString(walker)
|
|---|
| 346 | +" and time step "+toString(timestep)+" already filled?");
|
|---|
| 347 | BondVectors::weights_t &weights = inserter.first->second;
|
|---|
| 348 | ASSERT( weights.size() == ListOfBonds.size(),
|
|---|
| 349 | "ForceAnnealing::operator() - number of weights "
|
|---|
| 350 | +toString(weights.size())+" does not match number of bonds "
|
|---|
| 351 | +toString(ListOfBonds.size())+", error in calculation?");
|
|---|
| 352 |
|
|---|
| 353 | // projected gradient over all bonds and place in one of projected_forces
|
|---|
| 354 | // using the obtained weights
|
|---|
| 355 | BondVectors::forcestore_t forcestoring =
|
|---|
| 356 | boost::bind(&ForceAnnealing::ForceStore, _1, _2, _3, _4,
|
|---|
| 357 | boost::cref(bv), boost::ref(projected_forces));
|
|---|
| 358 | const Vector RemnantGradient = bv.getRemnantGradientForAtomAtStep(
|
|---|
| 359 | walker, walkerGradient, BondVectors, weights, timestep, forcestoring
|
|---|
| 360 | );
|
|---|
| 361 | RemnantGradient_per_atom.insert( std::make_pair(walker.getId(), RemnantGradient) );
|
|---|
| 362 | } else {
|
|---|
| 363 | LOG(2, "DEBUG: Gradient is " << walkerGradient << " less than "
|
|---|
| 364 | << MYEPSILON << " for atom " << walker);
|
|---|
| 365 | // note that projected_forces is initialized to full length and filled
|
|---|
| 366 | // with zeros. Hence, nothing to do here
|
|---|
| 367 | }
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | // step through each bond and shift the atoms
|
|---|
| 372 | std::map<atomId_t, Vector> GatheredUpdates; //!< gathers all updates which are applied at the end
|
|---|
| 373 |
|
|---|
| 374 | LOG(3, "DEBUG: current step is " << currentStep << ", given time step is " << CurrentTimeStep);
|
|---|
| 375 | const BondVectors::mapped_t bondvectors = bv.getBondVectorsAtStep(currentStep);
|
|---|
| 376 |
|
|---|
| 377 | for (BondVectors::container_t::const_iterator bondsiter = sorted_bonds.begin();
|
|---|
| 378 | bondsiter != sorted_bonds.end(); ++bondsiter) {
|
|---|
| 379 | const bond::ptr ¤t_bond = *bondsiter;
|
|---|
| 380 | const size_t index = bv.getIndexForBond(current_bond);
|
|---|
| 381 | const atom* bondatom[MAX_sides] = {
|
|---|
| 382 | current_bond->leftatom,
|
|---|
| 383 | current_bond->rightatom
|
|---|
| 384 | };
|
|---|
| 385 |
|
|---|
| 386 | // remove the edge
|
|---|
| 387 | #ifndef NDEBUG
|
|---|
| 388 | const bool status =
|
|---|
| 389 | #endif
|
|---|
| 390 | BGcreator.removeEdge(bondatom[leftside]->getId(), bondatom[rightside]->getId());
|
|---|
| 391 | ASSERT( status, "ForceAnnealing() - edge to found bond is not present?");
|
|---|
| 392 |
|
|---|
| 393 | // gather nodes for either atom
|
|---|
| 394 | BoostGraphHelpers::Nodeset_t bondside_set[MAX_sides];
|
|---|
| 395 | BreadthFirstSearchGatherer::distance_map_t distance_map[MAX_sides];
|
|---|
| 396 | for (size_t side=leftside;side<MAX_sides;++side) {
|
|---|
| 397 | bondside_set[side] = NodeGatherer(bondatom[side]->getId(), max_distance);
|
|---|
| 398 | distance_map[side] = NodeGatherer.getDistances();
|
|---|
| 399 | std::sort(bondside_set[side].begin(), bondside_set[side].end());
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | // re-add edge
|
|---|
| 403 | BGcreator.addEdge(bondatom[leftside]->getId(), bondatom[rightside]->getId());
|
|---|
| 404 |
|
|---|
| 405 | // do for both leftatom and rightatom of bond
|
|---|
| 406 | for (size_t side = leftside; side < MAX_sides; ++side) {
|
|---|
| 407 | const double &bondforce = projected_forces[0][side][index];
|
|---|
| 408 | const double &oldbondforce = projected_forces[1][side][index];
|
|---|
| 409 | const double bondforcedifference = fabs(bondforce - oldbondforce);
|
|---|
| 410 | LOG(4, "DEBUG: bondforce for " << (side == leftside ? "left" : "right")
|
|---|
| 411 | << " side of bond is " << bondforce);
|
|---|
| 412 | LOG(4, "DEBUG: oldbondforce for " << (side == leftside ? "left" : "right")
|
|---|
| 413 | << " side of bond is " << oldbondforce);
|
|---|
| 414 | // if difference or bondforce itself is zero, do nothing
|
|---|
| 415 | if ((fabs(bondforce) < MYEPSILON) || (fabs(bondforcedifference) < MYEPSILON))
|
|---|
| 416 | continue;
|
|---|
| 417 |
|
|---|
| 418 | // get BondVector to bond
|
|---|
| 419 | const BondVectors::mapped_t::const_iterator bviter =
|
|---|
| 420 | bondvectors.find(current_bond);
|
|---|
| 421 | ASSERT( bviter != bondvectors.end(),
|
|---|
| 422 | "ForceAnnealing() - cannot find current_bond ?");
|
|---|
| 423 | ASSERT( fabs(bviter->second.Norm() -1.) < MYEPSILON,
|
|---|
| 424 | "ForceAnnealing() - norm of BondVector is not one");
|
|---|
| 425 | const Vector &BondVector = bviter->second;
|
|---|
| 426 |
|
|---|
| 427 | // calculate gradient and position differences for stepwidth
|
|---|
| 428 | const Vector currentGradient = bondforce * BondVector;
|
|---|
| 429 | LOG(4, "DEBUG: current projected gradient for "
|
|---|
| 430 | << (side == leftside ? "left" : "right") << " side of bond is " << currentGradient);
|
|---|
| 431 | const Vector &oldPosition = bondatom[side]->getPositionAtStep(CurrentTimeStep-2 >= 0 ? CurrentTimeStep - 2 : 0);
|
|---|
| 432 | const Vector ¤tPosition = bondatom[side]->getPositionAtStep(currentStep);
|
|---|
| 433 | const Vector PositionDifference = currentPosition - oldPosition;
|
|---|
| 434 | LOG(4, "DEBUG: old position is " << oldPosition);
|
|---|
| 435 | LOG(4, "DEBUG: current position is " << currentPosition);
|
|---|
| 436 | LOG(4, "DEBUG: difference in position is " << PositionDifference);
|
|---|
| 437 | LOG(4, "DEBUG: bondvector is " << BondVector);
|
|---|
| 438 | const double projected_PositionDifference = PositionDifference.ScalarProduct(BondVector);
|
|---|
| 439 | LOG(4, "DEBUG: difference in position projected onto bondvector is "
|
|---|
| 440 | << projected_PositionDifference);
|
|---|
| 441 | LOG(4, "DEBUG: abs. difference in forces is " << bondforcedifference);
|
|---|
| 442 |
|
|---|
| 443 | // calculate step width
|
|---|
| 444 | double stepwidth =
|
|---|
| 445 | fabs(projected_PositionDifference)/bondforcedifference;
|
|---|
| 446 | if (fabs(stepwidth) < 1e-10) {
|
|---|
| 447 | // dont' warn in first step, deltat usage normal
|
|---|
| 448 | if (currentStep != 1)
|
|---|
| 449 | ELOG(1, "INFO: Barzilai-Borwein stepwidth is zero, using deltat " << currentDeltat << " instead.");
|
|---|
| 450 | stepwidth = currentDeltat;
|
|---|
| 451 | }
|
|---|
| 452 | Vector PositionUpdate = stepwidth * currentGradient;
|
|---|
| 453 | LOG(3, "DEBUG: Update would be " << stepwidth << "*" << currentGradient << " = " << PositionUpdate);
|
|---|
| 454 |
|
|---|
| 455 | // add PositionUpdate for all nodes in the bondside_set
|
|---|
| 456 | for (BoostGraphHelpers::Nodeset_t::const_iterator setiter = bondside_set[side].begin();
|
|---|
| 457 | setiter != bondside_set[side].end(); ++setiter) {
|
|---|
| 458 | const BreadthFirstSearchGatherer::distance_map_t::const_iterator diter
|
|---|
| 459 | = distance_map[side].find(*setiter);
|
|---|
| 460 | ASSERT( diter != distance_map[side].end(),
|
|---|
| 461 | "ForceAnnealing() - could not find distance to an atom.");
|
|---|
| 462 | const double factor = pow(damping_factor, diter->second+1);
|
|---|
| 463 | LOG(3, "DEBUG: Update for atom #" << *setiter << " will be "
|
|---|
| 464 | << factor << "*" << PositionUpdate);
|
|---|
| 465 | if (GatheredUpdates.count((*setiter))) {
|
|---|
| 466 | GatheredUpdates[(*setiter)] += factor*PositionUpdate;
|
|---|
| 467 | } else {
|
|---|
| 468 | GatheredUpdates.insert(
|
|---|
| 469 | std::make_pair(
|
|---|
| 470 | (*setiter),
|
|---|
| 471 | factor*PositionUpdate) );
|
|---|
| 472 | }
|
|---|
| 473 | }
|
|---|
| 474 | }
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
|
|---|
| 478 | iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
|
|---|
| 479 | atom &walker = *(*iter);
|
|---|
| 480 | // extract largest components for showing progress of annealing
|
|---|
| 481 | const Vector currentGradient = walker.getAtomicForce();
|
|---|
| 482 | for(size_t i=0;i<NDIM;++i)
|
|---|
| 483 | if (currentGradient[i] > maxComponents[i])
|
|---|
| 484 | maxComponents[i] = currentGradient[i];
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | // apply the gathered updates and set remnant gradients for atomic annealing
|
|---|
| 488 | for (std::map<atomId_t, Vector>::const_iterator iter = GatheredUpdates.begin();
|
|---|
| 489 | iter != GatheredUpdates.end(); ++iter) {
|
|---|
| 490 | const atomId_t &atomid = iter->first;
|
|---|
| 491 | const Vector &update = iter->second;
|
|---|
| 492 | atom* const walker = World::getInstance().getAtom(AtomById(atomid));
|
|---|
| 493 | ASSERT( walker != NULL,
|
|---|
| 494 | "ForceAnnealing() - walker with id "+toString(atomid)+" has suddenly disappeared.");
|
|---|
| 495 | LOG(3, "DEBUG: Applying update " << update << " to atom #" << atomid
|
|---|
| 496 | << ", namely " << *walker);
|
|---|
| 497 | walker->setPosition( walker->getPosition() + update - CommonTranslation);
|
|---|
| 498 | // walker->setAtomicForce( RemnantGradient_per_atom[walker->getId()] );
|
|---|
| 499 | }
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | /** Reset function to unset static entities and artificial velocities.
|
|---|
| 503 | *
|
|---|
| 504 | */
|
|---|
| 505 | void reset()
|
|---|
| 506 | {
|
|---|
| 507 | currentDeltat = 0.;
|
|---|
| 508 | currentStep = 0;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | private:
|
|---|
| 512 | //!> contains the current step in relation to maxsteps
|
|---|
| 513 | static size_t currentStep;
|
|---|
| 514 | //!> contains the maximum number of steps, determines initial and final step with currentStep
|
|---|
| 515 | size_t maxSteps;
|
|---|
| 516 | static double currentDeltat;
|
|---|
| 517 | //!> minimum deltat for internal while loop (adaptive step width)
|
|---|
| 518 | static double MinimumDeltat;
|
|---|
| 519 | //!> contains the maximum bond graph distance up to which shifts of a single atom are spread
|
|---|
| 520 | const int max_distance;
|
|---|
| 521 | //!> the shifted is dampened by this factor with the power of the bond graph distance to the shift causing atom
|
|---|
| 522 | const double damping_factor;
|
|---|
| 523 | };
|
|---|
| 524 |
|
|---|
| 525 | template <class T>
|
|---|
| 526 | double ForceAnnealing<T>::currentDeltat = 0.;
|
|---|
| 527 | template <class T>
|
|---|
| 528 | size_t ForceAnnealing<T>::currentStep = 0;
|
|---|
| 529 | template <class T>
|
|---|
| 530 | double ForceAnnealing<T>::MinimumDeltat = 1e-8;
|
|---|
| 531 |
|
|---|
| 532 | #endif /* FORCEANNEALING_HPP_ */
|
|---|