Conversation
…-derivative-of-temperature-update
…-derivative-of-temperature-update
…-derivative-of-temperature-update
…-derivative-of-temperature-update
…-temperature-update' of https://github.com/ImperialCollegeLondon/virtual_rainforest into 848-abiotic-model-add-latent-heat-flux-to-derivative-of-temperature-update
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #897 +/- ##
===========================================
- Coverage 93.96% 93.90% -0.07%
===========================================
Files 77 77
Lines 5864 5902 +38
===========================================
+ Hits 5510 5542 +32
- Misses 354 360 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
docs/source/virtual_ecosystem/implementation/abiotic_implementation.md
Outdated
Show resolved
Hide resolved
| ``` | ||
|
|
||
| This term approximates vertical conduction using the second spatial derivative of | ||
| temperature. |
There was a problem hiding this comment.
Might be worth giving either a citation to or writing out the temperature function that is being differentiated here
There was a problem hiding this comment.
Will need to look into this again, I noted this down in a separate issue #919
docs/source/virtual_ecosystem/implementation/abiotic_implementation.md
Outdated
Show resolved
Hide resolved
docs/source/virtual_ecosystem/implementation/abiotic_implementation.md
Outdated
Show resolved
Hide resolved
docs/source/virtual_ecosystem/implementation/abiotic_implementation.md
Outdated
Show resolved
Hide resolved
jacobcook1995
left a comment
There was a problem hiding this comment.
I had a bunch of comments which I'm adding so we can discuss them when we meet
…-derivative-of-temperature-update
…balance dict in microclimate
…-temperature-update' of https://github.com/ImperialCollegeLondon/virtual_rainforest into 848-abiotic-model-add-latent-heat-flux-to-derivative-of-temperature-update
…-derivative-of-temperature-update
…ation.md Co-authored-by: Jacob Cook <32497818+jacobcook1995@users.noreply.github.com>
…-temperature-update' of https://github.com/ImperialCollegeLondon/virtual_rainforest into 848-abiotic-model-add-latent-heat-flux-to-derivative-of-temperature-update
… canopy temperature
…-derivative-of-temperature-update
|
Thanks @jacobcook1995 for your feedback! I made a few more changes:
Variable values are not in the range of physically possible but there needs to be some calibration done. There are also still open TODOs regarding unit conversions and time intervals, will address this separately to not make this more complicated here... |
jacobcook1995
left a comment
There was a problem hiding this comment.
LGTM! Just had one small query
| density_water_array = density_water | ||
|
|
||
| for i in range(nrows): | ||
| for j in range(ncols): |
There was a problem hiding this comment.
Are these loops so you can solve the problem for each canopy layer for each cell individually?
There was a problem hiding this comment.
yes, the scipy function only takes scalar values
There was a problem hiding this comment.
oh didn't realise that (does make sense). Was going to suggest flagging this as a potential performance bottleneck that you could improve by using a vector rather than scalar solver but if the solver only takes scalars then that is moot really
There was a problem hiding this comment.
I thinks it's really fast but I can add a flag, good point
There was a problem hiding this comment.
Late to this party, but I suspect that this might be a bottleneck - many definitions of a cell specific function. I think a gridded version of a secant solver (which is what is being used here) might be faster, but this is definitely something to circle back to rather than worry about now.
There was a problem hiding this comment.
Actually, having said that, I've got a similar gridded root finding in pyrealm.splash which has been intermittently bothering me, so I've looked back into this and it is possible to parallelise this. I think this is going to be more efficient because it can operate over all cells and layers simultaneously - and numpy is great at handling those mass calculations rather than iterating over cells and layers. I suspect I'd want @dalonsoa or @alexdewar to weigh in on the implementation, but I think this is doable.
It makes more sense to use canned versions like scipy.optimise.newton - unless they impose restrictions like only scalar inputs.
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
from numpy.typing import NDArray
def secant_method(
f: Callable,
x0: NDArray[np.floating],
x1: NDArray[np.floating],
tol: float,
**kwargs,
) -> NDArray[np.floating]:
"""Secant root for array inputs"""
# Starting difference from root
unsolved = np.full_like(x0, True, dtype=np.bool_)
n_iter = 0
while np.any(unsolved):
# Calculate fx0 and fx1
fx0 = f(x0, **kwargs)
fx1 = f(x1, **kwargs)
# Update using secant method
x2 = x1 - fx1 * (x1 - x0) / (fx1 - fx0)
# Update unsolved values
unsolved = np.abs(fx0) > tol
x0, x1 = np.where(unsolved, x1, x0), np.where(unsolved, x2, x1)
n_iter += 1
print(f"{unsolved.sum()} unsolved at iteration {n_iter}")
return x2
def func(x: NDArray[np.floating], b: NDArray[np.floating]):
return x**2 - 10 + b
# Cell specific variation
b = np.array([[1, 2, 3, 4]])
# Starting points
x0 = np.full_like(b, 0)
x1 = np.full_like(b, 40)
# Solve
root = secant_method(func, x0=x0, x1=x1, tol=1e-8, b=b)
# Plot
x = np.broadcast_to(np.arange(0, 5, 0.01)[:, None], (500, 4))
plt.plot(x, func(x, b=b))
ax = plt.gca()
style = dict(linewidth=0.2, color="black")
ax.vlines(root, ymin=0, ymax=1, transform=ax.get_xaxis_transform(), **style)
ax.hlines(0, xmin=0, xmax=1, transform=ax.get_yaxis_transform(), **style)
plt.show()
The PR started out as adding a missing term to the derivative of the energy balance to fix #848 , I also made some small changes to the structure to make it more readable and updated the documentation.
The docs are built here
Fixes #848 #781
Type of change
Key checklist
pre-commitchecks:$ pre-commit run -a$ poetry run pytestFurther checks