Linking Excel to Fortran with ctypes; update

In the previous post arrays were created in Python and passed to a Fortran dll as ctype pointers.  The data written to these arrays in the Fortran routines was then converted back to Python arrays with statements such as: nodedisp = npc.as_array(nodedisp).  In fact this conversion is not necessary.  The data passed to the Fortran code is a pointer to the location of the arrays created in Python.  The Fortran results are therefore written to these arrays, which may be read directly from Python using the original array names.  More importantly, I found the results of using the npc.as_array() method to be inconsistent; sometimes returning the expected results, but sometimes empty or corrupted arrays.  Reading the results of the Fortran code using the original Python arrays is therefore both simpler and more reliable.

I have updated the previous post to reflect the revised process, and also updated the download file with the revised code.  The new file, including full open-source code, may be downloaded from:

This entry was posted in Excel, Finite Element Analysis, Fortran, Link to dll, Link to Python, Newton, NumPy and SciPy, VBA and tagged , , , , , , , , , , . Bookmark the permalink.

3 Responses to Linking Excel to Fortran with ctypes; update

  1. kalx says:

    It is even easier to call Fortran from C. I can’t wait until you figure out the FP data type. Just hand a pointer to a double to the Fortran routine and there is no copying of data. It just runs 10x faster.
    Like my grandpappy always told me, cut out the middleman.


    • dougaj4 says:

      Good to see you are still visiting 🙂
      There’s only a few milliseconds to be saved in data transfer time. 99.9% of the computation is in the Fortran, which takes a few seconds to a few minutes (or much longer for more complicated analyses which I’ll be looking at later).

      The only area with real scope for time saving is in the solution of the systems of linear equations, which is done with fairly simplistic algorithms in the code from the book. This is the main reason for introducing Python, which has some much better compiled routines for solving spares matrices.


  2. Pingback: Plane Strain FEA – Non linear staged analysis | Newton Excel Bach, not (just) an Excel Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.