-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansatz-generate.py
289 lines (237 loc) · 8.43 KB
/
ansatz-generate.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
# coding: utf-8
# In[3]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib
import numpy.linalg as la
import re
cmap = plt.get_cmap('jet')
plt.style.use('./large_plot.mplstyle')
np.set_printoptions(precision=5)
np.set_printoptions(suppress=True)
# # Generate a vector field
# In[4]:
def ellip1 (r, L,center):
x = (r[0]-center[0])*2/L[0]
y = (r[1]-center[1])*2/L[1]
z = (r[2]-center[2])*2/L[2]
return x**2+y**2 +z**2-1
# In[5]:
"""
Generates radial configuration
Inputs
r: radius of the sphere
dx: interval of grid
"""
def radial (r, dx = 0.1):
# Make sphere centered at origin
L = 2*r*np.ones(3);
centroid = np.asarray([0,0,0])
# Pad by 10%, round nx, ny, nz to be multiples of 10
l_box = L *1.1
l_box[2] = L[2]*1.01
nl = np.ceil(l_box/dx/10)*10
nl = np.asarray (nl, dtype= np.int32)
l_box = nl*dx
nx, ny, nz = nl
print ("nx, ny, nz", nx, ny, nz)
print ("total data points: ", nx*ny*nz)
# rr are the coordinates; nn are the director on the coordinate points
rr = np.zeros ((nx*ny*nz, 3))
nn = np.zeros ((nx*ny*nz, 3))
# create a mesh (fill in)
x = np.linspace(-l_box[0]/2, l_box[0]/2, nx)
y = np.linspace(-l_box[1]/2, l_box[1]/2, ny)
z = np.linspace(-l_box[2]/2, l_box[2]/2, nz)
xv, yv,zv = np.meshgrid(x, y, z,indexing='ij')
rr[:,0] = xv.flatten();rr[:,1] = yv.flatten();rr[:,2] = zv.flatten()
# Normalize the directors and avoid 0/0 error
nn = np.copy(rr)
nn[:,0] = rr[:,0]/ (np.linalg.norm (rr,axis =1) + 1.0E-5)
nn[:,1] = rr[:,1]/ (np.linalg.norm (rr,axis =1) + 1.0E-5)
nn[:,2] = rr[:,2]/ (np.linalg.norm (rr,axis =1) + 1.0E-5)
# identify which points are outside the sphere and set the director to zero
idx = np.where (ellip1(rr.T,L, centroid)>0)
print ("Points that are outside", idx)
print (len (np.asarray(idx[0])))
nn[idx] = 0
# return the parameters
consts = (L,centroid, l_box,nx,ny,nz,dx)
return rr, nn, consts, idx
# In[]:
def concentric (r, dx = 0.1):
# Make sphere centered at origin
L = 2*r*np.ones(3);
centroid = np.asarray([0,0,0])
# Pad by 10%, round nx, ny, nz to be multiples of 10
l_box = L *1.1
l_box[2] = L[2]*1.01
nl = np.ceil(l_box/dx/10)*10
nl = np.asarray (nl, dtype= np.int32)
l_box = nl*dx
nx, ny, nz = nl
print ("nx, ny, nz", nx, ny, nz)
print ("total data points: ", nx*ny*nz)
# rr are the coordinates; nn are the director on the coordinate points
rr = np.zeros ((nx*ny*nz, 3))
nn = np.zeros ((nx*ny*nz, 3))
# create a mesh (fill in)
x = np.linspace(-l_box[0]/2, l_box[0]/2, nx)
y = np.linspace(-l_box[1]/2, l_box[1]/2, ny)
z = np.linspace(-l_box[2]/2, l_box[2]/2, nz)
xv, yv,zv = np.meshgrid(x, y, z,indexing='ij')
rr[:,0] = xv.flatten();rr[:,1] = yv.flatten();rr[:,2] = zv.flatten()
angles = np.arctan2(rr[:,1],rr[:,0])
# Normalize the directors and avoid 0/0 error
nn = np.copy(rr)
nn[:,0] = np.sin(angles)
nn[:,1] = -np.cos(angles)
nn[:,2] = 0
# identify which points are outside the sphere and set the director to zero
idx = np.where (ellip1(rr.T,L, centroid)>0)
print ("Points that are outside", idx)
print (len (np.asarray(idx[0])))
nn[idx] = 0
# return the parameters
consts = (L,centroid, l_box,nx,ny,nz,dx)
return rr, nn, consts
# In[]:
def uniaxial (r, angle, dx = 0.1):
# Make sphere centered at origin
L = 2*r*np.ones(3);
centroid = np.asarray([0,0,0])
# Pad by 10%, round nx, ny, nz to be multiples of 10
l_box = L *1.1
l_box[2] = L[2]*1.01
nl = np.ceil(l_box/dx/10)*10
nl = np.asarray (nl, dtype= np.int32)
l_box = nl*dx
nx, ny, nz = nl
print ("nx, ny, nz", nx, ny, nz)
print ("total data points: ", nx*ny*nz)
# rr are the coordinates; nn are the director on the coordinate points
rr = np.zeros ((nx*ny*nz, 3))
nn = np.zeros ((nx*ny*nz, 3))
# create a mesh (fill in)
x = np.linspace(-l_box[0]/2, l_box[0]/2, nx)
y = np.linspace(-l_box[1]/2, l_box[1]/2, ny)
z = np.linspace(-l_box[2]/2, l_box[2]/2, nz)
xv, yv,zv = np.meshgrid(x, y, z,indexing='ij')
rr[:,0] = xv.flatten();rr[:,1] = yv.flatten();rr[:,2] = zv.flatten()
# Normalize the directors and avoid 0/0 error
nn = np.copy(rr)
nn[:,0] = np.cos(angle)
nn[:,1] = np.sin(angle)
nn[:,2] = 0
# identify which points are outside the sphere and set the director to zero
idx = np.where (ellip1(rr.T,L, centroid)>0)
print ("Points that are outside", idx)
print (len (np.asarray(idx[0])))
nn[idx] = 0
# return the parameters
consts = (L,centroid, l_box,nx,ny,nz,dx)
return rr, nn, consts
# In[6]:
# # Plot midplane to visualize
def plot_mid(rr,nn,consts, savename = None):
# Plot only the points inside the droplet
L,centroid, l_box,nx,ny,nz,dx = consts
idx = np.where (ellip1(rr.T,L, centroid)<0)
rr1 = rr[idx]; nn1 = nn[idx]
# Get the midplane
idx2 = np.where (np.abs(rr1[:,2]-0)<0.1*np.max(rr1) )
rr2 = rr1[idx2]; nn2 = nn1[idx2]
fig, ax = plt.subplots(figsize = (5,5))
ax.quiver(rr2[:,0], rr2[:,1], nn2[:,0], nn2[:,1], scale = 50, width = 1.0E-3,color = 'r', pivot = 'mid',headwidth =0, label = 'quintic')
ax.set_xlabel ("x")
ax.set_ylabel ("y")
ax.set_ylim(-l_box[0]/2,l_box[0]/2)
ax.set_xlim(-l_box[0]/2,l_box[0]/2)
plt.tight_layout()
if (savename != None):
plt.savefig(savename)
return
# # Save as a txt file
# In[7]:
def save_txt(rr,nn, consts, fname):
L,centroid, l_box,nx,ny,nz,dx = consts
X0 = np.hstack([rr,nn])
header = 'Radial; director file \n'
info0 = "Firstline:\nNx"+"\t" + "Ny" +"\t"+ "Nz"+"\t" +"dx"+"\t" + "dy" +"\t"+ "dz"+"\n"
info1 = "Secondline:\nX_min"+"\t" + "X_max" +"\t"+ "Y_min"+"\t" +"Y_max"+"\t" + "Z_min" +"\t"+ "Z_max"+"\n"
info2 = "Third line+ : data\nx \t y \t z \t n_x \t n_y \t n_z \n"
line0 = np.asarray([nx,ny,nz,dx,dx,dx])
line1 = np.asarray ([-l_box[0]/2, l_box[0]/2,-l_box[1]/2, l_box[1]/2,-l_box[2]/2, l_box[2]/2])
X = np.vstack([line0,line1, X0])
top = header+info0+info1+info2
np.savetxt(fname, X, fmt='%.4f', delimiter= '\t',header=top)
return
# In[8]:
# For filenames, replace dot by p
def replace_dot (num):
pattern = '\.'; repl = 'p'
string = '%.2f'%num
res = re.sub(pattern, repl, string, count=0, flags=0)
return res
# # Run
# In[9]:
if __name__ == "__main__":
plt.close("all")
"""
rr, nn, consts = radial (10, dx = 0.1)
l_box, nx,ny,nz,dx = consts
plot_mid(rr,nn,l_box,toplot = False)
fname = "Interpolated_Director_Field/10um-Radial-dx0p1.txt"
save_txt(rr,nn, consts, fname)
"""
# Radial size
"""
directory = "Interpolated_Director_Field/"
Rlist = np.asarray([10.5])
dxlist = np.asarray([0.2])
for R in Rlist:
for dx in dxlist:
rr, nn, consts, idx = radial (R, dx = dx)
l_box, nx,ny,nz,dx = consts
#plot_mid(rr,nn,l_box,idx, savename = None)
fname = replace_dot(R) +"um-Radial-"+replace_dot(dx) +".txt"
print (fname)
print (directory +fname)
save_txt(rr,nn, consts, directory+fname)
"""
# In[9]:
# Uniaxial quick test
directory = "Interpolated_Director_Field/"
Rlist = np.asarray([10.5])
dxlist = np.asarray([0.5])
anglelist = np.asarray([15,75,105,165])
R = 10.0; dx = 0.25
for angle in anglelist:
angle1 = angle/180*np.pi
rr, nn, consts = uniaxial (R, angle1, dx = dx)
L,centroid, l_box,nx,ny,nz,dx = consts
picname = replace_dot(R) +"um-uniaxial-"+replace_dot(angle) +".png"
#plot_mid(rr,nn, consts, savename = picname)
fname = replace_dot(R) +"um-uniaxial-"+replace_dot(angle) +".txt"
print (fname)
print (directory +fname)
save_txt(rr,nn, consts, directory+fname)
# In[]:
"""
# Uniaxial quick test
directory = "Interpolated_Director_Field/"
Rlist = np.asarray([10.5])
dxlist = np.asarray([0.5])
anglelist = np.asarray([0, 45,90])
R = 10.0; dx = 0.5
rr, nn, consts = concentric (R, dx = dx)
L,centroid, l_box,nx,ny,nz,dx = consts
picname = replace_dot(R) +"um-concentric" +".png"
plot_mid(rr,nn, consts, savename = picname)
fname = replace_dot(R) +"um-concentric" +".txt"
print (fname)
print (directory +fname)
#save_txt(rr,nn, consts, directory+fname)
"""