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
ParallelTestElmerModuleDriver.C
Go to the documentation of this file.
1 #include "ComLine.H"
11 #include "TestElmerModuleDriver.H"
12 #include "COMM.H"
13 
14 
15 namespace ElmerModuleDriver{
16 
20  typedef IRAD::Comm::CommunicatorObject CommType;
50  int ParallelTest(int argc,char *argv[])
51  {
52 
53  int com_initialized = COM_initialized();
54  bool com_initialized_pass = (com_initialized <= 0);
55 
56  COM_init(&argc, &argv);
57  if(com_initialized_pass)
58  com_initialized = (COM_initialized() > 0);
59 
60  // The default verbosity is 0
61  int verblevel = 0;
62 
63  // This sets everything up with MPI
64  ElmerModuleDriver::CommType communicator(&argc,&argv);
65  int rank = communicator.Rank();
66  int nproc = communicator.Size();
67  bool do_stdout = !rank;
68 
69  // This line creates the ElmerModuleDriver::TestComLine object and passes in
70  // the command line arguments as a (const char **).
71  TestComLine comline((const char **)(argv));
72  // The call to comline.Initialize() reads the command line arguments
73  // from the array passed in the previous line.
74  comline.Initialize();
75 
76 
77  // The ProcessOptions() call does detailed examination of the command
78  // line arguments to check for user errors or other problems. This call
79  // will return non-zero if there were errors on the commandline.
80  int clerr = comline.ProcessOptions();
81  // Check if the user just wanted to get the help and exit
82  if(!comline.GetOption("help").empty()){
83  // Print out the "long usage" (i.e. help) message to stdout
84  if(do_stdout){
85  std::cout << comline.LongUsage() << std::endl;
86  if(verblevel > 1)
87  std::cout << "ElmerModuleDriver::ParallelTest: Exiting test function (success)"
88  << std::endl;
89  }
90  communicator.SetExit(1);
91  }
92  if(communicator.Check())
93  return(0);
94  if(clerr){
95  if(do_stdout){
96  std::cout << comline.ErrorReport() << std::endl
97  << std::endl << comline.ShortUsage() << std::endl;
98  if(verblevel > 2)
99  std::cout << "ElmerModuleDriver::ParallelTest: Exiting test function (fail)" << std::endl;
100  }
101  communicator.SetExit(1);
102  }
103  if(communicator.Check())
104  return(1);
105 
106  // These outstreams allow the output to file to be set up and separated
107  // from the stdout.
108  std::ofstream Ouf;
109  std::ostream *Out = NULL;
110  if(do_stdout)
111  Out = &std::cout;
112 
113  // The next few lines populate some strings based on the
114  // users input from the commandline.
115  std::string OutFileName(comline.GetOption("output"));
116  std::string TestName(comline.GetOption("name"));
117  std::string ListName(comline.GetOption("list"));
118  std::string sverb(comline.GetOption("verblevel"));
119  std::string SourcePath(comline.GetOption("source"));
120 
121  // The following block parses and sets the verbosity level
122  if(!sverb.empty()){
123  verblevel = 1;
124  if(sverb != ".true."){
125  std::istringstream Istr(sverb);
126  Istr >> verblevel;
127  if(verblevel < 0)
128  verblevel = 1;
129  }
130  }
131 
132  // This block sets up the output file if the user specified one
133  if(!OutFileName.empty()){
134  if(Out){
135  Ouf.open(OutFileName.c_str());
136  if(!Ouf){
137  std::cout << "ElmerModuleDriver::ParallelTest> Error: Could not open output file, "
138  << OutFileName << " for test output. Exiting (fail)." << std::endl;
139  communicator.SetExit(1);
140  }
141  Out = &Ouf;
142  }
143  if(communicator.Check())
144  return(1);
145  }
146 
147  if(verblevel > 1 && Out)
148  *Out << "ElmerModuleDriver::ParallelTest: Entering test function" << std::endl;
149 
150  // Make an instance of the ElmerModuleDriver testing object, ElmerModuleDriver::ParallelTestingObject
151  ElmerModuleDriver::ParallelTestingObject<ElmerModuleDriver::CommType,ElmerModuleDriver::TestResults> test(communicator);
152  // Make an instance of the ElmerModuleDriver results object, ElmerModuleDriver::TestResults
154 
155  //Set the source directory for the testing object if it was input
156  if(!SourcePath.empty()){
157  test.SetSourceDirPath(SourcePath);
158  }
159 
160  // If the user specified a name, then run only the named test
161  if(!TestName.empty()){
162  // This call runs a test by name
163  test.RunTest(TestName,results);
164  }
165  // Otherwise, if the user specified a list, then read the list and
166  // run the listed tests.
167  else if(!ListName.empty()){
168  std::ifstream ListInf;
169  ListInf.open(ListName.c_str());
170  if(!ListInf){
171  if(Out)
172  *Out << "ElmerModuleDriver::ParallelTest> Error: Could not open list of tests in file "
173  << ListName << ". Exiting (fail)." << std::endl;
174  communicator.SetExit(1);
175  }
176  if(communicator.Check())
177  return(1);
178  std::string testname;
179  while(std::getline(ListInf,testname))
180  test.RunTest(testname,results);
181  ListInf.close();
182  }
183  else {
184  // This call runs all the tests for the ElmerModuleDriver namespace.
185  test.Process(results);
186  }
187 
188  COM_finalize();
189  if(com_initialized_pass)
190  com_initialized_pass = (COM_initialized() <= 0);
191 
192  if(Out)
193  *Out << results << std::endl;
194 
195  if(Out && Ouf)
196  Ouf.close();
197 
198  if((verblevel > 1) && Out)
199  *Out << "ElmerModuleDriver::ParallelTest: Exiting test function (success)" << std::endl;
200 
201  return(0);
202  }
203 };
204 
205 int main(int argc,char *argv[])
206 {
207  return(ElmerModuleDriver::ParallelTest(argc,argv));
208 }
int ParallelTest(int argc, char *argv[])
Drives the ElmerModuleDriver::TestObject.
ComLineObject for ElmerModuleDriver testing command-line interface.
IRAD::Util::TestResults TestResults
Project-specific test results type.
#define main
Definition: icoFoamModule.C:2
IRAD::Comm::CommunicatorObject CommType
Convenience typedef for CommunicatorObject.
Testing utilities for ElmerModuleDriver.