Skip to content

Conversation

@sloede
Copy link
Member

@sloede sloede commented Jul 6, 2023

I noticed in MESSy that the current Fortran bindings might be hard to use for people not familiar with C-style programming or calling C functions from Fortran. Specifically, the use of trim(adjustl(string)) // c_null_char for string arguments such as

CALL trixi_initialize(TRIM(ADJUSTL(trixi_runtime_path))//char(0))

(source) or the awkward comparison of boolean-intent return values against 0/1 such as

IF (trixi_is_finished(handle) .ne. 0) THEN

(source).

This PR changes the Fortran bindings to our C API such that all subroutines/functions that expect a string argument can now be supplied a regular Fortran string (i.e., a scalar of type character) and do the necessary conversions (trimming, appending null) internally. Similarly, functions expecting/returning a boolean value in the form of an integer (C-style booleans) now will work with Fortran logicals. Thus, the above two snippets can now be written as

CALL trixi_initialize(trixi_runtime_path)

and

IF (trixi_is_finished(handle)) THEN

For full control/as a fallback, the original functions/subroutines are available with a _c prefix, e.g., the Fortran subroutine julia_eval_string_c will directly call the C function julia_eval_string, while the Fortran subroutine julia_eval_string will exhibit the convenience behavior described above an internally call julia_eval_string_c.

Please let me know what you think about this @bgeihe @KerstinHartung, since I am not sure if this makes it easier to use libtrixi for Fortran or is just another source of confusion. (Note that we do not have any documentation of the official API yet, so please do not hold this against this PR 😬)

@codecov
Copy link

codecov bot commented Jul 6, 2023

Codecov Report

Merging #47 (f4dcda4) into main (a30fa0e) will increase coverage by 0.22%.
The diff coverage is 81.25%.

❗ Current head f4dcda4 differs from pull request most recent head 78ded28. Consider uploading reports for the commit 78ded28 to get more accurate results

@@            Coverage Diff             @@
##             main      #47      +/-   ##
==========================================
+ Coverage   74.43%   74.65%   +0.22%     
==========================================
  Files           4        5       +1     
  Lines         133      146      +13     
==========================================
+ Hits           99      109      +10     
- Misses         34       37       +3     
Flag Coverage Δ
unittests 74.65% <81.25%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/trixi.f90 75.00% <75.00%> (ø)
examples/simple_trixi_controller.f90 60.00% <100.00%> (+1.66%) ⬆️

Copy link
Collaborator

@benegee benegee left a comment

Choose a reason for hiding this comment

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

I have no idea of these Fortran peculiarities, but I would really like it for convenience.

@sloede sloede marked this pull request as ready for review July 6, 2023 13:37
@sloede sloede enabled auto-merge (squash) July 6, 2023 13:37
@sloede sloede merged commit e73f159 into main Jul 6, 2023
@sloede sloede deleted the msl/fortran-convenience-bindings branch July 6, 2023 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants