Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExampleProgram.H
Go to the documentation of this file.
1 #ifndef __EXAMPLE_PROGRAM_H__
8 #define __EXAMPLE_PROGRAM_H__
9 #ifdef _GRIDCONVERSION_PARALLEL_
10 #include "COMM.H"
11 #endif
12 #include "Global.H"
13 #include "Profiler.H"
14 
15 namespace GridConversion {
16 
37  namespace ExampleProgram {
41  typedef IRAD::Profiler::ProfilerObj ProfilerType;
45  typedef std::string StackType;
53  typedef IRAD::Global::GlobalObj<StackType,int,ProfilerType> GlobalType;
54 #ifdef _GRIDCONVERSION_PARALLEL_
55  typedef IRAD::Comm::CommunicatorObject CommType;
62  typedef IRAD::Global::ParallelGlobalObj<CommType,StackType,int,ProfilerType> PGlobalType;
63 #endif
64  class ExampleProgramComLine : public IRAD::Util::ComLineObject
73  {
74  public:
76  : IRAD::Util::ComLineObject()
77  {};
78  ExampleProgramComLine(const char *args[])
79  : IRAD::Util::ComLineObject(args)
80  {};
87  void Initialize(){
88  AddOption('h',"help");
89  AddOption('v',"verbosity",1);
90  AddOption('o',"output",2,"filename");
91  AddArgument("input",1);
92  AddHelp("help","Prints this long version of help.");
93  AddHelp("output","Specifies the name of the output file.");
94  std::ostringstream Ostr;
95  Ostr << "Mode-dependent input: Input <filename> for serial example"
96  << "\n\t\tprogram, or <number of divisions> for parallel example.";
97  AddArgHelp("input",Ostr.str());
98  // The following commented string formation is kept around to
99  // illustrate the required tabs and newlines to make the output
100  // look proper:
101  //
102  // Ostr << "Use fixed problem size in scalability analysis. Only makes"
103  // << "\n\t\tsense when scalability mode is enabled.";
104  // Ostr.str("");
105  Ostr.str("");
106  Ostr << "Example GridConversion program.";
107  _description.assign(Ostr.str());
108  };
109  };
110 
121  typedef IRAD::Global::Program<GlobalType,ComLineType> SerialProgramType;
122 #ifdef _GRIDCONVERSION_PARALLEL_
123  typedef IRAD::Global::Program<PGlobalType,ComLineType> ParallelProgramType;
130 #endif
131  class SerialProgram : public SerialProgramType
159  {
160  protected:
162  std::string output_name;
164  std::string input_name;
168  std::ofstream Ouf;
170  std::ifstream Inf;
171  public:
177  {};
181  SerialProgram(int nargs,char **args) :
182  SerialProgramType(nargs,args)
183  {};
187  virtual int Initialize()
188  {
189  int retval = SerialProgramType::Initialize();
190  if(!_command_line.GetOption("help").empty()){
191  std::ostringstream Ostr;
192  Ostr << _command_line.LongUsage() << std::endl;
193  StdOut(Ostr.str());
194  return(-1);
195  }
196  if(retval){
197  std::ostringstream Ostr;
198  Ostr << _command_line.ErrorReport() << std::endl
199  << std::endl << _command_line.ShortUsage() << std::endl;
200  ErrOut(Ostr.str());
201  return(retval);
202  }
203 
204  // Check if output file is specified
205  output_name = _command_line.GetOption("output");
206 
207  // Set input file to first argument
208  std::vector<std::string> args(_command_line.GetArgs());
209  input_name = args[0];
210 
211  // Check the verbosity level
212  std::string sverb(_command_line.GetOption("verbosity"));
213  if(sverb.empty() || sverb == ".true.")
214  verblevel = 1;
215  else {
216  std::istringstream Vin(sverb);
217  Vin >> verblevel;
218  if(verblevel < 0)
219  verblevel = 1;
220  }
221 
222  // If high verbosity, stick a configuration blurb to stdout.
223  if(verblevel > 1){
224  std::ostringstream Ostr;
225  Ostr << "Configuration:" << std::endl
226  << "verbosity = " << verblevel << std::endl
227  << "input_name = " << input_name << std::endl;
228  if(!output_name.empty())
229  Ostr << "output file = " << output_name << std::endl;
230  Ostr << std::endl;
231  StdOut(Ostr.str());
232  }
233 
234  return(0);
235  };
239  int VerbLevel() const { return verblevel;};
243  virtual ~SerialProgram() {
244  if(Ouf){
245  Ouf.close();
246  SetOutStream(std::cout);
247  }
248  };
252  virtual int Run();
253  };
254 
255 
256 #ifdef _GRIDCONVERSION_PARALLEL_
257  class ParallelProgram : public ParallelProgramType
326  {
327  protected:
329  std::string output_name;
331  int verblevel;
333  std::ofstream Ouf;
335  int ndiv;
336  public:
340  ParallelProgram() :
341  ParallelProgramType()
342  {verblevel = 0;};
346  ParallelProgram(int nargs,char **args) :
347  ParallelProgramType(nargs,args)
348  {verblevel = 0;};
352  ParallelProgram(ComLineType &comline,CommType &incomm)
353  {
354  this->_command_line.Copy(comline);
355  this->Init(_command_line.ProgramName(),incomm);
356  verblevel = 0;
357  };
361  virtual int Initialize()
362  {
363  int retval = ParallelProgramType::Initialize();
364  // Check if the user just entered -h or --help
365  if(!_command_line.GetOption("help").empty()){
366  std::ostringstream Ostr;
367  Ostr << _command_line.LongUsage() << std::endl;
368  StdOut(Ostr.str());
369  return(-1);
370  }
371  if(retval){ // then there were errors on the command line
372  std::ostringstream Ostr;
373  Ostr << _command_line.ErrorReport() << std::endl
374  << std::endl << _command_line.ShortUsage() << std::endl;
375  ErrOut(Ostr.str());
376  return(retval);
377  }
378 
379  // Everything is OK up to here, so check for an output file.
380  output_name = _command_line.GetOption("output");
381 
382  // Set number of divisions to first argument
383  std::vector<std::string> args(_command_line.GetArgs());
384  std::istringstream Istr(args[0]);
385  Istr >> ndiv;
386 
387  // Check the verbosity level
388  std::string sverb(_command_line.GetOption("verbosity"));
389  if(sverb.empty() || sverb == ".true.")
390  verblevel = 1;
391  else {
392  std::istringstream Vin(sverb);
393  Vin >> verblevel;
394  if(verblevel < 0)
395  verblevel = 1;
396  }
397 
398  // If high verbosity, stick a configuration blurb to stdout.
399  if(verblevel > 1){
400  std::ostringstream Ostr;
401  Ostr << "Configuration:" << std::endl
402  << "verbosity = " << verblevel << std::endl
403  << "number of divisions = " << ndiv << std::endl;
404  if(!output_name.empty())
405  Ostr << "output file = " << output_name << std::endl;
406  Ostr << std::endl;
407  StdOut(Ostr.str());
408  }
409 
410  return(0);
411  };
415  int VerbLevel() const { return verblevel;};
419  virtual ~ParallelProgram() {
420  if(Ouf){
421  Ouf.close();
422  SetOutStream(std::cout);
423  }
424  };
428  virtual int Run();
429  };
430 #endif
431 
432 
443  template<typename ProgramType>
444  int Driver(int argc,char *argv[])
445  {
446  ProgramType MyProgram(argc,argv);
447  int retval = MyProgram.Initialize();
448  if(retval){
449  if(retval > 0){ // then there was an error
450  MyProgram.ErrOut("Error in Program Initialization.\n");
451  return(1);
452  }
453  else return(0); // no error, but the program should not continue
454  }
455  if(MyProgram.VerbLevel() > 1)
456  MyProgram.StdOut("Calling RUN.\n");
457  if(MyProgram.Run()){
458  MyProgram.ErrOut("Error in Program RUN method.\n");
459  return(1);
460  }
461  if(MyProgram.VerbLevel() > 1){
462  std::ostringstream Ostr;
463  MyProgram.Report(Ostr);
464  MyProgram.StdOut(Ostr.str());
465  MyProgram.StdOut("Calling Finalize.\n");
466  }
467  if(MyProgram.Finalize()){
468  MyProgram.ErrOut("Error in Program Finalization.\n");
469  return(1);
470  }
471  if(MyProgram.VerbLevel() > 1)
472  MyProgram.StdOut("All done.\n");
473  return(0);
474  }
475 
476 
481 #ifdef _GRIDCONVERSION_PARALLEL_
482  inline double f(double x)
493  {
494  return (4.0 / (1.0 + x*x));
495  }
499  typedef GridConversion::ExampleProgram::ParallelProgram PEProgramType;
500 #endif
501  };
502 };
503 #endif
Implementation of the basic parts of the serial program example.
int VerbLevel() const
Returns verbosity level.
GridConversion::ExampleProgram::SerialProgram SEProgramType
Convenience definition for serial program type.
ExampleProgramComLine ComLineType
Convenience type definition for the example comline object.
std::string StackType
Convenience type definition for program stack.
Implementation of the basic parts of the serial program example.
virtual int Initialize()
Initializes native data structures from commandline args.
virtual int Run()
This function implements the main function executed by the program.
Definition: DriverProgram.C:16
std::ifstream Inf
Infile stream for input.
std::ofstream Ouf
Outfile stream for output.
IRAD::Global::GlobalObj< StackType, int, ProfilerType > GlobalType
Convenience type definition for the serial global object.
IRAD::Profiler::ProfilerObj ProfilerType
Convenience type definition for Profiler object.
The ComLineObject for the example programs.
virtual int Run()
This function implements the main function executed by the program.
IRAD::Comm::CommunicatorObject CommType
Convenience typedef for CommunicatorObject.
void int int REAL * x
Definition: read.cpp:74
std::string input_name
Name of input file.
std::string output_name
Name of file for output.
SerialProgram(int nargs, char **args)
Constructor designed to take the commandline args.
int Driver(int argc, char *argv[])
Unified driver for the example programs.
int VerbLevel() const
Returns verbosity level.
void Initialize()
This is where the actual options and arguments are described.
virtual int Initialize()
Initializes native data structures from commandline args.
IRAD::Global::Program< GlobalType, ComLineType > SerialProgramType
Convenience type definition for the serial program.