KSpace Module

Overview

freud.kspace.meshgrid2 Computes an n-dimensional meshgrid.
freud.kspace.SFactor3DPoints Compute the full 3D structure factor of a given set of points.
freud.kspace.AnalyzeSFactor3D Analyze the peaks in a 3D structure factor.
freud.kspace.SingleCell3D SingleCell3D objects manage data structures necessary to call the Fourier Transform functions that evaluate FTs for given form factors at a list of \(K\) points.
freud.kspace.FTfactory Factory to return an FT object of the requested type.
freud.kspace.FTbase Base class for FT calculation classes.
freud.kspace.FTdelta Fourier transform a list of delta functions.
freud.kspace.FTsphere Fourier transform for sphere.
freud.kspace.FTpolyhedron Fourier Transform for polyhedra.
freud.kspace.FTconvexPolyhedron Fourier Transform for convex polyhedra.
freud.kspace.rotate Axis-angle rotation.
freud.kspace.quatrot Apply a rotation quaternion.
freud.kspace.Constraint Constraint base class.
freud.kspace.AlignedBoxConstraint Axis-aligned Box constraint.
freud.kspace.constrainedLatticePoints Generate a list of points satisfying a constraint.
freud.kspace.reciprocalLattice3D Calculate reciprocal lattice vectors.
freud.kspace.DeltaSpot Base class for drawing diffraction spots on a 2D grid.
freud.kspace.GaussianSpot Draw diffraction spot as a Gaussian blur.

Details

Modules for calculating quantities in reciprocal space, including Fourier transforms of shapes and diffraction pattern generation.

Structure Factor

class freud.kspace.SFactor3DPoints(box, g)[source]

Compute the full 3D structure factor of a given set of points.

Given a set of points \(\vec{r}_i\), SFactor3DPoints computes the static structure factor \(S \left( \vec{q} \right) = C_0 \left| {\sum_{m=1}^{N} e^{\mathit{i}\vec{q}\cdot\vec{r_i}}} \right|^2\).

In this expression, \(C_0\) is a scaling constant chosen so that \(S\left(0\right) = 1\), and \(N\) is the number of particles.

\(S\) is evaluated on a grid of \(q\)-values \(\vec{q} = h \frac{2\pi}{L_x} \hat{i} + k \frac{2\pi}{L_y} \hat{j} + l \frac{2\pi}{L_z} \hat{k}\) for integer \(h,k,l: \left[-g,g\right]\) and \(L_x, L_y, L_z\) are the box lengths in each direction.

After calling compute(), access the \(q\) values with getQ(), the static structure factor values with getS(), and (if needed) the un-squared complex version of \(S\) with getSComplex(). All values are stored in 3D numpy.ndarray structures. They are indexed by \(a, b, c\) where \(a=h+g, b=k+g, c=l+g\).

Note

Due to the way that numpy arrays are indexed, access the returned S array as S[c,b,a] to get the value at \(q = \left(qx\left[a\right], qy\left[b\right], qz\left[c\right]\right)\).

Parameters:
  • box (freud.box.Box) – The simulation box.
  • g (int) – The number of grid points for \(q\) in each direction is 2*g+1.
compute(points)[source]

Compute the static structure factor of a given set of points.

After calling compute(), you can access the results with getS(), getSComplex(), and the grid with getQ().

Parameters:points ((\(N_{particles}\), 3) numpy.ndarray) – Points used to compute the static structure factor.
getQ()[source]

Get the \(q\) values at each point.

The structure factor S[c,b,a] is evaluated at the vector \(q = \left(qx\left[a\right], qy\left[b\right], qz\left[c\right]\right)\).

Returns:(qx, qy, qz).
Return type:tuple
getS()[source]

Get the computed static structure factor.

Returns:The computed static structure factor as a copy.
Return type:(X, Y) numpy.ndarray
getSComplex()[source]

Get the computed complex structure factor (if you need the phase information).

