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