void DamBreakSim::run()

{

   // Define the classes that are used to execute the simulation

   Sfx::PhySystem* w_phySystem=NULL;          // physics system

   Sfx::IPhysConfiguration* w_dbSetup=NULL;   // configure system

   Sfx::IPhysAlgorithm* w_phyAlgo=NULL;       // physics algorithm   

   Sfx::IPhyMeasurement* w_phyMeasure=NULL;   // physics measure

   Sfx::IFileDataStore* w_dataStore=NULL;     // data store

   Sfx::IFinalReport* w_finalReport=NULL;     // report simulation

 

   // ==============================================

   // Initialization phase (setup initial condition)

   // ==============================================

 

   // create the section flow for this simulation

   // Number of section correspond to the number of grid node

   // Section flow (unit width) for this simulation

   // with flat bottom (z=0) and frictionless

   std::set<Hyd::SectionFlow> w_setofSectFlow;

 

   // Dam-Break setting (Hudson report parameters as default)

   Swe::DamBreakSettings w_damBreakParams;

 

   // create the initial condition and set state variables

   Swe::DamBreakInitial w_damBreakInit(w_damBreakParams);

 

   // set dam-break init 

   w_damBreakInit.setIC(w_setofSectFlow);

 

   // sanity check (debugging purpose)

   assert( w_setofSectFlow.size()==101);

#if 1

   // debugging purpose (print parameters)

   std::cout << "Printing initial section flow information\n";

   std::for_each( w_setofSectFlow.begin(), w_setofSectFlow.end(),

                        std::mem_fun_ref(&Hyd::SectionFlow::print));

#endif

 

   // Return the supported provider interface

   Sfx::ILogger* w_pLog = SFX_GET_PROVIDER(ILogger);

   // sanity check

   if( w_pLog==NULL)

   {

      std::cerr << "Cannot get the provider interface\n";

   }

 

   // set the environment variable

   std::string w_resPath=m_resDir;

   std::string w_rootPath=Sfx::Directory::GetEnvVariablePath();

   w_rootPath+="\\";

   w_rootPath+="Test_SweCore01";

   w_rootPath+=w_resPath;

   std::string w_resFilename = "\\"+m_algoName+"_";

   w_resFilename+="FullResult.txt";

   w_rootPath+=w_resFilename;

 

           

   // ==============================================

   //    Now create the simulation environment

   //    that the user has defined

   // ==============================================

 

   try

   {

      // Create the physical system for this simulation

      std::cout << "Create the physical system\n";

      w_phySystem = new DamBreakSys();

      w_pLog->writeToLogFile( "Physical system created successfully");

 

      // Configure the objects in the physical system

      std::cout << "Configuring system" << "\n";

      w_dbSetup = new DBSetup(&w_setofSectFlow);

      w_dbSetup->configure(w_phySystem);

      w_pLog->writeToLogFile("System configured successfully");

 

      // Define the algorithm class to be used per

      // iteration on the physicalSystem

      std::cout << "Configuring Algorithm" << "\n";

      w_phyAlgo = new ValidateAlgo(&w_setofSectFlow);

      w_pLog->writeToLogFile("Physical algorithm created successfully");

                 

      // Define the measurement class to be used on the physical system

      std::cout << "Configuring Physical Measurement" << "\n";

      w_phyMeasure = new DbSysMeasurement();

      w_pLog->writeToLogFile("Physical measurement created successfully");

      w_phyMeasure->take(w_phySystem);

 

      // Configure the data store for usage based

      // on the type selected by the user

      std::cout << "Configuring the Persistent Data Source" << "\n";

      std::cout << "Create the data store" << "\n";

      w_dataStore = new DamBreakDataStore( w_rootPath);

      w_pLog->writeToLogFile( "Data Store created successfully");

      w_dataStore->open(); // open the data store for writing

      w_dataStore->save(w_phyMeasure);

 

      std::cout << "complete\n";

 

      // Configure the simulation report if there is one defined

      if ( SimulationMgr::getSingleton().doFinalReport())

      {

         std::cout << "Configuring the FinalReport" << "\n";

 

         w_finalReport = new FinalReport("GraphicsReport.txt");

         w_pLog->writeToLogFile("Final report created successfully");

         std::cout << "complete\n";

      }

    }

    catch ( std::bad_alloc& ba)

    {

      std::cerr << "Bad alloc caught: " << ba.what() << "\n";

    }

 

    

    std::cout << "Print simulation parameters of run\n";

    SimulationMgr::getSingleton().printParamOfRun();

    SimulationMgr::getSingleton().saveScenario();

    std::cout << "Ready to launch the simulation\n";

    std::cout << "Running simulation...\n";

 

    // Define references to simulation properties

    float64 timeStep = SimulationMgr::getSingleton().getTimeStep();

  

     bool performMeasurement =          SimulationMgr::getSingleton().getPerformMeasurement();

           

     int32 numberIterations = SimulationMgr::getSingleton().getNumIterationsMax();

 

     int32 iterationsPerMeasurement = SimulationMgr::getSingleton().getNumberMeasurements();

 

   // ==============================================

   //           Time loop (Run the simulation)

   // ==============================================

           

   for (int32 i = 0; i < numberIterations; i++)

   {

      // tracking simulation iteration

      SimulationMgr::getSingleton().incrIteration();

 

      // These calculations are always performed for every iteration

      w_phyAlgo->calculate(w_phySystem);

 

      // Update the time attribute of the simulation bean

      const float64 w_timeStep = SimulationMgr::getSingleton().getTimeStep();

 

      m_Tip.increaseTime( w_timeStep);

 

      // Current time of the simulation (deprecated)

      mTime = m_Tip.Time();

 

      // Exit if the simulation is over before the iterations are completed

      if (!SimulationMgr::getSingleton().isRunning())

      {

         try

         {

             w_dataStore->close();

         }

         catch (Sfx::ConnectionException& e)

         {

            throw new Sfx::ControllerException(e.what());

         }

 

         std::cout << "Complete";

 

         if ( SimulationMgr::getSingleton().doFinalReport())

         {

             w_finalReport->report(w_phySystem);

         }

       }

      // Test whether or not to take a user

      // defined measurement of the physical system

      if ((performMeasurement) && ((i % iterationsPerMeasurement) == 0))

      {

         // Take a user defined measurement

         w_phyMeasure->take(w_phySystem);

 

         // Save the measurement to the data store

         w_dataStore->save(w_phyMeasure);

      }

   }  // for loop (simulation iteration)

 

   // The simulation is complete so wrap it up.

   try

   {

      w_dataStore->close();

   }

   catch( Sfx::ConnectionException& e)

   {

      throw new Sfx::ControllerException( e.what());

   }

 

   std::cout << "complete\n";

 

   if (SimulationMgr::getSingleton().doFinalReport())

            w_finalReport->report(w_phySystem);

 

   // close logging file

   w_pLog->closeLogFile();

}