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
Third_Party_Modules/ElmerFSI/trunk/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 ElmerModuleDriver
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 << "ElmerModuleDriver:MakeProject: " << std::endl << "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 << "ElmerModuleDriver:MakeProject:" << std::endl
121  << " Creating a new project (" << NewName
122  << ") from project template (" << OriginalName
123  << ")." << std::endl << std::endl
124  << "Creating top level project directories...";
125  std::string dirname(NewName);
126  if(!IRAD::Sys::FILEEXISTS(NewName)){
127  if(verb > 1)
128  std::cout << "ElmerModuleDriver:MakeProject: Creating directory " << dirname << "...";
129  syserr = IRAD::Sys::CreateDirectory(dirname);
130  if(syserr){
131  std::cout << "ElmerModuleDriver:MakeProject: Unable to create directory " << dirname << "."
132  << std::endl;
133  return(1);
134  }
135  if(verb > 1)
136  std::cout << "done." << std::endl;
137  }
138  dirname += "/branches";
139  if(!IRAD::Sys::FILEEXISTS(dirname)){
140  if(verb > 1)
141  std::cout << "ElmerModuleDriver:MakeProject: Creating directory " << dirname << "...";
142  syserr = IRAD::Sys::CreateDirectory(dirname);
143  if(syserr){
144  std::cout << "ElmerModuleDriver:MakeProject: Unable to create directory " << dirname << "."
145  << std::endl;
146  return(1);
147  }
148  if(verb > 1)
149  std::cout << "ElmerModuleDriver:MakeProject: done." << std::endl;
150  }
151  dirname = NewName+"/tags";
152  if(!IRAD::Sys::FILEEXISTS(dirname)){
153  if(verb > 1)
154  std::cout << "ElmerModuleDriver:MakeProject: Creating directory " << dirname << "...";
155  syserr = IRAD::Sys::CreateDirectory(dirname);
156  if(syserr){
157  std::cout << "ElmerModuleDriver:MakeProject: Unable to create directory " << dirname << "."
158  << std::endl;
159  return(1);
160  }
161  if(verb > 1)
162  std::cout << "ElmerModuleDriver:MakeProject: done." << std::endl;
163  }
164  dirname.assign(NewName+"/examples");
165  if(!IRAD::Sys::FILEEXISTS(dirname)){
166  if(verb > 1)
167  std::cout << "ElmerModuleDriver:MakeProject: Creating directory " << dirname << "...";
168  syserr = IRAD::Sys::CreateDirectory(dirname);
169  if(syserr){
170  std::cout << "ElmerModuleDriver:MakeProject: Unable to create directory " << dirname << "."
171  << std::endl;
172  return(1);
173  }
174  if(verb > 1)
175  std::cout << "ElmerModuleDriver:MakeProject: done." << std::endl;
176  }
177  bool protect_svn = false;
178  dirname = NewName+"/trunk";
179  std::vector<std::string>::iterator pfi = ProtectedFiles.begin();
180  while(pfi != ProtectedFiles.end()){
181  std::string ProtectThisFile(dirname+"/"+*pfi++);
182  std::string ProtectedFile(ProtectThisFile+".backup");
183  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
184  IRAD::Sys::Rename(ProtectThisFile,ProtectedFile);
185  }
186  pfi = CommonFiles.begin();
187  while(pfi != CommonFiles.end()){
188  std::string ProtectThisFile(dirname+"/"+*pfi++);
189  std::string ProtectedFile(ProtectThisFile+".backup");
190  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
191  IRAD::Sys::Rename(ProtectThisFile,ProtectedFile);
192  }
193  std::ostringstream ComStr;
194  if(!IRAD::Sys::FILEEXISTS(dirname)){
195  if(verb > 1)
196  std::cout << "ElmerModuleDriver:MakeProject: Creating directory " << dirname << "...";
197  ComStr << "cp -r " << OriginalName << " " << dirname;
198  } else {
199  if(verb > 1)
200  std::cout << "ElmerModuleDriver:MakeProject: Making project files from template ...";
201  ComStr << "cp -r " << OriginalName << "/* " << dirname;
202  }
203  IRAD::Sys::InProcess System(ComStr.str());
204  std::string comline;
205  while(std::getline(System,comline)){
206  if(verb > 1)
207  std::cout << comline << std::endl;
208  }
209  if(verb)
210  std::cout << "ElmerModuleDriver:MakeProject: done." << std::endl;
211  if(verb)
212  std::cout << "ElmerModuleDriver:MakeProject: Cleaning up ...";
213  ComStr.str("");
214  ComStr << "rm -rf " << dirname << "/.svn";
215  System.Execute(ComStr.str());
216  int n = 0;
217  while(std::getline(System,comline))
218  n++;
219  pfi = ProtectedFiles.begin();
220  while(pfi != ProtectedFiles.end()){
221  std::string ProtectThisFile(dirname+"/"+*pfi++);
222  std::string ProtectedFile(ProtectThisFile+".backup");
223  if(IRAD::Sys::FILEEXISTS(ProtectedFile)){
224  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
225  IRAD::Sys::Remove(ProtectThisFile);
226  IRAD::Sys::Rename(ProtectedFile,ProtectThisFile);
227  }
228  }
229  pfi = CommonFiles.begin();
230  while(pfi != CommonFiles.end()){
231  std::string ProtectThisFile(dirname+"/"+*pfi++);
232  std::string ProtectedFile(ProtectThisFile+".backup");
233  std::string CommonFileTemplate(ProtectThisFile+".template");
234  if(IRAD::Sys::FILEEXISTS(ProtectedFile)){
235  if(IRAD::Sys::FILEEXISTS(ProtectThisFile))
236  IRAD::Sys::Rename(ProtectThisFile,CommonFileTemplate);
237  IRAD::Sys::Rename(ProtectedFile,ProtectThisFile);
238  }
239  }
240  if(verb)
241  std::cout << "ElmerModuleDriver:MakeProject: done." << std::endl;
242  if(verb > 1)
243  std::cout << "ElmerModuleDriver:MakeProject: Done creating new project files."
244  << std::endl;
245  if(verb)
246  std::cout << "ElmerModuleDriver:MakeProject: Renaming project...";
247  if(IRAD::Sys::ChDir(dirname)){
248  std::cout << "ElmerModuleDriver:MakeProject: Something went wrong, cannot find new project directory."
249  << std::endl;
250  return(1);
251  }
252  ComStr.str("");
253  ComStr << "grep -i " << OriginalName << " -r * | cut -d \":\" -f 1 | sort | uniq";
254  if(verb > 1)
255  std::cout << " " << ComStr.str() << std::endl;
256  System.Execute(ComStr.str());
257  std::vector<std::string> filenames;
258  if(verb > 1)
259  std::cout << "ElmerModuleDriver:MakeProject: Files to change:" << std::endl;
260  while(std::getline(System,comline)){
261  if(!Excluded(comline)){
262  filenames.push_back(comline);
263  if(verb > 1)
264  std::cout << " " << comline << std::endl;
265  }
266  }
267  std::vector<std::string>::iterator fni = filenames.begin();
268  if(verb > 1)
269  std::cout << "ElmerModuleDriver:MakeProject: Processing files....";
270  while(fni != filenames.end()){
271  std::string filename(*fni++);
272  if(verb > 1)
273  std::cout << "ElmerModuleDriver:MakeProject: File: " << filename << std::endl;
274  ComStr.str("");
275  ComStr << "sed -i 's/" << OriginalName << "/" << NewName << "/g' " << filename;
276  if(verb > 1)
277  std::cout << "ElmerModuleDriver:MakeProject: " << ComStr.str() << std::endl;
278  System.Execute(ComStr.str());
279  int n = 0;
280  while(std::getline(System,comline))
281  n++;
282  ComStr.str("");
283  ComStr << "sed -i 's/" << oupper << "/" << nupper << "/g' " << filename;
284  if(verb > 1)
285  std::cout << " " << ComStr.str() << std::endl;
286  // std::cout << ComStr.str() << std::endl;
287  System.Execute(ComStr.str());
288  while(std::getline(System,comline))
289  n++;
290  ComStr.str("");
291  ComStr << "sed -i 's/" << olower << "/" << nlower << "/g' " << filename;
292  if(verb > 1)
293  std::cout << " " << ComStr.str() << std::endl;
294  // std::cout << ComStr.str() << std::endl;
295  System.Execute(ComStr.str());
296  while(std::getline(System,comline))
297  n++;
298  }
299  if(verb > 1)
300  std::cout << "ElmerModuleDriver:MakeProject: Done processing file contents." << std::endl
301  << " Renaming files..." << std::endl;
302  ComStr.str("");
303  // Now the inside of all files is fixed, need to fix filenames
304  ComStr << "find . -name \"*" << OriginalName << "*\"";
305  System.Execute(ComStr.str());
306  std::string::size_type olen = OriginalName.length();
307  std::string::size_type nlen = NewName.length();
308  while(std::getline(System,comline)){
309  if(verb > 1)
310  std::cout << "ElmerModuleDriver:MakeProject: Renaming " << comline << " to ";
311  std::string newname(comline);
312  std::string::size_type x = newname.find(OriginalName);
313  while(x != std::string::npos){
314  newname.replace(x,olen,NewName);
315  x = newname.find(OriginalName);
316  }
317  ComStr.str("");
318  if(verb > 1)
319  std::cout << newname << std::endl;
320  ComStr << "mv " << comline << " " << newname;
321  // std::cout << ComStr.str() << std::endl;
322  IRAD::Sys::InProcess MV(ComStr.str());
323  }
324  if(verb > 1)
325  std::cout << "done." << std::endl;
326  return(0);
327  }
328 };
329 
330 int main(int argc,char *argv[])
331 {
332  return(ElmerModuleDriver::MakeProject(argc,argv));
333 }
bool Excluded(const std::string &filename)
Exclude certain template files from conversion in project creation.
int MakeProject(int argc, char *argv[])
Creates a new project from an ElmerModuleDriver base.
#define main
Definition: icoFoamModule.C:2