-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexo3.py
More file actions
171 lines (142 loc) · 4.59 KB
/
Copy pathexo3.py
File metadata and controls
171 lines (142 loc) · 4.59 KB
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
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sp
from scipy import linalg as la
from scipy.sparse.linalg import spsolve
import time
simTimeStart = time.time()
# Spatial discretization
xsize = 1e5
ysize = 1e5
Nx = 41
Ny = 31
dx = xsize/(Nx-1)
dy = ysize/(Ny-1)
x = np.linspace(0,xsize,Nx)
y = np.linspace(0,ysize,Ny)
W = np.zeros([Ny,Nx], dtype='float64')
PSI = np.zeros([Ny,Nx], dtype='float64')
# Setup model density (RHO) across the grid
rp = 2e4
gravY = 9.81
eta = 1e21
RHO = np.zeros([Ny,Nx])
for j in range(0,Nx):
for i in range(0,Ny):
r_current = np.sqrt((x[j] - xsize/2)**2 + (y[i] - ysize/2)**2)
if r_current > rp:
RHO[i,j] = 3300
else:
RHO[i,j] = 3200
# Compose global matrix (Vorticity)
N = Nx*Ny
Lw = sp.csr_matrix((N,N)).toarray()
Rw = np.zeros(N)
for j in range(0,Nx):
for i in range(0,Ny):
gk = (j)*Ny+i
if j==0 or j==Nx-1 or i==0 or i==Ny-1:
Lw[gk,gk] = 1
Rw[gk] = 0
else:
Lw[gk,gk-Ny] = 1/dx**2
Lw[gk,gk-1] = 1/dy**2
Lw[gk,gk] =-2/dx**2 - 2/dy**2
Lw[gk,gk+1] = 1/dy**2
Lw[gk,gk+Ny] = 1/dx**2
Rw[gk] = ((RHO[i,j+1] - RHO[i,j-1])/(2*dx)) * (gravY/eta)
# Solve global matrix and reload
# the algebraic solution to the geometrical array (PHI)
Sw = la.solve(Lw, Rw)
for j in range(0,Nx):
for i in range(0,Ny):
gk = (j)*Ny+i
W[i,j] = Sw[gk]
# Compose global matrix (PSI)
Lpsi = sp.csr_matrix((N,N)).toarray()
Rpsi = np.zeros(N)
for j in range(0,Nx):
for i in range(0,Ny):
gk = (j)*Ny+i
if j==0 or j==Nx-1 or i==0 or i==Ny-1:
Lpsi[gk,gk] = 1
Rpsi[gk] = 0
else:
Lpsi[gk,gk-Ny] = 1/dx**2
Lpsi[gk,gk-1] = 1/dy**2
Lpsi[gk,gk] =-2/dx**2 - 2/dy**2
Lpsi[gk,gk+1] = 1/dy**2
Lpsi[gk,gk+Ny] = 1/dx**2
Rpsi[gk] = Sw[gk]
# Solve global matrix and reload
# the algebraic solution to the geometrical array (PHI)
Spsi = la.solve(Lpsi, Rpsi)
#Spsi = spsolve(Lpsi, Rpsi)
for j in range(0,Nx):
for i in range(0,Ny):
gk = (j)*Ny+i
PSI[i,j] = Spsi[gk]
simTimeEnd = time.time() - simTimeStart
# Print results
print('')
print('______________')
print('Simulation overview')
print('______________')
print('Grid size: ', str(Nx), 'x', str(Ny))
print('Runtime: ', simTimeEnd, 'sec')
print('')
# Define X and Y component of the velocity vector
vX = np.zeros([Ny,Nx], dtype='float64')
vY = np.zeros([Ny,Nx], dtype='float64')
vX[0,1:Nx-1] = vX[1,1:Nx-1] # Boundary
vX[Ny-1,1:Nx-2] = vX[Ny-2,1:Nx-2] # Boundary
vY[1:Ny-2,0] = vY[1:Ny-2,1] # Boundary
vY[1:Ny-2,Nx-1] = vY[1:Ny-2,Nx-2] # Boundary
for j in range(0,Nx):
for i in range(0,Ny):
if j==0 or j==Nx-1 or i==0 or i==Ny-1:
vX[i,j] = 0
vY[i,j] = 0
else:
vY[i,j] = (PSI[i,j+1] - PSI[i,j-1])/(2*dx)
vX[i,j] = -(PSI[i+1,j] - PSI[i-1,j])/(2*dy)
# Plot results
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, axes = plt.subplots(2,3,figsize=(9,9),sharex=True, sharey=True)
ax1 = axes[0,0]
ax1.pcolormesh(x,y,RHO,shading='auto')
ax1.quiver(x[2:Nx:5],y[2:Ny:5],vX[2:Ny:5,2:Nx:5],vY[2:Ny:5,2:Nx:5],color='k')
ax1.set_title('colormap of RHO')
ax2 = axes[0,1]
c2 = ax2.pcolormesh(x,y,W, cmap='RdBu',shading='auto')
ax2.quiver(x[2:Nx:5],y[2:Ny:5],vX[2:Ny:5,2:Nx:5],vY[2:Ny:5,2:Nx:5],color='k')
ax2.set_title('colormap of W')
divAx2 = make_axes_locatable(ax2)
cAx2 = divAx2.append_axes("right", size="5%", pad=0.05)
fig.colorbar(c2, ax=ax2, cax=cAx2)
ax3 = axes[0,2]
c3 = ax3.pcolormesh(x,y,PSI,cmap='RdBu',shading='auto')
ax3.quiver(x[2:Nx:5],y[2:Ny:5],vX[2:Ny:5,2:Nx:5],vY[2:Ny:5,2:Nx:5],color='k')
ax3.set_title('colormap of PSI')
divAx3 = make_axes_locatable(ax3)
cAx3 = divAx3.append_axes("right", size="5%", pad=0.05)
fig.colorbar(c3, ax=ax3, cax=cAx3)
ax4 = axes[1,0]
c4 = ax4.pcolormesh(x,y,vX,cmap='RdBu',shading='auto')
ax4.quiver(x[2:Nx:5],y[2:Ny:5],vX[2:Ny:5,2:Nx:5],vY[2:Ny:5,2:Nx:5],color='k')
ax4.set_title('colormap of vX')
divAx4 = make_axes_locatable(ax4)
cAx4 = divAx4.append_axes("right", size="5%", pad=0.05)
fig.colorbar(c4, ax=ax4, cax=cAx4)
ax5 = axes[1,1]
c5 = ax5.pcolormesh(x,y,vY,cmap='RdBu',shading='auto')
ax5.quiver(x[2:Nx:5],y[2:Ny:5],vX[2:Ny:5,2:Nx:5],vY[2:Ny:5,2:Nx:5],color='k')
ax5.set_title('colormap of vY')
divAx5 = make_axes_locatable(ax5)
cAx5 = divAx5.append_axes("right", size="5%", pad=0.05)
fig.colorbar(c5, ax=ax5, cax=cAx5)
for ax in axes.flatten():
ax.set_aspect('equal')
fig.tight_layout()
plt.show()