00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef AMROC_VISUALGRID_H
00010 #define AMROC_VISUALGRID_H
00011
00019 #include "VisualGridBase.h"
00020 #include "HierarchyStorage.h"
00021 #include "VisualGridData.h"
00022 #include "Evaluator.h"
00023
00024 #include "BBoxList.h"
00025 #include "DAGHDefaults.h"
00026
00027 #include <vector>
00028 #include <utility>
00029 #include <map>
00030 #include <functional>
00031 #include <algorithm>
00032 #include <iostream>
00033 #include <string>
00034 #include <cstdio>
00035 #include <cfloat>
00036
00043 template <class VectorType>
00044 class VisualGrid : public VisualGridBase {
00045 typedef typename VectorType::InternalDataType DataType;
00046
00047 public:
00048 typedef VisualGridData<VectorType> data_block_type;
00049 typedef std::vector<data_block_type*> block_list_type;
00050 typedef HierarchyStorage<DataType> hierarchy_storage_type;
00051 typedef typename hierarchy_storage_type::data_block_type hdata_block_type;
00052 typedef typename hierarchy_storage_type::block_list_type hblock_list_type;
00053 typedef typename hblock_list_type::iterator hblock_list_iterator;
00054 typedef typename data_block_type::direction direction;
00055 typedef std::pair<int, int> node_pair_type;
00056 typedef std::multimap<int, int> node_pair_list_type;
00057 typedef std::vector<float> point_list_type;
00058 typedef Evaluator<VectorType> evaluator_type;
00059 typedef VisualGridBase vgridbase_type;
00060
00061 VisualGrid(evaluator_type* ev) :
00062 vgridbase_type(ev), _FKeys(1), _MinLevel(0), _MaxLevel(0), _NLevels(1), _MaxProcs(1024),
00063 StepFormat(0), StepDirectories(0), _CutOut(1), _Eliminate(1), _EliminateValue(-1.e37), _parData(0) {
00064 PlotLevel = new int[NLevels()];
00065 int i;
00066 for (i=0; i<NLevels(); i++)
00067 PlotLevel[i] = 0;
00068
00069 VectorType dummy;
00070 VectorLength = dummy.length();
00071 CompName = new std::string[VectorLength];
00072 CompDir = "-";
00073 CompRead = new bool[VectorLength];
00074 for (i=0; i<VectorLength; i++)
00075 CompRead[i] = false;
00076 for (i=0; i<3; i++) {
00077 Symmetry[i] = 0; Periodic[i] = 0;
00078 show[2*i] = 0; show[2*i+1] = -1.0; cut[i] = FLT_MAX;
00079 show_exact[i] = 0;
00080 }
00081 DimUsed = Coords(3,1);
00082 Make3d = false;
00083 for (i=0; i<DAGHMaxLevels; i++)
00084 _RefineFactor[i] = DAGHDefaultRefineFactor;
00085 }
00086
00087 ~VisualGrid() {
00088 delete PlotLevel;
00089 delete [] CompName;
00090 delete [] CompRead;
00091 }
00092
00093
00094
00095
00096 virtual void BlockListAppend(hdata_block_type& workdata, BBox& bb, int& comp, int& Level,
00097 int& proc, float* dx, float* geom) = 0;
00098
00099
00100 virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00101 ControlDevice GHCtrl = Ctrl.getSubDevice("GridHierarchy");
00102 char VariableName[32];
00103 for (int d=0; d<3; d++) {
00104 std::sprintf(VariableName,"Cells(%d)",d+1);
00105 RegisterAt(GHCtrl,VariableName,shape[d]);
00106 std::sprintf(VariableName,"GeomMin(%d)",d+1);
00107 RegisterAt(GHCtrl,VariableName,geom[2*d]);
00108 std::sprintf(VariableName,"GeomMax(%d)",d+1);
00109 RegisterAt(GHCtrl,VariableName,geom[2*d+1]);
00110 }
00111 RegisterAt(GHCtrl,"MaxLevels",_NLevels);
00112 for (register int i=0; i<DAGHMaxLevels; i++) {
00113 std::sprintf(VariableName,"RefineFactor(%d)",i+1);
00114 RegisterAt(GHCtrl,VariableName,_RefineFactor[i]);
00115 }
00116
00117 ControlDevice FilesCtrl = Ctrl.getSubDevice("Files");
00118 for (int comp=0; comp<VectorLength; comp++) {
00119 char ComponentName[16];
00120 std::sprintf(ComponentName,"-");
00121 CompName[comp] = ComponentName;
00122 std::sprintf(ComponentName,"OutputName(%d)",comp+1);
00123 RegisterAt(FilesCtrl,ComponentName,CompName[comp]);
00124 }
00125 RegisterAt(FilesCtrl,"OutputDirectory",CompDir);
00126 RegisterAt(FilesCtrl,"StepFormat",StepFormat);
00127 RegisterAt(FilesCtrl,"StepDirectories",StepDirectories);
00128 }
00129 virtual void register_at(ControlDevice& Ctrl) {
00130 register_at(Ctrl, ""); }
00131
00132 virtual void update() {
00133 std::string iname;
00134 if (UpdateName()) iname = *UpdateName();
00135 else iname = "display.in";
00136 ControlDevice DisplayCtrl(GetFileControlDevice(iname.c_str(),""));
00137 RegisterAt(DisplayCtrl,"Type",_FKeys);
00138 RegisterAt(DisplayCtrl,"DisplayMinLevel",_MinLevel);
00139 RegisterAt(DisplayCtrl,"DisplayMaxLevel",_MaxLevel);
00140 RegisterAt(DisplayCtrl,"CutOut",_Eliminate);
00141 RegisterAt(DisplayCtrl,"CutOutValue",_EliminateValue);
00142 RegisterAt(DisplayCtrl,"MaxProcs",_MaxProcs);
00143 delete PlotLevel;
00144 PlotLevel = new int[NLevels()];
00145 for (int lev=0; lev<NLevels(); lev++) {
00146 char LevelName[16];
00147 std::sprintf(LevelName,"PlotGrid(%d)",lev);
00148 RegisterAt(DisplayCtrl,LevelName,PlotLevel[lev]);
00149 }
00150 for (int d=0; d<3; d++) {
00151 char VariableName[16];
00152 std::sprintf(VariableName,"ShowMin(%d)",d+1);
00153 RegisterAt(DisplayCtrl,VariableName,show[2*d]);
00154 std::sprintf(VariableName,"ShowMax(%d)",d+1);
00155 RegisterAt(DisplayCtrl,VariableName,show[2*d+1]);
00156 std::sprintf(VariableName,"ShowCut(%d)",d+1);
00157 RegisterAt(DisplayCtrl,VariableName,cut[d]);
00158 std::sprintf(VariableName,"Symmetry(%d)",d+1);
00159 RegisterAt(DisplayCtrl,VariableName,Symmetry[d]);
00160 std::sprintf(VariableName,"Periodic(%d)",d+1);
00161 RegisterAt(DisplayCtrl,VariableName,Periodic[d]);
00162 }
00163 TheEvaluator().register_at(DisplayCtrl,"");
00164 DisplayCtrl.update();
00165
00166 TheEvaluator().update();
00167 }
00168
00169 virtual int SetUp(int step, std::string** filenames, int Nfilenames) {
00170 vgridbase_type::SetUp(step,filenames,Nfilenames);
00171 bool initialize = true;
00172
00173 if (initialize) {
00174 for (register int i=0; i<NLevels()-1; i++)
00175 if (_RefineFactor[i]<DAGHDefaultRefineFactor)
00176 _RefineFactor[i] = DAGHDefaultRefineFactor;
00177 _RefineFactor[NLevels()-1] = 1;
00178 int d, ii;
00179 for (ii=1; ii<NLevels(); ii++)
00180 for (d=0; d<3; d++)
00181 shape[d] *= _RefineFactor[ii-1];
00182
00183 float dm = 1.e37;
00184 for (d=0; d<3; d++) {
00185 dx[d] = (geom[2*d+1]-geom[2*d]) / shape[d];
00186 if (dx[d]>0. && dx[d]<dm) dm = dx[d];
00187 }
00188 for (d=0; d<3; d++) {
00189 if (dx[d] <= 0.0) {
00190 DimUsed(d) = 0;
00191 dx[d] = dm;
00192 shape[d] = 1;
00193 for (int comp=VectorLength-1; comp>d; comp--)
00194 CompName[comp] = CompName[comp-1];
00195 if (VectorLength >= 4)
00196 CompName[d+1] = "-";
00197 }
00198 }
00199
00200 for (d=0; d<3; d++) {
00201 if (Symmetry[d] != 0) {
00202 shape[d] += shape[d];
00203 if (Symmetry[d] > 0)
00204 geom[2*d+1] += geom[2*d+1]-geom[2*d];
00205 else
00206 geom[2*d] -= geom[2*d+1]-geom[2*d];
00207 }
00208 else if (Periodic[d] > 0) {
00209 shape[d] += Periodic[d]*shape[d];
00210 geom[2*d+1] += Periodic[d]*(geom[2*d+1]-geom[2*d]);
00211 }
00212 }
00213
00214 for (d=0; d<3; d++) {
00215 if (DimUsed(d) && cut[d]>=geom[2*d] && cut[d]<=geom[2*d+1]) {
00216 show[2*d] = cut[d];
00217 show[2*d+1] = cut[d];
00218 show_exact[d] = 1;
00219 }
00220 if (DimUsed(d)==0 || show[2*d]>show[2*d+1]) {
00221 show_shape[2*d] = 0;
00222 show_shape[2*d+1] = shape[d];
00223 }
00224 else {
00225 show_shape[2*d] = int((Max(show[2*d],geom[2*d])-
00226 geom[2*d]) / dx[d]);
00227 show_shape[2*d+1] = int((Min(show[2*d+1],geom[2*d+1])-
00228 geom[2*d]+dx[d]/2.0) / dx[d]);
00229 }
00230 }
00231 }
00232
00233 int i;
00234 for (i=0; i<Nfilenames; i++)
00235 CompName[i] = *filenames[i];
00236 for (i=0; i<VectorLength; i++) {
00237 if (CompName[i].c_str()[0] == '-')
00238 continue;
00239
00240 char ioname[DAGHBktGFNameWidth+16];
00241 OutputFileName(ioname,CompName[i].c_str(),step);
00242 hierarchy_storage_type* HS = new hierarchy_storage_type(NLevels());
00243 _parData=HS->ReadIn(ioname, Symmetry, Periodic, _Eliminate,
00244 _EliminateValue, MaxProcs());
00245 if (_parData >= 0) {
00246 CompRead[i] = true;
00247 if (HS->RealTime() >= _RealTime) _RealTime = HS->RealTime();
00248 std::cerr << "Reading Component " << i << " : " << CompName[i]
00249 << " at " << RealTime() << std::endl;
00250 }
00251
00252 int* NCells = new int[MaxProcs()*NLevels()];
00253 int pc, Level;
00254 for (pc=0; pc<MaxProcs()*NLevels(); pc++) NCells[pc] = 0;
00255 for (Level=0; Level<NLevels(); Level++)
00256 for (hblock_list_iterator hsbit=HS->BlockList(Level).begin();
00257 hsbit!=HS->BlockList(Level).end(); hsbit++)
00258 NCells[MaxProcs()*Level+(*hsbit).second] +=
00259 (*hsbit).first->bbox().size();
00260 int* WCells = new int[MaxProcs()];
00261 int* Work = new int[MaxProcs()];
00262 int WholeCells = 0, WholeWork = 0, NProcs = 0;
00263 for (pc=0; pc<MaxProcs(); pc++) {
00264 WCells[pc] = NCells[pc];
00265 Work[pc] = NCells[pc];
00266 int fac = 1;
00267 for (Level=1; Level<NLevels(); Level++) {
00268 fac *= RefineFactor(Level-1);
00269 WCells[pc] += NCells[MaxProcs()*Level+pc];
00270 Work[pc] += fac*NCells[MaxProcs()*Level+pc];
00271 }
00272 WholeCells += WCells[pc];
00273 WholeWork += Work[pc];
00274 if (Work[pc] != 0)
00275 if (pc > NProcs) NProcs = pc;
00276 }
00277 for (pc=0; pc<MaxProcs(); pc++)
00278 if (WCells[pc] != 0)
00279 std::cerr << "Processor " << pc << ": " << WCells[pc]
00280 << " Cells, Load: "
00281 << float(Work[pc]) / (float(WholeWork) / float(NProcs+1))
00282 << std::endl;
00283 std::cerr << "On all Procs: " << WholeCells << " Cells" << std::endl;
00284 delete [] NCells;
00285 delete [] WCells;
00286 delete [] Work;
00287
00288 if (initialize) {
00289 if (_MinLevel < 0)
00290 _MinLevel = 0;
00291 if (_MinLevel >= HS->NLevels())
00292 _MinLevel = HS->NLevels()-1;
00293 if (_MaxLevel < 0)
00294 _MaxLevel = 0;
00295 if (_MaxLevel >= HS->NLevels())
00296 _MaxLevel = HS->NLevels()-1;
00297 }
00298
00299 typename block_list_type::iterator bit=BlockList().begin();
00300 for (Level=MaxLevel(); Level>=MinLevel(); Level--) {
00301 int sh[3];
00302 int d;
00303 for (d=0; d<3; d++) {
00304 sh[d] = show_shape[2*d+1];
00305 if (sh[d]>show_shape[2*d]) sh[d] -= 1;
00306 }
00307 globalbb = BBox(3,show_shape[0],show_shape[2],show_shape[4],
00308 sh[0],sh[1],sh[2],1);
00309
00310 for (d=0; d<3; d++) {
00311 int maxrf=1;
00312 for (int ii=(show_exact[d] ? Level : 0); ii<NLevels()-1; ii++)
00313 maxrf *= _RefineFactor[ii];
00314 globalbb.coarsen(d,maxrf);
00315 globalbb.refine(d,maxrf,0);
00316 }
00317
00318 int cnt = 0;
00319 int cells = 0;
00320 for (hblock_list_iterator hsbit=HS->BlockList(Level).begin();
00321 hsbit!=HS->BlockList(Level).end(); hsbit++, cnt++) {
00322 if (initialize) {
00323 cells += (*hsbit).first->bbox().size();
00324 BBox bb((*hsbit).first->bbox());
00325 bb *= globalbb;
00326 bb.lower() = (bb.lower()/bb.stepsize())*bb.stepsize();
00327 bb.upper() = (bb.upper()/bb.stepsize())*bb.stepsize();
00328 BBoxList bbl;
00329 bbl.add(bb);
00330 if (_CutOut) {
00331 BBoxList bblall;
00332 for (typename block_list_type::iterator bitall=BlockList().begin();
00333 bitall!=BlockList().end(); bitall++)
00334 bblall.add(coarsen((**bitall).DB().bbox(),
00335 (*hsbit).first->bbox().stepsize()/
00336 (**bitall).DB().bbox().stepsize()));
00337 bbl -= bblall;
00338 }
00339 if (!bbl.isempty())
00340 for (BBox* bbp = bbl.first(); bbp; bbp=bbl.next())
00341 if (!bbp->empty())
00342 BlockListAppend(*((*hsbit).first), *bbp, i, Level, (*hsbit).second, dx, geom);
00343 }
00344 else {
00345 if (bit!=BlockList().end()) {
00346 bool inside;
00347 do {
00348 BBox bb = (*hsbit).first->bbox()*(**bit).DB().bbox();
00349 if (!bb.empty() &&
00350 bb.stepsize()==(**bit).DB().bbox().stepsize()) {
00351 BeginFastIndex3(source, (*hsbit).first->bbox(), (*hsbit).first->data(),
00352 DataType);
00353 BeginFastIndex3(target, (**bit).DB().bbox(),
00354 (**bit).DB().data(), VectorType);
00355 for_3 (n, m, l, bb, bb.stepsize())
00356 FastIndex3(target,n,m,l)(i) = FastIndex3(source,n,m,l);
00357 end_for
00358 EndFastIndex3(source);
00359 EndFastIndex3(target);
00360 inside=true;
00361 bit++;
00362 }
00363 else inside=false;
00364 } while (inside && bit!=BlockList().end());
00365 }
00366 }
00367 }
00368 if (initialize)
00369 std::cerr << "On Level " << Level << ": " << cnt << " Grids with "
00370 << cells << " Cells read in." << std::endl;
00371 }
00372
00373 initialize = false;
00374 delete HS;
00375 }
00376
00377 if (initialize)
00378 return -1;
00379
00380 for (i=0; i<VectorLength; i++) {
00381 if (CompName[i].c_str()[0] == '-') {
00382 std::cerr << "Setting Component " << i << " to zero." << std::endl;
00383 for (typename block_list_type::iterator bit=BlockList().begin();
00384 bit!=BlockList().end(); bit++) {
00385 BeginFastIndex3(target, (**bit).DB().bbox(),
00386 (**bit).DB().data(), VectorType);
00387 for_3 (n, m, l, (**bit).DB().bbox(), (**bit).DB().bbox().stepsize())
00388 FastIndex3(target,n,m,l)(i) = DataType(0.0);
00389 end_for
00390 EndFastIndex3(target);
00391 }
00392 }
00393 }
00394 return 0;
00395 }
00396
00397 void OutputFileTime(char *time, const int& Time) {
00398 if (StepFormat<=0)
00399 std::sprintf(time,"%d",Time);
00400 else if (StepFormat==1) {
00401 if (Time<=999999)
00402 std::sprintf(time,"%6.6d",Time);
00403 else if (Time<=99999999)
00404 std::sprintf(time,"%8.8d",Time);
00405 else
00406 std::sprintf(time,"%d",Time);
00407 }
00408 }
00409
00410 void OutputFileName(char *ioname, const char* name, const int& Time) {
00411 char time[20], help[DAGHBktGFNameWidth+256];
00412 OutputFileTime(time,Time);
00413 if (CompDir.c_str()[0] != '-' && CompDir.length()>0)
00414 std::sprintf(ioname,"%s/",CompDir.c_str());
00415 else
00416 ioname[0]='\0';
00417 if (StepDirectories>0) {
00418 std::sprintf(help,"%s/",time);
00419 strcat(ioname,help);
00420 }
00421 std::sprintf(help,"%s_%s",name,time);
00422 strcat(ioname,help);
00423 }
00424
00425 virtual int NCells() {
00426 int cells=0;
00427 for (typename block_list_type::iterator bit=BlockList().begin();
00428 bit!=BlockList().end(); bit++)
00429 cells += (*bit)->NCells();
00430 return cells;
00431 }
00432 virtual int NNodes() {
00433 int nodes=0;
00434 for (typename block_list_type::iterator bit=BlockList().begin();
00435 bit!=BlockList().end(); bit++)
00436 nodes += (*bit)->NNodes();
00437 return nodes;
00438 }
00439
00440 virtual void SetBlocks(int blocks[]) {
00441 int offset=0; int noffset=0;
00442 for (typename block_list_type::iterator bit=BlockList().begin();
00443 bit!=BlockList().end(); bit++)
00444 (*bit)->SetBlocks(blocks,offset,noffset);
00445 }
00446
00447 virtual void SetGridCells(float xyz[][3]) {
00448 int offset=0;
00449 std::cerr << "SetGridCells()";
00450 for (typename block_list_type::iterator bit=BlockList().begin();
00451 bit!=BlockList().end(); bit++)
00452 (*bit)->SetGridCells(xyz,offset);
00453 std::cerr << std::endl;
00454 }
00455 virtual void SetGridNodes(float xyz[][3]) {
00456 int offset=0;
00457 std::cerr << "SetGridNodes()";
00458 for (typename block_list_type::iterator bit=BlockList().begin();
00459 bit!=BlockList().end(); bit++)
00460 (*bit)->SetGridNodes(xyz,offset);
00461 std::cerr << std::endl;
00462 }
00463
00464 virtual void SetGridConns(int con[][8]) {
00465 int offset=0;
00466 std::cerr << "SetGridConns()";
00467 for (typename block_list_type::iterator bit=BlockList().begin();
00468 bit!=BlockList().end(); bit++)
00469 (*bit)->SetGridConns(con,offset);
00470 std::cerr << std::endl;
00471 }
00472
00473 virtual void SetScalCells(int *key,float v[]) {
00474 int offset=0;
00475 std::cerr << "SetScalCells()";
00476 for (typename block_list_type::iterator bit=BlockList().begin();
00477 bit!=BlockList().end(); bit++)
00478 (*bit)->SetScalCells(key,v,offset,CompRead);
00479 std::cerr << std::endl;
00480 }
00481 virtual void SetScalNodes(int *key,float v[]) {
00482 int offset=0;
00483 std::cerr << "SetScalNodes()";
00484 for (typename block_list_type::iterator bit=BlockList().begin();
00485 bit!=BlockList().end(); bit++)
00486 (*bit)->SetScalNodes(key,v,offset,CompRead);
00487 std::cerr << std::endl;
00488 }
00489
00490 virtual void SetPointsCells(int *key,point_list_type &p,float v[]) {
00491 std::cerr << "SetPointsCells()";
00492 point_list_type p_tmp(p);
00493 for (register unsigned int i=0;i<p.size()/3;i++) v[i]=-1.e37;
00494 for (typename block_list_type::iterator bit=BlockList().begin();
00495 bit!=BlockList().end(); bit++)
00496 (*bit)->SetPointsCells(key,p_tmp,v,CompRead);
00497 std::cerr << std::endl;
00498 }
00499 virtual void SetPointsNodes(int *key,point_list_type &p,float v[]) {
00500 std::cerr << "SetPointsNodes()";
00501 point_list_type p_tmp(p);
00502 for (register unsigned int i=0;i<p.size()/3;i++) v[i]=-1.e37;
00503 for (typename block_list_type::iterator bit=BlockList().begin();
00504 bit!=BlockList().end(); bit++)
00505 (*bit)->SetPointsNodes(key,p_tmp,v,CompRead);
00506 std::cerr << std::endl;
00507 }
00508
00509 virtual void VectNodes(int *key,float v[][3]) {
00510 int offset=0;
00511 std::cerr << "VectNodes()";
00512 for (typename block_list_type::iterator bit=BlockList().begin();
00513 bit!=BlockList().end(); bit++)
00514 (*bit)->VectNodes(key,v,offset,CompRead);
00515 std::cerr << std::endl;
00516 }
00517 virtual void VectCells(int *key,float v[][3]) {
00518 int offset=0;
00519 std::cerr << "VectCells()";
00520 for (typename block_list_type::iterator bit=BlockList().begin();
00521 bit!=BlockList().end(); bit++)
00522 (*bit)->VectCells(key,v,offset,CompRead);
00523 std::cerr << std::endl;
00524 }
00525
00526 virtual int Size() const { return BlockList().size(); }
00527 virtual const int& MinLevel() const { return _MinLevel; }
00528 virtual const int& MaxLevel() const { return _MaxLevel; }
00529 virtual const int& NLevels() const { return _NLevels; }
00530 virtual const int& MaxProcs() const { return _MaxProcs; }
00531 virtual const int& RefineFactor(const int l) const
00532 { return _RefineFactor[l]; }
00533 virtual const BBox& GlobalBBox() const { return globalbb; }
00534 virtual const int& FKeys() const { return _FKeys; }
00535
00536 virtual evaluator_type& TheEvaluator() const
00537 { return (evaluator_type &) vgridbase_type::TheEvaluator(); }
00538 virtual evaluator_type& TheEvaluator()
00539 { return (evaluator_type &) vgridbase_type::TheEvaluator(); }
00540
00541 block_list_type& BlockList() { return *_BlockList; }
00542 block_list_type& BlockList() const { return *_BlockList; }
00543
00544 const Coords Dimensions() const { return ( Make3d ? Coords(3,1): DimUsed) ; }
00545
00546 protected:
00547 block_list_type* _BlockList;
00548 int _FKeys;
00549 int _MinLevel;
00550 int _MaxLevel;
00551 int _NLevels;
00552 int _MaxProcs;
00553 int _RefineFactor[DAGHMaxLevels];
00554 int* PlotLevel;
00555 int shape[3];
00556 BBox globalbb;
00557 float geom[2*3];
00558 float show[2*3];
00559 float cut[3];
00560 int show_shape[2*3];
00561 int show_exact[3];
00562 float dx[3];
00563 public:
00564 Coords DimUsed;
00565 bool Make3d;
00566 protected:
00567 int VectorLength;
00568 std::string* CompName;
00569 std::string CompDir;
00570 int StepFormat, StepDirectories;
00571 bool* CompRead;
00572 int Symmetry[3];
00573 int Periodic[3];
00574 int _CutOut;
00575 int _Eliminate;
00576 DataType _EliminateValue;
00577 int _parData;
00578 };
00579
00580 #endif