Skip to content

Optimization with diffWOFOST

We provide an example notebook showing optimization of models' parameters with diffWOFOST. To get familiar with the concepts and implementation, check out Introduction in the documentation.

Model Open the notebook Access the source View the notebook
WOFOST_72 Potential Production Open In Colab Access the source code here
Phenology Open In Colab Access the source code here
Root dynamics Open In Colab Access the source code here
Leaf dynamics Open In Colab Access the source code here

Note

When calculating gradients, it is important to ensure that the predicted physical parameters are within realistic bounds regarding the crop and environmental conditions.

Also, when calculating gradients of an output w.r.t. parameters, it would be good to know in advance how the parameters in a model influence the outputs. If a parameter has little to no influence on an output, the gradient of the output w.r.t the parameter will be close to zero, which may not provide useful information for optimization.

Hybrid modeling with diffWOFOST

The differentiable nature of diffWOFOST allows for integration of machine learning (ML) models with physical crop models, enabling the creation of hybrid models that leverage the strengths of both approaches. In a hybrid modeling framework, crop models are SimulationObjects and the computations can be either physics-based or ML-based, see section How to run a model. Here we provide an example notebook to show how to replace the partitioning model in WOFOST72.

Model Open the notebook Access the source View the notebook
Hybrid partitioning in WOFOST72 Open In Colab Access the source code here

Note

The ml-based models in diffWOFOST, for example PartitioningMLP and PartitioningNN from ml_models/crop/partitioning.py are meant to be used as an example of how to replace a physics-based model with a machine learning model. They are not trained and are not evaluated against the original wofost model results. The purpose is to show how to run a ml-based model in diffWOFOST and how to integrate it with the physics-based models.

Variational data assimilation with diffWOFOST

The differentiable implementation of diffWOFOST also makes it possible to formulate data assimilation as a gradient-based optimization problem. In this example notebook, synthetic LAI and soil-moisture observations are combined with the crop model to estimate a parameter set that improves the simulated seasonal trajectory.

Model Open the notebook Access the source View the notebook
Variational data assimilation Open In Colab Access the source code here

Computing configuration

The object ComputeConfig provides a central configuration for 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

More info:

See the ComputeConfig API reference for more details.