From 05ba34b7b2e6534c33ca9c2be193baa9e56c40a5 Mon Sep 17 00:00:00 2001
From: Roslyn Henry <roslyn.henry@ed.ac.uk>
Date: Tue, 29 Sep 2020 14:52:33 +0100
Subject: [PATCH] Output gams demand information only when demand has been
 recalculated.

---
 src/ac/ed/lurg/country/AbstractCountryAgent.java      | 8 ++++----
 src/ac/ed/lurg/country/CountryAgent.java              | 2 +-
 src/ac/ed/lurg/country/CountryAgentManager.java       | 2 +-
 src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java | 2 +-
 src/ac/ed/lurg/demand/AbstractDemandManager.java      | 6 +++---
 src/ac/ed/lurg/demand/AbstractSSPDemandManager.java   | 6 +++---
 src/ac/ed/lurg/demand/DemandManagerFromFile.java      | 2 +-
 src/ac/ed/lurg/demand/DemandManagerSSP.java           | 2 +-
 src/ac/ed/lurg/demand/ElasticDemandManager.java       | 4 ++--
 9 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/ac/ed/lurg/country/AbstractCountryAgent.java b/src/ac/ed/lurg/country/AbstractCountryAgent.java
index 25283e15..c98997ab 100644
--- a/src/ac/ed/lurg/country/AbstractCountryAgent.java
+++ b/src/ac/ed/lurg/country/AbstractCountryAgent.java
@@ -53,14 +53,14 @@ public abstract class AbstractCountryAgent {
 		currentCountryPrices = countryPrices;
 	}
 	
