Changeset 226473d in sasmodels


Ignore:
Timestamp:
Jan 9, 2018 6:12:14 PM (6 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
2ab331f
Parents:
5fb0634
Message:

add Iqxy to realspace explorer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • explore/realspace.py

    ra1c32c2 r226473d  
    4444    """ 
    4545    return Rx(phi)*Ry(theta)*Rz(psi) 
     46 
     47def apply_view(points, view): 
     48    """ 
     49    Apply the view transform (theta, phi, psi) to a set of points. 
     50 
     51    Points are stored in a 3 x n numpy array. 
     52 
     53    View angles are in degrees. 
     54    """ 
     55    theta, phi, psi = view 
     56    return np.asarray((Rz(phi)*Ry(theta)*Rz(psi))*np.matrix(points.T)).T 
     57 
     58 
     59def invert_view(qx, qy, view): 
     60    """ 
     61    Return (qa, qb, qc) for the (theta, phi, psi) view angle at detector 
     62    pixel (qx, qy). 
     63 
     64    View angles are in degrees. 
     65    """ 
     66    theta, phi, psi = view 
     67    q = np.vstack((qx.flatten(), qy.flatten(), 0*qx.flatten())) 
     68    return np.asarray((Rz(-psi)*Ry(-theta)*Rz(-phi))*np.matrix(q)) 
     69 
    4670 
    4771class Shape: 
     
    219243        values = self.value.repeat(points.shape[0]) 
    220244        return values, self._adjust(points) 
     245 
     246def calc_Iqxy(qx, qy, rho, points, volume=1, view=(0, 0, 0)): 
     247    x, y, z = points.T 
     248    qx, qy = np.broadcast_arrays(qx, qy) 
     249    qa, qb, qc = invert_view(qx, qy, view) 
     250    rho, volume = np.broadcast_arrays(rho, volume) 
     251    values = rho*volume 
     252 
     253    # I(q) = |sum V(r) rho(r) e^(1j q.r)|^2 / sum V(r) 
     254    Iq = [abs(np.sum(values*np.exp(1j*(qa_k*x + qb_k*y + qc_k*z))))**2 
     255            for qa_k, qb_k, qc_k in zip(qa.flat, qb.flat, qc.flat)] 
     256    return np.array(Iq).reshape(qx.shape) / np.sum(volume) 
    221257 
    222258def _calc_Pr_nonuniform(r, rho, points): 
     
    333369        plt.legend() 
    334370 
     371def plot_calc_2d(qx, qy, Iqxy, theory=None): 
     372    import matplotlib.pyplot as plt 
     373    qx, qy = bin_edges(qx), bin_edges(qy) 
     374    #qx, qy = np.meshgrid(qx, qy) 
     375    if theory is not None: 
     376        plt.subplot(121) 
     377    plt.pcolormesh(qx, qy, np.log10(Iqxy)) 
     378    plt.xlabel('qx (1/A)') 
     379    plt.ylabel('qy (1/A)') 
     380    if theory is not None: 
     381        plt.subplot(122) 
     382        plt.pcolormesh(qx, qy, np.log10(theory)) 
     383        plt.xlabel('qx (1/A)') 
     384 
    335385def plot_points(rho, points): 
    336386    import mpl_toolkits.mplot3d 
     
    373423    Iq = Iq/Iq[0] 
    374424    return Iq 
     425 
     426def cylinder_Iqxy(qx, qy, radius, length, view=(0, 0, 0)): 
     427    qa, qb, qc = invert_view(qx, qy, view) 
     428    qab = np.sqrt(qa**2 + qb**2) 
     429    Fq = sas_2J1x_x(qab*radius) * j0(qc*length/2) 
     430    Iq = Fq**2 
     431    return Iq.reshape(qx.shape) 
    375432 
    376433def sphere_Iq(q, radius): 
     
    430487    pylab.show() 
    431488 
     489def check_shape_2d(shape, fn=None, view=(0, 0, 0)): 
     490    rho_solvent = 0 
     491    qx = qy = np.linspace(-1, 1, 100) 
     492    Qx, Qy = np.meshgrid(qx, qy) 
     493    sampling_density = 50000 / shape.volume() 
     494    rho, points = shape.sample(sampling_density) 
     495    Iqxy = calc_Iqxy(Qx, Qy, rho, points, view=view) 
     496    Iqxy += 0.001 * Iqxy.max() 
     497    theory = fn(Qx, Qy)+0.001 if fn is not None else None 
     498 
     499    import pylab 
     500    plot_calc_2d(qx, qy, Iqxy, theory=theory) 
     501    pylab.show() 
     502 
    432503def check_cylinder(radius=25, length=125, rho=2.): 
    433504    shape = EllipticalCylinder(radius, radius, length, rho) 
    434505    fn = lambda q: cylinder_Iq(q, radius, length) 
    435506    check_shape(shape, fn) 
     507 
     508def check_cylinder_2d(radius=25, length=125, rho=2., view=(0, 0, 0)): 
     509    shape = EllipticalCylinder(radius, radius, length, rho) 
     510    fn = lambda qx, qy, view=view: cylinder_Iqxy(qx, qy, radius, length, view=view) 
     511    check_shape_2d(shape, fn, view=view) 
    436512 
    437513def check_sphere(radius=125, rho=2): 
     
    454530if __name__ == "__main__": 
    455531    check_cylinder(radius=10, length=40) 
     532    #check_cylinder_2d(radius=10, length=40, view=(90,30,0)) 
    456533    #check_sphere() 
    457534    #check_csbox() 
Note: See TracChangeset for help on using the changeset viewer.