-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoise_utils.py
208 lines (182 loc) · 6.06 KB
/
noise_utils.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
def __h(phi):
#h function from Dorr2010
return phi*phi*phi*(10-15*phi+6*phi*phi)
def __hprime(phi):
#derivative of the h function from Dorr2010, w.r.t phi
return (30*phi*phi*(1-phi)*(1-phi))
def __g(phi):
#g function from Warren1995. Similar to that used in Dorr2010 and Granasy2014
return (phi*phi*(1-phi)*(1-phi))
def __gprime(phi):
#derivative of g function, w.r.t phi
return (4*phi*phi*phi - 6*phi*phi +2*phi)
#Numpy vectorized versions of above functions
_h = np.vectorize(__h)
_hprime = np.vectorize(__hprime)
_g = np.vectorize(__g)
_gprime = np.vectorize(__gprime)
def grad(phi, dx, dim):
r = []
for i in range(dim):
phim = np.roll(phi, 1, i)
phip = np.roll(phi, -1, i)
r.append((phip-phim)/(2*dx))
return r
def grad_l(phi, dx, dim):
r = []
for i in range(dim):
phim = np.roll(phi, 1, i)
r.append((phi-phim)/(dx))
return r
def grad_r(phi, dx, dim):
r = []
for i in range(dim):
phip = np.roll(phi, -1, i)
r.append((phip-phi)/(dx))
return r
def partial_l(phi, dx, i):
phim = np.roll(phi, 1, i)
return (phi-phim)/dx
def partial_r(phi, dx, i):
phip = np.roll(phi, -1, i)
return (phip-phi)/dx
def grad2(phi, dx, dim):
r = np.zeros_like(phi)
for i in range(dim):
phim = np.roll(phi, 1, i)
phip = np.roll(phi, -1, i)
r += (phip+phim-2*phi)/(dx*dx)
return r
def divagradb(a, b, dx, dim):
r = np.zeros_like(b)
for i in range(dim):
agradb = ((a + np.roll(a, -1, i))/2)*(np.roll(b, -1, i) - b)/dx
r += (agradb - np.roll(agradb, 1, i))/dx
return r
def gaq(gql, gqr, rgqsl, rgqsr, dqc, dx, dim):
r = np.zeros_like(dqc)
for i in range(dim):
r += ((0.5*(dqc+np.roll(dqc, -1, i))*gqr[i]/rgqsr[i])-(0.5*(dqc+np.roll(dqc, 1, i))*gql[i]/rgqsl[i]))/(dx)
return r
def renormalize(q1, q4):
q = np.sqrt(q1*q1+q4*q4)
return q1/q, q4/q
def loadArrays(path, timestep):
_q1 = np.load(path+'q1_'+str(timestep)+'.npy')
_q4 = np.load(path+'q4_'+str(timestep)+'.npy')
_c = np.load(path+'c_'+str(timestep)+'.npy')
_phi = np.load(path+'phi_'+str(timestep)+'.npy')
return timestep, _phi, _c, _q1, _q4
def loadArrays_3c(path, timestep):
_q1 = np.load(path+'q1_'+str(timestep)+'.npy')
_q4 = np.load(path+'q4_'+str(timestep)+'.npy')
_c1 = np.load(path+'c1_'+str(timestep)+'.npy')
_c2 = np.load(path+'c2_'+str(timestep)+'.npy')
_phi = np.load(path+'phi_'+str(timestep)+'.npy')
return timestep, _phi, _c1, _c2, _q1, _q4
def saveArrays(path, timestep, phi, c, q1, q4):
np.save(path+'phi_'+str(timestep), phi)
np.save(path+'c_'+str(timestep), c)
np.save(path+'q1_'+str(timestep), q1)
np.save(path+'q4_'+str(timestep), q4)
def saveArrays_3c(path, timestep, phi, c1, c2, q1, q4):
np.save(path+'phi_'+str(timestep), phi)
np.save(path+'c1_'+str(timestep), c1)
np.save(path+'c2_'+str(timestep), c2)
np.save(path+'q1_'+str(timestep), q1)
np.save(path+'q4_'+str(timestep), q4)
def applyBCs(phi, c, q1, q4, nbc):
if(nbc[0]):
c[:,0] = c[:,1]
c[:,-1] = c[:,-2]
phi[:,0] = phi[:,1]
phi[:,-1] = phi[:,-2]
q1[:,0] = q1[:,1]
q1[:,-1] = q1[:,-2]
q4[:,0] = q4[:,1]
q4[:,-1] = q4[:,-2]
if(nbc[1]):
c[0,:] = c[1,:]
c[-1,:] = c[-2,:]
phi[0,:] = phi[1,:]
phi[-1,:] = phi[-2,:]
q1[0,:] = q1[1,:]
q1[-1,:] = q1[-2,:]
q4[0,:] = q4[1,:]
q4[-1,:] = q4[-2,:]
def applyBCs_3c(phi, c1, c2, q1, q4, nbc):
if(nbc[0]):
c1[:,0] = c1[:,1]
c1[:,-1] = c1[:,-2]
c2[:,0] = c2[:,1]
c2[:,-1] = c2[:,-2]
phi[:,0] = phi[:,1]
phi[:,-1] = phi[:,-2]
q1[:,0] = q1[:,1]
q1[:,-1] = q1[:,-2]
q4[:,0] = q4[:,1]
q4[:,-1] = q4[:,-2]
if(nbc[1]):
c1[0,:] = c1[1,:]
c1[-1,:] = c1[-2,:]
c2[0,:] = c2[1,:]
c2[-1,:] = c2[-2,:]
phi[0,:] = phi[1,:]
phi[-1,:] = phi[-2,:]
q1[0,:] = q1[1,:]
q1[-1,:] = q1[-2,:]
q4[0,:] = q4[1,:]
q4[-1,:] = q4[-2,:]
def coreSection(array):
"""
Returns only the region of interest for plotting.
Removes the buffer cells used for Neumann Boundary Conditions
"""
nbc=[True, True]
returnArray = array
if(nbc[0]):
returnArray = returnArray[1:-1, :]
if(nbc[1]):
returnArray = returnArray[:, 1:-1]
return returnArray
def plotImages(phi):
"""
Plots the phi (order), c (composition), and q4 (orientation component) fields for a given step
Saves images to the defined path
"""
colors = [(0, 0, 1), (0, 1, 1), (0, 1, 0), (1, 1, 0), (1, 0, 0)]
cm = LinearSegmentedColormap.from_list('rgb', colors)
colors2 = [(0, 0, 1), (1, 1, 0), (1, 0, 0)]
cm2 = LinearSegmentedColormap.from_list('rgb', colors2)
fig, ax = plt.subplots()
plt.rcParams['figure.figsize'] = 4, 4
plt.title('phi')
cax = plt.imshow(coreSection(phi), cmap=cm2)
cbar = fig.colorbar(cax, ticks=[np.min(phi), np.max(phi)])
plt.show()
def plotImages_c(phi, c1, c2):
"""
Plots the phi (order), c (composition), and q4 (orientation component) fields for a given step
Saves images to the defined path
"""
colors = [(0, 0, 1), (0, 1, 1), (0, 1, 0), (1, 1, 0), (1, 0, 0)]
cm = LinearSegmentedColormap.from_list('rgb', colors)
colors2 = [(0, 0, 1), (1, 1, 0), (1, 0, 0)]
cm2 = LinearSegmentedColormap.from_list('rgb', colors2)
fig, ax = plt.subplots()
plt.rcParams['figure.figsize'] = 4, 4
plt.title('phi')
cax = plt.imshow(phi, cmap=cm2)
cbar = fig.colorbar(cax, ticks=[np.min(phi), np.max(phi)])
fig, ax = plt.subplots()
plt.title('c1')
cax = plt.imshow(c1, cmap=cm)
cbar = fig.colorbar(cax, ticks=[np.min(c1), np.max(c1)])
fig, ax = plt.subplots()
plt.title('c2')
cax = plt.imshow(c2, cmap=cm)
cbar = fig.colorbar(cax, ticks=[np.min(c2), np.max(c2)])
plt.show()