Filed line integration and other smaller python tools.

This commit is contained in:
Miikka Vaisala
2021-01-11 11:27:12 +08:00
parent 7878811820
commit 2dbf703c59
3 changed files with 473 additions and 26 deletions

View File

@@ -21,6 +21,7 @@
import numpy as np
import os
import pandas as pd
#Optional YT interface
try:
@@ -302,9 +303,116 @@ class Mesh:
self.jj = curl_of_curl(self.aa, self.minfo)
if trim:
self.bb = ( self.bb[0][3:-3, 3:-3, 3:-3],self.bb[1][3:-3, 3:-3, 3:-3],self.bb[2][3:-3, 3:-3, 3:-3])
self.xx_trim = self.xx[3:-3]
self.yy_trim = self.yy[3:-3]
self.zz_trim = self.zz[3:-3]
if get_jj:
self.jj = (self.jj[0][3:-3, 3:-3, 3:-3],self.jj[1][3:-3, 3:-3, 3:-3],self.jj[2][3:-3, 3:-3, 3:-3])
def Bfieldlines(self, footloc = 'default', vartype = 'B', maxstep = 1000):
dx = self.minfo.contents['AC_dsx']
dy = self.minfo.contents['AC_dsy']
dz = self.minfo.contents['AC_dsz']
if vartype == 'U':
#Trim to match
self.uu = (self.uu[0][3:-3, 3:-3, 3:-3],self.uu[1][3:-3, 3:-3, 3:-3],self.uu[2][3:-3, 3:-3, 3:-3])
def field_line_step(self, coord, ds):
#TODO assume that grid is at a cell centre
ix = np.argmin(np.abs(self.xx_trim - coord[0]))
iy = np.argmin(np.abs(self.yy_trim - coord[1]))
iz = np.argmin(np.abs(self.zz_trim - coord[2]))
if vartype == 'U':
Bcell_vec = np.array([self.uu[0][ix, iy, iz],
self.uu[1][ix, iy, iz],
self.uu[2][ix, iy, iz]])
else:
Bcell_vec = np.array([self.bb[0][ix, iy, iz],
self.bb[1][ix, iy, iz],
self.bb[2][ix, iy, iz]])
Bcell_abs = np.sqrt(Bcell_vec[0]**2.0 + Bcell_vec[1]**2.0 + Bcell_vec[2]**2.0)
coord_new = coord + (Bcell_vec/Bcell_abs)*ds
return coord_new
self.df_lines = pd.DataFrame()
ds = np.amin([self.minfo.contents['AC_dsx'],
self.minfo.contents['AC_dsy'],
self.minfo.contents['AC_dsz']])
ii = 0
if footloc == 'middlez':
ixtot = 6
iytot = 6
iztot = 1
xfoots = np.linspace(self.xx_trim.min(), self.xx_trim.max(), num = ixtot)
yfoots = np.linspace(self.yy_trim.min(), self.yy_trim.max(), num = iytot)
zfoots = np.array([(self.zz_trim.max() - self.zz_trim.min())/2.0 + self.zz_trim.min()])
elif footloc == 'cube':
ixtot = 5
iytot = 5
iztot = 5
xfoots = np.linspace(self.xx_trim.min()+3.0*dx, self.xx_trim.max()-3.0*dx, num = ixtot)
yfoots = np.linspace(self.yy_trim.min()+3.0*dy, self.yy_trim.max()-3.0*dy, num = iytot)
zfoots = np.linspace(self.zz_trim.min()+3.0*dz, self.zz_trim.max()-3.0*dz, num = iztot)
else:
ixtot = 6
iytot = 6
iztot = 1
xfoots = np.linspace(self.xx_trim.min(), self.xx_trim.max(), num = ixtot)
yfoots = np.linspace(self.yy_trim.min(), self.yy_trim.max(), num = iytot)
zfoots = np.array([self.zz_trim.min()])
imax = ixtot * iytot * iztot
for zfoot in zfoots:
for yfoot in yfoots:
for xfoot in xfoots:
print(ii, "/", imax-1)
integrate = 1
counter = 0
dstot = 0.0
coord = np.array([xfoot, yfoot, zfoot])
self.df_lines = self.df_lines.append({"line_num":ii,
"dstot":dstot,
"coordx":coord[0],
"coordy":coord[1],
"coordz":coord[2]},
ignore_index=True)
while integrate:
coord = field_line_step(self, coord, ds)
dstot += ds
self.df_lines = self.df_lines.append({"line_num":ii,
"dstot":dstot,
"coordx":coord[0],
"coordy":coord[1],
"coordz":coord[2]},
ignore_index=True)
counter += 1
if counter >= maxstep:
integrate = 0
if ((coord[0] > self.xx_trim.max()) or
(coord[1] > self.yy_trim.max()) or
(coord[2] > self.zz_trim.max()) or
(coord[0] < self.xx_trim.min()) or
(coord[1] < self.yy_trim.min()) or
(coord[2] < self.zz_trim.min())):
#print("out of bounds")
integrate = 0
if (np.isnan(coord[0]) or
np.isnan(coord[1]) or
np.isnan(coord[2])):
integrate = 0
ii += 1
#print(self.df_lines)
def get_jj(self, trim=False):
self.jj = curl_of_curl(self.aa, minfo, trim=False)
if trim:

View File

@@ -142,4 +142,103 @@ def plot_3(mesh, input_grid, title = '', fname = 'default', bitmap=False,
print('Saved %s_%s.png' % (fname, mesh.framenum))
plt.close(fig)
def volume_render(mesh, val1 = {"variable": None, "min": None, "max":None, "opacity":1.0}):
if val1["variable"] == "btot":
plt.figure()
bb_tot = np.sqrt(mesh.bb[0]**2.0 + mesh.bb[1]**2.0 + mesh.bb[2]**2.0)
array = bb_tot
varname = "btot"
meshxx = mesh.xx[3:-3]
meshyy = mesh.yy[3:-3]
meshzz = mesh.zz[3:-3]
if val1["variable"] == "utot":
plt.figure()
uu_tot = np.sqrt(mesh.uu[0]**2.0 + mesh.uu[1]**2.0 + mesh.uu[2]**2.0)
array = uu_tot
varname = "utot"
meshxx = mesh.xx
meshyy = mesh.yy
meshzz = mesh.zz
if val1["variable"] == "rho":
plt.figure()
array = np.exp(mesh.lnrho)
varname = "rho"
meshxx = mesh.xx
meshyy = mesh.yy
meshzz = mesh.zz
if val1["variable"] == "aa":
plt.figure()
aa_tot = np.sqrt(mesh.aa[0]**2.0 + mesh.aa[1]**2.0 + mesh.aa[2]**2.0)
array = aa_tot
varname = "aa"
meshxx = mesh.xx
meshyy = mesh.yy
meshzz = mesh.zz
#Histogram plot to find value ranges.
hist, bedges = np.histogram(array, bins=mesh.xx.size)
plt.plot(bedges[:-1], hist)
plt.yscale('log')
if val1["min"] != None or val1["max"] != None:
plt.plot([val1["min"],val1["min"]], [1,hist.max()], label=varname+" min")
plt.plot([val1["max"],val1["max"]], [1,hist.max()], label=varname+" max")
plt.legend()
plt.savefig('volrend_hist_%s_%s.png' % (varname, mesh.framenum))
plt.close()
if val1["min"] != None or val1["max"] != None:
#print(np.where(bb_tot < val1["min"]))
array[np.where(array < val1["min"])] = 0.0
array[np.where(array > val1["max"])] = 0.0
array[np.where(array > 0.0)] = val1["opacity"]
#plt.figure()
#plt.plot(bb_tot[:,64,64])
mapyz = array.sum(axis=0)
mapxz = array.sum(axis=1)
mapxy = array.sum(axis=2)
yy_yz, zz_yz = np.meshgrid(meshyy, meshzz, indexing='ij')
xx_xz, zz_xz = np.meshgrid(meshxx, meshzz, indexing='ij')
xx_xy, yy_xy = np.meshgrid(meshxx, meshyy, indexing='ij')
fig, ax = plt.subplots()
#plt.imshow(mapyz, vmin=0.0, vmax=1.0)
plt.pcolormesh(yy_yz, zz_yz, mapyz, vmin=0.0, vmax=1.0, shading='auto')
ax.set_aspect('equal')
ax.set_title(varname)
ax.set_xlabel('y')
ax.set_ylabel('z')
plt.savefig('volrend_%s_%s_%s.png' % (varname, "yz", mesh.framenum))
plt.close()
fig, ax = plt.subplots()
#plt.imshow(mapxz, vmin=0.0, vmax=1.0)
plt.pcolormesh(xx_xz, zz_xz, mapxz, vmin=0.0, vmax=1.0, shading='auto')
ax.set_aspect('equal')
ax.set_title(varname)
ax.set_xlabel('x')
ax.set_ylabel('z')
plt.savefig('volrend_%s_%s_%s.png' % (varname, "xz", mesh.framenum))
plt.close()
fig, ax = plt.subplots()
#plt.imshow(mapxy, vmin=0.0, vmax=1.0)
plt.pcolormesh(xx_xy, yy_xy, mapxy, vmin=0.0, vmax=1.0, shading='auto')
ax.set_aspect('equal')
ax.set_title(varname)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.savefig('volrend_%s_%s_%s.png' % (varname, "xy", mesh.framenum))
plt.close()
#plt.show()