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

colvar_grid_gradient Class Reference

Class for accumulating the gradient of a scalar function on a grid. More...

#include <colvargrid.h>

Inheritance diagram for colvar_grid_gradient:

colvar_grid< cvm::real > colvarparse List of all members.

Public Member Functions

 colvar_grid_gradient ()
 Default constructor.
virtual ~colvar_grid_gradient ()
 Destructor.
 colvar_grid_gradient (std::vector< int > const &nx_i)
 Constructor from specific sizes arrays.
 colvar_grid_gradient (std::vector< colvar * > &colvars)
 Constructor from a vector of colvars.
void acc_grad (std::vector< int > const &ix, cvm::real const *grads)
 Accumulate the gradient.
void acc_force (std::vector< int > const &ix, cvm::real const *forces)
 Accumulate the gradient based on the force (i.e. sums the opposite of the force).
virtual cvm::real value_output (std::vector< int > const &ix, size_t const &imult=0)
 Return the value of the function at ix divided by its number of samples (if the count grid is defined).
virtual void value_input (std::vector< int > const &ix, cvm::real const &new_value, size_t const &imult=0, bool add=false)
 Get the value from a formatted output and transform it into the internal representation (it may have been rescaled or manipulated).
std::istream & read_restart (std::istream &is)
 Read the grid from a restart.
std::ostream & write_restart (std::ostream &os)
 Write the grid to a restart.
cvm::real average ()
 Compute and return average value for a 1D gradient grid.
void write_1D_integral (std::ostream &os)
 If the grid is 1-dimensional, integrate it and write the integral to a file.

Public Attributes

colvar_grid_countsamples
 Provide the sample count by which each binned value should be divided.

Detailed Description

Class for accumulating the gradient of a scalar function on a grid.

Definition at line 969 of file colvargrid.h.


Constructor & Destructor Documentation

colvar_grid_gradient::colvar_grid_gradient  ) 
 

Default constructor.

Definition at line 107 of file colvargrid.C.

References cvm.

00108   : colvar_grid<cvm::real>(), samples (NULL)
00109 {}

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

Destructor.

Definition at line 981 of file colvargrid.h.

00982   {}

colvar_grid_gradient::colvar_grid_gradient std::vector< int > const &  nx_i  ) 
 

Constructor from specific sizes arrays.

Definition at line 111 of file colvargrid.C.

References cvm.

00112   : colvar_grid<cvm::real> (nx_i, 0.0, nx_i.size()), samples (NULL)
00113 {}

colvar_grid_gradient::colvar_grid_gradient std::vector< colvar * > &  colvars  ) 
 

Constructor from a vector of colvars.

Definition at line 115 of file colvargrid.C.

References cvm.

00116   : colvar_grid<cvm::real> (colvars, 0.0, colvars.size()), samples (NULL)
00117 {}


Member Function Documentation

void colvar_grid_gradient::acc_force std::vector< int > const &  ix,
cvm::real const *  forces
[inline]
 

Accumulate the gradient based on the force (i.e. sums the opposite of the force).

Definition at line 1001 of file colvargrid.h.

References colvar_grid< cvm::real >::address(), colvar_grid_count::incr_count(), and samples.

Referenced by colvarbias_meta::project_hills(), and colvarbias_abf::update().

01001                                                                           {
01002     for (size_t imult = 0; imult < mult; imult++) {
01003       data[address (ix) + imult] -= forces[imult];
01004     }
01005     if (samples)
01006       samples->incr_count (ix);
01007   }

void colvar_grid_gradient::acc_grad std::vector< int > const &  ix,
cvm::real const *  grads
[inline]
 

Accumulate the gradient.

Definition at line 991 of file colvargrid.h.

References colvar_grid< cvm::real >::address(), colvar_grid_count::incr_count(), and samples.

00991                                                                         {
00992     for (size_t imult = 0; imult < mult; imult++) {
00993       data[address (ix) + imult] += grads[imult];
00994     }
00995     if (samples)
00996       samples->incr_count (ix);
00997   }

cvm::real colvar_grid_gradient::average  )  [inline]
 

Compute and return average value for a 1D gradient grid.

Definition at line 1051 of file colvargrid.h.

References colvar_grid< cvm::real >::incr(), colvar_grid< cvm::real >::index_ok(), colvar_grid< cvm::real >::new_index(), colvarmodule::real, samples, colvar_grid< cvm::real >::value(), and colvar_grid< T >::value().

Referenced by colvarbias_abf::update(), and write_1D_integral().

01052   {
01053     size_t n = 0;
01054 
01055     if (nd != 1 || nx[0] == 0) {
01056       return 0.0;
01057     }
01058 
01059     cvm::real sum = 0.0;
01060     std::vector<int> ix = new_index();
01061     if (samples) {
01062       for ( ; index_ok (ix); incr (ix)) {
01063         if ( (n = samples->value (ix)) )
01064           sum += value (ix) / n;
01065       }
01066     } else {
01067       for ( ; index_ok (ix); incr (ix)) {
01068         sum += value (ix);
01069       }
01070     }
01071     return (sum / cvm::real (nx[0]));
01072   }

std::istream & colvar_grid_gradient::read_restart std::istream &  is  ) 
 

Read the grid from a restart.

Definition at line 119 of file colvargrid.C.

References colvarmodule::log(), colvar_grid< cvm::real >::parse_params(), and colvar_grid< cvm::real >::read_raw().

