2021-08-18 12:40:27 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import numpy as np
|
|
|
|
|
2021-08-18 14:42:10 +02:00
|
|
|
import trexio as tr
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
#=========================================================#
|
|
|
|
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
|
|
|
|
#=========================================================#
|
|
|
|
|
|
|
|
# 0: TREXIO_HDF5 ; 1: TREXIO_TEXT
|
|
|
|
TEST_TREXIO_BACKEND = tr.TREXIO_TEXT
|
|
|
|
OUTPUT_FILENAME_TEXT = 'test_py_swig.dir'
|
|
|
|
OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
|
|
|
|
|
|
|
|
|
|
|
|
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
|
|
|
output_filename = OUTPUT_FILENAME_HDF5
|
|
|
|
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
|
|
|
output_filename = OUTPUT_FILENAME_TEXT
|
|
|
|
else:
|
|
|
|
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
|
|
|
os.remove(output_filename)
|
|
|
|
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
|
|
|
shutil.rmtree(output_filename)
|
|
|
|
except:
|
|
|
|
print ('Nothing to remove.')
|
|
|
|
|
|
|
|
#=========================================================#
|
|
|
|
#============ WRITE THE DATA IN THE TEST FILE ============#
|
|
|
|
#=========================================================#
|
|
|
|
|
|
|
|
test_file = tr.open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
|
|
|
|
|
|
|
print(test_file)
|
|
|
|
|
|
|
|
nucleus_num = 12
|
|
|
|
|
|
|
|
tr.write_nucleus_num(test_file, nucleus_num)
|
|
|
|
|
|
|
|
# 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.]
|
|
|
|
charges_np = np.array(charges, dtype=np.float64)
|
|
|
|
|
|
|
|
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
|
|
|
# from the size of the list/array by SWIG using typemaps from numpy.i
|
2021-08-20 12:43:15 +02:00
|
|
|
rc = tr.write_nucleus_charge(test_file, charges_np)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
# initialize arrays of nuclear indices as a list and convert it to numpy array
|
|
|
|
indices = [i for i in range(nucleus_num)]
|
|
|
|
# type cast is important here because by default numpy transforms a list of integers into int64 array
|
|
|
|
indices_np = np.array(indices, dtype=np.int32)
|
|
|
|
|
|
|
|
# function call below works with both lists and numpy arrays, dimension needed for memory-safety is derived
|
|
|
|
# from the size of the list/array by SWIG using typemacs from numpy.i
|
2021-08-20 12:43:15 +02:00
|
|
|
tr.write_basis_nucleus_index(test_file, indices_np)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
point_group = 'B3U'
|
|
|
|
|
|
|
|
tr.write_nucleus_point_group(test_file, point_group)
|
|
|
|
|
|
|
|
labels = [
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H']
|
|
|
|
|
|
|
|
tr.write_nucleus_label(test_file,labels)
|
|
|
|
|
2021-08-18 14:42:10 +02:00
|
|
|
tr.close(test_file)
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
#============ READ THE DATA FROM THE TEST FILE ============#
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
test_file2 = tr.open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
|
|
|
|
|
|
|
rnum = tr.read_nucleus_num(test_file2)
|
|
|
|
assert rnum==nucleus_num
|
|
|
|
|
|
|
|
# safe call to read_safe array of float values
|
2021-08-20 12:43:15 +02:00
|
|
|
rcharges_np = tr.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-20 12:43:15 +02:00
|
|
|
rcharges_fail = tr.read_nucleus_charge(test_file2, dim=nucleus_num*5)
|
2021-08-18 14:42:10 +02:00
|
|
|
except Exception:
|
|
|
|
print("Unsafe call to safe API: successful")
|
2021-08-18 12:40:27 +02:00
|
|
|
|
|
|
|
# safe call to read_safe array of int values
|
2021-08-20 12:43:15 +02:00
|
|
|
rindices_np = tr.read_basis_nucleus_index(test_file2, dim=nucleus_num)
|
2021-08-18 12:40:27 +02:00
|
|
|
assert rindices_np.dtype is np.dtype(np.int32)
|
|
|
|
for i in range(nucleus_num):
|
|
|
|
assert rindices_np[i]==indices_np[i]
|
|
|
|
|
2021-08-20 12:43:15 +02:00
|
|
|
rlabels_2d = tr.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]
|
|
|
|
|
|
|
|
rpoint_group = tr.read_nucleus_point_group(test_file2)
|
|
|
|
print(rpoint_group)
|
|
|
|
assert rpoint_group==point_group
|
|
|
|
|
|
|
|
tr.close(test_file2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if TEST_TREXIO_BACKEND == tr.TREXIO_HDF5:
|
|
|
|
os.remove(output_filename)
|
|
|
|
elif TEST_TREXIO_BACKEND == tr.TREXIO_TEXT:
|
|
|
|
shutil.rmtree(output_filename)
|
|
|
|
except:
|
|
|
|
print (f'No output file {output_filename} has been produced')
|
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
|