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
Driver.H
Go to the documentation of this file.
1 #ifndef __ELMER_FOAM_FSI_H__
8 #define __ELMER_FOAM_FSI_H__
9 #ifdef _ELMERFOAMFSI_PARALLEL_
10 #include "COMM.H"
11 #endif
12 #include "Global.H"
13 #include "Profiler.H"
14 #include "com.h"
15 
16 namespace ElmerFoamFSI {
17 
38  // namespace ExampleProgram {
42  typedef IRAD::Profiler::ProfilerObj ProfilerType;
46  typedef std::string StackType;
54  typedef IRAD::Global::GlobalObj<StackType,int,ProfilerType> GlobalType;
55 #ifdef _ELMERFOAMFSI_PARALLEL_
56  typedef IRAD::Comm::CommunicatorObject CommType;
63  typedef IRAD::Global::ParallelGlobalObj<CommType,StackType,int,ProfilerType> PGlobalType;
64 #endif
65  class DriverComLine : public IRAD::Util::ComLineObject
74  {
75  public:
77  : IRAD::Util::ComLineObject()
78  {};
79  DriverComLine(const char *args[])
80  : IRAD::Util::ComLineObject(args)
81  {};
88  void Initialize(){
89  AddOption('h',"help");
90  AddOption('d',"debug");
91  AddOption('v',"verbosity",1);
92  AddOption('o',"output",2,"filename");
93  // AddArgument("input",1);
94  AddHelp("help","Prints this long version of help.");
95  AddHelp("debug","Turns on debugging output.");
96  AddHelp("output","Specifies the name of the output file.");
97  AddArgument("inputfile",1);
98  std::ostringstream Ostr;
99  Ostr << "A configuration file with parameters.";
100  // Ostr << "Mode-dependent input: Input <filename> for serial example"
101  // << "\n\t\tprogram, or <number of divisions> for parallel example.";
102  AddArgHelp("inputfile",Ostr.str());
103  // The following commented string formation is kept around to
104  // illustrate the required tabs and newlines to make the output
105  // look proper:
106  //
107  // Ostr << "Use fixed problem size in scalability analysis. Only makes"
108  // << "\n\t\tsense when scalability mode is enabled.";
109  // Ostr.str("");
110  Ostr.str("");
111  Ostr << "Example ElmerFoamFSI program.";
112  _description.assign(Ostr.str());
113  };
114  };
115 
126  typedef IRAD::Global::Program<GlobalType,ComLineType> SerialProgramType;
127 #ifdef _ELMERFOAMFSI_PARALLEL_
128  typedef IRAD::Global::Program<PGlobalType,ComLineType> ParallelProgramType;
135 #endif
164  {
165  protected:
167  std::string output_name;
169  std::string input_name;
173  std::ofstream Ouf;
175  std::ifstream Inf;
191  int argc;
193  char **argv;
194  public:
200  {};
204  SerialDriverProgram(int nargs,char **args) :
205  SerialProgramType(nargs,args), argc(nargs), argv(args)
206  {
207  int nargs_dupe = 2;
208  char **args_dupe;
209  args_dupe = new char * [3];
210  args_dupe[0] = const_cast<char *>("test");
211  args_dupe[1] = const_cast<char *>("-com-mpi");
212  args_dupe[2] = NULL;
213  COM_init(&nargs_dupe,&args_dupe);
214  };
218  virtual int Initialize()
219  {
220  int retval = SerialProgramType::Initialize();
221  if(!_command_line.GetOption("help").empty()){
222  std::ostringstream Ostr;
223  Ostr << _command_line.LongUsage() << std::endl;
224  StdOut(Ostr.str());
225  return(-1);
226  }
227  if(retval){
228  std::ostringstream Ostr;
229  Ostr << _command_line.ErrorReport() << std::endl
230  << std::endl << _command_line.ShortUsage() << std::endl;
231  ErrOut(Ostr.str());
232  return(retval);
233  }
234  if(!_command_line.GetOption("debug").empty()){
235  this->SetDebugLevel(2);
236  this->SetDebugStream(std::cout);
237  }
238  // Check if output file is specified
239  output_name = _command_line.GetOption("output");
240 
241  // Set input file to first argument
242  std::vector<std::string> args(_command_line.GetArgs());
243  input_name = args[0];
244 
245  // Check the verbosity level
246  std::string sverb(_command_line.GetOption("verbosity"));
247  if(sverb.empty() || sverb == ".true.")
248  verblevel = 1;
249  else {
250  std::istringstream Vin(sverb);
251  Vin >> verblevel;
252  if(verblevel < 0)
253  verblevel = 1;
254  }
255 
256  //Set the verblevel within IRAD
257  SetVerbLevel((char)verblevel);
258 
259  // If high verbosity, stick a configuration blurb to stdout.
260  if(verblevel > 1){
261  std::ostringstream Ostr;
262  Ostr << "Configuration:" << std::endl
263  << "verbosity = " << verblevel << std::endl
264  << "config file = " << input_name << std::endl;
265  if(!output_name.empty())
266  Ostr << "output file = " << output_name << std::endl;
267  Ostr << std::endl;
268  StdOut(Ostr.str());
269  }
270 
271  return(0);
272  };
276  int VerbLevel() const { return verblevel;};
281  if(COM_initialized())
282  COM_finalize();
283  Report(Ouf);
284  if(Ouf){
285  Ouf.close();
286  SetOutStream(std::cout);
287  }
288  };
292  int DriverRun();
293  };
294 
295 
296 #ifdef _ELMERFOAMFSI_PARALLEL_
297  class ParallelDriverProgram : public ParallelProgramType
366  {
367  protected:
369  std::string output_name;
371  std::string input_name;
373  int verblevel;
375  std::ofstream Ouf;
377  std::ifstream Inf;
379  int fluidsInitHandle;
381  int fluidsRunHandle;
383  int fluidsStepHandle;
385  int fluidsFinalizeHandle;
387  int structuresInitHandle;
389  int structuresRunHandle;
391  int structuresFinalizeHandle;
393  int argc;
395  char **argv;
396  public:
400  ParallelDriverProgram() :
401  ParallelProgramType()
402  {verblevel = 0;};
406  ParallelDriverProgram(int nargs,char **args) :
407  ParallelProgramType(nargs,args), argc(nargs), argv(args)
408  {
409  // initializing COM with -com-mpi to force calling MPI_init
410  int nargs_dupe = 2;
411  char **args_dupe;
412  args_dupe = new char * [3];
413  args_dupe[0] = const_cast<char *>("dummy");
414  args_dupe[1] = const_cast<char *>("-com-mpi");
415  args_dupe[2] = NULL;
416  COM_init(&nargs_dupe,&args_dupe);
417  // at this point we should have communicator setup
418  //std::cout << "My Rank = " << Rank() << std::endl;
419  };
423  ParallelDriverProgram(ComLineType &comline,CommType &incomm)
424  {
425  this->_command_line.Copy(comline);
426  this->Init(_command_line.ProgramName(),incomm);
427  verblevel = 0;
428  };
432  virtual int Initialize()
433  {
434  int retval = ParallelProgramType::Initialize();
435  // Check if the user just entered -h or --help
436  if(!_command_line.GetOption("help").empty()){
437  std::ostringstream Ostr;
438  Ostr << _command_line.LongUsage() << std::endl;
439  StdOut(Ostr.str());
440  return(-1);
441  }
442  if(retval){ // then there were errors on the command line
443  std::ostringstream Ostr;
444  Ostr << _command_line.ErrorReport() << std::endl
445  << std::endl << _command_line.ShortUsage() << std::endl;
446  ErrOut(Ostr.str());
447  return(retval);
448  }
449  // setting up debuger if needed
450  if(!_command_line.GetOption("debug").empty()){
451  this->SetDebugLevel(2);
452  this->SetDebugStream(std::cout);
453  }
454 
455  // Everything is OK up to here, so check for an output file.
456  output_name = _command_line.GetOption("output");
457 
458  // Set input file to first argument
459  std::vector<std::string> args(_command_line.GetArgs());
460  input_name = args[0];
461 
462  // Check the verbosity level
463  std::string sverb(_command_line.GetOption("verbosity"));
464  if(sverb.empty() || sverb == ".true.")
465  verblevel = 1;
466  else {
467  std::istringstream Vin(sverb);
468  Vin >> verblevel;
469  if(verblevel < 0)
470  verblevel = 1;
471  }
472 
473  //Set the verblevel within IRAD
474  SetVerbLevel((char)verblevel);
475 
476  // If high verbosity, stick a configuration blurb to stdout.
477  if(verblevel > 1){
478  std::ostringstream Ostr;
479  Ostr << "*****************************************************"<< std::endl;
480  Ostr << "* Reading the input file *"<< std::endl;
481  Ostr << "*****************************************************"<< std::endl;
482  Ostr << "* Configuration:" << std::endl
483  << "* verbosity = " << verblevel << std::endl;
484  if(!input_name.empty())
485  Ostr << "* input file = " << input_name << std::endl;
486  if(!output_name.empty())
487  Ostr << "* output file = " << output_name << std::endl;
488  Ostr << "*" << std::endl;
489  Ostr << "*****************************************************"<< std::endl;
490  StdOut(Ostr.str());
491  }
492 
493  return(0);
494  };
498  int VerbLevel() const { return verblevel;};
502  virtual ~ParallelDriverProgram() {
503  if(COM_initialized())
504  COM_finalize();
505  if(Ouf){
506  Ouf.close();
507  SetOutStream(std::cout);
508  }
509  };
513  int DriverRun();
514  };
515 #endif
516 
517 
528  template<typename ProgramType>
529  int Driver(int argc,char *argv[])
530  {
531  ProgramType MyProgram(argc,argv);
532  int retval = MyProgram.Initialize();
533  if(retval){
534  if(retval > 0){ // then there was an error
535  MyProgram.ErrOut("Error in Program Initialization.\n");
536  return(1);
537  }
538  else return(0); // no error, but the program should not continue
539  }
540  if(MyProgram.VerbLevel() > 1)
541  MyProgram.StdOut("Calling RUN.\n");
542  if(MyProgram.DriverRun()){
543  MyProgram.ErrOut("Error in Program RUN method.\n");
544  return(1);
545  }
546  if(MyProgram.VerbLevel() > 1){
547  std::ostringstream Ostr;
548  MyProgram.Report(Ostr);
549  MyProgram.StdOut(Ostr.str());
550  MyProgram.StdOut("Calling Finalize.\n");
551  }
552  if(MyProgram.Finalize()){
553  MyProgram.ErrOut("Error in Program Finalization.\n");
554  return(1);
555  }
556  if(MyProgram.VerbLevel() > 1)
557  MyProgram.StdOut("All done.\n");
558  return(0);
559  }
560 
561 
566 #ifdef _ELMERFOAMFSI_PARALLEL_
567  inline double f(double x)
578  {
579  return (4.0 / (1.0 + x*x));
580  }
584  typedef ElmerFoamFSI::ParallelDriverProgram PEProgramType;
585 #endif
586  // };
587 };
588 #endif
int structuresInitHandle
Handle/identifier for structures init.
Definition: Driver.H:185
IRAD::Global::GlobalObj< StackType, int, ProfilerType > GlobalType
Convenience type definition for the serial global object.
Definition: Driver.H:54
char ** argv
Object-local copy for argv.
Definition: Driver.H:193
The ComLineObject for the example programs.
Definition: Driver.H:73
int fluidsRunHandle
Handle/identifier for fluids run.
Definition: Driver.H:179
DriverComLine(const char *args[])
Definition: Driver.H:79
int DriverRun()
This function implements the main function executed by the program.
ElmerFoamFSI::SerialDriverProgram DriverProgramType
Convenience definition for serial program type.
Definition: Driver.H:565
std::string input_name
Name of input file.
Definition: Driver.H:169
std::string StackType
Convenience type definition for program stack.
Definition: Driver.H:46
DriverComLine ComLineType
Convenience type definition for the example comline object.
Definition: Driver.H:119
int structuresFinalizeHandle
Handle/identifier for structures finalize.
Definition: Driver.H:189
int fluidsStepHandle
Handle/identifier for fluids step.
Definition: Driver.H:181
std::ofstream Ouf
Outfile stream for output.
Definition: Driver.H:173
int fluidsFinalizeHandle
Handle/identifier for fluids finalize.
Definition: Driver.H:183
virtual ~SerialDriverProgram()
Destructor.
Definition: Driver.H:280
Implementation of the basic parts of the serial program example.
Definition: Driver.H:163
std::string output_name
Name of file for output.
Definition: Driver.H:167
int verblevel
Verbosity level.
Definition: Driver.H:171
IRAD::Profiler::ProfilerObj ProfilerType
Encapsulate example program-specific code constructs.
Definition: Driver.H:42
int argc
Object-local copy for argc.
Definition: Driver.H:191
int structuresRunHandle
Handle/identifier for structures run.
Definition: Driver.H:187
IRAD::Comm::CommunicatorObject CommType
Convenience typedef for CommunicatorObject.
SerialDriverProgram()
Default constructor.
Definition: Driver.H:198
virtual int Initialize()
Initializes native data structures from commandline args.
Definition: Driver.H:218
int Driver(int argc, char *argv[])
Unified driver for the example programs.
Definition: Driver.H:529
int fluidsInitHandle
Handle/identifier for fluids init.
Definition: Driver.H:177
IRAD::Global::ParallelGlobalObj< CommTypeIrad, StackType, int, ProfilerType > PGlobalType
int VerbLevel() const
Returns verbosity level.
Definition: Driver.H:276
IRAD::Global::Program< GlobalType, ComLineType > SerialProgramType
Convenience type definition for the serial program.
Definition: Driver.H:126
SerialDriverProgram(int nargs, char **args)
Constructor designed to take the commandline args.
Definition: Driver.H:204
void Initialize()
This is where the actual options and arguments are described.
Definition: Driver.H:88
std::ifstream Inf
Infile stream for input.
Definition: Driver.H:175