(!**************************************************************
   Tidal power lagoon extension for use with the EMPIRE model
   ============================================================= 

   file add_tidal_lagoons.mos
   ```````````````
   Declares additional variables/parameters to includ tidal lagoon power technology  in EMPIRE. 
   Create necessary variables. 
   Updates constraints.
   Read additional data required.
   
   (c) 2016 Trine Rollefsen Nęss and Linn Emelie Schäffer
       author: T.  Rollefsen Nęss, rev. June 2016
       author: L. E. Schäffer, rev June 2016
**************************************************************!)


!Include file in EMPIRE


!GENERATORS, HOURS, SCENARIOS, YEARS are defined in EMPIRE
declarations
    PATTERNS:       set of integer;
    TIDE_GEN:       set of integer;
end-declarations    

TIDE_GEN    :=      226..227; !tidal lagoon generators included in the data sets
PATTERNS    :=      1..2; !Production patterns included


!Declarations of parameters in matrix form
declarations
    PROD_TIDE:      array(PATTERNS,HOURS,TIDE_GEN)  of real; !production patterns
    CAP:            array(TIDE_GEN) of integer;  !capacity to be installed (if installing generator)
    SCALE:          array(TIDE_GEN) of real; !scaling factor
    SWANSEA_MW:     integer; !Parameter used for scaling production
end-declarations

SWANSEA_MW  :=  350; 

initializations from INPUT_DATA_FILE
    CAP;
    SCALE; 
end-initializations



initializations from INPUT_PROD_FILE
    PROD_TIDE;
end-initializations!)

!Variables
declarations    
    is_path     :   dynamic array(SCENARIOS,YEARS,SEASONS,GENERATORS,PATTERNS) of mpvar; !Binary decision varibale for choosing production pattern
end-declarations

!create binary decision variables for choosing production pattern

forall (ss in SCENARIOS, yy in YEARS, seas in SEASONS, gg in GENERATORS, pp in PATTERNS|TECH_TIDE(GEN_TECH(gg)) and (exists(gen_inv(yy,gg)) or GEN_CAP_INIT(gg) >0)) do
    create(is_path(ss,yy,seas,gg,pp));
    is_path(ss,yy,seas,gg,pp) is_binary;
end-do



declarations
    PathCon:            dynamic array (SCENARIOS,YEARS,SEASONS,GENERATORS) of linctr;
    Installcon:         dynamic array (SCENARIOS,YEARS,SEASONS,GENERATORS) of linctr;
    Installcon2:            dynamic array (SCENARIOS,YEARS,SEASONS,GENERATORS) of linctr;
    !NodeLoadBalanceCtr: dynamic array (SCENARIOS, YEARS, SEASONS, HOURS, NODES) of linctr;!defined in EMPIRE
    CapInstall:         dynamic array(SCENARIOS,YEARS,SEASONS,GENERATORS) of linctr;
end-declarations



!!only one production pattern can be used for each generator each season
forall(ss in SCENARIOS,yy in YEARS,seas in SEASONS,gg in GENERATORS|TECH_TIDE(GEN_TECH(gg))) do
    if (exists(gen_inv(yy,gg)) or GEN_CAP_INIT(gg) >0) then
        PathCon(ss,yy,seas,gg):=
            sum(pp in PATTERNS)is_path(ss,yy,seas,gg,pp)<=1;
    end-if
end-do

!Ensuring that sufficient capacity is installed
forall(ss in SCENARIOS,yy in YEARS, seas in SEASONS,gg in GENERATORS|TECH_TIDE(GEN_TECH(gg))) do
    if (exists(gen_cap(yy,gg))) then
        Installcon(ss,yy,seas,gg):=
            sum(pp in PATTERNS|pp>1) is_path(ss,yy,seas,gg,pp)*CAP(gg)<=gen_cap(yy,gg);
    end-if
end-do

!Setting production according to production pattern
forall(ss in SCENARIOS, yy in YEARS, seas in SEASONS, hh in HOURS,gg in GENERATORS | hh>= SEASON_F_L_HOUR(seas,1) and hh<= SEASON_F_L_HOUR(seas,2) and TECH_TIDE(GEN_TECH(gg)) and exists(gen_prod(ss, yy, hh, gg))) do
    gen_prod(ss, yy, hh, gg)=sum(pp in PATTERNS)(SCALE(gg)*PROD_TIDE(pp,hh,gg)/SWANSEA_MW)*CAP(gg)*is_path(ss,yy,seas,gg,pp);
end-do !)

!For fixing investments
(!
forall(gg in GENERATORS|TECH_TIDE(GEN_TECH(gg))) do
        InvForce(gg) :=
        sum(yy in 1..4)gen_inv(yy,gg)>=CAP(gg);
    end-do!)