From f6ffbdec76c92100e5b3ccd78946a09d33b89100 Mon Sep 17 00:00:00 2001 From: scemama Date: Mon, 13 Sep 2021 16:10:40 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20TREX-CoE?= =?UTF-8?q?/trexio@a880b0b354fd0a5c8962524e12120312013614ec=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.html | 4 +- Sparse.html | 16 +- index.html | 4 +- templator_front.html | 174 +++++++++--------- templator_hdf5.html | 52 +++--- templator_text.html | 112 ++++++------ trex.html | 118 ++++++------ tutorial_benzene.md | 414 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 654 insertions(+), 240 deletions(-) create mode 100644 tutorial_benzene.md diff --git a/README.html b/README.html index f4c1782..9ca26a8 100644 --- a/README.html +++ b/README.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + TREXIO source code documentation @@ -342,7 +342,7 @@ and bug reports should be submitted at

Author: TREX-CoE

-

Created: 2021-08-25 Wed 13:50

+

Created: 2021-09-13 Mon 16:10

Validate

diff --git a/Sparse.html b/Sparse.html index 7fca47c..dd9b5c5 100644 --- a/Sparse.html +++ b/Sparse.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + @@ -232,8 +232,8 @@ for the JavaScript code in this tag.

Table of Contents

@@ -241,8 +241,8 @@ for the JavaScript code in this tag. See templatorfront.org

-
-

1 Text back end

+
+

1 Text back end

As the size of the dataset should be extensible, the simplest @@ -256,8 +256,8 @@ The offset can be used with fseek(69L*offset, SEEK_SET)

-
-

2 HDF5 Back end

+
+

2 HDF5 Back end

We need to declare the number of rows of the dataset as @@ -278,7 +278,7 @@ If the offset+num > nmax, we need to extend the dataset.

-

Created: 2021-08-25 Wed 13:50

+

Created: 2021-09-13 Mon 16:10

Validate

diff --git a/index.html b/index.html index f4c1782..9ca26a8 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + TREXIO source code documentation @@ -342,7 +342,7 @@ and bug reports should be submitted at

Author: TREX-CoE

-

Created: 2021-08-25 Wed 13:50

+

Created: 2021-09-13 Mon 16:10

Validate

diff --git a/templator_front.html b/templator_front.html index c459985..385c6fb 100644 --- a/templator_front.html +++ b/templator_front.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + Front end API @@ -333,61 +333,61 @@ for the JavaScript code in this tag.

Table of Contents

-
-

1 Coding conventions

+
+

1 Coding conventions

  • integer types will be defined using types given in stdint.h
  • @@ -402,8 +402,8 @@ for the JavaScript code in this tag.
-
-

1.1 Memory allocation

+
+

1.1 Memory allocation

Memory allocation of structures can be facilitated by using the @@ -438,8 +438,8 @@ The maximum string size for the filenames is 4096 characters.

-
-

2 Front end

+
+

2 Front end

All calls to TREXIO are thread-safe. @@ -447,10 +447,10 @@ TREXIO front end is modular, which simplifies implementation of new back ends.

-
-

2.1 Error handling

+
+

2.1 Error handling

- +
@@ -750,8 +750,8 @@ The text strings are extracted from the previous table. -
-

2.2 Back ends

+
+

2.2 Back ends

