# Potential Mapping `pyextal.potentialMap` turns the structure factors of a refinement run into real-space maps: the electrostatic potential and the charge density of the refined crystal, of the reference crystal (the `.dat`'s structure factors, normally the IAM), and their difference. Maps are 2D sections (down a zone axis, the projected map) or 3D volumes over the unit cell. You can write them back into the dataset's HDF5 file under the run, look for peak shifts, take line profiles, and export them to VESTA. It replaces the standalone `potential_mapping` package, which read the old per-run `.npy` folders, and the old `pyextal.densityMap` module, whose functions are now deprecated aliases. ## Workflow ```python from pyextal import hdf5io, potentialMap as pm run = hdf5io.load_refinement_run("sample.h5") # latest run; or run=2 / "run_002" ctx = pm.setup(run) # parse the run's .dat, group beams # [110] projection: columns along [-1 1 0], rows along [0 0 1] proj = pm.project_2d(ctx, [-1, 1, 0], [0, 0, 1], layer=0, sampling=256, kind='potential') rho2 = pm.project_2d(ctx, [-1, 1, 0], [0, 0, 1], kind='charge') hdf5io.save_map(run, "proj110_potential", proj) # -> refinement/run_NNN/maps/ pm.plot_map_comparison(proj, save_dir="figures") peaks = pm.find_atom_peaks(proj, threshold_rel=0.5) for p in peaks: print(p.ref, "->", p.refined, f"{p.distance_A:.3f} A") vol = pm.synthesize_3d(ctx, sampling=(64, 64, 64), g_max=1.5) # |g| <= 1.5 / A chg = pm.charge_from_potential(vol) # Poisson's equation s = pm.structure_from_bloch() pm.write_chgcar("diff.CHGCAR", vol.diff, s.lattice, s.atoms) ``` `examples/potential_mapping/map_from_h5.py` runs all of this from the command line. It writes maps into the `.h5`, so work on a copy of a file you want to keep as it is. `setup` needs the `.dat` the run was refined against. Runs saved by current pyextal embed it (`save_refinement(..., dat=...)`). For older files, pass `setup(run, dat=...)`, or embed it once with `hdf5io.store_dat(h5py.File(h5, 'a')['refinement/run_001'], path)`. ## Where each structure factor comes from A run refines a few reflections; a map sums many beams. `LayerSF.resolve` gives each beam a value and records its `source`: | source | rule | |---|---| | `refined` | the beam is symmetry-equivalent to a refined reflection: the refined value times the translation phase between the two | | `friedel` | its Friedel mate is `refined`: F(g) = F(−g)* | | `model` | anything else: the `.dat`'s value (`lookupSF`), or `model_sf=` | With `symmetry_constraint='SpaceGroup'` the grouping is `pyextal.symmetry.GroupSymmetry` over the `.dat`'s beams. A glide plane or screw axis changes the phase of equivalent reflections (Waser, *Acta Cryst.* 1955), e.g. graphite's (0 1 1) and (0 −1 1) have opposite sign. A `'Friedel'` run applies each refined value only to its own hkl and −hkl. `MapResult.source`, `hkl`, `sf_refined` and `sf_reference` record the result for every beam. ## Grids, units and the cell metric - **3D**: point (i, j, k) is at fractional coordinates (i/N₀, j/N₁, k/N₂). `g_max` (1/Å) selects beams through the reciprocal metric tensor, so the cutoff is a sphere for any cell. The old `gLimit` was a cut on h²+k²+l², which is only right for a cubic cell. `hkl_range=(p0, p1, p2)` restricts the index box. - **2D**: row i, column j is at `i/ny·y_vector + j/nx·x_vector` (direct-lattice indices). There are `sampling` columns, and the rows follow the true length ratio |y|/|x| from the metric. Only g·x and g·y enter the phase, so with zero-order beams this is the projection down the zone axis. Sections with oblique axes are drawn at their real angle. - **Units** are those of the original code. Potential maps are U_g × `U2V(kv)`, in volts (kv defaults to the `.dat`'s voltage). Charge maps use the X-ray structure factor (`e2Xray`) over the cell volume, in e/ų. `charge_from_potential` applies −∇² spectrally using the real grid geometry, so the result is in map units per Ų. `method='finite_difference'` keeps the old 3-point stencil. - `MapResult.axes` is the fractional grid step along each array axis, and `MapResult.steps` the Cartesian one. Peak distances (`distance_A`) and the integration discs of `integrated_profile(..., lattice=...)` use them. ## Speed Both grids are uniform in fractional coordinates, so a map is a discrete Fourier transform evaluated on its grid points. The coefficients are scattered to their integer frequencies and transformed with one FFT, which is exact for any grid size. On Si [110] with 283 beams, a 256-column section went from 0.9 s to 4 ms, and a 64³ volume with 484 beams from 11 s to 13 ms. A 2D section whose axes are not integer lattice vectors falls back to a direct sum, which is separable along the two axes. ## Stored maps ``` refinement/run_NNN/maps// attrs: kind, dim, layer, timestamp, settings_json refined, reference, diff map arrays (complex) hkl, sf_refined, sf_reference beams and coefficients source 'refined' | 'friedel' | 'model' axes, lattice grid geometry ``` `hdf5io.load_map(run, name)` returns the `MapResult`, and `hdf5io.list_maps(run)` lists the stored maps. `save_map` refuses to replace a map unless you pass `overwrite=True`. ## Moving from `potential_mapping` | old | new | |---|---| | `load_optimization_data(dir)` + `parse_bloch_param(dat)` | `hdf5io.load_refinement_run(h5)` + `pm.setup(run)` | | `get_sf_dict(...)`, `sf_dicts[i]` | `ctx.layer(i)` (or `layer=i`) | | `project_potential_map(beams, sf_dict, sym, x, y)` | `pm.project_2d(ctx, x, y, kind='potential')` | | `project_charge_density_map(...)` | `pm.project_2d(..., kind='charge')` | | `compute_3d_potential_map(p0, p1, p2, ..., gLimit=)` | `pm.synthesize_3d(ctx, hkl_range=(p0, p1, p2), g_max=)` | | `ProjectionResult.density_map / ref_map / diff_map` | `MapResult.refined / reference / diff` | | `ProjectionResult.save(npz)` | `hdf5io.save_map(run, name, result)` | | `find_atom_peaks`, `find_atom_peaks_3d` | `pm.find_atom_peaks` (any dimension) | | `calculate_charge_from_potential_result` | `pm.charge_from_potential` | | `extract_line_profile`, `extract_integrated_profile` | `pm.line_profile`, `pm.integrated_profile` | | `get_structure_from_pyextal()` | `pm.structure_from_bloch()` → `Structure` | | `export_vesta_chgcar`, `load_vesta_chgcar` | `pm.write_chgcar`, `pm.read_chgcar` | | `plot_density_comparison` | `pm.plot_map_comparison` | The new code fixes these bugs in the old package: - The Friedel-only branch indexed a list of pairs (`val[1]`). - The charge maps ignored a custom X-ray conversion for the reference map. - The 3D charge map dropped `gLimit`. - The 2D aspect ratio and the `gLimit` cut assumed a cubic cell. - `densityMap.write_cube` wrote fractional atom positions and Angstrom lengths into a format that expects Cartesian Bohr.