* reploop.inc

DISPLAY "#### START: reploop.inc";    

* start: emi from land use change

$ONTEXT
shrconv2('crops',tcur(t),sim)$(NOT tmin(t)) = SUM(lndcro, QLAND.L(lndcro,t)) - SUM(lndcro, QLAND.L(lndcro,t-1));

shrconv2('livestock',tcur(t),sim)$(NOT tmin(t)) = SUM(lndliv, QLAND.L(lndliv,t)) - SUM(lndliv, QLAND.L(lndliv,t-1));
shrconv2(acfor,tcur(t),sim)$(shrconv2(acfor,t,sim)<0) = 0;

shrconv2(acfor,tcur(t),sim)$(shrconv2('crops',t,sim) + shrconv2('livestock',t,sim)) = 
  shrconv2(acfor,t,sim)/(shrconv2('crops',t,sim) + shrconv2('livestock',t,sim));



iemiconvX2('total',t,sim) = SUM(acnt, shrconv2(acnt,t,sim)*iemiconv(acnt));


emiconvindicX2('conv',tcur(t),sim)$(NOT tmin(t) AND (SUM(lndagrtot, QLAND.L(lndagrtot,t)) - SUM(lndagrtot, QLAND.L(lndagrtot,t-1)))>0) = 
  (SUM(lndagrtot, QLAND.L(lndagrtot,t)) - SUM(lndagrtot, QLAND.L(lndagrtot,t-1)))*scaling('qlandsolrep')*iemiconv('total');
$OFFTEXT

* in tons  
emiconvindicX2(acro,tcur(t),sim)$(NOT tmin(t)) = 
  [ SUM(fland, QF.L(fland,acro,t)) - SUM(fland, QF.L(fland,acro,t-1)) ] * scaling('qlandsolrep') * iemiconv(acro)*(-44/12);

emiconvindicX2(aliv,tcur(t),sim)$(NOT tmin(t)) = 
  [ SUM(fland, QF.L(fland,aliv,t)) - SUM(fland, QF.L(fland,aliv,t-1)) ] * scaling('qlandsolrep') * iemiconv(aliv)*(-44/12);

emiconvindicX2('fornprod',tcur(t),sim)$(NOT tmin(t)) = 
  [ SUM(lndfornprod, QLAND.L(lndfornprod,t)) - SUM(lndfornprod, QLAND.L(lndfornprod,t-1)) ] * scaling('qlandsolrep') * iemiconv('fornprod')*(-44/12);

emiconvindicX2('forplant',tcur(t),sim)$(NOT tmin(t)) = 
  [ SUM(lndforplant, QLAND.L(lndforplant,t)) - SUM(lndforplant, QLAND.L(lndforplant,t-1)) ] * scaling('qlandsolrep') * iemiconv('forplant')*(-44/12);
  
* end: emi from land use change



* start: level endogenous variables (name of variable  + X) ---------

WLANDAGRX(lndfortot,tcur(t),sim) = WLANDAGR.L(lndfortot,t);

DTFPREALX(a,tcur(t),sim) = DTFPREAL.L(a,t);
DDKINSTFPX(ins2,fcap,tcur(t),sim) = DDKINSTFP.L(ins2,fcap,t);


DINVVALFFX(tcur(t),sim) = DINVVALFF.L(t);
WFAX(f,a,tcur(t),sim) = WFA.L(f,a,t);
EXCAPRATX(f,a,tcur(t),sim) = EXCAPRAT.L(f,a,t);
QKASX(f,a,tcur(t),sim) = QKAS.L(f,a,t);

GAMMACX(c,ac,tcur(t),sim) = GAMMAC.L(c,ac,t);

TCO2X(a,tcur(t),sim) = TCO2.L(a,t);
TCO2SCALX(tcur(t),sim) = TCO2SCAL.L(t);
YTAXCO2X(tcur(t),sim) = YTAXCO2.L(t);
QXACX(a,c,tcur(t),sim) = QXAC.L(a,c,t);
PXACX(a,c,tcur(t),sim) = PXAC.L(a,c,t);
QDEFORX(lndfortot,tcur(t),sim) = QDEFOR.L(lndfortot,t);
QLANDX(lnd,tcur(t),sim) = QLAND.L(lnd,t);
REGPQDX(c,ac,tcur(t),sim) = REGPQD.L(c,ac,t);
PQDREALX(c,ac,tcur(t),sim) = PQDREAL.L(c,ac,t);
QTRSTFSCALX(instrst,tcur(t),sim) = QTRSTFSCAL.L(instrst,t);
QFSINITX(f,tcur(t),sim) = QFSINIT.L(f,t);
CALTFPAX(a,tcur(t),sim) = CALTFPA.L(a,t);
CALTFPX(tcur(t),sim) = CALTFP.L(t);
CPIX(tcur(t),sim) = CPI.L(t);
DKAX(f,a,tcur(t),sim) = DKA.L(f,a,t);
DKINSX(ins2,fcap,tcur(t),sim) = DKINS.L(ins2,fcap,t);
DPIX(tcur(t),sim) = DPI.L(t);
EGX(tcur(t),sim) = EG.L(t);
EHX(h,tcur(t),sim) = EH.L(h,t);

EMIX(ghg,ac,acp,tcur(t),sim) = EMI.L(ghg,ac,acp,t);
EMIX(ghg,ac,'total',tcur(t),sim)     = SUM(acnt, EMIX(ghg,ac,acnt,t,sim));

* express in same units as EMI
EMIX('CO2eq','LULC',ac,tcur(t),sim) = emiconvindicX2(ac,t,sim)*scaling('emirep')/scaling('emisol');
EMIX('CO2eq','LULC','total',tcur(t),sim) = SUM(acnt, emiconvindicX2(acnt,t,sim))*scaling('emirep')/scaling('emisol');

EMIX(ghg,'total',acp,tcur(t),sim)    = SUM(acnt, EMIX(ghg,acnt,acp,t,sim));


EMIX('total',ac,acp,tcur(t),sim)     = SUM(acnt, EMIX(acnt,ac,acp,t,sim));

* add emissions from land conversion
*EMIX('total','total','total',tcur(t),sim) = EMIX('total','total','total',t,sim) + emiconvindicX2('conv',t,sim);

EXRX(tcur(t),sim) = EXR.L(t);
FBORX(ins2,tcur(t),sim) = FBOR.L(ins2,t);
FDEBTX(ins2,tcur(t),sim) = FDEBT.L(ins2,t);
FDISCALX(tcur(t),sim) = FDISCAL.L(t);
FDIX(fcap,tcur(t),sim) = FDI.L(fcap,t);
FPRDAX(f,a,tcur(t),sim) = FPRDA.L(f,a,t);
GBORX(tcur(t),sim) = GBOR.L(t);
GDEBTX(tcur(t),sim) = GDEBT.L(t);
GDPMPX(tcur(t),sim) = GDPMP.L(t);
GOVRECGDPX(acgovrec,tcur(t),sim) = GOVRECGDP.L(acgovrec,t);
GOVSPNDGDPX(acgovspnd,tcur(t),sim) = GOVSPNDGDP.L(acgovspnd,t);
IADJ2X(ins2,tcur(t),sim) = IADJ2.L(ins2,t);
IADJX(ins2,fcap,tcur(t),sim) = IADJ.L(ins2,fcap,t);
INVVALGX(tcur(t),sim) = INVVALG.L(t);
LABPARTRATX(tcur(t),sim) = LABPARTRAT.L(t);
MPSSCALX(tcur(t),sim) = MPSSCAL.L(t);
MPSX(ins,tcur(t),sim) = MPS.L(ins,t);
NDFGX(tcur(t),sim) = NDFG.L(t);
NFFGX(tcur(t),sim) = NFFG.L(t);
NFFINSSCALX(tcur(t),sim) = NFFINSSCAL.L(t);
NFFNGX(tcur(t),sim) = NFFNG.L(t);
NGOVPAYGDPX(acngovpay,tcur(t),sim) = NGOVPAYGDP.L(acngovpay,t);
PAREALX(a,tcur(t),sim) = PAREAL.L(a,t);
PAX(a,tcur(t),sim) = PA.L(a,t);
PDDX(c,tcur(t),sim) = PDD.L(c,t);
PDSREALX(c,tcur(t),sim) = PDSREAL.L(c,t);
PDSX(c,tcur(t),sim) = PDS.L(c,t);
PENX(a,tcur(t),sim) = PEN.L(a,t);
PERX(c,r,tcur(t),sim) = PER.L(c,r,t);
PEX(c,tcur(t),sim) = PE.L(c,t);
PINTAX(a,tcur(t),sim) = PINTA.L(a,t);
PKENX(a,tcur(t),sim) = PKEN.L(a,t);
PKX(fcap,tcur(t),sim) = PK.L(fcap,t);
PLANDWATX(a,tcur(t),sim) = PLANDWAT.L(a,t);
PLLX(a,tcur(t),sim) = PLL.L(a,t);
PMRX(c,r,tcur(t),sim) = PMR.L(c,r,t);
PMX(c,tcur(t),sim) = PM.L(c,t);
PQDX(c,ac,tcur(t),sim) = PQD.L(c,ac,t);
PQSREALX(c,tcur(t),sim) = PQSREAL.L(c,t);
PQSX(c,tcur(t),sim) = PQS.L(c,t);
PSTARX(c,r,tcur(t),sim) = PSTAR.L(c,r,t);
PVAX(a,tcur(t),sim) = PVA.L(a,t);
PWATAX(a,tcur(t),sim) = PWATA.L(a,t);
PWERX(c,r,tcur(t),sim) = PWER.L(c,r,t);
PWMRX(c,r,tcur(t),sim) = PWMR.L(c,r,t);
PXX(c,tcur(t),sim) = PX.L(c,t);
QAX(a,tcur(t),sim) = QA.L(a,t);
QDX(c,tcur(t),sim) = QD.L(c,t);
QENX(a,tcur(t),sim) = QEN.L(a,t);
QERX(c,r,tcur(t),sim) = QER.L(c,r,t);
QEX(c,tcur(t),sim) = QE.L(c,t);
QFINSX(ins,f,tcur(t),sim) = QFINS.L(ins,f,t);
QFSX(f,tcur(t),sim) = QFS.L(f,t);
QFX(f,a,tcur(t),sim) = QF.L(f,a,t);
QFX('tot-lab',a,tcur(t),sim)         = SUM(flab, QF.L(flab,a,t));
QFX('tot-labm',a,tcur(t),sim)         = SUM(flabm, QF.L(flabm,a,t));
QFX('tot-labf',a,tcur(t),sim)         = SUM(flabf, QF.L(flabf,a,t));
QFX(ac,'total',tcur(t),sim)          = SUM(a, QFX(ac,a,t,sim));
QGSCALX(tcur(t),sim) = QGSCAL.L(t);
QGX(c,tcur(t),sim) = QG.L(c,t);
QHPCX(tcur(t),sim)                   = QHPC.L(t)*scaling('samsol')*scaling('samto1')/(scaling('qlabsol')*scaling('qlabto1'));
QHX(c,h,tcur(t),sim) = QH.L(c,h,t);
QINTAX(a,tcur(t),sim) = QINTA.L(a,t);
QINTX(c,a,tcur(t),sim) = QINT.L(c,a,t);
QINVX(c,tcur(t),sim) = QINV.L(c,t);
QKENX(a,tcur(t),sim) = QKEN.L(a,t);
QLABSCALX(tcur(t),sim) = QLABSCAL.L(t);
QLANDWATX(a,tcur(t),sim) = QLANDWAT.L(a,t);
QLLX(a,tcur(t),sim) = QLL.L(a,t);
QMRX(c,r,tcur(t),sim) = QMR.L(c,r,t);
QMX(c,tcur(t),sim) = QM.L(c,t);
QNGOSCALX(insngo,tcur(t),sim) = QNGOSCAL.L(insngo,t);
QNGOX(c,insngo,tcur(t),sim) = QNGO.L(c,insngo,t);
QQX(c,tcur(t),sim) = QQ.L(c,t);
QSSECSCALX(tcur(t),sim) = QSSECSCAL.L(t);
QSSECX(c,tcur(t),sim) = QSSEC.L(c,t);
QTRSTFX(c,instrst,tcur(t),sim) = QTRSTF.L(c,instrst,t);
QTRSTFX('total',instrst,tcur(t),sim) = SUM(c, QTRSTF.L(c,instrst,t)*PQD00(c,instrst));
QTX(c,tcur(t),sim) = QT.L(c,t);
QVAX(a,tcur(t),sim) = QVA.L(a,t);
QWATAX(a,tcur(t),sim) = QWATA.L(a,t);
QXX(c,tcur(t),sim) = QX.L(c,t);
REGPAX(a,tcur(t),sim) = REGPA.L(a,t);
REGPDSX(c,tcur(t),sim) = REGPDS.L(c,t);
REGPQSX(c,tcur(t),sim) = REGPQS.L(c,t);
REXRX(tcur(t),sim) = REXR.L(t);
RGDPFCX(tcur(t),sim) = RGDPFC.L(t);
RGDPMPX(tcur(t),sim) = RGDPMP.L(t);
RNDFGX(tcur(t),sim) = RNDFG.L(t);
RSURPGX(tcur(t),sim) = RSURPG.L(t);
SAVFX(tcur(t),sim) = SAVF.L(t);
SAVINSX(insdng,tcur(t),sim) = SAVINS.L(insdng,t);
SHIFX(ins,f,tcur(t),sim) = SHIF.L(ins,f,t);
SUBCSCALX(tcur(t),sim) = SUBCSCAL.L(t);
SUBCTX(tcur(t),sim) = SUBCT.L(t);
SUBCX(c,ac,tcur(t),sim) = SUBC.L(c,ac,t);
SURPGX(tcur(t),sim) = SURPG.L(t);
TASCALX(tcur(t),sim) = TASCAL.L(t);
TAX(a,tcur(t),sim) = TA.L(a,t);
TESCALX(taxexp,tcur(t),sim) = TESCAL.L(taxexp,t);
TEX(c,r,tcur(t),sim) = TE.L(c,r,t);
TFACTSCALX(taxfact,tcur(t),sim) = TFACTSCAL.L(taxfact,t);
TFACTX(f,a,tcur(t),sim) = TFACT.L(f,a,t);
TFPX(a,tcur(t),sim) = TFP.L(a,t);
TFSCALX(tcur(t),sim) = TFSCAL.L(t);
TFX(f,tcur(t),sim) = TF.L(f,t);
TMSCALX(taximp,tcur(t),sim) = TMSCAL.L(taximp,t);
TMX(c,r,tcur(t),sim) = TM.L(c,r,t);
TQSCALX(tcur(t),sim) = TQSCAL.L(t);
TQX(c,tcur(t),sim) = TQ.L(c,t);
TRNSFRSCALX(ac,tcur(t),sim) = TRNSFRSCAL.L(ac,t);
TRNSFRX(ac,ins,tcur(t),sim) = TRNSFR.L(ac,ins,t);
TVACSCALX(tcur(t),sim) = TVACSCAL.L(t);
TVACX(c,ac,tcur(t),sim) = TVAC.L(c,ac,t);
TYSCALX(tcur(t),sim) = TYSCAL.L(t);
TYX(ins,tcur(t),sim) = TY.L(ins,t);
UERATX(f,tcur(t),sim) = UERAT.L(f,t);
UERATX('tot-lab',tcur(t),sim)        = SUM(f, UERAT.L(f,t)*QFS.L(f,t)/SUM(fp$flab(fp), QFS.L(fp,t)));
WALRASX(tcur(t),sim) = WALRAS.L(t);
WFAVGX(f,tcur(t),sim) = WFAVG.L(f,t);
WFAVGX('tot-lab',tcur(t),sim)        = SUM(f$flab(f), SUM(a, WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t))) / SUM(f$flab(f), SUM(a, QF.L(f,a,t)));
WFDISTX(f,a,tcur(t),sim) = WFDIST.L(f,a,t);
WFX(f,tcur(t),sim) = WF.L(f,t);
YFX(f,tcur(t),sim) = YF.L(f,t);
YGX(tcur(t),sim) = YG.L(t);
YIFX(ins,f,tcur(t),sim) = YIF.L(ins,f,t);
YIX(insdng,tcur(t),sim) = YI.L(insdng,t);
YTAXACTX(tcur(t),sim) = YTAXACT.L(t);
YTAXCOMX(tcur(t),sim) = YTAXCOM.L(t);
YTAXDIRX(tcur(t),sim) = YTAXDIR.L(t);
YTAXEXPX(taxexp,tcur(t),sim) = YTAXEXP.L(taxexp,t);
YTAXFACTX(taxfact,tcur(t),sim) = YTAXFACT.L(taxfact,t);
YTAXFACX(tcur(t),sim) = YTAXFAC.L(t);
YTAXIMPX(taximp,tcur(t),sim) = YTAXIMP.L(taximp,t);
YTAXVACX(tcur(t),sim) = YTAXVAC.L(t);
QFMIGRX(ins,f,fp,t,sim) = QFMIGR.L(ins,f,fp,t);
WFRATX(f,fp,t,sim) = WFRAT.L(f,fp,t);
QFINSINITX(ins,f,t,sim) = QFINSINIT.L(ins,f,t);
QFSSCALX(f,t,sim) = QFSSCAL.L(f,t);
WFAX(f,a,tcur(t),sim)                = WFA.L(f,a,t);
DTRNSFSSECGOVX(t,sim) = DTRNSFSSECGOV.L(t);
TUSEX(sim,c,ac,tcur(t)) = TUSE.L(c,ac,t);


* end: level endogenous variables (name of variable  + X) -----------

qdstkX(ac,c,tcur(t),sim) = qdstk(ac,c,t);
qfsexogx(f,tcur(t),sim) = qfsexog(f,t);
dqfsexogx(f,tcur(t),sim) = dqfsexog(f,t);
qdeforbx(lndfortot,tcur(t),sim) = qdeforb(lndfortot,t);

wage11(ac,tcur(t),sim) = WFAVGX(ac,t,sim)*scaling('samsol')*scaling('samto1') / (scaling('qlabsol')*scaling('qlabto1'));


icax(c,a,tcur(t),sim) = ica(c,a,t);

wfax(f,a,tcur(t),sim)                = WF.L(f,t)*WFDIST.L(f,a,t);

wfa2x(f,acrep,tcur(t),sim)$SUM(a$macacrep(a,acrep), QF.L(f,a,t))
  = SUM(a$macacrep(a,acrep), WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t))/SUM(a$macacrep(a,acrep), QF.L(f,a,t));
wfarealx(f,a,tcur(t),sim)            = wfax(f,a,t,sim) / CPI.L(t);
wfrealx(f,tcur(t),sim)               = WF.L(f,t)/CPI.L(t);
qgbX(c,tcur(t),sim)                  = qgb(c,t);
trnsfrbX(ac,ins,tcur(t),sim)         = trnsfrb(ac,ins,t);
tmbX(c,r,tcur(t),sim)                = tmb(c,r,t);
dkinsbX(ins2,fcap,tcur(t),sim)       = dkinsb(ins2,fcap,t);
ddkinsx(ins2,f,tcur(t),sim)         = ddkins(ins2,f,t);
kggdpx(fcapg,tcur(t),sim)            = PK.L(fcapg,t)*SUM(ins, QFINS.L(ins,fcapg,t))/GDPMP.L(t);
kggdpx('total',tcur(t),sim)          = SUM(fcapg, kggdpx(fcapg,t,sim));
capgdpx(tcur(t),sim)                 = SUM(fcap, QFS.L(fcap,t)*PK.L(fcap,t))/GDPMP.L(t);
tstarx(c,r,tcur(t),sim)              = tstar(c,r,t);
qggdp(c,tcur(t),sim)                 = QG.L(c,t)*SUM(insgov, PQD.L(c,insgov,t)*pqddist(c,insgov)) / GDPMP.L(t);
qggdp('total',tcur(t),sim)           = SUM(c, qggdp(c,t,sim));
                                     
popX(h,tcur(t),sim)                  = pop(h,t);
popX('total',tcur(t),sim)            = SUM(h, pop(h,t));
                                     
subcbX(c,ac,tcur(t),sim)                   = subcb(c,ac,t);
                                     
qtrstbX(c,instrst,tcur(t),sim)             = qtrstb(c,instrst,t) ;
tq01x(c,sim)                         = tq01(c);
ty01x(ins,sim) = ty01(ins);


iemiX(ghg,ac,acp,tcur(t),sim) = iemi(ghg,ac,acp,t);

* model status
modsolstat('solvestat',tcur(t),sim) = IEEM.SOLVESTAT;
modsolstat('modelstat',tcur(t),sim) = IEEM.MODELSTAT;
* CNS
*modsolwarn(tcur(t),sim)$(modsolstat('modelstat',t,sim) NE 16 OR modsolstat('solvestat',t,sim) NE 1) = 1; 
* MCP
modsolwarn(tcur(t),sim)$(modsolstat('modelstat',t,sim) NE 1 OR modsolstat('solvestat',t,sim) NE 1) = 1; 


qeqdratx(sim,c,tcur(t))$(QE0(c,t) AND QD0(c,t)) = (QE.L(c,t)/QD.L(c,t))/(QE0(c,t)/QD0(c,t));


uerat2X(f,tcur(t),sim)$unemp(f,'UERAT00') = UERAT.L(f,t)*unemp(f,'UERAT200')/unemp(f,'UERAT00');
uerat2X('tot-lab',tcur(t),sim)            = SUM(f, uerat2X(f,t,sim)*QFS.L(f,t)/SUM(fp$flab(fp), QFS.L(fp,t)));


* qhpc2X in LCU per capita                               
qhpc2X(h,tcur(t),sim)               = 
  SUM(c, PQD00(c,h)*QH.L(c,h,t)*scaling('samsol')*scaling('samto1'))/(pop(h,t)*scaling('qlabsol')*scaling('qlabto1'));
qhpc2X('total',tcur(t),sim)         = 
  SUM((c,h), PQD00(c,h)*QH.L(c,h,t)*scaling('samsol')*scaling('samto1'))/SUM(h, pop(h,t)*scaling('qlabsol')*scaling('qlabto1'));

* yhpcX in LCU per capita                               
yhpcX(h,tcur(t),sim)               = 
  YI.L(h,t)*scaling('samsol')*scaling('samto1')/(pop(h,t)*scaling('qlabsol')*scaling('qlabto1'));
yhpcX('total',tcur(t),sim)         = 
  SUM(h, YI.L(h,t)*scaling('samsol')*scaling('samto1'))/SUM(h, pop(h,t)*scaling('qlabsol')*scaling('qlabto1'));



mpkX(ac,fcap,t,sim) = mpk(ac,fcap,t);
mpcapgovX(fcap,sim) = mpcapgov(fcap);
mtfpX(a,fcap,sim)   = mtfp(a,fcap);


* start: TFP calculation

* to do: 
* add qll2x, which is QLL without changes in factor-specific productivities
* add qken2x, which is QKEN without changes in factor-specific productivities

qva2x(a,tcur(t),sim) = TFP00(a) * phi_va(a) *
    ( (delta_ken(a)*QKEN.L(a,t)**(-rho_va(a)))$delta_ken(a)
    + (delta_ll(a)*QLL.L(a,t)**(-rho_va(a)))$delta_ll(a)
    + (delta_landwat(a)*QLANDWAT.L(a,t)**(-rho_va(a)))$delta_landwat(a) ) ** (-1/rho_va(a));



tfpaX(a,tcur(t),sim)$QVA00(a) = PVA00(a)*QVA.L(a,t) / (PVA00(a)*qva2X(a,t,sim));

tfpatotX(tcur(t),sim) = SUM(a, PVA00(a)*QVA.L(a,t)) / SUM(a, PVA00(a)*qva2X(a,t,sim));


* end: TPF calculation










* start: gdpindic ---------------------------------------------------

* Nominal

gdpindic('PrvCon','Nominal',tcur(t),sim)  =  SUM((c,h)$csam(c), PQD.L(c,h,t)*pqddist(c,h)*QH.L(c,h,t))
  + SUM((c,insngo), PQD.L(c,insngo,t)*QNGO.L(c,insngo,t));
gdpindic('PrvFixInv','Nominal',tcur(t),sim)  = SUM((ins2,fcapng), DKINS.L(ins2,fcapng,t)*PK.L(fcapng,t));
gdpindic('GovFixInv','Nominal',tcur(t),sim)  = SUM((ins2,fcapg), DKINS.L(ins2,fcapg,t)*PK.L(fcapg,t));
gdpindic('FixInv','Nominal',tcur(t),sim)  =  gdpindic('PrvFixInv','Nominal',t,sim) + gdpindic('GovFixInv','Nominal',t,sim);
*gdpindic('GovFixInvInfra','Nominal',tcur(t),sim)  = SUM((ins2,invginf), PK.L(invginf,t)*DKINS.L(ins2,invginf,t));
gdpindic('StockChange','Nominal',tcur(t),sim)  =  1e-15 + SUM((c,dstk), PQD.L(c,dstk,t)*qdstk('ngovz',c,t)) 
  + SUM((c,dstk), PQD.L(c,dstk,t)*qdstk('govz',c,t));
gdpindic('GovCon','Nominal',tcur(t),sim)  =  SUM((c,insgov), PQD.L(c,insgov,t)*pqddist(c,insgov)*QG.L(c,t))
  + SUM((c,insssec), PQD.L(c,insssec,t)*pqddist(c,insssec)*QSSEC.L(c,t));

gdpindic('Exports','Nominal',tcur(t),sim) =  SUM((c,r), PWER.L(c,r,t)*EXR.L(t)*QER.L(c,r,t)) 
  + SUM((c,instrst), PQD.L(c,instrst,t)*QTRSTF.L(c,instrst,t));
gdpindic('Imports','Nominal',tcur(t),sim) = -SUM((c,r), PWMR.L(c,r,t)*EXR.L(t)*QMR.L(c,r,t));
gdpindic('GDPMP','Nominal',tcur(t),sim)   =  SUM(igdp2, gdpindic(igdp2,'Nominal',t,sim));
gdpindic('NetIndTax','Nominal',tcur(t),sim) =
  + SUM(c, TQ.L(c,t)*PQS.L(c,t)*QQ.L(c,t))
  + YTAXVAC.L(t)
  - SUBCT.L(t)
  + SUM((c,r), TM.L(c,r,t)*EXR.L(t)*PWMR.L(c,r,t)*QMR.L(c,r,t))
  + SUM((c,r), TE.L(c,r,t)*EXR.L(t)*PWER.L(c,r,t)*QER.L(c,r,t))
  + SUM(a, TA.L(a,t)*PA.L(a,t)*QA.L(a,t));

* following line no incorpora termino TFP  
*gdpindic('GDPFC','Nominal',tcur(t),sim)   =  SUM((f,a), WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t));
gdpindic('GDPFC','Nominal',tcur(t),sim)   =  
  SUM(a, PVA.L(a,t)*QVA.L(a,t)) 
  - SUM((cen,a), PQD.L(cen,a,t)*pqddist(cen,a)*QINT.L(cen,a,t))
  - SUM((cwat,a), PQD.L(cwat,a,t)*pqddist(cwat,a)*QINT.L(cwat,a,t))
  - SUM((f,a)$fleo(f), WF.L(f,t)*WFDIST.L(f,a,t)*(1+TFACT.L(f,a,t))*QF.L(f,a,t));

gdpindic('GDPMP_alt','Nominal',tcur(t),sim)  =  gdpindic('GDPFC','Nominal',t,sim)
  + gdpindic('NetIndTax','Nominal',t,sim);

gdpindic('Absorption','Nominal',tcur(t),sim)  = gdpindic('GDPMP','Nominal',t,sim) - 
  gdpindic('Imports','Nominal',t,sim) - gdpindic('Exports','Nominal',t,sim);

* Real

gdpindic('PrvCon','Real',tcur(t),sim)  =  SUM((c,h)$csam(c), PQD00(c,h)*pqddist(c,h)*QH.L(c,h,t))
  + SUM((c,insngo), PQD00(c,insngo)*QNGO.L(c,insngo,t));
gdpindic('PrvFixInv','Real',tcur(t),sim)  = SUM((ins2,fcapng), PK00(fcapng)*DKINS.L(ins2,fcapng,t));
gdpindic('GovFixInv','Real',tcur(t),sim)  = SUM((ins2,fcapg), PK00(fcapg)*DKINS.L(ins2,fcapg,t));
gdpindic('FixInv','Real',tcur(t),sim)  =  gdpindic('PrvFixInv','Real',t,sim) + gdpindic('GovFixInv','Real',t,sim);
*gdpindic('GovFixInvInfra','Real',tcur(t),sim)  = SUM((ins2,invginf), PK00(invginf)*DKINS.L(ins2,invginf,t));
gdpindic('StockChange','Real',tcur(t),sim)  =  1e-15 + SUM((c,dstk), PQD00(c,dstk)*qdstk('ngovz',c,t)) 
  + SUM((c,dstk), PQD00(c,dstk)*qdstk('govz',c,t));
gdpindic('GovCon','Real',tcur(t),sim)  =  SUM((c,insgov), PQD00(c,insgov)*pqddist(c,insgov)*QG.L(c,t))
  + SUM((c,insssec), PQD00(c,insssec)*pqddist(c,insssec)*QSSEC.L(c,t));
gdpindic('Exports','Real',tcur(t),sim) =  SUM((c,r), PWER00(c,r)*EXR00*QER.L(c,r,t)) + SUM((c,instrst), PQD00(c,instrst)*QTRSTF.L(c,instrst,t));
gdpindic('Imports','Real',tcur(t),sim) = -SUM((c,r), PWMR00(c,r)*EXR00*QMR.L(c,r,t));
gdpindic('GDPMP','Real',tcur(t),sim)   =  SUM(igdp2, gdpindic(igdp2,'real',t,sim));

