gams2numpy01.gms : Test gams2numpy Python API string mode

Description

Test gams2numpy Python API string mode using the GAMS Embedded Code facility

Contributor: Clemens Westphal, July 2020


Small Model of Type : GAMS


Category : GAMS Test library


Main file : gams2numpy01.gms

$title 'Test gams2numpy API string mode' (GAMS2NUMPY01,SEQ=826)

$ontext
Test gams2numpy Python API string mode using the GAMS Embedded Code facility

Contributor: Clemens Westphal, July 2020
$offtext


* On the major platforms (Windows, Linux, Mac), GMSPYTHONLIB gets automatically set 
* to use the internal Python installation in sysdir/GMSPython.
$if not setEnv GMSPYTHONLIB $abort.noError Embedded code Python not ready to be used
$log --- Using Python library %sysEnv.GMSPYTHONLIB%

set i /i1*i10/;
set j /j1*j10/;

parameter p(i,j);
p(i,j) = uniform(0,10);

execute_unload 'p_reference.gdx', p;

*Test string mode
embeddedCode Python:
    import gams2numpy
    from gdxcc import *
    from gmdcc import *

    g2np = gams2numpy.Gams2Numpy(r"%gams.sysdir%.")
    gdx = new_gdxHandle_tp()
    gmd1 = new_gmdHandle_tp()
    gmd2 = new_gmdHandle_tp()
    
    rc, msg = gmdCreate(gmd1, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    
    rc, msg = gmdCreate(gmd2, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    
    rc, msg = gdxCreate(gdx, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    
    if not gdxOpenRead(gdx, 'p_reference.gdx'):
        raise Exception("Error opening GDX file "+'p_reference.gdx')
    p_arr = g2np.gdxReadSymbolStr(gdx, 'p')
    gdxClose(gdx)
    
    if not gdxOpenWrite(gdx, 'p_gdx1.gdx', ''):
        raise Exception("Error creating GDX file "+'p_gdx1.gdx')
    gdxStoreDomainSetsSet(gdx,0)
    gdxUELRegisterStrStart(gdx)
    for l in gams.get('*'):
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolStr(gdx, 'p', '', 2, GMS_DT_PAR, 0, p_arr)
    if gdxClose(gdx):
        raise Exception("Error closing GDX file")
    if not gdxFree(gdx):
        raise Exception("Error in gdxFree")

    rc = new_intp()
    for l in gams.get('*'):
        gmdMergeUel(gmd1, l)
    p_symPtr = gmdAddSymbolPy(gmd1, 'p', 2, GMS_DT_PAR, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")
    g2np.gmdFillSymbolStr(gmd1, p_symPtr, p_arr)
    gmdWriteGDX(gmd1, 'p_gmd1.gdx', False)
    
    rc = new_intp()
    for l in gams.get('*'):
        gmdMergeUel(gmd2, l)
    p_symPtr = gmdAddSymbolPy(gmd2, 'p', 2, GMS_DT_PAR, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")
    g2np.gmdFillSymbolStr(gmd2, p_symPtr, g2np.gmdReadSymbolStr(gmd1, 'p'))
    gmdWriteGDX(gmd2, 'p_gmd2.gdx', False)
    
    if not gmdFree(gmd1):
        raise Exception("Error in gmdFree")
    if not gmdFree(gmd2):
        raise Exception("Error in gmdFree")
endEmbeddedCode

execute 'gdxdiff p_reference.gdx p_gdx1.gdx > %system.nullfile%';
abort$errorlevel 'p_reference.gdx and p_gdx1.gdx are different';

execute 'gdxdiff p_reference.gdx p_gmd1.gdx > %system.nullfile%';
abort$errorlevel 'p_reference.gdx and p_gmd1.gdx are different';

execute 'gdxdiff p_reference.gdx p_gmd2.gdx > %system.nullfile%';
abort$errorlevel 'p_reference.gdx and p_gmd2.gdx are different';