Comments (9)
I can confirm this test is currently broken. I will fix it ASAP, and set up automated unit testing to prevent these kinds of regressions occurring in future.
from f90wrap.
There is one more thing I am not sure about: are wrapped subroutines with variables as intent(out)
supposed to return those values when calling them in Python? I can do this with a wrapped function:
function return_value_func(val_in) result(val_out)
integer(4) val_in, val_out
val_out = val_in + 10
end function return_value_func
will be wrapped such that when calling return_value_func()
in Python returns the expected value, for example:
from somemodule import library as lib
val_in = 1
val_out = lib.return_value_func(val_in)
val_out
>>> 11
However, a similar example with a subroutine
does not return the output variables.
subroutine return_value_sub(val_in, val_out)
integer(4), INTENT(in) :: val_in
integer(4), INTENT(out) :: val_out
val_out = val_in + 10
end subroutine return_value_sub
Further, both input and output variables are required when calling the subroutine from Python:
from somemodule import library as lib
val_in = 1
val_out = 0
val_out2 = lib.return_value_sub(val_in=val_in, val_out=val_out)
val_out
>>> 0
val_out2
>>> None
from f90wrap.
This is a bug in f90wrap. Fortran intent(out)
arguments should become return values in Python. This is a regression in the current code which I need to look into.
from f90wrap.
Thanks for clarifying. Since I have little Fortran experience, I was not sure if this was me or f90wrap.
from f90wrap.
This bug should be fixed in commit c1a43b5 - let me know if this works for you and I will close this issue. Sorry for the delay in finding time to fix this.
from f90wrap.
Sorry for the late response. This issue is indeed fixed now, thanks!!
I was also trying this with an array as output, for example:
SUBROUTINE create_array(array)
REAL(idp), DIMENSION(:), ALLOCATABLE, INTENT(out) :: array
INTEGER :: i
if (ALLOCATED(array)) DEALLOCATE(array)
ALLOCATE(array(10))
DO i=1,10
array(i) = i*10.0_idp
ENDDO
END SUBROUTINE create_array
but then I realized this actually doesn't work with a pointer/allocable as a subroutine argument based on the output:
DEBUG:root:removing routine create_array due to allocatable/pointer arguments
I guess this means that in general, when sharing arrays between Python and Fortran, the allocation should either take place in Python (and can be accessed/modified in Fortran when passing as an inout
argument in a subroutine), or should be allocated in Fortran and can be accessed in Python as an attribute of a module or derived type?
from f90wrap.
This is correct - allocatable subroutine arguments are not currently supported. However, allocatable arrays can be used inside derived types, so you can allocate/deallocate from Fortran as well as Python.
Here's a common pattern I use with constructor and destructor routines (code not tested...)
module derivedtype_module
type example
real(dp), allocatable :: x(:)
end type
interface initialise
module procedure example_initialise
end interface
interface finalise
module procedure example_finalise
end interface
contains
subroutine example_initialise(this, size)
type(example), intent(inout) :: this
integer, intent(in) :: size
call finalise(this)
allocate(this%x(size))
end subroutine
subroutine example_finalise(this)
type(example), intent(inout) :: this
if (allocated(this%x)) deallocate(this%x)
end subroutine
end module
from f90wrap.
Thanks! That is a quite nice example of how to deal with the allocation/deallocation from either Python or Fortran side. I've included the same principle in one of the derived type examples I just created in PR #21.
from f90wrap.
Marking as closed now.
from f90wrap.
Related Issues (20)
- Issues with Compiling MPI Program HOT 3
- Support for Intel oneAPI2022
- multidimensional arrays in derived types broken if compiled with long integers as default HOT 1
- Python 3.11 support HOT 8
- Pre-build f90wrap wheels for windows HOT 3
- Derived types containing allocatable character arrays
- `integer, value, intent(in)`/`integer, value` arguments converted to/wrapped as `real`
- Wrong module class name in a call to `f90wrap.runtime.FortranDerivedTypeArray` HOT 2
- numpy > 1.23 support HOT 2
- Switch to pyproject.toml based build system HOT 7
- duplicate symbol in formal argument list HOT 2
- Issues on Mac OS X - for f90wrap 0.2.12 HOT 1
- BUG: Routines including procedure arguments (callbacks) are excluded HOT 1
- Can `f90wrap`parse FORD docstrings?
- unexpected removal of procedures associated with derived types HOT 1
- install fails on macos 14 HOT 4
- Binary Wheels for Mac HOT 1
- example with fortran to python callback function fails with as of f90wrap v0.2.13: "bad argument to internal function" HOT 1
- Parser fails on asterisk in subroutine argument list HOT 2
- Fix Windows wheels HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from f90wrap.