pyextal.potentialMap¶
Potential and charge-density maps from refined structure factors.
Reads a JAX refinement run from the dataset’s HDF5 file, extends its refined
structure factors over the .dat’s beams by symmetry, and synthesises 2D
sections/projections and 3D maps of the refined crystal, of the reference
(.dat/IAM) crystal and of their difference. Maps can be written back under
the run (pyextal.hdf5io.save_map) or exported for VESTA.
Example:
from pyextal import hdf5io, potentialMap as pm
run = hdf5io.load_refinement_run("sub1/ri_3_ro5-7.h5") # latest run
ctx = pm.setup(run) # parses the run's .dat, groups beams
proj = pm.project_2d(ctx, [-1, 1, 0], [0, 0, 1], kind='potential')
hdf5io.save_map(run, "proj110_potential", proj)
vol = pm.synthesize_3d(ctx, sampling=(64, 64, 64), g_max=1.2)
s = pm.structure_from_bloch()
pm.write_chgcar("diff.CHGCAR", vol.diff, s.lattice, s.atoms)
Needs only the core dependencies (no JAX).
Submodules¶
Classes¶
One layer's refined structure factors, resolvable for any beam. |
|
The crystal a run was refined against, loaded into the Bloch engine. |
|
A refined map, its reference and their difference. |
|
A reference-map peak and the refined-map maximum near it. |
|
Atoms of the conventional cell. |
Functions¶
|
Parses the run's |
|
Cartesian direct-lattice vectors (rows a, b, c) in Angstrom. |
|
Refined, reference and difference map on the section spanned by two axes. |
|
Refined, reference and difference map over the unit cell. |
|
3D map from a structure-factor function, normalised by the cell volume. |
|
All non-zero hkl in a box, optionally cut to |
|
(ny, nx) of a 2D section: |
|
Factor from the structure factor U_g to the potential V_g in volts. |
|
Peaks of the reference map, each paired with the refined maximum nearby. |
|
|
|
Converts a potential MapResult into charge density by Poisson's equation. |
|
Values of a 3D periodic map along a straight line. |
|
A 3D map integrated over discs perpendicular to a line, along that line. |
Expands the asymmetric unit in the engine's |
|
|
Writes a 3D map as a VASP CHGCAR (values times the cell volume). |
|
Reads a CHGCAR written by write_chgcar (or VASP). |
|
Writes a 3D map as a Gaussian cube file (lengths in Bohr). |
|
Reads a cube file written by write_cube. |
|
Refined, reference and difference maps of a 2D result, with a line profile. |
|
Reference and refined 2D maps with paired peaks marked. |
|
|
|
Isosurface of a 3D map (marching cubes). |
Package Contents¶
- class pyextal.potentialMap.LayerSF¶
One layer’s refined structure factors, resolvable for any beam.
- Variables:
gvectors (np.ndarray) – (n, 3) refined reflections.
sf (np.ndarray) – (n, 4)
[xfre, xfim, afre, afim], physical units.sym (GroupSymmetry or None) – Space-group grouping; None for Friedel only.
layer (int) – Layer index in the run.
- gvectors: numpy.ndarray¶
- sf: numpy.ndarray¶
- sym: object = None¶
- layer: int = 0¶
- _lookup: dict = None¶
- __post_init__()¶
- _refined(g)¶
F(g) from the refined set, or None.
- resolve(beams, model_sf=None)¶
Electron structure factor (elastic part) for each beam.
- Parameters:
beams (array-like) – (n, 3) Miller indices.
model_sf (np.ndarray, optional) – (n, 4)
lookupSFformat (amplitude, phase in degrees, …) for the beams that are neither refined nor Friedel mates of refined ones. Defaults tolookupSF(beams).
- Returns:
(F, source)– complex (n,) and a str array (n,) of ‘refined’, ‘friedel’ or ‘model’.- Return type:
tuple
- class pyextal.potentialMap.MapContext¶
The crystal a run was refined against, loaded into the Bloch engine.
- Variables:
run (RefinedRun) – The refinement run.
dat (Path) – The
.datthat was parsed.beams (np.ndarray) – (n, 3) the
.dat’s beams, (0,0,0) excluded.sym (GroupSymmetry or None) – Grouping of those beams, or None for a Friedel-only run.
cell (np.ndarray) – a, b, c, alpha, beta, gamma.
lattice (np.ndarray) – (3, 3) Cartesian lattice vectors (rows).
metric (np.ndarray) – (3, 3) direct metric tensor, Angstrom^2.
volume (float) – Cell volume, Angstrom^3.
kv (float) – Accelerating voltage in volts, from the
.dat.
- run: object¶
- dat: pathlib.Path¶
- beams: numpy.ndarray¶
- sym: object¶
- cell: numpy.ndarray¶
- lattice: numpy.ndarray¶
- metric: numpy.ndarray¶
- volume: float¶
- kv: float¶
- layer(i=0)¶
The LayerSF of layer
i.
- pyextal.potentialMap.setup(run, dat=None, symmetry=None)¶
Parses the run’s
.datand groups its beams by symmetry.- Parameters:
run (RefinedRun) – From pyextal.hdf5io.load_refinement_run.
dat (path-like, optional) – The
.datto use. Defaults to the one embedded in the run (pyextal.hdf5io.dat_for).symmetry (str, optional) – ‘SpaceGroup’ or ‘Friedel’. Defaults to the run’s
symmetry_constraint.
- Returns:
MapContext
- pyextal.potentialMap.lattice_vectors(cell)¶
Cartesian direct-lattice vectors (rows a, b, c) in Angstrom.
a along x, b in the xy plane – the convention VESTA and VASP use.
- Parameters:
cell (sequence) – a, b, c (Angstrom), alpha, beta, gamma (degrees).
- Returns:
(3, 3), one lattice vector per row.
- Return type:
np.ndarray
- class pyextal.potentialMap.MapResult¶
A refined map, its reference and their difference.
- Variables:
refined (np.ndarray) – Map from the refined structure factors.
reference (np.ndarray) – Map from the reference (
.dat/IAM) ones.diff (np.ndarray) –
refined - reference.hkl (np.ndarray) – (n, 3) the beams summed.
sf_reference (sf_refined,) – (n,) complex coefficient of each beam, in the map’s units before the volume/voltage factor.
source (np.ndarray) – (n,) where each refined coefficient came from: ‘refined’, ‘friedel’ or ‘model’ (see LayerSF.resolve).
kind (str) – ‘potential’ or ‘charge’.
layer (int) – The run layer the refined map is from.
axes (np.ndarray) – (ndim, 3) one grid step along each array axis, in fractional direct-lattice coordinates.
lattice (np.ndarray) – (3, 3) Cartesian lattice vectors (rows).
settings (dict) – The arguments the map was made with.
- refined: numpy.ndarray¶
- reference: numpy.ndarray¶
- diff: numpy.ndarray¶
- hkl: numpy.ndarray¶
- sf_refined: numpy.ndarray¶
- sf_reference: numpy.ndarray¶
- source: numpy.ndarray¶
- kind: str¶
- layer: int¶
- axes: numpy.ndarray¶
- lattice: numpy.ndarray¶
- settings: dict¶
- property ndim¶
- property steps¶
(ndim, 3) Cartesian grid step along each array axis, Angstrom.
- cartesian_grid()¶
Cartesian position of every grid point, shape
map.shape + (3,).
- pyextal.potentialMap.project_2d(ctx, x_vector, y_vector, layer=0, beams=None, sampling=256, kind='potential', kv=None, model_sf=None, to_xray=None, reference_iam=False)¶
Refined, reference and difference map on the section spanned by two axes.
The grid point at row i, column j sits at
i/ny * y_vector + j/nx * x_vector. Onlyg . x_vectorandg . y_vectorenter the phase, so with the zero-order Laue zone beams this is the projected map down the zone axis.- Parameters:
ctx (MapContext) – From setup.
x_vector (array-like) – Section axes in direct-lattice indices, e.g.
[-1, 1, 0]and[0, 0, 1]for the [110] zone.y_vector (array-like) – Section axes in direct-lattice indices, e.g.
[-1, 1, 0]and[0, 0, 1]for the [110] zone.layer (int or LayerSF) – Run layer to map.
beams (array-like, optional) – (n, 3) beams to sum. Defaults to the
.dat’s beams.sampling (int or tuple) – Columns (rows follow the true axis lengths), or an explicit (ny, nx).
kind (str) – ‘potential’ (volts) or ‘charge’ (e / Angstrom^3).
kv (float, optional) – Voltage in volts for the potential; defaults to the
.dat’s.model_sf (np.ndarray or callable, optional) –
lookupSF-format values for beams the run did not refine, or a function of the beams.to_xray (callable, optional) – Electron-to-X-ray conversion with the signature of pyextal.callBloch.e2Xray (the default).
reference_iam (bool) – Reference from the IAM even where the
.datsets a structure factor.
- Returns:
MapResult
- pyextal.potentialMap.synthesize_3d(ctx, layer=0, sampling=(64, 64, 64), hkl_range=None, g_max=None, beams=None, kind='potential', kv=None, model_sf=None, to_xray=None, reference_iam=False)¶
Refined, reference and difference map over the unit cell.
The grid is
samplingpoints along a, b and c; point (i, j, k) sits at fractional coordinates(i/N0, j/N1, k/N2).- Parameters:
ctx (MapContext) – From setup.
layer (int or LayerSF) – Run layer to map.
sampling (tuple) – (N0, N1, N2).
hkl_range (tuple, optional) – (p0, p1, p2) box of beams to sum.
g_max (float, optional) – Keep beams with
|g| <= g_max(1/Angstrom, through the reciprocal metric, so right for any cell).beams (array-like, optional) – Explicit (n, 3) beams instead of the two above.
kind – As in project_2d.
kv – As in project_2d.
model_sf – As in project_2d.
to_xray – As in project_2d.
reference_iam – As in project_2d.
- Returns:
MapResult
- pyextal.potentialMap.fourier_synthesis(getSF, order, sampling)¶
3D map from a structure-factor function, normalised by the cell volume.
The engine behind the old pyextal.densityMap.fourierSynthesis: sums
getSF(hkl)[0] + 1j*getSF(hkl)[1]over|h|,|k|,|l| <= order(excluding 000) on a ``sampling``^3 fractional grid.- Parameters:
getSF (callable) – (h, k, l) -> sequence whose first two entries are the real and imaginary structure factor.
order (int) – Largest index.
sampling (int) – Grid points per axis.
- Returns:
Real (sampling, sampling, sampling) map.
- Return type:
np.ndarray
- pyextal.potentialMap.hkl_sphere(gstar, g_max=None, hkl_range=None)¶
All non-zero hkl in a box, optionally cut to
|g| <= g_max.- Parameters:
gstar (np.ndarray) – (3, 3) reciprocal metric tensor, Angstrom^-2.
g_max (float, optional) – Largest |g| in 1/Angstrom.
hkl_range (tuple, optional) – (p0, p1, p2), the box
|h| <= p0etc. Defaults to the smallest box that holds theg_maxsphere.
- Returns:
(n, 3) int.
- Return type:
np.ndarray
- pyextal.potentialMap.section_shape(metric, x_vector, y_vector, sampling)¶
(ny, nx) of a 2D section:
samplingcolumns, rows by true length.- Parameters:
metric (np.ndarray) – (3, 3) direct metric tensor.
x_vector (array-like) – Section axes, direct-lattice indices.
y_vector (array-like) – Section axes, direct-lattice indices.
sampling (int or tuple) – Columns, or an explicit (ny, nx).
- pyextal.potentialMap.U2V(kv)¶
Factor from the structure factor U_g to the potential V_g in volts.
- Parameters:
kv (float) – Accelerating voltage in volts.
- class pyextal.potentialMap.PeakShift¶
A reference-map peak and the refined-map maximum near it.
- Variables:
ref (tuple) – Grid index of the reference peak.
refined (tuple) – Grid index of the refined maximum.
refined_val (ref_val,) – Map values there.
shift (tuple) – Signed index offset, refined minus reference, wrapped to the nearest image.
distance_A (float) – Length of
shiftin Angstrom, through the map’s grid steps (right for non-orthogonal axes).
- ref: tuple¶
- refined: tuple¶
- ref_val: float¶
- refined_val: float¶
- shift: tuple¶
- distance_A: float¶
- property distance_px¶
Length of
shiftin grid steps.
- pyextal.potentialMap.find_atom_peaks(result, threshold_rel=0.5, min_distance=5, search_radius=5)¶
Peaks of the reference map, each paired with the refined maximum nearby.
Works on 2D and 3D maps; the grid is periodic.
- Parameters:
result (MapResult) – The maps.
threshold_rel (float) – Keep reference maxima above this fraction of the reference map’s maximum.
min_distance (int) – Size of the local-maximum filter, in grid steps.
search_radius (int) – Half-width of the window searched in the refined map, in grid steps.
- Returns:
list of PeakShift
- pyextal.potentialMap.charge_density(volume, steps=None, spacings=None)¶
-laplacian(volume): the charge density, up to a constant, of a potential.- Parameters:
volume (np.ndarray) – Periodic potential map.
steps (np.ndarray, optional) – (ndim, 3) Cartesian grid steps (MapResult.steps). Uses an exact spectral Laplacian, right for oblique axes; result in map units per Angstrom^2.
spacings (tuple, optional) – Per-axis spacing for the original 3-point finite-difference Laplacian, which assumes orthogonal axes. Used when
stepsis None (all 1.0 if both are None).
- Returns:
Same shape as
volume.- Return type:
np.ndarray
- pyextal.potentialMap.charge_from_potential(result, method='spectral', spacings=None)¶
Converts a potential MapResult into charge density by Poisson’s equation.
- Parameters:
result (MapResult) – Potential maps.
method (str) – ‘spectral’ (exact, uses the grid geometry) or ‘finite_difference’ (the original 3-point stencil with
spacings, default 1.0 per axis).spacings (tuple, optional) – For ‘finite_difference’.
- Returns:
kind='charge', the maps replaced by-laplacian.- Return type:
- pyextal.potentialMap.line_profile(volume, start, end, num_points=100)¶
Values of a 3D periodic map along a straight line.
- Parameters:
volume (np.ndarray) – 3D map on a fractional grid.
start (array-like) – End points in fractional coordinates (any image; wrapped into the cell).
end (array-like) – End points in fractional coordinates (any image; wrapped into the cell).
num_points (int) – Samples along the line.
- Returns:
(distance, values), the distance in fractional units.- Return type:
tuple
- pyextal.potentialMap.integrated_profile(volume, start, end, radius, num_points=100, grid_points=21, lattice=None)¶
A 3D map integrated over discs perpendicular to a line, along that line.
- Parameters:
volume (np.ndarray) – 3D map on a fractional grid.
start (array-like) – End points in fractional coordinates.
end (array-like) – End points in fractional coordinates.
radius (float) – Disc radius: fractional units, or Angstrom when
latticeis given.num_points (int) – Steps along the line.
grid_points (int) – Samples across each disc diameter.
lattice (np.ndarray, optional) – (3, 3) Cartesian lattice vectors. With it the discs are perpendicular, and round, in real space – needed for a non-cubic cell. Without it they are built in fractional coordinates (the original behaviour).
- Returns:
(distance, profile, points).distancealong the line (as the radius),pointsthe fractional sampling positions before wrapping, shape(num_points, grid_points, grid_points, 3).- Return type:
tuple
- class pyextal.potentialMap.Structure¶
Atoms of the conventional cell.
- Variables:
lattice (np.ndarray) – (3, 3) Cartesian lattice vectors (rows), Angstrom.
symbols (list of str) – Element of each atom.
frac (np.ndarray) – (n, 3) fractional coordinates in [0, 1).
numbers (np.ndarray) – (n,) atomic numbers.
- lattice: numpy.ndarray¶
- symbols: list¶
- frac: numpy.ndarray¶
- numbers: numpy.ndarray¶
- property atoms¶
[(symbol, [x, y, z]), ...], the form write_chgcar takes.
- pyextal.potentialMap.structure_from_bloch()¶
Expands the asymmetric unit in the engine’s
crystblock to the full cell.Needs a
.datparsed first (pyextal.callBloch.bloch_parse, or pyextal.potentialMap.setup).- Returns:
Structure
- pyextal.potentialMap.write_chgcar(path, volume, lattice, atoms=None, origin_shift=None)¶
Writes a 3D map as a VASP CHGCAR (values times the cell volume).
- Parameters:
path (path-like) – Output file.
volume (np.ndarray) – (Nx, Ny, Nz) map on the fractional grid.
lattice (np.ndarray) – (3, 3) Cartesian lattice vectors (rows), Angstrom.
atoms (list, optional) –
[(symbol, [x, y, z]), ...]fractional, e.g. Structure.atoms. Without atoms a dummy H at the origin is written so viewers still draw the cell.origin_shift (array-like, optional) – Fractional shift applied to the atoms and, rounded to whole grid steps, to the map.
- pyextal.potentialMap.read_chgcar(path)¶
Reads a CHGCAR written by write_chgcar (or VASP).
- Returns:
(volume, lattice, atoms), the map divided back by the cell volume.- Return type:
tuple
- pyextal.potentialMap.write_cube(path, volume, structure=None, comment='Generated by pyextal')¶
Writes a 3D map as a Gaussian cube file (lengths in Bohr).
- Parameters:
path (path-like) – Output file.
volume (np.ndarray) – (Nx, Ny, Nz) map on the fractional grid.
structure (Structure, optional) – Cell and atoms; defaults to structure_from_bloch().
comment (str) – Second header line.
- pyextal.potentialMap.read_cube(path)¶
Reads a cube file written by write_cube.
- Returns:
(volume, lattice, numbers, cart), lattice and atom positions in Angstrom.- Return type:
tuple
- pyextal.potentialMap.plot_map_comparison(result, line_center=None, label=None, save_dir=None)¶
Refined, reference and difference maps of a 2D result, with a line profile.
- Parameters:
result (MapResult) – A 2D map.
line_center (int, optional) – Column of the vertical line profile; defaults to the middle.
label (str, optional) – Title suffix and file-name stem; defaults to
layer<N>.save_dir (path-like, optional) – Writes
map_comparison_<label>.png.
- Returns:
(fig, axes)- Return type:
tuple
- pyextal.potentialMap.plot_peak_shifts(result, peaks, label=None, save_dir=None)¶
Reference and refined 2D maps with paired peaks marked.
- pyextal.potentialMap.plot_slices(volume, axis=0, n=4, clim=None, cmap='inferno')¶
nevenly spaced 2D slices of a 3D map.- Parameters:
volume (np.ndarray) – 3D map.
axis (int) – Axis the slices are taken along.
n (int) – Number of slices.
clim (tuple, optional) – (vmin, vmax).
cmap (str) – Colormap.
- Returns:
(fig, axes)- Return type:
tuple
- pyextal.potentialMap.plot_isosurface(volume, level, steps=None)¶
Isosurface of a 3D map (marching cubes).
- Parameters:
volume (np.ndarray) – 3D map.
level (float) – Isosurface value.
steps (np.ndarray, optional) – (3, 3) Cartesian grid steps (MapResult.steps) to draw the cell at its true shape; grid indices otherwise.
- Returns:
(fig, ax)- Return type:
tuple