Crop modules
diffwofost.physical_models.crop.phenology.DVS_Phenology
Bases: SimulationObject
Implements the algorithms for phenologic development in WOFOST.
Phenologic development in WOFOST is expresses using a unitless scale which takes the values 0 at emergence, 1 at Anthesis (flowering) and 2 at maturity. This type of phenological development is mainly representative for cereal crops. All other crops that are simulated with WOFOST are forced into this scheme as well, although this may not be appropriate for all crops. For example, for potatoes development stage 1 represents the start of tuber formation rather than flowering.
Phenological development is mainly governed by temperature and can be modified by the effects of day length and vernalization during the period before Anthesis. After Anthesis, only temperature influences the development rate.
Simulation parameters
| Name | Description | Type | Unit |
|---|---|---|---|
| TSUMEM | Temperature sum from sowing to emergence | SCr | |
| TBASEM | Base temperature for emergence | SCr | |
| TEFFMX | Maximum effective temperature for emergence | SCr | |
| TSUM1 | Temperature sum from emergence to anthesis | SCr | |
| TSUM2 | Temperature sum from anthesis to maturity | SCr | |
| IDSL | Switch for development options: temp only (0), +daylength | SCr | - |
| (1), +vernalization (>=2) | |||
| DLO | Optimal daylength for phenological development | SCr | hr |
| DLC | Critical daylength for phenological development | SCr | hr |
| DVSI | Initial development stage at emergence (may be >0 for | SCr | - |
| transplanted crops) | |||
| DVSEND | Final development stage | SCr | - |
| DTSMTB | Daily increase in temperature sum as a function of daily | TCr | |
| mean temperature |
State variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| DVS | Development stage | Y | - |
| TSUM | Temperature sum | N | |
| TSUME | Temperature sum for emergence | N | |
| DOS | Day of sowing | N | - |
| DOE | Day of emergence | N | - |
| DOA | Day of Anthesis | N | - |
| DOM | Day of maturity | N | - |
| DOH | Day of harvest | N | - |
| STAGE | Current stage (emerging|vegetative|reproductive|mature) |
N | - |
Rate variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| DTSUME | Increase in temperature sum for emergence | N | |
| DTSUM | Increase in temperature sum for anthesis or maturity | N | |
| DVR | Development rate | Y |
External dependencies:
None
Signals sent or handled
DVS_Phenology sends the crop_finish signal when maturity is
reached and the end_type is 'maturity' or 'earliest'.
Gradient mapping (which parameters have a gradient):
| Output | Parameters influencing it |
|---|---|
| DVS | ... |
| TSUM | ... |
[!NOTE] Notice that the gradient ∂DVS/∂TEFFMX is zero.
[!NOTE] The parameter IDSL it is not differentiable since it is a switch.
Methods:
-
initialize–:param day: start date of the simulation
-
calc_rates–Compute daily phenological development rates.
-
integrate–Integrate phenology states and manage stage transitions.
-
get_variable–Return the value of the specified state or rate variable.
Attributes:
initialize
:param day: start date of the simulation
:param kiosk: variable kiosk of this PCSE instance
:param parvalues: ParameterProvider object providing parameters as
key/value pairs
Source code in src/diffwofost/physical_models/crop/phenology.py
calc_rates
Compute daily phenological development rates.
Parameters:
-
day(date) –Current simulation date.
-
drv–Meteorological driver object with at least TEMP and LAT.
Logic
- Photoperiod reduction (DVRED) if IDSL >= 1 using daylength.
- Vernalisation factor (VERNFAC) if IDSL >= 2 and in vegetative stage.
- Stage-specific:
- emerging: temperature sum for emergence (DTSUME), DVR via TSUMEM.
- vegetative: temperature sum (DTSUM) scaled by VERNFAC and DVRED.
- reproductive: temperature sum (DTSUM) only temperature-driven.
- mature: all rates zero.
Sets
r.DTSUME, r.DTSUM, r.DVR.
Raises:
-
PCSEError–If STAGE unrecognized.
Source code in src/diffwofost/physical_models/crop/phenology.py
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | |
integrate
Integrate phenology states and manage stage transitions.
Parameters:
-
day(date) –Current simulation day.
-
delt(float, default:1.0) –Timestep length in days (default 1.0).
Sequence
- Integrates vernalisation module if active and in vegetative stage.
- Accumulates TSUME, TSUM, advances DVS by DVR.
- Checks threshold crossings to move through stages: emerging -> vegetative (DVS >= 0) vegetative -> reproductive (DVS >= 1) reproductive -> mature (DVS >= DVSEND)
Side Effects
- Emits crop_emerged signal on emergence.
- Emits crop_finish signal at maturity if end type matches.
Notes
Caps DVS at stage boundary values.
Raises:
-
PCSEError–If STAGE undefined.
Source code in src/diffwofost/physical_models/crop/phenology.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
get_variable
Return the value of the specified state or rate variable.
:param varname: Name of the variable.
Note that the get_variable() will searches for varname exactly
as specified (case sensitive).
Source code in src/diffwofost/physical_models/crop/phenology.py
diffwofost.physical_models.crop.partitioning.DVS_Partitioning
Bases: _BaseDVSPartitioning
Class for assimilate partitioning based on development stage (DVS).
DVS_Partitioning calculates the partitioning of the assimilates to roots,
stems, leaves and storage organs using fixed partitioning tables as a
function of crop development stage. The available assimilates are first
split into below-ground and aboveground using the values in FRTB. In a
second stage they are split into leaves (FLTB), stems (FSTB) and storage
organs (FOTB).
Since the partitioning fractions are derived from the state variable DVS they are regarded state variables as well.
Simulation parameters (To be provided in cropdata dictionary):
| Name | Description | Type | Unit |
|---|---|---|---|
| FRTB | Partitioning to roots as a function of development stage | TCr | - |
| FSTB | Partitioning to stems as a function of development stage | TCr | - |
| FLTB | Partitioning to leaves as a function of development stage | TCr | - |
| FOTB | Partitioning to storage organs as a function of development stage | TCr | - |
State variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| FR | Fraction partitioned to roots | Y | - |
| FS | Fraction partitioned to stems | Y | - |
| FL | Fraction partitioned to leaves | Y | - |
| FO | Fraction partitioned to storage organs | Y | - |
| PF | Partitioning factors packed in tuple | N | - |
Rate variables
None
External dependencies:
| Name | Description | Provided by | Unit |
|---|---|---|---|
| DVS | Crop development stage | DVS_Phenology | - |
Outputs
| Name | Description | Pbl | Unit |
|---|---|---|---|
| FR | Fraction partitioned to roots | Y | - |
| FL | Fraction partitioned to leaves | Y | - |
| FS | Fraction partitioned to stems | Y | - |
| FO | Fraction partitioned to storage organs | Y | - |
Gradient mapping (which parameters have a gradient):
| Output | Parameters influencing it |
|---|---|
| FR | FRTB, DVS |
| FL | FLTB, DVS |
| FS | FSTB, DVS |
| FO | FOTB, DVS |
Exceptions raised
A PartitioningError is raised if the partitioning coefficients to leaves, stems and storage organs on a given day do not add up to 1.
Methods:
-
initialize–Initialize the DVS_Partitioning simulation object.
-
integrate–Update partitioning factors based on development stage (DVS).
-
calc_rates–Return partitioning factors based on current DVS.
initialize
Initialize the DVS_Partitioning simulation object.
Parameters:
-
day–Start date of the simulation.
-
kiosk(VariableKiosk) –Variable kiosk of this PCSE instance.
-
parvalues(ParameterProvider) –Object providing parameters as key/value pairs.
Source code in src/diffwofost/physical_models/crop/partitioning.py
integrate
calc_rates
Return partitioning factors based on current DVS.
Rate calculation does nothing for partitioning as it is a derived state.
diffwofost.physical_models.crop.assimilation.WOFOST72_Assimilation
Bases: SimulationObject
Class implementing a WOFOST/SUCROS style assimilation routine.
WOFOST calculates the daily gross CO2 assimilation rate of a crop from the absorbed radiation and the photosynthesis-light response curve of individual leaves. This response is dependent on temperature and leaf age. The absorbed radiation is calculated from the total incoming radiation and the leaf area. Daily gross CO2 assimilation is obtained by integrating the assimilation rates over the leaf layers and over the day.
Simulation parameters (provide in cropdata dictionary)
| Name | Description | Type | Unit |
|---|---|---|---|
| AMAXTB | Max. leaf CO2 assimilation rate as function of DVS | TCr | kg CO2 ha⁻¹ leaf h⁻¹ |
| EFFTB | Light use effic. single leaf as a function of daily mean temperature | TCr | kg CO2 ha⁻¹ h⁻¹ /(J m⁻² s⁻¹) |
| KDIFTB | Extinction coefficient for diffuse visible light as function of DVS | TCr | - |
| TMPFTB | Reduction factor on AMAX as function of daily mean temperature | TCr | - |
| TMNFTB | Reduction factor on AMAX as function of daily minimum temperature | TCr | - |
Rate variables
This class returns the potential gross assimilation rate 'PGASS'
directly from the __call__() method, but also includes it as a rate variable.
| Name | Description | Pbl | Unit |
|---|---|---|---|
| PGASS | Potential gross assimilation | Y | kg CH2O ha⁻¹ d⁻¹ |
External dependencies
| Name | Description | Provided by | Unit |
|---|---|---|---|
| DVS | Crop development stage | DVS_Phenology | - |
| LAI | Leaf area index | Leaf_dynamics | - |
Weather inputs used
| Name | Description | Unit |
|---|---|---|
| IRRAD | Daily shortwave radiation | J m⁻² d⁻¹ |
| DTEMP | Daily mean temperature | °C |
| TMIN | Daily minimum temperature | °C |
| LAT | Latitude | degrees |
Outputs
| Name | Description | Pbl | Unit |
|---|---|---|---|
| PGASS | Potential gross assimilation | Y | kg CH2O ha⁻¹ d⁻¹ |
Gradient mapping (which parameters have a gradient):
| Output | Parameters influencing it |
|---|---|
| PGASS | AMAXTB, EFFTB, KDIFTB, TMPFTB, TMNFTB |
Methods:
-
initialize–Initialize the assimilation module.
-
calc_rates–Compute the potential gross assimilation rate (PGASS).
-
__call__–Calculate and return the potential gross assimilation rate (PGASS).
-
integrate–No state variables to integrate for this module.
Attributes:
initialize
Initialize the assimilation module.
Source code in src/diffwofost/physical_models/crop/assimilation.py
calc_rates
Compute the potential gross assimilation rate (PGASS).
Source code in src/diffwofost/physical_models/crop/assimilation.py
__call__
Calculate and return the potential gross assimilation rate (PGASS).
integrate
diffwofost.physical_models.crop.leaf_dynamics.WOFOST_Leaf_Dynamics
Bases: SimulationObject
Leaf dynamics for the WOFOST crop model.
Implementation of biomass partitioning to leaves, growth and senenscence
of leaves. WOFOST keeps track of the biomass that has been partitioned to
the leaves for each day (variable LV), which is called a leaf class).
For each leaf class the leaf age (variable 'LVAGE') and specific leaf area
(variable SLA) are also registered. Total living leaf biomass is
calculated by summing the biomass values for all leaf classes. Similarly,
leaf area is calculated by summing leaf biomass times specific leaf area
(LV * SLA).
Senescense of the leaves can occur as a result of physiological age, drought stress or self-shading.
Simulation parameters (provide in cropdata dictionary)
| Name | Description | Type | Unit |
|---|---|---|---|
| RGRLAI | Maximum relative increase in LAI. | SCr | ha ha⁻¹ d⁻¹ |
| SPAN | Life span of leaves growing at 35 Celsius | SCr | d |
| TBASE | Lower threshold temp. for ageing of leaves | SCr | C |
| PERDL | Max. relative death rate of leaves due to water stress | SCr | |
| TDWI | Initial total crop dry weight | SCr | kg ha⁻¹ |
| KDIFTB | Extinction coefficient for diffuse visible light as function of DVS | TCr | |
| SLATB | Specific leaf area as a function of DVS | TCr | ha kg⁻¹ |
State variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| LV | Leaf biomass per leaf class | N | kg ha⁻¹ |
| SLA | Specific leaf area per leaf class | N | ha kg⁻¹ |
| LVAGE | Leaf age per leaf class | N | d |
| LVSUM | Sum of LV | N | kg ha⁻¹ |
| LAIEM | LAI at emergence | N | - |
| LASUM | Total leaf area as sum of LV*SLA, not including stem and pod area | N | - |
| LAIEXP | LAI value under theoretical exponential growth | N | - |
| LAIMAX | Maximum LAI reached during growth cycle | N | - |
| LAI | Leaf area index, including stem and pod area | Y | - |
| WLV | Dry weight of living leaves | Y | kg ha⁻¹ |
| DWLV | Dry weight of dead leaves | N | kg ha⁻¹ |
| TWLV | Dry weight of total leaves (living + dead) | Y | kg ha⁻¹ |
Rate variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| GRLV | Growth rate leaves | N | kg ha⁻¹ d⁻¹ |
| DSLV1 | Death rate leaves due to water stress | N | kg ha⁻¹ d⁻¹ |
| DSLV2 | Death rate leaves due to self-shading | N | kg ha⁻¹ d⁻¹ |
| DSLV3 | Death rate leaves due to frost kill | N | kg ha⁻¹ d⁻¹ |
| DSLV | Maximum of DSLV1, DSLV2, DSLV3 | N | kg ha⁻¹ d⁻¹ |
| DALV | Death rate leaves due to aging | N | kg ha⁻¹ d⁻¹ |
| DRLV | Death rate leaves as a combination of DSLV and DALV | N | kg ha⁻¹ d⁻¹ |
| SLAT | Specific leaf area for current time step, adjusted for source/sink limited leaf expansion rate | N | ha kg⁻¹ |
| FYSAGE | Increase in physiological leaf age | N | - |
| GLAIEX | Sink-limited leaf expansion rate (exponential curve) | N | ha ha⁻¹ d⁻¹ |
| GLASOL | Source-limited leaf expansion rate (biomass increase) | N | ha ha⁻¹ d⁻¹ |
External dependencies
| Name | Description | Provided by | Unit |
|---|---|---|---|
| DVS | Crop development stage | DVS_Phenology | - |
| FL | Fraction biomass to leaves | DVS_Partitioning | - |
| FR | Fraction biomass to roots | DVS_Partitioning | - |
| SAI | Stem area index | WOFOST_Stem_Dynamics | - |
| PAI | Pod area index | WOFOST_Storage_Organ_Dynamics | - |
| TRA | Transpiration rate | Evapotranspiration | cm day⁻¹ ? |
| TRAMX | Maximum transpiration rate | Evapotranspiration | cm day⁻¹ ? |
| ADMI | Above-ground dry matter increase | CropSimulation | kg ha⁻¹ d⁻¹ |
| RFTRA | Reduction factor for transpiration (water & oxygen) | Y | - |
| RF_FROST | Reduction factor frost kill | FROSTOL (optional) | - |
Outputs
| Name | Description | Pbl | Unit |
|---|---|---|---|
| LAI | Leaf area index, including stem and pod area | Y | - |
| TWLV | Dry weight of total leaves (living + dead) | Y | kg ha⁻¹ |
Gradient mapping (which parameters have a gradient):
| Output | Parameters influencing it |
|---|---|
| LAI | TDWI, SPAN, RGRLAI, TBASE, KDIFTB, SLATB |
| TWLV | TDWI, PERDL |
[!NOTE] Notice that the following gradients are zero: - ∂SPAN/∂TWLV - ∂PERDL/∂TWLV - ∂KDIFTB/∂LAI
Methods:
-
initialize–Initialize the WOFOST_Leaf_Dynamics simulation object.
-
calc_rates–Calculate the rates of change for the leaf dynamics.
-
integrate–Integrate the leaf dynamics state variables.
Attributes:
initialize
Initialize the WOFOST_Leaf_Dynamics simulation object.
Parameters:
-
day(date) –The starting date of the simulation.
-
kiosk(VariableKiosk) –A container for registering and publishing (internal and external) state variables. See PCSE documentation for details.
-
parvalues(ParameterProvider) –A dictionary-like container holding all parameter sets (crop, soil, site) as key/value. The values are arrays or scalars. See PCSE documentation for details.
Source code in src/diffwofost/physical_models/crop/leaf_dynamics.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
calc_rates
Calculate the rates of change for the leaf dynamics.
Parameters:
-
day(date) –The current date of the simulation.
-
drv(WeatherDataContainer) –A dictionary-like container holding weather data elements as key/value. The values are arrays or scalars. See PCSE documentation for details.
Source code in src/diffwofost/physical_models/crop/leaf_dynamics.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
integrate
Integrate the leaf dynamics state variables.
Parameters:
-
day(date) –The current date of the simulation.
-
delt(float, default:1.0) –The time step for integration. Defaults to 1.0.
Source code in src/diffwofost/physical_models/crop/leaf_dynamics.py
diffwofost.physical_models.crop.root_dynamics.WOFOST_Root_Dynamics
Bases: SimulationObject
Root biomass dynamics and rooting depth.
Root growth and root biomass dynamics in WOFOST are separate processes, with the only exception that root growth stops when no more biomass is sent to the root system.
Root biomass increase results from the assimilates partitioned to
the root system. Root death is defined as the current root biomass
multiplied by a relative death rate (RDRRTB). The latter as a function
of the development stage (DVS).
Increase in root depth is a simple linear expansion over time until the
maximum rooting depth (RDM) is reached.
Simulation parameters
| Name | Description | Type | Unit |
|---|---|---|---|
| RDI | Initial rooting depth | SCr | cm |
| RRI | Daily increase in rooting depth | SCr | cm day⁻¹ |
| RDMCR | Maximum rooting depth of the crop | SCR | cm |
| RDMSOL | Maximum rooting depth of the soil | SSo | cm |
| TDWI | Initial total crop dry weight | SCr | kg ha⁻¹ |
| IAIRDU | Presence of air ducts in the root (1) or not (0) | SCr | - |
| RDRRTB | Relative death rate of roots as a function of development stage | TCr | - |
State variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| RD | Current rooting depth | Y | cm |
| RDM | Maximum attainable rooting depth at the minimum of the soil and crop maximum rooting depth | N | cm |
| WRT | Weight of living roots | Y | kg ha⁻¹ |
| DWRT | Weight of dead roots | N | kg ha⁻¹ |
| TWRT | Total weight of roots | Y | kg ha⁻¹ |
Rate variables
| Name | Description | Pbl | Unit |
|---|---|---|---|
| RR | Growth rate root depth | N | cm |
| GRRT | Growth rate root biomass | N | kg ha⁻¹ d⁻¹ |
| DRRT | Death rate root biomass | N | kg ha⁻¹ d⁻¹ |
| GWRT | Net change in root biomass | N | kg ha⁻¹ d⁻¹ |
Signals send or handled
None
External dependencies:
| Name | Description | Provided by | Unit |
|---|---|---|---|
| DVS | Crop development stage | DVS_Phenology | - |
| DMI | Total dry matter increase | CropSimulation | kg ha⁻¹ d⁻¹ |
| FR | Fraction biomass to roots | DVS_Partitioning | - |
Outputs:
| Name | Description | Provided by | Unit |
|---|---|---|---|
| RD | Current rooting depth | Y | cm |
| TWRT | Total weight of roots | Y | kg ha⁻¹ |
Gradient mapping (which parameters have a gradient):
| Output | Parameters influencing it |
|---|---|
| RD | RDI, RRI, RDMCR, RDMSOL |
| TWRT | TDWI, RDRRTB |
[!NOTE] Notice that the gradient ∂TWRT/∂RDRRTB is zero.
IMPORTANT NOTICE
Currently root development is linear and depends only on the fraction of assimilates send to the roots (FR) and not on the amount of assimilates itself. This means that roots also grow through the winter when there is no assimilation due to low temperatures. There has been a discussion to change this behaviour and make root growth dependent on the assimilates send to the roots: so root growth stops when there are no assimilates available for growth.
Finally, we decided not to change the root model and keep the original WOFOST approach because of the following reasons: - A dry top layer in the soil could create a large drought stress that reduces the assimilates to zero. In this situation the roots would not grow if dependent on the assimilates, while water is available in the zone just below the root zone. Therefore a dependency on the amount of assimilates could create model instability in dry conditions (e.g. Southern-Mediterranean, etc.). - Other solutions to alleviate the problem above were explored: only put this limitation after a certain development stage, putting a dependency on soil moisture levels in the unrooted soil compartment. All these solutions were found to introduce arbitrary parameters that have no clear explanation. Therefore all proposed solutions were discarded.
We conclude that our current knowledge on root development is insufficient to propose a better and more biophysical approach to root development in WOFOST.
Methods:
-
initialize–Initialize the model.
-
calc_rates–Calculate the rates of change of the state variables.
-
integrate–Integrate the state variables using the rates of change.
Attributes:
initialize
Initialize the model.
Parameters:
-
day(date) –The starting date of the simulation.
-
kiosk(VariableKiosk) –A container for registering and publishing (internal and external) state variables. See PCSE documentation for details.
-
parvalues(ParameterProvider) –A dictionary-like container holding all parameter sets (crop, soil, site) as key/value. The values are arrays or scalars. See PCSE documentation for details.
Source code in src/diffwofost/physical_models/crop/root_dynamics.py
calc_rates
Calculate the rates of change of the state variables.
Parameters:
-
day(date, default:None) –The current date of the simulation.
-
drv(WeatherDataContainer, default:None) –A dictionary-like container holding weather data elements as key/value. The values are arrays or scalars. See PCSE documentation for details.
Source code in src/diffwofost/physical_models/crop/root_dynamics.py
integrate
Integrate the state variables using the rates of change.
Parameters:
-
day(date, default:None) –The current date of the simulation.
-
delt(float, default:1.0) –The time step for integration. Defaults to 1.0.
Source code in src/diffwofost/physical_models/crop/root_dynamics.py
Utility (under development)
diffwofost.physical_models.config.Configuration
dataclass
Configuration(CROP: type[SimulationObject], SOIL: type[SimulationObject] | None = None, AGROMANAGEMENT: type[AncillaryObject] = AgroManager, OUTPUT_VARS: list = list(), SUMMARY_OUTPUT_VARS: list = list(), TERMINAL_OUTPUT_VARS: list = list(), OUTPUT_INTERVAL: str = 'daily', OUTPUT_INTERVAL_DAYS: int = 1, OUTPUT_WEEKDAY: int = 0, model_config_file: str | Path | None = None, description: str | None = None)
Class to store model configuration from a PCSE configuration files.
Methods:
-
from_pcse_config_file–Load the model configuration from a PCSE configuration file.
-
update_output_variable_lists–Updates the lists of output variables that are defined in the configuration file.
from_pcse_config_file
classmethod
Load the model configuration from a PCSE configuration file.
Parameters:
-
filename(str | Path) –Path to the configuraiton file. The path is first interpreted with respect to the current working directory and, if not found, it will then be interpreted with respect to the
conffolder in the PCSE package.
Returns:
-
Configuration(Self) –Model configuration instance
Raises:
-
FileNotFoundError–if the configuraiton file does not exist
-
RuntimeError–if parsing the configuration file fails
Source code in src/diffwofost/physical_models/config.py
update_output_variable_lists
update_output_variable_lists(output_vars: str | list | tuple | set | None = None, summary_vars: str | list | tuple | set | None = None, terminal_vars: str | list | tuple | set | None = None)
Updates the lists of output variables that are defined in the configuration file.
This is useful because sometimes you want the flexibility to get access to an additional model variable which is not in the standard list of variables defined in the model configuration file. The more elegant way is to define your own configuration file, but this adds some flexibility particularly for use in jupyter notebooks and exploratory analysis.
Note that there is a different behaviour given the type of the variable provided. List and string inputs will extend the list of variables, while set/tuple inputs will replace the current list.
Parameters:
-
output_vars(str | list | tuple | set | None, default:None) –the variable names to add/replace for the OUTPUT_VARS configuration variable
-
summary_vars(str | list | tuple | set | None, default:None) –the variable names to add/replace for the SUMMARY_OUTPUT_VARS configuration variable
-
terminal_vars(str | list | tuple | set | None, default:None) –the variable names to add/replace for the TERMINAL_OUTPUT_VARS configuration variable
Raises:
-
TypeError–if the type of the input arguments is not recognized
Source code in src/diffwofost/physical_models/config.py
diffwofost.physical_models.config.ComputeConfig
Central configuration for device and dtype settings.
This class provides a centralized way to control PyTorch device and dtype settings across all simulation objects in diffWOFOST. Instead of setting device and dtype individually for each class, use this central configuration to apply settings globally.
Default Behavior:
- Device: Automatically defaults to 'cuda' if available, otherwise 'cpu'
- Dtype: Defaults to torch.float64
Basic Usage:
>>> from diffwofost.physical_models.config import ComputeConfig
>>> import torch
>>>
>>> # Set device to CPU
>>> ComputeConfig.set_device('cpu')
>>>
>>> # Or use a torch.device object
>>> ComputeConfig.set_device(torch.device('cuda'))
>>>
>>> # Set dtype to float32
>>> ComputeConfig.set_dtype(torch.float32)
>>>
>>> # Get current settings
>>> device = ComputeConfig.get_device() # Returns: torch.device('cpu')
>>> dtype = ComputeConfig.get_dtype() # Returns: torch.float32
Using with Simulation Objects:
All simulation objects (e.g., WOFOST_Leaf_Dynamics, WOFOST_Phenology) automatically use the settings from ComputeConfig. No changes needed to instantiation code:
>>> from diffwofost.physical_models.config import ComputeConfig
>>> from diffwofost.physical_models.crop.leaf_dynamics import WOFOST_Leaf_Dynamics
>>>
>>> # Set global compute settings
>>> ComputeConfig.set_device('cuda')
>>> ComputeConfig.set_dtype(torch.float32)
>>>
>>> # Instantiate objects - they automatically use global settings
>>> leaf_dynamics = WOFOST_Leaf_Dynamics()
Switching Between Devices:
Useful for switching between GPU training and CPU evaluation:
>>> # Train on GPU
>>> ComputeConfig.set_device('cuda')
>>> ComputeConfig.set_dtype(torch.float32)
>>> # ... run training ...
>>>
>>> # Evaluate on CPU
>>> ComputeConfig.set_device('cpu')
>>> ComputeConfig.set_dtype(torch.float64)
>>> # ... run evaluation ...
Resetting to Defaults:
>>> ComputeConfig.reset_to_defaults()
Methods:
-
get_device–Get the current device setting.
-
set_device–Set the device to use for tensor operations.
-
get_dtype–Get the current dtype setting.
-
set_dtype–Set the dtype to use for tensor creation.
-
reset_to_defaults–Reset device and dtype to their default values.
get_device
classmethod
Get the current device setting.
Returns:
-
device–torch.device: The current device (cuda or cpu)
set_device
classmethod
Set the device to use for tensor operations.
Parameters:
-
device(str | device) –Device to use ('cuda', 'cpu', or torch.device object)
Example
ComputeConfig.set_device('cuda') ComputeConfig.set_device(torch.device('cpu'))
Source code in src/diffwofost/physical_models/config.py
get_dtype
classmethod
Get the current dtype setting.
Returns:
-
dtype–torch.dtype: The current dtype (e.g., torch.float32, torch.float64)
set_dtype
classmethod
Set the dtype to use for tensor creation.
Parameters:
-
dtype(dtype) –PyTorch dtype (torch.float32, torch.float64, etc.)
Example
ComputeConfig.set_dtype(torch.float32)
Source code in src/diffwofost/physical_models/config.py
reset_to_defaults
classmethod
diffwofost.physical_models.engine.Engine
Engine(parameterprovider, weatherdataprovider, agromanagement, config: str | Path | Configuration)
Bases: Engine
Source code in src/diffwofost/physical_models/engine.py
diffwofost.physical_models.utils.EngineTestHelper
EngineTestHelper(parameterprovider, weatherdataprovider, agromanagement, config, external_states=None, device=None, dtype=None)
Bases: Engine
An engine which is purely for running the YAML unit tests.