1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-11-19 12:32:40 +01:00

First commit

This commit is contained in:
q-posev 2022-05-02 13:39:22 +02:00
parent 5b6f530255
commit 036fcc6fda
9 changed files with 5787 additions and 0 deletions

182
process.py Normal file
View File

@ -0,0 +1,182 @@
import os
collect = False
process = False
get_name = False
block = []
res_str = ''
func_name = ''
arrays = {}
numbers = {}
qmckl_public_api = []
with open("include/qmckl.h", 'r') as f_in:
for line in f_in:
if get_name:
words = line.strip().split()
if '(' in words[0]:
func_name = words[0].split('(')[0]
else:
func_name = words[0]
if 'get' in func_name or 'set' in func_name:
qmckl_public_api.append(func_name)
get_name = False
if 'qmckl_exit_code' in line:
words = line.strip().split()
if len(words) > 1 and 'qmckl_exit_code' in words[0]:
# this means that the function name is on the same line as `qmckl_exit_code`
func_name = words[1].split('(')[0]
if 'get' in func_name or 'set' in func_name:
qmckl_public_api.append(func_name)
elif len(words) == 1:
# this means that the function name is the first element on the next line
get_name = True
#continue # do not `continue` here otherwise collect is not True for some functions
# process functions - oneliners (for arrays)
if 'size_max' in line and ';' in line:
tmp_list = line.split(',')
for i,s in enumerate(tmp_list):
if 'size_max' in s:
end_str = tmp_list[i].replace(';','').replace('\n','')
pattern = f"({tmp_list[i-1]} ,{end_str}"
datatype = tmp_list[i-1].replace('const','').replace('*','').split()[0]
arrays[func_name] = {
'datatype' : datatype,
'pattern' : pattern
}
#if 'qmckl_get_jastrow_type_nucl_vector' in func_name:
# print(line)
# print(pattern)
continue
# if size_max is not provided then the function should deal with numbers or string
#elif 'num' in line and 'get' in func_name:
elif ';' in line and 'get' in func_name:
# special case
if 'size_max' in line:
continue
#print(line)
tmp_str = line.split(',')[-1].strip()
pattern = tmp_str.replace(')','').replace(';','')
datatype = pattern.replace('const','').replace('*','').split()[0]
numbers[func_name] = {
'datatype' : datatype,
'pattern' : pattern
}
continue
# for multilne functions - append line by line to the list
else:
block.append(line)
collect = True
continue
# if size_max is encountered within the multiline function
if 'size_max' in line and collect:
#if 'qmckl_get_electron_rescale_factor_en' in func_name:
# print("LOL")
# this will not work for 2-line functions where array argument is on the same line as
# func name and size_max argument is on the next line
if not 'qmckl_exit_code' in block[-1] and not '*/' in line:
pattern = '(' + block[-1].strip() + line.strip().replace(';','')
datatype = pattern.replace('const','').replace('*','').replace('(','').split()[0]
collect = False
block = []
arrays[func_name] = {
'datatype' : datatype,
'pattern' : pattern
}
continue
#if 'num' in line and 'get' in func_name and not 'qmckl_get' in line and collect:
if 'get' in func_name and not 'qmckl_get' in line and collect and ';' in line:
print(func_name)
print(line)
pattern = line.replace(';','').replace(')','').strip()
datatype = pattern.replace('const','').replace('*','').split()[0]
collect = False
block = []
numbers[func_name] = {
'datatype' : datatype,
'pattern' : pattern
}
continue
# stop/continue multiline function analyzer
if collect and ')' in line:
collect = False
block = []
continue
else:
block.append(line)
continue
# remove buggy qmckl_get_electron_rescale_factor_en key
#arrays.pop('qmckl_get_electron_rescale_factor_en')
processed = list(arrays.keys()) + list(numbers.keys())
for pub_func in qmckl_public_api:
if pub_func not in processed and 'set' not in pub_func:
print("TODO", pub_func)
#print(v['datatype'])
for k,v in numbers.items():
print(v)
with open("python/pyqmckl_include.i", 'w') as f_out:
swig_type = ''
for v in numbers.values():
if 'int' in v['datatype']:
swig_type = 'int'
elif 'float' in v['datatype'] or 'double' in v['datatype']:
swig_type = 'float'
elif 'char' in v['datatype'] or 'bool' in v['datatype']:
print('SWIG, skipping')
continue
else:
raise TypeError(f"Unknown datatype for swig conversion: {v['datatype']}")
f_out.write(f"%apply {swig_type} *OUTPUT {{ {v['pattern']} }};\n")
for k,v in arrays.items():
if 'char' in v['datatype']:
print("String type")
pass
if len(v['pattern'].split(',')) != 2:
print('Problemo', k, v)
continue
if 'get' in k:
f_out.write(f"%apply ( {v['datatype']}* ARGOUT_ARRAY1 , int64_t DIM1 ) {{ {v['pattern']} }};\n")
elif 'set' in k:
f_out.write(f"%apply ( {v['datatype']}* IN_ARRAY1 , int64_t DIM1 ) {{ {v['pattern']} }};\n")
else:
print("HOW-TO ?", k)

18
python/build_pyqmckl.sh Executable file
View File

@ -0,0 +1,18 @@
set -e
set -x
source export_files.sh
cp qmckl.h pyqmckl.i numpy.i src/
cd src/
swig -python -py3 -o pyqmckl_wrap.c pyqmckl.i
gcc -c -fPIC -I/usr/include/python3.8 ${C_FILES} pyqmckl_wrap.c -ltrexio
gfortran -c -fPIC -I/usr/include/python3.8 ${F_FILES}
gcc -shared ${C_O_FILES} pyqmckl_wrap.o -o _pyqmckl.so

11
python/export_files.sh Normal file
View File

@ -0,0 +1,11 @@
export C_FILES="qmckl_electron.c qmckl_nucleus.c qmckl_determinant.c qmckl_error.c qmckl_verificarlo.c qmckl_jastrow.c qmckl_sherman_morrison_woodbury.c qmckl_trexio.c qmckl_numprec.c qmckl_context.c qmckl_mo.c qmckl_blas.c qmckl_memory.c qmckl_local_energy.c qmckl_point.c qmckl_ao.c"
export F_FILES="qmckl_electron_f.F90 qmckl_distance_f.F90 qmckl_nucleus_f.F90 qmckl_determinant_f.F90 qmckl_verificarlo_f.F90 qmckl_jastrow_f.F90 qmckl_mo_f.F90 qmckl_blas_f.F90 qmckl_local_energy_f.F90 qmckl_ao_f.F90"
export C_O_FILES="qmckl_electron.o qmckl_nucleus.o qmckl_determinant.o qmckl_error.o qmckl_verificarlo.o qmckl_jastrow.o qmckl_sherman_morrison_woodbury.o qmckl_trexio.o qmckl_numprec.o qmckl_context.o qmckl_mo.o qmckl_blas.o qmckl_memory.o qmckl_local_energy.o qmckl_point.o qmckl_ao.o"
export FH_FUNC_FILES="qmckl_electron_fh_func.F90 qmckl_distance_fh_func.F90 qmckl_nucleus_fh_func.F90 qmckl_error_fh_func.F90 qmckl_sherman_morrison_woodbury_fh_func.F90 qmckl_trexio_fh_func.F90 qmckl_numprec_fh_func.F90 qmckl_context_fh_func.F90 qmckl_mo_fh_func.F90 qmckl_blas_fh_func.F90 qmckl_point_fh_func.F90 qmckl_ao_fh_func.F90"
export FH_TYPE_FILES="qmckl_error_fh_type.F90 qmckl_context_fh_type.F90"
export H_FUNC_FILES="qmckl_electron_func.h qmckl_distance_func.h qmckl_nucleus_func.h qmckl_determinant_func.h qmckl_error_func.h qmckl_jastrow_func.h qmckl_sherman_morrison_woodbury_func.h qmckl_trexio_func.h qmckl_numprec_func.h qmckl_context_func.h qmckl_mo_func.h qmckl_blas_func.h qmckl_local_energy_func.h qmckl_point_func.h qmckl_ao_func.h"
export H_TYPE_FILES="qmckl_error_type.h qmckl_numprec_type.h qmckl_context_type.h"
export H_PRIVATE_FUNC_FILES="qmckl_electron_private_func.h qmckl_nucleus_private_func.h qmckl_determinant_private_func.h qmckl_error_private_func.h qmckl_verificarlo_private_func.h qmckl_jastrow_private_func.h qmckl_mo_private_func.h qmckl_blas_private_func.h qmckl_memory_private_func.h qmckl_local_energy_private_func.h qmckl_point_private_func.h qmckl_ao_private_func.h"
export H_PRIVATE_TYPE_FILES="qmckl_electron_private_type.h qmckl_nucleus_private_type.h qmckl_determinant_private_type.h qmckl_error_private_type.h qmckl_jastrow_private_type.h qmckl_numprec_private_type.h qmckl_context_private_type.h qmckl_mo_private_type.h qmckl_blas_private_type.h qmckl_memory_private_type.h qmckl_local_energy_private_type.h qmckl_point_private_type.h qmckl_ao_private_type.h"

