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 <memory>
19
20#include "colvarproxy_namd_version.h"
21
22// For NAMD_UNIFIED_REDUCTION and AtomIDList
23#include "NamdTypes.h"
24// For CMK_SMP && USE_CKLOOP
25#include "Node.h"
26
27#include "colvarmodule.h"
28#include "colvarproxy.h"
29#include "colvarvalue.h"
30
31
32class Controller;
34class Random;
35class SimParameters;
36
37
40
41protected:
42
45
48 std::vector<int> atoms_map;
49
51 SimParameters *simparams;
52
54 Controller const *controller;
55
57 std::unique_ptr<Random> random;
58
59 bool first_timestep;
60 cvm::step_number previous_NAMD_step;
61
63#if !defined (NAMD_UNIFIED_REDUCTION)
64 SubmitReduction *reduction;
65#endif
66#if defined(NODEGROUP_FORCE_REGISTER) && !defined(NAMD_UNIFIED_REDUCTION)
67 NodeReduction *nodeReduction;
68#endif
69
72 cvm::real amd_weight_factor;
73 void update_accelMD_info();
74
75public:
76
77 void init_tcl_pointers() override;
78
81
82 int setup() override;
83 int reset() override;
84
87
90
91 // synchronize the local arrays with requested or forced atoms
92 int update_atoms_map(AtomIDList::const_iterator begin,
93 AtomIDList::const_iterator end);
94
95 void calculate();
96
97 void log(std::string const &message) override;
98 void error(std::string const &message) override;
99 int set_unit_system(std::string const &units_in, bool check_only) override;
100 void add_energy(cvm::real energy) override;
101 void request_total_force(bool yesno) override;
102
103 bool total_forces_enabled() const override
104 {
106 }
107
108 int run_force_callback() override;
109 int run_colvar_callback(std::string const &name,
110 std::vector<const colvarvalue *> const &cvcs,
111 colvarvalue &value) override;
112 int run_colvar_gradient_callback(std::string const &name,
113 std::vector<const colvarvalue *> const &cvcs,
114 std::vector<cvm::matrix2d<cvm::real> > &gradient) override;
115
117
119
120 bool accelMD_enabled() const override;
121
122#if CMK_SMP && USE_CKLOOP
123 colvarproxy::smp_mode_t get_smp_mode() const override;
124
125 int set_smp_mode(smp_mode_t mode) override;
126
127 smp_mode_t get_preferred_smp_mode() const override {
128 return smp_mode_t::cvcs;
129 }
130 std::vector<smp_mode_t> get_available_smp_modes() const override {
131 std::vector<colvarproxy_smp::smp_mode_t> available_modes{
132 smp_mode_t::cvcs,
133 smp_mode_t::inner_loop,
134 smp_mode_t::none
135 };
136 return available_modes;
137 }
138
139 int smp_loop(int n_items, std::function<int (int)> const &worker) override;
140
141 int smp_biases_loop() override;
142
143 int smp_biases_script_loop() override;
144
145 friend void calc_colvars_items_smp(int first, int last, void *result, int paramNum, void *param);
146 friend void calc_cv_biases_smp(int first, int last, void *result, int paramNum, void *param);
147 friend void calc_cv_scripted_forces(int paramNum, void *param);
148
149 int smp_thread_id()
150 {
151 return CkMyRank();
152 }
153
154 int smp_num_threads()
155 {
156 return CkMyNodeSize();
157 }
158
159protected:
160
161 CmiNodeLock charm_lock_state;
162
163public:
164
165 int smp_lock()
166 {
167 CmiLock(charm_lock_state);
168 return COLVARS_OK;
169 }
170
171 int smp_trylock()
172 {
173 const int ret = CmiTryLock(charm_lock_state);
174 if (ret == 0) return COLVARS_OK;
175 else return COLVARS_ERROR;
176 }
177
178 int smp_unlock()
179 {
180 CmiUnlock(charm_lock_state);
181 return COLVARS_OK;
182 }
183
184#endif // #if CMK_SMP && USE_CKLOOP
185
187 int replica_index() override;
188 int num_replicas() override;
189 void replica_comm_barrier() override;
190 int replica_comm_recv(char* msg_data, int buf_len, int src_rep) override;
191 int replica_comm_send(char* msg_data, int msg_len, int dest_rep) override;
192
194 int init_atom(int atom_number) override;
195 int check_atom_id(int atom_number) override;
196 int init_atom(cvm::residue_id const &residue,
197 std::string const &atom_name,
198 std::string const &segment_id) override;
199 int check_atom_id(cvm::residue_id const &residue,
200 std::string const &atom_name,
201 std::string const &segment_id) override;
202 void clear_atom(int index) override;
203
204 void update_atom_properties(int index);
205
207 cvm::atom_pos const &pos2) const;
208
209 int load_atoms_pdb(char const *filename,
210 cvm::atom_group &atoms,
211 std::string const &pdb_field,
212 double pdb_field_value) override;
213
214 int load_coords_pdb(char const *filename,
215 std::vector<cvm::atom_pos> &pos,
216 const std::vector<int> &indices,
217 std::string const &pdb_field,
218 double const pdb_field_value) override;
219
220
221 int scalable_group_coms() override
222 {
223 return COLVARS_OK;
224 }
225 int init_atom_group(std::vector<int> const &atoms_ids) override;
226 void clear_atom_group(int index) override;
227
228 int update_group_properties(int index);
229
230#if NAMD_VERSION_NUMBER >= 34471681
231
233
234 int init_volmap_by_id(int volmap_id) override;
235
236 int init_volmap_by_name(const char *volmap_name) override;
237
238 int check_volmap_by_id(int volmap_id) override;
239
240 int check_volmap_by_name(char const *volmap_name) override;
241
242 int get_volmap_id_from_name(char const *volmap_name) override;
243
244 void clear_volmap(int index) override;
245
246 int compute_volmap(int flags,
247 int volmap_id,
248 cvm::atom_group* ag,
249 cvm::real *value,
250 cvm::real *atom_field) override;
251
253 template<class T>
254 void getGridForceGridValue(int flags,
255 T const *grid,
256 cvm::atom_group* ag,
257 cvm::real *value,
258 cvm::real *atom_field);
259
261 template<class T, int flags>
262 void GridForceGridLoop(T const *g,
263 cvm::atom_group* ag,
264 cvm::real *value,
265 cvm::real *atom_field);
266
267#endif
268
269 std::ostream &output_stream(std::string const &output_name,
270 std::string const description) override;
271
272 int flush_output_stream(std::string const &output_name) override;
273
274 int flush_output_streams() override;
275
276 int close_output_stream(std::string const &output_name) override;
277
278 int close_output_streams() override;
279
280 int backup_file(char const *filename) override;
281
284
287
289 int request_alch_energy_freq(int const freq);
290
292 int get_dE_dlambda(cvm::real* dE_dlambda);
293
294};
295
296
297#endif
Definition: GlobalMasterColvars.h:11
Arbitrary size array (two dimensions) suitable for linear algebra operations (i.e....
Definition: colvartypes.h:375
vector of real numbers with three components
Definition: colvartypes.h:726
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:98
int residue_id
Residue identifier.
Definition: colvarmodule.h:235
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:95
std::vector< int > atoms_ids
Array of 0-based integers used to uniquely associate atoms within the host program.
Definition: colvarproxy.h:267
Communication between colvars and NAMD (implementation of colvarproxy)
Definition: colvarproxy_namd.h:39
cvm::real get_accelMD_factor() const override
Get weight factor from accelMD.
void init_tcl_pointers() override
Set Tcl pointers.
int request_alch_energy_freq(int const freq)
Request energy computation every freq steps.
GlobalMasterColvars * globalmaster
Pointer to the parent GlobalMaster object.
Definition: colvarproxy_namd.h:44
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:103
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:51
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:54
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.
std::unique_ptr< Random > random
NAMD-style PRNG object.
Definition: colvarproxy_namd.h:57
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.
int scalable_group_coms() override
Whether this proxy implementation has capability for scalable groups.
Definition: colvarproxy_namd.h:221
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:71
SubmitReduction * reduction
Used to submit restraint energy as MISC.
Definition: colvarproxy_namd.h:64
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:48
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:269
virtual std::vector< smp_mode_t > get_available_smp_modes() const
Get available SMP modes.
Definition: colvarproxy.cpp:273
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:341
virtual int smp_thread_id()
Index of this thread.
Definition: colvarproxy.cpp:370
virtual int smp_trylock()
Attempt to lock the proxy's shared data.
Definition: colvarproxy.cpp:399
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:390
virtual int set_smp_mode(smp_mode_t mode)
Set the current SMP mode.
Definition: colvarproxy.cpp:287
virtual int smp_num_threads()
Number of threads sharing this address space.
Definition: colvarproxy.cpp:380
virtual int smp_unlock()
Release the lock.
Definition: colvarproxy.cpp:409
virtual int smp_biases_loop()
Distribute calculation of biases across threads.
Definition: colvarproxy.cpp:318
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:299
virtual smp_mode_t get_preferred_smp_mode() const
Get the preferred SMP mode.
Definition: colvarproxy.cpp:283
bool total_force_requested
Whether the total forces have been requested.
Definition: colvarproxy_system.h:180
Definition: colvarproxy.h:564
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:52
Collective variables main module.
Colvars proxy classes.