1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: strangLBFGS.C,v 1.1.4.7 2007/08/17 09:22:32 anhi Exp $
//
// Minimize the potential energy of a system using an improved version
// of the limited memory BFGS with Strang recurrences.
#include <BALL/MOLMEC/MINIMIZATION/strangLBFGS.h>
#include <BALL/MOLMEC/COMMON/forceField.h>
#include <limits>
//#define BALL_DEBUG
#undef BALL_DEBUG
// The default maximum number of stored vector pairs.
#define DEFAULT_NUMBER_OF_VECTOR_PAIRS 5
// The default "improved" flag
#define DEFAULT_IMPROVED true
namespace BALL
{
using namespace std;
const char* StrangLBFGSMinimizer::Option::IMPROVED = "improved";
const char* StrangLBFGSMinimizer::Option::NUM_OF_STORED_VECT_PAIRS = "num_of_stored_vect_pairs";
const bool StrangLBFGSMinimizer::Default::IMPROVED = DEFAULT_IMPROVED;
const Size StrangLBFGSMinimizer::Default::NUM_OF_STORED_VECT_PAIRS = DEFAULT_NUMBER_OF_VECTOR_PAIRS;
// Default constructor
StrangLBFGSMinimizer::StrangLBFGSMinimizer()
: EnergyMinimizer(),
line_search_(),
first_iter_(true),
number_of_atoms_(0),
max_num_of_vect_pairs_(DEFAULT_NUMBER_OF_VECTOR_PAIRS),
curr_num_of_vect_pairs_(0),
improved_(DEFAULT_IMPROVED),
rho_(),
stored_s_(),
stored_y_(),
work_val_(),
index_of_free_vect_(0),
initial_atoms_()
{
line_search_.setMinimizer(*this);
options.setDefaultBool(Option::IMPROVED, Default::IMPROVED);
options.setDefaultInteger(Option::NUM_OF_STORED_VECT_PAIRS, Default::NUM_OF_STORED_VECT_PAIRS);
}
// Constructor initialized with a force field
StrangLBFGSMinimizer::StrangLBFGSMinimizer(ForceField& force_field)
: EnergyMinimizer(),
line_search_(),
first_iter_(true),
number_of_atoms_(0),
max_num_of_vect_pairs_(DEFAULT_NUMBER_OF_VECTOR_PAIRS),
curr_num_of_vect_pairs_(0),
improved_(DEFAULT_IMPROVED),
rho_(),
stored_s_(),
stored_y_(),
work_val_(),
index_of_free_vect_(0),
initial_atoms_()
{
line_search_.setMinimizer(*this);
options.setDefaultBool(Option::IMPROVED, Default::IMPROVED);
options.setDefaultInteger(Option::NUM_OF_STORED_VECT_PAIRS, Default::NUM_OF_STORED_VECT_PAIRS);
valid_ = setup(force_field);
if (!valid_)
{
Log.error() << "StrangLBFGSMinimizer: setup failed! " << endl;
}
}
// Constructor initialized with a force field and a snapshot manager
StrangLBFGSMinimizer::StrangLBFGSMinimizer
(ForceField& force_field, SnapShotManager* ssm)
: EnergyMinimizer(),
line_search_(),
first_iter_(true),
number_of_atoms_(0),
max_num_of_vect_pairs_(DEFAULT_NUMBER_OF_VECTOR_PAIRS),
curr_num_of_vect_pairs_(0),
improved_(DEFAULT_IMPROVED),
rho_(),
stored_s_(),
stored_y_(),
work_val_(),
index_of_free_vect_(0),
initial_atoms_()
{
line_search_.setMinimizer(*this);
options.setDefaultBool(Option::IMPROVED, Default::IMPROVED);
options.setDefaultInteger(Option::NUM_OF_STORED_VECT_PAIRS, Default::NUM_OF_STORED_VECT_PAIRS);
valid_ = setup(force_field, ssm);
if (!valid_)
{
Log.error() << "StrangLBFGSMinimizer: setup failed! " << endl;
}
}
// Constructor initialized with a force field and a set of options
StrangLBFGSMinimizer::StrangLBFGSMinimizer
(ForceField& force_field, const Options& new_options)
: EnergyMinimizer(),
line_search_(),
first_iter_(true),
number_of_atoms_(0),
max_num_of_vect_pairs_(DEFAULT_NUMBER_OF_VECTOR_PAIRS),
curr_num_of_vect_pairs_(0),
improved_(DEFAULT_IMPROVED),
rho_(),
stored_s_(),
stored_y_(),
work_val_(),
index_of_free_vect_(0),
initial_atoms_()
{
line_search_.setMinimizer(*this);
options = new_options;
// The actual work is done in setup
valid_ = setup(force_field, new_options);
if (!valid_)
{
Log.error() << "StrangLBFGSMinimizer: setup failed! " << endl;
}
}
// Constructor initialized with a force field, a snapshot manager, and a set of options
StrangLBFGSMinimizer::StrangLBFGSMinimizer
(ForceField& force_field, SnapShotManager* ssm, const Options& new_options)
: EnergyMinimizer(),
line_search_(),
first_iter_(true),
number_of_atoms_(0),
max_num_of_vect_pairs_(DEFAULT_NUMBER_OF_VECTOR_PAIRS),
curr_num_of_vect_pairs_(0),
improved_(DEFAULT_IMPROVED),
rho_(),
stored_s_(),
stored_y_(),
work_val_(),
index_of_free_vect_(0),
initial_atoms_()
{
line_search_.setMinimizer(*this);
options = new_options;
// The actual work is done in setup
valid_ = setup(force_field, ssm, new_options);
if (!valid_)
{
Log.error() << "StrangLBFGSMinimizer: setup failed! " << endl;
}
}
// The destructor
StrangLBFGSMinimizer::~StrangLBFGSMinimizer()
{
}
// The copy constructor
StrangLBFGSMinimizer::StrangLBFGSMinimizer
(const StrangLBFGSMinimizer& rhs)
: EnergyMinimizer(rhs),
line_search_(rhs.line_search_),
first_iter_(rhs.first_iter_),
number_of_atoms_(rhs.number_of_atoms_),
max_num_of_vect_pairs_(rhs.max_num_of_vect_pairs_),
curr_num_of_vect_pairs_(rhs.curr_num_of_vect_pairs_),
improved_(rhs.improved_),
rho_(rhs.rho_),
stored_s_(rhs.stored_s_),
stored_y_(rhs.stored_y_),
work_val_(rhs.work_val_),
index_of_free_vect_(rhs.index_of_free_vect_),
initial_atoms_(rhs.initial_atoms_)
{
line_search_.setMinimizer(*this);
}
// The assignment operator
const StrangLBFGSMinimizer& StrangLBFGSMinimizer::operator =
(const StrangLBFGSMinimizer& rhs)
{
EnergyMinimizer::operator = (rhs);
line_search_ = rhs.line_search_;
first_iter_ = rhs.first_iter_;
number_of_atoms_ = rhs.number_of_atoms_;
max_num_of_vect_pairs_ = rhs.max_num_of_vect_pairs_;
curr_num_of_vect_pairs_ = rhs.curr_num_of_vect_pairs_;
improved_ = rhs.improved_;
rho_ = rhs.rho_;
stored_s_ = rhs.stored_s_;
stored_y_ = rhs.stored_y_;
work_val_ = rhs.work_val_;
index_of_free_vect_ = rhs.index_of_free_vect_;
initial_atoms_ = rhs.initial_atoms_;
line_search_.setMinimizer(*this);
return *this;
}
// This method is responsible for doing the specific setup of this class
bool StrangLBFGSMinimizer::specificSetup()
{
// Make sure the force field is assigned and valid!
if (force_field_ == 0 || !force_field_->isValid())
{
return false;
}
// Invalidate the initial gradient in order to ensure
// its re-evaluation at the start of minimize().
initial_grad_.invalidate();
// Get the options
max_num_of_vect_pairs_ = (Size)options.getInteger(Option::NUM_OF_STORED_VECT_PAIRS);
improved_ = options.getBool(Option::IMPROVED);
// Determine the number of atoms
number_of_atoms_ = force_field_->getNumberOfMovableAtoms();
// Start with the (initial) first iteration
first_iter_ = true;
return true;
}
// Set the maximum number of stored vector pairs
void StrangLBFGSMinimizer::setMaxNumOfStoredVectPairs(Size num)
{
max_num_of_vect_pairs_ = num;
options.setInteger(Option::NUM_OF_STORED_VECT_PAIRS, num);
}
// Return the maximum number of stored vector pairs
Size StrangLBFGSMinimizer::getMaxNumOfStoredVectPairs() const
{
return max_num_of_vect_pairs_;
}
// Set the improved flag
void StrangLBFGSMinimizer::setImprovedFlag(bool flag)
{
improved_ = flag;
options.setBool(Option::IMPROVED, flag);
}
// Return the improved flag
bool StrangLBFGSMinimizer::getImprovedFlag() const
{
return improved_;
}
void StrangLBFGSMinimizer::updateDirection()
{
// Define an alias for the atom vector
AtomVector& atoms(const_cast<AtomVector&>(getForceField()->getAtoms()));
if (!initial_grad_.isValid())
{
// Compute the initial energy and the initial forces
updateEnergy();
updateForces();
// Store them for later use
storeGradientEnergy();
}
if (first_iter_)
{
// This is the (initial) first iteration
// Save old positions
for(Size i = 0; i < number_of_atoms_; ++i)
{
initial_atoms_[i] = atoms[i]->getPosition();
}
// The first search direction is the normalized negative gradient
direction_ = initial_grad_;
direction_.negate();
direction_.normalize();
first_iter_ = false;
return;
}
// Just in case: check whether
// (1) (enough) memory is allocated for our search direction
// (2) the old gradient are invalid.
if ((direction_.size() == 0) || !old_grad_.isValid())
{
// This mustn't happen at all. If this routine is called all data(-structures) should be fine.
// But we are here! Something went wrong and we don't know what. Most kindly the data is
// modified by an external program/thread, so we do the best we can do: first we update the forces
// and set the direction to the negative gradient. Additionally, we force a restart.
Log.error() << "dir: " << direction_.isValid()
<< " initial_grad: " << initial_grad_.isValid()
<< " current_grad: " << current_grad_.isValid() << endl;
Log.error() << "StrangLBFGSMinimizer::updateDirection: invalid gradient or direction " << endl;
Log.error() << " cannot update stored vectors" << endl;
Log.error() << " cannot compute L-BFGS direction." << endl;
// Calculate the current gradient and determine the current
// direction as the direction of steepest descent.
updateForces();
direction_ = current_grad_;
direction_.negate();
direction_.normalize();
// Discard all data we have collected so far since we cannot trust in it any longer.
curr_num_of_vect_pairs_ = 0;
index_of_free_vect_ = 0;
first_iter_ = false;
return;
}
// First we update our stored vectors, so we're going to store an additional vector
// if we have space left, otherwise we replace the oldest vector pair by the new pair.
if (curr_num_of_vect_pairs_ < max_num_of_vect_pairs_)
{
++curr_num_of_vect_pairs_;
}
double sty = 0.;
double yty = 0.;
Size i;
Index k = number_of_atoms_*index_of_free_vect_;
// Main update loop. Do everything in one loop.
for(i = 0; i < number_of_atoms_; ++i, ++k)
{
// Compute the recent step in a way that reduces
// slightly rounding errors
stored_s_[k] = atoms[i]->getPosition() - initial_atoms_[i];
stored_y_[k] = initial_grad_[i] - old_grad_[i];
sty += stored_y_[k]*stored_s_[k];
yty += stored_y_[k]*stored_y_[k];
}
direction_ = initial_grad_;
direction_.negate();
// Compute the new scaling factor
double scale;
if (sty == 0.)
{
// We check for "exactly 0". If this happens something went wrong.
// We try to do our very best, which means we force the direction computation
// to be "possible".
rho_[index_of_free_vect_] = 1.;
scale = 1.;
}
else
{
rho_[index_of_free_vect_] = sty;
if ((sty > 0.) && (yty > cutlo_))
{
scale = sty/yty;
}
else
{
// This case should not happen due to our line search.
// Well, once more: we do our best...
scale = 1.;
}
}
// Now we are able to compute the search direction.
Size r;
Size j;
k = index_of_free_vect_+1;
if (improved_)
{
// First recurrence. Attention: we use an improved version. We use more up-to-date
// information for computing our initial hessian approximation. We use the techniques in
// Mehiddin Al-Baali, "Improved Hessian approximations for the limited memory BFGS method",
// Numerical Algorithms 22 (1999), 99-112
// with the newest correction pair. Therefore we have to set
// "i <= curr_num_of_vect_pairs_" instead of "i < curr_num_of_vect_pairs_".
for(i = 0; i <= curr_num_of_vect_pairs_; ++i)
{
--k;
if (k == -1)
{
k = curr_num_of_vect_pairs_-1;
}
double tmp = 0.;
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
tmp += stored_s_[j]*direction_[r];
}
tmp /= rho_[k];
work_val_[k] = tmp;
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
direction_[r] = -stored_y_[j]*tmp + direction_[r];
}
}
}
else
{
// First recurrence. Standard version
for(i = 0; i < curr_num_of_vect_pairs_; ++i)
{
--k;
if (k == -1)
{
k = curr_num_of_vect_pairs_-1;
}
double tmp = 0.;
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
tmp += stored_s_[j]*direction_[r];
}
tmp /= rho_[k];
work_val_[k] = tmp;
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
direction_[r] = -stored_y_[j]*tmp + direction_[r];
}
}
}
// Scaling
for(r = 0; r < number_of_atoms_; ++r)
{
direction_[r] *= scale;
}
if (improved_)
{
// Second recurrence. Attention: same case as above. We have to set
// "i <= curr_num_of_vect_pairs_" instead of "i < curr_num_of_vect_pairs_".
for(i = 0; i <= curr_num_of_vect_pairs_; ++i)
{
double tmp = 0.;
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
tmp += stored_y_[j]*direction_[r];
}
tmp = work_val_[k] - tmp/rho_[k];
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
direction_[r] = stored_s_[j]*tmp + direction_[r];
}
++k;
if (k == (Index) curr_num_of_vect_pairs_)
{
k = 0;
}
}
}
else
{
// Second recurrence, standard version
for(i = 0; i < curr_num_of_vect_pairs_; ++i)
{
double tmp = 0.;
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
tmp += stored_y_[j]*direction_[r];
}
tmp = work_val_[k] - tmp/rho_[k];
for(r = 0, j = k*number_of_atoms_; r < number_of_atoms_; ++r, ++j)
{
direction_[r] = stored_s_[j]*tmp + direction_[r];
}
++k;
if (k == (Index) curr_num_of_vect_pairs_)
{
k = 0;
}
}
}
// Compute norm and current directional derivative.
double norm = 0.0;
double dir_d = 0.0;
for (i = 0; i < number_of_atoms_; i++)
{
norm += direction_[i] * direction_[i];
dir_d += direction_[i] * initial_grad_[i];
}
norm = sqrt(norm);
if (dir_d > 0.)
{
// If the current search direction is NOT a descent direction
// something went wrong. We set the search direction to the
// normalized negative gradient and force a 'restart'.
direction_ = initial_grad_;
direction_.negate();
direction_.normalize();
curr_num_of_vect_pairs_ = 0;
index_of_free_vect_ = 0;
}
else
{
// Don't risc a "NaN"
if (norm >= cutlo_)
{
direction_.inv_norm = 1.0 / norm;
}
else
{
direction_.inv_norm = sqrt(std::numeric_limits<float>::max());
}
// Assign the norm and rms of the new direction
direction_.norm = norm;
direction_.rms = norm / (3.0 * (double)number_of_atoms_);
// Set the index for the next update
index_of_free_vect_ = (index_of_free_vect_+1) % max_num_of_vect_pairs_;
}
// Save old positions
for(Size i = 0; i < number_of_atoms_; ++i)
{
initial_atoms_[i] = atoms[i]->getPosition();
}
} // end of method 'updateDirection'
// The minimizer optimizes the energy of the system
// by using the limited memory BFGS method with Strang recurrence with improvement
// ideas of Al-Baali.
// Return value is true when no further steps can be taken!
bool StrangLBFGSMinimizer::minimize(Size iterations, bool resume)
{
aborted_ = false;
// Check for validity of minimizer and force field
if (!isValid() || getForceField() == 0 || !getForceField()->isValid())
{
Log.error() << "StrangLBFGSMinimizer::minimize: Minimizer is not initialized correctly!" << std::endl;
aborted_ = true;
return false;
}
// Check our number of the movable atoms...
Size noatoms = force_field_->getNumberOfMovableAtoms();
// (1) Make sure we have something worth moving.
if (noatoms == 0)
{
return true;
}
// (2) Check that outside of this minimizer the number of movable atoms didn't change
if (number_of_atoms_ != noatoms)
{
// The number of movable atoms has changed, so we must start from scratch
number_of_atoms_ = noatoms;
resume = false;
}
// (3) Check that outside of this minimizer the options didn't change
if ((Size)options.getInteger(Option::NUM_OF_STORED_VECT_PAIRS) != max_num_of_vect_pairs_)
{
max_num_of_vect_pairs_ = (Size)options.getInteger(Option::NUM_OF_STORED_VECT_PAIRS);
resume = false;
}
improved_ = options.getBool(Option::IMPROVED);
// Define an alias for the atom vector
AtomVector& atoms(const_cast<AtomVector&>(getForceField()->getAtoms()));
// If we start from scratch or the number of movable atoms has changed
// outside of this minimizer (i.e. no restart) we have to make sure
// to calculate all the quantities we need before we start and to allocate
// enough memory.
if (!resume)
{
// Reset the number of iterations if the job is not resumed.
setNumberOfIterations(0);
same_energy_counter_ = 0;
index_of_free_vect_ = 0;
curr_num_of_vect_pairs_ = 0;
current_grad_.invalidate();
initial_grad_.invalidate();
first_iter_ = true;
// Allocate our memory needed for the algorithm
rho_.resize(max_num_of_vect_pairs_);
stored_s_.resize(max_num_of_vect_pairs_*number_of_atoms_);
stored_y_.resize(max_num_of_vect_pairs_*number_of_atoms_);
work_val_.resize(max_num_of_vect_pairs_);
initial_atoms_.resize(number_of_atoms_);
// Obviously, we don't have "old" energies yet, so we initialize it a with
// sensible value. We don't need "old" gradients here.
old_energy_ = std::numeric_limits<float>::max();
}
Size max_iterations = std::min(getNumberOfIterations() + iterations, getMaxNumberOfIterations());
#ifdef BALL_DEBUG
Log.info() << "StrangLBFGSMinimizer: minimize(" << iterations << ", " << resume << ")" << endl;
#endif
// Save the current atom positions
atoms.savePositions();
bool converged = false;
// Iterate: while not converged and not enough iterations
while (!converged && (getNumberOfIterations() < max_iterations))
{
// Try to take a new step
double stp = findStep();
// Check whether we were successful.
if (stp > 0.0)
{
// Use this step as new reference step if findStep was successful
atoms.savePositions();
}
// Store the energy and gradient
old_energy_ = initial_energy_;
old_grad_ = initial_grad_;
// Store the current gradient and energy
storeGradientEnergy();
#ifdef BALL_DEBUG
Log.info() << "StrangLBFGSMinimizer::minimize: end of main: current grad RMS = " << current_grad_.rms << std::endl;
#endif
// Check for convergence.
converged = isConverged() || (stp == 0.);
// Increment iteration counter, take snapshots, print energy,
// update pair lists, and check the same-energy counter
finishIteration();
if ((!converged) && (stp < 0.))
{
// Nasty case: No convergence and the step computation failed.
// We must give up:-(
aborted_ = true;
return false;
}
if (Maths::isNan(force_field_->getEnergy()))
{
aborted_ = true;
return false;
}
if (Maths::isNan(getGradient().rbegin()->x) ||
Maths::isNan(getGradient().rbegin()->y) ||
Maths::isNan(getGradient().rbegin()->z))
{
aborted_ = true;
return false;
}
if (abort_by_energy_enabled_)
{
if (force_field_->getEnergy() > abort_energy_)
{
aborted_ = true;
return false;
}
}
}
return converged;
} // end of method 'minimize'
double StrangLBFGSMinimizer::findStep()
{
// Compute the new direction
updateDirection();
#ifdef BALL_DEBUG
Log.info() << "StrangLBFGSMinimizer::findStep: " << initial_energy_ << " " << current_grad_.norm << " " << direction_.norm << endl;
#endif
// Define an alias for the atom vector
AtomVector& atoms(const_cast<AtomVector&>(getForceField()->getAtoms()));
// We perform a line search along direction_
// No need to assure the maximum displacement here since our
// line search pays attention to this constraint.
bool result = line_search_.minimize(step_);
if (!result)
{
// Something went wrong.
// No success. We proceed with a restart.
// Set the search direction to the normalized negative gradient. Since we proceed
// with a restart, we mustn't update the stored vectors by 'updateDirection' and there is
// no need to compute anything by 'updateDirection'.
// Just in case: force field update (to update the pair list)
atoms.resetPositions();
force_field_->update();
// Compute the initial energy and the initial forces
initial_energy_ = force_field_->updateEnergy();
force_field_->updateForces();
initial_grad_.set(force_field_->getAtoms());
direction_ = initial_grad_;
direction_.negate();
direction_.normalize();
// We cannot trust in our data any more, so we force all routines to
// assume that we haven't collected any data so far.
curr_num_of_vect_pairs_ = 0;
index_of_free_vect_ = 0;
Size iter = 0;
while ((!result) && (iter < 12))
{
result = line_search_.minimize(step_);
if (!result)
{
for(Size i = 0; i < number_of_atoms_; ++i)
{
direction_[i] *= 0.5;
}
direction_.norm *= 0.5;
direction_.rms *= 0.5;
direction_.inv_norm *= 2.;
atoms.resetPositions();
}
else
{
return step_;
}
++iter;
}
// If we are here something went wrong.
// Not even such scaled steepest descent steps can manage
// the line search to exit successfully?
// We must be at a local minimizer...
step_ = 0.;
}
#ifdef BALL_DEBUG
Log.info() << "LineSearch: step = " << step_ << " result = " << result << endl;
#endif
return step_;
} // end of method 'findStep'
} // end of namespace BALL
|