Skip to content

Commit

Permalink
fix in activation order
Browse files Browse the repository at this point in the history
  • Loading branch information
jensdebruijn committed Sep 27, 2024
1 parent 6b0c751 commit b331e01
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions geb/agents/crop_farmers.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ def abstract_water(
activation_order.size, dtype=np.float32
)

has_access_to_irrigation_water = np.zeros(activation_order.size, dtype=np.bool_)
for activated_farmer_index in range(activation_order.size):
farmer = activation_order[activated_farmer_index]
farmer_fields = get_farmer_HRUs(field_indices, field_indices_by_farmer, farmer)
Expand All @@ -439,7 +438,6 @@ def abstract_water(
continue

# Determine whether farmer would have access to irrigation water this timestep. Regardless of whether the water is actually used. This is used for making investment decisions.
farmer_has_access_to_irrigation_water = False
for field in farmer_fields:
grid_cell = HRU_to_grid[field]

Expand All @@ -460,10 +458,6 @@ def abstract_water(
farmer_has_access_to_irrigation_water = True
break

has_access_to_irrigation_water[activated_farmer_index] = (
farmer_has_access_to_irrigation_water
)

# Actual irrigation from surface, reservoir and groundwater
# If farmer doesn't have access to irrigation water, skip the irrigation abstraction
if farmer_has_access_to_irrigation_water:
Expand Down Expand Up @@ -533,18 +527,18 @@ def abstract_water(
)

if surface_irrigated[farmer]:
# irrigation_water_demand_field = withdraw_channel(
# available_channel_storage_m3=available_channel_storage_m3,
# grid_cell=grid_cell,
# cell_area=cell_area,
# field=field,
# farmer=farmer,
# water_withdrawal_m=water_withdrawal_m,
# irrigation_water_demand_field=irrigation_water_demand_field,
# remaining_irrigation_limit_m3=remaining_irrigation_limit_m3,
# channel_abstraction_m3_by_farmer=channel_abstraction_m3_by_farmer,
# minimum_channel_storage_m3=100.0,
# )
irrigation_water_demand_field = withdraw_channel(
available_channel_storage_m3=available_channel_storage_m3,
grid_cell=grid_cell,
cell_area=cell_area,
field=field,
farmer=farmer,
water_withdrawal_m=water_withdrawal_m,
irrigation_water_demand_field=irrigation_water_demand_field,
remaining_irrigation_limit_m3=remaining_irrigation_limit_m3,
channel_abstraction_m3_by_farmer=channel_abstraction_m3_by_farmer,
minimum_channel_storage_m3=100.0,
)
assert water_withdrawal_m[field] >= 0

# command areas
Expand Down Expand Up @@ -608,7 +602,6 @@ def abstract_water(
water_consumption_m,
returnFlowIrr_m,
addtoevapotrans_m,
has_access_to_irrigation_water,
)


Expand Down Expand Up @@ -1517,13 +1510,13 @@ def activation_order_by_elevation(self):
# algorithm such that the random shuffling in the previous step is conserved
# in groups with identical elevation.
activation_order_shuffled = np.argsort(elevation_shuffled, kind="stable")[::-1]
argsort_agend_ids = agent_ids_shuffled[activation_order_shuffled]
# Return the agent ids ranks in the order of activation.
ranks = np.empty_like(argsort_agend_ids)
ranks[argsort_agend_ids] = np.arange(argsort_agend_ids.size)
# unshuffle the agent_ids to get the activation order
activation_order = agent_ids_shuffled[activation_order_shuffled]
if self.model.config["agent_settings"]["fix_activation_order"]:
self.activation_order_by_elevation_fixed = (self.n, ranks)
return ranks
self.activation_order_by_elevation_fixed = (self.n, activation_order)
# Check if the activation order is correct, by checking if elevation is decreasing
assert np.diff(elevation[activation_order]).max() <= 0
return activation_order

@property
def farmer_command_area(self):
Expand Down Expand Up @@ -1632,7 +1625,6 @@ def abstract_water(
water_consumption_m,
returnFlowIrr_m,
addtoevapotrans_m,
self.has_access_to_irrigation_water,
) = abstract_water(
self.model.current_day_of_year - 1,
self.n,
Expand Down

0 comments on commit b331e01

Please sign in to comment.