Returns:The computed static structure factor, as a copy, without taking the magnitude squared.
Return type:(X, Y) numpy.ndarray
class freud.kspace.AnalyzeSFactor3D(S)[source]

Analyze the peaks in a 3D structure factor.

Given a structure factor \(S\left(q\right)\) computed by classes such as SFactor3DPoints, AnalyzeSFactor3D performs a variety of analysis tasks.

  • Identifies peaks.
  • Provides a list of peaks and the vector \(\vec{q}\) positions at which they occur.
  • Provides a list of peaks grouped by \(q^2\)
  • Provides a full list of \(S\left(\left|q\right|\right)\) values vs \(q^2\) suitable for plotting the 1D analog of the structure factor.
  • Scans through the full 3D peaks and reconstructs the Bravais lattice.

Note

All of these operations work in an indexed integer \(q\)-space \(h,k,l\). Any peak position values returned must be multiplied by \(2\pi/L\) to get to real \(q\) values in simulation units.

Parameters:S (numpy.ndarray) – Static structure factor to analyze.
getPeakDegeneracy(cut)[source]

Get a dictionary of peaks indexed by \(q^2\).

Parameters:cut (numpy.ndarray) – All \(S\left(q\right)\) values greater than cut will be counted as peaks.
Returns:A dictionary with keys \(q^2\) and a list of peaks for the corresponding values.
Return type:dict
getPeakList(cut)[source]

Get a list of peaks in the structure factor.

Parameters:cut (float) – All \(S\left(q\right)\) values greater than cut will be counted as peaks.
Returns:peaks, \(q\) as lists.
Return type:list
getSvsQ()[source]

Get a list of all \(S\left(\left|q\right|\right)\) values vs \(q^2\).

Returns:S, qsquared.
Return type:numpy.ndarray
class freud.kspace.SingleCell3D(k, ndiv, dK, boxMatrix)[source]

SingleCell3D objects manage data structures necessary to call the Fourier Transform functions that evaluate FTs for given form factors at a list of \(K\) points. SingleCell3D provides an interface to helper functions to calculate \(K\) points for a desired grid from the reciprocal lattice vectors calculated from an input boxMatrix. State is maintained as set_ and update_ functions invalidate internal data structures and as fresh data is restored with update_ function calls. This should facilitate management with a higher-level UI such as a GUI with an event queue.

I’m not sure what sort of error checking would be most useful, so I’m mostly allowing ValueErrors and such exceptions to just occur and then propagate up through the calling functions to be dealt with by the user.

Parameters:
  • ndiv (int) – The resolution of the diffraction image grid.
  • k (float) – The angular wave number of the plane wave probe (Currently unused).
  • dK (float) – The k-space unit associated with the diffraction image grid spacing.
  • boxMatrix ((\(N_{particles}\), 3) numpy.ndarray) – The unit cell lattice vectors as columns in a 3x3 matrix.
  • scale (float) – nm per unit length (default 1.0).

Note

  • The set_ functions take a single parameeter and cause other internal data structures to become invalid.
  • The update_ and calculate functions restore the validity of these structures using internal data.
  • The functions are separate to make it easier to avoid unnecessary computation such as when changing multiple parameters before seeking output or when wrapping the code with an interface with an event queue.
add_ptype(name)[source]

Create internal data structures for new particle type by name.

Particle type is inactive when added because parameters must be set before FT can be performed.

Parameters:name (str) – particle name
calculate(*args, **kwargs)[source]

Calculate FT. The details and arguments will vary depending on the form factor chosen for the particles.

For any particle type-dependent parameters passed as keyword arguments, the parameter must be passed as a list of length max(p_type)+1 with indices corresponding to the particle types defined. In other words, type-dependent parameters are optional (depending on the set of form factors being calculated), but if included must be defined for all particle types.

get_form_factors()[source]

Get form factor names and indices.

Returns:List of factor names and indices.
Return type:list
get_ptypes()[source]

Get ordered list of particle names.

Returns:List of particle names.
Return type:list
remove_ptype(name)[source]

