Add a CalculateEnergyflow function in program.cs

Moved the CalculateEnergyFlow to Toplogy.cs
This commit is contained in:
atef 2023-08-31 10:13:28 +02:00
parent a462ee0610
commit 9e03965d5b
2 changed files with 86 additions and 64 deletions

View File

@ -98,6 +98,14 @@ internal static class Program
StatusRecord ReadStatus()
{
var acDc = acDcDevices.Read();
var dcDc = dcDcDevices.Read();
var relays = saliMaxRelaysDevice.Read();
var loadOnAcIsland = acIslandLoadMeter.Read();
var gridMeter = gridMeterDevice.Read();
var pvOnDc = amptDevice.Read();
var battery = batteryDevices.Read();
// ┌────┐ ┌────┐
// │ Pv │ │ Pv │ ┌────┐
// └────┘ └────┘ │ Pv │
@ -140,34 +148,10 @@ internal static class Program
// j = h + i - k [eq3]
var pvOnAcGrid = new AcPowerDevice { Power = 0 };
var pvOnAcIsland = new AcPowerDevice { Power = 0 };
var acDc = acDcDevices.Read();
var dcDc = dcDcDevices.Read();
var relays = saliMaxRelaysDevice.Read();
var loadOnAcIsland = acIslandLoadMeter.Read();
var gridMeter = gridMeterDevice.Read();
var pvOnDc = amptDevice.Read();
var battery = batteryDevices.Read();
var pvOnAcGrid = AcPowerDevice.Null;
var pvOnAcIsland = AcPowerDevice.Null;
var gridPower = gridMeter is null ? AcPower.Null : gridMeter.Ac.Power;
var islandLoadPower = loadOnAcIsland is null ? AcPower.Null : loadOnAcIsland.Ac.Power;
var inverterAcPower = acDc.Ac.Power;
var inverterDcPower = acDc.Dc.Power;
var a = gridPower;
var b = pvOnAcGrid.Power;
var e = pvOnAcIsland.Power;
var f = islandLoadPower;
var g = inverterAcPower;
var h = inverterDcPower;
var i = pvOnDc?.Dc.Power;
var k = dcDc.Dc.Link.Power;
var l = k;
var j = Sum(h, i, -k);
var d = Sum(f, g, -e);
var c = Sum(a, b, -d);
var flow = Topology.CalculateEnergyFlow(gridMeter, pvOnAcGrid, pvOnAcIsland, loadOnAcIsland, acDc, pvOnDc, dcDc);
return new StatusRecord
{
@ -181,10 +165,10 @@ internal static class Program
PvOnAcIsland = pvOnAcIsland,
PvOnDc = pvOnDc,
AcGridToAcIsland = new AcPowerDevice { Power = d },
LoadOnAcGrid = new AcPowerDevice { Power = c },
AcGridToAcIsland = flow.acGridToAcIsland,
LoadOnAcGrid = flow.loadOnAcGrid,
LoadOnAcIsland = loadOnAcIsland,
LoadOnDc = new DcPowerDevice { Power = j.Value},
LoadOnDc = flow.dcPowerDevice,
StateMachine = StateMachine.Default,
EssControl = EssControl.Default,
@ -246,7 +230,7 @@ internal static class Program
WriteControl(record);
record.CreateTopology().WriteLine();
record.CreateTopologyTextBlock().WriteLine();
//record.ToCsv().WriteLine();
@ -303,7 +287,7 @@ internal static class Program
var powerPerInverterPhase = nInverters > 0
? AcPower.FromActiveReactive(essControl.PowerSetpoint / nInverters / 3, 0)
: AcPower.Null;
: AcPower.FromActiveReactive(0,0);
//var powerPerInverterPhase = AcPower.Null;
@ -380,19 +364,5 @@ internal static class Program
return true;
}
private static ActivePower? Sum(ActivePower? e, ActivePower? f, ActivePower? g)
{
if (e is null || f is null || g is null)
return null;
return f + g + e;
}
private static AcPower? Sum(AcPower? e, AcPower? f, AcPower? g)
{
if (e is null || f is null || g is null)
return null;
return f + g + e;
}
}

View File

