Skip to content

Commit

Permalink
[BUGFIX] set_operation_model ordering with layout (#867)
Browse files Browse the repository at this point in the history
* Handling for order of set_operation model and setting layout.

* ruff.

* One more test added.
  • Loading branch information
misi9170 authored Apr 5, 2024
1 parent 27fe153 commit 99161f2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion floris/core/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def check_turbine_type(self, attribute: attrs.Attribute, value: Any) -> None:
if len(value) != 1 and len(value) != self.n_turbines:
raise ValueError(
"turbine_type must have the same number of entries as layout_x/layout_y or have "
"a single turbine_type value."
"a single turbine_type value. This error can arise if you set the turbine_type or "
"alter the operation model before setting the layout."
)

@turbine_library_path.validator
Expand Down
18 changes: 14 additions & 4 deletions floris/floris_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,13 +1451,23 @@ def set_operation_model(self, operation_model: str | List[str]):
operation_model (str): The operation model to set.
"""
if isinstance(operation_model, str):
operation_model = [operation_model]*self.core.farm.n_turbines
elif len(operation_model) != self.core.farm.n_turbines:
if len(self.core.farm.turbine_type) == 1:
# Set a single one here, then, and return
turbine_type = self.core.farm.turbine_definitions[0]
turbine_type["operation_model"] = operation_model
self.set(turbine_type=[turbine_type])
return
else:
operation_model = [operation_model]*self.core.farm.n_turbines

if len(operation_model) != self.core.farm.n_turbines:
raise ValueError(
"The length of the operation_model list must be equal to the number of turbines."
)
"The length of the operation_model list must be "
"equal to the number of turbines."
)

turbine_type_list = self.core.farm.turbine_definitions

for tindex in range(self.core.farm.n_turbines):
turbine_type_list[tindex]["turbine_type"] = (
turbine_type_list[tindex]["turbine_type"]+"_"+operation_model[tindex]
Expand Down
35 changes: 35 additions & 0 deletions tests/floris_model_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,38 @@ def test_set_operation_model():
fmodel.set(layout_x=[0, 0], layout_y=[0, 1000])
fmodel.set_operation_model(["simple-derating", "cosine-loss"])
assert fmodel.get_operation_model() == ["simple-derating", "cosine-loss"]

# Check that setting a single turbine type, and then altering the operation model works
fmodel.set(layout_x=[0, 0], layout_y=[0, 1000])
fmodel.set(turbine_type=["nrel_5MW"])
fmodel.set_operation_model("simple-derating")
assert fmodel.get_operation_model() == "simple-derating"

# Check that setting over mutliple turbine types works
fmodel.set(turbine_type=["nrel_5MW", "iea_15MW"])
fmodel.set_operation_model("simple-derating")
assert fmodel.get_operation_model() == "simple-derating"
fmodel.set_operation_model(["simple-derating", "cosine-loss"])
assert fmodel.get_operation_model() == ["simple-derating", "cosine-loss"]

# Check setting over single turbine type; then updating layout works
fmodel.set(turbine_type=["nrel_5MW"])
fmodel.set_operation_model("simple-derating")
fmodel.set(layout_x=[0, 0, 0], layout_y=[0, 1000, 2000])
assert fmodel.get_operation_model() == "simple-derating"

# Check that setting for multiple turbine types and then updating layout breaks
fmodel.set(layout_x=[0, 0], layout_y=[0, 1000])
fmodel.set(turbine_type=["nrel_5MW"])
fmodel.set_operation_model(["simple-derating", "cosine-loss"])
assert fmodel.get_operation_model() == ["simple-derating", "cosine-loss"]
with pytest.raises(ValueError):
fmodel.set(layout_x=[0, 0, 0], layout_y=[0, 1000, 2000])

# Check one more variation
fmodel.set(layout_x=[0, 0], layout_y=[0, 1000])
fmodel.set(turbine_type=["nrel_5MW", "iea_15MW"])
fmodel.set_operation_model("simple-derating")
fmodel.set(layout_x=[0, 0], layout_y=[0, 1000])
with pytest.raises(ValueError):
fmodel.set(layout_x=[0, 0, 0], layout_y=[0, 1000, 2000])

0 comments on commit 99161f2

Please sign in to comment.