Print Friendly and PDF

Python DLL

Nick Martin -

The GoldSim External element provides for passing information into and out of GoldSim in an “automated” fashion. The main limitation on External element use for many users is the requirement for creation and compilation of a dynamic link library (DLL) to handle information exchange. The External element DLL is required to respond to certain states or communications from GoldSim.  This element provides the memory address of an input array, the memory address of an output array, and provides an instruction to the DLL. This instruction tells the DLL which state it should provide calculations or responses for and can be one of initialize, calculate, report version, report number of arguments, and clean-up.

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed. (Python Software Foundation, 2015)”

Why might I want to use Python with GoldSim? Python is widely used in scientific computing; Python plays well with everyone; and, Python source code does not need to be compiled.

  • Python is widely used in scientific and numeric computing because of the powerful, open source computational and visualization libraries that are freely available.
  • Python plays well with others (e.g. languages, operating systems, dynamic libraries, networks, compiled executables, etc.) and so can be used to launch, control, pre-process inputs, and post-process outputs for other executables.
  • The Python source code does not need to be compiled. It can be created/edited with a text editor, saved, and then the simulation can be run again. In this new simulation, the custom Python code could do something completely different. This is what it means by providing access to the Python interpreter.

Python can be used with/through the GoldSim External element. One way to link the GoldSim External element to the Python interpreter is to use Cython to link the standard framework C language DLL provided in Appendix C of the GoldSim User’s Guide to the Python interpreter. Cython is a freely available library and package for Python which is both a programming language that blends Python with the static type system of C and C++, and a compiler that translates Cython code into efficient C and C++ code (Smith, 2015).

This linkage of the GoldSim External element to a C language DLL, to Cython wrapper code, to the Python interpreter sounds complicated, and it is fairly complex. Fortunately, a Python DLL HOWTO document was recently created which provides a cookbook recipe and source code for creating a DLL which links the External element in GoldSim to the Python interpreter. This “cookbook” provides the steps necessary to compile the DLL from the included source code using a compiler which can be downloaded for free. No specialized knowledge is required to get this DLL working and to implement the example GoldSim model.

This HOWTO addresses the main limitation on using the External element in that it takes care of all of the “low-level” stuff and provides a “higher-level” interface, i.e. Python, to link GoldSim to external programs. Although this DLL provides the user with a “higher-level” interface, the user will still need to know what to do with the programs at both ends of the “higher-level” link. And, the user will need to be able to use Python at the “advanced-beginner” level in order to do anything at this “higher-level”.

 

References:

Python Software Foundation. (2015). What is Python? Executive Summary. Retrieved September 16, 2015, from python: https://www.python.org/doc/essays/blurb/

Smith, K. (2015). Cython: A Guide for Python Programmers. Sebastopol, CA, USA: O'Reilly.

 

 

Download the HOWTO document and associated source code:

GoldSim Model Library

Have more questions? Submit a request

Comments