@ -1,10 +1,16 @@
using System.Diagnostics.CodeAnalysis;
using InnovEnergy.App.SaliMax.Ess;
using InnovEnergy.App.SaliMax.VirtualDevices;
using InnovEnergy.Lib.Devices.AMPT;
using InnovEnergy.Lib.Devices.Battery48TL;
using InnovEnergy.Lib.Devices.EmuMeter;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Units.Power;
using InnovEnergy.Lib.Utils;
using Ac3Bus = InnovEnergy.Lib.Units.Composite.Ac3Bus;
using AcPower = InnovEnergy.Lib.Units.Composite.AcPower;
namespace InnovEnergy.App.SaliMax;
@ -36,7 +42,7 @@ namespace InnovEnergy.App.SaliMax;
public static class Topology
{
public static TextBlock CreateTopology(this StatusRecord status)
public static TextBlock CreateTopologyTextBlock(this StatusRecord status)
{
// AC side
// a + b - c - d = 0 [eq1]
@ -58,14 +64,14 @@ public static class Topology
var b = status.PvOnAcGrid?.Power.Active;
var e = status.PvOnAcIsland?.Power.Active;
var f = status.LoadOnAcIsland?.Ac.Power.Active;
var g = status.AcDc.Ac.Power.Active;
var g = status.AcDc.Dc.Power.Value;
var h = g;
var i = status.PvOnDc?.Dc.Power;
var k = status.DcDc.Dc.Link.Power;
var i = status.PvOnDc?.Dc.Power.Value;
var k = status.DcDc.Dc.Link.Power.Value;
var l = k;
var j = Sum(h, i, -k);
var d = Sum(f, g, -e);
var c = Sum(a, b, -d);
var j = status.LoadOnDc?.Power.Value;
var d = status.AcGridToAcIsland?.Power.Active;
var c = status.LoadOnAcGrid?.Power.Active;
/////////////////////////////
@ -450,6 +456,51 @@ public static class Topology
}
private static TextBlock SwitchedFlow(Boolean? switchClosed, ActivePower? power, String kx)
{
return switchClosed is null ? TextBlock.FromString("??????????")
: !switchClosed.Value ? Switch.Open(kx)
: power is null ? TextBlock.FromString("??????????")
: Flow.Horizontal(power);
}
public static (AcPowerDevice? acGridToAcIsland, AcPowerDevice? loadOnAcGrid, DcPowerDevice? dcPowerDevice)
CalculateEnergyFlow(EmuMeterRegisters? gridMeter,
AcPowerDevice pvOnAcGrid,
AcPowerDevice pvOnAcIsland,
EmuMeterRegisters? loadOnAcIsland,
AcDcDevicesRecord acDc,
AmptStatus? pvOnDc,
DcDcDevicesRecord dcDc)
{
var gridPower = gridMeter?.Ac.Power.Active;
var islandLoadPower = loadOnAcIsland?.Ac.Power.Active;
var inverterAcPower = acDc.Ac.Power.Active;
var inverterDcPower = acDc.Dc.Power;
var a = gridPower;
var b = pvOnAcGrid.Power.Active;
var e = pvOnAcIsland.Power.Active;
var f = islandLoadPower;
var g = inverterAcPower;
var h = inverterDcPower;
var i = pvOnDc?.Dc.Power;
var k = dcDc.Dc.Link.Power;
var l = k;
var j = Sum(h, i, -k);
var d = Sum(f, g, -e);
var c = Sum(a, b, -d);
var acGridToAcIsland = d is null ? null : new AcPowerDevice { Power = AcPower.FromActiveReactive(d, 0) };
var loadOnAcGrid = c is null ? null : new AcPowerDevice { Power = AcPower.FromActiveReactive(c, 0) };
var dcPowerDevice = j is null ? null : new DcPowerDevice { Power = j };
return (acGridToAcIsland, loadOnAcGrid, dcPowerDevice);
}
private static ActivePower? Sum(ActivePower? e, ActivePower? f, ActivePower? g)
{
if (e is null || f is null || g is null)
@ -458,11 +509,12 @@ public static class Topology
return f + g + e;
}
private static TextBlock SwitchedFlow(Boolean? switchClosed, ActivePower? power, String kx)
private static DcPower? Sum(DcPower? e, DcPower? f, DcPower? g)
{
return switchClosed is null ? TextBlock.FromString("??????????")
: !switchClosed.Value ? Switch.Open(kx)
: power is null ? TextBlock.FromString("??????????")
: Flow.Horizontal(power);
if (e is null || f is null || g is null)
return null;
return f + g + e;
}
}