def field_cartesian(self, x, y, z): potential_sign = -1 if self.attractive else 1 x_grid, y_grid, z_grid = np.meshgrid(x, y, z) r_grid, theta_grid, phi_grid = Vectors3D.cartesian_to_spherical(x_grid, y_grid, z_grid) F_r = -potential_sign * self.a / r_grid**2 F_x, F_y, F_z = Vectors3D.spherical_to_cartesian(F_r, theta_grid, phi_grid) return F_x, F_y, F_z
def potential_on_grid(self, r_grid, theta_grid, phi_grid): if self.external_field[0] == 0: return np.zeros_like(r_grid) F_r = np.ones_like(r_grid) * self.external_field[0] F_theta = np.ones_like(r_grid) * self.external_field[1] F_phi = np.ones_like(r_grid) * self.external_field[2] x, y, z = Vectors3D.spherical_to_cartesian(r_grid, theta_grid, phi_grid) F_x, F_y, F_z = Vectors3D.spherical_to_cartesian(F_r, F_theta, F_phi) angles_grid = Vectors3D.angle_between_vectors((x, y, z), (F_x, F_y, F_z)) return F_r * r_grid * np.cos(angles_grid)
def field(self, r, theta, phi): r_grid, theta_grid, phi_grid = np.meshgrid(r, theta, phi) x_grid, y_grid, z_grid = Vectors3D.spherical_to_cartesian(r_grid, theta_grid, phi_grid) super_x = np.zeros_like(x_grid) super_y = np.zeros_like(y_grid) super_z = np.zeros_like(z_grid) for potential in self.potentials: F_r, F_theta, F_phi = potential.field(r, theta, phi) F_x, F_y, F_z = Vectors3D.spherical_to_cartesian(F_r, F_theta, F_phi) super_x += F_x super_y += F_y super_z += F_z super_r, super_theta, super_phi = Vectors3D.cartesian_to_spherical(super_x, super_y, super_z) return super_r, super_theta, super_phi
def plot_potential_cartesian(self, fig=None, x=None, y=None, z=None): if not self.USE_MAYAVI: return None if fig is None: fig = mlab.figure('Potential')#, bgcolor=(0, 0, 0)) #fig = mlab.figure('Potential', bgcolor=(0, 0, 0)) else: mlab.figure(fig, bgcolor=fig.scene.background) if x is None: x = np.linspace(1e-9, 5e-8, num=100, endpoint=True) if y is None: y = np.linspace(1e-9, 5e-8, num=100, endpoint=True) if z is None: z = np.linspace(1e-9, 5e-8, num=100, endpoint=True) x_grid, y_grid, z_grid = np.meshgrid(x, y, z) r_grid, theta_grid, phi_grid = Vectors3D.cartesian_to_spherical(x_grid, y_grid, z_grid) volumetric_data = self.potential_on_grid(r_grid, theta_grid, phi_grid) #volumetric_data = 1 / (x_grid**2 + y_grid**2 + z_grid**2) #mlab.contour3d(x_grid, y_grid, z_grid, volumetric_data, colormap='jet') #mlab.contour3d(volumetric_data, colormap='jet') source = mlab.pipeline.scalar_field(volumetric_data) min = volumetric_data.min() max = volumetric_data.max() vol = mlab.pipeline.volume(source) #vol = mlab.pipeline.volume(source, vmin=min + 0.65 * (max - min), # vmax=min + 0.9 * (max - min)) mlab.outline() return fig
def field_cartesian(self, x, y, z): grid = np.ones((np.array(x).size, np.array(y).size, np.array(z).size), dtype=np.float) ex, ey, ez = Vectors3D.spherical_to_cartesian(self.external_field[0], self.external_field[1], self.external_field[2]) F_x = grid * ex F_y = grid * ey F_z = grid * ez return F_x, F_y, F_z
def field_cartesian(self, x, y, z): x_grid, y_grid, z_grid = np.meshgrid(x, y, z) r_grid, theta_grid, phi_grid = Vectors3D.cartesian_to_spherical(x_grid, y_grid, z_grid) F_r = self.a / r_grid F_x, F_y, F_z = Vectors3D.spherical_to_cartesian(F_r, theta_grid, phi_grid) return F_x, F_y, F_z
def equation(r): field = self.field(r, theta, 0) field_x, field_y, field_z = Vectors3D.spherical_to_cartesian(field[0][0][0], field[1][0][0], field[2][0][0]) r_x, r_y, r_z = Vectors3D.spherical_to_cartesian(r, theta, 0) F_r = field_x * r_x + field_y * r_y + field_z * r_z return F_r