Skip to content

Instantly share code, notes, and snippets.

@flq flq/IiUnitTests.cs
Last active Dec 30, 2015

Embed
What would you like to do?
Gist for using NMeasure for surplus energy in atmosphere...
using System.Collections.Generic;
using Xunit;
using static NMeasure.U;
using static NMeasure.Tests.IiUnits;
namespace NMeasure.Tests
{
public class IIUnitsTests
{
public IIUnitsTests()
{
StandardUnitConfiguration.Use().Extend<IiUnitsDefinition>();
}
[Theory, MemberData("TestSource")]
public void SomeConversions(Unit first, Unit second, decimal input, decimal expected)
{
var m1 = input * first;
m1.ConvertTo(second).Value.IsEqualTo(expected);
}
[Fact]
public void SoWhatAboutTheSpeedOfLight()
{
var speedOfLight = PhysicalConstants.SpeedOfLight.ConvertTo(ShipLength / Heartbeat);
speedOfLight.Value.IsEqualTo(100000m);
}
public static IEnumerable<object[]> TestSource()
{
yield return new object[] { ShipLength, Kilometer, 1m, 2.4640m };
yield return new object[] { QMichron, Hour, 50m, 28.5388m };
}
}
internal static class IiUnits
{
private static IUnitFactory _units;
private static IUnitFactory UnitsFactory => _units ?? (_units = GetUnitFactory());
public static readonly Unit ShipLength = UnitsFactory.CreateRootUnit("SL");
public static readonly Unit MicroShipLength = UnitsFactory.CreateRootUnit("mSL");
public static readonly Unit Heartbeat = UnitsFactory.CreateRootUnit("IH");
public static readonly Unit Michron = UnitsFactory.CreateRootUnit("IMc");
public static readonly Unit QMichron = UnitsFactory.CreateRootUnit("IQMc");
public static readonly Unit GreatMichron = UnitsFactory.CreateRootUnit("IGMc");
public static readonly Unit Period = UnitsFactory.CreateRootUnit("IP");
public static readonly Unit ShipMass = UnitsFactory.CreateRootUnit("IM");
public static readonly Unit SunMass = UnitsFactory.CreateRootUnit("ISM");
public class IiUnitsDefinition : IUnitConfigurationExtension
{
public void Extend(UnitConfiguration c)
{
c.Unit(MicroShipLength)
.IsPhysicalUnit(_LENGTH)
.StartScale()
.To(ShipLength, 1000);
c.Unit(Heartbeat)
.IsPhysicalUnit(_TIME)
.StartScale()
.To(Michron, 50)
.To(QMichron, 50)
.To(GreatMichron, 50)
.To(Period, 250);
c.Unit(ShipMass)
.IsPhysicalUnit(_MASS)
.ConvertibleTo(SunMass, m => m / 10E+25m, m => m * 10E+25m);
c[ShipLength].ConvertValueBased(Kilometer, v => v * 2.4640476m, v => v / 2.4640476m);
c[Heartbeat].ConvertValueBased(Second, v => v * 0.821917808219178m, v => v / 0.821917808219178m);
c[ShipMass].ConvertValueBased(Kilogram, v => v * 55402.15m, v => v / 55402.15m);
}
}
}
}
using System;
using NMeasure;
using static NMeasure.U;
using static System.Math;
namespace UsinMB
{
public class Program
{
public static void Main(string[] args)
{
StandardUnitConfiguration.Use();
var rEarth = (6371 * Kilometer).ConvertTo(Meter);
var rAir = (6373 * Kilometer).ConvertTo(Meter);
var dAir = 1.225m * Kilogram / (Meter ^ 3);
var Cv = 718 * Joule / (Kilogram * Kelvin);
var tempIncrease = 2 * Kelvin;
var volume = 4d / 3d * PI * ((rAir ^ 3) - (rEarth ^ 3));
var surplusEnergy = (volume * dAir * Cv * tempIncrease).ConvertTo(GigaJoule);
var annualEnergyInGJ = 3600d * 23536500d;
Console.WriteLine(volume);
Console.WriteLine(volume * dAir);
Console.WriteLine(surplusEnergy);
Console.WriteLine(Convert.ToDouble(surplusEnergy.Value) / annualEnergyInGJ);
Console.ReadLine();
}
}
}
@flq

This comment has been minimized.

Copy link
Owner Author

commented Dec 29, 2015

Output:

1020449218718634278.0953m*m*m
1250050292930326990.6667kg
1795072220647.9496GJ
21.1854427124767
@flq

This comment has been minimized.

Copy link
Owner Author

commented Dec 29, 2015

Formatted...

1'020'449'218'718'634'278.1 m^3 (1.020e+18)
1'250'050'292'930'326'990.67 kg (1.250e+18)
1'795'072'220'647.95 GJ (1.795e+12)
21.19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.