Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

colvargrid_def.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 
00011 
00012 #ifndef COLVARGRID_DEF_H
00013 #define COLVARGRID_DEF_H
00014 
00015 #include <iostream>
00016 #include <iomanip>
00017 
00018 #include "colvarmodule.h"
00019 #include "colvarproxy.h"
00020 #include "colvar.h"
00021 #include "colvargrid.h"
00022 
00023 
00024 template <class T>
00025 std::istream & colvar_grid<T>::read_multicol(std::istream &is, bool add)
00026 {
00027   // Data in the header: nColvars, then for each
00028   // xiMin, dXi, nPoints, periodic flag
00029 
00030   std::string   hash;
00031   cvm::real     lower, width, x;
00032   size_t        n, periodic_flag;
00033   bool          remap;
00034   std::vector<T>        new_value;
00035   std::vector<int>      nx_read;
00036   std::vector<int>      bin;
00037 
00038   if ( cv.size() > 0 && cv.size() != nd ) {
00039     cvm::error("Cannot read grid file: number of variables in file differs from number referenced by grid.\n");
00040     return is;
00041   }
00042 
00043   if ( !(is >> hash) || (hash != "#") ) {
00044     cvm::error("Error reading grid at position "+
00045                cvm::to_str(static_cast<size_t>(is.tellg()))+
00046                " in stream(read \"" + hash + "\")\n", COLVARS_INPUT_ERROR);
00047     return is;
00048   }
00049 
00050   is >> n;
00051   if ( n != nd ) {
00052     cvm::error("Error reading grid: wrong number of collective variables.\n");
00053     return is;
00054   }
00055 
00056   nx_read.resize(n);
00057   bin.resize(n);
00058   new_value.resize(mult);
00059 
00060   if (this->has_parent_data && add) {
00061     new_data.resize(data.size());
00062   }
00063 
00064   remap = false;
00065   for (size_t i = 0; i < nd; i++ ) {
00066     if ( !(is >> hash) || (hash != "#") ) {
00067       cvm::error("Error reading grid at position "+
00068                  cvm::to_str(static_cast<size_t>(is.tellg()))+
00069                  " in stream(read \"" + hash + "\")\n");
00070       return is;
00071     }
00072 
00073     is >> lower >> width >> nx_read[i] >> periodic_flag;
00074 
00075 
00076     if ( (cvm::fabs(lower - lower_boundaries[i].real_value) > 1.0e-10) ||
00077          (cvm::fabs(width - widths[i] ) > 1.0e-10) ||
00078          (nx_read[i] != nx[i]) ) {
00079       cvm::log("Warning: reading from different grid definition (colvar "
00080                + cvm::to_str(i+1) + "); remapping data on new grid.\n");
00081       remap = true;
00082     }
00083   }
00084 
00085   if ( remap ) {
00086     // re-grid data
00087     while (is.good()) {
00088       bool end_of_file = false;
00089 
00090       for (size_t i = 0; i < nd; i++ ) {
00091         if ( !(is >> x) ) end_of_file = true;
00092         bin[i] = value_to_bin_scalar(x, i);
00093       }
00094       if (end_of_file) break;
00095 
00096       for (size_t imult = 0; imult < mult; imult++) {
00097         is >> new_value[imult];
00098       }
00099 
00100       if ( index_ok(bin) ) {
00101         for (size_t imult = 0; imult < mult; imult++) {
00102           value_input(bin, new_value[imult], imult, add);
00103         }
00104       }
00105     }
00106   } else {
00107     // do not re-grid the data but assume the same grid is used
00108     for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
00109       for (size_t i = 0; i < nd; i++ ) {
00110         is >> x;
00111       }
00112       for (size_t imult = 0; imult < mult; imult++) {
00113         is >> new_value[imult];
00114         value_input(ix, new_value[imult], imult, add);
00115       }
00116     }
00117   }
00118   has_data = true;
00119   return is;
00120 }
00121 
00122 
00123 template <class T>
00124 int colvar_grid<T>::read_multicol(std::string const &filename,
00125                                   std::string description,
00126                                   bool add)
00127 {
00128   std::istream &is = cvm::main()->proxy->input_stream(filename, description);
00129   if (!is) {
00130     return COLVARS_FILE_ERROR;
00131   }
00132   if (colvar_grid<T>::read_multicol(is, add)) {
00133     cvm::main()->proxy->close_input_stream(filename);
00134     return COLVARS_OK;
00135   }
00136   return COLVARS_FILE_ERROR;
00137 }
00138 
00139 
00140 template <class T>
00141 std::ostream & colvar_grid<T>::write_multicol(std::ostream &os) const
00142 {
00143   // Save the output formats
00144   std::ios_base::fmtflags prev_flags(os.flags());
00145 
00146   // Data in the header: nColvars, then for each
00147   // xiMin, dXi, nPoints, periodic
00148 
00149   os << std::setw(2) << "# " << nd << "\n";
00150   // Write the floating numbers in full precision
00151   os.setf(std::ios::scientific, std::ios::floatfield);
00152   for (size_t i = 0; i < nd; i++) {
00153     os << "# "
00154        << std::setw(cvm::cv_width) << std::setprecision(cvm::cv_prec) << lower_boundaries[i] << " "
00155        << std::setw(cvm::cv_width) << std::setprecision(cvm::cv_prec) << widths[i] << " "
00156        << std::setw(10) << nx[i] << "  "
00157        << periodic[i] << "\n";
00158   }
00159 
00160   for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
00161 
00162     if (ix.back() == 0) {
00163       // if the last index is 0, add a new line to mark the new record
00164       os << "\n";
00165     }
00166 
00167     for (size_t i = 0; i < nd; i++) {
00168       os << " "
00169          << std::setw(cvm::cv_width) << std::setprecision(cvm::cv_prec)
00170          << bin_to_value_scalar(ix[i], i);
00171     }
00172     os << " ";
00173     for (size_t imult = 0; imult < mult; imult++) {
00174       os << " "
00175          << std::setw(cvm::cv_width) << std::setprecision(cvm::cv_prec)
00176          << value_output(ix, imult);
00177     }
00178     os << "\n";
00179   }
00180 
00181   // Restore the output formats
00182   os.flags(prev_flags);
00183 
00184   return os;
00185 }
00186 
00187 
00188 template <class T>
00189 int colvar_grid<T>::write_multicol(std::string const &filename,
00190                                    std::string description) const
00191 {
00192   int error_code = COLVARS_OK;
00193   std::ostream &os = cvm::main()->proxy->output_stream(filename, description);
00194   if (!os) {
00195     return COLVARS_FILE_ERROR;
00196   }
00197   error_code |= colvar_grid<T>::write_multicol(os) ? COLVARS_OK :
00198     COLVARS_FILE_ERROR;
00199   cvm::main()->proxy->close_output_stream(filename);
00200   return error_code;
00201 }
00202 
00203 
00204 template <class T>
00205 std::ostream & colvar_grid<T>::write_opendx(std::ostream &os) const
00206 {
00207   // write the header
00208   os << "object 1 class gridpositions counts";
00209   size_t icv;
00210   for (icv = 0; icv < num_variables(); icv++) {
00211     os << " " << number_of_points(icv);
00212   }
00213   os << "\n";
00214 
00215   os << "origin";
00216   for (icv = 0; icv < num_variables(); icv++) {
00217     os << " " << (lower_boundaries[icv].real_value + 0.5 * widths[icv]);
00218   }
00219   os << "\n";
00220 
00221   for (icv = 0; icv < num_variables(); icv++) {
00222     os << "delta";
00223     for (size_t icv2 = 0; icv2 < num_variables(); icv2++) {
00224       if (icv == icv2) os << " " << widths[icv];
00225       else os << " " << 0.0;
00226     }
00227     os << "\n";
00228   }
00229 
00230   os << "object 2 class gridconnections counts";
00231   for (icv = 0; icv < num_variables(); icv++) {
00232     os << " " << number_of_points(icv);
00233   }
00234   os << "\n";
00235 
00236   os << "object 3 class array type double rank 0 items "
00237      << number_of_points() << " data follows\n";
00238 
00239   write_raw(os);
00240 
00241   os << "object \"collective variables scalar field\" class field\n";
00242   return os;
00243 }
00244 
00245 
00246 template <class T>
00247 int colvar_grid<T>::write_opendx(std::string const &filename,
00248                                  std::string description) const
00249 {
00250   int error_code = COLVARS_OK;
00251   std::ostream &os = cvm::main()->proxy->output_stream(filename, description);
00252   if (!os) {
00253     return COLVARS_FILE_ERROR;
00254   }
00255   error_code |= colvar_grid<T>::write_opendx(os) ? COLVARS_OK :
00256     COLVARS_FILE_ERROR;
00257   cvm::main()->proxy->close_output_stream(filename);
00258   return error_code;
00259 }
00260 
00261 #endif

Generated on Mon Apr 29 02:44:26 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002