generated Units

This commit is contained in:
ig 2023-05-24 12:04:01 +02:00
parent 21b49a1339
commit 4c76475eac
39 changed files with 225 additions and 942 deletions

View File

@ -1,20 +1,20 @@
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units.Generator;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Sum>]
public readonly partial struct ActivePower
{
public static String Unit => "W";
public static String Symbol => "P";
public ActivePower(Double value) => Value = value;
// P=UI
public static Voltage operator /(ActivePower power, Current current) => new Voltage(power.Value / current.Value);
public static Current operator /(ActivePower power, Voltage voltage) => new Current(power.Value / voltage.Value);
public static Energy operator *(ActivePower power, TimeSpan timeSpan) => power.Value / 1000 * timeSpan.TotalHours;
public static Energy operator *(TimeSpan timeSpan, ActivePower power) => power.Value / 1000 * timeSpan.TotalHours;

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = ActivePower;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(ActivePowerConverter))]
public readonly partial struct ActivePower
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class ActivePowerConverter : JsonConverter<ActivePower>
{
public override ActivePower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new ActivePower(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, ActivePower value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,9 +1,10 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
using static System.Math;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>("HAS_CONSTRUCTOR")][Generate<Sum>]
public readonly partial struct Angle
{
public static String Unit => "rad";

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Angle;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(AngleConverter))]
public readonly partial struct Angle
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class AngleConverter : JsonConverter<Angle>
{
public override Angle Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Angle(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Angle value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,13 +1,14 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>("HAS_CONSTRUCTOR")][Generate<Sum>]
public readonly partial struct ApparentPower
{
public static String Unit => "VA";
public static String Symbol => "S";
public ApparentPower(Double value)
{
if (value < 0) throw new ArgumentException("Apparent power cannot be negative", nameof(value));

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = ApparentPower;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(ApparentPowerConverter))]
public readonly partial struct ApparentPower
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class ApparentPowerConverter : JsonConverter<ApparentPower>
{
public override ApparentPower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new ApparentPower(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, ApparentPower value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,9 +1,11 @@
namespace InnovEnergy.Lib.Units.Composite;
public class Ac1Bus : AcPhase
public record Ac1Bus : AcPhase
{
public Frequency Frequency { get; internal init; }
protected Ac1Bus() {}
public Frequency Frequency { get; protected init; }
public static Ac1Bus FromVoltageCurrentFrequencyPhi(Double voltageRms,
@ -15,22 +17,8 @@ public class Ac1Bus : AcPhase
Current = currentRms,
Voltage = voltageRms,
Power = AcPower.FromVoltageCurrentPhi(voltageRms, currentRms, phi)
};
};
}
// [SuppressMessage("ReSharper", "RedundantCast")]
// public static Ac1Bus operator |(Ac1Bus left, Ac1Bus right)
// {
// var f = left.Frequency | right.Frequency;
// var p = (AcPhase)left | (AcPhase)right;
//
// return new Ac1Bus
// {
// Frequency = f,
// Current = p.Current,
// Voltage = p.Voltage,
// Phi = p.Phi
// };
// }

View File

@ -4,13 +4,15 @@ namespace InnovEnergy.Lib.Units.Composite;
#pragma warning disable CS8618
public class Ac3Bus
public record Ac3Bus
{
public AcPhase L1 { get; internal init; }
public AcPhase L2 { get; internal init; }
public AcPhase L3 { get; internal init; }
public AcPower Power { get; internal init; }
public Frequency Frequency { get; internal init; }
protected Ac3Bus() {}
public AcPhase L1 { get; protected init; }
public AcPhase L2 { get; protected init; }
public AcPhase L3 { get; protected init; }
public AcPower Power { get; protected init; }
public Frequency Frequency { get; protected init; }
public static Ac3Bus FromPhasesAndFrequency(AcPhase l1,
AcPhase l2,
@ -23,4 +25,4 @@ public class Ac3Bus
Power = AcPower.SumOf(l1.Power, l2.Power, l3.Power),
Frequency = frequency,
};
}
}

View File

@ -2,9 +2,11 @@ namespace InnovEnergy.Lib.Units.Composite;
#pragma warning disable CS8618
public class AcPhase : Bus
public record AcPhase : Bus
{
public AcPower Power { get; internal init; }
protected AcPhase(){}
public AcPower Power { get; protected init; }
public static AcPhase FromVoltageCurrentPhi(Voltage voltageRms,
Current currentRms,

View File

@ -2,15 +2,16 @@ using static System.Math;
namespace InnovEnergy.Lib.Units.Composite;
public class AcPower
public record AcPower
{
public ApparentPower Apparent { get; internal init; }
public ActivePower Active { get; internal init; }
public ReactivePower Reactive { get; internal init; }
public Angle Phi { get; internal init; }
public Double CosPhi { get; internal init; }
protected AcPower(){}
public ApparentPower Apparent { get; protected init; }
public ActivePower Active { get; protected init; }
public ReactivePower Reactive { get; protected init; }
public Angle Phi { get; protected init; }
public Double CosPhi { get; protected init; }
public static AcPower FromActiveReactiveApparent(ActivePower activePower, ReactivePower reactivePower, ApparentPower apparentPower)
{
var q = reactivePower.Value;
@ -24,7 +25,7 @@ public class AcPower
Reactive = q,
Apparent = s,
Phi = phi,
CosPhi = p/s,
CosPhi = Cos(phi),
};
}
@ -81,8 +82,4 @@ public class AcPower
CosPhi = Cos(phi.Value)
};
}
private static Double NormalizePhi(Double phi) => (phi + PI) % Tau - PI; // Tau is 2pi
}

