1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-08-24 22:21:43 +02:00

document test_api file

This commit is contained in:
q-posev 2021-08-21 13:14:59 +03:00
parent 6f7c23aa11
commit f30c6f60a6

View File

@ -14,6 +14,7 @@ OUTPUT_FILENAME_TEXT = 'test_py_swig.dir'
OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
# define TREXIO file name
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
output_filename = OUTPUT_FILENAME_HDF5
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
@ -22,6 +23,7 @@ else:
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
# remove TREXIO file if exists in the current directory
try:
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
os.remove(output_filename)
@ -34,12 +36,15 @@ except:
#============ WRITE THE DATA IN THE TEST FILE ============#
#=========================================================#
# create TREXIO file and open it for writing
test_file = tr.open(output_filename, 'w', TEST_TREXIO_BACKEND)
print(test_file)
# Print docstring of the tr.open function
#print(tr.open.__doc__)
nucleus_num = 12
# write nucleus_num in the file
tr.write_nucleus_num(test_file, nucleus_num)
# initialize charge arrays as a list and convert it to numpy array
@ -61,6 +66,7 @@ tr.write_basis_nucleus_index(test_file, indices_np)
point_group = 'B3U'
# write nucleus_point_group in the file
tr.write_nucleus_point_group(test_file, point_group)
labels = [
@ -77,20 +83,24 @@ labels = [
'H',
'H']
# write nucleus_label in the file
tr.write_nucleus_label(test_file,labels)
# close TREXIO file
tr.close(test_file)
#==========================================================#
#============ READ THE DATA FROM THE TEST FILE ============#
#==========================================================#
# open previously created TREXIO file, now in 'read' mode
test_file2 = tr.open(output_filename, 'r', TEST_TREXIO_BACKEND)
# read nucleus_num from file
rnum = tr.read_nucleus_num(test_file2)
assert rnum==nucleus_num
# safe call to read_safe array of float values
# safe call to read_nucleus_charge array of float values
rcharges_np = tr.read_nucleus_charge(test_file2, dim=nucleus_num)
assert rcharges_np.dtype is np.dtype(np.float64)
np.testing.assert_array_almost_equal(rcharges_np, charges_np, decimal=8)
@ -99,25 +109,28 @@ np.testing.assert_array_almost_equal(rcharges_np, charges_np, decimal=8)
try:
rcharges_fail = tr.read_nucleus_charge(test_file2, dim=nucleus_num*5)
except Exception:
print("Unsafe call to safe API: successful")
print("Unsafe call to safe API: checked")
# safe call to read_safe array of int values
# safe call to read array of int values (nuclear indices)
rindices_np = tr.read_basis_nucleus_index(test_file2, dim=nucleus_num)
assert rindices_np.dtype is np.dtype(np.int32)
for i in range(nucleus_num):
assert rindices_np[i]==indices_np[i]
# read array of nuclear labels
rlabels_2d = tr.read_nucleus_label(test_file2, dim=nucleus_num)
print(rlabels_2d)
for i in range(nucleus_num):
assert rlabels_2d[i]==labels[i]
# read a string corresponding to nuclear point group
rpoint_group = tr.read_nucleus_point_group(test_file2)
print(rpoint_group)
assert rpoint_group==point_group
# close TREXIO file
tr.close(test_file2)
# cleaning (remove the TREXIO file)
try:
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
os.remove(output_filename)