def double_pendulum(theta, length, mass): gr.clearws() gr.setviewport(0, 1, 0, 1) direction = [] position = [(0, 0, 0)] for i in range(2): direction.append( (sin(theta[i]) * length[i] * 2, -cos(theta[i]) * length[i] * 2, 0)) position.append([position[-1][j] + direction[-1][j] for j in range(3)]) gr3.clear() # draw pivot point gr3.drawcylindermesh(1, (0, 0.2, 0), (0, 1, 0), (0.4, 0.4, 0.4), 0.4, 0.05) gr3.drawcylindermesh(1, (0, 0.2, 0), (0, -1, 0), (0.4, 0.4, 0.4), 0.05, 0.2) gr3.drawspheremesh(1, (0, 0, 0), (0.4, 0.4, 0.4), 0.05) # draw rods gr3.drawcylindermesh(2, position, direction, (0.6, 0.6, 0.6) * 2, (0.05, 0.05), [l * 2 for l in length]) # draw bobs gr3.drawspheremesh(2, position[1:], (1, 1, 1) * 2, [m * 0.2 for m in mass]) gr3.drawimage(0, 1, 0, 1, 500, 500, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr.updatews() return
def init_gr3(): def log_callback(message): print "Log: ", message gr3.setlogcallback(log_callback) gr3.init([gr3.GR3_InitAttribute.GR3_IA_FRAMEBUFFER_WIDTH, 1024, gr3.GR3_InitAttribute.GR3_IA_FRAMEBUFFER_HEIGHT, 1024]) print gr3.getrenderpathstring() # Atome zeichnen gr3.drawspheremesh(len(atom_data), sphere_positions, sphere_colors, sphere_radii) # Atombindungen zeichnen gr3.drawcylindermesh(len(cylinder_positions), cylinder_positions, cylinder_directions, cylinder_colors, cylinder_radii, cylinder_lengths)
def pendulum(t, theta, omega, acceleration): gr.clearws() gr.setviewport(0, 1, 0, 1) x, y = (sin(theta) * 3.0, -cos(theta) * 3.0) gr3.clear() # draw pivot point gr3.drawspheremesh(1, (0, 0, 0), (0.4, 0.4, 0.4), 0.1) # draw rod gr3.drawcylindermesh(1, (0, 0, 0), (x, y, 0), (0.6, 0.6, 0.6), 0.05, 3.0) # draw sphere gr3.drawspheremesh(1, (x, y, 0), (1, 1, 1), 0.25) # show angular velocity V = 0.3 * omega - sign(omega) * 0.15 gr3.drawcylindermesh(1, (x, y, 0), (cos(theta), sin(theta), 0), (0, 0, 1), 0.05, V) gr3.drawconemesh(1, (x + cos(theta) * V, y + sin(theta) * V, 0), (-y, x, 0), (0, 0, 1), 0.1, sign(omega) * 0.25) # show angular acceleration A = 0.3 * acceleration gr3.drawcylindermesh(1, (x, y, 0), (sin(theta), cos(theta), 0), (1, 0, 0), 0.05, A) gr3.drawconemesh(1, (x + sin(theta) * A, y + cos(theta) * A, 0), (x, -y, 0), (1, 0, 0), 0.1, 0.25) # draw GR3 objects gr3.drawimage(0, 1, 0.15, 0.85, 500, 350, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr.settextfontprec(2, gr.TEXT_PRECISION_STRING) gr.setcharheight(0.024) gr.settextcolorind(1) gr.textext(0.05, 0.96, 'Damped Pendulum') gr.mathtex(0.05, 0.9, '\\omega=\\dot{\\theta}') gr.mathtex(0.05, 0.83, '\\dot{\\omega}=-\\gamma\\omega-\\frac{g}{l}sin(\\theta)') gr.setcharheight(0.020) gr.textext(0.05, 0.20, 't:%7.2f' % t) gr.textext(0.05, 0.16, '\\theta:%7.2f' % (theta / pi * 180)) gr.settextcolorind(4) gr.textext(0.05, 0.12, '\\omega:%7.2f' % omega) gr.settextcolorind(2) gr.textext(0.05, 0.08, 'y_{A}:%6.2f' % acceleration) gr.updatews() return
def init_gr3(): def log_callback(message): print("Log: ", message) gr3.setlogcallback(log_callback) gr3.init([ gr3.GR3_InitAttribute.GR3_IA_FRAMEBUFFER_WIDTH, 1024, gr3.GR3_InitAttribute.GR3_IA_FRAMEBUFFER_HEIGHT, 1024 ]) print(gr3.getrenderpathstring()) # draw atoms gr3.drawspheremesh(len(atom_data), sphere_positions, sphere_colors, sphere_radii) # draw atomic bonds gr3.drawcylindermesh(len(cylinder_positions), cylinder_positions, cylinder_directions, cylinder_colors, cylinder_radii, cylinder_lengths)
def _create_gr3_scene(molecule, show_bonds=True): """ Create the GR3 scene from the provided molecule and - if show_bonds is True (default) - the atomic bonds in the molecule. """ gr3.clear() num_atoms = len(molecule.atomic_numbers) gr3.drawspheremesh(num_atoms, molecule.positions, ATOM_COLORS[molecule.atomic_numbers], molecule.atomic_radii) if show_bonds and len(molecule.bonds.index_pairs) > 0: index_pairs = molecule.bonds.index_pairs num_bonds = len(molecule.bonds) bond_positions = molecule.positions[index_pairs[:, 0]] bond_directions = molecule.positions[index_pairs[:, 1]]-bond_positions bond_lengths = la.norm(bond_directions, axis=1) bond_directions /= bond_lengths.reshape(num_bonds, 1) bond_radii = np.ones(num_bonds, dtype=np.float32)*BOND_RADIUS bond_colors = np.ones((num_bonds, 3), dtype=np.float32)*0.3 gr3.drawcylindermesh(num_bonds, bond_positions, bond_directions, bond_colors, bond_radii, bond_lengths)
def drawAtoms(self, natoms, positions, colors=None, radii=None): gr3.drawspheremesh(natoms, positions, colors, radii)
#!/usr/bin/env python # -*- animation -*- """ 3D animation of a spring pendulum """ import math import gr import gr3 gr.setviewport(0, 1, 0, 1) gr3.setbackgroundcolor(1, 1, 1, 1) for t in range(200): f = 0.0375 * (math.cos(t*0.2) * 0.995**t + 1.3) n = 90 points = [(math.sin(i*math.pi/8), n*0.035-i*f, math.cos(i*math.pi/8)) for i in range(n-5)] points.append((0, points[-1][1], 0)) points.append((0, points[-1][1]-0.5, 0)) points.append((0, points[-1][1]-1, 0)) points.insert(0, (0, points[0][1], 0)) points.insert(0, (0, points[0][1]+2, 0)) colors = [(1, 1, 1)]*n radii = [0.1]*n gr3.clear() gr3.drawtubemesh(n, points, colors, radii) gr3.drawspheremesh(1, points[-1], colors, 0.75) gr.clearws() gr3.drawimage(0, 1, 0, 1, 500, 500, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr.updatews()
def create_scene(self): """ Create GR3 meshes. ``self.results`` contains the mesh data and in ``self.settings`` is specified which meshes shound be rendered. """ c = config.Colors.background gr3.setbackgroundcolor(c[0], c[1], c[2], 1.0) gr3.clear() if self.results is None: return show_domains = self.settings.show_domains show_surface_cavities = self.settings.show_surface_cavities show_center_cavities = self.settings.show_center_cavities if show_center_cavities and self.results.center_cavities is not None: show_surface_cavities = False elif show_surface_cavities and self.results.surface_cavities is not None: show_domains = False self.objectids = [None] edges = self.results.atoms.volume.edges num_edges = len(edges) edge_positions = [edge[0] for edge in edges] edge_directions = [[edge[1][i]-edge[0][i] for i in range(3)] for edge in edges] edge_lengths = [sum([c*c for c in edge])**0.5 for edge in edge_directions] edge_radius = min(edge_lengths)/200 if self.settings.show_bounding_box: gr3.drawcylindermesh(num_edges, edge_positions, edge_directions, [config.Colors.bounding_box]*num_edges, [edge_radius]*num_edges, edge_lengths) corners = list(set([tuple(edge[0]) for edge in edges] + [tuple(edge[1]) for edge in edges])) num_corners = len(corners) gr3.drawspheremesh(num_corners, corners, [config.Colors.bounding_box]*num_corners, [edge_radius]*num_corners) if self.settings.show_atoms and self.results.atoms is not None: visible_atom_indices = self.settings.visible_atom_indices if visible_atom_indices is not None: visible_atom_indices = [comp for comp in visible_atom_indices if 0 <= comp < self.results.atoms.number] else: visible_atom_indices = range(self.results.atoms.number) if len(visible_atom_indices) == 0: visible_atom_indices = None if visible_atom_indices is not None: visible_atom_indices = np.array(visible_atom_indices) gr3.drawspheremesh(len(visible_atom_indices), self.results.atoms.positions[visible_atom_indices], self.results.atoms.colors[visible_atom_indices], np.ones(len(visible_atom_indices))*config.OpenGL.atom_radius) if self.settings.show_bonds: bonds = self.results.atoms.bonds for start_index, target_indices in enumerate(bonds): if start_index not in visible_atom_indices: continue target_indices = np.array([i for i in target_indices if i in visible_atom_indices]) if len(target_indices) == 0: continue start_position = self.results.atoms.positions[start_index] target_positions = self.results.atoms.positions[target_indices] directions = target_positions - start_position bond_lengths = la.norm(directions, axis=1) directions /= bond_lengths.reshape(len(directions), 1) gr3.drawcylindermesh(len(target_indices), target_positions, -directions, [config.Colors.bonds] * self.results.atoms.number, np.ones(bond_lengths.shape)*config.OpenGL.bond_radius, bond_lengths) if self.results is None: return if show_domains and self.results.domains is not None: self.draw_cavities(self.results.domains, config.Colors.domain, 'domain', self.settings.visible_domain_indices) if show_surface_cavities and self.results.surface_cavities is not None: self.draw_cavities(self.results.surface_cavities, config.Colors.surface_cavity, 'surface cavity', self.settings.visible_surface_cavity_indices) if show_center_cavities and self.results.center_cavities is not None: self.draw_cavities(self.results.center_cavities, config.Colors.center_cavity, 'center cavity', self.settings.visible_center_cavity_indices)
# -*- animation -*- """ 3D animation of a spring pendulum """ import math import gr import gr3 gr.setviewport(0, 1, 0, 1) gr3.setbackgroundcolor(1, 1, 1, 1) for t in range(200): f = 0.0375 * (math.cos(t * 0.2) * 0.995**t + 1.3) n = 90 points = [(math.sin(i * math.pi / 8), n * 0.035 - i * f, math.cos(i * math.pi / 8)) for i in range(n - 5)] points.append((0, points[-1][1], 0)) points.append((0, points[-1][1] - 0.5, 0)) points.append((0, points[-1][1] - 1, 0)) points.insert(0, (0, points[0][1], 0)) points.insert(0, (0, points[0][1] + 2, 0)) colors = [(1, 1, 1)] * n radii = [0.1] * n gr3.clear() gr3.drawtubemesh(n, points, colors, radii) gr3.drawspheremesh(1, points[-1], colors, 0.75) gr.clearws() gr3.drawimage(0, 1, 0, 1, 500, 500, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr.updatews()