castep_elnes_parser module#

A simple python script for parsing CASTEP (http://www.castep.org/) output files especially for ELNES calculations.

This includes some functions for reading .cell, .castep, .bands, and .eels_mat files, calculating excitation energy, and forming core-loss spectra with Gaussian smearing.

Copyright (c) 2022 kiyou, nmdl-mizo This software is released under the MIT License, see LICENSE.

Note

This script is tested on input and output files of CASTEP version 8 and may not be incompatible to other versions.

In all papers using the CASTEP code, you should cite: “First principles methods using CASTEP”, Zeitschrift fuer Kristallographie 220(5-6) pp. 567-570 (2005) S. J. Clark, M. D. Segall, C. J. Pickard, P. J. Hasnip, M. J. Probert, K. Refson, M. C. Payne

If you use get_energies() for calculating excitation energy, please consider to cite: Mizoguchi, T.; Tanaka, I.; Gao, S.-P.; Pickard, C. J. “First-Principles Calculation of Spectral Features, Chemical Shift and Absolute Threshold of ELNES and XANES Using a Plane Wave Pseudopotential Method.” J. Phys. Condens. Matter 2009, 21 (10), 104204.

ceparser.gaussian(x, c, w, s)[source]#

gaussian function for smearing

Parameters
  • x (list or numpy array) – 1d list of energies

  • c (float) – center position (=mu)

  • w (float) – height scaling factor, weights

  • s (float) – standard deviation of gaussian smearing (=sigma)

Returns

numpy array of gaussian distribution

Return type

numpy array

ceparser.get_ae_energy_castep(lines, element='C', suffix=':ex')[source]#

get the atomic energy of the all-electron (ae) calculation from .castep output

Parameters
  • lines (list of str) – Castep output for a calculation run. Can be obtained by split_castep

  • element (str, default "C") – element name. e.g. “C”

  • suffix (str, default ":ex") – suffix for the site name e.g. “:ex”

Returns

ae_energy – ae energy in eV

Return type

float

ceparser.get_coords_cell(filename)[source]#

extract species and coordinates from .cell file

Parameters

filename (str) – path to the .cell file

Returns

coords_list – a list of two elements - a list of species - a numpy array of coordinations

Return type

list

ceparser.get_energies(filename_gs, filename_ex, element='C', suffix=':ex', gs_split=- 1, ex_split=- 1)[source]#

get energies from .castep outputs

Parameters
  • filename_gs (str) – path to the .castep file of the ground state

  • filename_ex (str) – path to the .castep file of the excited state

  • element (str, default "C") – element name. e.g. “C”

  • suffix (str, default ":ex") – suffix for the site name e.g. “:ex”

  • gs_split (int, default -1) – index of the split used for extracting energies from ground state calculation. -1 to latest.

  • ex_split (int, default -1) – index of the split used for extracting energies from excited state calculation. -1 to latest.

Returns

energy_dict

a dictionary wiht str keys of labels and flot values of energies in eV

gs_final: ground state final_energy, ex_final: excited state final energy, gs_ae: ground state atomic energy in the all-electron calculation, ex_ae: excited state atomic energy in the all-electron calculation, gs_pa: ground state atomic energy in the pseudo-atomic calculation, ex_pa: excited state atomic energy in the pseudo-atomic calculation, excitation_energy: excitation energy,

Return type

dict

Note

The calculation of excitation energy is based on the following paper: Mizoguchi, T.; Tanaka, I.; Gao, S.-P.; Pickard, C. J. “First-Principles Calculation of Spectral Features, Chemical Shift and Absolute Threshold of ELNES and XANES Using a Plane Wave Pseudopotential Method.” J. Phys. Condens. Matter 2009, 21 (10), 104204.

ceparser.get_final_energy_castep(lines)[source]#

get the final energy of the system from .castep output

Parameters

lines (list of str) – Castep output for a calculation run. Can be obtained by split_castep

Returns

final_energy – final energy in eV

Return type

float

ceparser.get_pa_energy_castep(lines, element='C', suffix=':ex')[source]#

get the atomic energy of the pseudo atomic (pa) calculation from .castep output

Parameters
  • lines (list of str) – Castep output for a calculation run. Can be obtained by split_castep

  • element (str, default "C") – element name. e.g. “C”

  • suffix (str, default ":ex") – suffix for the site name e.g. “:ex”

Returns

pa_energy – pseudo atomic energy in eV

Return type

float

ceparser.get_smeared_spectrum(energies, sigma=0.3, calc_dir='.', seed_name='case_elnes', e_origin='eigen_value', output_eV=True, atomic_unit=False)[source]#

get gaussian smeared spectra from a .bands file and a .eels_mat file

Parameters
  • energies (list or numpy array) – 1d list of energies

  • sigma (float, default 0.3) – standard deviation of gaussian smearing

  • calc_dir (str, default ".") – path to the directory containing .bands and .eels_mat

  • seed_name (str, default "case_elnes") – seed name of the calculation

  • e_origin (str, default "eigen_value") – set energy origin

  • output_eV (bool, default True) – whether output energy in eV (True) or hartree (False)

  • atomic_unit (bool, default False) – whether output dynamical structure factor in the unit of Bohr radius^2 (True) or angstrom^2 (False).

Returns

spectrum – numpy array of smeared spectra

Return type

numpy array

Examples

>>> !ls .
case_elnes.bands    case_elnes.eels_mat
>>> energies = np.arange(-4.999, 30.002, 0.001) # make an array for energies
>>> sp = get_smeared_spectrum(energies, 0.3) # parse and make spectra
>>> fig, ax = plt.subplots(1)
>>> ax.set_xlabel("Energy (eV)")
>>> ax.set_ylabel("Intensity (arb. u.)")
>>> ax.plot(energies, sp[0, 0], label="x") # plot a spectrum for x component of the 1st core projection
>>> ax.plot(energies, sp[0, 1], label="y") # plot a spectrum for y component of the 1st core projection
>>> ax.plot(energies, sp[0, 2], label="z") # plot a spectrum for z component of the 1st core projection
>>> ax.plot(energies, np.mean(sp[0], axis=0), label="total") # plot a total spectrum of the 1st core projection
ceparser.get_spectrum(calc_dir='.', seed_name='case_elnes', output_eV=True, atomic_unit=False)[source]#

get primitive spectral data from a .bands file and a .eels_mat file

Parameters
  • calc_dir (str, default ".") – path to the directory containing .bands and .eels_mat

  • seed_name (str, default "case_elnes") – seed name of the calculation

  • output_eV (bool, default True) – whether output energy in eV (True) or hartree (False)

  • atomic_unit (bool, default False) – whether output dynamical structure factor in the unit of Bohr radius^2 (True) or angstrom^2 (False).

Returns

spectrum_dict – a dictionary of the spectral data

Return type

dict

ceparser.read_bands(filename, output_eV=True)[source]#

extract data from .bands file

Parameters
  • filename (str) – path to the .bands file

  • output_eV (bool, default True) – whether output energy in eV (True) or hartree (False)

Returns

bands_dict – a dictionary of the extracted data

Return type

dict

ceparser.read_bin_data(f, dtype, data_byte, header_byte=4, footer_byte=4)[source]#

read data from binary file This function reads a binary chunk from current position.

Parameters
  • f (file object) – a file object opened in read only mode.

  • dtype (str) – dtype of the binary chunk

  • data_byte (int) – length of the binary chunk to read in byte

  • header_byte (int, default 4) – length of the header before the binary chunk to read in byte

  • footer_byte (int, default 4) – length of the footer after the binary chunk to read in byte

Returns

data – a data converted by struct.unpack

Return type

int, str, float, or list

ceparser.read_eels_mat(filename)[source]#

extract data from .eels_mat file

Parameters

filename (str) – path to the .eels_mat file

Returns

eels_mat_dict – a dictionary of the extracted data - tot_core_projectors - max_eigenvalues - sum_num_kpoints - num_spins - core_orbital_species - core_orbital_ion - core_orbital_n - core_orbital_lm - transition_matrix

Return type

dict

ceparser.split_castep(filename)[source]#

Split .castep file into each calculation

Running CASTEP several times yields a single .castep file with concatenated output. This function splits the outputs into a list of each calculation run.

Parameters

filename (str) – path to the .castep file

Returns

run_list – list of lines of castep output for each run

Return type

list