From 3162fa536543431389e89ee7c57510543ca0ed9f Mon Sep 17 00:00:00 2001
From: Peter Alexander <peter@blackhillock.co.uk>
Date: Thu, 22 Jun 2017 15:33:04 +0100
Subject: [PATCH] Revert to per-grid cell irrigation constraint

---
 GAMS/IntExtOpt.gms                                     | 6 +++---
 src/ac/ed/lurg/ModelConfig.java                        | 1 +
 src/ac/ed/lurg/ModelMain.java                          | 6 +++++-
 src/ac/ed/lurg/country/CountryAgent.java               | 4 +++-
 src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java | 2 +-
 5 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/GAMS/IntExtOpt.gms b/GAMS/IntExtOpt.gms
index 1383000c..b7566b86 100644
--- a/GAMS/IntExtOpt.gms
+++ b/GAMS/IntExtOpt.gms
@@ -123,7 +123,7 @@ $gdxin
        MIN_NET_IMPORT_CONSTRAINT(import_crop)           constraint on min net imports
        PASTURE_IMPORT_CONSTRAINT                        constraint to not import pasture
        PASTURE_EXPORT_CONSTRAINT
-       IRRIGATION_CONSTRAINT                            constraint no water usage
+       IRRIGATION_CONSTRAINT(location)                  constraint no water usage
        NET_SUPPLY_EQ(crop)                              calc net supply for crops
        AGRI_LAND_EXPANSION_CALC(location)               calc agriLandExpansion
        CROP_INCREASE_CALC(location)                     
@@ -180,8 +180,8 @@ $gdxin
  PASTURE_IMPORT_CONSTRAINT .. importAmount('pasture') =E= 0;
  PASTURE_EXPORT_CONSTRAINT ..  exportAmount('pasture') =E= 0;
   
- IRRIGATION_CONSTRAINT .. sum(location, irrigConstraint(location) * suitableLandArea(location)) =G= sum((crop, location), irrigMaxRate(crop, location) * irrigI(crop, location) * area(crop, location));
- 
+ IRRIGATION_CONSTRAINT(location) .. irrigConstraint(location) * suitableLandArea(location) =G= sum(crop, irrigMaxRate(crop, location) * irrigI(crop, location) * area(crop, location));
+
  AGRI_LAND_EXPANSION_CALC(location) .. agriLandExpansion(location) =G= sum(crop, area(crop, location) - previousArea(crop, location)); 
  
  CROP_INCREASE_CALC(location) .. cropIncrease(location) =G= sum(crop_less_pasture, area(crop_less_pasture, location) - previousArea(crop_less_pasture, location));
diff --git a/src/ac/ed/lurg/ModelConfig.java b/src/ac/ed/lurg/ModelConfig.java
index d61bc114..2ad2f69d 100644
--- a/src/ac/ed/lurg/ModelConfig.java
+++ b/src/ac/ed/lurg/ModelConfig.java
@@ -242,6 +242,7 @@ public class ModelConfig {
 	public static final boolean PROTECTED_AREAS_ENABLED = getBooleanProperty("PROTECTED_AREAS_ENABLED", true);
 	public static final double MIN_NATURAL_RATE = getDoubleProperty("MIN_NATURAL_RATE", 0.10);
 
+	public static final boolean DEBUG_JUST_DEMAND_OUTPUT = getBooleanProperty("DEBUG_JUST_DEMAND_OUTPUT", false);
 	public static final boolean DEBUG_LIMIT_COUNTRIES = getBooleanProperty("DEBUG_LIMIT_COUNTRIES", false);
 	public static final String DEBUG_COUNTRY_NAME = getProperty("DEBUG_COUNTRY_NAME", "United States of America");
 	public static final double PASTURE_MAX_IRRIGATION_RATE = getDoubleProperty("DEFAULT_MAX_IRRIGATION_RATE", 50.0); // shouldn't need this but some areas crops don't have a value, but was causing them to be selected
diff --git a/src/ac/ed/lurg/ModelMain.java b/src/ac/ed/lurg/ModelMain.java
index 8d79bfee..d85fdd4a 100644
--- a/src/ac/ed/lurg/ModelMain.java
+++ b/src/ac/ed/lurg/ModelMain.java
@@ -299,12 +299,16 @@ public class ModelMain {
 
 				for (CountryAgent country : countryAgents) {
 					Double d = country.getCurrentProjectedDemand().get(comm);
-					if (d != null)
+					if (d != null) {
 						demandAmount += d.doubleValue();
+						LogWriter.println(String.format("%s,%s,%.4f", country.getCountry(), comm.getGamsName(), d));
+					}
 				}
 				StringBuffer sbData = new StringBuffer();
 				sbData.append(String.format("%d,%s", timestep.getYear(), comm.getGamsName()));
 				sbData.append(String.format(",%.1f", demandAmount));
+				
+				LogWriter.println("Global demand " + timestep.getYear() + " " + comm.getGamsName() + " " + demandAmount + "\n");
 
 				outputFile.write(sbData.toString());
 				outputFile.newLine();
diff --git a/src/ac/ed/lurg/country/CountryAgent.java b/src/ac/ed/lurg/country/CountryAgent.java
index 3b014a51..f2293887 100644
--- a/src/ac/ed/lurg/country/CountryAgent.java
+++ b/src/ac/ed/lurg/country/CountryAgent.java
@@ -110,7 +110,9 @@ public class CountryAgent {
 		else if (countryYieldSurfaces.size() == 0 ) {
 			LogWriter.printlnError("No yield values for " + country + " so skipping it");
 		}
-		else {
+		else if (ModelConfig.DEBUG_JUST_DEMAND_OUTPUT) { // if this debug flag is set we don't do the optimisation
+		}
+		else {			
 			if (yieldClusters==null)
 				yieldClusters = calcYieldClusters(countryYieldSurfaces);  // this should only be on the first timestep
 			
diff --git a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
index fcb27e8a..21d113de 100644
--- a/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
+++ b/src/ac/ed/lurg/country/gams/GamsLocationOptimiser.java
@@ -158,7 +158,7 @@ public class GamsLocationOptimiser {
 			setGamsParamValue(irrigConstraintP.addRecord(Integer.toString(locationId)), irrigConstraint, 3);
 		}
 
-		if (DEBUG) LogWriter.println("\nDemand");
+		if (DEBUG) LogWriter.println("\nDemand: " + inputData.getCountryInput().getCountry() + " " + inputData.getTimestep().getYear());
 		GamsCountryInput countryInput = inputData.getCountryInput();
 		addCommodityMapParm(inDB.addParameter("demand", 1), countryInput.getProjectedDemand(), 4);
 
-- 
GitLab