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_dependencies();
117
119 virtual int reset();
120
121private:
122
124 colvarbias();
125
128
129public:
130
132 virtual int clear();
133
135 virtual int clear_state_data();
136
138 virtual ~colvarbias();
139
141 virtual std::string const get_state_params() const;
142
144 int check_matching_state(std::string const &conf);
145
147 virtual int set_state_params(std::string const &state_conf);
148
150 virtual std::ostream & write_state_data(std::ostream &os)
151 {
152 return os;
153 }
154
157 {
158 return os;
159 }
160
162 virtual std::istream & read_state_data(std::istream &is)
163 {
164 return is;
165 }
166
169 {
170 return is;
171 }
172
177 std::ostream &write_state_data_key(std::ostream &os, std::string const &key, bool header = true) const;
178
184 bool header = true) const;
185
186private:
187
191 template <typename IST> IST &read_state_data_key_template_(IST &is, std::string const &key);
192
193public:
194
198 std::istream & read_state_data_key(std::istream &is, std::string const &key);
199
203 cvm::memory_stream & read_state_data_key(cvm::memory_stream &is, std::string const &key);
204
205private:
206
208 template <typename IST> IST & read_state_template_(IST &is);
209
210public:
211
213 std::ostream &write_state(std::ostream &os);
214
217
219 std::istream & read_state(std::istream &is);
220
223
225 int write_state_prefix(std::string const &prefix);
226
228 int write_state_string(std::string &output);
229
231 int read_state_prefix(std::string const &prefix);
232
234 int read_state_string(char const *buffer);
235
237 virtual std::ostream & write_traj_label(std::ostream &os);
238
240 virtual std::ostream & write_traj(std::ostream &os);
241
243 virtual int setup_output()
244 {
245 return COLVARS_OK;
246 }
247
250
252 virtual int write_output_files()
253 {
254 return COLVARS_OK;
255 }
256
258 std::string output_prefix;
259
262 {
263 return COLVARS_OK;
264 }
265
266 inline cvm::real get_energy()
267 {
268 return bias_energy;
269 }
270
272 static std::vector<feature *> cvb_features;
273
275 virtual const std::vector<feature *> &features() const
276 {
277 return cvb_features;
278 }
279 virtual std::vector<feature *> &modify_features()
280 {
281 return cvb_features;
282 }
283 static void delete_features() {
284 for (size_t i=0; i < cvb_features.size(); i++) {
285 delete cvb_features[i];
286 }
287 cvb_features.clear();
288 }
289
290protected:
291
295 std::vector<colvar *> colvars;
296
298 std::vector<colvarvalue> colvar_values;
299
301 std::vector<colvarvalue> colvar_forces;
302
304 std::vector<colvarvalue> previous_colvar_forces;
305
308
311
315
318
321
325 std::vector<int> biasing_force_scaling_factors_bin;
326};
327
328
331
333class colvarbias_ti : public virtual colvarbias {
334public:
335
336 colvarbias_ti(char const *key);
337 virtual ~colvarbias_ti();
338
339 virtual int init(std::string const &conf);
340 virtual int init_grids();
341 virtual int update();
342
345 virtual int update_system_forces(std::vector<colvarvalue> const
346 *subtract_forces);
347
348 virtual std::string const get_state_params() const;
349 virtual int set_state_params(std::string const &state_conf);
350 virtual std::ostream & write_state_data(std::ostream &os);
352 virtual std::istream & read_state_data(std::istream &is);
354 virtual int write_output_files();
355
356protected:
357
359 std::vector<colvarvalue> ti_system_forces;
360
362 std::shared_ptr<colvar_grid_gradient> ti_avg_forces;
363
365 std::shared_ptr<colvar_grid_count> ti_count;
366
369 std::vector<int> ti_bin;
370};
371
372#endif
Colvar_grid derived class to hold counters in discrete n-dim colvar space.
Definition: colvargrid.h:959
Class for accumulating the gradient of a scalar function on a grid.
Definition: colvargrid.h:1555
Class for accumulating a scalar function on a grid.
Definition: colvargrid.h:1242
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:333
std::shared_ptr< colvar_grid_count > ti_count
Histogram of sampled data.
Definition: colvarbias.h:365
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:935
virtual std::string const get_state_params() const
Write the values of specific mutable properties to a string.
Definition: colvarbias.cpp:923
std::vector< colvarvalue > ti_system_forces
Forces exerted from the system to the associated variables.
Definition: colvarbias.h:359
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:961
virtual int update()
Definition: colvarbias.cpp:864
virtual int write_output_files()
Write any output files that this bias may have (e.g. PMF files)
Definition: colvarbias.cpp:1015
virtual int update_system_forces(std::vector< colvarvalue > const *subtract_forces)
Definition: colvarbias.cpp:870
virtual int init(std::string const &conf)
Parse config string and (re)initialize.
Definition: colvarbias.cpp:791
std::vector< int > ti_bin
Definition: colvarbias.h:369
virtual int set_state_params(std::string const &state_conf)
Read the values of specific mutable properties from a string.
Definition: colvarbias.cpp:929
std::shared_ptr< colvar_grid_gradient > ti_avg_forces
Averaged system forces.
Definition: colvarbias.h:362
Collective variable bias, base class.
Definition: colvarbias.h:23
virtual int init_dependencies()
Initialize dependency tree.
Definition: colvarbias.cpp:144
virtual int set_state_params(std::string const &state_conf)
Read the values of specific mutable properties from a string.
Definition: colvarbias.cpp:511
virtual int change_configuration(std::string const &conf)
Load new configuration - force constant and/or centers only.
Definition: colvarbias.cpp:424
int communicate_forces()
Send forces to the collective variables.
Definition: colvarbias.cpp:381
colvarbias(colvarbias &)
Copy constructor.
bool matching_state
Flag used to tell if the state string being read is for this bias.
Definition: colvarbias.h:320
virtual int calc_energy(std::vector< colvarvalue > const *values)
Definition: colvarbias.cpp:365
cvm::step_number state_file_step
Step number read from the last state file.
Definition: colvarbias.h:317
int add_colvar(std::string const &cv_name)
Add a new collective variable to this bias.
Definition: colvarbias.cpp:300
IST & read_state_template_(IST &is)
Generic stream reading function (formatted and not)
Definition: colvarbias.cpp:563
virtual int end_of_step()
Carry out operations needed before next step is run.
Definition: colvarbias.cpp:418
virtual int write_state_to_replicas()
If this bias is communicating with other replicas through files, send it to them.
Definition: colvarbias.h:261
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:150
virtual int local_sample_count(int radius)
Return the average number of samples in a given "radius" around current bin.
Definition: colvarbias.cpp:459
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:162
virtual std::ostream & write_traj(std::ostream &os)
Output quantities such as the bias energy to the trajectory file.
Definition: colvarbias.cpp:762
cvm::real bias_energy
Current energy of this bias (colvar_forces should be obtained by deriving this)
Definition: colvarbias.h:307
size_t num_variables() const
How many variables are defined for this bias.
Definition: colvarbias.h:42
virtual int calc_forces(std::vector< colvarvalue > const *values)
Definition: colvarbias.cpp:372
int read_state_prefix(std::string const &prefix)
Read the bias state from a file with this name or prefix.
Definition: colvarbias.cpp:665
int write_state_prefix(std::string const &prefix)
Write the bias state to a file with the given prefix.
Definition: colvarbias.cpp:636
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:486
virtual int clear_state_data()
Delete only the allocatable data (save memory)
Definition: colvarbias.cpp:293
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:168
virtual int current_bin()
Calculate the bin index for a given bias.
Definition: colvarbias.cpp:447
virtual int bin_num()
Give the total number of bins for a given bias.
Definition: colvarbias.cpp:441
virtual std::string const get_state_params() const
Write the values of specific mutable properties to a string.
Definition: colvarbias.cpp:477
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:156
virtual int write_output_files()
Write any output files that this bias may have (e.g. PMF files)
Definition: colvarbias.h:252
virtual cvm::real energy_difference(std::string const &conf)
Calculate change in energy from using alternate configuration.
Definition: colvarbias.cpp:432
colvar * variables(int i) const
Access the i-th variable.
Definition: colvarbias.h:54
virtual bool can_accumulate_data()
Definition: colvarbias.cpp:354
std::vector< colvarvalue > previous_colvar_forces
Forces last applied by this bias to the variables.
Definition: colvarbias.h:304
std::istream & read_state(std::istream &is)
Read the bias configuration from a formatted stream.
Definition: colvarbias.cpp:624
size_t output_freq
Frequency for writing output files.
Definition: colvarbias.h:249
std::vector< colvarvalue > colvar_values
Up to date value of each colvar.
Definition: colvarbias.h:298
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:275
std::vector< colvar * > colvars
Pointers to collective variables to which the bias is applied; current values and metric functions wi...
Definition: colvarbias.h:295
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:314
virtual std::ostream & write_traj_label(std::ostream &os)
Write a label to the trajectory file (comment line)
Definition: colvarbias.cpp:752
virtual int setup_output()
(Re)initialize the output files (does not write them yet)
Definition: colvarbias.h:243
colvarbias()
Default constructor.
Definition: colvarbias.cpp:241
std::ostream & write_state(std::ostream &os)
Write the bias configuration to a formatted stream.
Definition: colvarbias.cpp:520
std::ostream & write_state_data_key(std::ostream &os, std::string const &key, bool header=true) const
Definition: colvarbias.cpp:706
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:723
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:653
int read_state_string(char const *buffer)
Read the bias state from this string buffer.
Definition: colvarbias.cpp:683
std::string bias_type
Keyword indicating the type of this bias.
Definition: colvarbias.h:30
virtual int clear()
Delete everything.
Definition: colvarbias.cpp:252
bool b_output_energy
Whether to write the current bias energy from this bias to the trajectory file.
Definition: colvarbias.h:310
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:301
std::istream & read_state_data_key(std::istream &is, std::string const &key)
Definition: colvarbias.cpp:740
virtual ~colvarbias()
Destructor.
Definition: colvarbias.cpp:246
virtual int update()
Definition: colvarbias.cpp:332
virtual size_t replica_share_freq() const
Report the frequency at which this bias needs to communicate with replicas.
Definition: colvarbias.cpp:471
virtual int reset()
Set to zero all mutable data.
Definition: colvarbias.cpp:231
static std::vector< feature * > cvb_features
Implementation of the feature list for colvarbias.
Definition: colvarbias.h:272
std::string output_prefix
Use this prefix for all output files.
Definition: colvarbias.h:258
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:324
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:95
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:92
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.