Skip to content

Add Fortran API#174

Open
awvwgk wants to merge 10 commits intowavefunction91:masterfrom
awvwgk:fortran-api
Open

Add Fortran API#174
awvwgk wants to merge 10 commits intowavefunction91:masterfrom
awvwgk:fortran-api

Conversation

@awvwgk
Copy link
Collaborator

@awvwgk awvwgk commented Jan 28, 2026

Closes #66

@awvwgk awvwgk self-assigned this Jan 28, 2026
@awvwgk awvwgk added the enhancement New feature or request label Jan 28, 2026
@awvwgk awvwgk force-pushed the fortran-api branch 5 times, most recently from 1832b49 to 59ab170 Compare January 28, 2026 13:43
@awvwgk awvwgk changed the title [WIP] Add Fortran API Add Fortran API Jan 28, 2026
@awvwgk awvwgk added the Fortran API Related to the Fortran API label Jan 28, 2026
@awvwgk awvwgk force-pushed the fortran-api branch 3 times, most recently from fcaf2e3 to d2fbfbc Compare January 29, 2026 21:01
- provide C enums
- add atom and molecule types
- add basis set and shell definitions
- add molecule grid and runtime environment
- add load balancer to C API
- add molecular weights for C API
- add functional class wrapping ExchCXX
- add xc integrator and matrix type
- add references for functionals
- add support for reading and writing from HDF5 in C
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds Fortran/C bindings to GauXC to enable integration with legacy Fortran codes. The implementation introduces a C API layer and Fortran interfaces that bind to the C functions using iso_c_binding.

Changes:

  • Added C API with headers and implementation for core functionality (molecule, basisset, matrix, integrator, etc.)
  • Added Fortran modules that interface with the C API using iso_c_binding
  • Updated build system to support optional C and Fortran API compilation via CMake options
  • Added test coverage for C API interoperability in existing test files

Reviewed changes

Copilot reviewed 66 out of 66 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
CMakeLists.txt Added GAUXC_ENABLE_C and GAUXC_ENABLE_FORTRAN options with build logic
src/CMakeLists.txt Conditional compilation of C/Fortran sources based on enabled features
include/gauxc/*.h C API headers for all major components (molecule, basisset, matrix, integrator, etc.)
src/c_*.cxx C API implementation wrapping C++ classes
src/.f90, src/.F90 Fortran modules providing iso_c_binding interfaces to C API
include/gauxc/util/c_*.hpp Helper utilities for C-C++ interop (pointer casting, conversions)
include/gauxc/gauxc_config.{h,f}.in Configuration templates for C/Fortran feature detection
tests/moltypes_test.cxx Added C API interoperability tests
src/external/c_hdf5_{read,write}.cxx C API for HDF5 I/O operations
src/external/hdf5_{read,write}.f90 Fortran wrappers for HDF5 operations
Comments suppressed due to low confidence (1)

include/gauxc/gauxc_config.h.in:14

  • The configuration macro GAUXC_HAS_C is defined in gauxc_config.f.in (line 1) but missing from gauxc_config.h.in. This creates an inconsistency where C/C++ code won't have access to the GAUXC_HAS_C macro to conditionally compile C API code, even though it's available in Fortran. Add #cmakedefine GAUXC_HAS_C to gauxc_config.h.in.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wavefunction91 wavefunction91 mentioned this pull request Feb 6, 2026
15 tasks
#else
& (status) &
#endif
& result(rt) bind(c)
Copy link

@kjh-giesbertz kjh-giesbertz Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure that the c variant is called which transforms the fortran MPI communicator

Suggested change
& result(rt) bind(c)
& result(rt) bind(c, name="gauxc_runtime_environment_new_fortran")

delete detail::get_basisset_ptr(*basis);
basis->ptr = nullptr;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some additional routines to querry info in basisset object. I found it useful to check during debugging.

Suggested change
int32_t gauxc_basisset_nshells(GauXCStatus* status, const GauXCBasisSet basis) {
status->code = 0;
if (basis.ptr == nullptr) {
status->code = 1;
status->message = "No basis set associated to this object";
return 0;
}
return detail::get_basisset_ptr(basis)->nshells();
}
int32_t gauxc_basisset_ngtos(GauXCStatus* status, const GauXCBasisSet basis) {
status->code = 0;
if (basis.ptr == nullptr) {
status->code = 1;
status->message = "No basis set associated to this object";
return 0;
}
return detail::get_basisset_ptr(basis)->nbf();
}

!> @return Pointer to the newly created basis set object
type(gauxc_basisset_type) :: basis
end function gauxc_basisset_new_from_shells
end interface

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface the additional basis set routines also from fortran

Suggested change
end interface
integer(c_int32_t) function gauxc_basisset_nshells(status, basis) bind(c)
import :: c_int32_t, gauxc_status_type, gauxc_basisset_type
implicit none
!> @param status Status of the operation
type(gauxc_status_type), intent(out) :: status
type(gauxc_basisset_type), value :: basis
end function
integer(c_int32_t) function gauxc_basisset_ngtos(status, basis) bind(c)
import :: c_int32_t, gauxc_status_type, gauxc_basisset_type
implicit none
!> @param status Status of the operation
type(gauxc_status_type), intent(out) :: status
type(gauxc_basisset_type), value :: basis
end function
end interface

}
return env;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a variant which additionally transforms a fortran MPI communicator to the c-version

Suggested change
GauXCRuntimeEnvironment gauxc_runtime_environment_new_fortran(
GauXCStatus* status
GAUXC_MPI_CODE(, MPI_Fint f_comm)
) {
MPI_Comm c_comm;
c_comm = MPI_Comm_f2c(f_comm);
return gauxc_runtime_environment_new(status GAUXC_MPI_CODE(, c_comm));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Fortran API Related to the Fortran API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Fortran/C Bindings

2 participants