Beispiel #1
0
        public void TestPowers()
        {
            UnitOfMeasure m2 = sys.GetUOM(Unit.SQUARE_METRE);
            UnitOfMeasure p2 = sys.CreatePowerUOM(UnitType.AREA, "m2^1", "m2^1", "square metres raised to power 1", m2, 1);
            UnitOfMeasure p4 = sys.CreatePowerUOM(UnitType.UNCLASSIFIED, "m2^2", "m2^2", "square metres raised to power 2",
                                                  m2, 2);

            double amount = 10;

            Quantity q1 = new Quantity(amount, m2);
            Quantity q3 = new Quantity(amount, p4);

            Quantity q4 = q3.Divide(q1);

            Assert.IsTrue(IsCloseTo(q4.Amount, 1, DELTA6));
            Assert.IsTrue(q4.UOM.GetBaseUOM().Equals(m2));

            Quantity q2 = q1.Convert(p2);

            Assert.IsTrue(IsCloseTo(q2.Amount, amount, DELTA6));
            Assert.IsTrue(q2.UOM.GetBaseUOM().Equals(m2));

            // power method
            UnitOfMeasure ft  = sys.GetUOM(Unit.FOOT);
            UnitOfMeasure ft2 = sys.GetUOM(Unit.SQUARE_FOOT);

            q1 = new Quantity(10, ft);

            q3 = q1.Power(2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 100, DELTA6));
            Assert.IsTrue(q3.UOM.GetBaseSymbol().Equals(ft2.GetBaseSymbol()));

            q4 = q3.Convert(sys.GetUOM(Unit.SQUARE_METRE));
            Assert.IsTrue(IsCloseTo(q4.Amount, 9.290304, DELTA6));

            q3 = q1.Power(1);
            Assert.IsTrue(q3.Amount.Equals(q1.Amount));
            Assert.IsTrue(q3.UOM.GetBaseSymbol().Equals(q1.UOM.GetBaseSymbol()));

            q3 = q1.Power(0);
            Assert.IsTrue(q3.Amount.Equals(1));
            Assert.IsTrue(q3.UOM.GetBaseSymbol().Equals(sys.GetOne().GetBaseSymbol()));

            q3 = q1.Power(-1);
            Assert.IsTrue(q3.Amount.Equals(0.1));
            Assert.IsTrue(q3.UOM.Equals(ft.Invert()));

            q3 = q1.Power(-2);
            Assert.IsTrue(q3.Amount.Equals(0.01));
            Assert.IsTrue(q3.UOM.Equals(ft2.Invert()));
        }