View File

@ -0,0 +1,11 @@
export C_FILES="src/qmckl_electron.c src/qmckl_nucleus.c src/qmckl_determinant.c src/qmckl_error.c src/qmckl_verificarlo.c src/qmckl_jastrow.c src/qmckl_sherman_morrison_woodbury.c src/qmckl_trexio.c src/qmckl_numprec.c src/qmckl_context.c src/qmckl_mo.c src/qmckl_blas.c src/qmckl_memory.c src/qmckl_local_energy.c src/qmckl_point.c src/qmckl_ao.c"
export F_FILES="src/qmckl_electron_f.F90 src/qmckl_distance_f.F90 src/qmckl_nucleus_f.F90 src/qmckl_determinant_f.F90 src/qmckl_verificarlo_f.F90 src/qmckl_jastrow_f.F90 src/qmckl_mo_f.F90 src/qmckl_blas_f.F90 src/qmckl_local_energy_f.F90 src/qmckl_ao_f.F90"
export C_O_FILES="src/qmckl_electron.o src/qmckl_nucleus.o src/qmckl_determinant.o src/qmckl_error.o src/qmckl_verificarlo.o src/qmckl_jastrow.o src/qmckl_sherman_morrison_woodbury.o src/qmckl_trexio.o src/qmckl_numprec.o src/qmckl_context.o src/qmckl_mo.o src/qmckl_blas.o src/qmckl_memory.o src/qmckl_local_energy.o src/qmckl_point.o src/qmckl_ao.o"
export FH_FUNC_FILES="src/qmckl_electron_fh_func.F90 src/qmckl_distance_fh_func.F90 src/qmckl_nucleus_fh_func.F90 src/qmckl_error_fh_func.F90 src/qmckl_sherman_morrison_woodbury_fh_func.F90 src/qmckl_trexio_fh_func.F90 src/qmckl_numprec_fh_func.F90 src/qmckl_context_fh_func.F90 src/qmckl_mo_fh_func.F90 src/qmckl_blas_fh_func.F90 src/qmckl_point_fh_func.F90 src/qmckl_ao_fh_func.F90"
export FH_TYPE_FILES="src/qmckl_error_fh_type.F90 src/qmckl_context_fh_type.F90"
export H_FUNC_FILES="src/qmckl_electron_func.h src/qmckl_distance_func.h src/qmckl_nucleus_func.h src/qmckl_determinant_func.h src/qmckl_error_func.h src/qmckl_jastrow_func.h src/qmckl_sherman_morrison_woodbury_func.h src/qmckl_trexio_func.h src/qmckl_numprec_func.h src/qmckl_context_func.h src/qmckl_mo_func.h src/qmckl_blas_func.h src/qmckl_local_energy_func.h src/qmckl_point_func.h src/qmckl_ao_func.h"
export H_TYPE_FILES="src/qmckl_error_type.h src/qmckl_numprec_type.h src/qmckl_context_type.h"
export H_PRIVATE_FUNC_FILES="src/qmckl_electron_private_func.h src/qmckl_nucleus_private_func.h src/qmckl_determinant_private_func.h src/qmckl_error_private_func.h src/qmckl_verificarlo_private_func.h src/qmckl_jastrow_private_func.h src/qmckl_mo_private_func.h src/qmckl_blas_private_func.h src/qmckl_memory_private_func.h src/qmckl_local_energy_private_func.h src/qmckl_point_private_func.h src/qmckl_ao_private_func.h"
export H_PRIVATE_TYPE_FILES="src/qmckl_electron_private_type.h src/qmckl_nucleus_private_type.h src/qmckl_determinant_private_type.h src/qmckl_error_private_type.h src/qmckl_jastrow_private_type.h src/qmckl_numprec_private_type.h src/qmckl_context_private_type.h src/qmckl_mo_private_type.h src/qmckl_blas_private_type.h src/qmckl_memory_private_type.h src/qmckl_local_energy_private_type.h src/qmckl_point_private_type.h src/qmckl_ao_private_type.h"

