-
Notifications
You must be signed in to change notification settings - Fork 3
Make respiration differentiable/vectorized
#74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| """Maintenance respiration for the WOFOST crop model.""" | ||
|
|
||
| import datetime | ||
| import torch | ||
| from pcse.base import SimulationObject | ||
| from pcse.base.parameter_providers import ParameterProvider | ||
| from pcse.base.variablekiosk import VariableKiosk | ||
| from pcse.base.weather import WeatherDataContainer | ||
| from pcse.decorators import prepare_rates | ||
| from diffwofost.physical_models.base import TensorParamTemplate | ||
| from diffwofost.physical_models.base import TensorRatesTemplate | ||
| from diffwofost.physical_models.config import ComputeConfig | ||
| from diffwofost.physical_models.traitlets import Tensor | ||
| from diffwofost.physical_models.utils import AfgenTrait | ||
| from diffwofost.physical_models.utils import _broadcast_to | ||
| from diffwofost.physical_models.utils import _get_drv | ||
|
|
||
|
|
||
| class WOFOST_Maintenance_Respiration(SimulationObject): | ||
| """Maintenance respiration in WOFOST. | ||
|
|
||
| WOFOST calculates the maintenance respiration as proportional to the dry | ||
| weights of the plant organs to be maintained, where each plant organ can be | ||
| assigned a different maintenance coefficient. Multiplying organ weight | ||
| with the maintenance coeffients yields the relative maintenance respiration | ||
| (`RMRES`) which is than corrected for senescence (parameter `RFSETB`). Finally, | ||
| the actual maintenance respiration rate is calculated using the daily mean | ||
| temperature, assuming a relative increase for each 10 degrees increase | ||
| in temperature as defined by `Q10`. | ||
|
|
||
| **Simulation parameters** (provide in cropdata dictionary) | ||
|
|
||
| | Name | Description | Type | Unit | | ||
| |--------|---------------------------------------------------------- |------|------------------| | ||
| | Q10 | Relative increase in maintenance respiration rate with | SCr | - | | ||
| | | each 10 degrees increase in temperature | | - | | ||
| | RMR | Relative maintenance respiration rate for roots | SCr | kg CH₂O kg⁻¹ d⁻¹ | | ||
| | RMS | Relative maintenance respiration rate for stems | SCr | kg CH₂O kg⁻¹ d⁻¹ | | ||
| | RML | Relative maintenance respiration rate for leaves | SCr | kg CH₂O kg⁻¹ d⁻¹ | | ||
| | RMO | Relative maintenance respiration rate for storage organs | SCr | kg CH₂O kg⁻¹ d⁻¹ | | ||
| | RFSETB | Reduction factor for senescence | TCr | - | | ||
|
|
||
| **Rate variables** | ||
|
|
||
| | Name | Description | Pbl | Unit | | ||
| |-------|--------------------------------------------|----|-------------------| | ||
| | PMRES | Potential maintenance respiration rate | N | kg CH₂O ha⁻¹ d⁻¹ | | ||
|
|
||
| **Signals send or handled** | ||
|
|
||
| None | ||
|
|
||
| **External dependencies** | ||
|
|
||
| | Name | Description | Provided by | Unit | | ||
| |------|-------------------------------------|--------------------------------|-----------| | ||
| | DVS | Crop development stage | DVS_Phenology | - | | ||
| | WRT | Dry weight of living roots | WOFOST_Root_Dynamics | kg ha⁻¹ | | ||
| | WST | Dry weight of living stems | WOFOST_Stem_Dynamics | kg ha⁻¹ | | ||
| | WLV | Dry weight of living leaves | WOFOST_Leaf_Dynamics | kg ha⁻¹ | | ||
| | WSO | Dry weight of living storage organs | WOFOST_Storage_Organ_Dynamics | kg ha⁻¹ | | ||
|
|
||
| **Outputs** | ||
|
|
||
| | Name | Description | Pbl | Unit | | ||
| |-------|--------------------------------------------|----|---------------------| | ||
| | PMRES | Potential maintenance respiration rate | N | kg CH₂O ha⁻¹ d⁻¹ | | ||
|
|
||
| **Gradient mapping (which parameters have a gradient):** | ||
|
|
||
| | Output | Parameters influencing it | | ||
| |--------|------------------------------------------| | ||
| | PMRES | Q10, RMR, RML, RMS, RMO, RFSETB | | ||
| """ | ||
|
|
||
| @property | ||
| def device(self): | ||
| """Get device from ComputeConfig.""" | ||
| return ComputeConfig.get_device() | ||
|
|
||
| @property | ||
| def dtype(self): | ||
| """Get dtype from ComputeConfig.""" | ||
| return ComputeConfig.get_dtype() | ||
|
|
||
| class Parameters(TensorParamTemplate): | ||
| Q10 = Tensor(-99.0) | ||
| RMR = Tensor(-99.0) | ||
| RML = Tensor(-99.0) | ||
| RMS = Tensor(-99.0) | ||
| RMO = Tensor(-99.0) | ||
| RFSETB = AfgenTrait() | ||
|
|
||
| class RateVariables(TensorRatesTemplate): | ||
| PMRES = Tensor(0.0) | ||
|
|
||
| def initialize( | ||
| self, | ||
| day: datetime.date, | ||
| kiosk: VariableKiosk, | ||
| parvalues: ParameterProvider, | ||
| shape: tuple | None = None, | ||
| ): | ||
| """Initialize the maintenance respiration module. | ||
|
|
||
| Args: | ||
| day: Start date of the simulation | ||
| kiosk: Variable kiosk of this PCSE instance | ||
| parvalues: ParameterProvider object providing parameters as key/value pairs | ||
| shape: Shape of the parameters tensors (optional) | ||
| """ | ||
| self.params = self.Parameters(parvalues, shape=shape) | ||
| self.rates = self.RateVariables(kiosk, shape=shape) | ||
| self.kiosk = kiosk | ||
|
|
||
| @prepare_rates | ||
| def calc_rates(self, day: datetime.date, drv: WeatherDataContainer): | ||
| """Calculate maintenance respiration rates. | ||
|
|
||
| Args: | ||
| day: Current date | ||
| drv: Weather data for the current day | ||
| """ | ||
| p = self.params | ||
| kk = self.kiosk | ||
| r = self.rates | ||
|
|
||
| Q10 = p.Q10 | ||
| RMR = p.RMR | ||
| RML = p.RML | ||
| RMS = p.RMS | ||
| RMO = p.RMO | ||
|
|
||
| WRT = kk["WRT"] | ||
| WLV = kk["WLV"] | ||
| WST = kk["WST"] | ||
| WSO = kk["WSO"] | ||
| # [!] DVS needs to be broadcasted explicetly because it is used | ||
| # in torch.where and the kiosk does not format it correctly | ||
| # TODO see #22 | ||
| DVS = _broadcast_to(kk["DVS"], p.shape, self.dtype, self.device) | ||
SCiarella marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| TEMP = _get_drv(drv.TEMP, p.shape, self.dtype, self.device) | ||
|
|
||
| RMRES = RMR * WRT + RML * WLV + RMS * WST + RMO * WSO | ||
| RMRES = RMRES * p.RFSETB(DVS) | ||
| TEFF = Q10 ** ((TEMP - 25.0) / 10.0) | ||
| PMRES = RMRES * TEFF | ||
|
|
||
| # No maintenance respiration before emergence (DVS < 0). | ||
| r.PMRES = torch.where(DVS < 0, torch.zeros_like(PMRES), PMRES) | ||
|
|
||
| def __call__(self, day: datetime.date, drv: WeatherDataContainer): | ||
| """Calculate and return maintenance respiration (PMRES).""" | ||
| self.calc_rates(day, drv) | ||
| return self.rates.PMRES | ||
|
|
||
| def integrate(self, day: datetime.date, delt: float = 1.0): | ||
| """No state variables to integrate for this module.""" | ||
| return | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fnattino to avoid problems with the
torch.wherestatement on L150, I need to explicitly broadcast this kiosk parameter here.Do you think that is there a better way? maybe making the
kioskwith templates like in #75?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. This is related to #22,
accepting external dependencies through kiosk variable. Right nowEngineTestHelperusesVariableKioskTestHelperfrom utils. For now, this solution is okay and we will fix this in another PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SCiarella @fnattino I added #22 as a task to the board, that one of us can work on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I think there is no way around here - we had to do the same in the partitioning module, where the same DVS variable has to be explicitely broadcasted to the parameters' shape.