-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_cost_pop.m
231 lines (167 loc) · 7.16 KB
/
get_cost_pop.m
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
%....................Cost function............%
function cost = get_cost_pop(x)
global max_col;
global max_row;
global filename;
global index_wells;
global glo_stress_period;
global all_cost;
global well_position_mat;
global cp;
global gen_wise_data;
global top;
f_name = [filename '.mfs'];
cost = ones([size(x,1) 2]);
ra = ones([size(x,1) 3])
d = ones([size(x,1) 1]);
..............read well data to update file...%
% well_data = read_well();
% well_data.discharge(:) = x;
% write_well(well_data);
total_discharge = 0;
crop_data = readtable('crop_values.csv');
well_groups = size(index_wells,1);
for p = 1:size(x,1)
cd(['.\' filename '_MODFLOW'])
temp = h5read([filename '.h5'],'///Well/07. Property');
arcs = size(temp,2) - size(index_wells,2);
xn = x(p,1:well_groups);
temp = h5read([filename '.h5'],'///Well/07. Property');
total_discharge = 0;
for i = 1:numel(xn)
temp(:,[false([1 arcs]) index_wells(i,:)],1) = ones(glo_stress_period,sum(index_wells(i,:))).*xn(i);
% total discharge calculated
total_discharge = total_discharge + xn(i)*sum(index_wells(i,:));
end
h5write([filename '.h5'],'///Well/07. Property',temp);
[s t] = system(sprintf('mf2k_h5.exe "%s" ', f_name));
head_data = readDat([filename '.hed']);
time_head = [head_data.time];
temp_head = {head_data.values};
temp_head = cat(3,temp_head{:});
temp = readDat([filename '.drw']);
drawdown = temp(end).values;
cd('.\..')
%% pollutant part
h_val = [ temp_head(32,78,15), temp_head(37,62,15), temp_head(125,28,15), temp_head(142,51,15) ];
porosity = 0.30;
conc_value = (x(p,(well_groups+1):end)./h_val).*(0.015/porosity);
change_conc(conc_value);
%% Run MT3DMS
run_mt3dms();
%----------------------------------
%% READ THE CONCENTRATION OUTPUT FILE
cd(['.\' filename '_MT3DMS'])
ucn_data = readMT3D('MT3D001.UCN');
cd('.\..')
%% Calculate environmental cost
t_steps = [ucn_data.time];
t_diff = time_head - [0, time_head(1:end-1)];
temp = abs(t_steps - time_head');
cols = zeros([numel(time_head) 1]);
%correction for small random time steps
for i = 1:size(temp,1)
cols(i) = find(min(temp(i,:))==temp(i,:));
end
% id = t_diff<50 & t_diff>1;
id = cols;
conc_data = {ucn_data.values};
conc_data = cat(3,conc_data{:});
conc_data = conc_data(:,:,id);
weighted_conc = zeros([numel(t_steps) 1]);
% for steps = 1:numel(t_steps)
% temp = conc_data(:,:,steps)~=-999;
% temp2 = conc_data(:,:,steps);
% weighted_conc(steps) = mean(temp2(temp))*t_diff(steps);
% end
% mean_conc =sum(weighted_conc)/sum(t_diff);
conc_threshold = 40;
affected_area = sum(sum(conc_data(:,:,end)>conc_threshold));
%--------------------------------------
%conc_river = conc_data(,,end);
%--------------------------------------------
% Runmodflow
if(contains(t,'Error'))
cost(p,1) = 1000000;
cost(p,2) = 1000000;
cost(p,3) = 1000000;
cost(p,4) = 1000000;
% cost
else
if size(head_data,1)<glo_stress_period
% penalty multiplied by the number of unconverged stress periods
cost(p,1) = 100000*(glo_stress_period-size(head_data,1));
cost(p,2) = 100000*(glo_stress_period-size(head_data,1));
cost(p,3) = 100000*(glo_stress_period-size(head_data,1));
cost(p,4) = 100000*(glo_stress_period-size(head_data,1));
else
% Calculate Energy cost
hft = repmat(top,1,1,23) - temp_head; % hft: head from top
const = 9.8 /0.75 * 0.26; % constant = gamma / efficiency * unit cost
energy_cost = zeros([numel(t_diff) 1]);
for steps = 1:numel(t_diff)
temp_hft = hft(:,:,steps);
for communes = 1:size(index_wells,1)
% constant * Head Difference * Required Discharge
temp_hft_well = temp_hft(well_position_mat(index_wells(communes,:)));
temp_hft_well = temp_hft_well(temp_hft_well<80);
energy_cost(steps) = energy_cost(steps) + abs(const*sum(temp_hft_well)*x(p,communes)*t_diff(steps));
end
end
total_energy_cost = sum(energy_cost)/4.67; % 4.67 years in the total simulation time
%calculate food production
food_poly = zeros([numel(conc_value) 1]);
fel_val = x(p,(well_groups+1):end);
for poly = 1:numel(fel_val)
ids = crop_data.polygon == poly;
temp_data = crop_data(ids,:);
temp_npk = [ temp_data.max_yield.*(1-temp_data.b_nut.*exp(-temp_data.c_N*fel_val(poly))), ...
temp_data.max_yield.*(1-temp_data.b_nut.*exp(-temp_data.c_P2O5*fel_val(poly))), ...
temp_data.max_yield.*(1-temp_data.b_K2O.*exp(-temp_data.c_K2O*fel_val(poly))) ];
temp_food = min(temp_npk, [] ,2);
food_poly(poly) = sum(temp_food.*temp_data.Area);
end
% Energy consumed to be minimized
cost(p,1) = total_energy_cost;
% discharge -> negative, (maximization)
cost(p,2) = total_discharge; % total_discharge
% total area contaminated to be minimized so positive
cost(p,3) = affected_area*250*250;
% total food to be maximized
cost(p,4) = -1*sum(food_poly)*1000; %converting tonnes to kg
% add constraints to drawdown
dd = drawdown(well_position_mat);
dd_threshold = 2;
index = dd>dd_threshold;
dd_distance = sqrt(sum((dd(index)-dd_threshold).^2));
% drawndown should not be greater than 2.
% decreasing the magnitude as penalty
cost(p,1) = cost(p,1) + cp*dd_distance*1e4 ;
cost(p,2) = cost(p,2) + cp*dd_distance ;
cost(p,3) = cost(p,3) + cp*dd_distance*100;
cost(p,4) = cost(p,4) + cp*dd_distance ;
d(p) = dd_distance;
% only valid solutions without penalty are recorded in the
% variable all_cost
%%%%%%%%%%% R-A exchanges %%%%%%%%%%%%%%%%%%%
cd(['.\' filename '_MODFLOW']);
temp2 = reading_ccf();
cd('.\..');
temp2 = temp2{7,glo_stress_period*8};
total_leakage_out = sum(temp2(temp2(:)<0));
total_leakage_in = sum(temp2(temp2(:)>0));
ids = temp2~=0;
% leakage out is negative and is needed to be maximised
ra(p,1) = total_leakage_out;
ra(p,2) = total_leakage_in;
temp2 = conc_data(:,:,end);
ra(p,3) = sum(sum(temp2(ids)));
end
end
end
gen_wise_data{end+1,1} = {x};
gen_wise_data{end,2} = {cost};
gen_wise_data{end,3} = {d};
gen_wise_data{end,4} = {ra};
all_cost = vertcat(all_cost,[cost d x ra]);
end