Commit 5bd921ce authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

next13

parent 1c24bbcc
Pipeline #2309 failed with stage
in 1 minute and 9 seconds
......@@ -174,7 +174,8 @@ struct data_array
// accessor to the id part if Item
Identifier get_id(const T&);
T& get(Identifier id); // return item[id & 0xFFFF];
T& get(Identifier id); // return item[id & 0xFFFF];
const T& get(Identifier id) const; // return item[id & 0xFFFF];
/**
* @brief Get a T from an ID.
......@@ -370,7 +371,14 @@ template<typename T, typename Identifier>
T&
data_array<T, Identifier>::get(Identifier id)
{
return items[get_index(id)];
return items[get_index(id)].item;
}
template<typename T, typename Identifier>
const T&
data_array<T, Identifier>::get(Identifier id) const
{
return items[get_index(id)].item;
}
template<typename T, typename Identifier>
......
......@@ -70,7 +70,7 @@ struct View
std::string location;
double timestep = 1.0;
enum class view_type
enum view_type
{
timed = 0,
alloc = 1 << 1,
......@@ -81,7 +81,159 @@ struct View
finish = 1 << 6,
};
view_type type = view_type::timed;
unsigned type = view_type::timed;
};
struct vec2
{
float x, y;
constexpr vec2() noexcept
: x(0.f)
, y(0.f)
{}
constexpr vec2(float x_, float y_) noexcept
: x(x_)
, y(y_)
{}
friend constexpr vec2 operator*(const vec2& lhs, const float rhs) noexcept
{
return vec2(lhs.x * rhs, lhs.y * rhs);
}
friend constexpr vec2 operator/(const vec2& lhs, const float rhs) noexcept
{
return vec2(lhs.x / rhs, lhs.y / rhs);
}
friend constexpr vec2 operator+(const vec2& lhs, const vec2& rhs) noexcept
{
return vec2(lhs.x + rhs.x, lhs.y + rhs.y);
}
friend constexpr vec2 operator-(const vec2& lhs, const vec2& rhs) noexcept
{
return vec2(lhs.x - rhs.x, lhs.y - rhs.y);
}
friend constexpr vec2 operator*(const vec2& lhs, const vec2& rhs) noexcept
{
return vec2(lhs.x * rhs.x, lhs.y * rhs.y);
}
friend constexpr vec2 operator/(const vec2& lhs, const vec2& rhs) noexcept
{
return vec2(lhs.x / rhs.x, lhs.y / rhs.y);
}
friend constexpr vec2& operator+=(vec2& lhs, const vec2& rhs) noexcept
{
lhs.x += rhs.x;
lhs.y += rhs.y;
return lhs;
}
friend constexpr vec2& operator-=(vec2& lhs, const vec2& rhs) noexcept
{
lhs.x -= rhs.x;
lhs.y -= rhs.y;
return lhs;
}
friend constexpr vec2& operator*=(vec2& lhs, const float rhs) noexcept
{
lhs.x *= rhs;
lhs.y *= rhs;
return lhs;
}
friend constexpr vec2& operator/=(vec2& lhs, const float rhs) noexcept
{
lhs.x /= rhs;
lhs.y /= rhs;
return lhs;
}
};
struct vec3
{
float x, y, z;
constexpr vec3() noexcept
: x(0.f)
, y(0.f)
, z(0.f)
{}
constexpr vec3(float x_, float y_, float z_) noexcept
: x(x_)
, y(y_)
, z(z_)
{}
friend constexpr vec3 operator*(const vec3& lhs, const float rhs) noexcept
{
return vec3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
}
friend constexpr vec3 operator/(const vec3& lhs, const float rhs) noexcept
{
return vec3(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs);
}
friend constexpr vec3 operator+(const vec3& lhs, const vec3& rhs) noexcept
{
return vec3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
friend constexpr vec3 operator-(const vec3& lhs, const vec3& rhs) noexcept
{
return vec3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
}
friend constexpr vec3 operator*(const vec3& lhs, const vec3& rhs) noexcept
{
return vec3(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * lhs.z);
}
friend constexpr vec3 operator/(const vec3& lhs, const vec3& rhs) noexcept
{
return vec3(lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z);
}
friend constexpr vec3& operator+=(vec3& lhs, const vec3& rhs) noexcept
{
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.z += rhs.z;
return lhs;
}
friend constexpr vec3& operator-=(vec3& lhs, const vec3& rhs) noexcept
{
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.z -= rhs.z;
return lhs;
}
friend constexpr vec3& operator*=(vec3& lhs, const float rhs) noexcept
{
lhs.x *= rhs;
lhs.y *= rhs;
lhs.z *= rhs;
return lhs;
}
friend constexpr vec3& operator/=(vec3& lhs, const float rhs) noexcept
{
lhs.x /= rhs;
lhs.y /= rhs;
lhs.z /= rhs;
return lhs;
}
};
enum class value_type
......@@ -91,7 +243,8 @@ enum class value_type
integer64,
real32,
real64,
string,
vec2_32,
vec3_32
};
struct Value
......@@ -151,6 +304,61 @@ struct Class
std::string name;
};
struct State
{};
struct SimDynamics
{
// 1) classical hierarchy model? => no
// 2) function with state:
// std::function<Time(State, Time)> start;
// std::function<Time(State, Time)> transition;
// std::function<void(State)> output;
// 3) Each library gives a DataArray<T> of atomic model.
// dynamics_id gives the correct library
//
unsigned dynamics_id;
unsigned state_id;
bool debug;
};
struct SimElement
{
double tn;
double tl;
enum class simulator_element_type
{
simulator,
coordinator
};
simulator_element_type type = simulator_element_type::simulator;
};
struct Simulation
{
enum class simulation_type
{
devs,
dsde
};
enum simulation_option
{
none = 0,
flat = 1
};
double begin = 0.0;
double end = 1.0;
double current = 0.0;
unsigned id = 0u;
simulation_type type;
unsigned option;
};
struct Vpz
{
std::string name;
......@@ -160,22 +368,23 @@ struct Vpz
bits::data_array<Dynamics, ID> dynamics;
bits::data_array<View, ID> views;
bits::data_array<Value, ID> values;
bits::data_array<NamedValue, ID> named_values;
bits::data_array<Condition, ID> conditions;
bits::data_array<Port, ID> input_ports;
bits::data_array<Port, ID> output_ports;
bits::data_array<Model, ID> models;
bits::data_array<Class, ID> classes;
bits::data_array<Value, ID> values;
bits::data_array<NamedValue, ID> named_values;
bits::data_array<std::int32_t, ID> integer32;
bits::data_array<std::int64_t, ID> integer64;
bits::data_array<float, ID> real32;
bits::data_array<double, ID> real64;
bits::data_array<std::string, ID> string;
bits::data_array<vec2, ID> vec2_32;
bits::data_array<vec3, ID> vec3_32;
bits::linker<ID, ID> value_links{ 4096 };
bits::linker<ID, ID> namedvalue_links{ 4096 };
bits::linker<ID, ID> named_value_links{ 4096 };
bits::linker<ID, ID> model_dynamics{ 4096 };
bits::linker<ID, ID> model_parent{ 4096 };
bits::multi_linker<ID, ID> model_input_ports{ 4096 };
......@@ -201,18 +410,8 @@ struct Vpz
integer64.init(object_capacity);
real32.init(object_capacity);
real64.init(object_capacity);
string.init(object_capacity);
}
void write(FILE& f, Dynamics& dynamic)
{
fprintf(&f,
" <dynamic name=\"%s\" package=\"%s\" library=\"%s\" "
"language=\"%s\"\n",
dynamic.name.c_str(),
dynamic.package.c_str(),
dynamic.library.c_str(),
dynamic.language.c_str());
vec2_32.init(object_capacity);
vec3_32.init(object_capacity);
}
// void write(FILE& f, Condition& cond)
......@@ -231,6 +430,103 @@ struct Vpz
// fprintf(&f, " </conditions>\n");
// }
void write_dynamics(FILE& f)
{
fprintf(&f, " <dynamics>\n");
Dynamics* dyn = nullptr;
while (dynamics.next(dyn)) {
fprintf(&f,
" <dynamic name=\"%s\" package=\"%s\" library=\"%s\" "
"language=\"%s\"\n",
dyn->name.c_str(),
dyn->package.c_str(),
dyn->library.c_str(),
dyn->language.c_str());
}
fprintf(&f, " </dynamics>\n");
}
void write(FILE& f, const NamedValue& named_value)
{
fprintf(&f, " <port name=\"%s\">\n", named_value.name.c_str());
auto id = named_value_links[named_values.get_id(named_value)];
switch (named_value.type) {
case value_type::none:
fprintf(&f, " </null>\n");
break;
case value_type::integer32:
fprintf(&f, " <integer>%d</integer>\n", integer32.get(id));
break;
case value_type::integer64:
fprintf(&f, " <integer>%ld</integer>\n", integer64.get(id));
break;
case value_type::real32:
fprintf(&f, " <double>%f</double>\n", real32.get(id));
break;
case value_type::real64:
fprintf(&f, " <double>%f<double/>\n", real64.get(id));
break;
case value_type::vec2_32: {
auto& v = vec2_32.get(id);
fprintf(&f, " <vec2 x=\"%f\" y=\"%f\">\n", v.x, v.y);
} break;
case value_type::vec3_32: {
auto& v = vec3_32.get(id);
fprintf(
&f, " <vec3 x=\"%f\" y=\"%f\" z=\"%f\">\n", v.x, v.y, v.z);
} break;
}
fprintf(&f, " </port>\n");
}
void write_conditions(FILE& f)
{
fprintf(&f, " <conditions>\n");
Condition* cond = nullptr;
while (conditions.next(cond)) {
fprintf(&f, " <condition name=\"%s\">\n", cond->name.c_str());
for (auto it = condition_named_values.begin(
&conditions, conditions.get_id(*cond)),
et = condition_named_values.end(&conditions);
it != et;
++it) {
auto* namedvalue = named_values.try_to_get(*it);
if (namedvalue) {
write(f, *namedvalue);
}
}
fprintf(&f, " </condition>\n");
}
fprintf(&f, " </condition>\n");
}
void write_views(FILE& f)
{
fprintf(&f, " <views>\n");
fprintf(&f, " <outputs>\n");
View* view = nullptr;
while (views.next(view)) {
fprintf(&f,
" <output name=\"%s\" location=\"%s\" package=\"%s\" "
"library=\"%s\" />\n",
view->name.c_str(),
view->location.c_str(),
view->package.c_str(),
view->library.c_str());
}
fprintf(&f, " </outputs>\n");
fprintf(&f, " </views>\n");
}
void write(FILE& f)
{
fprintf(
......@@ -240,54 +536,12 @@ struct Vpz
"\"https://www.vle-project.org/vle-1.1.0.dtd\">\n");
{
fprintf(&f, " <dynamics>\n");
Dynamics* dyn = nullptr;
while (dynamics.next(dyn)) {
fprintf(&f,
" <dynamic name=\"%s\" package=\"%s\" library=\"%s\" "
"language=\"%s\"\n",
dyn->name.c_str(),
dyn->package.c_str(),
dyn->library.c_str(),
dyn->language.c_str());
}
fprintf(&f, " </dynamics>\n");
}
write_dynamics(f);
{
fprintf(&f, " <experiment name=\"%s\">\n", name.c_str());
fprintf(&f, " <conditions>\n");
Condition* cond = nullptr;
while (conditions.next(cond)) {
fprintf(&f, " <condition name=\"%s\">\n",
cond->name.c_str());
for (auto it = condition_named_values.begin(
&conditions, conditions.get_id(*cond)),
et = condition_named_values.end(&conditions);
it != et; ++it) {
auto* namedvalue = named_values.try_to_get(*it);
fprintf(&f, " <port name=\"%s\">\n",
namedvalue->name.c_str());
switch (namedvalue->type) {
case value_type::real32: {
auto *v = real32.try_to_get(namedvalue_links[*it]);
fprintf(&f, " <double>%f</double>\n",
static_cast<double>(*v));
}
break;
default:
break;
// to be continued
}
}
fprintf(&f, " </condition>\n");
}
write_conditions(f);
write_views(f);
fprintf(&f, " </experiment>\n");
}
......@@ -301,6 +555,66 @@ struct Vpz
fprintf(&f, "</vle_project>\n");
}
Dynamics& add_dynamics(std::string name,
std::string package,
std::string library,
std::string language)
{
Dynamics& ret = dynamics.alloc();
ret.name = std::move(name);
ret.package = std::move(package);
ret.library = std::move(library);
ret.language = std::move(language);
return ret;
}
void destroy(Dynamics& dynamic)
{
dynamics.free(dynamic);
}
View& add_timed_view(std::string name,
std::string package,
std::string library,
std::string location,
double timestep)
{
View& ret = views.alloc();
ret.name = std::move(name);
ret.package = std::move(package);
ret.library = std::move(library);
ret.location = std::move(location);
ret.timestep = timestep;
ret.type = View::view_type::timed;
return ret;
}
View& add_event_view(std::string name,
std::string package,
std::string library,
std::string location,
int type)
{
View& ret = views.alloc();
ret.name = std::move(name);
ret.package = std::move(package);
ret.library = std::move(library);
ret.location = std::move(location);
ret.type = type;
return ret;
}
void destroy(View& view)
{
views.free(view);
}
Model& add_model(std::string name, Model::model_type type)
{
Model& ret = models.alloc();
......@@ -356,106 +670,249 @@ struct Vpz
models.free(id_model);
}
Port& add_input_port(const Model& model, std::string name)
Value& add_value()
{
Port& ret = input_ports.alloc();
ret.name = name;
auto& v = values.alloc();
v.type = value_type::none;
auto id_model = models.get_id(model);
return v;
}
model_input_ports.emplace(id_model, input_ports.get_id(ret));
Value& add_value(float value)
{
auto& v = values.alloc();
v.type = value_type::real32;
return ret;
auto& x = real32.alloc();
x = value;
value_links.emplace(values.get_id(v), real32.get_id(x));
return v;
}
void destroy_input_port(Port& port)
Value& add_value(double value)
{
input_ports.free(port);
auto& v = values.alloc();
v.type = value_type::real64;
auto& x = real64.alloc();
x = value;
value_links.emplace(values.get_id(v), real64.get_id(x));
return v;
}
void destroy_output_port(Port& port)
Value& add_value(std::int32_t value)
{
input_ports.free(port);
auto& v = values.alloc();
v.type = value_type::integer32;
auto& x = integer32.alloc();
x = value;
value_links.emplace(values.get_id(v), integer32.get_id(x));
return v;
}
Port& add_output_port(const Model& model, std::string name)
Value& add_value(std::int64_t value)
{
Port& ret = output_ports.alloc();
ret.name = name;
auto& v = values.alloc();
v.type = value_type::integer64;
auto id_model = models.get_id(model);
auto& x = integer64.alloc();
x = value;
value_links.emplace(values.get_id(v), integer64.get_id(x));
model_output_ports.emplace(id_model, output_ports.get_id(ret));
return v;
}
return