-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDRIP_allocation_routine.jl
76 lines (61 loc) · 2.71 KB
/
DRIP_allocation_routine.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## This file defines a deterministic algorithm for DRIP, and can be used as a
#skeleton as we decide how to include optimization components
## ASSUMPTION 1: All supply is upstream of all demand - we may want to alter
## this with some sort of distance variable for all the nodes, but might be
## unnecesarily complicated for now
function DRIP_allocation_routine(demand_nodes::Array{}, supply_nodes::Array{},
reservoir_nodes::Array{}, numyears::Int64)
#get maximum priority number
max_demand_priority = 0
for node = 1:length(demand_nodes)
max_demand_priority = max(max_demand_priority, demand_nodes[node]["priority"])
end
#sort demand nodes
demand_nodes= sort(demand_nodes, by = x -> x["priority"])
#track the total current supply
current_supply= 0.
#track how much of each demand node gets demand_frac_filleded
demand_frac_filled = zeros(length(demand_nodes))
for m = 1:numyears * 12
s_index = m
m_index = m%12
if m_index == 0
m_index = 12
end
#add the monthly supply to the current_supply, assuming the supply is in
#cubic feet per second
for node = 1:length(supply_nodes)
current_supply += supply_nodes[node]["inflow"][:CMS][s_index] * 60*60*24*modays[m_index]
end
#loop over the demand
current_node = 1
for p = 1:max_demand_priority
current_set = []
while current_node <= length(demand_nodes) && demand_nodes[current_node]["priority"] == p
push!(current_set, demand_nodes[current_node])
current_node += 1
end
#calculate the total demand for this priority set, and get all of the
#Locs for the priority set to use later
total_demand = 0.
Locs = []
for node = 1:length(current_set)
total_demand += current_set[node]["size"] * current_set[node]["rate"][m_index]
push!(Locs, current_set[node]["Loc"])
end
#if there is enough supply, allocate all of it to these nodes,
#subtract from current_supply, mark these demand nodes as taken
#care of 100%, and move on
if current_supply >= total_demand
current_supply -= total_demand
demand_frac_filled[Locs] = 1
#if there is not enough supply, allocate it equally between these
#nodes, mark these nodes as taken care of x%, and break out of loop
else
demand_frac_filled[Locs] = current_supply / total_demand
current_supply = 0.
end
end
end
return demand_frac_filled
end