2021-07-19 17:59:26 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
from pytrexio import *
|
|
|
|
|
|
|
|
#=========================================================#
|
|
|
|
#======== SETUP THE BACK END AND OUTPUT FILE NAME ========#
|
|
|
|
#=========================================================#
|
|
|
|
|
|
|
|
# 0: TREXIO_HDF5 ; 1: TREXIO_TEXT
|
2021-07-26 19:27:45 +02:00
|
|
|
TEST_TREXIO_BACKEND = TREXIO_TEXT
|
2021-07-19 17:59:26 +02:00
|
|
|
OUTPUT_FILENAME_TEXT = 'test_py_swig.dir'
|
|
|
|
OUTPUT_FILENAME_HDF5 = 'test_py_swig.h5'
|
|
|
|
|
|
|
|
|
2021-07-26 19:27:45 +02:00
|
|
|
if TEST_TREXIO_BACKEND == TREXIO_HDF5:
|
2021-07-19 17:59:26 +02:00
|
|
|
output_filename = OUTPUT_FILENAME_HDF5
|
2021-07-26 19:27:45 +02:00
|
|
|
elif TEST_TREXIO_BACKEND == TREXIO_TEXT:
|
2021-07-19 17:59:26 +02:00
|
|
|
output_filename = OUTPUT_FILENAME_TEXT
|
|
|
|
else:
|
2021-07-26 19:27:45 +02:00
|
|
|
raise ValueError ('Specify one of the supported back ends as TEST_TREXIO_BACKEND')
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
2021-07-26 19:27:45 +02:00
|
|
|
if TEST_TREXIO_BACKEND == TREXIO_HDF5:
|
2021-07-19 17:59:26 +02:00
|
|
|
os.remove(output_filename)
|
2021-07-26 19:27:45 +02:00
|
|
|
elif TEST_TREXIO_BACKEND == TREXIO_TEXT:
|
2021-07-19 17:59:26 +02:00
|
|
|
shutil.rmtree(output_filename)
|
|
|
|
except:
|
2021-07-26 18:49:26 +02:00
|
|
|
print (f'Test file {output_filename} does not exist')
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
#=========================================================#
|
|
|
|
#============ WRITE THE DATA IN THE TEST FILE ============#
|
|
|
|
#=========================================================#
|
|
|
|
|
|
|
|
test_file = trexio_open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
|
|
|
|
|
|
|
nucleus_num = 12
|
|
|
|
|
|
|
|
rc = trexio_write_nucleus_num(test_file, nucleus_num)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
charges = doubleArray(nucleus_num)
|
|
|
|
for i in range(nucleus_num):
|
|
|
|
if i < nucleus_num/2:
|
|
|
|
charges[i] = 6.
|
|
|
|
else:
|
|
|
|
charges[i] = 1.
|
|
|
|
|
|
|
|
rc = trexio_write_nucleus_charge(test_file, charges)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
point_group = 'B3U'
|
|
|
|
|
|
|
|
rc = trexio_write_nucleus_point_group(test_file, point_group, 10)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
labels = [
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'C',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H',
|
|
|
|
'H']
|
|
|
|
|
|
|
|
rc = trexio_write_nucleus_label(test_file, labels, 10)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
rc = trexio_close(test_file)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
#============ READ THE DATA FROM THE TEST FILE ============#
|
|
|
|
#==========================================================#
|
|
|
|
|
|
|
|
test_file2 = trexio_open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
|
|
|
|
|
|
|
result = trexio_read_nucleus_num(test_file2)
|
|
|
|
assert result[0]==0
|
|
|
|
assert result[1]==nucleus_num
|
|
|
|
#print(result)
|
|
|
|
|
|
|
|
charges2 = doubleArray(nucleus_num)
|
|
|
|
for i in range(nucleus_num):
|
|
|
|
charges2[i] = -1.
|
|
|
|
|
|
|
|
rc = trexio_read_nucleus_charge(test_file2, charges2)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
for i in range(nucleus_num):
|
|
|
|
assert charges2[i]==charges[i]
|
|
|
|
|
2021-07-26 18:49:26 +02:00
|
|
|
# [WIP]: ideally, the list of strings should be returned as below
|
|
|
|
#rc, label_2d = trexio_read_nucleus_label(test_file2, 10)
|
|
|
|
# [WIP]: currently only low-level routines (return one long string instead of an array of strings) work
|
|
|
|
rc, labels_1d = trexio_read_nucleus_label_low(test_file2, 10)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-26 18:49:26 +02:00
|
|
|
|
2021-07-26 19:27:45 +02:00
|
|
|
labels_2d = [label for label in labels_1d.split(TREXIO_DELIM) if label]
|
2021-07-26 18:49:26 +02:00
|
|
|
print(labels_2d)
|
2021-07-19 17:59:26 +02:00
|
|
|
for i in range(nucleus_num):
|
2021-07-26 18:49:26 +02:00
|
|
|
assert labels_2d[i]==labels[i]
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
rc = trexio_close(test_file2)
|
2021-07-26 19:27:45 +02:00
|
|
|
assert rc==TREXIO_SUCCESS
|
2021-07-19 17:59:26 +02:00
|
|
|
|
|
|
|
#==========================================================#
|
|
|
|
|