Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef SYSTEM_UPDATER_FROM_XDRFILE_H
00036 #define SYSTEM_UPDATER_FROM_XDRFILE_H
00037
00038
00039
00040
00041
00042
00043
00044 #include <cstdlib>
00045 #include <boost/tuple/tuple.hpp>
00046 #include <iostream>
00047
00048 #define CPLUSPLUS
00049 #include <xdrfile/xdrfile.h>
00050 #undef CPLUSPLUS
00051
00052
00053
00054 namespace ESBTL{
00055
00068 template <class System>
00069 class System_updater_from_xdrfile{
00070 typedef std::map<unsigned,typename System::Atom*> Map_id_to_atom;
00071 Map_id_to_atom selected_atoms_;
00072
00073 XDRFILE* input_file_;
00074 unsigned first_frame_id_;
00075 unsigned last_frame_id_;
00076 unsigned nb_atoms_to_read_;
00077 unsigned current_frame_;
00078 public:
00079
00091 template <class System_iterator>
00092 System_updater_from_xdrfile(System_iterator begin, System_iterator end,unsigned model_selected_,const std::string& xtc_fname,
00093 unsigned max_atoms,unsigned read_from=1,
00094 unsigned read_to=std::numeric_limits<unsigned>::max() ):first_frame_id_(read_from),last_frame_id_(read_to),nb_atoms_to_read_(max_atoms),current_frame_(0)
00095 {
00096 input_file_=xdrfile_open(xtc_fname.c_str(), "r");
00097 if (input_file_ == NULL){
00098 std::cerr << "Error while opening xtc file " << xtc_fname << std::endl;
00099 exit (EXIT_FAILURE);
00100 }
00101
00102 for (System_iterator it_sys=begin;it_sys!=end;++it_sys){
00103 typename System::Model& model=it_sys->get_model(model_selected_);
00104 for (typename System::Model::Atoms_iterator it_atm=model.atoms_begin();it_atm!=model.atoms_end();++it_atm){
00105 assert( selected_atoms_.find(it_atm->atom_serial_number())==selected_atoms_.end() );
00106 selected_atoms_.insert(std::make_pair(it_atm->atom_serial_number(),&(*it_atm)));
00107 }
00108 }
00109
00110
00111 int magic=0;
00112 int result=xdrfile_read_int(&magic,1,input_file_);
00113 if (result==0){
00114 std::cerr << "xtc file provided contains no frame: error while reading magic number" << std::endl;
00115 exit (EXIT_FAILURE);
00116 }
00117 assert (magic==1995);
00118
00119 if (first_frame_id_!=1)
00120 next_frame(true);
00121
00122 assert(current_frame_+1==first_frame_id_);
00123 }
00124
00125 ~System_updater_from_xdrfile(){
00126
00127 if (input_file_!=NULL)
00128 xdrfile_close(input_file_);
00129 }
00130
00131
00132
00133
00139 boost::tuple<bool,double,int> next_frame(bool is_init=false){
00140 if (!has_more_frames())
00141 return boost::make_tuple(false,-1,-1);
00142
00143 int magic,result, natoms,step;
00144 float time, prec, box[9];
00145 float xyz[3*nb_atoms_to_read_];
00146
00147 do
00148 {
00149 ++current_frame_;
00150
00151 xdrfile_read_int(&natoms,1,input_file_);
00152 assert(natoms == static_cast<int>(nb_atoms_to_read_));
00153 xdrfile_read_int(&step,1,input_file_);
00154 xdrfile_read_float(&time,1,input_file_);
00155 xdrfile_read_float(box,9,input_file_);
00156 result = xdrfile_decompress_coord_float(xyz,&natoms,&prec,input_file_);
00157 assert(result != 0);
00158
00159 result=xdrfile_read_int(&magic,1,input_file_);
00160 if (result==0){
00161 if (current_frame_ <= first_frame_id_ - 1){
00162 std::cerr << "Starting frame id is greater than the number of frames in the provided file" << std::endl;
00163 exit(EXIT_FAILURE);
00164 }
00165 xdrfile_close(input_file_);
00166 input_file_=NULL;
00167 }
00168 assert (result==0 || magic==1995);
00169 }
00170 while( current_frame_ < first_frame_id_ - 1 );
00171
00172
00173 if (!is_init)
00174 for(typename Map_id_to_atom::iterator it_sel= selected_atoms_.begin(); it_sel!=selected_atoms_.end();++it_sel){
00175 unsigned i=it_sel->first;
00176 typename System::Atom::Point_3 new_center(
00177 xyz[3*(i-1)]*10,
00178 xyz[3*(i-1)+1]*10,
00179 xyz[3*(i-1)+2]*10
00180 );
00181
00182 static_cast<typename System::Atom::Point_3&>(*it_sel->second)=new_center;
00183 }
00184 return boost::make_tuple(true,time,step);
00185 }
00186
00188 bool has_more_frames(){
00189 return (input_file_!=NULL && current_frame_ !=last_frame_id_);
00190 }
00191
00192 const unsigned& current_frame_id(){return current_frame_;}
00193 };
00194
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 #endif //SYSTEM_UPDATER_FROM_XDRFILE_H
00215