46 static const bool value =
true;
54 SimSessionCoupled::SimSessionCoupled(
55 const std::shared_ptr<MergedPreferences>& prefs,
56 const ptree& project_info,
57 const std::shared_ptr<SimShell::Ws::IWorkspace>& workspaceModel
64 m_sim_thread(new QThread(this)),
65 m_parameter_buffer({
false, ptree() })
67 auto projects = ReadSubprojectList(project_info);
68 if (projects.empty()) {
69 throw Exception(
"Failed to open coupled project: subproject list is empty");
72 for (
const auto& project : projects) {
73 m_projects[project.first] =
74 OpenSubproject(project.first, project.second, workspaceModel);
77 vector<shared_ptr<Sim>> sims;
78 for (
const auto& project : m_projects) {
79 sims.push_back(static_pointer_cast<ISimSession>(project.second->GetSession())->GetSim());
81 m_sim->Initialize(project_info, sims);
87 for (
auto project : m_projects) {
88 if (project.second->IsOpened()) {
89 project.second->Close();
94 std::shared_ptr<SimSessionCoupled::RootViewerType>
97 SimShell::Viewer::IViewerNode::ChildrenMap m;
98 m[
"Log Dock Window"] = make_shared<SimShell::Viewer::ViewerNode<LogWindowViewer, SimPT_Sim::CoupledSim>>(
99 m_preferences->GetChild(
"viewers.logwindow"), parent);
100 for (
auto project : m_projects) {
101 auto project_root_viewer = project.second->GetSession()->CreateRootViewer(parent);
102 m[project.first] = make_shared<SimShell::Viewer::SubjectViewerNodeWrapper<CoupledSim>>(project_root_viewer);
104 return make_shared<SimShell::Viewer::SubjectNode<CoupledSim>>(m_preferences->GetChild(
"viewers"), m_sim, move(m));
107 SimSessionCoupled::ExportersType
116 return m_sim->GetParameters();
123 ss <<
"Coupled simulation:";
125 for (
auto project : m_projects) {
127 ss <<
'\t' << project.first
136 auto timings = m_sim->GetTimings();
137 timings.Merge(m_timings);
138 return timings.GetRecords<
Duration>();
143 lock_guard<mutex> parameters_guard(m_viewers_mutex);
145 for (
auto project : m_projects) {
146 project.second->GetSession()->ForceExport();
150 std::shared_ptr<SimShell::Ws::IProject> SimSessionCoupled::OpenSubproject(
151 const string& project_name,
152 const string& file_name,
153 const shared_ptr<SimShell::Ws::IWorkspace>& workspaceModel)
155 shared_ptr<SimShell::Ws::IProject> p;
157 auto project_it = workspaceModel->Find(project_name);
158 if (project_it == workspaceModel->end() ) {
159 throw Exception(
"Failed to open subproject " + project_name+
": Not Found");
161 if (!file_name.empty()) {
162 project_it->second->Open(file_name);
164 project_it->second->Open();
166 p = project_it->second.Project();
167 }
catch (std::runtime_error& e) {
168 throw Exception(
"Failed to open subproject " + project_name +
": " + e.what());
171 if (!p || !p->IsOpened()) {
172 throw Exception(
"Failed to open subproject " + project_name);
180 lock_guard<mutex> parameters_guard(m_parameters_mutex);
181 m_parameter_buffer.pt =
pt;
182 m_parameter_buffer.updated =
true;
187 assert(steps >= -1 &&
"Steps should be positive or -1");
188 m_steps_limit = steps;
192 sim_worker->moveToThread(m_sim_thread);
195 connect(m_sim_thread, SIGNAL(started()), sim_worker, SLOT(Work()));
196 connect(
this, SIGNAL(ExecuteWorkUnit()), sim_worker, SLOT(Work()));
197 connect(sim_worker, SIGNAL(Worked(QString)),
this, SLOT(ExecuteViewUnit(QString)));
198 connect(m_sim_thread, SIGNAL(finished()), sim_worker, SLOT(deleteLater()));
202 if (m_parameter_buffer.updated) {
203 m_sim->Reinitialize(m_parameter_buffer.pt);
206 lock_guard<mutex> viewers_guard(m_viewers_mutex);
208 SimPT_Sim::Event::CoupledSimEvent::Type::Forced));
210 m_parameter_buffer.updated =
false;
212 m_sim_thread->start();
220 m_sim_thread->quit();
221 m_sim_thread->wait();
226 std::vector<std::pair<std::string, std::string>>
227 SimSessionCoupled::ReadSubprojectList(
const ptree& pt)
229 vector<pair<string, string>> projects;
230 const auto& projects_pt = pt.get_child(
"vleaf2.coupled_project.subproject_array");
231 for (
auto pair : projects_pt) {
232 const auto& project_pt = pair.second;
233 projects.push_back(make_pair(
234 project_pt.get<std::string>(
"name"),
235 project_pt.get<std::string>(
"file",
"")
241 void SimSessionCoupled::ExecuteViewUnit(QString worker_message)
243 if (worker_message !=
"") {
244 throw Exception(worker_message.toStdString());
247 lock_guard<mutex> viewers_guard(m_viewers_mutex);
250 Stopclock viewersChrono(
"viewers",
true);
251 for (
auto project : m_projects) {
252 auto sim = static_pointer_cast<ISimSession>(project.second->GetSession())->GetSim();
254 SimPT_Sim::Event::SimEvent::Type::Stepped);
258 SimPT_Sim::Event::CoupledSimEvent::Type::Stepped);
260 m_timings.Record(viewersChrono.GetName(), viewersChrono.Get());
266 if ((m_steps_limit == 0) || m_sim->IsAtTermination()) {
270 lock_guard<mutex> parameters_guard(m_parameters_mutex);
271 if (m_parameter_buffer.updated) {
272 m_sim->Reinitialize(m_parameter_buffer.pt);
275 lock_guard<mutex> viewers_guard(m_viewers_mutex);
277 m_sim, m_sim->GetSimStep(),
278 SimPT_Sim::Event::CoupledSimEvent::Type::Forced));
280 m_parameter_buffer.updated =
false;
283 emit ExecuteWorkUnit();
virtual void StopSimulation()
Interface for SimSessionCoupled.
virtual void SetParameters(const ptree &)
void InfoMessage(const std::string &message, const InfoMessageReason &reason)
Emitted when an event with an accompanying info message has been done/occurred (Multithreading cannot...
Core data used during model execution.
An event transmitted by a simulator.
ptree pt
Ptree containing parameters.
Namespace for SimPT shell package.
Interface for handling an opened/running simulation.
virtual ~SimSessionCoupled()
Destructor virtual, just in case.
Extremely simple Exception root class.
virtual std::shared_ptr< RootViewerType > CreateRootViewer(SimShell::Gui::Controller::AppController *parent=nullptr)
Namespace for the core simulator.
Utility for running simulation in a separate thread.
Interface for ViewerNode.
Header for LogWindowViewer.
Interface for RootViewerNode.
An event transmitted by a Coupled Simulator.
virtual void ForceExport()
Sim, the actual simulator.
Interface for IWorkspace.
Interfaces for simulator session.
std::string GetStatusMessage() const
Return the status message.
virtual Timings GetTimings() const
Get timing information from simulation and viewers.
virtual void StartSimulation(int steps=-1)
Header file for Exception class.
virtual const ptree & GetParameters() const
Coupled Simulator: multiple simulators and couplers.
virtual ExportersType GetExporters()
SimPT_Sim::ClockMan::Stopwatch< std::chrono::system_clock > Stopclock
Stopwatch to measure time durations.
Namespace for generic graphical shell for simulators.
typename std::chrono::system_clock::duration Duration
Type for time duration units.
Interface for CoupledSim.