Skip to content

Commit

Permalink
fleshed out launcher header
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Jan 8, 2024
1 parent a19326d commit e459ffd
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 77 deletions.
4 changes: 2 additions & 2 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Pos=536,59
Size=550,526

[Window][Welcome to the Tsunami Simulator GUI!]
Pos=118,58
Size=360,180
Pos=646,135
Size=360,377

[Window][Simulation parameters]
Pos=60,60
Expand Down
107 changes: 55 additions & 52 deletions src/Launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @author Luca-Philipp Grumbach
* @author Richard Hofmann
*
* # Description
* Entry-point for simulations.
* # Description
* Class that launches and controls the simulation.
**/

#include "Launcher.h"
Expand All @@ -14,10 +14,11 @@
#include <cmath>
#include <fstream>
#include <limits>
#include <chrono>

#ifndef BENCHMARK
#include <filesystem>
#endif
#include <chrono>

// external libraries
#ifdef USEOMP
Expand Down Expand Up @@ -99,26 +100,14 @@ tsunami_lab::t_real l_scalingY;
//-------------END OF VARIABLES-------------//
//------------------------------------------//

/**
* Determines if a string ends with another string.
*
* @param i_str input string to check
* @param i_suffix possible suffix of i_str
* @return true if i_str ends with i_suffix, otherwise false.
*/
bool endsWith(std::string const &i_str, std::string const &i_suffix)
bool tsunami_lab::Launcher::endsWith(std::string const &i_str, std::string const &i_suffix)
{
if (i_str.length() < i_suffix.length()) return false;
if (i_str.length() < i_suffix.length())
return false;
return i_str.compare(i_str.length() - i_suffix.length(), i_suffix.length(), i_suffix) == 0;
}

#ifndef BENCHMARK
/**
* Sets up the required folder structure.
*
* @return void
*/
void setupFolders()
void tsunami_lab::Launcher::setupFolders()
{
// create solutions folder
if (!std::filesystem::exists("solutions"))
Expand All @@ -131,9 +120,13 @@ void setupFolders()
if (!std::filesystem::exists("checkpoints"))
std::filesystem::create_directory("checkpoints");
}
#endif

void configureFiles()
void tsunami_lab::Launcher::loadConfigDataFromFile(std::string i_configFilePath){
std::ifstream l_configFile(i_configFilePath);
l_configData = json::parse(l_configFile);
}

void tsunami_lab::Launcher::configureFiles()
{
l_outputFileName = l_configData.value("outputFileName", "solution");
l_netCdfOutputPathString = "solutions/" + l_outputFileName + ".nc";
Expand Down Expand Up @@ -161,7 +154,7 @@ void configureFiles()
}
}

void loadConfiguration()
void tsunami_lab::Launcher::loadConfiguration()
{
l_solver = l_configData.value("solver", "fwave");
// read size config
Expand Down Expand Up @@ -219,7 +212,7 @@ void loadConfiguration()
#endif
}

void constructSetup()
void tsunami_lab::Launcher::constructSetup()
{
if (l_setupChoice == "GENERALDISCONTINUITY1D")
{
Expand Down Expand Up @@ -327,7 +320,7 @@ void constructSetup()
}
}

void setUpNetCdf()
void tsunami_lab::Launcher::setUpNetCdf()
{
if (l_setupChoice == "CHECKPOINT")
{
Expand Down Expand Up @@ -369,7 +362,7 @@ void setUpNetCdf()
}
}

void constructSolver()
void tsunami_lab::Launcher::constructSolver()
{
l_dx = l_simulationSizeX / l_nx;
l_dy = l_simulationSizeY / l_ny;
Expand Down Expand Up @@ -403,7 +396,9 @@ void constructSolver()
l_netCdf->read(l_checkPointFilePath, "momentumY", &l_hvCheck);
l_netCdf->read(l_checkPointFilePath, "bathymetry", &l_bCheck);

#ifdef USEOMP
#pragma omp parallel for
#endif
for (tsunami_lab::t_idx l_cy = 0; l_cy < l_ny; l_cy++)
{
for (tsunami_lab::t_idx l_cx = 0; l_cx < l_nx; l_cx++)
Expand Down Expand Up @@ -436,7 +431,9 @@ void constructSolver()

if (l_setupChoice != "CHECKPOINT")
{
#ifdef USEOMP
#pragma omp parallel for
#endif
for (tsunami_lab::t_idx l_cy = 0; l_cy < l_ny; l_cy++)
{
tsunami_lab::t_real l_y = l_cy * l_dy + l_offsetY;
Expand Down Expand Up @@ -475,7 +472,7 @@ void constructSolver()
}
}

void loadBathymetry(std::string *i_file)
void tsunami_lab::Launcher::loadBathymetry(std::string *i_file)
{
// load bathymetry from file
if (l_bathymetryFilePath.length() > 0)
Expand Down Expand Up @@ -508,7 +505,7 @@ void loadBathymetry(std::string *i_file)
}
}

void loadStations()
void tsunami_lab::Launcher::loadStations()
{
// set up stations
if (l_configData.contains("stations"))
Expand All @@ -534,9 +531,9 @@ void loadStations()
}
}

void addStation(tsunami_lab::t_real i_locationX,
tsunami_lab::t_real i_locationY,
std::string i_stationName)
void tsunami_lab::Launcher::addStation(tsunami_lab::t_real i_locationX,
tsunami_lab::t_real i_locationY,
std::string i_stationName)
{
// location cell
tsunami_lab::t_idx l_cx = (i_locationX - l_offsetX) / l_dx;
Expand All @@ -548,7 +545,7 @@ void addStation(tsunami_lab::t_real i_locationX,
l_waveProp));
}

