Collective Variables Module - Developer Documentation
colvarproxy_vmd.h
1 // -*- c++ -*-
2 
3 #ifndef COLVARPROXY_VMD_H
4 #define COLVARPROXY_VMD_H
5 
6 #include "colvarproxy_vmd_version.h"
7 
8 #include <tcl.h>
9 
10 #include "DrawMolecule.h"
11 #include "Timestep.h"
12 #include "Inform.h"
13 
14 #include "colvarmodule.h"
15 #include "colvarproxy.h"
16 #include "colvartypes.h"
17 #include "colvaratoms.h"
18 
19 
20 int tcl_colvars(ClientData clientData, Tcl_Interp *interp,
21  int objc, Tcl_Obj *const objv[]);
22 
25 class colvarproxy_vmd : public colvarproxy {
26 
27 protected:
28 
30  Tcl_Interp *interp;
32  VMDApp *vmd;
34  int vmdmolid;
36  DrawMolecule *vmdmol;
38  long int vmdmol_frame;
40  Inform msgColvars;
41 
42 public:
43 
44 
45  friend class cvm::atom;
46 
47  colvarproxy_vmd(Tcl_Interp *interp, VMDApp *vmd, int molid);
48  ~colvarproxy_vmd();
49 
50  int setup();
51 
52  int update_input();
55 
57  {
58  return 1.0;
59  }
60 
62  {
63  return 0.001987191;
64  }
65 
67  {
68  // TODO define, document and implement a user method to set the value of this
69  return 300.0;
70  }
71 
72  inline cvm::real dt()
73  {
74  // TODO define, document and implement a user method to set the value of this
75  return 1.0;
76  }
77 
79  {
80  return vmd_random_gaussian();
81  }
82 
83  inline int get_frame(long int &f)
84  {
85  f = vmdmol_frame;
86  return COLVARS_OK;
87  }
88 
89  int set_frame(long int f);
90 
91  std::string input_prefix_str;
92  std::string input_prefix()
93  {
94  return input_prefix_str;
95  }
96 
97  std::string restart_output_prefix()
98  {
99  // note: this shouldn't need to be called in VMD anyway
100  return output_prefix_str;
101  }
102 
103  std::string output_prefix_str;
104  inline std::string output_prefix()
105  {
106  return output_prefix_str;
107  }
108 
109  char *script_obj_to_str(unsigned char *obj);
110 
111  void add_energy(cvm::real energy);
112 
113 private:
114  bool total_force_requested;
115 public:
116  void request_total_force(bool yesno);
117 
119  cvm::atom_pos const &pos2);
121  cvm::atom_pos const &pos2);
122 
123  void select_closest_image(cvm::atom_pos &pos,
124  cvm::atom_pos const &ref_pos);
125 
126  std::string error_output;
127  void log(std::string const &message);
128  void error(std::string const &message);
129  void fatal_error(std::string const &message);
130  void exit(std::string const &message);
131 
132  // Callback functions
133  int run_force_callback();
134  int run_colvar_callback(std::string const &name,
135  std::vector<const colvarvalue *> const &cvcs,
136  colvarvalue &value);
137  int run_colvar_gradient_callback(std::string const &name,
138  std::vector<const colvarvalue *> const &cvc_values,
139  std::vector<cvm::matrix2d<cvm::real> > &gradient);
140 
141  int load_atoms(char const *filename,
142  cvm::atom_group &atoms,
143  std::string const &pdb_field,
144  double const pdb_field_value = 0.0);
145 
146  int load_coords(char const *filename,
147  std::vector<cvm::atom_pos> &pos,
148  const std::vector<int> &indices,
149  std::string const &pdb_field,
150  double const pdb_field_value = 0.0);
151 
152  int init_atom(int atom_number);
153  int check_atom_id(int atom_number);
154  int init_atom(cvm::residue_id const &residue,
155  std::string const &atom_name,
156  std::string const &segment_id);
157  int check_atom_id(cvm::residue_id const &residue,
158  std::string const &atom_name,
159  std::string const &segment_id);
160 
161 };
162 
163 
164 
166  cvm::atom_pos const &pos2)
167 {
168  // TODO: add in the proxy constructor a check for orthonormal PBCs
169  Timestep *ts = vmdmol->get_frame(vmdmol_frame);
170  cvm::real const a = ts->a_length;
171  cvm::real const b = ts->b_length;
172  cvm::real const c = ts->c_length;
173  cvm::rvector diff = (pos2 - pos1);
174  if (a*a > 1.0e-12) {
175  while (diff.x <= -0.5*a) diff.x += a;
176  while (diff.x > 0.5*a) diff.x -= a;
177  }
178  if (b*b > 1.0e-12) {
179  while (diff.y <= -0.5*b) diff.y += b;
180  while (diff.y > 0.5*b) diff.y -= b;
181  }
182  if (c*c > 1.0e-12) {
183  while (diff.z <= -0.5*c) diff.z += c;
184  while (diff.z > 0.5*c) diff.z -= c;
185  }
186  return diff;
187 }
188 
189 
190 inline void colvarproxy_vmd::select_closest_image(cvm::atom_pos &pos,
191  cvm::atom_pos const &ref_pos)
192 {
193  cvm::rvector const diff = position_distance(ref_pos, pos);
194  pos = ref_pos + diff;
195 }
196 
197 
199  cvm::atom_pos const &pos2)
200 {
201  cvm::rvector const d = position_distance(pos1, pos2);
202  return cvm::real(d.x*d.x + d.y*d.y + d.z*d.z);
203 }
204 
205 
206 
207 #endif
208 
static void exit(std::string const &message)
Print a message to the main log and exit normally.
cvm::real temperature()
Target temperature of the simulation (K units)
Definition: colvarproxy_vmd.h:66
cvm::real dt()
Time step of the simulation (fs)
Definition: colvarproxy_vmd.h:72
Communication between colvars and VMD (implementation of colvarproxy)
Definition: colvarproxy_vmd.h:25
void request_total_force(bool yesno)
Tell the proxy whether total forces are needed (may not always be available)
Stores numeric id, mass and all mutable data for an atom, mostly used by a cvc.
Definition: colvaratoms.h:21
int run_force_callback()
Run a user-defined colvar forces script.
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.
int init_atom(int atom_number)
vector of real numbers with three components
Definition: colvartypes.h:714
Colvars proxy classes.
int set_frame(long int f)
Set the current frame number (as well as colvarmodule::it)
Interface between the collective variables module and the simulation or analysis program (NAMD...
Definition: colvarproxy.h:529
cvm::real rand_gaussian()
Pseudo-random number with Gaussian distribution.
Definition: colvarproxy_vmd.h:78
Collective variables main module.
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:76
void error(std::string const &message)
Print a message to the main log and let the rest of the program handle the error. ...
cvm::real unit_angstrom()
Value of the unit for atomic coordinates with respect to angstroms (used by some variables for hard-c...
Definition: colvarproxy_vmd.h:56
static std::string & output_prefix()
Accessor for the above.
Definition: colvarmodule.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
cvm::real boltzmann()
Boltzmann constant.
Definition: colvarproxy_vmd.h:61
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_vmd.h:165
int update_input()
Update data required by the colvars module (e.g. cache atom positions)
void log(std::string const &message)
Print a message to the main log.
int residue_id
Residue identifier.
Definition: colvarmodule.h:93
void add_energy(cvm::real energy)
Pass restraint energy value for current timestep to MD engine.
Tcl_Interp * interp
pointer to the VMD Tcl interpreter
Definition: colvarproxy_vmd.h:30
void fatal_error(std::string const &message)
Print a message to the main log and exit with error code.
long int vmdmol_frame
current frame (returned by vmdmol->frame())
Definition: colvarproxy_vmd.h:38
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_vmd.h:198
int update_atomic_properties()
Update mass, charge, etc.
cvm::atom_pos pos
Current position (copied from the program, can be modified if necessary)
Definition: colvaratoms.h:41
int get_frame(long int &f)
Save the current frame number in the argument given.
Definition: colvarproxy_vmd.h:83
int vmdmolid
VMD molecule id being used (must be provided at construction)
Definition: colvarproxy_vmd.h:34
int check_atom_id(int atom_number)
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...
char * script_obj_to_str(unsigned char *obj)
Convert a script object (Tcl or Python call argument) to a C string.
VMDApp * vmd
pointer to the VMD main object
Definition: colvarproxy_vmd.h:32
Inform msgColvars
output object
Definition: colvarproxy_vmd.h:40
DrawMolecule * vmdmol
pointer to VMD molecule (derived from vmdmolid)
Definition: colvarproxy_vmd.h:36
Group of atom objects, mostly used by a cvc object to gather all atomic data.
Definition: colvaratoms.h:142