Exemplo n.º 1
0
    def test_multiply(self):
        #multiplication should error for units with offsets

        x = PhysicalQuantity('1g')
        y = PhysicalQuantity('2s')
        z = PhysicalQuantity('1 degC')

        self.assertEqual(
            x.unit * y.unit,
            PhysicalUnit({
                's': 1,
                'kg': 1
            }, .001, _get_powers(mass=1, time=1), 0))
        self.assertEqual(
            y.unit * x.unit,
            PhysicalUnit({
                's': 1,
                'kg': 1
            }, .001, _get_powers(mass=1, time=1), 0))

        try:
            x.unit * z.unit
        except TypeError as err:
            self.assertEqual(str(err),
                             "cannot multiply units with non-zero offset")
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 2
0
    def test_neg_known_Values(self):
        #__neg__ should flip sign of value for physical quantity

        x = PhysicalQuantity('5V')
        self.assertEqual((-x).value, -5)
        x = PhysicalQuantity('-9.8m')
        self.assertEqual((-x).value, 9.8)
Exemplo n.º 3
0
    def test_pow(self):
        #power should error for offest units and for non-integer powers

        x = PhysicalQuantity('1m')
        y = PhysicalQuantity('1degF')

        z = x**3
        self.assertEqual(z.unit, PhysicalQuantity('1m**3').unit)
        x = z**(1.0 / 3.0)  #checks inverse integer units
        self.assertEqual(x.unit, PhysicalQuantity('1m').unit)

        #test offset units:
        try:
            y**17
        except TypeError as err:
            self.assertEqual(str(err),
                             'cannot exponentiate units with non-zero offset')
        else:
            self.fail('Expecting TypeError')

        #test non-integer powers
        try:
            x**1.2
        except TypeError as err:
            self.assertEqual(
                str(err), 'Only integer and inverse integer exponents allowed')
        else:
            self.fail('Expecting TypeError')
        try:
            x**(5.0 / 2.0)
        except TypeError as err:
            self.assertEqual(
                str(err), 'Only integer and inverse integer exponents allowed')
        else:
            self.fail('Expecting TypeError')
Exemplo n.º 4
0
 def test_pi(self):
     # Fixes issue 786
     x = PhysicalQuantity('1rpm')
     x.convert_to_unit('rad/min')
     self.assertAlmostEqual(x.value,
                            PhysicalQuantity('6.283185rad/min').value,
                            places=3)
Exemplo n.º 5
0
    def test_conversion_factor_to(self):
        #conversion_factor_to should errror for units with different base
        #power, should error for units with incompativle offset

        w = PhysicalQuantity('1cm')
        x = PhysicalQuantity('1m')
        y = PhysicalQuantity('1degF')
        z1 = PhysicalQuantity('1degC')
        z2 = PhysicalQuantity('1degK')

        self.assertEqual(w.unit.conversion_factor_to(x.unit), 1 / 100.0)
        try:  #incompatible units
            w.unit.conversion_factor_to(y.unit)
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("Expecting TypeError")
        #compatible offset units
        self.assertEqual(z1.unit.conversion_factor_to(z2.unit), 1.0)
        try:  #incompatible offset units
            y.unit.conversion_factor_to(z2.unit)
        except TypeError as err:
            msg = "Unit conversion (degF to degK) cannot be expressed as a simple multiplicative factor"
            self.assertEqual(str(err), msg)
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 6
0
 def test_pi(self):
     # Fixes issue 786
     x = PhysicalQuantity('1rpm')
     x.convert_to_unit('rad/min')
     self.assertAlmostEqual(x.value,
                            PhysicalQuantity('6.283185rad/min').value,
                            places=3)
Exemplo n.º 7
0
    def test_division(self):
        #division should error when working with offset units

        w = PhysicalQuantity('2kg')
        x = PhysicalQuantity('1g')
        y = PhysicalQuantity('2s')
        z = PhysicalQuantity('1 degC')

        quo = w.unit/x.unit
        quo2 = x.unit/y.unit

        self.assertEqual(quo, PhysicalUnit({'kg': 1, 'g': -1},
                                                 1000.0, _get_powers(),0))
        self.assertEqual(quo2, PhysicalUnit({'s': -1, 'g': 1},
                                                  0.001,
                                                  _get_powers(mass=1, time=-1),0))
        quo = y.unit/2.0
        self.assertEqual(quo, PhysicalUnit({'s': 1, "2.0":-1},
                                                 .5, _get_powers(time=1), 0))
        quo = 2.0/y.unit
        self.assertEqual(quo, PhysicalUnit({'s': -1,"2.0":1},2,
                                                 _get_powers(time=-1),0))
        try:
            x.unit / z.unit
        except TypeError as err:
            self.assertEqual(str(err),"cannot divide units with non-zero offset")
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 8
0
    def test_pos_known_Values(self):
        #should retain sign for value of physical quantity

        x = PhysicalQuantity('5V')
        self.assertEqual((+x).value, 5)
        x = PhysicalQuantity('-9.8m')
        self.assertEqual((+x).value, -9.8)
Exemplo n.º 9
0
    def test__is_compatible__known__Values(self):
        #is_compatible should return True for compatible units and False for
        #incompatible ones

        testvals=(('5m', 'cm',True), ('1s', 'ms',True), ('1m', 'ms',False))
        for q1, q2, bool in testvals:
            x = PhysicalQuantity(q1)
            self.assertEqual(x.is_compatible(q2), bool)
Exemplo n.º 10
0
    def test__is_compatible__known__Values(self):
        #is_compatible should return True for compatible units and False for
        #incompatible ones

        testvals=(('5m', 'cm',True), ('1s', 'ms',True), ('1m', 'ms',False))
        for q1, q2, bool in testvals:
            x = PhysicalQuantity(q1)
            self.assertEqual(x.is_compatible(q2), bool)
Exemplo n.º 11
0
    def config_changed(self, update_parent=True):
        """ Calculate and save our unit conversion factor.
        """
        super(UnitConversionPComp, self).config_changed(update_parent)

        src = PhysicalQuantity(1.0, self._srcunits)
        target = self._meta['out0'].get('units')
        src.convert_to_unit(target)
        self.grad = src.get_value()
Exemplo n.º 12
0
    def ensure_init(self):
        """Make sure our inputs and outputs have been
        initialized.
        """
        super(UnitConversionPComp, self).ensure_init()

        src = PhysicalQuantity(1.0, self._srcunits)
        target = self._meta['out0'].get('units')
        src.convert_to_unit(target)
        self.grad = src.get_value()
Exemplo n.º 13
0
    def ensure_init(self):
        """Make sure our inputs and outputs have been
        initialized.
        """
        super(UnitConversionPComp, self).ensure_init()

        src    = PhysicalQuantity(1.0, self._srcunits)
        target = self._meta['out0'].get('units')
        src.convert_to_unit(target)
        self.grad = src.get_value()
Exemplo n.º 14
0
    def test_abs_known_Values(self):
        #__abs__ should give known result with known input

        x = PhysicalQuantity('-5V')
        self.assertEqual(abs(x).unit, x.unit)
        self.assertEqual(abs(x).value, 5)

        x = PhysicalQuantity('5V')
        self.assertEqual(abs(x).unit, x.unit)
        self.assertEqual(abs(x).value, 5)
Exemplo n.º 15
0
    def test_prefix_plus_math(self):
        # From an issue: m**2 converts fine, but cm**2 does not.

        x1 = convert_units(1.0, 'm**2', 'cm**2')
        self.assertEqual(x1, 10000.0)

        # Let's make sure we can dclare some complicated units
        x = PhysicalQuantity('7200nm**3/kPa*dL')

        #from issue 825, make sure you can handle single characters before a /
        x = PhysicalQuantity('1 g/kW')
Exemplo n.º 16
0
def unit_xform(node, in_units, out_units):
    """Transforms an ast into expr*scaler+adder where scaler
    and adder are from units conversion.
    """
    inpq = PhysicalQuantity(1.0, in_units)
    outpq = PhysicalQuantity(1.0, out_units)
    try:
        scaler, adder = inpq.unit.conversion_tuple_to(outpq.unit)
    except TypeError:
        raise TypeError("units '%s' are incompatible with assigning units of '%s'" % (inpq.get_unit_name(), outpq.get_unit_name()))
    return scaler_adder_xform(node, scaler, adder)
Exemplo n.º 17
0
 def test_name(self):
     #name should return a mathematically correct representation of the unit
     x1 = PhysicalQuantity('1m')
     x2 = PhysicalQuantity('1kg')
     y = 1 / x1
     self.assertEqual(y.unit.name(), '1/m')
     y = 1 / x1 / x1
     self.assertEqual(y.unit.name(), '1/m**2')
     y = x1**2
     self.assertEqual(y.unit.name(), 'm**2')
     y = x2 / (x1**2)
     self.assertEqual(y.unit.name(), 'kg/m**2')
Exemplo n.º 18
0
    def test_lt(self):
        x = PhysicalQuantity('1 d')
        y = PhysicalQuantity('2 d')
        self.assertTrue(x < y)
        self.assertTrue(y > x)
        self.assertEqual(x, x)

        try:
            x < 2
        except TypeError as err:
            self.assertEqual("Incompatible types", str(err))
        else:
            self.fail('Expecting TypeError')
Exemplo n.º 19
0
    def test_in_units_of(self):
        #in_units_of should return a new PhysicalQuantity with the requested
        #unit, leaving the old unit as it was

        x = PhysicalQuantity('5cm')
        y = x.in_units_of('m')
        self.assertEqual(y, PhysicalQuantity('0.05m'))
        self.assertEqual(x, PhysicalQuantity('5cm'))

        x = PhysicalQuantity('5cm')
        try:
            y = x.in_units_of('degC')
        except TypeError as err:
            self.assertEqual(str(err), 'Incompatible units')
        else:
            self.fail("TypeError expected")
Exemplo n.º 20
0
    def test_pow_known_Values(self):
        #__pow__ should give known result with known input
        #the unit of the power should be the power of the input units

        #test integer exponent
        x = PhysicalQuantity('5V')
        self.assertEqual((x**2).value, 5**2)
        self.assertEqual((x**2).unit, x.unit**2)

        #test for inverse integer exponent
        x = PhysicalQuantity('1m**2')
        y = PhysicalQuantity('1m')
        self.assertEqual(x**(1.0 / 2.0), y)
        self.assertEqual(x / y, y)

        #test for error from non integer exponent
        try:
            x**2.5
        except TypeError as err:
            self.assertEqual(
                str(err), "Only integer and inverse integer exponents allowed")
        else:
            self.fail("Expecting TypeError")

        #test for error on offset units
        x = PhysicalQuantity('1degC')
        try:
            x**2
        except TypeError as err:
            self.assertEqual(str(err),
                             'cannot exponentiate units with non-zero offset')
        else:
            self.fail("expected TypeError")

        #test for error if exponent is a PhysicalQuantity
        try:
            x**x
        except TypeError as err:
            self.assertEqual(str(err), 'Exponents must be dimensionless')
        else:
            self.fail("expected TypeError")
        try:  #__rpow__
            2**x
        except TypeError as err:
            self.assertEqual(str(err), 'Exponents must be dimensionless')
        else:
            self.fail("expected TypeError")
Exemplo n.º 21
0
    def test_repr_str(self):
        #__repr__should return a string which could be used to contruct the
        #unit instance, __str__ should return a string with just the unit
        #name for str

        u = PhysicalQuantity('1 d')
        self.assertEqual(repr(u.unit),
                         "PhysicalUnit({'d': 1},86400.0,%s,0.0)" % _get_powers(time=1))
        self.assertEqual(str(u.unit), "<PhysicalUnit d>")
Exemplo n.º 22
0
    def test_cmp(self):
        #should error for incompatible units, if they are compatible then it
        #should cmp on their factors

        x = PhysicalQuantity('1 d')
        y = PhysicalQuantity('1 s')
        z = PhysicalQuantity('1 ft')

        self.assertTrue(x > y)
        self.assertEqual(x, x)
        self.assertTrue(y < x)

        try:
            x < z
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 23
0
    def test_sub_known_Values(self):
        #subtraction should give known result with known input
        #__rsub__ should give the negative of __sub__
        #the units of the results should be the same as the units of the calling instance

        known_sub_Values=(('1m', '5m',-4), ('1cm', '1cm',0), ('1cm', '5m',-499.0),
                          ('7km', '1m',6.999))

        for q1,q2, sum in known_sub_Values:
            x = PhysicalQuantity(q1)
            y = PhysicalQuantity(q2)
            self.assertEqual((x-y).value, sum)
            self.assertEqual((x-y).unit, x.unit)
            self.assertEqual(x.__rsub__(y).value, -sum)
            self.assertEqual(x.__rsub__(y).unit, x.unit)

        #test for error if incompatible units
        q1 = PhysicalQuantity('1cm')
        q2 = PhysicalQuantity('1kg')

        try:
            q1 - q2
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("expecting TypeError")

        #test offset units
        q1 = PhysicalQuantity('1degK')
        q2 = PhysicalQuantity('1degR')
        q3 = PhysicalQuantity('1degC')

        result = q1 - q2
        self.assertAlmostEqual(result.value, .444,3)
        self.assertEqual(result.unit, q1.unit)

        try:
            q3 - q2
        except TypeError as err:
            msg = "Unit conversion (degR to degC) cannot be expressed as a simple multiplicative factor"
            self.assertEqual(str(err), msg)
        else:
            self.fail('expecting TypeError')
