More on Long Lost

"Long Lost" extras

“Long Lost” extras

This weekend was shooting for my daughter’s short film “Long Lost” at Callan Park in Sydney. The film is based on the story of a French soldier, but coincidentally there was an Australian soldier who went through the same ordeal of losing all memory of his earlier life, and this man lived for many years at the same Callan Park, which was at the time a psychiatric hospital:

The history of forgetting, from shell shock to PTSD

For more details of the film, and to support crowd funding see:

Long Lost, a WW1 short film – crowd funding site

Posted in Bach, Films | Tagged , , , , | Leave a comment

Run Fortran from Excel with Python and F2Py

I have previously posted on the use of the Silverfrost Fortran compiler with Excel (e.g. Linking Excel to Fortran).  More recently I have been concentrating on accessing high performance compiled routines via Python and the Python maths and science libraries Numpy and Scipy (and also the Alglib library, as featured in the previous post).  Numpy also includes a built-in library to link to Fortran code (F2Py), allowing the vast range of Fortran public domain software to be accessed from Python, and hence from Excel.

In this post I will describe linking to a simple function to generate Fibonacci Numbers, and a routine to generate the stiffness matrix for 3D frame analysis.  Later posts will look at incorporating the stiffness matrix routine into a complete Excel based 3D frame analysis program.

The first requirement is to install Python, including Numpy.  As mentioned in the previous post, I can recommend the Anaconda package, but any other package including Numpy should work equally well.

It is also necessary to install a Fortran compiler.  After some trial and error I found that the MinGW (Minimalist GNU for Windows) package worked well.  See Getting Started for installation instructions.

Having installed Python and a Fortran compiler, look for the F2PY folder, which on my system was at:  c:\Users\Doug\Anaconda\Lib\site-packages\numpy\f2py\.  I found the F2py User Guide invaluable for getting started.  If not included in the F2py docs it can be found at: F2Py pdf User Guide.

Having installed the necessary packages, the first sample program in the user guide generates a series of Fibonacci Numbers.  See the user guide for the Fortran code, and the procedure for generating a compiled .pyd file that can be called from Python.  Having generated a module called fib.pyd, containing a single function, fib, the function may be called with the following Python code:

import fib
def pyfib(n):
    return fib.fib(n)

Linking to this from Excel, using ExcelPython:

Function xl_Fib(n)
Dim res As Variant
    On Error GoTo rtnres
    Set res = Py.Call(Py.Module("framep44_2"), "pyfib", Py.Tuple(n))
    xl_Fib = Py.Var(res, 2)
    Exit Function
rtnres:
    xl_Fib = Err.Description
End Function

… which generates the results below:

fib1

The procedure for the stiffness matrix routine is similar. The Fortran source code was based on a function listed in Programming The Finite Element Method.  Having generated the compiled module, frame_for2.pyd, with F2Py, this can be called from Python with the code below:

import frame_for2 as ff2
def getkm3UDF_F2(propm, coord, gamrad):
    propm = propm[0]
    coord = np.array(coord)
    km = ff2.getkm3_f(propm, coord, gamrad)
    return km.tolist()

As before, Linking to this from Excel, using ExcelPython:

Function xl_km3(PropA As Variant, Coord As Variant, Gamrad As Double) As Variant

On Error GoTo RtnErr
PropA = PropA.Value2
Coord = Coord.Value2

Set args = Py.Tuple(PropA, Coord, Gamrad)
xl_km3 = Py.Var(Py.Call(Py.Module("framep44_2"), "getkm3UDF_F2", args))
   Exit Function
RtnErr:
    xl_km3 = Err.Description

End Function

… which generates the 12 x 12 stiffness matrix

km3-1

Posted in Excel, Finite Element Analysis, Fortran, Frame Analysis, Link to Python, Maths, Newton | Tagged , , , , , , , , | 6 Comments

ExcelPython2, Alglib and Spline-Matrix update

The ExcelPython add-in, offering easy access to Python code from Excel VBA, has had a major update to Release 2.  The new version provides new features and simpler installation, but has some incompatibilities with code written for the previous  version, so the Excel Spline-Matrix  spreadsheet has been updated to work with the new release.

Those wishing to generate their own ExcelPython applications can download the latest release from the ExcelPython github site.  If you just want to use the spline-matrix spreadsheet all the necessary ExcelPython files are included in the download; see below for installation details.

As well as the included ExcelPython files the spreadsheet requires:

  1. Python version 2.6 or later, or any version 3, with PYWin32 installed.  The Alglib code does not require Numpy, but the Python interface code does, so this must be installed.  I can recommend the Anaconda package, which is comprehensive with simple installation.
  2. The Alglib Python package, which can be downloaded here, with installation instructions here.  The Python for Windows package comes with full compiled code, and has very simple installation (but install Python first).
  3. After installing Python then Alglib, simply copy the contents of the Spline-Matrix zip file to any convenient folder.