View File

@ -4,8 +4,8 @@ namespace InnovEnergy.Lib.Units.Composite;
[SuppressMessage("ReSharper", "MemberCanBeProtected.Global")]
public abstract class Bus
public abstract record Bus
{
public Voltage Voltage { get; internal init; }
public Current Current { get; internal init; }
public Voltage Voltage { get; protected init; }
public Current Current { get; protected init; }
}

View File

@ -1,15 +1,15 @@
namespace InnovEnergy.Lib.Units.Composite;
public class DcBus : Bus
public record DcBus : Bus
{
private DcBus() {}
protected DcBus() {}
public ActivePower Power { get; internal init; }
public ActivePower Power { get; protected init; }
public static DcBus FromVoltageCurrent(Voltage voltage, Current current) => new()
{
Voltage = voltage,
Current = current,
Power = current * voltage,
Power = current.Value * voltage.Value,
};
}

View File

@ -1,15 +1,16 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Sum>]
public readonly partial struct Current
{
public static String Unit => "A";
public static String Symbol => "I";
public Current(Double value) => Value = value;
// P=UI
public static ActivePower operator *(Current current, Voltage voltage) => new ActivePower(current.Value * voltage.Value);

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Current;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(CurrentConverter))]
public readonly partial struct Current
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class CurrentConverter : JsonConverter<Current>
{
public override Current Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Current(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Current value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -0,0 +1,22 @@
using InnovEnergy.Lib.SrcGen.Attributes;
using InnovEnergy.Lib.Time.Unix;
namespace InnovEnergy.Lib.Units;
[Generate<Operators>][Generate<Sum>]
public readonly partial struct DcPower
{
public static String Unit => "W";
public static String Symbol => "P";
// P=UI
public static Voltage operator /(DcPower power, Current current) => new Voltage(power.Value / current.Value);
public static Current operator /(DcPower power, Voltage voltage) => new Current(power.Value / voltage.Value);
public static Energy operator *(DcPower power, TimeSpan timeSpan) => power.Value / 1000 * timeSpan.TotalHours;
public static Energy operator *(TimeSpan timeSpan, DcPower power) => power.Value / 1000 * timeSpan.TotalHours;
public static Energy operator *(DcPower power, UnixTimeSpan timeSpan) => power.Value * timeSpan.Ticks / 3_600_000;
public static Energy operator *(UnixTimeSpan timeSpan, DcPower power) => power.Value * timeSpan.Ticks / 3_600_000;
}

View File

@ -1,16 +1,15 @@
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units.Generator;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Sum>]
public readonly partial struct Energy
{
public static String Unit => "kWh";
public static String Symbol => "E";
public Energy(Double value) => Value = value;
public static ActivePower operator /(Energy energy, TimeSpan timeSpan) => energy.Value * 1000 / timeSpan.TotalHours ;
public static ActivePower operator /(Energy energy, UnixTimeSpan timeSpan) => energy.Value * 3_600_000 / timeSpan.Ticks;
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Energy;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(EnergyConverter))]
public readonly partial struct Energy
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class EnergyConverter : JsonConverter<Energy>
{
public override Energy Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Energy(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Energy value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,9 +1,9 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>("HAS_CONSTRUCTOR")][Generate<Sum>]
public readonly partial struct Frequency
{
public static String Unit => "Hz";

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Frequency;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(FrequencyConverter))]
public readonly partial struct Frequency
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class FrequencyConverter : JsonConverter<Frequency>
{
public override Frequency Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Frequency(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Frequency value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -0,0 +1,11 @@
// ReSharper disable once CheckNamespace
namespace InnovEnergy.Lib.Units;
public readonly struct Average
{
public Average(Double value) => throw new NotImplementedException();
public Double Value => throw new NotImplementedException();
public static Average operator |(Average left, Average right) => new((left.Value + right.Value) / 2);
}

View File

@ -1,6 +0,0 @@
using System.Text.Json.Serialization;
namespace InnovEnergy.Lib.Units.Generator;
internal class GenerateAttribute : Attribute
{}

View File

@ -0,0 +1,51 @@
using System.Diagnostics.CodeAnalysis;
using InnovEnergy.Lib.Utils;
// ReSharper disable once CheckNamespace
namespace InnovEnergy.Lib.Units;
[SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")]
public readonly struct Operators
{
public static String Unit => throw new NotImplementedException();
public static String Symbol => throw new NotImplementedException();
#if !HAS_CONSTRUCTOR
public Operators(Double value) => Value = value;
#endif
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static Operators operator *(Double scalar, Operators t) => new Operators(scalar * t.Value);
public static Operators operator *(Operators t, Double scalar) => new Operators(scalar * t.Value);
public static Operators operator /(Operators t, Double scalar) => new Operators(t.Value / scalar);
// addition
public static Operators operator +(Operators left, Operators right) => new Operators(left.Value + right.Value);
public static Operators operator -(Operators left, Operators right) => new Operators(left.Value - right.Value);
public static Operators operator -(Operators t) => new Operators(-t.Value);
// compare
public static Boolean operator ==(Operators left, Operators right) => left.Value == right.Value;
public static Boolean operator !=(Operators left, Operators right) => left.Value != right.Value;
public static Boolean operator > (Operators left, Operators right) => left.Value > right.Value;
public static Boolean operator < (Operators left, Operators right) => left.Value < right.Value;
public static Boolean operator >=(Operators left, Operators right) => left.Value >= right.Value;
public static Boolean operator <=(Operators left, Operators right) => left.Value <= right.Value;
// conversion
public static implicit operator Operators(Double d) => new Operators(d);
// equality
public Boolean Equals(Operators other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is Operators other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}

View File

@ -0,0 +1,12 @@
using System.Diagnostics.CodeAnalysis;
// ReSharper disable once CheckNamespace
namespace InnovEnergy.Lib.Units;
public readonly struct Parallel
{
public Parallel(Double value) => throw new NotImplementedException();
public Double Value => throw new NotImplementedException();
public static Parallel operator |(Parallel left, Parallel right) => new((left.Value * right.Value) / (left.Value + right.Value));
}

View File

@ -0,0 +1,11 @@
// ReSharper disable once CheckNamespace
namespace InnovEnergy.Lib.Units;
public readonly struct Sum
{
public Sum(Double value) => throw new NotImplementedException();
public Double Value => throw new NotImplementedException();
public static Sum operator |(Sum left, Sum right) => new(left.Value + right.Value);
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define AggregationType
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Template;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(TemplateConverter))]
public readonly partial struct Template
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class TemplateConverter : JsonConverter<Template>
{
public override Template Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Template(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Template value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,15 +0,0 @@
#!/usr/bin/env bash
scriptDir=$( dirname -- "$0"; )
cd "$scriptDir/.." || exit
for match in $(grep -e '\[Generate\]' -o *.cs | tr -d '[]')
do
path="${match%:*}"
type="${match#*:}"
file=$(basename -- "$path")
class="${file%.*}"
echo "generating $file"
sed "s/Template/$class/g; s/AggregationType/$type/g" "./Generator/Template.txt" > "./$class.generated.cs"
done

View File

@ -1,12 +1,12 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Sum>]
public readonly partial struct Percent
{
public static String Unit => "%";
public static String Symbol => "%"; // ??
public Percent(Double value) => Value = value;
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Percent;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(PercentConverter))]
public readonly partial struct Percent
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class PercentConverter : JsonConverter<Percent>
{
public override Percent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Percent(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Percent value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,13 +1,12 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Sum>]
public readonly partial struct ReactivePower
{
public static String Unit => "var";
public static String Symbol => "Q";
public ReactivePower(Double value) => Value = value;
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = ReactivePower;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(ReactivePowerConverter))]
public readonly partial struct ReactivePower
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class ReactivePowerConverter : JsonConverter<ReactivePower>
{
public override ReactivePower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new ReactivePower(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, ReactivePower value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -0,0 +1,7 @@
namespace InnovEnergy.Lib.Units.Reflection;
public class UnitAttribute : Attribute
{
public String Unit { get; }
public UnitAttribute(String unit) => Unit = unit;
}

View File

@ -1,19 +1,16 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Parallel>]
public readonly partial struct Resistance
{
public static String Unit => "Ω";
public static String Symbol => "R";
public Resistance(Double value) => Value = value;
// U=RI
public static Voltage operator *(Resistance resistance, Current current) => new Voltage(resistance.Value * current.Value);
// // U=RI
// public static Voltage operator *(Resistance resistance, Current current) => new Voltage(resistance.Value * current.Value);
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Resistance;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(ResistanceConverter))]
public readonly partial struct Resistance
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class ResistanceConverter : JsonConverter<Resistance>
{
public override Resistance Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Resistance(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Resistance value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -1,15 +1,13 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
using T = Temperature;
[Generate]
[Generate<Operators>][Generate<Average>]
public readonly partial struct Temperature
{
public static String Unit => "°C";
public static String Symbol => "T";
public Temperature(Double value) => Value = value;
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Temperature;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(TemperatureConverter))]
public readonly partial struct Temperature
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class TemperatureConverter : JsonConverter<Temperature>
{
public override Temperature Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Temperature(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Temperature value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}

View File

@ -7,7 +7,7 @@ public static class Units
public static Current A (this Double value) => value;
public static Voltage V (this Double value) => value;
public static ActivePower W (this Double value) => value;
public static ActivePower W (this Double value) => value;
public static ReactivePower Var (this Double value) => value;
public static ApparentPower Va (this Double value) => value;
public static Resistance Ohm (this Double value) => value;
@ -15,6 +15,32 @@ public static class Units
public static Angle Rad (this Double value) => value;
public static Temperature Celsius(this Double value) => value;
public static Energy KWh (this Double value) => value;
public static String ToCsv<T>(this T thing)
{
IEnumerable<Object> GetChildren(Object parent)
{
parent
.GetType()
.GetProperties();
}
thing.TraverseLeavesWithPath()
if (thing is null)
return new Dictionary<String, String>();
return thing
.GetType()
.GetProperties()
.Select(p => (p.Name, Value: p.GetValue(thing)?.ToString()))
.Where(p => p.Value != null)
.ToDictionary(p => p.Name, p=>p.Value!);
}
}
public static class Prefixes

View File

@ -6,16 +6,14 @@
<Import Project="../InnovEnergy.Lib.props" />
<ItemGroup>
<PackageReference Include="DecimalMath.DecimalEx" Version="1.0.2" />
<ProjectReference Include="../Utils/Utils.csproj" />
<ProjectReference Include="../Time/Time.csproj" />
<ProjectReference Include="../SrcGen/SrcGen.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Utils/Utils.csproj" />
<ProjectReference Include="../Time/Time.csproj" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="./Generator/generate.sh" />
<MSBuild Projects="../SrcGen/SrcGen.csproj" />
<Exec Command="../SrcGen/bin/$(Configuration)/$(TargetFramework)/SrcGen $(MSBuildProjectFullPath)" />
</Target>
</Project>

View File

@ -1,21 +1,18 @@
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.SrcGen;
using InnovEnergy.Lib.SrcGen.Attributes;
namespace InnovEnergy.Lib.Units;
[Generate]
[Generate<Operators>][Generate<Average>]
public readonly partial struct Voltage
{
public static String Unit => "V";
public static String Symbol => "U";
public Voltage(Double value) => Value = value;
// U=RI
public static Current operator /(Voltage voltage, Resistance resistance) => new Current(voltage.Value / resistance.Value);
// P=UI
public static ActivePower operator *(Voltage voltage, Current current) => new ActivePower(current.Value * voltage.Value);
}
}

View File

@ -1,68 +0,0 @@
#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source.
#define Generate
using static System.Math;
using System.Text.Json;
using System.Text.Json.Serialization;
using InnovEnergy.Lib.Utils;
using System.CodeDom.Compiler;
namespace InnovEnergy.Lib.Units;
using T = Voltage;
[GeneratedCode("generate.sh", "1")]
[JsonConverter(typeof(VoltageConverter))]
public readonly partial struct Voltage
{
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
public static T operator +(T left, T right) => new T(left.Value + right.Value);
public static T operator -(T left, T right) => new T(left.Value - right.Value);
public static T operator -(T t) => new T(-t.Value);
// compare
public static Boolean operator ==(T left, T right) => left.Value == right.Value;
public static Boolean operator !=(T left, T right) => left.Value != right.Value;
public static Boolean operator > (T left, T right) => left.Value > right.Value;
public static Boolean operator < (T left, T right) => left.Value < right.Value;
public static Boolean operator >=(T left, T right) => left.Value >= right.Value;
public static Boolean operator <=(T left, T right) => left.Value <= right.Value;
// conversion
public static implicit operator T(Double d) => new T(d);
// equality
public Boolean Equals(T other) => Value == other.Value;
public override Boolean Equals(Object? obj) => obj is T other && Equals(other);
public override Int32 GetHashCode() => Value.GetHashCode();
}
internal class VoltageConverter : JsonConverter<Voltage>
{
public override Voltage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Voltage(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Voltage value, JsonSerializerOptions options)
{
var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits);
writer.WriteNumberValue(rounded);
}
}