OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
GenerateOpalSimulation.h
Go to the documentation of this file.
1 #ifndef __GENERATE_SIMULATION_H__
2 #define __GENERATE_SIMULATION_H__
3 
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <string>
8 #include <map>
9 #include <cstdlib>
10 
11 #include <vector>
12 #include <boost/algorithm/string.hpp>
13 
14 #include "Util/OptPilotException.h"
15 
32 
33 public:
34 
43  GenerateOpalSimulation(std::string tmplFile, std::string varDictionary,
44  std::map<std::string,std::string> userValues) {
45  varDictionary_ = varDictionary;
46  tmplFile_ = tmplFile;
47 
48  // some variables need to be scaled
49  scaleVars_.insert(std::pair<std::string, double>("GUNSOLB", 1.));
50 
51  // add user values and then fill using the data file without
52  // overwriting user values..
53  dictionary_.insert(userValues.begin(),userValues.end());
55  }
56 
58  dictionary_.clear();
59  scaleVars_.clear();
60  }
61 
68  void writeInputFile(std::string outputFile) {
69 
70  std::ifstream infile(tmplFile_.c_str());
71  std::ostringstream outdata;
72  outdata.precision(15);
73 
74  while(infile.good()) {
75  std::string line;
76  std::getline(infile, line, '\n');
77 
78  //XXX doing the inverse would be better
80  for(;itr != dictionary_.end(); itr++) {
81  size_t pos = line.find("_" + itr->first + "_");
82  while(pos != std::string::npos) {
83  line.replace(pos, itr->first.length() + 2, itr->second);
84  pos = line.find("_" + itr->first + "_");
85  }
86  }
87 
88  outdata << line << std::endl;
89  }
90  infile.close();
91 
92  // ensure the contents are written to disk
93  std::ofstream outfile(outputFile.c_str());
94  outfile.precision(15);
95  outfile << outdata.str();
96  outfile.flush();
97  outfile.rdbuf()->pubsync();
98 
99  outfile.close();
100  }
101 
102 
103 private:
104 
106  std::string tmplFile_;
108  std::string varDictionary_;
110  std::map<std::string, std::string> dictionary_;
112  std::map<std::string, double> scaleVars_;
113 
127  void fillDictionary() {
128 
129  std::ifstream infile;
130  infile.open(varDictionary_.c_str(), std::ifstream::in);
131 
132  char tmp[1024];
133  unsigned int line_nr = 0;
134  while(infile.good()) {
135  std::fill_n(tmp, 1024, '\0');
136  infile.getline(tmp, 1024);
137  line_nr++;
138  if(tmp[0] != '#') {
139  std::string stmp(tmp);
140  boost::trim(stmp);
141  if(stmp.size() == 0)
142  continue;
143 
144  std::vector<std::string> all_strings;
145  boost::split(all_strings, stmp,
146  boost::is_any_of("\r\n\v\f\t "),
147  boost::token_compress_on);
148 
149  if(all_strings.size() < 2) {
150  std::cout << "PROBLEM with the following line "
151  << "(at least name and value required)!"
152  << std::endl;
153  std::cout << stmp << std::endl;
154  std::ostringstream ex;
155  ex << "Invalid data file on line " << line_nr;
156  throw OptPilotException(
157  "GenerateOpalSimulation::fillDictionary()",
158  ex.str());
159  }
160 
161  std::string varname = all_strings[0];
162  std::string value = all_strings[1];
163  scale(varname, &value);
164  dictionary_.insert(std::pair<std::string, std::string>(
165  varname, value));
166  }
167  }
168 
169  infile.close();
170  }
171 
172 
174  void scale(std::string name, std::string *value) {
175  if(scaleVars_.count(name) > 0) {
176  std::istringstream instr(*value);
177  double val;
178  instr >> val;
179  val *= scaleVars_[name];
180 
181  // we keep values as strings, since we have to write them to
182  // output streams anyways..
183  std::ostringstream of;
184  of << val;
185  *value = of.str();
186  }
187  }
188 
189 };
190 
191 #endif
std::string varDictionary_
data filename
Generates an OPAL input file from data and template file.
void scale(std::string name, std::string *value)
Helper method to scale variable if necessary.
std::map< std::string, double > scaleVars_
holds all variables that need scaling
std::map< std::string, std::string > dictionary_
holds parsed template data
int precision() const
Definition: Inform.h:115
GenerateOpalSimulation(std::string tmplFile, std::string varDictionary, std::map< std::string, std::string > userValues)
const std::string name
std::string::iterator iterator
Definition: MSLang.h:16
void writeInputFile(std::string outputFile)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string tmplFile_
template filename