TREXIO has several back ends: @@ -783,8 +783,8 @@ lines that correspond to the TREXIO_JSON back end (not implemented

-
-

2.3 Read/write behavior

+
+

2.3 Read/write behavior

Every time a reading function is called, the data is read from the @@ -812,8 +812,8 @@ concurrent programs, the behavior is not specified.

-
-

2.4 TREXIO file type

+
+

2.4 TREXIO file type

trexio_s is the the main type for TREXIO files, visible to the users @@ -847,8 +847,8 @@ TREXIO files will have as a first argument the TREXIO file handle.

-
-

2.5 Polymorphism of the file handle

+
+

2.5 Polymorphism of the file handle

Polymorphism of the trexio_t type is handled by ensuring that the @@ -867,8 +867,8 @@ corresponding types for all back ends can be safely casted to

-
-

2.6 File opening

+
+

2.6 File opening

trexio_open creates a new TREXIO file or opens existing one. @@ -1095,8 +1095,8 @@ know if we need to shift by 1 arrays of indices.

-
-

2.7 File closing

+
+

2.7 File closing

trexio_close closes an existing trexio_t file. @@ -1192,12 +1192,12 @@ output:

-
-

3 Templates for front end

+
+

3 Templates for front end

-
-

3.1 Description

+
+

3.1 Description

Consider the following block of trex.json: @@ -1387,8 +1387,8 @@ or double precision for integer and floating point numbers.

-
-

3.2 Templates for front end has/read/write a single dimensioning variable

+
+

3.2 Templates for front end has/read/write a single dimensioning variable

This section concerns API calls related to dimensioning variables. @@ -1457,8 +1457,8 @@ This section concerns API calls related to dimensioning variables.

-
-

3.2.1 C templates for front end

+
+

3.2.1 C templates for front end

The C templates that correspond to each of the abovementioned @@ -1647,8 +1647,8 @@ precision (see Table above).

-
-

3.2.2 Fortran templates for front end

+
+

3.2.2 Fortran templates for front end

The Fortran templates that provide an access to the C API calls from Fortran. @@ -1734,8 +1734,8 @@ These templates are based on the use of iso_c_binding. Pointers hav

-
-

3.3 Templates for front end has/read/write a dataset of numerical data

+
+

3.3 Templates for front end has/read/write a dataset of numerical data

This section concerns API calls related to datasets. @@ -1804,8 +1804,8 @@ This section concerns API calls related to datasets.

-
-

3.3.1 C templates for front end

+
+

3.3.1 C templates for front end

The C templates that correspond to each of the abovementioned functions can be found below. @@ -2124,8 +2124,8 @@ The basic (non-suffixed) API call on datasets deals with double precision (see T

-
-

3.3.2 Fortran templates for front end

+
+

3.3.2 Fortran templates for front end

The Fortran templates that provide an access to the C API calls from Fortran. @@ -2211,8 +2211,8 @@ These templates are based on the use of iso_c_binding. Pointers hav

-
-

3.4 Sparse data structures

+
+

3.4 Sparse data structures

Sparse data structures are used typically for large tensors such as @@ -2353,12 +2353,12 @@ For the values,

-
-

3.5 Templates for front end has/read/write a dataset of strings

+
+

3.5 Templates for front end has/read/write a dataset of strings

-
-

3.5.1 Introduction

+
+

3.5.1 Introduction

This section concerns API calls related to datasets of strings. @@ -2398,8 +2398,8 @@ This section concerns API calls related to datasets of strings.

-
-

3.5.2 C templates for front end

+
+

3.5.2 C templates for front end

First parameter is the TREXIO file handle. Second parameter is the variable to be written/read @@ -2642,8 +2642,8 @@ to/from the TREXIO file (except for trexio_has_ functi

-
-

3.5.3 Fortran templates for front end

+
+

3.5.3 Fortran templates for front end

The Fortran templates that provide an access to the C API calls from Fortran. @@ -2740,12 +2740,12 @@ These templates are based on the use of iso_c_binding. Pointers hav

-
-

3.6 Templates for front end has/read/write a single string attribute

+
+

3.6 Templates for front end has/read/write a single string attribute

-
-

3.6.1 Introduction

+
+

3.6.1 Introduction

This section concerns API calls related to string attributes. @@ -2785,8 +2785,8 @@ This section concerns API calls related to string attributes.

-
-

3.6.2 C templates for front end

+
+

3.6.2 C templates for front end

trexio_exit_code
@@ -2886,8 +2886,8 @@ This section concerns API calls related to string attributes.
 
-
-

3.6.3 Fortran templates for front end

+
+

3.6.3 Fortran templates for front end

The Fortran templates that provide an access to the C API calls from Fortran. @@ -2966,8 +2966,8 @@ These templates are based on the use of iso_c_binding. Pointers hav

-
-

4 Fortran helper/wrapper functions

+
+

4 Fortran helper/wrapper functions

The function below adapts the original C-based trexio_open for Fortran. @@ -3101,7 +3101,7 @@ two code are identical, i.e. if the assert statement pass.

Author: TREX-CoE

-

Created: 2021-08-25 Wed 13:50

+

Created: 2021-09-13 Mon 16:10

Validate

diff --git a/templator_hdf5.html b/templator_hdf5.html index 5cdc997..a3a8a11 100644 --- a/templator_hdf5.html +++ b/templator_hdf5.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + HDF5 back end @@ -311,27 +311,27 @@ for the JavaScript code in this tag.

Table of Contents

-
-

1 HDF5 back end

+
+

1 HDF5 back end

-
-

1.1 Template for HDF5 definitions

+
+

1.1 Template for HDF5 definitions

#define $GROUP$_GROUP_NAME          "$group$"
@@ -343,8 +343,8 @@ for the JavaScript code in this tag.
 
-
-

1.2 Template for HDF5 structures

+
+

1.2 Template for HDF5 structures

typedef struct trexio_hdf5_s {
@@ -359,8 +359,8 @@ for the JavaScript code in this tag.
 
-
-

1.3 Template for HDF5 init/deinit

+
+

1.3 Template for HDF5 init/deinit

trexio_exit_code
@@ -440,8 +440,8 @@ for the JavaScript code in this tag.
 
-
-

1.4 Template for HDF5 has/read/write a single dimensioning variable

+
+

1.4 Template for HDF5 has/read/write a single dimensioning variable

trexio_exit_code
@@ -557,8 +557,8 @@ for the JavaScript code in this tag.
 
-
-

1.5 Template for HDF5 has/read/write a dataset of numerical data

+
+

1.5 Template for HDF5 has/read/write a dataset of numerical data

trexio_exit_code
@@ -687,8 +687,8 @@ for the JavaScript code in this tag.
 
-
-

1.6 Template for HDF5 has/read/write a dataset of strings

+
+

1.6 Template for HDF5 has/read/write a dataset of strings

trexio_exit_code
@@ -892,8 +892,8 @@ for the JavaScript code in this tag.
 
-
-

1.7 Template for HDF5 has/read/write a single string attribute

+
+

1.7 Template for HDF5 has/read/write a single string attribute

trexio_exit_code
@@ -1020,7 +1020,7 @@ for the JavaScript code in this tag.
 

Author: TREX-CoE

-

Created: 2021-08-25 Wed 13:50

+

Created: 2021-09-13 Mon 16:10

Validate

diff --git a/templator_text.html b/templator_text.html index 09da4c7..a59bdc3 100644 --- a/templator_text.html +++ b/templator_text.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + TEXT back end @@ -311,26 +311,26 @@ for the JavaScript code in this tag.

Table of Contents

-
-

1 TEXT back end

+
+

1 TEXT back end

The "file" produced by the text back end is a directory with one @@ -361,8 +361,8 @@ The file is written when closed, or when the flush function is called.

-
-

1.1 Template for group-related structures in text back end

+
+

1.1 Template for group-related structures in text back end

typedef struct $group$_s {
@@ -380,8 +380,8 @@ The file is written when closed, or when the flush function is called.
 
-
-

1.2 Template for general structure in text back end

+
+

1.2 Template for general structure in text back end

typedef struct rdm_s {
@@ -407,8 +407,8 @@ The file is written when closed, or when the flush function is called.
 
-
-

1.3 Initialize function (constant part)

+
+

1.3 Initialize function (constant part)

trexio_exit_code
@@ -509,8 +509,8 @@ The file is written when closed, or when the flush function is called.
 
-
-

1.4 Deinitialize function (templated part)

+
+

1.4 Deinitialize function (templated part)

trexio_exit_code
@@ -535,8 +535,8 @@ The file is written when closed, or when the flush function is called.
 
-
-

1.5 Template for text read struct

+
+

1.5 Template for text read struct

$group$_t*
@@ -807,8 +807,8 @@ trexio_text_read_$group$ (trexio_text_t* 
 
-
-

1.6 Template for text flush struct

+
+

1.6 Template for text flush struct

trexio_exit_code
@@ -872,8 +872,8 @@ trexio_text_read_$group$ (trexio_text_t* 
 
-
-

1.7 Template for text free memory

+
+

1.7 Template for text free memory

Memory is allocated when reading. The following function frees memory. @@ -918,8 +918,8 @@ Memory is allocated when reading. The following function frees memory.

-
-

1.8 Template for has/read/write the num attribute

+
+

1.8 Template for has/read/write the num attribute

trexio_exit_code
@@ -981,8 +981,8 @@ Memory is allocated when reading. The following function frees memory.
 
-
-

1.9 Template for has/read/write the dataset of numerical data

+
+

1.9 Template for has/read/write the dataset of numerical data

The group_dset array is assumed allocated with the appropriate size. @@ -1078,8 +1078,8 @@ The group_dset array is assumed allocated with the appropriate size

-
-

1.10 Template for has/read/write the dataset of strings

+
+

1.10 Template for has/read/write the dataset of strings

The group_dset array is assumed allocated with the appropriate size. @@ -1181,8 +1181,8 @@ The group_dset array is assumed allocated with the appropriate size

-
-

1.11 Template for has/read/write the string attribute

+
+

1.11 Template for has/read/write the string attribute

trexio_exit_code
@@ -1256,12 +1256,12 @@ The group_dset array is assumed allocated with the appropriate size
 
-
-

1.12 RDM struct (hard-coded)

+
+

1.12 RDM struct (hard-coded)

-
-

1.12.1 Read the complete struct

+
+

1.12.1 Read the complete struct

rdm_t* trexio_text_read_rdm(trexio_text_t* const file);
@@ -1356,8 +1356,8 @@ The group_dset array is assumed allocated with the appropriate size
 
-
-

1.12.2 Flush the complete struct

+
+

1.12.2 Flush the complete struct

trexio_exit_code trexio_text_flush_rdm(trexio_text_t* const file);
@@ -1399,8 +1399,8 @@ The group_dset array is assumed allocated with the appropriate size
 
-
-

1.12.3 Free memory

+
+

1.12.3 Free memory

Memory is allocated when reading. The followig function frees memory. @@ -1436,8 +1436,8 @@ Memory is allocated when reading. The followig function frees memory.

-
-

1.12.4 Read/Write the onee attribute

+
+

1.12.4 Read/Write the onee attribute

The one_e array is assumed allocated with the appropriate size. @@ -1503,8 +1503,8 @@ The one_e array is assumed allocated with the appropriate size.

-
-

1.12.5 Read/Write the twoe attribute

+
+

1.12.5 Read/Write the twoe attribute

two_e is a sparse data structure, which can be too large to fit @@ -1612,7 +1612,7 @@ file for each sparse float structure.

Author: TREX-CoE

-

Created: 2021-08-25 Wed 13:50

+

Created: 2021-09-13 Mon 16:10

Validate

diff --git a/trex.html b/trex.html index 3dd8b21..9619062 100644 --- a/trex.html +++ b/trex.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + TREX Configuration file @@ -333,28 +333,28 @@ for the JavaScript code in this tag.

Table of Contents

@@ -386,8 +386,8 @@ arrays are 0-based. Hence, we introduce the index type which is an 1-based int in the Fortran interface and 0-based otherwise.

-
-

1 Metadata (metadata group)

+
+

1 Metadata (metadata group)

As we expect our files to be archived in open-data repositories, we @@ -397,7 +397,7 @@ which have participated to the creation of the file, a list of authors of the file, and a textual description.

- +
@@ -464,15 +464,15 @@ authors of the file, and a textual description. -
-

2 Electron (electron group)

+
+

2 Electron (electron group)

We consider wave functions expressed in the spin-free formalism, where the number of ↑ and ↓ electrons is fixed.

-
+
@@ -511,15 +511,15 @@ the number of ↑ and ↓ electrons is fixed. -
-

3 Nucleus (nucleus group)

+
+

3 Nucleus (nucleus group)

The nuclei are considered as fixed point charges. Coordinates are given in Cartesian \((x,y,z)\) format.

-
+
@@ -579,8 +579,8 @@ given in Cartesian \((x,y,z)\) format. -
-

4 TODO Effective core potentials (ecp group)

+
+

4 TODO Effective core potentials (ecp group)

An effective core potential (ECP) \(V_A^{\text{pp}}\) replacing the @@ -625,7 +625,7 @@ letters are parameters.

  • \(\hat{V}_\text{ecp,nl} = \sum_A \hat{V}_A^{\text{nl}}\) : non-local component
  • -
    +
    @@ -734,8 +734,8 @@ letters are parameters. -
    -

    5 Basis set (basis group)

    +
    +

    5 Basis set (basis group)

    We consider here basis functions centered on nuclei. Hence, we enable @@ -788,7 +788,7 @@ If the the basis function is not considered normalized, \(\mathcal{N}_s=1\). All the basis set parameters are stored in one-dimensional arrays:

    -
    +
    @@ -957,8 +957,8 @@ prim_factor = -
    -

    6 Atomic orbitals (ao group)

    +
    +

    6 Atomic orbitals (ao group)

    Going from the atomic basis set to AOs implies a systematic @@ -1006,13 +1006,13 @@ shell, as in the GAMESS convention where

    In such a case, one should set the normalization of the shell (in -the Basis set section) to \(\mathcal{N}_{z^2}\), which is the +the Basis set section) to \(\mathcal{N}_{z^2}\), which is the normalization factor of the atomic orbitals in spherical coordinates. The normalization factor of the \(xy\) function which should be introduced here should be \(\frac{\mathcal{N}_{xy}}{\mathcal{N}_{z^2}}\).

    -
    +
    @@ -1064,8 +1064,8 @@ introduced here should be \(\frac{\mathcal{N}_{xy}}{\mathcal{N}_{z^2}}\).
    -
    -

    6.1 One-electron integrals (ao_1e_int group)

    +
    +

    6.1 One-electron integrals (ao_1e_int group)

    • \[ \hat{V}_{\text{ne}} = \sum_{A=1}^{N_\text{nucl}} @@ -1083,7 +1083,7 @@ The one-electron integrals for a one-electron operator \(\hat{O}\) are over atomic orbitals.

      - +
      @@ -1150,8 +1150,8 @@ over atomic orbitals. -
      -

      6.2 Two-electron integrals (ao_2e_int group)

      +
      +

      6.2 Two-electron integrals (ao_2e_int group)

      The two-electron integrals for a two-electron operator \(\hat{O}\) are @@ -1172,7 +1172,7 @@ notation. \mathbf{r}_j \vert)}{\vert \mathbf{r}_i - \mathbf{r}_j \vert} \] : electron-electron long range potential -

      +
      @@ -1212,10 +1212,10 @@ notation. -
      -

      7 Molecular orbitals (mo group)

      +
      +

      7 Molecular orbitals (mo group)

      -
      +
      @@ -1281,8 +1281,8 @@ notation.
    -
    -

    7.1 One-electron integrals (mo_1e_int group)

    +
    +

    7.1 One-electron integrals (mo_1e_int group)

    The operators as the same as those defined in the @@ -1290,7 +1290,7 @@ The operators as the same as those defined in the the basis of molecular orbitals.

    - +
    @@ -1357,8 +1357,8 @@ the basis of molecular orbitals. -
    -

    7.2 Two-electron integrals (mo_2e_int group)

    +
    +

    7.2 Two-electron integrals (mo_2e_int group)

    The operators as the same as those defined in the @@ -1366,7 +1366,7 @@ The operators as the same as those defined in the the basis of molecular orbitals.

    -
    +
    @@ -1406,13 +1406,13 @@ the basis of molecular orbitals. -
    -

    8 TODO Slater determinants

    +
    +

    8 TODO Slater determinants

    -
    -

    9 TODO Reduced density matrices (rdm group)

    +
    +

    9 TODO Reduced density matrices (rdm group)

    -
    +
    @@ -1465,15 +1465,15 @@ the basis of molecular orbitals. -
    -

    10 Appendix

    +
    +

    10 Appendix

    -
    -

    10.1 Python script from table to json

    +
    +

    10.1 Python script from table to json

    -
    print("""#+begin_src python :tangle trex.json""")
    +
    print("""#+begin_src python :tangle trex.json""")
     print("""    "%s": {"""%(title))
     indent = "        "
     f1 = 0 ; f2 = 0 ; f3 = 0
    @@ -1526,7 +1526,7 @@ the basis of molecular orbitals.
     

    Author: TREX-CoE

    -

    Created: 2021-08-25 Wed 13:50

    +

    Created: 2021-09-13 Mon 16:10

    Validate

    diff --git a/tutorial_benzene.md b/tutorial_benzene.md new file mode 100644 index 0000000..184be3a --- /dev/null +++ b/tutorial_benzene.md @@ -0,0 +1,414 @@ +# TREXIO Tutorial + +This interactive Tutorial covers some basic use cases of the TREXIO library based on the Python API. At this point, it is assumed that the TREXIO Python package has been sucessfully installed on the user machine or in the virtual environment. If this is not the case, feel free to follow the [installation guide](https://github.com/TREX-CoE/trexio/blob/master/python/README.md). + +## Importing TREXIO + +First of all, let's import the TREXIO package. + + +```python +try: + import trexio +except ImportError: + raise Exception("Unable to import trexio. Please check that trexio is properly instaled.") +``` + +If no error occurs, then it means that the TREXIO package has been sucessfully imported. Within the current import, TREXIO attributes can be accessed using the corresponding `trexio.attribute` notation. If you prefer to bound a shorter name to the imported module (as commonly done by the NumPy users with `import numpy as np`), this is also possible. To do so, replace `import trexio` with `import trexio as tr` for example. To learn more about importing modules, see the corresponding page of the [Python documentation](https://docs.python.org/3/tutorial/modules.html#more-on-modules). + +## Creating a new TREXIO file + +TREXIO currently supports two back ends for file I/O: + +1. `TREXIO_HDF5`, which relies on extensive use of the [HDF5 library](https://portal.hdfgroup.org/display/HDF5/HDF5) and the associated binary file format. This back end is optimized for high performance but it requires HDF5 to be installed on the user machine. + +2. `TREXIO_TEXT`, which relies on basic I/O operations that are available in the standard C library. This back end is not optimized for performance but it is supposed to work "out-of-the-box" since there are no external dependencies. + +Armed with these new definitions, let's proceed with the tutorial. The first task is to create a TREXIO file called `benzene_demo.h5`. But first we have to remove the file if it exists in the current directory + + +```python +filename = 'benzene_demo.h5' + +import os +try: + os.remove(filename) +except: + print(f"File {filename} does not exist.") +``` + + File benzene_demo.h5 does not exist. + + +We are now ready to create a new TREXIO file: + + +```python +demo_file = trexio.File(filename, mode='w', back_end=trexio.TREXIO_HDF5) +``` + +This creates an instance of the `trexio.File` class, which we refer to as `demo_file` in this tutorial. You can check that the corresponding file called `benzene_demo.h5` exists in the root directory. It is now open for writing as indicated by the user-supplied argument `mode='w'`. The file has been initiated using `TREXIO_HDF5` back end and will be accessed accordingly from now on. The information about back end is stored internally by TREXIO, which means that there is no need to specify it every time the I/O operation is performed. If the file named `benzene_demo.h5` already exists, then it is re-opened for writing (and not truncated to prevent data loss). + +## Writing data in the TREXIO file + +Prior to any work with TREXIO library, we highly recommend users to read about [TREXIO internal configuration](https://trex-coe.github.io/trexio/trex.html), which explains the structure of the wavefunction file. The reason is that TREXIO API has a naming convention, which is based on the groups and variables names that are pre-defined by the developers. In this Tutorial, we will only cover contents of the `nucleus` group. Note that custom groups and variables can be added to the TREXIO API. + +In this Tutorial, we consider benzene molecule (C6H6) as an example. Since benzene has 12 atoms, let's specify it in the previously created `demo_file`. In order to do so, one has to call `trexio.write_nucleus_num` function, which accepts an instance of the `trexio.File` class as a first argument and an `int` value corresponding to the number of nuclei as a second argument. + + +```python +nucleus_num = 12 +``` + + +```python +trexio.write_nucleus_num(demo_file, nucleus_num) +``` + +In fact, all API functions that contain `write_` prefix can be used in a similar way. +Variables that contain `_num` suffix are important part of the TREXIO file because some of them define dimensions of arrays. For example, `nucleus_num` variable corresponds to the number of atoms, which will be internally used to write/read the `nucleus_coord` array of nuclear coordinates. In order for TREXIO files to be self-consistent, overwriting num-suffixed variables is currently disabled. + +The number of atoms is not sufficient to define a molecule. Let's first create a list of nuclear charges, which correspond to benzene. + + +```python +charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.] +``` + +According to the TREX configuration file, there is a `charge` attribute of the `nucleus` group, which has `float` type and `[nucleus_num]` dimension. The `charges` list defined above fits nicely in the description and can be written as follows + + +```python +trexio.write_nucleus_charge(demo_file, charges) +``` + +**Note: TREXIO function names only contain parts in singular form.** This means that, both `write_nucleus_charges` and `write_nuclear_charges` are invalid API calls. These functions simply do not exist in the `trexio` Python package and the corresponding error message should appear. + + Alternatively, one can provide a list of nuclear labels (chemical elements from the periodic table) that correspond to the aforementioned charges. There is a `label` attribute of the `nucleus` group, which has `str` type and `[nucleus_num]` dimension. Let's create a list of 12 strings, which correspond to 6 carbon and 6 hydrogen atoms: + + +```python +labels = [ + 'C', + 'C', + 'C', + 'C', + 'C', + 'C', + 'H', + 'H', + 'H', + 'H', + 'H', + 'H'] +``` + +This can now be written using the corresponding `trexio.write_nucleus_label` function: + + +```python +trexio.write_nucleus_label(demo_file, labels) +``` + +Two examples above demonstrate how to write arrays of numbers or strings in the file. TREXIO also supports I/O operations on single numerical or string attributes. In fact, in this Tutorial you have already written one numerical attribute: `nucleus_num`. Let's now write a string `'D6h'`, which indicates a point group of benzene molecule. According to the TREX configuration file, `point_group` is a `str` attribute of the `nucleus` group, thus it can be written in the `demo_file` as follows + + +```python +point_group = 'D6h' +``` + + +```python +trexio.write_nucleus_point_group(demo_file, point_group) +``` + +### Writing NumPy arrays (float or int types) + +The aforementioned examples cover the majority of the currently implemented functionality related to writing data in the file. It is worth mentioning that I/O of numerical arrays in TREXIO Python API relies on extensive use of the [NumPy package](https://numpy.org/). This will be discussed in more details in the [section about reading data](#Reading-data-from-the-TREXIO-file). However, TREXIO `write_` functions that work with numerical arrays also accept `numpy.ndarray` objects. For example, consider a `coords` list of nuclear coordinates that correspond to benzene molecule + + +```python +coords = [ + [0.00000000 , 1.39250319 , 0.00000000 ], + [-1.20594314 , 0.69625160 , 0.00000000 ], + [-1.20594314 , -0.69625160 , 0.00000000 ], + [0.00000000 , -1.39250319 , 0.00000000 ], + [1.20594314 , -0.69625160 , 0.00000000 ], + [1.20594314 , 0.69625160 , 0.00000000 ], + [-2.14171677 , 1.23652075 , 0.00000000 ], + [-2.14171677 , -1.23652075 , 0.00000000 ], + [0.00000000 , -2.47304151 , 0.00000000 ], + [2.14171677 , -1.23652075 , 0.00000000 ], + [2.14171677 , 1.23652075 , 0.00000000 ], + [0.00000000 , 2.47304151 , 0.00000000 ], + ] +``` + +Let's take advantage of using NumPy arrays with fixed precision for floating point numbers. But first, try to import the `numpy` package + + +```python +try: + import numpy as np +except ImportError: + raise Exception("Unable to import numpy. Please check that numpy is properly instaled.") +``` + +You can now convert the previously defined `coords` list into a numpy array with fixed `float64` type as follows + + +```python +coords_np = np.array(coords, dtype=np.float64) +``` + +TREXIO functions that write numerical arrays accept both lists and numpy arrays as a second argument. That is, both `trexio.write_nucleus_coord(demo_file, coords)` and `trexio.write_nucleus_coord(demo_file, coords_np)` are valid API calls. Let's use the latter and see if it works + + +```python +trexio.write_nucleus_coord(demo_file, coords_np) +``` + +Congratulations, you have just completed the `nucleus` section of the TREXIO file for benzene molecule! Note that TREXIO API is rather permissive and do not impose any strict ordering on the I/O operations. The only requirement is that dimensioning (`_num` suffixed) variables have to be written in the file **before** writing arrays that depend on these variables. For example, attempting to write `nucleus_charge` or `nucleus_coord` fails if `nucleus_num` has not been written. + +### TREXIO error handling + +TREXIO Python API provides the `trexio.Error` class which simplifies exception handling in the Python scripts. This class wraps up TREXIO return codes and propagates them all the way from the C back end to the Python front end. Let's try to write a negative number of basis set shells `basis_num` in the TREXIO file. + + +```python +try: + trexio.write_basis_num(demo_file, -256) +except trexio.Error as e: + print(f"TREXIO error message: {e.message}") +``` + + TREXIO error message: Invalid argument 2 + + +The error message says **Invalid argument 2**, which indicates that the user-provided value `-256` is not valid. + +As mentioned before, `_num`-suffixed variables cannot be overwritten in the file. But what happens if you accidentally attempt to do so? Let's have a look at the `write_nucleus_num` function as an example: + + +```python +try: + trexio.write_nucleus_num(demo_file, 24) +except trexio.Error as e: + print(f"TREXIO error message: {e.message}") +``` + + TREXIO error message: Attribute already exists + + +The API rightfully complains that the target attribute already exists and cannot be overwritten. + +Alternatively, the aforementioned case can be handled using `trexio.has_nucleus_num` function as follows + + +```python +if not trexio.has_nucleus_num: + trexio.write_nucleus_num(demo_file, 24) +``` + +TREXIO functions with `has_` prefix return `True` if the corresponding variable exists and `False` otherwise. + +What about writing arrays? Let's try to write an list of 48 nuclear indices instead of 12 + + +```python +indices = [i for i in range(nucleus_num*4)] +``` + + +```python +try: + trexio.write_basis_nucleus_index(demo_file, indices) +except trexio.Error as e: + print(f"TREXIO error message: {e.message}") +``` + + TREXIO error message: Access to memory beyond allocated + + +According to the TREX configuration file, `nucleus_index` attribute of a `basis` group is supposed to have `[nucleus_num]` elements. In the example above, we have tried to write 4 times more elements, which might lead to memory and/or file corruption. Luckily, TREXIO internally checks the array dimensions and returns an error in case of inconsistency. + +## Closing the TREXIO file + +It is good practice to close the TREXIO file at the end of the session. In fact, `trexio.File` class has a destructor, which normally takes care of that. However, if you intend to re-open the TREXIO file, it has to be closed explicitly before. This can be done using the `close` method, i.e. + + +```python +demo_file.close() +``` + +Good! You are now ready to inspect the contents of the `benzene_demo.h5` file using the reading functionality of TREXIO. + +## Reading data from the TREXIO file + +First, let's try to open an existing TREXIO file in read-only mode. This can be done by creating a new instance of the `trexio.File` class but this time with `mode='r'` argument. Back end has to be specified as well. + + +```python +demo_file_r = trexio.File(filename, mode='r', back_end=trexio.TREXIO_HDF5) +``` + +When reading data from the TREXIO file, the only required argument is a previously created instance of `trexio.File` class. In our case, it is `demo_file_r`. TREXIO functions with `read_` prefix return the desired variable as an output. For example, `nucleus_num` value can be read from the file as follows + + +```python +nucleus_num_r = trexio.read_nucleus_num(demo_file_r) +``` + + +```python +print(f"nucleus_num from {filename} file ---> {nucleus_num_r}") +``` + + nucleus_num from benzene_demo.h5 file ---> 12 + + +The function call assigns `nucleus_num_r` to 12, which is consistent with the number of atoms in benzene that we wrote in the previous section. + +All calls to functions that read data can be done in a very similar way. The key point here is a function name, which in turn defines the output format. Hopefully by now you got used to the TREXIO naming convention and the contents of the `nucleus` group. Which function would you call to read a `point_group` attribute of the `nucleus` group? What type does it return? See the answer below: + + +```python +point_group_r = trexio.read_nucleus_point_group(demo_file_r) +``` + + +```python +print(f"nucleus_point_group from {filename} TREXIO file ---> {point_group_r}\n") +print(f"Is return type of read_nucleus_point_group a string? ---> {isinstance(point_group_r, str)}") +``` + + nucleus_point_group from benzene_demo.h5 TREXIO file ---> D6h + + Is return type of read_nucleus_point_group a string? ---> True + + +The `trexio.read_nucleus_point_group` function call returns a string `D6h`, which is exactly what we provided in the previous section. Now, let's read nuclear charges and labels. + + +```python +labels_r = trexio.read_nucleus_label(demo_file_r) +``` + + +```python +print(f"nucleus_label from {filename} file \n---> {labels_r}") +``` + + nucleus_label from benzene_demo.h5 file + ---> ['C', 'C', 'C', 'C', 'C', 'C', 'H', 'H', 'H', 'H', 'H', 'H'] + + + +```python +charges_r = trexio.read_nucleus_charge(demo_file_r) +``` + + +```python +print(f"nucleus_charge from {filename} file \n---> {charges_r}") +``` + + nucleus_charge from benzene_demo.h5 file + ---> [6. 6. 6. 6. 6. 6. 1. 1. 1. 1. 1. 1.] + + +The values are consistent with each other and with the previously written data. Not bad. What about the format of the output? + + +```python +print(f"nucleus_label return type: {type(labels_r)}") +``` + + nucleus_label return type: + + +This makes sense, isn't it? We have written a `list` of nuclear labels and have received back a `list` of values from the file. What about nuclear charges? + + +```python +print(f"nucleus_charge return type: {type(charges_r)}") +``` + + nucleus_charge return type: + + +Looks like `trexio.read_nucleus_charge` function returns a `numpy.ndarray` even though we have provided a python-ic `list` to `trexio.write_nucleus_charge` in the previous section. Why is it so? As has been mentioned before, TREXIO Python API internally relies on the use of the NumPy package to communicate arrays of `float`-like or `int`-like values. This prevents some memory leaks and grants additional flexibility to the API. What kind of flexibility? Check this out: + + +```python +print(f"return dtype in NumPy notation: ---> {charges_r.dtype}") +``` + + return dtype in NumPy notation: ---> float64 + + +It means that the default precision of the TREXIO output is double (`np.float64`) for arrays of floating point numbers like `nucleus_charge`. But what if you do not need this extra precision and would like to read nuclear charges in single (`np.float32`) or even reduced (e.g. `np.float16`) precision? TREXIO Python API provides an additional (optional) argument for this. This argument is called `dtype` and accepts one of the [NumPy data types](https://numpy.org/doc/stable/user/basics.types.html). For example, + + +```python +charges_np = trexio.read_nucleus_charge(demo_file_r, dtype=np.float32) +``` + + +```python +print(f"return dtype in NumPy notation: ---> {charges_np.dtype}") +``` + + return dtype in NumPy notation: ---> float32 + + +### Reading multidimensional arrays + +So far, we have only read flat 1D arrays. However, we have also written a 2D array of nuclear coordinates. Let's now read it back from the file: + + +```python +coords_r = trexio.read_nucleus_coord(demo_file_r) +``` + + +```python +print(f"nucleus_coord from {filename} TREXIO file: \n{coords_r}") +``` + + nucleus_coord from benzene_demo.h5 TREXIO file: + [[ 0. 1.39250319 0. ] + [-1.20594314 0.6962516 0. ] + [-1.20594314 -0.6962516 0. ] + [ 0. -1.39250319 0. ] + [ 1.20594314 -0.6962516 0. ] + [ 1.20594314 0.6962516 0. ] + [-2.14171677 1.23652075 0. ] + [-2.14171677 -1.23652075 0. ] + [ 0. -2.47304151 0. ] + [ 2.14171677 -1.23652075 0. ] + [ 2.14171677 1.23652075 0. ] + [ 0. 2.47304151 0. ]] + + + +```python +print(f"return shape: ---> {coords_r.shape}") +``` + + return shape: ---> (12, 3) + + +We can see that TREXIO returns a 2D array with 12 rows and 3 columns, which is consistent with the `nucleus_coord` dimensions `[nucleus_num, 3]`. What this means is that **by default TREXIO reshapes the output flat array into a multidimensional one** whenever applicable. This is done based on the shape specified in the TREX configuration file. + +In some cases, it might be a good idea to explicitly check that the data exists in the file before reading it. This can be achieved using `has_`-suffixed functions of the API. For example, + + +```python +if trexio.has_nucleus_coord(demo_file_r): + coords_safer = trexio.read_nucleus_coord(demo_file_r) +``` + +## Conclusion + +In this Tutorial, you have created a TREXIO file using HDF5 back end and have written the number of atoms, point group, nuclear charges, labels and coordinates, which correspond to benzene molecule. You have also learned how to read this data back from the TREXIO file and how to handle some TREXIO errors.