这段代码使用 Jupyter Notebook 的魔法命令 %%writefile
将代码写入一个名为 gddmodel.py
的 Python 文件中。该文件定义了一个简单的模型,用于累积作物生长周期内的生长度日(Growing Degree Days, GDD)。以下是对代码的详细解释:
# Here we import some components from PCSE that are building blocks for# any model in PCSE.from pcse.base import SimulationObject, StatesTemplate, RatesTemplate, ParamTemplatefrom pcse.traitlets import Float
SimulationObject
:所有 PCSE 模型的基本类。
StatesTemplate
:用于定义状态变量的模板。
RatesTemplate
:用于定义变化率变量的模板。
ParamTemplate
:用于定义参数的模板。
Float
:用于定义浮点数类型的参数。
# Models in PCSE always inherit from SimulationObjectclassGrowingDegreeDayModel(SimulationObject):"""A simple model to accumulate growing degree days between start and end of the crop cycle. """
GrowingDegreeDayModel
类继承自 SimulationObject
,这是所有 PCSE 模型的基本要求。
# This defines the model parameters. It inherits from ParamTemplate, this ensures that parameters# receive their value and it signals when parameters are missing.classParameters(ParamTemplate): BaseTemperature = Float
Parameters
类继承自 ParamTemplate
,用于定义模型的参数。
BaseTemperature
:定义了基温,这是一个浮点数类型的参数。
# State variables always inherit from StatesTemplate. StatesTemplate provides certain behaviour# such as that state variables must be initialized.classStateVariables(StatesTemplate): GDD = Float
StateVariables
类继承自 StatesTemplate
,用于定义模型的状态变量。
GDD
:累积的生长度日,这是一个浮点数类型的状态变量。
# Rate variables are somewhat similar to states but are initialized automatically to zero.classRateVariables(RatesTemplate): rGDD = Float
RateVariables
类继承自 RatesTemplate
,用于定义模型的变化率变量。
rGDD
:每日生长度日的变化率,这是一个浮点数类型的变化率变量。
definitialize(self, day, kiosk, parameters):"""Initialize runs only once, when the model is started. It always has three variables: - day: the day when the model starts - kiosk: the VariableKiosk which is an object which is shared between model components - parameters: an object providing the model parameters """# The code below initializes the parameters, state and rate variables.# Note that the initial value of state variable (here: GDD) must be provided. self.params = self.Parameters(parameters) self.states = self.StateVariables(kiosk, GDD=0.0) self.rates = self.RateVariables(kiosk)
initialize
方法在模型启动时仅运行一次。
day
:模型开始的日期。
kiosk
:变量共享对象,用于在模型组件之间共享变量。
parameters
:提供模型参数的对象。
初始化参数、状态变量和变化率变量。注意,状态变量 GDD
的初始值必须提供。
defcalc_rates(self, day, drv):"""calc_rates computes the rate of change that occur during the current day. It always has two variables: - the current day (a date) - the driving variables `drv` that contain the meteorological inputs """# Here we compute the increase in GDD and assign it to rate variable rGDD self.rates.rGDD =max(0.0, drv.TEMP - self.params.BaseTemperature)
calc_rates
方法计算当前天的变化率。
day
:当前日期。
drv
:包含气象输入的驱动变量。
计算每日生长度日的变化率 rGDD
,公式为 max(0.0, drv.TEMP - self.params.BaseTemperature)
,确保结果非负。
defintegrate(self, day, delt):"""This performs the integration of rates of change onto the states. It always has two variables: - the current day - the time step, which is fixed at 1.0 day """# Here we add rGDD multiplied by delt to GDD.# The multiplication with `delt` is mostly for educational purposes (since delt=1.0) although# this may change in the future. self.states.GDD += self.rates.rGDD * delt
integrate
方法将变化率积分到状态变量上。
day
:当前日期。
delt
:时间步长,固定为 1.0 天。
将变化率 rGDD
乘以时间步长 delt
并加到状态变量 GDD
上。这里的乘法主要是为了教育目的(因为 delt
固定为 1.0),但将来可能会有所变化。
这个 GrowingDegreeDayModel
类定义了一个简单的模型,用于累积作物生长周期内的生长度日(GDD)。模型通过继承 SimulationObject
类,定义了参数、状态变量和变化率变量,并实现了初始化、计算变化率和积分变化率的方法。通过这些方法,模型可以在每一天更新累积的生长度日。