colvarproxy_namd.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 
00003 // This file is part of the Collective Variables module (Colvars).
00004 // The original version of Colvars and its updates are located at:
00005 // https://github.com/colvars/colvars
00006 // Please update all Colvars source files before making any changes.
00007 // If you wish to distribute your changes, please submit them to the
00008 // Colvars repository at GitHub.
00009 
00010 #ifndef COLVARPROXY_NAMD_H
00011 #define COLVARPROXY_NAMD_H
00012 
00013 #include "colvarproxy_namd_version.h"
00014 
00015 #include "Vector.h"
00016 #include "ResizeArray.h"
00017 #include "NamdTypes.h"
00018 #include "SimParameters.h"
00019 #include "Lattice.h"
00020 #include "GlobalMaster.h"
00021 #include "Random.h"
00022 
00023 #include "colvarmodule.h"
00024 #include "colvarproxy.h"
00025 #include "colvarvalue.h"
00026 
00027 // For replica exchange
00028 #include "converse.h"
00029 #include "DataExchanger.h"
00030 
00033 class colvarproxy_namd : public colvarproxy, public GlobalMaster {
00034 
00035 protected:
00036 
00039   std::vector<int> atoms_map;
00040 
00042   SimParameters const *simparams;
00043 
00045   BigReal thermostat_temperature;
00046 
00048   Random random;
00049 
00051   size_t restart_frequency_s;
00052 
00053   bool first_timestep;
00054   size_t previous_NAMD_step;
00055 
00056   bool total_force_requested;
00057 
00059   SubmitReduction *reduction;
00060 
00061 #ifdef NAMD_TCL
00062   Tcl_Interp *interp; // Tcl interpreter embedded in NAMD
00063 #endif
00064 
00065 public:
00066 
00067   friend class cvm::atom;
00068 
00069   colvarproxy_namd();
00070   ~colvarproxy_namd();
00071 
00072   int setup();
00073   int reset();
00074 
00075   // synchronize the local arrays with requested or forced atoms
00076   int update_atoms_map(AtomIDList::const_iterator begin, AtomIDList::const_iterator end);
00077 
00078   void calculate();
00079 
00080   void log(std::string const &message);
00081   void error(std::string const &message);
00082   void fatal_error(std::string const &message);
00083   void exit(std::string const &message);
00084   void add_energy(cvm::real energy);
00085   void request_total_force(bool yesno);
00086 
00087   bool total_forces_enabled() const
00088   {
00089     return total_force_requested;
00090   }
00091 
00092   int run_force_callback();
00093   int run_colvar_callback(std::string const &name,
00094                           std::vector<const colvarvalue *> const &cvcs,
00095                           colvarvalue &value);
00096   int run_colvar_gradient_callback(std::string const &name,
00097                                    std::vector<const colvarvalue *> const &cvcs,
00098                                    std::vector<cvm::matrix2d<cvm::real> > &gradient);
00099 
00100   cvm::real unit_angstrom()
00101   {
00102     return 1.0;
00103   }
00104 
00105   cvm::real boltzmann()
00106   {
00107     return 0.001987191;
00108   }
00109 
00110   cvm::real temperature()
00111   {
00112     return thermostat_temperature;
00113   }
00114 
00115   cvm::real rand_gaussian()
00116   {
00117     return random.gaussian();
00118   }
00119 
00120   cvm::real dt()
00121   {
00122     return simparams->dt;
00123   }
00124 
00125   int smp_enabled()
00126   {
00127 #if CMK_SMP && USE_CKLOOP
00128     if (b_smp_active) {
00129       return COLVARS_OK;
00130     }
00131     return COLVARS_ERROR;
00132 #else
00133     return COLVARS_NOT_IMPLEMENTED;
00134 #endif
00135   }
00136 
00137 #if CMK_SMP && USE_CKLOOP
00138   int smp_colvars_loop();
00139 
00140   int smp_biases_loop();
00141 
00142   int smp_biases_script_loop();
00143 
00144   friend void calc_colvars_items_smp(int first, int last, void *result, int paramNum, void *param);
00145   friend void calc_cv_biases_smp(int first, int last, void *result, int paramNum, void *param);
00146 #endif
00147 
00148   friend void calc_cv_scripted_forces(int paramNum, void *param);
00149 
00150   int smp_thread_id()
00151   {
00152     return CkMyRank();
00153   }
00154 
00155   int smp_num_threads()
00156   {
00157     return CkMyNodeSize();
00158   }
00159 
00160 protected:
00161 
00162   CmiNodeLock smp_lock_state;
00163 
00164 public:
00165 
00166   int smp_lock()
00167   {
00168     smp_lock_state = CmiCreateLock();
00169     return COLVARS_OK;
00170   }
00171 
00172   int smp_trylock()
00173   {
00174     return COLVARS_NOT_IMPLEMENTED;
00175   }
00176 
00177   int smp_unlock()
00178   {
00179     CmiDestroyLock(smp_lock_state);
00180     return COLVARS_OK;
00181   }
00182 
00183   // Replica communication functions.
00184   bool replica_enabled() {
00185 #if CMK_HAS_PARTITION
00186     return true;
00187 #else
00188     return false;
00189 #endif
00190   }
00191 
00192   int replica_index() {
00193     return CmiMyPartition();
00194   }
00195 
00196   int replica_num() {
00197     return CmiNumPartitions();
00198   }
00199 
00200   void replica_comm_barrier() {
00201     replica_barrier();
00202   }
00203 
00204   int replica_comm_recv(char* msg_data, int buf_len, int src_rep) {
00205     DataMessage *recvMsg = NULL;
00206     replica_recv(&recvMsg, src_rep, CkMyPe());
00207     CmiAssert(recvMsg != NULL);
00208     int retval = recvMsg->size;
00209     if (buf_len >= retval) {
00210       memcpy(msg_data,recvMsg->data,retval);
00211     } else {
00212       retval = 0;
00213     }
00214     CmiFree(recvMsg);
00215     return retval;
00216   }
00217 
00218   int replica_comm_send(char* msg_data, int msg_len, int dest_rep) {
00219     replica_send(msg_data, msg_len, dest_rep, CkMyPe());
00220     return msg_len;
00221   }
00222 
00223   int replica_comm_send()
00224   {
00225     return COLVARS_OK;
00226   }
00227 
00228   int replica_comm_async_send()
00229   {
00230     return COLVARS_OK;
00231   }
00232 
00233   inline size_t restart_frequency()
00234   {
00235     return restart_frequency_s;
00236   }
00237 
00238   int init_atom(int atom_number);
00239   int check_atom_id(int atom_number);
00240   int init_atom(cvm::residue_id const &residue,
00241                 std::string const     &atom_name,
00242                 std::string const     &segment_id);
00243   int check_atom_id(cvm::residue_id const &residue,
00244                     std::string const     &atom_name,
00245                     std::string const     &segment_id);
00246   void clear_atom(int index);
00247 
00248   inline void update_atom_properties(int index)
00249   {
00250     Molecule *mol = Node::Object()->molecule;
00251     // update mass
00252     atoms_masses[index] = mol->atommass(atoms_ids[index]);
00253     // update charge
00254     atoms_charges[index] = mol->atomcharge(atoms_ids[index]);
00255   }
00256 
00257   cvm::rvector position_distance(cvm::atom_pos const &pos1,
00258                                  cvm::atom_pos const &pos2);
00259   cvm::real position_dist2(cvm::atom_pos const &pos1,
00260                            cvm::atom_pos const &pos2);
00261 
00262   void select_closest_image(cvm::atom_pos &pos,
00263                             cvm::atom_pos const &ref_pos);
00264 
00265 
00266   int load_atoms(char const *filename,
00267                  cvm::atom_group &atoms,
00268                  std::string const &pdb_field,
00269                  double const pdb_field_value = 0.0);
00270 
00271   int load_coords(char const *filename,
00272                   std::vector<cvm::atom_pos> &pos,
00273                   const std::vector<int> &indices,
00274                   std::string const &pdb_field,
00275                   double const pdb_field_value = 0.0);
00276 
00277 
00278   int scalable_group_coms()
00279   {
00280     return COLVARS_OK;
00281   }
00282   int init_atom_group(std::vector<int> const &atoms_ids);
00283   void clear_atom_group(int index);
00284   int update_group_properties(int index);
00285 
00286   std::ostream * output_stream(std::string const &output_name,
00287                                std::ios_base::openmode mode);
00288   int flush_output_stream(std::ostream *os);
00289   int close_output_stream(std::string const &output_name);
00290   int backup_file(char const *filename);
00291 
00292   char *script_obj_to_str(unsigned char *obj);
00293 };
00294 
00295 
00296 inline cvm::rvector colvarproxy_namd::position_distance(cvm::atom_pos const &pos1,
00297                                                         cvm::atom_pos const &pos2)
00298 {
00299   Position const p1(pos1.x, pos1.y, pos1.z);
00300   Position const p2(pos2.x, pos2.y, pos2.z);
00301   // return p2 - p1
00302   Vector const d = this->lattice->delta(p2, p1);
00303   return cvm::rvector(d.x, d.y, d.z);
00304 }
00305 
00306 
00307 inline cvm::real colvarproxy_namd::position_dist2(cvm::atom_pos const &pos1,
00308                                                   cvm::atom_pos const &pos2)
00309 {
00310   Lattice const *l = this->lattice;
00311   Vector const p1(pos1.x, pos1.y, pos1.z);
00312   Vector const p2(pos2.x, pos2.y, pos2.z);
00313   Vector const d = l->delta(p1, p2);
00314   return cvm::real(d.x*d.x + d.y*d.y + d.z*d.z);
00315 }
00316 
00317 
00318 inline void colvarproxy_namd::select_closest_image(cvm::atom_pos &pos,
00319                                                    cvm::atom_pos const &ref_pos)
00320 {
00321   Position const p(pos.x, pos.y, pos.z);
00322   Position const rp(ref_pos.x, ref_pos.y, ref_pos.z);
00323   ScaledPosition const srp = this->lattice->scale(rp);
00324   Position const np = this->lattice->nearest(p, srp);
00325   pos.x = np.x;
00326   pos.y = np.y;
00327   pos.z = np.z;
00328 }
00329 
00330 
00331 
00332 #endif

Generated on Sat Sep 23 01:17:11 2017 for NAMD by  doxygen 1.4.7