Skip to content

Instantly share code, notes, and snippets.

@bradlipovsky
Created May 5, 2023 16:25
Show Gist options
  • Save bradlipovsky/2c060a507d825a5f899ab6fd763c94a2 to your computer and use it in GitHub Desktop.
Save bradlipovsky/2c060a507d825a5f899ab6fd763c94a2 to your computer and use it in GitHub Desktop.
Simple 1d implicit Euler Finite Difference Heat flow calculation
import numpy as np
n = 10
dt = 1
alpha = 1e-6 * 3.15e7
dx = 10
c = dt * alpha / (dx)**2
z = np.arange(0,n*dx,dx)
Q = 0 # change this
k = 1 # change this too
# Define FD scheme
B = np.diag([1+2*c] * n) + np.diag([-c] * (n-1),k=1)\
+ np.diag([-c] * (n-1),k=-1)
un = np.zeros((n,1))
unm1 = np.zeros((n,1))
unm1[int(n/2)] = 1
'''
Set up time loop here
'''
un = np.linalg.solve(B,unm1)
# Enforce Dirichlet boundary conditions
un[0] = 1
# un[-1] = 0
# FD coefficients
c1 = -4
c2 = 1
c3 = -3
un[-1] = (c1*un[-2] + c2*un[-3] - Q/k * 2*dx)/c3
import matplotlib.pyplot as plt
plt.plot(z,unm1)
plt.plot(z,un)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment