0
$\begingroup$

I am trying to run a design optimisation proccess for some orbital parrameters of spacecraft. To do this, I need to be able to edit and run a GMAT script from MATLAB. Is this possible? If so, how might I go about it?

I don't need to read any outputs of GMAT. As a minimal working example, I am hoping for something like the following. Where RunGMAT is a matlab function which inserts the inclination into the GMAT script and then runs the mission.

for inclination = 0 :5 : 360

       RunGMAT(inclination)

end

So far, I have followed the steps in the following link, however this seems to do the oppositie of what I need. It runs MATLAB functions inside a GMAT script, but I need to run GMAT scripts from MATLAB.

Any help would be greatly apreciated, Thankyou, Lee

$\endgroup$
2
  • $\begingroup$ Do you have an example of what you want to do, specifically with GMAT? I don’t have any experience with that in particular, but have a couple ideas depending of what the GMAT code actually looks like (and one of those is kind of hacky but could work). You can definitely run external scripts from MATLAB and a parse & edit text files pretty easily as well, so if there isn’t a binding for MATLAB & your GMAT script then that could be a starting point. $\endgroup$ Jun 4, 2023 at 12:16
  • $\begingroup$ in addition to the answer below I feel compelled to say that the example you've trotted out seems like something GMAT can do on its own based on previous questions $\endgroup$
    – Erin Anne
    Jun 5, 2023 at 0:50

2 Answers 2

3
$\begingroup$

In the GMAT Documentation, look for "GMAT API Reference" under your appropriate version (R2020a, where they introduced the feature, or later). An API is an "application programming interface" made to let one computer program talk to another; in this case, various programming languages including MATLAB and GMAT.

Follow the instructions in Setting up the GMAT API and you should be able to get to this point

>> addpath('/<TopLevelGMATFolder>/bin/')
>> load_gmat
No script provided to load.

ans =

Instance of GMAT Moderator is initialized. No script ready to run.

>> sat = GMATAPI.Construct('Spacecraft','MySat')

sat =

Object of type Spacecraft named MySat

>> GMATAPI.ShowObjects()

ans =

Current GMAT Objects

   EarthMJ2000Eq
   EarthMJ2000Ec
   EarthFixed
   EarthICRF
   SolarSystemBarycenter
   MySat

The SolarSystem contains the following bodies:

   [Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Luna]

I am not a GMAT user and can't answer any questions about this. I just looked for the documentation, where this is documented.

$\endgroup$
1
$\begingroup$

I'm not super fluent in the GMAT MATLAB API, but I am familiar with the Python API and I think most of the functional commands operate in the same way. To run what you want in python, you could do:

import sys
sys.path.append("/path/to/gmat/bin")
sys.path.append("/path/to/gmat/api")
script_path = "/path/to/gmat/script"

from load_gmat import *  # equivalent to load_gmat

def run_gmat(inclination):                    # This would be your RunGmat function
    did_load = gmat.LoadScript(script_path)   # Load fresh for each run
    if did_load is False:                     # Check if load failed
        print('Script failed to load')
        return

    sat = gmat.GetObject("DefaultSC")         # Can replace DefaultSC with spacecraft name
    sat.SetField('INC', inclination)          # Set the Satellites INC parameter        
    
    gmat.Initialize()                         # May not be necessary since loading a script

    did_run = gmat.RunScript()                # Run script with new INC value
    if did_run is False:                      # Check if run failed
        print('Script failed to run')
        return
    else:
        print('Script ran successfully, INC=', inclination)
        return


for inclination in range(0, 360, 5): 
    run_gmat(inclination)                    # Can iterate

This is a pretty basic implementation of looping through values. If you want to retrieve any values after a run, you can use the GetRuntimeObject call in the same way I used the GetObject call on both objects and variables.

As I mentioned, not super fluent in how the MATLAB interface works, but I assume the same function would look something like:

function did_run = RunGmat(inclination)
load_gmat   
did_load = GMATAPI.LoadScript('/path/to/script');
if did_load == false
  disp('failed to load');
  return
end

sat = GMATAPI.GetObject("DefaultSC");
sat.SetField('INC', inclination);

did_run = GMATAPI.RunScript();
if did_run == false
  disp('failed to run');
else
  disp("successfully ran");
end
$\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.