Exemplo n.º 24
0
    def test_div_known_Values(self):
        #__div__ should give known result with known input
        #the unit of the product should be the product of the units

        #scalar division
        x = PhysicalQuantity('1cm')
        y = 12.3
        z = 1/12.3
        self.assertAlmostEqual((x/y).value, PhysicalQuantity('%f cm'%z).value, 4)
        self.assertEqual((x/y).unit, PhysicalQuantity('%f cm'%z).unit)
        self.assertEqual(y/x, PhysicalQuantity('12.3cm**-1'))

        #unitless result
        x = PhysicalQuantity('1.0m')
        y = PhysicalQuantity('5m')
        quo = 1.0/5
        # if quotient is unit-less (that is, x and y are additively compatible)
        # re-arranges x & y in terms of the known quotient and __rdiv__ and checks for consistency
        self.assertEqual((x/y), quo)
        self.assertEqual(x.__rdiv__(y), 1/quo)
        self.assertEqual(type(x/y), float)

        x = PhysicalQuantity('3cm')
        y = PhysicalQuantity('5s')
        quo = 3.0/5
        # if quotient has a unit (x and y are additively incompatible)
        # re-arranges x & y in terms of the known quotient and __rdiv__ and checks for consistency
        self.assertEqual((x/y).value, quo)
        self.assertEqual(x.__rdiv__(y).value, 1/quo)

        self.assertEqual((x/y).unit, x.unit/y.unit)
        self.assertEqual(x.__rdiv__(y).unit, y.unit/x.unit)
        self.assertEqual(str(x/y), '0.6 cm/s')
Exemplo n.º 25
0
    def test_init(self):
        #__init__ should have the same result regardless of the
        #constructor calling pattern

        x = PhysicalQuantity('1m')
        y = PhysicalQuantity(1, 'm')
        self.assertEqual(x.value, y.value)
        self.assertEqual(x.unit, y.unit)

        z = PhysicalQuantity('1dam')  #check for two letter prefixes

        #error for improper init argument
        try:
            x = PhysicalQuantity('m')
        except TypeError as err:
            self.assertEqual(str(err),
                             "No number found in input argument: 'm'")
        else:
            self.fail("Expecting TypeError")

        try:
            x = PhysicalQuantity('1in')
        except ValueError as err:
            self.assertEqual(str(err), "no unit named 'in' is defined")
        else:
            self.fail("Expecting ValueError")

        try:
            x = PhysicalQuantity(1, None)
        except TypeError as err:
            self.assertEqual(str(err), "None is not a unit")
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 26
0
    def test_convert_to_unit(self):
        #convert_to_unit should change the unit of the calling instance to the requested new unit
        x = PhysicalQuantity('5cm')
        x.convert_to_unit('m')
        self.assertEqual(x, PhysicalQuantity('0.05m'))

        #Test for no compatible units
        x = PhysicalQuantity('5cm')
        try:
            x.convert_to_unit('kg')
        except TypeError as err:
            self.assertEqual(str(err), 'Incompatible units')
        else:
            self.fail("TypeError expected")

        x = PhysicalQuantity('1.0psi')
        x.convert_to_unit('psf')
        self.assertEqual(x, PhysicalQuantity('144.0psf'))
Exemplo n.º 27
0
    def test_sub_known_Values(self):
        #subtraction should give known result with known input
        #__rsub__ should give the negative of __sub__
        #the units of the results should be the same as the units of the calling instance

        known_sub_Values = (('1m', '5m', -4), ('1cm', '1cm', 0),
                            ('1cm', '5m', -499.0), ('7km', '1m', 6.999))

        for q1, q2, sum in known_sub_Values:
            x = PhysicalQuantity(q1)
            y = PhysicalQuantity(q2)
            self.assertEqual((x - y).value, sum)
            self.assertEqual((x - y).unit, x.unit)
            self.assertEqual(x.__rsub__(y).value, -sum)
            self.assertEqual(x.__rsub__(y).unit, x.unit)

        #test for error if incompatible units
        q1 = PhysicalQuantity('1cm')
        q2 = PhysicalQuantity('1kg')

        try:
            q1 - q2
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("expecting TypeError")

        #test offset units
        q1 = PhysicalQuantity('1degK')
        q2 = PhysicalQuantity('1degR')
        q3 = PhysicalQuantity('1degC')

        result = q1 - q2
        self.assertAlmostEqual(result.value, .444, 3)
        self.assertEqual(result.unit, q1.unit)

        try:
            q3 - q2
        except TypeError as err:
            msg = "Unit conversion (degR to degC) cannot be expressed as a simple multiplicative factor"
            self.assertEqual(str(err), msg)
        else:
            self.fail('expecting TypeError')
