|
| 1 | +#include "Config.h" |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +namespace Utilities |
| 6 | +{ |
| 7 | + |
| 8 | + //----------------------------------- |
| 9 | + // Read |
| 10 | + //----------------------------------- |
| 11 | + bool ConfigFile::Read( const std::string & ConfigFilename ) |
| 12 | + { |
| 13 | + Size_Type nBufferSize = 0; |
| 14 | + char *pBuffer = ReadFileToBuf( nBufferSize, ConfigFilename); |
| 15 | + |
| 16 | + if( nBufferSize<= 0 || !pBuffer ) |
| 17 | + { |
| 18 | + std::cerr << "ERROR: Unable to read config file" << endl; |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + if( !Parse( std::string( pBuffer, nBufferSize ) ) ) |
| 23 | + { |
| 24 | + std::cerr << "ERROR: Unable to parse config file" << endl; |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + if( pBuffer ) |
| 29 | + delete[] pBuffer; |
| 30 | + |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + //----------------------------------- |
| 35 | + // Parser |
| 36 | + //----------------------------------- |
| 37 | + bool ConfigFile::Parse( const string & sBuf ) |
| 38 | + { |
| 39 | + |
| 40 | + const char* KeywordStrings[] = |
| 41 | + { |
| 42 | + |
| 43 | + "#", // comment |
| 44 | + |
| 45 | + "GridDimension", |
| 46 | + "InputStrainRate", |
| 47 | + "MaxNRIter", |
| 48 | + "TimeStep", |
| 49 | + "NumTimeIter", |
| 50 | + "NRTolerence", |
| 51 | + "TwoGrainInit", |
| 52 | + "FileInit", |
| 53 | + |
| 54 | + "CRSS", |
| 55 | + "GammaDotBase", |
| 56 | + "RateSensitivity" |
| 57 | + }; |
| 58 | + |
| 59 | + enum EKeyword |
| 60 | + { |
| 61 | + // comment |
| 62 | + eComment, |
| 63 | + |
| 64 | + eGridDimension, |
| 65 | + eInputStrainRate, |
| 66 | + eMaxNRIter, |
| 67 | + eTimeStep, |
| 68 | + eNumTimeIter, |
| 69 | + eNRTolerence, |
| 70 | + eTwoGrainInit, |
| 71 | + eFileInit, |
| 72 | + |
| 73 | + eCRSS, |
| 74 | + eGammaDotBase, |
| 75 | + eRateSensitivity, |
| 76 | + |
| 77 | + // error check on number of keywords |
| 78 | + eNumKeywords |
| 79 | + }; |
| 80 | + |
| 81 | + vector< vector<string> > vsTokens; |
| 82 | + Tokenize( vsTokens, sBuf, " \t\n"); |
| 83 | + |
| 84 | + |
| 85 | + bool *vInitializationCheck; |
| 86 | + bool *vRequirementCheck; |
| 87 | + vInitializationCheck = new bool[eNumKeywords]; // a check list to see which variable is not initialized |
| 88 | + vRequirementCheck = new bool[eNumKeywords]; // a check list to see which variable is not initialized |
| 89 | + |
| 90 | + for( int i = 0; i < eNumKeywords; i ++ ) |
| 91 | + { |
| 92 | + vInitializationCheck[i] = false; |
| 93 | + vRequirementCheck[i] = true; |
| 94 | + } |
| 95 | + vRequirementCheck[eFileInit] = false; |
| 96 | + vRequirementCheck[eTwoGrainInit] = false; |
| 97 | + |
| 98 | + |
| 99 | + for(Size_Type i = 0; i < vsTokens.size(); i ++) |
| 100 | + { |
| 101 | + Size_Type iFirstToken; |
| 102 | + |
| 103 | + if ( vsTokens[i].size() == 0) |
| 104 | + { |
| 105 | + iFirstToken = eComment; |
| 106 | + } |
| 107 | + else if ( vsTokens[i][0].find_first_of( KeywordStrings[ eComment ] ) == 0) // if first token is comment |
| 108 | + { |
| 109 | + iFirstToken = eComment; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + // Identify the keyword in the beginning of the line |
| 114 | + for( iFirstToken = 0; iFirstToken < eNumKeywords; iFirstToken ++ ) |
| 115 | + { |
| 116 | + if(strcmp(KeywordStrings[iFirstToken], vsTokens[i][0].c_str()) == 0) |
| 117 | + break; |
| 118 | + } |
| 119 | + std::cout << i + 1 << " " << KeywordStrings[iFirstToken] << " " |
| 120 | + << vsTokens[i][0].c_str() << " Token " << iFirstToken << endl; |
| 121 | + } |
| 122 | + vInitializationCheck[ iFirstToken ] = true; |
| 123 | + |
| 124 | + switch( iFirstToken ) // look at first token of each line |
| 125 | + { |
| 126 | + |
| 127 | + case eComment: // Comment |
| 128 | + break; |
| 129 | + case eGridDimension: |
| 130 | + RUNTIME_ASSERT( vsTokens[i].size() >= 4, |
| 131 | + "GridDimension: Expect 3 parameters [NumX NumY NumZ]"); |
| 132 | + |
| 133 | + NumX = atoi( vsTokens[i][1].c_str() ); |
| 134 | + NumY = atoi( vsTokens[i][2].c_str() ); |
| 135 | + NumZ = atoi( vsTokens[i][3].c_str() ); |
| 136 | + break; |
| 137 | + |
| 138 | + case eInputStrainRate: |
| 139 | + { |
| 140 | + bool bSuccess = HasLineContinuation( i, 3, vsTokens, KeywordStrings, eNumKeywords ); |
| 141 | + RUNTIME_ASSERT( bSuccess, "InputStrainRate: Expect s00 s11 s22 \n <empty space> s10 s11 s12 \n <empty space> s20 s21 s22 " ); |
| 142 | + |
| 143 | + for(int di = 0; di < 3; di ++ ) |
| 144 | + { |
| 145 | + ++i; |
| 146 | + InputStrainRate.m[di][0] = atof( vsTokens[i][0].c_str() ); |
| 147 | + InputStrainRate.m[di][1] = atof( vsTokens[i][1].c_str() ); |
| 148 | + InputStrainRate.m[di][2] = atof( vsTokens[i][2].c_str() ); |
| 149 | + } |
| 150 | + std::cout << "Parsed input strain rate " << std::endl; |
| 151 | + std::cout << InputStrainRate << std::endl; |
| 152 | + } |
| 153 | + break; |
| 154 | + case eMaxNRIter: |
| 155 | + MaxNRIter = atoi( vsTokens[i][1].c_str() ); |
| 156 | + break; |
| 157 | + |
| 158 | + case eTimeStep: |
| 159 | + TimeStep = atof( vsTokens[i][1].c_str() ); |
| 160 | + break; |
| 161 | + |
| 162 | + case eNumTimeIter: |
| 163 | + NumTimeIter = atoi( vsTokens[i][1].c_str() ); |
| 164 | + break; |
| 165 | + |
| 166 | + case eNRTolerence: |
| 167 | + NRTolerence = atof( vsTokens[i][1].c_str() ); |
| 168 | + break; |
| 169 | + |
| 170 | + case eTwoGrainInit: |
| 171 | + InitMethod = eInitTwoGrain; |
| 172 | + break; |
| 173 | + |
| 174 | + case eFileInit: |
| 175 | + { |
| 176 | + RUNTIME_ASSERT( vsTokens[i].size() >= 2, |
| 177 | + "FileInit: Expect Filename for initialization" ); |
| 178 | + |
| 179 | + InputMicrostructureFilename = vsTokens[i][1]; |
| 180 | + std::ifstream fd; |
| 181 | + fd.open( InputMicrostructureFilename.c_str() ); |
| 182 | + if( !fd ) |
| 183 | + { |
| 184 | + std::cout << "FileInit " << InputMicrostructureFilename << " Not found " << std::endl; |
| 185 | + exit(0); |
| 186 | + } |
| 187 | + fd.close(); |
| 188 | + InitMethod = eInitFile; |
| 189 | + break; |
| 190 | + } |
| 191 | + case eCRSS: |
| 192 | + RUNTIME_ASSERT( vsTokens[i].size() >= VPFFT::FCC_CrystalTest::NumSystems, |
| 193 | + "GridDimension: Expect the same number of parameters as number slip systems in the symmetry\n"); |
| 194 | + |
| 195 | + CRSS.resize( VPFFT::FCC_CrystalTest::NumSystems ); |
| 196 | + for( int n = 1; n <= VPFFT::FCC_CrystalTest::NumSystems; n ++ ) |
| 197 | + CRSS[n] = atof( vsTokens[i][n].c_str() ); |
| 198 | + |
| 199 | + break; |
| 200 | + case eGammaDotBase: |
| 201 | + RUNTIME_ASSERT( vsTokens[i].size() >= VPFFT::FCC_CrystalTest::NumSystems, |
| 202 | + "GridDimension: Expect the same number of parameters as number slip systems in the symmetry\n"); |
| 203 | + GammaDotBase.resize( VPFFT::FCC_CrystalTest::NumSystems ); |
| 204 | + for( int n = 1; n <= VPFFT::FCC_CrystalTest::NumSystems; n ++ ) |
| 205 | + GammaDotBase[n] = atof( vsTokens[i][n].c_str() ); |
| 206 | + |
| 207 | + break; |
| 208 | + case eRateSensitivity: |
| 209 | + RUNTIME_ASSERT( vsTokens[i].size() >= VPFFT::FCC_CrystalTest::NumSystems, |
| 210 | + "GridDimension: Expect the same number of parameters as number slip systems in the symmetry\n"); |
| 211 | + RateSensitivity.resize( VPFFT::FCC_CrystalTest::NumSystems ); |
| 212 | + for( int n = 1; n <= VPFFT::FCC_CrystalTest::NumSystems; n ++ ) |
| 213 | + RateSensitivity[n] = atoi( vsTokens[i][n].c_str() ); |
| 214 | + |
| 215 | + break; |
| 216 | + |
| 217 | + default: |
| 218 | + { |
| 219 | + std::cerr << "[ConfigFile] Error: syntax not recognized: Line " << i |
| 220 | + << " Keyword: " << vsTokens[i][0] << endl; |
| 221 | + return false; |
| 222 | + } |
| 223 | + } |
| 224 | + } // end loop over tokens |
| 225 | + |
| 226 | + bool Success = true; |
| 227 | + for( int i = 0; i < eNumKeywords; i ++ ) |
| 228 | + { |
| 229 | + if( !vInitializationCheck[i] && vRequirementCheck[i] ) |
| 230 | + { |
| 231 | + std::cerr << "[ConfigFile] Initialization Error: Missing parameter: " |
| 232 | + << KeywordStrings[i] << " not optional." << std::endl; |
| 233 | + Success = false; |
| 234 | + } |
| 235 | + } |
| 236 | + if( ! vInitializationCheck[eFileInit] && |
| 237 | + ! vInitializationCheck[eTwoGrainInit] ) |
| 238 | + { |
| 239 | + std::cerr << "Initialization scheme (either FileInit or TwoGrain init) needs to be specified." << std::endl; |
| 240 | + Success = false; |
| 241 | + } |
| 242 | + |
| 243 | + if( !Success ) |
| 244 | + { |
| 245 | + std::cerr << "[ConfigFile] Initialization Failed" << std::endl; |
| 246 | + exit(0); |
| 247 | + } |
| 248 | + delete [] vInitializationCheck; |
| 249 | + delete [] vRequirementCheck; |
| 250 | + |
| 251 | + std::cout << "GridDimension " |
| 252 | + << NumX << " " |
| 253 | + << NumY << " " |
| 254 | + << NumZ << std::endl; |
| 255 | + std::cout << "InputStrainRate " << std::endl |
| 256 | + << InputStrainRate << std::endl; |
| 257 | + std::cout << "MaxNRIter " << MaxNRIter << std::endl; |
| 258 | + std::cout << "TimeStep " << TimeStep << std::endl; |
| 259 | + std::cout << "InitMethod " << InitMethod << std::endl; |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | + |
| 264 | + //-------------------------------------------- |
| 265 | + // Returns a Null terminated C-string |
| 266 | + //-------------------------------------------- |
| 267 | + char* ConfigFile::ReadFileToBuf( Size_Type & nBufferSize, std::string filename) |
| 268 | + { |
| 269 | + std::ifstream infile; |
| 270 | + infile.open(filename.c_str()); |
| 271 | + FILE *pFile; |
| 272 | + char *pBuffer; |
| 273 | + |
| 274 | + if ( (pFile = fopen(filename.c_str(), "r" )) == NULL ){ |
| 275 | + cerr << "[ReadFileToBuf]Cannot Open File: " << filename.c_str() << endl; |
| 276 | + return NULL; |
| 277 | + } |
| 278 | + |
| 279 | + // Get the size of the file |
| 280 | + fseek( pFile, 0L, SEEK_END ); // Position to end of file |
| 281 | + nBufferSize = ftell( pFile ); // Get file length |
| 282 | + rewind( pFile ); // Back to start of file |
| 283 | + |
| 284 | + if( nBufferSize <= 0 ) |
| 285 | + return NULL; |
| 286 | + |
| 287 | + // Read in the entire file and close the file handle |
| 288 | + pBuffer = new char[nBufferSize + 1]; |
| 289 | + |
| 290 | + if ( fread( pBuffer, nBufferSize, 1, pFile ) <= 0 ){ |
| 291 | + fclose( pFile ); |
| 292 | + cerr << "[ReadFileToBuf]:File read error" << endl; |
| 293 | + return NULL; |
| 294 | + } |
| 295 | + |
| 296 | + fclose( pFile ); |
| 297 | + |
| 298 | + pBuffer[ nBufferSize ] = '\0'; // NULL termination of string |
| 299 | + return pBuffer; |
| 300 | + } |
| 301 | + |
| 302 | + |
| 303 | + |
| 304 | +} |
0 commit comments