Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/212 provide overview of modified variables #296

Merged
merged 34 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1dccc2e
#212 Map of modified variables now exposed through execution
Jun 24, 2019
3df90fb
Merge branch 'master' into feature/212-provide-overview-of-modified-v…
Jun 24, 2019
4e4d8cf
#212 Using variable_id instead of variable_index, finished test
Jun 25, 2019
3bd16cd
Merge remote-tracking branch 'origin/feature/212-provide-overview-of-…
Jun 25, 2019
b9e0bd3
#212 Added to C API
Jun 25, 2019
fa848b4
#212 Remove unused function
Jun 26, 2019
1d3d365
#212 Updated C API for modified vars
Jul 2, 2019
9d76d9a
#212 Fixed duplication issue in list of modified variables
Jul 3, 2019
3d51279
#212 Use unordered_set instead to avoid registering multiple modifier…
Jul 3, 2019
07ed0a9
Merge branch 'master' into feature/212-provide-overview-of-modified-v…
Jul 4, 2019
6a074dd
#212 Add exception import
Jul 4, 2019
6b3631b
#212 Remove logging
Jul 8, 2019
f86c838
#212 GCC 7 adaptation
Jul 8, 2019
57b2761
#212 Review follow-up changes
Jul 9, 2019
71734d7
#212 Review follow-up changes
Jul 9, 2019
6c2b5f4
#212 More review follow-up changes
Jul 12, 2019
5c9e730
#212 Should not count twice
Jul 12, 2019
ae8e577
Update include/cse/execution.hpp
restenb Jul 12, 2019
782f1ff
Update include/cse.h
restenb Jul 12, 2019
c32f3a1
Update include/cse.h
restenb Jul 12, 2019
fbe6dbc
Update include/cse.h
restenb Jul 12, 2019
f73f772
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
468caf0
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
2d62a07
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
ce12052
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
4c14cae
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
e8a5929
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
60cca9d
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
97a6155
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
7c2b96f
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
417b898
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
d5f28d4
Update include/cse/algorithm.hpp
restenb Jul 12, 2019
a8027de
#212 Fixed const references all the way down
Jul 12, 2019
a666017
Merge remote-tracking branch 'origin/feature/212-provide-overview-of-…
Jul 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions include/cse.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ int cse_slave_get_num_variables(cse_execution* execution, cse_slave_index slave)
*/
int cse_slave_get_variables(cse_execution* execution, cse_slave_index slave, cse_variable_description variables[], size_t numVariables);

/// Returns the number of variables in the execution that currently has an active modifier (all slaves).
int cse_get_num_modified_variables(cse_execution* execution);

/// A struct containing information about a slave which has been added to an execution.
typedef struct
{
Expand All @@ -374,6 +377,17 @@ typedef struct
cse_slave_index index;
} cse_slave_info;

/// A struct containing variable information.
typedef struct
{
/// The index of the slave containing the variable.
cse_slave_index slave_index;
/// The type of the variable.
cse_variable_type type;
/// The index of the variable.
cse_variable_index variable_index;
} cse_variable_id;


/// Returns the number of slaves which have been added to an execution.
size_t cse_execution_get_num_slaves(cse_execution* execution);
Expand Down Expand Up @@ -912,6 +926,19 @@ int cse_scenario_is_running(cse_manipulator* manipulator);
/// Aborts the execution of a running scenario
int cse_scenario_abort(cse_manipulator* manipulator);

/*
restenb marked this conversation as resolved.
Show resolved Hide resolved
* Retrieves a list of the currently modified variables in the simulation.
*
* \param [in] execution
* The execution.
* \param [in] ids
restenb marked this conversation as resolved.
Show resolved Hide resolved
* A list of cse_variable_id structs to contain the variable information.
*
restenb marked this conversation as resolved.
Show resolved Hide resolved
* \returns
* 0 on success and -1 on error.
*/
int cse_get_modified_variables(cse_execution* execution, cse_variable_id ids[], size_t numVariables);

