read_xyz_file Subroutine

public 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)

Arguments

Type IntentOptional 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


Calls

proc~~read_xyz_file~~CallsGraph proc~read_xyz_file read_xyz_file proc~error_add_context error_t%error_add_context proc~read_xyz_file->proc~error_add_context proc~error_has_error error_t%error_has_error proc~read_xyz_file->proc~error_has_error proc~error_set error_t%error_set proc~read_xyz_file->proc~error_set proc~read_xyz_string read_xyz_string proc~read_xyz_file->proc~read_xyz_string proc~read_xyz_string->proc~error_set proc~int_to_string int_to_string proc~read_xyz_string->proc~int_to_string proc~split_lines split_lines proc~read_xyz_string->proc~split_lines

Called by

proc~~read_xyz_file~~CalledByGraph proc~read_xyz_file read_xyz_file proc~initialize_system_geometry initialize_system_geometry proc~initialize_system_geometry->proc~read_xyz_file

Variables

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


Source Code

   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