Skip to content
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

Feature/plants model #312

Merged
merged 24 commits into from
Sep 12, 2023
Merged

Feature/plants model #312

merged 24 commits into from
Sep 12, 2023

Conversation

davidorme
Copy link
Collaborator

Description

This PR further extends the Plants Model to add the final (well, we can dream) parts of the API for modelling plant growth. None of the functions do any actual science yet, but the required inputs and internal variables are set up and the flow of the model is complete.

The PR:

  • Extends the Cohort object to track the canopy area of members within the canopy layer and the gross primary productivity.
  • Adds the fraction of absorbed photosynthetically active radiation (f_APAR) within each canopy layer to the canopy model generation.
  • Uses f_APAR and the canopy top photon flux density to estimate the absorbed photons in µmol m2 s1 in each layer.
  • The P Model will then take configured forcing variables to calculate light use efficiency, but these variables are currently just checked for presence.
  • Now the GPP can be calculated for each stem: the sum of absorbed irradiance per m2 s1 scaled by the canopy area and elapsed time and the light use efficiency
  • A GPP allocation process then calculates the change in cohort size - the process is currently trivial but will adopt the T Model from pyrealm.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist

  • Make sure you've run the pre-commit checks: $ pre-commit run -a
  • All tests pass: $ poetry run pytest

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Copy link
Collaborator

@jacobcook1995 jacobcook1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just had a few minor comments

virtual_rainforest/models/plants/plants_model.py Outdated Show resolved Hide resolved
ground = np.where(self.data["layer_roles"].data == "surface")[0]
self.data["layer_absorbed_irradiation"][ground] = ground_irradiance

def estimate_gpp(self, time_index: int) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly sceptical of estimate_gpp and allocate_gpp being included as methods of the PlantsModel, not so much from a code functionality point of view but feel like this could end up as a huge script if methods that low level are added. But that might be a misplaced fear if most of the functionality is farmed out to PyRealm

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is my expectation - these should essentially extract numpy arrays from our Data instance and feed them into pyrealm functions. If they get larger, I will split them out into another submodule.

for cohort in community:
cohort.gpp = np.nansum(
cohort.canopy_area * cell_gpp_per_m2 * seconds_since_last_update
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a long method that does quite a few things (or at least will do in future), I think it might be better to split it into simpler sub-functions. Though that can probably wait until the comments in it are filled out

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - it may actually get shorter!

@@ -68,6 +89,8 @@ class PlantsModel(BaseModel):
vars_updated = (
"leaf_area_index", # NOTE - LAI is integrated into the full layer roles
"layer_heights", # NOTE - includes soil, canopy and above canopy heights
"layer_fapar",
"layer_absorbed_irradiation",
"herbivory",
"transpiration",
"canopy_evaporation",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the distinction here that transpiration is water from the canopy interception pool and canopy_evaporation is the water that plants such out of the soil? If so, I will update the input to the hydrology model to canopy_evaporation to account for root water uptake. Also, is this going to be an accumulated values over all layers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other way around, isn't it? Transpiration should be the stomatal conductance rate scaled up to the amount of photosynthesis and canopy_evaporation is water loss from the (currently non existent) canopy interception pool. I've not implemented any of that though, so if there is something better to do, let me know!

I can do transpiration per canopy layer - the stomatal conductance is a prediction of the P Model, which will be run within layers. In fact, I'll have to - but can always sum it if we don't need the vertical profile. For the evaporation - if we have a layer specific interception model, then that too! I think that's another PR though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sorry, I mixed that up, I did mean transpiration.
I have implemented a simple interception pool for the whole canopy (then I got carried away and changed a million other little things and it's now a complete overhaul of the hydrology model, which you will soon see...), so we could include it in one of the future PRs in the hydrology model, I don't think it necessarily needs to touch the plant model because it only needs to LAI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also only relevant for us if we want to close the water balance

Copy link
Collaborator

@vgro vgro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some small suggestions in a few places. In particular, you want to make sure to use pytest.approx for doing floating-point comparisons in unit tests, otherwise you can get spurious test failures if the data change subtly or you run them on different hardware.

virtual_rainforest/models/plants/canopy.py Show resolved Hide resolved
virtual_rainforest/models/plants/canopy.py Outdated Show resolved Hide resolved
virtual_rainforest/models/plants/canopy.py Outdated Show resolved Hide resolved
tests/models/plants/test_plants_model.py Outdated Show resolved Hide resolved
tests/models/plants/test_plants_model.py Outdated Show resolved Hide resolved
tests/models/plants/test_plants_model.py Outdated Show resolved Hide resolved
tests/models/plants/test_plants_model.py Outdated Show resolved Hide resolved
tests/models/plants/test_plants_model.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Sep 11, 2023

Codecov Report

Merging #312 (3a0de2f) into develop (bb71f2f) will increase coverage by 0.08%.
Report is 36 commits behind head on develop.
The diff coverage is 93.84%.

@@             Coverage Diff             @@
##           develop     #312      +/-   ##
===========================================
+ Coverage    96.97%   97.05%   +0.08%     
===========================================
  Files           52       52              
  Lines         2346     2513     +167     
===========================================
+ Hits          2275     2439     +164     
- Misses          71       74       +3     
Files Changed Coverage Δ
virtual_rainforest/models/plants/plants_model.py 92.59% <91.42%> (-1.41%) ⬇️
virtual_rainforest/models/plants/canopy.py 94.23% <95.45%> (+1.20%) ⬆️
virtual_rainforest/models/plants/community.py 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Edit: Except for the CI failures...

@davidorme
Copy link
Collaborator Author

Yeah - working on those!

@davidorme davidorme merged commit 72faccb into develop Sep 12, 2023
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants