Commit 2b080b2b authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

core: refactoring input and output ports

parent d8b48438
Pipeline #39479 passed with stage
in 1 minute and 29 seconds
......@@ -18,28 +18,6 @@
#include "implot.h"
#include <imgui.h>
namespace ImGui {
template<typename RealType>
bool
InputReal(const char* label,
RealType* v,
RealType step = irt::zero,
RealType step_fast = irt::zero,
const char* format = "%.6f",
ImGuiInputTextFlags flags = 0)
{
if constexpr (std::is_same_v<RealType, float>) {
return InputFloat(label, v, step, step_fast, format, flags);
}
if constexpr (std::is_same_v<RealType, double>) {
return InputDouble(label, v, step, step_fast, format, flags);
}
}
} // namespace ImGui
namespace irt {
// Forward declaration
......@@ -174,10 +152,10 @@ struct editor
simulation sim;
external_source srcs;
irt::real simulation_begin = 0.0;
irt::real simulation_end = 10.0;
irt::real simulation_current = 10.0;
irt::real simulation_next_time = 0.0;
irt::real simulation_begin = 0;
irt::real simulation_end = 10;
irt::real simulation_current = 10;
irt::real simulation_next_time = 0;
long simulation_bag_id = 0;
int step_by_step_bag = 0;
......
......@@ -9,6 +9,8 @@
#include <fmt/format.h>
#include <imgui.h>
namespace irt {
/// Helper to display a little (?) mark which shows a tooltip when hovered. In
......@@ -34,4 +36,35 @@ format(small_string<N>& str, const char* fmt, const Args&... args)
} // namespace irt
namespace ImGui {
template<typename RealType>
bool
InputReal(const char* label,
RealType* v,
RealType step = irt::zero,
RealType step_fast = irt::zero,
const char* format = "%.6f",
ImGuiInputTextFlags flags = 0)
{
if constexpr (std::is_same_v<RealType, float>) {
return InputFloat(label, v, step, step_fast, format, flags);
}
if constexpr (std::is_same_v<RealType, double>) {
return InputDouble(label, v, step, step_fast, format, flags);
}
}
template<typename... Args>
void
TextFormat(const char* fmt, const Args&... args)
{
irt::small_string<64> buffer;
irt::format(buffer, fmt, args...);
ImGui::TextUnformatted(buffer.c_str());
}
} // namespace ImGui
#endif
This diff is collapsed.
......@@ -64,8 +64,7 @@ plot_output_callback(const irt::observer& obs,
return;
}
while (!plot_output->xs.empty() &&
static_cast<double>(plot_output->xs.back()) == tl)
while (!plot_output->xs.empty() && plot_output->xs.back() == tl)
pop_data(plot_output->xs, plot_output->ys);
switch (type) {
......@@ -84,12 +83,12 @@ plot_output_callback(const irt::observer& obs,
for (auto td = tl; td < t; td += plot_output->time_step) {
const auto e = td - tl;
const auto value =
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / 2.0);
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / two);
push_data(plot_output->xs, plot_output->ys, td, value);
}
const auto e = t - tl;
const auto value =
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / 2.0);
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / two);
push_data(plot_output->xs, plot_output->ys, t, value);
} break;
......@@ -97,14 +96,14 @@ plot_output_callback(const irt::observer& obs,
for (auto td = tl; td < t; td += plot_output->time_step) {
const auto e = td - tl;
const auto value = obs.msg[0] + obs.msg[1] * e +
(obs.msg[2] * e * e / 2.0) +
(obs.msg[3] * e * e * e / 3.0);
(obs.msg[2] * e * e / two) +
(obs.msg[3] * e * e * e / three);
push_data(plot_output->xs, plot_output->ys, td, value);
}
const auto e = t - tl;
const auto value = obs.msg[0] + obs.msg[1] * e +
(obs.msg[2] * e * e / 2.0) +
(obs.msg[3] * e * e * e / 3.0);
(obs.msg[2] * e * e / two) +
(obs.msg[3] * e * e * e / three);
push_data(plot_output->xs, plot_output->ys, t, value);
} break;
......@@ -116,10 +115,10 @@ plot_output_callback(const irt::observer& obs,
void
file_discrete_output_callback(const irt::observer& obs,
const irt::dynamics_type type,
const irt::time tl,
const irt::time t,
const irt::observer::status s)
const irt::dynamics_type type,
const irt::time tl,
const irt::time t,
const irt::observer::status s)
{
auto* out = reinterpret_cast<file_discrete_output*>(obs.user_data);
......@@ -153,12 +152,12 @@ file_discrete_output_callback(const irt::observer& obs,
for (auto td = tl; td < t; td += out->time_step) {
const auto e = td - tl;
const auto value =
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / 2.0);
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / two);
fmt::print(out->ofs, "{},{}\n", td, value);
}
const auto e = t - tl;
const auto value =
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / 2.0);
obs.msg[0] + obs.msg[1] * e + (obs.msg[2] * e * e / two);
fmt::print(out->ofs, "{},{}\n", t, value);
} break;
......@@ -166,14 +165,14 @@ file_discrete_output_callback(const irt::observer& obs,
for (auto td = tl; td < t; td += out->time_step) {
const auto e = td - tl;
const auto value = obs.msg[0] + obs.msg[1] * e +
(obs.msg[2] * e * e / 2.0) +
(obs.msg[3] * e * e * e / 3.0);
(obs.msg[2] * e * e / two) +
(obs.msg[3] * e * e * e / three);
fmt::print(out->ofs, "{},{}\n", td, value);
}
const auto e = t - tl;
const auto value = obs.msg[0] + obs.msg[1] * e +
(obs.msg[2] * e * e / 2.0) +
(obs.msg[3] * e * e * e / 3.0);
(obs.msg[2] * e * e / two) +
(obs.msg[3] * e * e * e / three);
fmt::print(out->ofs, "{},{}\n", t, value);
} break;
......@@ -257,10 +256,10 @@ file_output_callback(const irt::observer& obs,
static void
show_simulation_run(window_logger& /*log_w*/, editor& ed)
{
ImGui::Text("Current time %g", ed.simulation_current);
ImGui::Text("Current bag %ld", ed.simulation_bag_id);
ImGui::Text("Next time %g", ed.simulation_next_time);
ImGui::Text("Model %lu", (unsigned long)ed.sim.sched.size());
ImGui::TextFormat("Current time {:.6f}", ed.simulation_current);
ImGui::TextFormat("Current bag {}", ed.simulation_bag_id);
ImGui::TextFormat("Next time {:.6f}", ed.simulation_next_time);
ImGui::TextFormat("Model {}", (unsigned long)ed.sim.sched.size());
ImGui::SliderFloat("Speed",
&ed.synchronize_timestep,
......@@ -349,7 +348,7 @@ application::show_simulation_window()
show_simulation_run(log_w, *ed);
if (ed->st != editor_status::editing) {
ImGui::Text("Current: %g", ed->simulation_current);
ImGui::TextFormat("Current: {:.6f}", ed->simulation_current);
const double duration = ed->simulation_end - ed->simulation_begin;
const double elapsed =
......
......@@ -14,90 +14,92 @@
namespace irt {
/*
static void
show_random_distribution_text(const random_source& src) noexcept
{
switch (src.distribution) {
case distribution_type::uniform_int:
ImGui::Text("a: %d", src.a32);
ImGui::Text("b: %d", src.b32);
break;
case distribution_type::uniform_real:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::bernouilli:
ImGui::Text("p: %f", src.p);
break;
case distribution_type::binomial:
ImGui::Text("p: %f", src.p);
ImGui::Text("t: %d", src.t32);
break;
case distribution_type::negative_binomial:
ImGui::Text("p: %f", src.p);
ImGui::Text("t: %d", src.k32);
break;
case distribution_type::geometric:
ImGui::Text("p: %f", src.p);
break;
case distribution_type::poisson:
ImGui::Text("mean: %f", src.mean);
break;
case distribution_type::exponential:
ImGui::Text("lambda: %f", src.lambda);
break;
case distribution_type::gamma:
ImGui::Text("alpha: %f", src.alpha);
ImGui::Text("beta: %f", src.beta);
break;
case distribution_type::weibull:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::exterme_value:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::normal:
ImGui::Text("mean: %f", src.mean);
ImGui::Text("stddev: %f", src.stddev);
break;
case distribution_type::lognormal:
ImGui::Text("m: %f", src.m);
ImGui::Text("s: %f", src.s);
break;
case distribution_type::chi_squared:
ImGui::Text("n: %f", src.n);
break;
case distribution_type::cauchy:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::fisher_f:
ImGui::Text("m: %f", src.m);
ImGui::Text("s: %f", src.n);
break;
case distribution_type::student_t:
ImGui::Text("n: %f", src.n);
break;
}
switch (src.distribution) {
case distribution_type::uniform_int:
ImGui::Text("a: %d", src.a32);
ImGui::Text("b: %d", src.b32);
break;
case distribution_type::uniform_real:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::bernouilli:
ImGui::Text("p: %f", src.p);
break;
case distribution_type::binomial:
ImGui::Text("p: %f", src.p);
ImGui::Text("t: %d", src.t32);
break;
case distribution_type::negative_binomial:
ImGui::Text("p: %f", src.p);
ImGui::Text("t: %d", src.k32);
break;
case distribution_type::geometric:
ImGui::Text("p: %f", src.p);
break;
case distribution_type::poisson:
ImGui::Text("mean: %f", src.mean);
break;
case distribution_type::exponential:
ImGui::Text("lambda: %f", src.lambda);
break;
case distribution_type::gamma:
ImGui::Text("alpha: %f", src.alpha);
ImGui::Text("beta: %f", src.beta);
break;
case distribution_type::weibull:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::exterme_value:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::normal:
ImGui::Text("mean: %f", src.mean);
ImGui::Text("stddev: %f", src.stddev);
break;
case distribution_type::lognormal:
ImGui::Text("m: %f", src.m);
ImGui::Text("s: %f", src.s);
break;
case distribution_type::chi_squared:
ImGui::Text("n: %f", src.n);
break;
case distribution_type::cauchy:
ImGui::Text("a: %f", src.a);
ImGui::Text("b: %f", src.b);
break;
case distribution_type::fisher_f:
ImGui::Text("m: %f", src.m);
ImGui::Text("s: %f", src.n);
break;
case distribution_type::student_t:
ImGui::Text("n: %f", src.n);
break;
}
}
*/
static void
show_random_distribution_input(random_source& src) noexcept
......
This diff is collapsed.
......@@ -72,13 +72,13 @@ static inline const char* dynamics_type_names[] = { "none",
static_assert(std::size(dynamics_type_names) ==
static_cast<sz>(dynamics_type_size()));
static inline const char* str_empty[] = { "" };
static inline const char* str_integrator[] = { "x-dot", "reset" };
static inline const char* str_empty[] = { "" };
static inline const char* str_integrator[] = { "x-dot", "reset" };
static inline const char* str_adaptative_integrator[] = { "quanta",
"x-dot",
"reset" };
static inline const char* str_in_1[] = { "in" };
static inline const char* str_in_2[] = { "in-1", "in-2" };
static inline const char* str_in_1[] = { "in" };
static inline const char* str_in_2[] = { "in-1", "in-2" };
static inline const char* str_in_3[] = { "in-1", "in-2", "in-3" };
static inline const char* str_in_4[] = { "in-1", "in-2", "in-3", "in-4" };
static inline const char* str_value_if_else[] = { "value",
......@@ -246,7 +246,7 @@ get_input_port_names(const dynamics_type type) noexcept
irt_unreachable();
}
static inline const char* str_out_1[] = { "out" };
static inline const char* str_out_1[] = { "out" };
static inline const char* str_out_cross[] = { "if-value",
"else-value",
"event" };
......@@ -389,12 +389,12 @@ get_output_port_names(const dynamics_type type) noexcept
class streambuf : public std::streambuf
{
public:
std::streambuf* m_stream_buffer = { nullptr };
std::streamsize m_file_position = { 0 };
int m_line_number = { 1 };
int m_last_line_number = { 1 };
int m_column = { 0 };
int m_prev_column = { -1 };
std::streambuf* m_stream_buffer = { nullptr };
std::streamsize m_file_position = { 0 };
int m_line_number = { 1 };
int m_last_line_number = { 1 };
int m_column = { 0 };
int m_prev_column = { -1 };
streambuf(std::streambuf* sbuf)
: m_stream_buffer(sbuf)
......@@ -417,7 +417,7 @@ protected:
if (traits_type::eq_int_type(rc, traits_type::to_int_type('\n'))) {
++m_line_number;
m_prev_column = m_column + 1;
m_column = -1;
m_column = -1;
}
++m_column;
......@@ -432,8 +432,8 @@ protected:
if (traits_type::eq_int_type(c, traits_type::to_int_type('\n'))) {
--m_line_number;
m_last_line_number = m_line_number;
m_column = m_prev_column;
m_prev_column = 0;
m_column = m_prev_column;
m_prev_column = 0;
}
--m_column;
......@@ -445,17 +445,17 @@ protected:
return m_stream_buffer->sungetc();
}
std::ios::pos_type seekoff(std::ios::off_type pos,
std::ios_base::seekdir dir,
std::ios::pos_type seekoff(std::ios::off_type pos,
std::ios_base::seekdir dir,
std::ios_base::openmode mode) override final
{
if (dir == std::ios_base::beg &&
pos == static_cast<std::ios::off_type>(0)) {
m_last_line_number = 1;
m_line_number = 1;
m_column = 0;
m_prev_column = -1;
m_file_position = 0;
m_line_number = 1;
m_column = 0;
m_prev_column = -1;
m_file_position = 0;
return m_stream_buffer->pubseekoff(pos, dir, mode);
}
......@@ -463,15 +463,15 @@ protected:
return std::streambuf::seekoff(pos, dir, mode);
}
std::ios::pos_type seekpos(std::ios::pos_type pos,
std::ios::pos_type seekpos(std::ios::pos_type pos,
std::ios_base::openmode mode) override final
{
if (pos == static_cast<std::ios::pos_type>(0)) {
m_last_line_number = 1;
m_line_number = 1;
m_column = 0;
m_prev_column = -1;
m_file_position = 0;
m_line_number = 1;
m_column = 0;
m_prev_column = -1;
m_file_position = 0;
return m_stream_buffer->pubseekpos(pos, mode);
}
......@@ -483,7 +483,7 @@ protected:
class reader
{
private:
streambuf buf;
streambuf buf;
std::istream is;
struct mapping
......@@ -500,6 +500,26 @@ private:
{
return index < other.index;
}
bool operator<(int i) const noexcept { return index < i; }
};
struct mapping_compare
{
bool operator()(const mapping& lhs, const mapping& rhs)
{
return lhs.index < rhs.index;
}
bool operator()(int index, const mapping& rhs)
{
return index < rhs.index;
}
bool operator()(const mapping& lhs, int index)
{
return lhs.index < index;
}
};
struct position
......@@ -516,13 +536,13 @@ private:
std::vector<model_id> map;
std::vector<position> positions; /* Stores positions of the models. */
std::vector<mapping> constant_mapping;
std::vector<mapping> binary_file_mapping;
std::vector<mapping> random_mapping;
std::vector<mapping> text_file_mapping;
std::vector<mapping> constant_mapping;
std::vector<mapping> binary_file_mapping;
std::vector<mapping> random_mapping;
std::vector<mapping> text_file_mapping;
int source_number = 0;
int model_number = 0;
int model_number = 0;
char temp_1[32];
char temp_2[32];
......@@ -535,18 +555,12 @@ public:
~reader() noexcept = default;
int model_error = 0;
int model_error = 0;
int connection_error = 0;
int line_error() const noexcept
{
return buf.m_line_number;
}
int line_error() const noexcept { return buf.m_line_number; }
int column_error() const noexcept
{
return buf.m_column;
}
int column_error() const noexcept { return buf.m_column; }
position get_position(const sz index) const noexcept
{
......@@ -569,8 +583,8 @@ public:
return status::success;
}
status operator()(simulation& sim,
external_source& srcs,
status operator()(simulation& sim,
external_source& srcs,
function_ref<void(const model_id)> f) noexcept
{
irt_return_if_bad(do_read_data_source(srcs));
......@@ -643,7 +657,7 @@ private:
status do_read_constant_source(external_source& srcs) noexcept
{
u32 id;
sz size;
sz size;
if (!(is >> id >> size))
return status::io_file_format_error;
......@@ -924,7 +938,7 @@ private:
irt_return_if_fail(mdl_dst, status::io_file_format_model_unknown);
output_port* out = nullptr;
input_port* in = nullptr;
input_port* in = nullptr;
irt_return_if_bad(get_output_port(*mdl_src, port_src_index, out));
irt_return_if_bad(get_input_port(*mdl_dst, port_dst_index, in));
......@@ -939,18 +953,18 @@ private:
}
bool convert(const std::string_view dynamics_name,
dynamics_type* type) noexcept
dynamics_type* type) noexcept
{
struct string_to_type
{
constexpr string_to_type(const std::string_view n,
const dynamics_type t)
const dynamics_type t)
: name(n)
, type(t)
{}
const std::string_view name;
dynamics_type type;
dynamics_type type;
};