Sierra Toolkit Version of the Day
PreParse.cpp
00001 #include <stk_util/diag/PreParse.hpp>
00002 #include <stk_util/diag/Env.hpp>
00003 #include <stk_util/parallel/Exception.hpp>
00004 
00005 //----------------------------------------------------------------------
00006 
00007 namespace sierra {
00008 
00009   std::string CreateSubCycleInputFile( const std::string& ifile, bool debug ) {
00010 
00011     boost::regex reStripFileExtension;
00012     std::string regionStripFileExtensionRegexp("\\.[^.]*$");
00013     reStripFileExtension.assign(regionStripFileExtensionRegexp);
00014     std::string baseName = boost::regex_replace(ifile,reStripFileExtension,"");
00015     std::string extension = ifile.substr(baseName.size(),ifile.size());
00016     if ( debug ) {
00017       std::cout << "Input Base Name: " << baseName << " Extension: " << extension << std::endl;
00018     }
00019     std::string nfile = baseName;
00020     nfile.append(".subcycle");
00021     nfile.append(extension);
00022 
00023     if(sierra::Env::parallel_rank() == 0) { // Write New File Only on processor 0
00024 
00025       std::cout << "Input File: " << ifile << " Being Converted for Subcycling to: " << nfile << std::endl;
00026 
00027       boost::regex reName;
00028       std::string regionNameRegexp("\\s+region\\s+\\w+");
00029       reName.assign(regionNameRegexp,boost::regex_constants::icase);
00030 
00031       std::string appendCoarseName("$MATCH_AutoCoarseRegion");
00032       std::string appendFineName("$MATCH_AutoFineRegion");
00033 
00034       boost::regex reOutput;
00035       std::string regionOutputRegexp("\\s*database\\s+name\\s*=\\s*");
00036       reOutput.assign(regionOutputRegexp,boost::regex_constants::icase);
00037 
00038       std::string prependCoarseName("$MATCHCoarse_");
00039       std::string prependFineName("$MATCHFine_");
00040 
00041       boost::regex reBegin;
00042       reBegin.assign("^\\s*begin\\>", boost::regex_constants::icase);
00043       boost::regex reEnd;
00044       reEnd.assign("^\\s*end\\>", boost::regex_constants::icase);
00045 
00046       boost::regex reRegion;
00047       std::string regionRegexp("^\\s*begin\\s+presto\\s+region\\>");
00048       reRegion.assign(regionRegexp,boost::regex_constants::icase);
00049       std::vector< std::vector< std::string > > extractedRegion = ExtractCommandBlocksInFile(regionRegexp, ifile, debug);
00050       if (extractedRegion.size()!=1) throw RuntimeError() << "Subcycling currently supports only one region.";
00051 
00052       boost::regex reRegionParameters;
00053       std::string regionParametersRegexp("^\\s*begin\\s+parameters\\s+for\\s+presto\\s+region\\>");
00054       reRegionParameters.assign(regionParametersRegexp,boost::regex_constants::icase);
00055       std::vector< std::vector< std::string > > extractedRegionParameters = ExtractCommandBlocksInFile(regionParametersRegexp, ifile, debug);
00056 
00057       boost::regex reTimeStepIncrease;
00058       std::string timeStepIncreaseRegexp("^\\s*time\\s+step\\s+increase\\s+factor\\s*=\\s*");
00059       reTimeStepIncrease.assign(timeStepIncreaseRegexp,boost::regex_constants::icase);
00060 
00061       std::ofstream nstream;
00062 
00063       nstream.open(nfile.c_str());
00064 
00065       std::ifstream fileStream(ifile.c_str());
00066       if ( fileStream.bad() ) {
00067         std::cerr << "Unable to open file " << ifile << std::endl;
00068       }
00069       std::string line;
00070       int numOpen = 0;
00071       int stopWriteCondition = -1;
00072       std::vector<int> stopWriteConditions;
00073       stopWriteConditions.push_back(0); // Region
00074       stopWriteConditions.push_back(0); // Region Parameters
00075       int regionParamBlock = 0;
00076       while( std::getline( fileStream, line ) ) {
00077         // Monitor numOpen block commands
00078         if ( boost::regex_search(line,reBegin) ) {
00079           numOpen += 1;
00080         } else if ( boost::regex_search(line,reEnd) ) {
00081           numOpen -= 1;
00082         }
00083         if ( boost::regex_search(line,reRegion) ) { // Check For Region Block
00084           stopWriteCondition = 0;
00085           stopWriteConditions[stopWriteCondition] = numOpen - 1;
00086           std::string newLine;
00087           for( unsigned int i(0); i < extractedRegion[0].size(); ++i ) {
00088             if ( i == 1 ) {
00089               std::string levelLine = "      subcycle region level = 0   #THIS IS COARSE REGION";
00090               nstream << levelLine << std::endl;
00091             }
00092             newLine = boost::regex_replace(extractedRegion[0][i],reName,appendCoarseName);
00093             newLine = boost::regex_replace(newLine,reOutput,prependCoarseName);
00094             nstream << newLine << std::endl;
00095           }
00096           for( unsigned int i(0); i < extractedRegion[0].size(); ++i ) {
00097             if ( i == 1 ) {
00098               std::string levelLine = "      subcycle region level = 1  #THIS IS FINE REGION";
00099               nstream << levelLine << std::endl;
00100             }
00101             newLine = boost::regex_replace(extractedRegion[0][i],reName,appendFineName);
00102             newLine = boost::regex_replace(newLine,reOutput,prependFineName);
00103             nstream << newLine << std::endl;
00104           }
00105         } else if ( boost::regex_search(line,reRegionParameters) ) { // Check For Region Parameters
00106           stopWriteCondition = 1;
00107           stopWriteConditions[stopWriteCondition] = numOpen - 1;
00108           bool needsTimeStepIncrease = true;
00109           for( unsigned int i(0); i < extractedRegionParameters[regionParamBlock].size(); ++i ) {
00110             nstream << boost::regex_replace(extractedRegionParameters[regionParamBlock][i],reName,appendCoarseName) << std::endl;
00111             if ( boost::regex_search( extractedRegionParameters[regionParamBlock][i], reTimeStepIncrease ) ) {
00112               needsTimeStepIncrease = false;
00113             }
00114           }
00115           for( unsigned int i(0); i < extractedRegionParameters[regionParamBlock].size(); ++i ) {
00116             if ( needsTimeStepIncrease && i == (extractedRegionParameters[regionParamBlock].size() - 1) ) {
00117               nstream << "          time step increase factor = 2.0" << std::endl;
00118             }
00119             nstream << boost::regex_replace(extractedRegionParameters[regionParamBlock][i],reName,appendFineName) << std::endl;
00120           }
00121           regionParamBlock++;
00122         }
00123         if ( stopWriteCondition < 0 ) {
00124           nstream << line << std::endl;
00125         } else {
00126           if ( stopWriteConditions[stopWriteCondition] == numOpen ) {
00127             stopWriteCondition = -1;
00128           }
00129         }
00130       }
00131       nstream.close();
00132     }
00133 
00134     MPI_Barrier( sierra::Env::parallel_comm() ); // Wait for proccessor to write new file
00135 
00136     return nfile;
00137 
00138   }
00139 
00140 
00141 } // namespace sierra
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines