Skip to content

Commit

Permalink
update capi
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMVale committed Jun 4, 2024
1 parent d5cfd96 commit 9d74f30
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 53 deletions.
1 change: 1 addition & 0 deletions c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the C interface for calling the Fortran version of ODRPACK.
65 changes: 39 additions & 26 deletions include/odrpack.h → c/include/ordpack/odrpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
#define ODRPACK_EXTERN extern
#endif

typedef void (*minpack_func)(
int /* n */,
const double * /* x */,
double * /* fvec */,
int * /* iflag */,
void * /* udata */);

/**
* @brief User-supplied function for evaluating the model, computing predicted values and their Jacobians.
*
Expand All @@ -37,23 +30,43 @@ typedef void (*minpack_func)(
* 1 - current `beta` and `x+delta` are not acceptable; ODRPACK95 should select values closer to most recently used values if possible,
* -1 - current `beta` and `x+delta` are not acceptable; ODRPACK95 should stop.
*/
typedef void (*odrpack_fcn)(
int /* n */,
int /* m */,
int /* np */,
int /* nq */,
int /* ldn */,
int /* ldm */,
int /* ldnp */,
const double * /* beta */,
const double * /* xplusd */,
const int * /* ifixb */,
const int * /* ifixx */,
int /* ldifx */,
int /* ideval */,
double * /* f */,
double * /* fjacb */,
double * /* fjacd */,
int * /* istop */
ODRPACK_EXTERN typedef void (*odrpack_fcn)(
int n,
int m,
int np,
int nq,
int ldn,
int ldm,
int ldnp,
const double *beta,
const double *xplusd,
const int *ifixb,
const int *ifixx,
int ldifx,
int ideval,
double *f,
double *fjacb,
double *fjacd,
int *istop);

);
/**
* @brief Wrapper for the ODR routine including only mandatory arguments.
*
* @param fcn User-supplied subroutine for evaluating the model.
* @param n Number of observations.
* @param m Number of columns of data in the independent variable.
* @param np Number of function parameters.
* @param nq Number of responses per observation.
* @param beta Function parameters. Array of size np.
* @param y Dependent variable. Unused when the model is implicit. 2D array of size n x nq.
* @param x Explanatory variable. 2D array of size n x m.
*/
ODRPACK_EXTERN void odr_c(
odrpack_fcn fcn,
int n,
int m,
int np,
int nq,
double *beta,
const double *y,
const double *x);
56 changes: 29 additions & 27 deletions src/odrpack_capi.f90 → c/odrpack_c.f90
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module odrpack_capi
use, intrinsic :: iso_c_binding, only : c_int, c_double, c_funptr, c_ptr, c_f_procpointer
module odrpack_c
use, intrinsic :: iso_c_binding, only : c_int, c_double
use odrpack, only: odr
use odrpack_kinds, only: wp
implicit none
private

public :: odr_c

abstract interface
subroutine fcn_tc(n, m, np, nq, ldn, ldm, ldnp, beta, xplusd, ifixb, ifixx, ldifx, &
ideval, f, fjacb, fjacd, istop) bind(c)
Expand All @@ -27,11 +29,34 @@ subroutine fcn_tc(n, m, np, nq, ldn, ldm, ldnp, beta, xplusd, ifixb, ifixx, ldif
real(c_double), intent(out) :: fjacb(ldn, ldnp, nq)
real(c_double), intent(out) :: fjacd(ldn, ldm, nq)
integer(c_int), intent(out) :: istop
end subroutine fcn_tc
end subroutine
end interface

contains

subroutine odr_c(fcn, n, m, np, nq, beta, y, x) bind(c)
!! "Short" wrapper for the ODR routine including only mandatory arguments.
procedure(fcn_tc) :: fcn
!! User-supplied subroutine for evaluating the model.
integer(c_int), intent(in), value :: n
!! Number of observations.
integer(c_int), intent(in), value :: m
!! Number of columns of data in the independent variable.
integer(c_int), intent(in), value :: np
!! Number of function parameters.
integer(c_int), intent(in), value :: nq
!! Number of responses per observation.
real(c_double), intent(inout) :: beta(np)
!! Function parameters.
real(c_double), intent(in) :: y(n, nq)
!! Dependent variable. Unused when the model is implicit.
real(c_double), intent(in) :: x(n, m)
!! Explanatory variable.

call odr(fcn, n, m, np, nq, beta, y, x)

end subroutine odr_c

! subroutine odr_c( &
! fcn, &
! n, m, np, nq, &
Expand Down Expand Up @@ -118,27 +143,4 @@ end subroutine fcn_tc

! end subroutine

subroutine odr_c(fcn, n, m, np, nq, beta, y, x) bind(c)
!! "Short" wrapper for the ODR routine including only mandatory arguments.
procedure(fcn_tc) :: fcn
!! User-supplied subroutine for evaluating the model.
integer(c_int), intent(in), value :: n
!! Number of observations.
integer(c_int), intent(in), value :: m
!! Number of columns of data in the independent variable.
integer(c_int), intent(in), value :: np
!! Number of function parameters.
integer(c_int), intent(in), value :: nq
!! Number of responses per observation.
real(c_double), intent(inout) :: beta(np)
!! Function parameters.
real(c_double), intent(in) :: y(n, nq)
!! Dependent variable. Unused when the model is implicit.
real(c_double), intent(in) :: x(n, m)
!! Explanatory variable.

call odr(fcn, n, m, np, nq, beta, y, x)

end subroutine

end module odrpack_capi
end module odrpack_c

0 comments on commit 9d74f30

Please sign in to comment.