embgms01.gms : Test for embedded code facility

Description

Test functionality of the GAMS embedded code facility for GAMS during compilation time

Contributor: Michael Bussieck, January 2023


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embgms01.gms

$title 'Test for embedded code facility' (EMBGMS01,SEQ=925)

$ontext
Test functionality of the GAMS embedded code facility for GAMS during compilation time

Contributor: Michael Bussieck, January 2023
$offtext


Set i       / i1 text for i1
              i2 text for i2 /
    iout(i)
    j       / j1*j3 /
    ij(i,j) / #i.#j /
    i_want(i) / i1, i2 /
    i_want_ex(i) / i1 text 1, i2 text 2 /
    ij_want(i,j) / i1.j1, i1.j2 /
    ij_want_merge(i,j) / i1.j1, i1.j2, i1.j3 /
    ij_want_ex(i,j) / i1.j1 text 1, i1.j2 text 2/;

Scalar p0 /3.14/
       p0_want / 3.14 /;

Parameter p1(i)   / #i 3.14 /
          p2(i,j) / #i.#j 3.14 /
          p1_want(i) / #i 3.14 /
          p2_want(i,j) / i1.j1 3.14, i1.j2 3.14 /
          p2_want_merge(i,j) / i1.j1 3.14, i1.j2 3.14, i1.j3 3.14 /;

