Notebook 3 Functions

See below for the functions needed to build the visualizations and DFT calculator in Notebook 3.

NB3_functions.edge_cleaner(func_3d, nx, ny, nz)

Sets outermost layer (or two) of grid values of a 3D function to zero.

NB3_functions.integ_3d(func_3d, dx, dy, dz)

Integrates a 3D function over all defined space.

NB3_functions.norm_psi_and_den(e_vecs, ...)

Normalizes raw eigenvectors from the solver and finds electron density.

NB3_functions.noninter_kin_e(norm_eigenvecs, ...)

Finds noninteracting KS kinetic energy.

NB3_functions.grid_density(l_x, l_y, l_z[, ...])

Generates 3D scatter plot representing grid for DFT calculations.

NB3_functions.exch_equation(den_number)

Displays LaTeX equation of LDA exchange potential.

NB3_functions.exch_pot_eq(den_number)

Displays line plot of LDA exchange with scatterpoint at density value.

NB3_functions.LDA_c_display(ws_radius)

Displays line plot of Chachiyo LDA correlation potential with a scatterpoint and LaTeX equation at the Wigner-Seitz radius (r_s) value.

NB3_functions.GGAx_pot_eq(den_num, grad_den_num)

Generates line plot of PBE exchange enhancement factor with a LaTeX equation and scatterpoint at the specified density + gradient value.

NB3_functions.hartree_plotter(freq, ...)

Display diagram solving for potential from model density in 1D.

NB3_functions.hamiltonian_display(...)

Generates LaTeX equation of effective single-particle Hamiltonian.

NB3_functions.energy_plot(den_log, ener_log, ...)

Generates iteration number vs total energy plot.

NB3_functions.hard_walls(potential, nx, ny, nz)

Sets potential of outermost grid points to 1,000.

NB3_functions.hartree(den, kin_oper, dx, dy, ...)

Uses Poisson's equation to find Hartree potential from density.

NB3_functions.lda_exchange(den, dx, dy, dz)

Finds LDA exchange potential and energy from density.

NB3_functions.lda_correlation(den, dx, dy, dz)

Finds LDA correlation potential and energy from density.

NB3_functions.RDG(den, der_1st, nx, ny, nz)

Finds dimensionless reduced density gradient (needed for PBE exchange).

NB3_functions.pbe_exchange(den, D1st, dx, ...)

Finds PBE exchange potential and energy from density + gradient.

NB3_functions.cor_den_grad(den, der_1st, nx, ...)

Finds dimensionless correlation density gradient (used in PBE correlation).

NB3_functions.pbe_correlation(den, der_1st, ...)

Finds PBE correlation potential and energy from density + gradient.

NB3_functions.edge_cleaner(func_3d, nx, ny, nz, num_edges=1)

Sets outermost layer (or two) of grid values of a 3D function to zero.

This is needed for the density because of the discontinuities in the numeric 2nd derivatives at the box edges.

Parameters:
  • func_3d (np.array) – Flattened 3D grid of points.

  • nx (int) – Number of grid points in x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

  • num_edges (int) – Number of border layers to set to zero (either 1 or 2).

Returns:

A flattened array of grid points.

NB3_functions.integ_3d(func_3d, dx, dy, dz)

Integrates a 3D function over all defined space.

Parameters:
  • func_3d (np.array) – 3D grid of points

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

Returns:

Integrated 3D function as scalar value.

NB3_functions.norm_psi_and_den(e_vecs, occ_states, dx, dy, dz)

Normalizes raw eigenvectors from the solver and finds electron density.

