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)
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()
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()
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'))
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'))
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
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())
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