0
$\begingroup$

I have managed to successfully setup and use the MATLAB interface in GMAT, with a combination of the documentation and this helpful video, which gave me all the information I needed. Alas, that is not the case for the Python interface. I have found 3 main sources of information:

Here's what I've done so far:

  1. Download and install Python 3.7 from here, (the 64-bit version as I'm using the 64-bit GMAT version).
  2. Add /Python37 and /Python37/Scripts to the system Path variable.
  3. Create a system environment variable called PYTHONPATH, and in it add Python37/Lib/site-packages and /Python37.

And then what? The documentation doesn't go into much detail into how to declare or call Python functions in the GMAT script. It implies something about creating .py file (without saying where to place it. The bin folder where GMAT.exe is?) with the example name PythonModule, which defines the PythonFunction functions we want to use, and then calling them in the script with this command:

[output_argument[, output_argument]...] = Python.PythonModule.PythonFunction(input_argument[, input_argument]...)

I tried to do the first example here and I get: **** ERROR **** GmatBase Exception Thrown: The Moderator cannot create a Command type "CallPythonFunction", which implies that even if I had setup PythonModule.py correctly it wouldn't work, as GMAT does not detect the Python interface. This is confirmed by right-clicking on the Mission Sequence, and the CallPythonFunction command not appearing.

So, how can I get the Python interface to work and after that how do I use it?

$\endgroup$

1 Answer 1

2
$\begingroup$

If you haven't already, definitely check out the Ex_R2015a_PythonInterface.script example in the samples directory. In the startup file there is a variable that can be set to certain directories that will allow GMAT to access the .py files inside. The default of this is PYTHON_MODULE_PATH = ROOT_PATH/userfunctions/python, and you can add additional lines with the same PYTHON_MODULE_PATH call and they will add them to the GMAT environment path. I.e.:

PYTHON_MODULE_PATH     = ROOT_PATH/userfunctions/python
PYTHON_MODULE_PATH     = path/to/my/directory

You mentioned the call method, which is correct, but to show an example we can use the ArrayFunctions.py module that exists under userfunctions/python. Inside this script module there is a function called mag which returns the magnitude of an array, with the input arguments of the array and the size of the elements to use. To call this inside of the GMAT Mission Sequence we could do the following:

Create Variable arrayMag
Create Array myArray[1,3]
myArray(1,1) = 10
myArray(1,2) = 20
myArray(1,3) = 30

BeginMissionSequence

[arrayMag] = Python.ArrayFunctions.mag(myArray, 3)
Write arrayMag

GMAT should write an answer of (roughly) 37.42 to the console.

If that doesn't work, make sure the correct version of the Python interface plugin is defined in the startup file. If your using Python 3.7, then libPythonInterface_py37 should be used:

PLUGIN                  = ../plugins/libPythonInterface_py37
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.