gdpindic('NetIndTax','Real',tcur(t),sim) =
  + SUM(c, TQ.L(c,t)*(PDD00(c)*QD.L(c,t) + PM00(c)*QM.L(c,t)))
  + SUM((c,r), TM.L(c,r,t)*EXR00*PWMR00(c,r)*QMR.L(c,r,t))
  + SUM((c,r), TE.L(c,r,t)*EXR00*PWER00(c,r)*QER.L(c,r,t))
  + SUM(a, TA.L(a,t)*PA00(a)*QA.L(a,t))
  + SUM((c,a),         (1-SUBC.L(c,a,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,a,t)*QINT.L(c,a,t))
  + SUM((c,h),         (1-SUBC.L(c,h,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,h,t)*QH.L(c,h,t))
  + SUM((c,insgov),    (1-SUBC.L(c,insgov,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,insgov,t)*QG.L(c,t))
  + SUM((c,insssec),    (1-SUBC.L(c,insssec,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,insssec,t)*QSSEC.L(c,t))
  
  + SUM((c,instrst),    (1-SUBC.L(c,instrst,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,instrst,t)*QTRSTF.L(c,instrst,t))  
  + SUM((c,insngo),    (1-SUBC.L(c,insngo,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,insngo,t)*QNGO.L(c,insngo,t))  
  + SUM((c,fcapng),     (1-SUBC.L(c,fcapng,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,fcapng,t)*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)))
  + SUM((c,fcapg),      (1-SUBC.L(c,fcapg,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,fcapg,t)*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)))
  + SUM((c,dstk),      (1-SUBC.L(c,dstk,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,dstk,t)*qdstk('ngovz',c,t))
  + SUM((c,dstk),     (1-SUBC.L(c,dstk,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,dstk,t)*qdstk('govz',c,t))
  + SUM((c,cp,tacm,r), (1-SUBC.L(c,tacm,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,tacm,t)*icm(c,cp,r)*QMR.L(cp,r,t))
  + SUM((c,cp,tace,r), (1-SUBC.L(c,tace,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,tace,t)*ice(c,cp,r)*QER.L(cp,r,t))
  + SUM((c,cp,tacd),   (1-SUBC.L(c,tacd,t))*PQS00(c)*(1+TQ.L(c,t))*TVAC.L(c,tacd,t)*icd(c,cp)*QD.L(cp,t))
  - SUM((c,a),         SUBC.L(c,a,t)*PQS00(c)*(1+TQ.L(c,t))*QINT.L(c,a,t))
  - SUM((c,h),         SUBC.L(c,h,t)*PQS00(c)*(1+TQ.L(c,t))*QH.L(c,h,t))
  - SUM((c,insgov),    SUBC.L(c,insgov,t)*PQS00(c)*(1+TQ.L(c,t))*QG.L(c,t))
  - SUM((c,insssec),    SUBC.L(c,insssec,t)*PQS00(c)*(1+TQ.L(c,t))*QSSEC.L(c,t))  
  - SUM((c,instrst),    SUBC.L(c,instrst,t)*PQS00(c)*(1+TQ.L(c,t))*QTRSTF.L(c,instrst,t))  
  - SUM((c,insngo),    SUBC.L(c,insngo,t)*PQS00(c)*(1+TQ.L(c,t))*QNGO.L(c,insngo,t))
  - SUM((c,fcapng),      SUBC.L(c,fcapng,t)*PQS00(c)*(1+TQ.L(c,t))*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)))
  - SUM((c,fcapg),      SUBC.L(c,fcapg,t)*PQS00(c)*(1+TQ.L(c,t))*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)))
  - SUM((c,dstk),      SUBC.L(c,dstk,t)*PQS00(c)*(1+TQ.L(c,t))*qdstk('ngovz',c,t))
  - SUM((c,dstk),     SUBC.L(c,dstk,t)*PQS00(c)*(1+TQ.L(c,t))*qdstk('govz',c,t))
  - SUM((c,cp,tacm,r), SUBC.L(c,tacm,t)*PQS00(c)*(1+TQ.L(c,t))*icm(c,cp,r)*QMR.L(cp,r,t))
  - SUM((c,cp,tace,r), SUBC.L(c,tace,t)*PQS00(c)*(1+TQ.L(c,t))*ice(c,cp,r)*QER.L(cp,r,t))
  - SUM((c,cp,tacd),   SUBC.L(c,tacd,t)*PQS00(c)*(1+TQ.L(c,t))*icd(c,cp)*QD.L(cp,t));

*gdpindic('GDPFC','Real',tcur(t),sim) =  SUM((f,a), WF00(f)*WFDIST00(f,a)*QF.L(f,a,t));
gdpindic('GDPFC','Real',tcur(t),sim)   =  
  SUM(a, PVA00(a)*QVA.L(a,t)) 
  - SUM((cen,a), PQD00(cen,a)*pqddist(cen,a)*QINT.L(cen,a,t))
  - SUM((cwat,a), PQD00(cwat,a)*pqddist(cwat,a)*QINT.L(cwat,a,t))
  - SUM((f,a)$fleo(f), WF00(f)*WFDIST00(f,a)*(1+TFACT.L(f,a,t))*QF.L(f,a,t));

gdpindic('Absorption','Real',tcur(t),sim)  = gdpindic('GDPMP','Real',t,sim) - 
  gdpindic('Imports','Real',t,sim) - gdpindic('Exports','Real',t,sim);

* re-scale
gdpindic(igdp,'Nominal',tcur(t),sim) = gdpindic(igdp,'Nominal',t,sim)*scaling('samsolrep');
gdpindic(igdp,'Real',tcur(t),sim) = gdpindic(igdp,'Real',t,sim)*scaling('samsolrep');
  
* Nomimanl Shares

gdpindic(igdp,'GDPshr',tcur(t),sim) = 100*gdpindic(igdp,'Nominal',t,sim) /
  gdpindic('gdpmp','Nominal',t,sim);

* Real Shares

gdpindic(igdp,'GDPshr-Real',tcur(t),sim) = 100*gdpindic(igdp,'Real',t,sim) /
  gdpindic('GDPMP','Real',t,sim);

* change sign for imports (from negative to positive)
gdpindic('Imports',kgdp,tcur(t),sim) = -gdpindic('Imports',kgdp,t,sim);
  

gdpindic(igdp,'deflator',tcur(t),sim)$gdpindic(igdp,'real',t,sim) = gdpindic(igdp,'nominal',t,sim)/gdpindic(igdp,'real',t,sim);
  
DISPLAY gdpindic;

* end: gdpindic -----------------------------------------------------



* start: gdpexpindic ------------------------------------------------

gdpexpindic(igdp,'Nominal',tcur(t),sim) = gdpindic(igdp,'Nominal',t,sim)/scaling('samsolrep'); 
gdpexpindic(igdp,'Real',tcur(t),sim) = gdpindic(igdp,'Real',t,sim)/scaling('samsolrep'); 

gdpexpindic('GNI','Nominal',tcur(t),sim) = gdpexpindic('GDPMP','Nominal',t,sim) 
  - SUM((insrow,f), YIF.L(insrow,f,t))
  + SUM((f,insrow), TRNSFR.L(f,insrow,t));
  
gdpexpindic('GNDI','Nominal',tcur(t),sim) = gdpexpindic('GNI','Nominal',t,sim) 
  - SUM((insrow,insdng), TRNSFR.L(insrow,insdng,t))
  - SUM((insrow,insgov), TRNSFR.L(insrow,insgov,t))
  + SUM((h,insrow),      TRNSFR.L(h,insrow,t))
  + SUM((insdnh,insrow), TRNSFR.L(insdnh,insrow,t));

gdpexpindic('GNSAV','Nominal',tcur(t),sim) = gdpexpindic('GNDI','Nominal',t,sim) 
  - gdpexpindic('PrvCon','Nominal',t,sim) 
  - gdpexpindic('GovCon','Nominal',t,sim);

gdpexpindic('InvTot','Nominal',tcur(t),sim) = gdpexpindic('FixInv','Nominal',t,sim) + gdpexpindic('StockChange','Nominal',t,sim);

gdpexpindic('InvTot_alt','Nominal',tcur(t),sim) = gdpexpindic('GNSAV','Nominal',t,sim) + SAVF.L(t)*EXR.L(t);
 
* start: to check 
gapgdpexpindic(tcur(t),sim) = gdpexpindic('InvTot_alt','Nominal',t,sim) 
  - gdpexpindic('FixInv','Nominal',t,sim)  - gdpexpindic('StockChange','Nominal',t,sim);
* end: to check

gdpexpindic('DeprCap','Nominal',tcur(t),sim) = 
  SUM((ins,fcap), QFINS.L(ins,fcap,t)*deprcap(fcap)*PK.L(fcap,t));

* start: EmiVal

* USD 30 per ton * exrdol00
* must be in same units as SAM
* note: EMIX is volume variable

* in USD
gdpexpindic('EmiVal','Nominal',tcur(t),sim) = 
  ( EMIX('total','total','total',t,sim)*scaling('emisol')*scaling('emito1')*30 );

* in LCU and SAM units
gdpexpindic('EmiVal','Nominal',tcur(t),sim) = 
  gdpexpindic('EmiVal','Nominal',t,sim)*misc('exrdol00')/scaling('samto1');

* in LCU and samsol units
gdpexpindic('EmiVal','Nominal',tcur(t),sim) = 
  gdpexpindic('EmiVal','Nominal',t,sim)/scaling('samsol');

* end: EmiVal
urntx('DeplForStock',tcur(t),sim)$SUM((ffor,afor), QF.L(ffor,afor,t)) = 
  SUM((ffor,afor), WF.L(ffor,t)*WFDIST.L(ffor,afor,t)*QF.L(ffor,afor,t))/SUM((ffor,afor), QF.L(ffor,afor,t));

gdpexpindic('DeplForStock','Nominal',tcur(t),sim) = 
* deforested area times unit rent of ffor in afor
  SUM(lndfortot, QDEFOR.L(lndfortot,t))*SUM(trnt, urntx('DeplForStock',t,sim)/( (1+0.04)**(ORD(trnt)-1) ));

*!! to do:
*      DeplMinStock      depletion of mineral resources

gdpexpindic('GenuineSavProx','Nominal',tcur(t),sim) = 
* deforested area times unit rent of ffor in afor
  SUM(lndfortot, QDEFOR.L(lndfortot,t))*SUM(trnt, urntx('DeplForStock',t,sim)/( (1+0.04)**(ORD(trnt)-1) ));

* Genuine Savings
gdpexpindic('GenuineSavProx',kgdp,tcur(t),sim) = gdpexpindic('GNSAV',kgdp,t,sim)
*  - gdpexpindic('DeprCap',kgdp,t,sim)
  - gdpexpindic('DeplForStock',kgdp,t,sim)
  - gdpexpindic('DeplMinStock',kgdp,t,sim)
  - gdpexpindic('EmiVal',kgdp,t,sim);
  
* real values for items adjusted by GDP deflator
gdpexpindic(igdpexp,'Real',tcur(t),sim)$(toreal=1) = gdpexpindic(igdpexp,'Nominal',t,sim)/gdpindic('GDPMP','deflator',t,sim);

* real values for items adjusted by CPI
gdpexpindic(igdpexp,'Real',tcur(t),sim)$(toreal=2) = gdpexpindic(igdpexp,'Nominal',t,sim)/CPI.L(t);

* re-scale
gdpexpindic(maccol,kgdp,tcur(t),sim) = gdpexpindic(maccol,kgdp,t,sim)*scaling('samsolrep');


* Nominal shares

gdpexpindic(maccol,'GDPshr',tcur(t),sim) = 100*gdpexpindic(maccol,'Nominal',t,sim) /
  gdpexpindic('GDPMP','Nominal',t,sim);





* end: gdpexpindic --------------------------------------------------



* start: SIMSAM -----------------------------------------------------

* filas tax
SIMSAM(taxdir,insdng,tcur(t),sim) = TY.L(insdng,t) * YI.L(insdng,t);
SIMSAM(taximp,c,tcur(t),sim) = SUM(r$maprtimp(r,taximp), TM.L(c,r,t)*EXR.L(t)*PWMR.L(c,r,t)*QMR.L(c,r,t));
SIMSAM(taxexp,c,tcur(t),sim) = SUM(r$maprtexp(r,taxexp), TE.L(c,r,t)*EXR.L(t)*PWER.L(c,r,t)*QER.L(c,r,t));
SIMSAM(taxcom,c,tcur(t),sim) = TQ.L(c,t)*PQS.L(c,t)*QQ.L(c,t);
SIMSAM(taxvatc,c,tcur(t),sim) = 
      SUM(a,         (1-SUBC.L(c,a,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,a,t)*QINT.L(c,a,t))
    + SUM(h,         (1-SUBC.L(c,h,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,h,t)*QH.L(c,h,t))
    + SUM(insgov,    (1-SUBC.L(c,insgov,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,insgov,t)*QG.L(c,t))
    + SUM(insssec,    (1-SUBC.L(c,insssec,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,insssec,t)*QSSEC.L(c,t))
    
    + SUM(instrst,    (1-SUBC.L(c,instrst,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,instrst,t)*QTRSTF.L(c,instrst,t))    
    + SUM(insngo,    (1-SUBC.L(c,insngo,t))*PQS.L(c,t)*(1+TQ.L(c,t)+REGPQS.L(c,t))*TVAC.L(c,insngo,t)*QNGO.L(c,insngo,t))
    + SUM(fcapng,     (1-SUBC.L(c,fcapng,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,fcapng,t)*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)))
    + SUM(fcapg,      (1-SUBC.L(c,fcapg,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,fcapg,t)*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)))
    + SUM(dstk,      (1-SUBC.L(c,dstk,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,dstk,t)*qdstk('ngovz',c,t))
    + SUM(dstk,     (1-SUBC.L(c,dstk,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,dstk,t)*qdstk('govz',c,t))
    + SUM((cp,tacm,r), (1-SUBC.L(c,tacm,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,tacm,t)*icm(c,cp,r)*QMR.L(cp,r,t))
    + SUM((cp,tace,r), (1-SUBC.L(c,tace,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,tace,t)*ice(c,cp,r)*QER.L(cp,r,t))
    + SUM((cp,tacd),   (1-SUBC.L(c,tacd,t))*PQS.L(c,t)*(1+TQ.L(c,t))*TVAC.L(c,tacd,t)*icd(c,cp)*QD.L(cp,t));
SIMSAM(taxact,a,tcur(t),sim) = TA.L(a,t)*PA.L(a,t)*QA.L(a,t);
SIMSAM(taxco2,a,tcur(t),sim) = TCO2.L(a,t)*CPI.L(t)*SUM(acnt$EMI00('CO2',acnt,a), EMI.L('CO2',acnt,a,t));

SIMSAM(taxfact,a,tcur(t),sim) = SUM(f$mtfactf(taxfact,f), TFACT.L(f,a,t)*WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t));

SIMSAM(taxfac,f,tcur(t),sim) = TF.L(f,t)*YF.L(f,t);
SIMSAM(subcom,c,tcur(t),sim) =
      SUM(a,         -SUBC.L(c,a,t)*PQS.L(c,t)*(1+TQ.L(c,t))*QINT.L(c,a,t))
    + SUM(h,         -SUBC.L(c,h,t)*PQS.L(c,t)*(1+TQ.L(c,t))*QH.L(c,h,t))
    + SUM(insgov,    -SUBC.L(c,insgov,t)*PQS.L(c,t)*(1+TQ.L(c,t))*QG.L(c,t))
    + SUM(insssec,    -SUBC.L(c,insssec,t)*PQS.L(c,t)*(1+TQ.L(c,t))*QSSEC.L(c,t))
    + SUM(instrst,    -SUBC.L(c,instrst,t)*PQS.L(c,t)*(1+TQ.L(c,t))*QTRSTF.L(c,instrst,t))    
    + SUM(insngo,    -SUBC.L(c,insngo,t)*PQS.L(c,t)*(1+TQ.L(c,t)+REGPQS.L(c,t))*QNGO.L(c,insngo,t))    
    + SUM(fcapng,      -SUBC.L(c,fcapng,t)*PQS.L(c,t)*(1+TQ.L(c,t))*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)))
    + SUM(fcapg,      -SUBC.L(c,fcapg,t)*PQS.L(c,t)*(1+TQ.L(c,t))*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)))
    + SUM(dstk,      -SUBC.L(c,dstk,t)*PQS.L(c,t)*(1+TQ.L(c,t))*qdstk('ngovz',c,t))
    + SUM(dstk,     -SUBC.L(c,dstk,t)*PQS.L(c,t)*(1+TQ.L(c,t))*qdstk('govz',c,t))
    + SUM((cp,tacm,r), -SUBC.L(c,tacm,t)*PQS.L(c,t)*(1+TQ.L(c,t))*icm(c,cp,r)*QMR.L(cp,r,t))
    + SUM((cp,tace,r), -SUBC.L(c,tace,t)*PQS.L(c,t)*(1+TQ.L(c,t))*ice(c,cp,r)*QER.L(cp,r,t))
    + SUM((cp,tacd),   -SUBC.L(c,tacd,t)*PQS.L(c,t)*(1+TQ.L(c,t))*icd(c,cp)*QD.L(cp,t));

* filas tax use
SIMSAM('tax-use',c,tcur(t),sim) = 
    + PQS.L(c,t)*SUM(a,       TUSE.L(c,a,t)*QINT.L(c,a,t))
    + PQS.L(c,t)*SUM(h,       TUSE.L(c,h,t)*QH.L(c,h,t))
    + PQS.L(c,t)*SUM(insngo,  TUSE.L(c,insngo,t)*QNGO.L(c,insngo,t))
    + PQS.L(c,t)*SUM(insssec, TUSE.L(c,insssec,t)*QSSEC.L(c,t))
    + PQS.L(c,t)*SUM(insgov,  TUSE.L(c,insgov,t)*QG.L(c,t))
    + PQS.L(c,t)*SUM(instrst, TUSE.L(c,instrst,t)*QTRSTF.L(c,instrst,t))
    + PQS.L(c,t)*SUM(fcapng,  TUSE.L(c,fcapng,t)*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)))
    + PQS.L(c,t)*SUM(fcapg,   TUSE.L(c,fcapg,t)*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)))
    + PQS.L(c,t)*SUM(dstk,    TUSE.L(c,dstk,t)*qdstk('ngovz',c,t))
    + PQS.L(c,t)*SUM(dstk,    TUSE.L(c,dstk,t)*qdstk('govz',c,t))
    + PQS.L(c,t)*SUM((cp,r),  TUSE.L(c,cp,t)*icm(c,cp,r)*QMR.L(cp,r,t))
    + PQS.L(c,t)*SUM((cp,r),  TUSE.L(c,cp,t)*ice(c,cp,r)*QER.L(cp,r,t))
    + PQS.L(c,t)*SUM(cp,      TUSE.L(c,cp,t)*icd(c,cp)*QD.L(cp,t));    

* filas regact    
SIMSAM(regact,a,tcur(t),sim) = REGPA.L(a,t)*PA.L(a,t)*QA.L(a,t);    
    
* filas regcom
SIMSAM(regcom,c,tcur(t),sim) = REGPQS.L(c,t)*PQS.L(c,t)*QQ.L(c,t) + REGPDS.L(c,t)*PDS.L(c,t)*QD.L(c,t)
    + PQS.L(c,t)*SUM(a,       REGPQD.L(c,a,t)*QINT.L(c,a,t))
    + PQS.L(c,t)*SUM(h,       REGPQD.L(c,h,t)*QH.L(c,h,t))
    + PQS.L(c,t)*SUM(insngo,  REGPQD.L(c,insngo,t)*QNGO.L(c,insngo,t))
    + PQS.L(c,t)*SUM(insssec, REGPQD.L(c,insssec,t)*QSSEC.L(c,t))
    + PQS.L(c,t)*SUM(insgov,  REGPQD.L(c,insgov,t)*QG.L(c,t))
    + PQS.L(c,t)*SUM(instrst, REGPQD.L(c,instrst,t)*QTRSTF.L(c,instrst,t))
    + PQS.L(c,t)*SUM(fcapng,  REGPQD.L(c,fcapng,t)*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)))
    + PQS.L(c,t)*SUM(fcapg,   REGPQD.L(c,fcapg,t)*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)))
    + PQS.L(c,t)*SUM(dstk,    REGPQD.L(c,dstk,t)*qdstk('ngovz',c,t))
    + PQS.L(c,t)*SUM(dstk,    REGPQD.L(c,dstk,t)*qdstk('govz',c,t))
    + PQS.L(c,t)*SUM((cp,r),  REGPQD.L(c,cp,t)*icm(c,cp,r)*QMR.L(cp,r,t))
    + PQS.L(c,t)*SUM((cp,r),  REGPQD.L(c,cp,t)*ice(c,cp,r)*QER.L(cp,r,t))
    + PQS.L(c,t)*SUM(cp,      REGPQD.L(c,cp,t)*icd(c,cp)*QD.L(cp,t));    

