Sierra Toolkit  Version of the Day
PreParse.cpp
1 #include <stk_util/diag/PreParse.hpp>
2 #include <stk_util/diag/Env.hpp>
4 
5 //----------------------------------------------------------------------
6 
7 namespace sierra {
8 
9  std::string CreateSubCycleInputFile( const std::string& ifile, bool debug ) {
10 
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());
16  if ( debug ) {
17  std::cout << "Input Base Name: " << baseName << " Extension: " << extension << std::endl;
18  }
19  std::string nfile = baseName;
20  nfile.append(".subcycle");
21  nfile.append(extension);
22 
23  if(sierra::Env::parallel_rank() == 0) { // Write New File Only on processor 0
24 
25  std::cout << "Input File: " << ifile << " Being Converted for Subcycling to: " << nfile << std::endl;
26 
27  boost::regex reName;
28  std::string regionNameRegexp("\\s+region\\s+\\w+");
29  reName.assign(regionNameRegexp,boost::regex_constants::icase);
30 
31  std::string appendCoarseName("$MATCH_AutoCoarseRegion");
32  std::string appendFineName("$MATCH_AutoFineRegion");
33 
34  boost::regex reOutput;
35  std::string regionOutputRegexp("\\s*database\\s+name\\s*=\\s*");
36  reOutput.assign(regionOutputRegexp,boost::regex_constants::icase);
37 
38  std::string prependCoarseName("$MATCHCoarse_");
39  std::string prependFineName("$MATCHFine_");
40 
41  boost::regex reBegin;
42  reBegin.assign("^\\s*begin\\>", boost::regex_constants::icase);
43  boost::regex reEnd;
44  reEnd.assign("^\\s*end\\>", boost::regex_constants::icase);
45 
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.";
51 
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);
56 
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);
60 
61  std::ofstream nstream;
62 
63  nstream.open(nfile.c_str());
64 
65  std::ifstream fileStream(ifile.c_str());
66  if ( fileStream.bad() ) {
67  std::cerr << "Unable to open file " << ifile << std::endl;
68  }
69  std::string line;
70  int numOpen = 0;
71  int stopWriteCondition = -1;
72  std::vector<int> stopWriteConditions;
73  stopWriteConditions.push_back(0); // Region
74  stopWriteConditions.push_back(0); // Region Parameters
75  int regionParamBlock = 0;
76  while( std::getline( fileStream, line ) ) {
77  // Monitor numOpen block commands
78  if ( boost::regex_search(line,reBegin) ) {
79  numOpen += 1;
80  } else if ( boost::regex_search(line,reEnd) ) {
81  numOpen -= 1;
82  }
83  if ( boost::regex_search(line,reRegion) ) { // Check For Region Block
84  stopWriteCondition = 0;
85  stopWriteConditions[stopWriteCondition] = numOpen - 1;
86  std::string newLine;
87  for( unsigned int i(0); i < extractedRegion[0].size(); ++i ) {
88  if ( i == 1 ) {
89  std::string levelLine = " subcycle region level = 0 #THIS IS COARSE REGION";
90  nstream << levelLine << std::endl;
91  }
92  newLine = boost::regex_replace(extractedRegion[0][i],reName,appendCoarseName);
93  newLine = boost::regex_replace(newLine,reOutput,prependCoarseName);
94  nstream << newLine << std::endl;
95  }
96  for( unsigned int i(0); i < extractedRegion[0].size(); ++i ) {
97  if ( i == 1 ) {
98  std::string levelLine = " subcycle region level = 1 #THIS IS FINE REGION";
99  nstream << levelLine << std::endl;
100  }
101  newLine = boost::regex_replace(extractedRegion[0][i],reName,appendFineName);
102  newLine = boost::regex_replace(newLine,reOutput,prependFineName);
103  nstream << newLine << std::endl;
104  }
105  } else if ( boost::regex_search(line,reRegionParameters) ) { // Check For Region Parameters
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;
113  }
114  }
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;
118  }
119  nstream << boost::regex_replace(extractedRegionParameters[regionParamBlock][i],reName,appendFineName) << std::endl;
120  }
121  regionParamBlock++;
122  }
123  if ( stopWriteCondition < 0 ) {
124  nstream << line << std::endl;
125  } else {
126  if ( stopWriteConditions[stopWriteCondition] == numOpen ) {
127  stopWriteCondition = -1;
128  }
129  }
130  }
131  nstream.close();
132  }
133 
134  MPI_Barrier( sierra::Env::parallel_comm() ); // Wait for proccessor to write new file
135 
136  return nfile;
137 
138  }
139 
140 
141 } // namespace sierra
Definition: Env.cpp:53
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...
Definition: StringUtil.cpp:33
int parallel_rank()
function parallel_rank returns the rank of this processor in the current mpi communicator.
Definition: Env.cpp:318
ExTemp1< std::runtime_error > RuntimeError
Defined in <stdexcept>
Definition: Exception.hpp:775
MPI_Comm parallel_comm()
Function parallel_comm returns the current MPI communicator used by the sierra environment.
Definition: Env.cpp:323