Skip to content

Commit

Permalink
Large updates to the framework
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkinast committed Mar 11, 2024
1 parent 6834aaf commit 86fa7fd
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 95 deletions.
7 changes: 7 additions & 0 deletions Easy2Sim.Optimization/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Easy2Sim.Optimization
{
public class Class1
{

}
}
9 changes: 9 additions & 0 deletions Easy2Sim.Optimization/Easy2Sim.Optimization.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions Easy2Sim/Easy2Sim.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Easy2Sim", "Easy2Sim.csproj", "{3BE37AAA-1E6E-41EF-99A3-B9FD50886F92}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy2Sim.Optimization", "..\Easy2Sim.Optimization\Easy2Sim.Optimization.csproj", "{DEA429CB-604C-4A5A-9554-B2EAA5DB3CAE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3BE37AAA-1E6E-41EF-99A3-B9FD50886F92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3BE37AAA-1E6E-41EF-99A3-B9FD50886F92}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BE37AAA-1E6E-41EF-99A3-B9FD50886F92}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BE37AAA-1E6E-41EF-99A3-B9FD50886F92}.Release|Any CPU.Build.0 = Release|Any CPU
{DEA429CB-604C-4A5A-9554-B2EAA5DB3CAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEA429CB-604C-4A5A-9554-B2EAA5DB3CAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEA429CB-604C-4A5A-9554-B2EAA5DB3CAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEA429CB-604C-4A5A-9554-B2EAA5DB3CAE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C33B355E-3552-4745-854C-B6B0390C641F}
EndGlobalSection
EndGlobal
9 changes: 6 additions & 3 deletions Easy2Sim/Environment/ComponentRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ static ComponentRegister()
RegisteredComponents = new Dictionary<Guid, object>();
}
/// <summary>
/// Get one specific Simulation Environment by its unique Id
/// Get one specific Simulation Environment by its unique guid <paramref name="id"/>
/// </summary>
/// <param name="id"></param>
/// <param name="id">
/// </param>
/// <returns></returns>
public static SimulationEnvironment? GetEnvironment(Guid id)
{
Expand All @@ -31,7 +32,7 @@ static ComponentRegister()
}

