Skip to content

Commit a3bbcfd

Browse files
committed
project: fix hsm-w simulation parameter initialization
1 parent 2583cc7 commit a3bbcfd

File tree

2 files changed

+78
-67
lines changed

2 files changed

+78
-67
lines changed

lib/include/irritator/core.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4871,12 +4871,6 @@ class hierarchical_state_machine
48714871

48724872
void clear() noexcept
48734873
{
4874-
i1 = 0;
4875-
i2 = 0;
4876-
r1 = 0;
4877-
r2 = 0;
4878-
timer = time_domain<time>::infinity;
4879-
48804874
ports.fill(0.0);
48814875
values.reset();
48824876

lib/src/project.cpp

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -421,94 +421,96 @@ static auto get_outcoming_connection(const modeling& mod,
421421
return nb;
422422
}
423423

424-
static auto make_tree_hsm_leaf(simulation_copy& sc,
425-
generic_component& gen,
426-
const child_id ch_id,
427-
const model_id new_mdl_id,
428-
hsm_wrapper& dyn) noexcept -> status
429-
{
430-
const auto child_index = get_index(ch_id);
431-
const auto compo_id =
432-
enum_cast<component_id>(gen.children_parameters[child_index].integers[0]);
433-
434-
if (const auto* compo = sc.mod.components.try_to_get<component>(compo_id)) {
435-
if (compo->type == component_type::hsm) {
436-
const auto hsm_id = compo->id.hsm_id;
437-
debug::ensure(sc.mod.hsm_components.try_to_get(hsm_id));
438-
const auto* shsm = sc.hsm_mod_to_sim.get(hsm_id);
439-
const auto shsm_id = *shsm;
440-
sc.pj.sim.parameters[new_mdl_id].integers[0] = ordinal(shsm_id);
441-
dyn.id = shsm_id;
442-
debug::ensure(sc.pj.sim.hsms.try_to_get(shsm_id));
443-
444-
dyn.exec.i1 = static_cast<i32>(
445-
gen.children_parameters[child_index].integers[1]);
446-
dyn.exec.i2 = static_cast<i32>(
447-
gen.children_parameters[child_index].integers[2]);
448-
// @TODO missing external source in integers[3] and [4].
449-
450-
dyn.exec.r1 = gen.children_parameters[child_index].reals[0];
451-
dyn.exec.r2 = gen.children_parameters[child_index].reals[1];
452-
dyn.exec.timer = gen.children_parameters[child_index].reals[2];
424+
static auto make_tree_hsm_leaf(const simulation_copy& sc,
425+
const parameter& mod_parameter,
426+
parameter& sim_parameter,
427+
hsm_wrapper& dyn) noexcept -> status
428+
{
429+
const auto id_param_0 = mod_parameter.integers[hsm_wrapper_tag::id];
430+
const auto compo_id = enum_cast<component_id>(id_param_0);
431+
const auto* compo = sc.mod.components.try_to_get<component>(compo_id);
453432

454-
return success();
455-
}
456-
}
433+
debug::ensure(compo and compo->type == component_type::hsm);
434+
435+
if (not(compo and compo->type == component_type::hsm))
436+
return new_error(project_errc::component_unknown);
457437

458-
return new_error(project_errc::component_unknown);
438+
const auto hsm_id = compo->id.hsm_id;
439+
440+
debug::ensure(sc.mod.hsm_components.try_to_get(hsm_id));
441+
const auto* shsm = sc.hsm_mod_to_sim.get(hsm_id);
442+
const auto shsm_id = *shsm;
443+
444+
debug::ensure(sc.pj.sim.hsms.try_to_get(shsm_id));
445+
const auto shsm_ord = ordinal(shsm_id);
446+
447+
sim_parameter.integers[hsm_wrapper_tag::id] = shsm_ord;
448+
dyn.id = shsm_id;
449+
450+
return success();
459451
}
460452

461-
static auto make_tree_constant_leaf(simulation_copy& sc,
462-
tree_node& parent,
463-
generic_component& gen,
464-
const child_id ch_id,
465-
constant& dyn) noexcept -> status
453+
static auto make_tree_constant_leaf(simulation_copy& sc,
454+
tree_node& parent,
455+
const parameter& mod_parameter,
456+
parameter& sim_parameter,
457+
constant& dyn) noexcept -> status
466458
{
467-
const auto child_index = get_index(ch_id);
459+
const auto raw_type = mod_parameter.integers[constant_tag::i_type];
460+
debug::ensure(0 <= raw_type and raw_type < constant::init_type_count);
468461

469-
switch (dyn.type) {
462+
const auto type_64 =
463+
0 <= raw_type and raw_type < constant::init_type_count ? raw_type : 0;
464+
465+
const auto type = enum_cast<constant::init_type>(type_64);
466+
467+
switch (type) {
470468
case constant::init_type::constant:
471469
break;
472470

473471
case constant::init_type::incoming_component_all: {
474472
if (auto nb = get_incoming_connection(sc.mod, parent); nb.has_value()) {
475-
gen.children_parameters[child_index].reals[0] = *nb;
476-
dyn.value = *nb;
473+
sim_parameter.reals[constant_tag::value] = *nb;
474+
dyn.value = *nb;
477475
} else
478476
return nb.error();
479477
} break;
480478

481479
case constant::init_type::outcoming_component_all: {
482480
if (auto nb = get_outcoming_connection(sc.mod, parent);
483481
nb.has_value()) {
484-
gen.children_parameters[child_index].reals[0] = *nb;
485-
dyn.value = *nb;
482+
sim_parameter.reals[constant_tag::value] = *nb;
483+
dyn.value = *nb;
486484
} else
487485
return nb.error();
488486
} break;
489487

490488
case constant::init_type::incoming_component_n: {
491-
const auto id = enum_cast<port_id>(dyn.port);
489+
const auto port = mod_parameter.integers[constant_tag::i_port];
490+
const auto id = enum_cast<port_id>(port);
491+
492492
if (not sc.mod.components.get<component>(parent.id).x.exists(id))
493493
return new_error(project_errc::component_port_x_unknown);
494494

495495
if (auto nb = get_incoming_connection(sc.mod, parent, id);
496496
nb.has_value()) {
497-
gen.children_parameters[child_index].reals[0] = *nb;
498-
dyn.value = *nb;
497+
sim_parameter.reals[constant_tag::value] = *nb;
498+
dyn.value = *nb;
499499
} else
500500
return nb.error();
501501
} break;
502502

503503
case constant::init_type::outcoming_component_n: {
504-
const auto id = enum_cast<port_id>(dyn.port);
504+
const auto port = mod_parameter.integers[constant_tag::i_port];
505+
const auto id = enum_cast<port_id>(port);
506+
505507
if (not sc.mod.components.get<component>(parent.id).y.exists(id))
506508
return new_error(project_errc::component_port_y_unknown);
507509

508510
if (auto nb = get_outcoming_connection(sc.mod, parent, id);
509511
nb.has_value()) {
510-
gen.children_parameters[child_index].reals[0] = *nb;
511-
dyn.value = *nb;
512+
sim_parameter.reals[constant_tag::value] = *nb;
513+
dyn.value = *nb;
512514
} else
513515
return nb.error();
514516
} break;
@@ -517,13 +519,13 @@ static auto make_tree_constant_leaf(simulation_copy& sc,
517519
return success();
518520
}
519521

520-
static auto make_tree_leaf(simulation_copy& sc,
521-
tree_node& parent,
522-
generic_component& gen,
523-
const std::string_view uid,
524-
dynamics_type mdl_type,
525-
child_id ch_id,
526-
generic_component::child& ch) noexcept
522+
static auto make_tree_leaf(simulation_copy& sc,
523+
tree_node& parent,
524+
const generic_component& gen,
525+
const std::string_view uid,
526+
dynamics_type mdl_type,
527+
child_id ch_id,
528+
const generic_component::child& ch) noexcept
527529
-> expected<model_id>
528530
{
529531
if (not sc.pj.sim.models.can_alloc()) {
@@ -568,16 +570,31 @@ static auto make_tree_leaf(simulation_copy& sc,
568570
}
569571
}
570572

571-
sc.pj.sim.parameters[new_mdl_id].copy_to(new_mdl);
572-
573573
if constexpr (std::is_same_v<Dynamics, hsm_wrapper>) {
574-
irt_check(make_tree_hsm_leaf(sc, gen, ch_id, new_mdl_id, dyn));
574+
// For hsm-wrapper, we need to update the hsm_id in simulation
575+
// parameters which is different between modeling and simulation.
576+
577+
irt_check(make_tree_hsm_leaf(sc,
578+
gen.children_parameters[ch_id],
579+
sc.pj.sim.parameters[new_mdl_id],
580+
dyn));
575581
}
576582

577583
if constexpr (std::is_same_v<Dynamics, constant>) {
578-
irt_check(make_tree_constant_leaf(sc, parent, gen, ch_id, dyn));
584+
// For constant, we need to update the real simulation parameter
585+
// that are different between modeling and simulation about input
586+
// connection.
587+
588+
irt_check(
589+
make_tree_constant_leaf(sc,
590+
parent,
591+
gen.children_parameters[ch_id],
592+
sc.pj.sim.parameters[new_mdl_id],
593+
dyn));
579594
}
580595

596+
sc.pj.sim.parameters[new_mdl_id].copy_to(new_mdl);
597+
581598
return success();
582599
}));
583600

0 commit comments

Comments
 (0)