/// <summary> /// Set up the simulation. /// </summary> /// <param name="entities">The entities that are included in the simulation.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entities"/> is <c>null</c>.</exception> private void Setup(IEntityCollection entities) { // Validate the entities entities.ThrowIfNull(nameof(entities)); if (entities.Count == 0) { // No entities! Don't stop here, but at least warn the user. SpiceSharpWarning.Warning(this, Properties.Resources.Simulations_NoEntities.FormatString(Name)); } // Create all simulation states CreateStates(); // Create all entity behaviors (using the created simulation states) CreateBehaviors(entities); }
/// <summary> /// Initializes a new instance of the <see cref="SubcircuitDefinition"/> class. /// </summary> /// <param name="entities">The entities.</param> /// <param name="pins">The pins.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entities"/> is <c>null</c>.</exception> public SubcircuitDefinition(IEntityCollection entities, params string[] pins) { Entities = entities.ThrowIfNull(nameof(entities)); if (pins != null) { _pins = new string[pins.Length]; for (var i = 0; i < pins.Length; i++) { _pins[i] = pins[i].ThrowIfNull("node {0}".FormatString(i + 1)); } } else { _pins = Array <string> .Empty(); } }
/// <summary> /// Creates all behaviors for the simulation. /// </summary> /// <param name="entities">The entities.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entities"/> is <c>null</c>.</exception> protected virtual void CreateBehaviors(IEntityCollection entities) { entities.ThrowIfNull(nameof(entities)); EntityBehaviors = new BehaviorContainerCollection(entities.Comparer); // Automatically create the behaviors of entities that need priority void BehaviorsNotFound(object sender, BehaviorsNotFoundEventArgs args) { if (entities.TryGetEntity(args.Name, out var entity)) { entity.CreateBehaviors(this); if (EntityBehaviors.TryGetBehaviors(entity.Name, out var container)) { args.Behaviors = container; } } } EntityBehaviors.BehaviorsNotFound += BehaviorsNotFound; // Create the behaviors Statistics.BehaviorCreationTime.Start(); try { foreach (var entity in entities) { if (!EntityBehaviors.Contains(entity.Name)) { entity.CreateBehaviors(this); } } } finally { Statistics.BehaviorCreationTime.Stop(); } EntityBehaviors.BehaviorsNotFound -= BehaviorsNotFound; }
/// <summary> /// Initializes a new instance of the <see cref="Circuit"/> class. /// </summary> /// <param name="entities">The entities.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="entities"/> is <c>null</c>.</exception> public Circuit(IEntityCollection entities) { _entities = entities.ThrowIfNull(nameof(entities)); }
/// <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; }