$title  Run a Systematic Sensitivity Analysis for the BCA Model

*       ------------------------------------------------------------------------------
*       Model-Specific Header

*       Systematic senstivity analysis for the BCA model over five parameters.  
*       Here we define the set of optional values:

sets    ds              Dataset aggregation /emfagg,emfdis,g20dis,g20agg,emfall/,
        coalition       Coalition structure /eur, oecd, oecdchn/
        verbca          Voluntary export restraint /yes, no/
        lic_exempt      Lower income countries exempt /yes,no/
        metric          Metric employed to measure border carbon /mrio, energy, fuel/;

$eval ns card(ds)*card(coalition)*card(verbca)*card(lic_exempt)*card(metric)

set     scn     Enumeration of scenarios /1*%ns%/;

set     inputs(scn,ds,coalition,verbca,lic_exempt,metric)  Scenario definitions;

*       ------------------------------------------------------------------------------
*       Generic SSA Code:

*       Load Multiple Processors

parameter       nprocessor Default number of processors to load /2/;

*       Under NT we can use the environment variable to define 
*       the number of processors to employ.  The environment  
*       variable %NUMBER_OF_PROCESSORS% returns the number of
*       logical processors which is typical twice the number of 
*       physical cores.  

*       Comment out the next line if you want to fix the number of 
*       processors:

$if %system.filesys% == MSNT  nprocessor = round(%sysenv.NUMBER_OF_PROCESSORS%/2);


file    kbat /runmodel.bat/; kbat.lw=0; put kbat;

parameter       processor       Processor count /0/;

put     '@echo off'/
        'if not exist listings\nul mkdir listings'/
        'if not exist gdx\nul mkdir gdx'//
        'if not "%','1"=="" goto processor%','1'//;
        for (processor=1 to nprocessor,
          put 'start /HIGH runmodel ',processor:0:0/;
        );
        put 'goto :eof'/;

*       Assign inputs to scenarios:

set     sc(scn)         Current scenario /1/;
loop((ds,coalition,verbca,lic_exempt,metric),
        inputs(sc,ds,coalition,verbca,lic_exempt,metric) = yes;
        sc(scn+1)$sc(scn) = yes;
        sc(scn)$sc(scn+1) = no;
);
execute_unload 'ssa.gdx', scn, inputs;

parameter       nscn    Scenario count;

processor = 0;
loop(scn,
  loop(inputs(scn,ds,coalition,verbca,lic_exempt,metric),
          if (scn.val > (processor/nprocessor)*card(scn), 
            if (processor>0, 
              put 'title Processor ',processor:0:0,' (100%%)'/;
              put 'goto :eof'/;);
            processor = processor + 1;
            put //':processor',processor:0:0/;
            nscn = 0;
          );
          put 'title Processor ',processor:0:0,' (',
                round(100*nscn/(card(scn)/nprocessor)):0:0,'%%)'/;

*       ------------------------------------------------------------------------------
*       Model-Specific Code: 

          put 'gams model ',
                ' o=listings\',scn.tl,'.lst',
                ' gdx=gdx\',scn.tl,'.gdx',
                ' --ds=',ds.tl,
                ' --coalition=',coalition.tl,
                ' --verbca=',verbca.tl,
                ' --lic_exempt=',lic_exempt.tl,
                ' --metric=',metric.tl/;

*       ------------------------------------------------------------------------------

          nscn = nscn + 1;
        );
);
put 'title Processor ',nprocessor:0:0,' (100%%)'/;

putclose;


$title Produce a Pivot Report Keyed to Input Assumptions $if not set output $set output PivotReport $gdxin 'ssa.gdx' set scn(*) Scenarios indices; $load scn alias (ds,coalition,verbca,lic_exempt,metric,*) set inputs(scn,ds,coalition,verbca,lic_exempt,metric) Input associations; $load inputs parameter welfare(scn,*,*,*) Welfare report $call 'gdxmerge gdx\*.gdx id=welfare' $gdxin merged.gdx $load welfare parameter ev Equivalent variation in social welfare (%); alias (swmetric,policy,*); loop(inputs(scn,ds,coalition,verbca,lic_exempt,metric), ev(policy,swmetric,ds,coalition,verbca,lic_exempt,metric) = welfare(scn,policy,swmetric,"EV%"); ); execute_unload 'pivotdata.gdx',ev; $onecho >gdxxrw.rsp par=ev rng=ev!a2 cdim=0 $offecho execute 'gdxxrw i=pivotdata.gdx o=%output%.xlsx @gdxxrw.rsp'