Re: [f2py] Question about reading fortran codes in Python

David Ochoa <[email protected]> Sat, 24 Nov 2012 23:06:21 -0200
Newsgroups gmane.comp.python.f2py.user
Message-ID <CADFoYWpMsUC3ePtEkLSyuDe_HathpMa7=jWkCK_ZkkvTEAJ7RA@mail.gmail.com>
--===============4309217166792441293==
Content-Type: multipart/alternative; boundary=bcaec54fae3aff677004cf476de6

--bcaec54fae3aff677004cf476de6
Content-Type: text/plain; charset=ISO-8859-1

Razib:

Hi! I've been using f2py lately and i needed something similar to what you
describe and i managed to get it working. Let's suppose that i have three
files, two f95 and one py. The subroutine foo is called from inside its own
file, from python, and (passed as external) from another fortran file.

Is obviously only a workaround, and it will become very complex if a lot of
functions are used/shared between files. But it's helping me to have an
organized way to develop them.

Hope it help,
David O.

fortran1.f95 -> fortran1.so
------

SUBROUTINE foo(arg1,arg2...)
...
END SUBROUTINE foo

SUBROUTINE bar(arg,...)
   ...
   CALL foo(...)
   ...
END SUBROUTINE bar

---------

fortran2.f95  -> fortran2.so
------

SUBROUTINE fomatic(*foo*,arg1,arg2,...
     IMPLICIT none
    * EXTERNAL foo*
     INTEGER arg1...

...
    CALL foo(...)

....
END SUBROUTINE fomatic

-------

------
program.py
-------

....
import fortran1 as f1
import fortran2 as f2


....
f1.foo (arg...)

...
f1.bar(arg....)

.....
f2.fomatic(*f1.foo*,arg1....)

...

-------



On Sat, Nov 24, 2012 at 8:47 PM, Hariprasath Ganesan <
[email protected]> wrote:

> Hi Tamal,
>
> I would propose you a simple trick....First of all make sure that your
> complex Fortran code is bug free....Once that is done ....you can play it
> with f2py....
>
> I will quote you a short example by the way...
>
> ---------------------------------------------------------------
>
> subroutine main(list of arguments)
>
>     implicit none
> !   Declaration part
>
> !f2py call
>     -----
>     call example(list of arguments)
>     -----
>     ----
>
> end subroutine main
> subroutine example(list of arguments)
>     implicit none
> !   Declaration part
>     --------------------
>    ---------------------
>
> end subroutine example
>
>
> -------------------------------------------------------------------------------------------------------------------
>
> In the above mentioned example both the subroutines are in the same file
> say "check.f90"......But you should notice one thing that your utility
> subroutine named "example " do not have a f2py declaration part...because
> it will make internal calls among Fortran subroutines by "call" statement
> in your subroutine named "main" ,which python..i.e f2py need not to know
> necessarily....I hope you got my point.....
>
> In a similar fashion you can develop a complex subroutines callable &
> executable by f2py.....
>
> NOTE: If you are getting an error....Reasons could be the following...
>
> 1.)  Datatype Incompatibility between Python and Fortran...
> 2.)  No of arguments mismatch during call
> 3.)  wrong Fortran Syntax in the source code
> 4.)  Logical error (but  usually don't affect f2py compilation)
>
> I hope,I tried to address your problem,,,,,
>
> Regards,
> Hari.
>
> On Sat, Nov 24, 2012 at 7:10 PM, Razibul Islam <tamal105-/[email protected]> wrote:
>
>> Hello Hari,
>> Thanks for the answer. I am also a new user of f2py. I have a little bit
>> similar problem. I have a code which consists of several file and function.
>> I have tried to compile single function with f2py and it works. But could
>> you please tell me how to compile a complete program with several files and
>> subroutines?
>> Thanks in Advance
>>
>> Razib
>>
>>   ------------------------------
>> *From:* Hariprasath Ganesan <[email protected]>
>> *To:* Bahtiyor Zohidov <bahtiyor_zohidov-JGs/[email protected]>; For users of the f2py
>> program <f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]>
>> *Sent:* Tuesday, November 20, 2012 3:24 PM
>> *Subject:* Re: [f2py] Question about reading fortran codes in Python
>>
>> Hi Bahithyor,
>>
>> Let we assume you have an executable Fortran file say name.f90 ( I
>> assumed Fortran 90)....Then if you are using Linux and I assumed that you
>> have Python,numpy and f2py installed readily...
>>
>> Open your command terminal....type f2py...
>> You will see an usage message on Terminal with version & license detail
>> at the end....
>>
>> then type the command as follows on terminal
>> (eg: My Fortran file - Hello.f90)
>>
>> $ f2py -c -m tom Hello.f90
>>
>> --------------------------------------
>> If your Fortran file is bug free then you would see  a file named
>> 'tom.so' which is basically a shared object file readable by python will
>> get generated....
>>
>> For more insight I would give you an example...which I tried first.....
>>
>> My Fortran code:
>>
>> ---------------------------------------------------------------------------
>>
>> SUBROUTINE CHECK (A)
>>    INTEGER :: A
>>    PRINT*, "YOUR f2py IS WORKING FINE"
>>    PRINT*, "ENTERED VALUE IS =",A
>> END SUBROUTINE
>>
>> -------------------------------------------------------------
>> 1.) Save the above script as Hello.f90
>> 2.) In terminal type " f2py -c -m tom Hello.f90"
>> 3.) type ls in terminal
>> 4.) you should see "tom.so" now
>>
>>
>>
>> 5.) type python in terminal
>> 6.)you will get into interpreter
>>     >>> import tom
>>     >>> tom.check(2)
>>     YOUR f2py IS WORKING  FINE
>>     ENTERED VALUE IS =    2
>>
>>
>> *************** GOOD LUCK **************************
>>
>>
>>
>> REGARDS,
>> HARI.
>>
>>
>>
>> On Tue, Nov 20, 2012 at 9:59 AM, Bahtiyor Zohidov <
>> bahtiyor_zohidov-JGs/[email protected]> wrote:
>>
>> Dear Python users
>>
>> I would like to know how to import Fortran codes to Python(Because I have
>> fortran codes which I should use them in Python) .. I tried to setup f2py.
>> I couldn't figure out the steps of setup and what to do. I am beginner in
>> f2py. Please, help me!!!
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>>
>>
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>>
>>
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>>
>>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>

