|
| 1 | +# Basic concepts in the Framework |
| 2 | + |
| 3 | +To create a new simulation component, the base class SimulationBase should be used. |
| 4 | +Once instantiated, the Environment will automatically set a simulation index based on the order of instantiation. |
| 5 | +This index defines the execution order of the components in one time step. |
| 6 | +If this should be changed the method SimulationBase\SetIndexManually(int index) can be used. |
| 7 | +Typically this simulation index starts at 0 and is increased by one per instantiated component. |
| 8 | +When setting it manually, even negative values can be used. |
| 9 | + |
| 10 | +=== "Index example - Main " |
| 11 | + |
| 12 | + ``` { .csharp .annotate .select } |
| 13 | + SimulationEnvironment environment = new SimulationEnvironment(); |
| 14 | + DiscreteSolver solver = new DiscreteSolver(environment); |
| 15 | + |
| 16 | + Sine sine1 = new(environment, solver); // (1) |
| 17 | + Sine sine2 = new(environment, solver); // (2) |
| 18 | + |
| 19 | + sine1.SetIndexManually(3) // (3) |
| 20 | + ``` |
| 21 | + { .annotate } |
| 22 | + |
| 23 | + 1. sine1 has the simulation index 0 |
| 24 | + 2. sine2 has the simulation index 1 |
| 25 | + 2. sine1 has the simulation index 3 |
| 26 | + |
| 27 | +In a dynamic calculation, each components "DynamicCalculation" method is executed once per time step. |
| 28 | +When using a discrete solver, each component is added to time step 0. |
| 29 | +Further events have to be added. This are the possible ways to add events: |
| 30 | + |
| 31 | +1. **DiscreteSolver/AddEvent(SimulationBase simulationBase)** |
| 32 | + |
| 33 | + If a component should add a event in the same simulation time the solvers AddEvent can be used. |
| 34 | + |
| 35 | +2. **DiscreteSolver/AddEventAtTime(SimulationBase simulationBase, long simulationTime)** |
| 36 | + |
| 37 | + Similar to the first variant, however, a time can be specified |
| 38 | + |
| 39 | +3. **Connection changed** |
| 40 | + |
| 41 | + If two components are connected and the value changes, an event is automatically added for the connected component. |
| 42 | + To recognize a change the C# Equals method is used on the objects. |
| 43 | + |
| 44 | + |
| 45 | +=== "Simple simulation component - Sine" |
| 46 | + |
| 47 | + ``` { .csharp .annotate .select } |
| 48 | + using Easy2Sim.Connect.Attributes; |
| 49 | + using Easy2Sim.Environment; |
| 50 | + using Easy2Sim.Solvers; |
| 51 | + using Newtonsoft.Json; |
| 52 | + |
| 53 | + namespace StandardLibrary.Mathematical.Source |
| 54 | + { |
| 55 | + public class Sine : SimulationBase // (1) |
| 56 | + { |
| 57 | + [Output] // (2) |
| 58 | + [JsonProperty] // (3) |
| 59 | + public double Output; |
| 60 | + [JsonProperty] |
| 61 | + public double Amplitude; |
| 62 | + [JsonProperty] |
| 63 | + public double Frequency; |
| 64 | + [JsonProperty] |
| 65 | + public double Offset; |
| 66 | + [JsonProperty] |
| 67 | + public int NumberOfSamples; |
| 68 | + public Sine() // (4) |
| 69 | + { |
| 70 | + Amplitude = 1.0; |
| 71 | + Frequency = 10.0; |
| 72 | + Offset = 0; |
| 73 | + Output = 0.0; |
| 74 | + NumberOfSamples = 100; |
| 75 | + } |
| 76 | + public Sine(SimulationEnvironment environment, SolverBase solverBase) : base(environment, solverBase) // (5) |
| 77 | + { |
| 78 | + Amplitude = 1.0; |
| 79 | + Frequency = 10.0; |
| 80 | + Offset = 0; |
| 81 | + Output = 0.0; |
| 82 | + NumberOfSamples = 100; |
| 83 | + } |
| 84 | + public override void DynamicCalculation() // (6) |
| 85 | + { |
| 86 | + if (Solver == null) return; |
| 87 | + |
| 88 | + double timeInSeconds = (double)Solver.BaseModel.SimulationTime / NumberOfSamples; |
| 89 | + double angle = 2 * Math.PI * Frequency * timeInSeconds + Offset; |
| 90 | + double sineValue = Amplitude * Math.Sin(angle); |
| 91 | + |
| 92 | + Output = sineValue; |
| 93 | + } |
| 94 | + public override string SerializeToJson() // (7) |
| 95 | + { |
| 96 | + return JsonConvert.SerializeObject(this); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + ``` |
| 101 | + { .annotate } |
| 102 | + |
| 103 | + 1. Each simulation component needs to implement SimulationBase |
| 104 | + 2. Output defines, that this Property can be connected to a Input of another component |
| 105 | + 3. To allow serialization and deserialization, each component needs to be serializable |
| 106 | + 4. An empty constructor is needed for the serialization framework |
| 107 | + 5. Use this constructor when you create components, it will register the components in the framework |
| 108 | + 6. DynamicCalculation is called in every simulated time step in the Dynamic solver |
| 109 | + 7. The SerializeToJson method needs to be overwritten in order to allow the framework to serialize this component |
| 110 | + |
0 commit comments