Skip to content

Commit

Permalink
add test functions for CostCurve and FuelCurve
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrypotts committed Aug 13, 2024
1 parent 6a184d9 commit 8d4f081
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/infrasys/production_variable_cost_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class CostCurve(ProductionVariableCostCurve):
vom_units: Annotated[
InputOutputCurve,
Field(description="(default: natural units (MW)) The units for the x-axis of the curve"),
] = InputOutputCurve(LinearFunctionData(0.0))
] = InputOutputCurve(
function_data=LinearFunctionData(proportional_term=0.0, constant_term=0.0)
)


class FuelCurve(ProductionVariableCostCurve):
Expand All @@ -44,7 +46,9 @@ class FuelCurve(ProductionVariableCostCurve):
vom_units: Annotated[
InputOutputCurve,
Field(description="(default: natural units (MW)) The units for the x-axis of the curve"),
] = InputOutputCurve(LinearFunctionData(0.0))
] = InputOutputCurve(
function_data=LinearFunctionData(proportional_term=0.0, constant_term=0.0)
)
fuel_cost: Annotated[
float,
Field(
Expand Down
100 changes: 100 additions & 0 deletions tests/test_production_variable_cost_curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from infrasys.production_variable_cost_curve import CostCurve, FuelCurve
from infrasys.function_data import LinearFunctionData
from infrasys.value_curves import InputOutputCurve
from infrasys import Component
from .models.simple_system import SimpleSystem


class CurveComponent(Component):
cost_curve: CostCurve


def test_cost_curve():
# Cost curve
cost_curve = CostCurve(
value_curve=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=1.0, constant_term=2.0)
),
vom_units=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=2.0, constant_term=1.0)
),
)

assert cost_curve.value_curve.function_data.proportional_term == 1.0
assert cost_curve.vom_units.function_data.proportional_term == 2.0


def test_fuel_curve():
# Fuel curve
fuel_curve = FuelCurve(
value_curve=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=1.0, constant_term=2.0)
),
vom_units=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=2.0, constant_term=1.0)
),
fuel_cost=2.5,
)

assert fuel_curve.value_curve.function_data.proportional_term == 1.0
assert fuel_curve.vom_units.function_data.proportional_term == 2.0
assert fuel_curve.fuel_cost == 2.5


def test_value_curve_custom_serialization():
component = CurveComponent(
name="test",
cost_curve=CostCurve(
value_curve=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=1.0, constant_term=2.0)
),
vom_units=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=2.0, constant_term=1.0)
),
),
)

model_dump = component.model_dump(mode="json")
assert model_dump["cost_curve"]["value_curve"]["function_data"]["proportional_term"] == 1.0

model_dump = component.model_dump(context={"magnitude_only": True})
assert model_dump["cost_curve"]["value_curve"]["function_data"]["proportional_term"] == 1.0

model_dump = component.model_dump(mode="json", context={"magnitude_only": True})
assert model_dump["cost_curve"]["value_curve"]["function_data"]["proportional_term"] == 1.0


def test_value_curve_serialization(tmp_path):
system = SimpleSystem(auto_add_composed_components=True)

v1 = CurveComponent(
name="test",
cost_curve=CostCurve(
value_curve=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=1.0, constant_term=2.0)
),
vom_units=InputOutputCurve(
function_data=LinearFunctionData(proportional_term=2.0, constant_term=1.0)
),
),
)
system.add_component(v1)
filename = tmp_path / "value_curve.json"

system.to_json(filename, overwrite=True)
system2 = SimpleSystem.from_json(filename)

assert system2 is not None

v2 = system2.get_component(CurveComponent, "test")

assert v2 is not None
assert isinstance(v1.cost_curve.value_curve.function_data, LinearFunctionData)
assert (
v1.cost_curve.value_curve.function_data.proportional_term
== v2.cost_curve.value_curve.function_data.proportional_term
)
assert (
v1.cost_curve.value_curve.function_data.constant_term
== v2.cost_curve.value_curve.function_data.constant_term
)
3 changes: 0 additions & 3 deletions tests/test_values_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,12 @@ def test_value_curve_custom_serialization():
)

model_dump = component.model_dump(mode="json")
print(model_dump)
assert model_dump["value_curve"]["function_data"]["proportional_term"] == 1.0

model_dump = component.model_dump(context={"magnitude_only": True})
# print(model_dump)
assert model_dump["value_curve"]["function_data"]["proportional_term"] == 1.0

model_dump = component.model_dump(mode="json", context={"magnitude_only": True})
# print(model_dump)
assert model_dump["value_curve"]["function_data"]["proportional_term"] == 1.0


Expand Down

0 comments on commit 8d4f081

Please sign in to comment.