Skip to content
Snippets Groups Projects
AbstractDemandManager.java 3.96 KiB
package ac.ed.lurg.demand;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import ac.ed.lurg.ModelConfig;
import ac.ed.lurg.country.CompositeCountry;
import ac.ed.lurg.country.CompositeCountryManager;
import ac.ed.lurg.country.SingleCountry;
import ac.ed.lurg.types.CommodityType;
import ac.ed.lurg.types.CropType;
import ac.ed.lurg.utils.LogWriter;

public abstract class AbstractDemandManager implements Serializable {

	private static final long serialVersionUID = 8622023691732185744L;
	protected transient CompositeCountryManager compositeCountryManager;
	protected transient CalorieManager calorieManager;
	protected transient BioenergyDemandManager bioenergyDemandManager;
	protected transient CerealFractionsManager cerealFractionsManager;

	public AbstractDemandManager(CompositeCountryManager compositeCountryManager, CalorieManager calorieManager) {
		setup(compositeCountryManager, calorieManager);
	}
	
	protected void setup(CompositeCountryManager compositeCountryManager, CalorieManager calorieManager) {
		this.compositeCountryManager = compositeCountryManager;
		this.calorieManager = calorieManager; 
		bioenergyDemandManager = new BioenergyDemandManager();
		cerealFractionsManager = new CerealFractionsManager(compositeCountryManager);
	}

	public Map<CommodityType, Double> getDemand(CompositeCountry cc, int year, Map<CommodityType, Double> prices, boolean outputGamsDemand) {

		if (!ModelConfig.CHANGE_DEMAND_YEAR)
			year = ModelConfig.BASE_YEAR;

		Map<CommodityType, Double> demandMap = new HashMap<CommodityType, Double>();
		Map<CommodityType, Double> foodDemandMap;

		for (SingleCountry c : compositeCountryManager.getAllForCompositeCountry(cc)) {

			try {
				foodDemandMap = getFoodDemand(c, year, prices, outputGamsDemand);
			} catch (Exception e) {
				LogWriter.printlnError("Error: AbstractDemandManager not including " + c + " due to exception");
				LogWriter.print(e);
				continue;
			}
				
			for (CommodityType commodity : CommodityType.getAllFoodItems()) {

				Double food = foodDemandMap.get(commodity);
				double bioenergy = bioenergyDemandManager.getFirstGenBioenergyDemand(c, year, commodity);
				// LogWriter.println(String.format("Country %s comm %s: %f",
				// country, commodity, bioenergy));
				
				double newDemand = ((food == null ? 0.0 : food.doubleValue()) + bioenergy);
				Double prevDemand = demandMap.get(commodity);

				if (prevDemand != null)
					newDemand += prevDemand.doubleValue(); // aggregate if
															// composite country

				demandMap.put(commodity, newDemand);
			}
		}
		
		return demandMap;
	}

	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) {}
	
	public double getFirstGenBioenergyDemand(CompositeCountry cc, int year, CommodityType commodity) {

		if (!ModelConfig.CHANGE_DEMAND_YEAR)
			year = ModelConfig.BASE_YEAR;

		double bioenergyDemand = 0.0;
		for (SingleCountry c : compositeCountryManager.getAllForCompositeCountry(cc)) {
			
				double newDemand = bioenergyDemandManager.getFirstGenBioenergyDemand(c, year, commodity);
				bioenergyDemand += newDemand; // aggregate if composite country
		}
		return bioenergyDemand;
	}
	
	public Map<CommodityType, Map<CropType, Double>> getBaseDemandFracts(CompositeCountry cc) {
		return cerealFractionsManager.getBaseDemandFracts(cc);
	}
	
	public double getSecondGenBioenergyDemand(CompositeCountry cc, int year) {
		if (!ModelConfig.CHANGE_DEMAND_YEAR)
			year = ModelConfig.BASE_YEAR;
		
		double bioenergyDemand = 0.0;
		for (SingleCountry c : compositeCountryManager.getAllForCompositeCountry(cc)) {
			
				double newDemand = bioenergyDemandManager.getSecondGenBioenergyDemand(c, year);
				bioenergyDemand += newDemand; // aggregate if composite country
		}
		LogWriter.println(cc.getName() + " Gen2 bioenergy demand = " + bioenergyDemand);
	
		return bioenergyDemand;
	}

}