-
Notifications
You must be signed in to change notification settings - Fork 22
Bindings: C
C/C++ bindings are also provided by libRustBCA. Pure C bindings can be provided with cbindgen, but the RustBCA.h file automatically generated frmo cbindgen needs to manually have the math constants added, as those don't transliterate directly between Rust and C. Building libRustBCA with:
cargo build --release --lib
Creates the appropriate shared object library, in this case liblibRustBCA.so, in target/release/. In the main directory, the header file, RustBCA.h, is located with the function and struct signatures of the C bindings.
To use RustBCA's libraries, first, ensure you have updated Python libraries (on linux: apt-get install python3-dev). Then, make sure the RustBCA shared library is accessible to g++. Then, build with:
g++ RustBCA.c
If you've built with the python bindings (--features python), include the python libraries to build:
g++ RustBCA.c -I/usr/include/python3.8 -lpython3.8
The preferred C interface function is currently compound_bca_list_c. This function takes one input, InputCompoundBCA, and outputs and has one output, OutputBCA. These are described below.
InputCompoundBCA is for multi-component targets, but one species of incident ion. It has the structure:
struct InputCompoundBCA {
uintptr_t len;
/// vx, vy, vz
double (*velocities)[3];
double Z1;
double m1;
double Ec1;
double Es1;
uintptr_t num_species_target;
double *Z2;
double *m2;
double *n2;
double *Ec2;
double *Es2;
double *Eb2;
};
Where len is the number of particles, velocities is a list of incident ion velocities, and the material and ion parameters are as stated previously.
struct OutputBCA {
uintptr_t len;
double (*particles)[9];
};
The output of OutputBCA is these parameters:
len: number of particles in OutputBCA
Z, M, E, x, y, z, ux, uy, uz: atomic number, atomic mass (amu) energy (eV), position (angstrom), direction
- x, y, z: starting position in angstroms (Recommended: 0, 0, 0)
- ux, uy, uz: initial direction
- E: energy in eV
- Z1: atomic number of incident ion
- M1: atomic mass of incident ion (amu)
- Ec1: cutoff energy of incident ion (eV)
- Es1: surface binding energy of incident ion (eV)
- Z2: atomic number of material
- M2: atomic mass of material (amu)
- Ec2: cutoff energy of material (eV)
- Es2: surface binding energy of material (eV)
- n2: number density of material (1/angstrom^3)
- Eb2: bulk binding energy of material (eV)
RustBCA.c
#include "RustBCA.h"
#include <iostream>
#include <vector>
int main() {
OutputBCA output;
double velocities[2][3] = {{500000.0, 0.1, 0.0}, {500000.0, 0.1, 0.0}};
double Z[1] = {74.0};
double m[1] = {184.0};
double n[1] = {0.06306};
double Ec[1] = {1.0};
double Es[1] = {8.79};
double Eb[1] = {0.0};
InputCompoundBCA input = {
2,
velocities,
1.0,
1.0,
1.0,
1.0,
1,
Z,
m,
n,
Ec,
Es,
Eb
};
//output = simple_bca_c(0., 0., 0., 0.5, 0.5, 0.00, 2000.0, 2.0, 4.0, 1.0, 0.0, 74.0, 184.0, 1.0, 8.79, 0.06306, 0.0);
output = compound_bca_list_c(input);
std::cout << "Particle 1 Z: ";
std::cout << output.particles[0][0];
std::cout << std::endl;
std::cout << "Particle 1 E [eV]: ";
std::cout << output.particles[0][2];
std::cout << std::endl;
std::cout << "Particle 2 Z: ";
std::cout << output.particles[1][0];
std::cout << std::endl;
std::cout << "Particle 2 E [eV]: ";
std::cout << output.particles[1][2];
std::cout << std::endl;
return 0.;
}
Click Pages above to see all pages in the Wiki.
This page is a work in progress.