Skip to content

Commit fc647d5

Browse files
committed
Update documentation and add a sample
1 parent 94f379e commit fc647d5

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

docs/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The documentation covers the following aspects:
77
+ [Processes](#processes)
88
+ [Resources](#resources)
99
+ [Samples](#samples)
10+
+ [Environments](#environments)
1011
* [Monitoring](#monitoring)
1112
+ [Reports](#reports)
1213

@@ -82,6 +83,23 @@ A *Container* contains a continuous amount of some substance. Again, the substan
8283

8384
In any case, it is simple to extend the standard resources given that the code is open source. The classes are of moderate complexity, e.g. the *Resource* class is described in about 200 lines of code.
8485

86+
### Environments
87+
88+
Sim# offers several different simulation environments. The default environment is called `Simulation` and is the fastest. There is also a `ThreadSafeSimulation` which synchronises access to the event queue. This is useful if there is a separate thread that may create processes or which may fire events. It should be noted that the simulation itself is not multi-threaded. At any time there may only be a single process that is active. The "simulation thread" is that thread that called the `Run()` method.
89+
90+
#### Realtime Simulation
91+
92+
Starting with Sim# 3.3 we introduce a `PseudoRealtimeSimulation` environment that derives from `ThreadSafeSimulation`. It will put the simulation thread to sleep for the duration given by the next event in the queue, i.e., a delay of 5 seconds will put the simulation to sleep 5 seconds. An actual realtime guarantee is however not made. In fact, the overhead of the environment, for instance waking up processes, adding and removing from the event queue, and so on are not accounted for. Thus, this may lag behind the actual wall clock time.
93+
94+
However, this environment allows *switching between real and virtual time* and also accepts a realtime scale so that it can be used to perform faster and slower than the actual real time by a certain factor. It offers to method to control the speed:
95+
96+
* `SetVirtualTime()` will process items as fast as `ThreadSafeSimulation`
97+
* `SetRealtime(double scale)` will switch the simulation into realtime mode.
98+
99+
These methods are thread safe and may be called from different threads.
100+
101+
Obtaining the current simulation time using `Now` in realtime mode will account for the duration that the simulation is sleeping. Thus, calling this property from a different thread will always receive the actual simulation time, i.e., the time of the last event plus the elapsed time.
102+
85103
### Samples
86104

87105
Processes that interact with common resources may create highly dynamic behavior which may not be analytically tractable and thus have to be simulated. Sim# includes a number of samples that, while being easy to understand and simple, show how to model certain processes such as preemption, interruption, handover of resource requests and more. A short summary of the provided samples together with the highlights are given in the following:
@@ -134,6 +152,12 @@ This model describes a simple producer-consumer situation. It shows how processe
134152

135153
These model describe a two-step production. The first step may be blocked by the second. The models should show how one process may obtain a resource, but another processes releases that resource.
136154

155+
##### [PseudoRealTimeSample](../src/Samples/PseudoRealTimeSample.cs)
156+
157+
* Running a process that awaits some timeout in realtime
158+
159+
This model describes a single process that will define a timeout which is awaited in realtime.
160+
137161
## Monitoring
138162

139163
Monitoring has been introduced with Sim# 3.2. Instead of following the SimPy approach which is difficult to translate to .NET. The implementation in Sim# is more akin to [Salabim](https://www.salabim.org).

src/Benchmark/Benchmark.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="CommandLineParser" Version="2.3.0" />
17+
<PackageReference Include="CommandLineParser" Version="2.6.0" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

src/Samples/PseudoRealtimeSample.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#region License Information
2+
/*
3+
* This file is part of SimSharp which is licensed under the MIT license.
4+
* See the LICENSE file in the project root for more information.
5+
*/
6+
#endregion
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Diagnostics;
11+
12+
namespace SimSharp.Samples {
13+
public class PseudoRealtimeSample {
14+
15+
public IEnumerable<Event> Process(PseudoRealtimeSimulation sim) {
16+
var then = sim.Now;
17+
var sw = Stopwatch.StartNew();
18+
yield return sim.Timeout(TimeSpan.FromSeconds(1));
19+
sw.Stop();
20+
var now = sim.Now;
21+
Console.WriteLine($"Elapsed wall clock time {sw.Elapsed.TotalSeconds}s, elapsed simulation time {(now - then).TotalSeconds}s.");
22+
}
23+
24+
public void Simulate(int rseed = 42) {
25+
// Setup and start the simulation
26+
var env = new PseudoRealtimeSimulation(rseed);
27+
env.Log("== Pseudo-Realtime Sample ==");
28+
29+
env.Process(Process(env));
30+
31+
env.Run(TimeSpan.FromSeconds(2));
32+
}
33+
}
34+
}

src/Samples/RunAllSamples.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public static void Main(string[] args) {
2828
new KanbanControl().Simulate();
2929
Console.WriteLine();
3030
new MM1Queueing().Simulate();
31+
Console.WriteLine();
32+
new PseudoRealtimeSample().Simulate();
3133
}
3234
}
3335
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
26+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
2727
<PackageReference Include="xunit" Version="2.4.1" />
2828
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
2929
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)