Beispiel #2
0
        public void TestSIUnits()
        {
            sys.ClearCache();

            UnitOfMeasure newton  = sys.GetUOM(Unit.NEWTON);
            UnitOfMeasure metre   = sys.GetUOM(Unit.METRE);
            UnitOfMeasure m2      = sys.GetUOM(Unit.SQUARE_METRE);
            UnitOfMeasure cm      = sys.GetUOM(Prefix.CENTI, metre);
            UnitOfMeasure mps     = sys.GetUOM(Unit.METRE_PER_SEC);
            UnitOfMeasure joule   = sys.GetUOM(Unit.JOULE);
            UnitOfMeasure m3      = sys.GetUOM(Unit.CUBIC_METRE);
            UnitOfMeasure farad   = sys.GetUOM(Unit.FARAD);
            UnitOfMeasure nm      = sys.GetUOM(Unit.NEWTON_METRE);
            UnitOfMeasure coulomb = sys.GetUOM(Unit.COULOMB);
            UnitOfMeasure volt    = sys.GetUOM(Unit.VOLT);
            UnitOfMeasure watt    = sys.GetUOM(Unit.WATT);
            UnitOfMeasure cm2     = sys.CreateProductUOM(UnitType.AREA, "square centimetres", "cm" + (char)0xB2, "", cm, cm);
            UnitOfMeasure cv      = sys.CreateProductUOM(UnitType.ENERGY, "CxV", "C·V", "Coulomb times Volt", coulomb, volt);
            UnitOfMeasure ws      = sys.CreateProductUOM(UnitType.ENERGY, "Wxs", "W·s", "Watt times second", watt,
                                                         sys.GetSecond());
            UnitOfMeasure ft3 = sys.GetUOM(Unit.CUBIC_FOOT);
            UnitOfMeasure hz  = sys.GetUOM(Unit.HERTZ);

            double oneHundred = 100;

            Assert.IsTrue(nm.GetBaseSymbol().Equals(joule.GetBaseSymbol()));
            Assert.IsTrue(cv.GetBaseSymbol().Equals(joule.GetBaseSymbol()));
            Assert.IsTrue(ws.GetBaseSymbol().Equals(joule.GetBaseSymbol()));

            Quantity q1 = new Quantity(10, newton);
            Quantity q2 = new Quantity(10, metre);
            Quantity q3 = q1.Multiply(q2);

            Assert.IsTrue(IsCloseTo(q3.Amount, oneHundred, DELTA6));
            Assert.IsTrue(q3.UOM.GetBaseSymbol().Equals(nm.GetBaseSymbol()));
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));
            Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6));

            q3 = q3.Convert(joule);
            Assert.IsTrue(IsCloseTo(q3.Amount, oneHundred, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(joule));
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));

            q3 = q3.Convert(nm);
            Assert.IsTrue(IsCloseTo(q3.Amount, oneHundred, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(nm));
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));

            double bd1 = 10000;

            q1 = new Quantity(oneHundred, cm);
            q2 = q1.Convert(metre);
            Assert.IsTrue(IsCloseTo(q2.Amount, 1, DELTA6));
            Assert.IsTrue(q2.UOM.Enumeration.Equals(Unit.METRE));
            Assert.IsTrue(IsCloseTo(q2.UOM.ScalingFactor, 1, DELTA6));

            q2 = q2.Convert(cm);
            Assert.IsTrue(IsCloseTo(q2.Amount, oneHundred, DELTA6));
            Assert.IsTrue(IsCloseTo(q2.UOM.ScalingFactor, 0.01, DELTA6));

            q2 = q1;
            q3 = q1.Multiply(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 10000, DELTA6));
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 0.0001, DELTA6));
            Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6));

            Quantity q4 = q3.Convert(m2);

            Assert.IsTrue(q4.UOM.Equals(m2));
            Assert.IsTrue(IsCloseTo(q4.Amount, 1, DELTA6));

            q3 = q3.Convert(m2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 1, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(m2));
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));

            q3 = q3.Convert(cm2);
            Assert.IsTrue(IsCloseTo(q3.Amount, bd1, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(cm2));
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));

            // power
            Quantity      onem3  = new Quantity(1, m3);
            String        cm3sym = "cm" + (char)0xB3;
            UnitOfMeasure cm3    = sys.CreatePowerUOM(UnitType.VOLUME, cm3sym, cm3sym, null, cm, 3);
            Quantity      megcm3 = new Quantity(1E+06, cm3);

            Quantity qft3 = onem3.Convert(ft3);

            Assert.IsTrue(IsCloseTo(qft3.Amount, 35.31466672148859, DELTA6));

            Quantity qtym3 = qft3.Convert(m3);

            Assert.IsTrue(IsCloseTo(qtym3.Amount, 1, DELTA6));

            Quantity qm3 = megcm3.Convert(m3);

            Assert.IsTrue(IsCloseTo(qm3.Amount, 1, DELTA6));
            qm3 = qm3.Convert(cm3);
            Assert.IsTrue(IsCloseTo(qm3.Amount, 1E+06, DELTA6));

            Quantity qcm3 = onem3.Convert(cm3);

            Assert.IsTrue(IsCloseTo(qcm3.Amount, 1E+06, DELTA6));

            // inversions
            UnitOfMeasure u    = metre.Invert();
            string        sym  = u.AbscissaUnit.Symbol;
            string        diop = sys.GetUOM(Unit.DIOPTER).Symbol;

            Assert.IsFalse(sym.Equals(diop));

            u = mps.Invert();
            Assert.IsTrue(u.Symbol.Equals("s/m"));

            UnitOfMeasure uom = sys.CreateQuotientUOM(UnitType.UNCLASSIFIED, "1/F", "1/F", "one over farad", sys.GetOne(),
                                                      farad);

            Assert.IsTrue(uom.Symbol.Equals("1/F"));

            // hz to radians per sec
            q1 = new Quantity(10, sys.GetUOM(Unit.HERTZ));
            q2 = q1.Convert(sys.GetUOM(Unit.RAD_PER_SEC));
            double twentyPi = (double)20 * Math.PI;

            Assert.IsTrue(IsCloseTo(q2.Amount, twentyPi, DELTA6));

            q3 = q2.Convert(sys.GetUOM(Unit.HERTZ));
            Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6));

            // rpm to radians per second
            q1 = new Quantity(10, sys.GetUOM(Unit.REV_PER_MIN));
            q2 = q1.Convert(sys.GetUOM(Unit.RAD_PER_SEC));
            Assert.IsTrue(IsCloseTo(q2.Amount, 1.04719755119, DELTA6));

            q3 = q2.Convert(sys.GetUOM(Unit.REV_PER_MIN));
            Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6));

            q1 = new Quantity(10, hz);
            q2 = new Quantity(1, sys.GetMinute());
            q3 = q1.Multiply(q2).Convert(sys.GetOne());
            Assert.IsTrue(IsCloseTo(q3.Amount, 600, DELTA6));

            q1 = new Quantity(1, sys.GetUOM(Unit.ELECTRON_VOLT));
            q2 = q1.Convert(sys.GetUOM(Unit.JOULE));
            Assert.IsTrue(IsCloseTo(q2.Amount, 1.60217656535E-19, DELTA6));
        }