#ifdef __cplusplus
} // extern(C)
#endif
Expand Down
53 changes: 45 additions & 8 deletions include/cse/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,59 @@ class simulator : public observable
* solver doesn't use error estimation, it will just ignore this
* parameter.
*/

restenb marked this conversation as resolved.
Show resolved Hide resolved
virtual boost::fibers::future<void> setup(
time_point startTime,
std::optional<time_point> stopTime,
std::optional<double> relativeTolerance) = 0;

/**
*
* Returns all variable_indexes of real type with an active modifier.
*
* @return modifiedRealIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index> get_modified_real_indexes() const = 0;
restenb marked this conversation as resolved.
Show resolved Hide resolved

/**
*
* Returns all variable_indexes of integer type with an active modifier.
*
* @return modifiedIntegerIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index> get_modified_integer_indexes() const = 0;
restenb marked this conversation as resolved.
Show resolved Hide resolved

/**
*
* Returns all variable_indexes of boolean type with an active modifier.
*
* @return modifiedBooleanIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index> get_modified_boolean_indexes() const = 0;
restenb marked this conversation as resolved.
Show resolved Hide resolved

/**
* Updates the simulator with new input values and makes it calculate
* new output values, without advancing logical time.
*
* This function can be used in the initialisation phase, after `setup()`
* has been called and before the first `do_step()` call. It enables
* iterative initialisation of the system. The purpose could be to
* propagate initial values between simulators and/or bring the system
* to a steady state.
*
* Returns all variable_indexes of string type with an active modifier.
*
* @return modifiedStringIndexes
* Unordered set of all variable_indexes that currently have an active modifier.
*/
virtual const std::unordered_set<variable_index> get_modified_string_indexes() const = 0;
restenb marked this conversation as resolved.
Show resolved Hide resolved

/**
* Updates the simulator with new input values and makes it calculate
restenb marked this conversation as resolved.
Show resolved Hide resolved
* new output values, without advancing logical time.
restenb marked this conversation as resolved.
Show resolved Hide resolved
*
restenb marked this conversation as resolved.
Show resolved Hide resolved
* This function can be used in the initialisation phase, after `setup()`
restenb marked this conversation as resolved.
Show resolved Hide resolved
* has been called and before the first `do_step()` call. It enables
restenb marked this conversation as resolved.
Show resolved Hide resolved
* iterative initialisation of the system. The purpose could be to
restenb marked this conversation as resolved.
Show resolved Hide resolved
* propagate initial values between simulators and/or bring the system
restenb marked this conversation as resolved.
Show resolved Hide resolved
* to a steady state.
restenb marked this conversation as resolved.
Show resolved Hide resolved
*/
restenb marked this conversation as resolved.
Show resolved Hide resolved

restenb marked this conversation as resolved.
Show resolved Hide resolved
virtual boost::fibers::future<void> do_iteration() = 0;

