Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RocstarTest.H
Go to the documentation of this file.
1 #ifndef __ROCSTAR_TEST_H__
13 #define __ROCSTAR_TEST_H__
14 #ifdef _ROCSTAR_PARALLEL_
15 #include "COMM.H"
16 #endif
17 #include "Testing.H"
18 #include "ExampleHeader.H"
19 #include "Profiler.H"
20 #include <vector>
21 #include <cmath>
22 
23 namespace Rocstar {
24 
28  namespace TestFixture {
34  double F1(double x) { return (2.0*x); };
40  double F2(double x) { return (3.0*x*x); };
41  };
51 
70  template<typename ResultsType>
71  class TestingObject : public IRAD::Util::TestingObject<ResultsType>
72  {
73  typedef IRAD::Util::TestingObject<ResultsType> TestingObjectBaseType;
74  protected:
75  // Example test fixtures for Rocstar testing.
76  //
77  // In a real project, there would be many test fixtures
78  // in the TestingObject.
82  std::string ExampleTestFixture;
86  std::vector<int> N;
87  public:
92 
96  virtual void Epilogue() {};
97 
105  virtual void Prologue(){
106  ExampleTestFixture.assign("ExampleTestData");
107  for(int i = 10;i < 10000000;i*=10) N.push_back(i);
108  }
114  double F1(double x) { return (2.0*x); };
120  double F2(double x) { return (3.0*x*x); };
130  virtual void Test__ExampleFunction(ResultsType &result) {
131  // This is an actual test of the function called
132  // ExampleFunction. The name Test__XXXXX will
133  // eventually help automated utilities with
134  // running tests by name.
135  std::string ExampleResult(ExampleFunction(ExampleTestFixture));
136  result.UpdateResult("ExampleFunction:Works",
137  ExampleResult == ExampleTestFixture);
138  result.UpdateResult("ExampleFunction:Fails",
139  ExampleResult != ExampleTestFixture);
140  }
155  virtual void Test__TrapezoidQuadrature(ResultsType &result) {
156  std::ostringstream Ostr;
157  std::vector<double> E;
158  size_t n = 2*N.size();
159  bool runs = true;
160  for(std::vector<int>::iterator i = N.begin();i != N.end();i++){
161  double Ii = 0.0;
162  try {
164  } catch (...) {
165  runs = false;
166  }
167  E.push_back(std::fabs(Ii-1.0));
168  }
169  bool order2 = (E[0] < 1e-14);
170  for(std::vector<int>::iterator i = N.begin();i != N.end();i++){
171  double Ii = 0.0;
172  try {
174  } catch (...) {
175  runs = false;
176  }
177  E.push_back(std::fabs(Ii-1.0));
178  }
179  result.UpdateResult("TrapezoidQuadrature:Runs",runs);
180  result.UpdateResult("TrapezoidQuadrature:Accurate",E[n-1] < 1e-12);
181  for(int i = N.size();i < n - 1;i++){
182  double e = E[i+1]/E[i];
183  double n1 = static_cast<double>(N[i-N.size()])/static_cast<double>(N[(i-N.size())+1]);
184  double p = std::log(e)/std::log(n1);
185  p -= 2;
186  p = std::abs(p);
187  if(p > 2e-2){
188  order2 = false;
189  }
190  }
191  result.UpdateResult("TrapezoidQuadrature:Order2",order2);
192  }
193 
207  virtual void Test__MidPointQuadrature(ResultsType &result) {
208  std::ostringstream Ostr;
209  std::vector<double> Ibar;
210  std::vector<double> E;
211  size_t n = 2*N.size();
212  bool runs = true;
213  bool order2 = true;
214  for(std::vector<int>::iterator i = N.begin();i != N.end();i++){
215  double Ii = 0.0;
216  try {
218  } catch (...) {
219  runs = false;
220  }
221  Ibar.push_back(Ii);
222  E.push_back(std::fabs(Ii-1.0));
223  }
224  order2 = (E[0] < 1e-14);
225  for(std::vector<int>::iterator i = N.begin();i != N.end();i++){
226  double Ii = 0.0;
227  try {
229  } catch (...) {
230  runs = false;
231  }
232  Ibar.push_back(Ii);
233  E.push_back(std::fabs(Ii-1.0));
234  }
235  result.UpdateResult("MidPointQuadrature:Runs",runs);
236  result.UpdateResult("MidPointQuadrature:Accurate",E[n-1] < 1e-12);
237  for(int i = N.size();i < n - 1;i++){
238  double e = E[i+1]/E[i];
239  double n1 = static_cast<double>(N[i-N.size()])/static_cast<double>(N[(i-N.size())+1]);
240  double p = std::log(e)/std::log(n1);
241  p -= 2;
242  p = std::abs(p);
243  if(p > 1e-2){
244  order2 = false;
245  }
246  }
247  result.UpdateResult("MidPointQuadrature:Order2",order2);
248  }
249 
255  virtual void Process(ResultsType &result){
256  Prologue();
257  Test__ExampleFunction(result);
259  Test__MidPointQuadrature(result);
260  Epilogue();
261  }
262 
269  virtual void RunTest(const std::string &name,ResultsType &result)
270  {
271  Prologue();
272  if(name == "ExampleFunction")
273  Test__ExampleFunction(result);
274  else if(name == "TrapezoidQuadrature")
276  else if(name == "MidPointQuadrature")
277  Test__MidPointQuadrature(result);
278  Epilogue();
279  }
280 
287  virtual void ProcessTests(std::list<std::string> &test_names,ResultsType &result){
288  Prologue();
289  std::list<std::string>::iterator tni = test_names.begin();
290  while(tni != test_names.end())
291  RunTest(*tni++,result);
292  Epilogue();
293  }
294 
295 
296  };
297 
298 #ifdef _ROCSTAR_PARALLEL_
299  template<typename CommType,typename ResultsType>
310  class ParallelTestingObject : TestingObject<ResultsType>
311  {
312  protected:
313  // Example test fixtures for parallel Rocstar testing.
314  //
318  CommType _communicator;
322  std::vector<int> N;
323  public:
327  ParallelTestingObject(CommType &incomm) :
328  TestingObject<ResultsType>(), _communicator(incomm) {};
329  CommType &GetCommunicator() { return(_communicator); };
330 
334  virtual void Epilogue() {};
335 
343  virtual void Prologue(){
344  for(int i = _communicator.Size();i <= 1000000;i*=2) N.push_back(i);
345  };
346 
363  virtual void Test__ParallelTrapezoidQuadrature(ResultsType &result) {
364  std::ostringstream Ostr;
365  int fixed_n = 1000000;
366  int rank = _communicator.Rank();
367  int nproc = _communicator.Size();
368  double Ii = 0.0;
369  bool runs = true;
370  bool accurate = true;
371  bool order2 = true;
372  bool scales = true;
373  std::vector<double> E;
374  std::vector<double> times;
375  for(int i = 1;i <= nproc;i*=nproc){
376  CommType subcomm;
377  int color = (rank < i);
378  _communicator.Split(color,rank,subcomm);
379  int nproc_color = subcomm.Size();
380  double time0 = IRAD::Profiler::Time();
381  if(color){
382  Ii = 0.0;
383  for(int j = 0;j < 200;j++){
384  if(runs){
385  try {
387  } catch (...) {
388  subcomm.SetExit(1);
389  }
390  if(subcomm.Check())
391  runs = false;
392  }
393  }
394  if(runs){
395  double Itot = 0.0;
396  subcomm.AllReduce(Ii, Itot,IRAD::Comm::DTDOUBLE, IRAD::Comm::SUMOP);
397  double error = 0.0;
398  error = std::abs(Itot - (200*nproc_color));
399  accurate = (error < 1e-14);
400  order2 = accurate;
401  }
402  }
403  times.push_back(IRAD::Profiler::Time() - time0);
404  if(nproc == 1)
405  break;
406  }
407  if(!rank){
408  double dt = *times.rbegin() - *times.begin();
409  double percent_change = dt/(*times.begin());
410  scales = (percent_change < (nproc*.005));
411  }
412  result.UpdateResult("ParallelTrapezoidQuadrature:Runs",runs);
413  result.UpdateResult("ParallelTrapezoidQuadrature:Accurate",accurate);
414  if(nproc > 1) // only report scaling results when procs are more than 1.
415  result.UpdateResult("ParallelTrapezoidQuadrature:WeakScaling",scales);
416  times.resize(0);
417  if(nproc > 1){ // If running on more than one proc, then do strong scaling test.
418  for(int i = 1;i <= nproc;i*=nproc){
419  CommType subcomm;
420  int color = (rank < i);
421  _communicator.Split(color,rank,subcomm);
422  int nproc_color = subcomm.Size();
423  double time0 = IRAD::Profiler::Time();
424  if(color){
425  int npart = *N.rbegin()/nproc_color;
426  Ii = 0.0;
427  for(int j = 0;j < 200;j++){
428  if(runs){
429  try {
431  } catch (...) {
432  subcomm.SetExit(1);
433  }
434  if(subcomm.Check())
435  runs = false;
436  }
437  }
438  if(runs){
439  double Itot = 0.0;
440  subcomm.AllReduce(Ii, Itot,IRAD::Comm::DTDOUBLE, IRAD::Comm::SUMOP);
441  double error = 0.0;
442  error = std::abs(Itot - (200*nproc_color));
443  accurate = (error < 1e-14);
444  order2 = accurate;
445  }
446  }
447  times.push_back(IRAD::Profiler::Time() - time0);
448  }
449  if(!rank){
450  double n_t = (*times.begin())/(*times.rbegin());
451  double n_p = n_t - nproc;
452  n_p = std::abs(n_p)/nproc;
453  scales = (n_p < 2e-1);
454  }
455  result.UpdateResult("ParallelTrapezoidQuadrateure:StrongScaling",scales);
456  }
457  for(int i = nproc;i <= 1000000;i*=10){
458  if(runs){
459  Ii = 0.0;
460  int n = i/nproc;
461  try {
463  } catch (...) {
464  _communicator.SetExit(1);
465  }
466  if(_communicator.Check())
467  runs = false;
468  if(runs){
469  double Itot = 0.0;
470  _communicator.AllReduce(Ii, Itot,IRAD::Comm::DTDOUBLE, IRAD::Comm::SUMOP);
471  double error = 0.0;
472  error = std::abs(Itot - nproc);
473  E.push_back(error);
474  }
475  }
476  }
477  int esize = E.size();
478  for(int i = 0;i < esize-1;i++){
479  double e = E[i+1]/E[i];
480  double n1 = .1;
481  double p = std::log(e)/std::log(n1);
482  p -= 2;
483  p = std::abs(p);
484  if(p > 1e-1){
485  order2 = false;
486  }
487  }
488  result.UpdateResult("ParallelTrapezoidQuadrature:Order2",order2);
489  }
490 
491 
508  virtual void Test__ParallelMidPointQuadrature(ResultsType &result) {
509  std::ostringstream Ostr;
510  int fixed_n = 1000000;
511  int rank = _communicator.Rank();
512  int nproc = _communicator.Size();
513  double Ii = 0.0;
514  bool runs = true;
515  bool accurate = true;
516  bool order2 = true;
517  bool scales = true;
518  std::vector<double> E;
519  std::vector<double> times;
520  for(int i = 1;i <= nproc;i*=nproc){
521  CommType subcomm;
522  int color = (rank < i);
523  _communicator.Split(color,rank,subcomm);
524  int nproc_color = subcomm.Size();
525  double time0 = IRAD::Profiler::Time();
526  if(color){
527  Ii = 0.0;
528  for(int j = 0;j < 200;j++){
529  if(runs){
530  try {
531  Ii += Rocstar::MidPointQuadrature(TestFixture::F1,0,1,fixed_n);
532  } catch (...) {
533  subcomm.SetExit(1);
534  }
535  if(subcomm.Check())
536  runs = false;
537  }
538  }
539  if(runs){
540  double Itot = 0.0;
541  subcomm.AllReduce(Ii, Itot,IRAD::Comm::DTDOUBLE, IRAD::Comm::SUMOP);
542  double error = 0.0;
543  error = std::abs(Itot - (200*nproc_color));
544  accurate = (error < 1e-14);
545  order2 = accurate;
546  }
547  }
548  times.push_back(IRAD::Profiler::Time() - time0);
549  if(nproc == 1)
550  break;
551  }
552  if(!rank){
553  double dt = *times.rbegin() - *times.begin();
554  double percent_change = dt/(*times.begin());
555  scales = (percent_change < (nproc*.005));
556  // std::cout << "Weak scaling timings:" << std::endl;
557  // std::vector<double>::iterator ti = weak_times.begin();
558  // while(ti != weak_times.end()){
559  // std::cout << ti-weak_times.begin()+1 << " "
560  // << *ti << std::endl;
561  // ti++;
562  // }
563  }
564  result.UpdateResult("ParallelMidPointQuadrature:Runs",runs);
565  result.UpdateResult("ParallelMidPointQuadrature:Accurate",accurate);
566  if(nproc > 1) // only report scaling results for nproc > 1
567  result.UpdateResult("ParallelMidPointQuadrature:WeakScaling",scales);
568  times.resize(0);
569  if(nproc > 1){ // only do scaling test if nproc > 1
570  for(int i = 1;i <= nproc;i*=nproc){
571  CommType subcomm;
572  int color = (rank < i);
573  _communicator.Split(color,rank,subcomm);
574  int nproc_color = subcomm.Size();
575  double time0 = IRAD::Profiler::Time();
576  if(color){
577  int npart = *N.rbegin()/nproc_color;
578  Ii = 0.0;
579  for(int j = 0;j < 200;j++){
580  if(runs){
581  try {
583  } catch (...) {
584  subcomm.SetExit(1);
585  }
586  if(subcomm.Check())
587  runs = false;
588  }
589  }
590  if(runs){
591  double Itot = 0.0;
592  subcomm.AllReduce(Ii, Itot,IRAD::Comm::DTDOUBLE, IRAD::Comm::SUMOP);
593  double error = 0.0;
594  error = std::abs(Itot - (200*nproc_color));
595  accurate = (error < 1e-14);
596  order2 = accurate;
597  }
598  }
599  times.push_back(IRAD::Profiler::Time() - time0);
600  }
601  if(!rank){
602  double n_t = (*times.begin())/(*times.rbegin());
603  double n_p = n_t - nproc;
604  n_p = std::abs(n_p)/nproc;
605  scales = (n_p < 2e-1);
606  }
607  result.UpdateResult("ParallelMidPointQuadrateure:StrongScaling",scales);
608  }
609  for(int i = nproc;i <= 1000000;i*=10){
610  if(runs){
611  Ii = 0.0;
612  int n = i/nproc;
613  try {
615  } catch (...) {
616  _communicator.SetExit(1);
617  }
618  if(_communicator.Check())
619  runs = false;
620  if(runs){
621  double Itot = 0.0;
622  _communicator.AllReduce(Ii, Itot,IRAD::Comm::DTDOUBLE, IRAD::Comm::SUMOP);
623  double error = 0.0;
624  error = std::abs(Itot - nproc);
625  E.push_back(error);
626  }
627  }
628  }
629  int esize = E.size();
630  for(int i = 0;i < esize-1;i++){
631  double e = E[i+1]/E[i];
632  double n1 = .1;
633  double p = std::log(e)/std::log(n1);
634  p -= 2;
635  p = std::abs(p);
636  if(p > 1e-1){
637  order2 = false;
638  }
639  }
640  result.UpdateResult("ParallelMidPointQuadrature:Order2",order2);
641  }
642 
648  virtual void Process(ResultsType &result){
649  Prologue();
650  Test__ParallelTrapezoidQuadrature(result);
651  Test__ParallelMidPointQuadrature(result);
652  Epilogue();
653  }
654 
661  virtual void RunTest(const std::string &name,ResultsType &result)
662  {
663  if(name == "ParallelTrapezoidQuadrature")
664  Test__ParallelTrapezoidQuadrature(result);
665  if(name == "ParallelMidPointQuadrature")
666  Test__ParallelMidPointQuadrature(result);
667  }
668 
675  virtual void ProcessTests(std::list<std::string> &test_names,ResultsType &result){
676  Prologue();
677  std::list<std::string>::iterator tni = test_names.begin();
678  while(tni != test_names.end())
679  RunTest(*tni++,result);
680  Epilogue();
681  }
682 
683  };
684 #endif
685 };
686 #endif
double TrapezoidQuadrature(double(*f)(double), double x0, double xn, int n)
Integrates f with composite trapezoid rule.
double F1(double x)
Simple test function fixture returns .
Definition: RocstarTest.H:114
IRAD::Comm::CommunicatorObject CommType
Convenience typedef for CommunicatorObject.
std::vector< int > N
A set of values for testing quadrature methods.
Definition: RocstarTest.H:86
double F1(double x)
Simple test function fixture returns .
Definition: RocstarTest.H:34
virtual void Process(ResultsType &result)
Runs all tests implemented by the Rocstar::TestingObject.
Definition: RocstarTest.H:255
std::string ExampleFunction(const std::string &instring)
Example function for GridConversion (this is a brief description).
double F2(double x)
Simple test function fixture returns .
Definition: RocstarTest.H:40
CImg< _cimg_Tfloat > log(const CImg< T > &instance)
Definition: CImg.h:6021
Project-specific testing object.
Definition: RocstarTest.H:71
virtual void RunTest(const std::string &name, ResultsType &result)
Runs a test specified by name.
Definition: RocstarTest.H:269
virtual void Test__MidPointQuadrature(ResultsType &result)
Test for Rocstar::MidPointQuadrature.
Definition: RocstarTest.H:207
virtual void Epilogue()
Tears down the testing fixtures if needed.
Definition: RocstarTest.H:96
virtual void Test__ExampleFunction(ResultsType &result)
Test for Rocstar::ExampleFunction.
Definition: RocstarTest.H:130
virtual void ProcessTests(std::list< std::string > &test_names, ResultsType &result)
Runs a list of tests specified by name.
Definition: RocstarTest.H:287
IRAD::Util::TestResults TestResults
Project-specific test results type.
blockLoc i
Definition: read.cpp:79
double MidPointQuadrature(double(*f)(double), double x0, double xn, int n)
Integrates f with composite midpoint rule.
void int int REAL * x
Definition: read.cpp:74
const NT & n
IRAD::Util::TestingObject< ResultsType > TestingObjectBaseType
Definition: RocstarTest.H:73
virtual void Prologue()
Sets up the data fixtures for the tests.
Definition: RocstarTest.H:105
double F2(double x)
Simple test function fixture returns .
Definition: RocstarTest.H:120
Example C++ header file for GridConversion.
TestingObject()
Default constructor.
Definition: RocstarTest.H:91
MPI_Comm GetCommunicator()
Definition: GetComm.C:6
virtual void Test__TrapezoidQuadrature(ResultsType &result)
Test for Rocstar::TrapezoidQuadrature.
Definition: RocstarTest.H:155
j indices j
Definition: Indexing.h:6
NT abs(const NT &x)
Definition: number_utils.h:130
static int rank
Definition: advectest.C:66
std::string ExampleTestFixture
A sample string test fixture.
Definition: RocstarTest.H:82
IRAD::Util::TestResults TestResults
Project-specific test results type.
Definition: RocstarTest.H:41