Carbon Cycle Simulator File & Code
carbon_cycle.html
—
HTML,
7Kb
File contents
<html>
<!--
Documentation for the LineChart API is here:
http://code.google.com/apis/visualization/documentation/gallery/linechart.html
-->
<head>
<title>Carbon Exchange Cycle</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
var Parameters =
{
runtime: 20, // length of simulation in years
year: 2010, // starting year
temperature: 14, // global average temperature (Celsius)
temperatureAdjustmentDelay: 10, // delay in years taken for temperature to adjust to changes caused by Radiative Forcing
atmosphericCO2: 824, // 824 Gt (billion tonnes)
emissionsCO2: 7.5, // net rate of 7.5 Gt/year of CO2 put into the atmosphere by human activity
climateSensitivity: 1.46, // parameter to Radiative Forcing equation (see wikipedia "Radiative Forcing")
oceanSourcedCO2: 90, // 90 Gt/year from oceans
oceanSinkedCO2: 92, // 92 Gt/year from oceans
landSourcedCO2: 120, // 120 Gt/year from land
landSinkedCO2: 120, // 120 Gt/year from land
mitigationStartTime: 10, // number of years into simulation at which point mitigation efforts begin
reducedEmissionsCO2: 3, // mitigated net rate of 3 Gt/year of CO2 put into the atmosphere by human activity
artificialSinkedCO2: 0 // eg from carbon air capture or geo-engineering efforts
};
google.load("visualization", "1", {packages:["linechart"]});
google.setOnLoadCallback(onReady);
function onReady()
{
// init fields from param defaults
for(var p in Parameters)
{
var field = document.getElementById('text_' + p);
if(field)
field.value = Parameters[p];
}
runSimulation();
document.getElementById('buttonPlot').disabled = false;
}
function onPlot()
{
// update params from fields
for(var p in Parameters)
{
var field = document.getElementById('text_' + p);
if(field)
Parameters[p] = parseFloat(field.value);
}
runSimulation();
}
function runSimulation() {
function co2Sources(t) { return oceanSources(t) + landSources(t) + co2Emissions(t); }
function co2Sinks(t) { return oceanSinks(t) + landSinks(t) + artificialSinks(t); }
function oceanSources(t) { return Parameters.oceanSourcedCO2; }
function oceanSinks(t) { return Parameters.oceanSinkedCO2; }
function landSources(t) { return Parameters.landSourcedCO2; }
function landSinks(t) { return Parameters.landSinkedCO2; }
function co2Emissions(t) { return t < Parameters.mitigationStartTime ? Parameters.emissionsCO2 : Parameters.reducedEmissionsCO2; }
function artificialSinks(t) { return t < Parameters.mitigationStartTime ? 0 : Parameters.artificialSinkedCO2; }
// compute temp difference due to Radiative Forcing (from wikipedia "Radiative Forcing")
function tempChange(co2) { return Parameters.climateSensitivity * 5.35 * Math.log(co2/Parameters.atmosphericCO2); }
function smooth(func, t, delay) {
// compute a moving average of func, over the range of the delay
var values = [];
for(var i = t; i >= 0 && values.length <= delay; i--)
values.push(func(i));
return avg(values);
}
var years = [Parameters.year];
var co2s = [Parameters.atmosphericCO2];
var temps = [Parameters.temperature];
var dtemps = [0.0];
for(var t=1; t < Parameters.runtime; t++)
{
years[t] = years[t-1] + 1;
var dco2 = co2Sources(t) - co2Sinks(t);
co2s[t] = co2s[t-1] + dco2;
dtemps[t] = tempChange(co2s[t]);
// temperature does not "accumulate" - is always computed as offset from the starting temperature
temps[t] = Parameters.temperature + smooth(function(t) { return dtemps[t]; }, t, Parameters.temperatureAdjustmentDelay);
}
plot(
document.getElementById('co2_div'),
years,
co2s,
{ title: "Atmospheric CO2", xLabel: "year", yLabel: "co2 (Gt)" }
);
plot(
document.getElementById('temp_div'),
years,
temps,
{ title: "Temperature", xLabel: "year", yLabel: "temp (C)" }
);
}
/*
divContainer - HTML div element to hold the graph
xSeries - array of data for x axis
ySeries - array of data for y axis
options -
xLabel - label for x axis
yLabel - label for y axis
other options as defined by linechart documentation (http://code.google.com/apis/visualization/documentation/gallery/linechart.html)
*/
function plot(divContainer, xSeries, ySeries, options)
{
var data = new google.visualization.DataTable();
// set some default options if not specified
options.width = options.width || 400;
options.height = options.height || 240;
options.titleX = options.titleX || options.xLabel;
options.titleY = options.titleY || options.yLabel;
options.legend = options.legend || 'none';
// add a column for each data label
data.addColumn('string', options.xLabel);
data.addColumn('number', options.yLabel);
// set number of rows
data.addRows(xSeries.length);
// populate the table
for(var i = 0; i < xSeries.length; i++)
{
data.setValue(i, 0, xSeries[i].toString());
data.setValue(i, 1, ySeries[i]);
}
// draw the chart
var chart = new google.visualization.LineChart(divContainer);
chart.draw(data, options);
}
function avg(values) {
var sum = 0;
for(var i = 0; i < values.length; i++)
sum += values[i];
return sum/values.length;
}
</script>
</head>
<body>
<p><h1>
Carbon Exchange Cycle simulation
</h1></p>
<table>
<tr>
<td>Runtime (years):</td>
<td><input id="text_runtime" type="text" size="2"/></td>
<td>length of simulation in years</td>
</tr>
<tr>
<td>Natural CO2 land sources (Gt/yr):</td>
<td><input id="text_landSourcedCO2" type="text" size="2"/></td>
<td>net rate of CO2 naturally sourced into the atmosphere from land</td>
</tr>
<tr>
<td>Natural CO2 land sinks (Gt/yr):</td>
<td><input id="text_landSinkedCO2" type="text" size="2"/></td>
<td>net rate of CO2 naturally sinked into land</td>
</tr>
<tr>
<td>Natural CO2 ocean sources (Gt/yr):</td>
<td><input id="text_oceanSourcedCO2" type="text" size="2"/></td>
<td>net rate of CO2 naturally sourced into the atmosphere from ocean</td>
</tr>
<tr>
<td>Natural CO2 ocean sinks (Gt/yr):</td>
<td><input id="text_oceanSinkedCO2" type="text" size="2"/></td>
<td>net rate of CO2 naturally sinked into ocean</td>
</tr>
<tr>
<td>CO2 emissions (Gt/yr):</td>
<td><input id="text_emissionsCO2" type="text" size="2"/></td>
<td>net rate of CO2 put into the atmosphere by human activity</td>
</tr>
<tr>
<td>Mitigation efforts start time (year, 0 - runtime):</td>
<td><input id="text_mitigationStartTime" type="text" size="2"/></td>
<td>year at which mitigation efforts begin</td>
</tr>
<tr>
<td>Reduced CO2 emissions (Gt/yr):</td>
<td><input id="text_reducedEmissionsCO2" type="text" size="2"/></td>
<td>net rate of CO2 put into the atmosphere by human activity, after reduction efforts implemented</td>
</tr>
<tr>
<td>Artificial CO2 sinks (carbon air capture, geo-engineering) (Gt/yr):</td>
<td><input id="text_artificialSinkedCO2" type="text" size="2"/></td>
<td>net rate of CO2 removed from atmosphere by artifical means</td>
</tr>
<tr>
<td>Temperature Adjustment Delay (years):</td>
<td><input id="text_temperatureAdjustmentDelay" type="text" size="2"/></td>
<td>delay in years for temperature to adjust to changes caused by CO2 concentration</td>
</tr>
<tr><td>
<input id="buttonPlot" type="button" value="Plot" onclick="javascript:onPlot()" disabled="true"/>
</td></tr>
</table>
<span id="co2_div"></span>
<span id="temp_div"></span>
</body>
</html>
