Commit c0149dca authored by Lukas Riedel's avatar Lukas Riedel

Add initialization methods for convenience

* 'Dorie::Setup::init' creates the ParameterTree, the base logger,
    and the MPIHelper and returns them as a tuple
* 'Dorie::Setup::debug_hook' halts the program for attaching a
    debugger
* Adapt executables to new functions
* Rename `common/setup_inifile.hh` to `common/setup.hh`
* Convert indentation to spaces in `common/setup.hh`
parent cc0cea09
#ifndef DUNE_DORIE_COMMON_SETUP_HH
#define DUNE_DORIE_COMMON_SETUP_HH
#include <iostream>
#include <memory>
#include <vector>
#include <tuple>
#include <thread>
#include <chrono>
#include <dune/common/exceptions.hh>
#include <dune/common/parametertree.hh>
#include <dune/common/parametertreeparser.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/dorie/common/logging.hh>
namespace Dune{
namespace Dorie{
/**
* @brief Helper namespace to configurate/setup input files
*/
namespace Setup
{
/// Initialize the base logger and config file
/** Create the Dune MPI Helper, read the parameter file tree, and
* initialize the base logger. Apply log level at primary process
* after displaying greetings and name of config file loaded.
*
* \param argc Number of arguments of the main program
* \param argv Argument character array of the main program
* \param greetings Initial message of the logger (log-level `info`,
* will *always* be displayed)
*/
auto init (int& argc,
char**& argv,
const std::string greetings="Initializing program")
-> std::tuple<Dune::ParameterTree,
std::shared_ptr<spdlog::logger>,
Dune::MPIHelper&>
{
// initialize the helper
auto& helper = Dune::MPIHelper::instance(argc, argv);
// Initialize global logger
auto log = Dune::Dorie::create_base_logger(helper,
spdlog::level::info);
log->info(greetings);
// read arguments
if (argc!=2) {
log->error("This routine expects the config file "
"as single argument!");
DUNE_THROW(Dune::IOError, "Wrong number of arguments");
}
const std::string inifilename = argv[1];
// Read ini file
log->info("Reading parameter file: {}", inifilename);
Dune::ParameterTree inifile;
Dune::ParameterTreeParser ptreeparser;
ptreeparser.readINITree(inifilename, inifile);
// set appropriate log level
const auto log_level = inifile.get<std::string>("output.logLevel");
if (helper.rank() == 0) {
log->set_level(spdlog::level::from_str(log_level));
}
if (helper.size() > 1) {
log->debug("Running in parallel on {} nodes",
helper.size());
}
return std::tuple<Dune::ParameterTree,
std::shared_ptr<spdlog::logger>,
Dune::MPIHelper&>
(inifile, log, helper);
}
/// Halt the process for a debugger.
/** Stops the current process for attaching a debugger.
* This is useful when running multiple process in parallel, which
* requires one debugger to attach to every process. After attaching,
* set the integer `i` to a value other than zero (`0`), to continue.
*
* The respective process ID is displayed in the log messages.
*/
void debug_hook ()
{
// cache log level
const auto log = get_logger(log_base);
const auto _log_level_cache = log->level();
log->set_level(spdlog::level::info);
// inform the user
log->warn("Process halted due to debug mode");
log->warn("Attach a debugger and "
"set variable i to a value != 0");
log->flush();
// actual sleeping
int i = 0;
using namespace std::chrono_literals;
while (i == 0) {
std::this_thread::sleep_for(50ms);
}
// we continue
log->info("Commencing ...");
log->set_level(_log_level_cache);
}
/*------------------------------------------------------------------------*//**
* @brief Set up the configuration keys for Richards simulations
*
* @param ini The inifile
*/
Dune::ParameterTree prep_ini_for_richards(const Dune::ParameterTree& ini)
{
const auto log = get_logger(log_base);
Dune::ParameterTree richards_ini(ini.sub("richards"));
// move grid extensions into richards category (needed for reading parameters)
richards_ini["grid.extensions"] = ini["grid.extensions"];
const std::string adapt_policy_str = ini.get<std::string>("adaptivity.policy");
// copy adaptivity keywords into richards category
if (adapt_policy_str == "waterFlux") {
richards_ini.sub("adaptivity") = ini.sub("adaptivity");
} else if (adapt_policy_str != "none") {
log->error("Unknown adaptivity policy: {}", adapt_policy_str);
DUNE_THROW(NotImplemented, "Unknown adaptativity policy");
}
return richards_ini;
}
};
} // namespace Dorie
} // namespace Dune
#endif // DUNE_DORIE_COMMON_SETUP_HH
#include <iostream>
#include <memory>
#include <vector>
#include <dune/common/exceptions.hh>
#include <dune/common/parametertree.hh>
namespace Dune{
namespace Dorie{
/**
* @brief Helper namespace to configurate/setup input files
*/
namespace Setup
{
/*------------------------------------------------------------------------*//**
* @brief Set up the configuration keys for Richards simulations
*
* @param ini The inifile
*/
Dune::ParameterTree prep_ini_for_richards(const Dune::ParameterTree& ini)
{
Dune::ParameterTree richards_ini(ini.sub("richards"));
// move grid extensions into richards category (needed for reading parameters)
richards_ini["grid.extensions"] = ini["grid.extensions"];
const std::string adapt_policy_str = ini.get<std::string>("adaptivity.policy");
// copy adaptivity keywords into richards category
if (adapt_policy_str == "waterFlux") {
richards_ini.sub("adaptivity") = ini.sub("adaptivity");
} else if (adapt_policy_str != "none") {
DUNE_THROW(NotImplemented, "Not known adaptativity policy ("
<< ini.get<std::string>("adaptivity.policy") << ")!");
}
return richards_ini;
}
};
}
}
\ No newline at end of file
......@@ -20,9 +20,8 @@
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/uggrid.hh>
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/setup.hh>
#include <dune/dorie/common/grid_creator.hh>
#include <dune/dorie/common/setup_inifile.hh>
#include <dune/dorie/model/richards/richards.hh>
......@@ -76,60 +75,13 @@ int main(int argc, char** argv)
try{
Dune::Timer timer;
//Initialize Mpi
Dune::MPIHelper& helper = Dune::MPIHelper::instance(argc, argv);
// Initialize all the stuff!
const std::string grt = "Starting DORiE";
auto [inifile, log, helper] = Dune::Dorie::Setup::init(argc, argv, grt);
// Initialize global logger
const auto log = Dune::Dorie::create_base_logger(helper,
spdlog::level::info);
log->info("Starting DORiE");
if (argc!=2) {
log->error("This routine expects the config file "
"as single argument!");
DUNE_THROW(Dune::IOError, "Wrong number of arguments");
}
const std::string inifilename = argv[1];
// Read ini file
log->info("Reading parameter file: {}", inifilename);
Dune::ParameterTree inifile;
Dune::ParameterTreeParser ptreeparser;
ptreeparser.readINITree(inifilename,inifile);
const auto log_level = inifile.get<std::string>("output.logLevel");
if (helper.rank() == 0) {
log->set_level(spdlog::level::from_str(log_level));
}
if (helper.size() > 1) {
log->debug("Running in parallel on {} nodes",
helper.size());
}
// Allow for a debugger as gdb or lldb to hook into the process, even if run in parallel
// (by manually setting the variable i to a nonzero value)
bool debugMode = inifile.get<bool>("misc.debugMode");
if (debugMode)
{
// cache log level
const auto _log_level_cache = log->level();
log->set_level(spdlog::level::info);
if (helper.rank() == 0) {
log->warn("Application halted due to debug mode");
}
log->warn("Attach a debugger and "
"set variable i to a value != 0");
log->flush();
// actual sleeping
int i = 0;
while (0 == i) {
sleep(5);
}
log->info("Commencing ...");
log->set_level(_log_level_cache);
// halt process for debugger
if (inifile.get<bool>("misc.debugMode")) {
Dune::Dorie::Setup::debug_hook();
}
// setup richards configuration
......
......@@ -14,41 +14,14 @@
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/uggrid.hh>
#include <dune/dorie/common/setup.hh>
#include <dune/dorie/test/test-grid-creation.hh>
int main(int argc, char** argv)
{
try{
//Initialize Mpi
Dune::MPIHelper& helper = Dune::MPIHelper::instance(argc, argv);
if (argc < 2)
DUNE_THROW(Dune::IOError,"No parameter file specified!");
const std::string inifilename = argv[1];
// Initialize global logger
const auto log = Dune::Dorie::create_base_logger(helper,
spdlog::level::debug);
// Read ini file
Dune::ParameterTree inifile;
Dune::ParameterTreeParser ptreeparser;
ptreeparser.readINITree(inifilename, inifile);
// maybe attach debugger
if (helper.size() > 1 && inifile.get<bool>("misc.debugMode", false))
{
int i = 0;
log->warn("Application halted due to debug mode. Attach now.");
while (0 == i)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
log->info("Commencing...");
}
const auto log_level = inifile.get<std::string>("output.logLevel");
if (helper.rank() == 0) {
log->set_level(spdlog::level::from_str(log_level));
}
// Initialize ALL the things!
auto [inifile, log, helper] = Dune::Dorie::Setup::init(argc, argv);
const auto dimensions = inifile.get<int>("grid.dimensions");
if (dimensions == 2) {
......
......@@ -14,8 +14,7 @@
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/uggrid.hh>
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/setup_inifile.hh>
#include <dune/dorie/common/setup.hh>
#include <dune/dorie/common/grid_creator.hh>
#include <dune/dorie/common/util.hh>
#include <dune/dorie/test/test-parameterization.hh>
......@@ -44,26 +43,8 @@ using CubeAdaptive = Dune::Dorie::BaseTraits<Dune::UGGrid<dim>,
int main (int argc, char** argv)
{
try{
// initialize MPI if needed
auto& helper = Dune::MPIHelper::instance(argc, argv);
// Initialize global logger
const auto log = Dune::Dorie::create_base_logger(helper,
spdlog::level::debug);
// Read ini file
if (argc!=2)
DUNE_THROW(Dune::IOError,
"Call this program with arguments <config>");
const std::string inifilename = argv[1];
Dune::ParameterTree inifile;
Dune::ParameterTreeParser ptreeparser;
ptreeparser.readINITree(inifilename,inifile);
const auto log_level = inifile.get<std::string>("output.logLevel");
if (helper.rank() == 0) {
log->set_level(spdlog::level::from_str(log_level));
}
// Initialize ALL the things!
auto [inifile, log, helper] = Dune::Dorie::Setup::init(argc, argv);
// setup richards configuration
Dune::ParameterTree richards_config = Dune::Dorie::Setup::prep_ini_for_richards(inifile);
......
......@@ -46,7 +46,7 @@ public:
void write_data() const override
{
std::cout << "current time: " << _current_time << std::endl;
this->_log->info("Current time: {}", _current_time);
_data_was_written = true;
}
......
......@@ -17,8 +17,7 @@
#include <dune/grid/uggrid.hh>
#include <dune/dorie/common/grid_creator.hh>
#include <dune/dorie/common/setup_inifile.hh>
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/setup.hh>
template<int dim, int order>
using Simplex = Dune::Dorie::RichardsSimulationTraits<Dune::Dorie::BaseTraits<Dune::UGGrid<dim>,
......@@ -36,26 +35,8 @@ using CubeAdaptive = Dune::Dorie::RichardsSimulationTraits<Dune::Dorie::BaseTrai
int main (int argc, char** argv)
{
try{
// initialize MPI if needed
auto& helper = Dune::MPIHelper::instance(argc, argv);
// Initialize global logger
const auto log = Dune::Dorie::create_base_logger(helper,
spdlog::level::debug);
// Read ini file
if (argc!=2)
DUNE_THROW(Dune::IOError,
"Call this program with arguments <config>");
const std::string inifilename = argv[1];
Dune::ParameterTree inifile;
Dune::ParameterTreeParser ptreeparser;
ptreeparser.readINITree(inifilename,inifile);
const auto log_level = inifile.get<std::string>("output.logLevel");
if (helper.rank() == 0) {
log->set_level(spdlog::level::from_str(log_level));
}
// Initialize ALL the things!
auto [inifile, log, helper] = Dune::Dorie::Setup::init(argc, argv);
// setup richards configuration
Dune::ParameterTree richards_config = Dune::Dorie::Setup::prep_ini_for_richards(inifile);
......
......@@ -16,6 +16,7 @@
#include <dune/common/parametertreeparser.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/dorie/common/setup.hh>
#include <dune/dorie/common/grid_creator.hh>
#include <dune/dorie/model/transport/transport.hh>
#include <dune/dorie/model/transport/transport.cc>
......@@ -48,19 +49,10 @@ int main(int argc, char** argv) {
try{
Dune::Timer timer;
//Initialize Mpi
Dune::MPIHelper& helper = Dune::MPIHelper::instance(argc, argv);
// Initialize ALL the things!
auto [inifile, log, helper] = Dune::Dorie::Setup::init(argc, argv);
// Read ini file
if (argc!=2)
DUNE_THROW(Dune::IOError,
"Call this program with arguments <config>");
const std::string inifilename = argv[1];
Dune::ParameterTree inifile, inifile_transport;
Dune::ParameterTreeParser ptreeparser;
ptreeparser.readINITree(inifilename,inifile);
inifile_transport = inifile.sub("transport");
auto inifile_transport = inifile.sub("transport");
// move grid extensions into richards category (needed for reading parameters)
inifile_transport["grid.extensions"] = inifile["grid.extensions"];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment