Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarbias.h
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef COLVARBIAS_H
11#define COLVARBIAS_H
12
13#include <memory>
14
15#include "colvar.h"
16#include "colvarparse.h"
17#include "colvardeps.h"
18
20
23 : public virtual colvarparse, public virtual colvardeps {
24public:
25
27 std::string name;
28
30 std::string bias_type;
31
33 std::string state_keyword;
34
36 int rank;
37
39 int add_colvar(std::string const &cv_name);
40
42 inline size_t num_variables() const
43 {
44 return colvars.size();
45 }
46
48 inline std::vector<colvar *> *variables()
49 {
50 return &colvars;
51 }
52
54 inline colvar * variables(int i) const
55 {
56 return colvars[i];
57 }
58
61 virtual int update();
62
65 virtual bool can_accumulate_data();
66
70 virtual int calc_energy(std::vector<colvarvalue> const *values);
71
75 virtual int calc_forces(std::vector<colvarvalue> const *values);
76
79
81 virtual int end_of_step();
82
84 virtual int change_configuration(std::string const &conf);
85
87 virtual cvm::real energy_difference(std::string const &conf);
88
90 // FIXME this is currently 1D only
91 virtual int bin_num();
93 // FIXME this is currently 1D only
94 virtual int current_bin();
96 virtual int bin_count(int bin_index);
98 virtual int local_sample_count(int radius);
99
101 virtual int replica_share();
102
104 virtual size_t replica_share_freq() const;
105
107 virtual void analyze() {}
108
110 colvarbias(char const *key);
111
113 virtual int init(std::string const &conf);
114
116 virtual int init_mts(std::string const &conf);
117
119 virtual int init_dependencies();
120
122 virtual int reset();
123
124private:
125
127 colvarbias();
128
131
132public:
133
135 virtual int clear();
136
138 virtual int clear_state_data();
139
141 virtual ~colvarbias();
142
144 virtual std::string const get_state_params() const;
145
147 int check_matching_state(std::string const &conf);
148
150 virtual int set_state_params(std::string const &state_conf);
151
153 virtual std::ostream & write_state_data(std::ostream &os)
154 {
155 return os;
156 }
157
160 {
161 return os;
162 }
163
165 virtual std::istream & read_state_data(std::istream &is)
166 {
167 return is;
168 }
169
172 {
173 return is;
174 }
175
180 std::ostream &write_state_data_key(std::ostream &os, std::string const &key, bool header = true) const;
181
187 bool header = true) const;
188
189private:
190
194 template <typename IST> IST &read_state_data_key_template_(IST &is, std::string const &key);
195
196public:
197
201 std::istream & read_state_data_key(std::istream &is, std::string const &key);
202
206 cvm::memory_stream & read_state_data_key(cvm::memory_stream &is, std::string const &key);
207
208private:
209
211 template <typename IST> IST & read_state_template_(IST &is);
212
213public:
214
216 std::ostream &write_state(std::ostream &os);
217
220
222 std::istream & read_state(std::istream &is);
223
226
228 int write_state_prefix(std::string const &prefix);
229
231 int write_state_string(std::string &output);
232
234 int read_state_prefix(std::string const &prefix);
235
237 int read_state_string(char const *buffer);
238
240 virtual std::ostream & write_traj_label(std::ostream &os);
241
243 virtual std::ostream & write_traj(std::ostream &os);
244
246 virtual int setup_output()
247 {
248 return COLVARS_OK;
249 }
250
253
255 virtual int write_output_files()
256 {
257 return COLVARS_OK;
258 }
259
261 std::string output_prefix;
262
265 {
266 return COLVARS_OK;
267 }
268
269 inline cvm::real get_energy()
270 {
271 return bias_energy;
272 }
273
275 static std::vector<feature *> cvb_features;
276
278 virtual const std::vector<feature *> &features() const
279 {
280 return cvb_features;
281 }
282 virtual std::vector<feature *> &modify_features()
283 {
284 return cvb_features;
285 }
286 static void delete_features() {
287 for (size_t i=0; i < cvb_features.size(); i++) {
288 delete cvb_features[i];
289 }
290 cvb_features.clear();
291 }
292
293protected:
294
298 std::vector<colvar *> colvars;
299
301 std::vector<colvarvalue> colvar_values;
302
304 std::vector<colvarvalue> colvar_forces;
305
307 std::vector<colvarvalue> previous_colvar_forces;
308
311
314
318
321
324
328 std::vector<int> biasing_force_scaling_factors_bin;
329};
330
331
334
336class colvarbias_ti : public virtual colvarbias {
337public:
338
339 colvarbias_ti(char const *key);
340 virtual ~colvarbias_ti();
341
342 virtual int init(std::string const &conf);
343 virtual int init_grids();
344 virtual int update();
345
348 virtual int update_system_forces(std::vector<colvarvalue> const
349 *subtract_forces);
350
351 virtual std::string const get_state_params() const;
352 virtual int set_state_params(std::string const &state_conf);
353 virtual std::ostream & write_state_data(std::ostream &os);
355 virtual std::istream & read_state_data(std::istream &is);
357 virtual int write_output_files();
358
359protected:
360
362 std::vector<colvarvalue> ti_system_forces;
363
365 std::string grid_conf;
366
368 std::shared_ptr<colvar_grid_gradient> ti_avg_forces;
369
371 std::shared_ptr<colvar_grid_count> ti_count;
372
375 std::vector<int> ti_bin;
376};
377
378#endif
Colvar_grid derived class to hold counters in discrete n-dim colvar space.
Definition: colvargrid.h:1002
Class for accumulating the gradient of a scalar function on a grid.
Definition: colvargrid.h:1598
Class for accumulating a scalar function on a grid.
Definition: colvargrid.h:1283
A collective variable (main class); to be defined, it needs at least one object of a derived class of...
Definition: colvar.h:53
Base class for unconstrained thermodynamic-integration FE estimator.
Definition: colvarbias.h:336
std::shared_ptr< colvar_grid_count > ti_count
Histogram of sampled data.
Definition: colvarbias.h:371
virtual std::ostream & write_state_data(std::ostream &os)
Write all mutable data not already written by get_state_params() to a formatted stream.
Definition: colvarbias.cpp:984
virtual std::string const get_state_params() const
Write the values of specific mutable properties to a string.
Definition: colvarbias.cpp:972
std::vector< colvarvalue > ti_system_forces
Forces exerted from the system to the associated variables.
Definition: colvarbias.h:362
virtual std::istream & read_state_data(std::istream &is)
Read all mutable data not already set by set_state_params() from a formatted stream.
Definition: colvarbias.cpp:1010
virtual int update()
Definition: colvarbias.cpp:904
virtual int write_output_files()
Write any output files that this bias may have (e.g. PMF files)
Definition: colvarbias.cpp:1064
virtual int update_system_forces(std::vector< colvarvalue > const *subtract_forces)
Definition: colvarbias.cpp:910
virtual int init(std::string const &conf)
Parse config string and (re)initialize.
Definition: colvarbias.cpp:830
std::vector< int > ti_bin
Definition: colvarbias.h:375
std::string grid_conf
Grid configuration parameters (also used by grids in derived classes)
Definition: colvarbias.h:365
virtual int set_state_params(std::string const &state_conf)
Read the values of specific mutable properties from a string.
Definition: colvarbias.cpp:978
std::shared_ptr< colvar_grid_gradient > ti_avg_forces
Averaged system forces.
Definition: colvarbias.h:368
Collective variable bias, base class.
Definition: colvarbias.h:23
virtual int init_dependencies()
Initialize dependency tree.
Definition: colvarbias.cpp:180
virtual int set_state_params(std::string const &state_conf)
Read the values of specific mutable properties from a string.
Definition: colvarbias.cpp:550
virtual int change_configuration(std::string const &conf)
Load new configuration - force constant and/or centers only.
Definition: colvarbias.cpp:463
int communicate_forces()
Send forces to the collective variables.
Definition: colvarbias.cpp:420
colvarbias(colvarbias &)
Copy constructor.
bool matching_state
Flag used to tell if the state string being read is for this bias.
Definition: colvarbias.h:323
virtual int calc_energy(std::vector< colvarvalue > const *values)
Definition: colvarbias.cpp:404
cvm::step_number state_file_step
Step number read from the last state file.
Definition: colvarbias.h:320
int add_colvar(std::string const &cv_name)
Add a new collective variable to this bias.
Definition: colvarbias.cpp:339
IST & read_state_template_(IST &is)
Generic stream reading function (formatted and not)
Definition: colvarbias.cpp:602
virtual int end_of_step()
Carry out operations needed before next step is run.
Definition: colvarbias.cpp:457
virtual int write_state_to_replicas()
If this bias is communicating with other replicas through files, send it to them.
Definition: colvarbias.h:264
virtual std::ostream & write_state_data(std::ostream &os)
Write all mutable data not already written by get_state_params() to a formatted stream.
Definition: colvarbias.h:153
virtual int local_sample_count(int radius)
Return the average number of samples in a given "radius" around current bin.
Definition: colvarbias.cpp:498
virtual std::istream & read_state_data(std::istream &is)
Read all mutable data not already set by set_state_params() from a formatted stream.
Definition: colvarbias.h:165
virtual std::ostream & write_traj(std::ostream &os)
Output quantities such as the bias energy to the trajectory file.
Definition: colvarbias.cpp:801
cvm::real bias_energy
Current energy of this bias (colvar_forces should be obtained by deriving this)
Definition: colvarbias.h:310
size_t num_variables() const
How many variables are defined for this bias.
Definition: colvarbias.h:42
virtual int init_mts(std::string const &conf)
Initialize multiple-time-stepping options.
Definition: colvarbias.cpp:149
virtual int calc_forces(std::vector< colvarvalue > const *values)
Definition: colvarbias.cpp:411
int read_state_prefix(std::string const &prefix)
Read the bias state from a file with this name or prefix.
Definition: colvarbias.cpp:704
int write_state_prefix(std::string const &prefix)
Write the bias state to a file with the given prefix.
Definition: colvarbias.cpp:675
int check_matching_state(std::string const &conf)
Check the name of the bias vs. the given string, set the matching_state flag accordingly.
Definition: colvarbias.cpp:525
virtual int clear_state_data()
Delete only the allocatable data (save memory)
Definition: colvarbias.cpp:332
virtual cvm::memory_stream & read_state_data(cvm::memory_stream &is)
Read all mutable data not already set by set_state_params() from an unformatted stream.
Definition: colvarbias.h:171
virtual int current_bin()
Calculate the bin index for a given bias.
Definition: colvarbias.cpp:486
virtual int bin_num()
Give the total number of bins for a given bias.
Definition: colvarbias.cpp:480
virtual std::string const get_state_params() const
Write the values of specific mutable properties to a string.
Definition: colvarbias.cpp:516
virtual cvm::memory_stream & write_state_data(cvm::memory_stream &os)
Write all mutable data not already written by get_state_params() to an unformatted stream.
Definition: colvarbias.h:159
virtual int write_output_files()
Write any output files that this bias may have (e.g. PMF files)
Definition: colvarbias.h:255
virtual cvm::real energy_difference(std::string const &conf)
Calculate change in energy from using alternate configuration.
Definition: colvarbias.cpp:471
colvar * variables(int i) const
Access the i-th variable.
Definition: colvarbias.h:54
virtual bool can_accumulate_data()
Definition: colvarbias.cpp:393
std::vector< colvarvalue > previous_colvar_forces
Forces last applied by this bias to the variables.
Definition: colvarbias.h:307
std::istream & read_state(std::istream &is)
Read the bias configuration from a formatted stream.
Definition: colvarbias.cpp:663
size_t output_freq
Frequency for writing output files.
Definition: colvarbias.h:252
std::vector< colvarvalue > colvar_values
Up to date value of each colvar.
Definition: colvarbias.h:301
virtual void analyze()
Perform analysis tasks.
Definition: colvarbias.h:107
virtual const std::vector< feature * > & features() const
Implementation of the feature list accessor for colvarbias.
Definition: colvarbias.h:278
std::vector< colvar * > colvars
Pointers to collective variables to which the bias is applied; current values and metric functions wi...
Definition: colvarbias.h:298
std::string name
Name of this bias.
Definition: colvarbias.h:27
bool has_data
Whether this bias has already accumulated information (for history-dependent biases)
Definition: colvarbias.h:317
virtual std::ostream & write_traj_label(std::ostream &os)
Write a label to the trajectory file (comment line)
Definition: colvarbias.cpp:791
virtual int setup_output()
(Re)initialize the output files (does not write them yet)
Definition: colvarbias.h:246
colvarbias()
Default constructor.
Definition: colvarbias.cpp:280
std::ostream & write_state(std::ostream &os)
Write the bias configuration to a formatted stream.
Definition: colvarbias.cpp:559
std::ostream & write_state_data_key(std::ostream &os, std::string const &key, bool header=true) const
Definition: colvarbias.cpp:745
std::vector< colvar * > * variables()
Access the variables vector.
Definition: colvarbias.h:48
IST & read_state_data_key_template_(IST &is, std::string const &key)
Definition: colvarbias.cpp:762
std::string state_keyword
Keyword used in state files (== bias_type most of the time)
Definition: colvarbias.h:33
virtual int init(std::string const &conf)
Parse config string and (re)initialize.
Definition: colvarbias.cpp:44
int write_state_string(std::string &output)
Write the bias state to a string.
Definition: colvarbias.cpp:692
int read_state_string(char const *buffer)
Read the bias state from this string buffer.
Definition: colvarbias.cpp:722
std::string bias_type
Keyword indicating the type of this bias.
Definition: colvarbias.h:30
virtual int clear()
Delete everything.
Definition: colvarbias.cpp:291
bool b_output_energy
Whether to write the current bias energy from this bias to the trajectory file.
Definition: colvarbias.h:313
int rank
Track how many times a bias of this type was defined.
Definition: colvarbias.h:36
std::vector< colvarvalue > colvar_forces
Current forces from this bias to the variables.
Definition: colvarbias.h:304
std::istream & read_state_data_key(std::istream &is, std::string const &key)
Definition: colvarbias.cpp:779
virtual ~colvarbias()
Destructor.
Definition: colvarbias.cpp:285
virtual int update()
Definition: colvarbias.cpp:371
virtual size_t replica_share_freq() const
Report the frequency at which this bias needs to communicate with replicas.
Definition: colvarbias.cpp:510
virtual int reset()
Set to zero all mutable data.
Definition: colvarbias.cpp:270
static std::vector< feature * > cvb_features
Implementation of the feature list for colvarbias.
Definition: colvarbias.h:275
std::string output_prefix
Use this prefix for all output files.
Definition: colvarbias.h:261
colvar_grid_scalar * biasing_force_scaling_factors
The biasing forces will be scaled by the factor in this grid if b_bias_force_scaled is true.
Definition: colvarbias.h:327
Parent class for a member object of a bias, cv or cvc etc. containing features and their dependencies...
Definition: colvardeps.h:34
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:150
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:147
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
Definition: colvars_memstream.h:30
Parsing functions for collective variables.