MCPcopy Create free account
hub / github.com/NanoComp/meep / plot3D

Function plot3D

python/visualization.py:1092–1260  ·  view source on GitHub ↗
(sim, save_to_image: bool = False, image_name: str = "sim.png", **kwargs)

Source from the content-addressed store, hash-verified

1090
1091
1092def plot3D(sim, save_to_image: bool = False, image_name: str = "sim.png", **kwargs):
1093 from vispy.scene.visuals import Box, Mesh
1094 from vispy.scene import SceneCanvas, transforms
1095
1096 try:
1097 from skimage.measure import marching_cubes
1098 except:
1099 from skimage.measure import marching_cubes_lewiner as marching_cubes
1100 from vispy.visuals.filters import ShadingFilter
1101
1102 # Set canvas
1103 canvas = SceneCanvas(keys="interactive", bgcolor="white")
1104
1105 view = canvas.central_widget.add_view()
1106 view.camera = "turntable"
1107
1108 # Get domain measurements
1109 sim_center, sim_size = sim.geometry_center, sim.cell_size
1110
1111 xmin, xmax, ymin, ymax, zmin, zmax = mp.visualization.box_vertices(
1112 sim_center, sim_size, sim.is_cylindrical
1113 )
1114
1115 grid_resolution = sim.resolution
1116
1117 Nx = int((xmax - xmin) * grid_resolution + 1)
1118 Ny = int((ymax - ymin) * grid_resolution + 1)
1119 Nz = int((zmax - zmin) * grid_resolution + 1)
1120
1121 xtics = np.linspace(xmin, xmax, Nx)
1122 ytics = np.linspace(ymin, ymax, Ny)
1123 ztics = np.linspace(zmin, zmax, Nz)
1124
1125 # Get eps for geometry
1126 eps_data = np.round(np.real(sim.get_epsilon_grid(xtics, ytics, ztics)), 2)
1127
1128 unique = np.unique(np.abs(eps_data)).tolist()
1129
1130 # Remove background material
1131 unique.remove(np.round(np.abs(np.asarray(sim.default_material.epsilon_diag)), 2)[0])
1132
1133 mesh_midpoint = (sim_size[0] / 2, sim_size[1] / 2, sim_size[2] / 2)
1134
1135 light_dir = (0, 0, -1, 0)
1136
1137 # Build geometry
1138 for i, eps in enumerate(unique):
1139 eps_ = np.array(eps_data.flatten() == eps).astype(int).reshape(eps_data.shape)
1140 marching_cube = marching_cubes(
1141 eps_,
1142 0.99,
1143 spacing=(sim.cell_size.x / Nx, sim.cell_size.y / Ny, sim.cell_size.z / Nz),
1144 )
1145 vertices, faces = marching_cube[0], marching_cube[1]
1146
1147 mesh = Mesh(
1148 vertices,
1149 faces,

Callers

nothing calls this directly

Calls 5

get_boundary_volumesFunction · 0.85
_build_3d_pmlFunction · 0.85
get_epsilon_gridMethod · 0.80
normMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected