From af4674513110b29fd6fc0179d2fb170a9d3c18be Mon Sep 17 00:00:00 2001
From: Bart Arendarczyk <s1924442@ed.ac.uk>
Date: Mon, 5 Jun 2023 12:33:36 +0100
Subject: [PATCH] Wood demand rebasing bug fix.

---
 src/ac/ed/lurg/demand/ElasticDemandManager.java | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/ac/ed/lurg/demand/ElasticDemandManager.java b/src/ac/ed/lurg/demand/ElasticDemandManager.java
index 5d37b13f..46be9b3d 100755
--- a/src/ac/ed/lurg/demand/ElasticDemandManager.java
+++ b/src/ac/ed/lurg/demand/ElasticDemandManager.java
@@ -194,6 +194,7 @@ public class ElasticDemandManager extends AbstractSSPDemandManager {
 
 		Map<WoodCommodityType, Double> woodDemands = new HashMap<>();
 		SspData baseSspData = sspManager.get(ssp_scenario, baseYearToRebaseConsumption, country);
+		double baseGdpPc = baseSspData.getGdpPc();
 
 		Map<WoodCommodityType, Double> baseExpCpcMap = baseWoodCpcCache.get(country);
 		if (baseExpCpcMap == null) {  // we should only do this in the first year
@@ -208,9 +209,10 @@ public class ElasticDemandManager extends AbstractSSPDemandManager {
 			double newExpCpc = entry.getValue();
 			double baseExpCpc = baseExpCpcMap.get(commodity);
 			double baseCpc = woodDemandManager.getBaseDemand(country, commodity);
+			double cpc = rebaseConsumption(baseGdpPc, baseCpc, baseExpCpc, gdpPc, newExpCpc);
+			cpc = Math.max(cpc, 0); // could be negative due to rebasing so need to max 0
 
 			// Rebased consumption
-			double cpc = baseCpc + (newExpCpc - baseExpCpc);
 			double d = cpc * population;
 			woodDemands.put(commodity, d);
 		}
-- 
GitLab