Exemplo n.º 28
0
    def test_in_units_of(self):
        #in_units_of should return a new PhysicalQuantity with the requested
        #unit, leaving the old unit as it was

        x = PhysicalQuantity('5cm')
        y = x.in_units_of('m')
        self.assertEqual(y, PhysicalQuantity('0.05m'))
        self.assertEqual(x, PhysicalQuantity('5cm'))

        x = PhysicalQuantity('5cm')
        try:
            y = x.in_units_of('degC')
        except TypeError as err:
            self.assertEqual(str(err), 'Incompatible units')
        else:
            self.fail("TypeError expected")
Exemplo n.º 29
0
    def test_conversion_tuple_to(self):
        #test_conversion_tuple_to shoudl error when units have different power lists

        w = PhysicalQuantity('1cm')
        x = PhysicalQuantity('1m')
        y = PhysicalQuantity('1degF')
        z1 = PhysicalQuantity('1degC')
        z2 = PhysicalQuantity('1degK')

        #check for non offset units
        self.assertEqual(w.unit.conversion_tuple_to(x.unit), (1 / 100.0, 0))

        #check for offset units
        result = y.unit.conversion_tuple_to(z1.unit)
        self.assertAlmostEqual(result[0], 0.556, 3)
        self.assertAlmostEqual(result[1], -32.0, 3)

        #check for incompatible units
        try:
            x.unit.conversion_tuple_to(z1.unit)
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("Expecting TypeError")
Exemplo n.º 30
0
    def test_mul_known_Values(self):
        #multiplication should give known result with known input
        #the unit of the product should be the product of the units

        #PhysicalQuanity * scalar
        x = PhysicalQuantity('1cm')
        y = 12.3
        self.assertEqual(x * y, PhysicalQuantity('12.3cm'))
        self.assertEqual(y * x, PhysicalQuantity('12.3cm'))

        #PhysicalQuantity * PhysicalQuantity
        x = PhysicalQuantity('1cm')
        y = PhysicalQuantity('1cm')
        z = PhysicalQuantity('1cm**-1')
        self.assertEqual((x * y).value, 1)
        self.assertEqual((x * y).unit, x.unit * y.unit)
        self.assertEqual(str(x * y), '1.0 cm**2')

        #multiplication where the result is dimensionless
        self.assertEqual((x * z), 1.0)
        self.assertEqual(type(x * z), float)
        self.assertEqual(str(x * z), '1.0')

        x = PhysicalQuantity('7kg')
        y = PhysicalQuantity('10.5m')
        self.assertEqual((x * y).value, 73.5)
        self.assertEqual((x * y).unit, x.unit * y.unit)
        self.assertEqual(str(x * y), '73.5 kg*m')

        #test for error from offset units
        z = PhysicalQuantity('1degC')
        try:
            x * z
        except TypeError as err:
            self.assertEqual(str(err),
                             "cannot multiply units with non-zero offset")
        else:
            self.fail("TypeError expected")
Exemplo n.º 31
0
def unit_xform(node, in_units, out_units):
    """Transforms an ast into expr*scaler+adder where scaler
    and adder are from units conversion.
    """
    inpq = PhysicalQuantity(1.0, in_units)
    outpq = PhysicalQuantity(1.0, out_units)
    try:
        scaler, adder = inpq.unit.conversion_tuple_to(outpq.unit)
    except TypeError:
        raise TypeError(
            "units '%s' are incompatible with assigning units of '%s'" %
            (inpq.get_unit_name(), outpq.get_unit_name()))
    return scaler_adder_xform(node, scaler, adder)
Exemplo n.º 32
0
    def test_in_base_units(self):
        #in_base_units() should return a new PhysicalQuantity instance
        #using the base units, leaving the original instance intact

        x = PhysicalQuantity(1, '1/h')
        y = x.in_base_units()

        self.assertEqual(y, PhysicalQuantity(1/3600.0, '1/s'))
        self.assertEqual(x, PhysicalQuantity(1, '1/h'))

        x = PhysicalQuantity(1, 'ft**-3')
        y = x.in_base_units()
        self.assertEqual(y, PhysicalQuantity(35.314666721488585, '1/m**3'))

        x = PhysicalQuantity(1, 'ft**3')
        y = x.in_base_units()
        self.assertEqual(y, PhysicalQuantity(0.028316846592000004, 'm**3'))

        x = PhysicalQuantity('5cm')
        y = x.in_base_units()
        self.assertEqual(y, PhysicalQuantity('0.05m'))
        self.assertEqual(x, PhysicalQuantity('5cm'))
