Skip to content

Commit

Permalink
Wait flag endpoint update (#1979) (#1981)
Browse files Browse the repository at this point in the history
* Wait flag endpoint update (#1979)

* start testing and working on the wait for current time flag interaction with the forwarding time coordinator which mainly impacts endpoints

* fix more test cases

* fix blocking test

* fix failing tests

* Automated formatting of repo files (#1980)

Co-authored-by: Philip Top <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix clang-tidy issues

* Automated formatting of repo files (#1982)

Co-authored-by: Philip Top <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix duplicate https://

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
phlptp and github-actions[bot] authored Jun 30, 2021
1 parent c8467b0 commit 87f1198
Show file tree
Hide file tree
Showing 18 changed files with 569 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ For example, let's look at our JSON config file of the Battery federate from the

```

We can see from this config file that we need to find API method to assign the `name`, `loglevel`, `coreType`, `period`, `uninterruptible`, `terminate_on_error`, `wait_for_current_time_update`, and `pub`/`subs`. In this example, we will be using the [PyHELICS API methods](https://https://python.helics.org/api/capi-py). This section will discuss how to translate JSON config files to API methods, how to configure the federate with these API calls in the co-simulation, and how to dynamically register publications and subscriptions with other federates.
We can see from this config file that we need to find API method to assign the `name`, `loglevel`, `coreType`, `period`, `uninterruptible`, `terminate_on_error`, `wait_for_current_time_update`, and `pub`/`subs`. In this example, we will be using the [PyHELICS API methods](https://python.helics.org/api/capi-py). This section will discuss how to translate JSON config files to API methods, how to configure the federate with these API calls in the co-simulation, and how to dynamically register publications and subscriptions with other federates.

### Translation from JSON to PyHELICS API methods

Expand Down Expand Up @@ -140,7 +140,7 @@ h.helicsFederateInfoSetFlagOption(fedinfo, h.HELICS_FLAG_WAIT_FOR_CURRENT_TIME_U
If you find yourself wanting to set additional properties, there are a handful of places you can look:

- [C++ source code](https://docs.helics.org/en/latest/doxygen/helics__enums_8h_source.html): Do a string search for the JSON property. This can provide clarity into which `enum` to use from the API.
- [PyHELICS API methods](https://https://python.helics.org/api/capi-py): API methods specific to PyHELICS, with suggestions for making the calls pythonic.
- [PyHELICS API methods](https://python.helics.org/api/capi-py): API methods specific to PyHELICS, with suggestions for making the calls pythonic.
- [Configuration Options Reference](../../../references/configuration_options_reference.html): API calls for C++, C, Python, and Julia

### Federate Integration with API calls
Expand Down
2 changes: 0 additions & 2 deletions src/helics/application_api/Federate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,8 @@ IterationResult Federate::enterExecutingMode(IterationRequest iterate)
initializeToExecuteStateTransition(res);
break;
case IterationResult::ERROR_RESULT:
// LCOV_EXCL_START
currentMode = Modes::ERROR_STATE;
break;
// LCOV_EXCL_STOP
case IterationResult::HALTED:
currentMode = Modes::FINISHED;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/helics/core/ActionMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,9 @@ static constexpr frozen::unordered_map<int, frozen::string, 8> errorStrings = {
{max_broker_count_exceeded, "the maximum number of brokers or cores has been reached"},
{mismatch_broker_key_error_code, "Broker key does not match"}};

const char* commandErrorString(int errorcode)
const char* commandErrorString(int errorCode)
{
const auto* res = errorStrings.find(errorcode);
const auto* res = errorStrings.find(errorCode);
return (res != errorStrings.end()) ? res->second.data() : static_cast<const char*>(unknownStr);
}

Expand Down
7 changes: 4 additions & 3 deletions src/helics/core/ActionMessageDefintions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,14 @@ enum cmd_error_codes : int {
duplicate_broker_name_error_code = 7,
mismatch_broker_key_error_code = 9,
max_federate_count_exceeded = 11,
max_broker_count_exceeded = 13
max_broker_count_exceeded = 13,
multiple_wait_for_current_time_flags = 15
};

/** return a string associated with a particular error code
@param[in,out] errorcode The error to get the string for
@param[in,out] errorCode The error to get the string for
@return a pointer to string with the name
*/
const char* commandErrorString(int errorcode);
const char* commandErrorString(int errorCode);

} // namespace helics
14 changes: 6 additions & 8 deletions src/helics/core/CommonCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,6 @@ IterationResult CommonCore::enterExecutingMode(LocalFederateId federateID, Itera
setActionFlag(exec, indicator_flag);
addActionMessage(exec);

// TODO(PT): check for error conditions?
return fed->enterExecutingMode(iterate, false);
}

Expand Down Expand Up @@ -4640,11 +4639,10 @@ void CommonCore::setInterfaceTag(helics::InterfaceHandle handle,
}
}

const std::string& CommonCore::getFederateTag(LocalFederateId federateID,
const std::string& tag) const
const std::string& CommonCore::getFederateTag(LocalFederateId fid, const std::string& tag) const
{
auto* fed = getFederateAt(federateID);
if (federateID == gLocalCoreId) {
auto* fed = getFederateAt(fid);
if (fid == gLocalCoreId) {
static thread_local std::string val;
val = const_cast<CommonCore*>(this)->query(
"core",
Expand All @@ -4660,7 +4658,7 @@ const std::string& CommonCore::getFederateTag(LocalFederateId federateID,
return fed->getTag(tag);
}

void CommonCore::setFederateTag(LocalFederateId federateID,
void CommonCore::setFederateTag(LocalFederateId fid,
const std::string& tag,
const std::string& value)
{
Expand All @@ -4669,7 +4667,7 @@ void CommonCore::setFederateTag(LocalFederateId federateID,
throw InvalidParameter("tag cannot be an empty string for setFederateTag");
}

if (federateID == gLocalCoreId) {
if (fid == gLocalCoreId) {
ActionMessage tagcmd(CMD_CORE_TAG);
tagcmd.source_id = getGlobalId();
tagcmd.dest_id = tagcmd.source_id;
Expand All @@ -4678,7 +4676,7 @@ void CommonCore::setFederateTag(LocalFederateId federateID,
return;
}

auto* fed = getFederateAt(federateID);
auto* fed = getFederateAt(fid);
if (fed == nullptr) {
throw(InvalidIdentifier("federateID not valid (setFlag)"));
}
Expand Down
19 changes: 18 additions & 1 deletion src/helics/core/CoreBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,17 @@ void CoreBroker::processCommand(ActionMessage&& command)
}
} else if (command.dest_id == global_broker_id_local) {
if (timeCoord->processTimeMessage(command)) {
timeCoord->updateTimeFactors();
if (enteredExecutionMode) {
timeCoord->updateTimeFactors();
} else {
auto res = timeCoord->checkExecEntry();
if (res == MessageProcessingResult::NEXT_STEP) {
enteredExecutionMode = true;
LOG_TIMING(global_broker_id_local,
getIdentifier(),
"entering Exec Mode");
}
}
}
} else {
routeMessage(command);
Expand Down Expand Up @@ -2257,6 +2267,9 @@ void CoreBroker::processError(ActionMessage& command)
command.payload.to_string());
if (command.source_id == global_broker_id_local) {
setBrokerState(broker_state_t::errored);
if (command.action() == CMD_GLOBAL_ERROR) {
setErrorState(command.messageID, command.payload.to_string());
}
broadcast(command);
if (!isRootc) {
command.setAction(CMD_LOCAL_ERROR);
Expand All @@ -2267,7 +2280,11 @@ void CoreBroker::processError(ActionMessage& command)

if (command.source_id == parent_broker_id || command.source_id == root_broker_id) {
setBrokerState(broker_state_t::errored);
if (command.action() == CMD_GLOBAL_ERROR) {
setErrorState(command.messageID, command.payload.to_string());
}
broadcast(command);
return;
}

auto* brk = getBrokerById(GlobalBrokerId(command.source_id));
Expand Down
33 changes: 19 additions & 14 deletions src/helics/core/FederateState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,22 +474,27 @@ IterationResult FederateState::enterExecutingMode(IterationRequest iterate, bool
time_granted = initializationTime;
allowed_send_time = initializationTime;
}
switch (iterate) {
case IterationRequest::FORCE_ITERATION:
fillEventVectorNextIteration(time_granted);
break;
case IterationRequest::ITERATE_IF_NEEDED:
if (ret == MessageProcessingResult::NEXT_STEP) {
fillEventVectorUpTo(time_granted);
} else {
if (ret != MessageProcessingResult::ERROR_RESULT) {
switch (iterate) {
case IterationRequest::FORCE_ITERATION:
fillEventVectorNextIteration(time_granted);
}
break;
case IterationRequest::NO_ITERATIONS:
fillEventVectorUpTo(time_granted);
break;
break;
case IterationRequest::ITERATE_IF_NEEDED:
if (ret == MessageProcessingResult::NEXT_STEP) {
fillEventVectorUpTo(time_granted);
} else {
fillEventVectorNextIteration(time_granted);
}
break;
case IterationRequest::NO_ITERATIONS:
if (wait_for_current_time) {
fillEventVectorInclusive(time_granted);
} else {
fillEventVectorUpTo(time_granted);
}
break;
}
}

unlock();
#ifndef HELICS_DISABLE_ASIO
if ((realtime) && (ret == MessageProcessingResult::NEXT_STEP)) {
Expand Down
Loading

0 comments on commit 87f1198

Please sign in to comment.