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
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: forceField.C,v 1.40.26.4 2007/05/18 12:06:00 oliver Exp $
//
#include <BALL/MOLMEC/COMMON/forceField.h>
#include <BALL/MOLMEC/COMMON/forceFieldComponent.h>
#include <BALL/MOLMEC/COMMON/periodicBoundary.h>
#include <BALL/KERNEL/bond.h>
#include <BALL/KERNEL/forEach.h>
#include <limits>
using namespace std;
namespace BALL
{
// default constructor
ForceField::ForceField()
: options(),
periodic_boundary(*this),
system_(0),
atoms_(),
parameters_(),
valid_(false),
name_("Force Field"),
energy_(0.0),
number_of_movable_atoms_(0),
use_selection_(false),
selection_enabled_(true),
update_time_stamp_(),
setup_time_stamp_(),
unassigned_atoms_(),
max_number_of_errors_(std::numeric_limits<Size>::max()),
number_of_errors_(0)
{
}
void ForceField::clear()
{
options.clear();
periodic_boundary.clear();
name_ = "Force Field";
energy_ = 0.0;
system_ = 0;
atoms_.clear();
number_of_movable_atoms_ = 0;
parameters_.clear();
use_selection_ = false;
selection_enabled_ = true;
valid_ = true;
update_time_stamp_.clear();
setup_time_stamp_.clear();
// remove all force field components from the list after
// calling their destructors
vector<ForceFieldComponent*>::iterator it;
for (it = components_.begin(); it != components_.end(); ++it)
{
delete (*it);
}
components_.clear();
unassigned_atoms_.clear();
max_number_of_errors_= std::numeric_limits<Size>::max();
number_of_errors_ = 0;
}
// copy constructor
ForceField::ForceField(const ForceField& force_field)
: options(force_field.options),
periodic_boundary(force_field.periodic_boundary),
system_(force_field.system_),
atoms_(force_field.atoms_),
parameters_(force_field.parameters_),
valid_(force_field.valid_),
name_(force_field.name_),
energy_(force_field.energy_),
number_of_movable_atoms_(force_field.number_of_movable_atoms_),
use_selection_(force_field.use_selection_),
selection_enabled_(force_field.selection_enabled_),
update_time_stamp_(force_field.update_time_stamp_),
setup_time_stamp_(force_field.setup_time_stamp_),
max_number_of_errors_(force_field.max_number_of_errors_),
number_of_errors_(0)
{
// Copy the component vector and its components.
for (Size i = 0; i < force_field.components_.size(); i++)
{
components_.push_back((ForceFieldComponent*)force_field.components_[i]->create());
}
}
// assignment operator
ForceField& ForceField::operator = (const ForceField& force_field)
{
// guard against self assignment
if (&force_field != this)
{
atoms_.clear();
atoms_ = force_field.atoms_;
number_of_movable_atoms_ = force_field.number_of_movable_atoms_;
name_ = force_field.name_;
energy_ = force_field.energy_;
options = force_field.options;
system_ = force_field.system_;
parameters_ = force_field.parameters_;
periodic_boundary = force_field.periodic_boundary;
use_selection_ = force_field.use_selection_;
selection_enabled_ = force_field.selection_enabled_;
valid_ = force_field.valid_;
max_number_of_errors_= force_field.max_number_of_errors_;
number_of_errors_ = 0;
Size i;
for (i = 0; i < components_.size(); i++)
{
delete components_[i];
}
components_.clear();
for (i = 0; i < force_field.components_.size(); i++)
{
components_.push_back((ForceFieldComponent*)force_field.components_[i]->create());
}
}
return (*this);
}
// Constructor initialized with a system
ForceField::ForceField(System& system)
: options(),
periodic_boundary(*this),
system_(0),
atoms_(),
parameters_(),
valid_(false),
name_("Force Field"),
energy_(0.0),
number_of_movable_atoms_(0),
use_selection_(false),
selection_enabled_(true),
update_time_stamp_(),
setup_time_stamp_(),
unassigned_atoms_(),
max_number_of_errors_(std::numeric_limits<Size>::max()),
number_of_errors_(0)
{
bool result = setup(system);
if (!result)
{
Log.error() << "Force Field setup failed! " << endl;
valid_ = false;
}
}
// Constructor intialized with a system and a set of options
ForceField::ForceField(System& system, const Options& new_options)
: options(),
periodic_boundary(*this),
system_(0),
atoms_(),
parameters_(),
valid_(false),
name_("Force Field"),
energy_(0.0),
number_of_movable_atoms_(0),
use_selection_(false),
selection_enabled_(true),
update_time_stamp_(),
setup_time_stamp_(),
unassigned_atoms_(),
max_number_of_errors_(std::numeric_limits<Size>::max()),
number_of_errors_(0)
{
bool result = setup(system, new_options);
if (!result)
{
Log.error() << "Force Field setup failed! " << endl;
valid_ = false;
}
}
// destructor
ForceField::~ForceField()
{
clear();
valid_ = false;
}
// Is the force field valid
bool ForceField::isValid() const
{
return valid_;
}
// setup methods
bool ForceField::setup(System& system)
{
unassigned_atoms_.clear();
number_of_errors_ = 0;
// store the specified system
system_ = &system;
// Setup periodic boundary
if (!periodic_boundary.setup())
{
Log.error() << "setup of periodic boundary failed" << endl;
return false;
}
// Update the use_selection_ flag.
use_selection_ = (selection_enabled_ && system_->containsSelection());
// collect the atoms of the system in the atoms_vector_
collectAtoms_(system);
Size old_size = (Size)atoms_.size();
// generate the vector of molecules if periodic boundary is enabled
if (periodic_boundary.isEnabled())
{
periodic_boundary.generateMoleculesVector();
}
// force field specific parts
bool success = false;
try
{
success = specificSetup();
}
catch(...)
{
}
if (!success)
{
Log.error() << "Force Field specificSetup failed!" << endl;
return false;
}
// If specificSetup cleared this array, it wants to tell us
// that it had to change the system a bit (e.g. CHARMM replacing
// hydrogens by united atoms). So, we have to recalculated the vector.
if (atoms_.size() != old_size)
{
collectAtoms_(system);
}
// Call the setup method for each force field component.
vector<ForceFieldComponent*>::iterator it;
for (it = components_.begin(); (it != components_.end()) && success; ++it)
{
success = false;
try
{
success = (*(*it)).setup();
}
catch(...)
{
}
if (!success)
{
Log.error() << "Force Field Component setup of " << (*it)->name_ << " failed!" << endl;
}
}
// ?????
update();
// Remember the setup time
setup_time_stamp_.stamp();
// If the setup failed, our force field becomes invalid!
valid_ = success;
return success;
}
// collect all atoms
void ForceField::collectAtoms_(const System& system)
{
// Clear existing atoms entries.
atoms_.clear();
// Check for selected atoms.
// Consider selection only if it has not been disabled
// and there are selected atoms in the system.
// If any of the atoms are selected, the atoms_ array holds
// the selected atoms first (0 < i < number_of_movable_atoms_)
number_of_movable_atoms_ = 0;
AtomConstIterator atom_it;
if (getUseSelection())
{
// We store the selected atoms only!
// All other atoms will not be considered in the
// calculation -- they become invisible to the force field.
AtomConstIterator atom_it = system.beginAtom();
for (; +atom_it; ++atom_it)
{
if (atom_it->isSelected())
{
atoms_.push_back(const_cast<Atom*>(&(*atom_it)));
}
}
number_of_movable_atoms_ = (Size)atoms_.size();
}
else
{
// We store ALL atoms in the atom vector -- selection
// has been disabled for this purpose.
for (atom_it = system.beginAtom(); +atom_it; ++atom_it)
{
atoms_.push_back(const_cast<Atom*>(&(*atom_it)));
}
// Make sure the selected atoms are in the front
sortSelectedAtomVector_();
}
}
void ForceField::sortSelectedAtomVector_()
{
if (system_->containsSelection() && (atoms_.size() > 1))
{
// Exchange sort.
Position first = 0;
Position last = (Position)(atoms_.size() - 1);
while (last >= first)
{
while ((first < atoms_.size()) && atoms_[first]->isSelected())
{
first++;
}
while ((last > 0) && !atoms_[last]->isSelected())
{
last--;
}
if ((last > 0) && (first < atoms_.size()) && (first < last))
{
Atom* tmp = atoms_[first];
atoms_[first] = atoms_[last];
atoms_[last] = tmp;
}
}
number_of_movable_atoms_ = first;
}
else
{
number_of_movable_atoms_ = (Size)atoms_.size();
}
}
// Setup with a system and a set of options
bool ForceField::setup(System& system, const Options& new_options)
{
options = new_options;
return setup(system);
}
ForceFieldParameters& ForceField::getParameters()
{
return parameters_;
}
// Returns the name of the force field
String ForceField::getName() const
{
return name_;
}
// Sets the name of force field to name
void ForceField::setName(const String& name)
{
name_ = name;
}
const TimeStamp& ForceField::getUpdateTime() const
{
return update_time_stamp_;
}
const TimeStamp& ForceField::getSetupTime() const
{
return setup_time_stamp_;
}
// Returns the number of atoms stored in the vector atoms_
Size ForceField::getNumberOfAtoms() const
{
// if the force field is invalid, there's not much we can do
if (!isValid())
{
return 0;
}
return (Size)atoms_.size();
}
// Returns the number of movable (non-fixed) atoms stored in the vector atoms_
Size ForceField::getNumberOfMovableAtoms() const
{
// if the force field is invalid, there's not much we can do
if (!isValid())
{
return 0;
}
// check whether the selection changed since the last call
// to update and call update otherwise
if (update_time_stamp_.isOlderThan(system_->getSelectionTime()))
{
// Call update if the selection time stamp changed since
// the last call to update. This ensures consistency of
// the selection information in pair lists, bond lists, etc.
// Also make sure the movable atoms are still in the front
// of the atom vevtor.
// we have to do a bit of nasty casting to hide this, but that's OK here --
// it still is *kind of* const :-)
const_cast<ForceField*>(this)->sortSelectedAtomVector_();
const_cast<ForceField*>(this)->update();
// Update the use_selection_ flag.
// this const_cast is _bad_
bool* use_ptr = const_cast<bool*>(&use_selection_);
*use_ptr = (selection_enabled_ && system_->containsSelection());
}
return number_of_movable_atoms_;
}
void ForceField::updateForces()
{
// check for validity of the force field
if (!isValid())
{
return;
}
// Set forces to zero
for (AtomVector::Iterator it = atoms_.begin(); it != atoms_.end(); ++it)
{
(*it)->setForce(RTTI::getDefault<Vector3>());
}
performRequiredUpdates_();
// call each component - they will add their forces...
vector<ForceFieldComponent*>::iterator component_it = components_.begin();
for (; component_it != components_.end(); ++component_it)
{
if ((**component_it).isEnabled())
{
(*component_it)->updateForces();
}
}
}
// Calculate the RMS of the gradient
double ForceField::getRMSGradient() const
{
double sum = 0;
AtomVector::ConstIterator it(atoms_.begin());
for (; it != atoms_.end(); ++it)
{
sum += (*it)->getForce().getSquareLength();
}
sum = sqrt(sum/(3 * (double)atoms_.size()));
sum *= Constants::AVOGADRO / 1e13;
return(sum);
}
double ForceField::getEnergy() const
{
return energy_;
}
double ForceField::updateEnergy()
{
// check for validity of the force field
if (!isValid())
{
return 0.0;
}
// clear the total energy
energy_ = 0;
performRequiredUpdates_();
// call each component and add their energies
vector<ForceFieldComponent*>::iterator it;
for (it = components_.begin(); it != components_.end(); ++it)
{
if (!(**it).isEnabled()) continue;
energy_ += (*it)->updateEnergy();
}
// return the resulting energy
return energy_;
}
Size ForceField::getUpdateFrequency() const
{
return 1;
}
void ForceField::update()
{
// check for validity of the force field
if (!valid_)
{
return;
}
// iterate over all components and
// call their update methods
vector<ForceFieldComponent*>::iterator it;
for (it = components_.begin(); it != components_.end(); ++it)
{
if ((**it).isEnabled())
{
(*it)->update();
}
}
// remember the time of the last update
update_time_stamp_.stamp();
}
bool ForceField::specificSetup()
{
return true;
}
// Insert a component in the component vector
void ForceField::insertComponent(ForceFieldComponent* force_field_component)
{
components_.push_back(force_field_component);
force_field_component->setForceField(*this);
}
// Remove the component
void ForceField::removeComponent(const ForceFieldComponent* force_field_component)
{
// call each component - test if equal - remove
vector<ForceFieldComponent*>::iterator it;
for (it = components_.begin(); it != components_.end(); ++it)
{
if ((*it) == force_field_component)
{
// Destruct the component -- we are responsible for the mem management.
delete *it;
components_.erase(it);
break ;
}
}
}
// Remove the component
void ForceField::removeComponent(const String& name)
{
// call each component - test if equal - remove
vector<ForceFieldComponent*>::iterator it;
for (it = components_.begin(); it != components_.end(); ++it)
{
if ((*it)->getName() == name)
{
// Destruct the component -- we are responsible for the mem management.
delete *it;
components_.erase(it);
break ;
}
}
}
// Returns the number of components
Size ForceField::countComponents() const
{
return (Size)components_.size();
}
// Returns the component with index "index"
ForceFieldComponent* ForceField::getComponent(const Size index) const
{
if (index >= components_.size())
{
return 0;
}
return components_[index];
}
ForceFieldComponent* ForceField::getComponent(const String& name) const
{
for (Size i = 0; i < components_.size(); ++i)
{
if (components_[i]->getName() == name)
{
return components_[i];
}
}
return 0;
}
void ForceField::setMaximumNumberOfErrors(Size nr)
{
max_number_of_errors_= nr;
}
Size ForceField::getMaximumNumberOfErrors() const
{
return max_number_of_errors_;
}
HashSet<const Atom*>& ForceField::getUnassignedAtoms()
{
return unassigned_atoms_;
}
std::ostream& ForceField::error()
{
number_of_errors_++;
if (number_of_errors_ > max_number_of_errors_)
{
throw Exception::TooManyErrors(__FILE__, __LINE__);
}
return Log.error();
}
void ForceField::performRequiredUpdates_()
{
// check whether the selection changed since the last call
// to update and call update otherwise
if (update_time_stamp_.isOlderThan(system_->getSelectionTime()))
{
// Call update if the selection time stamp changed since
// the last call to update. This ensures consistency of
// the selection information in pair lists, bond lists, etc.
// Also make sure the movable atoms are still in the front
// of the atom vevtor.
sortSelectedAtomVector_();
update();
// Update the use_selection_ flag.
use_selection_ = (selection_enabled_ && system_->containsSelection());
}
if (setup_time_stamp_.isOlderThan(system_->getModificationTime()))
{
// complain if someone meddled with the system while simulating it
Log.error() << "ForceField::updateForces: Error! System topology was modified (i.e., atoms, residues"
"or the like were inserted or removed) after ForceField::Setup was called for the last"
"time. The results obtained from this simulation might be erroneous.\n";
}
}
# ifdef BALL_NO_INLINE_FUNCTIONS
# include <BALL/MOLMEC/COMMON/forceField.iC>
# endif
} // namespace BALL
|