Exemplo n.º 33
0
    def test_add_known_Values(self):
        #addition should give known result with known input.
        #he resulting unit should be the same as the unit of the calling instance
        #The units of the results should be the same as the units of the calling instance

        #test addition function for allowed addition values
        known_add_values = (('1m', '5m', 6), ('1cm', '1cm', 2), ('1cm', '1ft',
                                                                 31.48))
        for q1, q2, result in known_add_values:
            x = PhysicalQuantity(q1)
            y = PhysicalQuantity(q2)
            sum = x + y
            self.assertEqual(sum.value, result)
            self.assertEqual(sum.unit, x.unit)

        #test for error if incompatible units
        q1 = PhysicalQuantity('1cm')
        q2 = PhysicalQuantity('1kg')

        try:
            q1 + q2
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("expecting TypeError")

        #test offset units
        q1 = PhysicalQuantity('1degK')
        q2 = PhysicalQuantity('1degR')
        q3 = PhysicalQuantity('1degC')

        result = q1 + q2
        self.assertAlmostEqual(result.value, 1.556, 3)
        self.assertEqual(result.unit, q1.unit)

        try:
            q3 + q2
        except TypeError as err:
            msg = "Unit conversion (degR to degC) cannot be expressed as a simple multiplicative factor"
            self.assertEqual(str(err), msg)
        else:
            self.fail('expecting TypeError')
Exemplo n.º 34
0
    def test_convert_to_unit(self):
        #convert_to_unit should change the unit of the calling instance to the requested new unit
        x = PhysicalQuantity('5cm')
        x.convert_to_unit('m')
        self.assertEqual(x, PhysicalQuantity('0.05m'))

        #Test for no compatible units
        x = PhysicalQuantity('5cm')
        try:
            x.convert_to_unit('kg')
        except TypeError as err:
            self.assertEqual(str(err), 'Incompatible units')
        else:
            self.fail("TypeError expected")

        x = PhysicalQuantity('1.0psi')
        x.convert_to_unit('psf')
        self.assertEqual(x, PhysicalQuantity('144.0psf'))
Exemplo n.º 35
0
    def test_sin_cos_tan_known_Values(self):
        #__sin__ should give known result with known input

        x = PhysicalQuantity('0 rad')
        x.sin()
        self.assertEqual(x.sin(), math.sin(x.value))
        self.assertEqual(x.cos(), math.cos(x.value))
        self.assertEqual(x.tan(), math.tan(x.value))

        x = PhysicalQuantity('1m')
        try:
            x.sin()
        except TypeError as err:
            self.assertEqual(str(err),"Argument of sin must be an angle")
        else:
            self.fail("TypeError expected")

        try:
            x.cos()
        except TypeError as err:
            self.assertEqual(str(err),"Argument of cos must be an angle")
        else:
            self.fail("TypeError expected")

        try:
            x.tan()
        except TypeError as err:
            self.assertEqual(str(err),"Argument of tan must be an angle")
        else:
            self.fail("TypeError expected")
Exemplo n.º 36
0
    def test_nonzero(self):
        #__nonzero__ should return true in a boolean test

        x = PhysicalQuantity('1degK')
        self.assertTrue(x)
Exemplo n.º 37
0
    def test_sin_cos_tan_known_Values(self):
        #__sin__ should give known result with known input

        x = PhysicalQuantity('0 rad')
        x.sin()
        self.assertEqual(x.sin(), math.sin(x.value))
        self.assertEqual(x.cos(), math.cos(x.value))
        self.assertEqual(x.tan(), math.tan(x.value))

        x = PhysicalQuantity('1m')
        try:
            x.sin()
        except TypeError as err:
            self.assertEqual(str(err), "Argument of sin must be an angle")
        else:
            self.fail("TypeError expected")

        try:
            x.cos()
        except TypeError as err:
            self.assertEqual(str(err), "Argument of cos must be an angle")
        else:
            self.fail("TypeError expected")

        try:
            x.tan()
        except TypeError as err:
            self.assertEqual(str(err), "Argument of tan must be an angle")
        else:
            self.fail("TypeError expected")
Exemplo n.º 38
0
 def test_new_units(self):
     # Hour added to test problem in Classic OpenMDAO Ticket 466
     # knot, rev, month added to test problem in Issue 804
     x = PhysicalQuantity('7200s')
     x.convert_to_unit('h')
     self.assertEqual(x, PhysicalQuantity('2h'))
     x = PhysicalQuantity('5knot')
     x.convert_to_unit('nm/h')
     self.assertEqual(x, PhysicalQuantity('5nmi/h'))
     x = PhysicalQuantity('33rev/min')
     x.convert_to_unit('rpm')
     self.assertEqual(x, PhysicalQuantity('33rpm'))
     x = PhysicalQuantity('12mo')
     x.convert_to_unit('yr')
     self.assertEqual(x, PhysicalQuantity('1yr'))
     x = PhysicalQuantity('1Mibyte')
     x.convert_to_unit('Kibyte')
     self.assertEqual(x, PhysicalQuantity('1024Kibyte'))
Exemplo n.º 39
0
def _corrected_massflow(domain, surface, weights, reference_state):
    """
    Returns corrected mass flow for a mesh surface as a
    :class:`PhysicalQuantity`.
    """
    zone_name, imin, imax, jmin, jmax, kmin, kmax = surface
    zone = getattr(domain, zone_name)
    grid = zone.grid_coordinates
    flow = zone.flow_solution
    cylindrical = zone.coordinate_system == CYLINDRICAL
    cell_center = flow.grid_location == CELL_CENTER

    if cylindrical:
        c1 = grid.z.item
        c2 = grid.r.item
        c3 = grid.t.item
    else:
        c1 = grid.x.item
        c2 = grid.y.item
        c3 = grid.z.item

    try:
        density = flow.density.item
        if cylindrical:
            mom_c1 = flow.momentum.z.item
            mom_c2 = flow.momentum.r.item
            mom_c3 = flow.momentum.t.item
        else:
            mom_c1 = flow.momentum.x.item
            mom_c2 = flow.momentum.y.item
            mom_c3 = flow.momentum.z.item
        pressure = flow.pressure.item
    except AttributeError:
        vnames = ('density', 'momentum', 'pressure')
        raise AttributeError('For corrected mass flow, zone %s is missing'
                             ' one or more of %s.' % (zone_name, vnames))
    try:
        gam = flow.gamma.item
    except AttributeError:
        gam = None  # Use passed-in scalar gamma.

    if imin == imax:
        imax += 1  # Ensure range() returns face index.
        face_normal = _iface_normal
        face_value = _iface_cell_value if cell_center else _iface_node_value
    elif jmin == jmax:
        jmax += 1
        face_normal = _jface_normal
        face_value = _jface_cell_value if cell_center else _jface_node_value
    else:
        kmax += 1
        face_normal = _kface_normal
        face_value = _kface_cell_value if cell_center else _kface_node_value

    try:
        lref = reference_state['length_reference']
        pref = reference_state['pressure_reference']
        rgas = reference_state['ideal_gas_constant']
        tref = reference_state['temperature_reference']
        if gam is None:
            gamma = reference_state['specific_heat_ratio'].value
    except KeyError:
        vals = ('length_reference', 'pressure_reference', 'ideal_gas_constant',
                'temperature_reference', 'specific_heat_ratio')
        raise AttributeError('For corrected mass flow, reference_state is'
                             ' missing one or more of %s.' % (vals,))

    rhoref = pref / rgas / tref
    vref = (rgas * tref).sqrt()
    momref = rhoref * vref
    wref = momref * lref * lref

    pstd = PhysicalQuantity(14.696, 'psi')
    pstd.convert_to_unit(pref.get_unit_name())

    tstd = PhysicalQuantity(518.67, 'degR')
    tstd.convert_to_unit(tref.get_unit_name())

    lref = lref.value
    pref = pref.value
    rgas = rgas.value
    rhoref = rhoref.value
    momref = momref.value
    pstd = pstd.value
    tstd = tstd.value
    total = 0.
    for i in range(imin, imax):
        ip1 = i + 1
        for j in range(jmin, jmax):
            jp1 = j + 1
            for k in range(kmin, kmax):
                kp1 = k + 1

                rho = face_value(density, ip1, jp1, kp1) * rhoref
                rvu = face_value(mom_c1, ip1, jp1, kp1) * momref
                rvv = face_value(mom_c2, ip1, jp1, kp1) * momref
                rvw = face_value(mom_c3, ip1, jp1, kp1) * momref
                ps = face_value(pressure, ip1, jp1, kp1) * pref
                if gam is not None:
                    gamma = face_value(gam, ip1, jp1, kp1)
                sc1, sc2, sc3 = face_normal(c1, c2, c3, i, j, k, cylindrical,
                                            lref)

                w = rvu*sc1 + rvv*sc2 + rvw*sc3

                u2 = (rvu*rvu + rvv*rvv + rvw*rvw) / (rho*rho)
                a2 = (gamma * ps) / rho
                mach2 = u2 / a2
                ts = ps / (rho * rgas)
                tt = ts * (1. + (gamma-1.)/2. * mach2)

                pt = ps * pow(1. + (gamma-1.)/2. * mach2, gamma/(gamma-1.))

                wc = w * sqrt(tt/tstd) / (pt/pstd)
                total += wc

    return PhysicalQuantity(total, wref.get_unit_name())
