SWE
/import/home/rettenbs/src/SWE/src/scenarios/SWE_AsagiScenario.hh
Go to the documentation of this file.
00001 
00029 #ifndef SWEASAGISCENARIO_HPP_
00030 #define SWEASAGISCENARIO_HPP_
00031 
00032 #include <cassert>
00033 #include <cstring>
00034 #include <string>
00035 #include <iostream>
00036 #include <map>
00037 #include <asagi.h>
00038 #include "SWE_Scenario.hh"
00039 
00040 class SWE_AsagiGrid
00041 {
00042 private:
00044         asagi::Grid* _grid;
00046         unsigned int _refCount;
00047 public:
00048         SWE_AsagiGrid()
00049         {
00050                 _grid = asagi::Grid::create();
00051                 _refCount = 0;
00052         }
00053 
00054         void open(const std::string &i_filename)
00055         {
00056                 if (_refCount > 0)
00057                         return;
00058 
00059                 int l_asagiOpen = _grid->open(i_filename.c_str());
00060 
00061                 //open the grid
00062                 if( l_asagiOpen != 0 ) {
00063                         std::cout << "Could not open bathymetry file: " << i_filename << std::endl;
00064                 std::cout << "Error code: " << l_asagiOpen << std::endl;
00065                 assert(false);
00066                 }
00067 
00068                 _refCount++;
00069         }
00070 
00071         void close()
00072         {
00073                 _refCount--;
00074 
00075                 if (_refCount > 0)
00076                         // At least one more scenario is using this grid
00077                         // -> do nothing
00078                         return;
00079 
00080                 // This grid is no longer used
00081                 delete _grid;
00082         }
00083 
00084         asagi::Grid& grid()
00085         {
00086                 return *_grid;
00087         }
00088 };
00089 
00090 class SWE_AsagiScenario: public SWE_Scenario {
00091 //private:
00093         static std::map<std::string, SWE_AsagiGrid> grids;
00094 
00096     SWE_AsagiGrid &bathymetryGrid;
00097 
00099     SWE_AsagiGrid &displacementGrid;
00100 
00102     const bool dynamicDisplacement;
00103 
00105     const float duration;
00106 
00107 #ifndef NDEBUG
00108 
00109     float bathymetryRange[4];
00110 #endif
00111 
00113     float displacementRange[4];
00114 
00116     float dynamicDisplacementTimeRange[2];
00117 
00119    float simulationArea[4];
00120 
00121   public:
00131    SWE_AsagiScenario ( const std::string i_bathymetryFile,
00132                        const std::string i_displacementFile,
00133                        const float i_duration,
00134                        const float i_simulationArea[4],
00135                        const bool i_dynamicDisplacement = false ):
00136                        bathymetryGrid(grids[i_bathymetryFile]),
00137                        displacementGrid(grids[i_displacementFile]),
00138                        dynamicDisplacement(i_dynamicDisplacement),
00139                        duration(i_duration)
00140   {
00141 
00142        // open bathymetry grid
00143            bathymetryGrid.open(i_bathymetryFile);
00144 
00145        // open displacement grid
00146       displacementGrid.open(i_displacementFile);
00147 
00148 #ifndef NDEBUG
00149       //read grid information
00150       bathymetryRange[0] = bathymetryGrid.grid().getXMin();
00151       bathymetryRange[1] = bathymetryGrid.grid().getXMax();
00152       bathymetryRange[2] = bathymetryGrid.grid().getYMin();
00153       bathymetryRange[3] = bathymetryGrid.grid().getYMax();
00154 #endif
00155 
00156       displacementRange[0] = displacementGrid.grid().getXMin();
00157       displacementRange[1] = displacementGrid.grid().getXMax();
00158       displacementRange[2] = displacementGrid.grid().getYMin();
00159       displacementRange[3] = displacementGrid.grid().getYMax();
00160       if(dynamicDisplacement == false) {
00161         dynamicDisplacementTimeRange[0] = dynamicDisplacementTimeRange[1] = 0;
00162       }
00163       else {
00164         dynamicDisplacementTimeRange[0] = displacementGrid.grid().getZMin();
00165         dynamicDisplacementTimeRange[1] = displacementGrid.grid().getZMax();
00166       }
00167 
00168       memcpy(simulationArea, i_simulationArea, sizeof(simulationArea));
00169 
00170 #ifndef NDEBUG
00171 #ifdef PRINT_ASAGI_INFORMATION
00172       //print information
00173       std::cout << "  *** scenarios::Asagi created" << std::endl
00174                 << "    i_bathymetryFile=" << i_bathymetryFile << std::endl
00175                 << "    i_displacementFile=" << i_displacementFile << std::endl
00176                 << "    duration= " << duration << std::endl
00177                 << "    bathymetryRange[0]=" << bathymetryRange[0] << std::endl
00178                 << "    bathymetryRange[1]=" << bathymetryRange[1] << std::endl
00179                 << "    bathymetryRange[2]=" << bathymetryRange[2] << std::endl
00180                 << "    bathymetryRange[3]=" << bathymetryRange[3] << std::endl
00181                 << "    displacementRange[0]=" << displacementRange[0] << std::endl
00182                 << "    displacementRange[1]=" << displacementRange[1] << std::endl
00183                 << "    displacementRange[2]=" << displacementRange[2] << std::endl
00184                 << "    displacementRange[3]=" << displacementRange[3] << std::endl
00185                 << "    dynamicDisplacementTimeRange[0]=" << dynamicDisplacementTimeRange[0] << std::endl
00186                 << "    dynamicDisplacementTimeRange[1]=" << dynamicDisplacementTimeRange[1] << std::endl
00187                 << "    simulationArea[0]=" << simulationArea[0] << std::endl
00188                 << "    simulationArea[1]=" << simulationArea[1] << std::endl
00189                 << "    simulationArea[2]=" << simulationArea[2] << std::endl
00190                 << "    simulationArea[3]=" << simulationArea[3] << std::endl;
00191 #endif
00192 #endif
00193 
00194     }
00195 
00196     virtual ~SWE_AsagiScenario() {
00197     }
00198 
00199     void deleteGrids() {
00200       bathymetryGrid.close();
00201       displacementGrid.close();
00202     }
00203 
00204     //methods from SWE_SCENARIO
00205 
00213     float getWaterHeight( float i_positionX,
00214                           float i_positionY ) {
00215       assert(i_positionX > bathymetryRange[0]);
00216       assert(i_positionX < bathymetryRange[1]);
00217       assert(i_positionY > bathymetryRange[2]);
00218       assert(i_positionY < bathymetryRange[3]);
00219 
00220       float bathymetryValue = bathymetryGrid.grid().getFloat2D(i_positionX, i_positionY);
00221 
00222       if( bathymetryValue > (float)0. ) {
00223         return 0.;
00224       }
00225       else {
00226         return -bathymetryValue;
00227       }
00228     }
00229 
00237     float getBathymetry( const float i_positionX,
00238                          const float i_positionY ) {
00239       //assert that the 2D wrapper is not used for 3D displacements
00240       //assert(dynamicDisplacement == false);
00241       // no assertation for compability
00242 
00243       return getBathymetryAndDynamicDisplacement(i_positionX, i_positionY, 0);
00244     }
00245 
00254     float getBathymetryAndDynamicDisplacement( const float i_positionX,
00255                                                const float i_positionY,
00256                                                const float i_time ) {
00257       assert(i_positionX > bathymetryRange[0]);
00258       assert(i_positionX < bathymetryRange[1]);
00259       assert(i_positionY > bathymetryRange[2]);
00260       assert(i_positionY < bathymetryRange[3]);
00261 
00262       float bathymetryValue = bathymetryGrid.grid().getFloat2D(i_positionX, i_positionY);
00263 
00264       //bathymetryValue = (float) 0.; //TODO: remove: old file format
00265 
00266       float displacementValue = (float) 0.;
00267 
00268       if ( i_positionX > displacementRange[0] &&
00269            i_positionX < displacementRange[1] &&
00270            i_positionY > displacementRange[2] &&
00271            i_positionY < displacementRange[3] ) {
00272         if(dynamicDisplacement == false)
00273           displacementValue = displacementGrid.grid().getFloat2D(i_positionX, i_positionY);
00274         else
00275           displacementValue = displacementGrid.grid().getFloat3D(i_positionX, i_positionY, i_time);
00276       }
00277 
00278       return bathymetryValue + displacementValue;
00279     }
00280 
00286     bool dynamicDisplacementAvailable(const float i_time) {
00287       if( i_time > dynamicDisplacementTimeRange[0] &&
00288           i_time < dynamicDisplacementTimeRange[1] )
00289         return true;
00290       else
00291         return false;
00292     }
00293 
00298     float endSimulation() {
00299       return duration;
00300     };
00301 
00307     BoundaryType getBoundaryType( BoundaryEdge i_edge ) {
00308       //nothing other than outflow/transparent boundary conditions makes sense in a real simulation
00309       return OUTFLOW;
00310     }
00311 
00317     float getBoundaryPos(BoundaryEdge i_edge) {
00318        if ( i_edge == BND_LEFT )
00319          return simulationArea[0];
00320        else if ( i_edge == BND_RIGHT)
00321          return simulationArea[1];
00322        else if ( i_edge == BND_BOTTOM )
00323          return simulationArea[2];
00324        else
00325          return simulationArea[3];
00326     };
00327 };
00328 
00329 #endif /* SWEASAGISCENARIO_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends