Files
BWRSimlator/BWR.core/Simulator.cs

141 lines
5.0 KiB
C#

// MyBWRSimulator.Core/Simulation/Simulator.cs
namespace MyBWRSimulator.Core.Simulation
{
using MyBWRSimulator.Core.Components.Abstract;
using MyBWRSimulator.Core.Data;
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// The orchestrator of the simulation. Manages components, runs the simulation loop, and provides data.
/// </summary>
public class Simulator
{
private List<Component> _components;
private double _simulationTime;
private bool _isPaused;
public Simulator()
{
_components = new List<Component>();
_simulationTime = 0.0;
_isPaused = false;
}
/// <summary>
/// Adds a component to the simulation.
/// </summary>
/// <param name="component">The component to add.</param>
public void AddComponent(Component component)
{
if (component != null && !_components.Contains(component))
{
_components.Add(component);
_components = _components.OrderBy(c => c.UpdatePriority).ToList(); // Re-sort after adding
component.Initialize();
Console.WriteLine($"Component '{component.Name}' ({component.ID}) added.");
}
}
/// <summary>
/// Removes a component from the simulation.
/// </summary>
/// <param name="component">The component to remove.</param>
public void RemoveComponent(Component component)
{
if (component != null && _components.Remove(component))
{
_components = _components.OrderBy(c => c.UpdatePriority).ToList(); // Re-sort after removing
component.Shutdown();
Console.WriteLine($"Component '{component.Name}' ({component.ID}) removed.");
}
}
/// <summary>
/// Loads components and connections from a configuration file.
/// </summary>
/// <param name="filePath">The path to the configuration JSON file.</param>
public void LoadPlantConfiguration(string filePath)
{
ConfigurationLoader.LoadConfiguration(filePath, out _components);
_components = _components.OrderBy(c => c.UpdatePriority).ToList(); // Ensure sorted after load
Console.WriteLine($"Loaded { _components.Count} components from configuration.");
}
/// <summary>
/// Runs the simulation for a specified duration with a given time step.
/// </summary>
/// <param name="duration">Total simulation duration (e.g., in seconds).</param>
/// <param name="timeStep">Time step for each update iteration (e.g., in seconds).</param>
public void RunSimulation(double duration, double timeStep)
{
Console.WriteLine($"Starting simulation for {duration} seconds with a time step of {timeStep} seconds...");
_isPaused = false;
while (_simulationTime < duration && !_isPaused)
{
Console.WriteLine($"\n--- Simulation Time: {_simulationTime:F2} s ---");
foreach (var component in _components)
{
component.Update(timeStep);
}
_simulationTime += timeStep;
// For console output, add a small delay to see updates
System.Threading.Thread.Sleep(100);
}
if (_simulationTime >= duration)
{
Console.WriteLine("\nSimulation finished.");
}
else if (_isPaused)
{
Console.WriteLine("\nSimulation paused.");
}
// Call shutdown for all components at the end of the simulation
foreach (var component in _components)
{
component.Shutdown();
}
}
/// <summary>
/// Pauses the simulation.
/// </summary>
public void PauseSimulation()
{
_isPaused = true;
}
/// <summary>
/// Resumes the simulation.
/// </summary>
public void ResumeSimulation()
{
_isPaused = false;
}
/// <summary>
/// Gets current simulation data from all components.
/// (Placeholder - you'll define the structure of this data more concretely later)
/// </summary>
/// <returns>A dictionary containing simulation data.</returns>
public Dictionary<string, object> GetSimulationData()
{
var data = new Dictionary<string, object>();
data["SimulationTime"] = _simulationTime;
data["ComponentStates"] = _components.Select(c => new
{
c.ID,
c.Name
// Add specific properties for each component type here
// Example for Pump: (c as Pump)?.PumpSpeed
// Example for FluidTank: (c as FluidTank)?.CurrentVolume
}).ToList();
return data;
}
}
}