GIWAXSPal

Tolbert Lab, UCLA

2022–2025

MATLAB
Project Description

GIWAXSPal is a scientific computing package for simulating grazing-incidence wide-angle X-ray scattering (GIWAXS) patterns from molecular structures. By comparing simulated patterns with experimental measurements, researchers can evaluate proposed models of molecular organization. Originally developed to predict the scattering behavior of semicrystalline semiconducting polymers, the package includes geometry modeling tools tailored to polymer structures while supporting pattern prediction from arbitrary atomic geometries.

GIWAXSPal supports independent calculation of 1D scattering profiles, 2D scattering patterns, and 3D reciprocal-space intensity distributions, allowing researchers to investigate how different features of a proposed molecular structure contribute to discrepancies between simulated and experimental patterns. The package also provides tools for post-processing simulated patterns and converting them into datasets suitable for further analysis.

Polymer crystal structure Example polymer crystal structure
Experimental and simulated GIWAXS patterns Experimental and simulated GIWAXS patterns
Technical Architecture

GIWAXSPal is implemented in MATLAB and supports molecular crystal construction using XYZ coordinate files. Internally, the package represents crystal geometry through three abstractions: a motif (molecule or monomer), a cell (a repeating unit of lattice points), and a crystal (a group of cells). These objects are combined to construct simulated crystals while remaining independently manipulable within a larger sample class. This architecture mirrors the motif, cell, and crystal decomposition used by the scattering algorithm, exploiting translational symmetry to reduce computation and enabling the simulation of larger lattices without explicitly storing every atomic coordinate.

Intermediate geometry objects can be cached as JSON files and reloaded as needed. This allows partially constructed geometries to be saved and reused across scripted crystal construction and scattering simulation workflows. The resulting scattering data can be processed and exported as CSV datasets for external analysis. Simulated GIWAXS patterns can be saved using MATLAB's plotting tools, and the package provides additional utilities for generating animated GIFs of molecular structures and scattering visualizations.

Modeling procedure for 2D and 3D patterns Modeling procedure for 2D and 3D patterns
Engineering Highlights
Factorized and Vectorized Pattern Prediction

The primary computational challenge in developing GIWAXSPal was efficiently evaluating the structure factor equation across a simulated detector. For a structure containing N atoms and a detector with X × Y pixels, a naïve iterative implementation requires O(NXY) operations, quickly becoming prohibitively expensive for large structures and high-resolution patterns.

To address this bottleneck, I developed a factorized structure factor solver that exploits the translational symmetry of crystalline structures. By decomposing atomic positions into motif, cell, and crystal contributions, the structure factor equation can be separated into products of smaller summations, reducing redundant calculations. These summations are then reorganized into vectorized matrix operations that take advantage of MATLAB's optimized numerical routines.

Symmetry-based atom partitioning Symmetry-based atom partitioning
\( \displaystyle{ \begin{aligned} \; F(\mathbf{q}_i) &= \sum_{j} f_{j} e^{ \mathrm{i} \mathbf{q}_i \cdot \mathbf{r}_j } , \quad \mathbf{r}_j = \mathbf{m}_k + \mathbf{c}_l + \mathbf{k}_m \\ &= \sum_{k,l,m} f_{k} e^{ \mathrm{i} \mathbf{q}_i \cdot (\mathbf{m}_k + \mathbf{c}_l + \mathbf{k}_m) } \end{aligned} \\ \boxed{ F(\mathbf{q}_i) = \underbrace{ \left( \sum_{k} f_{k} e^{ \mathrm{i} \mathbf{q}_i \cdot m_k} \right) }_{ \mathrm{motif} } \underbrace{ \left( \sum_{l} e^{ \mathrm{i} \mathbf{q}_i \cdot \mathbf{c}_l} \right) }_{ \mathrm{cell} } \underbrace{ \left( \sum_{m} e^{ \mathrm{i} \mathbf{q}_i \cdot \mathbf{k}_m} \right) }_{ \mathrm{crystal} } } } \)

The vectorized implementation achieved approximately 100× faster pattern prediction than an iterative implementation of the factorized calculation, enabling simulations of larger structures and higher-resolution scattering patterns.

Iterative vs optimized algorithm performance Iterative vs optimized algorithm performance
Linear effective scaling at high computational load Linear effective scaling at high computational load
Atom Selection Within a Plot

Although MATLAB provides extensive tools for 3D visualization, selecting individual objects within a plot is not straightforward. To support interactive molecular geometry manipulation, I developed a ray-based picking algorithm that identifies atoms near a user's cursor.

The algorithm accounts for the current viewing orientation and projects a ray from the cursor into the 3D scene, perpendicular to the viewing plane. A finite-radius cylindrical selection region surrounding the ray identifies all atomic coordinates within its bounds, which are then stored for subsequent geometric transformations. By rotating the molecular structure to align desired atoms along the viewing direction, users can select entire groups of atoms with a single click.

Collaborators

GIWAXSPal was designed, developed, and maintained by me as part of my PhD research at the University of California, Los Angeles (UCLA), under the guidance of my principal investigator, Professor Sarah Tolbert.