5.3. Dumping data to file
The current version of PyBEST supports the following file formats to store data on disk,
File format |
Description
|
---|---|
|
PyBEST’s internal format. This format allows you to store
all PyBEST objects in binary form. All checkpoint files
are dump using the h5 file format. They can be used to
perform restarts.
|
|
Store molecular coordinates in an xyz file. By default,
all coordinates are converted to Angstrom.
|
|
Store orbitals and basis set information in the molden
file format. PyBEST is interfaced to libint2’s molden
reader. This works for basis sets that include up to g
functions.
|
|
Dump Hamiltonian (including all external terms) in the
Molpro FCIDUMP format. All one-electron integrals have
to be transformed in the molecular orbital basis.
Furthermore, all one-electron terms have to be provided
as one single term.
|
In order to dump data using one of the above mentioned file formats, all data
has to be assigned to an IOData
container using
the attribute names defined in Naming conventions in PyBEST.
Note
If you choose different variable names, some PyBEST operations are not fully supported and some features might break.
Once filled, the to_file()
method stores
all information contained in the IOData
container
to disk. The file format is determined automatically through the file extension,
# Dump IOData container to internal file format
# ---------------------------------------------
data.to_file('checkpoint.h5')
Changing the file extension to one of the supported file formats mentioned above will steer PyBEST’s dumping behavior.
5.3.1. Filling the IOData
container
In principle, the IOData
constructor accepts any
keyword arguments, which is then stored as an attribute. All attributes are
optional. Thus, even an empty IOData
container
can be initialized. The IOData
container is
rather felixble: once constructed, attributes can be set and removed on the fly.
The code snippet below shows how to assign, modify, and delete attributes
(all objects are defined in Naming conventions in PyBEST),
# Assign to IOData using constructor
# ----------------------------------
data = IOData(kin=kin, ne=ne, eri=eri, e_core=e_core)
# Update IOData: add new attribute (orbitals)
# -------------------------------------------
data.orb_a = orb
# Update IOData: delete attribute (orbitals)
# ------------------------------------------
del data.orb_a
# Update IOData: modify attribute (core energy)
# ---------------------------------------------
data.e_core = 5.0
5.3.2. Dumping to the internal h5 format
The example below summarizes all steps to generate a PyBEST internal checkpoint
file using the file extension .h5
,
# Assign to IOData using constructor
# ----------------------------------
data = IOData(kin=kin, ne=ne, eri=eri, e_core=e_core)
# Dump to internal checkpoint file
# --------------------------------
data.to_file("checkpoint.h5")
5.3.3. Dumping an xyz file
The example below, summarizes all steps to export the coordinates into an xyz
file.
First, the atoms and the coordinates need to be stored in the
IOData
container. This information is stored
in an Basis
instance (here gobasis
)
and can be accessed
using the attributes atom
and coordinates
, respectively,
# Construct IOData container for xyz
# Both atoms and coordinates are stored in gobasis
# ------------------------------------------------
data = IOData(atom=gobasis.atom, coordinates=gobasis.coordinates)
# Dump to xyz file
# ----------------
data.to_file("mol.xyz")
5.3.4. Dumping a molden file
A detailed instruction on how to export orbitals to the molden format can be found in Generating molden files. The example below, briefly summarizes how to dump the orbitals, coordinates, and atomic basis set to disk using the molden format,
# First, construct IOData container, include gobasis and orbitals as attribute
data = IOData(gobasis=gobasis, orb_a=orb)
# Now, we can write the molden file
# ---------------------------------
data.to_file("water-scf.molden")
5.3.5. Dumping to the FCIDUMP format
A detailed instruction on how to export a Hamiltonian into the FCIDUMP format can be found in FCIDUMP format. The example below, briefly summarizes how to dump the Hamiltonian in the FCIDUMP format including the transformation from the atomic to the molecular orbital basis,
# Write to a FCIDUMP file
# -----------------------
data = IOData(one=one, two=two, e_core=e_core, nelec=20, ms2=0)
data.to_file("hamiltonian_mo.FCIDUMP")
5.3.6. Example Python scripts
Several complete examples can be found in the directory data/examples/iodata
.
5.3.6.1. Summary of all supported dumping options
This is a basic example that summarizes all steps mentioned above, namely, how
to construct, modify, and store data to the internal .h5
, the .xyz
,
the .molden
, and the FCIDUMP
format.
In the example below, a restricted Hartree-Fock calculation is performed in
order to generate some molecular orbitals (see The Self-Consistent Field Module for a detailed
instruction on Hartree-Fock calculations).
from pybest import context
from pybest.gbasis import get_gobasis
from pybest.gbasis.gobasis import (
compute_eri,
compute_kinetic,
compute_nuclear,
compute_nuclear_repulsion,
compute_overlap,
)
from pybest.io.iodata import IOData
from pybest.linalg.dense import DenseLinalgFactory
from pybest.orbital_utils import transform_integrals
from pybest.scf import AufbauOccModel
from pybest.wrappers.hf import RHF
# Define coordinate file.
# ----------------------------------------------------
coord = context.get_fn("test/water.xyz")
# Create a Gaussian basis set
# ---------------------------
gobasis = get_gobasis("cc-pvdz", coord)
lf = DenseLinalgFactory(gobasis.nbasis)
occ_model = AufbauOccModel(5)
orb = lf.create_orbital()
# Construct Hamiltonian
# ---------------------
kin = compute_kinetic(gobasis)
ne = compute_nuclear(gobasis)
eri = compute_eri(gobasis)
e_core = compute_nuclear_repulsion(gobasis)
olp = compute_overlap(gobasis)
# Get Hartree-Fock MOs
# --------------------
hf = RHF(lf, occ_model)
hf_output = hf(kin, ne, eri, e_core, orb, olp)
# Assign to IOData using constructor
# ----------------------------------
data = IOData(kin=kin, ne=ne, eri=eri, e_core=e_core)
# Update IOData: add new attribute (orbitals)
# -------------------------------------------
data.orb_a = orb
# Update IOData: delete attribute (orbitals)
# ------------------------------------------
del data.orb_a
# Update IOData: modify attribute (core energy)
# ---------------------------------------------
data.e_core = 5.0
# Print content of IOData
# -----------------------
print(data.__dict__)
# Dump to internal checkpoint file
# --------------------------------
data.to_file("checkpoint.h5")
# Construct IOData container for xyz
# Both atoms and coordinates are stored in gobasis
# ------------------------------------------------
data = IOData(atom=gobasis.atom, coordinates=gobasis.coordinates)
# Dump to xyz file
# ----------------
data.to_file("mol.xyz")
# Write SCF results to a molden file
# ----------------------------------
# First, construct IOData container, include gobasis and orbitals as attribute
data = IOData(gobasis=gobasis, orb_a=orb)
# Now, we can write the molden file
# ---------------------------------
data.to_file("water-scf.molden")
# Transform Hamiltinian to MO basis
# ---------------------------------
# transform integrals for restricted orbitals orb
t_ints = transform_integrals(kin, ne, eri, orb)
# transformed one-electron integrals: attribute 'one' (list)
(one,) = t_ints.one # or: one = ti_.one[0]
# transformed two-electron integrals: attribute 'two' (list)
(two,) = t_ints.two # or: two = ti_.two[0]
# Write to a FCIDUMP file
# -----------------------
data = IOData(one=one, two=two, e_core=e_core, nelec=20, ms2=0)
data.to_file("hamiltonian_mo.FCIDUMP")