Debian Codegraph @

include/BALL/MOLMEC/MINIMIZATION/energyMinimizer.h

raw symbols SCIP
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 // $Id: energyMinimizer.h,v 1.44.20.3 2007/08/07 09:12:33 aleru Exp $
5 //
6
7 // Energy Minimizer: A class for minimizing the energy of molecular systems
8
9 #ifndef BALL_MOLMEC_MINIMIZATION_ENERGYMINIMIZER_H
10 #define BALL_MOLMEC_MINIMIZATION_ENERGYMINIMIZER_H
11
12 #ifndef BALL_DATATYPE_OPTIONS_H
13 # include <BALL/DATATYPE/options.h>
14 #endif
15
16 #ifndef BALL_MOLMEC_COMMON_GRADIENT_H
17 # include <BALL/MOLMEC/COMMON/gradient.h>
18 #endif
19
20 namespace BALL
21 {
22 class ForceField;
23 class SnapShotManager;
24
25 /** EnergyMinimizer.
26 * Base class for all minimizer for geometry optimization.
27 * \ingroup MolmecEnergyMinimizer
28 */
29 class BALL_EXPORT EnergyMinimizer
30 {
31 public:
32
33 /** @name Option definitions
34 */
35 //@{
36
37 /** Option names
38 */
39 struct BALL_EXPORT Option
40 {
41 /** Max number of iterations
42 */
43 static const char* MAXIMAL_NUMBER_OF_ITERATIONS;
44
45 /** Energy output frequency
46 */
47 static const char* ENERGY_OUTPUT_FREQUENCY;
48
49 /** Trajectory ouput frequency
50 */
51 static const char* SNAPSHOT_FREQUENCY;
52
53 /** Number of iteration
54 */
55 static const char* NUMBER_OF_ITERATION;
56
57 /** Energy difference bound
58 */
59 static const char* ENERGY_DIFFERENCE_BOUND;
60
61 /** The number of iterations without any change in energy. This
62 * is used to detect convergence.
63 */
64 static const char* MAX_SAME_ENERGY;
65
66 /** The maximum RMS gradient allowed for convergence.
67 * If the current rms gradient is below this one, we are converged.
68 */
69 static const char* MAX_GRADIENT;
70
71 /** Max shift of an atom per iteration
72 */
73 static const char* MAXIMUM_DISPLACEMENT;
74 };
75
76 struct BALL_EXPORT Default
77 {
78 /** Max number of iterations
79 */
80 static Size MAXIMAL_NUMBER_OF_ITERATIONS;
81
82 /** Energy output frequency
83 */
84 static Size ENERGY_OUTPUT_FREQUENCY;
85
86 /** Snapshot frequency
87 */
88 static Size SNAPSHOT_FREQUENCY;
89
90 /** Number of iteration
91 */
92 static Size NUMBER_OF_ITERATION;
93
94 /** Energy difference bound.
95 * The energy difference needed for assuming 'equal energy'
96 */
97 static float ENERGY_DIFFERENCE_BOUND;
98
99 /** The number of iterations without any change in energy. This
100 * is used to detect convergence.
101 * If this number is reached, we assume convergence.
102 */
103 static Size MAX_SAME_ENERGY;
104
105 /** Default value for the maximum RMS gradient. This value is used for
106 * deciding when to stop the minimisation.
107 */
108 static float MAX_GRADIENT;
109
110 /** Max shift
111 */
112 static float MAXIMUM_DISPLACEMENT;
113 };
114
115 //@}
116 /** @name Constructors and Destructors
117 */
118 //@{
119
120 BALL_CREATE(EnergyMinimizer)
121
122 /** Default constructor.
123 */
124 EnergyMinimizer();
125
126 /** Constructor.
127 */
128 EnergyMinimizer(ForceField& force_field);
129
130 /** Constructor.
131 */
132 EnergyMinimizer(ForceField& force_field, const Options& options);
133
134 /** Copy constructor
135 */
136 EnergyMinimizer(const EnergyMinimizer& energy_minimizer);
137
138 /** Destructor.
139 */
140 virtual ~EnergyMinimizer();
141
142 //@}
143 /** @name Assignments
144 */
145 //@{
146
147 /** Assignment operator
148 */
149 const EnergyMinimizer& operator=(const EnergyMinimizer& energy_minimizer);
150
151 //@}
152
153 /** Equality operator
154 */
155 bool operator == (const EnergyMinimizer& energy_minimizer) ;
156
157 /** @name Debugging and Diagnostics
158 */
159 //@{
160
161 /** Is the energy minimizer valid : did the setup work?
162 */
163 bool isValid() const;
164
165 //@}
166 /** @name Setup methods
167 */
168 //@{
169
170 /** Sets up the energy minimizer.
171 */
172 bool setup(ForceField& force_field);
173
174 /** Sets up the energy minimizer.
175 */
176 bool setup(ForceField& force_field, SnapShotManager *ssm);
177
178 /** Sets up the energy minimizer.
179 */
180 bool setup(ForceField& force_field,
181 SnapShotManager *ssm, const Options& options);
182
183 /** Sets up the energy minimizer.
184 */
185 bool setup(ForceField& force_field, const Options& options);
186
187 /** Specific setup
188 */
189 virtual bool specificSetup();
190
191 //@}
192 /** @name Accessors
193 */
194 //@{
195
196 /** Implements the convergence criterion.
197 * If the convergence criterion is fulfilled, this method
198 * returns <b>true</b>. The convergence criterion is implemented as one
199 * of two conditions:
200 *
201 * (1) {RMS gradient} is below max_rms_gradient_
202 * (2) \link same_energy_counter_ same_energy_counter_ \endlink is above \link max_same_energy_ max_same_energy_ \endlink
203 *
204 * If any of these conditions hold \link isConverged isConverged \endlink returns <b>true</b>.
205 * This method should be reimplemented in derived classes for a different
206 * convergence criterion.
207 */
208 virtual bool isConverged() const;
209
210 /** Calculate the next step.
211 * This method is implemented in each minimizer class and
212 * tries to determine the next step to be taken.
213 * It typically performs a line search.
214 * The value returned is usually the step length with respect to the
215 * current direction.
216 * @return -1 to indicate failure
217 */
218 virtual double findStep();
219
220 /** Update the search direction.
221 * This method is implemented by the derived classes to implement a
222 * method to determine a new search direction.
223 */
224 virtual void updateDirection();
225
226 /** Update energy.
227 * This method calls <tt>force_field_->updateEnergy()</tt> and stores
228 * the result in <tt>current_energy_</tt>.
229 */
230 virtual double updateEnergy();
231
232 /** Update forces and store them in current_grad_.
233 * This method calls <tt>force_field_->updateForces()</tt> and stores them in
234 * \link current_grad_ current_grad_ \endlink .
235 */
236 virtual void updateForces();
237
238 /** Store the current energy and gradient.
239 * The current gradient and current energy is copied into
240 * initial energy and initial gradient. This is usually done at
241 * the start of an iteration.
242 */
243 void storeGradientEnergy();
244
245 /** Print the energy.
246 * This method is called by \link finishIteration finishIteration \endlink after every
247 * \link energy_output_frequency_ energy_output_frequency_ \endlink steps.
248 * It prints the current RMS gradient and the current energy to \link Log Log \endlink <tt>.info()</tt>.
249 * @see setEnergyOutputFrequency
250 * @see getEnergyOutputFrequency
251 */
252 virtual void printEnergy() const;
253
254 /** Take a snapshot of the system.
255 * This method is called by \link finishIteration finishIteration \endlink after every
256 * \link snapshot_frequency_ snapshot_frequency_ \endlink steps.
257 * It saves a <tt>SnapShot</tt> of the current atom coordinates to a \link SnapShotManager SnapShotManager \endlink
258 * (if enabled).
259 * @see setSnapShotFrequency
260 * @see getSnapShotFrequency
261 */
262 virtual void takeSnapShot() const;
263
264 /** Finishing step for this iteration.
265 * This method should be called at the end of the main iteration
266 * loop implemented in \link minimize minimize \endlink . It takes over some administrative stuff:
267 *
268 * - increment the iteration counter \link number_of_iterations_ number_of_iterations_ \endlink
269 * - call \link takeSnapShot takeSnapShot \endlink if necessary
270 * - call \link printEnergy printEnergy \endlink if necessary
271 * - call \link ForceField::update ForceField::update \endlink if necessary (to rebuild the pair lists!)
272 * - update the \link same_energy_counter_ same_energy_counter_ \endlink tested in \link isConverged isConverged \endlink
273 *
274 * This method should be overwritten only in rare cases. Even then, the programmer
275 * should make sure to call <tt>EnergyMinimizer::finishIteration</tt> or
276 * has to take care of the above items himself.
277 *
278 * All derived classes should call this method at the end of the minimize main loop.
279 * Otherwise strange things might happen.
280 * @see minimize
281 */
282 virtual void finishIteration();
283
284 /** Return the number of iterations performed.
285 */
286 Size getNumberOfIterations() const;
287
288 /** Return a reference to the current search direction
289 */
290 Gradient& getDirection();
291
292 /** Return a reference to the current gradient
293 */
294 Gradient& getGradient();
295
296 /** Return a reference to the initial gradient
297 */
298 Gradient& getInitialGradient();
299
300 /** Return the current energy
301 */
302 double getEnergy() const;
303
304 /** Return a reference to the current energy
305 */
306 double& getEnergy();
307
308 /** Return the initial energy
309 */
310 double getInitialEnergy() const;
311
312 /** Return a mutable reference to the initial energy
313 */
314 double& getInitialEnergy();
315
316 /** Set the number of iterations performed so far.
317 */
318 void setNumberOfIterations(Size number_of_iterations);
319
320 /** Get the maximum number of iterations
321 */
322 Size getMaxNumberOfIterations() const;
323
324 /** Set the maximum number of iterations
325 */
326 void setMaxNumberOfIterations(Size number_of_iterations);
327
328 /** Set the maximum number of iterations allowed with equal energy
329 * (second convergence criterion)
330 */
331 void setMaxSameEnergy(Size number);
332
333 /** Get the maximum number of iterations allowed with equal energy
334 * (second convergence criterion)
335 */
336 Size getMaxSameEnergy() const;
337
338 /** Set the energy output frequency
339 */
340 void setEnergyOutputFrequency(Size energy_output_frequency);
341
342 /** Get the energy ouput frequency
343 */
344 Size getEnergyOutputFrequency() const;
345
346 /** Set the energy difference bound for convergence
347 */
348 void setEnergyDifferenceBound(float energy_difference_bound);
349
350 /** Get the energy difference bound
351 */
352 float getEnergyDifferenceBound() const;
353
354 /** Set the maximum RMS gradient (first convergence criterion).
355 * The gradient unit of the gradient is <b>kJ/(mol \AA)</b>.
356 */
357 void setMaxGradient(float max_gradient);
358
359 /** Get the maximum RMS gradient (first convergence criterion).
360 * The gradient unit of the gradient is <b>kJ/(mol \AA)</b>.
361 */
362 float getMaxGradient() const;
363
364 /** Set the maximum displacement value.
365 * This is the maximum distance an atom may be moved by the minimizer in one iteration.
366 */
367 void setMaximumDisplacement(float maximum_displacement);
368
369 /** Get the maximum displacement value
370 */
371 float getMaximumDisplacement() const;
372
373 /** Set the snapshot frequency
374 */
375 void setSnapShotFrequency(Size snapshot_frequency);
376
377 /** Get the snapshot output frequency
378 */
379 Size getSnapShotFrequency() const;
380
381 /** Return the force field of the energy minimizer
382 */
383 ForceField* getForceField();
384
385 /** Return the number of force updates since the start of the minimization.
386 */
387 Size getForceUpdateCounter() const ;
388
389 /** Return the number of energy updates since the start of the minimization.
390 */
391 Size getEnergyUpdateCounter() const ;
392
393 /** Minimize the energy of the system bound to the force field.
394 * If a number of steps is given, the minimization is aborted after
395 * that number of steps, regardless of the number of steps given in
396 * the options (<tt>MAX_STEPS</tt>). Together with the <tt>resume</tt> option
397 * this feature is used to extract properties or visualize the results
398 * in the course of the minimization. If <tt>resume</tt> is set to <b>true</b>,
399 * the minimization continues with the former step width and settings.
400 * @param steps maximum number of steps to be taken
401 * @param resume <b>true</b> if the minimization is to be resumed with the previous settings
402 * @return bool - <b>true</b> if the minimization has converged
403 */
404 virtual bool minimize(Size steps = 0, bool resume = false);
405
406 /** Specify if the MDSimulation aborts if the Energy is greater than abort_energy_
407 */
408 void enableEnergyAbortCondition(bool state);
409
410 /// Query if the MDSimulation aborts if the Energy is greater than abort_energy_
411 bool energyAbortConditionEnabled() const;
412
413 /** Set the value for the energy, that will result in aborting the minization,
414 * if it will be surpassed.
415 * Default value: 10^9.
416 */
417 void setEnergyToAbort(float value);
418
419 ///
420 float getEnergyToAbort() const;
421
422 /** Return true, if the minimization was aborted, e.g. because of strange
423 * energies or gradient.
424 */
425 bool wasAborted() const ;
426
427 //@}
428 /** @name Public Attributes
429 */
430 //@{
431
432 /** Options
433 */
434 Options options;
435 //@}
436
437 protected:
438
439 /** @name Protected Attributes
440 */
441 //@{
442
443 /** The gradient at the beginning of the current minimization step.
444 */
445 Gradient initial_grad_;
446
447 /** The current gradient.
448 */
449 Gradient current_grad_;
450
451 /** The energy at the beginning of the current minimization step.
452 */
453 double initial_energy_;
454
455 /** The current energy.
456 */
457 double current_energy_;
458
459 /** The gradient from the last step
460 */
461 Gradient old_grad_;
462
463 /** The energy from the last step
464 */
465 double old_energy_;
466
467 /** The current search direction
468 */
469 Gradient direction_;
470
471 /** The boolean variable indicates if the setup of the energy minimizer was successful
472 */
473 bool valid_;
474
475 /** Pointer to a SnapShotManager for storing snapshots of the system
476 */
477 SnapShotManager* snapshot_;
478
479 /** The force field bound to the energy minimizer.
480 * Among other data the force field contains the molecular system
481 * whose energy will be minimized by the energy minimizer.
482 */
483 ForceField* force_field_;
484
485 /** The current iteration number
486 */
487 Size number_of_iterations_;
488
489 /** Maximum number of iterations
490 */
491 Size maximal_number_of_iterations_;
492
493 /** Frequency of energy output
494 */
495 Size energy_output_frequency_;
496
497 /** Frequency of atom coordinate ouput
498 */
499 Size snapshot_frequency_;
500
501 /** If the energy difference (before and after an iteration)
502 * is smaller than this bound, the minimization procedure stops.
503 */
504 double energy_difference_bound_;
505
506 /** The maximum RMS gradient tolerated (first convergence criterion)
507 */
508 double max_gradient_;
509
510 /** The maximum number of iterations with same energy.
511 * When this number is reached, we assume the system to have converged
512 * (second convergence criterion)
513 */
514 Size max_same_energy_;
515
516 /** A counter for the number of steps with a similar energy.
517 */
518 Size same_energy_counter_;
519
520 /** The maximal shift of an atom per iteration step (in Angstrom).
521 */
522 float maximum_displacement_;
523
524 /** Internal counter: how often is a force update done.
525 * Measure for the speed of minimization.
526 */
527 Size force_update_counter_;
528
529 /** Internal counter: how often is an energy update done.
530 * Measure for the speed of minimization.
531 */
532 Size energy_update_counter_;
533
534 /** Numerical lower bound: we don't want to compute the reciprocal
535 * of a number which is lower than 'cutlo_'.
536 */
537 float cutlo_;
538
539 /** The last step size (in respect of the length of the computed direction vector),
540 * so the length of the last step was \f$step\cdot|direction|\f$.
541 */
542 double step_;
543
544 //_
545 bool abort_by_energy_enabled_;
546
547 //_
548 float abort_energy_;
549
550 //_
551 bool aborted_;
552
553 //_@}
554 };
555
556 } // namespace Ball
557
558 #endif // BALL_MOLMEC_MINIMIZATION_ENERGYMINIMIZER_H