1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00

Added json in trexio.h

This commit is contained in:
Anthony Scemama 2023-10-23 17:44:31 +02:00
parent 49462add49
commit 6ff3fdfae0
2 changed files with 281 additions and 14 deletions

View File

@ -1,6 +1,5 @@
const TREXIO_H: &str = "../../include/trexio.h";
const WRAPPER_H: &str = "wrapper.h";
const JSON_FILE: &str = "../../trex.json";
const GENERATED_RS: &str = "src/generated.rs";
use std::env;
@ -15,7 +14,7 @@ use std::path::Path;
/// Checks that the version specified in the Cargo.toml file is consistent with the version of the TREXIO C library.
fn check_version(bindings: PathBuf) -> Result<(), String> {
fn check_version(bindings: &PathBuf) -> Result<(), String> {
// Read version from Cargo.toml
let mut rust_version = String::new();
{
@ -629,11 +628,26 @@ pub fn write_{group_l}_{element_l}(&self, offset: usize, data: &[{typ}]) -> Resu
/// Reads the JSON file, processes its contents, and generates Rust functions according to the specifications in the JSON data.
fn make_functions() -> std::io::Result<()> {
let json_file = Path::new(JSON_FILE);
let generated_rs = Path::new(GENERATED_RS);
let data: Value = serde_json::from_reader(File::open(&json_file)?)?;
fn make_functions(bindings: &PathBuf) -> std::io::Result<()> {
let file = File::open(bindings)?;
let reader = io::BufReader::new(file);
let mut json_file = String::new();
for line in reader.lines() {
let line = line?;
if line.contains("trexio_json") {
let index_start = match line.find('{') {
Some(x) => x,
_ => panic!("trexio_json not found"),
};
json_file = line[index_start..line.len()-5]
.replace(r#"\""#,"\"")
.replace(r#"\n"#,"\n")
.replace(r#"\t"#,"");
eprintln!("{}", json_file);
break;
}
}
let data: Value = serde_json::from_str(&json_file).unwrap();
let mut r: Vec<String> = vec![
String::from("
@ -655,6 +669,7 @@ impl File {
r.push(String::from("}"));
let generated_rs = Path::new(GENERATED_RS);
let mut f = File::create(&generated_rs)?;
f.write_all(r.join("\n").as_bytes())?;
Ok(())
@ -665,9 +680,6 @@ impl File {
fn main() {
make_interface().unwrap();
make_functions().unwrap();
// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search=/usr/local/lib");
@ -676,7 +688,6 @@ fn main() {
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=build.py");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
@ -695,8 +706,12 @@ fn main() {
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings_path = out_path.join("bindings.rs");
bindings
.write_to_file(out_path.join("bindings.rs"))
.write_to_file(&bindings_path)
.expect("Couldn't write bindings!");
check_version(out_path.join("bindings.rs")).unwrap();
check_version(&bindings_path).unwrap();
make_interface().unwrap();
make_functions(&bindings_path).unwrap();
}

View File

@ -20,6 +20,257 @@
** C
We include in thr trexio.h file a text variable that contains the
contents of the ~trex.json~ confguration file, generated from the
~trex.org~ file.
#+NAME: trex_json
#+begin_src python :results drawer
res = "static const char* trexio_json = "
with open('../../trex.json','r') as f:
for line in f:
res += " \" " + line.rstrip().replace('"', '\\"') + "\\n\"\n"
res += ";\n"
return res
#+end_src
#+RESULTS: trex_json
:results:
" {\n"
" \n"
" \"metadata\": {\n"
" \"code_num\" : [ \"dim\", [] ]\n"
" , \"code\" : [ \"str\", [ \"metadata.code_num\" ] ]\n"
" , \"author_num\" : [ \"dim\", [] ]\n"
" , \"author\" : [ \"str\", [ \"metadata.author_num\" ] ]\n"
" , \"package_version\" : [ \"str\", [] ]\n"
" , \"description\" : [ \"str\", [] ]\n"
" , \"unsafe\" : [ \"int\", [] ]\n"
" } ,\n"
" \n"
" \"nucleus\": {\n"
" \"num\" : [ \"dim\" , [] ]\n"
" , \"charge\" : [ \"float\", [ \"nucleus.num\" ] ]\n"
" , \"coord\" : [ \"float\", [ \"nucleus.num\", \"3\" ] ]\n"
" , \"label\" : [ \"str\" , [ \"nucleus.num\" ] ]\n"
" , \"point_group\" : [ \"str\" , [] ]\n"
" , \"repulsion\" : [ \"float\", [] ]\n"
" } ,\n"
" \n"
" \"cell\": {\n"
" \"a\" : [ \"float\", [ \"3\" ] ]\n"
" , \"b\" : [ \"float\", [ \"3\" ] ]\n"
" , \"c\" : [ \"float\", [ \"3\" ] ]\n"
" , \"G_a\" : [ \"float\", [ \"3\" ] ]\n"
" , \"G_b\" : [ \"float\", [ \"3\" ] ]\n"
" , \"G_c\" : [ \"float\", [ \"3\" ] ]\n"
" , \"two_pi\" : [ \"int\" , [] ]\n"
" } ,\n"
" \n"
" \"pbc\": {\n"
" \"periodic\" : [ \"int\" , [] ]\n"
" , \"k_point\" : [ \"float\", [ \"3\" ] ]\n"
" } ,\n"
" \n"
" \"electron\": {\n"
" \"num\" : [ \"dim\", [] ]\n"
" , \"up_num\" : [ \"int\", [] ]\n"
" , \"dn_num\" : [ \"int\", [] ]\n"
" } ,\n"
" \n"
" \"state\": {\n"
" \"num\" : [ \"dim\" , [] ]\n"
" , \"id\" : [ \"index\", [] ]\n"
" , \"energy\" : [ \"float\", [] ]\n"
" , \"current_label\" : [ \"str\" , [] ]\n"
" , \"label\" : [ \"str\" , [ \"state.num\" ] ]\n"
" , \"file_name\" : [ \"str\" , [ \"state.num\" ] ]\n"
" } ,\n"
" \n"
" \"basis\": {\n"
" \"type\" : [ \"str\" , [] ]\n"
" , \"prim_num\" : [ \"dim\" , [] ]\n"
" , \"shell_num\" : [ \"dim\" , [] ]\n"
" , \"nao_grid_num\" : [ \"dim\" , [] ]\n"
" , \"interp_coeff_cnt\" : [ \"dim\" , [] ]\n"
" , \"nucleus_index\" : [ \"index\", [ \"basis.shell_num\" ] ]\n"
" , \"shell_ang_mom\" : [ \"int\" , [ \"basis.shell_num\" ] ]\n"
" , \"shell_factor\" : [ \"float\", [ \"basis.shell_num\" ] ]\n"
" , \"r_power\" : [ \"int\" , [ \"basis.shell_num\" ] ]\n"
" , \"nao_grid_start\" : [ \"index\", [ \"basis.shell_num\" ] ]\n"
" , \"nao_grid_size\" : [ \"dim\" , [ \"basis.shell_num\" ] ]\n"
" , \"shell_index\" : [ \"index\", [ \"basis.prim_num\" ] ]\n"
" , \"exponent\" : [ \"float\", [ \"basis.prim_num\" ] ]\n"
" , \"coefficient\" : [ \"float\", [ \"basis.prim_num\" ] ]\n"
" , \"prim_factor\" : [ \"float\", [ \"basis.prim_num\" ] ]\n"
" , \"e_cut\" : [ \"float\", [] ]\n"
" , \"nao_grid_radius\" : [ \"float\", [ \"basis.nao_grid_num\" ] ]\n"
" , \"nao_grid_phi\" : [ \"float\", [ \"basis.nao_grid_num\" ] ]\n"
" , \"nao_grid_grad\" : [ \"float\", [ \"basis.nao_grid_num\" ] ]\n"
" , \"nao_grid_lap\" : [ \"float\", [ \"basis.nao_grid_num\" ] ]\n"
" , \"interpolator_kind\" : [ \"str\" , [] ]\n"
" , \"interpolator_phi\" : [ \"float\", [ \"basis.nao_grid_num\", \"basis.interp_coeff_cnt\" ] ]\n"
" , \"interpolator_grad\" : [ \"float\", [ \"basis.nao_grid_num\", \"basis.interp_coeff_cnt\" ] ]\n"
" , \"interpolator_lap\" : [ \"float\", [ \"basis.nao_grid_num\", \"basis.interp_coeff_cnt\" ] ]\n"
" } ,\n"
" \n"
" \"ecp\": {\n"
" \"max_ang_mom_plus_1\" : [ \"int\" , [ \"nucleus.num\" ] ]\n"
" , \"z_core\" : [ \"int\" , [ \"nucleus.num\" ] ]\n"
" , \"num\" : [ \"dim\" , [] ]\n"
" , \"ang_mom\" : [ \"int\" , [ \"ecp.num\" ] ]\n"
" , \"nucleus_index\" : [ \"index\", [ \"ecp.num\" ] ]\n"
" , \"exponent\" : [ \"float\", [ \"ecp.num\" ] ]\n"
" , \"coefficient\" : [ \"float\", [ \"ecp.num\" ] ]\n"
" , \"power\" : [ \"int\" , [ \"ecp.num\" ] ]\n"
" } ,\n"
" \n"
" \"grid\": {\n"
" \"description\" : [ \"str\" , [] ]\n"
" , \"rad_precision\" : [ \"float\", [] ]\n"
" , \"num\" : [ \"dim\" , [] ]\n"
" , \"max_ang_num\" : [ \"int\" , [] ]\n"
" , \"min_ang_num\" : [ \"int\" , [] ]\n"
" , \"coord\" : [ \"float\", [ \"grid.num\" ] ]\n"
" , \"weight\" : [ \"float\", [ \"grid.num\" ] ]\n"
" , \"ang_num\" : [ \"dim\" , [] ]\n"
" , \"ang_coord\" : [ \"float\", [ \"grid.ang_num\" ] ]\n"
" , \"ang_weight\" : [ \"float\", [ \"grid.ang_num\" ] ]\n"
" , \"rad_num\" : [ \"dim\" , [] ]\n"
" , \"rad_coord\" : [ \"float\", [ \"grid.rad_num\" ] ]\n"
" , \"rad_weight\" : [ \"float\", [ \"grid.rad_num\" ] ]\n"
" } ,\n"
" \n"
" \"ao\": {\n"
" \"cartesian\" : [ \"int\" , [] ]\n"
" , \"num\" : [ \"dim\" , [] ]\n"
" , \"shell\" : [ \"index\", [ \"ao.num\" ] ]\n"
" , \"normalization\" : [ \"float\", [ \"ao.num\" ] ]\n"
" } ,\n"
" \n"
" \"ao_1e_int\": {\n"
" \"overlap\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"kinetic\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"potential_n_e\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"ecp\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"core_hamiltonian\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"overlap_im\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"kinetic_im\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"potential_n_e_im\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"ecp_im\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" , \"core_hamiltonian_im\" : [ \"float\", [ \"ao.num\", \"ao.num\" ] ]\n"
" } ,\n"
" \n"
" \"ao_2e_int\": {\n"
" \"eri\" : [ \"float sparse\", [ \"ao.num\", \"ao.num\", \"ao.num\", \"ao.num\" ] ]\n"
" , \"eri_lr\" : [ \"float sparse\", [ \"ao.num\", \"ao.num\", \"ao.num\", \"ao.num\" ] ]\n"
" , \"eri_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"eri_cholesky\" : [ \"float sparse\", [ \"ao_2e_int.eri_cholesky_num\", \"ao.num\", \"ao.num\" ] ]\n"
" , \"eri_lr_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"eri_lr_cholesky\" : [ \"float sparse\", [ \"ao_2e_int.eri_lr_cholesky_num\", \"ao.num\", \"ao.num\" ] ]\n"
" } ,\n"
" \n"
" \"mo\": {\n"
" \"type\" : [ \"str\" , [] ]\n"
" , \"num\" : [ \"dim\" , [] ]\n"
" , \"coefficient\" : [ \"float\", [ \"mo.num\", \"ao.num\" ] ]\n"
" , \"coefficient_im\" : [ \"float\", [ \"mo.num\", \"ao.num\" ] ]\n"
" , \"class\" : [ \"str\" , [ \"mo.num\" ] ]\n"
" , \"symmetry\" : [ \"str\" , [ \"mo.num\" ] ]\n"
" , \"occupation\" : [ \"float\", [ \"mo.num\" ] ]\n"
" , \"energy\" : [ \"float\", [ \"mo.num\" ] ]\n"
" , \"spin\" : [ \"int\" , [ \"mo.num\" ] ]\n"
" } ,\n"
" \n"
" \"mo_1e_int\": {\n"
" \"overlap\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"kinetic\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"potential_n_e\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"ecp\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"core_hamiltonian\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"overlap_im\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"kinetic_im\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"potential_n_e_im\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"ecp_im\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"core_hamiltonian_im\" : [ \"float\", [ \"mo.num\", \"mo.num\" ] ]\n"
" } ,\n"
" \n"
" \"mo_2e_int\": {\n"
" \"eri\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"eri_lr\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"eri_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"eri_cholesky\" : [ \"float sparse\", [ \"mo_2e_int.eri_cholesky_num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"eri_lr_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"eri_lr_cholesky\" : [ \"float sparse\", [ \"mo_2e_int.eri_lr_cholesky_num\", \"mo.num\", \"mo.num\" ] ]\n"
" } ,\n"
" \n"
" \"determinant\": {\n"
" \"num\" : [ \"dim readonly\" , [] ]\n"
" , \"list\" : [ \"int special\" , [ \"determinant.num\" ] ]\n"
" , \"coefficient\" : [ \"float buffered\", [ \"determinant.num\" ] ]\n"
" } ,\n"
" \n"
" \"csf\": {\n"
" \"num\" : [ \"dim readonly\" , [] ]\n"
" , \"coefficient\" : [ \"float buffered\", [ \"csf.num\" ] ]\n"
" , \"det_coefficient\" : [ \"float sparse\" , [ \"csf.num\", \"determinant.num\" ] ]\n"
" } ,\n"
" \n"
" \"amplitude\": {\n"
" \"single\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"single_exp\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"double\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"double_exp\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"triple\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"triple_exp\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"quadruple\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"quadruple_exp\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" } ,\n"
" \n"
" \"rdm\": {\n"
" \"1e\" : [ \"float\" , [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"1e_up\" : [ \"float\" , [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"1e_dn\" : [ \"float\" , [ \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_upup\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_dndn\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_updn\" : [ \"float sparse\", [ \"mo.num\", \"mo.num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"2e_cholesky\" : [ \"float sparse\", [ \"rdm.2e_cholesky_num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_upup_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"2e_upup_cholesky\" : [ \"float sparse\", [ \"rdm.2e_upup_cholesky_num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_dndn_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"2e_dndn_cholesky\" : [ \"float sparse\", [ \"rdm.2e_dndn_cholesky_num\", \"mo.num\", \"mo.num\" ] ]\n"
" , \"2e_updn_cholesky_num\" : [ \"dim\" , [] ]\n"
" , \"2e_updn_cholesky\" : [ \"float sparse\", [ \"rdm.2e_updn_cholesky_num\", \"mo.num\", \"mo.num\" ] ]\n"
" } ,\n"
" \n"
" \"jastrow\": {\n"
" \"type\" : [ \"str\" , [] ]\n"
" , \"en_num\" : [ \"dim\" , [] ]\n"
" , \"ee_num\" : [ \"dim\" , [] ]\n"
" , \"een_num\" : [ \"dim\" , [] ]\n"
" , \"en\" : [ \"float\" , [ \"jastrow.en_num\" ] ]\n"
" , \"ee\" : [ \"float\" , [ \"jastrow.ee_num\" ] ]\n"
" , \"een\" : [ \"float\" , [ \"jastrow.een_num\" ] ]\n"
" , \"en_nucleus\" : [ \"index\" , [ \"jastrow.en_num\" ] ]\n"
" , \"een_nucleus\" : [ \"index\" , [ \"jastrow.een_num\" ] ]\n"
" , \"ee_scaling\" : [ \"float\" , [] ]\n"
" , \"en_scaling\" : [ \"float\" , [ \"nucleus.num\" ] ]\n"
" } ,\n"
" \n"
" \"qmc\": {\n"
" \"num\" : [ \"dim\" , [] ]\n"
" , \"point\" : [ \"float\", [ \"qmc.num\", \"electron.num\", \"3\" ] ]\n"
" , \"psi\" : [ \"float\", [ \"qmc.num\" ] ]\n"
" , \"e_loc\" : [ \"float\", [ \"qmc.num\" ] ]\n"
" }\n"
" \n"
" }\n"
;
:end:
#+begin_src c :tangle prefix_front.h :noweb yes
<<header>>
#ifndef TREXIO_H
@ -32,6 +283,7 @@ extern "C" {
#include <stdbool.h>
#include <stdint.h>
<<trex_json()>>
typedef int32_t trexio_exit_code;
#+end_src