Remove internal data structures associated with ptype name.

Parameters:name (str) – Particle type to remove.

Note

This shouldn’t usually be necessary, since particle types may be set inactive or have any of their properties updated through set_ methods.

set_active(name)[source]

Set particle type active.

Parameters:name (str) – Particle name.
set_box(boxMatrix)[source]

Set box matrix.

Parameters:boxMatrix ((3, 3) numpy.ndarray) – Unit cell box matrix.
set_dK(dK)[source]

Set grid spacing in diffraction image.

Parameters:dK (float) – Difference in \(K\) vector between two adjacent diffraction image grid points.
set_form_factor(name, ff)[source]

Set scattering form factor.

Parameters:
set_inactive(name)[source]

Set particle type inactive.

Parameters:name (str) – Particle name.
set_k(k)[source]

Set angular wave number of plane wave probe.

Parameters:k (float) – \(\left|k_0\right|\).
set_ndiv(ndiv)[source]

Set number of grid divisions in diffraction image.

Parameters:ndiv (int) – Define diffraction image as ndiv x ndiv grid.
set_param(particle, param, value)[source]

Set named parameter for named particle.

Parameters:
  • particle (str) – Particle name.
  • param (str) – Parameter name.
  • value (float) – Parameter value.
set_rq(name, position, orientation)[source]

Set positions and orientations for a particle type.

To best maintain valid state in the event of changing numbers of particles, position and orientation are updated in a single method.

Parameters:
  • name (str) – Particle type name.
  • position ((N,3) numpy.ndarray) – Array of particle positions.
  • orientation ((N,4) numpy.ndarray) – Array of particle quaternions.
set_scale(scale)[source]

Set scale factor. Store global value and set for each particle type.

Parameters:scale (float) – nm per unit for input file coordinates.
update_K_constraint()[source]

Recalculate constraint used to select \(K\) values.

The constraint used is a slab of epsilon thickness in a plane perpendicular to the \(k_0\) propagation, intended to provide easy emulation of TEM or relatively high-energy scattering.

update_Kpoints()[source]

Update \(K\) points at which to evaluate FT.

Note

If the diffraction image dimensions change relative to the reciprocal lattice, the \(K\) points need to be recalculated.

update_bases()[source]

Update the direct and reciprocal space lattice vectors.

Note

If scale or boxMatrix is updated, the lattice vectors in direct and reciprocal space need to be recalculated.

class freud.kspace.FTfactory[source]

Factory to return an FT object of the requested type.

addFT(name, constructor, args=None)[source]

Add an FT class to the factory.

Parameters:
  • name (str) – Identifying string to be returned by getFTlist().
  • constructor (str) – Class / function name to be used to create new FT objects.
  • args (list) – Set default argument object to be used to construct FT objects.
getFTlist()[source]

Get an ordered list of named FT types.

Returns:List of FT names.
Return type:list
getFTobject(i, args=None)[source]

Get a new instance of an FT type from list returned by getFTlist().

Parameters:
  • i (int) – Index into list returned by getFTlist().
  • args – Argument object used to initialize FT, overriding default set at addFT().
class freud.kspace.FTbase[source]

Base class for FT calculation classes.

getFT()[source]

Return Fourier Transform.

Returns:Fourier Transform.
Return type:numpy.ndarray
get_density(density)[source]

Get density.

Returns:Density.
Return type:numpy.complex64
get_parambyname(name)[source]

Get named parameter for object.

Parameters:name (str) – Parameter name. Must exist in list returned by get_params().
Returns:Parameter value.
Return type:float
get_params()[source]

Get the parameter names accessible with set_parambyname().

Returns:Parameter names.
Return type:list
get_scale()[source]

Get scale.

Returns:Scale.
Return type:float
set_K(K)[source]

Set \(K\) points to be evaluated.

Parameters:K (numpy.ndarray) – List of \(K\) vectors at which to evaluate FT.
set_density(density)[source]

Set density.

