load17.gms : Test $declareAndLoad and $gdxLoadAll

Description

Test the dollar control options $gdxLoadAll and $declareAndLoad which allow a
convenient import of all (even undeclared) symbols from a GDX file at compile
time.

Contributor: Lutz Westermann, March 2023


Small Model of Type : GAMS


Category : GAMS Test library


Main file : load17.gms

$title 'Test $declareAndLoad and $gdxLoadAll' (LOAD17,SEQ=938)

$onText
Test the dollar control options $gdxLoadAll and $declareAndLoad which allow a
convenient import of all (even undeclared) symbols from a GDX file at compile
time. 

Contributor: Lutz Westermann, March 2023
$offText


* Generate some input
$call.checkErrorLevel gamslib -q 1
$call.checkErrorLevel gams trnsport gdx=trns lo=%GAMS.lo%

* $declareAndLoad should take everything
$echo $declareAndLoad trns.gdx > test.gms
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff trns.gdx test.gdx > %system.NullFile%

* $declareAndLoad should take everything - also if there is some (matching) declaration already
$onEcho > test.gms
Set i;
Scalar f;
Variable x(*,*);
$declareAndLoad trns.gdx 
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff trns.gdx test.gdx > %system.NullFile%

* $declareAndLoad should take everything - also if there is some (matching) declaration already, even with "wrong" data, if we use $onMultiR
$onEcho > test.gms
Set i / 'chicago', 'brunswick' /;
Scalar f / 89 /;
Variable x(*,*);
$onMultiR
$declareAndLoad trns.gdx 
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff trns.gdx test.gdx > %system.NullFile%

* $declareAndLoad should throw an error, if we have data already for symbols to be loaded and $onMulti[R] is not set
$onEcho > test.gms
Set i / 'chicago', 'brunswick' /;
Scalar f / 89 /;
Variable x(*,*);
$declareAndLoad trns.gdx 
$offEcho
$call gams test.gms gdx=test lo=%GAMS.lo%
$ifE errorLevel==0 $abort Expected error in line %system.IncLine%

* $declareAndLoad should throw an error, if we have a mismatching declaration
$onEcho > test.gms
Set i(*,*);
Equation f;
Variable x(*);
$declareAndLoad trns.gdx 
$offEcho
$call gams test.gms gdx=test lo=%GAMS.lo%
$ifE errorLevel==0 $abort Expected error in line %system.IncLine%

**************************

* $gdxLoadAll should take everything that is known
$onEcho > test.gms
Set i, j;
Parameter a(i), b(j), d(i,j), f, c(i,j);
Variable x(i,j), z;
Equation cost, supply(i), demand(j);
$gdxLoadAll trns.gdx 
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff trns.gdx test.gdx > %system.NullFile%

* $gdxLoadAll should take everything that is known - also if there is some (matching) declaration already, even with "wrong" data, if we use $onMultiR
$onEcho > test.gms
Set i / 'London' /, j;
Parameter a(i), b(j), d(i,j), f / 91 /, c(i,j);
Variable x(i,j), z;
Equation cost, supply(i), demand(j);
$onMultiR
$gdxLoadAll trns.gdx 
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff trns.gdx test.gdx > %system.NullFile%

* $gdxLoadAll should throw an error, if we have data already for symbols to be loaded and $onMulti[R] is not set
$onEcho > test.gms
Set i / 'London' /, j;
Parameter a(i), b(j), d(i,j), f / 91 /, c(i,j);
Variable x(i,j), z;
Equation cost, supply(i), demand(j);
$gdxLoadAll trns.gdx 
$offEcho
$call gams test.gms gdx=test lo=%GAMS.lo%
$ifE errorLevel==0 $abort Expected error in line %system.IncLine%

* $declareAndLoad should throw an error, if we have a mismatching declaration
$onEcho > test.gms
Set i(*,*);
Equation f;
Variable x(*);
$gdxLoadAll trns.gdx 
$offEcho
$call gams test.gms gdx=test lo=%GAMS.lo%
$ifE errorLevel==0 $abort Expected error in line %system.IncLine%

* Alias as domain should not cause any trouble (see #5769)
$onEcho > alias.gms
Set i / i1*i3/;
Alias (i,j);
Parameter p(j) / #j 1900 /;
$offEcho
$call gams alias.gms gdx=alias lo=%GAMS.lo%

$onEcho > test.gms
$declareAndLoad alias.gdx
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff alias.gdx test.gdx > %system.NullFile%

$onEcho > test.gms
Set i;
Alias (i,j);
Parameter p(j);
$gdxLoadAll alias.gdx
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff alias.gdx test.gdx > %system.NullFile%

* $declareAndLoad should be OK to use functions and "new keywords" as identifiers (see #5777)
$onEcho > data.gms
Scalar gamma /4/
       sAnd  /2/;
$offEcho
$call.checkErrorLevel gams data.gms gdx=data lo=%GAMS.lo%

$onEcho > test.gms
$if set pregamma scalar mygamma; mygamma = gamma(2);
$if set presand scalar mysand; set i /1*2/; mysand = sand(i, 1);
$declareAndLoad data.gdx
abort$(gamma<>4 or sand<>2) 'wrong gamma or sand', gamma, sand;
$if set postgamma scalar mygamspost; mygammapost = gamma(2);
$if set postsand scalar mysandpost; set j /1*2/; mysandpost = sand(j, 1);
gamma = system.gamma(2);
$offEcho
$call.checkErrorLevel gams test.gms lo=%GAMS.lo%
$call.checkErrorLevel gams test.gms --presand=1 lo=%GAMS.lo%
$call gams test.gms --pregamma=1 lo=%GAMS.lo%
$ifE errorLevel=0 $abort test with --pregamma=1 should fail
$call gams test.gms --postgamma=1 lo=%GAMS.lo%
$ifE errorLevel=0 $abort test with --postgamma=1 should fail
$call gams test.gms --presand=1 --postsand=1 lo=%GAMS.lo%
$ifE errorLevel=0 $abort test with --presand=1 and --postsand=1 should fail

* $declareAndLoad adds new acronyms as well
$onEcho > acronym.gms
Acronym a1;
Scalar a /a1/;
$offEcho
$call.checkErrorLevel gams acronym.gms gdx=acronym lo=%GAMS.lo%

$onEcho > test.gms
Acronym a2;
$declareAndLoad acronym.gdx
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff acronym.gdx test.gdx > %system.NullFile%

* $declareAndLoad adds new aliases as well
$onEcho > alias.gms
Alias (*,u,u2);
$offEcho
$call.checkErrorLevel gams alias.gms gdx=alias lo=%GAMS.lo%

$onEcho > test.gms
$declareAndLoad alias.gdx
$offEcho
$call.checkErrorLevel gams test.gms gdx=test lo=%GAMS.lo%
$call.checkErrorLevel gdxdiff alias.gdx test.gdx > %system.NullFile%