% run the gdxmrw_tr2 example

% In this example, we use an indexed GDX file created in
% Matlab via GDXMRW's iwgdx() call to drive the transport model.  We
% read a similar file containing the solution back in to verify that
% the result is as expected.

clear all

% read in the raw data for the trnsport model
trdata

% we write cap, dem, distance, and f in the call below
% the first 3 are renamed, but f is not
iwgdx ('trdat2', 'a', cap,  'b', dem, 'd', distance, 'f');
fprintf('Matlab data written to GDX file trdat2\n');

solGDX = 'trsol2.gdx';
if exist(solGDX,'file')
  delete (solGDX);
end

flag = system ('gams gdxmrw_tr2 lo=2');

if flag ~= 0
  error('gams run failed: rc = %d', flag);
end
if ~ exist(solGDX,'file')
   error([ 'Results file ', solGDX, ' does not exist after gams run' ]);
end
fprintf('Model gdxmrw_tr2.gms finished\n');

return

% now get the outputs from the GDX file produced by the GAMS run
ss = irgdx (solGDX);

modelstat = ss.modelstat;
solvestat = ss.solvestat;

fprintf('modelstat = %d (1 = optimal solution)\n', modelstat);
fprintf('solvestat = %d (1 = normal completion)\n\n', solvestat);

if (modelstat ~= 1) || (solvestat ~= 1)
  error ('Model not solved as expected')
end

z = ss.zz;
x = ss.xx;

fprintf('optimal value = %g\n\n', z);