3183
python/numpy.i Normal file

File diff suppressed because it is too large Load Diff

52
python/pyqmckl.i Normal file
View File

@ -0,0 +1,52 @@
%module pyqmckl
/* Define SWIGWORDSIZE in order to properly align long integers on 64-bit system */
#define SWIGWORDSIZE64
%{
#define SWIG_FILE_WITH_INIT
/* Include the headers in the wrapper code */
#include "qmckl.h"
%}
/* Include stdint to recognize types from stdint.h */
%include <stdint.i>
/* Include typemaps to play with input/output re-casting
Useful when working with C pointers
*/
%include typemaps.i
/* Return TREXIO exit code from trexio_open as part of the output tuple */
%apply int *OUTPUT { qmckl_exit_code *exit_code};
/* This enables access to trexio_[...]_read_dset_str_low set of functions
in order to return one long string with TREXIO_DELIM delimeter as 2-nd argument of output tuple
*/
%include <cstring.i>
/* This block is needed make SWIG treat (double * dset_out|_in, int64_t dim_out|_in) pattern
as a special case in order to return the NumPy array to Python from C pointer to array
provided by trexio_read_safe_[dset_num] function.
NOTE: numpy.i is currently not part of SWIG but included in the numpy distribution (under numpy/tools/swig/numpy.i)
*/
%include "numpy.i"
%init %{
import_array();
%}
/* Typemaps below change the type of numpy array dimensions from int to int64_t */
%numpy_typemaps(double, NPY_DOUBLE, int64_t)
%numpy_typemaps(float, NPY_FLOAT, int64_t)
%numpy_typemaps(int32_t, NPY_INT32, int64_t)
%numpy_typemaps(int64_t, NPY_INT64, int64_t)
/* Include typemaps generated by the process.py */
%include pyqmckl_include.i
/* Parse the header files to generate wrappers */
%include "qmckl.h"

96
python/pyqmckl_include.i Normal file
View File