/**
Expand Down
2 changes: 2 additions & 0 deletions include/cse/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class execution
/// Returns the current real time factor target
double get_real_time_factor_target();

/// Returns a map of currently modified variables
std::vector<variable_id> get_modified_variables();
restenb marked this conversation as resolved.
Show resolved Hide resolved
/// Set initial value for a variable of type real. Must be called before simulation is started.
void set_real_initial_value(simulator_index sim, variable_index var, double value);

Expand Down
32 changes: 31 additions & 1 deletion src/c/cse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ int cse_slave_get_num_variables(cse_execution* execution, cse_slave_index slave)
return -1;
}

int cse_get_num_modified_variables(cse_execution* execution)
{
return static_cast<int>(execution->cpp_execution->get_modified_variables().size());
}

cse_variable_variability to_variable_variability(const cse::variable_variability& vv)
{
switch (vv) {
Expand Down Expand Up @@ -316,7 +321,6 @@ int cse_slave_get_variables(cse_execution* execution, cse_slave_index slave, cse
}
}


struct cse_slave_s
{
std::string address;
Expand Down Expand Up @@ -825,6 +829,7 @@ int cse_observer_start_observing(cse_observer* observer, cse_slave_index slave,
return failure;
}
}

int cse_observer_stop_observing(cse_observer* observer, cse_slave_index slave, cse_variable_type type, cse_variable_index index)
{
try {
Expand Down Expand Up @@ -1056,3 +1061,28 @@ int cse_scenario_abort(cse_manipulator* manipulator)
return failure;
}
}

int cse_get_modified_variables(cse_execution* execution, cse_variable_id ids[], size_t numVariables)
{
try {
auto modified_vars = execution->cpp_execution->get_modified_variables();
size_t counter = 0;

if (!modified_vars.empty()) {
for (; counter < std::min(numVariables, modified_vars.size()); counter++) {
ids[counter].slave_index = modified_vars[counter].simulator;
ids[counter].type = to_c_variable_type(modified_vars[counter].type);
ids[counter].variable_index = modified_vars[counter].index;

++counter;
restenb marked this conversation as resolved.
Show resolved Hide resolved
}
}

return static_cast<int>(counter);
} catch (...) {
execution->state = CSE_EXECUTION_ERROR;
execution->error_code = CSE_ERRC_UNSPECIFIED;
handle_current_exception();
return failure;
}
}
5 changes: 5 additions & 0 deletions src/cpp/cse/slave_simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class slave_simulator : public simulator
variable_index index,
std::function<std::string(std::string_view)> modifier) override;

std::unordered_set<variable_index> get_modified_real_indexes() override;
std::unordered_set<variable_index> get_modified_integer_indexes() override;
std::unordered_set<variable_index> get_modified_boolean_indexes() override;
std::unordered_set<variable_index> get_modified_string_indexes() override;

boost::fibers::future<void> setup(
time_point startTime,
std::optional<time_point> stopTime,
Expand Down
45 changes: 44 additions & 1 deletion src/cpp/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "cse/algorithm.hpp"
#include "cse/slave_simulator.hpp"
#include "cse/timer.hpp"
#include <cse/exception.hpp>

#include <boost/functional/hash.hpp>

#include <cse/exception.hpp>
#include <sstream>
#include <unordered_map>
#include <utility>
Expand Down Expand Up @@ -195,6 +195,44 @@ class execution::impl
return timer_.get_real_time_factor_target();
}

std::vector<variable_id> get_modified_variables()
restenb marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<variable_id> modifiedVariables;

auto index = 0;
for (const auto& sim : simulators_) {

const auto& realIndexes = sim->get_modified_real_indexes();
const auto& intIndexes = sim->get_modified_integer_indexes();
const auto& boolIndexes = sim->get_modified_boolean_indexes();
const auto& stringIndexes = sim->get_modified_string_indexes();

for (const auto& varIndex : realIndexes) {
variable_id var = {index, variable_type::real, varIndex};
modifiedVariables.push_back(var);
}

for (const auto& varIndex : intIndexes) {
variable_id var = {index, variable_type::integer, varIndex};
modifiedVariables.push_back(var);
}

for (const auto& varIndex : boolIndexes) {
variable_id var = {index, variable_type::boolean, varIndex};
modifiedVariables.push_back(var);
}

for (const auto& varIndex : stringIndexes) {
variable_id var = {index, variable_type::string, varIndex};
modifiedVariables.push_back(var);
}

index++;
}

return modifiedVariables;
}

void set_real_initial_value(simulator_index sim, variable_index var, double value)
{
if (initialized_) {
Expand Down Expand Up @@ -359,6 +397,11 @@ double execution::get_real_time_factor_target() {
return pimpl_->get_real_time_factor_target();
}

std::vector<variable_id> execution::get_modified_variables()
{
return pimpl_->get_modified_variables();
}

void execution::set_real_initial_value(simulator_index sim, variable_index var, double value)
{
pimpl_->set_real_initial_value(sim, var, value);
Expand Down
Loading