Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarproxy_namd.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 COLVARPROXY_NAMD_H
11#define COLVARPROXY_NAMD_H
12
13#ifndef NAMD_VERSION_NUMBER
14// Assume 2.14b1 for now until the NAMD macro is merged
15#define NAMD_VERSION_NUMBER 34471681
16#endif
17
18#include "Vector.h"
19#include "ResizeArray.h"
20#include "NamdTypes.h"
21#include "SimParameters.h"
22#include "Lattice.h"
23#include "GlobalMaster.h"
24#include "Random.h"
25#include "ConfigList.h"
26
27#include "colvarmodule.h"
28#include "colvarproxy.h"
29#include "colvarvalue.h"
30
31#define GLOBAL_MASTER_CKLOOP_CALC_ITEM 2000
32#define GLOBAL_MASTER_CKLOOP_CALC_BIASES 2001
33#define GLOBAL_MASTER_CKLOOP_CALC_SCRIPTED_BIASES 2002
34
37class colvarproxy_namd : public colvarproxy, public GlobalMaster {
38
39protected:
40
43 std::vector<int> atoms_map;
44
46 SimParameters *simparams;
47
49 Controller const *controller;
50
52 Random random;
53
54 bool first_timestep;
55 cvm::step_number previous_NAMD_step;
56
58#if !defined (NAMD_UNIFIED_REDUCTION)
59 SubmitReduction *reduction;
60#endif
61#if defined(NODEGROUP_FORCE_REGISTER) && !defined(NAMD_UNIFIED_REDUCTION)
62 NodeReduction *nodeReduction;
63#endif
64
67 cvm::real amd_weight_factor;
68 void update_accelMD_info();
69
70public:
71
72 void init_tcl_pointers() override;
73
76
77 int setup() override;
78 int reset() override;
79
82
85
86 // synchronize the local arrays with requested or forced atoms
87 int update_atoms_map(AtomIDList::const_iterator begin,
88 AtomIDList::const_iterator end);
89
90 void calculate();
91
92 void log(std::string const &message) override;
93 void error(std::string const &message) override;
94 int set_unit_system(std::string const &units_in, bool check_only) override;
95 void add_energy(cvm::real energy) override;
96 void request_total_force(bool yesno) override;
97
98 bool total_forces_enabled() const override
99 {
101 }
102
103 int run_force_callback() override;
104 int run_colvar_callback(std::string const &name,
105 std::vector<const colvarvalue *> const &cvcs,
106 colvarvalue &value) override;
107 int run_colvar_gradient_callback(std::string const &name,
108 std::vector<const colvarvalue *> const &cvcs,
109 std::vector<cvm::matrix2d<cvm::real> > &gradient) override;
110
112 {
113 return random.gaussian();
114 }
115
116 cvm::real get_accelMD_factor() const override {
117 return amd_weight_factor;
118 }
119
120 bool accelMD_enabled() const override {
121 return accelMDOn;
122 }
123
124#if CMK_SMP && USE_CKLOOP
125 colvarproxy::smp_mode_t get_smp_mode() const override;
126
127 int set_smp_mode(smp_mode_t mode) override;
128
129 int smp_loop(int n_items, std::function<int (int)> const &worker) override;
130
131 int smp_biases_loop() override;
132
133 int smp_biases_script_loop() override;
134
135 friend void calc_colvars_items_smp(int first, int last, void *result, int paramNum, void *param);
136 friend void calc_cv_biases_smp(int first, int last, void *result, int paramNum, void *param);
137 friend void calc_cv_scripted_forces(int paramNum, void *param);
138
139 int smp_thread_id()
140 {
141 return CkMyRank();
142 }
143
144 int smp_num_threads()
145 {
146 return CkMyNodeSize();
147 }
148
149protected:
150
151 CmiNodeLock charm_lock_state;
152
153public:
154
155 int smp_lock()
156 {
157 CmiLock(charm_lock_state);
158 return COLVARS_OK;
159 }
160
161 int smp_trylock()
162 {
163 const int ret = CmiTryLock(charm_lock_state);
164 if (ret == 0) return COLVARS_OK;
165 else return COLVARS_ERROR;
166 }
167
168 int smp_unlock()
169 {
170 CmiUnlock(charm_lock_state);
171 return COLVARS_OK;
172 }
173
174#endif // #if CMK_SMP && USE_CKLOOP
175
177 int replica_index() override;
178 int num_replicas() override;
179 void replica_comm_barrier() override;
180 int replica_comm_recv(char* msg_data, int buf_len, int src_rep) override;
181 int replica_comm_send(char* msg_data, int msg_len, int dest_rep) override;
182
184 int init_atom(int atom_number) override;
185 int check_atom_id(int atom_number) override;
186 int init_atom(cvm::residue_id const &residue,
187 std::string const &atom_name,
188 std::string const &segment_id) override;
189 int check_atom_id(cvm::residue_id const &residue,
190 std::string const &atom_name,
191 std::string const &segment_id) override;
192 void clear_atom(int index) override;
193
194 void update_atom_properties(int index);
195
197 cvm::atom_pos const &pos2) const;
198
199 int load_atoms_pdb(char const *filename,
200 cvm::atom_group &atoms,
201 std::string const &pdb_field,
202 double pdb_field_value) override;
203
204 int load_coords_pdb(char const *filename,
205 std::vector<cvm::atom_pos> &pos,
206 const std::vector<int> &indices,
207 std::string const &pdb_field,
208 double const pdb_field_value) override;
209
210
211 int scalable_group_coms() override
212 {
213 return COLVARS_OK;
214 }
215 int init_atom_group(std::vector<int> const &atoms_ids) override;
216 void clear_atom_group(int index) override;
217
218 int update_group_properties(int index);
219
220#if NAMD_VERSION_NUMBER >= 34471681
221
223
224 int init_volmap_by_id(int volmap_id) override;
225
226 int init_volmap_by_name(const char *volmap_name) override;
227
228 int check_volmap_by_id(int volmap_id) override;
229
230 int check_volmap_by_name(char const *volmap_name) override;
231
232 int get_volmap_id_from_name(char const *volmap_name) override;
233
234 void clear_volmap(int index) override;
235
236 int compute_volmap(int flags,
237 int volmap_id,
238 cvm::atom_group* ag,
239 cvm::real *value,
240 cvm::real *atom_field) override;
241
243 template<class T>
244 void getGridForceGridValue(int flags,
245 T const *grid,
246 cvm::atom_group* ag,
247 cvm::real *value,
248 cvm::real *atom_field);
249
251 template<class T, int flags>
252 void GridForceGridLoop(T const *g,
253 cvm::atom_group* ag,
254 cvm::real *value,
255 cvm::real *atom_field);
256
257#endif
258
259 std::ostream &output_stream(std::string const &output_name,
260 std::string const description) override;
261
262 int flush_output_stream(std::string const &output_name) override;
263
264 int flush_output_streams() override;
265
266 int close_output_stream(std::string const &output_name) override;
267
268 int close_output_streams() override;
269
270 int backup_file(char const *filename) override;
271
274
277
279 int request_alch_energy_freq(int const freq);
280
282 int get_dE_dlambda(cvm::real* dE_dlambda);
283
284};
285
286
287#endif
Arbitrary size array (two dimensions) suitable for linear algebra operations (i.e....
Definition: colvartypes.h:373
vector of real numbers with three components
Definition: colvartypes.h:724
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:139
int residue_id
Residue identifier.
Definition: colvarmodule.h:264
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:136
std::vector< int > atoms_ids
Array of 0-based integers used to uniquely associate atoms within the host program.
Definition: colvarproxy.h:266
Communication between colvars and NAMD (implementation of colvarproxy)
Definition: colvarproxy_namd.h:37
cvm::real get_accelMD_factor() const override
Get weight factor from accelMD.
Definition: colvarproxy_namd.h:116
void init_tcl_pointers() override
Set Tcl pointers.
int request_alch_energy_freq(int const freq)
Request energy computation every freq steps.
int init_volmap_by_id(int volmap_id) override
int check_volmap_by_id(int volmap_id) override
int replica_index() override
Index of this replica.
int init_atom(cvm::residue_id const &residue, std::string const &atom_name, std::string const &segment_id) override
int load_atoms_pdb(char const *filename, cvm::atom_group &atoms, std::string const &pdb_field, double pdb_field_value) override
Read a selection of atom IDs from a PDB coordinate file.
bool total_forces_enabled() const override
Are total forces being used?
Definition: colvarproxy_namd.h:98
Random random
NAMD-style PRNG object.
Definition: colvarproxy_namd.h:52
void clear_atom(int index) override
Used by the atom class destructor: rather than deleting the array slot (costly) set the corresponding...
int init_atom(int atom_number) override
void error(std::string const &message) override
Print a message to the main log and/or let the host code know about it.
int update_target_temperature()
Get the target temperature from the NAMD thermostats supported so far.
int init_volmap_by_name(const char *volmap_name) override
int replica_comm_send(char *msg_data, int msg_len, int dest_rep) override
Send data to other replica.
int send_alch_lambda(void)
Set value of alchemical lambda parameter in back-end.
void add_energy(cvm::real energy) override
Pass restraint energy value for current timestep to MD engine.
void clear_atom_group(int index) override
Used by the atom_group class destructor.
int init_atom_group(std::vector< int > const &atoms_ids) override
Prepare this group for collective variables calculation, selecting atoms by internal ids (0-based)
SimParameters * simparams
Pointer to the NAMD simulation input object.
Definition: colvarproxy_namd.h:46
void init_atoms_map()
Allocate an atoms map with the same size as the NAMD topology.
Controller const * controller
Pointer to Controller object.
Definition: colvarproxy_namd.h:49
int load_coords_pdb(char const *filename, std::vector< cvm::atom_pos > &pos, const std::vector< int > &indices, std::string const &pdb_field, double const pdb_field_value) override
Load a set of coordinates from a PDB file.
int setup() override
(Re)initialize required member data (called after the module)
int backup_file(char const *filename) override
Rename the given file, before overwriting it.
int set_unit_system(std::string const &units_in, bool check_only) override
Request to set the units used internally by Colvars.
int num_replicas() override
Total number of replicas.
int get_volmap_id_from_name(char const *volmap_name) override
Get the numeric ID of the given volumetric map (for the MD program)
int check_atom_id(int atom_number) override
int compute_volmap(int flags, int volmap_id, cvm::atom_group *ag, cvm::real *value, cvm::real *atom_field) override
Re-weigh an atomic field (e.g. a colvar) by the value of a volumetric map.
int check_replicas_enabled() override
Indicate if multi-replica support is available and active.
std::ostream & output_stream(std::string const &output_name, std::string const description) override
int close_output_stream(std::string const &output_name) override
Closes the given output file/channel.
int get_dE_dlambda(cvm::real *dE_dlambda)
Get energy derivative with respect to lambda.
int flush_output_streams() override
Flushes all output files/channels.
int check_volmap_by_name(char const *volmap_name) override
int check_volmaps_available() override
Test whether this implementation can use volumetric maps as CVs.
cvm::real rand_gaussian() override
Pseudo-random number with Gaussian distribution.
Definition: colvarproxy_namd.h:111
int scalable_group_coms() override
Whether this proxy implementation has capability for scalable groups.
Definition: colvarproxy_namd.h:211
int run_force_callback() override
Run a user-defined colvar forces script.
void log(std::string const &message) override
Print a message to the main log.
void replica_comm_barrier() override
Synchronize replica with others.
int check_atom_id(cvm::residue_id const &residue, std::string const &atom_name, std::string const &segment_id) override
Check that this atom is valid, but do not initialize it yet.
int reset() override
Reset proxy state, e.g. requested atoms.
void GridForceGridLoop(T const *g, cvm::atom_group *ag, cvm::real *value, cvm::real *atom_field)
Implementation of inner loop; allows for atom list computation and use.
bool accelMDOn
Accelerated MD reweighting factor.
Definition: colvarproxy_namd.h:66
SubmitReduction * reduction
Used to submit restraint energy as MISC.
Definition: colvarproxy_namd.h:59
void clear_volmap(int index) override
Used by the CVC destructors.
void getGridForceGridValue(int flags, T const *grid, cvm::atom_group *ag, cvm::real *value, cvm::real *atom_field)
Abstraction of the two types of NAMD volumetric maps.
void request_total_force(bool yesno) override
Tell the proxy whether total forces are needed (they may not always be available)
int flush_output_stream(std::string const &output_name) override
Flushes the given output file/channel.
cvm::rvector position_distance(cvm::atom_pos const &pos1, cvm::atom_pos const &pos2) const
Get the PBC-aware distance vector between two positions.
std::vector< int > atoms_map
Array of atom indices (relative to the colvarproxy arrays), usedfor faster copy of atomic data.
Definition: colvarproxy_namd.h:43
int get_alch_lambda(cvm::real *lambda)
Get value of alchemical lambda parameter from back-end.
int close_output_streams() override
Close all open files/channels to prevent data loss.
int check_atom_name_selections_available() override
Check whether it is possible to select atoms by residue number name.
int replica_comm_recv(char *msg_data, int buf_len, int src_rep) override
Receive data from other replica.
virtual smp_mode_t get_smp_mode() const
Get the current SMP mode.
Definition: colvarproxy.cpp:268
virtual int smp_biases_script_loop()
Distribute calculation of biases across threads 2nd through last, with all scripted biased on 1st thr...
Definition: colvarproxy.cpp:333
virtual int smp_thread_id()
Index of this thread.
Definition: colvarproxy.cpp:362
virtual int smp_trylock()
Attempt to lock the proxy's shared data.
Definition: colvarproxy.cpp:391
virtual int smp_lock()
Lock the proxy's shared data for access by a thread, if threads are implemented; if not implemented,...
Definition: colvarproxy.cpp:382
virtual int set_smp_mode(smp_mode_t mode)
Set the current SMP mode.
Definition: colvarproxy.cpp:276
virtual int smp_num_threads()
Number of threads sharing this address space.
Definition: colvarproxy.cpp:372
virtual int smp_unlock()
Release the lock.
Definition: colvarproxy.cpp:401
virtual int smp_biases_loop()
Distribute calculation of biases across threads.
Definition: colvarproxy.cpp:310
virtual int smp_loop(int n_items, std::function< int(int)> const &worker)
Distribute computation over threads using OpenMP, unless overridden in the backend (e....
Definition: colvarproxy.cpp:291
bool total_force_requested
Whether the total forces have been requested.
Definition: colvarproxy_system.h:180
Definition: colvarproxy.h:559
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
Store the information of a group of atoms in a structure-of-arrays (SoA) style.
Definition: colvaratoms.h:51
Collective variables main module.
Colvars proxy classes.