Gowdy solver
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
grid/gridInitialData.h
Go to the documentation of this file.
00001 /**
00002  *  @file      gridInitialData.h
00003  *  @brief     Reads initial data into the grid.
00004  *  @author    Mikica Kocic
00005  *  @copyright GNU General Public License (GPLv3).
00006  */
00007 
00008 /** Reads the initial data from the given file.
00009  */
00010 class GridInitialData
00011 {
00012     UniformGrid* gridDriver; //!< The grid
00013     bool  binaryFormat;   //!< Read binary data from the input file.
00014     std::string fileName; //!< Input filename.
00015     std::vector<Int> input; //! The list of grid functions
00016 
00017 public:
00018 
00019     /** Constructs the initial data reader as specified in the parameter file.
00020      */
00021     GridInitialData( Parameters& params, UniformGrid& ug )
00022         : gridDriver(&ug), binaryFormat(false)
00023     {
00024         input.reserve( 100 );
00025         input.push_back( fld::r );
00026 
00027         params.get( "input.file",   fileName, "stdin" );
00028         params.get( "input.binary", binaryFormat, true );
00029 
00030         slog << "Initial Data:" << std::endl << std::endl
00031              << "    file = " << fileName << ",  binary = " << binaryFormat
00032              << std::endl << std::endl;
00033     }
00034 
00035     /** The given grid functions will be read as the initial data.
00036      */
00037     void gridFunctions( const std::vector<Int>& gfs )
00038     {
00039         input.insert( input.end(), gfs.begin(), gfs.end() );
00040     }
00041 
00042     /** Loads the initial data.
00043      */
00044     bool load ()
00045     {
00046         GridPoint** grid = gridDriver->get_grid ();
00047         Int nLen   = gridDriver->get_nLen ();
00048         Int nGhost = gridDriver->get_nGhost ();
00049 
00050         FILE* inf = fileName == "stdin" ? stdin
00051                   : fopen( fileName.c_str(), binaryFormat ? "rb" : "r" );
00052         if( ! inf ) {
00053             std::cerr << "Error: Could not open " << fileName << std::endl;
00054             return false;
00055         }
00056 
00057         // If using MPI, offset the initial data depending on our rank
00058         if ( gridDriver->mpiSize () > 1 )
00059         {
00060             for( Int n = 0; n < nLen * gridDriver->mpiRank(); ++n )
00061             {
00062                 if( ! IORecord( grid[0][0] ).read( inf, binaryFormat, input ) ) { // dummy read
00063                     return false;
00064                 }
00065             }
00066         }
00067 
00068         for( Int n = nGhost; n < nGhost + nLen; ++n )
00069         {
00070             if( ! IORecord( grid[0][n] ).read( inf, binaryFormat, input ) ) {
00071                 return false;
00072             }
00073         }
00074 
00075         if ( inf != stdin ) {
00076             fclose( inf );
00077         }
00078 
00079         return true;
00080     }
00081 };