Collective Variables Module - Developer Documentation
colvarproxy.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 #ifndef COLVARPROXY_H
4 #define COLVARPROXY_H
5 
6 #include <fstream>
7 #include <list>
8 
9 #include "colvarmodule.h"
10 #include "colvarvalue.h"
11 
12 
26 
27 
28 // forward declarations
29 class colvarscript;
30 
31 
34 
35 public:
36 
39 
41  virtual ~colvarproxy_system();
42 
45  virtual cvm::real unit_angstrom() = 0;
46 
48  virtual cvm::real boltzmann() = 0;
49 
51  virtual cvm::real temperature() = 0;
52 
54  virtual cvm::real dt() = 0;
55 
57  virtual cvm::real rand_gaussian(void) = 0;
58 
60  virtual void add_energy(cvm::real energy) = 0;
61 
63  virtual cvm::rvector position_distance(cvm::atom_pos const &pos1,
64  cvm::atom_pos const &pos2) = 0;
65 
68  virtual cvm::real position_dist2(cvm::atom_pos const &pos1,
69  cvm::atom_pos const &pos2);
70 
72  virtual void request_total_force(bool yesno);
73 
75  virtual bool total_forces_enabled() const;
76 
78  virtual bool total_forces_same_step() const;
79 };
80 
81 
84 
85 public:
86 
89 
91  virtual ~colvarproxy_atoms();
92 
95  virtual int init_atom(int atom_number) = 0;
96 
99  virtual int check_atom_id(int atom_number) = 0;
100 
104  virtual int init_atom(cvm::residue_id const &residue,
105  std::string const &atom_name,
106  std::string const &segment_id);
107 
109  virtual int check_atom_id(cvm::residue_id const &residue,
110  std::string const &atom_name,
111  std::string const &segment_id);
112 
115  virtual void clear_atom(int index);
116 
122  virtual int load_atoms(char const *filename,
123  cvm::atom_group &atoms,
124  std::string const &pdb_field,
125  double const pdb_field_value = 0.0);
126 
130  virtual int load_coords(char const *filename,
131  std::vector<cvm::atom_pos> &pos,
132  const std::vector<int> &indices,
133  std::string const &pdb_field,
134  double const pdb_field_value = 0.0);
135 
137  int reset();
138 
140  inline int get_atom_id(int index) const
141  {
142  return atoms_ids[index];
143  }
144 
146  inline cvm::real get_atom_mass(int index) const
147  {
148  return atoms_masses[index];
149  }
150 
152  inline cvm::real get_atom_charge(int index) const
153  {
154  return atoms_charges[index];
155  }
156 
158  inline cvm::rvector get_atom_position(int index) const
159  {
160  return atoms_positions[index];
161  }
162 
164  inline cvm::rvector get_atom_total_force(int index) const
165  {
166  return atoms_total_forces[index];
167  }
168 
170  inline void apply_atom_force(int index, cvm::rvector const &new_force)
171  {
172  atoms_new_colvar_forces[index] += new_force;
173  }
174 
176  inline cvm::rvector get_atom_velocity(int index)
177  {
178  cvm::error("Error: reading the current velocity of an atom "
179  "is not yet implemented.\n",
180  COLVARS_NOT_IMPLEMENTED);
181  return cvm::rvector(0.0);
182  }
183 
184  inline std::vector<int> *modify_atom_ids()
185  {
186  return &atoms_ids;
187  }
188 
189  inline std::vector<cvm::real> *modify_atom_masses()
190  {
191  return &atoms_masses;
192  }
193 
194  inline std::vector<cvm::real> *modify_atom_charges()
195  {
196  return &atoms_charges;
197  }
198 
199  inline std::vector<cvm::rvector> *modify_atom_positions()
200  {
201  return &atoms_positions;
202  }
203 
204  inline std::vector<cvm::rvector> *modify_atom_total_forces()
205  {
206  return &atoms_total_forces;
207  }
208 
209  inline std::vector<cvm::rvector> *modify_atom_new_colvar_forces()
210  {
211  return &atoms_new_colvar_forces;
212  }
213 
214 protected:
215 
218  std::vector<int> atoms_ids;
220  std::vector<size_t> atoms_ncopies;
222  std::vector<cvm::real> atoms_masses;
224  std::vector<cvm::real> atoms_charges;
226  std::vector<cvm::rvector> atoms_positions;
228  std::vector<cvm::rvector> atoms_total_forces;
230  std::vector<cvm::rvector> atoms_new_colvar_forces;
231 
234  int add_atom_slot(int atom_id);
235 
236 };
237 
238 
242 
243 public:
244 
247 
249  virtual ~colvarproxy_atom_groups();
250 
252  int reset();
253 
255  virtual int scalable_group_coms();
256 
258  virtual int init_atom_group(std::vector<int> const &atoms_ids);
259 
261  virtual void clear_atom_group(int index);
262 
264  inline int get_atom_group_id(int index) const
265  {
266  return atom_groups_ids[index];
267  }
268 
270  inline cvm::real get_atom_group_mass(int index) const
271  {
272  return atom_groups_masses[index];
273  }
274 
276  inline cvm::real get_atom_group_charge(int index) const
277  {
278  return atom_groups_charges[index];
279  }
280 
282  inline cvm::rvector get_atom_group_com(int index) const
283  {
284  return atom_groups_coms[index];
285  }
286 
288  inline cvm::rvector get_atom_group_total_force(int index) const
289  {
290  return atom_groups_total_forces[index];
291  }
292 
294  inline void apply_atom_group_force(int index, cvm::rvector const &new_force)
295  {
296  atom_groups_new_colvar_forces[index] += new_force;
297  }
298 
301  {
302  cvm::error("Error: reading the current velocity of an atom group is not yet implemented.\n",
303  COLVARS_NOT_IMPLEMENTED);
304  return cvm::rvector(0.0);
305  }
306 
307 protected:
308 
311  std::vector<int> atom_groups_ids;
313  std::vector<size_t> atom_groups_ncopies;
315  std::vector<cvm::real> atom_groups_masses;
317  std::vector<cvm::real> atom_groups_charges;
319  std::vector<cvm::rvector> atom_groups_coms;
321  std::vector<cvm::rvector> atom_groups_total_forces;
323  std::vector<cvm::rvector> atom_groups_new_colvar_forces;
324 
326  int add_atom_group_slot(int atom_group_id);
327 };
328 
329 
332 
333 public:
334 
336  colvarproxy_smp();
337 
339  virtual ~colvarproxy_smp();
340 
344 
346  virtual int smp_enabled();
347 
349  virtual int smp_colvars_loop();
350 
352  virtual int smp_biases_loop();
353 
355  virtual int smp_biases_script_loop();
356 
358  virtual int smp_thread_id();
359 
361  virtual int smp_num_threads();
362 
364  virtual int smp_lock();
365 
367  virtual int smp_trylock();
368 
370  virtual int smp_unlock();
371 
372 protected:
373 
376 };
377 
378 
381 
382 public:
383 
386 
388  virtual ~colvarproxy_replicas();
389 
391  virtual bool replica_enabled();
392 
394  virtual int replica_index();
395 
397  virtual int replica_num();
398 
400  virtual void replica_comm_barrier();
401 
403  virtual int replica_comm_recv(char* msg_data, int buf_len, int src_rep);
404 
406  virtual int replica_comm_send(char* msg_data, int msg_len, int dest_rep);
407 
408 };
409 
410 
413 
414 public:
415 
418 
420  virtual ~colvarproxy_script();
421 
423  virtual char *script_obj_to_str(unsigned char *obj);
424 
428 
431 
434 
436  virtual int run_force_callback();
437 
438  virtual int run_colvar_callback(
439  std::string const &name,
440  std::vector<const colvarvalue *> const &cvcs,
441  colvarvalue &value);
442 
443  virtual int run_colvar_gradient_callback(
444  std::string const &name,
445  std::vector<const colvarvalue *> const &cvcs,
446  std::vector<cvm::matrix2d<cvm::real> > &gradient);
447 };
448 
449 
452 
453 public:
454 
456  colvarproxy_io();
457 
459  virtual ~colvarproxy_io();
460 
462  // Returns error code
463  virtual int get_frame(long int &);
464 
466  // Returns error code
467  virtual int set_frame(long int);
468 
471  virtual std::ostream *output_stream(std::string const &output_name,
472  std::ios_base::openmode mode =
473  std::ios_base::out);
474 
476  virtual int flush_output_stream(std::ostream *os);
477 
479  virtual int close_output_stream(std::string const &output_name);
480 
482  virtual int backup_file(char const *filename);
483 
485  inline int backup_file(std::string const &filename)
486  {
487  return backup_file(filename.c_str());
488  }
489 
491  inline std::string & input_prefix()
492  {
493  return input_prefix_str;
494  }
495 
497  inline std::string & restart_output_prefix()
498  {
499  return restart_output_prefix_str;
500  }
501 
504  inline std::string & output_prefix()
505  {
506  return output_prefix_str;
507  }
508 
509 protected:
510 
513  std::string input_prefix_str, output_prefix_str, restart_output_prefix_str;
514 
517  std::list<std::ostream *> output_files;
519  std::list<std::string> output_stream_names;
520 
521 };
522 
523 
524 
530  : public colvarproxy_system,
531  public colvarproxy_atoms,
533  public colvarproxy_smp,
534  public colvarproxy_replicas,
535  public colvarproxy_script,
536  public colvarproxy_io
537 {
538 
539 public:
540 
543 
545  colvarproxy();
546 
548  virtual ~colvarproxy();
549 
551  virtual int reset();
552 
554  virtual int setup();
555 
559  virtual int update_input();
560 
562  virtual int update_output();
563 
565  virtual void log(std::string const &message) = 0;
566 
568  virtual void error(std::string const &message) = 0;
569 
571  virtual void fatal_error(std::string const &message) = 0;
572 
574  virtual size_t restart_frequency();
575 
577  inline bool simulation_running() const
578  {
579  return b_simulation_running;
580  }
581 
582 protected:
583 
586 
587 };
588 
589 
590 #endif
Method for scripting language interface (Tcl or Python)
Definition: colvarproxy.h:412
cvm::real get_atom_mass(int index) const
Get the mass of the given atom.
Definition: colvarproxy.h:146
cvm::real get_atom_charge(int index) const
Get the charge of the given atom.
Definition: colvarproxy.h:152
virtual cvm::real unit_angstrom()=0
Value of the unit for atomic coordinates with respect to angstroms (used by some variables for hard-c...
std::string input_prefix_str
Prefix to be used for input files (restarts, not configuration)
Definition: colvarproxy.h:513
bool simulation_running() const
Whether a simulation is running (warn against irrecovarable errors)
Definition: colvarproxy.h:577
std::vector< cvm::real > atom_groups_charges
Total charges of the atom groups (allow redefinition during a run, as done e.g. in LAMMPS) ...
Definition: colvarproxy.h:317
virtual bool total_forces_same_step() const
Are total forces from the current step available?
Definition: colvarproxy.cpp:40
std::vector< size_t > atoms_ncopies
Keep track of how many times each atom is used by a separate colvar object.
Definition: colvarproxy.h:220
bool b_simulation_running
Whether a simulation is running (warn against irrecovarable errors)
Definition: colvarproxy.h:585
cvm::rvector get_atom_group_com(int index) const
Read the current position of the center of mass given atom group.
Definition: colvarproxy.h:282
cvm::real get_atom_group_charge(int index) const
Get the charge of the given atom group.
Definition: colvarproxy.h:276
cvm::real get_atom_group_mass(int index) const
Get the mass of the given atom group.
Definition: colvarproxy.h:270
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
Collective variables module (main class)
Definition: colvarmodule.h:62
colvarproxy_system()
Constructor.
Definition: colvarproxy.cpp:17
void * omp_lock_state
Lock state for OpenMP.
Definition: colvarproxy.h:375
Interface between the collective variables module and the simulation or analysis program (NAMD...
Definition: colvarproxy.h:529
cvm::rvector get_atom_position(int index) const
Read the current position of the given atom.
Definition: colvarproxy.h:158
std::string & output_prefix()
Prefix to be used for output files (final system configuration)
Definition: colvarproxy.h:504
std::vector< cvm::real > atom_groups_masses
Total masses of the atom groups.
Definition: colvarproxy.h:315
Collective variables main module.
std::list< std::string > output_stream_names
Identifiers for output_stream objects: by default, these are the names of the files.
Definition: colvarproxy.h:519
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:76
bool b_smp_active
Definition: colvarproxy.h:343
static int error(std::string const &message, int code=COLVARS_ERROR)
Print a message to the main log and set global error code.
Definition: colvarmodule.cpp:1587
bool force_script_defined
is a user force script defined?
Definition: colvarproxy.h:430
virtual cvm::real dt()=0
Time step of the simulation (fs)
virtual void request_total_force(bool yesno)
Tell the proxy whether total forces are needed (may not always be available)
Definition: colvarproxy.cpp:26
cvm::rvector get_atom_velocity(int index)
Read the current velocity of the given atom.
Definition: colvarproxy.h:176
std::list< std::ostream * > output_files
Currently opened output files: by default, these are ofstream objects. Allows redefinition to impleme...
Definition: colvarproxy.h:517
bool have_scripts
Do we have a scripting interface?
Definition: colvarproxy.h:433
void apply_atom_force(int index, cvm::rvector const &new_force)
Request that this force is applied to the given atom.
Definition: colvarproxy.h:170
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:34
Methods for multiple-replica communication.
Definition: colvarproxy.h:380
int get_atom_id(int index) const
Get the numeric ID of the given atom (for the program)
Definition: colvarproxy.h:140
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::rvector > atom_groups_total_forces
Most recently updated total forces on the com of each group.
Definition: colvarproxy.h:321
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
std::vector< cvm::rvector > atom_groups_new_colvar_forces
Forces applied from colvars, to be communicated to the MD integrator.
Definition: colvarproxy.h:323
int get_atom_group_id(int index) const
Get the numeric ID of the given atom group (for the MD program)
Definition: colvarproxy.h:264
Methods for data input/output.
Definition: colvarproxy.h:451
Definition: colvarscript.h:21
virtual bool total_forces_enabled() const
Are total forces being used?
Definition: colvarproxy.cpp:34
colvarscript * script
Definition: colvarproxy.h:427
cvm::rvector get_atom_total_force(int index) const
Read the current total force of the given atom.
Definition: colvarproxy.h:164
std::vector< cvm::rvector > atoms_total_forces
Most recent total forces on each atom.
Definition: colvarproxy.h:228
int residue_id
Residue identifier.
Definition: colvarmodule.h:93
std::string & restart_output_prefix()
Prefix to be used for output restart files.
Definition: colvarproxy.h:497
virtual void add_energy(cvm::real energy)=0
Pass restraint energy value for current timestep to MD engine.
Definition: colvarproxy.cpp:23
Methods for accessing the simulation system (PBCs, integrator, etc)
Definition: colvarproxy.h:33
virtual cvm::real temperature()=0
Target temperature of the simulation (K units)
std::vector< size_t > atom_groups_ncopies
Keep track of how many times each group is used by a separate cvc.
Definition: colvarproxy.h:313
cvm::rvector get_atom_group_velocity(int index)
Read the current velocity of the given atom group.
Definition: colvarproxy.h:300
virtual cvm::real boltzmann()=0
Boltzmann constant.
std::vector< cvm::rvector > atom_groups_coms
Current centers of mass of the atom groups.
Definition: colvarproxy.h:319
int backup_file(std::string const &filename)
Rename the given file, before overwriting it.
Definition: colvarproxy.h:485
std::string & input_prefix()
Prefix of the input state file.
Definition: colvarproxy.h:491
virtual cvm::real rand_gaussian(void)=0
Pseudo-random number with Gaussian distribution.
virtual 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.cpp:46
std::vector< cvm::rvector > atoms_positions
Current three-dimensional positions of the atoms.
Definition: colvarproxy.h:226
std::vector< int > atom_groups_ids
Array of 0-based integers used to uniquely associate atom groups within the host program.
Definition: colvarproxy.h:311
colvarmodule * colvars
Pointer to the main object.
Definition: colvarproxy.h:542
Container of atomic data for processing by Colvars.
Definition: colvarproxy.h:83
Methods for SMP parallelization.
Definition: colvarproxy.h:331
cvm::rvector get_atom_group_total_force(int index) const
Read the current total force of the given atom group.
Definition: colvarproxy.h:288
Container of atom group data (allow collection of aggregated atomic data)
Definition: colvarproxy.h:241
virtual cvm::rvector position_distance(cvm::atom_pos const &pos1, cvm::atom_pos const &pos2)=0
Get the PBC-aware distance vector between two positions.
void apply_atom_group_force(int index, cvm::rvector const &new_force)
Request that this force is applied to the given atom group.
Definition: colvarproxy.h:294
std::vector< cvm::rvector > atoms_new_colvar_forces
Forces applied from colvars, to be communicated to the MD integrator.
Definition: colvarproxy.h:230
virtual ~colvarproxy_system()
Destructor.
Definition: colvarproxy.cpp:20
Group of atom objects, mostly used by a cvc object to gather all atomic data.
Definition: colvaratoms.h:142