Variable v0      / fx 3.14 /
         v1(i)   / #i.fx 3.14 /
         v2(i,j) / #i.#j.fx 3.14 /
         v0_want / l 3.14, m 0, lo 0, up 10, scale 1 /
         v1_want(i) / i1.(l 3.14, m 0, lo 0, up 10, scale 1),
                      i2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         v2_want(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                        i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         v2_want_merge(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j3.(l 3.14, m 0, lo 0, up 10, scale 1) /;

equation e0      / fx 3.14 /
         e1(i)   / #i.fx 3.14 /
         e2(i,j) / #i.#j.fx 3.14 /
         e0_want / l 3.14, m 0, lo 0, up 10, scale 1 /
         e1_want(i) / i1.(l 3.14, m 0, lo 0, up 10, scale 1),
                      i2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         e2_want(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                        i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         e2_want_merge(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j3.(l 3.14, m 0, lo 0, up 10, scale 1) /;

$call rm -f embgms01_log.txt
$onechoV > checkData
$gdxout have.gdx
$unload %1
$gdxout
$call gdxdiff wanted.gdx have.gdx >> embgms01_log.txt
$if errorlevel 1 $abort %2
$offecho

$save embgms01

*** Test default list behavior ***

*Test 1.1: Test default list behavior of scalars
$onEmbeddedCode GAMS: r=embgms01
  abort$(p0<>3.14) "p0 bad";
$offEmbeddedCode

*Test 1.2: Test default list behavior of one dimensional parameters
$onEmbeddedCode GAMS: r=embgms01
  loop(i, abort$(p1(i)<>3.14) 'p1 bad');
$offEmbeddedCode

*Test 1.3: Test default list behavior of multi dimensional parameters
$onEmbeddedCode GAMS: r=embgms01
  loop((i,j), abort$(p2(i,j)<>3.14) 'p2 bad');
$offEmbeddedCode

*Test 1.4: Test default list behavior of one dimensional sets
$onEmbeddedCode GAMS: r=embgms01
  set ix(i) / i1*i2 /, diff(i);
  diff(i) = i(i) xor ix(i);
  abort$(card(diff)) 'i1 bad', diff;
$offEmbeddedCode

*Test 1.5: Test default list behavior of multi dimensional sets
$onEmbeddedCode GAMS: r=embgms01
  set ijx(i,j) / #i.#j /, diff(i,j);
  diff(i,j) = ij(i,j) xor ijx(i,j);
  abort$(card(diff)) 'ij bad', diff;
$offEmbeddedCode

*Test 1.6: Test default list behavior of scalar variables
$onEmbeddedCode GAMS: r=embgms01
  abort$(v0.l    <>3.14) 'bad v0.l';
  abort$(v0.m    <>0.00) 'bad v0.m';
  abort$(v0.lo   <>3.14) 'bad v0.lo';
  abort$(v0.up   <>3.14) 'bad v0.up';
  abort$(v0.scale<>1.00) 'bad v0.scale';
$offEmbeddedCode

*Test 1.7: Test default list behavior of one dimensional variables
$onEmbeddedCode GAMS: r=embgms01
  loop(i,
     abort$(v1.l    (i)<>3.14) 'bad v1.l';
     abort$(v1.m    (i)<>0.00) 'bad v1.m';
     abort$(v1.lo   (i)<>3.14) 'bad v1.lo';
     abort$(v1.up   (i)<>3.14) 'bad v1.up';
     abort$(v1.scale(i)<>1.00) 'bad v1.scale';
  );
$offEmbeddedCode

*Test 1.8: Test default list behavior of multi dimensional variables
$onEmbeddedCode GAMS: r=embgms01
  loop((i,j),
     abort$(v2.l    (i,j)<>3.14) 'bad v2.l';
     abort$(v2.m    (i,j)<>0.00) 'bad v2.m';
     abort$(v2.lo   (i,j)<>3.14) 'bad v2.lo';
     abort$(v2.up   (i,j)<>3.14) 'bad v2.up';
     abort$(v2.scale(i,j)<>1.00) 'bad v2.scale';
  );
$offEmbeddedCode

*Test 1.9: Test default list behavior of scalar equations
$onEmbeddedCode GAMS: r=embgms01
  abort$(e0.l    <>3.14) 'bad e0.l';
  abort$(e0.m    <>0.00) 'bad e0.m';
  abort$(e0.lo   <>3.14) 'bad e0.lo';
  abort$(e0.up   <>3.14) 'bad e0.up';
  abort$(e0.scale<>1.00) 'bad e0.scale';
$offEmbeddedCode

*Test 1.10: Test default list behavior of one dimensional equations
$onEmbeddedCode GAMS: r=embgms01
  loop(i,
     abort$(e1.l    (i)<>3.14) 'bad e1.l';
     abort$(e1.m    (i)<>0.00) 'bad e1.m';
     abort$(e1.lo   (i)<>3.14) 'bad e1.lo';
     abort$(e1.up   (i)<>3.14) 'bad e1.up';
     abort$(e1.scale(i)<>1.00) 'bad e1.scale';
  );
$offEmbeddedCode

*Test 1.11: Test default list behavior of multi dimensional equations
$onEmbeddedCode GAMS: r=embgms01
  loop((i,j),
     abort$(e2.l    (i,j)<>3.14) 'bad e2.l';
     abort$(e2.m    (i,j)<>0.00) 'bad e2.m';
     abort$(e2.lo   (i,j)<>3.14) 'bad e2.lo';
     abort$(e2.up   (i,j)<>3.14) 'bad e2.up';
     abort$(e2.scale(i,j)<>1.00) 'bad e2.scale';
  );
$offEmbeddedCode

*** Test writing symbol data using labels ***
$onMultiR
*Test 4.1: Test writing of scalars
$gdxout wanted.gdx
$unload p0_want=p0
$gdxout

$clear p0
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  p0 = 3.14;
$offEmbeddedCode p0
$batinclude checkData p0 'Test 4.1 failed in GAMS'

*Test 4.2: Test writing of one dimensional parameters
$gdxout wanted.gdx
$unload p1_want=p1
$gdxout

$clear p1
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  p1(i) = 3.14;
$offEmbeddedCode p1
$batinclude checkData p1 'Test 4.2 failed in GAMS'

*Test 4.3: Test writing of multi dimensional parameters
$gdxout wanted.gdx
$unload p2_want=p2
$gdxout

$clear p2
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  p2('i1', 'j1') = 3.14;
  p2('i1', 'j2') = 3.14;
$offEmbeddedCode p2
$batinclude checkData p2 'Test 4.3 failed in GAMS'

*Test 4.4: Test writing of one dimensional sets
$gdxout wanted.gdx
$unload i_want=iout
$gdxout

$clear iout
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  iout(i) = yes;
$offEmbeddedCode iout
$batinclude checkData iout 'Test 4.4a failed in GAMS'

$gdxout wanted.gdx
$unload i_want_ex=iout
$gdxout

$clear iout
$onEmbeddedCode GAMS: r=embgms01
$ onMultiR
  set iout / 'i1' "text 1", 'i2' "text 2" /;
$offEmbeddedCode iout
$batinclude checkData iout 'Test 4.4b failed in GAMS'

*Test 4.5: Test writing of multi dimensional sets
$gdxout wanted.gdx
$unload ij_want=ij
$gdxout

$clear ij
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  ij('i1', 'j1') = yes;
  ij('i1', 'j2') = yes;
$offEmbeddedCode ij
$batinclude checkData ij 'Test 4.5a failed in GAMS'

$gdxout wanted.gdx
$unload ij_want_ex=ij
$gdxout

$clear ij
$onEmbeddedCode GAMS: r=embgms01
$ onMultiR
  set ij / 'i1'.'j1' "text 1", 'i1'.'j2' "text 2" /;
$offEmbeddedCode ij
$batinclude checkData ij 'Test 4.5b failed in GAMS'

*Test 4.6: Test writing of scalar variable
$gdxout wanted.gdx
$unload v0_want=v0
$gdxout

$clear v0
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  v0.l     =  3.14;
  v0.m     =  0.00;
  v0.lo    =  0.00;
  v0.up    = 10.00;
  v0.scale =  1.00;
$offEmbeddedCode v0
$batinclude checkData v0 'Test 4.6 failed in GAMS'

*Test 4.7: Test writing of one dimensonal variables
$gdxout wanted.gdx
$unload v1_want=v1
$gdxout

$clear v1
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  loop(i,
     v1.l    (i) =  3.14;
     v1.m    (i) =  0.00;
     v1.lo   (i) =  0.00;
     v1.up   (i) = 10.00;
     v1.scale(i) =  1.00;
  );
$offEmbeddedCode v1
$batinclude checkData v1 'Test 4.7 failed in GAMS'

*Test 4.8: Test writing of multi dimensonal variables
$gdxout wanted.gdx
$unload v2_want=v2
$gdxout

$clear v2
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  set ij2(i,j) / i1.j1, i1.j2 /;
  loop(ij2(i,j),
     v2.l    (i,j) =  3.14;
     v2.m    (i,j) =  0.00;
     v2.lo   (i,j) =  0.00;
     v2.up   (i,j) = 10.00;
     v2.scale(i,j) =  1.00;
  );
$offEmbeddedCode v2
$batinclude checkData v2 'Test 4.8 failed in GAMS'

*Test 4.9: Test writing of scalar equations
$gdxout wanted.gdx
$unload e0_want=e0
$gdxout

$clear e0
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  e0.l     =  3.14;
  e0.m     =  0.00;
  e0.lo    =  0.00;
  e0.up    = 10.00;
  e0.scale =  1.00;
$offEmbeddedCode e0
$batinclude checkData e0 'Test 4.9 failed in GAMS'


*Test 4.10: Test writing of one dimensonal equations
$gdxout wanted.gdx
$unload e1_want=e1
$gdxout

$clear e1
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  loop(i,
     e1.l    (i) =  3.14;
     e1.m    (i) =  0.00;
     e1.lo   (i) =  0.00;
     e1.up   (i) = 10.00;
     e1.scale(i) =  1.00;
  );
$offEmbeddedCode e1
$batinclude checkData e1 'Test 4.10 failed in GAMS'

*Test 4.11: Test writing of multi dimensonal equations
$gdxout wanted.gdx
$unload e2_want=e2
$gdxout

$clear e2
$save embgms01
$onEmbeddedCode GAMS: r=embgms01
  set ij2(i,j) / i1.j1, i1.j2 /;
  loop(ij2(i,j),
     e2.l    (i,j) =  3.14;
     e2.m    (i,j) =  0.00;
     e2.lo   (i,j) =  0.00;
     e2.up   (i,j) = 10.00;
     e2.scale(i,j) =  1.00;
  );
$offEmbeddedCode e2
$batinclude checkData e2 'Test 4.11 failed in GAMS'