def testOhmUnits(unit_database_posc): q = ObtainQuantity("ohm/m") assert q.ConvertScalarValue(1, "ohm/km") == 1000
def testSpringDashpotUnits(unit_database_posc): """ Units used to define Spring-Dashpot movements """ q = ObtainQuantity("Ns/m") assert "force per velocity" == q.GetQuantityType() assert approx(abs(q.ConvertScalarValue(1, "lbf.s/ft") - 14.5939029372), 7) == 0 q = ObtainQuantity("Nm/rad") assert "moment per angle" == q.GetQuantityType() assert approx(abs(q.ConvertScalarValue(1, "lbf.ft/dega") - 836.169044926), 7) == 0 q = ObtainQuantity("Nms/rad") assert "moment per angular velocity" == q.GetQuantityType() assert (approx( abs(q.ConvertScalarValue(1, "lbf.ft.s/dega") - 0.017453292519943), 7) == 0)
def testPoscKVmm(unit_database_posc): """ V/m to KV/mm (multiply by 1e6) """ q = ObtainQuantity("V/m") assert q.ConvertScalarValue(1, "KV/mm") == 1e6
def testDefaultValue(unit_database_len_pressure) -> None: """ Scalar constructor considers the minimum and maximum values when default_value is not defined """ db = unit_database_len_pressure db.AddCategory(category="my length", quantity_type="length", min_value=100.0, max_value=200.0) # if the default value is not defined, the scalar should not try to set the initial/first # value of the new instance to 0.0, but it should assume the minimum allowed value. # should not raise ValueError length = Scalar("my length") assert length.GetValue() == 100.0 length = Scalar(ObtainQuantity("m", "my length")) assert length.GetValue() == 100.0 length = Scalar(ObtainQuantity("m")) assert length.GetValue() == 0.0 # invalid default value (< min) with pytest.raises(AssertionError): db.AddCategory( category="my pressure", quantity_type="pressure", default_value=50.0, min_value=100.0, max_value=200.0, ) # invalid default value (> max) with pytest.raises(AssertionError): db.AddCategory( category="my pressure", quantity_type="pressure", default_value=300.0, min_value=100.0, max_value=200.0, ) # invalid default value (<= min) with pytest.raises(AssertionError): db.AddCategory( category="my pressure", quantity_type="pressure", default_value=100.0, min_value=100.0, max_value=200.0, is_min_exclusive=True, ) # invalid default value (>= min) with pytest.raises(AssertionError): db.AddCategory( category="my pressure", quantity_type="pressure", default_value=200.0, min_value=100.0, max_value=200.0, is_max_exclusive=True, ) db.AddCategory( category="my pressure", quantity_type="pressure", min_value=100.0, max_value=200.0, default_value=150.0, is_min_exclusive=True, is_max_exclusive=False, ) pressure = Scalar("my pressure") assert pressure.GetValue() == 150.0 # default_value not informed. checking if the interval limits are respected # when is_min_exclusive or is_max_exclusive have been informed, so the default_value # must be with pytest.raises(RuntimeError): db.AddCategory( category="my pressure 2", quantity_type="pressure", min_value=100.0, max_value=200.0, is_max_exclusive=True, )
def testReadOnlyQuantityCopy(unit_database_well_length) -> None: q0 = ObtainQuantity("m", "well-length") q1 = q0.Copy() assert q0 == q1
def testQuantityEq(unit_database_well_length) -> None: q0 = ObtainQuantity("m", "well-length") q1 = ObtainQuantity("m", "well-length") assert q0 == q1
def testOperation() -> None: unit_database = UnitDatabase.GetSingleton() unit_database.CheckDefaultUnitDatabase() q = ObtainQuantity(unit="m", category="length") SCALAR_OPERATION = [ operator.add, operator.sub, operator.truediv, operator.mul, operator.mod ] SCALAR_TYPES = (int, float) for operation in SCALAR_OPERATION: for cast_type in SCALAR_TYPES: # Left q2 = operation(cast_type(2), q) assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" # Right q2 = operation(q, cast_type(2)) assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = abs(q) assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = q + q assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = q - q assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = q * int(2) assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = q * 2.0 assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = q * q assert q2.GetQuantityType() == "(length) ** 2" assert q2.GetUnit() == "m2" q2 = q * q * q assert q2.GetQuantityType() == "(length) ** 3" assert q2.GetUnit() == "m3" # Check pow q2 = q**1 assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = q**2 assert q2.GetQuantityType() == "(length) ** 2" assert q2.GetUnit() == "m2" q2 = q**3 assert q2.GetQuantityType() == "(length) ** 3" assert q2.GetUnit() == "m3" q2 = pow(q, 3) assert q2.GetQuantityType() == "(length) ** 3" assert q2.GetUnit() == "m3" q2 = (q * q) / q assert q2.GetQuantityType() == "length" assert q2.GetUnit() == "m" q2 = (q * q) / ObtainQuantity(unit="d", category="time") assert q2.GetQuantityType() == "(length) ** 2 / time" assert q2.GetUnit() == "m2/d" with pytest.raises(TypeError): operator.mul(q, "s")