|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from tridiagonal import solve_tridiagonal |
| 6 | + |
| 7 | +# ---------------------------------------------------------------------- |
| 8 | +# Solve the heat conduction equation, assuming: |
| 9 | +# 1. steady-state |
| 10 | +# 2. top boundary is zero gradient |
| 11 | +# 3. source term is time-dependent |
| 12 | +# 4. bottom BC is time dependent |
| 13 | +# ---------------------------------------------------------------------- |
| 14 | + |
| 15 | +if __name__ == "__main__": |
| 16 | + |
| 17 | + # tidal characteristics: |
| 18 | + ampDiurnal = 50.0 # in K |
| 19 | + phaseDiurnal = 15.0 # in hours, where peak occurs |
| 20 | + ampSemi = 3.0 # in K |
| 21 | + phaseSemi = 12.0 # in hours, where peak occurs |
| 22 | + |
| 23 | + dx = 0.025 |
| 24 | + |
| 25 | + # set x with 1 ghost cell on both sides: |
| 26 | + x = np.arange(-dx, 10 + 2 * dx, dx) |
| 27 | + |
| 28 | + nDays = 30 |
| 29 | + dtime = 1.0 # hours |
| 30 | + times = np.arange(0, nDays * 24.0, dtime) |
| 31 | + |
| 32 | + period = 27.0 * 24.0 # in hours |
| 33 | + f107 = 100.0 + 15.0 * np.sin(times / period * 2.0 * np.pi) |
| 34 | + |
| 35 | + # Create 2d arrays for plotting: |
| 36 | + nTimes = len(times) |
| 37 | + nAlts = len(x) |
| 38 | + temp2d = np.zeros((nAlts, nTimes)) |
| 39 | + alt2d = np.zeros((nAlts, nTimes)) |
| 40 | + time2d = np.zeros((nAlts, nTimes)) |
| 41 | + |
| 42 | + # make an altitude array for plotting: |
| 43 | + alt = 100.0 + x * 50.0 |
| 44 | + |
| 45 | + t_lower = 200.0 |
| 46 | + |
| 47 | + nPts = len(x) |
| 48 | + |
| 49 | + # set default coefficients for the solver: |
| 50 | + a = np.zeros(nPts) + 1 |
| 51 | + b = np.zeros(nPts) - 2 |
| 52 | + c = np.zeros(nPts) + 1 |
| 53 | + d = np.zeros(nPts) |
| 54 | + |
| 55 | + # boundary conditions (bottom - fixed): |
| 56 | + a[0] = 0 |
| 57 | + b[0] = 1 |
| 58 | + c[0] = 0 |
| 59 | + d[0] = t_lower |
| 60 | + |
| 61 | + # top - zero gradient (Tn - Tn-1 = 0): |
| 62 | + a[-1] = -1 |
| 63 | + b[-1] = 1 |
| 64 | + c[-1] = 0 |
| 65 | + d[-1] = 0.0 |
| 66 | + |
| 67 | + for i, time in enumerate(times): |
| 68 | + |
| 69 | + ut = time % 24.0 |
| 70 | + # UT-dependent heating function: |
| 71 | + peakEnergy = f107[i] / 2.0 |
| 72 | + heat = peakEnergy * np.sin(ut * np.pi / 24.0) |
| 73 | + # at night, there is still chemistry, which adds heat: |
| 74 | + if (heat < 25.0): |
| 75 | + heat = 25.0 |
| 76 | + |
| 77 | + unmodeled = np.random.normal(1.0, 0.1) |
| 78 | + heat = heat * unmodeled |
| 79 | + |
| 80 | + # add sources: |
| 81 | + l = ((x > 1.0) & (x < 7.5)) |
| 82 | + d[l] = -heat * (10.0 - x[l])/10.0 * (dx ** 2) |
| 83 | + |
| 84 | + # time-dependent bottom BC: |
| 85 | + ut2rad = np.pi / 12.0 |
| 86 | + diurnal = ampDiurnal * np.cos((ut - phaseDiurnal) * ut2rad) |
| 87 | + semi = ampSemi * np.cos(2 * (ut - phaseSemi) * ut2rad) |
| 88 | + gravity_wave = np.random.normal(0.0, 5.0) |
| 89 | + t_lower = 200.0 + diurnal + semi + gravity_wave |
| 90 | + d[0] = t_lower |
| 91 | + |
| 92 | + # solve for Temperature: |
| 93 | + t = solve_tridiagonal(a, b, c, d) |
| 94 | + |
| 95 | + # fill in arrays for plotting |
| 96 | + temp2d[:, i] = t |
| 97 | + alt2d[:, i] = alt |
| 98 | + time2d[:, i] = time/24.0 |
| 99 | + |
| 100 | + # create plot: |
| 101 | + fig = plt.figure(figsize = (10,10)) |
| 102 | + ax = fig.add_subplot(111) |
| 103 | + ax.contourf(time2d, alt2d, temp2d) |
| 104 | + |
| 105 | + plotfile = 'conduction_v5.png' |
| 106 | + print('writing : ',plotfile) |
| 107 | + fig.savefig(plotfile) |
| 108 | + plt.close() |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments