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() 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 │ │ Pv │ ┌────┐
// └────┘ └────┘ │ Pv │ // └────┘ └────┘ │ Pv │
@ -138,36 +146,12 @@ internal static class Program
// g = h assuming no losses in ACDC // g = h assuming no losses in ACDC
// k = l assuming no losses in DCDC // k = l assuming no losses in DCDC
// j = h + i - k [eq3] // j = h + i - k [eq3]
var pvOnAcGrid = new AcPowerDevice { Power = 0 };
var pvOnAcIsland = new AcPowerDevice { Power = 0 };
var flow = Topology.CalculateEnergyFlow(gridMeter, pvOnAcGrid, pvOnAcIsland, loadOnAcIsland, acDc, pvOnDc, dcDc);
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);
return new StatusRecord return new StatusRecord
{ {
@ -181,10 +165,10 @@ internal static class Program
PvOnAcIsland = pvOnAcIsland, PvOnAcIsland = pvOnAcIsland,
PvOnDc = pvOnDc, PvOnDc = pvOnDc,
AcGridToAcIsland = new AcPowerDevice { Power = d }, AcGridToAcIsland = flow.acGridToAcIsland,
LoadOnAcGrid = new AcPowerDevice { Power = c }, LoadOnAcGrid = flow.loadOnAcGrid,
LoadOnAcIsland = loadOnAcIsland, LoadOnAcIsland = loadOnAcIsland,
LoadOnDc = new DcPowerDevice { Power = j.Value}, LoadOnDc = flow.dcPowerDevice,
StateMachine = StateMachine.Default, StateMachine = StateMachine.Default,
EssControl = EssControl.Default, EssControl = EssControl.Default,
@ -246,7 +230,7 @@ internal static class Program
WriteControl(record); WriteControl(record);
record.CreateTopology().WriteLine(); record.CreateTopologyTextBlock().WriteLine();
//record.ToCsv().WriteLine(); //record.ToCsv().WriteLine();
@ -303,7 +287,7 @@ internal static class Program
var powerPerInverterPhase = nInverters > 0 var powerPerInverterPhase = nInverters > 0
? AcPower.FromActiveReactive(essControl.PowerSetpoint / nInverters / 3, 0) ? AcPower.FromActiveReactive(essControl.PowerSetpoint / nInverters / 3, 0)
: AcPower.Null; : AcPower.FromActiveReactive(0,0);
//var powerPerInverterPhase = AcPower.Null; //var powerPerInverterPhase = AcPower.Null;
@ -380,19 +364,5 @@ internal static class Program
return true; 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 System.Diagnostics.CodeAnalysis;
using InnovEnergy.App.SaliMax.Ess; using InnovEnergy.App.SaliMax.Ess;
using InnovEnergy.App.SaliMax.VirtualDevices;
using InnovEnergy.Lib.Devices.AMPT;
using InnovEnergy.Lib.Devices.Battery48TL; 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;
using InnovEnergy.Lib.Units.Power; using InnovEnergy.Lib.Units.Power;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using Ac3Bus = InnovEnergy.Lib.Units.Composite.Ac3Bus; using Ac3Bus = InnovEnergy.Lib.Units.Composite.Ac3Bus;
using AcPower = InnovEnergy.Lib.Units.Composite.AcPower;
namespace InnovEnergy.App.SaliMax; namespace InnovEnergy.App.SaliMax;
@ -36,7 +42,7 @@ namespace InnovEnergy.App.SaliMax;
public static class Topology public static class Topology
{ {
public static TextBlock CreateTopology(this StatusRecord status) public static TextBlock CreateTopologyTextBlock(this StatusRecord status)
{ {
// AC side // AC side
// a + b - c - d = 0 [eq1] // a + b - c - d = 0 [eq1]
@ -58,14 +64,14 @@ public static class Topology
var b = status.PvOnAcGrid?.Power.Active; var b = status.PvOnAcGrid?.Power.Active;
var e = status.PvOnAcIsland?.Power.Active; var e = status.PvOnAcIsland?.Power.Active;
var f = status.LoadOnAcIsland?.Ac.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 h = g;
var i = status.PvOnDc?.Dc.Power; var i = status.PvOnDc?.Dc.Power.Value;
var k = status.DcDc.Dc.Link.Power; var k = status.DcDc.Dc.Link.Power.Value;
var l = k; var l = k;
var j = Sum(h, i, -k); var j = status.LoadOnDc?.Power.Value;
var d = Sum(f, g, -e); var d = status.AcGridToAcIsland?.Power.Active;
var c = Sum(a, b, -d); var c = status.LoadOnAcGrid?.Power.Active;
///////////////////////////// /////////////////////////////
@ -449,15 +455,7 @@ public static class Topology
return TextBlock.AlignCenterVertical(flow, box); return TextBlock.AlignCenterVertical(flow, box);
} }
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 TextBlock SwitchedFlow(Boolean? switchClosed, ActivePower? power, String kx) private static TextBlock SwitchedFlow(Boolean? switchClosed, ActivePower? power, String kx)
{ {
return switchClosed is null ? TextBlock.FromString("??????????") return switchClosed is null ? TextBlock.FromString("??????????")
@ -465,4 +463,58 @@ public static class Topology
: power is null ? TextBlock.FromString("??????????") : power is null ? TextBlock.FromString("??????????")
: Flow.Horizontal(power); : 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)
return null;
return f + g + e;
}
private static DcPower? Sum(DcPower? e, DcPower? f, DcPower? g)
{
if (e is null || f is null || g is null)
return null;
return f + g + e;
}
} }