Referenced by colvarbias_meta::read_restart().

00120 {
00121   size_t const start_pos = is.tellg();
00122   std::string key, conf;
00123   if ((is >> key) && (key == std::string ("grid_parameters"))) {
00124     is.seekg (start_pos, std::ios::beg);
00125     is >> colvarparse::read_block ("grid_parameters", conf);
00126     parse_params (conf);
00127   } else {
00128     cvm::log ("Grid parameters are missing in the restart file, using those from the configuration.\n");
00129     is.seekg (start_pos, std::ios::beg);
00130   }
00131   read_raw (is);
00132   return is;
00133 }

virtual void colvar_grid_gradient::value_input std::vector< int > const &  ix,
cvm::real const &  new_value,
size_t const &  imult = 0,
bool  add = false
[inline, virtual]
 

Get the value from a formatted output and transform it into the internal representation (it may have been rescaled or manipulated).

Reimplemented from colvar_grid< cvm::real >.

Definition at line 1025 of file colvargrid.h.

References colvar_grid< cvm::real >::address(), colvar_grid_count::new_count(), samples, and colvar_grid< T >::value().

01029   {
01030     if (add) {
01031       if (samples)
01032         data[address (ix) + imult] += new_value * samples->new_count (ix);
01033       else
01034         data[address (ix) + imult] += new_value;
01035     } else {
01036       if (samples)
01037         data[address (ix) + imult] = new_value * samples->value (ix);
01038       else
01039         data[address (ix) + imult] = new_value;
01040     }
01041   }

virtual cvm::real colvar_grid_gradient::value_output std::vector< int > const &  ix,
size_t const &  imult = 0
[inline, virtual]
 

Return the value of the function at ix divided by its number of samples (if the count grid is defined).

Reimplemented from colvar_grid< cvm::real >.

Definition at line 1011 of file colvargrid.h.

References colvar_grid< cvm::real >::address(), colvarmodule::real, samples, and colvar_grid< T >::value().

01013   {
01014     if (samples)
01015       return (samples->value (ix) > 0) ?
01016         (data[address (ix) + imult] / cvm::real (samples->value (ix))) :
01017         0.0;
01018     else
01019       return data[address (ix) + imult];
01020   }

void colvar_grid_gradient::write_1D_integral std::ostream &  os  ) 
 

If the grid is 1-dimensional, integrate it and write the integral to a file.

Definition at line 142 of file colvargrid.C.

References average(), colvarmodule::fatal_error(), colvar_grid< cvm::real >::incr(), colvar_grid< cvm::real >::index_ok(), colvar_grid< cvm::real >::new_index(), colvarmodule::real, samples, colvar_grid< cvm::real >::value(), and colvar_grid< T >::value().

00143 {
00144   cvm::real bin, min, integral;
00145   std::vector<cvm::real> int_vals;
00146   
00147   os << "#       xi            A(xi)\n";
00148 
00149   if ( cv.size() != 1 ) {
00150     cvm::fatal_error ("Cannot write integral for multi-dimensional gradient grids.");
00151   }
00152 
00153   integral = 0.0;
00154   int_vals.push_back ( 0.0 );
00155   bin = 0.0;
00156   min = 0.0;
00157 
00158   // correction for periodic colvars, so that the PMF is periodic
00159   cvm::real corr;
00160   if ( periodic[0] ) {
00161     corr = average();
00162   } else {
00163     corr = 0.0;
00164   }
00165 
00166   for (std::vector<int> ix = new_index(); index_ok (ix); incr (ix), bin += 1.0 ) {
00167           
00168     if (samples) {
00169       size_t const samples_here = samples->value (ix);
00170       if (samples_here)
00171         integral += (value (ix) / cvm::real (samples_here) - corr) * cv[0]->width;
00172     } else {
00173       integral += (value (ix) - corr) * cv[0]->width;
00174     }
00175 
00176     if ( integral < min ) min = integral;
00177     int_vals.push_back ( integral );
00178   }
00179 
00180   bin = 0.0;
00181   for ( int i = 0; i < nx[0]; i++, bin += 1.0 ) {
00182     os << std::setw (10) << cv[0]->lower_boundary.real_value + cv[0]->width * bin << " "
00183        << std::setw (cvm::cv_width)
00184        << std::setprecision (cvm::cv_prec)
00185        << int_vals[i] - min << "\n";
00186   }
00187 
00188   os << std::setw (10) << cv[0]->lower_boundary.real_value + cv[0]->width * bin << " "
00189      << std::setw (cvm::cv_width)
00190      << std::setprecision (cvm::cv_prec)
00191      << int_vals[nx[0]] - min << "\n";
00192 
00193   return;
00194 }

std::ostream & colvar_grid_gradient::write_restart std::ostream &  os  ) 
 

Write the grid to a restart.

Definition at line 135 of file colvargrid.C.

References colvar_grid< cvm::real >::write_params(), and colvar_grid< cvm::real >::write_raw().

Referenced by colvarbias_meta::write_restart().

00136 {
00137   write_params (os);
00138   write_raw (os);
00139   return os;
00140 }


Member Data Documentation

colvar_grid_count* colvar_grid_gradient::samples
 

Provide the sample count by which each binned value should be divided.

Definition at line 975 of file colvargrid.h.

Referenced by acc_force(), acc_grad(), average(), colvarbias_abf::colvarbias_abf(), value_input(), value_output(), and write_1D_integral().


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