From 3c2197b36a69fcb6d710337448d30d37430c2347 Mon Sep 17 00:00:00 2001
From: Anthony Scemama <scemama@irsamc.ups-tlse.fr>
Date: Mon, 27 Jun 2022 17:15:27 +0200
Subject: [PATCH 1/4] Update examples.org

---
 examples.org | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)

diff --git a/examples.org b/examples.org
index eaa7d58..b8ac7fa 100644
--- a/examples.org
+++ b/examples.org
@@ -3,7 +3,8 @@
 #+SETUPFILE: docs/theme.setup
 
   
-* Accessing sparse quantities
+* Accessing sparse quantities (integrals)
+
 ** Fortran
   :PROPERTIES:
   :header-args:    :tangle  print_energy.f90
@@ -270,3 +271,69 @@ program print_energy
 
 end program
     #+end_src
+
+
+* Reading determinants
+
+** Fortran
+  :PROPERTIES:
+  :header-args:    :tangle  print_dets.f90
+  :END:
+   
+   #+begin_src f90
+program test
+
+  use trexio
+  implicit none
+  
+  character*(128)           :: filename   ! Name of the input file
+  integer(trexio_exit_code) :: rc         ! Return code for error checking
+  integer(trexio_t)         :: trex_determinant_file
+
+  integer*8, allocatable :: buffer(:,:,:)
+  integer(8) :: offset, icount, BUFSIZE
+  integer :: ndet, int64_num, m
+
+  integer :: occ_num_up, occ_num_dn
+  integer, allocatable :: orb_list_up(:), orb_list_dn(:)
+
+  call getarg(1, filename)
+
+  trex_determinant_file = trexio_open(filename, 'r', TREXIO_AUTO, rc)
+  if (rc /= TREXIO_SUCCESS) then
+     call trexio_string_of_error(rc, err_msg)
+     print *, 'Error opening TREXIO file: '//trim(err_msg)
+     stop
+  end if
+  
+  rc = trexio_read_determinant_num(trex_determinant_file, ndet)
+  print *, 'ndet', ndet
+
+  rc = trexio_get_int64_num(trex_determinant_file, int64_num)
+  print *, 'int64_num', int64_num
+
+  BUFSIZE = 1000_8
+  allocate(buffer(int64_num, 2, BUFSIZE))
+  allocate(orb_list_up(int64_num*64), orb_list_dn(int64_num*64))
+
+  offset = 0_8
+  icount = BUFSIZE
+  do while (icount == BUFSIZE)
+    if (offset < ndet) then
+      rc = trexio_read_determinant_list(trex_determinant_file, offset, icount, buffer)
+      offset = offset + icount
+    else
+      icount = 0
+    end if
+    print *, '---'
+    do m=1,icount
+      rc = trexio_to_orbital_list_up_dn(int64_num, buffer(1,1,m), &
+           orb_list_up, orb_list_dn, occ_num_up, occ_num_dn)
+      print '(100(I3,X))', (orb_list_up(1:occ_num_up)), (orb_list_dn(1:occ_num_dn))
+      print *, ''
+    end do
+ end do
+
+end
+   #+end_src
+  

From 5f259d8e19ab28288e928fd743b25bd4b1f8d0f1 Mon Sep 17 00:00:00 2001
From: Anthony Scemama <scemama@irsamc.ups-tlse.fr>
Date: Mon, 27 Jun 2022 17:17:08 +0200
Subject: [PATCH 2/4] Update examples.org

---
 examples.org | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/examples.org b/examples.org
index b8ac7fa..ab93928 100644
--- a/examples.org
+++ b/examples.org
@@ -289,6 +289,8 @@ program test
   character*(128)           :: filename   ! Name of the input file
   integer(trexio_exit_code) :: rc         ! Return code for error checking
   integer(trexio_t)         :: trex_determinant_file
+  character*(128)  :: err_msg    ! Error message
+
 
   integer*8, allocatable :: buffer(:,:,:)
   integer(8) :: offset, icount, BUFSIZE

From a77021c9d39a4d33b117504f16ac3b427ad52eca Mon Sep 17 00:00:00 2001
From: Evgeny Posenitskiy <45995097+q-posev@users.noreply.github.com>
Date: Tue, 28 Jun 2022 10:15:38 +0200
Subject: [PATCH 3/4] Add missing deallocation and error handling

---
 examples.org | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/examples.org b/examples.org
index ab93928..241c63e 100644
--- a/examples.org
+++ b/examples.org
@@ -286,10 +286,10 @@ program test
   use trexio
   implicit none
   
-  character*(128)           :: filename   ! Name of the input file
-  integer(trexio_exit_code) :: rc         ! Return code for error checking
-  integer(trexio_t)         :: trex_determinant_file
-  character*(128)  :: err_msg    ! Error message
+  character*(128)           :: filename               ! Name of the input file
+  integer(trexio_exit_code) :: rc                     ! Return code for error checking
+  integer(trexio_t)         :: trex_determinant_file  ! TREXIO file handle
+  character*(128)           :: err_msg                ! Error message
 
 
   integer*8, allocatable :: buffer(:,:,:)
@@ -309,9 +309,19 @@ program test
   end if
   
   rc = trexio_read_determinant_num(trex_determinant_file, ndet)
+  if (rc /= TREXIO_SUCCESS) then
+     call trexio_string_of_error(rc, err_msg)
+     print *, 'Error reading determinant_num: '//trim(err_msg)
+     stop
+  end if
   print *, 'ndet', ndet
 
   rc = trexio_get_int64_num(trex_determinant_file, int64_num)
+  if (rc /= TREXIO_SUCCESS) then
+     call trexio_string_of_error(rc, err_msg)
+     print *, 'Error reading int64_num: '//trim(err_msg)
+     stop
+  end if
   print *, 'int64_num', int64_num
 
   BUFSIZE = 1000_8
@@ -334,7 +344,9 @@ program test
       print '(100(I3,X))', (orb_list_up(1:occ_num_up)), (orb_list_dn(1:occ_num_dn))
       print *, ''
     end do
- end do
+  end do
+
+  deallocate(buffer, orb_list_dn, orb_list_up)
 
 end
    #+end_src

From 5e4531f810cc19994e31d186a6f14220a77849b9 Mon Sep 17 00:00:00 2001
From: Anthony Scemama <scemama@irsamc.ups-tlse.fr>
Date: Fri, 1 Jul 2022 10:29:44 +0200
Subject: [PATCH 4/4] Typos

---
 tools/generator_tools.py |  2 +-
 trex.org                 | 15 +++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/tools/generator_tools.py b/tools/generator_tools.py
index 04cbe07..ab03b9b 100644
--- a/tools/generator_tools.py
+++ b/tools/generator_tools.py
@@ -232,7 +232,7 @@ def iterative_populate_file (filename: str, paths: dict, detailed_all: dict) ->
             for line in f_in :
                 id = check_triggers(line, triggers)
                 if id == 0:
-                    # special case for proper error handling when deallocting text groups
+                    # special case for proper error handling when deallocating text groups
                     error_handler = '  if (rc != TREXIO_SUCCESS) return rc;\n'
                     populated_line = iterative_replace_line(line, '$group$', detailed_all['groups'], add_line=error_handler)
                     f_out.write(populated_line)
diff --git a/trex.org b/trex.org
index e9c1a55..3b684fc 100644
--- a/trex.org
+++ b/trex.org
@@ -37,6 +37,8 @@ fetched using multiple function calls to perform I/O on buffers.
 For more information on how to read/write sparse data structures, see
 the [[./examples.html][examples]].
 
+For determinants, the ~special~ attribute is present in the type. This
+means that the source code is not produced by the generator, but hand-written.
 
   #+begin_src python :tangle trex.json :exports none
 {
@@ -678,11 +680,16 @@ prim_factor =
   in the memory.
 
   \[
-  D_I = \alpha_1 \alpha_2 \ldots \alpha_{n\uparrow} \beta_1 \beta_2 \ldots \beta_{n\downarrow}
+  D_I = \alpha_1 \alpha_2 \ldots \alpha_{n_\uparrow} \beta_1 \beta_2 \ldots \beta_{n_\downarrow}
   \]
 
-  where $\alpha$ and $\beta$ denote $\uparrow$-spin and $\downarrow$-spin electrons, respectively,
-  $n\uparrow$ and $n\downarrow$ correspond to ~electron.up_num~ and ~electron.dn_num~, respectively.
+  where $\alpha$ and $\beta$ denote \uparrow-spin and \downarrow-spin electrons, respectively,
+  $n_\uparrow$ and $n_\downarrow$ correspond to ~electron.up_num~ and ~electron.dn_num~, respectively.
+
+  Note: the ~special~ attribute is present in the types, meaning that the source node is not
+  produced by the code generator.
+
+  An illustration on how to read determinants is presented in the [[./examples.html][examples]].
 
   #+NAME: determinant
   | Variable      | Type            | Dimensions                    | Description                                            |
@@ -734,7 +741,7 @@ prim_factor =
   The reduced density matrices are defined in the basis of molecular
   orbitals.
 
-  The $\uparrow$-spin and $\downarrow$-spin components of the one-body
+  The \uparrow-spin and \downarrow-spin components of the one-body
   density matrix are given by
   \begin{eqnarray*}
     \gamma_{ij}^{\uparrow}   &=& \langle \Psi | \hat{a}^{\dagger}_{j\alpha}\, \hat{a}_{i\alpha} | \Psi \rangle \\