--bcaec54fae3aff677004cf476de6
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<span class=3D"" style>Razib</span>:<div><br></div><div>Hi! I&#39;ve been u=
sing f2py lately and i needed something similar to what you describe and i =
managed to get it working. Let&#39;s suppose that i have three files, two f=
95 and one <span class=3D"" style>py</span>. The subroutine foo is called f=
rom inside its own file, from python, and (passed as external) from another=
 <span class=3D"" style>fortran</span> file.</div>

<div><br></div><div>Is obviously only a workaround, and it will become very=
 complex if a lot of functions are used/shared between files. But it&#39;s =
helping me to have an organized way to develop them.</div><div><br></div>

<div>Hope it help,</div><div>David O.</div><div><br></div><div>fortran1.f95=
 -&gt; fortran1.so</div><div>------</div><blockquote style=3D"margin:0 0 0 =
40px;border:none;padding:0px"><div>SUBROUTINE foo(arg1,arg2...)</div><div>

...</div><div>END SUBROUTINE foo</div><div>=A0</div><div>SUBROUTINE bar(<sp=
an class=3D"" style>arg</span>,...)</div><div>=A0 =A0...</div><div>=A0 =A0C=
ALL foo(...)</div><div>=A0 =A0...</div><div>END SUBROUTINE bar</div></block=
quote><div>
---------</div>
<div><br></div><div>fortran2.f95 =A0-&gt; fortran2.so</div><div>------</div=
><blockquote style=3D"margin:0 0 0 40px;border:none;padding:0px"><div>SUBRO=
UTINE <span class=3D"" style>fomatic</span>(<b>foo</b>,arg1,arg2,...</div><=
div>

=A0 =A0 =A0IMPLICIT none</div><div>=A0 =A0 <b>=A0EXTERNAL foo</b></div><div=
>=A0 =A0 =A0INTEGER arg1...</div><div><br></div><div>...</div><div>=A0 =A0 =
CALL foo(...)</div><div><br></div><div>....</div><div>END SUBROUTINE <span =
class=3D"" style>fomatic</span></div>

</blockquote><div>-------</div><div><br></div><div>------</div><div>program=
.<span class=3D"" style>py</span></div><div>-------</div><blockquote style=
=3D"margin:0 0 0 40px;border:none;padding:0px"><div>....</div><div>import f=
ortran1 as f1</div>

<div>import fortran2 as f2</div><div>=A0</div><div><br></div><div>....</div=
><div>f1.foo (<span class=3D"" style>arg</span>...)</div><div><br></div><di=
v>...</div><div>f1.bar(<span class=3D"" style>arg</span>....)</div><div><br=
>
</div>
<div>.....</div><div>f2.<span class=3D"" style>fomatic</span>(<b>f1.foo</b>=
,arg1....)</div><div><br></div><div>...</div></blockquote><div>-------</div=
><div><br></div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_quot=
e">

On Sat, Nov 24, 2012 at 8:47 PM, Hariprasath Ganesan <span dir=3D"ltr">&lt;=
<a href=3D"mailto:[email protected]" target=3D"_blank">hari.dynamics@=
gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi Tamal,<div><br></div><div>I would propose you a simple trick....First of=
 all make sure that your complex Fortran code is bug free....Once that is d=
one ....you can play it with f2py....</div><div><br></div><div>I will quote=
 you a short example by the way... =A0</div>


<div><br></div><div>-------------------------------------------------------=
--------</div><div><br></div><div>subroutine main(list of arguments)</div><=
div>=A0 =A0=A0</div><div>=A0 =A0 implicit none</div><div>! =A0 Declaration =
part</div>


<div><br></div><div>!f2py call</div><div>=A0 =A0 -----</div><div>=A0 =A0 ca=
ll example(list of arguments)</div><div>=A0 =A0 -----</div><div>=A0 =A0 ---=
-</div><div><br></div><div>end subroutine main</div><div>subroutine example=
(list of arguments)</div>


<div>=A0 =A0=A0implicit none</div><div>! =A0 Declaration part</div><div>=A0=
 =A0 --------------------</div><div>=A0 =A0---------------------</div><div>=
<br></div><div>end subroutine example</div><div><br></div><div>------------=
---------------------------------------------------------------------------=
----------------------------</div>


<div><br></div><div>In the above mentioned example both the subroutines are=
 in the same file say &quot;check.f90&quot;......But you should notice one =
thing that your utility subroutine named &quot;example &quot; do not have a=
 f2py declaration part...because it will make internal calls among Fortran =
subroutines by &quot;call&quot; statement in your subroutine named &quot;ma=
in&quot; ,which python..i.e f2py need not to know necessarily....I hope you=
 got my point.....</div>


<div><br></div><div>In a similar fashion you can develop a complex subrouti=
nes callable &amp; executable by f2py.....</div><div><br></div><div>NOTE: I=
f you are getting an error....Reasons could be the following...</div><div>


<br></div><div>1.) =A0Datatype Incompatibility between Python and Fortran..=
.</div><div>2.) =A0No of arguments mismatch during call</div><div>3.) =A0wr=
ong Fortran Syntax in the source code</div><div>4.) =A0Logical error (but =
=A0usually don&#39;t affect f2py compilation)</div>


<div><br></div><div>I hope,I tried to address your problem,,,,,</div><div><=
br></div><div>Regards,</div><div>Hari.</div><div class=3D"HOEnZb"><div clas=
s=3D"h5"><div><br><div class=3D"gmail_quote">On Sat, Nov 24, 2012 at 7:10 P=
M, Razibul Islam <span dir=3D"ltr">&lt;<a href=3D"mailto:tamal105-/[email protected]=
" target=3D"_blank">tamal105-/[email protected]</a>&gt;</span> wrote:<br>


<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div><div style=3D"font-size:12pt;font-famil=
y:times new roman,new york,times,serif">Hello Hari,<br>Thanks for the answe=
r. I am also a new user of f2py. I have a little bit similar problem. I hav=
e a code which consists of several file and function. I have tried to compi=
le single function with f2py and it works. But could you please tell me how=
 to compile a complete program with several files and subroutines?<br>


Thanks in Advance <br><div><br></div><div>Razib<br></div><div></div><div><b=
r></div>  <div style=3D"font-family:times new roman,new york,times,serif;fo=
nt-size:12pt"> <div style=3D"font-family:times new roman,new york,times,ser=
if;font-size:12pt">


 <div dir=3D"ltr"> <font face=3D"Arial"> <hr size=3D"1">  <b><span style=3D=
"font-weight:bold">From:</span></b> Hariprasath Ganesan &lt;<a href=3D"mail=
to:[email protected]" target=3D"_blank">[email protected]</a>&g=
t;<br> <b><span style=3D"font-weight:bold">To:</span></b> Bahtiyor Zohidov
 &lt;<a href=3D"mailto:bahtiyor_zohidov-JGs/[email protected]" target=3D"_blank">bahtiyor=
_zohidov-JGs/[email protected]</a>&gt;; For users of the f2py program &lt;<a href=3D"mail=
to:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]" target=3D"_blank">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a>&gt;=
 <br>


 <b><span style=3D"font-weight:bold">Sent:</span></b> Tuesday, November 20,=
 2012 3:24 PM<br> <b><span style=3D"font-weight:bold">Subject:</span></b> R=
