Skip to content

Commit c0ea946

Browse files
Change TimeAdvanceMode::ByMinimalDuration to trigger SimTasks with timestamps of all other sync. participants; Add Integration Tests; Remove automatic creation of TimeSynService in SimTestHarness
Signed-off-by: Konrad Breitsprecher <Konrad.Breitsprecher@vector.com>
1 parent 7a4364e commit c0ea946

12 files changed

Lines changed: 428 additions & 49 deletions

File tree

Demos/api/Orchestration/DynSimStep.cpp

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,74 @@ std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp)
1717

1818
int main(int argc, char** argv)
1919
{
20-
if (argc != 2)
20+
if (argc != 5 && argc != 6)
2121
{
22-
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] << " <ParticipantName>" << std::endl;
22+
std::cerr << "Wrong number of arguments! Start demo with: " << argv[0] <<
23+
" <ParticipantName> "
24+
"<StepSize> "
25+
"[-A (Autonomous) | -C (Coordinated)] "
26+
"[-M (ByMinimalDuration) | -D (ByOwnDuration)] "
27+
"[-R (Optional; Randomize StepSize (1ms to 10ms) with 10% probability every step)]" << std::endl;
2328
return -1;
2429
}
30+
// Arg 1: Participant Name
2531
std::string participantName(argv[1]);
2632

33+
// Arg 2: Step Size
34+
auto stepSize = std::chrono::milliseconds(std::stoi(argv[2]));
35+
std::cout << "Starting with stepSize=" << stepSize << std::endl;
36+
37+
// Arg 3: Operation Mode
38+
auto operationMode = SilKit::Services::Orchestration::OperationMode::Coordinated;
39+
if (std::string(argv[3]) == "-A")
40+
{
41+
std::cout << "Using OperationMode::Autonomous" << std::endl;
42+
operationMode = SilKit::Services::Orchestration::OperationMode::Autonomous;
43+
}
44+
else if (std::string(argv[3]) == "-C")
45+
{
46+
std::cout << "Using OperationMode::Coordinated" << std::endl;
47+
}
48+
else
49+
{
50+
std::cerr << "Unknown third argument '" << argv[3] << "'. Did you mean '-A' for autonomous mode or '-C' for coordinated mode?" << std::endl;
51+
return -1;
52+
}
53+
54+
// Arg 4: Time Advance Mode
55+
auto timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration;
56+
if (std::string(argv[4]) == "-M")
57+
{
58+
std::cout << "Using TimeAdvanceMode::ByMinimalDuration" << std::endl;
59+
}
60+
else if (std::string(argv[4]) == "-D")
61+
{
62+
timeAdvanceMode = SilKit::Services::Orchestration::TimeAdvanceMode::ByOwnDuration;
63+
std::cout << "Using TimeAdvanceMode::ByOwnDuration" << std::endl;
64+
}
65+
else
66+
{
67+
std::cerr << "Unknown argument '" << argv[4]
68+
<< "'. Did you mean '-M' for TimeAdvanceMode::ByMinimalDuration or '-D' for TimeAdvanceMode::ByOwnDuration?" << std::endl;
69+
return -1;
70+
}
71+
72+
// Arg 5: Optional Randomize Step Size
73+
bool randomizeStepSize = false;
74+
if (argc == 6)
75+
{
76+
if (std::string(argv[5]) == "-R")
77+
{
78+
randomizeStepSize = true;
79+
std::cout << "Randomizing step size every 10 steps." << std::endl << std::endl;
80+
}
81+
else
82+
{
83+
std::cerr << "Unknown argument '" << argv[5] << "'. Did you mean '-R' to randomize the step size every 10 steps?" << std::endl;
84+
return -1;
85+
}
86+
}
87+
2788
try
2889
{
2990
// Setup participant, lifecycle, time synchronization and logging.
@@ -34,12 +95,10 @@ int main(int argc, char** argv)
3495
auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri);
3596
auto logger = participant->GetLogger();
3697

37-
auto* lifecycleService =
38-
participant->CreateLifecycleService({SilKit::Services::Orchestration::OperationMode::Coordinated});
98+
auto* lifecycleService = participant->CreateLifecycleService({operationMode});
3999

40-
auto* timeSyncService = lifecycleService->CreateTimeSyncService(SilKit::Services::Orchestration::TimeAdvanceMode::ByMinimalDuration);
100+
auto* timeSyncService = lifecycleService->CreateTimeSyncService(timeAdvanceMode);
41101

