-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCTc.py
421 lines (340 loc) · 13.2 KB
/
CTc.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import numpy as np
import matplotlib.pyplot as plt
import copy
import tkinter as tk
from tkinter import ttk
"""A 1D implementation of thermal conductivity through soil and air"""
__author__ = "Alexander H. Jarosch ([email protected])"
__date__ = "2020-01-29 -- 2021-03-04"
__copyright__ = "Copyright (C) 2020 ThetaFrame Solutions"
__license__ = "GNU GPL Version 3"
__version__ = "7.0"
"""
This script is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This script is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this script. If not, see <http://www.gnu.org/licenses/>.
"""
# time constant as we calculate in SI
sec_in_year = 365.25 * 24 * 3600
window = tk.Tk()
window.title("Cave Heat Conduction Model with Snow | ver 7.0")
window.geometry('700x600')
# input for depth
lbl_dc = tk.Label(window, text="Depth to Simulate [m]")
lbl_dc.grid(column=0, row=0, sticky="W")
txt_dc = tk.Entry(window, width=10)
txt_dc.insert(tk.END, '500.0')
txt_dc.grid(column=1, row=0)
lbl_zres = tk.Label(window, text="Vertical Resolution N")
lbl_zres.grid(column=0, row=1, sticky="W")
txt_zres = tk.Entry(window, width=10)
txt_zres.insert(tk.END, '251')
txt_zres.grid(column=1, row=1)
# input for jan tmp
lbl_jaT = tk.Label(window, text="January Mean TMP [C]")
lbl_jaT.grid(column=0, row=2, sticky="W")
txt_jaT = tk.Entry(window, width=10)
txt_jaT.insert(tk.END, '-12.0')
txt_jaT.grid(column=1, row=2)
# input for jul tmp
lbl_juT = tk.Label(window, text="July Mean TMP [C]")
lbl_juT.grid(column=0, row=3, sticky="W")
txt_juT = tk.Entry(window, width=10)
txt_juT.insert(tk.END, '8.0')
txt_juT.grid(column=1, row=3)
# input for snow delta_tmp
lbl_sdT = tk.Label(window, text="Snow delta TMP [C]")
lbl_sdT.grid(column=0, row=4, sticky="W")
txt_sdT = tk.Entry(window, width=10)
txt_sdT.insert(tk.END, '0.0')
txt_sdT.grid(column=1, row=4)
# input for soil temp
lbl_iTg = tk.Label(window, text="Initial Ground TMP [C]")
lbl_iTg.grid(column=0, row=5, sticky="W")
txt_iTg = tk.Entry(window, width=10)
txt_iTg.insert(tk.END, '0.0')
txt_iTg.grid(column=1, row=5)
# input for thermal diffusivity
lbl_alphaG = tk.Label(window, text="Thermal Diffusivity Ground [m^2 / s]")
lbl_alphaG.grid(column=0, row=7, sticky="W")
txt_alphaG = tk.Entry(window, width=10)
txt_alphaG.insert(tk.END, '1.0e-6')
txt_alphaG.grid(column=1, row=7)
# input for model runtime
lbl_rtime = tk.Label(window, text="Model Runtime [years]")
lbl_rtime.grid(column=0, row=9, sticky="W")
txt_rtime = tk.Entry(window, width=10)
txt_rtime.insert(tk.END, '400')
txt_rtime.grid(column=1, row=9)
# input for plot interval
lbl_intP = tk.Label(window, text="Interval to Plot [years]")
lbl_intP.grid(column=0, row=10, sticky="W")
txt_intP = tk.Entry(window, width=10)
txt_intP.insert(tk.END, '50')
txt_intP.grid(column=1, row=10)
# check if you want to see the points in the line
pltpoints = tk.IntVar()
cb_prtP = tk.Checkbutton(window, text='Plot model points', variable=pltpoints,
onvalue=1, offvalue=0)
cb_prtP.grid(column=0, row=11, sticky="W")
# check if you want to see the points in the line
pltintT = tk.IntVar()
cb_prtiT = tk.Checkbutton(window, text='Plot initial TMP profile',
variable=pltintT, onvalue=1, offvalue=0)
cb_prtiT.grid(column=0, row=12, sticky="W")
# check if you want to see the points in the line
pltLeg = tk.IntVar()
cb_prtLeg = tk.Checkbutton(window, text='Plot Legend', variable=pltLeg,
onvalue=1, offvalue=0)
cb_prtLeg.grid(column=0, row=13, sticky="W")
# save solution
svSol = tk.IntVar()
cb_svSol = tk.Checkbutton(window, text='Save Final Solution', variable=svSol,
onvalue=1, offvalue=0)
cb_svSol.grid(column=0, row=14, sticky="W")
txt_svSol = tk.Entry(window, width=20)
txt_svSol.insert(tk.END, 'solution.txt')
txt_svSol.grid(column=1, row=14)
# read solution
rdSol = tk.IntVar()
cb_rdSol = tk.Checkbutton(window, text='Read Initial TMP Profile',
variable=rdSol, onvalue=1, offvalue=0)
cb_rdSol.grid(column=0, row=15, sticky="W")
txt_rdSol = tk.Entry(window, width=20)
txt_rdSol.insert(tk.END, 'solution_in.txt')
txt_rdSol.grid(column=1, row=15)
lbl_Tplot = tk.Label(window, text="Which TMP to plot:")
lbl_Tplot.grid(column=0, row=16, sticky="W")
box_Tplot = ttk.Combobox(window, values=["annual mean",
"january",
"july"], width=19)
box_Tplot.grid(column=1, row=16)
box_Tplot.current(0)
# forcing with annual cycle
def T_surf(t):
jan_mean_tmp = float(txt_jaT.get()) # deg C
jul_mean_tmp = float(txt_juT.get()) # deg C
TMP_amp = (np.abs(jan_mean_tmp) + np.abs(jul_mean_tmp)) / 2
TMP_now = jan_mean_tmp+TMP_amp*(1+np.cos(2*np.pi*1/sec_in_year*t-np.pi))
return TMP_now
def T_snow(t):
snow_amp = float(txt_sdT.get()) # deg C
TMP_snow = snow_amp/2 * (1 + np.cos(2 * np.pi * 2/sec_in_year * t))
# block out the summer period, which is between 3 and 9 (months)
# TMP_snow[np.logical_and(np.mod(t, sec_in_year) >= 7889400.0,
# np.mod(t, sec_in_year) <= 23668200.0)] = 0.0
if np.mod(t, sec_in_year) >= 7889400.0 and np.mod(t, sec_in_year) <= 23668200.0:
TMP_snow = 0.0
return TMP_snow
def updateS():
dt_range = sec_in_year/1500
t_range = np.arange(0, 2*sec_in_year+dt_range, dt_range)
TMP_snow_plot = np.zeros(len(t_range))
for i in range(len(t_range)):
TMP_snow_plot[i] = T_snow(t_range[i])
# do tmp info update
amean_T_a = np.mean(T_surf(t_range))
amean_T_eff = np.mean(T_surf(t_range) + TMP_snow_plot)
amean_T_txt = "%0.2f C" % amean_T_a
amean_Te_txt = "%0.2f C" % amean_T_eff
lbl_meanT2.configure(text=amean_T_txt)
lbl_meanT2.update()
lbl_meanT2e.configure(text=amean_Te_txt)
lbl_meanT2e.update()
# do vert res update
[zga, dz_is] = np.linspace(0, float(txt_dc.get()), int(txt_zres.get()),
retstep=True)
dz_txt = "%0.4f m" % dz_is
lbl_dz2.configure(text=dz_txt)
lbl_dz2.update()
def updateSP():
dt_range = sec_in_year/1500
t_range = np.arange(0, 2*sec_in_year+dt_range, dt_range)
TMP_snow_plot = np.zeros(len(t_range))
for i in range(len(t_range)):
TMP_snow_plot[i] = T_snow(t_range[i])
# do tmp info update
amean_T_a = np.mean(T_surf(t_range))
amean_T_eff = np.mean(T_surf(t_range) + TMP_snow_plot)
amean_T_txt = "%0.2f C" % amean_T_a
amean_Te_txt = "%0.2f C" % amean_T_eff
lbl_meanT2.configure(text=amean_T_txt)
lbl_meanT2.update()
lbl_meanT2e.configure(text=amean_Te_txt)
lbl_meanT2e.update()
# do vert res update
[zga, dz_is] = np.linspace(0, float(txt_dc.get()), int(txt_zres.get()),
retstep=True)
dz_txt = "%0.4f m" % dz_is
lbl_dz2.configure(text=dz_txt)
lbl_dz2.update()
plt.figure()
plt.title("Surface TMP forcing")
plt.plot(t_range/sec_in_year*12, T_surf(t_range), 'b', label="air TMP")
plt.plot(t_range/sec_in_year*12, TMP_snow_plot, 'r', label="snow deltaTMP")
plt.plot(t_range/sec_in_year*12, T_surf(t_range) + TMP_snow_plot, 'g',
label="effective TMP")
plt.xlabel("Time in months")
plt.ylabel("TMP in C")
plt.legend()
plt.show()
def clicked():
updateS()
""" PARAMETER SPACE """
# thermal diffusivities in SI units
alpha_ground = float(txt_alphaG.get()) # m^2 / s
# geometry
depth_to_cave = float(txt_dc.get()) # m
Nz_ground = int(txt_zres.get()) # m
# initial temperatures
t_init_ground = float(txt_iTg.get()) # deg C
# time domain
t_save = int(txt_intP.get()) # years
t_total = int(txt_rtime.get()) # years
t_solve = np.arange(0, t_total+1, 1)
Nt = len(t_solve)
""" Model Code """
# space domain
[z1, dz1] = np.linspace(0, depth_to_cave, Nz_ground, retstep=True)
# add a point to do the bottom BC which we don't plot
[z, dz] = np.linspace(0, depth_to_cave+2*dz1, Nz_ground+2, retstep=True)
Nz = len(z)
z_total = z[-1]
# diffusivity
D = alpha_ground
T_data = np.zeros((Nt+1, Nz))
if rdSol.get() == 1:
T_init_read = np.loadtxt(txt_rdSol.get(), delimiter=',')
T_data[0, :] = T_init_read
else:
# set initial TMP conditions
T_data[0, :] = t_init_ground
# lower boundary condition
# T_data[0, -2] = t_init_ground + deltaT_BC
# define FDM index array for space
# make i index array
index_i = np.arange(1, Nz-1)
# make i plus array
index_i_plus = np.arange(2, Nz)
# make i minus array
index_i_minus = np.arange(0, Nz-2)
# set initial profile
T_data[0, 0] = T_surf(0) + T_snow(0)
# which tmp to work with
con_Tplot = box_Tplot.current()
# overall time in seconds
t = 0
t_month = sec_in_year/12
# time stepping loop
u = copy.deepcopy(T_data[0, :])
for n in range(Nt-1):
# u = copy.deepcopy(T_data[n, :])
u_m = np.zeros((12, Nz))
u_m_mean = 0
for month in range(12):
t_stab = 0
while t_stab < t_month:
dt_stab = 0.5 * dz**2 / np.max(D)
dt_use = min(dt_stab, t_month - t_stab)
t_stab = t_stab + dt_use
t = t + dt_use
space_diff = u[index_i_plus] - 2*u[index_i] + u[index_i_minus]
# boundary conditions and time step
u[index_i] = u[index_i] + D*(dt_use/dz**2)*space_diff
# upper BC
u[0] = T_surf(t) + T_snow(t)
# lower BC
u[Nz-1] = u[Nz-2]
time_info = "%0.4f years:" % (t/sec_in_year)
lbl_time2.configure(text=time_info)
lbl_time2.update()
u_m[month, :] = copy.deepcopy(u)
# have a selection code for which tmp
if con_Tplot == 0:
u_m_mean = np.mean(u_m, 0)
elif con_Tplot == 1:
u_m_mean = u_m[0, :]
elif con_Tplot == 2:
u_m_mean = u_m[6, :]
else:
print("Annual TMP plotting error")
# annual mean debug code
# plt.figure()
# # for mo in range(12):
# # plt.plot(u_m[mo, :], z)
# plt.plot(u_m[0, :], z, 'b')
# plt.plot(u_m[6, :], z, 'g')
# plt.plot(u_m_mean, z, 'r', linewidth=2)
# plt.ylim(z_total, 0)
# framename = 'frame_%04d_%04d.png' % (t/sec_in_year, month+1)
# plt.savefig(framename)
# plt.close()
T_data[n+1, :] = copy.deepcopy(u_m_mean)
# save solution
if svSol.get() == 1:
np.savetxt(txt_svSol.get(), T_data[-2, :], delimiter=',')
# Visuals
if pltpoints.get() == 0:
mstring = ''
elif pltpoints.get() == 1:
mstring = '.'
if pltintT.get() == 0:
pltrange = range(t_save, Nt, t_save)
elif pltintT.get() == 1:
pltrange = range(0, Nt, t_save)
plt.figure()
if con_Tplot == 0:
plt.title("Annual mean TMP with depth")
elif con_Tplot == 1:
plt.title("January TMP with depth")
elif con_Tplot == 2:
plt.title("July mean TMP with depth")
else:
print("Annual TMP plotting figur label error")
for pidx in pltrange:
plt.plot(T_data[pidx, :-2], z[:-2], marker=mstring,
label=("Year %d" % t_solve[pidx]))
plt.plot([0, 0], [0, z_total], '--k')
plt.ylim(z_total, 0)
if pltLeg.get() == 1:
plt.legend()
plt.xlabel("TMP in C")
plt.ylabel("Depth in m")
plt.show()
lbl_info = tk.Label(window, text="Model Setup Info:")
lbl_info.grid(column=1, row=17)
lbl_dz = tk.Label(window, text="Vertical Resolution dz:")
lbl_dz.grid(column=0, row=18, sticky="W")
lbl_dz2 = tk.Label(window, text="%0.4f m" % (2))
lbl_dz2.grid(column=1, row=18)
amean_T = np.mean([float(txt_jaT.get()), float(txt_juT.get())])
lbl_meanT = tk.Label(window, text="Annual Mean Air TMP:")
lbl_meanT.grid(column=0, row=19, sticky="W")
lbl_meanT2 = tk.Label(window, text="%0.2f C" % amean_T)
lbl_meanT2.grid(column=1, row=19)
lbl_meanTe = tk.Label(window, text="Annual Mean Effective TMP:")
lbl_meanTe.grid(column=0, row=20, sticky="W")
lbl_meanT2e = tk.Label(window, text="%0.2f C" % amean_T)
lbl_meanT2e.grid(column=1, row=20)
btn = tk.Button(window, text="Update Setup / Plot Input", command=updateSP)
btn.grid(column=1, row=21)
lbl_time = tk.Label(window, text="Model Time:")
lbl_time.grid(column=0, row=22, sticky="W")
lbl_time2 = tk.Label(window, text="%0.4f years" % (0))
lbl_time2.grid(column=1, row=22)
btn = tk.Button(window, text="Update Setup and Run Model", command=clicked)
btn.grid(column=1, row=23)
lbl_space = tk.Label(window, text="")
lbl_space.grid(column=0, row=24)
lbl_space = tk.Label(window, text="")
lbl_space.grid(column=0, row=25)
lbl_CR = tk.Label(window, text="Ver. 7.0 - Copyright (C) 2021 ThetaFrame Solutions, GPLv3")
lbl_CR.grid(column=1, row=26)
window.mainloop()