* filas phantom tax

SIMSAM('phantom-taxcom',c,tcur(t),sim) = SUM(h, GAMMAC.L(c,h,t)*PQS.L(c,t)*QH.L(c,h,t))
  + SUM(a, GAMMAC.L(c,a,t)*PQS.L(c,t)*QINT.L(c,a,t));

    
    
* filas impl-taxwat
SIMSAM('impl-taxwat',cwat,tcur(t),sim) = 
    SUM(a, PQD.L(cwat,a,t)*(pqddist(cwat,a)-1)*QINT.L(cwat,a,t))
  + SUM(h, PQD.L(cwat,h,t)*(pqddist(cwat,h)-1)*QH.L(cwat,h,t));
    
    
* filas com
SIMSAM(c,a,tcur(t),sim)     = PQD.L(c,a,t)*pqddist(c,a)*QINT.L(c,a,t);
SIMSAM(c,h,tcur(t),sim)$csam(c)     = PQD.L(c,h,t)*pqddist(c,h)*QH.L(c,h,t);
SIMSAM(c,invng,tcur(t),sim) = SUM(fcapng$mfcapinv(fcapng,invng), PQD.L(c,fcapng,t)*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t)));
SIMSAM(c,invg,tcur(t),sim) = SUM(fcapg$mfcapinv(fcapg,invg), PQD.L(c,fcapg,t)*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t)));
SIMSAM(c,dstk,tcur(t),sim)  = PQD.L(c,dstk,t)*(qdstk('ngovz',c,t)+qdstk('govz',c,t));
SIMSAM(c,insgov,tcur(t),sim) = PQD.L(c,insgov,t)*pqddist(c,insgov)*QG.L(c,t);
SIMSAM(c,insssec,tcur(t),sim) = PQD.L(c,insssec,t)*pqddist(c,insssec)*QSSEC.L(c,t);
SIMSAM(c,insngo,tcur(t),sim) = PQD.L(c,insngo,t)*pqddist(c,insngo)*QNGO.L(c,insngo,t);
SIMSAM(c,r,tcur(t),sim) = PWER.L(c,r,t)*EXR.L(t)*QER.L(c,r,t);
SIMSAM(c,tacd,tcur(t),sim)  = SUM(cp, PQD.L(c,tacd,t)*icd(c,cp)*QD.L(cp,t));
SIMSAM(c,tacm,tcur(t),sim)  = SUM(r$maprtacm(r,tacm), SUM(cp, PQD.L(c,tacm,t)*icm(c,cp,r)*QMR.L(cp,r,t)));
SIMSAM(c,tace,tcur(t),sim)  = SUM(r$maprtace(r,tace), SUM(cp, PQD.L(c,tace,t)*ice(c,cp,r)*QER.L(cp,r,t)));
SIMSAM(c,instrst,tcur(t),sim)  = PQD.L(c,instrst,t)*QTRSTF.L(c,instrst,t);

* filas marg
SIMSAM(tacd,c,tcur(t),sim) = SUM(ct, PQD.L(ct,tacd,t)*icd(ct,c)*QD.L(c,t));
SIMSAM(tacm,c,tcur(t),sim) = SUM(r$maprtacm(r,tacm), SUM(ct, PQD.L(ct,tacm,t)*icm(ct,c,r)*QMR.L(c,r,t)));
SIMSAM(tace,c,tcur(t),sim) = SUM(r$maprtace(r,tace), SUM(ct, PQD.L(ct,tace,t)*ice(ct,c,r)*QER.L(c,r,t)));

* filas fac
SIMSAM(f,a,tcur(t),sim)$fncap(f)     = WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t);
SIMSAM(f,a,tcur(t),sim)$fcap(f)     = WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t);
SIMSAM(f,insrow,tcur(t),sim) = TRNSFR.L(f,insrow,t);

* filas capins
SIMSAM(capinsdng,insdng,tcur(t),sim) = SAVINS.L(insdng,t);
SIMSAM(capinsgov,insgov,tcur(t),sim) = SURPG.L(t) + INVVALG.L(t);
SIMSAM(capinsrow,insrow,tcur(t),sim) = EXR.L(t)*SAVF.L(t);

SIMSAM(capinsgov,capinsdng,tcur(t),sim) = NDFG.L(t);
SIMSAM(capinsgov,capinsrow,tcur(t),sim) = NFFG.L(t)*EXR.L(t);
SIMSAM(capinsdng,capinsrow,tcur(t),sim) = NFFNG.L(t)*EXR.L(t);

* filas invng
SIMSAM(invng,capinsdng,tcur(t),sim) = SUM(fcapng$mfcapinv(fcapng,invng), PK.L(fcapng,t)*DKINS.L('ngovz',fcapng,t) - FDI.L(fcapng,t)*EXR.L(t));
SIMSAM(invng,capinsrow,tcur(t),sim) = SUM(fcapng$mfcapinv(fcapng,invng), FDI.L(fcapng,t)*EXR.L(t));
SIMSAM(invng,capinsgov,tcur(t),sim) = SUM(fcapng$mfcapinv(fcapng,invng), PK.L(fcapng,t)*DKINS.L('govz',fcapng,t));

* filas invg
SIMSAM(invg,capinsgov,tcur(t),sim) = SUM(fcapg$mfcapinv(fcapg,invg), DKINS.L('govz',fcapg,t)*PK.L(fcapg,t));
SIMSAM(invg,capinsdng,tcur(t),sim) = SUM(fcapg$mfcapinv(fcapg,invg), DKINS.L('ngovz',fcapg,t)*PK.L(fcapg,t));


* filas dstk
SIMSAM(dstk,capinsdng,tcur(t),sim) = SUM(c, PQD.L(c,dstk,t)*qdstk('ngovz',c,t));
SIMSAM(dstk,capinsgov,tcur(t),sim) = SUM(c, PQD.L(c,dstk,t)*qdstk('govz',c,t));

* filas r
SIMSAM(r,c,tcur(t),sim) = EXR.L(t)*PWMR.L(c,r,t)*QMR.L(c,r,t);

* filas row
SIMSAM(insrow,r,tcur(t),sim)      = SUM(c, EXR.L(t)*PWMR.L(c,r,t)*QMR.L(c,r,t)) - SUM(c, EXR.L(t)*PWER.L(c,r,t)*QER.L(c,r,t));
* EH unico socio comercial, r = insrow; queda trnsfr desde row hacia row
SIMSAM(insrow,insrow,tcur(t),sim) = 0;
SIMSAM(insrow,f,tcur(t),sim)      = YIF.L(insrow,f,t);
SIMSAM(insrow,insdng,tcur(t),sim) = TRNSFR.L(insrow,insdng,t);
SIMSAM(insrow,'gov',tcur(t),sim)  = TRNSFR.L(insrow,'gov',t);

* filas insdng
SIMSAM(insdng,f,tcur(t),sim)       = YIF.L(insdng,f,t);
SIMSAM(insdng,'gov',tcur(t),sim)   = TRNSFR.L(insdng,'gov',t);
SIMSAM(insdng,'row',tcur(t),sim)   = TRNSFR.L(insdng,'row',t);
SIMSAM(insdng,insdngp,tcur(t),sim) = TRNSFR.L(insdng,insdngp,t);

* filas hhhd
SIMSAM(h,'phantom-taxcom',tcur(t),sim) = SUM(c, GAMMAC.L(c,h,t)*PQS.L(c,t)*QH.L(c,h,t));


* filas insssec
SIMSAM(insssec,taxfact,tcur(t),sim) = SUM(a, SIMSAM(taxfact,a,t,sim)*shytaxfact(insssec,taxfact));


* filas instrst
SIMSAM(instrst,insrow,tcur(t),sim) = SUM(c, SIMSAM(c,instrst,t,sim));

* filas act
SIMSAM(a,c,tcur(t),sim) = PXAC.L(a,c,t)*QXAC.L(a,c,t);
SIMSAM(a,'phantom-taxcom',tcur(t),sim) = SUM(c, GAMMAC.L(c,a,t)*PQS.L(c,t)*QINT.L(c,a,t));
SIMSAM(a,taxfact,tcur(t),sim) = YTAXFACT.L(taxfact,t)*shytaxfact(a,taxfact);

* filas gov
SIMSAM(insgov,taxdir,tcur(t),sim) = SUM(insdng, SIMSAM(taxdir,insdng,t,sim));
SIMSAM(insgov,taxcom,tcur(t),sim) = SUM(c, SIMSAM(taxcom,c,t,sim));
SIMSAM(insgov,taximp,tcur(t),sim) = SUM(c, SIMSAM(taximp,c,t,sim));
SIMSAM(insgov,taxexp,tcur(t),sim) = SUM(c, SIMSAM(taxexp,c,t,sim));
SIMSAM(insgov,taxact,tcur(t),sim) = SUM(a, SIMSAM(taxact,a,t,sim));
SIMSAM(insgov,taxco2,tcur(t),sim) = SUM(a, SIMSAM(taxco2,a,t,sim));
SIMSAM(insgov,taxfact,tcur(t),sim) = SUM(a, SIMSAM(taxfact,a,t,sim)*shytaxfact(insgov,taxfact));
SIMSAM(insgov,taxfac,tcur(t),sim) = SUM(f, SIMSAM(taxfac,f,t,sim));
SIMSAM(insgov,taxvatc,tcur(t),sim) = SUM(c, SIMSAM(taxvatc,c,t,sim));
SIMSAM(insgov,subcom,tcur(t),sim) = SUM(c, SIMSAM(subcom,c,t,sim));
SIMSAM(insgov,'tax-use',tcur(t),sim) = SUM(c, SIMSAM('tax-use',c,t,sim));

