Code Monkey home page Code Monkey logo

Comments (9)

jameskermode avatar jameskermode commented on August 17, 2024

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.

davidovitch avatar davidovitch commented on August 17, 2024

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.

jameskermode avatar jameskermode commented on August 17, 2024

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.

davidovitch avatar davidovitch commented on August 17, 2024

Thanks for clarifying. Since I have little Fortran experience, I was not sure if this was me or f90wrap.

from f90wrap.

jameskermode avatar jameskermode commented on August 17, 2024

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.

davidovitch avatar davidovitch commented on August 17, 2024

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.

jameskermode avatar jameskermode commented on August 17, 2024

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.

davidovitch avatar davidovitch commented on August 17, 2024

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.

jameskermode avatar jameskermode commented on August 17, 2024

Marking as closed now.

from f90wrap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.