Calculating distance in z direction

Hi everyone,

I would like to some help with getting/calculating distance between 2 sites in z-direction.
Here is what I would like to do using pymatgen

  1. Load a CONTCAR file using Structure
  2. I need to calculate distance between 2 sites in z-direction
  3. Arrange them in ascending order in z-direction so that I can use pymatgen.analysis.defects.core - Vacancy to create surface vacancy

I tried pymatgen.core.sites - Site to get the site but it didnt not work with the Structure file.
I also tried Structure.get_distance(i,j) , for example:
al2o3_b = Structure.from_file('./Al2O3/Bulk_Al2O3_550_5/CONTCAR') al2o3_b.get_distance(al2o3_b[0],al2o3_b[1])
It also did not work.

any recommendation?

Thank you for your time!

Do I understand you right here? You have site of interest and want to sort your structure’s sites according to the z-distance from that site in ascending order of distance:

from pymatgen.core import Structure

structure = Structure.from_file('CONTCAR')
ref_site = ... # YOUR SITE OF INTEREST HERE
structure.sort(key=lambda x: abs(x.coords[2] - ref_site.coords[2])))

Alternatively, if you just want to find surface sites, i.e. the sites in the plane that have the largest z-val, you can do:

from pymatgen.core import Structure

structure = Structure.from_file('CONTCAR')
structure.sort(key=lambda x: x.coords[2]

Hi,
Thank you for your respond!
I want to sort my structure to find my site of interest (The surface site)(In the other words, I need to get z-coords from the structure input). But. first, I need to calculate the distance between 2 sites in the z-direction to have value to compare after structure relaxation.

I think using the info I gave you in my first response along with:

zdist = abs(site1.coords[2] - site2.coords[2])

to get a specific z-distance, you should have all you need. If you need more help you can post your structures.

1 Like