Collective Variables Module - Developer Documentation
colvarproxy_namd.h
1 // -*- c++ -*-
2 
3 #ifndef COLVARPROXY_NAMD_H
4 #define COLVARPROXY_NAMD_H
5 
6 #include "colvarproxy_namd_version.h"
7 
8 #include "Vector.h"
9 #include "ResizeArray.h"
10 #include "NamdTypes.h"
11 #include "SimParameters.h"
12 #include "Lattice.h"
13 #include "GlobalMaster.h"
14 #include "Random.h"
15 
16 #include "colvarmodule.h"
17 #include "colvarproxy.h"
18 #include "colvarvalue.h"
19 
20 // For replica exchange
21 #include "converse.h"
22 #include "DataExchanger.h"
23 
26 class colvarproxy_namd : public colvarproxy, public GlobalMaster {
27 
28 protected:
29 
32  std::vector<int> atoms_map;
33 
36 
39 
41  Random random;
42 
45 
46  bool first_timestep;
47  size_t previous_NAMD_step;
48 
49  bool total_force_requested;
50 
52  SubmitReduction *reduction;
53 
54 #ifdef NAMD_TCL
55  Tcl_Interp *interp; // Tcl interpreter embedded in NAMD
56 #endif
57 
58 public:
59 
60  friend class cvm::atom;
61 
64 
65  int setup();
66  int reset();
67 
68  // synchronize the local arrays with requested or forced atoms
69  int update_atoms_map(AtomIDList::const_iterator begin, AtomIDList::const_iterator end);
70 
71  void calculate();
72 
73  void log(std::string const &message);
74  void error(std::string const &message);
75  void fatal_error(std::string const &message);
76  void exit(std::string const &message);
77  void add_energy(cvm::real energy);
78  void request_total_force(bool yesno);
79 
80  bool total_forces_enabled() const
81  {
82  return total_force_requested;
83  }
84 
85  int run_force_callback();
86  int run_colvar_callback(std::string const &name,
87  std::vector<const colvarvalue *> const &cvcs,
88  colvarvalue &value);
89  int run_colvar_gradient_callback(std::string const &name,
90  std::vector<const colvarvalue *> const &cvcs,
91  std::vector<cvm::matrix2d<cvm::real> > &gradient);
92 
94  {
95  return 1.0;
96  }
97 
99  {
100  return 0.001987191;
101  }
102 
104  {
105  return thermostat_temperature;
106  }
107 
109  {
110  return random.gaussian();
111  }
112 
114  {
115  return simparams->dt;
116  }
117 
118 #if CMK_SMP && USE_CKLOOP
119  int smp_enabled()
120  {
121  if (b_smp_active) {
122  return COLVARS_OK;
123  }
124  return COLVARS_ERROR;
125  }
126 
127  int smp_colvars_loop();
128 
129  int smp_biases_loop();
130 
132 
133  friend void calc_colvars_items_smp(int first, int last, void *result, int paramNum, void *param);
134  friend void calc_cv_biases_smp(int first, int last, void *result, int paramNum, void *param);
135  friend void calc_cv_scripted_forces(int paramNum, void *param);
136 
137  int smp_thread_id()
138  {
139  return CkMyRank();
140  }
141 
142  int smp_num_threads()
143  {
144  return CkMyNodeSize();
145  }
146 
147 protected:
148 
149  CmiNodeLock charm_lock_state;
150 
151 public:
152 
153  int smp_lock()
154  {
155  charm_lock_state = CmiCreateLock();
156  return COLVARS_OK;
157  }
158 
159  int smp_trylock()
160  {
161  return COLVARS_NOT_IMPLEMENTED;
162  }
163 
164  int smp_unlock()
165  {
166  CmiDestroyLock(charm_lock_state);
167  return COLVARS_OK;
168  }
169 
170 #endif // #if CMK_SMP && USE_CKLOOP
171 
172  // Replica communication functions.
174 #if CMK_HAS_PARTITION
175  return true;
176 #else
177  return false;
178 #endif
179  }
180 
182  return CmiMyPartition();
183  }
184 
185  int replica_num() {
186  return CmiNumPartitions();
187  }
188 
190  replica_barrier();
191  }
192 
193  int replica_comm_recv(char* msg_data, int buf_len, int src_rep) {
194  DataMessage *recvMsg = NULL;
195  replica_recv(&recvMsg, src_rep, CkMyPe());
196  CmiAssert(recvMsg != NULL);
197  int retval = recvMsg->size;
198  if (buf_len >= retval) {
199  memcpy(msg_data,recvMsg->data,retval);
200  } else {
201  retval = 0;
202  }
203  CmiFree(recvMsg);
204  return retval;
205  }
206 
207  int replica_comm_send(char* msg_data, int msg_len, int dest_rep) {
208  replica_send(msg_data, msg_len, dest_rep, CkMyPe());
209  return msg_len;
210  }
211 
212  int replica_comm_send()
213  {
214  return COLVARS_OK;
215  }
216 
217  int replica_comm_async_send()
218  {
219  return COLVARS_OK;
220  }
221 
222  inline size_t restart_frequency()
223  {
224  return restart_frequency_s;
225  }
226 
227  int init_atom(int atom_number);
228  int check_atom_id(int atom_number);
229  int init_atom(cvm::residue_id const &residue,
230  std::string const &atom_name,
231  std::string const &segment_id);
232  int check_atom_id(cvm::residue_id const &residue,
233  std::string const &atom_name,
234  std::string const &segment_id);
235  void clear_atom(int index);
236 
237  inline void update_atom_properties(int index)
238  {
239  Molecule *mol = Node::Object()->molecule;
240  // update mass
241  atoms_masses[index] = mol->atommass(atoms_ids[index]);
242  // update charge
243  atoms_charges[index] = mol->atomcharge(atoms_ids[index]);
244  }
245 
247  cvm::atom_pos const &pos2);
249  cvm::atom_pos const &pos2);
250 
251  void select_closest_image(cvm::atom_pos &pos,
252  cvm::atom_pos const &ref_pos);
253 
254 
255  int load_atoms(char const *filename,
256  cvm::atom_group &atoms,
257  std::string const &pdb_field,
258  double const pdb_field_value = 0.0);
259 
260  int load_coords(char const *filename,
261  std::vector<cvm::atom_pos> &pos,
262  const std::vector<int> &indices,
263  std::string const &pdb_field,
264  double const pdb_field_value = 0.0);
265 
266 
268  {
269  return COLVARS_OK;
270  }
271  int init_atom_group(std::vector<int> const &atoms_ids);
272  void clear_atom_group(int index);
273  int update_group_properties(int index);
274 
275  std::ostream * output_stream(std::string const &output_name,
276  std::ios_base::openmode mode);
277  int flush_output_stream(std::ostream *os);
278  int close_output_stream(std::string const &output_name);
279  int backup_file(char const *filename);
280 
281  char *script_obj_to_str(unsigned char *obj);
282 };
283 
284 
286  cvm::atom_pos const &pos2)
287 {
288  Position const p1(pos1.x, pos1.y, pos1.z);
289  Position const p2(pos2.x, pos2.y, pos2.z);
290  // return p2 - p1
291  Vector const d = this->lattice->delta(p2, p1);
292  return cvm::rvector(d.x, d.y, d.z);
293 }
294 
295 
297  cvm::atom_pos const &pos2)
298 {
299  Lattice const *l = this->lattice;
300  Vector const p1(pos1.x, pos1.y, pos1.z);
301  Vector const p2(pos2.x, pos2.y, pos2.z);
302  Vector const d = l->delta(p1, p2);
303  return cvm::real(d.x*d.x + d.y*d.y + d.z*d.z);
304 }
305 
306 
307 inline void colvarproxy_namd::select_closest_image(cvm::atom_pos &pos,
308  cvm::atom_pos const &ref_pos)
309 {
310  Position const p(pos.x, pos.y, pos.z);
311  Position const rp(ref_pos.x, ref_pos.y, ref_pos.z);
312  ScaledPosition const srp = this->lattice->scale(rp);
313  Position const np = this->lattice->nearest(p, srp);
314  pos.x = np.x;
315  pos.y = np.y;
316  pos.z = np.z;
317 }
318 
319 
320 
321 #endif
bool replica_enabled()
Indicate if multi-replica support is available and active.
Definition: colvarproxy_namd.h:173
static void exit(std::string const &message)
Print a message to the main log and exit normally.
std::ostream * output_stream(std::string const &output_name, std::ios_base::openmode mode)
Returns a reference to the given output channel; if this is not open already, then open it...
virtual int smp_num_threads()
Number of threads sharing this address space.
Definition: colvarproxy.cpp:322
cvm::real temperature()
Target temperature of the simulation (K units)
Definition: colvarproxy_namd.h:103
Random random
NAMD-style PRNG object.
Definition: colvarproxy_namd.h:41
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:283
Definition: SimParameters.h:100
int load_coords(char const *filename, std::vector< cvm::atom_pos > &pos, const std::vector< int > &indices, std::string const &pdb_field, double const pdb_field_value=0.0)
Load the coordinates for a group of atoms from a file (usually a PDB); if "pos" is already allocated...
cvm::real boltzmann()
Boltzmann constant.
Definition: colvarproxy_namd.h:98
Communication between colvars and NAMD (implementation of colvarproxy)
Definition: colvarproxy_namd.h:26
Stores numeric id, mass and all mutable data for an atom, mostly used by a cvc.
Definition: colvaratoms.h:21
int scalable_group_coms()
Whether this proxy implementation has capability for scalable groups.
Definition: colvarproxy_namd.h:267
int init_atom(int atom_number)
virtual int smp_trylock()
Attempt to lock the proxy&#39;s shared data.
Definition: colvarproxy.cpp:341
cvm::real dt()
Time step of the simulation (fs)
Definition: colvarproxy_namd.h:113
int close_output_stream(std::string const &output_name)
Closes the given output channel.
std::vector< cvm::real > atoms_masses
Masses of the atoms (allow redefinition during a run, as done e.g. in LAMMPS)
Definition: colvarproxy.h:222
vector of real numbers with three components
Definition: colvartypes.h:714
size_t restart_frequency_s
How often NAMD is instructed to write state files.
Definition: colvarproxy_namd.h:44
Colvars proxy classes.
cvm::real rand_gaussian()
Pseudo-random number with Gaussian distribution.
Definition: colvarproxy_namd.h:108
Interface between the collective variables module and the simulation or analysis program (NAMD...
Definition: colvarproxy.h:529
int reset()
Reset proxy state, e.g. requested atoms.
int init_atom_group(std::vector< int > const &atoms_ids)
Prepare this group for collective variables calculation, selecting atoms by internal ids (0-based) ...
Collective variables main module.
virtual int smp_biases_loop()
Distribute calculation of biases across threads.
Definition: colvarproxy.cpp:260
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:76
bool b_smp_active
Definition: colvarproxy.h:343
int index
Index in the colvarproxy arrays (NOT in the global topology!)
Definition: colvaratoms.h:26
std::vector< int > atoms_map
Array of atom indices (relative to the colvarproxy arrays), usedfor faster copy of atomic data...
Definition: colvarproxy_namd.h:32
void clear_atom(int index)
Used by the atom class destructor: rather than deleting the array slot (costly) set the corresponding...
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:34
void request_total_force(bool yesno)
Tell the proxy whether total forces are needed (may not always be available)
std::vector< int > atoms_ids
Array of 0-based integers used to uniquely associate atoms within the host program.
Definition: colvarproxy.h:218
std::vector< cvm::real > atoms_charges
Charges of the atoms (allow redefinition during a run, as done e.g. in LAMMPS)
Definition: colvarproxy.h:224
size_t restart_frequency()
Restarts will be written each time this number of steps has passed.
Definition: colvarproxy_namd.h:222
void log(std::string const &message)
Print a message to the main log.
SimParameters const * simparams
Pointer to the NAMD simulation input object.
Definition: colvarproxy_namd.h:35
bool total_forces_enabled() const
Are total forces being used?
Definition: colvarproxy_namd.h:80
void error(std::string const &message)
Print a message to the main log and let the rest of the program handle the error. ...
int replica_comm_recv(char *msg_data, int buf_len, int src_rep)
Receive data from other replica.
Definition: colvarproxy_namd.h:193
int replica_index()
Index of this replica.
Definition: colvarproxy_namd.h:181
int residue_id
Residue identifier.
Definition: colvarmodule.h:93
char * script_obj_to_str(unsigned char *obj)
Convert a script object (Tcl or Python call argument) to a C string.
void clear_atom_group(int index)
Used by the atom_group class destructor.
int setup()
(Re)initialize required member data after construction
cvm::rvector position_distance(cvm::atom_pos const &pos1, cvm::atom_pos const &pos2)
Get the PBC-aware distance vector between two positions.
Definition: colvarproxy_namd.h:285
cvm::real position_dist2(cvm::atom_pos const &pos1, cvm::atom_pos const &pos2)
Get the PBC-aware square distance between two positions; may need to be reimplemented independently f...
Definition: colvarproxy_namd.h:296
int check_atom_id(int atom_number)
virtual int smp_thread_id()
Index of this thread.
Definition: colvarproxy.cpp:312
void replica_comm_barrier()
Synchronize replica.
Definition: colvarproxy_namd.h:189
cvm::atom_pos pos
Current position (copied from the program, can be modified if necessary)
Definition: colvaratoms.h:41
virtual int smp_unlock()
Release the lock.
Definition: colvarproxy.cpp:352
BigReal thermostat_temperature
Self-explained.
Definition: colvarproxy_namd.h:38
int load_atoms(char const *filename, cvm::atom_group &atoms, std::string const &pdb_field, double const pdb_field_value=0.0)
Read atom identifiers from a file.
void add_energy(cvm::real energy)
Pass restraint energy value for current timestep to MD engine.
int flush_output_stream(std::ostream *os)
Flushes the given output channel.
virtual int smp_enabled()
Whether threaded parallelization is available (TODO: make this a cvm::deps feature) ...
Definition: colvarproxy.cpp:223
int replica_num()
Total number of replica.
Definition: colvarproxy_namd.h:185
SubmitReduction * reduction
Used to submit restraint energy as MISC.
Definition: colvarproxy_namd.h:52
int backup_file(char const *filename)
Rename the given file, before overwriting it.
int replica_comm_send(char *msg_data, int msg_len, int dest_rep)
Send data to other replica.
Definition: colvarproxy_namd.h:207
int run_force_callback()
Run a user-defined colvar forces script.
cvm::real unit_angstrom()
Value of the unit for atomic coordinates with respect to angstroms (used by some variables for hard-c...
Definition: colvarproxy_namd.h:93
virtual int smp_lock()
Lock the proxy&#39;s shared data for access by a thread, if threads are implemented; if not implemented...
Definition: colvarproxy.cpp:332
virtual int smp_colvars_loop()
Distribute calculation of colvars (and their components) across threads.
Definition: colvarproxy.cpp:236
void fatal_error(std::string const &message)
Print a message to the main log and exit with error code.
Group of atom objects, mostly used by a cvc object to gather all atomic data.
Definition: colvaratoms.h:142