| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | // $Id: forceField.h,v 1.33 2005/12/23 17:01:51 amoll Exp $ |
| 5 | // |
| 6 | |
| 7 | #ifndef BALL_MOLMEC_COMMON_FORCEFIELD_H |
| 8 | #define BALL_MOLMEC_COMMON_FORCEFIELD_H |
| 9 | |
| 10 | #ifndef BALL_COMMON_H |
| 11 | # include <BALL/common.h> |
| 12 | #endif |
| 13 | |
| 14 | #ifndef BALL_KERNEL_SYSTEM_H |
| 15 | # include <BALL/KERNEL/system.h> |
| 16 | #endif |
| 17 | |
| 18 | #ifndef BALL_DATATYPE_OPTIONS_H |
| 19 | # include <BALL/DATATYPE/options.h> |
| 20 | #endif |
| 21 | |
| 22 | #ifndef BALL_CONCEPT_TIMESTAMP_H |
| 23 | # include <BALL/CONCEPT/timeStamp.h> |
| 24 | #endif |
| 25 | |
| 26 | #ifndef BALL_MOLMEC_PARAMETER_FORCEFIELDPARAMETERS_H |
| 27 | # include <BALL/MOLMEC/PARAMETER/forceFieldParameters.h> |
| 28 | #endif |
| 29 | |
| 30 | #ifndef BALL_MOLMEC_PARAMETER_ATOMTYPES_H |
| 31 | # include <BALL/MOLMEC/PARAMETER/atomTypes.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifndef BALL_MOLMEC_COMMON_PERIODIC_BOUNDARY_H |
| 35 | # include <BALL/MOLMEC/COMMON/periodicBoundary.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifndef BALL_MOLMEC_COMMON_ATOMVECTOR_H |
| 39 | # include <BALL/MOLMEC/COMMON/atomVector.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <vector> |
| 43 | |
| 44 | namespace BALL |
| 45 | { |
| 46 | class ForceFieldComponent; |
| 47 | |
| 48 | /** Force field class. |
| 49 | This class is used to represent a general force field. |
| 50 | Each force field by itself is composed by several |
| 51 | different force field components which are represented |
| 52 | by \link ForceFieldComponent ForceFieldComponent \endlink objects. \par |
| 53 | Each ForceField object provides a list of components |
| 54 | which may be manipulated by the user to generate the |
| 55 | force field he needs. \par |
| 56 | ForceField only represents a baseclass to the specific force field |
| 57 | implementations (e.g. \link AMBER AMBER \endlink ) and implements the common interface |
| 58 | and the neccessary mechanisms to administer the force field components. \par |
| 59 | A typical force field contains a small number of components (e.g. bond stretch, |
| 60 | bend, torsion and non-bonding interactions). |
| 61 | A specialized forcefield has to implement each of these components (by deriving them |
| 62 | from \link ForceFieldComponent ForceFieldComponent \endlink and must be derived from ForceField. |
| 63 | Basically only the default constructor for the new forcefield has to |
| 64 | be modified to create an instance of each of the components and |
| 65 | register them by calling \link insertComponent insertComponent \endlink . But in order to implement |
| 66 | the setup methods of the force field components efficiently, |
| 67 | jobs like assigning atom types, reading parameter files and the such should be |
| 68 | provided for all force field components by the corrseponding ForceField |
| 69 | object. This should be implemented in the force field-specific \link specificSetup specificSetup \endlink |
| 70 | method. \par |
| 71 | For an efficient and easy to use implementation of a forcefield |
| 72 | parameter file, please refer to \link ForceFieldParameters ForceFieldParameters \endlink and its |
| 73 | related objects. \par |
| 74 | Each force field provides as well energy and its derivatives, the force |
| 75 | on each atom. A calculation of the energy is done by calling |
| 76 | \link updateEnergy updateEnergy \endlink . This method iterates over the list of force field |
| 77 | components and invokes the updateEnergy method of each of these components. |
| 78 | The total energy may then be retrieved by calling \link getEnergy getEnergy \endlink . \par |
| 79 | Analogously, a force calculation is performed by invoking \link updateForces updateForces \endlink . |
| 80 | \link getForces getForces \endlink returns a pointer to an array containing the forces |
| 81 | for each atom. \par |
| 82 | |
| 83 | \ingroup MolmecCommon |
| 84 | */ |
| 85 | class BALL_EXPORT ForceField |
| 86 | { |
| 87 | public: |
| 88 | |
| 89 | friend class ForceFieldComponent; |
| 90 | |
| 91 | /** @name Type Definitions |
| 92 | */ |
| 93 | //@{ |
| 94 | |
| 95 | /** Atom pair vector. |
| 96 | This type is used to represent "pair lists". In fact, it is |
| 97 | a <tt>vector</tt> of <tt>pair</tt>s of atom pointers. |
| 98 | */ |
| 99 | typedef std::vector<std::pair<Atom*, Atom*> > PairVector; |
| 100 | |
| 101 | //@} |
| 102 | /** @name Constructors and Destructors |
| 103 | */ |
| 104 | //@{ |
| 105 | |
| 106 | BALL_CREATE(ForceField) |
| 107 | |
| 108 | /** Default constructor. |
| 109 | A default-constructed force field is not valid! |
| 110 | It becomes valid after a successful call to setup. |
| 111 | */ |
| 112 | ForceField(); |
| 113 | |
| 114 | /** Constructor. |
| 115 | The successful setup of the force field can be verified |
| 116 | by the \link isValid isValid \endlink method. |
| 117 | */ |
| 118 | ForceField(System& system); |
| 119 | |
| 120 | /** Constructor. |
| 121 | The successful setup of the force field can be verified |
| 122 | by the \link isValid isValid \endlink method. |
| 123 | */ |
| 124 | ForceField(System& system, const Options& options); |
| 125 | |
| 126 | /** Copy constructor |
| 127 | */ |
| 128 | ForceField(const ForceField& force_field); |
| 129 | |
| 130 | /** Destructor. |
| 131 | */ |
| 132 | virtual ~ForceField(); |
| 133 | |
| 134 | /** Clear method. |
| 135 | */ |
| 136 | virtual void clear() |
| 137 | ; |
| 138 | |
| 139 | //@} |
| 140 | /** @name Assignments |
| 141 | */ |
| 142 | //@{ |
| 143 | |
| 144 | /** Assignment operator |
| 145 | */ |
| 146 | ForceField& operator = (const ForceField& force_field); |
| 147 | |
| 148 | //@} |
| 149 | /** @name Debugging and Diagnostics |
| 150 | */ |
| 151 | //@{ |
| 152 | |
| 153 | /** Check the force field's validity. |
| 154 | A force field is valid if it is bound to a system |
| 155 | and \link setup setup \endlink was successful. |
| 156 | */ |
| 157 | bool isValid() const |
| 158 | ; |
| 159 | |
| 160 | //@} |
| 161 | /** @name Setup methods |
| 162 | */ |
| 163 | //@{ |
| 164 | |
| 165 | /** Sets up the force field and its components. |
| 166 | */ |
| 167 | bool setup(System& system); |
| 168 | |
| 169 | /** Sets up the force field and its components. |
| 170 | */ |
| 171 | bool setup(System& system, const Options& options); |
| 172 | |
| 173 | /** Force field specific setup. |
| 174 | This method is called by setup. |
| 175 | @throws BALL::Exception::TooManyErrors |
| 176 | */ |
| 177 | virtual bool specificSetup(); |
| 178 | |
| 179 | /** Set the number of atoms, for which the setup of the forcefield can |
| 180 | fail, until the setup() methods aborts and return false. |
| 181 | By default, there is no limit set. |
| 182 | */ |
| 183 | void setMaximumNumberOfErrors(Size nr); |
| 184 | |
| 185 | /** Get the number of atoms, for which the setup of the forcefield can |
| 186 | fail, until the setup() methods aborts and return false. |
| 187 | */ |
| 188 | Size getMaximumNumberOfErrors() const; |
| 189 | |
| 190 | /// Get the atoms, for which the force field setup failed. |
| 191 | HashSet<const Atom*>& getUnassignedAtoms(); |
| 192 | |
| 193 | //@} |
| 194 | /** @name Accessors |
| 195 | */ |
| 196 | //@{ |
| 197 | |
| 198 | /** Sets the force field name. |
| 199 | */ |
| 200 | void setName(const String& name); |
| 201 | |
| 202 | /** Returns the force field name |
| 203 | */ |
| 204 | String getName() const; |
| 205 | |
| 206 | /** Returns the number of atoms stored in the force field |
| 207 | */ |
| 208 | Size getNumberOfAtoms() const; |
| 209 | |
| 210 | /** Returns the number of non-fixed atoms stored in the force field. |
| 211 | If the option <tt>SELECTION_FIXED</tt> is set to <b>true</b> or |
| 212 | <tt>SELECTION_MOVABLE</tt> is set to <b>true</b> the atom array |
| 213 | is split. The first section (indices 0 to \link getNumberOfMovableAtoms getNumberOfMovableAtoms \endlink ) |
| 214 | contains the atoms that are to be moved, the rest of the array contains |
| 215 | the fixed atoms. |
| 216 | */ |
| 217 | Size getNumberOfMovableAtoms() const; |
| 218 | |
| 219 | /** Returns a reference to the atom vector |
| 220 | */ |
| 221 | BALL_INLINE |
| 222 | const AtomVector& getAtoms() const ; |
| 223 | |
| 224 | /** Returns a pointer to the system |
| 225 | */ |
| 226 | BALL_INLINE |
| 227 | System* getSystem() ; |
| 228 | |
| 229 | /** Returns a const pointer to the system |
| 230 | */ |
| 231 | BALL_INLINE |
| 232 | const System* getSystem() const ; |
| 233 | |
| 234 | /** Return the status of the selection mechanism |
| 235 | */ |
| 236 | BALL_INLINE |
| 237 | bool getUseSelection() const ; |
| 238 | |
| 239 | /** Set the status of the selection mechanism |
| 240 | */ |
| 241 | BALL_INLINE |
| 242 | void disableSelection() ; |
| 243 | |
| 244 | /** Set the status of the selection mechanism |
| 245 | */ |
| 246 | BALL_INLINE |
| 247 | void enableSelection() ; |
| 248 | |
| 249 | /** Return the status of the selection mechanism */ |
| 250 | BALL_INLINE |
| 251 | bool isSelectionEnabled() const ; |
| 252 | |
| 253 | /** Returns a pointer to the parameter file |
| 254 | */ |
| 255 | ForceFieldParameters& getParameters(); |
| 256 | |
| 257 | /** Returns the number of components registered by the force field. |
| 258 | */ |
| 259 | Size countComponents() const; |
| 260 | |
| 261 | /** Return the point of time of the last call to update. |
| 262 | ForceField contains a time stamp which is used to determine |
| 263 | whether the selection or even the topology of the system |
| 264 | has changed. Every time update is called, the |
| 265 | \link update_time_stamp_ update_time_stamp_ \endlink is updated. Similarly, all setup methods |
| 266 | update the \link setup_time_stamp_ setup_time_stamp_ \endlink |
| 267 | */ |
| 268 | const TimeStamp& getUpdateTime() const |
| 269 | ; |
| 270 | |
| 271 | /** Return the point of time of the last call to setup. |
| 272 | ForceField contains a time stamp which is used to determine |
| 273 | whether the selection or even the topology of the system |
| 274 | has changed. Every time update is called, the |
| 275 | \link update_time_stamp_ update_time_stamp_ \endlink is updated. Similarly, all setup methods |
| 276 | update the \link setup_time_stamp_ setup_time_stamp_ \endlink |
| 277 | */ |
| 278 | const TimeStamp& getSetupTime() const |
| 279 | ; |
| 280 | |
| 281 | /** Insert a new component into the force field's component list. |
| 282 | Responsability for the destruction of the component is passed on to |
| 283 | the ForceField instance. |
| 284 | */ |
| 285 | void insertComponent(ForceFieldComponent* force_field_component); |
| 286 | |
| 287 | /** Remove a component from the force field's component list. |
| 288 | The ForceFieldComponent will be destructed and removed from the component list. |
| 289 | */ |
| 290 | void removeComponent(const ForceFieldComponent* force_field_component); |
| 291 | |
| 292 | /** Remove a component from the force field's component list. |
| 293 | */ |
| 294 | void removeComponent(const String& name); |
| 295 | |
| 296 | /** Return a pointer to the specified force field component. |
| 297 | If the specified index does not exist, 0 is returned. |
| 298 | The given index should be smaller than the value returned by |
| 299 | countComponents. The first component in the list has the index zero. |
| 300 | */ |
| 301 | ForceFieldComponent* getComponent(const Size index) const; |
| 302 | |
| 303 | /** Return a pointer to the specified force field component. |
| 304 | If a component with the specified name does not exist, 0 is returned. |
| 305 | */ |
| 306 | ForceFieldComponent* getComponent(const String& name) const; |
| 307 | |
| 308 | /** Return the sum of energies of all registered force field components. |
| 309 | No calculation will be performed. This method simply returns the |
| 310 | last value for the total energy calculated by updateEnergy. |
| 311 | @return double - energy in kJ/mol |
| 312 | */ |
| 313 | double getEnergy() const; |
| 314 | |
| 315 | /** Calculate the sum of energies of all force field components and returns its value. |
| 316 | */ |
| 317 | double updateEnergy(); |
| 318 | |
| 319 | /** Calculate the forces caused by each component and updates the current forces. |
| 320 | */ |
| 321 | void updateForces(); |
| 322 | |
| 323 | /** Calculates the RMS of the current gradient |
| 324 | */ |
| 325 | double getRMSGradient() const; |
| 326 | |
| 327 | /** Return the update frequency for pair lists etc. |
| 328 | This method is used by minimizers or the MD simulation to determine the number |
| 329 | of iterations between two calls to \link update update \endlink . |
| 330 | */ |
| 331 | virtual Size getUpdateFrequency() const; |
| 332 | |
| 333 | /** Update internal data structures. |
| 334 | The force field may use this method to update internal data structures |
| 335 | (e.g. pair lists) periodically. The MD simulation class as well as the minimizer classes |
| 336 | use \link getUpdateFrequency getUpdateFrequency \endlink to determine the number of iterations |
| 337 | between two calls to update. \par |
| 338 | The default implementation calls \link ForceFieldComponent::update ForceFieldComponent::update \endlink for |
| 339 | each component in the force field. |
| 340 | @throws BALL::Exception::TooManyErrors |
| 341 | */ |
| 342 | virtual void update(); |
| 343 | |
| 344 | /** Get the current results in String form |
| 345 | (Generic function to be overloaded in derived classes.) |
| 346 | */ |
| 347 | virtual String getResults() const |
| 348 | { return "undefined";} |
| 349 | |
| 350 | //_ Report an error and increase the error counter |
| 351 | std::ostream& error(); |
| 352 | |
| 353 | //@} |
| 354 | /** @name Public Attributes |
| 355 | */ |
| 356 | //@{ |
| 357 | |
| 358 | /** Force field options |
| 359 | */ |
| 360 | Options options; |
| 361 | |
| 362 | /** Periodic boundary |
| 363 | */ |
| 364 | PeriodicBoundary periodic_boundary; |
| 365 | |
| 366 | //@} |
| 367 | |
| 368 | protected: |
| 369 | /*_ Collect all atoms into the atoms_ vector. |
| 370 | */ |
| 371 | void collectAtoms_(const System& system); |
| 372 | |
| 373 | /*_ Sort the atom vector wrt selection. |
| 374 | */ |
| 375 | void sortSelectedAtomVector_(); |
| 376 | |
| 377 | /** |
| 378 | * Check whether the force field needs to be updated and perform the necessary steps |
| 379 | */ |
| 380 | virtual void performRequiredUpdates_(); |
| 381 | |
| 382 | /*_ @name Protected Attributes |
| 383 | */ |
| 384 | //_@{ |
| 385 | |
| 386 | /*_ The system the force field is bound to |
| 387 | */ |
| 388 | System* system_; |
| 389 | |
| 390 | /*_ The atoms in the simulated system |
| 391 | */ |
| 392 | AtomVector atoms_; |
| 393 | |
| 394 | /*_ An object containing the force field parameters read from a file |
| 395 | */ |
| 396 | ForceFieldParameters parameters_; |
| 397 | |
| 398 | /*_ The boolean variable indicates if the setup of the force field was successful |
| 399 | */ |
| 400 | bool valid_; |
| 401 | |
| 402 | /*_ The force field name |
| 403 | */ |
| 404 | String name_; |
| 405 | |
| 406 | /*_ The total energy |
| 407 | */ |
| 408 | double energy_; |
| 409 | |
| 410 | /*_ The components of the force field |
| 411 | */ |
| 412 | vector<ForceFieldComponent*> components_; |
| 413 | |
| 414 | /*_ The number of movable atoms in the force field |
| 415 | */ |
| 416 | Size number_of_movable_atoms_; |
| 417 | |
| 418 | /*_ Do we have to check whether atoms are selected? |
| 419 | */ |
| 420 | bool use_selection_; |
| 421 | |
| 422 | /*_ This flag can temporarily disable the selection. |
| 423 | */ |
| 424 | bool selection_enabled_; |
| 425 | |
| 426 | /*_ The time of the last call to update. |
| 427 | */ |
| 428 | TimeStamp update_time_stamp_; |
| 429 | |
| 430 | /*_ The time of the last call to setup. |
| 431 | */ |
| 432 | TimeStamp setup_time_stamp_; |
| 433 | |
| 434 | //_ Atoms, for which the setup of the force field fails |
| 435 | HashSet<const Atom*> unassigned_atoms_; |
| 436 | |
| 437 | //_ max number of unassigned atoms |
| 438 | Size max_number_of_errors_; |
| 439 | |
| 440 | Size number_of_errors_; |
| 441 | |
| 442 | //_@} |
| 443 | }; |
| 444 | |
| 445 | # ifndef BALL_NO_INLINE_FUNCTIONS |
| 446 | # include <BALL/MOLMEC/COMMON/forceField.iC> |
| 447 | # endif |
| 448 | |
| 449 | } // namespace BALL |
| 450 | |
| 451 | #endif // BALL_MOLMEC_COMMON_FORCEFIELD_H |