-	private void calculateDemand() {
+	private void calculateDemand(boolean outputGamsDemand) {
 		Map<CommodityType, Double> producerPrices = getProducerCommodityPrices();
-		currentProjectedDemand = demandManager.getDemand(country, currentTimestep.getYear(), producerPrices);
+		currentProjectedDemand = demandManager.getDemand(country, currentTimestep.getYear(), producerPrices, outputGamsDemand);
 	}
 	
-	protected void calculateCountryPricesAndDemand(Map<CropType, GlobalPrice> worldPrices) {
+	protected void calculateCountryPricesAndDemand(Map<CropType, GlobalPrice> worldPrices, boolean outputGamsDemand) {
 		calculateCountryPrices(worldPrices);
-		calculateDemand();
+		calculateDemand(outputGamsDemand);
 	}
 	
 	private Map<CommodityType, Double> getProducerCommodityPrices() {
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 6b3ca7a9..4bcb755a 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -121,7 +121,7 @@ public class CountryAgent extends AbstractCountryAgent {
 			Map<CropType, GlobalPrice> worldPrices, double globalGen2EcIncrease) {
 
 		// project demand
-		calculateCountryPricesAndDemand(worldPrices);
+		calculateCountryPricesAndDemand(worldPrices, false);
 		
 		if (saveGamsGdxFiles && ModelConfig.PRICE_ELASTIC_DEMAND)
 			saveGDXFile("demand");
diff --git a/src/ac/ed/lurg/country/CountryAgentManager.java b/src/ac/ed/lurg/country/CountryAgentManager.java
index 8fe33be5..8340d27a 100644
--- a/src/ac/ed/lurg/country/CountryAgentManager.java
+++ b/src/ac/ed/lurg/country/CountryAgentManager.java
@@ -138,7 +138,7 @@ public class CountryAgentManager {
 	public void recalculateDemandForAll() {
 		for (CountryAgent ca : gamsCountryAgents) {
 			ca.shockGDP();
-			ca.calculateCountryPricesAndDemand(internationalMarket.getWorldPrices());
+			ca.calculateCountryPricesAndDemand(internationalMarket.getWorldPrices(), true);
 		}
 	}
 	
diff --git a/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java b/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
index 7ab4740c..af0145d6 100644
--- a/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
+++ b/src/ac/ed/lurg/country/crafty/CraftyCountryAgent.java
@@ -29,7 +29,7 @@ public class CraftyCountryAgent extends AbstractCountryAgent {
 	}
 
 	public void updateProduction(Map<CropType, Double> cropProduction, Map<CropType, GlobalPrice> worldPrices) {
-		calculateCountryPricesAndDemand(worldPrices);
+		calculateCountryPricesAndDemand(worldPrices, false);
 
 		cropUsageData = new HashMap<CropType, CropUsageData>();
 		for (Entry<CropType, Double> entry : cropProduction.entrySet()) {
diff --git a/src/ac/ed/lurg/demand/AbstractDemandManager.java b/src/ac/ed/lurg/demand/AbstractDemandManager.java
index 18a17674..81808f06 100644
--- a/src/ac/ed/lurg/demand/AbstractDemandManager.java
+++ b/src/ac/ed/lurg/demand/AbstractDemandManager.java
@@ -27,7 +27,7 @@ public abstract class AbstractDemandManager {
 		cerealFractionsManager = new CerealFractionsManager(compositeCountryManager);
 	}
 
-	public Map<CommodityType, Double> getDemand(CompositeCountry cc, int year, Map<CommodityType, Double> prices) {
+	public Map<CommodityType, Double> getDemand(CompositeCountry cc, int year, Map<CommodityType, Double> prices, boolean outputGamsDemand) {
 
 		if (!ModelConfig.CHANGE_DEMAND_YEAR)
 			year = ModelConfig.BASE_YEAR;
@@ -37,7 +37,7 @@ public abstract class AbstractDemandManager {
 
 		for (SingleCountry c : compositeCountryManager.getAllForCompositeCountry(cc)) {
 
-			foodDemandMap = getFoodDemand(c, year, prices);
+			foodDemandMap = getFoodDemand(c, year, prices, outputGamsDemand);
 
 			for (CommodityType commodity : CommodityType.getAllFoodItems()) {
 
@@ -60,7 +60,7 @@ public abstract class AbstractDemandManager {
 		return demandMap;
 	}
 
-	protected abstract Map<CommodityType, Double> getFoodDemand(SingleCountry c, int year, Map<CommodityType, Double> prices);
+	protected abstract Map<CommodityType, Double> getFoodDemand(SingleCountry c, int year, Map<CommodityType, Double> prices, boolean outputGamsDemand);
 	
 	public void updateGdpLossesFromShock(CompositeCountry cc, int year, double shockMagnitude) {}
 
diff --git a/src/ac/ed/lurg/demand/AbstractSSPDemandManager.java b/src/ac/ed/lurg/demand/AbstractSSPDemandManager.java
index 7781a43a..56da459d 100644
--- a/src/ac/ed/lurg/demand/AbstractSSPDemandManager.java
+++ b/src/ac/ed/lurg/demand/AbstractSSPDemandManager.java
@@ -48,7 +48,7 @@ public abstract class AbstractSSPDemandManager extends AbstractDemandManager {
 	}
 
 	@Override
-	protected Map<CommodityType, Double> getFoodDemand(SingleCountry c, int year, Map<CommodityType, Double> prices) {
+	protected Map<CommodityType, Double> getFoodDemand(SingleCountry c, int year, Map<CommodityType, Double> prices, boolean outputGamsDemand) {
 		SspData sd = sspManager.get(ssp_scenario, year, c);
 		Map<CommodityType, Double> foodDemandMap = new HashMap<CommodityType, Double>();
 
@@ -67,7 +67,7 @@ public abstract class AbstractSSPDemandManager extends AbstractDemandManager {
 		SspData usaSd = sspManager.get(ssp_scenario, year, usa);
 		
 		LogWriter.println("Got ssp data for " + c.getCountryName() + " of " + sd);
-		foodDemandMap = getFoodDemandMap(c, year, gdpPcYear * ModelConfig.SSP_GDP_PC_FACTOR, sd.getPopulation() * ModelConfig.SSP_POPULATION_FACTOR, prices, usaSd.getGdpPc());
+		foodDemandMap = getFoodDemandMap(c, year, gdpPcYear * ModelConfig.SSP_GDP_PC_FACTOR, sd.getPopulation() * ModelConfig.SSP_POPULATION_FACTOR, prices, usaSd.getGdpPc(), outputGamsDemand);
 		
 		if(!ModelConfig.SHOCKS_POSSIBLE){
 			return foodDemandMap;
@@ -85,5 +85,5 @@ public abstract class AbstractSSPDemandManager extends AbstractDemandManager {
 		return projectedCpc;
 	}
 
-	abstract Map<CommodityType, Double> getFoodDemandMap(SingleCountry c, int year, double gdpPc, double population, Map<CommodityType, Double> prices, double usaGdpPc);
+	abstract Map<CommodityType, Double> getFoodDemandMap(SingleCountry c, int year, double gdpPc, double population, Map<CommodityType, Double> prices, double usaGdpPc, boolean outputGamsDemand);
 }
diff --git a/src/ac/ed/lurg/demand/DemandManagerFromFile.java b/src/ac/ed/lurg/demand/DemandManagerFromFile.java
index 766ea12c..7d084ec5 100644
--- a/src/ac/ed/lurg/demand/DemandManagerFromFile.java
+++ b/src/ac/ed/lurg/demand/DemandManagerFromFile.java
@@ -21,7 +21,7 @@ public class DemandManagerFromFile extends AbstractDemandManager {
 	}
 
 	@Override
-	protected Map<CommodityType, Double> getFoodDemand(SingleCountry c, int year, Map<CommodityType, Double> prices) {
+	protected Map<CommodityType, Double> getFoodDemand(SingleCountry c, int year, Map<CommodityType, Double> prices, boolean outputGamsDemand) {
 		Map<CommodityType, Double> foodDemandMap = new HashMap<CommodityType, Double>();
 
 		for (CommodityType commodity : CommodityType.getAllFoodItems()) {
diff --git a/src/ac/ed/lurg/demand/DemandManagerSSP.java b/src/ac/ed/lurg/demand/DemandManagerSSP.java
index 683b00aa..c139c239 100644
--- a/src/ac/ed/lurg/demand/DemandManagerSSP.java
+++ b/src/ac/ed/lurg/demand/DemandManagerSSP.java
@@ -19,7 +19,7 @@ public class DemandManagerSSP extends AbstractSSPDemandManager {
 	}
 
 	@Override
-	Map<CommodityType, Double> getFoodDemandMap(SingleCountry c, int year, double gdpPc, double population, Map<CommodityType, Double> prices, double usaGdpPc) {
+	Map<CommodityType, Double> getFoodDemandMap(SingleCountry c, int year, double gdpPc, double population, Map<CommodityType, Double> prices, double usaGdpPc, boolean outputGamsDemand) {
 		Map<CommodityType, Double> aFoodDemandMap = new HashMap<CommodityType, Double>();
 		SspData baseSspData = sspManager.get(ssp_scenario, ModelConfig.BASE_YEAR, c);
 		double baseGdpPc = baseSspData.getGdpPc();
diff --git a/src/ac/ed/lurg/demand/ElasticDemandManager.java b/src/ac/ed/lurg/demand/ElasticDemandManager.java
index cac52278..eca6b8d8 100755
--- a/src/ac/ed/lurg/demand/ElasticDemandManager.java
+++ b/src/ac/ed/lurg/demand/ElasticDemandManager.java
@@ -35,7 +35,7 @@ public class ElasticDemandManager extends AbstractSSPDemandManager {
 	}
 
 	@Override
-	Map<CommodityType, Double> getFoodDemandMap(SingleCountry c, int year, double gdpPc, double population, Map<CommodityType, Double> producerPrices, double usaGdpPc) {
+	Map<CommodityType, Double> getFoodDemandMap(SingleCountry c, int year, double gdpPc, double population, Map<CommodityType, Double> producerPrices, double usaGdpPc, boolean outputGamsDemand) {
 		Map<CommodityType, Double> consumerPrices = new HashMap<CommodityType, Double>();
 
 		Map<CommodityType, Double> kcalPerT = calorieManager.get(c);
@@ -97,7 +97,7 @@ public class ElasticDemandManager extends AbstractSSPDemandManager {
 			foodDemands.put(commodity, d);
 		}
 
-		writeGamsDemandOutputs(inputData, gamsOutput, foodDemands, population);
+		if (outputGamsDemand) writeGamsDemandOutputs(inputData, gamsOutput, foodDemands, population);
 
 		return foodDemands;
 	}
-- 
GitLab