Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

colvarbias_harmonic Class Reference

Harmonic restraint, optionally moving towards a target (implementation of ). More...

#include <colvarbias.h>

Inheritance diagram for colvarbias_harmonic:

colvarbias colvarparse List of all members.

Public Member Functions

virtual void update ()
 Retrieve colvar values and calculate their biasing forces.
virtual std::istream & read_restart (std::istream &is)
 Read the bias configuration from a restart file.
virtual std::ostream & write_restart (std::ostream &os)
 Write the bias configuration to a restart file.
 colvarbias_harmonic (std::string const &conf, char const *key)
 Constructor.
virtual ~colvarbias_harmonic ()
 Destructor.

Protected Attributes

std::vector< colvarvaluecolvar_centers
 Restraint centers.
cvm::real force_k
 Restraint force constant.
cvm::real force_k_target
 Restraint force constant (target value).
size_t force_k_target_nsteps
 Number of steps required to reach the new force constant.
std::vector< colvarvaluecolvar_targets
 New restraint centers.
size_t targets_nsteps
 Number of steps required to reach the new restraint centers.
std::vector< cvm::realtarget_steps
 Amplitude of the restraint centers' motion at each step towards the new values (calculated from target_nsteps).

Detailed Description

Harmonic restraint, optionally moving towards a target (implementation of ).

Definition at line 64 of file colvarbias.h.


Constructor & Destructor Documentation

colvarbias_harmonic::colvarbias_harmonic std::string const &  conf,
char const *  key
 

Constructor.

Definition at line 73 of file colvarbias.C.

References colvar_centers, colvar_targets, colvarmodule::debug(), colvarmodule::fatal_error(), force_k, force_k_target, force_k_target_nsteps, colvarmodule::log(), targets_nsteps, and colvar::type().

00075   : colvarbias (conf, key), 
00076     force_k_target_nsteps (0), targets_nsteps (0)
00077 {
00078   get_keyval (conf, "forceConstant", force_k, 1.0);
00079   for (size_t i = 0; i < colvars.size(); i++) {
00080     if (colvars[i]->width != 1.0)
00081       cvm::log ("The force constant for colvar \""+colvars[i]->name+
00082                 "\" will be rescaled to "+
00083                 cvm::to_str (force_k/(colvars[i]->width*colvars[i]->width))+
00084                 " according to the specified width.\n");
00085   }
00086 
00087   // get the initial restraint centers
00088   colvar_centers.resize (colvars.size());
00089   for (size_t i = 0; i < colvars.size(); i++) {
00090     colvar_centers[i].type (colvars[i]->type());
00091   }
00092   if (get_keyval (conf, "centers", colvar_centers, colvar_centers)) {
00093     for (size_t i = 0; i < colvars.size(); i++) {
00094       colvar_centers[i].apply_constraints();
00095     }
00096   } else {
00097     colvar_centers.clear();
00098     cvm::fatal_error ("Error: must define the initial centers of the restraints.\n");
00099   }
00100 
00101   if (colvar_centers.size() != colvars.size())
00102     cvm::fatal_error ("Error: number of harmonic centers does not match "
00103                       "that of collective variables.\n");
00104 
00105   //   colvar_targets.resize (colvars.size());
00106   //   for (size_t i = 0; i < colvars.size(); i++) {
00107   //     colvar_targets[i].type (colvars[i]->type());
00108   //   }
00109   colvar_targets = colvar_centers;
00110   if (get_keyval (conf, "targets", colvar_targets, colvar_targets)) {
00111 
00112     for (size_t i = 0; i < colvar_targets.size(); i++) {
00113       colvar_targets[i].apply_constraints();
00114     }
00115 
00116     get_keyval (conf, "targetsNumSteps", targets_nsteps, 0);
00117     if (!targets_nsteps)
00118       cvm::fatal_error ("Error: the number of steps for moving "
00119                         "the restraint centers must be non-zero.\n");
00120   } else {
00121     colvar_targets.clear();
00122   }
00123 
00124   if (get_keyval (conf, "forceConstantTarget", force_k_target, 0.0)) {
00125     get_keyval (conf, "forceConstantTargetNumSteps", force_k_target_nsteps, 1000);
00126     if (!force_k_target_nsteps)
00127       cvm::fatal_error ("Error: the number of steps for changing "
00128                         "the force constant must be non-zero.\n");
00129   }
00130 
00131   if (cvm::debug())
00132     cvm::log ("Done initializing a new harmonic restraint bias.\n");
00133 }