Beispiel #3
0
        public void TestSIQuantity()
        {
            double ten = 10;

            UnitOfMeasure litre    = sys.GetUOM(Unit.LITRE);
            UnitOfMeasure m3       = sys.GetUOM(Unit.CUBIC_METRE);
            UnitOfMeasure m2       = sys.GetUOM(Unit.SQUARE_METRE);
            UnitOfMeasure m        = sys.GetUOM(Unit.METRE);
            UnitOfMeasure cm       = sys.GetUOM(Prefix.CENTI, m);
            UnitOfMeasure mps      = sys.GetUOM(Unit.METRE_PER_SEC);
            UnitOfMeasure secPerM  = sys.CreateQuotientUOM(UnitType.UNCLASSIFIED, null, "s/m", null, sys.GetSecond(), m);
            UnitOfMeasure oneOverM = sys.GetUOM(Unit.DIOPTER);
            UnitOfMeasure fperm    = sys.GetUOM(Unit.FARAD_PER_METRE);

            UnitOfMeasure oneOverCm = sys.CreateScalarUOM(UnitType.UNCLASSIFIED, null, "1/cm", null);

            oneOverCm.SetConversion(100, oneOverM);

            Quantity q1 = new Quantity(ten, litre);
            Quantity q2 = q1.Convert(m3);

            Assert.IsTrue(IsCloseTo(q2.Amount, 0.01, DELTA6));
            Assert.IsTrue(q2.UOM.Equals(m3));

            q2 = q1.Convert(litre);
            Assert.IsTrue(IsCloseTo(q2.Amount, ten, DELTA6));
            Assert.IsTrue(q2.UOM.Equals(litre));

            // Add
            q1 = new Quantity(2, m);
            q2 = new Quantity(2, cm);
            Quantity q3 = q1.Add(q2);

            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));
            Assert.IsTrue(q3.UOM.AbscissaUnit.Equals(m));
            Assert.IsTrue(IsCloseTo(q3.UOM.Offset, 0, DELTA6));
            Assert.IsTrue(IsCloseTo(q3.Amount, 2.02, DELTA6));

            Quantity q4 = q3.Convert(cm);

            Assert.IsTrue(IsCloseTo(q4.Amount, 202, DELTA6));
            Assert.IsTrue(q4.UOM.Equals(cm));

            // Subtract
            q3 = q3.Subtract(q1);
            Assert.IsTrue(IsCloseTo(q3.UOM.ScalingFactor, 1, DELTA6));
            Assert.IsTrue(q3.UOM.AbscissaUnit.Equals(m));
            Assert.IsTrue(IsCloseTo(q3.Amount, 0.02, DELTA6));

            q4 = q3.Convert(cm);
            Assert.IsTrue(IsCloseTo(q4.Amount, 2, DELTA6));
            Assert.IsTrue(q4.UOM.Equals(cm));

            // Multiply
            q3 = q1.Multiply(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 4, DELTA6));
            UnitOfMeasure u = q3.UOM;

            Assert.IsTrue(IsCloseTo(u.ScalingFactor, 0.01, DELTA6));
            Assert.IsTrue(u.GetBaseSymbol().Equals(m2.GetBaseSymbol()));

            q4 = q3.Divide(q3);
            Assert.IsTrue(IsCloseTo(q4.Amount, 1, DELTA6));
            Assert.IsTrue(q4.UOM.Equals(sys.GetOne()));

            q4 = q3.Divide(q1);
            Assert.IsTrue(q4.Equals(q2));

            q4 = q3.Convert(m2);
            Assert.IsTrue(IsCloseTo(q4.Amount, 0.04, DELTA6));
            Assert.IsTrue(q4.UOM.Equals(m2));

            // Divide
            q3 = q3.Divide(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 2.0, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(m));
            Assert.IsTrue(q3.Equals(q1));

            q3 = q3.Convert(m);
            Assert.IsTrue(IsCloseTo(q3.Amount, 2.0, DELTA6));

            q1 = new Quantity(0, litre);

            try
            {
                q2 = q1.Divide(q1);
                Assert.Fail("Divide by zero)");
            }
            catch (Exception)
            {
            }

            q1 = q3.Convert(cm).Divide(ten);
            Assert.IsTrue(IsCloseTo(q1.Amount, 20, DELTA6));

            // Invert
            q1 = new Quantity(10, mps);
            q2 = q1.Invert();
            Assert.IsTrue(IsCloseTo(q2.Amount, 0.1, DELTA6));
            Assert.IsTrue(q2.UOM.Equals(secPerM));

            q2 = q2.Invert();
            Assert.IsTrue(IsCloseTo(q2.Amount, 10, DELTA6));
            Assert.IsTrue(q2.UOM.Equals(mps));

            q1 = new Quantity(10, cm);
            q2 = q1.Invert();
            Assert.IsTrue(IsCloseTo(q2.Amount, 0.1, DELTA6));
            u = q2.UOM;
            Assert.IsTrue(u.Equals(oneOverCm));

            q2 = q2.Convert(m.Invert());
            Assert.IsTrue(IsCloseTo(q2.Amount, 10, DELTA6));
            Assert.IsTrue(q2.UOM.Equals(oneOverM));

            Assert.IsTrue(q2.ToString() != null);

            // Newton-metres Divided by metres
            q1 = new Quantity(10, sys.GetUOM(Unit.NEWTON_METRE));
            q2 = new Quantity(1, sys.GetUOM(Unit.METRE));
            q3 = q1.Divide(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(sys.GetUOM(Unit.NEWTON)));

            // length multiplied by force
            q1 = new Quantity(10, sys.GetUOM(Unit.NEWTON));
            q2 = new Quantity(1, sys.GetUOM(Unit.METRE));
            q3 = q1.Multiply(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6));
            UnitOfMeasure nm1 = q3.UOM;
            UnitOfMeasure nm2 = sys.GetUOM(Unit.NEWTON_METRE);

            Assert.IsTrue(nm1.GetBaseSymbol().Equals(nm2.GetBaseSymbol()));
            q4 = q3.Convert(sys.GetUOM(Unit.JOULE));
            Assert.IsTrue(q4.UOM.Equals(sys.GetUOM(Unit.JOULE)));

            // farads
            q1 = new Quantity(10, fperm);
            q2 = new Quantity(1, m);
            q3 = q1.Multiply(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(sys.GetUOM(Unit.FARAD)));

            // amps
            q1 = new Quantity(10, sys.GetUOM(Unit.AMPERE_PER_METRE));
            q2 = new Quantity(1, m);
            q3 = q1.Multiply(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 10, DELTA6));
            Assert.IsTrue(q3.UOM.Equals(sys.GetUOM(Unit.AMPERE)));

            // Boltzmann and Avogadro
            Quantity boltzmann = sys.GetQuantity(Constant.BOLTZMANN_CONSTANT);
            Quantity avogadro  = sys.GetQuantity(Constant.AVAGADRO_CONSTANT);
            Quantity gas       = sys.GetQuantity(Constant.GAS_CONSTANT);
            Quantity qR        = boltzmann.Multiply(avogadro);

            Assert.IsTrue(IsCloseTo(qR.UOM.ScalingFactor, gas.UOM.ScalingFactor, DELTA6));

            // Sieverts
            q1 = new Quantity(20, sys.GetUOM(Prefix.MILLI, Unit.SIEVERTS_PER_HOUR));
            q2 = new Quantity(24, sys.GetHour());
            q3 = q1.Multiply(q2);
            Assert.IsTrue(IsCloseTo(q3.Amount, 480, DELTA6));
        }