Fortran interface reference

The chemfiles module is built around the main types of chemfiles: chfl_trajectory, chfl_frame, chfl_cell, chfl_topology, chfl_residue, chfl_atom, chfl_selection, and chfl_property. For more information about these types, please see the chemfiles overview.


Atomic indexes in chemfiles starts at 0, not 1. That means that the first atom in a frame have the index 0, not 1. This should be taken in account when using chemfiles functions. Fortran arrays returned by function still have indexes starting at 1.

program indexing
    use iso_fortran_env, only: real64
    use chemfiles
    implicit none
    type(chfl_frame)      :: frame
    type(chfl_atom)       :: atom
    real(real64), pointer :: positions(:, :)

    ! Initialize the frame ...

    ! Get the first atom in the frame
    atom = frame%atom(0)
    call atom%free()
    ! Get the second atom in the frame
    atom = frame%atom(1)
    call atom%free()

    positions => frame%positions()
    ! positions is a fortran array, so positions(1, :) now contains
    ! the positions of the first atom, and not positions(0, :)
end program


All the free functions and types have the chfl_ prefix. Most functions take an optional status argument which will indicate the status of the operation. It should be CHFL_SUCCESS if everything was OK, and another value in case of error.

When creating a variable of one of the chemfiles types, the first routine to be called should be an initialization routine. It can be either the init routine for default initialization, or another routine documented as initializing.

These initialization function should only be called once. In order to free the memory asssociated with any chemfiles variable, the free subroutine should be called. After a call the the free subroutine, the init subroutine can be called again whithout any memory leak risk. Not initializing chemfiles variables will lead to errors and segmentation faults.

type(chfl_cell) :: cell
type(chfl_frame) :: frame

! Initialize the variables
call cell%init([20, 20, 20])
call frame%init()

! free the memory
call cell%free()
call frame%free()