SIMSAM(insgov,insrow,tcur(t),sim)  = TRNSFR.L(insgov,insrow,t);
SIMSAM(insgov,insdng,tcur(t),sim) = TRNSFR.L(insgov,insdng,t);
SIMSAM(insgov,f,tcur(t),sim)      = YIF.L('gov',f,t);
SIMSAM(insgov,regact,tcur(t),sim) = SUM(a, shryregpa(insgov,a,t)*REGPA.L(a,t)*PA.L(a,t)*QA.L(a,t));
SIMSAM(insgov,regcom,tcur(t),sim) = SUM(c, shryregpqs(insgov,c,t)*REGPQS.L(c,t)*PQS.L(c,t)*QQ.L(c,t)) 
  + SUM(c, shryregpds(insgov,c,t)*REGPDS.L(c,t)*PDS.L(c,t)*QD.L(c,t))

  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(a,       REGPQD.L(c,a,t)*QINT.L(c,a,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(h,       REGPQD.L(c,h,t)*QH.L(c,h,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(insngo,  REGPQD.L(c,insngo,t)*QNGO.L(c,insngo,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(insssec, REGPQD.L(c,insssec,t)*QSSEC.L(c,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*             REGPQD.L(c,insgov,t)*QG.L(c,t))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(instrst, REGPQD.L(c,instrst,t)*QTRSTF.L(c,instrst,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(fcapng,  REGPQD.L(c,fcapng,t)*capcomp(fcapng,c)*SUM(ins2, DKINS.L(ins2,fcapng,t))))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(fcapg,   REGPQD.L(c,fcapg,t)*capcomp(fcapg,c)*SUM(ins2, DKINS.L(ins2,fcapg,t))))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(dstk,    REGPQD.L(c,dstk,t)*qdstk('ngovz',c,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(dstk,    REGPQD.L(c,dstk,t)*qdstk('govz',c,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM((cp,r),  REGPQD.L(c,cp,t)*icm(c,cp,r)*QMR.L(cp,r,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM((cp,r),  REGPQD.L(c,cp,t)*ice(c,cp,r)*QER.L(cp,r,t)))
  + SUM(c, shryregpqd(insgov,c,t)*PQS.L(c,t)*SUM(cp,      REGPQD.L(c,cp,t)*icd(c,cp)*QD.L(cp,t)))
  
  ;


SIMSAM(insgov,'impl-taxwat',tcur(t),sim) = SUM(cwat, SIMSAM('impl-taxwat',cwat,t,sim));




* filas total
SIMSAM('total',ac,tcur(t),sim) = SUM(acntp, SIMSAM(acntp,ac,t,sim));
SIMSAM(ac,'total',tcur(t),sim) = SUM(acntp, SIMSAM(ac,acntp,t,sim));

* end: SIMSAM -------------------------------------------------------


simsambalchk(acnt,tcur(t),sim) = SIMSAM(acnt,'total',t,sim) - SIMSAM('total',acnt,t,sim);
errsimsambalchk(ac,tcur(t),sim)$(ABS(simsambalchk(ac,t,sim)) > 1e-4) = 1/0;
DISPLAY errsimsambalchk;

* re-scale
SIMSAM(ac,acp,tcur(t),sim) = SIMSAM(ac,acp,t,sim)*scaling('samsolrep');

simsamgdp(ac,acp,tcur(t),sim) = 100*SIMSAM(ac,acp,t,sim)/gdpindic('GDPMP','Nominal',t,sim);



* start: investindic ------------------------------------------------

investindic(inv,'Nominal',tcur(t),sim) = SUM(capins, SIMSAM(inv,capins,t,sim));
investindic(inv,'Real',tcur(t),sim)    = SUM(fcap$mfcapinv(fcap,inv), PK00(fcap)*SUM(ins2, DKINS.L(ins2,fcap,t)))*scaling('samsolrep');
investindic(inv,'GDPshr',tcur(t),sim)  = SUM(fcap$mfcapinv(fcap,inv), 100*PK.L(fcap,t)*SUM(ins2, DKINS.L(ins2,fcap,t)))*scaling('samsolrep')/gdpindic('gdpmp','Nominal',t,sim);

investindic('tot-invng',kgdp,tcur(t),sim) = SUM(invng, investindic(invng,kgdp,t,sim));
investindic('tot-invg',kgdp,tcur(t),sim) = SUM(invg, investindic(invg,kgdp,t,sim));

* end: investindic --------------------------------------------------



* start: sectorstruc ------------------------------------------------

*Note: For VAshr, PRDshr, and EMPshr, data scaled by share of activity
*in output of commodity to avoid double-counting when there is not a
*one-to-one mapping between activities and commodities.

*sectorstruc(c,'VAshr',tcur(t),sim)       = 1e-15 + 100 * SUM((f,a), theta(a,c,t)*WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t)) / SUM((f,a), WF.L(f,t)*WFDIST.L(f,a,t)*QF.L(f,a,t));
sectorstruc(c,'VAshr',tcur(t),sim)       = 1e-15 + 100 * SUM(a, theta(a,c,t)*PVA.L(a,t)*QVA.L(a,t)) / SUM(a, PVA.L(a,t)*QVA.L(a,t));

sectorstruc('total','VAshr',tcur(t),sim) = SUM(c, sectorstruc(c,'VAshr',t,sim));

sectorstruc(c,'PRDshr',tcur(t),sim)       = 1e-15 + 100 * SUM(a, theta(a,c,t)*PA.L(a,t)*QA.L(a,t)) / SUM(ap, PA.L(ap,t)*QA.L(ap,t));
sectorstruc('total','PRDshr',tcur(t),sim) = SUM(c, sectorstruc(c,'PRDshr',t,sim));

sectorstruc(c,'EMPshr',tcur(t),sim)       = 1e-15 + 100 * SUM(a, SUM(f$flab(f), theta(a,c,t)*QF.L(f,a,t))) / SUM(ap, SUM(f$flab(f), QF.L(f,ap,t)));
sectorstruc('total','EMPshr',tcur(t),sim) = SUM(c, sectorstruc(c,'EMPshr',t,sim));

* expos
*!! a precios mundiales
sectorstruc(c,'EXPshr',tcur(t),sim)       = 1e-15 + 100 * [ SUM(r, PWER.L(c,r,t)*EXR.L(t)*QER.L(c,r,t)) 
  + SUM(instrst, QTRSTF.L(c,instrst,t)*PQD.L(c,instrst,t)) ] 
  / 
  [ SUM((cp,r), PWER.L(cp,r,t)*EXR.L(t)*QER.L(cp,r,t)) + SUM((cp,instrst), QTRSTF.L(cp,instrst,t)*PQD.L(cp,instrst,t)) ];
sectorstruc('total','EXPshr',tcur(t),sim) = SUM(c, sectorstruc(c,'EXPshr',t,sim));

* impos
*!! a precios mundiales
sectorstruc(c,'IMPshr',tcur(t),sim)       = 1e-15 + 100 * SUM(r, PWMR.L(c,r,t)*EXR.L(t)*QMR.L(c,r,t)) / SUM((cp,r), PWMR.L(cp,r,t)*EXR.L(t)*QMR.L(cp,r,t));
sectorstruc('total','IMPshr',tcur(t),sim) = SUM(c, sectorstruc(c,'IMPshr',t,sim));

* expos
$ONTEXT
sectorstruc(c,'EXP-OUTshr',tcur(t),sim)$(PX.L(c,t)*QX.L(c,t))
  = 1e-15 + 100 * SUM(r, PWER.L(c,r,t)*EXR.L(t)*QER.L(c,r,t)) / (PX.L(c,t)*QX.L(c,t));
sectorstruc('total','EXP-OUTshr',tcur(t),sim) =  100 * SUM((cp,r), PWER.L(cp,r,t)*EXR.L(t)*QER.L(cp,r,t)) / SUM(cp, PX.L(cp,t)*QX.L(cp,t));
$OFFTEXT

sectorstruc(c,'EXP-OUTshr',tcur(t),sim)$(PX.L(c,t)*QX.L(c,t)) = 1e-15 + 100 * [ SUM(r, PER.L(c,r,t)*QER.L(c,r,t)) 
  + SUM(instrst, QTRSTF.L(c,instrst,t)*PQD.L(c,instrst,t)) ] 
  / 
  (PX.L(c,t)*QX.L(c,t));
sectorstruc('total','EXP-OUTshr',tcur(t),sim) =  100 * SUM((cp,r), PER.L(cp,r,t)*QER.L(cp,r,t)) / SUM(cp, PX.L(cp,t)*QX.L(cp,t));


* impos
$ONTEXT
sectorstruc(c,'IMP-DEMshr',tcur(t),sim)$(PQ.L(c,t)*QQ.L(c,t)) =
  1e-15 + 100 * SUM(r, PWMR.L(c,r,t)*EXR.L(t)*QMR.L(c,r,t)) / (PQ.L(c,t)*QQ.L(c,t));
sectorstruc('total','IMP-DEMshr',tcur(t),sim) = 100 * SUM((cp,r), PWMR.L(cp,r,t)*EXR.L(t)*QMR.L(cp,r,t)) / SUM(cp, PQ.L(cp,t)*QQ.L(cp,t));
$OFFTEXT
sectorstruc(c,'IMP-DEMshr',tcur(t),sim)$(PQS.L(c,t)*QQ.L(c,t)) =
  1e-15 + 100 * SUM(r, PMR.L(c,r,t)*QMR.L(c,r,t)) / ( PQS.L(c,t)*QQ.L(c,t) );
sectorstruc('total','IMP-DEMshr',tcur(t),sim) = 100 * SUM((cp,r), PMR.L(cp,r,t)*QMR.L(cp,r,t)) / SUM(cp, PQS.L(cp,t)*QQ.L(cp,t));

* end: sectorstruc --------------------------------------------------



* start: sectorstruc2 -----------------------------------------------

sectorstruc2(acrep,sectorcol1,tcur(t),sim) = SUM(ac$macacrep(ac,acrep), sectorstruc(ac,sectorcol1,t,sim));

sectorstruc2(acrep,'EXP-OUTshr',tcur(t),sim)$(SUM(c$macacrep(c,acrep), PX.L(c,t)*QX.L(c,t)))
  = 1e-15 + 100 * [ SUM(c$macacrep(c,acrep), SUM(r, PER.L(c,r,t)*QER.L(c,r,t))) 
    + SUM(c$macacrep(c,acrep), SUM(instrst, QTRSTF.L(c,instrst,t)*PQD.L(c,instrst,t))) ]
    / 
    SUM(c$macacrep(c,acrep), PX.L(c,t)*QX.L(c,t));


sectorstruc2('total3','EXP-OUTshr',tcur(t),sim) = sectorstruc('total','EXP-OUTshr',t,sim);

sectorstruc2(acrep,'IMP-DEMshr',tcur(t),sim)$(SUM(c$macacrep(c,acrep), PQS.L(c,t)*QQ.L(c,t)))
  = 1e-15 + 100 * SUM(c$macacrep(c,acrep), SUM(r, PMR.L(c,r,t)*QMR.L(c,r,t))) / SUM(c$macacrep(c,acrep), PQS.L(c,t)*QQ.L(c,t));
sectorstruc2('total3','IMP-DEMshr',tcur(t),sim) = sectorstruc('total','IMP-DEMshr',t,sim);

* end: sectorstruc2 -------------------------------------------------


* start: taxstruc ---------------------------------------------------

taxstruc(taxdir,'TaxRev',tcur(t),sim)  = SUM(insdng, SIMSAM(taxdir,insdng,t,sim));
taxstruc(taximp,'TaxRev',tcur(t),sim)  = SUM(c, SIMSAM(taximp,c,t,sim));
taxstruc(taxexp,'TaxRev',tcur(t),sim)  = SUM(c, SIMSAM(taxexp,c,t,sim));
taxstruc(taxcom,'TaxRev',tcur(t),sim)  = SUM(c, SIMSAM(taxcom,c,t,sim));
taxstruc(taxact,'TaxRev',tcur(t),sim)  = SUM(a, SIMSAM(taxact,a,t,sim));
taxstruc(taxvatc,'TaxRev',tcur(t),sim)  = SUM(c, SIMSAM(taxvatc,c,t,sim));
taxstruc(taxfact,'TaxRev',tcur(t),sim) = SUM(a, SUM(f$mtfactf(taxfact,f), SIMSAM(taxfact,a,t,sim)));
taxstruc('total','TaxRev',tcur(t),sim) = SUM(actax, taxstruc(actax,'TaxRev',t,sim));

* GDPshr
taxstruc(ac,'GDPshr',tcur(t),sim)  = 100 * taxstruc(ac,'TaxRev',t,sim) / gdpindic('GDPMP','Nominal',t,sim);

* shrTaxRev
taxstruc(ac,'shrTaxRev',tcur(t),sim)  = 100 * taxstruc(ac,'TaxRev',t,sim) / SUM((actax,insgov), SIMSAM(insgov,actax,t,sim));

* end: taxstruc -----------------------------------------------------

* start: taxstrucdis

taxstrucdis(taximp,dsam,c,'TaxRev',tcur(t),sim)$SUM(dsamp, SIMSAM(c,dsamp,t,sim)) = 
  SIMSAM(taximp,c,t,sim) * SIMSAM(c,dsam,t,sim)/SUM(dsamp, SIMSAM(c,dsamp,t,sim));

taxstrucdis(taxcom,dsam,c,'TaxRev',tcur(t),sim)$SUM(dsamp, SIMSAM(c,dsamp,t,sim)) = 
  SIMSAM(taxcom,c,t,sim) * SIMSAM(c,dsam,t,sim)/SUM(dsamp, SIMSAM(c,dsamp,t,sim));

taxstrucdis(taxvatc,dsam,c,'TaxRev',tcur(t),sim)  = SIMSAM(taxvatc,c,t,sim) * SIMSAM(c,dsam,t,sim)/SUM(dsamp, SIMSAM(c,dsamp,t,sim));

taxstrucdis(ac,'total',c,taxcol,tcur(t),sim) = SUM(dsam, taxstrucdis(ac,dsam,c,taxcol,t,sim));

* end: taxstrucdis


* start: fiscalindic ------------------------------------------------

fiscalindic('IncCur-DirTax','Nominal',tcur(t),sim)    = SUM((insgov,taxdir), SIMSAM(insgov,taxdir,t,sim));
fiscalindic('IncCur-FacTax','Nominal',tcur(t),sim)    = SUM((insgov,taxfac), SIMSAM(insgov,taxfac,t,sim));
fiscalindic('IncCur-ActTax','Nominal',tcur(t),sim)    = SUM((insgov,taxact), SIMSAM(insgov,taxact,t,sim));
fiscalindic('IncCur-CO2Tax','Nominal',tcur(t),sim)    = SUM((insgov,taxco2), SIMSAM(insgov,taxco2,t,sim));

fiscalindic('IncCur-FacActTax','Nominal',tcur(t),sim) = SUM((insgov,taxfact), SIMSAM(insgov,taxfact,t,sim));
fiscalindic('IncCur-ComTax','Nominal',tcur(t),sim)    = SUM((insgov,taxcom), SIMSAM(insgov,taxcom,t,sim))
  + SUM(insgov, SIMSAM(insgov,'impl-taxwat',t,sim));
fiscalindic('IncCur-VACTax','Nominal',tcur(t),sim)     = SUM((insgov,taxvatc), SIMSAM(insgov,taxvatc,t,sim));
fiscalindic('IncCur-ImpTax','Nominal',tcur(t),sim)    = SUM((insgov,taximp), SIMSAM(insgov,taximp,t,sim));
fiscalindic('IncCur-ExpTax','Nominal',tcur(t),sim)    = SUM((insgov,taxexp), SIMSAM(insgov,taxexp,t,sim));
fiscalindic('IncCur-CO2Tax','Nominal',tcur(t),sim)    = SUM((insgov,taxco2), SIMSAM(insgov,taxco2,t,sim));
fiscalindic('IncCur-TaxUse','Nominal',tcur(t),sim)    = SUM((insgov), SIMSAM(insgov,'tax-use',t,sim));

fiscalindic('IncCur-TrnsfrDom','Nominal',tcur(t),sim) = SUM((insgov,insd), SIMSAM(insgov,insd,t,sim));
fiscalindic('IncCur-TrnsfrFor','Nominal',tcur(t),sim) = SUM((insgov,insrow), SIMSAM(insgov,insrow,t,sim));
fiscalindic('IncCur-FacInc','Nominal',tcur(t),sim)    = SUM((insgov,f), SIMSAM(insgov,f,t,sim));

fiscalindic('IncCur-Regul','Nominal',tcur(t),sim)    = SUM((insgov,regact), SIMSAM(insgov,regact,t,sim)) 
  + SUM((insgov,regcom), SIMSAM(insgov,regcom,t,sim));    


fiscalindic('IncCur-Total','Nominal',tcur(t),sim)     = SUM(fiscalcol1, fiscalindic(fiscalcol1,'Nominal',t,sim));

fiscalindic('SpndCur-Con','Nominal',tcur(t),sim)       = SUM((c,insgov), SIMSAM(c,insgov,t,sim));  
fiscalindic('SpndCur-ComSub','Nominal',tcur(t),sim)       = SUM((insgov,subcom), -SIMSAM(insgov,subcom,t,sim));  
fiscalindic('SpndCur-TrnsfrDom','Nominal',tcur(t),sim) = SUM((insd,insgov), SIMSAM(insd,insgov,t,sim));
fiscalindic('SpndCur-TrnsfrFor','Nominal',tcur(t),sim) = SUM(insgov, SIMSAM('row',insgov,t,sim));
fiscalindic('SpndCur-Total','Nominal',tcur(t),sim)     = SUM(fiscalcol2, fiscalindic(fiscalcol2,'Nominal',t,sim));

fiscalindic('GovSav','Nominal',tcur(t),sim)      = SUM((capinsgov,insgov), SIMSAM(capinsgov,insgov,t,sim));

*fiscalindic('SpndCap-FixInv','Nominal',tcur(t),sim) = SUM(fcap, DKINS.L('govz',fcap,t)*PK.L(fcap,t))*scaling('samsolrep');
fiscalindic('SpndCap-FixInv','Nominal',tcur(t),sim) = SUM((inv,capinsgov), SIMSAM(inv,capinsgov,t,sim));

fiscalindic('SpndCap-StockChange','Nominal',tcur(t),sim) = SUM((dstk,capinsgov), SIMSAM(dstk,capinsgov,t,sim));
  
fiscalindic('GovSurp','Nominal',tcur(t),sim) = fiscalindic('GovSav','Nominal',t,sim) 
  - fiscalindic('SpndCap-FixInv','Nominal',t,sim)
  - fiscalindic('SpndCap-StockChange','Nominal',t,sim);
fiscalindic('IncCap-NetDomFin','Nominal',tcur(t),sim) = NDFG.L(t)*scaling('samsolrep');
fiscalindic('IncCap-NetForFin','Nominal',tcur(t),sim) = NFFG.L(t)*EXR.L(t)*scaling('samsolrep');

fiscalindic('Inc-Total','Nominal',tcur(t),sim) = SUM(fiscalcol1,fiscalindic(fiscalcol1,'Nominal',t,sim));
fiscalindic('Spnd-Total','Nominal',tcur(t),sim) = SUM(fiscalcol2,fiscalindic(fiscalcol2,'Nominal',t,sim));


fiscalindic(fiscalcol,'GDPshr',tcur(t),sim)$gdpindic('gdpmp','Nominal',t,sim) = 100 * fiscalindic(fiscalcol,'Nominal',t,sim) / gdpindic('gdpmp','Nominal',t,sim);


* check balance in fiscalindic
* SAVG - YG + EG = 0
errfiscalindic1(tcur(t),sim)$
  (ABS(fiscalindic('GovSav','Nominal',t,sim)/scaling('samsolrep') 
     - fiscalindic('IncCur-Total','Nominal',t,sim)/scaling('samsolrep')
     + fiscalindic('SpndCur-Total','Nominal',t,sim)/scaling('samsolrep')) >1e-3)=1/0;
DISPLAY errfiscalindic1;

errfiscalindic2(tcur(t),sim)$
  (ABS(fiscalindic('Inc-Total','Nominal',t,sim)/scaling('samsolrep') 
     - fiscalindic('Spnd-Total','Nominal',t,sim)/scaling('samsolrep')) >1e-3)=1/0;
DISPLAY errfiscalindic2;



* end: fiscalindic --------------------------------------------------

* start: BdP --------------------------------------------------------

* inflows
bopindic('InCur-Exports','Nominal',tcur(t),sim) = SUM((c,r), SIMSAM(c,r,t,sim));
bopindic('InCur-Tourism','Nominal',tcur(t),sim) = SUM((c,instrst), SIMSAM(c,instrst,t,sim));
bopindic('InCur-Trnsfr-NoGov','Nominal',tcur(t),sim) = SUM((insdng,insrow), SIMSAM(insdng,insrow,t,sim));
bopindic('InCur-Trnsfr-Gov','Nominal',tcur(t),sim) = SUM((insgov,insrow), SIMSAM(insgov,insrow,t,sim));
bopindic('InCur-FacInc','Nominal',tcur(t),sim) = SUM((f,insrow), SIMSAM(f,insrow,t,sim));
bopindic('InCur-RowSav','Nominal',tcur(t),sim) = SUM((capinsrow,insrow), SIMSAM(capinsrow,insrow,t,sim));
bopindic('InCur-Total','Nominal',tcur(t),sim) = SUM(bopcol1, bopindic(bopcol1,'Nominal',t,sim));

bopindic('InCap-FDI','Nominal',tcur(t),sim) = SUM((inv,capinsrow), SIMSAM(inv,capinsrow,t,sim));
bopindic('InCap-NetForFin-NoGov','Nominal',tcur(t),sim) = SUM((capinsdng,capinsrow), SIMSAM(capinsdng,capinsrow,t,sim));
bopindic('InCap-NetForFin-Gov','Nominal',tcur(t),sim) = SUM((capinsgov,capinsrow), SIMSAM(capinsgov,capinsrow,t,sim));
bopindic('InCap-NetForFin-Total','Nominal',tcur(t),sim) = bopindic('InCap-FDI','Nominal',t,sim)
  + bopindic('InCap-NetForFin-NoGov','Nominal',t,sim)
  + bopindic('InCap-NetForFin-Gov','Nominal',t,sim);

    
    
* outflows
bopindic('OutCur-Imports','Nominal',tcur(t),sim)  = SUM((r,c), SIMSAM(r,c,t,sim));
bopindic('OutCur-Trnsfr-NoGov','Nominal',tcur(t),sim) = SUM((insrow,insdng), SIMSAM(insrow,insdng,t,sim));
bopindic('OutCur-Trnsfr-Gov','Nominal',tcur(t),sim) = SUM((insrow,insgov), SIMSAM(insrow,insgov,t,sim));
bopindic('OutCur-FacInc','Nominal',tcur(t),sim) = SUM((insrow,f), SIMSAM(insrow,f,t,sim));
bopindic('OutCur-Total','Nominal',tcur(t),sim) = SUM(bopcol2, bopindic(bopcol2,'Nominal',t,sim));



bopindic(bopcol,'NomFCU',tcur(t),sim) = bopindic(bopcol,'Nominal',t,sim)/EXR.L(t);
bopindic(bopcol,'GDPshr',tcur(t),sim)$gdpindic('gdpmp','Nominal',t,sim) = 100 * (bopindic(bopcol,'Nominal',t,sim) / gdpindic('gdpmp','Nominal',t,sim));


* check balance in bopindic
errbopindic1(tcur(t),sim)$
  (ABS(bopindic('InCur-RowSav','Nominal',t,sim)/scaling('samsolrep') - bopindic('InCap-NetForFin-Total','Nominal',t,sim)/scaling('samsolrep'))>1e-4)=1/0;
DISPLAY errbopindic1;

errbopindic2(tcur(t),sim)$
  (ABS(bopindic('InCur-Total','Nominal',t,sim)/scaling('samsolrep') - bopindic('OutCur-Total','Nominal',t,sim)/scaling('samsolrep'))>1e-4)=1/0;
DISPLAY errbopindic2;

* end: BdP ----------------------------------------------------------


* start: sectorindic ------------------------------------------------

sectorindic(c,'VA','Real',tcur(t),sim)       = ( SUM(a, theta(a,c,t)*PVA00(a)*QVA.L(a,t)) 
  + SUM((f,a)$fleo(f), theta(a,c,t)*WF00(f)*WFDIST00(f,a)*(1+TFACT.L(f,a,t))*QF.L(f,a,t)) ) * scaling('samsolrep');
sectorindic('total','VA','Real',tcur(t),sim) = SUM(c, sectorindic(c,'VA','Real',t,sim));

sectorindic(a,'EMP','Real',tcur(t),sim)       = QFX('tot-lab',a,t,sim)*scaling('qlabsolrep');
sectorindic('total','EMP','Real',tcur(t),sim) = SUM(a, sectorindic(a,'EMP','Real',t,sim));

sectorindic(c,'EXP','Real',tcur(t),sim)       = SUM(r, PWER00(c,r)*EXR00*QER.L(c,r,t))*scaling('samsolrep');
sectorindic('total','EXP','Real',tcur(t),sim) = SUM(c, sectorindic(c,'EXP','Real',t,sim));

sectorindic(c,'IMP','Real',tcur(t),sim)       = SUM(r, PWMR00(c,r)*EXR00*QMR.L(c,r,t))*scaling('samsolrep');
sectorindic('total','IMP','Real',tcur(t),sim) = SUM(c, sectorindic(c,'IMP','Real',t,sim));

sectorindic(c,'DOM','Real',tcur(t),sim)       = PDS00(c)*QD.L(c,t)*scaling('samsolrep');
sectorindic('total','DOM','Real',tcur(t),sim) = SUM(c, sectorindic(c,'DOM','Real',t,sim));

sectorindic(c,'Output','Real',tcur(t),sim)       = SUM(a, theta(a,c,t)*PA00(a)*QAX(a,t,sim))*scaling('samsolrep');
sectorindic('total','Output','Real',tcur(t),sim) = SUM(c, sectorindic(c,'Output','Real',t,sim));

sectorindic(c,'VAshr','Real',tcur(t),sim) = 100*sectorindic(c,'VA','Real',t,sim)/SUM(cp, sectorindic(cp,'VA','Real',t,sim));


* end: sectorindic --------------------------------------------------



* start: sectorindic2 -----------------------------------------------

sectorindic2(acrep,sectorcol3,kgdp,tcur(t),sim) = SUM(ac$macacrep(ac,acrep), sectorindic(ac,sectorcol3,kgdp,t,sim));

* end: sectorindic2 -------------------------------------------------


* start: ssecindic --------------------------------------------------

ssecindic('IncCurSSEC-FacActTax','Nominal',tcur(t),sim) = SUM((insssec,taxfact), SIMSAM(insssec,taxfact,t,sim));
ssecindic('IncCurSSEC-FacInc','Nominal',tcur(t),sim) = SUM((insssec,f), SIMSAM(insssec,f,t,sim));
ssecindic('IncCurSSEC-TrnsfrGov','Nominal',tcur(t),sim) = SUM((insssec,insgov), SIMSAM(insssec,insgov,t,sim));
ssecindic('IncCurSSEC-TrnsfrDomNGov','Nominal',tcur(t),sim) = SUM((insssec,insdng), SIMSAM(insssec,insdng,t,sim));
ssecindic('IncCurSSEC-TrnsfrRow','Nominal',tcur(t),sim) = SUM((insssec,insrow), SIMSAM(insssec,insrow,t,sim));
 
ssecindic('IncCurSSEC-Total','Nominal',tcur(t),sim) = SUM(sseccol1, ssecindic(sseccol1,'Nominal',t,sim));

ssecindic('SpndCurSSEC-Con','Nominal',tcur(t),sim) = SUM((c,insssec), SIMSAM(c,insssec,t,sim));
ssecindic('SpndCurSSEC-TrnsfrGov','Nominal',tcur(t),sim) = SUM((insgov,insssec), SIMSAM(insgov,insssec,t,sim));
ssecindic('SpndCurSSEC-TrnsfrDomNGov','Nominal',tcur(t),sim) = SUM((insdng,insssec), SIMSAM(insdng,insssec,t,sim));
ssecindic('SpndCurSSEC-TrnsfrRow','Nominal',tcur(t),sim) = SUM((insrow,insssec), SIMSAM(insrow,insssec,t,sim));

ssecindic('SpndCurSSEC-Total','Nominal',tcur(t),sim) = SUM(sseccol2, ssecindic(sseccol2,'Nominal',t,sim));

ssecindic('SavSSEC','Nominal',tcur(t),sim)      = SUM((capinsdng,insssec), SIMSAM(capinsdng,insssec,t,sim));

ssecindic(sseccol,'GDPshr',tcur(t),sim)$gdpindic('gdpmp','Nominal',t,sim) = 
  100 * ssecindic(sseccol,'Nominal',t,sim) / gdpindic('gdpmp','Nominal',t,sim);


* check balance in ssecindic
errssecindic1(tcur(t),sim)$
  (ABS(ssecindic('SavSSEC','Nominal',t,sim)/scaling('samsolrep') 
     - ssecindic('IncCurSSEC-Total','Nominal',t,sim)/scaling('samsolrep')
     + ssecindic('SpndCurSSEC-Total','Nominal',t,sim)/scaling('samsolrep')) >1e-4)=1/0;
DISPLAY errssecindic1;




* end: ssecindic ----------------------------------------------------



* start: welfare ----------------------------------------------------


* ev
ev(h,tcur(t),sim) = PROD(c$(PQDX(c,h,t,sim)>0 AND csam(c)), (PQD0(c,h,t)/PQD.L(c,h,t))**delta_les(c,h)) * ( EH.L(h,t) - SUM(c, qhmin(c,h,t)*PQD.L(c,h,t)) )
  - ( EH0(h,t) - SUM(c, qhmin(c,h,t)*PQD0(c,h,t)) );

ev(h,tcur(t),sim) = ev(h,t,sim)*scaling('samsolrep');

* aggregate by assuming utilitarian social welfare function
ev('total',tcur(t),sim) = SUM(h, ev(h,t,sim));

* cv

cv(h,tcur(t),sim) = ( EH.L(h,t) - SUM(c, qhmin(c,h,t)*PQD.L(c,h,t)) )
  - PROD(c$(PQDX(c,h,t,sim)>0 AND PQD00(c,h) AND csam(c)), (PQD.L(c,h,t)/PQD0(c,h,t))**delta_les(c,h)) * ( EH0(h,t) - SUM(c, qhmin(c,h,t)*PQD0(c,h,t)) );
  
cv(h,tcur(t),sim) = cv(h,t,sim)*scaling('samsolrep');

* aggregate by assuming utilitarian social welfare function
cv('total',tcur(t),sim) = SUM(h, cv(h,t,sim));
  

  
* end: welfare ------------------------------------------------------



* start: macrosam ---------------------------------------------------

* fila tax2
MACROSAM('tax2','act2',tcur(t),sim) = SUM((actax,a), SIMSAM(actax,a,t,sim));
MACROSAM('tax2','com2',tcur(t),sim) = SUM((actax,c), SIMSAM(actax,c,t,sim)) + SUM((subcom,c), SIMSAM(subcom,c,t,sim));
MACROSAM('tax2','fac2',tcur(t),sim) = SUM((actax,f), SIMSAM(actax,f,t,sim));
MACROSAM('tax2','hhd2',tcur(t),sim) = SUM((actax,insdng), SIMSAM(actax,insdng,t,sim));

* filas com2
MACROSAM('com2','act2',tcur(t),sim) = SUM((c,a), SIMSAM(c,a,t,sim));
MACROSAM('com2','hhd2',tcur(t),sim) = SUM((c,insdng), SIMSAM(c,insdng,t,sim));
MACROSAM('com2','gov2',tcur(t),sim) = SUM((c,insgov), SIMSAM(c,insgov,t,sim)) + SUM((c,insssec), SIMSAM(c,insssec,t,sim));
MACROSAM('com2','s-i2',tcur(t),sim) = SUM((c,invng), SIMSAM(c,invng,t,sim)) + SUM((c,invg), SIMSAM(c,invg,t,sim)) + SUM((c,dstk), SIMSAM(c,dstk,t,sim));
MACROSAM('com2','row2',tcur(t),sim) = SUM((c,r), SIMSAM(c,r,t,sim));

* filas fac2
MACROSAM('fac2','act2',tcur(t),sim) = SUM((f,a), SIMSAM(f,a,t,sim));
MACROSAM('fac2','row2',tcur(t),sim) = SUM(f, SIMSAM(f,'row',t,sim));

* filas act2
MACROSAM('act2','com2',tcur(t),sim) = SUM((a,c), SIMSAM(a,c,t,sim));

* filas gov2
MACROSAM('gov2','tax2',tcur(t),sim) = SUM(actax, SIMSAM('gov',actax,t,sim)) + SUM(subcom, SIMSAM('gov',subcom,t,sim));
MACROSAM('gov2','hhd2',tcur(t),sim) = SUM(insdng, SIMSAM('gov',insdng,t,sim));
MACROSAM('gov2','row2',tcur(t),sim) = SIMSAM('gov','row',t,sim);
MACROSAM('gov2','fac2',tcur(t),sim) = SUM(f, SIMSAM('gov',f,t,sim));

* filas hhd2
MACROSAM('hhd2','fac2',tcur(t),sim) = SUM((insdng,f), SIMSAM(insdng,f,t,sim));
MACROSAM('hhd2','gov2',tcur(t),sim) = SUM(insdng, SIMSAM(insdng,'gov',t,sim));
MACROSAM('hhd2','row2',tcur(t),sim) = SUM(insdng, SIMSAM(insdng,'row',t,sim));

* filas row2
MACROSAM('row2','hhd2',tcur(t),sim) = SUM(insdng, SIMSAM('row',insdng,t,sim));
MACROSAM('row2','gov2',tcur(t),sim) = SIMSAM('row','gov',t,sim);
MACROSAM('row2','fac2',tcur(t),sim) = SUM(f, SIMSAM('row',f,t,sim));
MACROSAM('row2','com2',tcur(t),sim) = SUM((c,r), SIMSAM(r,c,t,sim));

* filas s-i2
MACROSAM('s-i2','hhd2',tcur(t),sim) = SUM((capinsdng,insdng), SIMSAM(capinsdng,insdng,t,sim));
MACROSAM('s-i2','gov2',tcur(t),sim) = SUM((capinsgov,insgov), SIMSAM(capinsgov,insgov,t,sim)) + SUM((capinsgov,insssec), SIMSAM(capinsgov,insssec,t,sim));
MACROSAM('s-i2','row2',tcur(t),sim) = SUM((capinsrow,insrow), SIMSAM(capinsrow,insrow,t,sim));

* totales macrosam
MACROSAM('total2',ac2ntp,tcur(t),sim) = SUM(ac2nt,  MACROSAM(ac2nt,ac2ntp,t,sim));
MACROSAM(ac2nt,'total2',tcur(t),sim)   = SUM(ac2ntp, MACROSAM(ac2nt,ac2ntp,t,sim));

* end: macrosam -----------------------------------------------------

macrosambalchk(ac2nt,tcur(t),sim) = MACROSAM(ac2nt,'total2',t,sim) - MACROSAM('total2',ac2nt,t,sim);

* start: macrosamF --------------------------------------------------

MACROSAMF(ac2,ac2p,tcur(t),sim) = MACROSAM(ac2,ac2p,t,sim);
MACROSAMF('fac2',ac2,tcur(t),sim) = 0;
MACROSAMF(ac2,'fac2',tcur(t),sim) = 0;
* fac in rows
MACROSAMF('lab2','act2',tcur(t),sim) = SUM((flab,a), SIMSAM(flab,a,t,sim));
MACROSAMF('cap2','act2',tcur(t),sim) = SUM((fnlab,a), SIMSAM(fnlab,a,t,sim));
MACROSAMF('lab2','row2',tcur(t),sim) = SUM((flab,a), SIMSAM(flab,'row',t,sim));
MACROSAMF('cap2','row2',tcur(t),sim) = SUM((fnlab,a), SIMSAM(fnlab,'row',t,sim));
* fac in cols
MACROSAMF('hhd2','lab2',tcur(t),sim) = SUM((insdng,flab), SIMSAM(insdng,flab,t,sim));
MACROSAMF('hhd2','cap2',tcur(t),sim) = SUM((insdng,fnlab), SIMSAM(insdng,fnlab,t,sim));
MACROSAMF('gov2','cap2',tcur(t),sim) = SUM(fnlab, SIMSAM('gov',fnlab,t,sim));
MACROSAMF('row2','lab2',tcur(t),sim) = SUM(flab, SIMSAM('row',flab,t,sim));
MACROSAMF('row2','cap2',tcur(t),sim) = SUM(fnlab, SIMSAM('row',fnlab,t,sim));

macrosamfbalchk(ac2nt,tcur(t),sim) = MACROSAMF(ac2nt,'total2',t,sim) - MACROSAMF('total2',ac2nt,t,sim);


* end: macrosamF ----------------------------------------------------



* start: actvashr ---------------------------------------------------

*actvashr('Nominal',a,tcur(t),sim) = 100 * SUM(fp, SIMSAM(fp,a,t,sim)) / SUM(ap, SUM(fp, SIMSAM(fp,ap,t,sim)));
*actvashr('Real',a,tcur(t),sim)    = 100 * SUM(f, WF00(f)*WFDIST00(f,a)*QF.L(f,a,t)) / SUM(ap, SUM(f, WF00(f)*WFDIST00(f,ap)*QF.L(f,ap,t)));

actvashr('Nominal',a,tcur(t),sim) = 100 * [ PVA.L(a,t)*QVA.L(a,t) 
  - SUM(cen, PQD.L(cen,a,t)*pqddist(cen,a)*QINT.L(cen,a,t))
  - SUM(cwat, PQD.L(cwat,a,t)*pqddist(cwat,a)*QINT.L(cwat,a,t)) ]
/ [ SUM(ap, PVA.L(ap,t)*QVA.L(ap,t)
  - SUM(cen, PQD.L(cen,ap,t)*pqddist(cen,ap)*QINT.L(cen,ap,t))
  - SUM(cwat, PQD.L(cwat,ap,t)*pqddist(cwat,ap)*QINT.L(cwat,ap,t))) ];


actvashr('Real',a,tcur(t),sim) = 100 * [ PVA00(a)*QVA.L(a,t) 
  - SUM(cen, PQD00(cen,a)*pqddist(cen,a)*QINT.L(cen,a,t))
  - SUM(cwat, PQD00(cwat,a)*pqddist(cwat,a)*QINT.L(cwat,a,t)) ]
/ [ SUM(ap, PVA00(ap)*QVA.L(ap,t)
  - SUM(cen, PQD00(cen,ap)*pqddist(cen,ap)*QINT.L(cen,ap,t))
  - SUM(cwat, PQD00(cwat,ap)*pqddist(cwat,ap)*QINT.L(cwat,ap,t))) ];
 
actvashr(kgdp,'total',tcur(t),sim) = SUM(a, actvashr(kgdp,a,t,sim));


* end: actvashr -----------------------------------------------------

* start: actvashr2

actvashr2(kgdp,acrep,tcur(t),sim) = SUM(ac$macacrep(ac,acrep), actvashr(kgdp,ac,t,sim));

* end: actvashr2

* start: actashr ---------------------------------------------------

actashr('Nominal',a,tcur(t),sim) = 100 * SIMSAM('total',a,t,sim) / SUM(ap, SIMSAM('total',ap,t,sim));
actashr('Real',a,tcur(t),sim)    = 100 * PA00(a)*QA.L(a,t) / SUM(ap, PA00(ap)*QA.L(ap,t));

actashr(kgdp,'total',tcur(t),sim) = SUM(a, actvashr(kgdp,a,t,sim));


* end: actashr -----------------------------------------------------

* start: actashr2

actashr2(kgdp,acrep,tcur(t),sim) = SUM(ac$macacrep(ac,acrep), actashr(kgdp,ac,t,sim));

* end: actashr2


* start: facdemstruc ------------------------------------------------

facdemstruc(f,a,tcur(t),sim)$SUM(fp, SIMSAM(fp,a,t,sim)) =
  100 * SIMSAM(f,a,t,sim) / SUM(fp, SIMSAM(fp,a,t,sim));

facdemstruc(f,'total',tcur(t),sim) = 100 * SUM(a, SIMSAM(f,a,t,sim)) / SUM(a, SUM(fp, SIMSAM(fp,a,t,sim)));

facdemstruc('total',ac,tcur(t),sim) = SUM(f, facdemstruc(f,ac,t,sim));

* end: facdemstruc --------------------------------------------------


* start: facdemstruc2 -----------------------------------------------


facdemstruc2(acrepp,acrep,tcur(t),sim)$SUM(fp, SUM(a$macacrep(a,acrep), SIMSAM(fp,a,t,sim))) =
  100 * SUM(f$macacrep(f,acrepp), SUM(a$macacrep(a,acrep), SIMSAM(f,a,t,sim))) / SUM(f, SUM(a$macacrep(a,acrep), SIMSAM(f,a,t,sim)));

facdemstruc2(acrepp,'total3',tcur(t),sim) = SUM(f$macacrep(f,acrepp), facdemstruc(f,'total',t,sim));

facdemstruc2('total3',acrep,tcur(t),sim) = SUM(acrepp, facdemstruc2(acrepp,acrep,t,sim));



* end: facdemstruc2 -------------------------------------------------




* start: tradindic --------------------------------------------------

* expos
tradindic(c,r,'EXPvol',tcur(t),sim)       = PWER00(c,r)*EXR00*QER.L(c,r,t);
tradindic('total',r,'EXPvol',tcur(t),sim) = SUM(c, tradindic(c,r,'EXPvol',t,sim));
tradindic(ac,'total','EXPvol',tcur(t),sim) = SUM(r, tradindic(ac,r,'EXPvol',t,sim));

* impos
tradindic(c,r,'IMPvol',tcur(t),sim)       = PWMR00(c,r)*EXR00*QMR.L(c,r,t);
tradindic('total',r,'IMPvol',tcur(t),sim) = SUM(c, tradindic(c,r,'IMPvol',t,sim));
tradindic(ac,'total','IMPvol',tcur(t),sim) = SUM(r, tradindic(ac,r,'IMPvol',t,sim));

* rescale
tradindic(ac,acp,'EXPvol',tcur(t),sim) = tradindic(ac,acp,'EXPvol',t,sim)*scaling('samsolrep');
tradindic(ac,acp,'IMPvol',tcur(t),sim) = tradindic(ac,acp,'IMPvol',t,sim)*scaling('samsolrep');

* trade shares; participaciones socios comerciales
tradindic(acp,ac,'exp-cntry-shr',tcur(t),sim)$tradindic(acp,'total','EXPvol',t,sim) =
  100 * tradindic(acp,ac,'EXPvol',t,sim) / tradindic(acp,'total','EXPvol',t,sim);

tradindic(acp,ac,'imp-cntry-shr',tcur(t),sim)$tradindic(acp,'total','IMPvol',t,sim) =
  100 * tradindic(acp,ac,'IMPvol',t,sim) / tradindic(acp,'total','IMPvol',t,sim);

* trade shares; participaciones socios comerciales
tradindic(ac,acp,'exp-com-shr',tcur(t),sim)$tradindic('total',acp,'EXPvol',t,sim) =
  100 * tradindic(ac,acp,'EXPvol',t,sim) / tradindic('total',acp,'EXPvol',t,sim);

tradindic(ac,acp,'imp-com-shr',tcur(t),sim)$tradindic('total',acp,'IMPvol',t,sim) =
  100 * tradindic(ac,acp,'IMPvol',t,sim) / tradindic('total',acp,'IMPvol',t,sim);
  
* aranceles
tradindic(c,r,'t-imp',t,sim)       = TM.L(c,r,t)*100;
tradindic(c,r,'t-imp-socio',t,sim) = tstar(c,r,t)*100;


* end: tradindic ----------------------------------------------------

* start: tradindic2 -------------------------------------------------

tradindic2(acrep,acrepp,'EXPvol',tcur(t),sim) = SUM(ac$macacrep(ac,acrep), SUM(acp$macacrep(acp,acrepp), tradindic(ac,acp,'EXPvol',t,sim)));
tradindic2(acrep,acrepp,'IMPvol',tcur(t),sim) = SUM(ac$macacrep(ac,acrep), SUM(acp$macacrep(acp,acrepp), tradindic(ac,acp,'IMPvol',t,sim)));

tradindic2(acrep,acrepp,'exp-cntry-shr',tcur(t),sim)$tradindic2(acrep,'total3','EXPvol',t,sim) =  
  100 * tradindic2(acrep,acrepp,'EXPvol',t,sim) / tradindic2(acrep,'total3','EXPvol',t,sim);

tradindic2(acrep,acrepp,'imp-cntry-shr',tcur(t),sim)$tradindic2(acrep,'total3','IMPvol',t,sim) =  
  100 * tradindic2(acrep,acrepp,'IMPvol',t,sim) / tradindic2(acrep,'total3','IMPvol',t,sim);

tradindic2(acrep,acrepp,'exp-com-shr',tcur(t),sim)$tradindic2('total3',acrepp,'EXPvol',t,sim) =  
  100 * tradindic2(acrep,acrepp,'EXPvol',t,sim) / tradindic2('total3',acrepp,'EXPvol',t,sim);

tradindic2(acrep,acrepp,'imp-com-shr',tcur(t),sim)$tradindic2('total3',acrepp,'IMPvol',t,sim) =  
  100 * tradindic2(acrep,acrepp,'IMPvol',t,sim) / tradindic2('total3',acrepp,'IMPvol',t,sim);

  
  
* end: tradindic2 ---------------------------------------------------
  
* start: tariffstruc ------------------------------------------------

tariffstruc(c,r,'t-impos',tcur(t),sim) = 1e-15 + 100*TM.L(c,r,t);
tariffstruc(c,r,'t-impos-soc',tcur(t),sim) = 1e-15 + 100*tstar(c,r,t);

* end: tariffstruc --------------------------------------------------







* start: elestruc 
elestruc(aele,'OutputVolume',tcur(t),sim) = QA.L(aele,t);
elestruc(aele,'VAnom',tcur(t),sim) = PVA.L(aele,t)*QVA.L(aele,t) - SUM(cen, PQD.L(cen,aele,t)*pqddist(cen,aele)*QINT.L(cen,aele,t));
elestruc(aele,'VAreal',tcur(t),sim) = PVA00(aele)*QVA.L(aele,t) - SUM(cen, PQD00(cen,aele)*QINT.L(cen,aele,t));
elestruc(aele,'GDPshr',tcur(t),sim) = 100*elestruc(aele,'VAnom',t,sim)/GDPMP.L(t);
elestruc(aele,'shrTOT',tcur(t),sim) = 100*elestruc(aele,'VAnom',t,sim)/SUM(aelep, elestruc(aelep,'VAnom',t,sim));
elestruc(aele,'SupPrice',tcur(t),sim) = PA.L(aele,t);
elestruc('total',elecol,tcur(t),sim) = SUM(aele, elestruc(aele,elecol,t,sim));
elestruc('total','SupPrice',tcur(t),sim) = 0;

* end: elestruc  

* start: demstruc
    
demstruc('Nominal',c,'IntermCon',tcur(t),sim) = SUM(a, SIMSAM(c,a,t,sim));
demstruc('Nominal',c,'DistMarg',tcur(t),sim) = SUM(tacd, SIMSAM(c,tacd,t,sim)) + SUM(tacm, SIMSAM(c,tacm,t,sim)) + SUM(tace, SIMSAM(c,tace,t,sim));
demstruc('Nominal',c,'PrvCon',tcur(t),sim) = SUM(h, SIMSAM(c,h,t,sim));  
demstruc('Nominal',c,'GovCon',tcur(t),sim) = SUM(insgov, SIMSAM(c,insgov,t,sim)) + SUM(insssec, SIMSAM(c,insssec,t,sim));  
demstruc('Nominal',c,'FixInv',tcur(t),sim) = SUM(inv, SIMSAM(c,inv,t,sim));
demstruc('Nominal',c,'StockChange',tcur(t),sim) = SUM(dstk, SIMSAM(c,dstk,t,sim));
demstruc('Nominal',c,'Exports',tcur(t),sim) = SUM(r, SIMSAM(c,r,t,sim)) + SUM(instrst, SIMSAM(c,instrst,t,sim));
demstruc('Nominal',c,'TotDem',tcur(t),sim) = SUM(acnt, SIMSAM(c,acnt,t,sim));
demstruc('Nominal','total',demcol,tcur(t),sim) = SUM(c, demstruc('Nominal',c,demcol,t,sim));

demstruc('shrTOT',ac,demcol,tcur(t),sim)$demstruc('Nominal',ac,'TotDem',t,sim) = 100*demstruc('Nominal',ac,demcol,t,sim)/demstruc('Nominal',ac,'TotDem',t,sim);
demstruc('GDPshr',ac,demcol,tcur(t),sim) = 100*demstruc('Nominal',ac,demcol,t,sim)/gdpindic('gdpmp','Nominal',t,sim);


  
* end: demstruc  

* start: demstruc2

demstruc2('Nominal',acrep,'IntermCon',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(a, SIMSAM(c,a,t,sim)));
demstruc2('Nominal',acrep,'DistMarg',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(tacd, SIMSAM(c,tacd,t,sim)) + SUM(tacm, SIMSAM(c,tacm,t,sim)) + SUM(tace, SIMSAM(c,tace,t,sim)));
demstruc2('Nominal',acrep,'PrvCon',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(h, SIMSAM(c,h,t,sim)));  
demstruc2('Nominal',acrep,'GovCon',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(insgov, SIMSAM(c,insgov,t,sim)) + SUM(insssec, SIMSAM(c,insssec,t,sim)));  
demstruc2('Nominal',acrep,'FixInv',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(inv, SIMSAM(c,inv,t,sim)));
demstruc2('Nominal',acrep,'StockChange',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(dstk, SIMSAM(c,dstk,t,sim)));
demstruc2('Nominal',acrep,'Exports',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(r, SIMSAM(c,r,t,sim))) 
  + SUM(c$macacrep(c,acrep), SUM(instrst, SIMSAM(c,instrst,t,sim)));
demstruc2('Nominal',acrep,'TotDem',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(acnt, SIMSAM(c,acnt,t,sim)));
demstruc2('Nominal','total3',demcol,tcur(t),sim) = SUM(acrep, demstruc2('Nominal',acrep,demcol,t,sim));

demstruc2('shrTOT',acrep,demcol,tcur(t),sim)$demstruc2('Nominal',acrep,'TotDem',t,sim) = 100*demstruc2('Nominal',acrep,demcol,t,sim)/demstruc2('Nominal',acrep,'TotDem',t,sim);
demstruc2('GDPshr',acrep,demcol,tcur(t),sim) = 100*demstruc2('Nominal',acrep,demcol,t,sim)/gdpindic('gdpmp','Nominal',t,sim);

* end: demstruc2

* start: demstrucdet
    
demstrucdet('Nominal',c,acnt,tcur(t),sim) = SIMSAM(c,acnt,t,sim);
demstrucdet('Nominal',c,'total',tcur(t),sim) = SUM(acnt, SIMSAM(c,acnt,t,sim));

demstrucdet('shrTOT',ac,acnt,tcur(t),sim)$demstrucdet('Nominal',ac,'total',t,sim) = 
  100*demstrucdet('Nominal',ac,acnt,t,sim)/demstrucdet('Nominal',ac,'total',t,sim);
demstrucdet('GDPshr',ac,acnt,tcur(t),sim) = 100*demstrucdet('Nominal',ac,acnt,t,sim)/gdpindic('gdpmp','Nominal',t,sim);


  
* end: demstrucdet



* start: labordemstruc

labordemstruc(f,a,tcur(t),sim)$(fva(f) AND flab(f) AND SUM(fp$(fva(fp) AND flab(fp)), QF.L(fp,a,t))) = 
  100*QF.L(f,a,t)/SUM(fp$(fva(fp) AND flab(fp)), QF.L(fp,a,t));

* total activities excluding leisure
labordemstruc(f,'total',tcur(t),sim)$(fva(f) AND flab(f))
  = 100*SUM(a, QF.L(f,a,t))/SUM(fp$(fva(fp) AND flab(fp)), SUM(a, QF.L(fp,a,t)));
labordemstruc('total',ac,tcur(t),sim) = SUM(flab, labordemstruc(flab,ac,t,sim));

* end: labordemstruc


* start: labordemstruc2

labordemstruc2(acrep,acrepp,tcur(t),sim) = 
  SUM(f$(fva(f) AND flab(f) AND macacrep(f,acrep)), SUM(a$macacrep(a,acrepp), QF.L(f,a,t)));
labordemstruc2('total3',acrepp,tcur(t),sim) = SUM(acrep, labordemstruc2(acrep,acrepp,t,sim));

labordemstruc2(acrep,'total3',tcur(t),sim) = SUM(acrepp, labordemstruc2(acrep,acrepp,t,sim));

labordemstruc2(acrep,acrepp,tcur(t),sim)$labordemstruc2('total3',acrepp,t,sim) = 
  100*labordemstruc2(acrep,acrepp,t,sim)/labordemstruc2('total3',acrepp,t,sim);


* end: labordemstruc2




* start: employsectorstruc

employsectorstruc(f,a,tcur(t),sim)$(fva(f) AND flab(f)) = 100*QF.L(f,a,t)/SUM(ap, QF.L(f,ap,t));
employsectorstruc('total',a,tcur(t),sim) 
  = 100*SUM(f$(fva(f) AND flab(f)), QF.L(f,a,t)) / SUM(f$(fva(f) AND flab(f)), SUM(ap, QF.L(f,ap,t)));

employsectorstruc(ac,'total',tcur(t),sim) = SUM(a, employsectorstruc(ac,a,t,sim));

* end: employsectorstruc



* start: employsectorstruc2

employsectorstruc2(acrep,acrepp,tcur(t),sim) =
  SUM(ac$macacrep(ac,acrep), SUM(acp$macacrep(acp,acrepp), employsectorstruc(ac,acp,t,sim)));
;

* end: employsectorstruc2





* start: incomestruc

incomestruc('INCval',ins,f,tcur(t),sim) = SIMSAM(ins,f,t,sim);
incomestruc('INCval',ins,insp,tcur(t),sim) = SIMSAM(ins,insp,t,sim);
incomestruc('INCval',insgov,'tax-tot',tcur(t),sim) = 
  SUM(actax, SIMSAM(insgov,actax,t,sim))
  + SIMSAM(insgov,'impl-taxwat',t,sim);

incomestruc('INCval',insssec,'tax-tot',tcur(t),sim) = SUM(taxfact, SIMSAM(insssec,taxfact,t,sim));
    
incomestruc('INCval',ins,'regul-tot',tcur(t),sim) = 
  SUM(regact, SIMSAM(ins,regact,t,sim)) + SUM(regcom, SIMSAM(ins,regcom,t,sim));    

incomestruc('INCval',ins,'phantom-taxcom',tcur(t),sim) = 
  SIMSAM(ins,'phantom-taxcom',t,sim);
  
incomestruc('INCval',ins,'tax-use',tcur(t),sim) = 
  SIMSAM(ins,'tax-use',t,sim);  
  
incomestruc('INCval',insrow,'imports2',tcur(t),sim) = SUM(c, SIMSAM(insrow,c,t,sim));
incomestruc('INCval',insrow,'tradbal',tcur(t),sim) = SUM(r, SIMSAM(insrow,r,t,sim));

    
incomestruc('INCval',ins,'total',tcur(t),sim) = SUM(acnt, incomestruc('INCval',ins,acnt,t,sim));


incomestruc('INCval',ins,'dummy',tcur(t),sim) = SIMSAM(ins,'total',t,sim);



* en caso del gobierno, deben restarse los subsidios de los ingresos, por como aparecen en SAM
incomestruc('INCval',insgov,'dummy',tcur(t),sim) = incomestruc('INCval',insgov,'dummy',t,sim) - SUM(subcom, SIMSAM(insgov,subcom,t,sim));

errincomestruc(ins,tcur(t),sim) = incomestruc('INCval',ins,'dummy',t,sim) - incomestruc('INCval',ins,'total',t,sim);
errincomestruc(ins,tcur(t),sim)$(ABS(incomestruc('INCval',ins,'dummy',t,sim) - incomestruc('INCval',ins,'total',t,sim))>1e-6) = 1/0;
DISPLAY errincomestruc;



incomestruc('INCval',ins,'dummy',tcur(t),sim) = 0;

incomestruc('INCshr',ins,ac,tcur(t),sim) = 100*incomestruc('INCval',ins,ac,t,sim)/SUM(acnt, incomestruc('INCval',ins,acnt,t,sim));



* end: incomestruc    


* start: incomestruc2

incomestruc2('INCval',acrep,acrepp,tcur(t),sim) = SUM(ac$macacrep(ac,acrep), SUM(acp$macacrep(acp,acrepp), incomestruc('INCval',ac,acp,t,sim)));  
incomestruc2('INCshr',acrep,acrepp,tcur(t),sim)$incomestruc2('INCval',acrep,'total3',t,sim) = 
  100*incomestruc2('INCval',acrep,acrepp,t,sim) / incomestruc2('INCval',acrep,'total3',t,sim);

* end: incomestruc2 


* start: incomedistindic --------------------------------------------


incomedistindic('POPshr',h,tcur(t),sim) = 100*pop(h,t)/SUM(hp, pop(hp,t));
incomedistindic('INCshr',h,tcur(t),sim) = 100*SIMSAM(h,'total',t,sim)/SUM(hp, SIMSAM(hp,'total',t,sim));
incomedistindic('CONshr',h,tcur(t),sim) = 100*SUM(c, SIMSAM(c,h,t,sim))/SUM((cp,hp), SIMSAM(cp,hp,t,sim));
incomedistindic(inccol,'total',tcur(t),sim) = SUM(h, incomedistindic(inccol,h,t,sim));
incomedistindic('INCpc',ac,tcur(t),sim)  = yhpcX(ac,t,sim);
incomedistindic('CONpc',ac,tcur(t),sim)  = qhpc2X(ac,t,sim);  
  
  
  
* end: incomedistindic ----------------------------------------------


* start: hhdconindic

* same units as SAM
hhdconindic(c,h,'Real',tcur(t),sim) = PQD00(c,h)*QH.L(c,h,t)*scaling('samsolrep');
hhdconindic(c,'total','Real',tcur(t),sim) = SUM(h, PQD00(c,h)*QH.L(c,h,t))*scaling('samsolrep');
hhdconindic('total',ac,'Real',tcur(t),sim) = SUM(c, hhdconindic(c,ac,'Real',t,sim));

* real LCU per capita
hhdconindic(ac,acp,'RealPC',tcur(t),sim)$hhdconindic(ac,acp,'Real',t,sim) = 
  hhdconindic(ac,acp,'Real',t,sim)*scaling('samTo1')/(popX(acp,t,sim)*scaling('qlabsol')*scaling('qlabTo1'));
  

* end: hhdconindic


* same units as SAM
hhdconindic2(acrep,acrepp,'Real',tcur(t),sim) = SUM(c$macacrep(c,acrep), SUM(h$macacrep(h,acrepp), hhdconindic(c,h,'Real',t,sim)));

* real LCU per capita




* start: debtindic

debtindic('NonGov-ForDebt-GDPshr',tcur(t),sim) = 100*FDEBT.L('ngovz',t)*EXR.L(t)/GDPMP.L(t);
debtindic('Gov-ForDebt-GDPshr',tcur(t),sim) = 100*FDEBT.L('govz',t)*EXR.L(t)/GDPMP.L(t);
debtindic('Gov-DomDebt-GDPshr',tcur(t),sim) = 100*GDEBT.L(t)*EXR.L(t)/GDPMP.L(t);
  
* end: debtindic  


* start: spndstruc

spndstruc('SPNDval',ac,ins,tcur(t),sim) = SIMSAM(ac,ins,t,sim);
spndstruc('SPNDshr',ac,ins,tcur(t),sim) = spndstruc('SPNDval',ac,ins,t,sim)/spndstruc('SPNDval','total',ins,t,sim)*100;

* end: spndstruc
  
  
* start: coststruc

coststruc(ac,a,tcur(t),sim) = SIMSAM(ac,a,t,sim)/SIMSAM('total',a,t,sim)*100;

* end: coststruc


* start: trstindic

trstindic('Real',c,instrst,tcur(t),sim) = PQD00(c,instrst)*QTRSTF.L(c,instrst,t);
trstindic('SPNDshr',c,instrst,tcur(t),sim) = SIMSAM(c,instrst,t,sim)/SUM(cp, SIMSAM(cp,instrst,t,sim)) * 100;
trstindic(kgdp,'total',instrst,tcur(t),sim) = SUM(c, trstindic(kgdp,c,instrst,t,sim));

* end: trstindic

* start: trstindic2

trstindic2(kgdp,acrep,instrst,tcur(t),sim) = SUM(ac$macacrep(ac,acrep), trstindic(kgdp,ac,instrst,t,sim));


* end: trstindic2


* start: emiindic

emiindic('EmiVol',ac,tcur(t),sim) = EMIX('total','total',ac,t,sim)*scaling('emisolrep');
emiindic('EmiSHR-ByUser',ac,tcur(t),sim)$SUM(acnt, emiindic('EmiVol',acnt,t,sim)) = 
  100*emiindic('EmiVol',ac,t,sim)/SUM(acnt, emiindic('EmiVol',acnt,t,sim));

* end: emiindic

* start: emiindic2

emiindic2('EmiVol',acrep,tcur(t),sim) = SUM(acp$macacrep(acp,acrep), emiindic('EmiVol',acp,t,sim));
emiindic2('EmiSHR-ByUser',acrep,tcur(t),sim) = SUM(acp$macacrep(acp,acrep), emiindic('EmiSHR-ByUser',acp,t,sim));

* end: emiindic2

* start: emicindic

emicindic('EmiVol',ac,acp,tcur(t),sim) = EMIX('total',ac,acp,t,sim)*scaling('emisolrep');

emicindic('EmiSHR-ByUser',ac,acp,tcur(t),sim)$SUM(acnt, emicindic('EmiVol',ac,acnt,t,sim)) = 
  100*emicindic('EmiVol',ac,acp,t,sim)/SUM(acnt, emicindic('EmiVol',ac,acnt,t,sim));

emicindic('EmiSHR-ByCommodity',ac,acp,tcur(t),sim)$SUM(acnt, emicindic('EmiVol',acnt,acp,t,sim)) = 
  100*emicindic('EmiVol',ac,acp,t,sim)/SUM(acnt, emicindic('EmiVol',acnt,acp,t,sim));


* end: emicindic

* start: emicindic2

emicindic2(emicol,ac,acrep,tcur(t),sim) = SUM(acp$macacrep(acp,acrep), emicindic(emicol,ac,acp,t,sim));

emicindic2('EmiSHR-ByCommodity',ac,acrep,tcur(t),sim)$SUM(acnt, emicindic2('EmiVol',acnt,acrep,t,sim)) = 
  100*emicindic2('EmiVol',ac,acrep,t,sim)/SUM(acnt, emicindic2('EmiVol',acnt,acrep,t,sim));


* end: emicindic2


emisourcesX('ENER',t,sim) = EMIX('CO2','total','total',t,sim)*scaling('emisolrep');
emisourcesX('AFOLU',t,sim) = EMIX('CO2eq','AFOLU','total',t,sim)*scaling('emisolrep');
emisourcesX('LULC',t,sim) = EMIX('CO2eq','LULC','total',t,sim)*scaling('emisolrep');
emisourcesX('total',t,sim) = EMIX('total','total','total',t,sim)*scaling('emisolrep');

* start: landuseindic -----------------------------------------------


landuseindic('VOL',lnd,tcur(t),sim) = QLAND.L(lnd,t)*scaling('qlandsolrep');
landuseindic('VOL',lndforplant,tcur(t),sim) = landuseindic('VOL',lndforplant,t,sim) + SUM((ffor2,a), QF.L(ffor2,a,t))*scaling('qlandsolrep');
landuseindic('VOL',lndfortot,tcur(t),sim) = landuseindic('VOL',lndfortot,t,sim) + SUM((ffor2,a), QF.L(ffor2,a,t))*scaling('qlandsolrep');


* end: landuseindic -------------------------------------------------

*!!
errlanduseindic(lndfortot,tcur(t),sim)$(landuseindic('VOL',lndfortot,t,sim)<0) = 1/0;
errlanduseindic(lndforplant,tcur(t),sim)$(landuseindic('VOL',lndforplant,t,sim)<0) = 1/0;
DISPLAY errlanduseindic;

* fortot NE (forplant + fornprod)
errlanduseindic2(lndfortot,tcur(t),sim)$(ABS(landuseindic('VOL',lndfortot,t,sim) - 
  (SUM(lndforplant$mlndforplantlndfortot(lndforplant,lndfortot), landuseindic('VOL',lndforplant,t,sim))
  +SUM(lndfornprod$mlndfornprodlndfortot(lndfornprod,lndfortot), landuseindic('VOL',lndfornprod,t,sim)))) > 1) = 1/0;
DISPLAY errlanduseindic2;


* start: landuseindicDD

*landuseindicDD(kgdp,lnd,tcur(t),sim) = landuseindic(kgdp,lnd,t,sim) - landuseindic(kgdp,lnd,t,'base');

* end: landuseindicDD



* start: landusetotindic --------------------------------------------

landusetotindic('VOL','fortot',tcur(t),sim) = SUM(lndfortot, landuseindic('VOL',lndfortot,t,sim));
landusetotindic('VOL','forplant',tcur(t),sim) = SUM(lndforplant, landuseindic('VOL',lndforplant,t,sim));
landusetotindic('VOL','fornprod',tcur(t),sim) = SUM(lndfornprod, landuseindic('VOL',lndfornprod,t,sim));
landusetotindic('VOL','crops',tcur(t),sim) = SUM(lndcro, landuseindic('VOL',lndcro,t,sim));
landusetotindic('VOL','livestock',tcur(t),sim) = SUM(lndliv, landuseindic('VOL',lndliv,t,sim));
landusetotindic('VOL','agrtot',tcur(t),sim) = landusetotindic('VOL','crops',t,sim) + landusetotindic('VOL','livestock',t,sim);
landusetotindic('VOL','burned',tcur(t),sim) = SUM(lndburned, landuseindic('VOL',lndburned,t,sim));

landusetotindic('VOL','total-lnd',tcur(t),sim) = 
  landusetotindic('VOL','crops',t,sim) 
  + landusetotindic('VOL','livestock',t,sim)
  + landusetotindic('VOL','forplant',t,sim)
  + landusetotindic('VOL','fornprod',t,sim)
  + landusetotindic('VOL','burned',t,sim);

* deforestation
landusetotindic('VOL','defor',tcur(t),sim) = SUM(lndfortot, QDEFOR.L(lndfortot,t))*scaling('qlandsolrep');
landusetotindic('VOL','defor-acc',tcur(t),sim) = landusetotindic('VOL','defor',t,sim) + landusetotindic('VOL','defor-acc',t-1,sim);

* net deforestation (relevant when exogenous change in total forest not due to deforestation for agr use)
* fortot(t) = fortot(t-1) - defor(t-1)
* defor(t-1) = fortot(t-1) - fortot(t)
* note the difference in the time sets
landusetotindic('VOL','defornet',tcur(t),sim) = -landusetotindic('VOL','fortot',t,sim) + landusetotindic('VOL','fortot',t-1,sim);
landusetotindic('VOL','defornet-acc',tcur(t),sim) = landusetotindic('VOL','defornet-acc',t,sim) + landusetotindic('VOL','defornet-acc',t-1,sim);



landusetotindic('shrTOT',acfor,tcur(t),sim)$(landusetotindic('VOL','fortot',t,sim) + landusetotindic('VOL','agrtot',t,sim)) = 
  100*landusetotindic('VOL',acfor,t,sim) / ( landusetotindic('VOL','fortot',t,sim) + landusetotindic('VOL','agrtot',t,sim) );



* end: landusetotindic ----------------------------------------------


* start: landusetotindicDD

landusetotindicDD(kgdp,acfor,tcur(t),sim) = landusetotindic(kgdp,acfor,t,sim) - landusetotindic(kgdp,acfor,t,'base');

* end: landusetotindicDD


* todo: delete
* start: emiconvindicX

shrconv('crops',tcur(t),sim)$(NOT tmin(t)) = landusetotindic('VOL','crops',t,sim) - landusetotindic('VOL','crops',t-1,sim);
shrconv('livestock',tcur(t),sim)$(NOT tmin(t)) = landusetotindic('VOL','livestock',t,sim) - landusetotindic('VOL','livestock',t-1,sim);
shrconv(acfor,tcur(t),sim)$(shrconv(acfor,t,sim)<0) = 0;

shrconv(acfor,tcur(t),sim)$(shrconv('crops',t,sim) + shrconv('livestock',t,sim)) = 
  shrconv(acfor,t,sim)/(shrconv('crops',t,sim) + shrconv('livestock',t,sim));


iemiconvX('total',t,sim) = SUM(acnt, shrconv(acnt,t,sim)*iemiconv(acnt));



emiconvindicX('conv',tcur(t),sim)$(NOT tmin(t) AND (landusetotindic('VOL','agrtot',t,sim) - landusetotindic('VOL','agrtot',t-1,sim) > 0)) = 
  (landusetotindic('VOL','agrtot',t,sim) - landusetotindic('VOL','agrtot',t-1,sim))*iemiconv('total');


* end: emiconvindicX




DISPLAY "#### END: reploop.inc";    
