CO2 in the atmosphere

Created by kenwebb on June 14, 2010. Votes: 17 Views: 428 Vote for this app. Tags: climatechange

Once the app is launched with Java Web Start, open it by selecting File --> Open.

The December 2009 issue of National Geographic had a short article on The Carbon Bathtub. It describes a simple model of carbon dioxide (CO2) in the Earth's atmosphere, by analogy with the flow of water into and out of a bathtub.

Main carbon bathtub class

Created by kenwebb on June 14, 2010. Votes: 12 Vote for this subtree. Tags: java

This is the main Java class in the carbon bathtub app.

<CarbonBathtub implName="lang:beanshell:inline:"><![CDATA[
package org.primordion.user.app.Chameleon.CarbonBathtub;

import org.primordion.xholon.app.Application;
import org.primordion.xholon.base.IMessage;
import org.primordion.xholon.base.Xholon;

public class CarbonBathtub extends Xholon {
  
  private boolean acting = false;
  
  private int startYear = 2008;
  private int endYear   = 2070;
  private int currentYear = 0;
  
  public void act() {
    if (acting) {
      currentYear++;
      if (currentYear == endYear) {
        ((Application)getApp()).invokeDataPlotter();
        ((Application)getApp()).setUseDataPlotter("none");
        acting = false;
      }
      super.act();
    }
  }
  
  private static final String start = "Start";
  
  private String[] actions = {start};
  
  public String[] getActionList() {
    return actions;
  }
  
  public void setActionList(String[] actionList) {
    actions = actionList;
  }
  
  public void doAction(String action) {
    if (start.equals(action)) {
      start();
    }
  }
  
  public void processReceivedMessage(IMessage msg) {
    doAction((String)msg.getData());
  }
  
  protected void start() {
    if (!acting) {
      if (currentYear == endYear) {
        // this is a restart
        getFirstChild().reconfigure();
      }
      currentYear = startYear;
      getApp().setTimeStep(startYear);
      generateDataPlotter();
      acting = true;
    }
  }

  protected void generateDataPlotter() {
    Application app = (Application)getApp();
    app.setUseDataPlotter("JFreeChart"); // or "google"
    app.setDataPlotterParams("CO2 in the Atmosphere,Year,MTons CO2,./statistics/,stats,1,WRITE_AS_LONG");
    app.createChart();
  }
  
  public boolean isActing() {
    return acting;
  }

  public void setActing(boolean acting) {
    this.acting = acting;
  }

  public int getStartYear() {
    return startYear;
  }

  public void setStartYear(int startYear) {
    this.startYear = startYear;
  }

  public int getEndYear() {
    return endYear;
  }

  public void setEndYear(int endYear) {
    this.endYear = endYear;
  }

  public int getCurrentYear() {
    return currentYear;
  }

  public void setCurrentYear(int currentYear) {
    this.currentYear = currentYear;
  }
  
}
new CarbonBathtub();
]]></CarbonBathtub>

Atmosphere

Created by kenwebb on June 14, 2010. Votes: 12 Vote for this subtree. Tags: java

The amount of CO2 in the atmosphere is maintained in this class.

<Atmosphere implName="lang:beanshell:inline:"><![CDATA[
import org.primordion.xholon.base.Xholon;
import org.primordion.xholon.base.IXholonClass;

public class Atmosphere extends Xholon
{
  private static final double INITIAL_CARBON_MTONS = 820000000000.0;
  private double carbonMTons = INITIAL_CARBON_MTONS; // 2008 average
  
  public void reconfigure() {
    carbonMTons = INITIAL_CARBON_MTONS;
    super.reconfigure();
  }
  
  public double getVal() {
    return carbonMTons;
  }
  
  public void setVal(Double val) {
    this.carbonMTons = val;
  }
  
  public int getXhType() {
    return IXholonClass.XhtypePurePassiveObject;
  }
  
}
new Atmosphere();
]]></Atmosphere>

All in one

Created by kenwebb on June 14, 2010. Votes: 12 Vote for this subtree. Tags: java

These are all the children of the CarbonBathtub node, combined into one subtree. If you select this subtree, you do NOT need to separately copy and paste the Atmosphere subtree.

<_-.carbonbathtub>

<Atmosphere implName="lang:beanshell:inline:"><![CDATA[
import org.primordion.xholon.base.Xholon;
import org.primordion.xholon.base.IXholonClass;

public class Atmosphere extends Xholon
{
  private static final double INITIAL_CARBON_MTONS = 820000000000.0;
  private double carbonMTons = INITIAL_CARBON_MTONS; // 2008 average
  
  public void reconfigure() {
    carbonMTons = INITIAL_CARBON_MTONS;
    super.reconfigure();
  }
  
  public double getVal() {
    return carbonMTons;
  }
  
  public void setVal(Double val) {
    this.carbonMTons = val;
  }
  
  public int getXhType() {
    return IXholonClass.XhtypePurePassiveObject;
  }
  
}
new Atmosphere();
]]></Atmosphere>

<InAllHumanMadeCo2 implName="lang:beanshell:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class InAllHumanMadeCo2 extends Xholon {
  
  private static final double INITIAL_CARBON_PERYEAR = 9100000000.0;
  private double carbonPerYear = INITIAL_CARBON_PERYEAR; // rate
  private double rateChange    = 1.02; // 1.01 is +1%, 1.00 is steady, 0.99 is -1%
  private IXholon atmosphere = null;
  
  public void reconfigure() {
    carbonPerYear = INITIAL_CARBON_PERYEAR;
    rateChange -= 0.01;
    super.reconfigure();
  }
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() + carbonPerYear;
    carbonPerYear *= rateChange;
    atmosphere.setVal(new Double(newVal));
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
  public double getRateChange() {return rateChange;}
  public void setRateChange(double rateChange) {this.rateChange = rateChange;}
  
}
new InAllHumanMadeCo2();
]]></InAllHumanMadeCo2>

<OutAbsorbedByPlantsAndSoils implName="lang:beanshell:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class OutAbsorbedByPlantsAndSoils extends Xholon {
  
  private double carbonPerYear = 9100000000.0 * 0.3;
  private IXholon atmosphere = null;
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() - carbonPerYear;
    atmosphere.setVal(new Double(newVal));
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
}
new OutAbsorbedByPlantsAndSoils();
]]></OutAbsorbedByPlantsAndSoils>

<OutAbsorbedByOceans implName="lang:beanshell:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class OutAbsorbedByOceans extends Xholon {
  
  private double carbonPerYear = 9100000000.0 * 0.25;
  private IXholon atmosphere = null;
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() - carbonPerYear;
    atmosphere.setVal(new Double(newVal));
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
}
new OutAbsorbedByOceans();
]]></OutAbsorbedByOceans>

<OutAbsorbedBySedimentsAndRocks implName="lang:beanshell:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class OutAbsorbedBySedimentsAndRocks extends Xholon {
  
  private double carbonPerYear = 9100000000.0 * 0.009;
  private IXholon atmosphere = null;
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() - carbonPerYear;
    atmosphere.setVal(new Double(newVal));
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
}
new OutAbsorbedBySedimentsAndRocks();
]]></OutAbsorbedBySedimentsAndRocks>

</_-.carbonbathtub>