User Tools

Site Tools


tutorials:radiation-model-in-crop_model2

Plotting the absorbed light in a chart

Open the file lightchart.gsz. We now want to see model output as a chart (as you might know from R). The total light intercepted by all leaves is output into a chart, and this at every step of the simulation.

For this, we have to declare a kind of data table in which the output is stored:

//insert a table here for the output:
const DatasetRef lightdata = new DatasetRef("Light intercepted by canopy");

We then have to initiate the chart in the init() method. For this, we will use the new method initChart() that will be defined later:

protected void init()
[
	//the chart is initialized here: 
	{
		initChart();
	}
 
	Axiom ==> [Plane().(setShader(WHITE))] 
		Bud(1, PHYLLOCHRON, 1);
	==>> ^ M(50) RU(180) MyLight;
]

Note that if you want to declare imperative code in a method that also contains rules (like the present init() method), then you have to put it into a pair of curly braces, { }. Also, we want to update the chart at every step, so we add a new line to the grow() method:

public void grow ()
{
	run();
	lm.compute();
	absorb();
	//update the chart here
	updateChart();
}

The latter is another new method, updateChart(), that we still need to define. The two new methods are defined, first initChart():

//definition of the method initChart(): 
protected void initChart()
{
	lightdata.clear();
	chart(lightdata, XY_PLOT);
}

This method does something with the data table lightdata, in fact it empties it at each new run, using the method clear()(L. 112). The chart command does the actual plotting: It creates an XY plot with the data table, using as x the time (simulation steps) and as y the sum of light absorbed by all leaves. The table is filled up with data by the updateChart() method:

//definition of the method updateChart(): 
protected void updateChart()
{
	lightdata.addRow().set(0, sum((* Leaf *)[al]));
}

The addRow() method of DatasetRef does the actual job, more specifically its sub-method set(), which adds the data into the 0th column of the table: The data are produced by the sum method, which searches for all leaves (* Leaf *) that are already produced and then sums up their parameter al. Searching for objects in the graph is a very powerful option in GroIMP. You can also do this at any time of the simulation by typing search commands in the XL Console, for instance the following command:

will search for all objects Leaf in the scene/graph (you have to write “Example1.Leaf” to indicate that the module was defined within the class “Example1.rgg”) the value of the parameter al and calculate the sum of it. If you omit Example1, the module Leaf cannot be found and you get an error message instead:

You can also have a look at the data table itself:

Main Menu: Panels → Explorers → Datasets

This will give you a new window:

When you double-click on the Dataset “- Light intercepted…” you get the actual table, with two columns, # and A, where the first column contains the steps, the second the sum of light intercepted by the leaves:

tutorials/radiation-model-in-crop_model2.txt · Last modified: 2025/05/27 17:53 by barley1965