vtf-logo

FileInput.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2007 California Institute of Technology
00004 // Ralf Deiterding, ralf@amroc.net
00005 
00006 #ifndef AMROC_FILE_INPUT_H
00007 #define AMROC_FILE_INPUT_H
00008 
00016 #include "DAGHIO.h"
00017 
00024 template <class VectorType, int dim>
00025 class FileInput : public AMRBase<VectorType,dim> {
00026   typedef AMRBase<VectorType,dim> base;
00027   typedef typename VectorType::InternalDataType DataType;
00028 
00029 public:
00030   typedef typename base::vec_grid_fct_type vec_grid_fct_type;  
00031   typedef typename base::vec_grid_data_type vec_grid_data_type;
00032   typedef GridFunction<DataType,dim> grid_fct_type;
00033   typedef GridData<DataType,dim> grid_data_type;
00034 
00035   typedef LevelTransfer<DataType,dim> leveltransfer_type;
00036   typedef GFLevelTransferSpecificFunc<leveltransfer_type,DataType,dim> leveltransfer_functor_type;
00037 
00038   FileInput() : base(), _Ncnt(0), _InputType(DAGHIO_HDF_NCSA) {
00039     CompName = (std::string*) 0;
00040     _LevelTransfer = (leveltransfer_type *) 0;
00041     _ProlongFunc = (leveltransfer_functor_type *) 0;
00042     _RestrictFunc = (leveltransfer_functor_type *) 0;
00043     _where = BBox(dim,1);
00044     _to = BBox(dim,1);
00045     _from = BBox(dim,1);
00046   } 
00047 
00048   virtual ~FileInput() {
00049     if (CompName) delete [] CompName;
00050     if (_ProlongFunc) delete _ProlongFunc;
00051     if (_RestrictFunc) delete _RestrictFunc;
00052   }
00053 
00054   virtual void register_at(ControlDevice& Ctrl) { register_at(Ctrl, ""); }
00055   virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00056     LocCtrl = Ctrl.getSubDevice(prefix+"InputFiles");    
00057     RegisterAt(LocCtrl,"Type",_InputType);
00058     if (CompName) delete [] CompName;
00059     CompName = new std::string[MAXCOMPONENTS];
00060     for (int d=0; d<MAXCOMPONENTS; d++) {
00061       char Name[16];
00062       std::sprintf(Name,"-");
00063       CompName[d] = Name;
00064       std::sprintf(Name,"InputName(%d)",d+1);
00065       RegisterAt(LocCtrl,Name,CompName[d]);
00066     }    
00067     WhereCtrl = LocCtrl.getSubDevice(prefix+"Where");
00068     for (int d=0; d<dim; d++) {
00069       char text[8];
00070       std::sprintf(text,"lb(%d)",d+1);
00071       RegisterAt(WhereCtrl,text,_where.lower(d));
00072       std::sprintf(text,"ub(%d)",d+1);
00073       RegisterAt(WhereCtrl,text,_where.upper(d));
00074       std::sprintf(text,"s(%d)",d+1);
00075       RegisterAt(WhereCtrl,text,_where.stepsize(d));
00076     }
00077     ToCtrl = LocCtrl.getSubDevice(prefix+"To");
00078     for (int d=0; d<dim; d++) {
00079       char text[8];
00080       std::sprintf(text,"lb(%d)",d+1);
00081       RegisterAt(ToCtrl,text,_to.lower(d));
00082       std::sprintf(text,"ub(%d)",d+1);
00083       RegisterAt(ToCtrl,text,_to.upper(d));
00084       std::sprintf(text,"s(%d)",d+1);
00085       RegisterAt(ToCtrl,text,_to.stepsize(d));
00086     }
00087     FromCtrl = LocCtrl.getSubDevice(prefix+"From");
00088     for (int d=0; d<dim; d++) {
00089       char text[8];
00090       std::sprintf(text,"lb(%d)",d+1);
00091       RegisterAt(FromCtrl,text,_from.lower(d));
00092       std::sprintf(text,"ub(%d)",d+1);
00093       RegisterAt(FromCtrl,text,_from.upper(d));
00094       std::sprintf(text,"s(%d)",d+1);
00095       RegisterAt(FromCtrl,text,_from.stepsize(d));
00096     }
00097     if (_LevelTransfer) _LevelTransfer->register_at(Ctrl, ""); 
00098   }
00099   virtual void init() { 
00100     DAGHIOInit(); 
00101     if (_LevelTransfer) _LevelTransfer->init();
00102   }
00103   virtual void update() {
00104     int d=MAXCOMPONENTS;
00105     for (; d>0; d--) 
00106       if (CompName[d-1].c_str()[0] != '-') 
00107         break;
00108     if (d>0) SetNcnt(d);
00109     if (_LevelTransfer) _LevelTransfer->update();
00110   }
00111   virtual void finish() {
00112     if (base::_Hierarchy) CloseIO();
00113     if (base::_Hierarchy) DAGHIOEnd(base::GH());  
00114     if (CompName) {
00115       delete [] CompName;
00116       CompName = (std::string*) NULL;
00117     }
00118     if (_LevelTransfer) _LevelTransfer->finish();
00119   }
00120 
00121   virtual void SetupData(GridHierarchy* gh, const int& ghosts) {
00122     base::SetupData(gh, ghosts);
00123     DAGHSetIOType(base::GH(), _InputType);   
00124     if (_LevelTransfer) _LevelTransfer->SetupData(gh, ghosts);
00125     if (_LevelTransfer) {
00126       _ProlongFunc = new leveltransfer_functor_type(_LevelTransfer, &leveltransfer_type::Prolong);
00127       _RestrictFunc = new leveltransfer_functor_type(_LevelTransfer, &leveltransfer_type::Restrict);
00128     }
00129   }
00130 
00131   //---------------------------------------------------------------------- 
00132   // Input vector of state
00133   //---------------------------------------------------------------------- 
00134   virtual void ReadIn(vec_grid_fct_type& u, grid_fct_type& IOfunc) {
00135     for (int cnt=0; cnt<Ncnt(); cnt++) { 
00136       if (CompName[cnt].c_str()[0] == '-') 
00137         continue;
00138       int lev;
00139       for (lev=0; lev<=FineLevel(base::GH()); lev++) {
00140         int Time = CurrentTime(base::GH(),lev); 
00141         forall (u,Time,lev,c)           
00142           equals_from(IOfunc(Time,lev,c), u(Time,lev,c), cnt);
00143         end_forall      
00144       }
00145       ReadIn(IOfunc,CompName[cnt].c_str());
00146       for (lev=0; lev<=FineLevel(base::GH()); lev++) {
00147         int Time = CurrentTime(base::GH(),lev); 
00148         forall (u,Time,lev,c)           
00149           equals_to(u(Time,lev,c), IOfunc(Time,lev,c), cnt);
00150         end_forall      
00151       }
00152     } 
00153   }   
00154 
00155   //---------------------------------------------------------------------- 
00156   // Input scalar function
00157   //---------------------------------------------------------------------- 
00158   virtual void ReadIn(grid_fct_type& IOfunc, const char* name) {
00159     for (int lev=0; lev<=FineLevel(base::GH()); lev++) {
00160       int Time = CurrentTime(base::GH(),lev); 
00161       ReadIn(IOfunc,name,Time,lev);
00162     }
00163   }   
00164 
00165   //---------------------------------------------------------------------- 
00166   // Input scalar function
00167   //---------------------------------------------------------------------- 
00168   virtual void ReadIn(grid_fct_type& IOfunc, const char* name, 
00169                       const int& Time, const int& Level) {
00170 
00171     if (_LevelTransfer && _ProlongFunc && Level>0) {
00172       int old_prolong_flag = DAGHTrue;
00173       if (!IOfunc.prolong()) old_prolong_flag = DAGHFalse;
00174       GFLevelTransferFunc<DataType,dim>* old_ProlongFunc = GetProlongFunction(IOfunc);
00175       SetProlongFunction(IOfunc, _ProlongFunc);
00176       IOfunc.GF_SetProlongFlag(DAGHTrue);
00177       int TimeCoarse = CurrentTime(base::GH(),Level-1); 
00178       Prolong(IOfunc, TimeCoarse, Level-1, Time, Level);
00179       SetProlongFunction(IOfunc, old_ProlongFunc);
00180       IOfunc.GF_SetProlongFlag(old_prolong_flag);
00181     }
00182 
00183     if (_where.empty()) {
00184       if (_to.empty() || _from.empty())
00185         ReadPlain(IOfunc,name,Time,Level);
00186       else {
00187         BBox to(_to), from(_from);
00188         Coords ss(StepSize(IOfunc,Level));
00189         for (int i=0;i<dim;i++) {
00190           if (to.stepsize(i)>ss(i))
00191             to.refine(i,to.stepsize(i)/ss(i),0);
00192           else to.coarsen(i,ss(i)/to.stepsize(i));
00193           if (from.stepsize(i)>ss(i))
00194             from.refine(i,from.stepsize(i)/ss(i),0);
00195           else from.coarsen(i,ss(i)/from.stepsize(i));
00196         }
00197         ReadPlain(IOfunc,name,Time,Level,to,from);
00198       }
00199     }
00200     else {
00201       BBox where(_where);
00202       Coords ss(StepSize(IOfunc,Level));
00203       for (int i=0;i<dim;i++) {
00204         if (where.stepsize(i)>ss(i))
00205           where.refine(i,where.stepsize(i)/ss(i),0);
00206         else where.coarsen(i,ss(i)/where.stepsize(i));
00207       }
00208       ReadPlain(IOfunc,name,Time,Level,where);
00209     }
00210   }   
00211 
00212   void ReadPlain(grid_fct_type& IOfunc, const char* name, 
00213                  const int& Time, const int& Level,
00214                  const BBox& where=BBox::_empty_bbox) {
00215     int me = MY_PROC;     
00216     char ioname[DAGHBktGFNameWidth+16];
00217     std::sprintf(ioname,"%s",name);
00218     double t_old = GetPhysicalTime(IOfunc,Time,Level);
00219     if (me == VizServer) 
00220       ( std::cout << " *** Reading " << ioname << " " ).flush();
00221     if (where.empty()) 
00222       Read(IOfunc,Time,Level,DAGH_Double,ioname);      
00223     else
00224       Read(IOfunc,Time,Level,where,DAGH_Double,ioname);      
00225     double t = GetPhysicalTime(IOfunc,Time,Level);
00226     if (me == VizServer) 
00227       std::cout << "at t = " << t << " to level " << Level 
00228                 << " time index " << Time << std::endl;
00229     SetPhysicalTime(IOfunc,Time,Level,t_old);
00230 #ifndef DAGH_NO_MPI
00231     MPI_Barrier(comm_service::comm()); 
00232 #endif
00233   }   
00234 
00235   void ReadPlain(grid_fct_type& IOfunc, const char* name, 
00236                  const int& Time, const int& Level,
00237                  const BBox& to, const BBox& from) {
00238     int me = MY_PROC;     
00239     char ioname[DAGHBktGFNameWidth+16];
00240     std::sprintf(ioname,"%s",name);
00241     double t_old = GetPhysicalTime(IOfunc,Time,Level);
00242     if (me == VizServer) 
00243       ( std::cout << " *** Reading " << ioname << " " ).flush();
00244     if (to.empty() || from.empty()) 
00245       Read(IOfunc,Time,Level,DAGH_Double,ioname);      
00246     else
00247       Read(IOfunc,Time,Level,to,from,DAGH_Double,ioname);      
00248     double t = GetPhysicalTime(IOfunc,Time,Level);
00249     if (me == VizServer) 
00250       std::cout << "at t = " << t << " to level " << Level 
00251                 << " time index " << Time << std::endl;
00252     SetPhysicalTime(IOfunc,Time,Level,t_old);
00253 #ifndef DAGH_NO_MPI
00254     MPI_Barrier(comm_service::comm()); 
00255 #endif
00256   }   
00257 
00258   //---------------------------------------------------------------------- 
00259   // Input scalar data block
00260   //---------------------------------------------------------------------- 
00261   virtual void ReadIn(grid_data_type& IOdata, const char* name, 
00262                       const int& Time, const int& Level) {
00263     ReadPlain(IOdata,name,Time,Level);
00264   }   
00265 
00266   virtual void ReadPlain(grid_data_type& IOdata, const char* name, 
00267                          const int& Time, const int& Level) {
00268     int me = MY_PROC;     
00269     char ioname[DAGHBktGFNameWidth+16];
00270     std::sprintf(ioname,"%s_%d",name,Time); 
00271 
00272     gdhdr head;
00273     head.type = DAGHSingle;
00274     head.owner = me;
00275     head.gfid = 10000;
00276     head.gfdatatype = DAGH_Double;
00277     head.gfstaggertype = DAGHCellCentered;
00278     std::strncpy(head.gfname,ioname,DAGHBktGFNameWidth-1);
00279     head.bbox = IOdata.bbox();
00280     head.time = 0;
00281     head.time_value = Time;
00282     double Current_Time = 0.;
00283     std::memcpy(head.physical_time,(char *)&Current_Time,sizeof(double));
00284     head.level = Level;
00285     head.index = 0;
00286     if (me == VizServer) 
00287       ( std::cout << " *** Reading " << ioname << " " ).flush();
00288 
00289     (base::GH().DAGH_IORead())(base::GH(), &head, IOdata.data());
00290 
00291     std::memcpy((char *)&Current_Time,head.physical_time,sizeof(double));
00292     if (me == VizServer) 
00293       std::cout << "at t = " << Current_Time << std::endl;
00294   }  
00295 
00296   virtual void CloseIO() { DAGHIOClose(base::GH()); }
00297   inline void SetNcnt(const int& cnt) { _Ncnt = cnt; }
00298   inline const int& Ncnt() const { return _Ncnt; }
00299   inline const int& InputType() const { return _InputType; }
00300 
00301   inline void SetLevelTransfer(leveltransfer_type* _leveltransfer) 
00302   { _LevelTransfer = _leveltransfer; }
00303   inline leveltransfer_type& LevelTransfer_() { return *_LevelTransfer; }
00304   inline const leveltransfer_type& LevelTransfer_() const { return *_LevelTransfer; }
00305 
00306 protected:
00307   int _Ncnt, _InputType;
00308   std::string* CompName;
00309   ControlDevice LocCtrl, WhereCtrl, ToCtrl, FromCtrl;
00310   leveltransfer_type* _LevelTransfer;
00311   leveltransfer_functor_type *_ProlongFunc, *_RestrictFunc;  
00312   BBox _where, _to, _from;
00313 };
00314 
00315 
00316 #endif

Generated on Fri Aug 24 13:00:50 2007 for AMROC Fluid-solver Framework - by  doxygen 1.4.7