ElmerFoamFSI  2.0
ElmerFoamFSI is fluid-solid interaction simulation application built up from OpenFOAM CFD and Elmer CSM coupled through the IMPACT multiphysics software integration infrastructure.
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros Groups Pages
src/MakeProject.C
Go to the documentation of this file.
1 
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 #include <algorithm>
15 #include <cstdio>
16 
17 #include "UnixUtils.H"
18 #include "FDUtils.H"
19 
20 namespace ElmerFoamFSI
21 {
25  bool Excluded(const std::string &filename)
26  {
27  std::string::size_type x = filename.find("IR.dox");
28  if(x != std::string::npos)
29  return(true);
30  x = filename.find("MakeProject.dox");
31  if(x != std::string::npos)
32  return(true);
33  x = filename.find("ExistingProject.dox");
34  if(x != std::string::npos)
35  return(true);
36  return(false);
37  }
38 
39 
77  int MakeProject(int argc,char *argv[])
78  {
79  if(argc < 3){
80  std::cout << "Usage:" << std::endl << std::endl
81  << argv[0] << " <template name> <new name> [verb level]" << std::endl
82  << std::endl
83  << "This program will read the project template from the <template name> "
84  << std::endl
85  << "directory and create a new \"blank\" IllinoisRocstar project"
86  << std::endl
87  << "named <new name>, in a directory named <new name>."
88  << std::endl
89  << "An optional verblevel of 1 or 2 can be given to make the process"
90  << std::endl << "more verbose." << std::endl;
91  return(0);
92  }
93  std::string OriginalName(argv[1]);
94  std::string NewName(argv[2]);
95  int verb = 0;
96  if(argv[3]){
97  verb = 1;
98  int v = atoi(argv[3]);
99  if(v > 0)
100  verb = v;
101  }
102  std::vector<std::string> ProtectedFiles;
103  ProtectedFiles.push_back("AUTHORS");
104  ProtectedFiles.push_back("CTestConfig.cmake");
105  ProtectedFiles.push_back("LICENSE");
106  ProtectedFiles.push_back(".svn");
107  std::vector<std::string> CommonFiles;
108  CommonFiles.push_back("CMakeLists.txt");
109  int syserr = 0;
110  std::string olower(OriginalName);
111  std::string oupper(OriginalName);
112  std::string nlower(NewName);
113  std::string nupper(NewName);
114  std::transform(olower.begin(),olower.end(),olower.begin(),tolower);
115  std::transform(oupper.begin(),oupper.end(),oupper.begin(),toupper);
116  std::transform(nlower.begin(),nlower.end(),nlower.begin(),tolower);
117  std::transform(nupper.begin(),nupper.end(),nupper.begin(),toupper);
118 
119  if(verb)
120  std::cout << "Creating a new project (" << NewName
121  << ") from project template (" << OriginalName
122  << ")." << std::endl << std::endl
123  << "Creating top level project directories...";
124  std::string dirname(NewName);
125  if(!IRAD::Sys::FILEEXISTS(NewName)){
126  if(verb > 1)
127  std::cout << " Creating directory " << dirname << "...";
128  syserr = IRAD::Sys::CreateDirectory(dirname);
129  if(syserr){
130  std::cout << "Unable to create directory " << dirname << "."
131  << std::endl;
132  return(1);
133  }
134  if(verb > 1)
135  std::cout << "done." << std::endl;
136  }
137  dirname += "/branches";
138  if(!IRAD::Sys::FILEEXISTS(dirname)){
139  if(verb > 1)
140  std::cout << " Creating directory " << dirname << "...";
141  syserr = IRAD::Sys::CreateDirectory(dirname);
142  if(syserr){
143  std::cout << "Unable to create directory " << dirname << "."
144  << std::endl;
145  return(1);
146  }
147  if(verb > 1)
148  std::cout << "done." << std::endl;
149  }
150  dirname = NewName+"/tags";
151  if(!IRAD::Sys::FILEEXISTS(dirname)){
152  if(verb > 1)
153  std::cout << " Creating directory " << dirname << "...";
154  syserr = IRAD::Sys::CreateDirectory(dirname);
155  if(syserr){
156  std::cout << "Unable to create directory " << dirname << "."
157  << std::endl;
158  return(1);
159  }
160  if(verb > 1)
161  std::cout << "done." << std::endl;
162  }
163  dirname.assign(NewName+"/examples");
164  if(!IRAD::Sys::FILEEXISTS(dirname)){
165  if(verb > 1)
166  std::cout << " Creating directory " << dirname << "...";
167  syserr = IRAD::Sys::CreateDirectory(dirname);
168  if(syserr){
169  std::cout << "Unable to create directory " << dirname << "."
170  << std::endl;
171  return(1);
172  }
173  if(verb > 1)
174  std::cout << "done." << std::endl;
175  }
176  bool protect_svn = false;
177  dirname = NewName+"/trunk";
178  std::vector<std::string>::iterator pfi = ProtectedFiles.begin();
179  while(pfi != ProtectedFiles.end()){
180  std::string ProtectThisFile(dirname+"/"+*pfi++);
181  std::string ProtectedFile(ProtectThisFile+".backup");
182  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
183  IRAD::Sys::Rename(ProtectThisFile,ProtectedFile);
184  }
185  pfi = CommonFiles.begin();
186  while(pfi != CommonFiles.end()){
187  std::string ProtectThisFile(dirname+"/"+*pfi++);
188  std::string ProtectedFile(ProtectThisFile+".backup");
189  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
190  IRAD::Sys::Rename(ProtectThisFile,ProtectedFile);
191  }
192  std::ostringstream ComStr;
193  if(!IRAD::Sys::FILEEXISTS(dirname)){
194  if(verb > 1)
195  std::cout << " Creating directory " << dirname << "...";
196  ComStr << "cp -r " << OriginalName << " " << dirname;
197  } else {
198  if(verb > 1)
199  std::cout << " Making project files from template ...";
200  ComStr << "cp -r " << OriginalName << "/* " << dirname;
201  }
202  IRAD::Sys::InProcess System(ComStr.str());
203  std::string comline;
204  while(std::getline(System,comline)){
205  if(verb > 1)
206  std::cout << comline << std::endl;
207  }
208  if(verb)
209  std::cout << "done." << std::endl;
210  if(verb)
211  std::cout << "Cleaning up ...";
212  ComStr.str("");
213  ComStr << "rm -rf " << dirname << "/.svn";
214  System.Execute(ComStr.str());
215  int n = 0;
216  while(std::getline(System,comline))
217  n++;
218  pfi = ProtectedFiles.begin();
219  while(pfi != ProtectedFiles.end()){
220  std::string ProtectThisFile(dirname+"/"+*pfi++);
221  std::string ProtectedFile(ProtectThisFile+".backup");
222  if(IRAD::Sys::FILEEXISTS(ProtectedFile)){
223  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
224  IRAD::Sys::Remove(ProtectThisFile);
225  IRAD::Sys::Rename(ProtectedFile,ProtectThisFile);
226  }
227  }
228  pfi = CommonFiles.begin();
229  while(pfi != CommonFiles.end()){
230  std::string ProtectThisFile(dirname+"/"+*pfi++);
231  std::string ProtectedFile(ProtectThisFile+".backup");
232  std::string CommonFileTemplate(ProtectThisFile+".template");
233  if(IRAD::Sys::FILEEXISTS(ProtectedFile)){
234  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
235  IRAD::Sys::Rename(ProtectThisFile,CommonFileTemplate);
236  IRAD::Sys::Rename(ProtectedFile,ProtectThisFile);
237  }
238  }
239  if(verb)
240  std::cout << "done." << std::endl;
241  if(verb > 1)
242  std::cout << "Done creating new project files."
243  << std::endl;
244  if(verb)
245  std::cout << "Renaming project...";
246  if(IRAD::Sys::ChDir(dirname)){
247  std::cout << "Something went wrong, cannot find new project directory."
248  << std::endl;
249  return(1);
250  }
251  ComStr.str("");
252  ComStr << "grep -i " << OriginalName << " -r * | cut -d \":\" -f 1 | sort | uniq";
253  if(verb > 1)
254  std::cout << " " << ComStr.str() << std::endl;
255  System.Execute(ComStr.str());
256  std::vector<std::string> filenames;
257  if(verb > 1)
258  std::cout << " Files to change:" << std::endl;
259  while(std::getline(System,comline)){
260  if(!Excluded(comline)){
261  filenames.push_back(comline);
262  if(verb > 1)
263  std::cout << " " << comline << std::endl;
264  }
265  }
266  std::vector<std::string>::iterator fni = filenames.begin();
267  if(verb > 1)
268  std::cout << " Processing files....";
269  while(fni != filenames.end()){
270  std::string filename(*fni++);
271  if(verb > 1)
272  std::cout << " File: " << filename << std::endl;
273  ComStr.str("");
274  ComStr << "sed -i 's/" << OriginalName << "/" << NewName << "/g' " << filename;
275  if(verb > 1)
276  std::cout << " " << ComStr.str() << std::endl;
277  System.Execute(ComStr.str());
278  int n = 0;
279  while(std::getline(System,comline))
280  n++;
281  ComStr.str("");
282  ComStr << "sed -i 's/" << oupper << "/" << nupper << "/g' " << filename;
283  if(verb > 1)
284  std::cout << " " << ComStr.str() << std::endl;
285  // std::cout << ComStr.str() << std::endl;
286  System.Execute(ComStr.str());
287  while(std::getline(System,comline))
288  n++;
289  ComStr.str("");
290  ComStr << "sed -i 's/" << olower << "/" << nlower << "/g' " << filename;
291  if(verb > 1)
292  std::cout << " " << ComStr.str() << std::endl;
293  // std::cout << ComStr.str() << std::endl;
294  System.Execute(ComStr.str());
295  while(std::getline(System,comline))
296  n++;
297  }
298  if(verb > 1)
299  std::cout << " Done processing file contents." << std::endl
300  << " Renaming files..." << std::endl;
301  ComStr.str("");
302  // Now the inside of all files is fixed, need to fix filenames
303  ComStr << "find . -name \"*" << OriginalName << "*\"";
304  System.Execute(ComStr.str());
305  std::string::size_type olen = OriginalName.length();
306  std::string::size_type nlen = NewName.length();
307  while(std::getline(System,comline)){
308  if(verb > 1)
309  std::cout << " Renaming " << comline << " to ";
310  std::string newname(comline);
311  std::string::size_type x = newname.find(OriginalName);
312  while(x != std::string::npos){
313  newname.replace(x,olen,NewName);
314  x = newname.find(OriginalName);
315  }
316  ComStr.str("");
317  if(verb > 1)
318  std::cout << newname << std::endl;
319  ComStr << "mv " << comline << " " << newname;
320  // std::cout << ComStr.str() << std::endl;
321  IRAD::Sys::InProcess MV(ComStr.str());
322  }
323  if(verb > 1)
324  std::cout << "done." << std::endl;
325  return(0);
326  }
327 };
328 
329 int main(int argc,char *argv[])
330 {
331  return(ElmerFoamFSI::MakeProject(argc,argv));
332 }
bool Excluded(const std::string &filename)
Exclude certain template files from conversion in project creation.
#define main
Definition: icoFoamModule.C:2
int MakeProject(int argc, char *argv[])
Creates a new project from an ElmerFoamFSI base.