33 using boost::property_tree::ptree;
39 CoupledSim::CoupledSim()
40 : m_sim_step(0), m_sim_ODE_steps(0)
46 return *m_cd.m_parameters;
51 return m_project_name;
66 Timings timings = m_timings;
67 for (
const auto& simPair : m_simulators) {
68 timings.Merge(simPair.second->GetTimings());
75 for (
const auto &sim : simulators) {
76 if (m_simulators.count(sim->GetProjectName()) == 0) {
77 m_simulators[sim->GetProjectName()] = sim;
79 throw Exception(
"Subsimulations with the same name!");
83 const ptree& sim_pt = pt.get_child(
"vleaf2");
84 m_project_name = sim_pt.get<std::string>(
"project");
86 m_sim_step = sim_pt.get<
unsigned int>(
"sim_step", 0U);
87 m_sim_ODE_steps = sim_pt.get<
unsigned int>(
"sim_ODE_coupling_steps", 1U);
88 m_cd.m_parameters = make_shared<ptree>();
95 for (
const auto& simPair : m_simulators) {
96 status = status && simPair.second->IsAtTermination();
105 for (
const auto& simPair : m_simulators) {
106 status = status && simPair.second->IsStationary();
116 const auto& coupler_array_pt = parameters.get_child(
"coupler_array");
117 for (
const auto& pair : coupler_array_pt) {
119 const auto type = pair.second.get<std::string>(
"type");
120 const auto from = pair.second.get<std::string>(
"from");
121 const auto to = pair.second.get<std::string>(
"to");
124 throw Exception(
"Couldn't find coupler type: " + type);
126 auto fromIt = m_simulators.find(from);
127 if (fromIt == m_simulators.end()) {
128 throw Exception(
"Couldn't find 'from'-simulation used in coupler: " + from);
130 auto toIt = m_simulators.find(to);
131 if (toIt == m_simulators.end()) {
132 throw Exception(
"Couldn't find 'to'-simulation used in coupler: " + to);
134 auto coupler = factory.
Get(type)();
135 coupler->Initialize(pair.second.get_child(
"parameters"),
136 fromIt->second->GetCoreData(), toIt->second->GetCoreData());
137 m_couplers.push_back(coupler);
144 const int n = m_sim_ODE_steps;
145 const auto params = m_simulators.begin()->second->GetCoreData().m_parameters;
146 const double time_slice = params->get<
double>(
"model.time_step")/static_cast<double>(n);
149 vector<unique_ptr<TimeSlicer>> t_slicers;
150 for (
const auto& sim : m_simulators) {
151 t_slicers.push_back(sim.second->TimeSlicing());
155 vector<std::thread> sim_threads;
156 for (
int i = 0; i < n; ++i) {
157 for (
const auto& t_slicer : t_slicers) {
158 sim_threads.push_back(thread(
159 [&t_slicer, time_slice] () {t_slicer->Go(time_slice, SimPhase::_1);}
164 for (
auto& thread : sim_threads) {
170 Stopclock couplerChrono(
"couplers",
true);
171 for (
auto coupler : m_couplers) {
174 m_timings.Record(couplerChrono.
GetName(), couplerChrono.
Get());
178 for (
const auto& t_slicer : t_slicers) {
179 sim_threads.push_back(thread(
181 t_slicer->Go(0.0, SimPhase::_2);
182 t_slicer->IncrementStepCount();
186 for (
auto& thread : sim_threads) {
std::string GetRunDate() const
Core data used during model execution.
Namespace for miscellaneous utilities.
void Initialize(const boost::property_tree::ptree &pt, const std::vector< std::shared_ptr< Sim >> &simulators)
Initialize with full configuration (complete setup prior to first use).
void IncrementStepCount()
Increase the current time step counter by one.
std::string GetProjectName() const
std::string GetName() const
Return name of this stopwatch.
bool IsStationary() const
const boost::property_tree::ptree & GetParameters() const
Extremely simple Exception root class.
void Reinitialize(const boost::property_tree::ptree ¶meters)
Namespace for the core simulator.
bool IsAtTermination() const
bool IsValid(const std::string &name) const
Check whether function name is present.
A coupler factory map that is used to register names for these couplers.
Sim, the actual simulator.
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
FunctionType Get(const std::string &name) const
Return function for given name.
Provides wall-clock time stamp using the time call.
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Header file for Exception class.
Timings GetTimings() const
std::string ToString() const
Returns string with the time stamp after eliminating newline.
Interface for CouplerFactories.
Interface for CoupledSim.
Namespace for clock and timekeeping related classes.