473,465 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using ctypes in Python - arrays and derived types from Fortran 90

1 New Member
Hello Folks!

I try to call from Python several routines contained in a .so library generated from some Fortran 90 routines. All the objects in the library are resolved using the -fPIC flag when compiling the .so.

I have a sample code which looks like

Expand|Select|Wrap|Line Numbers
  1. from numpy import *
  2. from ctypes import *
  3.  
  4. lib_test = cdll.LoadLibrary("./lib_test.so")
  5.  
  6. method = lib_test.__test_MOD_wrap_xbelem
  7. method.restype = None
  8.  
  9. MaxElNod = 3
  10. NumElems = 2
  11. NumNodes = (c_int *NumElems)()
  12. Length   = zeros(NumElems, dtype=double, order="Fortran")
  13. RBMass   = zeros((MaxElNod*NumElems,6,6), dtype=double, order="Fortran")
  14.  
  15. # Call the function in the .so
  16. method(byref(c_int(NumElems)), byref(NumNodes), Length.ctypes.data_as(POINTER(c_double)), RBMass.ctypes.data_as(POINTER(c_double)) )
  17.  
I have few questions I hope you can help.

1) The code works fine as given above. However, I would like to pass the integer array (bold font above) as done with the array of type double. I would be keen to have a code which looks more like this

Expand|Select|Wrap|Line Numbers
  1. NumNodes = zeros(NumElems, dtype=int, order="Fortran")
  2.  
  3. method(byref(c_int(NumElems)), NumNodes.ctypes.data_as(POINTER(c_int)), Length.ctypes.data_as(POINTER(c_double)), RBMass.ctypes.data_as(POINTER(c_double)) )
  4.  
In this second case, the array NumNodes is equal to array([8589934594, 0]), whereas both elements of NumNodes should be equal to 2. I haven't said earlier, but NumNodes is only initialized in Python and it gets the values in the Fortran90 where they are computed in correct way. The correct results are produced with the first sample code. I am not able to figure out what I miss, as I presumed that the similar operations performed on the arrays of type double could be applied to arrays of type int.

2) The other question I have is about derived types. The Fortran 90 code makes use of derived types, and this is the reason that we have decided to create a .so library out of it to work with Python. At the moment, the Python script does not work with a corresponding structure and the fields of the derived types have been given each to a separate variable. This means that several input arguments are passed to a wrapper in the .so, and then appropriate fields are compacted to form a derived type in Fortran90. Arrays are also converted to multi-dimensional entities when it is the case.

I suppose I can ask for your expertise on some further tips. Imagine in Fortran 90 there is a derived type of the form

Expand|Select|Wrap|Line Numbers
  1.  type xbelem
  2.   integer:: NumNodes
  3.   real(8):: Length
  4.   real(8):: RBMass(MaxElNod,6,6)
  5.  end type xbelem
  6.  
which is used in some routines, and it is initialized as

Expand|Select|Wrap|Line Numbers
  1. type(xbelem),intent(inout):: Elem(NumElems)
  2.  
What is the similar structure in Python? and how to pass it to the .so library?

I would like to thank for any help you can provide.
Kind regards,
Andrea.
Dec 6 '11 #1
0 1741

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shalabh Chaturvedi | last post by:
Announcing the initial public draft of: The New Python Part 1 - Types and Objects From the abstract: "Describes the different new-style objects, and how they are related in the new Python...
0
by: Natsu Mizutani | last post by:
Hello, I'm trying to wrap a c++ library using MPI inside with boost.python (or SWIG). I managed to find that calling `MPI::Init()` embeded in a c++ funtion would not work. So, I decided to use...
1
by: muttu2244 | last post by:
Hi Everybody Here i have a query for you, that is How can I refresh the windows desktop using the python script? Thanks in advance for having given a thought on my query. Regards
1
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml...
1
by: thianseng | last post by:
Hi All, Urgent SOS!!! I developed my project using pyton 2.5 under winXP and had an issue in loading my dll if the target user is using Win2000. Initially, I thought it was caused by the dlls I...
2
by: Kevin D. Smith | last post by:
I would like to move files and directories to the Recycle Bin on Windows from Python. I have found some older articles describing how to do this, but they require additional packages to be...
3
by: patelss23 | last post by:
Hello All, I am dealing with this weird bug. I have a function in C and I have written python bindings for it using ctypes. I can call this function for couple of times and then suddenly it...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
1
by: Romesh Khaddar | last post by:
Hi, I know that to access any dll from python one should use ctypes. Till now I haven't been able to get it working (I tried both declarations to initialize). Problem that I think is that the dll...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.