Description
Dump model Jacobian to GDX via GAMS/Convert, read the Jacobian into Matlab via GDXMRW, and use Matlab's spy command to view the Jacobian structure. Intended use: interactive Matlab session Author: Steve
Category : GAMS Data Utilities library
Main file : GDXMRWShowJac1.gms includes : trnsport.gms GDXMRWShowJac1.gms
$title Visualize model Jacobian in Matlab
$ontext
Dump model Jacobian to GDX via GAMS/Convert, read the Jacobian
into Matlab via GDXMRW, and use Matlab's spy command
to view the Jacobian structure.
Intended use: interactive Matlab session
Author: Steve
$offtext
$echo 'dumpgdx jac.gdx' > convert.opt
$onecho > model.gms
$include trnsport.gms
option lp = convert;
transport.optfile = 1;
solve transport using lp min z;
$offecho
$call gams model.gms lo=%GAMS.lo%
$onecho > jdump.m
%% show Jacobian matrix generated by GAMS/Convert
% read index sets i,j
% since we know they are not subsets of other sets, we can
% read compressed and not lose any info
si = struct('name','i', 'compress','true');
i = rgdx('jac.gdx', si);
i.val
sj = struct('name','j', 'compress','true');
j = rgdx('jac.gdx', sj);
j.val
% use a filtered read to get A(i,j) in sparse form (the default),
% and use spconvert to convert to a full-form Matlab matrix
% with sparse storage for visualizing with spy
sA = struct('name','A');
sA.uels = {i.uels{1},j.uels{1}};
A = rgdx('jac.gdx', sA);
AA = spconvert(A.val);
spy(AA)
$offecho
$set WHICH which
$if not %system.filesys% == UNIX $set WHICH where
$call %WHICH% matlab
$ifThen errorLevel 1
$clearError
$abort.noerror 'Matlab is not available!';
$else
$call.Async matlab -r "cd %GAMS.cdir%;jdump" -nosplash -nodisplay
$endIf