virtual colvarbias_harmonic::~colvarbias_harmonic  )  [inline, virtual]
 

Destructor.

Definition at line 81 of file colvarbias.h.

00081 {}


Member Function Documentation

std::istream & colvarbias_harmonic::read_restart std::istream &  is  )  [virtual]
 

Read the bias configuration from a restart file.

Implements colvarbias.

Definition at line 194 of file colvarbias.C.

References colvar_centers, colvarmodule::fatal_error(), force_k, and colvarmodule::log().

00195 {
00196   size_t const start_pos = is.tellg();
00197 
00198   cvm::log ("Restarting harmonic bias \""+
00199             this->name+"\".\n");
00200 
00201   std::string key, brace, conf;
00202   if ( !(is >> key)   || !(key == "harmonic") ||
00203        !(is >> brace) || !(brace == "{") ||
00204        !(is >> colvarparse::read_block ("configuration", conf)) ) {
00205 
00206     cvm::log ("Error: in reading restart configuration for harmonic bias \""+
00207               this->name+"\" at position "+
00208               cvm::to_str (is.tellg())+" in stream.\n");
00209     is.clear();
00210     is.seekg (start_pos, std::ios::beg);
00211     is.setstate (std::ios::failbit);
00212     return is;
00213   }
00214 
00215 //   int id = -1; 
00216   std::string name = "";
00217 //   if ( ( (colvarparse::get_keyval (conf, "id", id, -1, colvarparse::parse_silent)) &&
00218 //          (id != this->id) ) ||
00219   if ( (colvarparse::get_keyval (conf, "name", name, std::string (""), colvarparse::parse_silent)) &&
00220        (name != this->name) )
00221     cvm::fatal_error ("Error: in the restart file, the "
00222                       "\"harmonic\" block has a wrong name\n");
00223 //   if ( (id == -1) && (name == "") ) {
00224   if (name.size() == 0) {
00225     cvm::fatal_error ("Error: \"harmonic\" block in the restart file "
00226                       "has no identifiers.\n");
00227   }
00228 
00229   if (targets_nsteps) {
00230     cvm::log ("Reading the updated restraint centers from the restart.\n");
00231     if (!get_keyval (conf, "centers", colvar_centers))
00232       cvm::fatal_error ("Error: restraint centers are missing in the restart.\n");
00233   }
00234 
00235   if (force_k_target_nsteps) {
00236     cvm::log ("Reading the updated force constant from the restart.\n");
00237     if (!get_keyval (conf, "forceConstant", force_k))
00238       cvm::fatal_error ("Error: force cosntant is missing in the restart.\n");
00239   }
00240 
00241   is >> brace;
00242   if (brace != "}") {
00243     cvm::fatal_error ("Error: corrupt restart information for harmonic bias \""+
00244                       this->name+"\": no matching brace at position "+
00245                       cvm::to_str (is.tellg())+" in the restart file.\n");
00246     is.setstate (std::ios::failbit);
00247   }
00248   return is;
00249 }

void colvarbias_harmonic::update  )  [virtual]
 

Retrieve colvar values and calculate their biasing forces.

Implements colvarbias.

Definition at line 136 of file colvarbias.C.

References colvarvalue::apply_constraints(), colvar_centers, colvar_targets, colvarmodule::debug(), colvar::dist2_lgrad(), force_k, colvarmodule::log(), colvarvalue::norm(), colvarmodule::real, colvarmodule::step_absolute(), target_steps, and targets_nsteps.

