mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-08-24 22:21:43 +02:00
Using methods
This commit is contained in:
parent
af9bc4360d
commit
06be52c0d1
@ -81,13 +81,15 @@ def make_functions():
|
||||
"dim readonly" : "i64",
|
||||
"index" : "i64",
|
||||
"str" : "str"}
|
||||
r = []
|
||||
|
||||
r = ["""
|
||||
impl File {""" ]
|
||||
|
||||
for group in data:
|
||||
group_l = group.lower()
|
||||
r += [ """
|
||||
pub fn has_{group_l}(trex_file: File) -> Result<bool, ExitCode> {
|
||||
let rc = unsafe { c::trexio_has_{group}(trex_file) };
|
||||
pub fn has_{group_l}(&self) -> Result<bool, ExitCode> {
|
||||
let rc = unsafe { c::trexio_has_{group}(self.ptr) };
|
||||
match rc {
|
||||
c::TREXIO_SUCCESS => Ok(true),
|
||||
c::TREXIO_HAS_NOT => Ok(false),
|
||||
@ -102,8 +104,8 @@ pub fn has_{group_l}(trex_file: File) -> Result<bool, ExitCode> {
|
||||
type_r = convert_r[data[group][element][0]]
|
||||
element_l = element.lower()
|
||||
r += [ """
|
||||
pub fn has_{group_l}_{element_l}(trex_file: File) -> Result<bool, ExitCode> {
|
||||
let rc = unsafe { c::trexio_has_{group}_{element}(trex_file) };
|
||||
pub fn has_{group_l}_{element_l}(&self) -> Result<bool, ExitCode> {
|
||||
let rc = unsafe { c::trexio_has_{group}_{element}(self.ptr) };
|
||||
match rc {
|
||||
c::TREXIO_SUCCESS => Ok(true),
|
||||
c::TREXIO_HAS_NOT => Ok(false),
|
||||
@ -120,18 +122,18 @@ pub fn has_{group_l}_{element_l}(trex_file: File) -> Result<bool, ExitCode> {
|
||||
if data[group][element][1] == []:
|
||||
if data[group][element][0] in [ "int", "float", "dim", "index" ]:
|
||||
r += [ """
|
||||
pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode> {
|
||||
pub fn read_{group_l}_{element_l}(&self) -> Result<{type_r}, ExitCode> {
|
||||
let mut data_c: {type_c} = 0{type_c};
|
||||
let (rc, data) = unsafe {
|
||||
let rc = c::trexio_read_{group}_{element}_64(trex_file, &mut data_c);
|
||||
let rc = c::trexio_read_{group}_{element}_64(self.ptr, &mut data_c);
|
||||
(rc, data_c.try_into().expect("try_into failed in read_{group_l}_{element_l}"))
|
||||
};
|
||||
rc_return(rc, data)
|
||||
}
|
||||
|
||||
pub fn write_{group_l}_{element_l}(trex_file: File, data: {type_r}) -> Result<(), ExitCode> {
|
||||
pub fn write_{group_l}_{element_l}(&self, data: {type_r}) -> Result<(), ExitCode> {
|
||||
let data: {type_c} = data.try_into().expect("try_into failed in write_{group_l}_{element_l}");
|
||||
let rc = unsafe { c::trexio_write_{group}_{element}_64(trex_file, data) };
|
||||
let rc = unsafe { c::trexio_write_{group}_{element}_64(self.ptr, data) };
|
||||
rc_return(rc, ())
|
||||
}
|
||||
"""
|
||||
@ -144,21 +146,21 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: {type_r}) -> Result<()
|
||||
|
||||
elif data[group][element][0] in [ "str" ]:
|
||||
r += [ """
|
||||
pub fn read_{group_l}_{element_l}(trex_file: File, capacity: usize) -> Result<String, ExitCode> {
|
||||
pub fn read_{group_l}_{element_l}(&self, capacity: usize) -> Result<String, ExitCode> {
|
||||
let mut data_c = String::with_capacity(capacity);
|
||||
let data_c = data_c.as_mut_ptr() as *mut c_char;
|
||||
let (rc, data) = unsafe {
|
||||
let rc = c::trexio_read_{group}_{element}(trex_file, data_c, capacity.try_into().expect("try_into failed in read_{group_l}_{element_l}"));
|
||||
let rc = c::trexio_read_{group}_{element}(self.ptr, data_c, capacity.try_into().expect("try_into failed in read_{group_l}_{element_l}"));
|
||||
(rc, String::from_raw_parts(data_c as *mut u8, capacity, capacity))
|
||||
};
|
||||
rc_return(rc, data)
|
||||
}
|
||||
|
||||
pub fn write_{group_l}_{element_l}(trex_file: File, data: &str) -> Result<(), ExitCode> {
|
||||
pub fn write_{group_l}_{element_l}(&self, data: &str) -> Result<(), ExitCode> {
|
||||
let size : i32 = data.len().try_into().expect("try_into failed in write_{group_l}_{element_l}");
|
||||
let data = string_to_c(data);
|
||||
let data = data.as_ptr() as *const c_char;
|
||||
let rc = unsafe { c::trexio_write_{group}_{element}(trex_file, data, size) };
|
||||
let rc = unsafe { c::trexio_write_{group}_{element}(self.ptr, data, size) };
|
||||
rc_return(rc, ())
|
||||
}
|
||||
"""
|
||||
@ -172,10 +174,10 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: &str) -> Result<(), Ex
|
||||
|
||||
elif data[group][element][0] in [ "dim readonly" ]:
|
||||
r += [ """
|
||||
pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode> {
|
||||
pub fn read_{group_l}_{element_l}(&self) -> Result<{type_r}, ExitCode> {
|
||||
let mut data_c: {type_c} = 0{type_c};
|
||||
let (rc, data) = unsafe {
|
||||
let rc = c::trexio_read_{group}_{element}_64(trex_file, &mut data_c);
|
||||
let rc = c::trexio_read_{group}_{element}_64(self.ptr, &mut data_c);
|
||||
(rc, data_c.try_into().expect("try_into failed in read_{group_l}_{element_l}"))
|
||||
};
|
||||
rc_return(rc, data)
|
||||
@ -192,13 +194,13 @@ pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode>
|
||||
else:
|
||||
|
||||
if data[group][element][0] in [ "int", "float", "dim", "index" ]:
|
||||
t = [ """pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<Vec<{type_r}>, ExitCode> {
|
||||
t = [ """pub fn read_{group_l}_{element_l}(&self) -> Result<Vec<{type_r}>, ExitCode> {
|
||||
let size = 1;""" ]
|
||||
t_prime = []
|
||||
for dim in data[group][element][1]:
|
||||
try: # A dimensioning variable
|
||||
dim_group, dim_element = dim.split('.')
|
||||
t_prime += [f" let size = size * read_{dim_group}_{dim_element}(trex_file)?;" ]
|
||||
t_prime += [f" let size = size * self.read_{dim_group}_{dim_element}()?;" ]
|
||||
except: # Only an integer
|
||||
t_prime += [f" let size = size * {dim};"]
|
||||
t += t_prime
|
||||
@ -206,7 +208,7 @@ pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode>
|
||||
let data: Vec<{type_r}> = Vec::with_capacity(size);
|
||||
let data_c = data.as_ptr() as *mut {type_c};
|
||||
let (rc, data) = unsafe {
|
||||
let rc = c::trexio_read_safe_{group}_{element}_64(trex_file, data_c, size.try_into().expect("try_into failed in read_{group}_{element}"));
|
||||
let rc = c::trexio_read_safe_{group}_{element}_64(self.ptr, data_c, size.try_into().expect("try_into failed in read_{group}_{element}"));
|
||||
(rc, data)
|
||||
};
|
||||
rc_return(rc, data)
|
||||
@ -221,10 +223,10 @@ pub fn read_{group_l}_{element_l}(trex_file: File) -> Result<{type_r}, ExitCode>
|
||||
.replace("{element_l}",element_l) ]
|
||||
|
||||
r += [ """
|
||||
pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<{type_r}>) -> Result<(), ExitCode> {
|
||||
pub fn write_{group_l}_{element_l}(&self, data: Vec<{type_r}>) -> Result<(), ExitCode> {
|
||||
let size: i64 = data.len().try_into().expect("try_into failed in write_{group_l}_{element_l}");
|
||||
let data = data.as_ptr() as *const {type_c};
|
||||
let rc = unsafe { c::trexio_write_safe_{group}_{element}_64(trex_file, data, size) };
|
||||
let rc = unsafe { c::trexio_write_safe_{group}_{element}_64(self.ptr, data, size) };
|
||||
rc_return(rc, ())
|
||||
}
|
||||
"""
|
||||
@ -236,13 +238,13 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<{type_r}>) -> Resu
|
||||
.replace("{element_l}",element_l) ]
|
||||
|
||||
elif data[group][element][0] in [ "str" ]:
|
||||
t = [ """pub fn read_{group_l}_{element_l}(trex_file: File, capacity: usize) -> Result<Vec<String>, ExitCode> {
|
||||
t = [ """pub fn read_{group_l}_{element_l}(&self, capacity: usize) -> Result<Vec<String>, ExitCode> {
|
||||
let size = 1;""" ]
|
||||
t_prime = []
|
||||
for dim in data[group][element][1]:
|
||||
try: # A dimensioning variable
|
||||
dim_group, dim_element = dim.split('.')
|
||||
t_prime += [f" let size = size * read_{dim_group}_{dim_element}(trex_file)?;" ]
|
||||
t_prime += [f" let size = size * self.read_{dim_group}_{dim_element}()?;" ]
|
||||
except: # Only an integer
|
||||
t_prime += [f" let size = size * {dim};"]
|
||||
t += t_prime
|
||||
@ -251,7 +253,7 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<{type_r}>) -> Resu
|
||||
let data_c = data.as_ptr() as *mut *mut c_char;
|
||||
|
||||
let (rc, data) = unsafe {
|
||||
let rc = c::trexio_read_{group}_{element}(trex_file, data_c, capacity.try_into().expect("try_into failed in read_{group}_{element}") );
|
||||
let rc = c::trexio_read_{group}_{element}(self.ptr, data_c, capacity.try_into().expect("try_into failed in read_{group}_{element}") );
|
||||
(rc, data)
|
||||
};
|
||||
rc_return(rc, data)
|
||||
@ -266,7 +268,7 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<{type_r}>) -> Resu
|
||||
.replace("{element_l}",element_l) ]
|
||||
|
||||
r += [ """
|
||||
pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<&str>) -> Result<(), ExitCode> {
|
||||
pub fn write_{group_l}_{element_l}(&self, data: Vec<&str>) -> Result<(), ExitCode> {
|
||||
let mut size = 0;
|
||||
for s in data.iter() {
|
||||
let l = s.len();
|
||||
@ -277,7 +279,7 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<&str>) -> Result<(
|
||||
let data_c : Vec<*const c_char> = data_c.iter().map(|x| x.as_ptr() as *const c_char).collect::<Vec<_>>();
|
||||
let size : i32 = size.try_into().expect("try_into failed in write_{group}_{element}");
|
||||
let data_c = data_c.as_ptr() as *mut *const c_char;
|
||||
let rc = unsafe { c::trexio_write_{group}_{element}(trex_file, data_c, size) };
|
||||
let rc = unsafe { c::trexio_write_{group}_{element}(self.ptr, data_c, size) };
|
||||
rc_return(rc, ())
|
||||
}
|
||||
"""
|
||||
@ -293,6 +295,7 @@ pub fn write_{group_l}_{element_l}(trex_file: File, data: Vec<&str>) -> Result<(
|
||||
|
||||
|
||||
|
||||
r += [ "}" ]
|
||||
with open(generated_rs,'w') as f:
|
||||
f.write('\n'.join(r))
|
||||
|
||||
|
@ -11,9 +11,6 @@ pub use exit_code::ExitCode;
|
||||
pub mod back_end;
|
||||
pub use back_end::BackEnd;
|
||||
|
||||
/// Type for a TREXIO file
|
||||
pub type File = *mut c::trexio_t;
|
||||
|
||||
pub const PACKAGE_VERSION : &str = unsafe { std::str::from_utf8_unchecked(c::TREXIO_PACKAGE_VERSION) };
|
||||
|
||||
fn rc_return<T>(rc : c::trexio_exit_code, result: T) -> Result<T,ExitCode> {
|
||||
@ -30,36 +27,42 @@ fn string_to_c(s: &str) -> std::ffi::CString {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pub fn open(file_name: &str, mode: char, back_end: BackEnd) -> Result<File, ExitCode> {
|
||||
let file_name_c = string_to_c(file_name);
|
||||
let file_name_c = file_name_c.as_ptr() as *const c_char;
|
||||
let mode = mode as c_char;
|
||||
let back_end = back_end.to_c();
|
||||
let rc: *mut c::trexio_exit_code = &mut c::TREXIO_SUCCESS.clone();
|
||||
let result = unsafe { c::trexio_open(file_name_c, mode, back_end, rc) };
|
||||
let rc = unsafe { *rc };
|
||||
rc_return(rc, result)
|
||||
/// Type for a TREXIO file
|
||||
pub struct File {
|
||||
ptr: *mut c::trexio_t,
|
||||
}
|
||||
|
||||
pub fn close(file: File) -> Result<(), ExitCode> {
|
||||
let rc = unsafe { c::trexio_close(file) };
|
||||
rc_return(rc, ())
|
||||
}
|
||||
|
||||
pub fn inquire(file_name: &str) -> Result<bool, ExitCode> {
|
||||
let file_name_c = string_to_c(file_name);
|
||||
let file_name_c = file_name_c.as_ptr() as *const c_char;
|
||||
let rc = unsafe { c::trexio_inquire(file_name_c) };
|
||||
match ExitCode::from(rc) {
|
||||
ExitCode::Failure => Ok(false),
|
||||
ExitCode::Success => Ok(true),
|
||||
x => Err(x),
|
||||
impl File {
|
||||
|
||||
pub fn open(file_name: &str, mode: char, back_end: BackEnd) -> Result<File, ExitCode> {
|
||||
let file_name_c = string_to_c(file_name);
|
||||
let file_name_c = file_name_c.as_ptr() as *const c_char;
|
||||
let mode = mode as c_char;
|
||||
let back_end = back_end.to_c();
|
||||
let rc: *mut c::trexio_exit_code = &mut c::TREXIO_SUCCESS.clone();
|
||||
let result = unsafe { c::trexio_open(file_name_c, mode, back_end, rc) };
|
||||
let rc = unsafe { *rc };
|
||||
rc_return(rc, File { ptr: result })
|
||||
}
|
||||
|
||||
pub fn close(self) -> Result<(), ExitCode> {
|
||||
let rc = unsafe { c::trexio_close(self.ptr) };
|
||||
rc_return(rc, ())
|
||||
}
|
||||
|
||||
pub fn inquire(file_name: &str) -> Result<bool, ExitCode> {
|
||||
let file_name_c = string_to_c(file_name);
|
||||
let file_name_c = file_name_c.as_ptr() as *const c_char;
|
||||
let rc = unsafe { c::trexio_inquire(file_name_c) };
|
||||
match ExitCode::from(rc) {
|
||||
ExitCode::Failure => Ok(false),
|
||||
ExitCode::Success => Ok(true),
|
||||
x => Err(x),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
include!("generated.rs");
|
||||
|
||||
|
||||
|
@ -56,32 +56,32 @@ pub fn test_write(file_name: &str, back_end: BackEnd) -> Result<(), trexio::Exit
|
||||
|
||||
|
||||
println!("{}", file_name);
|
||||
assert!( ! trexio::inquire(file_name)? );
|
||||
assert!( ! trexio::File::inquire(file_name)? );
|
||||
|
||||
let trex_file = trexio::open(file_name, 'w', back_end)?;
|
||||
let trex_file = trexio::File::open(file_name, 'w', back_end)?;
|
||||
|
||||
assert!( ! trexio::has_nucleus(trex_file)? );
|
||||
assert!( ! trexio::has_nucleus_num(trex_file)? );
|
||||
assert!( ! trexio::has_nucleus_charge(trex_file)? );
|
||||
assert!( ! trexio::has_ao_2e_int(trex_file)? );
|
||||
assert!( ! trexio::has_ao_2e_int_eri(trex_file)? );
|
||||
assert!( ! trexio::has_determinant_list(trex_file)? );
|
||||
assert!( ! trex_file.has_nucleus()? );
|
||||
assert!( ! trex_file.has_nucleus_num()? );
|
||||
assert!( ! trex_file.has_nucleus_charge()? );
|
||||
assert!( ! trex_file.has_ao_2e_int()? );
|
||||
assert!( ! trex_file.has_ao_2e_int_eri()? );
|
||||
assert!( ! trex_file.has_determinant_list()? );
|
||||
|
||||
trexio::write_nucleus_num(trex_file, nucleus_num)?;
|
||||
trexio::write_nucleus_charge(trex_file, charge)?;
|
||||
trexio::write_nucleus_point_group(trex_file, sym_str)?;
|
||||
trexio::write_nucleus_coord(trex_file, coord)?;
|
||||
trexio::write_nucleus_label(trex_file, label)?;
|
||||
trexio::write_basis_shell_num(trex_file, basis_shell_num)?;
|
||||
trexio::write_basis_nucleus_index(trex_file, basis_nucleus_index)?;
|
||||
trexio::write_state_id(trex_file, state_id)?;
|
||||
trex_file.write_nucleus_num(nucleus_num)?;
|
||||
trex_file.write_nucleus_charge(charge)?;
|
||||
trex_file.write_nucleus_point_group(sym_str)?;
|
||||
trex_file.write_nucleus_coord(coord)?;
|
||||
trex_file.write_nucleus_label(label)?;
|
||||
trex_file.write_basis_shell_num(basis_shell_num)?;
|
||||
trex_file.write_basis_nucleus_index(basis_nucleus_index)?;
|
||||
trex_file.write_state_id(state_id)?;
|
||||
|
||||
if ! trexio::has_ao_num(trex_file)? {
|
||||
trexio::write_ao_num(trex_file, ao_num)?;
|
||||
if ! trex_file.has_ao_num()? {
|
||||
trex_file.write_ao_num(ao_num)?;
|
||||
}
|
||||
|
||||
if ! trexio::has_mo_num(trex_file)? {
|
||||
trexio::write_mo_num(trex_file, mo_num)?;
|
||||
if ! trex_file.has_mo_num()? {
|
||||
trex_file.write_mo_num(mo_num)?;
|
||||
}
|
||||
|
||||
let mut energy = Vec::with_capacity(mo_num);
|
||||
@ -89,8 +89,8 @@ pub fn test_write(file_name: &str, back_end: BackEnd) -> Result<(), trexio::Exit
|
||||
let e: f64 = i as f64 -100.0f64;
|
||||
energy.push(e);
|
||||
}
|
||||
println!("{:#?}", energy);
|
||||
trex_file.write_mo_energy(energy)?;
|
||||
|
||||
trexio::close(trex_file)
|
||||
trex_file.close()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user