Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarbias_opes.h
1#ifndef COLVARBIAS_OPES_H
2#define COLVARBIAS_OPES_H
3
4// This code is mainly adapted from the PLUMED opes module, which uses the
5// LGPLv3 license as shown below:
6/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 Copyright (c) 2020-2021 of Michele Invernizzi.
8
9 This file is part of the OPES plumed module.
10
11 The OPES plumed module is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 The OPES plumed module is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with plumed. If not, see <http://www.gnu.org/licenses/>.
23+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
24
25#include "colvarbias.h"
26
27#include <vector>
28#include <memory>
29
30// OPES_METAD implementation: swiped from OPESmetad.cpp of PLUMED
32public:
34 struct kernel {
35 cvm::real m_height;
36 std::vector<cvm::real> m_center;
37 std::vector<cvm::real> m_sigma;
38 kernel() {}
39 kernel(cvm::real h, const std::vector<cvm::real>& c,
40 const std::vector<cvm::real>& s):
41 m_height(h), m_center(c), m_sigma(s) {}
42 };
49 };
51 colvarbias_opes(char const *key);
53 int init(std::string const &conf) override;
55 int update() override;
57 std::ostream &write_state_data(std::ostream &os) override;
59 std::istream &read_state_data(std::istream &is) override;
65 int write_output_files() override;
66private:
67 int update_opes();
68 int calculate_opes();
69 void save_state();
70 cvm::real getProbAndDerivatives(const std::vector<cvm::real>& cv, std::vector<cvm::real>& der_prob) const;
71 cvm::real evaluateKernel(const kernel& G, const std::vector<cvm::real>& x) const;
72 cvm::real evaluateKernel(const kernel& G, const std::vector<cvm::real>& x, std::vector<cvm::real>& accumulated_derivative, std::vector<cvm::real>& dist) const;
73 void addKernel(const double height, const std::vector<cvm::real>& center, const std::vector<cvm::real>& sigma, const double logweight);
74 void addKernel(const double height, const std::vector<cvm::real>& center, const std::vector<cvm::real>& sigma);
75 size_t getMergeableKernel(const std::vector<cvm::real>& giver_center, const size_t giver_k) const;
76 void mergeKernels(kernel& k1, const kernel& k2) const;
77 void updateNlist(const std::vector<cvm::real>& center);
78 struct traj_line {
79 double rct;
80 double zed;
81 double neff;
82 double work;
83 size_t nker;
84 size_t nlker;
85 size_t nlsteps;
86 };
87 void writeTrajBuffer();
88 void showInfo() const;
89 template <typename OST> OST &write_state_data_template_(OST &os) const;
90 template <typename IST> IST &read_state_data_template_(IST &os);
91 std::string const traj_file_name(const std::string& suffix) const;
92 int collectSampleToPMFGrid();
93 int computePMF();
94 int writePMF(const std::unique_ptr<colvar_grid_scalar>& pmf_grid, const std::string &filename, bool keep_open);
95private:
96 cvm::real m_kbt;
97 cvm::real m_barrier;
98 cvm::real m_biasfactor;
99 cvm::real m_bias_prefactor;
100 cvm::real m_temperature;
101 cvm::step_number m_pace;
102 cvm::step_number m_adaptive_sigma_stride;
103 cvm::step_number m_adaptive_counter;
104 unsigned long long m_counter;
105 cvm::real m_compression_threshold;
106 cvm::real m_compression_threshold2;
107 bool m_adaptive_sigma;
108 bool m_fixed_sigma;
109 bool m_no_zed;
110 // bool m_restart;
111 bool m_nlist;
112 bool m_recursive_merge;
113 std::vector<cvm::real> m_nlist_param;
114 std::vector<cvm::real> m_sigma0;
115 std::vector<cvm::real> m_sigma_min;
116 cvm::real m_epsilon;
117 cvm::real m_sum_weights;
118 cvm::real m_sum_weights2;
119 cvm::real m_cutoff;
120 cvm::real m_cutoff2;
121 cvm::real m_zed;
122 cvm::real m_old_kdenorm;
123 cvm::real m_kdenorm;
124 cvm::real m_val_at_cutoff;
125 cvm::real m_rct;
126 cvm::real m_neff;
127 std::vector<kernel> m_kernels;
128 std::vector<kernel> m_delta_kernels;
129 std::vector<cvm::real> m_av_cv;
130 std::vector<cvm::real> m_av_M2;
131 std::ostringstream m_kernels_output;
132 std::vector<cvm::real> m_nlist_center;
133 std::vector<size_t> m_nlist_index;
134 std::vector<cvm::real> m_nlist_dev2;
135 size_t m_nlist_steps;
136 bool m_nlist_update;
137 bool m_nlist_pace_reset;
138 size_t m_nker;
139 bool m_calc_work;
140 cvm::real m_work;
144 std::string replica_id;
145 size_t m_num_walkers;
146 size_t shared_freq;
147 size_t m_num_threads;
148 size_t m_nlker;
149 // size_t m_state_stride;
150 // std::unordered_map<std::string, std::string> m_kernel_output_components;
151 std::string m_kernels_output_headers;
152 cvm::step_number m_traj_output_frequency;
153 traj_line m_traj_line;
154 std::ostringstream m_traj_oss;
155 bool m_is_first_step;
156 std::vector<cvm::real> m_cv;
157 // For saving states
158 decltype(m_zed) m_saved_zed;
159 decltype(m_sum_weights) m_saved_sum_weights;
160 decltype(m_sum_weights2) m_saved_sum_weights2;
161 decltype(m_kernels) m_saved_kernels;
162 // PMF grid from reweighting
163 bool m_pmf_grid_on;
164 std::vector<colvar*> m_pmf_cvs;
165 std::unique_ptr<colvar_grid_scalar> m_reweight_grid;
166 std::unique_ptr<colvar_grid_scalar> m_pmf_grid;
167 cvm::step_number m_pmf_hist_freq;
168 bool m_pmf_shared; // shared PMF among replicas
169 std::unique_ptr<colvar_grid_scalar> m_global_reweight_grid;
170 std::unique_ptr<colvar_grid_scalar> m_global_pmf_grid;
171 bool m_explore;
172};
173
174#endif // COLVARBIAS_OPES_H
Definition: colvarbias_opes.h:31
int init(std::string const &conf) override
Initializer.
Definition: colvarbias_opes.cpp:62
std::istream & read_state_data(std::istream &is) override
Read the state from a text file for restarting.
Definition: colvarbias_opes.cpp:1412
std::string replica_id
Identifier for this replica.
Definition: colvarbias_opes.h:144
Communication comm
Communication between different replicas.
Definition: colvarbias_opes.h:142
int write_output_files() override
Write to files at restart steps.
Definition: colvarbias_opes.cpp:1694
Communication
Communication between different replicas.
Definition: colvarbias_opes.h:44
@ multiple_replicas
Hills added concurrently by several replicas.
Definition: colvarbias_opes.h:48
@ single_replica
One replica (default)
Definition: colvarbias_opes.h:46
int update() override
Per-timestep update.
Definition: colvarbias_opes.cpp:1138
std::ostream & write_state_data(std::ostream &os) override
Save the state to a text file for restarting.
Definition: colvarbias_opes.cpp:1258
Collective variable bias, base class.
Definition: colvarbias.h:23
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
Definition: colvars_memstream.h:30
The Gaussian kernel data structure.
Definition: colvarbias_opes.h:34
Definition: colvarbias_opes.h:78