Logistic Growth

This tutorial translates a logistic growth equation into a Cropbox specification, configures a scenario, runs the model, and examines the result. It develops the model introduced in the Quick Start. The next tutorial adds weather-driven phenology.

Before starting, add the packages used below to your active Julia environment:

using Pkg
Pkg.add(["Cropbox", "DataFrames"])

Run the code blocks in order in the same Julia session.

Start from the process equation

Let whole-plant biomass $W$ follow logistic growth:

\[\frac{dW}{dt} = rW\left(1 - \frac{W}{W_f}\right), \qquad W(0) = W_0.\]

Before writing code, identify the role and behavior of every symbol.

SymbolMeaningCropbox role
$r$relative growth rateconfigurable value preserved during a run
$W_f$potential final biomassconfigurable value preserved during a run
$W_0$initial biomassconfigurable initial value
$W$current biomassstate accumulated from its growth rate

This semantic inventory is the bridge between an equation and the model DSL.

Specify the model

using Cropbox
using DataFrames

@system LogisticGrowth(Controller) begin
    r: growth_rate      => 0.05 ~ preserve(parameter, u"d^-1")
    Wf: final_biomass   => 300  ~ preserve(parameter, u"g")
    W0: initial_biomass => 0.25 ~ preserve(parameter, u"g")

    W(W, r, Wf): biomass => r * W * (1 - W / Wf) ~ accumulate(init = W0, u"g")

    t(context.clock.time): time                  ~ track(u"d")
end
Main.LogisticGrowth

The growth equation is the same as in the Quick Start. This version adds t to expose time in days as a named output. For a walkthrough of the declaration syntax, see How Cropbox Works.

Inspect the specification

Check the configurable surface before building a scenario.

parameters(LogisticGrowth)

Config for 1 system:

LogisticGrowth
r=0.05 d^-1
Wf=300 g
W0=0.25 g

Use look for the complete declaration or one variable.

look(LogisticGrowth, :W)
[doc]

[code]
  W(W, r, Wf):biomass => r * W * (1 - W / Wf) ~ accumulate(init = W0, u"g")

Configure a scenario

Defaults make the model runnable, but an external configuration keeps the scientific specification separate from a particular experiment.

config = @config Clock => :step => 1u"d"

Config for 1 system:

Clock
step=24 hr

The model parameters keep their declared defaults. This configuration changes only the clock step. System type keys allow Cropbox to validate parameter names and units when you change model parameters in a later scenario.

Run the simulation

Request an explicit index and target so the output contract is clear.

result = simulate(LogisticGrowth;
    config,
    stop = 300u"d",
    index = :t,
    target = :W,
)

first(result, 5)
5×2 DataFrame
RowtW
Quantity…Quantity…
10.0 d0.25 g
21.0 d0.26249 g
32.0 d0.275603 g
43.0 d0.28937 g
54.0 d0.303825 g

The model updates at Clock.step. The returned DataFrame includes the initial state and subsequent daily snapshots through the stop time.

Compare with observations

Evaluation uses the same specification and configuration. These illustrative values stand in for measured biomass; they are not experimental observations.

observations = DataFrame(
    t = [0, 100, 200, 300]u"d",
    W = [0.25, 33, 284, 300]u"g",
)

visualize(observations, LogisticGrowth, :t, :W;
    config,
    stop = 300u"d",
    name = "illustrative data",
    names = ["model"],
    xlab = "Time",
    ylab = "Biomass",
)
Example block output

The points show the supplied data and the line shows the simulated trajectory. Use the same time index and biomass variable to calculate the root mean squared error (RMSE):

evaluate(LogisticGrowth, observations;
    config,
    index = :t,
    target = :W,
    stop = 300u"d",
    metric = :rmse,
)
1.696962000939863 g

calibrate extends this step by searching bounded parameter ranges. Keep the data used for calibration separate from independent validation data; see Evaluate and Calibrate Models for the full workflow.

Optional: inspect the dependency graph

The graph below exposes scheduling details. It is useful for investigating dependencies, but reading its generated stages is not a prerequisite for building the model above. Inspection and Inspect and Visualize Models provide the related lookup tools. If you installed Cropbox v0.4.2, skip this optional section: the structure-plot forms of visualize were added after that release.

Show the generated dependency graph

visualize exposes the graph Cropbox uses to order declarations and update stages, without running a simulation:

d = visualize(LogisticGrowth)
Example block output

The terminal representation gives the generated update order:

println(repr(MIME("text/plain"), d))
Dependency update order: [config, r, Wf, W0, ∘context, context, W, ⋆W, t, ⋆context]

Generated stages such as ∘context, ⋆context, and ⋆W remain visible because this is the actual scheduling graph rather than a redrawn scientific summary. The two rectangles W and ⋆W refer to phases of one accumulating variable, not two biomass pools. See Model structure for the shape legend, focused selections, and image export.

Continue

Next, follow Weather-driven Phenology to add calendar time and weather input. For questions about this example: