Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Public Member Functions | Protected Types | Protected Attributes | Private Member Functions | List of all members
Utopia::PseudoParent< RNG > Class Template Reference

A class to use at the top level of the model hierarchy as a mock parent. More...

#include <model.hh>

Collaboration diagram for Utopia::PseudoParent< RNG >:
Collaboration graph
[legend]

Public Member Functions

 PseudoParent (const std::string cfg_path)
 Constructor that only requires path to a config file.
 
 PseudoParent (const std::string cfg_path, const std::string output_path, const int seed=42, const std::string output_file_mode="w", const double emit_interval=5.)
 Constructor that allows granular control over config parameters.
 
Level get_level () const
 Return the hierarchical level within the model hierarchy.
 
std::string get_full_name () const
 Return the full name of the PseudoParent: an empty string.
 
Config get_cfg () const
 Return the config node of the Pseudo model, i.e. the root node.
 
std::shared_ptr< HDFFileget_hdffile () const
 Return a pointer to the HDF data file.
 
std::shared_ptr< HDFGroupget_hdfgrp () const
 Return a pointer to the HDF group, which is the base group of the file.
 
Time get_write_start () const
 Return the parameter that controls when write_data is called first.
 
Time get_write_every () const
 Return the parameter that controls how often write_data is called.
 
std::shared_ptr< RNG > get_rng () const
 Return a pointer to the RNG.
 
std::shared_ptr< spdlog::logger > get_logger () const
 Return a pointer to the logger of this model.
 
Time get_time_max () const
 The maximum time value as it can be found in the config.
 
std::shared_ptr< MonitorManagerget_monitor_manager () const
 Return the monitor manager of this model.
 
const Monitorget_monitor () const
 Return the monitor of this model.
 

Protected Types

using Config = Utopia::DataIO::Config
 
using HDFFile = Utopia::DataIO::HDFFile
 
using HDFGroup = Utopia::DataIO::HDFGroup
 
using MonitorManager = Utopia::DataIO::MonitorManager
 
using Monitor = Utopia::DataIO::Monitor
 
using Time = std::size_t
 
using Level = std::size_t
 

Protected Attributes

const Level _level
 The hierarchical level.
 
const Config _cfg
 The config node.
 
const std::shared_ptr< HDFFile_hdffile
 Pointer to the HDF5 file where data is written to.
 
const std::shared_ptr< RNG > _rng
 Pointer to a RNG that can be shared between models.
 
const std::shared_ptr< spdlog::logger > _log
 Pointer to the logger of this (pseudo) model.
 
const std::shared_ptr< MonitorManager_monitor_mgr
 The monitor manager.
 
Monitor _monitor
 The monitor instance of this root model.
 

Private Member Functions

void setup_loggers () const
 Set up the global loggers with levels specified in the config file.
 
void set_log_level () const
 Set the log level for the pseudo parent from the base_cfg.
 

Detailed Description

template<typename RNG = DefaultRNG>
class Utopia::PseudoParent< RNG >

A class to use at the top level of the model hierarchy as a mock parent.

It is especially useful when initializing a top-level model as the the model constructor that expects a Model-class-like object can be used. This class also takes care to load and hold a configuration file, to create a HDFFile for output, and to initialize a shared RNG. A template parameter exists that allows customization of the RNG class.

Template Parameters
RNGThe RNG type to use

Member Typedef Documentation

◆ Config

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::Config = Utopia::DataIO::Config
protected

◆ HDFFile

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::HDFFile = Utopia::DataIO::HDFFile
protected

◆ HDFGroup

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::HDFGroup = Utopia::DataIO::HDFGroup
protected

◆ Level

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::Level = std::size_t
protected

◆ Monitor

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::Monitor = Utopia::DataIO::Monitor
protected

◆ MonitorManager

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::MonitorManager = Utopia::DataIO::MonitorManager
protected

◆ Time

template<typename RNG = DefaultRNG>
using Utopia::PseudoParent< RNG >::Time = std::size_t
protected

Constructor & Destructor Documentation

◆ PseudoParent() [1/2]

template<typename RNG = DefaultRNG>
Utopia::PseudoParent< RNG >::PseudoParent ( const std::string  cfg_path)
inline

Constructor that only requires path to a config file.

From the config file, all necessary information is extracted, i.e.: the path to the output file ('output_path') and the seed of the shared RNG ('seed'). These keys have to be located at the top level of the configuration file.

Parameters
cfg_pathThe path to the YAML-formatted configuration file
1064 :
1065 // The hierarchical level is 0
1066 _level(0),
1067 // Initialize the config node from the path to the config file
1068 _cfg(YAML::LoadFile(cfg_path)),
1069 // Create a file at the specified output path and store the shared pointer
1070 _hdffile(std::make_shared<HDFFile>(
1071 get_as<std::string>("output_path", _cfg),
1072 get_as<std::string>("output_file_mode", _cfg, "w")
1073 )),
1074 // Initialize the RNG from a seed
1075 _rng(std::make_shared<RNG>(get_as<int>("seed", _cfg))),
1076 // And initialize the root logger at warning level
1077 _log(Utopia::init_logger("root", spdlog::level::warn, false)),
1078 // Create a monitor manager and a root monitor
1079 _monitor_mgr(std::make_shared<MonitorManager>(
1080 get_as<double>("monitor_emit_interval", _cfg))
1081 ),
1083 {
1084 setup_loggers(); // global loggers
1085 set_log_level(); // this log level
1087
1088 _log->info("Initialized PseudoParent from config file");
1089 _log->debug("cfg_path: {}", cfg_path);
1090 _log->debug("output_path: {}", get_as<std::string>("output_path",
1091 _cfg));
1092 _log->debug("RNG seed: {}", get_as<int>("seed", _cfg));
1093 _log->debug("emit_interval: {}s",
1094 get_as<double>("monitor_emit_interval", _cfg));
1095 }
static void init(const DataIO::Config &cfg)
Initialize parallel features based on configuration setting.
Definition parallel.hh:113
Monitor _monitor
The monitor instance of this root model.
Definition model.hh:1052
void set_log_level() const
Set the log level for the pseudo parent from the base_cfg.
Definition model.hh:1233
const Config _cfg
The config node.
Definition model.hh:1034
const std::shared_ptr< RNG > _rng
Pointer to a RNG that can be shared between models.
Definition model.hh:1040
const std::shared_ptr< spdlog::logger > _log
Pointer to the logger of this (pseudo) model.
Definition model.hh:1046
const std::shared_ptr< MonitorManager > _monitor_mgr
The monitor manager.
Definition model.hh:1049
const Level _level
The hierarchical level.
Definition model.hh:1031
void setup_loggers() const
Set up the global loggers with levels specified in the config file.
Definition model.hh:1217
const std::shared_ptr< HDFFile > _hdffile
Pointer to the HDF5 file where data is written to.
Definition model.hh:1037
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
std::shared_ptr< spdlog::logger > init_logger(const std::string name, const spdlog::level::level_enum level, const bool throw_on_exist=true)
Initialize a logger with a certain name and log level.
Definition logging.hh:31

◆ PseudoParent() [2/2]

template<typename RNG = DefaultRNG>
Utopia::PseudoParent< RNG >::PseudoParent ( const std::string  cfg_path,
const std::string  output_path,
const int  seed = 42,
const std::string  output_file_mode = "w",
const double  emit_interval = 5. 
)
inline

Constructor that allows granular control over config parameters.

