-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper_functions.py
More file actions
152 lines (107 loc) · 3.97 KB
/
Copy pathHelper_functions.py
File metadata and controls
152 lines (107 loc) · 3.97 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
import numpy as np
import math
def gauss(x, y, L):
"""
Generates a Gaussian.
"""
g = np.exp(-0.5 * (x**2 + y**2) / L**2)
return g
def paircountN2(num, N):
"""
Generates a list of coordinate pairs.
"""
locs = np.ceil(np.random.rand(num, 2) * N).astype(int)
return locs
def pairfieldN2(L, dx, h1, wlayer):
"""
Creates the weather matrix for the storms, S_st in paper.
"""
voldw = np.sum(np.sum(wlayer)) * dx**2
area = L**2
wcorrect = voldw / area
Wmat = wlayer - wcorrect
return Wmat
def Axl(f, l, r):
"""
Redundant?
"""
fa = 0.5 * (f + f[:, l-1])
return fa
def Ayl(f, l, r):
"""
Redundant?
"""
fa = 0.5 * (f + f[l-1, :])
return fa
def viscND(vel, Re, n, dx):
"""
n is exponent of Laplacian operator
Where visc term is nu*(-1)^(n+1) (\/^2)^n
so for regular viscosity n = 1, for hyperviscosity n=2
TODO: for n=1 nu is not defined...
"""
if n == 1:
field = vel[0,:] + vel[r-1,:] + vel[:,r-1]-4*vel;
field = (nu/dx**2) * field
return field
if n == 2:
field = 2*np.roll(vel,(1,1),axis=(0,1)) + 2*np.roll(vel,(1,-1),axis=(0,1)) + 2*np.roll(vel,(-1,1),axis=(0,1)) + 2*np.roll(vel,(-1,-1),axis=(0,1)) - 8*np.roll(vel, 1, axis=0) - 8*np.roll(vel, -1, axis=0) - 8*np.roll(vel, 1, axis=1) - 8*np.roll(vel, -1, axis=1) + np.roll(vel, 2, axis=0) + np.roll(vel, -2, axis=0) + np.roll(vel, 2, axis=1) + np.roll(vel, -2, axis=1) + 20*vel
field = -1/Re*(1/dx**4)*field
return field
def pairshapeN2(locs, x, y, Br2, Wsh, N, dx):
"""
Create Gaussians on smaller scales and then convolve them with the weather layer.
"""
rad = int(np.ceil(np.sqrt(1/Br2) / dx))
xg, yg = np.meshgrid(range(-rad, rad+1), range(-rad, rad+1))
gaus = Wsh * np.exp(-(Br2 * dx**2) / 0.3606 * ((xg + 0.5)**2 + (yg + 0.5)**2))
wlayer = np.zeros(x.shape)
buf = rad
bufmat = np.zeros((N + 2 * rad, N + 2 * rad))
nlocs = locs + rad
corners = nlocs - rad
for jj in range(locs.shape[0]):
bufmat[corners[jj, 0]:corners[jj, 0] + gaus.shape[0],
corners[jj, 1]:corners[jj, 1] + gaus.shape[1]] += gaus
wlayer = bufmat[buf:buf+N, buf:buf+N]
addlayer1 = np.zeros_like(wlayer)
addlayer2 = np.zeros_like(wlayer)
addlayer3 = np.zeros_like(wlayer)
addlayer4 = np.zeros_like(wlayer)
addcorn1 = np.zeros_like(wlayer)
addcorn2 = np.zeros_like(wlayer)
addcorn3 = np.zeros_like(wlayer)
addcorn4 = np.zeros_like(wlayer)
addlayer1[:buf, :] = bufmat[buf+N:, buf:buf+N]
addlayer2[:, :buf] = bufmat[buf:buf+N, buf+N:]
addlayer3[-buf:, :] = bufmat[:buf, buf:buf+N]
addlayer4[:, -buf:] = bufmat[buf:buf+N, :buf]
addcorn1[:buf, :buf] = bufmat[buf+N:, buf+N:]
addcorn2[-buf:, -buf:] = bufmat[:buf, :buf]
addcorn3[:buf, -buf:] = bufmat[buf+N:, :buf]
addcorn4[-buf:, :buf] = bufmat[:buf, buf+N:]
wlayer += (addlayer1 + addlayer2 + addlayer3 + addlayer4 +
addcorn1 + addcorn2 + addcorn3 + addcorn4)
layersum = np.sum(wlayer)
return wlayer
def BernN2(u1,v1,u2,v2,gm,c22h,c12h,h1,h2,ord,r):
"""
Generates Bernstein polynomial
"""
if ord == 1:
B1 = 'broke'
B2 = 'broke'
else:
B1 = c12h*h1 + c22h*h2 + 0.25*(u1**2 + np.roll(u1, -1, axis=1)**2 + v1**2 + np.roll(v1, -1, axis=0)**2)
B2 = gm*c12h*h1 + c22h*h2 + 0.25*(u1**2 + np.roll(u1, -1, axis=1)**2 + v1**2 + np.roll(v1, -1, axis=0)**2)
return B1, B2
def xflux(f, u, dx, dt):
fl = np.roll(f, 1, axis=1)
fr = f
fa = 0.5 * u * (fl+fr)
return fa
def yflux(f, v, dx, dt):
fl = np.roll(f, 1, axis=0)
fr = f
fa = 0.5 * v * (fl+fr)
return fa