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#include <memory>
14
15#include "colvarproxy_namd_version.h"
16
17// For NAMD_UNIFIED_REDUCTION and AtomIDList
18#include "NamdTypes.h"
19// For CMK_SMP && USE_CKLOOP
20#include "Node.h"
21
22#include "colvarmodule.h"
23#include "colvarproxy.h"
24#include "colvarvalue.h"
25
26
27class Controller;
29class GridforceFullMainGrid;
30class Random;
31class SimParameters;
32
33
36public:
37
40
43
45 int init(); // no override
46
48 int init_module(); // no override
49
50 void init_tcl_pointers() override;
51 int setup() override;
52 int reset() override;
53
56
59
63 void request_gm_atom_by_id(int aid, int index);
64
66 int update_gm_atoms_map(AtomIDList::const_iterator begin, AtomIDList::const_iterator end);
67
70
73
76
79
82
83protected:
84
87
89 std::vector<int> gm_atoms_map;
90
92 SimParameters *simparams = nullptr;
93
95 Controller const *controller = nullptr;
96
98 std::unique_ptr<Random> random;
99
101 bool first_timestep = true;
102
105
108
110#if !defined (NAMD_UNIFIED_REDUCTION)
111 SubmitReduction *reduction;
112#endif
113#if defined(NODEGROUP_FORCE_REGISTER) && !defined(NAMD_UNIFIED_REDUCTION)
114 NodeReduction *nodeReduction;
115#endif
116
119 cvm::real amd_weight_factor;
120 void update_accelMD_info();
121
122public:
123
124 void calculate();
125
126 void log(std::string const &message) override;
127 void error(std::string const &message) override;
128 int set_unit_system(std::string const &units_in, bool check_only) override;
129 void add_energy(cvm::real energy) override;
130 void request_total_force(bool yesno) override;
131
132 bool total_forces_enabled() const override
133 {
135 }
136
137 int run_force_callback() override;
138 int run_colvar_callback(std::string const &name,
139 std::vector<const colvarvalue *> const &cvcs,
140 colvarvalue &value) override;
141 int run_colvar_gradient_callback(std::string const &name,
142 std::vector<const colvarvalue *> const &cvcs,
143 std::vector<cvm::matrix2d<cvm::real> > &gradient) override;
144
146
148
149 bool accelMD_enabled() const override;
150
151#if CMK_SMP && USE_CKLOOP
152 smp_mode_t get_preferred_smp_mode() const override {
153 return smp_mode_t::cvcs;
154 }
155 std::vector<smp_mode_t> get_available_smp_modes() const override {
156 std::vector<colvarproxy_smp::smp_mode_t> available_modes{
157 smp_mode_t::cvcs,
158 smp_mode_t::inner_loop,
159 smp_mode_t::none
160 };
161 return available_modes;
162 }
163
164 int smp_loop(int n_items, std::function<int (int)> const &worker) override;
165
166 int smp_biases_loop() override;
167
168 int smp_biases_script_loop() override;
169
170 friend void calc_colvars_items_smp(int first, int last, void *result, int paramNum, void *param);
171 friend void calc_cv_biases_smp(int first, int last, void *result, int paramNum, void *param);
172 friend void calc_cv_scripted_forces(int paramNum, void *param);
173
174 int smp_thread_id()
175 {
176 return CkMyRank();
177 }
178
179 int smp_num_threads()
180 {
181 return CkMyNodeSize();
182 }
183
184protected:
185
186 CmiNodeLock charm_lock_state;
187
188public:
189
190 int smp_lock()
191 {
192 CmiLock(charm_lock_state);
193 return COLVARS_OK;
194 }
195
196 int smp_trylock()
197 {
198 const int ret = CmiTryLock(charm_lock_state);
199 if (ret == 0) return COLVARS_OK;
200 else return COLVARS_ERROR;
201 }
202
203 int smp_unlock()
204 {
205 CmiUnlock(charm_lock_state);
206 return COLVARS_OK;
207 }
208
209#endif // #if CMK_SMP && USE_CKLOOP
210
212 int replica_index() override;
213 int num_replicas() override;
214 void replica_comm_barrier() override;
215 int replica_comm_recv(char* msg_data, int buf_len, int src_rep) override;
216 int replica_comm_send(char* msg_data, int msg_len, int dest_rep) override;
217
219 int init_atom(int atom_number) override;
220 int check_atom_id(int atom_number) override;
221 int init_atom(cvm::residue_id const &residue,
222 std::string const &atom_name,
223 std::string const &segment_id) override;
224 int check_atom_id(cvm::residue_id const &residue,
225 std::string const &atom_name,
226 std::string const &segment_id) override;
227 void clear_atom(int index) override;
228
229 void update_atom_properties(int index);
230
231 enum class e_pdb_field {
232 none,
233 occ,
234 beta,
235 x,
236 y,
237 z,
238 ntot
239 };
240
241 e_pdb_field pdb_field_str2enum(std::string const &pdb_field_str);
242
243 int load_atoms_pdb(char const *filename,
244 cvm::atom_group &atoms,
245 std::string const &pdb_field,
246 double pdb_field_value) override;
247
248 int load_coords_pdb(char const *filename,
249 std::vector<cvm::atom_pos> &pos,
250 const std::vector<int> &indices,
251 std::string const &pdb_field,
252 double const pdb_field_value) override;
253
254
256
257 int init_atom_group(std::vector<int> const &atoms_ids) override;
258 void clear_atom_group(int index) override;
259
260 int update_group_properties(int index);
261
263
265
266
268 int request_engine_volmap_by_id(int volmap_id) override;
269
271 int request_engine_volmap_by_name(std::string const &volmap_name) override;
272
274 void request_globalmaster_volmap(int volmap_id);
275
277 int init_internal_volmap_by_id(int volmap_id) override;
278
280 int init_internal_volmap_by_name(std::string const &volmap_name) override;
281
283 int load_internal_volmap_from_file(std::string const &filename) override;
284
285 int clear_volmap(int index) override;
286
287 int compute_volmap(int flags,
288 int index,
289 cvm::atom_group* ag,
290 cvm::real *value,
291 cvm::real *atom_field) override;
292
294 template<class T>
295 void getGridForceGridValue(int flags,
296 T const *grid,
297 cvm::atom_group* ag,
298 cvm::real *value,
299 cvm::real *atom_field);
300
302 template<class T, int flags>
303 void GridForceGridLoop(T const *g,
304 cvm::atom_group* ag,
305 cvm::real *value,
306 cvm::real *atom_field);
307
308 std::ostream &output_stream(std::string const &output_name,
309 std::string const description) override;
310
311 int flush_output_stream(std::string const &output_name) override;
312
313 int flush_output_streams() override;
314
315 int close_output_stream(std::string const &output_name) override;
316
317 int close_output_streams() override;
318
319 int backup_file(char const *filename) override;
320
323
326
328 int request_alch_energy_freq(int const freq);
329
331 int get_dE_dlambda(cvm::real* dE_dlambda);
332
333protected:
334
336 std::vector<std::unique_ptr<GridforceFullMainGrid>> internal_gridforce_grids_;
337};
338
339
340#endif
Definition: GlobalMasterColvars.h:11
Arbitrary size array (two dimensions) suitable for linear algebra operations (i.e....
Definition: colvartypes.h:376
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:99
int residue_id
Residue identifier.
Definition: colvarmodule.h:248
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:96
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:35
cvm::real get_accelMD_factor() const override
Get weight factor from accelMD.
void init_tcl_pointers() override
Set Tcl pointers.
void send_gm_atom_forces()
Send Colvars forces to GlobalMaster.
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:86
int clear_volmap(int index) override
Used by the CVC destructors.
int replica_index() override
Index of this replica.
int request_engine_volmap_by_id(int volmap_id) override
Select a MGridForces map for computation by NAMD.
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:132
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.
bool first_timestep
Use to distinguish between "run 0" and actual runs.
Definition: colvarproxy_namd.h:101
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:92
void request_globalmaster_volmap(int volmap_id)
Add map to GlobalMaster client (if not already in them)
cvm::step_number NAMD_step
Current NAMD simulation step (promoted from int)
Definition: colvarproxy_namd.h:104
int check_engine_volmaps_available() override
Test whether the engine supports offloaded computation of volumetric maps.
Controller const * controller
Pointer to Controller object.
Definition: colvarproxy_namd.h:95
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 init()
Initialize proxy data based on the chosen interface.
int setup() override
(Re)initialize required member data (called after the module)
int load_internal_volmap_from_file(std::string const &filename) override
Load a map internally independent from MGridForces.
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.
std::vector< int > gm_atoms_map
Map from NAMD atom indices to colvarproxy array indices (used by GlobalMaster)
Definition: colvarproxy_namd.h:89
void init_gm_atoms_map()
Create map from NAMD atom indices to colvarproxy array indices (used by GlobalMaster)
void set_gm_object(GlobalMasterColvars *gm)
Tell the proxy that it will be using the GlobalMaster interface.
int update_gm_atoms_map(AtomIDList::const_iterator begin, AtomIDList::const_iterator end)
Update map to reflect other requested atoms, including other GlobalMaster objects.
int check_atom_id(int atom_number) override
cvm::step_number previous_NAMD_step
Previous NAMD simulation step; used to test if the simulation is advancing.
Definition: colvarproxy_namd.h:107
int check_replicas_enabled() override
Indicate if multi-replica support is available and active.
int request_engine_volmap_by_name(std::string const &volmap_name) override
Select a MGridForces map for computation by NAMD.
std::vector< std::unique_ptr< GridforceFullMainGrid > > internal_gridforce_grids_
Pointers to internally managed maps (set to nullptr for maps loaded by NAMD)
Definition: colvarproxy_namd.h:336
std::ostream & output_stream(std::string const &output_name, std::string const description) override
int setup_gm_atom_group_buffers()
Create and zero out data buffers for atom groups requested through GlobalMaster.
int init_module()
Initialize the module.
int close_output_stream(std::string const &output_name) override
Closes the given output file/channel.
int setup_gm_volmap_buffers()
Create and zero out data buffers for grid objects requested through GlobalMaster.
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:98
int check_volmaps_available() override
Test whether the engine provides code to load volumetric maps as CVs.
cvm::real rand_gaussian() override
Pseudo-random number with Gaussian distribution.
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 read_gm_atom_buffers()
Read the current coordinates and total forces.
void replica_comm_barrier() override
Synchronize replica with others.
int setup_gm_atom_buffers()
Create and zero out data buffers for atoms requested through GlobalMaster.
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 compute_volmap(int flags, int index, 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 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:118
int check_scalable_group_coms() override
Check if this proxy supports offloaded computation of centers of mass (outside Colvars)
SubmitReduction * reduction
Used to submit restraint energy as MISC.
Definition: colvarproxy_namd.h:111
void request_gm_atom_by_id(int aid, int index)
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.
int get_alch_lambda(cvm::real *lambda)
Get value of alchemical lambda parameter from back-end.
int init_internal_volmap_by_id(int volmap_id) override
Select a MGridForces map for internal computation (frontend)
int init_internal_volmap_by_name(std::string const &volmap_name) override
Select a MGridForces map for internal computation (frontend)
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 std::vector< smp_mode_t > get_available_smp_modes() const
Get available SMP modes.
Definition: colvarproxy.cpp:285
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:352
virtual int smp_thread_id()
Index of this thread.
Definition: colvarproxy.cpp:380
virtual int smp_trylock()
Attempt to lock the proxy's shared data.
Definition: colvarproxy.cpp:409
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:400
virtual int smp_num_threads()
Number of threads sharing this address space.
Definition: colvarproxy.cpp:390
virtual int smp_unlock()
Release the lock.
Definition: colvarproxy.cpp:419
virtual int smp_biases_loop()
Distribute calculation of biases across threads.
Definition: colvarproxy.cpp:330
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:311
virtual smp_mode_t get_preferred_smp_mode() const
Get the preferred SMP mode.
Definition: colvarproxy.cpp:295
bool total_force_requested
Whether the total forces have been requested.
Definition: colvarproxy_system.h:182
Definition: colvarproxy.h:568
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.