Parameters
cfg_pathThe path to the YAML-formatted configuration file
output_pathWhere the HDF5 file is to be located
seedThe seed the RNG is initialized with (default: 42)
output_file_modeThe access mode of the HDF5 file (default: w)
emit_intervalThe monitor emit interval (in seconds)
1111 :
1112 // The hierarchical level is 0
1113 _level(0),
1114 // Initialize the config node from the path to the config file
1115 _cfg(YAML::LoadFile(cfg_path)),
1116 // Create a file at the specified output path
1117 _hdffile(std::make_shared<HDFFile>(output_path, output_file_mode)),
1118 // Initialize the RNG from a seed
1119 _rng(std::make_shared<RNG>(seed)),
1120 // And initialize the root logger at warning level
1121 _log(Utopia::init_logger("root", spdlog::level::warn, false)),
1122 // Create a monitor manager and a "root" monitor
1123 _monitor_mgr(std::make_shared<MonitorManager>(emit_interval)),
1125 {
1126 setup_loggers(); // global loggers
1127 set_log_level(); // this log level
1129
1130 _log->info("Initialized PseudoParent from parameters");
1131 _log->debug("cfg_path: {}", cfg_path);
1132 _log->debug("output_path: {} (mode: {})",
1134 _log->debug("RNG seed: {}", seed);
1135 _log->debug("emit_interval: {}", emit_interval);
1136 }

Member Function Documentation

◆ get_cfg()

template<typename RNG = DefaultRNG>
Config Utopia::PseudoParent< RNG >::get_cfg ( ) const
inline

Return the config node of the Pseudo model, i.e. the root node.

1153 {
1154 return _cfg;
1155 }

◆ get_full_name()

template<typename RNG = DefaultRNG>
std::string Utopia::PseudoParent< RNG >::get_full_name ( ) const
inline

Return the full name of the PseudoParent: an empty string.

1148 {
1149 return "";
1150 }

◆ get_hdffile()

template<typename RNG = DefaultRNG>
std::shared_ptr< HDFFile > Utopia::PseudoParent< RNG >::get_hdffile ( ) const
inline

Return a pointer to the HDF data file.

1158 {
1159 return _hdffile;
1160 }

◆ get_hdfgrp()

template<typename RNG = DefaultRNG>
std::shared_ptr< HDFGroup > Utopia::PseudoParent< RNG >::get_hdfgrp ( ) const
inline

Return a pointer to the HDF group, which is the base group of the file.

1163 {
1164 return _hdffile->get_basegroup();
1165 }

◆ get_level()

template<typename RNG = DefaultRNG>
Level Utopia::PseudoParent< RNG >::get_level ( ) const
inline

Return the hierarchical level within the model hierarchy.

1143 {
1144 return _level;
1145 }

◆ get_logger()

template<typename RNG = DefaultRNG>
std::shared_ptr< spdlog::logger > Utopia::PseudoParent< RNG >::get_logger ( ) const
inline

Return a pointer to the logger of this model.

1183 {
1184 return _log;
1185 }

◆ get_monitor()

template<typename RNG = DefaultRNG>
const Monitor & Utopia::PseudoParent< RNG >::get_monitor ( ) const
inline

Return the monitor of this model.

1201 {
1202 return _monitor;
1203 }

◆ get_monitor_manager()

template<typename RNG = DefaultRNG>
std::shared_ptr< MonitorManager > Utopia::PseudoParent< RNG >::get_monitor_manager ( ) const
inline

Return the monitor manager of this model.

1196 {
1197 return _monitor_mgr;
1198 }

◆ get_rng()

template<typename RNG = DefaultRNG>
std::shared_ptr< RNG > Utopia::PseudoParent< RNG >::get_rng ( ) const
inline

Return a pointer to the RNG.

1178 {
1179 return _rng;
1180 }

◆ get_time_max()

template<typename RNG = DefaultRNG>
Time Utopia::PseudoParent< RNG >::get_time_max ( ) const
inline

The maximum time value as it can be found in the config.

Currently, this reads the num_steps key, but this might be changed in the future to allow continuous time steps.

1191 {
1192 return get_as<Time>("num_steps", _cfg);
1193 }

◆ get_write_every()

template<typename RNG = DefaultRNG>
Time Utopia::PseudoParent< RNG >::get_write_every ( ) const
inline

Return the parameter that controls how often write_data is called.

1173 {
1174 return get_as<Time>("write_every", _cfg, 1);
1175 }

◆ get_write_start()

template<typename RNG = DefaultRNG>
Time Utopia::PseudoParent< RNG >::get_write_start ( ) const
inline

Return the parameter that controls when write_data is called first.

1168 {
1169 return get_as<Time>("write_start", _cfg, 0);
1170 }

◆ set_log_level()

template<typename RNG = DefaultRNG>
void Utopia::PseudoParent< RNG >::set_log_level ( ) const
inlineprivate

Set the log level for the pseudo parent from the base_cfg.

1233 {
1234 _log->set_level(
1235 spdlog::level::from_str(get_as<std::string>("model",
1236 _cfg["log_levels"]))
1237 );
1238 }

◆ setup_loggers()

template<typename RNG = DefaultRNG>
void Utopia::PseudoParent< RNG >::setup_loggers ( ) const
inlineprivate

Set up the global loggers with levels specified in the config file.

Extract the following keys from the _cfg member:

- ``log_levels.core``:      the Core module log level; required
- ``log_levels.data_io``:   the DataIO module log level; required
- ``log_levels.data_mngr``: the DataIO::DataManager log level;
                            optional, defaults to ``warn``
- ``log_pattern``:          the global log pattern
1217 {
1219 spdlog::level::from_str(
1220 get_as<std::string>("core", _cfg["log_levels"])
1221 ),
1222 spdlog::level::from_str(
1223 get_as<std::string>("data_io", _cfg["log_levels"])
1224 ),
1225 spdlog::level::from_str(
1226 get_as<std::string>("data_mngr", _cfg["log_levels"], "warn")
1227 ),
1228 get_as<std::string>("log_pattern", _cfg, "")
1229 );
1230 }
void setup_loggers(const spdlog::level::level_enum level_core=spdlog::level::warn, const spdlog::level::level_enum level_data_io=spdlog::level::warn, const spdlog::level::level_enum level_data_mngr=spdlog::level::warn, const std::string &log_pattern="")
Set up and register the global loggers and set the global log pattern.
Definition logging.hh:66

Member Data Documentation

◆ _cfg

template<typename RNG = DefaultRNG>
const Config Utopia::PseudoParent< RNG >::_cfg
protected

The config node.

◆ _hdffile

template<typename RNG = DefaultRNG>
const std::shared_ptr<HDFFile> Utopia::PseudoParent< RNG >::_hdffile
protected

Pointer to the HDF5 file where data is written to.

◆ _level

template<typename RNG = DefaultRNG>
const Level Utopia::PseudoParent< RNG >::_level
protected

The hierarchical level.

◆ _log

template<typename RNG = DefaultRNG>
const std::shared_ptr<spdlog::logger> Utopia::PseudoParent< RNG >::_log
protected

Pointer to the logger of this (pseudo) model.

Required for passing on the logging level if unspecified for the respective model

◆ _monitor

template<typename RNG = DefaultRNG>
Monitor Utopia::PseudoParent< RNG >::_monitor
protected

The monitor instance of this root model.

◆ _monitor_mgr

template<typename RNG = DefaultRNG>
const std::shared_ptr<MonitorManager> Utopia::PseudoParent< RNG >::_monitor_mgr
protected

The monitor manager.

◆ _rng

template<typename RNG = DefaultRNG>
const std::shared_ptr<RNG> Utopia::PseudoParent< RNG >::_rng
protected

Pointer to a RNG that can be shared between models.


The documentation for this class was generated from the following file: