Disclaimer:
This article describes how to use the GSPy interface to run Python scripts from GoldSim models. This feature requires advanced knowledge of both C and Python programming languages, as well as the Embedding and Extending Python library. The user is responsible for creating and compiling the DLL file that links GoldSim and Python, as well as configuring the Python environment correctly. Any errors or issues that arise from using this feature are likely due to a problem with the DLL code and its compile environment and are not related to the GoldSim framework, and we cannot provide support or troubleshooting for them. Please use this feature at your own risk and discretion. You are welcome to ask DLL related questions in our public forum: Modeling Application Questions – GoldSim Help Center.
Follow these instructions to set up an environment for using Python scripts that are embedded in a C/C++ DLL for linking to GoldSim. These steps use some specific tools (like Anaconda), which provide relatively little hassle. You can use other tools if you know what you are doing. We may not be able to support or help troubleshoot if you follow a different path from what is described here.
Setup the Environment
Below are the steps to setup and configure the environment for running GoldSim with a DLL with embedded Python.
Note: Unless you know what you’re doing, it is recommended that you start this process on a machine that is free of any existing Python environments to prevent any conflicts with older versions that are currently set in your environment. If you un-install any IDEs or python interpreters, you should restart the computer before continuing with the below steps.
Note: This article assumes you will install the 64-bit version of Anaconda. If you plan to implement error messaging from GSPyModule.py to GoldSim, you will need to use the 32-bit Anaconda distribution and the 32-bit GSPy binaries (GSPy.pyd and TestGSPy.exe) located in the '\bin\32-bit' folder. As of 2018-04-13, there is a known issue in the handling of error messages from 64-bit external DLLs. These steps walk through a setup that assumes you are using the 64-bit binaries. If you want to compile and use 32-bit binaries, it is recommended that you follow these steps for 64-bit binaries first, just to make sure it is working, then move to a different configuration.
- Download and install Anaconda 64-bit from here https://www.anaconda.com/download/
- Note that if you are using Anaconda in a commercial environment with more than 200 employees, you need to purchase a Commercial Edition License.
- Follow the defaults during the installation and do not check the boxes when you see the page shown below.
- After installation, start up the Anaconda Navigator.
- From within the navigator, start the command prompt (alternatively, you can start the “Anaconda PowerShell Prompt” from the Windows Start menu).
- In the terminal, type in the following to determine the version of Python you have: (base) C:\Users\JasonLillywhite>python –version Python 3.8.5
- Make sure this is the version you expect to see. These steps have only been tested in the version shown above.
- Download the GSPy.zip file found at the bottom of this article and save it in a new folder called “GSPy” in a location that you can easily work from. I saved mine in my Documents directory.
- Unzip the contents here. Below is a screen capture showing how my files appear in the new GSPy directory.
- Create a new directly under “GSPy” called “Working” and copy the following files into that directory:
- \bin\64-bit\GSPy.pyd
- \bin\64-bit\TestGSPy.exe
- \gsm\Scalar Addition\GSPyModule_ScalarAddition.py
- \gsm\Scalar Addition\GSPyScalarAddition.gsm
-
- From the Windows Start menu, start typing “environment” and select the option for your account as shown below.
- Under the User variables section, click New…
- Set the variable name to GSPY and the value to the path and click OK.
- Select the Path variable and click “Edit…”
- Click the New button and type %GSPY% then click OK
- From the Windows Start menu, start typing “environment” and select the option for your account as shown below.
The setup is complete. The next section covers the steps needed to test the environment.
Testing the Environment
Follow these steps to test the environment and make sure you can run the GoldSim test models provided with the GSPy binaries.
- Browse to the Working folder in Windows File Explorer, hold the Shift key and right-click then select “Open PowerShell window here” (alternatively open a command prompt and change directories to that same Working directory.
- Type TestGSPy.exe, press Enter and confirm that no error messages are generated.
- The output should looks something like this:
-
Inputs:
10.0, 20.0, 30.0
40.0, 50.0, 60.0
1.0, 2.0, 3.0
4.0, 5.0, 6.0
Version number reported from Python module: 0.10
Note: Optional "InitializeSimulation" function not defined in Python module
Number of inputs reported from Python module: 2
Number of outputs reported from Python module: 2
Number of inputs reported from Python module: 2
Number of outputs reported from Python module: 2
Running Python function: DoCalcsAndReturnValues...
Note: Optional "FinalizeSimulation" function not defined in Python module
Result:
30.0, 200.0, 0.0 - If you get errors at this point, stop here and double check that you followed the above steps carefully and also check the troubleshooting section below.
Test Python's Numpy Library
The next test model makes use of Python’s numpy library so we need to re-install this before continuing with the test.
- Start the Anaconda Navigator and launch the command prompt.
- Uninstall the numpy by typing the following command: pip uninstall numpy
- Re-install numpy: pip install numpy
- In Windows File Explorer, copy the matrix addition files to the clipboard:
- Browse to the Working directory and delete the Python file called “GSPyModule.py”
- Paste in the 2 files on your clipboard then copy and paste the “GSPyModule_MatrixAddition.py” file and rename it to “GSPyModule.py”.
- Close all instances of GoldSim (in case any existing models are still connected to your DLL.
- Start GoldSim.
- Open the model you pasted into the Working directory called “GSPyMatrixAddition.gsm” and run it.
- You should see the model run to completion now.
Troubleshooting
If you see an error like the one below, there is a good chance it is due to the Python script not finding the numpy library. It likely indicates that an error in your Python code. Almost any error in the Python code will result in this error message from GoldSim.
If you see the error shown above, please try the following.
- Make sure to have the 64-bit version of GSPy.pyd in the same folder as the GoldSim model.
- Make sure there is a python file named GSPyModule.py containing your python code in the same folder as the GoldSim model.
- Uninstall and re-install numpy
- Uninstall and re-install scipy
If you have done this and still get an error, let us know.
Comments
0 comments
Please sign in to leave a comment.