Parameters:density (numpy.complex64) – Density.
set_parambyname(name, value)[source]

Set named parameter for object.

Parameters:
  • name (str) – Parameter name. Must exist in list returned by get_params().
  • value (float) – Parameter value to set.
set_rq(r, q)[source]

Set \(r\), \(q\) values.

Parameters:
set_scale(scale)[source]

Set scale.

Parameters:scale (float) – Scale.
class freud.kspace.FTdelta[source]

Fourier transform a list of delta functions.

compute(*args, **kwargs)[source]

Compute FT.

Calculate \(S = \sum_{\alpha} e^{-i \mathbf{K} \cdot \mathbf{r}_{\alpha}}\).

set_K(K)[source]

Set \(K\) points to be evaluated.

Parameters:K (numpy.ndarray) – List of \(K\) vectors at which to evaluate FT.
set_density(density)[source]

Set density.

Parameters:density (numpy.complex64) – density
set_rq(r, q)[source]

Set \(r\), \(q\) values.

Parameters:
set_scale(scale)[source]

Set scale.

Parameters:scale (float) – Scale.

Note

For a scale factor, \(\lambda\), affecting the scattering density \(\rho\left(r\right)\), \(S_{\lambda}\left (k\right) == \lambda^3 * S\left(\lambda * k\right)\)

class freud.kspace.FTsphere[source]

Fourier transform for sphere.

Calculate \(S = \sum_{\alpha} e^{-i \mathbf{K} \cdot \mathbf{r}_{\alpha}}\).

get_radius()[source]

Get radius parameter.

If appropriate, return value should be scaled by get_parambyname('scale') for interpretation.

Returns:Unscaled radius.
Return type:float
set_radius(radius)[source]

Set radius parameter.

Parameters:radius (float) – Sphere radius will be stored as given, but scaled by scale parameter when used by methods.
class freud.kspace.FTpolyhedron[source]

Fourier Transform for polyhedra.

compute(*args, **kwargs)[source]

Compute FT.

Calculate \(S = \sum_{\alpha} e^{-i \mathbf{K} \cdot \mathbf{r}_{\alpha}}\).

get_radius()[source]

Get radius parameter.

If appropriate, return value should be scaled by get_parambyname('scale') for interpretation.

Returns:Unscaled radius.
Return type:float
set_K(K)[source]

Set \(K\) points to be evaluated.

Parameters:K (numpy.ndarray) – List of \(K\) vectors at which to evaluate FT.
set_density(density)[source]

Set density.

Parameters:density (numpy.complex64) – Density.
set_params(verts, facets, norms, d, areas, volume)[source]

Construct list of facet offsets.

Parameters:
  • verts ((\(N_{particles}\), 3) numpy.ndarray) – Vertex coordinates.
  • facets ((\(N_{facets}\), 3) numpy.ndarray) – Facet vertex indices.
  • norms ((\(N_{facets}\), 3) numpy.ndarray) – Facet normals.
  • d ((\(N_{facets}-1\)) numpy.ndarray) – Facet distances.
  • area ((\(N_{facets}-1\)) numpy.ndarray) – Facet areas.
  • volume (float) – Polyhedron volume.
set_radius(radius)[source]

Set radius of in-sphere.

Parameters:radius (float) – Radius of inscribed sphere without scale applied.
set_rq(r, q)[source]

Set \(r\), \(q\) values.

Parameters:
class freud.kspace.FTconvexPolyhedron[source]

Fourier Transform for convex polyhedra.

Parameters:hull ((\(N_{verts}\), 3) numpy.ndarray) – Convex hull object.
Spoly2D(i, k)[source]

Calculate Fourier transform of polygon.

Parameters:
  • i (float) – Face index into self.hull simplex list.
  • k (numpy.ndarray) – Angular wave vector at which to calculate \(S\left(i\right)\).
Spoly3D(k)[source]

Calculate Fourier transform of polyhedron.

Parameters:k (int) – Angular wave vector at which to calculate \(S\left(i\right)\).
compute_py(*args, **kwargs)[source]