Parameters:
  • e_vecs (np.array) – Array of eigenvectors from the eigenvalue solver.

  • occ_states (int) – Number of occupied KS states (i.e. # electrons / 2).

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

Returns:

  • norm_psi (np.array): Array of normalized eigenvectors.

  • el_den (np.array): Electron density as array.

NB3_functions.noninter_kin_e(norm_eigenvecs, occ_states, kin_mat, dx, dy, dz, nx, ny, nz)

Finds noninteracting KS kinetic energy.

Parameters:
  • norm_eigenvecs (np.array) – array of normalized eigenvectors as columns/rows

  • occ_states (int) – number of occupied KS states (i.e. # electrons / 2)

  • kin_mat (scipy.sparse.sparray) – kinetic energy operator as sparse matrix

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

  • nx (int) – Number of grid points in each x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

Returns:

Scalar energy value in Ha.

NB3_functions.grid_density(l_x, l_y, l_z, plotting_lib='plotly')

Generates 3D scatter plot representing grid for DFT calculations.

Parameters:
  • l_x (int) – Box length in x-direction.

  • l_y (int) – Idem for y.

  • l_z (int) – Idem for z.

Example:

>>> # View numerical grid for a 16x8x3 Box
>>> grid_density(16, 8, 3, plotting_lib='plotly')
_images/grid_density.png
NB3_functions.exch_equation(den_number)

Displays LaTeX equation of LDA exchange potential.

Parameters:

den_number (float) – Value of electron density, n(r), at a specific position..

Example:

>>> # equation for electronic exchange at a point with a density of 0.50 e/Bohr**3
>>> exch_equation(0.50)
\[v_{\text{X, n(r)=0.50}}^{\text{LDA}}= - \frac{3}{4} \left(\frac{3}{\pi}\right)^{1/3} (0.50)^{1/3} = -0.586\ \text{Ha/e}\]
NB3_functions.exch_pot_eq(den_number)

Displays line plot of LDA exchange with scatterpoint at density value.

Parameters:

den_number (float) – Value of electron density, n(r).

Example:

>>> # plot for lda electronic exchange with a point at a density of 0.50 e/Bohr**3
>>> exch_pot_eq(0.50)
_images/LDA_exch.png
NB3_functions.LDA_c_display(ws_radius)

Displays line plot of Chachiyo LDA correlation potential with a scatterpoint and LaTeX equation at the Wigner-Seitz radius (r_s) value.

Parameters:

ws_radius (float) – Wigner-Seitz radius (inversely related to density).

Example:

>>> # display equation and diagram for LDA correlation with point @ r_s = 2.00
>>> LDA_c_display(2.00)
\[v_{\text{C}, r_s=2.00}^{\text{LDA}}=a\ln\left(1+\frac{b}{(2.00)}+\frac{b}{(2.00)^2}\right) = -0.059\ \text{Ha/e}\]
_images/LDA_cor.png
NB3_functions.GGAx_pot_eq(den_num, grad_den_num)

Generates line plot of PBE exchange enhancement factor with a LaTeX equation and scatterpoint at the specified density + gradient value.

Parameters:
  • den_num (float) – value of the electron density

  • grad_den_num (float) – gradient of electron density at same point

Example:

>>> # Display GGA exchange equation and diagram for n(r) = 0.15 and grad n(r) = 1.00
>>> GGAx_pot_eq(0.15, 1.00)
\[s=\frac{|1.00|}{2 \cdot 3^{1/3} \pi^{2/3} (0.150)^{4/3}}=2.028 \ \ \ F_{x,s=2.028} = 1 + \kappa - \frac{\kappa}{\left(1 + \frac{\mu (2.028)^2}{\kappa} \right)} = 1.425\]
_images/GGA_exch_plot.png
NB3_functions.hartree_plotter(freq, solve_poisson)

Display diagram solving for potential from model density in 1D.

Parameters:
  • freq (int) – coefficient for frequency in trig function

  • solve_poisson (bool) – display (True) potential from trig functionc

Example:

>>> # solve poisson equation for trig function with a frequency of 3
>>> hartree_plotter(3, True)
\[ \begin{align}\begin{aligned}n(x) = \sin^2(2\pi(3)x)\\\nabla^2 V(x) = -4\pi n(x) \xrightarrow[\text{linalg.cg}]{\text{scipy}} V(x)\end{aligned}\end{align} \]
_images/1D_hartree.png
NB3_functions.hamiltonian_display(functional, har, ex, cor)

Generates LaTeX equation of effective single-particle Hamiltonian.

Parameters:
  • functional (str) – XC potential (LDA or PBE)

  • har (bool) – Hartree potential on or off

  • ex (bool) – exchange term on or off

  • cor (bool) – correlation term on or off

Example:

>>> # display effective hamiltonian with exch and hartree and without correlation
>>> hamiltonian_display('LDA', True, True, False)
\[\hat{h}_i = \hat{T}_{\text{kin},i} + v_{\text{Ha}}(n(r)) + v_{\text{X}}^{\text{LDA}}(n(r)) + 0\]
NB3_functions.energy_plot(den_log, ener_log, converge_state, show_fig=True, save_fig=False, filename=None)

Generates iteration number vs total energy plot.

Parameters:
  • den_log (list) – list of numpy arrays with electron density

  • ener_log (list) – list of total energy values in Ha

  • converge_state (bool) – SCF loop ended in a converged (True) or unconverged (False) state

  • show_fig (bool) – display figure to output (True) or not (False)

  • save_fig (bool) – save a .png file of diagram

  • filename (str) – filename to save to (w/o extension)

NB3_functions.hard_walls(potential, nx, ny, nz)

Sets potential of outermost grid points to 1,000. Used for testing.

Parameters:
  • potential (np.array) – KS (or other) potential as a flattened array

  • nx (int) – Grid points in x-direction.

  • ny (int) – Grid points in y-direction.

  • nz (int) – Grid points in z-direction.

Returns:

Flattened array of grid points.

NB3_functions.hartree(den, kin_oper, dx, dy, dz, nx, ny, nz)

Uses Poisson’s equation to find Hartree potential from density.

Parameters:
  • den (np.array) – Electron density.

  • kin_oper (np.array) – Kinetic energy operator as sparse matrix.

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

  • nx (int) – Number of grid points in x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

Returns:

  • v_ha_flat (np.array): Hartree potential as flattened array.

  • v_ha_ener (float): Hartree energy (in Ha units); needed to compute total energy

NB3_functions.lda_exchange(den, dx, dy, dz)

Finds LDA exchange potential and energy from density.

Parameters:
  • den (np.array) – Electron density.

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

Returns:

  • exch_pot_lda (np.array): LDA exchange potential.

  • exch_ener_lda (float): LDA exchnage energy (in Ha).

NB3_functions.lda_correlation(den, dx, dy, dz)

Finds LDA correlation potential and energy from density.

Parameters:
  • den (np.array) – Electron density.

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

Returns:

  • corr_pot (np.array): LDA correlation potential.

  • corr_en (float): LDA correlation energy (in Ha).

NB3_functions.RDG(den, der_1st, nx, ny, nz)

Finds dimensionless reduced density gradient (needed for PBE exchange).

Parameters:
  • den (np.array) – Electron density.

  • der_1st (np.array) – 1st derivative operator as sparse matrix.

  • nx (int) – Number of grid points in x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

Returns:

  • RDG (np.array): Reduced density gradient as matrix.

NB3_functions.pbe_exchange(den, D1st, dx, dy, dz, nx, ny, nz)

Finds PBE exchange potential and energy from density + gradient.

Parameters:
  • den (np.array) – Electron density.

  • D1st (np.array) – 1st derivative operator as sparse matrix.

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

  • nx (int) – Number of grid points in x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

Returns:

  • exch_pot_pbe (np.array): PBE exchange potential.

  • exch_ener_pbe (float): PBE exchange energy (in Ha).

NB3_functions.cor_den_grad(den, der_1st, nx, ny, nz)

Finds dimensionless correlation density gradient (used in PBE correlation).

Parameters:
  • den (np.array) – Electron density.

  • der_1st (np.array) – 1st derivative operator as sparse matrix.

  • nx (int) – Number of grid points in x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

Returns:

  • t (np.array): Correlation density gradient.

NB3_functions.pbe_correlation(den, der_1st, dx, dy, dz, nx, ny, nz)

Finds PBE correlation potential and energy from density + gradient.

Parameters:
  • den (np.array) – Electron density.

  • der_1st (np.array) – 1st derivative operator as sparse matrix.

  • dx (float) – Differential volume element in x-direction.

  • dy (float) – Idem for y.

  • dz (float) – Idem for z.

  • nx (int) – Number of grid points in x-direction.

  • ny (int) – Idem for y.

  • nz (int) – Idem for z.

Returns:

  • cor_pot_pbe (np.array): PBE correlation potential.

  • cor_ener_pbe (float): PBE correlation energy (in Ha).