Skip to content

C API Structure

Cheol Ho Choi edited this page Mar 24, 2024 · 8 revisions

The C API is done using an OQP handle as shown below in the example between C and Fortran.

C Side

typedef double xyz_t[3];

typedef struct oqp_handle_t {
  void *inf;
  xyz_t *xyz;
  double *qn;
  double *mass;
  xyz_t *grad;
  struct molecule *mol_prop;
  struct energy_results *mol_energy;
  struct dft_parameters *dft;
  struct control_parameters *control;
} oqp_handle_t;

Fortran Side

type, bind(C) :: oqp_handle_t
  type(c_ptr) :: inf
  type(c_ptr) :: xyz
  type(c_ptr) :: qn
  type(c_ptr) :: mass
  type(c_ptr) :: grad
  type(c_ptr) :: mol_prop
  type(c_ptr) :: mol_energy
  type(c_ptr) :: dft
  type(c_ptr) :: control
end type

Overall process of utilizing OQP through C API

  1. Use OQP header
/* Using OQP library in the code: */
#include "oqp.h"
  1. Initializing OQP handle
/* Initialize handle: */
struct oqp_handle_t *oqp = oqp_init();
  1. Setting up parameters
/* Set runtime options */
oqp->control->hamilton = 10; // HF
oqp->control->scftype = 1; // RHF
oqp->control->maxit = 30; // Number of SCF iterations
oqp->control->conv = 1.0e-6; // SCF convergence threshold
/* Molecular properties */
oqp->mol_prop->charge = 0;
oqp->mol_prop->mult = 1;
oqp->mol_prop->nelec = 2;
oqp->mol_prop->nelec_A = 1;
oqp->mol_prop->nelec_B = 1;
oqp->mol_prop->nocc = 1;

/* Coordinates */
int natoms = 2;
double x[2] = {-0.64, 0.64};
double y[2] = { 0.00, 0.00};
double z[2] = { 0.00, 0.00};
double q[2] = { 1.00, 1.00};
double mass[2] = { 1.00, 1.00};

/* Fill OQP internal data structures */oqp_set_atoms(oqp, natoms, x, y, z, q, mass);

/* Add basis set to the molecule */
apply_basis(oqp, &Cbas, &Clog, &Cshl);

  1. Running Calculation
/* Compute 1e integrals */
hsandt(oqp, &Clog, &Cshl, &Chst);
/* Guess initial density */
guess_huckel(oqp, &Clog, &Cshl, &Chst, &Cdmt);
/* Run HF energy calculation */
hf_dft_energy(oqp, &Clog, &Cshl, &Chst, &Cdmt, &Cmoe);
  1. Getting the Results
/* The calculation results are available in output files
   as well as in the `oqp` handle */
/* E.g. print energy */
printf("RHF energy of the H2 molecule is: %f Hartree\n",
        oqp->mol_energy->energy);
/* Destroy OQP handle when it is not needed */
oqp_clean(oqp);

[Going back to Developer Documentation](Developer's Documentation)

Clone this wiki locally