/// <summary>
/// Get one specific Solver by its unique ID
/// Get one specific Solver by its unique guid <paramref name="id"/>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Expand All @@ -46,6 +47,7 @@ static ComponentRegister()
/// <summary>
/// Add any component (solver, environment) to the component register
/// </summary>
/// <param name="componentGuid">Guid of the component which should be added</param>
public static void AddComponent(Guid componentGuid, object component)
{
RegisteredComponents.Add(componentGuid, component);
Expand All @@ -54,6 +56,7 @@ public static void AddComponent(Guid componentGuid, object component)
/// <summary>
/// Remove any component (solver, environment) from the component register
/// </summary>
/// <param name="componentGuid"> <c>Guid</c> of the component which should be removed</param>
public static void RemoveComponent(Guid componentGuid)
{
RegisteredComponents.Remove(componentGuid);
Expand Down
30 changes: 28 additions & 2 deletions Easy2Sim/Environment/Easy2SimLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,39 @@
namespace Easy2Sim.Environment
{
/// <summary>
/// Class that can be used to send information over a udp port
/// The Logger of the Easy2Sim Framework.
/// Set when creating a new Environment.
/// <code>
/// SimulationEnvironment environment = new();
/// Logger l1 = new LoggerConfiguration()
/// .MinimumLevel.Verbose()
/// .WriteTo.Console()
/// .CreateLogger();
/// environment.SetLogConfiguration(l1);
/// </code>
///
/// </summary>
public class Easy2SimLogging
{
/// <summary>
/// Serilog is used for logging.
/// Can be used to log to any destination which serilog supports.
/// Make sure to set the correct minimum logging level.
/// </summary>
public Logger? Logger { get; set; }


/// <summary>
/// Method can be used to send information over a Udp client.
/// </summary>
/// <param name="message">
/// Message to send
/// </param>
/// <param name="ipAddress">
/// Destination ip Address
/// </param>
/// <param name="port">
/// Destination port where the message should be sent
/// </param>
public void UdpInformation(string message, string ipAddress = "127.0.0.1", int port = 12345)
{
UdpClient udpClient = new UdpClient();
Expand Down
82 changes: 74 additions & 8 deletions Easy2Sim/Environment/SimulationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

namespace Easy2Sim.Environment
{



/// <summary>
/// Base class for simulation components.
/// </summary>
Expand Down Expand Up @@ -35,32 +32,52 @@ public abstract class SimulationBase : IFrameworkBase
/// </summary>
[JsonProperty]
public string Name { get; private set; }

/// <summary>
/// Index in the simulation, this value is used
/// Index in the simulation.
/// In each time stamp in the simulation, components are called in order of the simulation index.
/// </summary>
[JsonProperty]
public int Index => _simulationIndex;

/// <summary>
/// Set the simulation index of a component manually.
/// Set the simulation <paramref name="index"/> of a component manually.
/// Make sure that all indexes are correct!
/// </summary>
/// <param name="index"></param>
/// <param name="index">
/// The index is typically automatically generated based on creation order of the components.
/// This parameter allows to overwrite it.
/// </param>
public void SetIndexManually(int index)
{
_simulationIndex = index;
}

/// <summary>
/// Set the simulation <paramref name="name"/> of a component manually.
/// Make sure that all names are unique and correct!
/// </summary>
/// <param name="name">
/// A unique name for each simulation component is generated automatically.
/// This parameter allows to change it.
/// </param>
public void SetNameManually(string name)
{
Name = name;
}

[JsonIgnore]
private SimulationEnvironment? _simulationEnvironment;

/// <summary>
/// Simulation environment where the simulation component is registered.
/// </summary>
[JsonIgnore]
public SimulationEnvironment? SimulationEnvironment
{
get
{
_simulationEnvironment = ComponentRegister.GetEnvironment(SimulationEnvironmentGuid);

return _simulationEnvironment;
}
}
Expand All @@ -69,6 +86,9 @@ public SimulationEnvironment? SimulationEnvironment
private SolverBase? _solverBase;


/// <summary>
/// Solver where the component is registered.
/// </summary>
[JsonIgnore]
public SolverBase? Solver
{
Expand All @@ -81,7 +101,10 @@ public SolverBase? Solver
}



/// <summary>
/// Automatically set the simulation index based on the current index in the environment.
/// This method is called, when a new component is created.
/// </summary>
private void SetSimulationIndex()
{
if (SimulationEnvironment == null) return;
Expand Down Expand Up @@ -143,6 +166,43 @@ protected SimulationBase(Guid environmentGuid)
SimulationEnvironment?.AddComponent(this);
}

/// <summary>
/// Returns all simulation bases for a given output parameter name
/// </summary>
/// <param name="output">Name of the output field or property</param>
/// <returns></returns>
public List<SimulationBase> GetConnectedInputComponents(string output)
{
if (SimulationEnvironment == null)
return new List<SimulationBase>();

List<SimulationBase> resultList = new List<SimulationBase>();
List<SimulationBase> connectedComponents = SimulationEnvironment.Model.Connections
.Where(x => x.Source == this)
.Where(x => x.SourceName == output)
.Select(x => x.Target).ToList();
resultList.AddRange(connectedComponents);
return resultList;
}

/// <summary>
/// Returns all simulation bases for a given input parameter name
/// </summary>
/// <param name="input">Name of the input field or property</param>
/// <returns></returns>
public List<SimulationBase> GetConnectedOutputComponents(string input)
{
if (SimulationEnvironment == null)
return new List<SimulationBase>();

List<SimulationBase> resultList = new List<SimulationBase>();
List<SimulationBase> connectedComponents = SimulationEnvironment.Model.Connections
.Where(x => x.Target == this)
.Where(x => x.TargetName == input)
.Select(x => x.Source).ToList();
resultList.AddRange(connectedComponents);
return resultList;
}


/// <summary>
Expand All @@ -166,8 +226,14 @@ public virtual void DiscreteCalculation() { }
/// </summary>
public virtual void DynamicCalculation() { }

/// <summary>
/// This method is called at the end of the simulation
/// When calculate finish ends
/// </summary>
public virtual void End() { }
/// <summary>
/// Serialize to json uses the default constructor.
/// Each component needs to be serializable.
/// </summary>
public abstract string SerializeToJson();

Expand Down
Loading

0 comments on commit 86fa7fd

Please sign in to comment.