You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The section of prorad.py where it initializes the grid by looping over values in serial or even parallel is a choke point in speed:
print("'params.grid_nthreads' not specified. Initializing grid in serial.")
foriinfield_xrange_idx:
forjinfield_yrange_idx:
forkinfield_zrange_idx:
x=X_coords[i]
y=Y_coords[j]
z=Z_coords[k]
gridvals[i,j,k,:] =params.fields((x,y,z))
There is a faster way to initialize the analytic grid, taking advantage of NumPy's vectorization capabilities. However, it requires more familiarity with the NumPy syntax by the user who writes the "fields()" function. I've written a local branch which leverages a new, vectorized "fields()" function in my input deck and then implements a new format in prorad.py called "analytic_grid2"; I can push it into this repo as an example if there is interest. I make a 300x300x300 analytic grid in a few seconds using the vectorized technique, but it a much smaller grid takes many times longer to generate with the original method.
Suggested addition to prorad.py:
iffformat=="analytic_grid2":
# Load user-defined analytic fields, Scott Feister's version using vectorized gridprint("Generating "+str(ngridx)+"x"+str(ngridy)+"x"+str(ngridz)+" grid from analytic fields...")
cyl_coords=Falsetry: cyl_coords=params.cyl_coordsexceptAttributeError: passlx, ly, lz=params.lx, params.ly, params.lzxoffset, yoffset, zoffset=params.gridcornerX_coords=np.linspace(xoffset, xoffset+lx, ngridx)
Y_coords=np.linspace(yoffset, yoffset+ly, ngridy)
Z_coords=np.linspace(zoffset, zoffset+lz, ngridz)
X, Y, Z=np.meshgrid(X_coords, Y_coords, Z_coords)
print("Initializing grid in vectorized serial.")
gridvals=params.fieldsvec((X, Y, Z), NUM_FIELDS)
gridspacings= (lx/ngridx, ly/ngridy, lz/ngridz)
grid=Grid(gridvals, gridspacings, (xoffset,yoffset,zoffset), (lx,ly,lz), cyl_coords=cyl_coords)
Corresponding example of definition of fields in input deck (a radial blob of E & B):
rad=0.01# Radius of blobdeffields(coord): # REQUIRED FOR ANALYTIC_GRIDx,y,z=coordr= (x**2+y**2+z**2)
ifr<rad: # Create strongest field in the centerEvec= (1-r/rad) *np.array([1e4, 1e4, 1e4])
Bvec= (1-r/rad) *np.array([-4e4, -4e4, -4e4])
else:
Evec=np.array([0, 0, 0])
Bvec=np.array([0, 0, 0])
return (Evec[0], Evec[1], Evec[2], Bvec[0], Bvec[1], Bvec[2], 0.0,0.0,0.0)
deffieldsvec(coord, NUM_FIELDS): # REQUIRED FOR ANALYTIC_GRID2X, Y, Z=coordR=X**2+Y**2+Z**2Evec=np.zeros((X.shape[0],X.shape[1],X.shape[2],3))
Bvec=np.zeros((X.shape[0],X.shape[1],X.shape[2],3))
gridvals=np.zeros((X.shape[0],X.shape[1],X.shape[2],NUM_FIELDS))
ct=R<radEvec[ct,:] =np.expand_dims((1-R[ct] /rad), axis=-1) *np.array([1e4, 1e4, 1e4])
Bvec[ct,:] =np.expand_dims((1-R[ct] /rad), axis=-1) *np.array([-4e4, -4e4, -4e4])
gridvals[:,:,:,0:3] =Evecgridvals[:,:,:,3:6] =Bvecgridvals[:,:,:,6:9] =0.0returngridvals
The text was updated successfully, but these errors were encountered:
The section of prorad.py where it initializes the grid by looping over values in serial or even parallel is a choke point in speed:
There is a faster way to initialize the analytic grid, taking advantage of NumPy's vectorization capabilities. However, it requires more familiarity with the NumPy syntax by the user who writes the "fields()" function. I've written a local branch which leverages a new, vectorized "fields()" function in my input deck and then implements a new format in prorad.py called "analytic_grid2"; I can push it into this repo as an example if there is interest. I make a 300x300x300 analytic grid in a few seconds using the vectorized technique, but it a much smaller grid takes many times longer to generate with the original method.
Suggested addition to prorad.py:
Corresponding example of definition of fields in input deck (a radial blob of E & B):
The text was updated successfully, but these errors were encountered: