Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perhaps I am wrong, but I don't think f2py can handle the <code>module</code>+<code>contains</code> feature of Fortran 90. If you turn your code into</p> <pre><code>function foo(x,y) result(z) integer, parameter :: dp = selected_real_kind(15) real(kind=dp), intent(in) :: x integer, parameter :: nelem = 3 integer, dimension(3), parameter : = (/3, 1, 2/) integer, dimension(nelem), intent(in) :: y real(kind=dp) :: z z = sum(b*y*x) end function </code></pre> <p>and compile it as before, it works:</p> <pre><code> &gt;&gt;&gt; x = 1.0000000000 &gt;&gt;&gt; y = [2, 3, 4] &gt;&gt;&gt; moddata.foo(x,y) 17.0 </code></pre> <hr> <p><strong>EDIT</strong></p> <p>The answer to <a href="https://stackoverflow.com/questions/10913003/f2py-array-valued-functions">this question on SO</a> says that f2py <em>doesn't understand how to turn a Fortran function into a python function</em>. So I changed the <code>function foo</code> to <code>subroutine foo2</code> and then compiled as <code>f2py moddata.f90 -m moddata</code> and got as output</p> <pre><code>Reading fortran codes... Reading file 'moddata.f90' (format:free) Post-processing... Block: moddata Block: moddata In: :moddata:moddata.f90:moddata get_parameters: got "invalid syntax (&lt;string&gt;, line 1)" on '(/3, 1, 2/)' Block: foo2 Post-processing (stage 2)... Block: moddata Block: unknown_interface Block: moddata Block: foo2 Building modules... Building module "moddata"... Constructing F90 module support for "moddata"... Variables: nelem b dp Constructing wrapper function "moddata.foo2"... getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="&lt;C typespec&gt;")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file). getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="&lt;C typespec&gt;")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file). getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="&lt;C typespec&gt;")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file). getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="&lt;C typespec&gt;")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file). getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="&lt;C typespec&gt;")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file). getctype: "real(kind=dp)" is mapped to C "float" (to override define dict(real = dict(dp="&lt;C typespec&gt;")) in /home/jdwood/Documents/Physics/Fortran/tests/f2py/.f2py_f2cmap file). z = foo2(x,y) Wrote C/API module "moddata" to file "./moddatamodule.c" Fortran 90 wrappers are saved to "./moddata-f2pywrappers2.f90" </code></pre> <p>So it does look like the double-precision was being lost, so following the suggestion to edit a file called <code>.f2py_f2cmap</code>, I did so and got no errors when it came to <code>dp</code>. However, it still gives an error for <code>nelem</code>, so I can think of two solutions to this:</p> <ol> <li>Stick with using <code>3</code> in place of <code>nelem</code></li> <li>Pass <code>nelem</code> and <code>b</code> as variables to the subroutine</li> </ol> <p>I also found, when using the <code>parameter(b = (/3.d0, 1.d0, 2.d0/) )</code> line, that I was getting a warning that read</p> <pre><code>analyzeline: Failed to evaluate '/3.e0+1j*( 1.e0+1j*( 2.e0/)'. Ignoring: invalid syntax (&lt;string&gt;, line 1) </code></pre> <p>I am not sure what to make of this. However, I do get 18 as an answer when I use <code>x=1.0</code> and <code>y=(/ 3, 6, 2/)</code> (both using python and using the module in a Fortran program).</p> <p>In short, <strong>completely avoid functions</strong> when you are using f2py.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload