Read molecular geometry from XYZ format file
Parses standard XYZ files with format: Line 1: Number of atoms Line 2: Comment/title line Lines 3+: Element X Y Z (coordinates in Angstrom)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
Path to XYZ file |
||
| type(geometry_type), | intent(out) | :: | geom |
Parsed molecular geometry |
||
| type(error_t), | intent(out) | :: | error |
Error handling |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=:), | private, | allocatable | :: | file_contents |
Full file content buffer |
||
| logical, | private | :: | file_exists |
Whether file exists on disk |
|||
| integer, | private | :: | file_size |
File size in bytes |
|||
| integer, | private | :: | io_stat |
I/O operation status |
|||
| integer, | private | :: | unit |
File unit number |
subroutine read_xyz_file(filename, geom, error) !! Read molecular geometry from XYZ format file !! !! Parses standard XYZ files with format: !! Line 1: Number of atoms !! Line 2: Comment/title line !! Lines 3+: Element X Y Z (coordinates in Angstrom) character(len=*), intent(in) :: filename !! Path to XYZ file type(geometry_type), intent(out) :: geom !! Parsed molecular geometry type(error_t), intent(out) :: error !! Error handling integer :: unit !! File unit number integer :: io_stat !! I/O operation status integer :: file_size !! File size in bytes logical :: file_exists !! Whether file exists on disk character(len=:), allocatable :: file_contents !! Full file content buffer ! Check if file exists inquire (file=filename, exist=file_exists, size=file_size) if (.not. file_exists) then call error%set(ERROR_IO, "XYZ file not found: "//trim(filename)) return end if ! Allocate buffer for entire file allocate (character(len=file_size) :: file_contents) ! Open and read entire file as stream open (newunit=unit, file=filename, status='old', action='read', & access='stream', form='unformatted', iostat=io_stat) if (io_stat /= 0) then call error%set(ERROR_IO, "Error opening file: "//trim(filename)) return end if read (unit, iostat=io_stat) file_contents close (unit) if (io_stat /= 0) then call error%set(ERROR_IO, "Error reading file: "//trim(filename)) return end if ! Parse the contents call read_xyz_string(file_contents, geom, error) if (error%has_error()) then call error%add_context("mqc_xyz_reader:read_xyz_file") return end if end subroutine read_xyz_file