42-
const auto stepSize = 10ms;
43102
static int stepCounter = 0;
44103
std::random_device rd;
45104
std::mt19937 rng(rd());
@@ -50,7 +109,8 @@ int main(int argc, char** argv)
50109

51110

52111
timeSyncService->SetSimulationStepHandler(
53-
[logger, timeSyncService, participantName, bounded_rand](std::chrono::nanoseconds now,
112+
[randomizeStepSize, logger, timeSyncService, participantName, bounded_rand](
113+
std::chrono::nanoseconds now,
54114
std::chrono::nanoseconds duration) {
55115
// The invocation of this handler marks the beginning of a simulation step.
56116
{
@@ -59,7 +119,7 @@ int main(int argc, char** argv)
59119
logger->Info(ss.str());
60120
}
61121

62-
if (bounded_rand(10) == 1)// && participantName == "P1")
122+
if (randomizeStepSize && bounded_rand(10) == 1)
63123
{
64124
auto rndStepDuration = bounded_rand(10);
65125
timeSyncService->SetStepDuration(std::chrono::milliseconds(rndStepDuration));
@@ -69,12 +129,7 @@ int main(int argc, char** argv)
69129
}
70130

71131
std::this_thread::sleep_for(500ms);
72-
// All messages sent here are guaranteed to arrive at other participants before their next simulation step is called.
73-
// So here, we can rely on having received all messages from the past (< now).
74-
// Note that this guarantee only holds for messages sent within a simulation step,
75-
// not for messages send outside of this handler (e.g. directly in a reception handler).
76132

77-
// Returning from the handler marks the end of a simulation step.
78133
}, stepSize);
79134

80135
auto finalStateFuture = lifecycleService->StartLifecycle();

Demos/api/Orchestration/SimStep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int main(int argc, char** argv)
3838

3939
auto* timeSyncService = lifecycleService->CreateTimeSyncService();
4040

41-
const auto stepSize = 2ms;
41+
const auto stepSize = 5ms;
4242

4343
timeSyncService->SetSimulationStepHandler(
4444
[logger](std::chrono::nanoseconds now, std::chrono::nanoseconds duration) {

SilKit/IntegrationTests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ add_silkit_test_to_executable(SilKitIntegrationTests
114114
SOURCES ITest_SimTask.cpp
115115
)
116116

117+
add_silkit_test_to_executable(SilKitIntegrationTests
118+
SOURCES ITest_DynStepSizes.cpp
119+
)
120+
121+
117122
add_silkit_test_to_executable(SilKitFunctionalTests
118123
SOURCES FTest_WallClockCoupling.cpp
119124
)

SilKit/IntegrationTests/Hourglass/Test_HourglassOrchestration.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ TEST_F(Test_HourglassOrchestration, SilKit_TimeSyncService_SetStepDuration)
447447
timeSyncService.SetStepDuration(stepDuration);
448448
}
449449

450-
451450
TEST_F(Test_HourglassOrchestration, SilKit_Experimental_TimeSyncService_AddOtherSimulationStepsCompletedHandler)
452451
{
453452
using testing::_;

SilKit/IntegrationTests/ITest_AsyncSimTask.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,12 @@ auto MakeCompletionThread(SimParticipant* p, ParticipantData* d) -> std::thread
302302

303303
TEST(ITest_AsyncSimTask, test_async_simtask_other_simulation_steps_completed_handler)
304304
{
305-
SimTestHarness testHarness({"A", "B", "C"}, "silkit://localhost:0");
305+
SimTestHarness testHarness({"A", "B", "C", "D"}, "silkit://localhost:0");
306306

307307
const auto a = testHarness.GetParticipant("A");
308308
const auto b = testHarness.GetParticipant("B");
309309
const auto c = testHarness.GetParticipant("C");
310+
const auto d = testHarness.GetParticipant("D");
310311

311312
ParticipantData ad, bd, cd;
312313

@@ -316,6 +317,8 @@ TEST(ITest_AsyncSimTask, test_async_simtask_other_simulation_steps_completed_han
316317
b->GetOrCreateLifecycleService()->SetStopHandler([&bd] { bd.running = false; });
317318
c->GetOrCreateLifecycleService()->SetStopHandler([&cd] { cd.running = false; });
318319

320+
d->GetOrCreateTimeSyncService()->SetSimulationStepHandler([](auto, auto) {}, 1ms);
321+
319322
const auto aLifecycleService = a->GetOrCreateLifecycleService();
320323

321324
a->GetOrCreateTimeSyncService()->SetSimulationStepHandlerAsync([aLifecycleService, &ad](auto now, auto) {

0 commit comments

Comments
 (0)