-
Notifications
You must be signed in to change notification settings - Fork 1
/
disolv.py
279 lines (209 loc) · 9.1 KB
/
disolv.py
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 7 13:22:42 2018
@author: sarcol
"""
import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import SolveEquation
def run(InDir, OutDir, calibrate=False, convertFEC=True, method='SLSQP',**kwargs):
"""
Collins, S and Bianchi, M. (2019) DISOLV: A Python package for the
interpretation of borehole dilution tests. Groundwater.
"""
def ConvertFEC(InData, Temp):
Concentrations = np.zeros(len(InData))
for i in range(len(InData)):
if InData[i] < 10000:
FEC20 = InData[i]/(1 + 0.024 * (Temp - 20))
Concentrations[i] = (1870 - np.sqrt(1870**2 - 160 * FEC20))/80
else:
FEC23 = InData[i]/(1 + 0.024 * (Temp - 23))/1000/1000 * 100
Concentrations[i] = (5.9738E-7 * FEC23**6 - 3.5136E-5 *
FEC23**5 + 7.823E-4 * FEC23**4 - 8.0334E-3 * FEC23**3 +
4.0791E-2 * FEC23**2 + 3.4996E-2 * FEC23 + 3.6104E-2) * 58.44
return Concentrations
# --------------------Minimise parameters-------------------------
minimise_param = {}
if kwargs is not None:
minimise_param.update(kwargs)
minimise_param.pop('fun',None)
minimise_param.pop('x0',None)
minimise_param.pop('method',None)
minimise_param.pop('constraints',None)
# ---------------------Get input parameters-----------------------
InDat = os.path.join(InDir, "in.csv")
Parameters = np.genfromtxt(InDat, delimiter=',', skip_footer=1)[:-1, 0]
if len(Parameters) < 8:
raise Exception("There are variables missing from the input file.",
"Please check.")
GWlevel = Parameters[0]
BHdepth = Parameters[1]
z = Parameters[2]
A = Parameters[3]
alpha = Parameters[4]
Dd = Parameters[5]
Cc = Parameters[6]
Temp = Parameters[7]
SatColumn = z * round((BHdepth - GWlevel)/z)
Bounds = np.genfromtxt(InDat, delimiter=',', skip_header=16,
skip_footer=1)[:4]
t = np.genfromtxt(InDat, delimiter=',', skip_header=18)
t = t[np.isfinite(t)]
t = np.concatenate((np.array([0]), t))
# ----------------------------Check inputs------------------------
in_con_raw = np.genfromtxt(os.path.join(InDir, "initialcondition.csv"),
delimiter=',', skip_header=1)
# ------------------Check for observation data--------------------
ObsFile = os.path.join(InDir, "measuredprofiles.csv")
ObsExist = os.path.isfile(ObsFile)
if ObsExist:
df = pd.read_csv(os.path.join(ObsFile))
ObsProfilesRaw = df.values
NObs = int(np.shape(ObsProfilesRaw)[1]/2)
else:
ObsProfilesRaw = np.copy(in_con_raw)
ObsProfilesRaw[:, 1] = np.NaN
calibrate = False
NObs = 0
if len(t)-1 != NObs:
raise Exception("{} output times given, but {}".format(len(t)-1, NObs),
" observation profiles found. These should be equal.")
if len(np.shape(in_con_raw)) == 1:
raise Exception('initialcondition.csv must have two columns:',
' depth and concentration')
# ------------------------Print information-------------------------
if not calibrate:
print("No automatic calibration")
else:
print("Automatic calibration")
print("Equation will be solved at times " +
str(t[1:].tolist()).strip('[]'))
print(str(NObs) + " measured profiles have been found")
# ----------------------------Grid---------------------------------
N_nodes = int((SatColumn+z)/z)
x = np.linspace(0, SatColumn, N_nodes)
# ---------Initial condition and observations-----------------------
ObservedProfiles = np.zeros([len(x), NObs])
if convertFEC:
Concentrations = ConvertFEC(in_con_raw[:, 1], Temp)
for i in range(NObs):
ObsProfilesRaw[:, 2 * i + 1] = ConvertFEC(
ObsProfilesRaw[:, 2 * i + 1], Temp)
else:
Concentrations = in_con_raw[:, 1]
in_con = np.interp(x, in_con_raw[:, 0]-GWlevel, Concentrations)
# -----Oberservation x points for automatic calibration-------------
for i in range(NObs):
ObservedProfiles[:, i] = np.interp(x, ObsProfilesRaw[:, 2 * i]
- GWlevel, ObsProfilesRaw[:, 2 * i + 1])
# -------------------Put flows in arrays------------------------
indata = np.genfromtxt(os.path.join(InDir, "flows.csv"),
delimiter=',', skip_header=1)
Nflows = np.shape(indata)[0]
indata[:, 0] = indata[:, 0] - GWlevel
indata[:, 2:] = indata[:, 2:] - GWlevel
for i in range(Nflows):
indata[i, 0] = round(indata[i, 0], 1)
inflows = np.zeros([Nflows, 2])
outflows = np.zeros([Nflows, 2])
for i in range(Nflows):
if indata[i, 1] < 0:
outflows[i, 0] = indata[i, 0]
outflows[i, 1] = -indata[i, 1]
if indata[i, 1] > 0:
inflows[i, 0] = indata[i, 0]
inflows[i, 1] = indata[i, 1]
# -------------------Call function that solves equation----------------
if not calibrate:
sim_profiles = SolveEquation.forward(N_nodes, inflows, outflows, z,
alpha, Cc, Nflows, in_con, t,
A, Dd)
# -------------------------Automatic calibration------------------------
else:
FracBounds = ()
if np.shape(indata)[1] == 4:
for i in range(Nflows):
FracBounds = FracBounds + ((indata[i, 2], indata[i, 3]), )
else:
for i in range(Nflows):
FracBounds = FracBounds + ((indata[i, 0], indata[i, 0]), )
output = SolveEquation.inverse(N_nodes, inflows, outflows, z, alpha,
Cc, Nflows, in_con, t, A,
ObservedProfiles, Bounds, FracBounds,
method, Dd, minimise_param)
alpha = output.x[0]
inflows[:, 1] = output.x[1:(Nflows + 1)]/np.sum(
output.x[1:(Nflows + 1)]) * output.x[-1]
outflows[:, 1] = output.x[(Nflows + 1):-(Nflows + 1)]/np.sum(
output.x[(Nflows + 1):-(Nflows + 1)]) * output.x[-1]
inflows[:, 0] = output.x[-(Nflows + 1):-1]
outflows[:, 0] = output.x[-(Nflows + 1):-1]
# ------Make inflos/outflows consistent with grid
for i in range(Nflows):
inflows[i, 0] = x[int(inflows[i, 0]/z)]
outflows[i, 0] = x[int(outflows[i, 0]/z)]
sim_profiles = SolveEquation.forward(N_nodes, inflows, outflows, z,
alpha, Cc, Nflows, in_con, t, A,
Dd)
out_values = np.zeros([N_nodes, NObs])
for j in range(N_nodes):
for i in range(len(t)-1):
out_values[j, i] = sim_profiles[i + 1, j]
colheads = []
for i in range(len(t)-1):
colheads.append("t = " + str(t[i+1]))
dfout = pd.DataFrame(data=out_values, index=x+GWlevel, columns=colheads)
dfout.index.name = "Depth [L]"
dfout.to_csv(os.path.join(OutDir, "profiles.csv"))
if calibrate:
out = "Dispersivity, " + str(alpha) + "\nFlow rates\nDepth [L]," +\
"Flow [L^2T^-1]\n"
for i in range(Nflows):
if inflows[i, 1] != 0:
out = out + str(inflows[i, 0] + GWlevel) + ',' +\
str(inflows[i, 1]) + '\n'
else:
out = out + str(outflows[i, 0] + GWlevel) + ',' +\
str(-outflows[i, 1]) + '\n'
f = open(os.path.join(OutDir, 'Output.csv'), 'w')
for item in out:
f.write("%s" % item)
f.close()
# ----------------------Plot results------------------------------
plt.figure(figsize=(5, 8))
for i in range(len(t)-1):
plt.plot(sim_profiles[i + 1, :], x + GWlevel, label=str(t[i + 1]))
if ObsExist:
plt.scatter(ObsProfilesRaw[:, 2*i + 1], ObsProfilesRaw[:, 2*i])
plt.legend(title="Time")
plt.xlabel('Salinity (kg/m$^3$)')
plt.ylabel('Depth below ground (m)')
plt.gca().invert_yaxis()
plt.savefig(os.path.join(OutDir, 'profiles.png'))
plt.close()
# ---------------------Calculate RMSE-------------------------------
if ObsExist:
rmse = np.sqrt(np.nansum((sim_profiles[1:, :] -
ObservedProfiles.T)**2)/(N_nodes*len(t)))
print("RMSE: " + str(rmse))
def main():
"""
Run the solver with default arguments
"""
my_parser = argparse.ArgumentParser()
my_parser.add_argument('-indir', default='Input')
my_parser.add_argument('-outdir', default='Output')
my_parser.add_argument('-calibrate', action='store_true')
my_parser.add_argument('-convertFEC', action='store_true')
my_parser.add_argument('-method', default='SLSQP')
args = my_parser.parse_args()
in_dir = os.path.join(os.getcwd(), args.indir)
out_dir = os.path.join(os.getcwd(), args.outdir)
run(in_dir, out_dir, calibrate=args.calibrate,
convertFEC=args.convertFEC, method=args.method)
if __name__ == "__main__":
main()