Skip to content

Add Fortran API#174

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

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

Conversation

@awvwgk
Copy link
Copy Markdown
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
Comment thread src/runtime_environment.F90 Outdated
Comment thread src/runtime_environment.F90 Outdated
Comment thread src/molecule.f90 Outdated
Comment thread src/molecule.f90 Outdated
Comment thread src/molecule.f90 Outdated
Comment thread src/functional.f90 Outdated
Comment thread src/functional.f90 Outdated
@awvwgk
Copy link
Copy Markdown
Collaborator Author

awvwgk commented Feb 23, 2026

Current version fails with OpenMPI due to missing f2c API in mpi_f08 module.

Copy link
Copy Markdown

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

Copilot reviewed 28 out of 28 changed files in this pull request and generated 13 comments.


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

)
if(GAUXC_ENABLE_MPI)
target_link_libraries( gauxc PUBLIC MPI::MPI_Fortran )
set( GAUXC_HAS_MPI_F08 TRUE CACHE BOOL "GauXC has MPI Fortran 2008 bindings" FORCE )
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

This unconditionally forces GAUXC_HAS_MPI_F08=TRUE whenever MPI is enabled, which can break builds on MPI implementations that do not provide the mpi_f08 module (or do not support the nonstandard %mpi_val access used later). Instead of forcing this flag, detect F08 support (e.g., via CMake/FindMPI variables like MPI_Fortran_HAVE_F08_MODULE, or a try_compile that use mpi_f08) and only define GAUXC_HAS_MPI_F08 when it is actually available; otherwise keep the fallback path that uses use mpi.

Suggested change
set( GAUXC_HAS_MPI_F08 TRUE CACHE BOOL "GauXC has MPI Fortran 2008 bindings" FORCE )
if(DEFINED MPI_Fortran_HAVE_F08_MODULE AND MPI_Fortran_HAVE_F08_MODULE)
set( GAUXC_HAS_MPI_F08 TRUE CACHE BOOL "GauXC has MPI Fortran 2008 bindings" )
else()
set( GAUXC_HAS_MPI_F08 FALSE CACHE BOOL "GauXC has MPI Fortran 2008 bindings" )
endif()

Copilot uses AI. Check for mistakes.
Comment thread src/fortran-api/runtime_environment.F90
Comment thread src/fortran-api/runtime_environment.F90
Comment thread src/fortran-api/runtime_environment.F90
Comment thread src/fortran-api/runtime_environment.F90
Comment thread tests/fortran_main.F90
Comment on lines +19 to +22
#ifdef GAUXC_HAS_MPI
call MPI_Init(mpi_ierr)
if (mpi_ierr /= MPI_SUCCESS) error stop 1
#endif
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

The test runner always use mpi_f08 when GAUXC_HAS_MPI is set, but the rest of the codebase has conditional support for both mpi and mpi_f08. If GAUXC_HAS_MPI is true but mpi_f08 is not available, this test binary will not compile. Additionally, it calls MPI_Init/MPI_Finalize unconditionally; since MPI_Initialized/MPI_Finalized are already imported, it would be safer to guard initialization/finalization to avoid runtime errors in environments where MPI is already initialized or finalized.

Copilot uses AI. Check for mistakes.
Comment thread tests/fortran_main.F90
Comment on lines +35 to +38
#ifdef GAUXC_HAS_MPI
call MPI_Finalize(mpi_ierr)
if (mpi_ierr /= MPI_SUCCESS) error stop 1
#endif
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

The test runner always use mpi_f08 when GAUXC_HAS_MPI is set, but the rest of the codebase has conditional support for both mpi and mpi_f08. If GAUXC_HAS_MPI is true but mpi_f08 is not available, this test binary will not compile. Additionally, it calls MPI_Init/MPI_Finalize unconditionally; since MPI_Initialized/MPI_Finalized are already imported, it would be safer to guard initialization/finalization to avoid runtime errors in environments where MPI is already initialized or finalized.

Copilot uses AI. Check for mistakes.
Comment on lines +40 to +48
character(kind=c_char, len=:), allocatable :: msg
character(kind=c_char), pointer :: pmsg(:)

if (c_associated(status%message)) then
slen = strlen(status%message)
call c_f_pointer(status%message, pmsg, [slen])
msg = transfer(pmsg(1:slen), "")
else
msg = ""
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

Using transfer(pmsg(1:slen), \"\") is not portable because the mold \"\" is default character kind, which may differ from c_char (and transfer does not perform kind conversion). This can produce incorrect results or fail on compilers where default character kind is not 1 byte. Prefer constructing msg using a c_char mold (e.g., allocate msg with character(kind=c_char,len=slen) and transfer with msg as the mold, or copy element-wise from pmsg).

Suggested change
character(kind=c_char, len=:), allocatable :: msg
character(kind=c_char), pointer :: pmsg(:)
if (c_associated(status%message)) then
slen = strlen(status%message)
call c_f_pointer(status%message, pmsg, [slen])
msg = transfer(pmsg(1:slen), "")
else
msg = ""
integer(c_int) :: i
character(kind=c_char, len=:), allocatable :: msg
character(kind=c_char), pointer :: pmsg(:)
if (c_associated(status%message)) then
slen = strlen(status%message)
call c_f_pointer(status%message, pmsg, [slen])
allocate(character(kind=c_char, len=slen) :: msg)
do i = 1, slen
msg(i:i) = pmsg(i)
end do
else
allocate(character(kind=c_char, len=0) :: msg)

Copilot uses AI. Check for mistakes.
Comment thread cmake/gauxc-config.cmake.in
Comment thread cmake/gauxc-test-drive.cmake
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

3 participants