* -------------- anar1 ressamg -------------------------------------------

*#### GENERATE NEW GLOBAL SAMG FOR EACH EXPERIMENT

* Load Sets required in this module
$ontext
$GDXIN data_in.GDX

$LOAD map_tmr_w map_ter_w map_tff_f map_marg_w

$GDXIN

$offtext

DISPLAY map_tmr_w, map_ter_w, map_tff_f, map_marg_w ;

* SAM assignment statements by ROWS of the Regional SAMs

$ontext

The data are stored as SAMG. For ALL regions the model uses a SAM that has the
following structure of potentially active submatrices: X marks submatrices
active in the database and model.

       1     2     3     4     5     6     7     8     9     10    11    12
    � COM � ACT � FAC � HOU � TM  � TE  � ITX � YTX � GOV � KAP � MAR � ROW
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
COM �  0  �  X  �  0  �  X  �  0  �  0  �  0  �  0  �  X  �  X  �  0  �  X
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
ACT �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
FAC �  0  �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
HOU �  0  �  0  �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
TM  �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
TE  �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
ITX �  X  �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
YTX �  0  �  0  �  X  �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
GOV �  0  �  0  �  0  �  0  �  X  �  X  �  X  �  X  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
KAP �  0  �  0  �  X  �  X  �  0  �  0  �  0  �  0  �  X  �  0  �  0  �  X
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
MAR �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----
ROW �  X  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  0  �  X  �  0
----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----�-----

$offtext

* OPTION DECIMALS = 6 ;

* -------- Commodity Accounts ---------

* SAMG3(c,a,r,clos,simr)       = ioqint(c,a,r) * resQINT(a,r,clos,simr) * resPQD(c,r,clos,simr) ;
 SAMG3(c,a,r,clos,simr)
              = ioqint(c,a,r) * ioqintqx(a,r) * resQX(a,r,clos,simr)
                      * resPQD(c,r,clos,simr) ;

 SAMG3(c,h,r,clos,simr)
              = resQCD(c,h,r,clos,simr) * resPQD(c,r,clos,simr)
                              * (1 + resTV(c,r,clos,simr)) ;

 SAMG3(c,"govt",r,clos,simr)
              = resQGD(c,r,clos,simr) * resPQD(c,r,clos,simr) ;

 SAMG3(c,"kap",r,clos,simr)
              = resQINVD(c,r,clos,simr) * resPQD(c,r,clos,simr) ;

 SAMG3(c,w,r,clos,simr)
              = resQER(c,w,r,clos,simr) * resPWE(c,w,r,clos,simr)
                  * resER(r,clos,simr) ;

* -------- Activity Accounts ----------

 SAMG3(a,c,r,clos,simr)
              = ioqxcqx(a,c,r) * resPXC(c,r,clos,simr)
                   * resQX(a,r,clos,simr) ;

* -------- Factor Accounts ------------

 SAMG3(f,a,r,clos,simr)
              = resFD(f,a,r,clos,simr) * resWF(f,r,clos,simr)
                  * resWFDIST(f,a,r,clos,simr) ;

* -------- Household Accounts ---------

 SAMG3(h,f,r,clos,simr)
              = hvash(h,f,r) * resYFDIST(f,r,clos,simr) ;

* -------- Tariff Accounts ------------

 SAMG3(tmr,c,r,clos,simr)
              = SUM(w$map_tmr_w(tmr,w),resTM(w,c,r,clos,simr)
                    * resPWM(w,c,r,clos,simr) * resQMR(w,c,r,clos,simr)
                    * resER(r,clos,simr)) ;

 SAMG3(tmrs,c,r,clos,simr)
              = SUM(w$map_tmrs_w(tmrs,w),resTMS(w,c,r,clos,simr)
                    * resQMR(w,c,r,clos,simr)) ;

* -------- Export Duty Accounts -------

 SAMG3(ter,c,r,clos,simr)
              = SUM(w$map_ter_w(ter,w),resTE(c,w,r,clos,simr)
                    * resPWE(c,w,r,clos,simr) * resER(r,clos,simr)
                    * resQER(c,w,r,clos,simr)) ;

* -------- Indirect Tax Accounts ------

 SAMG3("saltax",c,r,clos,simr)
              = resTS(c,r,clos,simr) * resPQS(c,r,clos,simr)
                     * resQQ(c,r,clos,simr) ;

 SAMG3("vattax",c,r,clos,simr)
              = SUM(h,resTV(c,r,clos,simr) * resPQD(c,r,clos,simr)
                     * resQCD(c,h,r,clos,simr)) ;

 SAMG3(tff,a,r,clos,simr)
              = SUM(f$MAP_TFF_F(tff,f),resTF(f,a,r,clos,simr)
                    * resWF(f,r,clos,simr) * resWFDIST(f,a,r,clos,simr)
                    * resFD(f,a,r,clos,simr)) ;

 SAMG3("prodtax",a,r,clos,simr)
              = resTX(a,r,clos,simr) * resPX(a,r,clos,simr)
                      * resQX(a,r,clos,simr) ;

* -------- Direct Tax Accounts --------

 SAMG3("dirtax",f,r,clos,simr)
              = resTYF(f,r,clos,simr) * (1-deprec(f,r))
                       * resYF(f,r,clos,simr) ;

 SAMG3("dirtax",h,r,clos,simr)
              = resTYH(h,r,clos,simr) * resYH(h,r,clos,simr) ;

* -------- Government Accounts --------

 SAMG3("govt",tmr,r,clos,simr)  = SUM(c,SAMG3(tmr,c,r,clos,simr)) ;

 SAMG3("govt",tmrs,r,clos,simr) = SUM(c,SAMG3(tmrs,c,r,clos,simr)) ;

 SAMG3("govt",ter,r,clos,simr)  = SUM(c,SAMG3(ter,c,r,clos,simr)) ;

 SAMG3("govt",tff,r,clos,simr)  = SUM(a,SAMG3(tff,a,r,clos,simr)) ;

 SAMG3("govt","saltax",r,clos,simr)
              = SUM(c,SAMG3("saltax",c,r,clos,simr)) ;

 SAMG3("govt","vattax",r,clos,simr)
              = SUM(c,SAMG3("vattax",c,r,clos,simr)) ;

 SAMG3("govt","prodtax",r,clos,simr)
              = SUM(a,SAMG3("prodtax",a,r,clos,simr)) ;

 SAMG3("govt","dirtax",r,clos,simr)
              = SUM(f,SAMG3("dirtax",f,r,clos,simr))
                 + SUM(h,SAMG3("dirtax",h,r,clos,simr)) ;

* -------- Trade Margin Accounts ------

 SAMG3(owatpmarg,cp,r,clos,simr)
              = SUM(w$map_marg_w(owatpmarg,w),
                 resPT("cserv",r,clos,simr) * resQMR(w,cp,r,clos,simr)
                   * margcor(w,"cserv",cp,r)) ;

* -------- Trade Accounts -------------

 SAMG3(w,c,r,clos,simr)
              = resPWMFOB(w,c,r,clos,simr) * resQMR(w,c,r,clos,simr)
                               * resER(r,clos,simr) ;

 SAMG3("wglo",owatpmarg,r,clos,simr)
              = SUM(c,SAMG3(owatpmarg,c,r,clos,simr)) ;


* -------- Capital Accounts -----------

 SAMG3("kap",f,r,clos,simr)
              = deprec(f,r) * resYF(f,r,clos,simr) ;

 SAMG3("kap",wgn,r,clos,simr)
              = SUM(c,SAMG3(wgn,c,r,clos,simr))
                 - SUM(c,SAMG3(c,wgn,r,clos,simr)) ;

 SAMG3("kap","wglo",r,clos,simr)
              = SUM((owatpmarg,c),SAMG3(owatpmarg,c,r,clos,simr))
                 - SUM(c,SAMG3(c,"wglo",r,clos,simr)) ;

 SAMG3("kap","hous",r,clos,simr)
              = SUM(h,(resYH(h,r,clos,simr)
                 *(1-resTYH(h,r,clos,simr)))*(resSHH(h,r,clos,simr))) ;

 SAMG3("kap","govt",r,clos,simr)  = resKAPGOV(r,clos,simr) ;

* -------- Account Totals -------------


 SAMG3("TOTAL",sacnp,r,clos,simr)   = SUM(sacn,SAMG3(sacn,sacnp,r,clos,simr)) ;
 SAMG3(sacn,"TOTAL",r,clos,simr)    = SUM(sacnp,SAMG3(sacn,sacnp,r,clos,simr)) ;

* -------- Checks on SAMG -------------

 SAMG3CHK(sac,r,clos,simr) = (ABS(SAMG3(sac,"TOTAL",r,clos,simr)
                             - SAMG3("TOTAL",sac,r,clos,simr))) ;

 SAMG3CHK(sac,r,clos,simr)$(ABS(SAMG3CHK(sac,r,clos,simr)
                             LT (100 * mod_cont("toldiffsam"))))
                           = 0.0 ;

 CNTSAMG3CHK(r,clos,simr)
             = SUM(sac$SAMG3CHK(sac,r,clos,simr), 1) ;

Display SAMG3CHK, CNTSAMG3CHK ;

Execute_Unload  'results/anar1_ressamg.gdx', SAMG3
                                             SAMG3CHK
                                             CNTSAMG3CHK
 ;

* -------------- end of anar1 ressamg -------------------------------------
