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 int me = MY_PROC;
00156 char ioname[DAGHBktGFNameWidth+256];
00157 OutputFileName(ioname,name,Time);
00158 if (std::strlen(ioname)>=DAGHBktGFNameWidth)
00159 ioname[DAGHBktGFNameWidth-1]='\0';
00160
00161 double t_old = GetPhysicalTime(IOfunc,Time,Level);
00162 SetPhysicalTime(IOfunc,Time,Level,t);
00163 if (Level == 0 && me == VizServer)
00164 std::cout << " *** Writing " << ioname << " at t = " << t << std::endl;
00165 if (where.empty())
00166 Write(IOfunc,Time,Level,DAGH_Double,ioname);
00167 else
00168 Write(IOfunc,Time,Level,where,DAGH_Double,ioname);
00169 SetPhysicalTime(IOfunc,Time,Level,t_old);
00170 DAGHIOFlush(base::GH());
00171 }
00172
00173
00174
00175
00176 virtual void WriteOut(grid_data_type& IOdata, const char* name,
00177 const int& Time, const int& Level, const double& t) {
00178 WritePlain(IOdata,name,Time,Level,t);
00179 }
00180
00181 virtual void WritePlain(grid_data_type& IOdata, const char* name,
00182 const int& Time, const int& Level, const double& t) {
00183 int me = MY_PROC;
00184 char ioname[DAGHBktGFNameWidth+256];
00185 OutputFileName(ioname,name,Time);
00186 if (std::strlen(ioname)>=DAGHBktGFNameWidth)
00187 ioname[DAGHBktGFNameWidth-1]='\0';
00188
00189 gdhdr head;
00190 head.type = DAGHSingle;
00191 head.owner = me;
00192 head.gfid = 10000;
00193 head.gfdatatype = DAGH_Double;
00194 head.gfstaggertype = DAGHCellCentered;
00195 std::strncpy(head.gfname,ioname,DAGHBktGFNameWidth-1);
00196 head.bbox = IOdata.bbox();
00197 head.time = 0;
00198 head.time_value = Time;
00199 double Current_Time = t;
00200 std::memcpy(head.physical_time,(char *)&Current_Time,sizeof(double));
00201 head.level = Level;
00202 head.index = 0;
00203 if (me == VizServer)
00204 std::cout << " *** Writing " << ioname << " at t = " << t << std::endl;
00205
00206 (base::GH().DAGH_IOWrite())(base::GH(), &head, IOdata.data());
00207 }
00208
00209 void OutputFileName(char *ioname, const char* name, const int& Time) {
00210 char time[20], help[DAGHBktGFNameWidth+256];
00211 if (StepFormat<=0)
00212 std::sprintf(time,"%d",Time);
00213 else if (StepFormat==1) {
00214 if (Time<=999999)
00215 std::sprintf(time,"%6.6d",Time);
00216 else if (Time<=99999999)
00217 std::sprintf(time,"%8.8d",Time);
00218 else
00219 std::sprintf(time,"%d",Time);
00220 }
00221 if (CompDir.c_str()[0] != '-' && CompDir.length()>0)
00222 std::sprintf(ioname,"%s/",CompDir.c_str());
00223 else
00224 ioname[0]='\0';
00225 if (StepDirectories>0) {
00226 std::sprintf(help,"%s/",time);
00227 std::strcat(ioname,help);
00228 }
00229 std::sprintf(help,"%s_%s",name,time);
00230 std::strcat(ioname,help);
00231 }
00232
00233 virtual void CloseIO() { DAGHIOClose(base::GH()); }
00234 inline void SetNcnt(const int& cnt) { _Ncnt = cnt; }
00235 inline const int& Ncnt() const { return _Ncnt; }
00236 inline const int& OutputType() const { return _OutputType; }
00237 inline const std::string& OutputName(const int cnt) const
00238 { assert (cnt>=0 && cnt<MAXCOMPONENTS); return CompName[cnt]; }
00239
00240 protected:
00241 int _Ncnt, _OutputType;
00242 int StepFormat, StepDirectories;
00243 std::string* CompName;
00244 std::string CompDir;
00245 ControlDevice LocCtrl, WhereCtrl;
00246 BBox _where;
00247 };
00248
00249
00250 #endif