The VBA Scripting Dictionary object is very useful (examples here and here), but the documentation is poor.
This is fixed at Data Dictionary in VBA – Complete Syntax Documentation, which provides just what it says.
The VBA Scripting Dictionary object is very useful (examples here and here), but the documentation is poor.
This is fixed at Data Dictionary in VBA – Complete Syntax Documentation, which provides just what it says.
As reported in the previous post in this series, although the compiled routines included in packages such as Numpy, Scipy, and Pysparse are very fast, other parts of the code turned out to be slower than the equivalent in VBA. Some experimentation with the frame analysis code has found two simple strategies that can substantially improve this situation:
In the case of the frame analysis program, the following code is called for each frame member in assembling the global stiffness matrix:
for i in range(0 , 12):
for j in range(0 , 12):
sum = 0.0
for k in range(0 , 12):
sum = sum + tt[i, k] * cc[k, j]
km[i, j] = sum
This code is performing matrix multiplication, and can be replace with a single line calling the Numpy dot function:
km = npy.dot(tt, cc)
This single change reduced the time to generate the solver input data from about 30 seconds to 6 seconds (the run time for the modified code was reduced by a factor of about 100). Some further modification of the Python code brought this down to about 4 seconds, which was faster than the VBA code, but was still longer than the run time for the solver, which was doing all the hard work, so there was clearly room for further improvement.
This was achieved using Numba, which to quote their web site is:
a just-in-time specializing compiler which compiles annotated Python and NumPy code to LLVM (through decorators). Its goal is to seamlessly integrate with the Python scientific software stack and produce optimized native code, as well as integrate with native foreign languages.
Implementing the Numba routines proved to be surprisingly easy; an example is shown below:
from numba import double, jit, autojit
...
fastkva = autojit(py_formkv)
lastk = fastkva(km, g, lastk, kva)
def py_formkv(km, G, k, kva):
idof = km.shape[0]
for i in range(0,idof):
m = G[i]
if m != 0:
for j in range(0,idof):
n = G[j]
if n != 0:
K = km[i,j]
if K != 0:
icd = n - m
if icd >= 0:
kva[k,2] = K #km[i,j]
kva[k,0] = n
kva[k,1] = m
k = k+1
return k
The function py_formkv is standard Python. The function fastkva is defined as a Numba compiled version of py_formkv, and this may then be called in the same way as any other function. This single call to Numba reduced the run time from 4 seconds to less than 1 second. Further improvements to the data generation time would no doubt be possible, but I will now be concentrating on the post-processing, currently all done in VBA, which takes about 5 seconds.
See more about Numba at: Numba vs. Cython: Take 2, where the author (who unlike me is an experienced Python programmer) has compared Numba to several alternatives, and found it to be the fastest (with a 1400 x speed-up, compared with the original Python code!).
I have previously looked at using Excel for generation of images from data listing 3D coordinates of points and lists of connections (see https://newtonexcelbach.wordpress.com/2012/09/27/daily-download-11-perspective-projection/ for instance), but this approach is limited in what can be done:
Paraview (to quote from their web site) is “an open-source, multi-platform data analysis and visualization application. ParaView users can quickly build visualizations to analyze their data using qualitative and quantitative techniques. The data exploration can be done interactively in 3D or programmatically using ParaView’s batch processing capabilities.
ParaView was developed to analyze extremely large datasets using distributed memory computing resources. It can be run on supercomputers to analyze datasets of exascale size as well as on laptops for smaller data.”
It came to my attention through the latest edition of “Programming the Finite Element Method” which I recently purchased. This publication contains Fortran routines for generating Paraview readable files of finite element models, including output data, which can then be used to generate surface contoured images and animations for instance. As well as being open-source, Paraview uses Python as a scripting language, which would seem to offer the possibility of linking it to Excel for direct transfer of data.
It must be said that the program appears to be sufficiently complex to require a significant learning period before it can be used effectively, but comments from a user at OSGeology suggest that this will be worth the effort.
A related product is the Kiwi Viewer which will allow the VTK files used by Paraview to be viewed on tablet and phone devices (both I-phone and Android).
You Tube video illustrating some basics of working in Paraview:
And the Kiwi Viewer in action:
I have updated the Downloads spreadsheet on Skydrive to include all new files added this year, and I have also added two Category columns, which link to the Download by Category page, giving more details and links for each file. The list can be sorted on-line if you select “edit” mode, or download and open in Excel.
Download files related to linking to Python from Excel are to be found in:
Python matrix functions in Excel, using Pyxll:
Download file: http://interactiveds.com.au/software/Matrixpyxll.zip
Python for VBA users – 4; Python data types: The main Python data types, including numpy arrays, with examples of their use in PyXll functions.
Download file: http://interactiveds.com.au/software/pyTypes.zip
Python for VBA users – 5; Using built in numpy functions:
Download file: http://interactiveds.com.au/software/py_Polynomial.zip
Also see the PyXll add-in, which is required for the downloads in this category.