00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef AMROC_FILE_OUTPUT_H
00010 #define AMROC_FILE_OUTPUT_H
00011
00019 #include "DAGHIO.h"
00020
00027 template <class VectorType, int dim>
00028 class FileOutput : public AMRBase<VectorType,dim> {
00029 typedef AMRBase<VectorType,dim> base;
00030 typedef typename VectorType::InternalDataType DataType;
00031
00032 public:
00033 typedef typename base::vec_grid_fct_type vec_grid_fct_type;
00034 typedef typename base::vec_grid_data_type vec_grid_data_type;
00035 typedef GridFunction<DataType,dim> grid_fct_type;
00036 typedef GridData<DataType,dim> grid_data_type;
00037
00038 FileOutput() : base(), _Ncnt(0), _OutputType(DAGHIO_HDF_NCSA),
00039 StepFormat(0), StepDirectories(0) {
00040 CompName = (std::string*) 0;
00041 CompDir = "-";
00042 _where = BBox(dim,1);
00043 }
00044
00045 virtual ~FileOutput() {
00046 if (CompName) delete [] CompName;
00047 }
00048
00049 virtual void register_at(ControlDevice& Ctrl) { register_at(Ctrl, ""); }
00050 virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00051 LocCtrl = Ctrl.getSubDevice(prefix+"Files");
00052 RegisterAt(LocCtrl,"Type",_OutputType);
00053 if (CompName) delete [] CompName;
00054 CompName = new std::string[MAXCOMPONENTS];
00055 for (int d=0; d<MAXCOMPONENTS; d++) {
00056 char Name[16];
00057 std::sprintf(Name,"-");
00058 CompName[d] = Name;
00059 std::sprintf(Name,"OutputName(%d)",d+1);
00060 RegisterAt(LocCtrl,Name,CompName[d]);
00061 }
00062 RegisterAt(LocCtrl,"OutputDirectory",CompDir);
00063 RegisterAt(LocCtrl,"StepFormat",StepFormat);
00064 RegisterAt(LocCtrl,"StepDirectories",StepDirectories);
00065 WhereCtrl = LocCtrl.getSubDevice(prefix+"Where");
00066 for (int d=0; d<dim; d++) {
00067 char text[8];
00068 std::sprintf(text,"lb(%d)",d+1);
00069 RegisterAt(WhereCtrl,text,_where.lower(d));
00070 std::sprintf(text,"ub(%d)",d+1);
00071 RegisterAt(WhereCtrl,text,_where.upper(d));
00072 std::sprintf(text,"s(%d)",d+1);
00073 RegisterAt(WhereCtrl,text,_where.stepsize(d));
00074 }
00075 }
00076 virtual void init() { DAGHIOInit(); }
00077 virtual void update() {
00078 int d=MAXCOMPONENTS;
00079 for (; d>0; d--)
00080 if (CompName[d-1].c_str()[0] != '-')
00081 break;
00082 if (d>0) SetNcnt(d);
00083 }
00084 virtual void finish() {
00085 if (base::_Hierarchy) CloseIO();
00086 #ifndef DAGH_NO_MPI
00087 if (comm_service::io_enabled() && comm_service::proc_num()>1) {
00088 double start_finalize = MPI_Wtime();
00089 while (MPI_Wtime()-start_finalize < 10.0) {}
00090 }
00091 #endif
00092 if (base::_Hierarchy) DAGHIOEnd(base::GH());
00093 if (CompName) {
00094 delete [] CompName;
00095 CompName = (std::string*) NULL;
00096 }
00097 }
00098
00099 virtual void SetupData(GridHierarchy* gh, const int& ghosts) {
00100 base::SetupData(gh, ghosts);
00101 DAGHSetIOType(base::GH(), _OutputType);
00102 }
00103
00104
00105
00106
00107 virtual void WriteOut(vec_grid_fct_type& u, grid_fct_type& IOfunc) {
00108 for (int cnt=0; cnt<Ncnt(); cnt++) {
00109 if (CompName[cnt].c_str()[0] == '-')
00110 continue;
00111 for (int lev=0; lev<=FineLevel(base::GH()); lev++) {
00112 int Time = CurrentTime(base::GH(),lev);
00113 double t = GetPhysicalTime(u,Time,lev);
00114 forall (u,Time,lev,c)
00115 equals_from(IOfunc(Time,lev,c), u(Time,lev,c), cnt);
00116 end_forall
00117 WriteOut(IOfunc,CompName[cnt].c_str(),Time,lev,t);
00118 }
00119 }
00120 }
00121
00122
00123
00124
00125 virtual void WriteOut(grid_fct_type& IOfunc, const char* name) {
00126 double t = GetPhysicalTime(IOfunc,CurrentTime(base::GH(),0),0);
00127 for (int lev=0; lev<=FineLevel(base::GH()); lev++) {
00128 int Time = CurrentTime(base::GH(),lev);
00129 WriteOut(IOfunc,name,Time,lev,t);
00130 }
00131 }
00132
00133
00134
00135
00136 virtual void WriteOut(grid_fct_type& IOfunc, const char* name,
00137 const int& Time, const int& Level, const double& t) {
00138 if (_where.empty())
00139 WritePlain(IOfunc,name,Time,Level,t);
00140 else {
00141 BBox where(_where);
00142 Coords ss(StepSize(IOfunc,Level));
00143 for (int i=0;i<dim;i++) {
00144 if (where.stepsize(i)>ss(i))
00145 where.refine(i,where.stepsize(i)/ss(i),0);
00146 else where.coarsen(i,ss(i)/where.stepsize(i));
00147 }
00148 WritePlain(IOfunc,name,Time,Level,t,where);
00149 }
00150 }
00151
00152 void WritePlain(grid_fct_type& IOfunc, const char* name,
00153 const int& Time, const int& Level, const double& t,
00154 const BBox& where=BBox::_empty_bbox,
00155 const bool erasefile=true,
00156 const bool delayedflush=true) {
00157 int me = MY_PROC;
00158 char ioname[DAGHBktGFNameWidth+256];
00159 OutputFileName(ioname,name,Time);
00160 if (std::strlen(ioname)>=DAGHBktGFNameWidth)
00161 ioname[DAGHBktGFNameWidth-1]='\0';
00162
00163 double t_old = GetPhysicalTime(IOfunc,Time,Level);
00164 SetPhysicalTime(IOfunc,Time,Level,t);
00165
00166 if (Level==0) {
00167 if (me==VizServer)
00168 std::cout << " *** Writing " << ioname << " at t = " << t << std::endl;
00169 if (erasefile) {
00170 char filename[256];
00171 filename[0]='\0';
00172 if (me==VizServer)
00173 std::sprintf(filename,"%s.hdf",ioname);
00174 #ifndef DAGH_NO_MPI
00175 if ((!comm_service::io_enabled()) && comm_service::proc_num()>1)
00176 std::sprintf(filename,"%s.%d.hdf",ioname,me);
00177 #endif
00178 if (filename[0]!='\0')
00179 remove(filename);
00180 }
00181 }
00182
00183 if (where.empty())
00184 Write(IOfunc,Time,Level,DAGH_Double,ioname);
00185 else
00186 Write(IOfunc,Time,Level,where,DAGH_Double,ioname);
00187 SetPhysicalTime(IOfunc,Time,Level,t_old);
00188 if (!delayedflush || Level==FineLevel(base::GH()))
00189 DAGHIOFlush(base::GH());
00190 }
00191
00192
00193
00194
00195 virtual void WriteOut(grid_data_type& IOdata, const char* name,
00196 const int& Time, const int& Level, const double& t) {
00197 WritePlain(IOdata,name,Time,Level,t);
00198 }
00199
00200 virtual void WritePlain(grid_data_type& IOdata, const char* name,
00201 const int& Time, const int& Level, const double& t) {
00202 int me = MY_PROC;
00203 char ioname[DAGHBktGFNameWidth+256];
00204 OutputFileName(ioname,name,Time);
00205 if (std::strlen(ioname)>=DAGHBktGFNameWidth)
00206 ioname[DAGHBktGFNameWidth-1]='\0';
00207
00208 gdhdr head;
00209 head.type = DAGHSingle;
00210 head.owner = me;
00211 head.gfid = 10000;
00212 head.gfdatatype = DAGH_Double;
00213 head.gfstaggertype = DAGHCellCentered;
00214 std::strncpy(head.gfname,ioname,DAGHBktGFNameWidth-1);
00215 head.bbox = IOdata.bbox();
00216 head.time = 0;
00217 head.time_value = Time;
00218 double Current_Time = t;
00219 std::memcpy(head.physical_time,(char *)&Current_Time,sizeof(double));
00220 head.level = Level;
00221 head.index = 0;
00222 if (me == VizServer)
00223 std::cout << " *** Writing " << ioname << " at t = " << t << std::endl;
00224
00225 (base::GH().DAGH_IOWrite())(base::GH(), &head, IOdata.data());
00226 }
00227
00228 void OutputFileName(char *ioname, const char* name, const int& Time) {
00229 char time[20], help[DAGHBktGFNameWidth+256];
00230 if (StepFormat<=0)
00231 std::sprintf(time,"%d",Time);
00232 else if (StepFormat==1) {
00233 if (Time<=999999)
00234 std::sprintf(time,"%6.6d",Time);
00235 else if (Time<=99999999)
00236 std::sprintf(time,"%8.8d",Time);
00237 else
00238 std::sprintf(time,"%d",Time);
00239 }
00240 if (CompDir.c_str()[0] != '-' && CompDir.length()>0)
00241 std::sprintf(ioname,"%s/",CompDir.c_str());
00242 else
00243 ioname[0]='\0';
00244 if (StepDirectories>0) {
00245 std::sprintf(help,"%s/",time);
00246 std::strcat(ioname,help);
00247 }
00248 std::sprintf(help,"%s_%s",name,time);
00249 std::strcat(ioname,help);
00250 }
00251
00252 virtual void CloseIO() { DAGHIOClose(base::GH()); }
00253 inline void SetNcnt(const int& cnt) { _Ncnt = cnt; }
00254 inline const int& Ncnt() const { return _Ncnt; }
00255 inline const int& OutputType() const { return _OutputType; }
00256 inline const std::string& OutputName(const int cnt) const
00257 { assert (cnt>=0 && cnt<MAXCOMPONENTS); return CompName[cnt]; }
00258
00259 protected:
00260 int _Ncnt, _OutputType;
00261 int StepFormat, StepDirectories;
00262 std::string* CompName;
00263 std::string CompDir;
00264 ControlDevice LocCtrl, WhereCtrl;
00265 BBox _where;
00266 };
00267
00268
00269 #endif