e: [f2py] Question about reading fortran codes in Python<br> </font> </div>


<div><div> <br>
<div>Hi Bahithyor,<br><br>Let we assume you have an executable Fortran file=
 say name.f90 ( I assumed Fortran 90)....Then if you are using Linux and I =
assumed that you have Python,numpy and f2py installed readily...<br><br>


Open your command terminal....type f2py...<br>
You will see an usage message on Terminal with version &amp; license detail=
 at the end....<br><br>then type the command as follows on terminal<br>(eg:=
 My Fortran file - Hello.f90)<br><br>$ f2py -c -m tom Hello.f90<br><br>



--------------------------------------<br>If your Fortran file is bug free =
then you would see=A0 a file named &#39;tom.so&#39; which is basically a sh=
ared object file readable by python will get generated....<br><br>For more =
insight I would give you an example...which I tried first.....<br>



<br>My Fortran code:<br>---------------------------------------------------=
------------------------<br><pre>SUBROUTINE CHECK (A)
   INTEGER :: A
   PRINT*, &quot;YOUR f2py IS WORKING FINE&quot;
   PRINT*, &quot;ENTERED VALUE IS =3D&quot;,A
END SUBROUTINE<br><br>-----------------------------------------------------=
--------<br>1.) Save the above script as Hello.f90<br>2.) In terminal type =
&quot; f2py -c -m tom Hello.f90&quot;<br>3.) type ls in terminal<br>4.) you=
 should see &quot;tom.so&quot; now<br>



