Skip to content

Instantly share code, notes, and snippets.

@wjiang
Created January 24, 2019 02:30
Show Gist options
  • Save wjiang/e1cc17bc8e74f4bb47c4c97ed34c4bed to your computer and use it in GitHub Desktop.
Save wjiang/e1cc17bc8e74f4bb47c4c97ed34c4bed to your computer and use it in GitHub Desktop.
simulate a torus in 3D
import numpy as np
import matplotlib.pyplot as plt
import mrcfile
n = 64 # 3D map size in pixels
distance_ringcenter_to_boxcenter = n/2.0 * 0.5
radius_ring = distance_ringcenter_to_boxcenter * 0.1
def buildTorus(n, distance_ringcenter_to_boxcenter, radius_ring):
# http://mathworld.wolfram.com/Torus.html
m = np.zeros((n, n, n), dtype=float)
z, y, x = np.mgrid[-n/2:n/2, -n/2:n/2, -n/2:n/2]
x2 = x*x
y2 = y*y
z2 = z*z
r2 = np.square(distance_ringcenter_to_boxcenter-np.sqrt(x2+y2))+z2
m = np.exp( -r2/(radius_ring*radius_ring))
return m
torus = buildTorus(n, distance_ringcenter_to_boxcenter, radius_ring)
with mrcfile.new('torus.mrc', overwrite=True) as mrc:
mrc.set_data(torus.astype(np.float32))
plt.figure(0)
plt.imshow(torus[n/2,:,:])
plt.gray()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment