-
Notifications
You must be signed in to change notification settings - Fork 2
/
advect1d.py
137 lines (102 loc) · 3.86 KB
/
advect1d.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
import limiters
import numpy as np
def flux(u,a,dx,dt,limiter):
"""
Compute fluxes between cells
u: Quantity to be passively advected
a: Velocity of flow
dx: Cell size
dt: Time step
limiter: String containing the name of one the flux limiter functions in limiters.py
"""
# Get the function to call for the flux limiter
S=getattr(limiters,limiter)
# Gradient across cell at left side of each face
sm=(u[1:-2]-u[:-3])/dx
# Gradient across cell at right side of each face
sp=(u[2:-1]-u[1:-2])/dx
# Separate the velocity into strictly positive and strictly negative vectors, for implementing the upwind scheme
ap=np.maximum(a,0)
am=np.minimum(a,0)
# Compute positive and negative fluxes across each face
fp=ap[1:-2]*(u[1:-2]+dx/2*(1-a[1:-2]*dt/dx)*S(sm,sp))
fm=am[2:-1]*(u[2:-1]-dx/2*(1-abs(a[2:-1])*dt/dx)*S(sm,sp))
# Add positive and negative fluxes together and return
return fp+fm
def flux_burgers(u,dx,dt,limiter):
"""
Compute fluxes between cells
u: Quantity to be passively advected
a: Velocity of flow
dx: Cell size
dt: Time step
limiter: String containing the name of one the flux limiter functions in limiters.py
"""
# Get the function to call for the flux limiter
S=getattr(limiters,limiter)
# Gradient across cell at left side of each face
sm=(u[1:-2]-u[:-3])/dx
# Gradient across cell at right side of each face
sp=(u[2:-1]-u[1:-2])/dx
# Separate the velocity into strictly positive and strictly negative vectors, for implementing the upwind scheme
ap=np.maximum(u,0)
am=np.minimum(u,0)
# Compute positive and negative fluxes across each face
fp=ap[1:-2]*(u[1:-2]/2+dx/2*(1-u[1:-2]*dt/dx)*S(sm,sp))
fm=am[2:-1]*(u[2:-1]/2-dx/2*(1-abs(u[2:-1])*dt/dx)*S(sm,sp))
# Add positive and negative fluxes together and return
return fp+fm
def step(u,a,dx,dt,limiter):
"""
Step forward in time using an explicit Euler scheme
u: Quantity to be passively advected
a: Velocity of flow
dx: Cell size
dt: Time step
limiter: String containing the name of one the flux limiter functions in limiters.py
"""
# Flux at all faces
f=flux(u,a,float(dx),float(dt),limiter)
# Flux at left-side faces
# (copied so it can be overwritten without affecting f)
f_left=f[:-1].copy()
# Flux at right-side faces
f_right=f[1:]
# Indices where shocks occur
shock_inds=np.where(a[1:]<a[:-1])[0]
# At shocks, compute fluxes at left-side faces using the velocity from
# the upstream cell. This prevents magnitude growth at the shock
# interface
f_shift_r=flux(u[:-1],a[1:],float(dx),float(dt),limiter)
f_left[shock_inds]=f_shift_r[shock_inds]
# Update u
u[2:-2]=u[2:-2]+dt/dx*(f_left-f_right)
def step_burgers(u,dx,dt,limiter):
"""
Step forward in time using an explicit Euler scheme
u: Quantity to be passively advected
a: Velocity of flow
dx: Cell size
dt: Time step
limiter: String containing the name of one the flux limiter functions in limiters.py
"""
f=flux_burgers(u,float(dx),float(dt),limiter)
u[2:-2]=u[2:-2]+dt/dx*(f[:-1]-f[1:])
def updateboundary(a,t,x_grid,x_bound,t_x,a_bound,t_a):
"""
Insert time-series solar wind data into the grid at the satellite location
a: Quantity to be passively advected
t: Simulation time
x_grid: Positions of cell edges
x_bound: Satellite location in the same coordinates as x_grid
t_x: Times for satellite positions
a_bound: Values of a to insert into grid
t_a: Times for a values
"""
from scipy.interpolate import interp1d
# Interpolate satellite position to simulation time
x=interp1d(t_x,x_bound)(t)
# Find which grid cell to update
ind=np.searchsorted(x_grid,x)
# Update a
a[ind:ind+1]=interp1d(t_a,a_bound)(t)