2021-08-30 17:23:24 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-08-18 12:40:27 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import numpy as np
|
|
|
|
|
2021-08-30 17:23:24 +02:00
|
|
|
import trexio
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
#=========================================================#
|
|
|
|
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
|
|
|
|
#=========================================================#
|
|
|
|
|
|
|
|
# 0: TREXIO_HDF5 ; 1: TREXIO_TEXT
|
2021-08-27 15:08:39 +02:00
|
|
|
TEST_TREXIO_BACKEND = 0
|
2021-08-18 12:40:27 +02:00
|
|
|
OUTPUT_FILENAME_TEXT = 'test_py_swig.dir'
|
|
|
|
OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
|
|
|
|
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# define TREXIO file name
|
2021-08-30 17:23:24 +02:00
|
|
|
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
2021-08-18 12:40:27 +02:00
|
|
|
output_filename = OUTPUT_FILENAME_HDF5
|
2021-08-30 17:23:24 +02:00
|
|
|
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
2021-08-18 12:40:27 +02:00
|
|
|
output_filename = OUTPUT_FILENAME_TEXT
|
|
|
|
else:
|
|
|
|
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
|
|
|
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# remove TREXIO file if exists in the current directory
|
2021-08-18 12:40:27 +02:00
|
|
|
try:
|
2021-08-30 17:23:24 +02:00
|
|
|
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
2021-08-18 12:40:27 +02:00
|
|
|
os.remove(output_filename)
|
2021-08-30 17:23:24 +02:00
|
|
|
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
2021-08-18 12:40:27 +02:00
|
|
|
shutil.rmtree(output_filename)
|
|
|
|
except:
|
2022-03-07 14:12:31 +01:00
|
|
|
print('Nothing to remove.')
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
#=========================================================#
|
|
|
|
#============ WRITE THE DATA IN THE TEST FILE ============#
|
|
|
|
#=========================================================#
|
|
|
|
|
2022-01-20 14:22:32 +01:00
|
|
|
trexio.info()
|
|
|
|
|
2022-03-07 14:12:31 +01:00
|
|
|
|
|
|
|
# test with ... as ... block
|
|
|
|
with trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND) as tfile:
|
|
|
|
trexio.write_metadata_description(tfile, "Test file produced by the Python API")
|
|
|
|
assert trexio.has_metadata_description(tfile)
|
|
|
|
assert tfile.isOpen
|
|
|
|
|
|
|
|
# the file handle can remain existing but the file itself is closed upon exit from the `with` block
|
|
|
|
assert not tfile.isOpen
|
|
|
|
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# create TREXIO file and open it for writing
|
2021-08-30 17:23:24 +02:00
|
|
|
test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
|
2022-01-19 14:08:38 +01:00
|
|
|
assert test_file.exists
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-08-30 17:23:24 +02:00
|
|
|
# Print docstring of the trexio.open function
|
|
|
|
#print(trexio.open.__doc__)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
nucleus_num = 12
|
|
|
|
|
2021-08-30 17:23:24 +02:00
|
|
|
try:
|
|
|
|
trexio.write_nucleus_num(test_file, -100)
|
|
|
|
except trexio.Error:
|
2021-09-22 16:40:55 +02:00
|
|
|
print("Raise error for an attempt to write negative nucleus_num: checked.")
|
2021-08-30 17:23:24 +02:00
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# write nucleus_num in the file
|
2021-08-30 17:23:24 +02:00
|
|
|
try:
|
|
|
|
trexio.write_nucleus_num(test_file, nucleus_num)
|
|
|
|
except:
|
|
|
|
raise
|
|
|
|
|
|
|
|
try:
|
|
|
|
trexio.write_nucleus_num(test_file, nucleus_num*2)
|
2021-09-13 11:46:31 +02:00
|
|
|
except trexio.Error:
|
2021-09-22 16:40:55 +02:00
|
|
|
print("Raise error for an attempt to overwrite nucleus_num: checked.")
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
# initialize charge arrays as a list and convert it to numpy array
|
|
|
|
charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.]
|
2021-08-26 12:14:46 +02:00
|
|
|
#charges_np = np.array(charges, dtype=np.float32)
|
|
|
|
charges_np = np.array(charges, dtype=np.int32)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-12-27 14:02:58 +01:00
|
|
|
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
2021-08-18 12:40:27 +02:00
|
|
|
# from the size of the list/array by SWIG using typemaps from numpy.i
|
2021-08-30 17:23:24 +02:00
|
|
|
trexio.write_nucleus_charge(test_file, charges_np)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-10-21 15:52:12 +02:00
|
|
|
basis_shell_num = 24
|
2021-08-18 12:40:27 +02:00
|
|
|
# initialize arrays of nuclear indices as a list and convert it to numpy array
|
2021-10-21 15:52:12 +02:00
|
|
|
indices = [i for i in range(basis_shell_num)]
|
2021-08-18 12:40:27 +02:00
|
|
|
# type cast is important here because by default numpy transforms a list of integers into int64 array
|
2021-08-26 12:14:46 +02:00
|
|
|
indices_np = np.array(indices, dtype=np.int64)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-10-21 15:52:12 +02:00
|
|
|
# first write basis_shell_num because it is needed to check dimensions of basis_nucleus_index in TREXIO >= 2.0.0
|
|
|
|
trexio.write_basis_shell_num(test_file, basis_shell_num)
|
2021-10-19 10:43:00 +02:00
|
|
|
|
2021-12-27 14:02:58 +01:00
|
|
|
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
2021-08-18 12:40:27 +02:00
|
|
|
# from the size of the list/array by SWIG using typemacs from numpy.i
|
2021-08-30 17:23:24 +02:00
|
|
|
trexio.write_basis_nucleus_index(test_file, indices_np)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-08-24 11:51:43 +02:00
|
|
|
# initialize a list of nuclear coordinates
|
|
|
|
coords = [
|
2021-09-07 17:14:23 +02:00
|
|
|
[ 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 ],
|
2021-08-24 11:51:43 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
# write coordinates in the file
|
2021-08-30 17:23:24 +02:00
|
|
|
trexio.write_nucleus_coord(test_file, coords)
|
2021-08-24 11:51:43 +02:00
|
|
|
|
2021-12-27 14:02:58 +01:00
|
|
|
|
|
|
|
# write mo_num (needed later to write sparse mo_2e_int_eri integrals)
|
|
|
|
trexio.write_mo_num(test_file, 600)
|
|
|
|
|
|
|
|
# write sparse data in the file
|
|
|
|
num_integrals = 100
|
|
|
|
indices = [i for i in range(num_integrals*4)]
|
|
|
|
values = [(3.14 + float(i)) for i in range(num_integrals)]
|
|
|
|
|
|
|
|
trexio.write_mo_2e_int_eri(test_file, 0, num_integrals, indices, values)
|
|
|
|
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# write nucleus_point_group in the file
|
2021-12-27 14:02:58 +01:00
|
|
|
point_group = 'B3U'
|
|
|
|
|
2021-08-30 17:23:24 +02:00
|
|
|
trexio.write_nucleus_point_group(test_file, point_group)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-12-27 14:02:58 +01:00
|
|
|
|
|
|
|
# write nucleus_label in the file
|
2021-08-18 12:40:27 +02:00
|
|
|
labels = [
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H']
|
|
|
|
|
2021-08-30 17:23:24 +02:00
|
|
|
trexio.write_nucleus_label(test_file,labels)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-12-27 14:02:58 +01:00
|
|
|
# close TREXIO file
|
2021-09-13 11:29:00 +02:00
|
|
|
# this call is no longer needed as we introduced TREXIO_File class which has a desctructor that closes the file
|
2021-08-30 17:23:24 +02:00
|
|
|
#trexio.close(test_file)
|
2021-12-27 14:02:58 +01:00
|
|
|
# without calling destructor on test_file the TREXIO_FILE is not getting created and the data is not written when using TEXT back end.
|
|
|
|
# This, the user still has to explicitly call destructor on test_file object instead of the trexio.close function.
|
2021-09-13 11:29:00 +02:00
|
|
|
# This is only an issue when the data is getting written and read in the same session (e.g. in Jupyter notebook)
|
2021-08-26 16:01:53 +02:00
|
|
|
del test_file
|
2021-08-26 12:14:46 +02:00
|
|
|
|
2021-08-27 15:08:39 +02:00
|
|
|
|
2022-01-24 16:29:40 +01:00
|
|
|
#==========================================================#
|
|
|
|
#========== DELETE THE GROUP FROM THE TEST FILE ===========#
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
unsafe_file = trexio.File(output_filename, 'u', TEST_TREXIO_BACKEND)
|
|
|
|
|
|
|
|
# overwrite existing data (only allowed in 'u' - unsafe mode)
|
|
|
|
trexio.write_nucleus_num(unsafe_file, nucleus_num)
|
|
|
|
trexio.write_nucleus_charge(unsafe_file, charges_np)
|
|
|
|
trexio.write_nucleus_coord(unsafe_file, coords)
|
|
|
|
trexio.write_nucleus_label(unsafe_file,labels)
|
|
|
|
trexio.write_nucleus_point_group(unsafe_file, point_group)
|
|
|
|
|
|
|
|
print("Overwriting the data in UNSAFE mode: checked")
|
|
|
|
|
|
|
|
# delete existing group (only allowed in 'u' - unsafe mode)
|
|
|
|
trexio.delete_nucleus(unsafe_file)
|
|
|
|
|
|
|
|
assert not trexio.has_nucleus_num(unsafe_file)
|
|
|
|
assert not trexio.has_nucleus_charge(unsafe_file)
|
|
|
|
assert not trexio.has_nucleus_coord(unsafe_file)
|
|
|
|
assert not trexio.has_nucleus_label(unsafe_file)
|
|
|
|
assert not trexio.has_nucleus_point_group(unsafe_file)
|
|
|
|
|
|
|
|
print("Deleting nucleus group in UNSAFE mode: checked")
|
|
|
|
|
|
|
|
# restore the deleted data
|
|
|
|
trexio.write_nucleus_num(unsafe_file, nucleus_num)
|
|
|
|
trexio.write_nucleus_charge(unsafe_file, charges_np)
|
|
|
|
trexio.write_nucleus_coord(unsafe_file, coords)
|
|
|
|
trexio.write_nucleus_label(unsafe_file,labels)
|
|
|
|
trexio.write_nucleus_point_group(unsafe_file, point_group)
|
|
|
|
|
|
|
|
del unsafe_file
|
|
|
|
|
2021-08-18 12:40:27 +02:00
|
|
|
#==========================================================#
|
|
|
|
#============ READ THE DATA FROM THE TEST FILE ============#
|
|
|
|
#==========================================================#
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# open previously created TREXIO file, now in 'read' mode
|
2022-01-25 15:00:55 +01:00
|
|
|
test_file2 = trexio.File(output_filename, 'r', trexio.TREXIO_AUTO)
|
2022-01-19 14:08:38 +01:00
|
|
|
assert test_file2.exists
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-09-13 11:29:00 +02:00
|
|
|
# check for existence of some of the previously written variables
|
2022-01-24 16:29:40 +01:00
|
|
|
assert trexio.has_nucleus_num(test_file2)
|
|
|
|
assert trexio.has_nucleus_charge(test_file2)
|
|
|
|
assert trexio.has_nucleus_coord(test_file2)
|
|
|
|
assert trexio.has_nucleus_label(test_file2)
|
|
|
|
assert trexio.has_nucleus_point_group(test_file2)
|
|
|
|
assert trexio.has_mo_2e_int_eri(test_file2)
|
2021-09-13 11:29:00 +02:00
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# read nucleus_num from file
|
2021-08-30 17:23:24 +02:00
|
|
|
rnum = trexio.read_nucleus_num(test_file2)
|
2021-08-18 12:40:27 +02:00
|
|
|
assert rnum==nucleus_num
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# safe call to read_nucleus_charge array of float values
|
2021-08-30 17:23:24 +02:00
|
|
|
rcharges_np = trexio.read_nucleus_charge(test_file2, dim=nucleus_num)
|
2021-08-18 12:40:27 +02:00
|
|
|
assert rcharges_np.dtype is np.dtype(np.float64)
|
|
|
|
np.testing.assert_array_almost_equal(rcharges_np, charges_np, decimal=8)
|
|
|
|
|
2021-08-18 14:42:10 +02:00
|
|
|
# unsafe call to read_safe should fail with error message corresponding to TREXIO_UNSAFE_ARRAY_DIM
|
|
|
|
try:
|
2021-08-30 17:23:24 +02:00
|
|
|
rcharges_fail = trexio.read_nucleus_charge(test_file2, dim=nucleus_num*5)
|
2021-09-13 11:46:31 +02:00
|
|
|
except trexio.Error:
|
2021-08-21 12:14:59 +02:00
|
|
|
print("Unsafe call to safe API: checked")
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# safe call to read array of int values (nuclear indices)
|
2021-10-21 15:52:12 +02:00
|
|
|
rindices_np_16 = trexio.read_basis_nucleus_index(test_file2, dim=basis_shell_num, dtype=np.int16)
|
2021-08-26 12:14:46 +02:00
|
|
|
assert rindices_np_16.dtype is np.dtype(np.int16)
|
2021-10-21 15:52:12 +02:00
|
|
|
for i in range(basis_shell_num):
|
2021-08-26 12:14:46 +02:00
|
|
|
assert rindices_np_16[i]==indices_np[i]
|
|
|
|
|
2021-10-21 15:52:12 +02:00
|
|
|
rindices_np_32 = trexio.read_basis_nucleus_index(test_file2, dim=basis_shell_num, dtype=np.int32)
|
2021-08-26 12:14:46 +02:00
|
|
|
assert rindices_np_32.dtype is np.dtype(np.int32)
|
2021-10-21 15:52:12 +02:00
|
|
|
for i in range(basis_shell_num):
|
2021-08-26 12:14:46 +02:00
|
|
|
assert rindices_np_32[i]==indices_np[i]
|
|
|
|
|
2021-08-30 17:23:24 +02:00
|
|
|
rindices_np_64 = trexio.read_basis_nucleus_index(test_file2)
|
2021-08-26 12:14:46 +02:00
|
|
|
assert rindices_np_64.dtype is np.dtype(np.int64)
|
2021-10-21 15:52:12 +02:00
|
|
|
assert rindices_np_64.size==basis_shell_num
|
|
|
|
for i in range(basis_shell_num):
|
2021-08-26 12:14:46 +02:00
|
|
|
assert rindices_np_64[i]==indices_np[i]
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-08-24 11:51:43 +02:00
|
|
|
# read nuclear coordinates without providing optional argument dim
|
2021-08-30 17:23:24 +02:00
|
|
|
rcoords_np = trexio.read_nucleus_coord(test_file2)
|
2021-08-27 15:08:39 +02:00
|
|
|
|
2021-08-24 11:51:43 +02:00
|
|
|
assert rcoords_np.size==nucleus_num*3
|
2021-08-27 15:08:39 +02:00
|
|
|
np.testing.assert_array_almost_equal(rcoords_np, np.array(coords).reshape(nucleus_num,3), decimal=8)
|
|
|
|
|
|
|
|
# set doReshape to False to get a flat 1D array (e.g. when reading matrices like nuclear coordinates)
|
2021-08-30 17:23:24 +02:00
|
|
|
#rcoords_reshaped_2 = trexio.read_nucleus_coord(test_file2, doReshape=False)
|
2021-08-24 11:51:43 +02:00
|
|
|
|
2021-12-27 14:02:58 +01:00
|
|
|
# read number of integrals already present in the file
|
|
|
|
assert trexio.has_mo_2e_int_eri(test_file2)
|
|
|
|
assert trexio.read_mo_2e_int_eri_size(test_file2)==num_integrals
|
|
|
|
|
|
|
|
# read sparse arrays on mo_2e_int_eri integrals
|
|
|
|
buf_size = 60
|
|
|
|
offset_file = 0
|
|
|
|
# read full buf_size (i.e. the one that does not reach EOF)
|
|
|
|
indices_sparse_np, value_sparse_np, read_buf_size, eof = trexio.read_mo_2e_int_eri(test_file2, offset_file, buf_size)
|
|
|
|
print(f'First complete sparse read size: {read_buf_size}')
|
|
|
|
#print(indices_sparse_np)
|
|
|
|
assert not eof
|
|
|
|
assert read_buf_size==buf_size
|
|
|
|
assert indices_sparse_np[0][0]==0
|
|
|
|
assert indices_sparse_np[read_buf_size-1][3]==read_buf_size*4-1
|
|
|
|
offset_file += buf_size
|
|
|
|
|
|
|
|
# read incomplete buf_size (i.e. the one that does reach EOF)
|
|
|
|
indices_sparse_np, value_sparse_np, read_buf_size, eof2 = trexio.read_mo_2e_int_eri(test_file2, offset_file, buf_size)
|
|
|
|
print(f'Second incomplete sparse read size: {read_buf_size}')
|
|
|
|
#print(indices_sparse_np)
|
|
|
|
assert eof2
|
|
|
|
assert read_buf_size==(num_integrals - buf_size)
|
|
|
|
assert indices_sparse_np[0][0]==offset_file*4
|
|
|
|
assert indices_sparse_np[read_buf_size-1][3]==(offset_file+read_buf_size)*4-1
|
|
|
|
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# read array of nuclear labels
|
2021-08-30 17:23:24 +02:00
|
|
|
rlabels_2d = trexio.read_nucleus_label(test_file2, dim=nucleus_num)
|
2021-08-18 12:40:27 +02:00
|
|
|
print(rlabels_2d)
|
|
|
|
for i in range(nucleus_num):
|
|
|
|
assert rlabels_2d[i]==labels[i]
|
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# read a string corresponding to nuclear point group
|
2021-08-30 17:23:24 +02:00
|
|
|
rpoint_group = trexio.read_nucleus_point_group(test_file2)
|
2021-12-27 14:02:58 +01:00
|
|
|
assert rpoint_group==point_group
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-09-13 11:29:00 +02:00
|
|
|
# another way to read only if the variable exists
|
2021-12-27 14:02:58 +01:00
|
|
|
if trexio.has_ao_num(test_file2):
|
2022-03-07 14:12:31 +01:00
|
|
|
rao_num = trexio.read_ao_num(test_file2)
|
2021-09-13 11:29:00 +02:00
|
|
|
else:
|
2021-12-27 14:02:58 +01:00
|
|
|
print("Pass on reading the non-existing variable ao_num: checked")
|
2021-09-13 11:29:00 +02:00
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# close TREXIO file
|
2021-08-30 17:23:24 +02:00
|
|
|
#trexio.close(test_file2)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
2021-08-21 12:14:59 +02:00
|
|
|
# cleaning (remove the TREXIO file)
|
2021-08-18 12:40:27 +02:00
|
|
|
try:
|
2022-03-07 14:12:31 +01:00
|
|
|
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
|
|
|
os.remove(output_filename)
|
|
|
|
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
|
|
|
shutil.rmtree(output_filename)
|
2021-08-18 12:40:27 +02:00
|
|
|
except:
|
2022-03-07 14:12:31 +01:00
|
|
|
print(f'No output file {output_filename} has been produced')
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
|
2021-09-22 16:40:55 +02:00
|
|
|
#==========================================================#
|
|
|
|
#======= OPEN NON-EXISTING FILE TO TEST TREXIO.OPEN =======#
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
try:
|
|
|
|
void_file = trexio.File('non_existing.file', 'r', TEST_TREXIO_BACKEND)
|
|
|
|
except trexio.Error as e:
|
|
|
|
if e.error == trexio.TREXIO_OPEN_ERROR:
|
|
|
|
print("Opening non-existing file returns TREXIO_OPEN_ERROR: checked")
|
|
|
|
else:
|
|
|
|
raise ValueError("[DEV]: error handling of trexio_open function has changed; check the consistency")
|
|
|
|
|
|
|
|
#==========================================================#
|