source: src/Dynamics/ForceAnnealing.hpp@ 0682c2

Candidate_v1.6.1 ChemicalSpaceEvaluator Exclude_Hydrogens_annealWithBondGraph
Last change on this file since 0682c2 was 0682c2, checked in by Frederik Heber <frederik.heber@…>, 7 years ago

Excluded hydrogens due to their small mass from spreading updates.

  • hydrogens will rather adapt to position changes of other atoms than the other way round.
  • Property mode set to 100644
File size: 34.3 KB
Line 
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#include <math.h>
20
21#include <boost/bind.hpp>
22
23#include "Atom/atom.hpp"
24#include "Atom/AtomSet.hpp"
25#include "CodePatterns/Assert.hpp"
26#include "CodePatterns/Info.hpp"
27#include "CodePatterns/Log.hpp"
28#include "CodePatterns/Verbose.hpp"
29#include "Descriptors/AtomIdDescriptor.hpp"
30#include "Dynamics/AtomicForceManipulator.hpp"
31#include "Dynamics/BondVectors.hpp"
32#include "Fragmentation/ForceMatrix.hpp"
33#include "Graph/BoostGraphCreator.hpp"
34#include "Graph/BoostGraphHelpers.hpp"
35#include "Graph/BreadthFirstSearchGatherer.hpp"
36#include "Helpers/helpers.hpp"
37#include "Helpers/defs.hpp"
38#include "LinearAlgebra/LinearSystemOfEquations.hpp"
39#include "LinearAlgebra/MatrixContent.hpp"
40#include "LinearAlgebra/Vector.hpp"
41#include "LinearAlgebra/VectorContent.hpp"
42#include "Thermostats/ThermoStatContainer.hpp"
43#include "Thermostats/Thermostat.hpp"
44#include "World.hpp"
45
46/** This class is the essential build block for performing structural optimization.
47 *
48 * Sadly, we have to use some static instances as so far values cannot be passed
49 * between actions. Hence, we need to store the current step and the adaptive-
50 * step width (we cannot perform a line search, as we have no control over the
51 * calculation of the forces).
52 *
53 * However, we do use the bond graph, i.e. if a single atom needs to be shifted
54 * to the left, then the whole molecule left of it is shifted, too. This is
55 * controlled by the \a max_distance parameter.
56 */
57template <class T>
58class ForceAnnealing : public AtomicForceManipulator<T>
59{
60public:
61 /** Constructor of class ForceAnnealing.
62 *
63 * \note We use a fixed delta t of 1.
64 *
65 * \param _atoms set of atoms to integrate
66 * \param _Deltat time step width in atomic units
67 * \param _IsAngstroem whether length units are in angstroem or bohr radii
68 * \param _maxSteps number of optimization steps to perform
69 * \param _max_distance up to this bond order is bond graph taken into account.
70 */
71 ForceAnnealing(
72 AtomSetMixin<T> &_atoms,
73 const double _Deltat,
74 bool _IsAngstroem,
75 const size_t _maxSteps,
76 const int _max_distance,
77 const double _damping_factor) :
78 AtomicForceManipulator<T>(_atoms, _Deltat, _IsAngstroem),
79 maxSteps(_maxSteps),
80 max_distance(_max_distance),
81 damping_factor(_damping_factor),
82 FORCE_THRESHOLD(1e-8)
83 {}
84
85 /** Destructor of class ForceAnnealing.
86 *
87 */
88 ~ForceAnnealing()
89 {}
90
91 /** Performs Gradient optimization.
92 *
93 * We assume that forces have just been calculated.
94 *
95 *
96 * \param _TimeStep time step to update (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
97 * \param offset offset in matrix file to the first force component
98 * \return false - need to continue annealing, true - may stop because forces very small
99 * \todo This is not yet checked if it is correctly working with DoConstrainedMD set >0.
100 */
101 bool operator()(
102 const int _TimeStep,
103 const size_t _offset,
104 const bool _UseBondgraph)
105 {
106 const int CurrentTimeStep = _TimeStep-1;
107 ASSERT( CurrentTimeStep >= 0,
108 "ForceAnnealing::operator() - a new time step (upon which we work) must already have been copied.");
109
110 // make sum of forces equal zero
111 AtomicForceManipulator<T>::correctForceMatrixForFixedCenterOfMass(
112 _offset,
113 CurrentTimeStep);
114
115 // are we in initial step? Then set static entities
116 Vector maxComponents(zeroVec);
117 if (currentStep == 0) {
118 currentDeltat = AtomicForceManipulator<T>::Deltat;
119 currentStep = 1;
120 LOG(2, "DEBUG: Initial step, setting values, current step is #" << currentStep);
121
122 // always use atomic annealing on first step
123 maxComponents = anneal(_TimeStep);
124 } else {
125 ++currentStep;
126 LOG(2, "DEBUG: current step is #" << currentStep);
127
128 // bond graph annealing is always followed by a normal annealing
129 if (_UseBondgraph)
130 maxComponents = annealWithBondGraph_BarzilaiBorwein(_TimeStep);
131 // cannot store RemnantGradient in Atom's Force as it ruins BB stepwidth calculation
132 else
133 maxComponents = anneal_BarzilaiBorwein(_TimeStep);
134 }
135
136
137 LOG(1, "STATUS: Largest remaining force components at step #"
138 << currentStep << " are " << maxComponents);
139
140 // check whether are smaller than threshold
141 bool AnnealingFinished = false;
142 double maxcomp = 0.;
143 for (size_t i=0;i<NDIM;++i)
144 maxcomp = std::max(maxcomp, fabs(maxComponents[i]));
145 if (maxcomp < FORCE_THRESHOLD) {
146 LOG(1, "STATUS: Force components are all less than " << FORCE_THRESHOLD
147 << ", stopping.");
148 currentStep = maxSteps;
149 AnnealingFinished = true;
150 }
151
152 // are we in final step? Remember to reset static entities
153 if (currentStep == maxSteps) {
154 LOG(2, "DEBUG: Final step, resetting values");
155 reset();
156 }
157
158 return AnnealingFinished;
159 }
160
161 /** Helper function to calculate the Barzilai-Borwein stepwidth.
162 *
163 * \param _PositionDifference difference in position between current and last step
164 * \param _GradientDifference difference in gradient between current and last step
165 * \return step width according to Barzilai-Borwein
166 */
167 double getBarzilaiBorweinStepwidth(const Vector &_PositionDifference, const Vector &_GradientDifference)
168 {
169 double stepwidth = 0.;
170 if (_GradientDifference.Norm() > MYEPSILON)
171 stepwidth = fabs(_PositionDifference.ScalarProduct(_GradientDifference))/
172 _GradientDifference.NormSquared();
173 if (fabs(stepwidth) < 1e-10) {
174 // dont' warn in first step, deltat usage normal
175 if (currentStep != 1)
176 ELOG(1, "INFO: Barzilai-Borwein stepwidth is zero, using deltat " << currentDeltat << " instead.");
177 stepwidth = currentDeltat;
178 }
179 return std::min(1., stepwidth);
180 }
181
182 /** Performs Gradient optimization on the atoms.
183 *
184 * We assume that forces have just been calculated.
185 *
186 * \param _TimeStep time step to update (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
187 * \return to be filled with maximum force component over all atoms
188 */
189 Vector anneal(
190 const int _TimeStep)
191 {
192 const int CurrentTimeStep = _TimeStep-1;
193 ASSERT( CurrentTimeStep >= 0,
194 "ForceAnnealing::anneal() - a new time step (upon which we work) must already have been copied.");
195
196 LOG(1, "STATUS: performing simple anneal with default stepwidth " << currentDeltat << " at step #" << currentStep);
197
198 Vector maxComponents;
199 bool deltat_decreased = false;
200 for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
201 iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
202 // atom's force vector gives steepest descent direction
203 const Vector &currentPosition = (*iter)->getPositionAtStep(CurrentTimeStep);
204 const Vector &currentGradient = (*iter)->getAtomicForceAtStep(CurrentTimeStep);
205 LOG(4, "DEBUG: currentPosition for atom #" << (*iter)->getId() << " is " << currentPosition);
206 LOG(4, "DEBUG: currentGradient for atom #" << (*iter)->getId() << " is " << currentGradient);
207// LOG(4, "DEBUG: Force for atom " << **iter << " is " << currentGradient);
208
209 // we use Barzilai-Borwein update with position reversed to get descent
210 double stepwidth = currentDeltat;
211 Vector PositionUpdate = stepwidth * currentGradient;
212 LOG(3, "DEBUG: Update would be " << stepwidth << "*" << currentGradient << " = " << PositionUpdate);
213
214 // extract largest components for showing progress of annealing
215 for(size_t i=0;i<NDIM;++i)
216 maxComponents[i] = std::max(maxComponents[i], fabs(currentGradient[i]));
217
218 // steps may go back and forth again (updates are of same magnitude but
219 // have different sign: Check whether this is the case and one step with
220 // deltat to interrupt this sequence
221 if (currentStep > 1) {
222 const int OldTimeStep = CurrentTimeStep-1;
223 ASSERT( OldTimeStep >= 0,
224 "ForceAnnealing::anneal() - if currentStep is "+toString(currentStep)
225 +", then there should be at least three time steps.");
226 const Vector &oldPosition = (*iter)->getPositionAtStep(OldTimeStep);
227 const Vector PositionDifference = currentPosition - oldPosition;
228 LOG(4, "DEBUG: oldPosition for atom #" << (*iter)->getId() << " is " << oldPosition);
229 LOG(4, "DEBUG: PositionDifference for atom #" << (*iter)->getId() << " is " << PositionDifference);
230 if ((PositionUpdate.ScalarProduct(PositionDifference) < 0)
231 && (fabs(PositionUpdate.NormSquared()-PositionDifference.NormSquared()) < 1e-3)) {
232 // for convergence we want a null sequence here, too
233 if (!deltat_decreased) {
234 deltat_decreased = true;
235 currentDeltat = .5*currentDeltat;
236 }
237 LOG(2, "DEBUG: Upgrade in other direction: " << PositionUpdate
238 << " > " << PositionDifference
239 << ", using deltat: " << currentDeltat);
240 PositionUpdate = currentDeltat * currentGradient;
241 }
242 }
243
244 // finally set new values
245 (*iter)->setPositionAtStep(_TimeStep, currentPosition + PositionUpdate);
246 }
247
248 return maxComponents;
249 }
250
251 /** Performs Gradient optimization on a single atom using BarzilaiBorwein step width.
252 *
253 * \param _atom atom to anneal
254 * \param OldTimeStep old time step
255 * \param CurrentTimeStep current time step whose gradient we've just calculated
256 * \param TimeStepToSet time step to update (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
257 */
258 void annealAtom_BarzilaiBorwein(
259 atom * const _atom,
260 const int &OldTimeStep,
261 const int &CurrentTimeStep,
262 const int &TimeStepToSet
263 )
264 {
265 // atom's force vector gives steepest descent direction
266 const Vector &oldPosition = _atom->getPositionAtStep(OldTimeStep);
267 const Vector &currentPosition = _atom->getPositionAtStep(CurrentTimeStep);
268 const Vector &oldGradient = _atom->getAtomicForceAtStep(OldTimeStep);
269 const Vector &currentGradient = _atom->getAtomicForceAtStep(CurrentTimeStep);
270 LOG(4, "DEBUG: oldPosition for atom #" << _atom->getId() << " is " << oldPosition);
271 LOG(4, "DEBUG: currentPosition for atom #" << _atom->getId() << " is " << currentPosition);
272 LOG(4, "DEBUG: oldGradient for atom #" << _atom->getId() << " is " << oldGradient);
273 LOG(4, "DEBUG: currentGradient for atom #" << _atom->getId() << " is " << currentGradient);
274// LOG(4, "DEBUG: Force for atom #" << _atom->getId() << " is " << currentGradient);
275
276 // we use Barzilai-Borwein update with position reversed to get descent
277 const Vector PositionDifference = currentPosition - oldPosition;
278 const Vector GradientDifference = (currentGradient - oldGradient);
279 double stepwidth = getBarzilaiBorweinStepwidth(PositionDifference, GradientDifference);
280 Vector PositionUpdate = stepwidth * currentGradient;
281 LOG(3, "DEBUG: Update would be " << stepwidth << "*" << currentGradient << " = " << PositionUpdate);
282
283 // finally set new values
284 _atom->setPositionAtStep(TimeStepToSet, currentPosition + PositionUpdate);
285 }
286
287 /** Performs Gradient optimization on the atoms using BarzilaiBorwein step width.
288 *
289 * \note this can only be called when there are at least two optimization
290 * time steps present, i.e. this must be preceded by a simple anneal().
291 *
292 * We assume that forces have just been calculated.
293 *
294 * \param _TimeStep time step to update (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
295 * \return to be filled with maximum force component over all atoms
296 */
297 Vector anneal_BarzilaiBorwein(
298 const int _TimeStep)
299 {
300 const int OldTimeStep = _TimeStep-2;
301 const int CurrentTimeStep = _TimeStep-1;
302 ASSERT( OldTimeStep >= 0,
303 "ForceAnnealing::anneal_BarzilaiBorwein() - we need two present optimization steps to compute stepwidth.");
304 ASSERT(currentStep > 1,
305 "ForceAnnealing::anneal_BarzilaiBorwein() - we need two present optimization steps to compute stepwidth.");
306
307 LOG(1, "STATUS: performing BarzilaiBorwein anneal at step #" << currentStep);
308
309 Vector maxComponents;
310 for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
311 iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
312
313 annealAtom_BarzilaiBorwein(*iter, OldTimeStep, CurrentTimeStep, _TimeStep);
314
315 // extract largest components for showing progress of annealing
316 const Vector &currentGradient = (*iter)->getAtomicForceAtStep(CurrentTimeStep);
317 for(size_t i=0;i<NDIM;++i)
318 maxComponents[i] = std::max(maxComponents[i], fabs(currentGradient[i]));
319 }
320
321 return maxComponents;
322 }
323
324 /** Helper function to insert \a PositionUpdate into a map for every atom.
325 *
326 * \param _GatheredUpdates map of updates per atom
327 * \param _LargestUpdate_per_Atom map with the largest update per atom for checking
328 * \param _atomno key for map
329 * \param _PositionUpdate update to add
330 * \param _factor optional dampening factor
331 */
332 void updateInserter(
333 std::map<atomId_t, Vector> &_GatheredUpdates,
334 std::map<atomId_t, double> &_LargestUpdate_per_Atom,
335 const atomId_t _atomno,
336 const Vector &_PositionUpdate,
337 const double _factor = 1.
338 )
339 {
340 if (_GatheredUpdates.count(_atomno)) {
341 _GatheredUpdates[_atomno] += _factor*_PositionUpdate;
342 _LargestUpdate_per_Atom[_atomno] =
343 std::max(_LargestUpdate_per_Atom[_atomno], _factor*_PositionUpdate.Norm());
344 } else {
345 _GatheredUpdates.insert(
346 std::make_pair(
347 _atomno,
348 _factor*_PositionUpdate) );
349 _LargestUpdate_per_Atom.insert(
350 std::make_pair(
351 _atomno,
352 _PositionUpdate.Norm()) );
353 }
354 }
355
356 /** Performs Gradient optimization on the bonds with BarzilaiBorwein stepwdith.
357 *
358 * \note this can only be called when there are at least two optimization
359 * time steps present, i.e. this must be preceeded by a simple anneal().
360 *
361 * We assume that forces have just been calculated. These forces are projected
362 * onto the bonds and these are annealed subsequently by moving atoms in the
363 * bond neighborhood on either side conjunctively.
364 *
365 *
366 * \param _TimeStep time step to update (i.e. \f$ t + \Delta t \f$ in the sense of the velocity verlet)
367 * \param maxComponents to be filled with maximum force component over all atoms
368 */
369 Vector annealWithBondGraph_BarzilaiBorwein(
370 const int _TimeStep)
371 {
372 const int OldTimeStep = _TimeStep-2;
373 const int CurrentTimeStep = _TimeStep-1;
374 ASSERT(OldTimeStep >= 0,
375 "annealWithBondGraph_BarzilaiBorwein() - we need two present optimization steps to compute stepwidth, and the new one to update on already present.");
376 ASSERT(currentStep > 1,
377 "annealWithBondGraph_BarzilaiBorwein() - we need two present optimization steps to compute stepwidth.");
378
379 LOG(1, "STATUS: performing BarzilaiBorwein anneal on bonds at step #" << currentStep);
380
381 Vector maxComponents;
382
383 // get nodes on either side of selected bond via BFS discovery
384 BoostGraphCreator BGcreator;
385 BGcreator.createFromRange(
386 AtomicForceManipulator<T>::atoms.begin(),
387 AtomicForceManipulator<T>::atoms.end(),
388 AtomicForceManipulator<T>::atoms.size(),
389 BreadthFirstSearchGatherer::AlwaysTruePredicate);
390 BreadthFirstSearchGatherer NodeGatherer(BGcreator);
391
392 /** We assume that a force is local, i.e. a bond is too short yet and hence
393 * the atom needs to be moved. However, all the adjacent (bound) atoms might
394 * already be at the perfect distance. If we just move the atom alone, we ruin
395 * all the other bonds. Hence, it would be sensible to move every atom found
396 * through the bond graph in the direction of the force as well by the same
397 * PositionUpdate. This is almost what we are going to do, see below.
398 *
399 * This is to make the force a little more global in the sense of a multigrid
400 * solver that uses various coarser grids to transport errors more effectively
401 * over finely resolved grids.
402 *
403 */
404
405 /** The idea is that we project the gradients onto the bond vectors and determine
406 * from the sum of projected gradients from either side whether the bond is
407 * to contract or to expand. As the gradient acting as the normal vector of
408 * a plane supported at the position of the atom separates all bonds into two
409 * sets, we check whether all on one side are contracting and all on the other
410 * side are expanding. In this case we may move not only the atom itself but
411 * may propagate its update along a limited-horizon BFS to neighboring atoms.
412 *
413 */
414
415 // initialize helper class for bond vectors using bonds from range of atoms
416 BondVectors bv;
417 bv.setFromAtomRange< T >(
418 AtomicForceManipulator<T>::atoms.begin(),
419 AtomicForceManipulator<T>::atoms.end(),
420 _TimeStep); // use time step to update here as this is the current set of bonds
421
422 std::vector< // which bond side
423 std::vector<double> > // over all bonds
424 projected_forces; // one for leftatoms, one for rightatoms
425 projected_forces.resize(BondVectors::MAX_sides);
426 for (size_t j=0;j<BondVectors::MAX_sides;++j)
427 projected_forces[j].resize(bv.size(), 0.);
428
429 // for each atom we need to project the gradient
430 for(typename AtomSetMixin<T>::const_iterator iter = AtomicForceManipulator<T>::atoms.begin();
431 iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
432 const atom &walker = *(*iter);
433 const Vector &walkerGradient = walker.getAtomicForceAtStep(CurrentTimeStep);
434 const double GradientNorm = walkerGradient.Norm();
435 LOG(3, "DEBUG: Gradient of atom #" << walker.getId() << ", namely "
436 << walker << " is " << walkerGradient << " with magnitude of "
437 << GradientNorm);
438
439 if (GradientNorm > MYEPSILON) {
440 bv.getProjectedGradientsForAtomAtStep(
441 walker, walkerGradient, CurrentTimeStep, projected_forces
442 );
443 } else {
444 LOG(2, "DEBUG: Gradient is " << walkerGradient << " less than "
445 << MYEPSILON << " for atom " << walker);
446 // note that projected_forces is initialized to full length and filled
447 // with zeros. Hence, nothing to do here
448 }
449 }
450
451 std::map<atomId_t, Vector> GatheredUpdates; //!< gathers all updates which are applied at the end
452 std::map<atomId_t, double> LargestUpdate_per_Atom; //!< check whether updates cancelled each other
453 for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
454 iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
455 atom &walker = *(*iter);
456
457 /// calculate step width
458 const Vector &oldPosition = (*iter)->getPositionAtStep(OldTimeStep);
459 const Vector &currentPosition = (*iter)->getPositionAtStep(CurrentTimeStep);
460 const Vector &oldGradient = (*iter)->getAtomicForceAtStep(OldTimeStep);
461 const Vector &currentGradient = (*iter)->getAtomicForceAtStep(CurrentTimeStep);
462 LOG(4, "DEBUG: oldPosition for atom #" << (*iter)->getId() << " is " << oldPosition);
463 LOG(4, "DEBUG: currentPosition for atom #" << (*iter)->getId() << " is " << currentPosition);
464 LOG(4, "DEBUG: oldGradient for atom #" << (*iter)->getId() << " is " << oldGradient);
465 LOG(4, "DEBUG: currentGradient for atom #" << (*iter)->getId() << " is " << currentGradient);
466// LOG(4, "DEBUG: Force for atom #" << (*iter)->getId() << " is " << currentGradient);
467
468 // we use Barzilai-Borwein update with position reversed to get descent
469 const Vector PositionDifference = currentPosition - oldPosition;
470 const Vector GradientDifference = (currentGradient - oldGradient);
471 double stepwidth = getBarzilaiBorweinStepwidth(PositionDifference, GradientDifference);
472 Vector PositionUpdate = stepwidth * currentGradient;
473 // cap updates (if non-zero) at 0.2 angstroem. BB tends to overshoot.
474 for (size_t i=0;i<NDIM;++i)
475 if (fabs(PositionUpdate[i]) > MYEPSILON)
476 PositionUpdate[i] = std::min(0.2, fabs(PositionUpdate[i]))*PositionUpdate[i]/fabs(PositionUpdate[i]);
477 LOG(3, "DEBUG: Update would be " << stepwidth << "*" << currentGradient << " = " << PositionUpdate);
478
479 if (walker.getElementNo() != 1) {
480 /** for each atom, we imagine a plane at the position of the atom with
481 * its atomic gradient as the normal vector. We go through all its bonds
482 * and check on which side of the plane the bond is. This defines whether
483 * the bond is contracting (+) or expanding (-) with respect to this atom.
484 *
485 * A bond has two atoms, however. Hence, we do this for either atom and
486 * look at the combination: Is it in sum contracting or expanding given
487 * both projected_forces?
488 */
489
490 /** go through all bonds and check projected_forces and side of plane
491 * the idea is that if all bonds on one side are contracting ones or expanding,
492 * respectively, then we may shift not only the atom with respect to its
493 * gradient but also its neighbors (towards contraction or towards
494 * expansion depending on direction of gradient).
495 * if they are mixed on both sides of the plane, then we simply shift
496 * only the atom itself.
497 * if they are not mixed on either side, then we also only shift the
498 * atom, namely away from expanding and towards contracting bonds.
499 *
500 * We may get this information right away by looking at the projected_forces.
501 * They give the atomic gradient of either atom projected onto the BondVector
502 * with an additional weight in [0,1].
503 */
504
505 // sign encodes side of plane and also encodes contracting(-) or expanding(+)
506 typedef std::vector<int> sides_t;
507 typedef std::vector<int> types_t;
508 sides_t sides;
509 types_t types;
510 const BondList& ListOfBonds = walker.getListOfBonds();
511 for(BondList::const_iterator bonditer = ListOfBonds.begin();
512 bonditer != ListOfBonds.end(); ++bonditer) {
513 const bond::ptr &current_bond = *bonditer;
514
515 // BondVector goes from bond::rightatom to bond::leftatom
516 const size_t index = bv.getIndexForBond(current_bond);
517 std::vector<double> &forcelist = (&walker == current_bond->leftatom) ?
518 projected_forces[BondVectors::leftside] : projected_forces[BondVectors::rightside];
519 // note that projected_forces has sign such as to indicate whether
520 // atomic gradient wants bond to contract (-) or expand (+).
521 // This goes into sides: Minus side points away from gradient, plus side point
522 // towards gradient.
523 //
524 // the sum of both bond sides goes into types, depending on which is
525 // stronger if either wants a different thing
526 const double &temp = forcelist[index];
527 if (fabs(temp) < MYEPSILON)
528 sides.push_back(1);
529 else
530 sides.push_back( -1.*temp/fabs(temp) ); // BondVectors has exactly opposite sign for sides decision
531 ASSERT( (sides.back() == 1) || (sides.back() == -1),
532 "ForceAnnealing() - sides is not in {-1,1}.");
533 const double sum =
534 projected_forces[BondVectors::leftside][index]+projected_forces[BondVectors::rightside][index];
535 types.push_back( sum/fabs(sum) );
536 LOG(4, "DEBUG: Bond " << *current_bond << " is on side " << sides.back()
537 << " and has type " << types.back());
538 }
539 // /// check whether both conditions are compatible:
540 // // i.e. either we have ++/-- for all entries in sides and types
541 // // or we have +-/-+ for all entries
542 // // hence, multiplying and taking the sum and its absolute value
543 // // should be equal to the maximum number of entries
544 // sides_t results;
545 // std::transform(
546 // sides.begin(), sides.end(),
547 // types.begin(),
548 // std::back_inserter(results),
549 // std::multiplies<int>);
550 // int result = abs(std::accumulate(results.begin(), results.end(), 0, std::plus<int>));
551
552 std::vector<size_t> first_per_side(2, (size_t)-1); //!< mark down one representative from either side
553 std::vector< std::vector<int> > types_per_side(2); //!< gather all types on each side
554 types_t::const_iterator typesiter = types.begin();
555 for (sides_t::const_iterator sidesiter = sides.begin();
556 sidesiter != sides.end(); ++sidesiter, ++typesiter) {
557 const size_t index = (*sidesiter+1)/2;
558 types_per_side[index].push_back(*typesiter);
559 if (first_per_side[index] == (size_t)-1)
560 first_per_side[index] = std::distance(const_cast<const sides_t &>(sides).begin(), sidesiter);
561 }
562 LOG(4, "DEBUG: First on side minus is " << first_per_side[0] << ", and first on side plus is "
563 << first_per_side[1]);
564 //!> enumerate types per side with a little witching with the numbers to allow easy setting from types
565 enum whichtypes_t {
566 contracting=0,
567 unset=1,
568 expanding=2,
569 mixed
570 };
571 std::vector<int> typeside(2, unset);
572 for(size_t i=0;i<2;++i) {
573 for (std::vector<int>::const_iterator tpsiter = types_per_side[i].begin();
574 tpsiter != types_per_side[i].end(); ++tpsiter) {
575 if (typeside[i] == unset) {
576 typeside[i] = *tpsiter+1; //contracting(0) or expanding(2)
577 } else {
578 if (typeside[i] != (*tpsiter+1)) // no longer he same type
579 typeside[i] = mixed;
580 }
581 }
582 }
583 LOG(4, "DEBUG: Minus side is " << typeside[0] << " and plus side is " << typeside[1]);
584
585 typedef std::vector< std::pair<atomId_t, atomId_t> > RemovedEdges_t;
586 if ((typeside[0] != mixed) || (typeside[1] != mixed)) {
587 const size_t sideno = ((typeside[0] != mixed) && (typeside[0] != unset)) ? 0 : 1;
588 LOG(4, "DEBUG: Chosen side is " << sideno << " with type " << typeside[sideno]);
589 ASSERT( (typeside[sideno] == contracting) || (typeside[sideno] == expanding),
590 "annealWithBondGraph_BB() - chosen side is neither expanding nor contracting.");
591 // one side is not mixed, all bonds on one side are of same type
592 // hence, find out which bonds to exclude
593 const BondList& ListOfBonds = walker.getListOfBonds();
594
595 // sideno is away (0) or in direction (1) of gradient
596 // tpyes[first_per_side[sideno]] is either contracting (-1) or expanding (+1)
597 // : side (i), where (i) means which bonds we keep for the BFS, bonds
598 // on side (-i) are removed
599 // If all bonds on side away (0) want expansion (+1), move towards side with atom: side 1
600 // if all bonds side towards (1) want contraction (-1), move away side with atom : side -1
601
602 // unsure whether this or do nothing in the remaining cases:
603 // If all bonds on side toward (1) want expansion (+1), move away side with atom : side -1
604 // (the reasoning is that the bond's other atom must have a stronger
605 // gradient in the same direction and they push along atoms in
606 // gradient direction: we don't want to interface with those.
607 // Hence, move atoms along on away side
608 // if all bonds side away (0) want contraction (-1), move towards side with atom: side 1
609 // (the reasoning is the same, don't interfere with update from
610 // stronger gradient)
611 // hence, the decision is only based on sides once we have picked a side
612 // depending on all bonds associated with have same good type.
613
614 // away from gradient (minus) and contracting
615 // or towards gradient (plus) and expanding
616 // gather all on same side and remove
617 const double sign =
618 (sides[first_per_side[sideno]] == types[first_per_side[sideno]])
619 ? sides[first_per_side[sideno]] : -1.*sides[first_per_side[sideno]];
620
621 LOG(4, "DEBUG: Removing edges from side with sign " << sign);
622 BondList::const_iterator bonditer = ListOfBonds.begin();
623 RemovedEdges_t RemovedEdges;
624 for (sides_t::const_iterator sidesiter = sides.begin();
625 sidesiter != sides.end(); ++sidesiter, ++bonditer) {
626 if (*sidesiter == sign) {
627 // remove the edge
628 const bond::ptr &current_bond = *bonditer;
629 LOG(5, "DEBUG: Removing edge " << *current_bond);
630 RemovedEdges.push_back( std::make_pair(
631 current_bond->leftatom->getId(),
632 current_bond->rightatom->getId())
633 );
634 #ifndef NDEBUG
635 const bool status =
636 #endif
637 BGcreator.removeEdge(RemovedEdges.back());
638 ASSERT( status, "ForceAnnealing() - edge to found bond is not present?");
639 }
640 }
641 // perform limited-horizon BFS
642 BoostGraphHelpers::Nodeset_t bondside_set;
643 BreadthFirstSearchGatherer::distance_map_t distance_map;
644 bondside_set = NodeGatherer(walker.getId(), max_distance);
645 distance_map = NodeGatherer.getDistances();
646 std::sort(bondside_set.begin(), bondside_set.end());
647
648 // re-add edge
649 for (RemovedEdges_t::const_iterator edgeiter = RemovedEdges.begin();
650 edgeiter != RemovedEdges.end(); ++edgeiter)
651 BGcreator.addEdge(edgeiter->first, edgeiter->second);
652
653 // update position with dampening factor on the discovered bonds
654 for (BoostGraphHelpers::Nodeset_t::const_iterator setiter = bondside_set.begin();
655 setiter != bondside_set.end(); ++setiter) {
656 const BreadthFirstSearchGatherer::distance_map_t::const_iterator diter
657 = distance_map.find(*setiter);
658 ASSERT( diter != distance_map.end(),
659 "ForceAnnealing() - could not find distance to an atom.");
660 const double factor = pow(damping_factor, diter->second+1);
661 LOG(3, "DEBUG: Update for atom #" << *setiter << " will be "
662 << factor << "*" << PositionUpdate);
663 updateInserter(GatheredUpdates, LargestUpdate_per_Atom, *setiter, PositionUpdate, factor);
664 }
665 } else {
666 // simple atomic annealing, i.e. damping factor of 1
667 updateInserter(GatheredUpdates, LargestUpdate_per_Atom, walker.getId(), PositionUpdate);
668 }
669 } else {
670 // hydrogens (are light-weighted and therefore) are always updated normally
671 LOG(3, "DEBUG: Update for hydrogen #" << walker.getId() << " will be " << PositionUpdate);
672 updateInserter(GatheredUpdates, LargestUpdate_per_Atom, walker.getId(), PositionUpdate);
673 }
674 }
675
676 for(typename AtomSetMixin<T>::iterator iter = AtomicForceManipulator<T>::atoms.begin();
677 iter != AtomicForceManipulator<T>::atoms.end(); ++iter) {
678 atom &walker = *(*iter);
679 // extract largest components for showing progress of annealing
680 const Vector &currentGradient = walker.getAtomicForceAtStep(CurrentTimeStep);
681 for(size_t i=0;i<NDIM;++i)
682 maxComponents[i] = std::max(maxComponents[i], fabs(currentGradient[i]));
683 }
684
685// // remove center of weight translation from gathered updates
686// Vector CommonTranslation;
687// for (std::map<atomId_t, Vector>::const_iterator iter = GatheredUpdates.begin();
688// iter != GatheredUpdates.end(); ++iter) {
689// const Vector &update = iter->second;
690// CommonTranslation += update;
691// }
692// CommonTranslation *= 1./(double)GatheredUpdates.size();
693// LOG(3, "DEBUG: Subtracting common translation " << CommonTranslation
694// << " from all updates.");
695
696 // apply the gathered updates and set remnant gradients for atomic annealing
697 Vector LargestUpdate;
698 for (std::map<atomId_t, Vector>::const_iterator iter = GatheredUpdates.begin();
699 iter != GatheredUpdates.end(); ++iter) {
700 const atomId_t &atomid = iter->first;
701 const Vector &update = iter->second;
702 atom* const walker = World::getInstance().getAtom(AtomById(atomid));
703 ASSERT( walker != NULL,
704 "ForceAnnealing() - walker with id "+toString(atomid)+" has suddenly disappeared.");
705 LOG(3, "DEBUG: Applying update " << update << " to atom #" << atomid
706 << ", namely " << *walker);
707 for (size_t i=0;i<NDIM;++i)
708 LargestUpdate[i] = std::max(LargestUpdate[i], fabs(update[i]));
709
710 std::map<atomId_t, double>::const_iterator largestiter = LargestUpdate_per_Atom.find(atomid);
711 ASSERT( largestiter != LargestUpdate_per_Atom.end(),
712 "ForceAnnealing() - walker with id "+toString(atomid)+" not in LargestUpdates.");
713 // if we had large updates but their sum is very small
714 if (update.Norm()/largestiter->second > MYEPSILON) {
715 walker->setPositionAtStep(_TimeStep,
716 walker->getPositionAtStep(CurrentTimeStep) + update); // - CommonTranslation);
717 } else {
718 // then recalc update with simple anneal
719 LOG(2, "WARNING: Updates on atom " << *iter << " cancel themselves, performing simple anneal step.");
720 annealAtom_BarzilaiBorwein(walker, OldTimeStep, CurrentTimeStep, _TimeStep);
721 }
722 }
723 LOG(1, "STATUS: Largest absolute update components are " << LargestUpdate);
724
725 return maxComponents;
726 }
727
728 /** Reset function to unset static entities and artificial velocities.
729 *
730 */
731 void reset()
732 {
733 currentDeltat = 0.;
734 currentStep = 0;
735 }
736
737private:
738 //!> contains the current step in relation to maxsteps
739 static size_t currentStep;
740 //!> contains the maximum number of steps, determines initial and final step with currentStep
741 size_t maxSteps;
742 static double currentDeltat;
743 //!> minimum deltat for internal while loop (adaptive step width)
744 static double MinimumDeltat;
745 //!> contains the maximum bond graph distance up to which shifts of a single atom are spread
746 const int max_distance;
747 //!> the shifted is dampened by this factor with the power of the bond graph distance to the shift causing atom
748 const double damping_factor;
749 //!> threshold for force components to stop annealing
750 const double FORCE_THRESHOLD;
751};
752
753template <class T>
754double ForceAnnealing<T>::currentDeltat = 0.;
755template <class T>
756size_t ForceAnnealing<T>::currentStep = 0;
757template <class T>
758double ForceAnnealing<T>::MinimumDeltat = 1e-8;
759
760#endif /* FORCEANNEALING_HPP_ */
Note: See TracBrowser for help on using the repository browser.