mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-11-03 20:54:07 +01:00
make dim an optional argument in read_ functions
if dim is None - the function read all necessary dimensions from the TREXIO file
This commit is contained in:
parent
c7565e9dec
commit
2911e91941
@ -53,7 +53,7 @@ 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
|
||||
rc = tr.write_nucleus_charge(test_file, charges_np)
|
||||
tr.write_nucleus_charge(test_file, charges_np)
|
||||
|
||||
# initialize arrays of nuclear indices as a list and convert it to numpy array
|
||||
indices = [i for i in range(nucleus_num)]
|
||||
@ -64,6 +64,25 @@ indices_np = np.array(indices, dtype=np.int32)
|
||||
# from the size of the list/array by SWIG using typemacs from numpy.i
|
||||
tr.write_basis_nucleus_index(test_file, indices_np)
|
||||
|
||||
# initialize a list of nuclear coordinates
|
||||
coords = [
|
||||
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 ,
|
||||
]
|
||||
|
||||
# write coordinates in the file
|
||||
tr.write_nucleus_coord(test_file, coords)
|
||||
|
||||
point_group = 'B3U'
|
||||
|
||||
# write nucleus_point_group in the file
|
||||
@ -117,6 +136,10 @@ assert rindices_np.dtype is np.dtype(np.int32)
|
||||
for i in range(nucleus_num):
|
||||
assert rindices_np[i]==indices_np[i]
|
||||
|
||||
# read nuclear coordinates without providing optional argument dim
|
||||
rcoords_np = tr.read_nucleus_coord(test_file2)
|
||||
assert rcoords_np.size==nucleus_num*3
|
||||
|
||||
# read array of nuclear labels
|
||||
rlabels_2d = tr.read_nucleus_label(test_file2, dim=nucleus_num)
|
||||
print(rlabels_2d)
|
||||
|
@ -1860,7 +1860,7 @@ def write_$group_dset$(trexio_file, dset_w) -> None:
|
||||
#+end_src
|
||||
|
||||
#+begin_src python :tangle read_dset_data_front.py
|
||||
def read_$group_dset$(trexio_file, dim: int):
|
||||
def read_$group_dset$(trexio_file, dim = None):
|
||||
"""Read the $group_dset$ array of numbers from the TREXIO file.
|
||||
|
||||
Parameters:
|
||||
@ -1868,8 +1868,9 @@ def read_$group_dset$(trexio_file, dim: int):
|
||||
trexio_file:
|
||||
TREXIO file handle.
|
||||
|
||||
dim: int
|
||||
dim (Optional): int
|
||||
Size of the block to be read from the file (i.e. how many items of $group_dset$ will be returned)
|
||||
If None, the function will read all necessary array dimensions from the file.
|
||||
|
||||
Returns:
|
||||
~dset_r~: numpy.ndarray
|
||||
@ -1880,6 +1881,16 @@ def read_$group_dset$(trexio_file, dim: int):
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
|
||||
# if dim is not specified, read dimensions from the TREXIO file
|
||||
if dim is None:
|
||||
$group_dset_dim$ = read_$group_dset_dim$(trexio_file)
|
||||
|
||||
dims_list = [$group_dset_dim_list$]
|
||||
dim = 1
|
||||
for i in range($group_dset_rank$):
|
||||
dim *= dims_list[i]
|
||||
|
||||
|
||||
try:
|
||||
rc, dset_r = trexio_read_safe_$group_dset$(trexio_file, dim)
|
||||
@ -2381,7 +2392,7 @@ def write_$group_dset$(trexio_file, dset_w: list) -> None:
|
||||
#+end_src
|
||||
|
||||
#+begin_src python :tangle read_dset_str_front.py
|
||||
def read_$group_dset$(trexio_file, dim: int) -> list:
|
||||
def read_$group_dset$(trexio_file, dim = None) -> list:
|
||||
"""Read the $group_dset$ array of strings from the TREXIO file.
|
||||
|
||||
Parameters:
|
||||
@ -2389,8 +2400,9 @@ def read_$group_dset$(trexio_file, dim: int) -> list:
|
||||
trexio_file:
|
||||
TREXIO file handle.
|
||||
|
||||
dim: int
|
||||
dim (Optional): int
|
||||
Size of the block to be read from the file (i.e. how many items of $group_dset$ will be returned)
|
||||
If None, the function will read all necessary array dimensions from the file.
|
||||
|
||||
Returns:
|
||||
~dset_r~: list
|
||||
@ -2401,6 +2413,17 @@ def read_$group_dset$(trexio_file, dim: int) -> list:
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
|
||||
# if dim is not specified, read dimensions from the TREXIO file
|
||||
if dim is None:
|
||||
$group_dset_dim$ = read_$group_dset_dim$(trexio_file)
|
||||
|
||||
dims_list = [$group_dset_dim_list$]
|
||||
dim = 1
|
||||
for i in range($group_dset_rank$):
|
||||
dim *= dims_list[i]
|
||||
|
||||
|
||||
try:
|
||||
rc, dset_1d_r = trexio_read_$group_dset$_low(trexio_file, PYTREXIO_MAX_STR_LENGTH)
|
||||
assert rc==TREXIO_SUCCESS
|
||||
@ -2409,6 +2432,7 @@ def read_$group_dset$(trexio_file, dim: int) -> list:
|
||||
except:
|
||||
raise
|
||||
|
||||
|
||||
try:
|
||||
dset_full = dset_1d_r.split(TREXIO_DELIM)
|
||||
dset_2d_r = [dset_full[i] for i in range(dim) if dset_full[i]]
|
||||
|
Loading…
Reference in New Issue
Block a user