Compute FT.

Calculate \(P = F * S\):

  • \(S = \sum_{\alpha} e^{-i \mathbf{K} \cdot \mathbf{r}_{\alpha}}\).
  • F is the analytical form factor for a polyhedron, computed with Spoly3D().
get_radius()[source]

Get radius parameter.

If appropriate, return value should be scaled by get_parambyname(‘scale’) for interpretation.

Returns:Unscaled radius.
Return type:float
set_radius(radius)[source]

Set radius of in-sphere.

Parameters:radius (float) – Radius of inscribed sphere without scale applied.

Diffraction Patterns

class freud.kspace.DeltaSpot[source]

Base class for drawing diffraction spots on a 2D grid.

Based on the dimensions of a grid, determines which grid points need to be modified to represent a diffraction spot and generates the values in that subgrid. Spot is a single pixel at the closest grid point.

Parameters:
  • shape – Number of grid points in each dimension.
  • extent – Range of x,y values associated with grid points.
get_gridPoints()[source]

Get indices of sub-grid.

Based on the type of spot and its center, return the grid mask of points containing the spot.

makeSpot(cval)[source]

Generate intensity value(s) at sub-grid points.

Parameters:cval (numpy.complex64) – Complex valued amplitude used to generate spot intensity.
set_xy(x, y)[source]

Set \(x\), \(y\) values of spot center.

Parameters:
  • x (float) – x value of spot center.
  • y (float) – y value of spot center.
class freud.kspace.GaussianSpot[source]

Draw diffraction spot as a Gaussian blur.

Grid points filled according to Gaussian at spot center.

Parameters:
  • shape – Number of grid points in each dimension.
  • extent – Range of x, y values associated with grid points.
makeSpot(cval)[source]

Generate intensity value(s) at sub-grid points.

Parameters:cval (numpy.complex64) – Complex valued amplitude used to generate spot intensity.
set_sigma(sigma)[source]

Define Gaussian.

Parameters:sigma (float) – Width of the Gaussian spot.
set_xy(x, y)[source]

Set \(x\), \(y\) values of spot center.

Parameters:
  • x (float) – x value of spot center.
  • y (float) – y value of spot center.

Utilities

class freud.kspace.Constraint[source]

Constraint base class.

Base class for constraints on vectors to define the API. All constraints should have a ‘radius’ defining a bounding sphere and a ‘satisfies’ method to determine whether an input vector satisfies the constraint.

satisfies(v)[source]

Constraint test.

Parameters:v – Vector to test against constraint.
class freud.kspace.AlignedBoxConstraint[source]

Axis-aligned Box constraint.

Tetragonal box aligned with the coordinate system. Consider using a small z dimension to serve as a plane plus or minus some epsilon. Set R < L for a cylinder.

satisfies(v)[source]

Constraint test.

Parameters:v – Vector to test against constraint.
freud.kspace.constrainedLatticePoints()[source]

Generate a list of points satisfying a constraint.

Parameters:
  • v1 (numpy.ndarray) – Lattice vector 1 along which to test points.
  • v2 (numpy.ndarray) – Lattice vector 2 along which to test points.
  • v3 (numpy.ndarray) – Lattice vector 3 along which to test points.
  • constraint (Constraint) – Constraint object to test lattice points against.
freud.kspace.reciprocalLattice3D()[source]

Calculate reciprocal lattice vectors.

3D reciprocal lattice vectors with magnitude equal to angular wave number.

Parameters:
Returns:

Reciprocal space vectors.

Return type:

list

Note

For unit test, dot(g[i], a[j]) = 2 * pi * diracDelta(i, j):
list of reciprocal lattice vectors
freud.kspace.meshgrid2(*arrs)[source]

Computes an n-dimensional meshgrid.

source: http://stackoverflow.com/questions/1827489/numpy-meshgrid-in-3d

Parameters:arrs (list) – Arrays to meshgrid.
Returns:Tuple of arrays.
Return type:tuple