41 #include <boost/optional.hpp>
42 #include <boost/property_tree/exceptions.hpp>
43 #include <boost/property_tree/xml_parser.hpp>
58 using boost::optional;
66 : m_is_stationary(false)
68 m_cd.m_coupled_sim_transfer_data = make_shared<CoreData::TransferMapType>();
69 m_cd.m_parameters = make_shared<ptree>();
70 m_cd.m_random_engine = make_shared<RandomEngine>();
71 m_cd.m_time_data = make_shared<TimeData>();
79 m_is_stationary = other.m_is_stationary;
80 m_project_name = other.m_project_name;
81 m_run_date = other.m_run_date;
82 m_timings = other.m_timings;
84 m_cd.m_coupled_sim_transfer_data = make_shared<CoreData::TransferMapType>();
86 m_cd.m_parameters = make_shared<ptree>(*other.m_cd.m_parameters);
87 m_cd.m_time_data = other.m_cd.m_time_data;
88 m_cd.m_random_engine = make_shared<RandomEngine>(*other.m_cd.m_random_engine);
99 return *m_cd.m_parameters;
104 return m_project_name;
114 return m_cd.m_time_data->m_sim_step;
119 return m_cd.m_time_data->m_sim_time;
133 re_state.put(
"type", re_info.type);
134 re_state.put(
"seed", re_info.seed);
135 re_state.put(
"state", re_info.state);
144 unsigned int i =
static_cast<unsigned int>(
GetSimTime());
146 <<
" ; Time: " << i <<
" -- " << Utils::ToColonString(seconds(i))
147 <<
" ; Cells: " << m_cd.m_mesh->GetCells().size();
158 ptree
const& sim_pt = root_pt.get_child(
"vleaf2");
161 m_project_name = sim_pt.get<
string>(
"project");
163 m_cd.m_time_data = std::make_shared<TimeData>();
166 m_cd.m_time_data->m_sim_step = sim_pt.get<
unsigned int>(
"sim_step", 0U);
167 m_cd.m_time_data->m_sim_time = sim_pt.get<
double>(
"sim_time");
174 m_cd.m_mesh = build_director.Build(sim_pt.get_child(
"mesh"));
177 RandomEngineInitialize(sim_pt);
187 m_cd.m_time_data = make_shared<TimeData>();
188 m_cd.m_time_data->m_sim_step = sim_state.
GetTimeStep();
189 m_cd.m_time_data->m_sim_time = sim_state.
GetTime();
196 m_cd.m_mesh = build_director.Build(sim_state.
GetMeshState());
199 RandomEngineInitialize(sim_state);
206 const unsigned int cell_count = m_cd.m_mesh->GetCells().size();
209 const double max_sim_time
210 = m_cd.m_parameters->get<
double>(
"termination.max_sim_time", numeric_limits<double>::max());
211 const unsigned int max_sim_step
212 = m_cd.m_parameters->get<
unsigned int>(
"termination.max_sim_steps", numeric_limits<unsigned int>::max());
213 const unsigned int max_cell_count
214 = m_cd.m_parameters->get<
unsigned int>(
"termination.max_cell_count", numeric_limits<unsigned int>::max());
215 const bool stationarity_check
216 = m_cd.m_parameters->get<
bool>(
"termination.stationarity_check");
218 return (sim_time >= max_sim_time) || (sim_step >= max_sim_step)
219 || (cell_count >= max_cell_count) || (stationarity_check && is_stationary);
224 return m_is_stationary;
227 bool Sim::RandomEngineInitialize(
const ptree& sim_pt)
248 const ptree& parameters = sim_pt.get_child(
"parameters");
249 const ptree& re_params_pt = parameters.get_child(
"random_engine");
252 optional<const ptree&> re_state = sim_pt.get_child_optional(
"random_engine_state");
255 status = m_cd.m_random_engine->Reinitialize(re_params_pt);
257 const auto params_type = re_params_pt.get<
string>(
"type");
258 optional<unsigned int> params_seed = re_params_pt.get_optional<
unsigned int>(
"seed");
259 const ptree& re_state_pt = re_state.get();
260 const auto state_type = re_state_pt.get<
string>(
"type");
261 const auto state_seed = re_state_pt.get<
unsigned int>(
"seed");
263 const bool types_match = (params_type == state_type);
264 const bool seeds_match = params_seed ? (params_seed.get() == state_seed) :
true;
266 if (types_match && seeds_match) {
267 status = m_cd.m_random_engine->Reinitialize(re_state_pt);
269 status = m_cd.m_random_engine->Reinitialize(re_params_pt);
276 bool Sim::RandomEngineInitialize(
const SimState& sim_state)
284 const ptree& re_params_pt = parameters.get_child(
"random_engine");
288 optional<string> re_state_type = re_state_pt.get_optional<
string>(
"type");
289 optional<unsigned int> re_state_seed = re_state_pt.get_optional<
unsigned int>(
"seed");
290 optional<string> re_state_state = re_state_pt.get_optional<
string>(
"state");
292 if (!re_state_type || !re_state_seed || !re_state_state) {
293 status = m_cd.m_random_engine->Reinitialize(re_params_pt);
295 const auto params_type = re_params_pt.get<
string>(
"type");
296 optional<unsigned int> params_seed = re_params_pt.get_optional<
unsigned int>(
"seed");
297 const bool types_match = (params_type == re_state_type.get());
298 const bool seeds_match = params_seed ? (params_seed.get() == re_state_seed.get()) :
true;
300 if (types_match && seeds_match) {
301 m_cd.m_random_engine->Reinitialize(re_state_pt);
303 status = m_cd.m_random_engine->Reinitialize(re_params_pt);
312 lock_guard<mutex> timestep_guard(m_timestep_mutex);
313 *m_cd.m_parameters = p;
321 void Sim::TimeSliceGo(
const std::lock_guard<std::mutex>&,
double time_slice, SimPhase phase)
324 assert((m_time_evolver !=
nullptr) && (
"TimeEvolver factory produces nullptr."));
325 assert((m_cd.
Check()) &&
"CoreData do no check out!");
329 const auto tup = m_time_evolver(time_slice, phase);
330 m_timings.Merge(get<0>(tup));
331 m_is_stationary = get<1>(tup);
337 void Sim::TimeSliceSetup(
const std::lock_guard<std::mutex>&)
340 assert((m_cd.
Check()) &&
"CoreData do no check out!");
344 m_time_evolver = f->CreateTimeEvolver(m_cd);
347 assert((m_time_evolver !=
nullptr) && (
"Time evolver factory produces nullptr."));
350 void Sim::TimeSliceWrapup(
const std::lock_guard<std::mutex>&)
357 return unique_ptr<TimeSlicer>(
new TimeSlicer(m_timestep_mutex,
this));
363 const double time_step = m_cd.m_parameters->get<
double>(
"model.time_step");
364 slicer->Go(time_step);
365 ++m_cd.m_time_data->m_sim_step;
372 const string mark =
" Generated by SimPT processing at " + time_stamp;
373 sim_pt.add(
"<xmlcomment>", mark);
380 sim_pt.put_child(
"vleaf2.mesh", m_cd.m_mesh->ToPtree());
383 sim_pt.put(
"vleaf2.random_engine_state.type", info.type);
384 sim_pt.put(
"vleaf2.random_engine_state.seed", info.seed);
385 sim_pt.put(
"vleaf2.random_engine_state.state", info.state);
double GetTime() const
Returns the value of the current time step in seconds.
Interface for MBMBBuilder.
bool IsStationary() const
void SetRandomEngineState(boost::property_tree::ptree const &re_state)
Sets the state of the random number engine used for generating random in the simulation.
Core data used during model execution.
Namespace for miscellaneous utilities.
MeshState GetMeshState() const
Get the state of the mesh.
Simulator: mesh & parameters, model & algorithms.
static std::shared_ptr< ComponentFactoryProxy > Create(const string &group_name, bool throw_ok=true)
Create a factory proxy.
bool CheckAll() const
Runs all of the of checks to verify consistency of the mesh.
std::string GetRunDate() const
Interface of RandomEngine.
Interface for TimeKeeper::Utils.
virtual ~Sim()
Destructor is virtual (.
std::unique_ptr< TimeSlicer > TimeSlicing()
Return a SimTimeSlicer to proceed through a time step using time slices.
The Copy-Based Mesh Builder directs the Mesh building process using another mesh. ...
double GetSimTime() const
Namespace for the core simulator.
friend class TimeSlicer
Make Timeslicer our friend.
void Initialize(const boost::property_tree::ptree &pt)
Initialize with full configuration (global info, parameters, random engine, mesh) i...
Interface for PBMBBuilder.
Macro defs for debug and logging.
void SetMeshState(MeshState const &mesh_state)
Sets the current state of the mesh in the simulation.
Class that directs ptree based mesh building process.
std::string GetProjectName() const
void SetTimeStep(int step)
Sets the index of the current time step.
std::shared_ptr< Mesh > Build(const Mesh &mesh)
Build a mesh exactly like the given mesh.
Proxy for dealing with the factories.
Interface of RandomEngineType.
std::string GetStatusMessage() const
Return a status message (time, steps, cellcount).
Sim & operator=(const Sim &other)
Assignment operator rebuilds copy of mesh.
std::string GetProjectName() const
Returns the name of the associated project.
Sim, the actual simulator.
void Reinitialize(const boost::property_tree::ptree &p)
boost::property_tree::ptree ToPtree() const
Serialize into ptree.
void SetParameters(boost::property_tree::ptree const ¶meters)
Sets the parameters currently used simulation model.
Contains the state of the whole Simulator at a given simulation step.
boost::property_tree::ptree GetParameters() const
Returns the parameters currently used simulation model.
bool IsAtTermination() const
const boost::property_tree::ptree & GetParameters() const
Provides wall-clock time stamp using the time call.
Header file for Exception class.
std::string ToString() const
Returns string with the time stamp after eliminating newline.
int GetTimeStep() const
Returns the index of the current time step.
Timings GetTimings() const
void ResetTimings()
Resets the execution timings.
void SetTime(double t)
Sets the value of the current time step in seconds.
void SetProjectName(std::string project_name)
Sets the name of the associated project.
boost::property_tree::ptree GetRandomEngineState() const
Returns the state of the random engine in a ptree.
std::function< std::tuple< SimTimingTraits::CumulativeTimings, bool >(double, SimPhase)> TimeEvolverComponent
Time Evolver component interface.
bool Check() const
Verify all pointers non-null.
SimState GetState() const
Provide sim state in format suitable for i/o.
Namespace for clock and timekeeping related classes.
MBMBDirector directs the Mesh building process using a MeshState object.