1 #include <stk_util/diag/PreParse.hpp> 2 #include <stk_util/diag/Env.hpp> 9 std::string CreateSubCycleInputFile(
const std::string& ifile,
bool debug ) {
11 boost::regex reStripFileExtension;
12 std::string regionStripFileExtensionRegexp(
"\\.[^.]*$");
13 reStripFileExtension.assign(regionStripFileExtensionRegexp);
14 std::string baseName = boost::regex_replace(ifile,reStripFileExtension,
"");
15 std::string extension = ifile.substr(baseName.size(),ifile.size());
17 std::cout <<
"Input Base Name: " << baseName <<
" Extension: " << extension << std::endl;
19 std::string nfile = baseName;
20 nfile.append(
".subcycle");
21 nfile.append(extension);
25 std::cout <<
"Input File: " << ifile <<
" Being Converted for Subcycling to: " << nfile << std::endl;
28 std::string regionNameRegexp(
"\\s+region\\s+\\w+");
29 reName.assign(regionNameRegexp,boost::regex_constants::icase);
31 std::string appendCoarseName(
"$MATCH_AutoCoarseRegion");
32 std::string appendFineName(
"$MATCH_AutoFineRegion");
34 boost::regex reOutput;
35 std::string regionOutputRegexp(
"\\s*database\\s+name\\s*=\\s*");
36 reOutput.assign(regionOutputRegexp,boost::regex_constants::icase);
38 std::string prependCoarseName(
"$MATCHCoarse_");
39 std::string prependFineName(
"$MATCHFine_");
42 reBegin.assign(
"^\\s*begin\\>", boost::regex_constants::icase);
44 reEnd.assign(
"^\\s*end\\>", boost::regex_constants::icase);
46 boost::regex reRegion;
47 std::string regionRegexp(
"^\\s*begin\\s+presto\\s+region\\>");
48 reRegion.assign(regionRegexp,boost::regex_constants::icase);
49 std::vector< std::vector< std::string > > extractedRegion = ExtractCommandBlocksInFile(regionRegexp, ifile, debug);
50 if (extractedRegion.size()!=1)
throw RuntimeError() <<
"Subcycling currently supports only one region.";
52 boost::regex reRegionParameters;
53 std::string regionParametersRegexp(
"^\\s*begin\\s+parameters\\s+for\\s+presto\\s+region\\>");
54 reRegionParameters.assign(regionParametersRegexp,boost::regex_constants::icase);
55 std::vector< std::vector< std::string > > extractedRegionParameters = ExtractCommandBlocksInFile(regionParametersRegexp, ifile, debug);
57 boost::regex reTimeStepIncrease;
58 std::string timeStepIncreaseRegexp(
"^\\s*time\\s+step\\s+increase\\s+factor\\s*=\\s*");
59 reTimeStepIncrease.assign(timeStepIncreaseRegexp,boost::regex_constants::icase);
61 std::ofstream nstream;
63 nstream.open(nfile.c_str());
65 std::ifstream fileStream(ifile.c_str());
66 if ( fileStream.bad() ) {
67 std::cerr <<
"Unable to open file " << ifile << std::endl;
71 int stopWriteCondition = -1;
72 std::vector<int> stopWriteConditions;
73 stopWriteConditions.push_back(0);
74 stopWriteConditions.push_back(0);
75 int regionParamBlock = 0;
78 if ( boost::regex_search(line,reBegin) ) {
80 }
else if ( boost::regex_search(line,reEnd) ) {
83 if ( boost::regex_search(line,reRegion) ) {
84 stopWriteCondition = 0;
85 stopWriteConditions[stopWriteCondition] = numOpen - 1;
87 for(
unsigned int i(0); i < extractedRegion[0].size(); ++i ) {
89 std::string levelLine =
" subcycle region level = 0 #THIS IS COARSE REGION";
90 nstream << levelLine << std::endl;
92 newLine = boost::regex_replace(extractedRegion[0][i],reName,appendCoarseName);
93 newLine = boost::regex_replace(newLine,reOutput,prependCoarseName);
94 nstream << newLine << std::endl;
96 for(
unsigned int i(0); i < extractedRegion[0].size(); ++i ) {
98 std::string levelLine =
" subcycle region level = 1 #THIS IS FINE REGION";
99 nstream << levelLine << std::endl;
101 newLine = boost::regex_replace(extractedRegion[0][i],reName,appendFineName);
102 newLine = boost::regex_replace(newLine,reOutput,prependFineName);
103 nstream << newLine << std::endl;
105 }
else if ( boost::regex_search(line,reRegionParameters) ) {
106 stopWriteCondition = 1;
107 stopWriteConditions[stopWriteCondition] = numOpen - 1;
108 bool needsTimeStepIncrease =
true;
109 for(
unsigned int i(0); i < extractedRegionParameters[regionParamBlock].size(); ++i ) {
110 nstream << boost::regex_replace(extractedRegionParameters[regionParamBlock][i],reName,appendCoarseName) << std::endl;
111 if ( boost::regex_search( extractedRegionParameters[regionParamBlock][i], reTimeStepIncrease ) ) {
112 needsTimeStepIncrease =
false;
115 for(
unsigned int i(0); i < extractedRegionParameters[regionParamBlock].size(); ++i ) {
116 if ( needsTimeStepIncrease && i == (extractedRegionParameters[regionParamBlock].size() - 1) ) {
117 nstream <<
" time step increase factor = 2.0" << std::endl;
119 nstream << boost::regex_replace(extractedRegionParameters[regionParamBlock][i],reName,appendFineName) << std::endl;
123 if ( stopWriteCondition < 0 ) {
124 nstream << line << std::endl;
126 if ( stopWriteConditions[stopWriteCondition] == numOpen ) {
127 stopWriteCondition = -1;
std::istream & getline(std::istream &is, sierra::String &s, char eol)
Function getline returns a string from the input stream which has been terminated by the newline char...
int parallel_rank()
function parallel_rank returns the rank of this processor in the current mpi communicator.
ExTemp1< std::runtime_error > RuntimeError
Defined in <stdexcept>
MPI_Comm parallel_comm()
Function parallel_comm returns the current MPI communicator used by the sierra environment.