Skip to content

959 sort out time interval in energy balance#988

Merged
vgro merged 28 commits intodevelopfrom
959-sort-out-time-interval-in-energy-balance
Sep 1, 2025
Merged

959 sort out time interval in energy balance#988
vgro merged 28 commits intodevelopfrom
959-sort-out-time-interval-in-energy-balance

Conversation

@vgro
Copy link
Copy Markdown
Collaborator

@vgro vgro commented Aug 6, 2025

This PR replaced the placeholder '1's with the actual time interval in the microclimate update.

To make this work with the varying number of canopy layers, I also fixed how the above ground layer thickness is calculated, a few directions of fluxes, and adjusted the vertical mixing - basically all the steps where a value from below is called and NaN has to be skipped.

I also tidied the tests a bit to check if the values fall in a sensible range instead of specific values and introduced a step to mask the nan values in the canopy. The next PR will be testing with a grid cell that has no vegetation at all.

Fixes #959

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
  • Relevant documentation reviewed and updated

@vgro vgro linked an issue Aug 6, 2025 that may be closed by this pull request
@vgro vgro requested review from davidorme and jacobcook1995 August 6, 2025 14:31
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Aug 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.13%. Comparing base (2bd8481) to head (4e28166).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #988      +/-   ##
===========================================
+ Coverage    95.11%   95.13%   +0.02%     
===========================================
  Files           80       80              
  Lines         6693     6724      +31     
===========================================
+ Hits          6366     6397      +31     
  Misses         327      327              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

vgro added 2 commits August 6, 2025 15:58
Copy link
Copy Markdown
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.

Had a few queries, but the general structure looks sensible to me


# If a valid upper value and a mixing coefficient are found, compute flux
if upper_i >= 0 and np.isfinite(mixing_coefficient[upper_i, j]):
delta_up = input_variable[upper_i, j] - center_val
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should one of delta_up or delta_down be reversed? Assuming a positive or negative gradient through the canopy, one of the delta values is going to yield negative values and then gets clipped by flux_up < 0

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I removed the clipping step to allow for flows in both directions

flux_down = 0.0

# ---- Update value ----
change = (flux_up + flux_down) * time_interval / layer_thickness[i, j]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this a double correction for layer thickness? The fluxes have already been divided through by thickness.

@vgro
Copy link
Copy Markdown
Collaborator Author

vgro commented Aug 18, 2025

I have made the suggested changes and added an extra step to ensure that the values stay within realistic limits (especially important for atmospheric humidity). I tried to vectorize where possible but couldn't find a way around the last for loop.

@vgro vgro requested a review from davidorme August 18, 2025 11:09
@davidorme davidorme mentioned this pull request Aug 27, 2025
8 tasks
Copy link
Copy Markdown
Collaborator

@davidorme davidorme left a comment

Choose a reason for hiding this comment

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

This looks good. I did have some difficulty getting my head around the new bit to redistribute values outside limits. I think that's partly because that process is embedded within mix_and_ventilate so it's hard to visualise/troubleshoot.

I've put in a PR (#1011) that breaks this part out into its own standalone clamping function and tests it separately - I can't get rid of the loop, but I think it can be calculated more simply. This feels like a better unit structure for the functionality. If that PR looks good to you, then I think this is ready to go?

@vgro
Copy link
Copy Markdown
Collaborator Author

vgro commented Sep 1, 2025

This looks good. I did have some difficulty getting my head around the new bit to redistribute values outside limits. I think that's partly because that process is embedded within mix_and_ventilate so it's hard to visualise/troubleshoot.

I've put in a PR (#1011) that breaks this part out into its own standalone clamping function and tests it separately - I can't get rid of the loop, but I think it can be calculated more simply. This feels like a better unit structure for the functionality. If that PR looks good to you, then I think this is ready to go?

Looks good to me, I just merged it. Could you please approve this PR when you're happy with it?

Copy link
Copy Markdown
Collaborator

@davidorme davidorme left a comment

Choose a reason for hiding this comment

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

👍

@vgro vgro merged commit ac7ca51 into develop Sep 1, 2025
13 checks passed
@vgro vgro deleted the 959-sort-out-time-interval-in-energy-balance branch September 1, 2025 14:27
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.

Sort out time interval in energy balance

4 participants