User Tools

Site Tools


User defined Functions or Subroutines

Q: We are writing a GAMS code which includes many modules. In each module, we need to do some similar work such as calculation of … This calculation involves several equations and it is painful to repeat it again and again in each module. Is there a way to build something like a user function or subroutine such that we can call this user function or subroutine for that calculation?

You can use separate GAMS programs as subroutines. The GDX facilities make exchanging data across different GAMS programs easy. The idea is the following:

* prepare input data a, b, c for subroutine sub_a
* export data to a GDX file
execute_unload 'in', a,b,c
execute '=gams sub_a'; abort$errorlevel 'problems with sub_a';
execute_load 'out', c,d,e

The GAMS program sub_a.gms reads GDX file in.gdx and exports its results into a GDX file named out.gdx. sub_a is a completely separate program sharing data with the calling program.

Another way of sharing GAMS code is to use the batinclude facility which is described in the GAMS User's Guide (Appendix D) or in Chapter 13 of the Bruce Mccarl GAMS Guide.

Distribution 23.7 introduced (user-defined) function libraries, for more details please consult the release notes 23.7.

interfaces/user_defined_functions_or_subroutines.txt · Last modified: 2017/09/02 17:45 by support