gams2numpy02.gms : Test gams2numpy Python API raw mode

Description

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

Contributor: Clemens Westphal, July 2020


Small Model of Type : GAMS


Category : GAMS Test library


Main file : gams2numpy02.gms

$title 'Test gams2numpy API raw mode' (GAMS2NUMPY02,SEQ=827)

$ontext
Test gams2numpy Python API raw 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 raw 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, uels = g2np.gdxReadSymbolRaw(gdx, 'p')
    gdxClose(gdx)

    if not gdxOpenWrite(gdx, 'p_gdx1.gdx', ''):
        raise Exception("Error creating GDX file "+'p_gdx1.gdx')
    gdxUELRegisterStrStart(gdx)
    for l in uels[1:]:
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolRaw(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 uels[1:]:
        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.gmdFillSymbolRaw(gmd1, p_symPtr, p_arr)
    gmdWriteGDX(gmd1, 'p_gmd1.gdx', False)
    
    rc = new_intp()
    p_arr, uels = g2np.gmdReadSymbolRaw(gmd1, 'p')
    for l in uels[1:]:
        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.gmdFillSymbolRaw(gmd2, p_symPtr, p_arr)
    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'