5.) type python in terminal<br>6.)you will get into interpreter<br>    &gt;=
&gt;&gt; import tom<br>    &gt;&gt;&gt; tom.check(2)<br>    YOUR f2py IS WO=
RKING  FINE<br>    ENTERED VALUE IS =3D    2<br><br><br>*************** GOO=
D LUCK **************************<br>



<br>REGARDS,<br>HARI.<br></pre><br><br><div>On Tue, Nov 20, 2012 at 9:59 AM=
, Bahtiyor Zohidov <span dir=3D"ltr">&lt;<a rel=3D"nofollow" href=3D"mailto=
:bahtiyor_zohidov-JGs/[email protected]" target=3D"_blank">bahtiyor_zohidov-JGs/[email protected]</a>&g=
t;</span> wrote:<br>



<blockquote style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex">
<div>Dear Python users<br><br>I would like to know how to import Fortran co=
des to Python(Because I have fortran codes which I should use them in Pytho=
n) .. I tried to setup f2py. I couldn&#39;t figure out the steps of setup a=
nd what to do. I am beginner in f2py. Please, help me!!!</div>




<br>_______________________________________________<br>
f2py-users mailing list<br>
<a rel=3D"nofollow" href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]" target=3D"_blank=
">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br>
<a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users" target=3D"_blank=
">http://cens.ioc.ee/mailman/listinfo/f2py-users</a><br>
<br></blockquote></div><br>
</div><br>_______________________________________________<br>f2py-users mai=
ling list<br><a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]" target=3D"_blank">f2=
py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br><a href=3D"http://cens.ioc.ee/mailman/listinfo/=
f2py-users" target=3D"_blank">http://cens.ioc.ee/mailman/listinfo/f2py-user=
s</a><br>


<br><br> </div></div></div> </div>  </div></div><br>_______________________=
________________________<br>
f2py-users mailing list<br>
<a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]" target=3D"_blank">f2py-users@cens=
.ioc.ee</a><br>
<a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users" target=3D"_blank=
">http://cens.ioc.ee/mailman/listinfo/f2py-users</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
f2py-users mailing list<br>
<a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]</a><br>
<a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users" target=3D"_blank=
">http://cens.ioc.ee/mailman/listinfo/f2py-users</a><br>
<br></blockquote></div><br></div>

--bcaec54fae3aff677004cf476de6--


--===============4309217166792441293==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
f2py-users mailing list
f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
http://cens.ioc.ee/mailman/listinfo/f2py-users

--===============4309217166792441293==--