The UFZ services GitLab and Mattermost will be unavailable on Monday, July 4 from 06:00 AM to 08:00 AM due to maintenance work.

Commit 5036b304 authored by Markus Millinger's avatar Markus Millinger
Browse files

Added check for run errors and infeasibility in gamsRun. The runs are rerun...

Added check for run errors and infeasibility in gamsRun. The runs are rerun until ok. Important especially for parallel runs.
parent a3f2ebc5
This diff is collapsed.
......@@ -21,17 +21,19 @@
function [gamsOut,g] = gamsRun(s,f,g,scenario,noScenarios,gamsTemp,version)
% Start GAMS function
tic
if contains(version,'ghgMax')
gamsTemp.ghgTarget.val = 1;
gamsTemp.solveOption.val = 0;
solName = ['matsol' num2str(scenario) '1'];
elseif contains(version,'costMin')
gamsTemp.solveOption.val = 1;
solName = ['matsol' num2str(scenario) '1'];
end
wgdx('matdata.gdx',g.d,g.C,g.I,g.J,g.F,g.B,g.BR,g.BF,g.tb,g.ts,g.fuelType,g.PwrSrc,g.PwrMix,g.PwrRes,...
error=1;
runComplete=0;
while error==1 && runComplete==0
if contains(version,'ghgMax')
gamsTemp.ghgTarget.val = 1;
gamsTemp.solveOption.val = 0;
solName = ['matsol' num2str(scenario) '1'];
elseif contains(version,'costMin')
gamsTemp.solveOption.val = 1;
solName = ['matsol' num2str(scenario) '1'];
end
wgdx('matdata.gdx',g.d,g.C,g.I,g.J,g.F,g.B,g.BR,g.BF,g.tb,g.ts,g.fuelType,g.PwrSrc,g.PwrMix,g.PwrRes,...
g.tDiesel,g.tEtOH,g.tCH4,g.tLNG,g.tH2,g.tEV,g.tAviationFuel,...
gamsTemp.costMarg,...
gamsTemp.costInv,...
......@@ -80,155 +82,164 @@ wgdx('matdata.gdx',g.d,g.C,g.I,g.J,g.F,g.B,g.BR,g.BF,g.tb,g.ts,g.fuelType,g.PwrS
gamsTemp.posResLoad,...
gamsTemp.solveOption);
system(['gams benopt.gms gdx=' solName]); %lo=3
% gams('benopt',...
% );%#ok<*SAGROW>
toc
% % Test if problem is infeasible
statstr.name = 'returnstat';
gamsOut.stat = rgdx(solName, statstr);
sdfdf = gamsOut.stat.val
% size(sdfdf)
tic
system(['gams benopt.gms gdx=' solName]); %lo=3
toc
% Test if problem is infeasible or run was faulty
statstr.name = 'returnstat';
gamsOut.stat = rgdx(solName, statstr);
if isequal(size(gamsOut.stat.val),[1 2])
runComplete = 1;
if gamsOut.stat.val(1,1)~=1 || gamsOut.stat.val(1,2)~=1
error = 1;
disp('infeasible - rerun')
else
error = 0;
end
else
runComplete = 0;
disp('run faulty - rerun')
end
end
% % if gamsOut.stat.val(1,1)~=1
% if stat.val(1,1)~=1 || stat.val(1,2)~=1
% error('Problem infeasible!')
% end
% end
% Import results from GAMS
TS2str.uels = {s.techNames, s.sectorNames};
TS2str.name = 'TS';
TS2str.form = 'full';
TS2data = rgdx(solName, TS2str);
g.TS2=cell(1,10);
for j=1:s.numSectors
g.TS2{j}=transpose(find(TS2data.val(:,j)==1));
end
prdstr.uels = {strsplit(num2str(s.year)), s.techNames, s.sectorNames};
prdstr.name = 'prd';
prdstr.form = 'full';
prd = rgdx(solName, prdstr);
gamsOut.prd=prd.val;
inststr.uels = {strsplit(num2str(s.year)), s.techNames};
inststr.name = 'inst';
inststr.form = 'full';
instCap = rgdx(solName, inststr);
gamsOut.instCap=instCap.val;
coststr.name = 'cost';
coststr.form = 'full';
cost = rgdx(solName, coststr);
gamsOut.cost=cost.val;
costAnnualstr.uels = {strsplit(num2str(s.year))};
costAnnualstr.name = 'costAnnual';
costAnnualstr.form = 'full';
costAnnual = rgdx(solName, costAnnualstr);
gamsOut.costAnnual=costAnnual.val;
biomassstr.uels = {strsplit(num2str(s.year)), s.techNames, f.biomassNames,g.cats};
biomassstr.name = 'biomassUse';
biomassstr.form = 'full';
biomassUse = rgdx(solName, biomassstr);
gamsOut.biomassUse=biomassUse.val;
biomassImportstr.uels = {strsplit(num2str(s.year)), s.techNames, f.biomassNames};
biomassImportstr.name = 'biomassUseImport';
biomassImportstr.form = 'full';
biomassUseImport = rgdx(solName, biomassImportstr);
gamsOut.biomassUseImport=biomassUseImport.val;
capstr.uels = {strsplit(num2str(s.year)), s.techNames};
capstr.name = 'cap';
capstr.form = 'full';
capCAP = rgdx(solName, capstr);
gamsOut.capCAP=capCAP.val;
costTechstr.uels = {strsplit(num2str(s.year)), s.techNames};
costTechstr.name = 'costTech';
costTechstr.form = 'full';
costTech = rgdx(solName, costTechstr);
gamsOut.techCost = costTech.val; %unit: M /year
ghgAbateTechstr.uels = {strsplit(num2str(s.year)), s.techNames};
ghgAbateTechstr.name = 'ghgAbateTech';
ghgAbateTechstr.form = 'full';
ghgAbateTech = rgdx(solName, ghgAbateTechstr);
gamsOut.ghgAbateTech = ghgAbateTech.val; %unit: ktCO2eq
%
% Import results from GAMS
TS2str.uels = {s.techNames, s.sectorNames};
TS2str.name = 'TS';
TS2str.form = 'full';
TS2data = rgdx(solName, TS2str);
g.TS2=cell(1,10);
for j=1:s.numSectors
g.TS2{j}=transpose(find(TS2data.val(:,j)==1));
end
prdstr.uels = {strsplit(num2str(s.year)), s.techNames, s.sectorNames};
prdstr.name = 'prd';
prdstr.form = 'full';
prd = rgdx(solName, prdstr);
gamsOut.prd=prd.val;
inststr.uels = {strsplit(num2str(s.year)), s.techNames};
inststr.name = 'inst';
inststr.form = 'full';
instCap = rgdx(solName, inststr);
gamsOut.instCap=instCap.val;
coststr.name = 'cost';
coststr.form = 'full';
cost = rgdx(solName, coststr);
gamsOut.cost=cost.val;
costAnnualstr.uels = {strsplit(num2str(s.year))};
costAnnualstr.name = 'costAnnual';
costAnnualstr.form = 'full';
costAnnual = rgdx(solName, costAnnualstr);
gamsOut.costAnnual=costAnnual.val;
biomassstr.uels = {strsplit(num2str(s.year)), s.techNames, f.biomassNames,g.cats};
biomassstr.name = 'biomassUse';
biomassstr.form = 'full';
biomassUse = rgdx(solName, biomassstr);
gamsOut.biomassUse=biomassUse.val;
biomassImportstr.uels = {strsplit(num2str(s.year)), s.techNames, f.biomassNames};
biomassImportstr.name = 'biomassUseImport';
biomassImportstr.form = 'full';
biomassUseImport = rgdx(solName, biomassImportstr);
gamsOut.biomassUseImport=biomassUseImport.val;
capstr.uels = {strsplit(num2str(s.year)), s.techNames};
capstr.name = 'cap';
capstr.form = 'full';
capCAP = rgdx(solName, capstr);
gamsOut.capCAP=capCAP.val;
costTechstr.uels = {strsplit(num2str(s.year)), s.techNames};
costTechstr.name = 'costTech';
costTechstr.form = 'full';
costTech = rgdx(solName, costTechstr);
gamsOut.techCost = costTech.val; %unit: M /year
ghgAbateTechstr.uels = {strsplit(num2str(s.year)), s.techNames};
ghgAbateTechstr.name = 'ghgAbateTech';
ghgAbateTechstr.form = 'full';
ghgAbateTech = rgdx(solName, ghgAbateTechstr);
gamsOut.ghgAbateTech = ghgAbateTech.val; %unit: ktCO2eq
%
% capFoutstr.uels = {strsplit(num2str(s.year)), s.techNames};
% capFoutstr.name = 'capFout';
% capFoutstr.form = 'full';
% capFout = rgdx(solName, capFoutstr);
% gamsOut.capFout = capFout.val;
powerResUsestr.uels = {strsplit(num2str(1:1:max(g.timeStepsIntraYear))), strsplit(num2str(s.year))};
powerResUsestr.name = 'residualLoadUse';
powerResUsestr.form = 'full';
powerResUse = rgdx(solName, powerResUsestr);
gamsOut.powerResUse = powerResUse.val;
dispatchPrdstr.uels = {strsplit(num2str(s.year)), strsplit(num2str(1:1:max(g.timeStepsIntraYear))), s.techNames, s.sectorNames};
dispatchPrdstr.name = 'prdDaily';
dispatchPrdstr.form = 'full';
dispatchPrd = rgdx(solName, dispatchPrdstr);
gamsOut.dispatchPrd = dispatchPrd.val;
CO2Usestr.uels = {strsplit(num2str(s.year))};
CO2Usestr.name = 'CO2use';
CO2Usestr.form = 'full';
CO2Use = rgdx(solName, CO2Usestr);
gamsOut.CO2Use=CO2Use.val;
powerResUsestr.uels = {strsplit(num2str(1:1:max(g.timeStepsIntraYear))), strsplit(num2str(s.year))};
powerResUsestr.name = 'residualLoadUse';
powerResUsestr.form = 'full';
powerResUse = rgdx(solName, powerResUsestr);
gamsOut.powerResUse = powerResUse.val;
dispatchPrdstr.uels = {strsplit(num2str(s.year)), strsplit(num2str(1:1:max(g.timeStepsIntraYear))), s.techNames, s.sectorNames};
dispatchPrdstr.name = 'prdDaily';
dispatchPrdstr.form = 'full';
dispatchPrd = rgdx(solName, dispatchPrdstr);
gamsOut.dispatchPrd = dispatchPrd.val;
CO2Usestr.uels = {strsplit(num2str(s.year))};
CO2Usestr.name = 'CO2use';
CO2Usestr.form = 'full';
CO2Use = rgdx(solName, CO2Usestr);
gamsOut.CO2Use=CO2Use.val;
if contains(version,'ghgMax')
ghgAbatementstr.name = 'ghgAbatement';
ghgAbatementstr.form = 'full';
ghgTarget = rgdx(solName, ghgAbatementstr);
gamsOut.ghgTarget=ghgTarget.val;
gamsOut.ghgTarget=ghgTarget.val;
end
%%
%%
% Densing results
%Dispatch (year,timeStep,tech)
%Dispatch (year,timeStep,tech)
gamsOut.dispatchPrd2 = sum(gamsOut.dispatchPrd,4);
% Energy production (time,tech) [PJ]
gamsOut.prd2 = (sum(gamsOut.prd,3).*s.powerByprod+sum(gamsOut.prd,3).*s.heatByprod); %3 to 2 dimensions sorted by techs
% Energy production (time,market) [PJ]
% Energy production (time,tech) [PJ]
gamsOut.prd2 = (sum(gamsOut.prd,3).*s.powerByprod+sum(gamsOut.prd,3).*s.heatByprod); %3 to 2 dimensions sorted by techs
% Energy production (time,market) [PJ]
% o.prd4 = sum(sum(o.prd3,2),1); %2 to 1 dimension, sorted by markets
% Biomass Use (time,tech) [PJ]
gamsOut.biomassUse2 = squeeze(sum(sum(gamsOut.biomassUse,4),3)); %4 to 2 dimensions sorted by techs
% Biomass Use (time,biomass Type) [PJ]
gamsOut.biomassUse3 = squeeze(sum(sum(gamsOut.biomassUse,2),4)); %4 to 2 dimensions sorted by biomass
% Biomass Use (time, tech, biomass) [PJ]
gamsOut.biomassUse4 = squeeze(sum(gamsOut.biomassUse,4)); %4 to 3 dimensions
% Biomass Use Import (time,tech) [PJ]
gamsOut.biomassUseImport2 = squeeze(sum(gamsOut.biomassUseImport,3)); %3 to 2 dimensions sorted by techs
% Biomass Use Import (time,biomass Type) [PJ]
gamsOut.biomassUseImport3 = squeeze(sum(gamsOut.biomassUseImport,2)); %3 to 2 dimensions sorted by biomass
%%Correcting total demand in PJ by the fuel economy per km
indexEl = find(contains(s.techNames,{'BEV'}));
indexH2 = find(contains(s.techNames,{'FCEV'}));
gamsOut.DemandRoadPass = g.Demand(6,:)-s.relativeFuelEconomy(indexEl).*(gamsOut.prd(:,indexEl,6))'+(gamsOut.prd(:,indexEl,6))';
gamsOut.DemandRoadPass = gamsOut.DemandRoadPass-s.relativeFuelEconomy(indexH2).*(gamsOut.prd(:,indexH2,6))'+(gamsOut.prd(:,indexH2,6))';
% Biomass Use (time,tech) [PJ]
gamsOut.biomassUse2 = squeeze(sum(sum(gamsOut.biomassUse,4),3)); %4 to 2 dimensions sorted by techs
% Biomass Use (time,biomass Type) [PJ]
gamsOut.biomassUse3 = squeeze(sum(sum(gamsOut.biomassUse,2),4)); %4 to 2 dimensions sorted by biomass
% Biomass Use (time, tech, biomass) [PJ]
gamsOut.biomassUse4 = squeeze(sum(gamsOut.biomassUse,4)); %4 to 3 dimensions
% Biomass Use Import (time,tech) [PJ]
gamsOut.biomassUseImport2 = squeeze(sum(gamsOut.biomassUseImport,3)); %3 to 2 dimensions sorted by techs
% Biomass Use Import (time,biomass Type) [PJ]
gamsOut.biomassUseImport3 = squeeze(sum(gamsOut.biomassUseImport,2)); %3 to 2 dimensions sorted by biomass
%%Correcting total demand in PJ by the fuel economy per km
indexEl = find(contains(s.techNames,{'BEV'}));
indexH2 = find(contains(s.techNames,{'FCEV'}));
gamsOut.DemandRoadPass = g.Demand(6,:)-s.relativeFuelEconomy(indexEl).*(gamsOut.prd(:,indexEl,6))'+(gamsOut.prd(:,indexEl,6))';
gamsOut.DemandRoadPass = gamsOut.DemandRoadPass-s.relativeFuelEconomy(indexH2).*(gamsOut.prd(:,indexH2,6))'+(gamsOut.prd(:,indexH2,6))';
for sector = 1:11
gamsOut.DemandAdapt(sector,:) = g.Demand(sector,:)-s.relativeFuelEconomy(indexH2,sector).*(gamsOut.prd(:,indexH2,sector))'+(gamsOut.prd(:,indexH2,sector))'...
-s.relativeFuelEconomy(indexEl,sector).*(gamsOut.prd(:,indexEl,sector))'+(gamsOut.prd(:,indexEl,sector))';%+((gamsOut(scenario).prd(:,indexH2,sector))*(1-s.relativeFuelEconomy(indexH2)))';
gamsOut.DemandAdapt(sector,:) = g.Demand(sector,:)-s.relativeFuelEconomy(indexH2,sector).*(gamsOut.prd(:,indexH2,sector))'+(gamsOut.prd(:,indexH2,sector))'...
-s.relativeFuelEconomy(indexEl,sector).*(gamsOut.prd(:,indexEl,sector))'+(gamsOut.prd(:,indexEl,sector))';%+((gamsOut(scenario).prd(:,indexH2,sector))*(1-s.relativeFuelEconomy(indexH2)))';
end
end
\ No newline at end of file
......@@ -72,9 +72,7 @@ for scenario = 1:noScenarios
[s,f] = ghgEmissions(s,f);
%% Monte Carlo sensitivity analysis example of VRE module
% [monteCarloOut,weatherYear,H2,runTimeGAMS,timeSteps,EVfactor,dRate,CO2source,H2max,landMax,CO2Use]...
% =
monteCarlo(100,s,f,g,techData);
monteCarlo(10,s,f,g,techData);
%% Scenario loop
switch scenario
case 1 %Base scenario
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment