Adding initial Project to git

This commit is contained in:
2025-12-29 19:58:26 +00:00
parent 15f9dce88c
commit b54c9bbb1c
24 changed files with 1396 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
namespace MyBWRSimulator.Core.Components.Abstract
{
using MyBWRSimulator.Core.Interfaces;
using MyBWRSimulator.Core.Ports;
using System.Collections.Generic;
using System.Linq; // Needed for .ToList()
/// <summary>
/// Abstract base class for all simulated physical components.
/// Implements ISimulatable, IFluidConnectable, and IThermalConnectable.
/// </summary>
public abstract class Component : ISimulatable, IFluidConnectable, IThermalConnectable
{
/// <summary>
/// Unique identifier for the component.
/// </summary>
public string ID { get; protected set; }
/// <summary>
/// Human-readable name of the component.
/// </summary>
public string Name { get; protected set; }
/// <summary>
/// Detailed description of the component.
/// </summary>
public string Description { get; protected set; }
/// <summary>
/// An integer value indicating the order in which components should be updated.
/// Lower numbers mean higher priority (updated first).
/// </summary>
public int UpdatePriority { get; protected set; }
/// <summary>
/// Indicates if the component is receiving sufficient Thermal power.
/// </summary>
public bool IsPowered { get; protected set; }
/// <summary>
/// Collection of fluid ports belonging to this component, accessible by their ID.
/// </summary>
protected Dictionary<string, FluidPort> FluidPorts { get; set; } = new Dictionary<string, FluidPort>();
/// <summary>
/// Collection of Thermal ports belonging to this component, accessible by their ID.
/// </summary>
protected Dictionary<string, ThermalPort> ThermalPorts { get; set; } = new Dictionary<string, ThermalPort>();
/// <summary>
/// Constructor for the Component base class.
/// </summary>
/// <param name="id">Unique identifier.</param>
/// <param name="name">Human-readable name.</param>
/// <param name="description">Description of the component.</param>
/// <param name="updatePriority">Priority for simulation updates.</param>
protected Component(string id, string name, string description, int updatePriority)
{
ID = id;
Name = name;
Description = description;
UpdatePriority = updatePriority;
}
/// <summary>
/// Initializes the component. Called once when the simulation starts or component is added.
/// </summary>
public virtual void Initialize()
{
Console.WriteLine($"Initializing {Name} ({ID})...");
// Placeholder for component-specific initialization logic
}
/// <summary>
/// Shuts down the component. Called once when the simulation ends or component is removed.
/// </summary>
public virtual void Shutdown()
{
Console.WriteLine($"Shutting down {Name} ({ID})...");
// Placeholder for component-specific cleanup logic
}
/// <summary>
/// Abstract method to update the component's state over a time step.
/// Must be implemented by concrete component classes.
/// </summary>
/// <param name="deltaTime">The time elapsed.</param>
public abstract void Update(double deltaTime);
/// <summary>
/// Returns a list of all fluid ports for this component.
/// </summary>
/// <returns>A list of FluidPort objects.</returns>
public List<FluidPort> GetFluidPorts() => FluidPorts.Values.ToList();
/// <summary>
/// Gets a fluid port by its unique ID.
/// </summary>
/// <param name="id">The ID of the fluid port.</param>
/// <returns>The FluidPort object if found, otherwise null.</returns>
public FluidPort GetFluidPortById(string id)
{
FluidPorts.TryGetValue(id, out FluidPort port);
return port;
}
/// <summary>
/// Returns a list of all Thermal ports for this component.
/// </summary>
/// <returns>A list of Thermal objects.</returns>
public List<ThermalPort> GetThermalPorts() => ThermalPorts.Values.ToList();
/// <summary>
/// Gets an Thermal port by its unique ID.
/// </summary>
/// <param name="id">The ID of the Thermal port.</param>
/// <returns>The ThermalPort object if found, otherwise null.</returns>
public ThermalPort GetThermalPortById(string id)
{
ThermalPorts.TryGetValue(id, out ThermalPort port);
return port;
}
/// <summary>
/// Helper method to add a fluid port to this component.
/// </summary>
/// <param name="port">The FluidPort to add.</param>
/// <exception cref="ArgumentException">Thrown if a port with the same ID already exists.</exception>
protected void AddFluidPort(FluidPort port)
{
if (FluidPorts.ContainsKey(port.ID))
{
throw new ArgumentException($"A fluid port with ID '{port.ID}' already exists on component '{Name}' ({ID}).");
}
port.ParentComponent = this;
FluidPorts.Add(port.ID, port);
}
/// <summary>
/// Helper method to add an Thermal port to this component.
/// </summary>
/// <param name="port">The ThermalPort to add.</param>
/// <exception cref="ArgumentException">Thrown if an Thermal port with the same ID already exists.</exception>
protected void AddThermalPort(ThermalPort port)
{
if (ThermalPorts.ContainsKey(port.ID))
{
throw new ArgumentException($"An Thermal port with ID '{port.ID}' already exists on component '{Name}' ({ID}).");
}
port.ParentComponent = this;
ThermalPorts.Add(port.ID, port);
}
}
}

View File

@@ -0,0 +1,77 @@
// MyBWRSimulator.Core/Components/Abstract/Port.cs
namespace MyBWRSimulator.Core.Components.Abstract
{
/// <summary>
/// Abstract base class for all types of connection points on components.
/// </summary>
public abstract class Port
{
/// <summary>
/// Unique ID for the port (e.g., "Inlet1", "OutletA").
/// </summary>
public string ID { get; protected set; }
/// <summary>
/// A reference to the Port it's connected to. Null if not connected.
/// </summary>
public Port ConnectedPort { get; protected set; }
/// <summary>
/// A reference back to the component this port belongs to.
/// </summary>
public Component ParentComponent { get; internal set; } // Internal set by AddPort methods
/// <summary>
/// Constructor for the Port base class.
/// </summary>
/// <param name="id">Unique identifier for the port.</param>
protected Port(string id)
{
ID = id;
}
/// <summary>
/// Establishes a connection between this port and another port.
/// </summary>
/// <param name="otherPort">The port to connect to.</param>
public virtual void Connect(Port otherPort)
{
if (otherPort == null)
{
Console.WriteLine($"Error: Cannot connect {ID} to null port.");
return;
}
if (ConnectedPort != null && ConnectedPort != otherPort)
{
Console.WriteLine($"Warning: {ID} is already connected to {ConnectedPort.ID}. Disconnecting first.");
Disconnect();
}
ConnectedPort = otherPort;
// For bidirectional connection, the other port should also connect to this one
if (otherPort.ConnectedPort != this)
{
otherPort.Connect(this);
}
Console.WriteLine($"Port {ID} connected to {otherPort.ID}.");
}
/// <summary>
/// Breaks the connection of this port.
/// </summary>
public virtual void Disconnect()
{
if (ConnectedPort != null)
{
Port oldConnectedPort = ConnectedPort;
ConnectedPort = null;
// Ensure the other port also disconnects from this one
if (oldConnectedPort.ConnectedPort == this)
{
oldConnectedPort.Disconnect();
}
Console.WriteLine($"Port {ID} disconnected from {oldConnectedPort.ID}.");
}
}
}
}