Following this procedure the Spline-Matrix spreadsheet should work with 32 bit Excel and Python with no changes of settings, other than enabling macros at start-up, if your security settings require this.  The 64 bit version of Excel Python is still under development, and I have not checked the spreadsheet with 64 bit Excel.  Any problems, please let me know.  Some examples of the included functions are shown below.  For a full list of the available functions, see the index sheet.

Download xl_Spline-Matrix2.zip

xl_Spline4-01

xl_Spline4-02

xl_Spline4-03

xl_Spline4-04

xl_Spline4-05

xl_Spline4-06

xl_Spline4-07

Posted in AlgLib, Excel, Link to Python, Maths, Newton, UDFs, VBA | Tagged , , , , , , , , | 4 Comments

More from June Tabor and Eric Bogle

No Man’s Land:

Well how do you do, young Willie McBride,Do you mind if I sit here down by your graveside
And rest for a while ‘neath the warm summer sun
I’ve been working all day and I’m nearly done.
I see by your gravestone you were only nineteen
When you joined the dead heroes of nineteen-sixteen.
I hope you died well and I hope you died clean
Or Willie McBride, was it slow and obscene.

Chorus :
Did they beat the drum slowly, did they play the fife lowly,
Did they sound the dead-march as they lowered you down.
Did the bugles play the Last Post and chorus,
Did the pipes play the ‘Flooers o’ the Forest’.

And did you leave a wife or a sweetheart behind
In some faithful heart is your memory enshrined
Although you died back there in nineteen-sixteen
In that faithful heart are you ever nineteen
Or are you a stranger without even a name
Enclosed and forgotten behind the glass frame
In a old photograph, torn and battered and stained
And faded to yellow in a brown leather frame.

The sun now it shines on the green fields of France
The warm summer breeze makes the red poppies dance
And look how the sun shines from under the clouds
There’s no gas, no barbed wire, there’s no guns firing now
But here in this graveyard it’s still no-man’s-land
The countless white crosses stand mute in the sand
To man’s blind indifference to his fellow man
To a whole generaation that were butchered and damned.

Now young Willie McBride I can’t help but wonder why
Do all those who lie here know why they died
And did they believe when they answered the cause
Did they really believe that this war would end wars
Well the sorrow, the suffering, the glory, the pain
The killing and dying was all done in vain
For young Willie McBride it all happened again
And again, and again, and again, and again.

Please support:

Long Lost, a WW1 short film – crowd funding site

And from another war, a song with many connections:

Steeleye Span – When I Was on Horseback

Posted in Bach, Films | Tagged , , , , , , | Leave a comment

Extending complex UDFs with simple VBA

One of the advantages of writing spreadsheet applications in the form of User Defined Functions (UDFs) is that the UDF may be used on the spreadsheet in the same way as a built-in function, allowing applications to be modified and extended with no additional coding.  Nonetheless, there are times when this approach becomes time consuming or limiting, and it is worth writing some additional VBA code to achieve a better solution.

The example presented in this post was prepared in response to a question at the Eng-Tips forum., looking for a way to sum a column of length dimensions, entered in text format in feet and fractional inches.

One way is to use a UDF to convert the text to decimal values in the adjacent column, sum this column using the Excel Sum function, then convert this vale back to feet and inches with as second UDF.

A much more convenient way can be generated with a few lines of VBA:

Function SumFtinf(FtinRange, Optional Denom As Long = 0)
Dim FtInVal As Range, DecSum As Double

For Each FtInVal In FtinRange
DecSum = DecSum + FtInf2m(FtInVal.Value2)
Next FtInVal

SumFtinf = M2Ftinf(DecSum, Denom)
End Function

This function uses two existing UDFs (from the Units4Excel spreadsheet).  FtInf2m converts a length text string, in feet and fractional or decimal inches, to a value in metres.  M2Ftinf converts the metric value back to feet and inches, with a specified fractional denominator, or decimal inches if Denom is not specified.  The work of the function is done with a simple For Each loop which:

  • Works on each cell in the range defined by FtinRange, giving the name following “For Each” to each cell in turn.
  • The value of each cell is extracted with the .Value2 property.  There is also a .Value property which will work, but .Value2 is quicker.  Note that the value in this case is a text string, not a number.
  • The text string is converted to a metric value with the Ftinf2m function, and added to the running total in DecSum.
  • After the For Each loop has looped through all the cells the metric total length is converted back to feet and inches with the M2Ftinf function, and this is the value returned by the SumFtinf function.

The name given to the active cell in the For Each loop may be any valid variable name.  The name “cell” is often used, which is descriptive, but may be confused with the “cells” object, which is an Excel object and not a valid variable name.

An example of the function in use is shown in the screenshot below:

SumFtinf

The new function has been added to the Units4Excel spreadsheet, which also contains a variety of unit conversion functions, and functions for unit aware evaluation of mathematical functions entered as text.

Download Units4Excel , including full open source code.

 

Posted in Excel, Maths, Newton, UDFs, VBA | 3 Comments