Exemplo n.º 40
0
 def execute(self):
     self.c = PhysicalQuantity(self.a + self.b,
                               'inch').in_units_of('ft').value
     self.d = PhysicalQuantity(self.a - self.b,
                               'inch').in_units_of('ft').value
Exemplo n.º 41
0
    def test_in_base_units(self):
        #in_base_units() should return a new PhysicalQuantity instance
        #using the base units, leaving the original instance intact

        x = PhysicalQuantity(1, '1/h')
        y = x.in_base_units()

        self.assertEqual(y, PhysicalQuantity(1 / 3600.0, '1/s'))
        self.assertEqual(x, PhysicalQuantity(1, '1/h'))

        x = PhysicalQuantity(1, 'ft**-3')
        y = x.in_base_units()
        self.assertEqual(y, PhysicalQuantity(35.314666721488585, '1/m**3'))

        x = PhysicalQuantity(1, 'ft**3')
        y = x.in_base_units()
        self.assertEqual(y, PhysicalQuantity(0.028316846592000004, 'm**3'))

        x = PhysicalQuantity('5cm')
        y = x.in_base_units()
        self.assertEqual(y, PhysicalQuantity('0.05m'))
        self.assertEqual(x, PhysicalQuantity('5cm'))
Exemplo n.º 42
0
    def __init__(self, zone, zone_name, reference_state):
        flow = zone.flow_solution
        cylindrical = zone.coordinate_system == CYLINDRICAL

        # 'pressure' required until we can determine dimensionalized
        # static pressure from 'Q' variables.
        try:
            self.density = flow.density.item
            momentum = flow.momentum
            self.pressure = flow.pressure.item
        except AttributeError:
            vnames = ('density', 'momentum', 'pressure')
            raise AttributeError('For corrected_mass_flow, zone %s is missing'
                                 ' one or more of %s.' % (zone_name, vnames))
        try:
            self.gam = flow.gamma.item
        except AttributeError:
            self.gam = None  # Use passed-in scalar gamma.

        if reference_state is None:
            raise ValueError('corrected_mass_flow must have units specified')

        try:
            lref = reference_state['length_reference']
            pref = reference_state['pressure_reference']
            rgas = reference_state['ideal_gas_constant']
            tref = reference_state['temperature_reference']
            if self.gam is None:
                self.gamma = reference_state['specific_heat_ratio'].value
        except KeyError:
            vals = ('length_reference', 'pressure_reference', 'ideal_gas_constant',
                    'temperature_reference', 'specific_heat_ratio')
            raise AttributeError('For corrected_mass_flow, reference_state is'
                                 ' missing one or more of %s.' % (vals,))

        rhoref = pref / rgas / tref
        vref = (rgas * tref).sqrt()
        momref = rhoref * vref
        self.wref = momref * lref * lref

        pstd = PhysicalQuantity(14.696, 'psi')
        pstd.convert_to_unit(pref.get_unit_name())

        tstd = PhysicalQuantity(518.67, 'degR')
        tstd.convert_to_unit(tref.get_unit_name())

        aref = lref * lref
        self.aref = aref.value
        self.pref = pref.value
        self.rgas = rgas.value
        self.rhoref = rhoref.value
        self.momref = momref.value
        self.pstd = pstd.value
        self.tstd = tstd.value

        if cylindrical:
            self.mom_c1 = None if momentum.z is None else momentum.z.item
            self.mom_c2 = momentum.r.item
            self.mom_c3 = momentum.t.item
        else:
            self.mom_c1 = momentum.x.item
            self.mom_c2 = None if momentum.y is None else momentum.y.item
            self.mom_c3 = None if momentum.z is None else momentum.z.item
Exemplo n.º 43
0
 def test_integers_in_unit_definition(self):
     x = PhysicalQuantity('10 1/min')
     self.assertEqual(x.unit.factor, 1 / 60.0)
     self.assertEqual(x.unit.powers, _get_powers(time=-1))