| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | // $Id: snapShotManager.h,v 1.19.20.1 2007/05/10 10:51:04 amoll Exp $ |
| 5 | // |
| 6 | |
| 7 | #ifndef BALL_MOLMEC_COMMON_SNAPSHOTMANAGER_H |
| 8 | #define BALL_MOLMEC_COMMON_SNAPSHOTMANAGER_H |
| 9 | |
| 10 | #ifndef BALL_MOLMEC_COMMON_SNAPSHOT_H |
| 11 | # include <BALL/MOLMEC/COMMON/snapShot.h> |
| 12 | #endif |
| 13 | |
| 14 | #ifndef BALL_DATATYPE_OPTIONS_H |
| 15 | # include <BALL/DATATYPE/options.h> |
| 16 | #endif |
| 17 | |
| 18 | #ifndef BALL_SYSTEM_FILE_H |
| 19 | # include <BALL/SYSTEM/file.h> |
| 20 | #endif |
| 21 | |
| 22 | namespace BALL |
| 23 | { |
| 24 | class TrajectoryFile; |
| 25 | class System; |
| 26 | class ForceField; |
| 27 | |
| 28 | /** Snapshot management e.g. for MD simulations. |
| 29 | This class manages a list of single SnapShot objects. |
| 30 | Snapshots are numbered starting with 1. \par |
| 31 | \ingroup MolmecCommon |
| 32 | */ |
| 33 | class BALL_EXPORT SnapShotManager |
| 34 | { |
| 35 | public: |
| 36 | |
| 37 | /// Local class for handling options |
| 38 | struct BALL_EXPORT Option |
| 39 | { |
| 40 | /** After how many snapshots shall they be flushed to the hard disk |
| 41 | @see Default::FLUSH_TO_DISK_FREQUENCY |
| 42 | @param frequency integer |
| 43 | */ |
| 44 | static const char* FLUSH_TO_DISK_FREQUENCY; |
| 45 | }; |
| 46 | |
| 47 | /// Local class for handling default values for the options |
| 48 | struct BALL_EXPORT Default |
| 49 | { |
| 50 | /** This is the default value of the number of snapshots to be taken |
| 51 | before writing them to hard disk. Default is ... |
| 52 | @see Option::FLUSH_TO_DISK_FREQUENCY |
| 53 | */ |
| 54 | static const Size FLUSH_TO_DISK_FREQUENCY; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | BALL_CREATE(SnapShotManager) |
| 59 | |
| 60 | /// @name Constructors and Destructors |
| 61 | //@{ |
| 62 | |
| 63 | /// Default constructor |
| 64 | SnapShotManager(); |
| 65 | |
| 66 | /** |
| 67 | * Constructor for a system and trajectory file. The internal reference |
| 68 | * to a force field will be set to NULL |
| 69 | * @param my_system the system to bind this manager to |
| 70 | * @param my_snapshot_file an optional file containing a trajectory for the my_system's atoms |
| 71 | */ |
| 72 | SnapShotManager(System* my_system, TrajectoryFile* my_snapshot_file = 0); |
| 73 | |
| 74 | /** This constructor expects a valid system, a valid force field |
| 75 | and the name of a snapshot file. |
| 76 | Any existing file of the given name will be overwritten. |
| 77 | @param my_system the system to bind this manager to |
| 78 | @param my_force_field the force field that is bound to the system |
| 79 | @param my_snapshot_file a file containing a trajectory for the my_system's atoms |
| 80 | */ |
| 81 | SnapShotManager(System* my_system, const ForceField* my_force_field, TrajectoryFile* my_snapshot_file); |
| 82 | |
| 83 | /** This constructor expects a valid system, a valid force field |
| 84 | and the name of a snapshot file. |
| 85 | Any existing file of the given name will be overwritten. |
| 86 | @param my_system the system to bind this manager to |
| 87 | @param my_force_field the force field that is bound to the system |
| 88 | @param my_options |
| 89 | @param filename the name of the snapshot file |
| 90 | */ |
| 91 | SnapShotManager (System* my_system, const ForceField* my_force_field, |
| 92 | const Options& my_options, TrajectoryFile* file); |
| 93 | |
| 94 | /// Copy constructor. |
| 95 | SnapShotManager(const SnapShotManager& manager); |
| 96 | |
| 97 | /// Destructor. |
| 98 | virtual ~SnapShotManager(); |
| 99 | |
| 100 | //@} |
| 101 | /** @name Setup methods |
| 102 | */ |
| 103 | //@{ |
| 104 | |
| 105 | /** This setup method sets all necessary members and calls setup afterwards. |
| 106 | @param my_system the System to which we want to bind this SnapShotManager |
| 107 | @param my_force_field the respective forcefield |
| 108 | @param my_snapshot_file the trajectory file we want to use |
| 109 | @return true, if the setup was succesful, false ow. |
| 110 | */ |
| 111 | bool setup(System* my_system, const ForceField* my_forcefield, TrajectoryFile* my_snapshot_file); |
| 112 | |
| 113 | /** This setup method sets all necessary members and calls setup afterwards. The internal |
| 114 | force field reference is set to NULL. |
| 115 | @param my_system the System to which we want to bind this SnapShotManager |
| 116 | @param my_snapshot_file the trajectory file we want to use |
| 117 | @return true, if the setup was succesful, false ow. |
| 118 | */ |
| 119 | bool setup(System* my_system, TrajectoryFile* my_snapshot_file); |
| 120 | |
| 121 | /** The setup method does all preparations necessary for using the |
| 122 | SnapshotManager. |
| 123 | @return true, if setup was succesful |
| 124 | */ |
| 125 | virtual bool setup(); |
| 126 | |
| 127 | //@} |
| 128 | /** @name Assignment |
| 129 | */ |
| 130 | //@{ |
| 131 | |
| 132 | /** The assignment operator. |
| 133 | */ |
| 134 | const SnapShotManager& operator = (const SnapShotManager& manager); |
| 135 | |
| 136 | /// Clear method |
| 137 | virtual void clear(); |
| 138 | |
| 139 | //@} |
| 140 | /// @name Debugging and diagnostics |
| 141 | //@{ |
| 142 | |
| 143 | /** Is the SnapshotManager ready for use? |
| 144 | */ |
| 145 | virtual bool isValid() const; |
| 146 | |
| 147 | //@} |
| 148 | /// @name Accessors |
| 149 | //@{ |
| 150 | |
| 151 | /** Set the system member |
| 152 | @param my_system a const pointer to the system to which this |
| 153 | SnapshotManager will be bound |
| 154 | */ |
| 155 | void setSystem(System* my_system); |
| 156 | |
| 157 | /// get a const pointer to the system member |
| 158 | System* getSystem() const; |
| 159 | |
| 160 | /// set the force field |
| 161 | void setForceField(const ForceField* my_ff); |
| 162 | |
| 163 | /// get a const pointer to the force field |
| 164 | const ForceField* getForceField() const; |
| 165 | |
| 166 | /// set the trajectory file |
| 167 | void setTrajectoryFile(TrajectoryFile* my_file); |
| 168 | |
| 169 | /// get a pointer to the trajectory file |
| 170 | TrajectoryFile* getTrajectoryFile() const; |
| 171 | |
| 172 | /** Set the frequency for saving snapshots to hard disk. |
| 173 | Every 'number' iterations, a save will be done. |
| 174 | @param number the number of snapshots to take before flushing to disk |
| 175 | */ |
| 176 | void setFlushToDiskFrequency(Size number); |
| 177 | |
| 178 | /** Get the current frequency for doing saves to hard disk |
| 179 | @return the number of snapshots to take before flushing them to disk |
| 180 | */ |
| 181 | Size getFlushToDiskFrequency() const; |
| 182 | |
| 183 | /** This method takes a snapshot of the system's current state and stores |
| 184 | it in main memory. If there is not sufficient space, the snapshots |
| 185 | collected so far are flushed to hard disk. The first snapshot taken |
| 186 | has index 1. |
| 187 | @throw BALL::File::CannotWrite thrown if the snapshot could not be flushed to disk |
| 188 | */ |
| 189 | virtual void takeSnapShot(); |
| 190 | |
| 191 | /** Read a certain SnapShot from a TrajectoryFile. This method tries to |
| 192 | read SnapShot number <b>number</b> from the file |
| 193 | @param number the number of the snapshot we want to read |
| 194 | @param snapshot a buffer for returning the snapshot |
| 195 | @return true if the snapshot could be read, <b>false</b> ow. |
| 196 | */ |
| 197 | virtual bool applySnapShot(Size number); |
| 198 | |
| 199 | /** Read a the first SnapShot from the associated TrajectoryFile. |
| 200 | @param snapshot a buffer for returning the snapshot |
| 201 | @return true if the snapshot could be read, <b>false</b> ow. |
| 202 | */ |
| 203 | virtual bool applyFirstSnapShot(); |
| 204 | |
| 205 | /** Read a the next SnapShot from the associated TrajectoryFile and |
| 206 | apply it to the system |
| 207 | @param snapshot a buffer for returning the snapshot |
| 208 | @return true if the snapshot could be read, <b>false</b> ow. |
| 209 | */ |
| 210 | virtual bool applyNextSnapShot(); |
| 211 | |
| 212 | /** This method applies the last SnapShot of the associated file |
| 213 | i. e. writes all available data (positions, forces, etc.) from the |
| 214 | SnapShot to the System. Note that a SnapShot does not need to have |
| 215 | all data. |
| 216 | */ |
| 217 | virtual bool applyLastSnapShot(); |
| 218 | |
| 219 | /// This method writes all snapshots taken so far to hard disk |
| 220 | /// @throw BALL::File::CannotWrite thrown if the snapshots could not be flushed to disk |
| 221 | virtual void flushToDisk(); |
| 222 | |
| 223 | /// |
| 224 | Size getNumberOfSnapShotsInBuffer() { return snapshot_buffer_.size(); } |
| 225 | |
| 226 | /// |
| 227 | Position getCurrentSnapshotNumber() const { return current_snapshot_ + 1; } |
| 228 | |
| 229 | /** Try to read all SnapShot 's from the TrajectoryFile into the memory. |
| 230 | */ |
| 231 | bool readFromFile(); |
| 232 | |
| 233 | /// Clear all currently loaded SnapShot 's. |
| 234 | void clearBuffer(); |
| 235 | |
| 236 | //@} |
| 237 | /// @name Public Attributes |
| 238 | //@{ |
| 239 | |
| 240 | /// The available options for this class. |
| 241 | Options options; |
| 242 | |
| 243 | //@} |
| 244 | //_ @name Protected Attributes |
| 245 | //_@{ |
| 246 | |
| 247 | protected: |
| 248 | |
| 249 | //_ The system to which the SnapshotManager is bound |
| 250 | System* system_ptr_; |
| 251 | |
| 252 | //_ The force field of the current system |
| 253 | const ForceField* force_field_ptr_; |
| 254 | |
| 255 | //_ A vector containing those snapshot objects currently in memory |
| 256 | vector<SnapShot> snapshot_buffer_; |
| 257 | |
| 258 | //_ The trajectory file where the data is saved in |
| 259 | TrajectoryFile* trajectory_file_ptr_; |
| 260 | |
| 261 | /*_ The frequency of saving snapshots in memory to disk. |
| 262 | After flush_to_disk_frequency_ iterations, a save is done. |
| 263 | */ |
| 264 | Size flush_to_disk_frequency_; |
| 265 | |
| 266 | //_ Number of taken SnapShot sine last flushToDisk |
| 267 | Size buffer_counter_; |
| 268 | |
| 269 | //_ Number of the current SnapShot (used with buffer_) |
| 270 | Position current_snapshot_; |
| 271 | |
| 272 | //_@} |
| 273 | /*_ @name Protected methods |
| 274 | */ |
| 275 | //_@{ |
| 276 | |
| 277 | /*_ Calculate the kinetic energy |
| 278 | */ |
| 279 | double calculateKineticEnergy_(); |
| 280 | |
| 281 | //_@} |
| 282 | |
| 283 | }; // end of class SnapshotManager |
| 284 | |
| 285 | } // namespace |
| 286 | #endif // BALL_MOLMEC_COMMON_SNAPSHOTMANAGER_H |