def testCurveRepr(unit_database): q1 = ObtainQuantity("m", "length") q2 = ObtainQuantity("d", "time") curve = Curve(Array(q1, []), Array(q2, [])) assert "Curve(m, d)[]" == repr(curve) curve = Curve(Array(q1, list(range(3))), Array(q2, list(range(3)))) assert "Curve(m, d)[(0, 0) (1, 1) (2, 2)]" == repr(curve) curve = Curve(Array(q1, list(range(100))), Array(q2, list(range(100)))) expected = ("Curve(m, d)[(0, 0) (1, 1) (2, 2) (3, 3) (4, 4) (5, 5) " "(6, 6) (7, 7) (8, 8) (9, 9) (10, 10) (11, 11) " "(12, 12) (13, 13) (14, 14) (15, 15) (16, 16) " "(17, 17) (18, 18) (19, 19) (20, 20) ... ]") assert repr(curve) == expected
def testSlice(unit_database) -> None: quantity = ObtainQuantity("m", "length") def MakeArray(values): return Array(quantity, values=values) domain = MakeArray([5, 10, 20, 30, 40, 50]) image = MakeArray([0, 1, 2, 3, 4, 5]) curve = Curve(image, domain) assert curve[0] == (5, 0) assert curve[:3] == ([5, 10, 20], [0, 1, 2]) assert curve[:] == ([5, 10, 20, 30, 40, 50], [0, 1, 2, 3, 4, 5])
def testCurves(unit_database) -> None: import numpy r = ObtainQuantity("m", "length") values10 = Array(r, values=numpy.array(list(range(10)), dtype=numpy.int32)) values9 = Array(r, values=numpy.array(list(range(9)), dtype=numpy.int32)) domain9 = Array("time", values=numpy.array(list(range(9)), dtype=numpy.int32), unit="s") domain10 = Array("time", values=numpy.array(list(range(10)), dtype=numpy.int32), unit="s") with pytest.raises(ValueError): Curve(values10, domain9) # type:ignore[arg-type] c = Curve(values10, domain10) # type:ignore[arg-type] with pytest.raises(ValueError): c.SetDomain(domain9) # type:ignore[arg-type] with pytest.raises(ValueError): c.SetImage(values9) # type:ignore[arg-type]
def test_to_curve(): type_error_msg = r"Expected pair \(image_array, domain_array\) or Curve" assert to_curve(None, is_optional=True) is None with pytest.raises(TypeError, match=type_error_msg): to_curve(None) image = Array("length", [1, 2, 3], "m") domain = Array("time", [0, 1.1, 2.2], "s") curve = Curve(image, domain) assert to_curve((([1, 2, 3], "m"), domain)) == curve with pytest.raises( TypeError, match=r"Curve image: Expected pair \(values, unit\) or Array" ): to_curve((("foo", [1, 2, 3], "m"), domain)) with pytest.raises(TypeError, match=type_error_msg): to_curve(("foo", [1, 2, 3], "m")) with pytest.raises(TypeError, match=type_error_msg): to_curve("foo") assert to_curve(curve) is curve
def to_curve(value: CurveLike, is_optional: bool = False, *, error_context: Optional[str] = None) -> Curve: """ Converter to be used with attr.ib, accepts tuples and Scalar as input, is used by `attrib_curve`. If `is_optional` is defined, the converter will also accept None, same as `to_scalar`. The `error_context` has the same behavior as in `to_scalar`. Ex.: @attr.s class Foo: bar = attrib_curve() Foo(bar=Curve(Array([1,2,3],"m"), Array([0,10,20],"s"))) Foo(bar=(Array([1,2,3],"m"), Array([0,10,20],"s"))) Foo(bar=(([1,2,3],"m"), ([0,10,20],"s"))) Foo(bar=(([1,2,3],"m"), Array([0,10,20],"s"))) """ if is_optional and value is None: return value if is_two_element_tuple(value): image = to_array(value[0], error_context=prepare_error_message( "Curve image", error_context)) domain = to_array(value[1], error_context=prepare_error_message( "Curve domain", error_context)) return Curve(image, domain) elif isinstance(value, Curve): return value message = prepare_error_message( f"Expected pair (image_array, domain_array) or Curve, got {value!r} (type: {type(value)})", error_context, ) raise TypeError(message)
): to_curve((("foo", [1, 2, 3], "m"), domain)) with pytest.raises(TypeError, match=type_error_msg): to_curve(("foo", [1, 2, 3], "m")) with pytest.raises(TypeError, match=type_error_msg): to_curve("foo") assert to_curve(curve) is curve @pytest.mark.parametrize( "attrib_creator, default", [ (attrib_scalar, Scalar(1, "m")), (attrib_array, Array([1, 2, 3], "m")), (attrib_curve, Curve(Array([1, 2, 3], "m"), Array([0, 1.1, 2.2], "s"))), ], ) def test_attrib_category_miss_match(attrib_creator: Callable, default: Any) -> None: with pytest.raises(ValueError, match="category and `category` must match"): attrib_creator(default, category="time") assert isinstance(attrib_creator(default), _CountingAttr) assert isinstance(attrib_creator(default, category="length"), _CountingAttr) @pytest.mark.parametrize( "attrib_creator, default", [ (attrib_scalar, (1, "m")), (attrib_array, ([1, 2, 3], "m")),
filler_material="Cement", material_above_filler="Cement", ) GAS_LIST_VALVE_DESCRIPTION = case_description.GasLiftValveEquipmentDescription( position=Scalar(800.0, "m"), diameter=Scalar("diameter", 1.0, "cm"), valve_type=constants.ValveType.CheckValve, delta_p_min=Scalar(4.0e6, "Pa"), discharge_coefficient=Scalar(0.826, "-"), ) HEAT_SOURCE_DESCRIPTION = case_description.HeatSourceEquipmentDescription( start=Scalar(200.0, "m"), length=Scalar(550.0, "m"), power=Scalar(20.0e3, "W"), power_curve=Curve( Array("power", [2e4, 2.1e4, 2.3e4], "W"), Array("time", [0, 10, 20], "h") ), ) INITIAL_CONDITIONS_DESCRIPTION = case_description.InitialConditionsDescription( pressures=case_builders.build_constant_initial_pressure_description(50.0, "bar"), velocities=case_builders.build_constant_initial_velocities_description( { constants.FLUID_GAS: Scalar(0.0, "m/s"), constants.FLUID_OIL: Scalar(0.0, "m/s"), constants.FLUID_WATER: Scalar(0, "m/s"), } ), volume_fractions=case_builders.build_constant_initial_volume_fractions_description( { constants.FLUID_GAS: Scalar("volume fraction", 0.999, "-"), constants.FLUID_OIL: Scalar("volume fraction", 0.001, "-"),