void deriveTimeStep()
void tsunami_lab::Launcher::deriveTimeStep()
{
// derive maximum wave speed in setup; the momentum is ignored
tsunami_lab::t_real l_speedMax = std::sqrt(9.81 * l_hMax);
Expand Down Expand Up @@ -588,11 +585,9 @@ void deriveTimeStep()
#endif
}

void runCalculation()
void tsunami_lab::Launcher::runCalculation()
{
#ifndef BENCHMARK
auto l_lastWrite = std::chrono::system_clock::now();
#endif
while (l_simTime < l_endTime)
{
//------------------------------------------//
Expand Down Expand Up @@ -674,7 +669,24 @@ void runCalculation()
}
}

int tsunami_lab::Launcher::start()
void tsunami_lab::Launcher::freeMemory()
{
delete l_setup;
delete l_waveProp;
#ifndef BENCHMARK
std::filesystem::remove(l_checkPointFilePathString);
delete l_netCdf;
for (tsunami_lab::io::Station *l_s : l_stations)
{
delete l_s;
}
#endif
}

//-------------------------------------------//
//----------------ENTRY POINT----------------//
//-------------------------------------------//
int tsunami_lab::Launcher::start(std::string i_config)
{
std::cout << "####################################" << std::endl;
std::cout << "### Tsunami Lab ###" << std::endl;
Expand All @@ -686,8 +698,11 @@ int tsunami_lab::Launcher::start()
std::cout << "### ###" << std::endl;
std::cout << "####################################" << std::endl;

// l_configFilePath = config;
// std::cout << "runtime configuration file: " << l_configFilePath << std::endl;
if (i_config != "")
{
l_configFilePath = i_config;
std::cout << "runtime configuration file: " << l_configFilePath << std::endl;
}

//-------------------------------------------//
//--------------File I/O Config--------------//
Expand All @@ -697,10 +712,7 @@ int tsunami_lab::Launcher::start()
#ifndef BENCHMARK
setupFolders();
#endif

// read configuration data from file
std::ifstream l_configFile(l_configFilePath);
l_configData = json::parse(l_configFile);
loadConfigDataFromFile(l_configFilePath);
#ifndef BENCHMARK
configureFiles();
#else
Expand Down Expand Up @@ -728,6 +740,7 @@ int tsunami_lab::Launcher::start()
runCalculation();
std::cout << "finished time loop" << std::endl;

// write station data to files
#ifndef BENCHMARK
for (tsunami_lab::io::Station *l_s : l_stations)
{
Expand All @@ -736,17 +749,7 @@ int tsunami_lab::Launcher::start()
#endif

// free memory
std::cout << "freeing memory" << std::endl;
delete l_setup;
delete l_waveProp;
#ifndef BENCHMARK
std::filesystem::remove(l_checkPointFilePathString);
delete l_netCdf;
for (tsunami_lab::io::Station *l_s : l_stations)
{
delete l_s;
}
#endif
freeMemory();
std::cout << "finished, exiting" << std::endl;
return EXIT_SUCCESS;
}
118 changes: 117 additions & 1 deletion src/Launcher.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @author Luca-Philipp Grumbach
* @author Richard Hofmann
*
* # Description
* Class that launches and controls the simulation.
**/

#ifndef TSUNAMI_LAB_LAUNCHER_H
#define TSUNAMI_LAB_LAUNCHER_H

Expand Down Expand Up @@ -38,8 +46,116 @@ namespace tsunami_lab

class tsunami_lab::Launcher
{
private:
/**
* Determines if a string ends with another string.
*
* @param i_str input string to check
* @param i_suffix possible suffix of i_str
* @return true if i_str ends with i_suffix, otherwise false.
*/
bool endsWith(std::string const &i_str, std::string const &i_suffix);

/**
* Helper method that sets up the required folder structure.
*
* @return void
*/
void setupFolders();

/**
* Helper method that sets up the required file structure and looks for a checkpoint file.
*
* @return void
*/
void configureFiles();

/**
* Helper method that loads configuration data from a config file.
*
* @return void
*/
void loadConfiguration();

/**
* Helper method that constructs a setup from the active setup choice.
*
* @return void
*/
void constructSetup();

/**
* Helper method that sets up the netcdf I/O.
* @return void
*/
void setUpNetCdf();

/**
* Helper method that constructs the solver.
*
* @return void
*/
void constructSolver();

/**
* Helper method that loads bathymetry from a .csv file into the wave propagation patch.
*
* @param i_file path of the .csv bathymetry file
* @return void
*/
void loadBathymetry(std::string *i_file);

/**
* Helper method that sets up the stations from the config data.
*
* @return void
*/
void loadStations();

/**
* Helper method for the derivation of a time step.
*
* @return void
*/
void deriveTimeStep();

/**
* Helper method that frees the allocated memory.
*
* @return void
*/
void freeMemory();

public:
int start();
/**
* Loads the config data from a file
*
* @param i_configFilePath path of the config file
* @return void
*/
void loadConfigDataFromFile(std::string i_configFilePath);

/**
* Sets up a station.
*
* @param i_locationX location of the station in x-direction
* @param i_locationX location of the station in y-direction
* @param i_stationName name of the station
* @return void
*/
void addStation(tsunami_lab::t_real i_locationX,
tsunami_lab::t_real i_locationY,
std::string i_stationName);

/**
* Starts the calculation with the set parameters.
*
* @param i_config path to the config file
* @return void
*/
void runCalculation();

int start(std::string i_config);
};

#endif
Loading

0 comments on commit e459ffd

Please sign in to comment.