Changes between Initial Version and Version 1 of SphericalHarmonics


Ignore:
Timestamp:
Aug 28, 2020, 11:47:34 AM (6 years ago)
Author:
goddard
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SphericalHarmonics

    v1 v1  
     1= Making a Spherical Harmonic Surface =
     2
     3Here is some Python code when opened in ChimeraX creates spherical harmonic surfaces.  The real part of the spherical harmonic is the surface radius.
     4
     5    r = Ylm(theta, phi) where Ylm is a spherical harmonic, l = 2, m=0,1,2.
     6
     7[[Image(harmonics.png)]]
     8
     9Python code for ChimeraX 1.0:
     10
     11
     12{{{
     13# Make a spherical harmonic surface.
     14
     15from math import sin, cos, pi, sqrt
     16
     17def spherical_surface(session,
     18                      radius_function,
     19                      theta_steps = 100,
     20                      phi_steps = 50,
     21                      positive_color = (255,100,100,255), # red, green, blue, alpha, 0-255
     22                      negative_color = (100,100,255,255)):
     23
     24    # Compute vertices and vertex colors
     25    vertices = []
     26    colors = []
     27    for t in range(theta_steps):
     28        theta = (t/theta_steps) * 2*pi
     29        ct, st = cos(theta), sin(theta)
     30        for p in range(phi_steps):
     31            phi = (p/(phi_steps-1)) * pi
     32            cp, sp = cos(phi), sin(phi)
     33            r = radius_function(theta, phi)
     34            xyz = (r*sp*ct, r*sp*st, r*cp)
     35            vertices.append(xyz)
     36            color = positive_color if r >= 0 else negative_color
     37            colors.append(color)
     38
     39    # Compute triangles, triples of vertex indices
     40    triangles = []
     41    for t in range(theta_steps):
     42        for p in range(phi_steps-1):
     43            i = t*phi_steps + p
     44            t1 = (t+1)%theta_steps
     45            i1 = t1*phi_steps + p
     46            triangles.append((i,i+1,i1+1))
     47            triangles.append((i,i1+1,i1))
     48
     49    # Create numpy arrays
     50    from numpy import array, float32, uint8, int32
     51    va = array(vertices, float32)
     52    ca = array(colors, uint8)
     53    ta = array(triangles, int32)
     54
     55    # Compute average vertex normal vectors
     56    from chimerax.surface import calculate_vertex_normals
     57    na = calculate_vertex_normals(va, ta)
     58
     59    # Create ChimeraX surface model
     60    from chimerax.core.models import Surface
     61    s = Surface('surface', session)
     62    s.set_geometry(va, na, ta)
     63    s.vertex_colors = ca
     64    session.models.add([s])
     65
     66    return s
     67
     68# Example spherical harmonic function Y(l=2,m=0) real part.
     69def y20_re(theta, phi):
     70    return 0.25*sqrt(5/pi) * (3*cos(phi)**2 - 1)
     71def y21_re(theta, phi):
     72    return -0.5*sqrt(15/(2*pi)) * sin(phi)*cos(phi) * cos(theta)
     73def y22_re(theta, phi):
     74    return 0.25*sqrt(15/(2*pi)) * sin(phi)**2 * cos(2*theta)
     75
     76# Create 3 surfaces
     77spherical_surface(session, y20_re)
     78
     79s1 = spherical_surface(session, y21_re)
     80from chimerax.geometry import translation
     81s1.position = translation((1,0,0))
     82
     83s2 = spherical_surface(session, y22_re)
     84s2.position = translation((2,0,0))
     85
     86}}}