36 #include <QCoreApplication>
42 using namespace chrono;
54 : m_sim(make_shared<
Sim>()),
56 m_sim_thread(new QThread(this)),
59 m_parameter_buffer({
false, ptree() })
61 m_sim->Initialize(sim_pt);
64 SimSession::SimSession(
const shared_ptr<MergedPreferences>& prefs,
const SimState& sim_state)
65 : m_sim(make_shared<
Sim>()),
67 m_sim_thread(new QThread(this)),
70 m_parameter_buffer({
false, ptree() })
72 m_sim->Initialize(sim_state);
75 SimSession::~SimSession()
79 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
81 m_sim->Notify({ m_sim, m_sim->GetSimStep(), SimEvent::Type::Done });
84 shared_ptr<SimSession::RootViewerType> SimSession::CreateRootViewer(
87 return make_shared<Viewer::RootViewerNode<Sim>>(
88 m_preferences->GetChild(
"viewers"), m_sim, parent);
91 shared_ptr<SimPT_Sim::Sim> SimSession::GetSim()
96 SimSession::ExportersType SimSession::GetExporters()
100 [&](
const string& file) {
101 auto pref = make_shared<BitmapGraphicsPreferences>();
102 pref->Update({m_preferences->GetChild(
"viewers.bitmap_graphics")});
103 pref->m_format = BitmapGraphicsPreferences::Bmp;
107 [&](
const string& file) {
111 [&](
const string& file) {
115 [&](
const string& file) {
116 auto pref = make_shared<BitmapGraphicsPreferences>();
117 pref->Update({m_preferences->GetChild(
"viewers.bitmap_graphics")});
118 pref->m_format = BitmapGraphicsPreferences::Jpeg;
122 [&](
const string& file) {
126 [&](
const string& file) {
127 auto pref = make_shared<VectorGraphicsPreferences>();
128 pref->Update({m_preferences->GetChild(
"viewers.vector_graphics")});
129 pref->m_format = VectorGraphicsPreferences::Pdf;
133 [&](
const string& file) {
134 auto pref = make_shared<BitmapGraphicsPreferences>();
135 pref->Update({m_preferences->GetChild(
"viewers.bitmap_graphics")});
136 pref->m_format = BitmapGraphicsPreferences::Png;
140 [&](
const string& file) {
144 [&](
const string& file) {
150 const ptree& SimSession::GetParameters()
const
152 return m_sim->GetParameters();
155 string SimSession::GetStatusMessage()
const
157 return m_sim->GetStatusMessage();
160 SimSession::Timings SimSession::GetTimings()
const
162 auto timings = m_sim->GetTimings();
163 timings.Merge(m_timings);
164 return timings.GetRecords();
167 void SimSession::ForceExport()
169 lock_guard<mutex> parameters_guard(m_viewers_mutex);
172 SimEvent e(m_sim, m_sim->GetSimStep(), SimEvent::Type::Forced);
176 void SimSession::ExecuteViewUnit(QString worker_message)
178 if (worker_message !=
"") {
179 throw Exception(worker_message.toStdString());
182 lock_guard<mutex> viewers_guard(m_viewers_mutex);
183 m_sim->Notify(
SimEvent(m_sim, m_sim->GetSimStep(), SimEvent::Type::Stepped));
185 emit InfoMessage(GetStatusMessage(), InfoMessageReason::Stepped);
189 if ((m_steps_limit == 0) || m_sim->IsAtTermination()) {
193 lock_guard<mutex> parameters_guard(m_parameters_mutex);
194 if (m_parameter_buffer.updated) {
195 m_sim->Reinitialize(m_parameter_buffer.pt);
198 lock_guard<mutex> viewers_guard(m_viewers_mutex);
199 m_sim->Notify(
SimEvent(m_sim, m_sim->GetSimStep(), SimEvent::Type::Forced));
201 m_parameter_buffer.updated =
false;
204 emit ExecuteWorkUnit();
209 void SimSession::SetParameters(
const ptree& pt)
211 lock_guard<mutex> parameters_guard(m_parameters_mutex);
213 m_parameter_buffer.pt = pt;
214 m_parameter_buffer.updated =
true;
217 void SimSession::StartSimulation(
int steps)
219 assert(steps >= -1 &&
"Steps should be positive or -1");
220 m_steps_limit = steps;
224 sim_worker->moveToThread(m_sim_thread);
227 connect(m_sim_thread, SIGNAL(started()), sim_worker, SLOT(Work()));
228 connect(
this, SIGNAL(ExecuteWorkUnit()), sim_worker, SLOT(Work()));
229 connect(sim_worker, SIGNAL(Worked(QString)),
this, SLOT(ExecuteViewUnit(QString)));
230 connect(m_sim_thread, SIGNAL(finished()), sim_worker, SLOT(deleteLater()));
234 if (m_parameter_buffer.updated) {
235 m_sim->Reinitialize(m_parameter_buffer.pt);
238 lock_guard<mutex> viewers_guard(m_viewers_mutex);
239 m_sim->Notify(
SimEvent(m_sim, m_sim->GetSimStep(), SimEvent::Type::Forced));
241 m_parameter_buffer.updated =
false;
243 if (m_sim->IsAtTermination()) {
244 emit InfoMessage(GetStatusMessage(), InfoMessageReason::Terminated);
246 m_sim_thread->start();
247 emit InfoMessage(GetStatusMessage(), InfoMessageReason::Started);
251 void SimSession::StopSimulation()
254 m_sim_thread->quit();
255 m_sim_thread->wait();
257 if (m_sim->IsAtTermination()) {
258 emit InfoMessage(GetStatusMessage(), InfoMessageReason::Terminated);
260 emit InfoMessage(GetStatusMessage(), InfoMessageReason::Stopped);
264 void SimSession::TimeStep()
Interface for XmlExporter.
static bool Export(std::shared_ptr< SimPT_Sim::Sim > sim, const std::string &filename, bool overwrite=true)
Export state to xml.
Namespace for miscellaneous utilities.
Interface for XmlGzExporter.
Simulator: mesh & parameters, model & algorithms.
An event transmitted by a simulator.
static bool Export(std::shared_ptr< SimPT_Sim::Sim > sim, std::string const &file_path, bool overwrite=true, std::shared_ptr< BitmapGraphicsPreferences > prefs=std::make_shared< BitmapGraphicsPreferences >())
Export snapshot of canvas in PNG format.
Namespace for SimPT shell package.
static bool Export(std::shared_ptr< SimPT_Sim::SimInterface > sim, std::string const &file_path, bool overwrite=true)
Export mesh state to csv format.
static std::string GetFileExtension()
File extension associated with this export format.
Namespace for graphical interface classes.
static bool Export(std::shared_ptr< SimPT_Sim::Sim > sim, const std::string &filename, bool overwrite=true)
Export state to xml.
Extremely simple Exception root class.
Namespace for the core simulator.
Namespace for session classes.
static bool Export(std::shared_ptr< SimPT_Sim::Sim > mesh, std::string const &file_path, bool overwrite=true)
Export mesh state to hdf5 format.
SimSession(const std::shared_ptr< MergedPreferences > &prefs, const ptree &sim)
Initializes with a ptree description of the simulator state.
static bool Export(std::shared_ptr< SimPT_Sim::SimInterface > sim, std::string const &file_path, bool overwrite=true, std::shared_ptr< VectorGraphicsPreferences > prefs=std::make_shared< VectorGraphicsPreferences >())
Export snapshot of canvas in PNG format.
Macro defs for debug and logging.
Utility for running simulation in a separate thread.
Namespace for generic workspace classes.
Interface for RootViewerNode.
Interface for MergedPreferences.
Interfaces for simulator session.
Contains the state of the whole Simulator at a given simulation step.
static std::string GetFileExtension()
File extension associated with this export format.
Namespace for workspace events.
Interface for PlyExporter.
Interface for Csv Exporter.
Interface for VectorGraphicsExporter.
Interface for BitmapGraphicsExporter.
Header file for Exception class.
Namespace event types raised by the simulator.
static std::string GetFileExtension()
File extension associated with this export format.
static std::string GetFileExtension()
File extension associated with this export format.
static std::string GetFileExtension()
File extension associated with this export format.
static bool Export(std::shared_ptr< SimPT_Sim::SimInterface > sim, std::string const &file_path, bool overwrite=true)
Export sim state to ply format.
Interface for Hdf5Exporter.
Namespace for clock and timekeeping related classes.