# GreenLight Model: A Greenhouse Simulation Tool

GreenLight is a Python package for simulating greenhouse environments, adapted from the original MATLAB version developed by David Katzin. The original MATLAB version can be found here (opens in a new tab). The main purpose of GreenLight is to provide researchers and greenhouse managers with a tool to better understand and optimize various performance metrics of a greenhouse, such as energy consumption, light distribution, and crop yield.

In this document, we will walk through how to configure, set up, and run the GreenLight model for basic greenhouse simulations.

## Features

A notable feature of the GreenLight Python version is its high execution speed over short time intervals for greenhouse time settings (e.g., 5 minutes, 1 hour, or 1 day). In fact, the Python version runs 5 to 10 times faster than the MATLAB version. Moreover, it may be up to 100 times faster compared to other Python interfaces. This speed advantage makes GreenLight an ideal choice for applications that require rapid simulation feedback, such as reinforcement learning.

## Initial Configuration

Before starting simulations, we first need to configure the basic parameters of the model. This includes defining the length of the growing season, the start date of the season, and the simulated climate conditions.

```
from greenlight import (
GreenLightModel,
extract_last_value_from_nested_dict,
calculate_energy_consumption,
plot_green_light,
)
# Length of the growth cycle, days, can be set as a fraction
season_length = 2
# The time interval for each model run, days, can be set as a fraction, 1/24/4 represents 15 minutes
season_interval = season_length
# Date of the first day of the growth cycle
first_day = 153
# The simulated location of growth, representing different climates
weatherInput = "ams"
```

## Setting Up the Model

Next, we will initialize the state of the greenhouse and create an instance of the GreenLight model. Here, we will define some key parameters of the greenhouse, such as its structure and environmental settings:

```
# Create an instance of GreenLight
model = GreenLightModel(weatherInput=weatherInput, first_day=first_day, isMature=True)
# Initialize the state of the greenhouse, more parameters can be set here
init_state = {
"p": {
# Greenhouse structure settings
"psi": 22, # Mean greenhouse cover slope [degrees]
"aFlr": 4e4, # Floor area [m^{2}]
"tSpDay": 19.0, # Temperature set point light period [°C]
"tSpNight": 11.5, # Temperature set point dark period [°C]
"co2SpDay": 1000, # CO2 setpoint during the light period [ppm]
}
}
```

## Running the Model

With the initial settings in place, we can run the model. The model can be run for a full growth cycle at once, or iteratively in multiple shorter intervals:

```
# Run the model based on the growth cycle and time interval, season_length = season_interval means the simulation process runs only once, i.e., the interval is the entire growth cycle
for current_step in range(int(season_length // season_interval)):
gl = model.run_model(init_state, season_length, season_interval, current_step)
init_state = gl
```

This code will iteratively run the model throughout the entire growth cycle, using the results of the previous iteration as the initial state for the next.

## Result Analysis

After the simulation is complete, we can calculate the total fruit yield and energy consumption for the entire growth cycle based on the simulation results:

```
dmc = 0.06 # Fruit dry matter content
total_yield = 1e-6 * calculate_energy_consumption(gl, "mcFruitHar") / dmc # Calculate total fruit yield, kg/m2
lampIn = 1e-6 * calculate_energy_consumption(gl, "qLampIn", "qIntLampIn") # Calculate lighting energy consumption, MJ/m2
boilIn = 1e-6 * calculate_energy_consumption(gl, "hBoilPipe", "hBoilGroPipe") # Calculate heating energy consumption, MJ/m2
print(f"Total yield is {total_yield} kg/m2, lighting energy consumption is {lampIn} MJ/m2, heating energy consumption is {boilIn} MJ/m2")
```

# Complete Code

Below is the complete code for conducting a greenhouse simulation using the GreenLight model:

```
from greenlight import (
GreenLightModel,
extract_last_value_from_nested_dict,
calculate_energy_consumption,
plot_green_light,
)
season_length = 2
season_interval = season_length
first_day = 153
weatherInput = "ams"
model = GreenLightModel(weatherInput=weatherInput, first_day=first_day, isMature=True)
init_state = {
"p": {
"psi": 22,
"aFlr": 4e4,
"tSpDay": 19.0,
"tSpNight": 11.5,
"co2SpDay": 1000,
}
}
for current_step in range(int(season_length // season_interval)):
gl = model.run_model(init_state, season_length, season_interval, current_step)
init_state = gl
dmc = 0.06
total_yield = 1e-6 * calculate_energy_consumption(gl, "mcFruitHar") / dmc
lampIn = 1e-6 * calculate_energy_consumption(gl, "qLampIn", "qIntLampIn")
boilIn = 1e-6 * calculate_energy_consumption(gl, "hBoilPipe", "hBoilGroPipe")
print(f"总产量为{total_yield} kg/m2，照明能量消耗为{lampIn} MJ/m2，加热能量消耗为{boilIn} MJ/m2")
```