Skip to content

Commit

Permalink
core: use simulation::t instead of external variable
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jul 10, 2024
1 parent 85809e6 commit 4730359
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 176 deletions.
1 change: 0 additions & 1 deletion app/gui/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ struct simulation_editor {

real simulation_begin = 0;
real simulation_end = 100;
real simulation_current = 0;
real simulation_display_current = 0;

/** Number of microsecond to run 1 unit of simulation time. The default is
Expand Down
2 changes: 1 addition & 1 deletion app/gui/generic/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ static status copy(simulation_editor& ed, const ImVector<int>& nodes) noexcept
auto src_mdl_id = app.sim.models.get_id(src_mdl);
auto dst_mdl_id = app.sim.models.get_id(dst_mdl);

irt_check(app.sim.make_initialize(dst_mdl, ed.simulation_current));
irt_check(app.sim.make_initialize(dst_mdl, app.sim.t));

mapping.data.emplace_back(src_mdl_id, dst_mdl_id);
}
Expand Down
89 changes: 47 additions & 42 deletions app/gui/simulation-component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ static void make_init_error_msg(application& app,

static void simulation_copy(application& app) noexcept
{
app.simulation_ed.simulation_state = simulation_status::initializing;
app.simulation_ed.simulation_current = app.simulation_ed.simulation_begin;
app.simulation_ed.simulation_state = simulation_status::initializing;

auto compo_id = app.pj.head();
auto* compo = app.mod.components.try_to_get(compo_id);
Expand All @@ -83,7 +82,8 @@ static void simulation_copy(application& app) noexcept
[&]() noexcept -> status {
irt_check(app.pj.set(app.mod, app.sim, *compo));
irt_check(app.mod.srcs.prepare());
irt_check(app.sim.initialize(app.simulation_ed.simulation_begin));
app.sim.t = app.simulation_ed.simulation_begin;
irt_check(app.sim.initialize());
app.simulation_ed.simulation_state = simulation_status::initialized;
return success();
},
Expand All @@ -106,8 +106,7 @@ static void simulation_copy(application& app) noexcept

static void simulation_init(application& app) noexcept
{
app.simulation_ed.simulation_state = simulation_status::initializing;
app.simulation_ed.simulation_current = app.simulation_ed.simulation_begin;
app.simulation_ed.simulation_state = simulation_status::initializing;

app.simulation_ed.tl.reset();

Expand All @@ -120,9 +119,11 @@ static void simulation_init(application& app) noexcept

attempt_all(
[&]() noexcept -> status {
app.sim.t = app.simulation_ed.simulation_begin;
app.simulation_ed.simulation_display_current = app.sim.t;
irt_check(simulation_init_observation(app));
irt_check(app.mod.srcs.prepare());
irt_check(app.sim.initialize(app.simulation_ed.simulation_begin));
irt_check(app.sim.initialize());
app.simulation_ed.simulation_state = simulation_status::initialized;
return success();
},
Expand All @@ -142,7 +143,7 @@ static status debug_run(simulation_editor& sim_ed) noexcept
{
auto& app = container_of(&sim_ed, &application::simulation_ed);

if (auto ret = run(sim_ed.tl, app.sim, sim_ed.simulation_current); !ret) {
if (auto ret = run(sim_ed.tl, app.sim, app.sim.t); !ret) {
auto& app = container_of(&sim_ed, &application::simulation_ed);
auto& n = app.notifications.alloc(log_level::error);
n.title = "Debug run error";
Expand All @@ -158,7 +159,7 @@ static status run(simulation_editor& sim_ed) noexcept
{
auto& app = container_of(&sim_ed, &application::simulation_ed);

if (auto ret = app.sim.run(sim_ed.simulation_current); !ret) {
if (auto ret = app.sim.run(); !ret) {
auto& app = container_of(&sim_ed, &application::simulation_ed);
auto& n = app.notifications.alloc(log_level::error);
n.title = "Run error";
Expand All @@ -174,7 +175,7 @@ static status debug_live_run(simulation_editor& sim_ed) noexcept
{
auto& app = container_of(&sim_ed, &application::simulation_ed);

if (auto ret = run(sim_ed.tl, app.sim, sim_ed.simulation_current); !ret) {
if (auto ret = run(sim_ed.tl, app.sim, app.sim.t); !ret) {
app.notifications.try_insert(
log_level::error, [&](auto& title, auto& msg) {
title = "Live debug run error";
Expand All @@ -191,7 +192,7 @@ static status live_run(simulation_editor& sim_ed) noexcept
{
auto& app = container_of(&sim_ed, &application::simulation_ed);

if (auto ret = app.sim.run(sim_ed.simulation_current); !ret) {
if (auto ret = app.sim.run(); !ret) {
app.notifications.try_insert(
log_level::error, [&](auto& title, auto& msg) {
title = "Live run error";
Expand Down Expand Up @@ -230,12 +231,14 @@ void simulation_editor::start_simulation_static_run() noexcept
if (auto ret = debug_run(app.simulation_ed); !ret) {
app.simulation_ed.simulation_state =
simulation_status::finish_requiring;
app.simulation_ed.simulation_display_current = app.sim.t;
return;
}
} else {
if (auto ret = run(app.simulation_ed); !ret) {
app.simulation_ed.simulation_state =
simulation_status::finish_requiring;
app.simulation_ed.simulation_display_current = app.sim.t;
return;
}
}
Expand All @@ -244,12 +247,11 @@ void simulation_editor::start_simulation_static_run() noexcept
app.simulation_ed.start_simulation_observation();

if (!app.simulation_ed.infinity_simulation &&
app.simulation_ed.simulation_current >=
app.simulation_ed.simulation_end) {
app.simulation_ed.simulation_current =
app.simulation_ed.simulation_end;
app.sim.t >= app.simulation_ed.simulation_end) {
app.sim.t = app.simulation_ed.simulation_end;
app.simulation_ed.simulation_state =
simulation_status::finish_requiring;
app.simulation_ed.simulation_display_current = app.sim.t;
return;
}

Expand All @@ -262,6 +264,8 @@ void simulation_editor::start_simulation_static_run() noexcept
} while (!stop_or_pause && duration_since_start <
app.simulation_ed.thread_frame_duration);

app.simulation_ed.simulation_display_current = app.sim.t;

if (app.simulation_ed.force_pause) {
app.simulation_ed.force_pause = false;
app.simulation_ed.simulation_state =
Expand Down Expand Up @@ -302,7 +306,7 @@ void simulation_editor::start_simulation_live_run() noexcept

simulation_state = simulation_status::running;

const auto sim_start_at = simulation_current;
const auto sim_start_at = app.sim.t;
auto sim_duration = 0.;
const auto start_at = stdc::high_resolution_clock::now();
auto duration = stdc::high_resolution_clock::now() - start_at;
Expand All @@ -327,28 +331,40 @@ void simulation_editor::start_simulation_live_run() noexcept
start_simulation_observation();

duration = stdc::high_resolution_clock::now() - start_at;
sim_duration = app.simulation_ed.simulation_current - sim_start_at;
sim_duration = app.sim.t - sim_start_at;

if (sim_duration >= sim_max_duration) {
const auto remaining =
stdc::microseconds(thread_frame_duration) - duration;
if (remaining > stdc::nanoseconds::zero()) {
std::this_thread::sleep_for(remaining);
duration = thread_frame_duration_ms;
}
break;
}

if (time_domain<time>::is_infinity(sim_duration)) {
if (infinity_simulation and
time_domain<time>::is_infinity(sim_duration)) {
const auto remaining =
stdc::microseconds(thread_frame_duration) - duration;
if (remaining > stdc::nanoseconds::zero()) {
std::this_thread::sleep_for(remaining);
duration = thread_frame_duration_ms;
}

simulation_current = sim_start_at + sim_max_duration;
simulation_display_current = simulation_current;
app.sim.t = sim_start_at + sim_max_duration;
break;
}

if (simulation_current >= simulation_end) {
simulation_current = simulation_end;
simulation_display_current = simulation_current;
if (not infinity_simulation and app.sim.t >= simulation_end) {
app.sim.t = simulation_end;
simulation_display_current = app.sim.t;
simulation_state = simulation_status::finish_requiring;
return;
}
}

simulation_display_current = app.sim.t;
app.simulation_ed.simulation_state = simulation_status::paused;
});
}
Expand Down Expand Up @@ -576,10 +592,8 @@ void simulation_editor::start_simulation_start_1() noexcept
}

if (!app.simulation_ed.infinity_simulation &&
app.simulation_ed.simulation_current >=
app.simulation_ed.simulation_end) {
app.simulation_ed.simulation_current =
app.simulation_ed.simulation_end;
app.sim.t >= app.simulation_ed.simulation_end) {
app.sim.t = app.simulation_ed.simulation_end;
app.simulation_ed.simulation_state =
simulation_status::finish_requiring;
return;
Expand Down Expand Up @@ -641,18 +655,16 @@ void simulation_editor::start_simulation_finish() noexcept
app.simulation_ed.start_simulation_observation();

if (app.simulation_ed.store_all_changes) {
if (auto ret = finalize(app.simulation_ed.tl,
app.sim,
app.simulation_ed.simulation_current);
if (auto ret = finalize(app.simulation_ed.tl, app.sim, app.sim.t);
!ret) {
auto& n = app.notifications.alloc();
n.title = "Simulation finalizing fail (with store all "
"changes option)";
app.notifications.enable(n);
}
} else {
if (auto ret = app.sim.finalize(app.simulation_ed.simulation_end);
!ret) {
app.sim.t = app.simulation_ed.simulation_end;
if (auto ret = app.sim.finalize(); !ret) {
auto& n = app.notifications.alloc();
n.title = "simulation finish fail";
app.notifications.enable(n);
Expand All @@ -671,9 +683,7 @@ void simulation_editor::start_simulation_advance() noexcept
if (app.simulation_ed.tl.can_advance()) {
attempt_all(
[&]() noexcept -> status {
return advance(app.simulation_ed.tl,
app.sim,
app.simulation_ed.simulation_current);
return advance(app.simulation_ed.tl, app.sim, app.sim.t);
},

[&](const simulation::part s) noexcept -> void {
Expand Down Expand Up @@ -701,9 +711,7 @@ void simulation_editor::start_simulation_back() noexcept
if (app.simulation_ed.tl.can_back()) {
attempt_all(
[&]() noexcept -> status {
return back(app.simulation_ed.tl,
app.sim,
app.simulation_ed.simulation_current);
return back(app.simulation_ed.tl, app.sim, app.sim.t);
},

[&](const simulation::part s) noexcept -> void {
Expand Down Expand Up @@ -731,9 +739,7 @@ void simulation_editor::start_enable_or_disable_debug() noexcept

attempt_all(
[&]() -> status {
irt_check(initialize(app.simulation_ed.tl,
app.sim,
app.simulation_ed.simulation_current));
irt_check(initialize(app.simulation_ed.tl, app.sim, app.sim.t));

return success();
},
Expand Down Expand Up @@ -781,8 +787,7 @@ void simulation_editor::start_simulation_model_add(const dynamics_type type,

attempt_all(
[&]() noexcept -> status {
irt_check(app.sim.make_initialize(
mdl, app.simulation_ed.simulation_current));
irt_check(app.sim.make_initialize(mdl, app.sim.t));

app.simulation_ed.models_to_move.emplace_enqueue(mdl_id,
ImVec2(x, y));
Expand Down
1 change: 0 additions & 1 deletion app/gui/simulation-editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ void simulation_editor::clear() noexcept

simulation_begin = 0;
simulation_end = 100;
simulation_current = 0;
simulation_display_current = 0;

nb_microsecond_per_simulation_time = 1000000;
Expand Down
10 changes: 5 additions & 5 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,17 @@ void run_simulation(irt::real begin,
irt_check(mod.init(init));
irt_check(pj.load(mod, sim, cache, file_name));

irt::time t = begin;
sim.t = begin;
const irt::time end = begin + duration;

irt_check(sim.srcs.prepare());
irt_check(sim.initialize(t));
irt_check(sim.initialize());

do {
irt_check(sim.run(t));
} while (t < end);
irt_check(sim.run());
} while (sim.t < end);

irt_check(sim.finalize(t));
irt_check(sim.finalize());

return irt::success();
},
Expand Down
Loading

0 comments on commit 4730359

Please sign in to comment.