Skip to content
Snippets Groups Projects
Commit d8d1b9a3 authored by adbeck2's avatar adbeck2
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 278 additions and 0 deletions
%% Cell type:code id: tags:
``` python
import numpy as np
def A_matrix(n):
A = np.zeros((n-1, n-1))
for i in range(0, len(A)):
for j in range(len(A[i])):
if j == i - 1:
A[i][j] = 1
elif j == i:
A[i][j] = -2
elif j == i + 1:
A[i][j] = 1
else:
A[i][j] = 0
return A
```
%% Cell type:code id: tags:
``` python
import numpy as np
def u_exact(x, t, a, b, T):
L = b-a
u_e = np.sin(np.pi*x/L)*np.cos(np.pi*t/T)
return u_e
def g_function(x, t, a, b, T, c_squared):
L = b-a
g = np.sin(np.pi*x/L)*np.cos(np.pi*t/T)*(c_squared*a**2*np.pi**2/(L**2) - b**2*np.pi**2/(T**2))
return g
def ibvp_step(x, state, n, deltat, deltax, tk, a, b, A, I, c_squared, T):
gt = g_function(x, tk, a, b, T, c_squared)
gt1 = g_function(x, tk+deltat, a, b, T, c_squared)
gt_concat = np.concatenate((np.zeros((n-1)), gt), axis=0)
gt1_concat = np.concatenate((np.zeros((n-1)), gt1), axis=0)
UL = np.zeros((n-1, n-1))
UR = I
#####PROBLEM LINE
LL = c_squared/(deltax**2)*A
LR = np.zeros((n-1, n-1))
temp1 = np.concatenate((UL,UR), axis=1)
temp2 = np.concatenate((LL,LR), axis=1)
f_matrix = np.concatenate((temp1,temp2), axis=0)
I_concat = np.identity(2*n-2)
LHS = I_concat - 1/2*deltat*f_matrix
RHS = state + (1/2*deltat*((np.matmul(f_matrix, state)) + gt_concat + gt1_concat))
state_updated = np.linalg.solve(LHS, RHS)
return state_updated
def solve_ibvp(state_initial, deltat, deltax, T, a, b, c_squared):
n = int(abs((b-a)/deltax))
nt = int(abs(T/deltat))
x = np.linspace(a+deltax, b-deltax, num=n-1)
t = np.linspace(0, T, num=nt+1)
A = A_matrix(n)
I = np.identity(n-1)
state_hat = [state_initial]
for h in range(len(t)-1):
state_updated = ibvp_step(x, state_hat[h], n, deltat, deltax, t[h], a, b, A, I, c_squared, T)
state_hat.append(state_updated)
state_hat = np.array(state_hat)
return state_hat, x, t
```
%% Cell type:code id: tags:
``` python
import numpy as np
a = 0
b = 1
L = b-a
#c_squared = 68.9e9 / 2710
c_squared = 1
deltat = 0.005
deltax = 0.001
T = 1
n = int(abs((b-a)/deltax))
nt = int(abs(T/deltat))
x = np.linspace(a+deltax, b-deltax, num=n-1)
ui = np.sin(np.pi*x/L)
vi = np.zeros((n-1))
initial_state = np.concatenate((ui, vi), axis=0)
state_hat, x, t = solve_ibvp(initial_state, deltat, deltax, T, a, b, c_squared)
print('done')
```
%% Output
done
%% Cell type:code id: tags:
``` python
u_true = []
for d in range(len(t)):
utk = u_exact(x, t[d], a, b, T)
u_true.append(utk)
u_true = np.array(u_true)
```
%% Cell type:code id: tags:
``` python
import matplotlib.pyplot as plt
import numpy as np
state_hat_split = np.hsplit(state_hat, 2)
index = 100
uhat = state_hat_split[0]
vhat = state_hat_split[1]
err = np.linalg.norm(uhat[index]-u_true[index]) / np.linalg.norm(u_true[index])
print(err)
plt.plot(x, state_hat_split[0][index], label='u_hat')
plt.plot(x, u_true[index],linestyle='dashed', label='u_true')
plt.legend()
plt.show()
# print(u_true[500])
# print(t[500])
# print(c_squared)
#print(uhat)
```
%% Output
1.2825066996978956e+16
This diff is collapsed.
This diff is collapsed.
File added
import numpy as np
x = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
y = np.hsplit(x, 2)
print(y)
# x = [np.array([0,1,2]), np.array([3,4,5])]
# print(x)
# y = np.array(x)
# print(y)
# x = np.array([0,1,2])
# y = np.array([3,4,5])
# z = np.concatenate((x,y), axis=0)
# print(z)
# a = np.array([[0,1], [4,5]])
# b = np.array([[2,3], [6,7]])
# c = np.array([[8,9], [12,13]])
# d = np.array([[10,11], [14,15]])
# x = np.bmat([[a, b], [c, d]])
# print(x)
# y = np.array([1,2,3,4])
# print(x@y)
# temp1 = np.concatenate((a,b), axis=1)
# temp2 = np.concatenate((c,d), axis=1)
# x = np.concatenate((temp1,temp2), axis=0)
# print(x)
# print(x@y)
Plots/Displacement/t0.png

27.2 KiB

Plots/Displacement/t005.png

29 KiB

Plots/Displacement/t01.png

30.9 KiB

Plots/Displacement/xpi2_mk3.png

60.4 KiB

Plots/Validation/t0.png

21.7 KiB

Plots/Validation/t05.png

14.2 KiB

Plots/Validation/t1.png

21.5 KiB

Plots/Validation/x05.png

22.4 KiB

Plots/Vibration/sigma_t05.png

67 KiB

Plots/Vibration/sigma_xpi2.png

53.5 KiB

Plots/Vibration/t001.png

41.7 KiB

Plots/Vibration/t01.png

63.1 KiB

Plots/Vibration/t05.png

60.4 KiB

Plots/Vibration/t1.png

59.6 KiB

Plots/Vibration/xpi2.png

48.6 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment