/// <summary> /// Runs the simulation on the specified circuit. /// </summary> /// <param name="circuit">The circuit to simulate.</param> /// <exception cref="ArgumentNullException">circuit</exception> /// <exception cref="CircuitException">{0}: No circuit nodes for simulation".FormatString(Name)</exception> public virtual void Run(EntityCollection entities) { entities.ThrowIfNull(nameof(entities)); // Setup the simulation OnBeforeSetup(EventArgs.Empty); SimulationStatistics.SetupTime.Start(); Status = Statuses.Setup; Setup(entities); SimulationStatistics.SetupTime.Stop(); OnAfterSetup(EventArgs.Empty); // Check that at least something is simulated if (Variables.Count < 1) { throw new CircuitException("{0}: No circuit nodes for simulation".FormatString(Name)); } // Execute the simulation Status = Statuses.Running; var beforeArgs = new BeforeExecuteEventArgs(false); var afterArgs = new AfterExecuteEventArgs(); do { // Before execution OnBeforeExecute(beforeArgs); // Execute simulation SimulationStatistics.ExecutionTime.Start(); Execute(); SimulationStatistics.ExecutionTime.Stop(); // Reset afterArgs.Repeat = false; OnAfterExecute(afterArgs); // We're going to repeat the simulation, change the event arguments if (afterArgs.Repeat) { beforeArgs = new BeforeExecuteEventArgs(true); } } while (afterArgs.Repeat); // Clean up the circuit OnBeforeUnsetup(EventArgs.Empty); SimulationStatistics.UnsetupTime.Start(); Status = Statuses.Unsetup; Unsetup(); SimulationStatistics.UnsetupTime.Stop(); OnAfterUnsetup(EventArgs.Empty); Status = Statuses.None; }
/// <inheritdoc/> public virtual void Rerun() { // Execute the simulation Status = SimulationStatus.Running; var beforeArgs = new BeforeExecuteEventArgs(false); var afterArgs = new AfterExecuteEventArgs(); do { // Before execution OnBeforeExecute(beforeArgs); // Execute simulation Statistics.ExecutionTime.Start(); try { Execute(); } finally { Statistics.ExecutionTime.Stop(); } // Reset afterArgs.Repeat = false; OnAfterExecute(afterArgs); // We're going to repeat the simulation, change the event arguments if (afterArgs.Repeat) { beforeArgs = new BeforeExecuteEventArgs(true); } } while (afterArgs.Repeat); // Clean up the circuit OnBeforeUnsetup(EventArgs.Empty); Statistics.FinishTime.Start(); try { Status = SimulationStatus.Unsetup; Finish(); } finally { Statistics.FinishTime.Stop(); } OnAfterUnsetup(EventArgs.Empty); Status = SimulationStatus.None; }
/// <summary> /// Runs the simulation on the specified circuit. /// </summary> /// <param name="circuit">The circuit to simulate.</param> /// <param name="stepByStep">Do simulation step by step via DoTick() (don't Finalize)</param> /// <exception cref="ArgumentNullException">circuit</exception> /// <exception cref="CircuitException">{0}: No circuit nodes for simulation".FormatString(Name)</exception> public virtual void Run(EntityCollection entities, bool stepByStep) { if (entities == null) { throw new ArgumentNullException(nameof(entities)); } StepByStepSimulation = stepByStep; // Setup the simulation OnBeforeSetup(EventArgs.Empty); Status = Statuses.Setup; Setup(entities); OnAfterSetup(EventArgs.Empty); // Check that at least something is simulated if (Variables.Count < 1) { throw new CircuitException("{0}: No circuit nodes for simulation".FormatString(Name)); } // Execute the simulation Status = Statuses.Running; var beforeArgs = new BeforeExecuteEventArgs(false); var afterArgs = new AfterExecuteEventArgs(); do { // Before execution OnBeforeExecute(beforeArgs); // Execute simulation Execute(); // Reset afterArgs.Repeat = false; OnAfterExecute(afterArgs); // We're going to repeat the simulation, change the event arguments if (afterArgs.Repeat) { beforeArgs = new BeforeExecuteEventArgs(true); } } while (afterArgs.Repeat); if (!StepByStepSimulation) { FinalizeSimulation(); } }
/// <summary> /// Raises the <see cref="E:BeforeExecute" /> event. /// </summary> /// <param name="args">The <see cref="BeforeExecuteEventArgs"/> instance containing the event data.</param> protected virtual void OnBeforeExecute(BeforeExecuteEventArgs args) => BeforeExecute?.Invoke(this, args);
/// <inheritdoc/> public virtual void Run(IEntityCollection entities) { entities.ThrowIfNull(nameof(entities)); // Setup the simulation OnBeforeSetup(EventArgs.Empty); Statistics.SetupTime.Start(); try { Status = SimulationStatus.Setup; Setup(entities); } finally { Statistics.SetupTime.Stop(); } OnAfterSetup(EventArgs.Empty); // Validate the input OnBeforeValidation(EventArgs.Empty); Statistics.ValidationTime.Start(); try { Status = SimulationStatus.Validation; Validate(entities); } finally { Statistics.ValidationTime.Stop(); } OnAfterValidation(EventArgs.Empty); // Execute the simulation Status = SimulationStatus.Running; var beforeArgs = new BeforeExecuteEventArgs(false); var afterArgs = new AfterExecuteEventArgs(); do { // Before execution OnBeforeExecute(beforeArgs); // Execute simulation Statistics.ExecutionTime.Start(); try { Execute(); } finally { Statistics.ExecutionTime.Stop(); } // Reset afterArgs.Repeat = false; OnAfterExecute(afterArgs); // We're going to repeat the simulation, change the event arguments if (afterArgs.Repeat) { beforeArgs = new BeforeExecuteEventArgs(true); } } while (afterArgs.Repeat); // Clean up the circuit OnBeforeUnsetup(EventArgs.Empty); Statistics.FinishTime.Start(); try { Status = SimulationStatus.Unsetup; Finish(); } finally { Statistics.FinishTime.Stop(); } OnAfterUnsetup(EventArgs.Empty); Status = SimulationStatus.None; }