00137 {
00138   if (cvm::debug())
00139     cvm::log ("Updating the harmonic bias \""+this->name+"\".\n");
00140   
00141   for (size_t i = 0; i < colvars.size(); i++) {
00142     colvar_forces[i] =
00143       (-0.5) * force_k /
00144       (colvars[i]->width * colvars[i]->width) *
00145       colvars[i]->dist2_lgrad (colvars[i]->value(),
00146                                colvar_centers[i]);
00147     if (cvm::debug())
00148       cvm::log ("dist_grad["+cvm::to_str (i)+
00149                 "] = "+cvm::to_str (colvars[i]->dist2_lgrad (colvars[i]->value(),
00150                                colvar_centers[i]))+"\n");
00151   }
00152 
00153   if (cvm::debug())
00154     cvm::log ("Current forces for the harmonic bias \""+
00155               this->name+"\": "+cvm::to_str (colvar_forces)+".\n");
00156 
00157   if (targets_nsteps) {
00158 
00159     if (!target_steps.size()) {
00160       // if this is the first calculation, calculate the advancement
00161       // at each simulation step
00162       target_steps.resize (colvars.size());
00163       for (size_t i = 0; i < colvars.size(); i++) {
00164         target_steps[i] = (::sqrt (colvars[i]->dist2 (colvar_centers[i],
00165                                                       colvar_targets[i]))) /
00166           cvm::real (targets_nsteps - cvm::step_absolute());
00167       }
00168       if (cvm::debug())
00169         cvm::log ("Center movements for the harmonic bias \""+
00170                   this->name+"\": "+cvm::to_str (target_steps)+".\n");
00171     }
00172 
00173     if (cvm::debug())
00174       cvm::log ("Current centers for the harmonic bias \""+
00175                 this->name+"\": "+cvm::to_str (colvar_centers)+".\n");
00176 
00177     if (cvm::step_absolute() < targets_nsteps) {
00178       // move the restraint centers in the direction of the targets
00179       for (size_t i = 0; i < colvars.size(); i++) {
00180         colvarvalue const d2grad =
00181           colvars[i]->dist2_lgrad (colvar_centers[i],
00182                                    colvar_targets[i]);
00183         colvar_centers[i] += target_steps[i] * (-1.0/d2grad.norm()) * d2grad;
00184         colvar_centers[i].apply_constraints();
00185       }
00186     }
00187   }
00188 
00189   if (cvm::debug())
00190     cvm::log ("Done updating the harmonic bias \""+this->name+"\".\n");
00191 }

std::ostream & colvarbias_harmonic::write_restart std::ostream &  os  )  [virtual]
 

Write the bias configuration to a restart file.

Implements colvarbias.

Definition at line 252 of file colvarbias.C.

References colvar_centers, and force_k.

00253 {
00254   os << "harmonic {\n"
00255      << "  configuration {\n"
00256     //      << "    id " << this->id << "\n"
00257      << "    name " << this->name << "\n";
00258 
00259   if (targets_nsteps) {
00260     os << "    centers ";
00261     for (size_t i = 0; i < colvars.size(); i++) {
00262       os << " " << colvar_centers[i];
00263     }
00264     os << "\n";
00265   }
00266 
00267   if (force_k_target_nsteps) {
00268     os << "    forceConstant "
00269        << std::setprecision (cvm::en_prec)
00270        << std::setw (cvm::en_width) << force_k << "\n";
00271   }
00272 
00273   os << "  }\n"
00274      << "}\n\n";
00275 
00276   return os;
00277 }


Member Data Documentation

std::vector<colvarvalue> colvarbias_harmonic::colvar_centers [protected]
 

Restraint centers.

Definition at line 87 of file colvarbias.h.

Referenced by colvarbias_harmonic(), read_restart(), update(), and write_restart().

std::vector<colvarvalue> colvarbias_harmonic::colvar_targets [protected]
 

New restraint centers.

Definition at line 99 of file colvarbias.h.

Referenced by colvarbias_harmonic(), and update().

cvm::real colvarbias_harmonic::force_k [protected]
 

Restraint force constant.

Definition at line 90 of file colvarbias.h.

Referenced by colvarbias_harmonic(), read_restart(), update(), and write_restart().

cvm::real colvarbias_harmonic::force_k_target [protected]
 

Restraint force constant (target value).

Definition at line 93 of file colvarbias.h.

Referenced by colvarbias_harmonic().

size_t colvarbias_harmonic::force_k_target_nsteps [protected]
 

Number of steps required to reach the new force constant.

Definition at line 96 of file colvarbias.h.

Referenced by colvarbias_harmonic().

std::vector<cvm::real> colvarbias_harmonic::target_steps [protected]
 

Amplitude of the restraint centers' motion at each step towards the new values (calculated from target_nsteps).

Definition at line 107 of file colvarbias.h.

Referenced by update().

size_t colvarbias_harmonic::targets_nsteps [protected]
 

Number of steps required to reach the new restraint centers.

Definition at line 103 of file colvarbias.h.

Referenced by colvarbias_harmonic(), and update().


The documentation for this class was generated from the following files:
Generated on Mon Nov 23 04:59:33 2009 for NAMD by  doxygen 1.3.9.1