@ -0,0 +1,96 @@
%apply int *OUTPUT { int64_t* const num };
%apply int *OUTPUT { int64_t* const num };
%apply float *OUTPUT { double* const rescale_factor_kappa };
%apply float *OUTPUT { double* const energy };
%apply int *OUTPUT { int64_t* const num };
%apply int *OUTPUT { int64_t* const up_num };
%apply int *OUTPUT { int64_t* const down_num };
%apply int *OUTPUT { int64_t* const walk_num };
%apply float *OUTPUT { double* const rescale_factor_kappa_ee };
%apply float *OUTPUT { double* const rescale_factor_kappa_en };
%apply float *OUTPUT { double* const distance };
%apply float *OUTPUT { double* const distance_rescaled };
%apply float *OUTPUT { double* const distance_rescaled_deriv_e };
%apply float *OUTPUT { double* const ee_pot };
%apply float *OUTPUT { double* distance };
%apply float *OUTPUT { double* distance_rescaled };
%apply float *OUTPUT { double* distance_rescaled_deriv_e };
%apply float *OUTPUT { double* const en_pot };
%apply int *OUTPUT { int64_t* const shell_num };
%apply int *OUTPUT { int64_t* const prim_num };
%apply int *OUTPUT { int64_t* const ao_num };
%apply int *OUTPUT { int64_t* mo_num };
%apply float *OUTPUT { double* const mo_vgl };
%apply float *OUTPUT { double* const det_vgl_alpha };
%apply float *OUTPUT { double* const det_vgl_beta };
%apply float *OUTPUT { double* const det_inv_matrix_alpha };
%apply float *OUTPUT { double* const det_inv_matrix_beta };
%apply float *OUTPUT { double* const det_adj_matrix_alpha };
%apply float *OUTPUT { double* const det_adj_matrix_beta };
%apply float *OUTPUT { double* const det_adj_matrix_alpha };
%apply float *OUTPUT { double* const det_adj_matrix_beta };
%apply int *OUTPUT { int64_t* const aord_num };
%apply int *OUTPUT { int64_t* const bord_num };
%apply int *OUTPUT { int64_t* const bord_num };
%apply int *OUTPUT { int64_t* const type_nucl_num };
%apply int *OUTPUT { int64_t* const dim_cord_vect };
%apply float *OUTPUT { double* const cord_vect_full };
%apply int *OUTPUT { int64_t* const lkpm_combined_index };
%apply float *OUTPUT { double* const tmp_c };
%apply float *OUTPUT { double* const dtmp_c };
%apply float *OUTPUT { double* const kinetic_energy };
%apply float *OUTPUT { double* const potential_energy };
%apply float *OUTPUT { double* const local_energy };
%apply float *OUTPUT { double* const drift_vector };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const coord,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const charge,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const coord,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* charge,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* coord,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* distance,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* distance_rescaled,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const coord,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { ( const double* coord , const int64_t size_max) };
%apply ( int64_t* IN_ARRAY1 , int64_t DIM1 ) { (const int64_t* nucleus_index,const int64_t size_max) };
%apply ( int64_t* IN_ARRAY1 , int64_t DIM1 ) { (const int64_t* nucleus_shell_num,const int64_t size_max) };
%apply ( int32_t* IN_ARRAY1 , int64_t DIM1 ) { (const int32_t* shell_ang_mom,const int64_t size_max) };
%apply ( int64_t* IN_ARRAY1 , int64_t DIM1 ) { (const int64_t* shell_prim_num,const int64_t size_max) };
%apply ( int64_t* IN_ARRAY1 , int64_t DIM1 ) { (const int64_t* shell_prim_index,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* shell_factor,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* exponent,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* coefficient,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* prim_factor,const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { (const double* ao_factor,const int64_t size_max) };
%apply ( int64_t* ARGOUT_ARRAY1 , int64_t DIM1 ) { (int64_t* const nucleus_shell_num,const int64_t size_max) };
%apply ( int64_t* ARGOUT_ARRAY1 , int64_t DIM1 ) { (int64_t* const nucleus_index,const int64_t size_max) };
%apply ( int32_t* ARGOUT_ARRAY1 , int64_t DIM1 ) { (int32_t* const shell_ang_mom,const int64_t size_max) };
%apply ( int64_t* ARGOUT_ARRAY1 , int64_t DIM1 ) { (int64_t* const shell_prim_num,const int64_t size_max) };
%apply ( int64_t* ARGOUT_ARRAY1 , int64_t DIM1 ) { (int64_t* const shell_prim_index,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const shell_factor,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const exponent,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const coefficient,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const prim_factor,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const ao_factor,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const primitive_vgl,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const shell_vgl,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const ao_vgl,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const ao_vgl,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const coefficient,const int64_t size_max) };
%apply ( int64_t* ARGOUT_ARRAY1 , int64_t DIM1 ) { ( int64_t* const type_nucl_num , const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { ( double * const aord_vector , const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { ( double * const bord_vector , const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { ( double * const cord_vector , const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { ( const double * aord_vector , const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { ( const double * bord_vector , const int64_t size_max) };
%apply ( double* IN_ARRAY1 , int64_t DIM1 ) { ( const double * cord_vector , const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const asymp_jasb,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const factor_ee,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const factor_ee_deriv_e,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const factor_en,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const factor_en_deriv_e,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const distance_rescaled,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const distance_rescaled,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const distance_rescaled,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const distance_rescaled,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const factor_een,const int64_t size_max) };
%apply ( double* ARGOUT_ARRAY1 , int64_t DIM1 ) { (double* const factor_een_deriv_e,const int64_t size_max) };

141
python/pytrexio.i Normal file
View File

@ -0,0 +1,141 @@
%module pytrexio
/* Define SWIGWORDSIZE in order to properly align long integers on 64-bit system */
#define SWIGWORDSIZE64
%{
#define SWIG_FILE_WITH_INIT
/* Include the headers in the wrapper code */
#include "trexio_s.h"
#include "trexio.h"
%}
/* Include stdint to recognize types from stdint.h */
%include <stdint.i>
/* NOTE:
carrays was useful before numpy.i was introduced.
For Python interface it's better to use numpy arrays instead of carrays, because the latter are less python-ic.
On the other hand, carrays might be more portable to other target languages.
// Include carrays to work with C pointers to arrays
%include "carrays.i"
// Include classes that correspond to integer and float arrays
%array_class(double, doubleArray);
%array_class(float, floatArray);
%array_class(int32_t, int32Array);
%array_class(int64_t, int64Array);
*/
/* Include typemaps to play with input/output re-casting
Useful when working with C pointers
*/
%include typemaps.i
/* Redefine the [int32_t*, int64_t*, float*, double*] num
pattern to be appended to the output tuple.
Useful for TREXIO read_num functions where the
num variable is modified by address
*/
/* Return num variables as part of the output tuple */
%apply int *OUTPUT { int32_t* const num};
%apply int *OUTPUT { int64_t* const num};
%apply int *OUTPUT { int32_t* const num_up};
%apply int *OUTPUT { int32_t* const num_dn};
%apply int *OUTPUT { int64_t* const num_up};
%apply int *OUTPUT { int64_t* const num_dn};
%apply float *OUTPUT { float* const num};
%apply float *OUTPUT { double* const num};
/* Return TREXIO exit code from trexio_open as part of the output tuple */
%apply int *OUTPUT { trexio_exit_code* const rc_open};
/* Return number of sparse data points stored in the file as part of the output tuple */
%apply int *OUTPUT { int64_t* const size_max};
/* Return number of sparse data points read from the file as part of the output tuple */
%apply int *INOUT { int64_t* const buffer_size_read};
/* Does not work for arrays (SIGSEGV) */
/* This enables access to trexio_[...]_read_dset_str_low set of functions
in order to return one long string with TREXIO_DELIM delimeter as 2-nd argument of output tuple
*/
%include <cstring.i>
/* This enables read of long strings with TREXIO_DELIM delimeters that can be further converted into an array of string */
%cstring_bounded_output(char* dset_out, 4096);
/* This enables read of single string attributes with pre-defined max_str_len
for Python we pre-define max_str_len = PYTREXIO_MAX_STR_LENGTH everywhere for simplicity
*/
%cstring_output_maxsize(char* const str_out, const int32_t max_str_len);
/* This block is needed make SWIG treat (double * dset_out|_in, int64_t dim_out|_in) pattern
as a special case in order to return the NumPy array to Python from C pointer to array
provided by trexio_read_safe_[dset_num] function.
NOTE: numpy.i is currently not part of SWIG but included in the numpy distribution (under numpy/tools/swig/numpy.i)
*/
%include "numpy.i"
%init %{
import_array();
%}
/* Typemaps below change the type of numpy array dimensions from int to int64_t */
%numpy_typemaps(double, NPY_DOUBLE, int64_t)
%numpy_typemaps(float, NPY_FLOAT, int64_t)
%numpy_typemaps(int32_t, NPY_INT32, int64_t)
%numpy_typemaps(int64_t, NPY_INT64, int64_t)
%numpy_typemaps(bitfield_t, NPY_INT64, int64_t)
/* Enable write|read_safe functions to convert numpy arrays from/to double arrays */
%apply (double* ARGOUT_ARRAY1, int64_t DIM1) {(double* const dset_out, const int64_t dim_out)};
%apply (double* IN_ARRAY1, int64_t DIM1) {(const double* dset_in, const int64_t dim_in)};
/* Enable write|read_safe functions to convert numpy arrays from/to float arrays */
%apply (float* ARGOUT_ARRAY1, int64_t DIM1) {(float* const dset_out, const int64_t dim_out)};
%apply (float* IN_ARRAY1, int64_t DIM1) {(const float* dset_in, const int64_t dim_in)};
/* Enable write|read_safe functions to convert numpy arrays from/to int32 arrays */
%apply (int32_t* ARGOUT_ARRAY1, int64_t DIM1) {(int32_t* const dset_out, const int64_t dim_out)};
%apply (int32_t* IN_ARRAY1, int64_t DIM1) {(const int32_t* dset_in, const int64_t dim_in)};
/* Enable write|read_safe functions to convert numpy arrays from/to int64 arrays */
%apply (int64_t* ARGOUT_ARRAY1, int64_t DIM1) {(int64_t* const dset_out, const int64_t dim_out)};
%apply (int64_t* IN_ARRAY1, int64_t DIM1) {(const int64_t* dset_in, const int64_t dim_in)};
/* Enable write|read_safe functions to convert numpy arrays from/to sparse arrays */
%apply (double* IN_ARRAY1, int64_t DIM1) {(const double* value_sparse_write, const int64_t size_value_write)};
%apply (int32_t* IN_ARRAY1, int64_t DIM1) {(const int32_t* index_sparse_write, const int64_t size_index_write)};
%apply (double* ARGOUT_ARRAY1, int64_t DIM1) {(double* const value_sparse_read, const int64_t size_value_read)};
%apply (int32_t* ARGOUT_ARRAY1, int64_t DIM1) {(int32_t* const index_sparse_read, const int64_t size_index_read)};
/* Enable write|read_safe functions to convert numpy arrays from orbital list arrays */
%apply (int32_t* ARGOUT_ARRAY1, int64_t DIM1) {(int32_t* const dset_up_out, const int64_t dim_up_out)};
%apply (int32_t* ARGOUT_ARRAY1, int64_t DIM1) {(int32_t* const dset_dn_out, const int64_t dim_dn_out)};
%apply (int64_t* ARGOUT_ARRAY1, int64_t DIM1) {(int64_t* const dset_up_out, const int64_t dim_up_out)};
%apply (int64_t* ARGOUT_ARRAY1, int64_t DIM1) {(int64_t* const dset_dn_out, const int64_t dim_dn_out)};
%apply (bitfield_t* IN_ARRAY1, int64_t DIM1) {(const bitfield_t* dset_in, const int64_t dim_in)};
/* This tells SWIG to treat char ** dset_in pattern as a special case
Enables access to trexio_[...]_write_dset_str set of functions directly, i.e.
by converting input list of strings from Python into char ** of C
*/
%typemap(in) char** dset_in {
/* Check if is a list */
if (PyList_Check($input)) {
int size = PyList_Size($input);
Py_ssize_t i = 0;
$1 = (char **) malloc((size+1)*sizeof(char *));
for (i = 0; i < size; i++) {
PyObject *o = PyList_GetItem($input, i);
if (PyUnicode_Check(o)) {
$1[i] = PyUnicode_AsUTF8(PyList_GetItem($input,i));
} else {
PyErr_Format(PyExc_TypeError, "list must contain strings. %d/%d element was not string.", i, size);
free($1);
return NULL;
}
}
$1[i] = 0;
} else {
PyErr_SetString(PyExc_TypeError, "not a list");
return NULL;
}
}
/* This cleans up the char ** array we malloc-ed before */
%typemap(freearg) char** dset_in {
free((char *) $1);
}
/* Parse the header files to generate wrappers */
%include "trexio_s.h"
%include "trexio.h"

2093
python/qmckl.h Normal file

File diff suppressed because it is too large Load Diff