![]() |
Gowdy solver
|
00001 /** 00002 * @file gridPoint.h 00003 * @brief Definitions of the GridPoint and the IORecord (input/output) class. 00004 * @author Mikica Kocic 00005 * @copyright GNU General Public License (GPLv3). 00006 */ 00007 00008 ///////////////////////////////////////////////////////////////////////////////////////// 00009 /** @defgroup g4 Grid driver */ 00010 ///////////////////////////////////////////////////////////////////////////////////////// 00011 /*@{*/ 00012 /** A set of variables that is tracked on a grid point. 00013 */ 00014 typedef Real GridPoint[ GFCNT ]; 00015 00016 /** A wrapper for @ref GridPoint so that @ref GridPoint's array of fields 00017 * can be treated as a compound object when reading/writing. 00018 */ 00019 class IORecord 00020 { 00021 Real* gridSlice; //!< The array of fields values at the grid point 00022 public: 00023 00024 IORecord( GridPoint& gp ) 00025 : gridSlice( gp ) 00026 {} 00027 00028 /** Resets all the variables to a specific value (by default NAN). 00029 */ 00030 void clear( Real value = NAN ) 00031 { 00032 // We assume that Reals are tightly packed in the struture. 00033 auto len = sizeof(GridPoint) / sizeof(Real); 00034 for( Real* fp = gridSlice; len > 0; --len, ++fp ) { 00035 *fp = value; 00036 } 00037 } 00038 00039 /** Reads the variable values from a file stream. 00040 */ 00041 bool read( FILE* inf, bool isBinary, const std::vector<Int>& input ) 00042 { 00043 clear (); 00044 00045 auto len = input.size(); 00046 Real data[ len ]; 00047 00048 if( isBinary ) 00049 { 00050 if( len != fread( data, sizeof(data[0]), len, inf ) ) { 00051 std::cerr << "Error: Premature end of grid." << std::endl; 00052 return false; 00053 } 00054 } 00055 else 00056 { 00057 char line[ input.size() * 80 ] = {0}; 00058 do { 00059 if( ! fgets( line, sizeof(line)-1, inf ) ) { 00060 std::cerr << "Error: Premature end of grid." << std::endl; 00061 return false; 00062 } 00063 } while( line[0] == '*' ); // Ignore comments (starting with '*') 00064 00065 size_t count = sscanf_Real( line, data, len ); 00066 if( count != len ) { 00067 std::cerr << "Error: Wrong number of variables." << std::endl; 00068 return false; 00069 } 00070 } 00071 00072 for( size_t i = 0; i < len; ++i ) { 00073 gridSlice[ input[i] ] = data[i]; 00074 } 00075 00076 return true; 00077 } 00078 00079 /** Writes the variable values to a file stream. 00080 * @warning The order of fields in data[] (output data record) 00081 * should match the Mathematica notebook! 00082 */ 00083 void write( FILE* outf, bool isBinary, const std::vector<Int> output ) 00084 { 00085 auto len = output.size(); /// @todo fld::output should be dynamic in ID class 00086 Real data[ len ]; 00087 00088 for( size_t i = 0; i < len; ++i ) { 00089 data[i] = gridSlice[ output[i] ]; 00090 } 00091 00092 if( isBinary ) { 00093 fwrite( data, sizeof(data[0]), len, outf ); 00094 } 00095 else { 00096 for( size_t i = 0; i < len; ++i ) { 00097 if ( i > 0 ) fputs( "\t", outf ); 00098 fputReal( outf, data[i] ); 00099 } 00100 fputs( "\n", outf ); 00101 } 00102 } 00103 }; 00104 /*@}*/ 00105 /////////////////////////////////////////////////////////////////////////////////////////