<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Overstorey</title>
<link>https://traitecoevo.github.io/overstorey/posts/</link>
<atom:link href="https://traitecoevo.github.io/overstorey/posts/index.xml" rel="self" type="application/rss+xml"/>
<description>a field guide to the plant model</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Sat, 20 Jun 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Moving cohort refining into C++</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-20-cohort-refining-cpp/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop ad280e9</span></p>
<p>This post summarises <a href="https://github.com/traitecoevo/plant/issues/408">issue #408</a>: moving the SCM’s adaptive cohort-schedule refinement out of R and into C++, and collapsing the surrounding R helpers onto one entry point. The governing constraint was that the FF16 reference baselines stay green at every step — the refactor changes <em>where</em> the work happens, not <em>what</em> it computes.</p>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>The solver carried two intertwined machines smeared across the R/C++ boundary:</p>
<ul>
<li><strong>A refinement loop</strong> that lived mostly in R (<code>build_schedule()</code>). Each iteration constructed a fresh <code>SCM</code>, <strong>manually single-stepped</strong> it from R to sample a per-node competition (LAI) error during the run, read a per-node reproduction error at the end, flagged nodes whose combined error exceeded <code>schedule_eps</code>, bisected the intervals below them, and repeated.</li>
<li><strong>Reproduction accounting</strong> that was already integrated inside the C++ ODE, but was then re-weighted <em>post-hoc</em> — <code>SCM</code> reached back into <code>node_schedule</code> to re-derive each node’s introduction time and re-queried <code>survival_weighting-&gt;density(t)</code>, even though both values were knowable the moment the node was born.</li>
</ul>
<p>The only reason the refinement loop was stuck in R was that the competition error had to be sampled <em>during</em> the run as a running maximum across steps — and the R code re-implemented the step loop just to observe it.</p>
</section>
<section id="the-key-insight" class="level2">
<h2 class="anchored" data-anchor-id="the-key-insight">The key insight</h2>
<p>Every “look it up later” path could be replaced by <strong>recording the value on the node at introduction</strong>. Once a <code>Node</code> owns its <code>introduction_time</code> and <code>density_at_birth</code>, <code>Species</code> can produce both the weighted-fitness vector and the integration x-axis itself, and <code>SCM</code> no longer needs <code>node_schedule</code> or <code>survival_weighting</code> for any reproduction calculation. With that in place, the running-max competition error can be maintained directly inside <code>SCM::run()</code> — removing the last thing forcing the loop into R.</p>
</section>
<section id="how-it-was-done-four-behaviour-preserving-phases" class="level2">
<h2 class="anchored" data-anchor-id="how-it-was-done-four-behaviour-preserving-phases">How it was done — four behaviour-preserving phases</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Phase</th>
<th>What moved into C++</th>
<th>Verification</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>1. Node-level bookkeeping</strong></td>
<td>Nodes stamp their own <code>introduction_time</code> + <code>patch_density_at_birth</code> at birth; reproduction methods read <code>species.node_times()</code> instead of reaching back into <code>node_schedule</code>/<code>survival_weighting</code>.</td>
<td>Full SCM / strategy / stochastic suites green; no R-facing signatures changed.</td>
</tr>
<tr class="even">
<td><strong>2. Error collection in <code>run()</code></strong></td>
<td>A <code>collect_errors</code> flag makes <code>SCM::run()</code> fold in the per-species running-max competition error; <code>combined_node_errors()</code> returns the exact per-node <code>max(competition, reproduction)</code> signal the R loop used to assemble.</td>
<td>Asserted bit-exact against the old <code>run_scm_error()$err$total</code> for FF16 single / two-species / refined schedules; durable regression test added.</td>
</tr>
<tr class="odd">
<td><strong>3. Refinement loop + <code>split_times</code></strong></td>
<td><code>SCM::refine_schedule()</code> owns the adaptive loop end-to-end (run → flag → upwind bisection → reset), reusing a single SCM instance.</td>
<td>Reproduces R <code>build_schedule</code> exactly (refined times, ODE times, offspring production) for FF16 and K93; durable test added.</td>
</tr>
<tr class="even">
<td><strong>4. Breaking API cleanup</strong></td>
<td>One entry point: <code>run_scm(p, env, ctrl, refine_schedule = FALSE, collect = FALSE, use_ode_times = FALSE)</code>. <code>run_scm_collect</code>, <code>run_scm_error</code>, and R-side <code>build_schedule</code>/<code>split_times</code> removed.</td>
<td>Full suite: 1832 pass, 0 fail, 1 pre-existing skip.</td>
</tr>
</tbody>
</table>
<p>The phasing is the point: phases 1–3 are each independently green and behaviour-preserving, so the numerics never moved. Only phase 4 — the API consolidation — is a deliberate breaking change.</p>
</section>
<section id="what-callers-see-now" class="level2">
<h2 class="anchored" data-anchor-id="what-callers-see-now">What callers see now</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Refine an adaptive schedule (was: build_schedule(p, ctrl))</span></span>
<span id="cb1-2">p_refined <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_scm</span>(p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ctrl =</span> ctrl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refine_schedule =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>parameters</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run and collect tidied history + reproduction outputs (was: run_scm_collect(x))</span></span>
<span id="cb1-5">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_scm</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collect =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</section>
<section id="follow-ups-outside-this-repo" class="level2">
<h2 class="anchored" data-anchor-id="follow-ups-outside-this-repo">Follow-ups outside this repo</h2>
<ul>
<li><strong><code>plant.assembly</code></strong> still calls the removed helpers (<code>build_schedule()</code>, <code>run_scm_collect()</code>) in <code>R/community_plant.R</code> and <code>scripts/example/ESA.Rmd</code>. These need the same migration: <code>build_schedule(p, ctrl = ctrl)</code> → <code>run_scm(p, ctrl = ctrl, refine_schedule = TRUE)$parameters</code>, and <code>run_scm_collect(x)</code> → <code>run_scm(x, collect = TRUE)</code>.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The FF16 reference baselines in <code>tests/testthat/FF16_reference/</code> are the safety net for this whole refactor. If any phase had shifted numerics beyond tolerance, that would be a bug — not an expected regeneration.</p>
</div>
</div>


</section>

 ]]></description>
  <category>scm</category>
  <category>refactor</category>
  <category>develop</category>
  <category>engineering</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-20-cohort-refining-cpp/</guid>
  <pubDate>Sat, 20 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Making root water uptake fast</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-19-root-water-uptake-performance/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop 81cfeab</span></p>
<p>This post summarises a sustained performance-engineering effort on the <code>root_water_uptake</code> branch of <a href="https://github.com/traitecoevo/plant"><code>plant</code></a>. It is a high-level account of what was achieved and which ideas worked; the blow-by-blow record — every A/B timing, profile, and rejected experiment — lives in the branch’s commit history and in the issues linked below.</p>
<section id="what-was-achieved" class="level2">
<h2 class="anchored" data-anchor-id="what-was-achieved">What was achieved</h2>
<p>In short: the new multi-layer root-water hydraulic solver went from a raw hotspot to a structurally optimised path, <strong>roughly an order of magnitude faster end-to-end</strong>, with the science held to a tolerance-level shift that moves <em>toward</em> the more accurate reference.</p>
<ul>
<li><strong>≈4.4× from the algorithmic stack</strong> (first working version → end of the early phase): 39.8 s → 9.0 s on the single-species MPL = 10 benchmark, from O(1) spline indexing, transpiration/integral-spline caches, the exact interval-mean conductivity, and int-keyed lookups. The integral-spline cache added a further ≈1.32×, for <strong>≈5–6× by the end of the early phase</strong>.</li>
<li><strong>A further ≈2.3× on the hot path afterwards</strong>, once each nested root-find was characterised individually and given a <em>superlinear</em> solver where that was provably safe: <strong>≈1.8×</strong> (TOMS748 on the inner <code>psi_stem_to_ci</code> solve) × <strong>≈1.2×</strong> (TOMS748 on <code>find_root_psi</code>) × <strong>~1.08×</strong> (halving the divisions in the per-layer <code>E_from_Soil</code> loop).</li>
<li><strong>~60–75× more accurate inverse hydraulic transform</strong> by seeding the integral-spline knots from the closed-form incomplete-gamma integral (issue #468) instead of a trapezoid sum — same hot-path cost, quadrature bias removed at source.</li>
<li><strong>A documented set of <em>rejected</em> levers</strong> — global TOMS748, Brent on the outer collar solve, a logistic vulnerability curve, <code>pow(x,2)→x*x</code>, splining the hydraulic cost, loosening the Cᵢ tolerance — each measured, found to be slower / less stable / less accurate, and recorded so they are not re-litigated.</li>
<li><strong>Two safety gates held throughout</strong>: every change is bit-identical <em>or</em> its numeric shift is quantified, and every solver swap must still pass the drought-stability scenario (issue #485) with finite output and no NaN.</li>
</ul>
<p>The model is now considered structurally optimised: ~75% of runtime is the irreducible nested root-find structure, and every bit-identical lever has been taken.</p>
</section>
<section id="what-the-model-is-and-why-it-was-a-hotspot" class="level2">
<h2 class="anchored" data-anchor-id="what-the-model-is-and-why-it-was-a-hotspot">What the model is, and why it was a hotspot</h2>
<p>The branch couples three pieces, integrated together by <code>run_scm</code>: a multi-layer <strong>soil-water bucket model</strong>, a depth-resolved <strong>root mass distribution</strong>, and a <strong>plant hydraulics</strong> solver (after Potkay et al.&nbsp;2021) that finds each individual’s water-stress operating point. The expensive part is the hydraulics: for every individual, at every ODE sub-step, the model solves for the root-collar water potential that maximises profit (assimilation minus hydraulic cost). That solve is <em>two nested root-finds</em> wrapped in an outer optimisation, and each inner evaluation sweeps all soil layers and evaluates vulnerability-curve splines. Profiling consistently put essentially all runtime in this <code>find_root_collar_psi</code> path — a flat profile spread across solver structure and spline evaluation, not memory allocation.</p>
</section>
<section id="the-optimisation-phases" class="level2">
<h2 class="anchored" data-anchor-id="the-optimisation-phases">The optimisation phases</h2>
<p>Work proceeded through roughly nine phases. The honest pattern: the early algorithmic wins were large; later phases were as much about <em>rejecting</em> plausible-looking levers as landing new ones. A summary of the main levers:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Lever</th>
<th>Outcome</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>O(1) spline indexing (equidistant-grid arithmetic vs binary search)</td>
<td><strong>Adopted</strong> — was the single hottest leaf; bit-identical</td>
</tr>
<tr class="even">
<td>Memoised <code>transpiration()</code> / integral-spline caches for the per-layer root-vuln integral</td>
<td><strong>Adopted</strong> — bit-identical; collapsed ~30 spline evals/call to ~1</td>
</tr>
<tr class="odd">
<td>Exact interval-mean of root conductivity via a pre-integrated curve</td>
<td><strong>Adopted</strong> — fewer evals <em>and</em> more accurate</td>
</tr>
<tr class="even">
<td>String-keyed name→index lookups cached as ints; per-time driver memo</td>
<td><strong>Adopted</strong> — bit-identical</td>
</tr>
<tr class="odd">
<td>TOMS748 swapped <em>globally</em> across both nested root-finds</td>
<td><strong>Rejected</strong> — produced NaN soil potentials, ran slower</td>
</tr>
<tr class="even">
<td>Brent on the <em>outer</em> collar profit-max</td>
<td><strong>Adopted then reverted</strong> — its argmax is non-smooth, and the demographic growth gradient needs a smooth operating point; reverted to golden-section search</td>
</tr>
<tr class="odd">
<td>TOMS748 on the <em>inner</em> <code>psi_stem_to_ci</code> solve only</td>
<td><strong>Adopted</strong> — target is smooth &amp; monotone; ~1.8×</td>
</tr>
<tr class="even">
<td>TOMS748 on the <code>find_root_psi</code> solve only</td>
<td><strong>Adopted</strong> — brackets provably finite &amp; opposite-sign; ~1.2×</td>
</tr>
<tr class="odd">
<td>Halve divisions in the <code>E_from_Soil</code> per-layer loop</td>
<td><strong>Adopted</strong> — <code>fdiv</code> latency was the real cost</td>
</tr>
<tr class="even">
<td>Spline the xylem hydraulic cost; <code>pow(x,2)→x*x</code>; loosen the Cᵢ tolerance</td>
<td><strong>Rejected</strong> — slower or no faster, or destabilised the coupled ODE</td>
</tr>
</tbody>
</table>
<p>Two recurring lessons shaped the rejections. First, <strong>bisection precision is load-bearing for coupled-ODE stability</strong>: loosening the inner Cᵢ tolerance drives a soil layer to zero moisture and trips a drought singularity (issue #485). Second, on Apple-ARM hardware a spline eval already costs about as much as the transcendentals it would replace, so the “spline everything” strategy hit diminishing returns.</p>
</section>
<section id="key-innovations" class="level2">
<h2 class="anchored" data-anchor-id="key-innovations">Key innovations</h2>
<p><strong>Solver choice, split by target.</strong> The early blanket rejection of superlinear solvers conflated two different functions. Once each nested root-find was characterised on its own, the <em>inner</em> <code>psi_stem_to_ci</code> solve (smooth, strictly monotone) and the <code>find_root_psi</code> solve (provably finite, opposite-sign brackets) both took TOMS748 safely — converging to the <em>same</em> tolerance in far fewer evaluations. The <em>outer</em> collar optimisation kept golden-section search specifically because its argmax feeds a demographic gradient that must vary smoothly. The single biggest reproducibility constraint throughout: a faster method is only acceptable if it reaches the same answer, not a looser one.</p>
<p><strong>Caching strategy.</strong> The cumulative-integral splines (transpiration supply and root vulnerability) are built once per strategy and evaluated O(1) on the hot path, with the per-solve, per-layer integral lookups memoised down to roughly one evaluation per call.</p>
<p><strong>Analytical gamma-integral hydraulic transform (issue <a href="https://github.com/traitecoevo/plant/issues/468">#468</a>).</strong> The Weibull xylem vulnerability curve <img src="https://latex.codecogs.com/png.latex?%5Cexp(-(m/b)%5Ec)"> has a closed-form integral in terms of the lower incomplete gamma function, <img src="https://latex.codecogs.com/png.latex?F(m)%20=%20(b/c)%5C,%5Cgamma(1/c,%5C,(m/b)%5Ec)">. This was verified against independent adaptive quadrature (agreement to ~1e−6 relative) and is now used to <strong>seed the integral-spline knots analytically</strong> instead of by a running trapezoid sum — same knots, same O(1) hot-path eval, but the quadrature bias removed at the source. The payoff lands on the <em>inverse</em> transform used in simulation: roughly 60–75× more accurate.</p>
<p><strong>Logistic vulnerability curve, evaluated and declined (issue <a href="https://github.com/traitecoevo/plant/issues/467">#467</a>).</strong> A logistic curve was assessed as a cheaper, AD-friendly alternative. Its forward integral is elementary, but its inverse is <em>not</em> explicit, and for the production stem curve (<img src="https://latex.codecogs.com/png.latex?c%5Capprox1.09">) it biases the transpiration integral by 4–13% — a model change, not a numerical refinement. Recommendation: keep the Weibull for the core; revisit a logistic only in an external calibration/AD context.</p>
</section>
<section id="measured-outcome" class="level2">
<h2 class="anchored" data-anchor-id="measured-outcome">Measured outcome</h2>
<p>The early algorithmic stack (O(1) splines, memoisation, integral-spline caches, the off-path Brent work) took the model from its first working version to several-fold faster — an end-to-end ≈4.4× was measured on one benchmark build before the later solver work, with further gains from the integral-spline cache. The two newer superlinear-solver wins on the hot path then added roughly 1.8× and 1.2× on the 15-layer scenario, and the <code>E_from_Soil</code> division work a further few percent. Numerically, most adopted changes are bit-identical; the solver swaps and the analytical transform shift results at the <em>tolerance level</em> (sub-percent), and where they move, they move <em>toward</em> the more accurate reference. The model is now considered structurally optimised: ~75% of runtime is the nested root-find structure, and every bit-identical lever has been taken.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post is pinned to a <code>develop</code> SHA. Absolute timings are machine-specific and deliberately not reproduced here as headline numbers; the durable results are the <em>ratios</em> and the adopted/rejected verdicts. For the full record — exact A/B tables, profiles, and the reasoning behind each rejection — see the <code>root_water_uptake</code> branch history in the <a href="https://github.com/traitecoevo/plant">plant repository</a> and issues <a href="https://github.com/traitecoevo/plant/issues/467">#467</a> and <a href="https://github.com/traitecoevo/plant/issues/468">#468</a>.</p>
</div>
</div>


</section>

 ]]></description>
  <category>tf24</category>
  <category>performance</category>
  <category>hydraulics</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-19-root-water-uptake-performance/</guid>
  <pubDate>Fri, 19 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A reference prototype for root water uptake</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-19-root-water-uptake-prototype/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop 81cfeab</span></p>
<p>This post preserves the standalone R <em>prototype</em> of the soil→root→stem→leaf hydraulic model that has since been ported to C++ (<code>src/leaf_model.cpp</code>: <code>E_from_Soil_to_Root_Collar</code>, <code>find_root_collar_psi</code>) and wired into the TF24 strategy. It follows Potkay et al.&nbsp;(2021) and is kept as the reference implementation / sanity check for the compiled model. For the consolidated maths see <a href="../../theory/assimilation-hydraulics.html">assimilation and hydraulics</a>.</p>
<p>Structure of the prototype:</p>
<ol type="1">
<li><strong>Geometry &amp; conductances</strong>: derive stem (<code>k_sw_max</code>) and leaf (<code>k_l</code>) hydraulic conductances from height/diameter allometry.</li>
<li><strong>Root collar pressure at E = 0</strong>: bisection search for the collar potential (<code>P_x_r</code>) where net soil exchange is zero (the wettest feasible point).</li>
<li><strong>Critical root collar pressure</strong>: bisection for the collar potential at which the leaf reaches its critical water potential (<code>P_x_l_crit</code>), integrating the pressure drop down the stem and through the leaf in <code>n</code> numerical steps.</li>
<li><strong>Profit curve</strong>: sweep <code>P_x_r</code> from the E = 0 point to critical, compute assimilation (Farquhar / Cowan-Farquhar marginal cost) vs hydraulic cost, and pick the profit-maximising operating point.</li>
</ol>
<p>The <strong>Test</strong> section at the end cross-checks the compiled <code>Leaf</code> object against this prototype.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The whole prototype is preserved as a readable reference. The chunks that load the prototype’s helper functions, run the numerical solves, and validate against the compiled <code>Leaf</code> are set to <code>#| eval: false</code> — they depend on machine-specific <code>source()</code> paths and the compiled object, and would be slow. The code is intact so it can be re-run after fixing those paths and building <code>plant</code>.</p>
</div>
</div>
<p>The prototype sources its helper functions (<code>E_from_Soil_to_Root_Collar</code>, the vulnerability curves <code>VC_l</code>/<code>VC_sw</code>/<code>VC_r</code>) from a working copy of <code>plant</code>. The original paths point at a different machine and must be edited to run, so this chunk is not executed.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/z3533545/Documents/GitHub/plant/R/leaf.R"</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/z3533545/Documents/GitHub/plant/R/root_uptake.R"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">H <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.44</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Height (m)</span></span>
<span id="cb3-2">LA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Leaf area (m^2)</span></span>
<span id="cb3-3">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n steps for numerical integration</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">b2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.3e-1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Power-law scaling coefficient for stem xylem conductance (mol H2o s^-1 MPa^-1)</span></span>
<span id="cb4-2">c2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.93</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exponent for power-law scaling of stem conductance</span></span>
<span id="cb4-3">D_ref <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reference diameter (m)?</span></span>
<span id="cb4-4">b0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">64.6</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Power-law scaling coefficient for height (m)</span></span>
<span id="cb4-5">c0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6411</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exponent for power-law scaling of height (unitless)</span></span>
<span id="cb4-6">D <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> D_ref <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (H<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>b0)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c0) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Diameter (m)?</span></span>
<span id="cb4-7">D_hw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> D<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Heartwood diameter (m)?</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># got from supps in Potkay et al. (2021)</span></span>
<span id="cb5-2">k_sw_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> b2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D_ref) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> c2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (D_hw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> (c2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Psi soil for layers starting from the top</span></span>
<span id="cb6-2">P_soil <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MPa</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Depth of middle of layer</span></span>
<span id="cb7-2">z_soil_mid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the minimum (driest soil layer)</span></span>
<span id="cb8-2">P_s_min <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(P_soil)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the depth of the driest soil layer</span></span>
<span id="cb9-2">z_P_s_min <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> z_soil_mid[P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_s_min]</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If the are multiple layers which are equally driest, define it as the deepest one</span></span>
<span id="cb9-5">z_P_s_min <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(z_P_s_min)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the depth of the driest soil layer</span></span>
<span id="cb10-2">P_s_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(P_soil)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the depth of the wettest soil layer</span></span>
<span id="cb11-2">z_P_s_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> z_soil_mid[P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_s_max]</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If the are multiple layers which are equally wettest, define it as the deepest one</span></span>
<span id="cb11-5">z_P_s_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(z_P_s_max)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Start defining the boundary conditions for the bisection search for the optimum root collar pressure (P_x_r)</span></span>
<span id="cb12-2"></span>
<span id="cb12-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The upper value (i.e. least negative value) is equal to the wettest soil layer</span></span>
<span id="cb12-4">P_x_r_UB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_s_max</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Density of water</span></span>
<span id="cb13-2">rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e3</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#kg m^-3</span></span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Acceleration due to gravity</span></span>
<span id="cb13-5">g <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.8</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m s^-2</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The lower boundary value of root collar pressure (i.e most negative) which is equivalent to the driest soil layer minus the gravitational potential</span></span>
<span id="cb14-2">P_x_r_LB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_s_min <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z_P_s_min <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If the intial boundary values too narrow (diff &lt; 1), make the difference equal to one</span></span>
<span id="cb15-2"></span>
<span id="cb15-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(P_x_r_LB_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_r_UB_0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>){</span>
<span id="cb15-4">    P_x_r_LB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_UB_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb15-5">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the middle root collar pressure between the two boundaries</span></span>
<span id="cb16-2"></span>
<span id="cb16-3">P_x_r_M_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (P_x_r_UB_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_x_r_LB_0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set up a vector of root collar pressure</span></span>
<span id="cb17-2"></span>
<span id="cb17-3">P_x_r_3_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(P_x_r_UB_0, P_x_r_M_0, P_x_r_LB_0)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a vector of transpiration values</span></span>
<span id="cb18-2"></span>
<span id="cb18-3">E_3_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># While loop continues until transpiration vector includes a negative and a positive value</span></span>
<span id="cb19-2"></span>
<span id="cb19-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>((<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-4">      (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb19-5"></span>
<span id="cb19-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iterate across the three root collar values</span></span>
<span id="cb19-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r_3_0)){</span>
<span id="cb19-8"></span>
<span id="cb19-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start with upper boundary first, through to lower</span></span>
<span id="cb19-10"></span>
<span id="cb19-11">    P_x_r_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[i]</span>
<span id="cb19-12"></span>
<span id="cb19-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate soil to root transpiration value for a given root collar psi</span></span>
<span id="cb19-14">    E_3_0[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(P_x_r_i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb19-15">  }</span>
<span id="cb19-16"></span>
<span id="cb19-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If no negative transpiration values then increase upper boundary root collar psi by an increment</span></span>
<span id="cb19-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb19-19">    P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>;</span>
<span id="cb19-20">  }</span>
<span id="cb19-21"></span>
<span id="cb19-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If no postive transpiration values then decrease lower boundary root collar psi by an increment</span></span>
<span id="cb19-23">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb19-24">    P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>;</span>
<span id="cb19-25">  }</span>
<span id="cb19-26"></span>
<span id="cb19-27">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define new midsection root collar value</span></span>
<span id="cb19-28">  P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb19-29">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set i at 0 (iterator for bisection search below)</span></span>
<span id="cb20-2">i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Iterate until dP_x_r_3_0 brought within tolerance (1e-3)</span></span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This bisection is to find the root collar pressure when transpiration equals zero</span></span>
<span id="cb20-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(P_x_r_3_0)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(P_x_r_3_0)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>){</span>
<span id="cb20-8"></span>
<span id="cb20-9">  i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb20-10"></span>
<span id="cb20-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stop if iteration goes too long</span></span>
<span id="cb20-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>){</span>
<span id="cb20-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>)</span>
<span id="cb20-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>()</span>
<span id="cb20-15">  }</span>
<span id="cb20-16"></span>
<span id="cb20-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Must have a negative and positive value, if not stop</span></span>
<span id="cb20-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb20-19">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>()</span>
<span id="cb20-20">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb20-21">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>()</span>
<span id="cb20-22">  }</span>
<span id="cb20-23"></span>
<span id="cb20-24"></span>
<span id="cb20-25"></span>
<span id="cb20-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the upper bound of transpiration in iteration, i.e. highest positive value</span></span>
<span id="cb20-27">  E_UB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]);</span>
<span id="cb20-28"></span>
<span id="cb20-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the lower bound of transpiration in iteration, i.e. lowest negative value</span></span>
<span id="cb20-30">  E_LB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]);</span>
<span id="cb20-31"></span>
<span id="cb20-32"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Root collar xylem pressures that correspond to ZERO transpiration rates</span></span>
<span id="cb20-33">    P_x_r_UB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> E_UB_0];</span>
<span id="cb20-34">    P_x_r_UB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(P_x_r_UB_0);</span>
<span id="cb20-35">    P_x_r_LB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> E_LB_0];</span>
<span id="cb20-36">    P_x_r_LB_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(P_x_r_LB_0);</span>
<span id="cb20-37">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mid-point for bisection</span></span>
<span id="cb20-38">    P_x_r_M_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (P_x_r_UB_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_x_r_LB_0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb20-39"></span>
<span id="cb20-40">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reconstruct range</span></span>
<span id="cb20-41"></span>
<span id="cb20-42">    P_x_r_3_0_new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(P_x_r_UB_0, P_x_r_M_0, P_x_r_LB_0);</span>
<span id="cb20-43"></span>
<span id="cb20-44">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If reconstructed range is same as original, stop</span></span>
<span id="cb20-45"></span>
<span id="cb20-46">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(P_x_r_3_0_new <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_r_3_0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>){</span>
<span id="cb20-47">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>()</span>
<span id="cb20-48">  }</span>
<span id="cb20-49"></span>
<span id="cb20-50">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find E based on middle of root collar psi range</span></span>
<span id="cb20-51"></span>
<span id="cb20-52">  P_x_r_3_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0_new;</span>
<span id="cb20-53"></span>
<span id="cb20-54">  E_3_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(E_UB_0, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, E_LB_0)</span>
<span id="cb20-55"></span>
<span id="cb20-56">  P_x_r_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>];</span>
<span id="cb20-57"></span>
<span id="cb20-58">  E_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(P_x_r_i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb20-59"></span>
<span id="cb20-60">  E_3_0[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> E_i</span>
<span id="cb20-61"></span>
<span id="cb20-62">  }</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Numerical soluution means that E_0 may not be perfectly zero, find the smallest postive E value</span></span>
<span id="cb21-2">E_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(E_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find the root collar pressure associated with the smallest positive E value</span></span>
<span id="cb22-2">P_x_r_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_0[E_3_0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> E_0];</span>
<span id="cb22-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If multiple root collar associated with that E, choose the most negative</span></span>
<span id="cb22-4">P_x_r_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(P_x_r_0);</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialise a critical leaf water potential value, it will become more negative by iterating the vulnerarbility curve below</span></span>
<span id="cb23-2">P_x_l_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Iterate P_x_l_crit increasingly negative until the fraction of conductance is 0.001, very low fracution</span></span>
<span id="cb24-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(P_x_l_crit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>)){</span>
<span id="cb24-3">    P_x_l_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>;</span>
<span id="cb24-4">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inital guesses for upper and lower bounds of critical root collar pressure for bisection method</span></span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the upper bound is the value at which transpiration is = 0</span></span>
<span id="cb25-4"></span>
<span id="cb25-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the lower bound is the critical leaf water potential</span></span>
<span id="cb25-6">P_x_r_UB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_0;</span>
<span id="cb25-7">P_x_r_LB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_crit;</span>
<span id="cb25-8">P_x_r_M_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (P_x_r_LB_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_x_r_UB_crit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">P_x_r_3_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(P_x_r_UB_crit, P_x_r_M_crit, P_x_r_LB_crit);</span>
<span id="cb26-2">P_x_l_3_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>);</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">k_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6e-2</span></span>
<span id="cb27-2"></span>
<span id="cb27-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>){</span>
<span id="cb27-4">  P_x_r_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_crit[i];</span>
<span id="cb27-5">  E_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(P_x_r_i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb27-6"></span>
<span id="cb27-7">  P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_i;</span>
<span id="cb27-8"></span>
<span id="cb27-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb27-10">    P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (E_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LA) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)));</span>
<span id="cb27-11">  }</span>
<span id="cb27-12"></span>
<span id="cb27-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb27-14">    P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E_i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>k_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)));</span>
<span id="cb27-15">  }</span>
<span id="cb27-16"></span>
<span id="cb27-17">  P_x_l_3_crit[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i</span>
<span id="cb27-18"></span>
<span id="cb27-19">  }</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>;</span>
<span id="cb28-2"></span>
<span id="cb28-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(P_x_r_3_crit)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">range</span>(P_x_r_3_crit)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>){</span>
<span id="cb28-4"></span>
<span id="cb28-5">  i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb28-6"></span>
<span id="cb28-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>){</span>
<span id="cb28-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"error"</span>)</span>
<span id="cb28-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>()</span>
<span id="cb28-10">    }</span>
<span id="cb28-11"></span>
<span id="cb28-12">  F_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_l_3_crit; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#objective function, F -- we seek to find that P_x_r that makes F equal to zero</span></span>
<span id="cb28-13"></span>
<span id="cb28-14">    F_UB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(F_value[F_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]);</span>
<span id="cb28-15">    F_UB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> F_UB[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb28-16">    F_LB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(F_value[F_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]);</span>
<span id="cb28-17">    F_LB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> F_LB[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb28-18"></span>
<span id="cb28-19">  P_x_r_UB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_crit[F_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> F_UB];</span>
<span id="cb28-20">  P_x_r_UB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_UB_crit[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb28-21"></span>
<span id="cb28-22">  P_x_r_LB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_crit[F_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> F_LB];</span>
<span id="cb28-23">  P_x_r_LB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_LB_crit[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb28-24"></span>
<span id="cb28-25">P_x_r_M_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (P_x_r_LB_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_x_r_UB_crit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb28-26"></span>
<span id="cb28-27">P_l_UB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_3_crit[P_x_r_3_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_r_UB_crit];</span>
<span id="cb28-28">P_l_LB_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_3_crit[P_x_r_3_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_r_LB_crit];</span>
<span id="cb28-29"></span>
<span id="cb28-30"></span>
<span id="cb28-31">  P_x_r_3_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(P_x_r_UB_crit, P_x_r_M_crit, P_x_r_LB_crit);</span>
<span id="cb28-32">    P_x_l_3_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(P_l_UB_crit, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, P_l_LB_crit);</span>
<span id="cb28-33"></span>
<span id="cb28-34">  P_x_r_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_3_crit[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>];</span>
<span id="cb28-35"></span>
<span id="cb28-36">    E_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(P_x_r_i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb28-37"></span>
<span id="cb28-38">      P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_i;</span>
<span id="cb28-39"></span>
<span id="cb28-40">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb28-41">        P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (E_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LA) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)));</span>
<span id="cb28-42">      }</span>
<span id="cb28-43"></span>
<span id="cb28-44">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb28-45">              P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)));</span>
<span id="cb28-46"></span>
<span id="cb28-47">      }</span>
<span id="cb28-48"></span>
<span id="cb28-49">      P_x_l_3_crit[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i;</span>
<span id="cb28-50"></span>
<span id="cb28-51">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">P_x_r_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(P_x_r_3_crit[P_x_l_3_crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]);</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">P_x_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(P_x_r_0, P_x_r_crit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>);</span>
<span id="cb30-2">P_x_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r))</span>
<span id="cb30-3">P_x_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r))</span>
<span id="cb30-4">E <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r))</span>
<span id="cb30-5">E_soil <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r));</span>
<span id="cb30-6">r_R_H <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r));</span>
<span id="cb30-7">r_R_V <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r));</span>
<span id="cb30-8">f_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r));</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_r)){</span>
<span id="cb31-2">  P_x_r_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r[i];</span>
<span id="cb31-3"></span>
<span id="cb31-4">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(P_x_r_i, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)</span>
<span id="cb31-5"></span>
<span id="cb31-6">  E[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> res[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb31-7">  E_soil[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> res[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]];</span>
<span id="cb31-8">  r_R_H[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> res[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]];</span>
<span id="cb31-9">  r_R_V[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> res[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]];</span>
<span id="cb31-10">  f_r[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> res[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]];</span>
<span id="cb31-11"></span>
<span id="cb31-12">  P_x_s_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r_i;</span>
<span id="cb31-13"></span>
<span id="cb31-14">   <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb31-15">        P_x_s_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_s_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (E_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LA) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)));</span>
<span id="cb31-16">   }</span>
<span id="cb31-17"></span>
<span id="cb31-18">  P_x_s[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_s_i;</span>
<span id="cb31-19"></span>
<span id="cb31-20">  P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_s_i;</span>
<span id="cb31-21"></span>
<span id="cb31-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb31-23">        P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)));</span>
<span id="cb31-24">    }</span>
<span id="cb31-25"></span>
<span id="cb31-26">  P_x_l[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i;</span>
<span id="cb31-27">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">T_a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span></span>
<span id="cb32-2"></span>
<span id="cb32-3">T_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> T_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_l)); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [C] -- assuming leaf temperature equals air temperature</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">satur_mol_fract_air <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0043</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0511</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T_a)); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#saturated mole fraction of vapor in air [kPa]</span></span>
<span id="cb33-2">satur_mol_fract_leaf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0043</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0511</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T_l)); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#saturated mole fraction of vapor in leaf [kPa]</span></span>
<span id="cb33-3">RH <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>);</span>
<span id="cb33-4">VPD_air <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> RH) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.61094</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.625</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (T_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">243.04</span>)); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#air-to-air vapor pressure deficit [kPa]</span></span>
<span id="cb33-5">VPD_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> satur_mol_fract_leaf <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> satur_mol_fract_air <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> VPD_air; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#leaf-to-air vapor pressure deficit in kPa</span></span>
<span id="cb33-6">VPD_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, VPD_l);</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">atm_kpa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.3</span></span>
<span id="cb34-2"></span>
<span id="cb34-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interpreting stomatal conductances, G_w and G_c</span></span>
<span id="cb34-4">G_w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(E<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>atm_kpa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> VPD_l); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#stomatal conductance to vapor [mol H2O * m^-2 * s^-1]</span></span>
<span id="cb34-5">G_w[E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>;</span>
<span id="cb34-6">G_w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, G_w);</span>
<span id="cb34-7">G_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> G_w <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#stomatal conductance to CO2 [mol CO2 * m^-2 * s^-1]</span></span></code></pre></div></div>
</div>
<!-- dropped: scratch chunk calling solve_ci() was commented out in source -->
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interpreting stomatal conductances, G_w and G_c</span></span>
<span id="cb35-2">G_w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> VPD_l); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#stomatal conductance to vapor [mol H2O * m^-2 * s^-1 * kPa^-1]</span></span>
<span id="cb35-3">G_w[E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>;</span>
<span id="cb35-4">G_w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, G_w);</span>
<span id="cb35-5">G_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> G_w <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#stomatal conductance to CO2 [mol CO2 * m^-2 * s^-1 * kPa^-1]</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">V_cmax <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span></span>
<span id="cb36-2">J_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">167</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-6</span></span>
<span id="cb36-3">R_abs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb36-4">var_kappa <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.9e-7</span></span>
<span id="cb36-5">J_phi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> var_kappa <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> R_abs; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#var_kappa is proportionality constant between J and R_abs in [mol CO2 * J^-1]</span></span>
<span id="cb36-6">c_prime2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.97</span></span>
<span id="cb36-7">J <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (J_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> J_phi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((J_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> J_phi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J_phi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime2); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#hyperbolic minimum of J_max and J_phi with curvature of 0.9</span></span>
<span id="cb36-8">Gamma_star <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">36e-6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.325</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [kPa]</span></span>
<span id="cb36-9">K_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">275e-6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.325</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [kPa]</span></span>
<span id="cb36-10">K_o <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">420000e-6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.325</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [kPa]</span></span>
<span id="cb36-11">R_d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> V_cmax; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#leaf dark respiration in [mol CO2 * m^-2 * s^-1]</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Eller et al. (2019; SOX appendix)</span></span>
<span id="cb37-2">o_a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span></span>
<span id="cb37-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gamma_star = 42.75</span></span>
<span id="cb37-4">c_a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">410e-6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.325</span></span>
<span id="cb37-5"></span>
<span id="cb37-6">a_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> V_cmax;</span>
<span id="cb37-7">a_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> J <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>;</span>
<span id="cb37-8">b_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> K_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> o_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> K_o);</span>
<span id="cb37-9">b_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Gamma_star;</span>
<span id="cb37-10">beta_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R_d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> a_c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_c);</span>
<span id="cb37-11">beta_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R_d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> a_j) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_j);</span>
<span id="cb37-12">gamma_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> a_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Gamma_star) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_c);</span>
<span id="cb37-13">gamma_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> a_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Gamma_star) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_j);</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">A_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>beta_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((beta_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> gamma_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> G_c)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>;</span>
<span id="cb38-2">A_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>beta_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((beta_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> gamma_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> G_c)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>;</span>
<span id="cb38-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A_j = real(A_j);</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># correct for if G_c == inf, in which case c_i = c_a</span></span>
<span id="cb39-2">A_c[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> a_c[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Gamma_star[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_c[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_d[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>];</span>
<span id="cb39-3">A_j[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> a_j[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Gamma_star[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_j[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_d[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>];</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#calculate photosynthesis by hyperbolic-minimum</span></span>
<span id="cb40-2"></span>
<span id="cb40-3">c_prime1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.97</span></span>
<span id="cb40-4"></span>
<span id="cb40-5">A_n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_j)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_j)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#net carbon assimilation rate [mol CO2 * m^-2 * s^-1]</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># determine dA_n/dR_abs by chain-rule, assuming negligle effect of</span></span>
<span id="cb41-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># electron-transport rate, J, on stomatal conductance, G_c (derrived</span></span>
<span id="cb41-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># assuming dG_c/dJ = 0)</span></span>
<span id="cb41-4">dA_n_dA_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_j) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ((A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_j)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_j)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1);</span>
<span id="cb41-5">dA_n_dA_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((A_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ((A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_j)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A_j)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime1);</span>
<span id="cb41-6">var_H <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (J<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Gamma_star) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_d) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb41-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#inv_Ohm = J/4 .* (c_a - Gamma_star) - R_d .* (c_a + 2 * Gamma_star);</span></span>
<span id="cb41-8">dA_j_dJ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Gamma_star)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (var_H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> A_j);</span>
<span id="cb41-9">dA_j_dJ[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Gamma_star[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b_j[G_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]);</span>
<span id="cb41-10">dJ_dJ_phi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((J_phi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J_max) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ((J_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> J_phi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J_phi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> c_prime2);</span>
<span id="cb41-11">dJ_phi_dR_abs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> var_kappa;</span>
<span id="cb41-12">dA_n_dR_abs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> dA_n_dA_j <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dA_j_dJ <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dJ_dJ_phi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dJ_phi_dR_abs;</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#determine Cowan &amp; Farquhar's (1977) marginal cost (lambda), dA_n/dE, by finite-difference</span></span>
<span id="cb42-2">c_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> c_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> A_n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> G_c;</span>
<span id="cb42-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dA_c_dc_i = a_c .* (Gamma_star + b_c) ./ (c_i + b_c).^2;</span></span>
<span id="cb42-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dA_j_dc_i = a_j .* (Gamma_star + b_j) ./ (c_i + b_j).^2;</span></span>
<span id="cb42-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x = dA_n_dA_c .* dA_c_dc_i + dA_n_dA_j .* dA_j_dc_i; %%% dA_n_dc_i</span></span>
<span id="cb42-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dA_n_dE = A_n ./ E .* x ./ (x + G_c); %%%Eq. 4 in Buckley et al. (2016) "Optimal Plant Water Economy"</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1">f_sw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> LA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> k_sw_max; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#%[unitless]</span></span>
<span id="cb43-2">f_sw[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(P_x_r[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], P_x_s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)))); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#different defintion since first value represents where E = 0</span></span>
<span id="cb43-3">f_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>  E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (P_x_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_l) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> k_l; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#[unitless]</span></span>
<span id="cb43-4">f_l[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#different defintion since first value represents where E = 0</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">dP <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-2</span>;</span>
<span id="cb44-2">dVC_r_dP_x_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_r))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dP;</span>
<span id="cb44-3">dVC_r_dP_soil <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_soil))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dP;</span>
<span id="cb44-4">dVC_r_dP_soil <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(dVC_r_dP_soil, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_l)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(dVC_r_dP_soil));</span>
<span id="cb44-5">d2VC_r_dP_soil2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> dP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_soil))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dP<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>;</span>
<span id="cb44-6">d2VC_r_dP_soil2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(d2VC_r_dP_soil2, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(P_x_l)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(d2VC_r_dP_soil2));</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1">df_r_dP_x_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (f_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_r</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_r))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_r); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [MPa^-1]</span></span>
<span id="cb45-2">df_r_dP_x_r[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_soil) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dVC_r_dP_soil[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_soil) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-2</span>]; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#needs to be corrected where P_x_r = P_soil</span></span>
<span id="cb45-3">d2f_r_dP_x_r2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>df_r_dP_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> dVC_r_dP_x_r) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (P_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_r); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [MPa^-2]</span></span>
<span id="cb45-4">d2f_r_dP_x_r2[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_soil) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> d2VC_r_dP_soil2[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_soil) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-1</span>]; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#needs to be corrected where P_x_r = P_soil</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">df_sw_dP_x_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (f_sw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_s))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_s); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [MPa^-1]</span></span>
<span id="cb46-2">df_sw_dP_x_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_r)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f_sw) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (P_x_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_x_s); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#in [MPa^-1]</span></span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1">r_R_V_sum <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> r_R_V</span>
<span id="cb47-2"></span>
<span id="cb47-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(r_R_V)){</span>
<span id="cb47-4">  r_R_V_sum[,i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(r_R_V[,i])</span>
<span id="cb47-5">}</span>
<span id="cb47-6"></span>
<span id="cb47-7">r_R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> r_R_H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> r_R_V_sum;</span>
<span id="cb47-8"></span>
<span id="cb47-9">a_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> LA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (f_sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> df_sw_dP_x_s;</span>
<span id="cb47-10">b_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> LA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (f_sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> df_sw_dP_x_r;</span>
<span id="cb47-11">c_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>LA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E_soil <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_R_H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> f_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> df_r_dP_x_r) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> r_R, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">MARGIN =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> sum);</span>
<span id="cb47-12">d_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_s)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> f_l;</span>
<span id="cb47-13">e_var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> f_l;</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1">k_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> e_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>f_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d_var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>a_var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(b_var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> LA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>f_sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_sw_max));</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1">c0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> c_var[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb49-2">i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(c_var))[(c_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> c0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(c_var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> c0))]</span>
<span id="cb49-3"></span>
<span id="cb49-4">P_x_r0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r[i];</span>
<span id="cb49-5">f_sw0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> f_sw[i];</span>
<span id="cb49-6">f_r0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> f_r[,i];</span>
<span id="cb49-7">df_r0_dP_x_r0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> df_r_dP_x_r[,i];</span>
<span id="cb49-8">d2f_r0_dP_x_r0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> d2f_r_dP_x_r2[,i];</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1">P_x_l0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>H<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>;</span>
<span id="cb50-2">f_l0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l0));</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1">E_soil0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> E_soil[,i];</span>
<span id="cb51-2">r_R0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> r_R[,i];</span>
<span id="cb51-3">r_R_H0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> r_R_H[,i];</span>
<span id="cb51-4">r_R_V0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> r_R_V[,i];</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1">c_r_H <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb52-2">c_r_V <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb52-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## CHECK</span></span>
<span id="cb52-4">dP_x_r0_dc_r_H <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> c_r_H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> E_soil0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r_R_H0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> r_R0;</span>
<span id="cb52-5">dP_x_r0_dc_r_V <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> c_r_V <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>((E_soil0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_R_V0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> r_R0)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(E_soil0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_R_V0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> r_R0)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]);</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1">k_cmax <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>f_l0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> LA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>f_sw0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_sw_max)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1">df_l0_dP_x_r0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> dP)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l0))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dP; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#calculated in a finite-difference approach</span></span>
<span id="cb54-2">df_l0_dH <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> df_l0_dP_x_r0;</span>
<span id="cb54-3">df_sw0_dP_x_r0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_r0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l0))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (rho<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>H<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>);</span>
<span id="cb54-4">df_sw0_dH <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l0)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> f_sw0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> H;</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1">c0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.64</span></span>
<span id="cb55-2">c1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.63</span></span>
<span id="cb55-3">dH_dD <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D;</span>
<span id="cb55-4">dW_dD <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> c1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> W <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D;</span>
<span id="cb55-5">W <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb55-6">D_hw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span></span>
<span id="cb55-7">dk_sw_max_dD <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (c2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (c2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> c2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (D_hw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> (c2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (D_hw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> (c2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>c0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>));</span>
<span id="cb55-8"></span>
<span id="cb55-9">dk_cmax_dD <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> k_cmax<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((df_l0_dH<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_l<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(f_l0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> LA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>df_sw0_dH<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_sw_max<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(f_sw0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>dH_dD <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb55-10">             LA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(k_sw_max<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>f_sw0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>dk_sw_max_dD);</span>
<span id="cb55-11">dk_cmax_dLA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> k_cmax<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k_sw_max<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>f_sw0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(c0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>LA<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>r_R0));</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb56" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># at E = E_max</span></span>
<span id="cb56-2">P_x_lcrit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(P_x_l[P_x_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>]);</span>
<span id="cb56-3">A_nmax <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> A_n[P_x_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_lcrit];</span>
<span id="cb56-4">E_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> E[P_x_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_lcrit];</span>
<span id="cb56-5">dA_n_dE_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> dA_n_dE[P_x_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_lcrit];</span>
<span id="cb56-6">dA_n_dR_abs_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> dA_n_dR_abs[P_x_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> P_x_lcrit];</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1">G_wmin <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb57-2"></span>
<span id="cb57-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(A_nmax <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb57-4">    F_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> A_n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> A_nmax <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k_c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> k_cmax;</span>
<span id="cb57-5">    dF_dP_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(F_value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(P_x_l);</span>
<span id="cb57-6">    i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb57-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(dF_dP_l)){</span>
<span id="cb57-8"></span>
<span id="cb57-9">      i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb57-10"></span>
<span id="cb57-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(dF_dP_l)){</span>
<span id="cb57-12">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">error</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ERROR'</span>)</span>
<span id="cb57-13">        }</span>
<span id="cb57-14"></span>
<span id="cb57-15">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(dF_dP_l[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb57-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(G_w[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> G_wmin){</span>
<span id="cb57-17">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb57-18">        }</span>
<span id="cb57-19">      }</span>
<span id="cb57-20"></span>
<span id="cb57-21">    }</span>
<span id="cb57-22">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1">P_x_l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l[i];</span>
<span id="cb58-2">P_x_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_s[i];</span>
<span id="cb58-3">P_x_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_r[i];</span></code></pre></div></div>
</div>
<section id="test" class="level2">
<h2 class="anchored" data-anchor-id="test">Test</h2>
<p>This section cross-checks the compiled <code>Leaf</code> object against the prototype. It depends on a local build of <code>plant</code> (<code>devtools::load_all</code>), the compiled <code>Leaf</code> class, and the prototype state computed above, so every chunk is left unevaluated.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(plant)</span>
<span id="cb59-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb59-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb59-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb59-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb59-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load_all</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb61" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1">sw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb61-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vcmax_25 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  umol m^-2 s^-1</span></span>
<span id="cb61-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.680147</span>,                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb61-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.898245</span>,                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -MPa</span></span>
<span id="cb61-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_crit =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.870283</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -MPa</span></span>
<span id="cb61-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20000</span>,                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hydraulic cost for Bartlett method umol m^-3 s^-1</span></span>
<span id="cb61-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># exponent for effect of hydraulic risk  (unitless)</span></span>
<span id="cb61-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">jmax_25 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">157.44</span>,              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  maximum electron transport rate umol m^-2 s^-1</span></span>
<span id="cb61-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hk_s =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># maximum hydraulic-dependent sapwood turnover rate yr ^ -1</span></span>
<span id="cb61-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>,                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># quantum yield of photosynthetic electron transport  (mol mol^-1)</span></span>
<span id="cb61-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curv_fact_elec_trans =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># curvature factor for the light response curve  (unitless)</span></span>
<span id="cb61-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curv_fact_colim =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>,        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># curvature factor for the colimited photosythnthesis equatiom</span></span>
<span id="cb61-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GSS_tol_abs =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>,            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  ???</span></span>
<span id="cb61-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vulnerability_curve_ncontrol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ????</span></span>
<span id="cb61-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ci_abs_tol =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ???</span></span>
<span id="cb61-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ci_niter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb61-17">)</span>
<span id="cb61-18"></span>
<span id="cb61-19">leaf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Leaf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vcmax=</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>vcmax_25, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>c, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_crit =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>psi_crit,</span>
<span id="cb61-20">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta2 =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>beta2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">jmax_25 =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>jmax_25, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hk_s =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>hk_s, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>a, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curv_fact_elec_trans =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>curv_fact_elec_trans, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curv_fact_colim =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>curv_fact_colim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GSS_tol_abs =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GSS_tol_abs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vulnerability_curve_ncontrol =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>vulnerability_curve_ncontrol, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ci_abs_tol =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ci_abs_tol, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ci_niter =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ci_niter, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g1_TF24 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">46.32995</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb62" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_r =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.30</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb62-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">E =</span> purrr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(psi_r,</span>
<span id="cb62-3">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> psi_r_E</span>
<span id="cb62-4"></span>
<span id="cb62-5">psi_r_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb62-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> psi_r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> E)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb62-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb63" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb63-1">find_psi_leaf_from_root <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(psi_root, E){</span>
<span id="cb63-2">  leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_physiology</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">PPFD =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil =</span> psi_root, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_moist =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">leaf_specific_conductance_max =</span> k_sw_max, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_vpd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ca =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sapwood_volume_per_leaf_area =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8e-04</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">608</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a_bio =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0245</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">leaf_temp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_o2_kpa =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_kpa =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.3</span>)</span>
<span id="cb63-3">  psi_stem <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transpiration_to_psi_stem</span>(E<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>kg_2_mol_h20)</span>
<span id="cb63-4">  psi_stem <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transpiration_to_psi_stem</span>(E)</span>
<span id="cb63-5">  leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_leaf_states_rates_from_psi_stem</span>(psi_stem)</span>
<span id="cb63-6">  ci_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ci_</span>
<span id="cb63-7">  assim_colimited_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>assim_colimited_</span>
<span id="cb63-8">  hydraulic_cost_TF <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hydraulic_cost_TF</span>(psi_stem)</span>
<span id="cb63-9"></span>
<span id="cb63-10">    P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>psi_root;</span>
<span id="cb63-11"></span>
<span id="cb63-12">  n_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb63-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_i){</span>
<span id="cb63-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># P_x_l_i = P_x_l_i - E/(n_i*k_sw_max*((leaf$proportion_of_conductivity(psi = -P_x_l_i) +</span></span>
<span id="cb63-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                                        leaf$proportion_of_conductivity(psi = -(P_x_l_i- E/(n_i*k_sw_max*(leaf$proportion_of_conductivity(psi = -P_x_l_i)))))/2)));</span></span>
<span id="cb63-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb63-17">        P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(n_i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>k_sw_max<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">proportion_of_conductivity</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>P_x_l_i))));</span>
<span id="cb63-18">  }</span>
<span id="cb63-19"></span>
<span id="cb63-20"></span>
<span id="cb63-21"></span>
<span id="cb63-22"></span>
<span id="cb63-23"></span>
<span id="cb63-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_x_l_i=</span> P_x_l_i,</span>
<span id="cb63-25">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_stem =</span> psi_stem,</span>
<span id="cb63-26">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">assim_colimited_ =</span> assim_colimited_,</span>
<span id="cb63-27">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hydraulic_cost_TF =</span> hydraulic_cost_TF))</span>
<span id="cb63-28">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb64" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1">psi_r_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">324</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_l =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span>(psi_r, E, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find_psi_leaf_from_root</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>.x, .y)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_wider</span>(psi_l) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> psi_r_l_E</span>
<span id="cb64-5"></span>
<span id="cb64-6">psi_r_l_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_stem =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>psi_stem) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_x_l_i =</span> P_x_l_i) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_root_collar =</span> psi_r) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil_1 =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil_2 =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(psi_soil_1, psi_soil_2, psi_root_collar, psi_stem, P_x_l_i)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb64-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> E)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb64-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb64-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_vline(xintercept = 0.00320) +</span></span>
<span id="cb64-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb64-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> a</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb65" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1">psi_r_l_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb65-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">profit =</span> assim_colimited_ <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> hydraulic_cost_TF) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb65-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">E_max_profit =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(profit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(profit), E, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb65-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(profit,hydraulic_cost_TF, assim_colimited_)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb65-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> E)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb65-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> name))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb65-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> E_max_profit)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb65-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Carbon inputs and outputs"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb65-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> b</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb66" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb66-1">a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>b</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb67" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb67-1">find_psi_leaf_from_root <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(psi_root, E){</span>
<span id="cb67-2">  leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_physiology</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">PPFD =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil =</span> psi_root, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">leaf_specific_conductance_max =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.8e-05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_vpd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ca =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sapwood_volume_per_leaf_area =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8e-04</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">608</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a_bio =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0245</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">leaf_temp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_o2_kpa =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_kpa =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.3</span>)</span>
<span id="cb67-3"></span>
<span id="cb67-4">   P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> psi_root;</span>
<span id="cb67-5"></span>
<span id="cb67-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb67-7">        P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (H <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LA) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_sw_max <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_sw</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)))</span>
<span id="cb67-8">    }</span>
<span id="cb67-9"></span>
<span id="cb67-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb67-11">        P_x_l_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_x_l_i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k_l <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">VC_l</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P_x_l_i)))</span>
<span id="cb67-12">    }</span>
<span id="cb67-13">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb68" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb68-1">solve_root_crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb68-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_r =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(P_x_r_UB_crit,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>psi_crit,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb68-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">root_E =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(psi_r, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> res</span>
<span id="cb68-4"></span>
<span id="cb68-5">  res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transpiration_leaf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span>
<span id="cb68-6"></span>
<span id="cb68-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb68-8">      leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_physiology</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">PPFD =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>psi_r[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">leaf_specific_conductance_max =</span> k_sw_max, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_vpd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ca =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sapwood_volume_per_leaf_area =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8e-04</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">608</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a_bio =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0245</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">leaf_temp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_o2_kpa =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">atm_kpa =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">101.3</span>)</span>
<span id="cb68-9"></span>
<span id="cb68-10">    res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transpiration_leaf[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> leaf<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transpiration</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_stem =</span> sw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>psi_crit)</span>
<span id="cb68-11"></span>
<span id="cb68-12">  }</span>
<span id="cb68-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(res)</span>
<span id="cb68-14">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb69" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb69-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Solve the critical root pressure (i.e. the root pressure at which leaf crit is reached and E = E)</span></span>
<span id="cb69-2"></span>
<span id="cb69-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve_root_crit</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb69-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff =</span> root_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> transpiration_leaf) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb69-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(diff) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(diff))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb69-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(psi_r) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> root_crit</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb70" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb70-1">psi_r_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb70-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_l =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2_dbl</span>(psi_r, E, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find_psi_leaf_from_root</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>.x, .y)))</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb71" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb71-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_r =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(P_x_r_UB_crit,root_crit,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb71-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">E =</span> purrr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(psi_r,</span>
<span id="cb71-3">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E_from_Soil_to_Root_Collar</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_soil =</span> P_soil, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> g)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> psi_r_E</span>
<span id="cb71-4"></span>
<span id="cb71-5">psi_r_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb71-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> psi_r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> E)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb71-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb72" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb72-1">psi_r_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># slice(1:76) %&gt;%</span></span>
<span id="cb72-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_l =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span>(psi_r, E, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find_psi_leaf_from_root</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>.x, .y)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_wider</span>(psi_l) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> psi_r_l_E</span>
<span id="cb72-5"></span>
<span id="cb72-6">psi_r_l_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_stem =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>psi_stem) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_x_l_i =</span> P_x_l_i) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_root_collar =</span> psi_r) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil_1 =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">psi_soil_2 =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(psi_soil_1, psi_soil_2, psi_root_collar, psi_stem, P_x_l_i)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb72-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> E)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb72-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> name)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb72-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_vline(xintercept = 0.00320) +</span></span>
<span id="cb72-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb72-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> a</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb73" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb73-1">psi_r_l_E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb73-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">profit =</span> assim_colimited_ <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> hydraulic_cost_TF) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb73-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(profit,hydraulic_cost_TF, assim_colimited_)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb73-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> E)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb73-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> name))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb73-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_vline(xintercept = 0.00320) +</span></span>
<span id="cb73-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Carbon inputs and outputs"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb73-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> b</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb74" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb74-1">a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>b</span></code></pre></div></div>
</div>


</section>

 ]]></description>
  <category>tf24</category>
  <category>hydraulics</category>
  <category>prototype</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-19-root-water-uptake-prototype/</guid>
  <pubDate>Fri, 19 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Reviewing the TF24 leaf model code</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-19-tf24-leaf-code-review/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop 81cfeab</span></p>
<p>This post summarises a structural code review of the two core TF24 hydraulics sources in <code>plant</code>: <code>src/leaf_model.cpp</code> (the plant hydraulic solver) and <code>src/tf24_strategy.cpp</code> (the strategy/allocation layer that drives it). The review targeted structure, clarity, and maintainability rather than new science — with speed called out only where existing profiling supported it.</p>
<p>Because both files sit on a numerically sensitive hot path, the governing constraint throughout was that every accepted change be validated <strong>bit-identically</strong> against <code>HEAD</code> (or its trajectory shift quantified) before being considered done. The headline result: eleven findings resolved, the science provably unchanged.</p>
<section id="what-changed-by-class-of-finding" class="level2">
<h2 class="anchored" data-anchor-id="what-changed-by-class-of-finding">What changed, by class of finding</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Class</th>
<th>What was addressed</th>
<th>Outcome</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Caching redundant work</td>
<td>Temperature/O2-dependent photosynthesis params recomputed every <code>set_physiology</code> call</td>
<td>Cached on <code>(leaf_temp_, atm_o2_kpa_)</code>; bit-identical, speed-neutral, kept for clarity</td>
</tr>
<tr class="even">
<td>Heap-allocation reuse</td>
<td>Per-call <code>mass_root_prop_</code> vector allocated in <code>net_mass_production_dt</code></td>
<td>Promoted to a reused member buffer; hygiene, not a measured speed win</td>
</tr>
<tr class="odd">
<td>Redundant builders (DRY)</td>
<td><code>setup_transpiration</code> / <code>setup_root_vulnerability</code> duplicated the knot-grid + gamma cumulative-integral build</td>
<td>Factored into a shared <code>build_cumulative_vulnerability_integral</code> helper</td>
</tr>
<tr class="even">
<td>Duplicated logic</td>
<td>Triple-inlined “shut down at psi_crit” early-exit in <code>find_root_collar_psi</code></td>
<td>Extracted <code>set_shutdown_state(...)</code> helper</td>
</tr>
<tr class="odd">
<td>Variable shadowing</td>
<td>Local <code>hydraulic_cost_</code> shadowing the member in <code>profit_psi_stem_*</code></td>
<td>Renamed locals to <code>cost</code></td>
</tr>
<tr class="even">
<td>Magic constants</td>
<td>Hard-coded root-distribution numbers and a duplicated unit factor</td>
<td>Named file-scope constants; unit factor documented and left inline (FP rounding)</td>
</tr>
<tr class="odd">
<td>Unit divergence</td>
<td><code>E_up_</code> in kg vs per-layer consumption in mol</td>
<td>Named <code>kg_per_mol_h2o</code>; intentional divergence documented at both sites</td>
</tr>
<tr class="even">
<td>Sign conventions</td>
<td><code>root_collar_psi_</code> and <code>opt_psi_stem_</code> flipped sign by code path</td>
<td>Made sign-consistent across all branches; diagnostic-only change</td>
</tr>
<tr class="odd">
<td>Naming / hygiene</td>
<td><code>soil_depth</code> arg that was really a layer index; signed/unsigned loop comparison; truncated comment</td>
<td>Renamed to <code>soil_layer</code>, loop types aligned, comment completed</td>
</tr>
</tbody>
</table>
<p>Two further items from the original log were resolved by being <strong>rejected</strong> or deferred on profiling grounds — the <code>hydraulic_cost_TF</code> inner-loop <code>exp/pow</code> and the <code>isfinite</code> guards in the transport hot loop — and are tracked in the companion performance notes rather than here.</p>
</section>
<section id="key-takeaways" class="level2">
<h2 class="anchored" data-anchor-id="key-takeaways">Key takeaways</h2>
<ul>
<li><strong>Bit-identical was the bar, not a bonus.</strong> Most changes were proven exact by construction (same arithmetic, same accumulation order). Where a change would have reordered a floating-point summation — collapsing the <code>60*60*12*365</code> unit product, or moving a kg conversion inside a per-layer loop — it was <em>deliberately not made</em>, because the adaptive ODE amplifies the rounding difference. Documenting why something stays “ugly” turned out to be as valuable as cleaning it up.</li>
<li><strong>The sign-convention fix was the only correctness finding.</strong> The apparent <code>transpiration</code> / <code>transpiration_to_psi_stem</code> discrepancy was confirmed <em>not</em> a bug — two domain-natural conventions, internally consistent. The genuine defect was diagnostic aux columns (<code>root_collar_psi_</code>, <code>opt_psi_stem_</code>) that stored signed-negative vs positive-magnitude potentials depending on code path. These are written out but never read back into any rate, so the fix is diagnostic-only — every driver column verified <code>max|Δ| = 0</code>.</li>
<li><strong>“Speed” findings were mostly clarity findings.</strong> Profiling showed the nested solver dominates; caching ~8 transcendentals and preallocating buffers were measured as washes. They were kept for code health, with the speed claims explicitly retracted in the record.</li>
<li><strong>A shared sign-conventions reference block</strong> now sits above <code>E_from_Soil_to_Root_Collar</code>, and a new <code>test-leaf.r</code> case (wet soil, zero light) locks in the sign relationships so the wart cannot silently return.</li>
</ul>
</section>
<section id="where-the-detail-lives" class="level2">
<h2 class="anchored" data-anchor-id="where-the-detail-lives">Where the detail lives</h2>
<p>This is a high-level summary. The per-item analysis — line references, before/after rationale, validation digests, and the deliberate non-changes — lives in the original review document and the commit history in the <code>plant</code> repository:</p>
<p><a href="https://github.com/traitecoevo/plant" class="uri">https://github.com/traitecoevo/plant</a></p>
<p>The review was conducted against the <code>develop</code> commit badged above; consult the commit history around that point for the line-by-line changes and the A/B validation records referenced here.</p>


</section>

 ]]></description>
  <category>tf24</category>
  <category>code-review</category>
  <category>hydraulics</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-19-tf24-leaf-code-review/</guid>
  <pubDate>Fri, 19 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Optimising the FF16 hot paths</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-18-ff16-hot-path-optimisation/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop f8dbc4c</span></p>
<p>This post summarises a profiling-driven optimisation pass over the FF16 solver (<a href="https://github.com/traitecoevo/plant/pull/471">PR #471</a>). The work was guided by <code>Rprof()</code> plus native <code>/usr/bin/sample</code> profiles of the <code>run_plant_benchmarks()</code> FF16 scenarios, and the governing constraint was that every accepted change be <strong>bit-identical</strong> against <code>HEAD</code> (or its trajectory shift quantified) before being kept. The headline result: roughly <strong>2.7-3.5x</strong> on the FF16 benchmarks, with the science provably unchanged.</p>
<section id="headline-numbers" class="level2">
<h2 class="anchored" data-anchor-id="headline-numbers">Headline numbers</h2>
<p>Measured as one-iteration <code>bench::mark()</code> timings (FF16 scenario):</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Stage</th>
<th style="text-align: right;"><code>scm</code></th>
<th style="text-align: right;"><code>build_schedule</code></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Original baseline (<code>4b5c43d1</code>)</td>
<td style="text-align: right;">242 ms</td>
<td style="text-align: right;">656 ms</td>
</tr>
<tr class="even">
<td>After the optimisation pass</td>
<td style="text-align: right;">~85 ms</td>
<td style="text-align: right;">~186 ms</td>
</tr>
<tr class="odd">
<td><strong>Speedup</strong></td>
<td style="text-align: right;"><strong>~2.7x</strong></td>
<td style="text-align: right;"><strong>~3.5x</strong></td>
</tr>
</tbody>
</table>
<p>The gains accumulated across ~15 individual changes, each profiled and verified in turn rather than landed as one big rewrite.</p>
</section>
<section id="where-the-time-was-going" class="level2">
<h2 class="anchored" data-anchor-id="where-the-time-was-going">Where the time was going</h2>
<p>Native profiling pointed at a handful of hot regions, all on the per-node, per-step physiology path:</p>
<ol type="1">
<li><strong>Crown assimilation quadrature</strong> — <code>std::function</code> callable overhead and redundant canopy-shape work dominated the integrand.</li>
<li><strong>Spline / interpolator lookups</strong> — repeated, bounds-checked evaluations and per-point binary searches during assimilation.</li>
<li><strong>Competition / environment recomputation</strong> — inverse-height competition ratios recomputed every call.</li>
<li><strong>Gradient calculation</strong> — physiology copied and recomputed via a per-call <code>Internals</code> allocation.</li>
<li><strong>String/map lookups</strong> — state and aux variables addressed by name on the hot path.</li>
</ol>
</section>
<section id="how-it-was-done" class="level2">
<h2 class="anchored" data-anchor-id="how-it-was-done">How it was done</h2>
<p>Each row below is an independently profiled, bit-identical change. Where machine drift between sessions made cumulative ratios unreliable, the honest figure was re-measured as a same-session A/B/A comparison (documented in the tracking note).</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Target</th>
<th>Change</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Assimilation quadrature</td>
<td>Direct-lambda integrand (drop <code>std::function</code>); eta-specialised canopy shape (issue #465); ratio-based <code>q()</code> path</td>
<td>Largest single contributor — ~1.4-1.5x from the canopy-shape specialisation alone</td>
</tr>
<tr class="even">
<td>Spline lookups</td>
<td>Inline interpolator accessors + unchecked eval; hoist <code>spline.max()</code> out of the integrand; O(1)-guess + nudge lookup replacing per-point binary search</td>
<td>~3.5x on <code>build_schedule</code> traces back largely here</td>
</tr>
<tr class="odd">
<td>Competition</td>
<td>Cache the inverse-height competition ratio; dependent-aux height-inverse cache</td>
<td>~1.7x cumulative on competition-bound paths</td>
</tr>
<tr class="even">
<td>Gradients</td>
<td>Reuse a thread-local scratch <code>Individual</code> (no per-call <code>Internals</code> alloc)</td>
<td>Removed per-gradient heap churn</td>
</tr>
<tr class="odd">
<td>State/rate addressing</td>
<td>Direct hot-path state/aux indices instead of string/map lookups</td>
<td>~1.9x on the indexed paths</td>
</tr>
<tr class="even">
<td>Allocation derivatives</td>
<td>Dedup <code>pow()</code> and reuse <code>area_sapwood</code>/<code>mass_sapwood</code> from <code>net_mass_production_dt</code></td>
<td>Bit-identical, timing-neutral hygiene; kept for clarity</td>
</tr>
<tr class="odd">
<td>FF16 strategy</td>
<td>Inline competition helpers + <code>area_leaf</code> into <code>ff16_strategy.h</code>; inline <code>util::is_finite</code> into a header</td>
<td>~1.2-1.3x from removing call overhead on the tightest loops</td>
</tr>
</tbody>
</table>
</section>
<section id="notes-on-measurement-discipline" class="level2">
<h2 class="anchored" data-anchor-id="notes-on-measurement-discipline">Notes on measurement discipline</h2>
<p>A recurring theme — and the reason the tracking note is so long — is that these runs are short (hundreds of ms) with a ~10% run-to-run noise floor, and the macOS <code>/usr/bin/sample</code> profiler perturbs <code>Rprof</code> totals when it attaches successfully. Several apparent “regressions” were measurement artefacts:</p>
<ul>
<li>A <strong>Sampler-Effect Audit</strong> isolated the inflation caused by concurrent sampling; the sample-off figures are the ones to trust for small changes.</li>
<li>Cross-session comparisons were re-grounded with same-session A/B/A re-measurements before any speedup was claimed.</li>
<li>An <strong>LTO experiment</strong> (issue #470) showed no measurable gain and was declined rather than kept speculatively.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Bit-identical optimisations that turned out to be timing-neutral were deliberately kept where they improved clarity (e.g.&nbsp;the <code>pow()</code> dedup), and declined where they added complexity for no measured benefit (LTO). “Faster” was never allowed to mean “different answer” — the FF16 reference baselines gate the whole pass.</p>
</div>
</div>


</section>

 ]]></description>
  <category>ff16</category>
  <category>performance</category>
  <category>develop</category>
  <category>engineering</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-18-ff16-hot-path-optimisation/</guid>
  <pubDate>Thu, 18 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A soil-water bucket model from AWRA-L</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-15-awra-soil-water/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop 81cfeab</span></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The observational dataset this post is built around (precipitation and soil moisture from Edaphic at the Corrie Downs and Delta sites) is <strong>not bundled</strong> with this repository. Every chunk that reads, prepares, or validates against that data is set <code>#| eval: false</code>. The self-contained model definitions (rate functions, ODE setup, parameter helpers) are kept verbatim and run.</p>
</div>
</div>
<section id="load-packages" class="level1">
<h1>Load packages</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span></code></pre></div></div>
</div>
</section>
<section id="load-observational-data" class="level1">
<h1>Load observational data</h1>
<p>First lets open up some observational data from Edaphic at the Corrie Downs site. It has precipitation and soil moisture observations.</p>
<p>Not run: reads the Corrie Downs observational CSV, which isn’t bundled with this repo.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/Documents/GitHub/plant/vignettes/models/export-edap61.Falster.NSW.Corrie Downs weather-6.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(Id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Units"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(.[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract layer names to identify depths. Layer 1 is 10cm deep, 2 20cm, etc.</span></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract_all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(soil_moist_obs)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>.x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>() <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> layernames</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(soil_moist_obs)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> layernames</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Turn timedate stamps into R readable dates</span></span>
<span id="cb2-14">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_15min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Prec_mm_per_15min,Timestamp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Soil_moist"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dom =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">day</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mod =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">minute</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hod =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hour</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">week</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">moy =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">month</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Precipitation is in units of mm/15mins</span></span>
<span id="cb2-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_15min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_mm_per_15min)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert those rates into units of mm/day (for each 15 step) %&gt;%</span></span>
<span id="cb2-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_day =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_mm_per_15min<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculating total rainfall per week</span></span>
<span id="cb2-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(week, depth) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_week =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Prec_mm_per_15min)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mutate(Prec_mm_per_week = Prec_mm_per_15min*96*7) %&gt;%</span></span>
<span id="cb2-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m3 water per m3 soil - measured using electrical field</span></span>
<span id="cb2-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Soil_moist =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Soil_moist)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs</span></code></pre></div></div>
</div>
<p>Not run: depends on the observational <code>soil_moist_obs</code> loaded above.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(Timestamp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Soil_moist))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> depth)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> a</span>
<span id="cb3-4"></span>
<span id="cb3-5">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(Timestamp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_mm_per_day))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> b</span>
<span id="cb3-8"></span>
<span id="cb3-9">a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>b</span></code></pre></div></div>
</div>
</section>
<section id="prepare-observational-data" class="level1">
<h1>Prepare observational data</h1>
<p>Not run: data prep depends on the observational <code>soil_moist_obs</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make date-delisted timestep</span></span>
<span id="cb4-2">soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Timestamp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> timestep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(timestep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(data) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs</span>
<span id="cb4-10"></span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating precipitation data table using the total rainfall per week data</span></span>
<span id="cb4-13">soil_moist_obs  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(week) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(timestep) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> Prec_obs</span>
<span id="cb4-17"></span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Spline function using the precipitation data</span></span>
<span id="cb4-19">Prec_spline <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">splinefun</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Prec_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Prec_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Prec_mm_per_week)</span></code></pre></div></div>
</div>
</section>
<section id="number-of-soil-layers" class="level1">
<h1>Number of soil layers</h1>
<p>Not run: derives the layer count from the observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">soil_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth))</span></code></pre></div></div>
</div>
</section>
<section id="frost-a.-j.-and-shokri-a.-2021-the-australian-landscape-water-balance-model-awra-l-v7.-technical-description-of-the-australian-water-resources-assessment-landscape-model-version-7.-bureau-of-meteorology-technical-report" class="level1">
<h1>Frost, A. J., and Shokri, A., (2021) The Australian Landscape Water Balance model (AWRA-L v7). Technical Description of the Australian Water Resources Assessment Landscape model version 7. Bureau of Meteorology Technical Report</h1>
</section>
<section id="melton-et-al.-2017-tiling-soil-textures-for-terrestrial-ecosystem-modelling-via-clustering-analysis-a-case-study-with-class-ctem-version-2.1-depending-on" class="level1">
<h1>Melton et al., (2017) Tiling soil textures for terrestrial ecosystem modelling via clustering analysis: a case study with CLASS-CTEM (version 2.1) depending on</h1>
</section>
<section id="cosby-et-al.-1984-a-statistical-exploration-of-the-relationships-of-soil-moisture-characteristics-to-the-physical-properties-of-soils" class="level1">
<h1>Cosby et al.&nbsp;(1984) A Statistical Exploration of the Relationships of Soil Moisture Characteristics to the Physical Properties of Soils</h1>
<section id="functions" class="level2">
<h2 class="anchored" data-anchor-id="functions">Functions</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">#### Parameters functions</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saturated hydraulic conductivity (mm day^-1) depends on sand content</span></span>
<span id="cb6-4"></span>
<span id="cb6-5">K_sat_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(sand_con){</span>
<span id="cb6-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Melton et al., (2017)</span></span>
<span id="cb6-7">  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0556e-6</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0352</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>sand_con<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">-2.035</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span></span>
<span id="cb6-8">}</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (%)</span></span>
<span id="cb6-11"></span>
<span id="cb6-12">Beta_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(avg_Slope){</span>
<span id="cb6-13">  avg_Slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb6-14">}</span>
<span id="cb6-15"></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation controls infiltration-excess runoff (mm day^-1)</span></span>
<span id="cb6-17"></span>
<span id="cb6-18">P_refmap_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(sand_con, Beta){</span>
<span id="cb6-19">  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">K_sat_</span>(sand_con)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Beta))</span>
<span id="cb6-20">}</span>
<span id="cb6-21"></span>
<span id="cb6-22"></span>
<span id="cb6-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil saturation capacity (mm)</span></span>
<span id="cb6-24"></span>
<span id="cb6-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parameters from Cosby et al. (1984)</span></span>
<span id="cb6-26">Sz_sat_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(sand_con, dz){</span>
<span id="cb6-27">  dz<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.142</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>sand_con)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb6-28">}</span>
<span id="cb6-29"></span>
<span id="cb6-30"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">#### Rates functions</span></span>
<span id="cb6-31"></span>
<span id="cb6-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Infiltration-excess runoff (mm day^-1)</span></span>
<span id="cb6-33"></span>
<span id="cb6-34">Qh_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(P, Qh_switch){</span>
<span id="cb6-35">  Qh_switch <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P_refmap<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(P<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>P_refmap)</span>
<span id="cb6-36">}</span>
<span id="cb6-37"></span>
<span id="cb6-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Drainage rate (mm day ^ -1)</span></span>
<span id="cb6-39"></span>
<span id="cb6-40">Dz_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(Sz, i){</span>
<span id="cb6-41">  K_sat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(Sz<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Sz_sat[i])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb6-42">}</span></code></pre></div></div>
</div>
</section>
<section id="soil-water-model" class="level2">
<h2 class="anchored" data-anchor-id="soil-water-model">Soil water model</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">rates_psi_FS_2021 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, Sz, params) {</span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Sz, params)), {</span>
<span id="cb7-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sz values are in mm</span></span>
<span id="cb7-4">    dSz_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, nlayers)</span>
<span id="cb7-5">    Dz <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, nlayers)</span>
<span id="cb7-6">    Qh <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>)</span>
<span id="cb7-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate fluxes at boundaries</span></span>
<span id="cb7-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># top boundary layer - depends on soil water content, rain, soil water evaporation</span></span>
<span id="cb7-9">    P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">P_func_</span>(t), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pmax(x, 0) forces all negative values to zero</span></span>
<span id="cb7-10">    Qh <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Qh_</span>(P, Qh_switch)</span>
<span id="cb7-11">    I <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Qh</span>
<span id="cb7-12">    Dz[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Dz_</span>(Sz[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-13">    dSz_dt[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> I <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Dz[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb7-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boundary below each node, except the last one</span></span>
<span id="cb7-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Eq. 3 + 11, Ireson et al. (2023)</span></span>
<span id="cb7-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(nlayers<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)) {</span>
<span id="cb7-17">      Dz[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Dz_</span>(Sz[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-18">      dSz_dt[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Dz[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Dz[i]</span>
<span id="cb7-19">    }</span>
<span id="cb7-20"></span>
<span id="cb7-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dSz_dt =</span> dSz_dt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Qh =</span> Qh, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> P, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">I =</span> I))</span>
<span id="cb7-22">  })</span>
<span id="cb7-23">}</span></code></pre></div></div>
</div>
</section>
<section id="ode-solver" class="level2">
<h2 class="anchored" data-anchor-id="ode-solver">Ode solver</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">solve <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(times, rates, state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rk4"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta"</span>) {</span>
<span id="cb8-2"></span>
<span id="cb8-3">  deSolve<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb8-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> state,</span>
<span id="cb8-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> times,</span>
<span id="cb8-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> rates,</span>
<span id="cb8-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> params,</span>
<span id="cb8-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> method) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(time, Qh, P, I), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> output)</span>
<span id="cb8-11">}</span></code></pre></div></div>
</div>
</section>
<section id="parameters" class="level2">
<h2 class="anchored" data-anchor-id="parameters">Parameters</h2>
<p>Not run: parameter block depends on <code>soil_layers</code>, derived from the observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb9-2">  nlayers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soil_layers</span>
<span id="cb9-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil depth (mm)s</span></span>
<span id="cb9-4">  soil_depth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5e3</span></span>
<span id="cb9-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Different depths through sequence (mm)</span></span>
<span id="cb9-6">  z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(soil_depth<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nlayers, soil_depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out=</span> nlayers)</span>
<span id="cb9-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distances between depths starting from 0 (mm)</span></span>
<span id="cb9-8">  dz <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,z))</span>
<span id="cb9-9">  clay_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb9-10">  sand_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb9-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saturated conducitivity (mm day ^-1)</span></span>
<span id="cb9-12">  K_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">K_sat_</span>(sand_con)</span>
<span id="cb9-13"></span>
<span id="cb9-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One value per soil layer</span></span>
<span id="cb9-15">  Sz_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sz_sat_</span>(sand_con, dz)</span>
<span id="cb9-16"></span>
<span id="cb9-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters (i.e. relating to the effect of slope on drainage)</span></span>
<span id="cb9-18">  P_refscale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.637</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb9-19">  avg_Slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb9-20">  Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Beta_</span>(avg_Slope) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb9-21">  P_refmap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">P_refmap_</span>(sand_con, Beta) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation (mm day^-1)</span></span>
<span id="cb9-22"></span>
<span id="cb9-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set to 0 to disable runoff</span></span>
<span id="cb9-24">  Qh_switch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-25"></span>
<span id="cb9-26">  P_ref <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_refmap<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>P_refscale <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb9-27"></span>
<span id="cb9-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Change which function is used to represent precipitation in the model</span></span>
<span id="cb9-29"></span>
<span id="cb9-30">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># P_func_ &lt;- function(t) {Prec_spline(t)} # Use spline function to represent precipitation</span></span>
<span id="cb9-31">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># P_func_ &lt;- function(t) {30*sin(t)+30} # Change precipitation function to a sine wave. First number is amplitude, second is the y-axis midpoint.</span></span>
<span id="cb9-32">  P_func_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t) {t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>} <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Testing constant precipitation</span></span>
<span id="cb9-33"></span>
<span id="cb9-34">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb9-35">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb9-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nlayers =</span> soil_layers,</span>
<span id="cb9-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_depth =</span> soil_depth,</span>
<span id="cb9-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> z,</span>
<span id="cb9-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clay_con =</span> clay_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb9-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sand_con =</span> sand_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb9-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K_sat =</span> K_sat,</span>
<span id="cb9-42">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sz_sat =</span> Sz_sat,</span>
<span id="cb9-43"></span>
<span id="cb9-44">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters</span></span>
<span id="cb9-45">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refscale =</span> P_refscale, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb9-46">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_Slope =</span> avg_Slope, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb9-47">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Beta =</span> Beta, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb9-48">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refmap =</span> P_refmap, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb9-49">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_ref =</span> P_ref, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb9-50">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Qh_switch =</span> Qh_switch, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Runoff switch</span></span>
<span id="cb9-51">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_func_ =</span> P_func_</span>
<span id="cb9-52">)</span></code></pre></div></div>
</div>
<p>Not run: timesteps are spanned from the observational record.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generates timesteps per 15 minutes</span></span>
<span id="cb10-2">timesteps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generates timesteps per 10 days</span></span>
<span id="cb10-5">timesteps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</div>
<p>Not run: initial state and the deSolve run depend on the observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting initial state of all soil layers</span></span>
<span id="cb11-2">init_state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nlayers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(Soil_moist) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>()</span>
<span id="cb11-7"></span>
<span id="cb11-8">Sz_solve_ode23 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> timesteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates=</span>rates_psi_FS_2021, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state=</span>init_state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ode23"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sz"</span>))</span></code></pre></div></div>
</div>
<p>Not run: validation plot compares the solve against the observed series.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(timestep) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># standardising decimal places for left join</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Soil_moist, Timestamp, depth, timestep) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs_rearranged</span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sz_solve %&gt;%</span></span>
<span id="cb12-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   mutate(timestep = as.numeric(time) %&gt;% round(digits = 4)) %&gt;%</span></span>
<span id="cb12-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   mutate(depth = as.numeric(depth)) -&gt; Sz_solve</span></span>
<span id="cb12-10"></span>
<span id="cb12-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> soil_moist_obs_rearranged, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Soil_moist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> timestep), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> Sz_solve_ode23, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Sz, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_line(data = Sz_solve_lsoda, aes(y = Sz, x = time), col = "blue", linetype = 2) +</span></span>
<span id="cb12-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> a</span>
<span id="cb12-17"></span>
<span id="cb12-18">Sz_solve_ode23 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># left_join(soil_moist_obs_rearranged) %&gt;%</span></span>
<span id="cb12-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ylim(0, 100) +</span></span>
<span id="cb12-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(P))) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> b</span>
<span id="cb12-23"></span>
<span id="cb12-24"></span>
<span id="cb12-25">Sz_solve_ode23 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># left_join(soil_moist_obs_rearranged) %&gt;%</span></span>
<span id="cb12-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ylim(0, 100) +</span></span>
<span id="cb12-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Qh))) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> c</span>
<span id="cb12-30"></span>
<span id="cb12-31"></span>
<span id="cb12-32">Sz_solve_ode23 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-33">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># left_join(soil_moist_obs_rearranged) %&gt;%</span></span>
<span id="cb12-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-35">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ylim(0, 100) +</span></span>
<span id="cb12-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(I))) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> d</span>
<span id="cb12-37"></span>
<span id="cb12-38"></span>
<span id="cb12-39">a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>c<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>d)</span>
<span id="cb12-40"></span>
<span id="cb12-41">a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>b</span></code></pre></div></div>
</div>
</section>
</section>
<section id="cpp-implementation" class="level1">
<h1>cpp implementation</h1>
<p>Not run: loads <code>plant</code> from a hardcoded local source tree.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load_all</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/Documents/GitHub/plant/"</span>)</span></code></pre></div></div>
</div>
<p>Not run: patch lifetime is derived from the observational time span.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">p0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scm_base_parameters</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TF24"</span>)</span>
<span id="cb14-2">p0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_patch_lifetime <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span></span>
<span id="cb14-3">p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_parameters</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trait_matrix</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0825</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lma"</span>), p0)</span></code></pre></div></div>
</div>
<p>Not run: environment is initialised from observed soil moisture and rainfall.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">n_depths <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span></span>
<span id="cb15-2">env <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Environment</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TF24"</span>)</span>
<span id="cb15-3">env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_soil_number_of_depths</span>(n_depths)</span>
<span id="cb15-4">env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>K_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> K_sat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span></span>
<span id="cb15-5">env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>soil_moist_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Sz_sat[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb15-6">env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>b_infil <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb15-7">inits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(timestep) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(data) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(Soil_moist)</span>
<span id="cb15-8">env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_soil_water_state</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(inits<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)))</span>
<span id="cb15-9">env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extrinsic_drivers_set_constant</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rainfall"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.365</span>)</span>
<span id="cb15-10"></span>
<span id="cb15-11">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(timestep, Prec_mm_per_15min) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>() <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs_nested</span>
<span id="cb15-14"></span>
<span id="cb15-15"></span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># env$extrinsic_drivers_set_variable(driver_name = "rainfall", x = (soil_moist_obs_nested$timestep - min(soil_moist_obs$timestep))/365, y = soil_moist_obs_nested$Prec_mm_per_15min/1000*(4*24*365))</span></span>
<span id="cb15-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb15-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># env$extrinsic_drivers_evaluate_range(driver_name = "rainfall", x = seq(0,0.2,length.out = 100))</span></span></code></pre></div></div>
</div>
<p>Not run: runs the SCM against the observation-derived environment above.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_scm</span>(p1, env, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collect =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb16-2"></span>
<span id="cb16-3">out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>soil_moist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_depth =</span> out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>env<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>soil_depth<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>soil_depth) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> Sz_solve_cpp</span>
<span id="cb16-5"></span>
<span id="cb16-6">Sz_solve_cpp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_depth =</span> soil_depth<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> Sz_solve_cpp</span>
<span id="cb16-8"></span>
<span id="cb16-9">Sz_solve_cpp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>timestep)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> Sz_solve_cpp</span>
<span id="cb16-11"></span>
<span id="cb16-12"></span>
<span id="cb16-13">Sz_solve_cpp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(soil_depth)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> Sz_solve_cpp</span></code></pre></div></div>
</div>
<p>Not run: compares the C++ solve against the observed series.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> soil_moist_obs_rearranged, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Soil_moist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> timestep), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> Sz_solve_cpp, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> soil_moist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom_line(data = Sz_solve_lsoda, aes(y = Sz, x = time), col = "blue", linetype = 2) +</span></span>
<span id="cb17-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>depth) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> a</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">soiltype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sand"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loamsand"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sandyloam"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loam"</span>,</span>
<span id="cb18-2">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"siltyloam"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sandyclayloam"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clayloam"</span>,                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"siltyclayloam"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sandyclay"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"siltyclay"</span>,</span>
<span id="cb18-3">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clay"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fullclay"</span>),</span>
<span id="cb18-4">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">43</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb18-5">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clay =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">47</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>))</span>
<span id="cb18-6"></span>
<span id="cb18-7">rmse <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span>
<span id="cb18-8">bias <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span></code></pre></div></div>
</div>
<p>Not run: sweeps soil textures, refitting against the observational data and computing RMSE/bias per type.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(soiltype)){</span>
<span id="cb19-2"></span>
<span id="cb19-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb19-4">  nlayers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soil_layers</span>
<span id="cb19-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil depth (mm)s</span></span>
<span id="cb19-6">  soil_depth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5e3</span></span>
<span id="cb19-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Different depths through sequence (mm)</span></span>
<span id="cb19-8">  z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(soil_depth<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nlayers, soil_depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out=</span> nlayers)</span>
<span id="cb19-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distances between depths starting from 0 (mm)</span></span>
<span id="cb19-10">  dz <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,z))</span>
<span id="cb19-11">  clay_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soiltype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>clay[i] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb19-12">  sand_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soiltype<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sand[i] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb19-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saturated conducitivity (mm day ^-1)</span></span>
<span id="cb19-14">  K_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">K_sat_</span>(sand_con)</span>
<span id="cb19-15"></span>
<span id="cb19-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One value per soil layer</span></span>
<span id="cb19-17">  Sz_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sz_sat_</span>(sand_con, dz)</span>
<span id="cb19-18"></span>
<span id="cb19-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters (i.e. relating to the effect of slope on drainage)</span></span>
<span id="cb19-20">  P_refscale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.637</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb19-21">  avg_Slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb19-22">  Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Beta_</span>(avg_Slope) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb19-23">  P_refmap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">P_refmap_</span>(clay_con, Beta) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb19-24">  P_ref <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_refmap<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>P_refscale <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb19-25"></span>
<span id="cb19-26">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb19-27">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb19-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nlayers =</span> soil_layers,</span>
<span id="cb19-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_depth =</span> soil_depth,</span>
<span id="cb19-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> z,</span>
<span id="cb19-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clay_con =</span> clay_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb19-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sand_con =</span> sand_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb19-33">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K_sat =</span> K_sat,</span>
<span id="cb19-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sz_sat =</span> Sz_sat,</span>
<span id="cb19-35"></span>
<span id="cb19-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters</span></span>
<span id="cb19-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refscale =</span> P_refscale, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb19-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_Slope =</span> avg_Slope, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb19-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Beta =</span> Beta, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb19-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refmap =</span> P_refmap, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb19-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_ref =</span> P_ref <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb19-42">)</span>
<span id="cb19-43"></span>
<span id="cb19-44"></span>
<span id="cb19-45">state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nlayers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(Soil_moist) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>()</span>
<span id="cb19-50"></span>
<span id="cb19-51"></span>
<span id="cb19-52">Sz_solve <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> timesteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates=</span>rates_psi_FS_2021, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state=</span>state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ode45"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sz"</span>)</span>
<span id="cb19-53"></span>
<span id="cb19-54">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Soil_moist, Timestamp, depth, timestep) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs_rearranged</span>
<span id="cb19-58"></span>
<span id="cb19-59"></span>
<span id="cb19-60">Sz_solve <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(soil_moist_obs_rearranged) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Sz, Soil_moist,timestep) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> res</span>
<span id="cb19-65"></span>
<span id="cb19-66">rmse[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmse</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sz, res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Soil_moist)</span>
<span id="cb19-67">bias[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Metrics<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bias</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Sz, res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Soil_moist)</span>
<span id="cb19-68"></span>
<span id="cb19-69">Sz_solve <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(timestep)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-72">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-73">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mutate(date = soil_moist_obs_rearranged$date) %&gt;%</span></span>
<span id="cb19-74">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(soil_moist_obs_rearranged) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-75">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> timestep)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-76">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Sz)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-77">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Soil_moist), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-78">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> p</span>
<span id="cb19-79"></span>
<span id="cb19-80"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p)</span>
<span id="cb19-81"></span>
<span id="cb19-82">}</span>
<span id="cb19-83"></span>
<span id="cb19-84">rmse</span>
<span id="cb19-85">bias</span></code></pre></div></div>
</div>
</section>
<section id="minimise-rmse" class="level1">
<h1>Minimise rmse</h1>
<p>Not run: refits the best-RMSE soil type against the observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb20-2"></span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Parameters</span></span>
<span id="cb20-5"></span>
<span id="cb20-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb20-7">  nlayers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soil_layers</span>
<span id="cb20-8">  soil_depth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5e3</span></span>
<span id="cb20-9">  z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(soil_depth<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nlayers, soil_depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out=</span> nlayers)</span>
<span id="cb20-10">  clay_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soiltype[i,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>clay <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb20-11">  sand_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soiltype[i,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sand <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb20-12">  K_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">K_sat_</span>(clay_con, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Kzsatscale_</span>(z))</span>
<span id="cb20-13">  Sz_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sz_sat_</span>(clay_con, sand_con, z)</span>
<span id="cb20-14"></span>
<span id="cb20-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters</span></span>
<span id="cb20-16">  P_refscale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.637</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb20-17">  avg_Slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb20-18">  Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Beta_</span>(avg_Slope) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb20-19">  P_refmap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">P_refmap_</span>(clay_con, Beta) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb20-20">  P_ref <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_refmap<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>P_refscale <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb20-21"></span>
<span id="cb20-22">  S_awc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">S_awc_</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>)</span>
<span id="cb20-23"></span>
<span id="cb20-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rain</span></span>
<span id="cb20-25">  P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Prec_mm_per_day)</span>
<span id="cb20-26"></span>
<span id="cb20-27">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb20-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb20-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nlayers =</span> soil_layers,</span>
<span id="cb20-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_depth =</span> soil_depth,</span>
<span id="cb20-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> z,</span>
<span id="cb20-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clay_con =</span> clay_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb20-33">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sand_con =</span> sand_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb20-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K_sat =</span> K_sat,</span>
<span id="cb20-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sz_sat =</span> Sz_sat,</span>
<span id="cb20-36"></span>
<span id="cb20-37">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters</span></span>
<span id="cb20-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refscale =</span> P_refscale, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb20-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_Slope =</span> avg_Slope, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb20-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Beta =</span> Beta, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb20-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refmap =</span> P_refmap, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb20-42">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_ref =</span> P_ref, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb20-43"></span>
<span id="cb20-44">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S_awc =</span> S_awc,</span>
<span id="cb20-45"></span>
<span id="cb20-46">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rain</span></span>
<span id="cb20-47">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> P</span>
<span id="cb20-48">)</span>
<span id="cb20-49"></span>
<span id="cb20-50"></span>
<span id="cb20-51">state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nlayers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(depth) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(Soil_moist)</span>
<span id="cb20-55"></span>
<span id="cb20-56"></span>
<span id="cb20-57">Sz_solve <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> timesteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates=</span>rates_psi_FS_2021, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state=</span>state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsoda"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sz"</span>)</span>
<span id="cb20-58"></span>
<span id="cb20-59">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Soil_moist, Timestamp, depth, timestep) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs_rearranged</span>
<span id="cb20-63"></span>
<span id="cb20-64">Sz_solve <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-66">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-67">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(soil_moist_obs_rearranged) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-68">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Sz))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Soil_moist)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth))</span></code></pre></div></div>
</div>
</section>
<section id="minimise-bias" class="level1">
<h1>Minimise bias</h1>
<p>Not run: refits the best-bias soil type against the observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb21-2"></span>
<span id="cb21-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Parameters</span></span>
<span id="cb21-4"></span>
<span id="cb21-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb21-6">  nlayers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soil_layers</span>
<span id="cb21-7">  soil_depth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5e3</span></span>
<span id="cb21-8">  z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(soil_depth<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nlayers, soil_depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out=</span> nlayers)</span>
<span id="cb21-9">  clay_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soiltype[i,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>clay <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb21-10">  sand_con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> soiltype[i,]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sand <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb21-11">  K_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">K_sat_</span>(clay_con, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Kzsatscale_</span>(z))</span>
<span id="cb21-12">  Sz_sat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sz_sat_</span>(clay_con, sand_con, z)</span>
<span id="cb21-13"></span>
<span id="cb21-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters</span></span>
<span id="cb21-15">  P_refscale <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.637</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb21-16">  avg_Slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb21-17">  Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Beta_</span>(avg_Slope) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb21-18">  P_refmap <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">P_refmap_</span>(clay_con, Beta) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb21-19">  P_ref <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P_refmap<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>P_refscale <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb21-20"></span>
<span id="cb21-21">  S_awc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">S_awc_</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>)</span>
<span id="cb21-22"></span>
<span id="cb21-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rain</span></span>
<span id="cb21-24">  P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Prec_mm_per_day)</span>
<span id="cb21-25"></span>
<span id="cb21-26">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb21-27">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Soil info</span></span>
<span id="cb21-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nlayers =</span> soil_layers,</span>
<span id="cb21-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_depth =</span> soil_depth,</span>
<span id="cb21-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> z,</span>
<span id="cb21-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clay_con =</span> clay_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clay (%)</span></span>
<span id="cb21-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sand_con =</span> sand_con, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sand (%)</span></span>
<span id="cb21-33">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K_sat =</span> K_sat,</span>
<span id="cb21-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sz_sat =</span> Sz_sat,</span>
<span id="cb21-35"></span>
<span id="cb21-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope parameters</span></span>
<span id="cb21-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refscale =</span> P_refscale, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unitless</span></span>
<span id="cb21-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_Slope =</span> avg_Slope, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># radians</span></span>
<span id="cb21-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Beta =</span> Beta, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slope (percent)</span></span>
<span id="cb21-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_refmap =</span> P_refmap, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unscaled reference precipitation</span></span>
<span id="cb21-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P_ref =</span> P_ref, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scaled reference precipitation</span></span>
<span id="cb21-42"></span>
<span id="cb21-43">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">S_awc =</span> S_awc,</span>
<span id="cb21-44"></span>
<span id="cb21-45">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rain</span></span>
<span id="cb21-46">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> P</span>
<span id="cb21-47">)</span>
<span id="cb21-48"></span>
<span id="cb21-49"></span>
<span id="cb21-50">state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nlayers) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(depth) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(Soil_moist)</span>
<span id="cb21-54"></span>
<span id="cb21-55"></span>
<span id="cb21-56">Sz_solve <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> timesteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates=</span>rates_psi_FS_2021, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state=</span>state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsoda"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sz"</span>)</span>
<span id="cb21-57"></span>
<span id="cb21-58">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Soil_moist, Timestamp, depth, timestep) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs_rearranged</span>
<span id="cb21-62"></span>
<span id="cb21-63">Sz_solve <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-66">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(soil_moist_obs_rearranged) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-67">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-68">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Sz))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-69">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Soil_moist)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(depth))</span></code></pre></div></div>
</div>
<section id="delta-bare-soil-example" class="level2">
<h2 class="anchored" data-anchor-id="delta-bare-soil-example">Delta bare soil example</h2>
<p>Not run: reads the Delta site observational CSV, which isn’t bundled with this repo.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/Documents/export-edap61.Falster.NSW.Delta weather (extra soil)-2.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(Id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Units"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(.[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs</span>
<span id="cb22-5"></span>
<span id="cb22-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract_all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(soil_moist_obs)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>.x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>() <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> layernames</span>
<span id="cb22-9"></span>
<span id="cb22-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(soil_moist_obs)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> layernames</span>
<span id="cb22-11"></span>
<span id="cb22-12">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_15min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Prec_mm_per_15min,Timestamp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Soil_moist"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-09-14"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dom =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">day</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mod =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">minute</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hod =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hour</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">moy =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">month</span>(Timestamp)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_15min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_mm_per_15min)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prec_mm_per_day =</span> Prec_mm_per_15min<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Soil_moist =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Soil_moist)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs</span></code></pre></div></div>
</div>
<p>Not run: depends on the Delta observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(Timestamp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Soil_moist))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> depth))</span></code></pre></div></div>
</div>
<p>Not run: depends on the Delta observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">soil_moist_obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(Timestamp), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Prec_mm_per_15min))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> depth))</span></code></pre></div></div>
</div>
<p>Not run: data prep depends on the Delta observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Timestamp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nest</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(Timestamp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">timestep =</span> timestep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(data) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> soil_moist_obs</span>
<span id="cb25-9"></span>
<span id="cb25-10">soil_moist_obs  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Timestamp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(Timestamp)<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> Prec_obs</span></code></pre></div></div>
</div>
<p>Not run: derives the layer count from the Delta observational data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">soil_layers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(soil_moist_obs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>depth))</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">rates_psi_FS_2021 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, Sz, params) {</span>
<span id="cb27-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Sz, params)), {</span>
<span id="cb27-3">    dSz_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, nlayers)</span>
<span id="cb27-4">    Dz_ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, nlayers)</span>
<span id="cb27-5"></span>
<span id="cb27-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate fluxes at boundaries</span></span>
<span id="cb27-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># top boundary layer - depends on soil water content and rain</span></span>
<span id="cb27-8">    I <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> P[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Qh_</span>(P[t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb27-9">    Dz_[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Dz</span>(Sz[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb27-10">    dSz_dt[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> I <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Dz_[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(Sz[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb27-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boundary below each node, except the last one</span></span>
<span id="cb27-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Eq. 3 + 11, Ireson et al. (2023)</span></span>
<span id="cb27-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(nlayers<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)) {</span>
<span id="cb27-14">      Dz_[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Dz</span>(Sz[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb27-15">      dSz_dt[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Dz_[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Dz_[i]</span>
<span id="cb27-16">    }</span>
<span id="cb27-17"></span>
<span id="cb27-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(dSz_dt))</span>
<span id="cb27-19">  })</span>
<span id="cb27-20">}</span></code></pre></div></div>
</div>
<p>Not run: solve depends on the observation-derived <code>timesteps</code> and <code>state</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">Sz_solve <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> timesteps, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates=</span>rates_psi_FS_2021, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state=</span>state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsoda"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sz"</span>)</span></code></pre></div></div>
</div>


</section>
</section>

 ]]></description>
  <category>tf24</category>
  <category>soil-water</category>
  <category>hydraulics</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-15-awra-soil-water/</guid>
  <pubDate>Mon, 15 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Hydraulics inner-loop benchmark</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2026-06-10-hydraulics-benchmark/</link>
  <description><![CDATA[ 




<p><span class="version-badge version-badge--dev">plant @develop a1b2c3d</span></p>
<p>This post is built against an <strong>unreleased</strong> commit on <code>develop</code>, so the badge pins to a SHA rather than a version. The simulation below is slow, so it’s pinned to its own <code>renv</code> snapshot as well — a later project-wide package upgrade can’t change this result even on a forced re-run.</p>
<section id="per-post-environment" class="level2">
<h2 class="anchored" data-anchor-id="per-post-environment">Per-post environment</h2>
<p>This directory carries its own lockfile, captured once when the post was written:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># run from this post's directory, once:</span></span>
<span id="cb1-2">renv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">snapshot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lockfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"renv.lock.dev"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prompt =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<p><code>plant_session_info()</code> (footer) reports <code>renv.lock.dev</code> when present, so the page records exactly which environment produced the figures. To reproduce: check out <code>plant</code> at the SHA above, <code>renv::restore(lockfile = "renv.lock.dev")</code>, delete this post’s <code>_freeze/</code> entry, re-render.</p>
</section>
<section id="the-sweep" class="level2">
<h2 class="anchored" data-anchor-id="the-sweep">The sweep</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(plant)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bench)</span>
<span id="cb2-3"></span>
<span id="cb2-4">lai_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(lai_grid, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(L) {</span>
<span id="cb2-7">  bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mark</span>(</span>
<span id="cb2-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_inner_ff16</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lai =</span> L),</span>
<span id="cb2-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hydraulic =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_inner_hydraulics</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lai =</span> L),</span>
<span id="cb2-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iterations =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-11">  )</span>
<span id="cb2-12">})</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ... ggplot of median time by method across lai_grid ...</span></span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Numbers here are frozen to commit <code>a1b2c3d</code>. When this work lands on <code>master</code>, supersede this post with a release-pinned version rather than editing it — keeping the develop snapshot as the historical record.</p>
</div>
</div>
</section>
<section id="reproducibility-footer" class="level2">
<h2 class="anchored" data-anchor-id="reproducibility-footer">Reproducibility footer</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plant_session_info</span>()</span></code></pre></div></div>
</div>


</section>

 ]]></description>
  <category>hydraulics</category>
  <category>performance</category>
  <category>develop</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2026-06-10-hydraulics-benchmark/</guid>
  <pubDate>Wed, 10 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>When the Richards equation fights back</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2025-07-29-richards-equation-instability/</link>
  <description><![CDATA[ 




<p>We’ve recently been trying to expand the TF24 strategy to handle soil water, but our initial attempt encountered serious instability after we implemented the Richards equation. The symptom was unmissable: with anything but an absurdly small time step the integrator produced nonsense within a few steps — soil moisture leaping above saturation, going negative, then collapsing to <code>NaN</code>. The same pathology showed up whether the equation was implemented in C++ or in R, which is the first clue that the problem is mathematical rather than a coding slip.</p>
<p>This post works through a minimal R reproduction of that instability, shows the contrast between an explicit and an implicit solver, and ends with the diagnosis: the discretised Richards equation is <strong>stiff</strong>, and no amount of careful coding will rescue an explicit Runge–Kutta scheme from it. The full back-and-forth — including the <img src="https://latex.codecogs.com/png.latex?%5Cpsi">-based form, a single-bucket fallback, and a delay-equation experiment — lives in <a href="https://github.com/traitecoevo/plant/discussions/444">traitecoevo/plant discussion #444</a>; here we keep just enough to stand on its own.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is a standalone numerical experiment. It prototypes the soil-water dynamics directly with <code>deSolve</code> and does <strong>not</strong> use the <code>plant</code> package API, so it isn’t pinned to a <code>plant</code> version — the point is the solver behaviour, which is independent of the model it will eventually live in.</p>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(deSolve)</span></code></pre></div></div>
</div>
<section id="the-model" class="level2">
<h2 class="anchored" data-anchor-id="the-model">The model</h2>
<p>We track volumetric soil water content <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> on a vertical grid of <img src="https://latex.codecogs.com/png.latex?n"> nodes. Each node exchanges water with its neighbours by Darcy flux, driven by gradients in the matric potential <img src="https://latex.codecogs.com/png.latex?%5Cpsi">, plus gravity. This is the <em>mixed form</em> of the Richards equation, following Ireson et al.&nbsp;(2023):</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5Cpartial%20%5Ctheta%7D%7B%5Cpartial%20t%7D%20=%20-%5Cfrac%7B%5Cpartial%20q%7D%7B%5Cpartial%20z%7D,%0A%5Cqquad%0Aq%20=%20-K(%5Ctheta)%5Cleft(%5Cfrac%7B%5Cpartial%20%5Cpsi%7D%7B%5Cpartial%20z%7D%20-%201%5Cright)."></p>
<p>The constitutive relations — hydraulic conductivity <img src="https://latex.codecogs.com/png.latex?K(%5Ctheta)"> and matric potential <img src="https://latex.codecogs.com/png.latex?%5Cpsi(%5Ctheta)"> — are the usual power laws:</p>
<p><img src="https://latex.codecogs.com/png.latex?K(%5Ctheta)%20=%20K_%5Ctext%7Bsat%7D%5Cleft(%5Cfrac%7B%5Ctheta%7D%7B%5Ctheta_%5Ctext%7Bsat%7D%7D%5Cright)%5E%7B2n_%5Cpsi%20+%203%7D,%0A%5Cqquad%0A%5Cpsi(%5Ctheta)%20=%20a_%5Cpsi%5Cleft(%5Cfrac%7B%5Ctheta%7D%7B%5Ctheta_%5Ctext%7Bsat%7D%7D%5Cright)%5E%7B-n_%5Cpsi%7D."></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">pow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y) x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>y</span>
<span id="cb2-2"></span>
<span id="cb2-3">soil_K_from_soil_theta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">440.628</span>,</span>
<span id="cb2-4">                                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_moist_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.453</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_psi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.8</span>) {</span>
<span id="cb2-5">  K_sat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> soil_moist_sat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_psi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-6">}</span>
<span id="cb2-7"></span>
<span id="cb2-8">psi_from_soil_moist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(soil_moist, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_moist_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.453</span>,</span>
<span id="cb2-9">                                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a_psi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_psi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.8</span>) {</span>
<span id="cb2-10">  a_psi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(soil_moist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> soil_moist_sat, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n_psi)</span>
<span id="cb2-11">}</span>
<span id="cb2-12"></span>
<span id="cb2-13">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_moist_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.453</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># saturated water content (m^3/m^3)</span></span>
<span id="cb2-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K_sat          =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">440.628</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># saturated conductivity (units below)</span></span>
<span id="cb2-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_psi          =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.8</span>,     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pore-size distribution exponent</span></span>
<span id="cb2-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a_psi          =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.7</span>,     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># air-entry scaling</span></span>
<span id="cb2-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b_infil        =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.0</span>,     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># infiltration shape parameter</span></span>
<span id="cb2-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rain           =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rainfall forcing</span></span>
<span id="cb2-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n              =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of soil nodes</span></span>
<span id="cb2-21">)</span></code></pre></div></div>
</div>
<p>The right-hand side assembles the inter-node fluxes — an infiltration flux at the surface that shuts off as the top layer saturates, Darcy fluxes between interior nodes, and a free-drainage lower boundary (<img src="https://latex.codecogs.com/png.latex?q%20=%20K"> at the bottom) — then differences them to get <img src="https://latex.codecogs.com/png.latex?d%5Ctheta/dt"> at each node:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb3-3"></span>
<span id="cb3-4">    psi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">psi_from_soil_moist</span>(theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_moist_sat =</span> soil_moist_sat)</span>
<span id="cb3-5">    K   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">soil_K_from_soil_theta</span>(theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">soil_moist_sat =</span> soil_moist_sat)</span>
<span id="cb3-6"></span>
<span id="cb3-7">    q  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-8">    dz <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n, n)</span>
<span id="cb3-9">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, n)</span>
<span id="cb3-10"></span>
<span id="cb3-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># top boundary: infiltration, shutting off as the top layer fills</span></span>
<span id="cb3-12">    q[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rain <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pow</span>(theta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> soil_moist_sat, b_infil))</span>
<span id="cb3-13"></span>
<span id="cb3-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># interior boundaries: Darcy flux (Eq. 3 + 11, Ireson et al. 2023)</span></span>
<span id="cb3-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) {</span>
<span id="cb3-16">      dpsi_dz  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (psi[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> psi[i]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dz[i]</span>
<span id="cb3-17">      q[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (K[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> K[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (dpsi_dz <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-18">    }</span>
<span id="cb3-19"></span>
<span id="cb3-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lower boundary: free drainage (Eq. 13, Ireson et al. 2023)</span></span>
<span id="cb3-21">    q[n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> K[n]</span>
<span id="cb3-22"></span>
<span id="cb3-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># divergence of flux gives the rate of change at each node (Eq. 10)</span></span>
<span id="cb3-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n)) {</span>
<span id="cb3-25">      dq_dz <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (q[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q[i]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dz[i]</span>
<span id="cb3-26">      dtheta_dt[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dq_dz</span>
<span id="cb3-27">    }</span>
<span id="cb3-28"></span>
<span id="cb3-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(dtheta_dt)</span>
<span id="cb3-30">  })</span>
<span id="cb3-31">}</span></code></pre></div></div>
</div>
<p>A small wrapper integrates the system and returns a long-format tibble, and a helper plots <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> at each depth over time:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">solve_soil <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(times, rates, state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rk4"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta"</span>) {</span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> times, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> rates, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> params, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> method) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depth"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> output) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(depth)))</span>
<span id="cb4-6">}</span>
<span id="cb4-7"></span>
<span id="cb4-8">plot_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(solution) {</span>
<span id="cb4-9">  solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> depth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time (yr)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(theta), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"node"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span>
<span id="cb4-14">}</span>
<span id="cb4-15"></span>
<span id="cb4-16">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb4-17">state <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2533</span>, n)</span></code></pre></div></div>
</div>
</section>
<section id="the-instability" class="level2">
<h2 class="anchored" data-anchor-id="the-instability">The instability</h2>
<p>Now integrate with the classic explicit Runge–Kutta solver (<code>rk4</code>) at a time step of <img src="https://latex.codecogs.com/png.latex?10%5E%7B-4%7D"> yr — about an hour, which sounds harmlessly small:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">fail <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve_soil</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-4</span>),</span>
<span id="cb5-2">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates =</span> rates, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rk4"</span>)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_solution</span>(fail)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-rk4-fail" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-rk4-fail-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://traitecoevo.github.io/overstorey/posts/2025-07-29-richards-equation-instability/index_files/figure-html/fig-rk4-fail-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-rk4-fail-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Explicit RK4 at Δt = 1e-4 yr. The solution explodes within two steps — moisture overshoots saturation, goes negative, then becomes <code>NaN</code> (gaps in the lines).
</figcaption>
</figure>
</div>
</div>
</div>
<p>It detonates almost immediately. Printing the first few steps for the top nodes shows the runaway in numbers: a steady start, then values flung well past the physical range, then <code>NaN</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">fail <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(depth <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> depth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"node "</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 4
  time      `node 1`  `node 2`  `node 3`  
  &lt;deSolve&gt; &lt;deSolve&gt; &lt;deSolve&gt; &lt;deSolve&gt; 
1 0e+00     0.2533000 0.2533000  0.2533000
2 1e-04     0.2531335 0.2544907  0.2527162
3 2e-04     0.1836168 0.5842660 -0.1377172
4 3e-04           NaN       NaN        NaN</code></pre>
</div>
</div>
<p>This is not a bug in the right-hand side — the rates are continuous and correct. It’s the explicit integrator amplifying tiny errors faster than the dynamics damp them, the hallmark of a <strong>stiff</strong> system.</p>
<p>The reflex fix is to shrink the step. At <img src="https://latex.codecogs.com/png.latex?10%5E%7B-5%7D"> yr (~5 minutes) RK4 <em>does</em> stay stable:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">ok <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve_soil</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-5</span>),</span>
<span id="cb8-2">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates =</span> rates, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rk4"</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_solution</span>(ok)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-rk4-ok" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-rk4-ok-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://traitecoevo.github.io/overstorey/posts/2025-07-29-richards-equation-instability/index_files/figure-html/fig-rk4-ok-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-rk4-ok-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Explicit RK4 at Δt = 1e-5 yr stays stable — but this is only the first hundredth of a year, and already cost 1000 steps. A full year needs 100,000.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The infiltration front is smooth and physical — but look at the cost. That figure covers just <img src="https://latex.codecogs.com/png.latex?1/100"> of a year and already took 1000 steps. Integrating a single year would need 100,000 explicit steps, and a forest simulation runs for centuries. This is not a viable solver for the model.</p>
</section>
<section id="the-fix-an-implicit-solver" class="level2">
<h2 class="anchored" data-anchor-id="the-fix-an-implicit-solver">The fix: an implicit solver</h2>
<p>Stiffness is solved by <em>implicit</em> methods, which evaluate the right-hand side at the end of the step and so remain stable at large step sizes. <code>deSolve</code>’s <code>lsoda</code> switches automatically between stiff and non-stiff modes. Swapping it in — and taking a step of <img src="https://latex.codecogs.com/png.latex?0.1"> yr, ten thousand times larger than the one RK4 needed — integrates the whole year cleanly:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">stable <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve_soil</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>),</span>
<span id="cb9-2">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rates =</span> rates, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">state =</span> state, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lsoda"</span>)</span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_solution</span>(stable)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-lsoda" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-lsoda-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://traitecoevo.github.io/overstorey/posts/2025-07-29-richards-equation-instability/index_files/figure-html/fig-lsoda-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-lsoda-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Implicit LSODA at Δt = 0.1 yr integrates a full year in ~10 steps with no instability.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Same right-hand side, same parameters, same initial condition — the only change is the solver, and the instability is gone. That is the clean experimental proof that the problem was stiffness all along.</p>
</section>
<section id="why-this-happens" class="level2">
<h2 class="anchored" data-anchor-id="why-this-happens">Why this happens</h2>
<p>The Richards equation is a nonlinear, degenerate elliptic–parabolic PDE. As Farthing &amp; Ogden (2017) put it, it is “degenerate” because its strongly nonlinear coefficients approach zero in parts of the solution domain. Here the conductivity <img src="https://latex.codecogs.com/png.latex?K(%5Ctheta)%20%5Cpropto%20%5Ctheta%5E%7B%5C,2n_%5Cpsi+3%7D"> spans many orders of magnitude across the profile: with <img src="https://latex.codecogs.com/png.latex?n_%5Cpsi%20=%204.8"> that exponent is about <img src="https://latex.codecogs.com/png.latex?12.6">, so a modest moisture gradient produces an enormous spread of local timescales between nodes. Fast nodes set the stability limit of an explicit step; slow nodes set how long you must integrate. The ratio between them <em>is</em> the stiffness, and it forces explicit methods into impractically tiny steps.</p>
</section>
<section id="takeaways-for-the-soil-water-work" class="level2">
<h2 class="anchored" data-anchor-id="takeaways-for-the-soil-water-work">Takeaways for the soil-water work</h2>
<ul>
<li>The instability is intrinsic to the discretised Richards equation, not to our implementation — it reproduced identically in C++ and R.</li>
<li>Explicit Runge–Kutta is the wrong tool. The model needs an implicit / adaptive stiff solver (LSODA, or a BDF-type method) if we keep the full Richards profile.</li>
<li>If a stiff solver proves awkward to embed in the strategy’s inner loop, the discussion explores cheaper alternatives — a single- or two-layer bucket model, or a Kirchhoff-transform linearisation (Ross 2003) — that sidestep the stiffness rather than solving it.</li>
</ul>
<p>The next step for TF24 soil water is to decide between embedding an implicit solver and adopting a reduced-order bucket model; this experiment is what ruled out the naive explicit route.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>Farthing, M. W. &amp; Ogden, F. L. (2017). Numerical solution of Richards’ equation: a review of advances and challenges. <em>Soil Science Society of America Journal</em>, 81(6), 1257–1269.</li>
<li>Ireson, A. M. et al.&nbsp;(2023). The finite volume method for solving the Richards equation. <em>Hydrology and Earth System Sciences</em>.</li>
<li>Ross, P. J. (2003). Modeling soil water and solute transport — fast, simplified numerical solutions. <em>Agronomy Journal</em>, 95(6), 1352–1361.</li>
<li><a href="https://github.com/traitecoevo/plant/discussions/444">traitecoevo/plant discussion #444 — Problem modelling soil water with Richards Equation</a>. ```</li>
</ul>


</section>

 ]]></description>
  <category>tf24</category>
  <category>soil-water</category>
  <category>numerics</category>
  <category>experimental</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2025-07-29-richards-equation-instability/</guid>
  <pubDate>Tue, 29 Jul 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Prototyping a soil water-balance bucket model</title>
  <link>https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is an exploratory experiment from 2021, kept for the record. It prototypes soil water dynamics directly with <code>deSolve</code> and does not use the <code>plant</code> package API — it is not maintained and may not reflect the current model.</p>
</div>
</div>
<p><em>Originally authored by Isaac Towers, 2021-05-19.</em></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(deSolve)</span></code></pre></div></div>
</div>
<p>Here I will develop the prototype for the water bucket model for the <strong>plant</strong> model tracking the rate of change in <img src="https://latex.codecogs.com/png.latex?%5Ctheta"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%5Ctheta%7D%7Bdt%7D%20=%20%5Cfrac%7B1%7D%7Bz%7D%5Cbigg(%7BR%5C,I(%5Ctheta)%20-%20E_T%20(%5Ctheta)%20-%20E_S%20(%5Ctheta)%20-%20J(%5Ctheta)%7D%5Cbigg)%20"></p>
<p>Variables</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctheta%20(%20t)">: soil volumetric water content <img src="https://latex.codecogs.com/png.latex?(m%5E3%20/%20m%5E3)"></p>
<p>Parameters</p>
<p><img src="https://latex.codecogs.com/png.latex?R">: Rainfall <img src="https://latex.codecogs.com/png.latex?(m%5E3%20m%5E%7B-2%7D%20/%20t)"> <img src="https://latex.codecogs.com/png.latex?I">: Infiltration rate (unitless) <img src="https://latex.codecogs.com/png.latex?E_T">: Transpiration (water used by plants) <img src="https://latex.codecogs.com/png.latex?(m%5E3%20m%5E%7B-2%7D%20/%20t)"> <img src="https://latex.codecogs.com/png.latex?E_S">: Soil surface evaporation <img src="https://latex.codecogs.com/png.latex?(m%5E3%20m%5E%7B-2%7D%20/%20t)"> <img src="https://latex.codecogs.com/png.latex?J">: Drainage from soil profile <img src="https://latex.codecogs.com/png.latex?(m%5E3%20m%5E%7B-2%7D%20/t)"> <img src="https://latex.codecogs.com/png.latex?z">: Soil depth (m)</p>
<p><img src="https://latex.codecogs.com/png.latex?I(%5Ctheta)%20=%20%201-%5Cbigg(%5Cfrac%7B%5Ctheta(t)%7D%7B%5Ctheta_%7Bsat%7D%7D%5Cbigg)%5Eb"></p>
<p><img src="https://latex.codecogs.com/png.latex?b">: Unitless parameter determining the shape of water accumulation in the soil moisture bank <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7Bsat%7D"> Soil moisture capacity <img src="https://latex.codecogs.com/png.latex?(m%5E3%20m%5E%7B-3%7D)"></p>
<p>Ok, lets first show that rain will eventually fill up the soil without any water outputs if we include a saturated water content</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%5Ctheta%7D%7Bdt%7D%20=%20R%20*%5Cbigg(1-%5Cbigg(%5Cfrac%7B%5Ctheta(t)%7D%7B%5Ctheta_%7Bsat%7D%7D%5Cbigg)%5Eb%5Cbigg)"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb2-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb2-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.210</span>)</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb2-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>,</span>
<span id="cb2-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>,</span>
<span id="cb2-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb2-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>b)</span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb2-16">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb2-18"></span>
<span id="cb2-19">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>z</span>
<span id="cb2-20"></span>
<span id="cb2-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb2-22">  })</span>
<span id="cb2-23">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb3-3">    params,</span>
<span id="cb3-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb3-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>b)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/logistic-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Cool, let’s now include a drainage factor.</p>
<p>The soil moisture content in the top layer of soil is defined the soil moisture between 0m depth and the first monited depth point as follows:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%5Ctheta_1%7D%7Bdt%7D%20=%20%5Cfrac%7B1%7D%7Bz_1%7D%5Cbigg(R%5C,I-J_1%5Cbigg),"></p>
<p>where rainfall enters the soil and drains at rate: <img src="https://latex.codecogs.com/png.latex?J"> and <img src="https://latex.codecogs.com/png.latex?1"> represents the first monitored soil depth.</p>
<p>For all layers below the first, the soil moisture content is defined as follows:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%5Ctheta_i%7D%7Bdt%7D%20=%20%5Cfrac%7B1%7D%7Bz_i%20-%20z_%7Bi-1%7D%7D%5Cbigg(J_%7Bi-1%7D-J_i%5Cbigg)~i%3E1,"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?J_%7Bi-1%7D"> is the drainage of soil water from above.</p>
<p>Because we are still unsure about how water flux works with regards to the balance bewteen matric and gravitational potential, we will use a much more simplified version of water flux employed by Duursma and Medlyn (2012) where water is assumed to only travel downwards and is directly equivalent to the soil hydraulic conductivity <img src="https://latex.codecogs.com/png.latex?K_z~(%20m~day%5E%7B-1%7D)">):</p>
<p><img src="https://latex.codecogs.com/png.latex?J_i=%20-K_z(%5Ctheta),"></p>
<p>where:</p>
<p><img src="https://latex.codecogs.com/png.latex?K_z=%20K_%7Bsat%7D(%5Cfrac%7B%5Ctheta%7D%7B%5Ctheta_%7Bsat%7D%7D)%5E%7Bnk%7D,"></p>
<p>and <img src="https://latex.codecogs.com/png.latex?K_%7Bsat%7D"> <img src="https://latex.codecogs.com/png.latex?(m~day%5E%7B-1%7D)"> and <img src="https://latex.codecogs.com/png.latex?nk"> (unitless) are empirical parameter which vary with soil type</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb5-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb5-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta1=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta2=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta3=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters for silty loam (Landsberg)</span></span>
<span id="cb5-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>,</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>,</span>
<span id="cb5-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#convert to day</span></span>
<span id="cb5-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ksat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>),</span>
<span id="cb5-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nk =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.9</span></span>
<span id="cb5-14">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-15">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ksat)</span>
<span id="cb5-16"></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb5-18">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb5-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb5-20">    dtheta1_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (ksat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb5-21">    dtheta2_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ksat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (ksat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb5-22">    dtheta3_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ksat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb5-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta1_dt,dtheta2_dt,dtheta3_dt)))</span>
<span id="cb5-24">  })</span>
<span id="cb5-25">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb6-3">    params,</span>
<span id="cb6-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb6-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb6-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb6-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb6-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ks"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Top Layer"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Middle Layer"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottom Layer"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/drainage-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>An alternative which does allow for upwards flux is to calculate water flux based on diffusivity,D, <img src="https://latex.codecogs.com/png.latex?(m%5E2~yr%5E%7B-1%7D)">:</p>
<p><img src="https://latex.codecogs.com/png.latex?J_i%20=%20-D_s%5Cfrac%7B%5Cdelta%5Ctheta_s%7D%7B%5Cdelta%20z%7D,"></p>
<p>where D is equal to:</p>
<p><img src="https://latex.codecogs.com/png.latex?D(%5Ctheta)%20=%20D_%7Bsat%7D(%5Cfrac%7B%5Ctheta_s%7D%7B%5Ctheta_%7Bsat%7D%7D)%5E%7Bn_D%7D."></p>
<p>In this example, <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20z"> is calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cdelta%20z%20=%20z_%7Bi+1%7D%20-%20z_i,"></p>
<p>and the same for <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20%5Ctheta">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cdelta%20%5Ctheta%20=%20%5Ctheta_%7Bs,i+1%7D%20-%20%5Ctheta_%7Bs,i%7D."></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb8-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb8-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta11=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta22=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta33=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta44=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb8-6"></span>
<span id="cb8-7"></span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters for silty loam (Landsberg)</span></span>
<span id="cb8-10">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb8-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb8-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>,</span>
<span id="cb8-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dsat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>,</span>
<span id="cb8-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>,</span>
<span id="cb8-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>),</span>
<span id="cb8-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb8-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">amp=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb8-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">I_constant=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb8-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sine_height=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb8-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>,</span>
<span id="cb8-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ksat=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">292.8</span>,</span>
<span id="cb8-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nk=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.9</span></span>
<span id="cb8-23">    ))</span>
<span id="cb8-24"></span>
<span id="cb8-25"></span>
<span id="cb8-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb8-27">theta_rates1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta_init, params) {</span>
<span id="cb8-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta_init, params)), {</span>
<span id="cb8-29">    dtheta1_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta11<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta22<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta11)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>)</span>
<span id="cb8-30">    dtheta2_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta11<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta22<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta11)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta22<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta33<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta22)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>)</span>
<span id="cb8-31">    dtheta3_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta22<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta33<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta22)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>((ksat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta33<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)</span>
<span id="cb8-32">    dtheta4_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((ksat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta33<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)</span>
<span id="cb8-33"></span>
<span id="cb8-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta1_dt,dtheta2_dt,dtheta3_dt,dtheta4_dt)))</span>
<span id="cb8-35">  })</span>
<span id="cb8-36">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">logistic_solution1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb9-3">    params,</span>
<span id="cb9-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb9-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb9-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb9-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates1,</span>
<span id="cb9-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>())</span></code></pre></div></div>
</div>
<p>The same idea, now wired up as a general multi-layer model with configurable monitored depths, runoff and deep-drainage bookkeeping, and a seasonal rainfall signal.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#set time period and time step</span></span>
<span id="cb10-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#how many soil depths should be monitored</span></span>
<span id="cb10-5">n<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#set depth of points, can be customised. N-1 in this case accounts for one customsied point, all others are the same</span></span>
<span id="cb10-8">standard_depth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb10-9">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(standard_depth, n<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>))</span>
<span id="cb10-10"></span>
<span id="cb10-11">i<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#set vector to collect gradients</span></span>
<span id="cb10-14">dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vector</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> n)</span>
<span id="cb10-15"></span>
<span id="cb10-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#set initial soil moisture content for each layer (boundary z[i] - z[i-1]) as well as the amount of water lost to deep drainage and runoff and the total amount of rainfall falling in period for sanity check, particulary important for when inclduing temporal variation</span></span>
<span id="cb10-17">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">runoff =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deep_drainage=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cumulative_rain=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb10-18"></span>
<span id="cb10-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#set parameter values</span></span>
<span id="cb10-20">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb10-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb10-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>,</span>
<span id="cb10-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dsat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>,</span>
<span id="cb10-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>,</span>
<span id="cb10-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb10-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb10-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ksat=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">292.8</span>,</span>
<span id="cb10-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nk=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.9</span>,</span>
<span id="cb10-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">amp=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb10-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sine_height=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb10-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>,</span>
<span id="cb10-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">I_constant =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-33">    ))</span>
<span id="cb10-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#soil moisture drainage model</span></span>
<span id="cb10-35"></span>
<span id="cb10-36">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb10-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb10-38"></span>
<span id="cb10-39">    I <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>I_constant<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)</span>
<span id="cb10-40">    S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> amp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>frequency<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>sine_height</span>
<span id="cb10-41"></span>
<span id="cb10-42">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n){</span>
<span id="cb10-43"></span>
<span id="cb10-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#top layer. Non-infiltrating rainfall is stored in runoff, but leaves the plant-accessible water budget forever.</span></span>
<span id="cb10-45">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb10-46">    inflow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>S<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>I</span>
<span id="cb10-47">    runoff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>S)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>I)</span>
<span id="cb10-48">    outflow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta[i])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>z[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb10-49"></span>
<span id="cb10-50">      }</span>
<span id="cb10-51"></span>
<span id="cb10-52">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#middle layers</span></span>
<span id="cb10-53">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n)) {</span>
<span id="cb10-54">     inflow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta[i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>z[i])</span>
<span id="cb10-55">     outflow<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta[i])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>z[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb10-56">      }</span>
<span id="cb10-57"></span>
<span id="cb10-58">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># last layer - water that outflows from this layer is gone forever. Values are stored in deep_drainage.</span></span>
<span id="cb10-59">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> (n)) {</span>
<span id="cb10-60">    inflow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta[i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>z[i])</span>
<span id="cb10-61">    outflow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ksat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)</span>
<span id="cb10-62">    deep_drainage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ksat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)</span>
<span id="cb10-63">      }</span>
<span id="cb10-64"></span>
<span id="cb10-65">    dtheta_dt[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> inflow <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> outflow</span>
<span id="cb10-66">    runoff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> runoff</span>
<span id="cb10-67">    deep_drainage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> deep_drainage</span>
<span id="cb10-68">    cumulative_rain <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>S</span>
<span id="cb10-69">    }</span>
<span id="cb10-70"></span>
<span id="cb10-71">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt, runoff, deep_drainage, cumulative_rain)))</span>
<span id="cb10-72">  })</span>
<span id="cb10-73">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb11-3">    params,</span>
<span id="cb11-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb11-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb11-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb11-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb11-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>())</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(time, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(., <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Soil_depth =</span> name) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>Soil_depth))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/multilayer-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>A hard-coded ten-layer expansion of the same scheme, kept here as scratch working.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#ignore below for now</span></span>
<span id="cb13-2"></span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb13-5">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb13-8">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta1=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta2=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta3=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta4=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta5=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta6=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta7=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta8=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta9=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta10=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">runoff =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb13-9"></span>
<span id="cb13-10"></span>
<span id="cb13-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters for silty loam (Landsberg)</span></span>
<span id="cb13-12">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb13-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb13-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>,</span>
<span id="cb13-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dsat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>,</span>
<span id="cb13-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span>,</span>
<span id="cb13-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>),</span>
<span id="cb13-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb13-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">I_constant=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb13-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">amp=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb13-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sine_height=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb13-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frequency=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>,</span>
<span id="cb13-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ksat=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">292.8</span>,</span>
<span id="cb13-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nk=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.9</span>))</span>
<span id="cb13-25"></span>
<span id="cb13-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb13-27">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta_init, params) {</span>
<span id="cb13-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta_init, params)), {</span>
<span id="cb13-29">    I <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)</span>
<span id="cb13-30">    S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> amp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>frequency<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>sine_height</span>
<span id="cb13-31">    dtheta1_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>S<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>I<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb13-32">    dtheta2_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-33">    dtheta3_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-34">    dtheta4_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta5<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta4)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-35">    dtheta5_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta5<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta4)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta5<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta6<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta5)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-36">    dtheta6_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta5<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta6<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta5)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta6<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta7<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta6)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-37">    dtheta7_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta6<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta7<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta6)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta7<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta8<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta7)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-38">    dtheta8_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta7<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta8<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta7)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta8<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta9<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta8)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb13-39">    dtheta9_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>((<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>dsat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta8<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta9<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta8)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>((ksat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta9<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)</span>
<span id="cb13-40">    dtheta10_dt<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ksat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta9<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)</span>
<span id="cb13-41">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ((12.2*24)*(theta9/theta_sat)^11.9)</span></span>
<span id="cb13-42">    runoff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>S<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>I)</span>
<span id="cb13-43"></span>
<span id="cb13-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta1_dt,dtheta2_dt, dtheta3_dt,dtheta4_dt,dtheta5_dt,dtheta6_dt,dtheta7_dt,dtheta8_dt,dtheta9_dt,dtheta10_dt</span>
<span id="cb13-45">, runoff)))</span>
<span id="cb13-46">  })</span>
<span id="cb13-47">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb14-3">    params,</span>
<span id="cb14-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb14-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb14-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb14-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb14-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>())</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-2"> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1st Layer"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2nd Layer"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3rd Layer"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4th Layer"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta5, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5th Layer"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta6, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6th Layer"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta7, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7th Layer"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta8, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"8th Layer"</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta9, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9th Layer"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/tenlayer-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Now let’s add evaporation. We will use the Penman-Monteith equation to model evaporation as a water flux (rather than an energy flux):</p>
<p><img src="https://latex.codecogs.com/png.latex?E=%5Cfrac%7B%5CDelta*%5Cvarphi_s+g_a*p_a*c_%7Bpa%7D*D%7D%7B%5CDelta+%5Cgamma(1+%5Cfrac%7Bg_a%7D%7Bg_s%7D)%7D*%5Cfrac%7B1%7D%7BL%7D,"></p>
<p>Where E is the evaporation in m (i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?mm">). This equation is dependent on the air temperature, which manifests its effect on evaporation through determining the vapour pressure deficit <img src="https://latex.codecogs.com/png.latex?D">, and the derivative of the saturated vapour pressure curve at a given air temperature <img src="https://latex.codecogs.com/png.latex?%5CDelta">, the net radiation reaching the soil surface <img src="https://latex.codecogs.com/png.latex?%5Cvarphi_s">, and soil moisture which affects the soil conductance <img src="https://latex.codecogs.com/png.latex?g_s">.</p>
<p>The equation for the saturated vapour pressure is dependent on air temperature <img src="https://latex.codecogs.com/png.latex?(T_%7Bair%7D~(%5E%5Ccirc%20C))"> only:</p>
<p><img src="https://latex.codecogs.com/png.latex?e_s=0.6108%5Cexp%5Cbigg(%5Cfrac%7B17.27*T_%7B%5Crm%20air%7D%7D%7BT_%7B%5Crm%20air%7D%20+%20237.3%7D%5Cbigg)."></p>
<p>Thus, <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%20%5Cfrac%7B%5Cdelta%20e_s%7D%7B%5Cdelta%20T_%7Bair%7D%7D."></p>
<p>To estimate the daily average vapour pressure deficit, the maximum <img src="https://latex.codecogs.com/png.latex?T_%7Bmax%7D"> and minimum <img src="https://latex.codecogs.com/png.latex?T_%7Bmin%7D"> air temperature can be used <img src="https://latex.codecogs.com/png.latex?%5Coverline%7BD%7D%20=%20e_s(T_%7B%5Crm%20max%7D)-e_s(T_%7B%5Crm%20min%7D),"></p>
<p>and the daytime average vapour pressure deficit <img src="https://latex.codecogs.com/png.latex?D_sd"> can be calculated by assuming a fraction of daytime hours <img src="https://latex.codecogs.com/png.latex?%5Calpha">: <img src="https://latex.codecogs.com/png.latex?%5Coverline%7BD_%7Bdaytime%7D%7D%20=%20%20%5Calpha%20*%20e_s(T_%7Bmax%7D)-%20(1-%5Calpha)%20*%20e_s(T_%7Bmin%7D)."></p>
<p>Radiation reaching the soil surface (i.e.&nbsp;passing through the tree canopy) is assumed to be entirely absorbed. Thus, net radiation at the soil surface is defined as the radiation incident on the tree canopy <img src="https://latex.codecogs.com/png.latex?%5Cvarphi_0"> minus the radiation absorbed the tree canopy <img src="https://latex.codecogs.com/png.latex?%5Cvarphi_na"></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cvarphi_s%20=%20%5Cvarphi_0%20-%20%5Cvarphi_%7Bna%7D,"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cvarphi_%7Bna%7D"> is dependent on the leaf area index (LAI) of the canopy and the light extinction coefficient (k):</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cvarphi_%7Bna%7D%20=%20%5Cvarphi_0*(1-e%5E%7B-k*LAI%7D)"></p>
<p>Conductance of water vapour through the soil moisture boundary layer <img src="https://latex.codecogs.com/png.latex?g_c"> is dependent on soil moisture, because resistance to evaporation increases as the soil becomes increasingly dry according the following equation. Using an equation proposed by Soares and Almeida (2001):</p>
<p><img src="https://latex.codecogs.com/png.latex?g_%7Bs%7D%20=%20g_%7Bs_%7Bmax%7D%7D*(%5Ctheta/%5Ctheta_%7Bsat%7D)"></p>
<p>Once through the soil moisture boundary layer, water vapour must then travel through the atmospheric boundary layer according to the rate <img src="https://latex.codecogs.com/png.latex?g_a">. For soil, a fixed value is currently assumed using value obtained from Soares and Almeida (2001) where <img src="https://latex.codecogs.com/png.latex?g_%7Ba_s%7D%20=%200.01%20m~s%5E%7B-1%7D"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb16-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3650</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb16-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>)</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb16-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb16-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>,</span>
<span id="cb16-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>,</span>
<span id="cb16-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb16-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gcmax=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0025</span>,</span>
<span id="cb16-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gb=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>,</span>
<span id="cb16-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vpd=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">900</span>,</span>
<span id="cb16-15">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.204</span>,</span>
<span id="cb16-16">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cpa =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1004</span>,</span>
<span id="cb16-17">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">66.1</span>,</span>
<span id="cb16-18">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>,</span>
<span id="cb16-19">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">air_temp=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>,</span>
<span id="cb16-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope_sat_vap_curve =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4098</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6108</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>((<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.27</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>air_temp)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(air_temp<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">+237.3</span>)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(air_temp<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">+237.3</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb16-21">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#radiation will be fed in from plant model based on its solar radiation submodel and plant lai</span></span>
<span id="cb16-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rad =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">434</span>),</span>
<span id="cb16-23">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_air_temp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>,</span>
<span id="cb16-24">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min_air_temp=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb16-25">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.45</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span id="cb16-26">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-27">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rad)</span>
<span id="cb16-28"></span>
<span id="cb16-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb16-30">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb16-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb16-32">    evap_numerator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> slope_sat_vap_curve <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>rad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>cpa<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>vpd</span>
<span id="cb16-33">    evap_denominator <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> slope_sat_vap_curve <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>gb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(gcmax<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)))</span>
<span id="cb16-34">    evap<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>evap_numerator<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>evap_denominator<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>L)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb16-35">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> evap)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>z</span>
<span id="cb16-36"></span>
<span id="cb16-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb16-38">  })</span>
<span id="cb16-39">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb17-3">    params,</span>
<span id="cb17-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb17-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb17-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb17-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb17-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rad"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>rad)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/evap-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb19-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb19-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta1=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta2=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta3=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb19-6"></span>
<span id="cb19-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb19-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb19-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(</span>
<span id="cb19-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.513</span>,</span>
<span id="cb19-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ds =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.86</span>,</span>
<span id="cb19-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>,</span>
<span id="cb19-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00274</span></span>
<span id="cb19-14">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-15">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ds)</span>
<span id="cb19-16"></span>
<span id="cb19-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb19-18">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb19-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb19-20"></span>
<span id="cb19-21">    dtheta_dt1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(ds<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta2)</span>
<span id="cb19-22">    dtheta_dt2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ds<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ((ds<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta3))</span>
<span id="cb19-23">    dtheta_dt3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ((ds<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>((theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nd)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>theta3))</span>
<span id="cb19-24"></span>
<span id="cb19-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt1,dtheta_dt2,dtheta_dt3)))</span>
<span id="cb19-26">  })</span>
<span id="cb19-27">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb20-3">    params,</span>
<span id="cb20-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb20-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb20-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb20-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb20-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ks"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/diffusivity-3layer-plot-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/diffusivity-3layer-plot-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/diffusivity-3layer-plot-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb24-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb24-3"></span>
<span id="cb24-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb24-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>)</span>
<span id="cb24-6"></span>
<span id="cb24-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb24-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb24-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(</span>
<span id="cb24-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00021388</span>),</span>
<span id="cb24-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb24-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nk =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.007</span>,</span>
<span id="cb24-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.8</span>,</span>
<span id="cb24-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pw =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb24-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">apsi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.87</span>,</span>
<span id="cb24-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">npsi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ks)</span>
<span id="cb24-18"></span>
<span id="cb24-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb24-20">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb24-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb24-22"></span>
<span id="cb24-23">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(((ks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(g<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pw)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> apsi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> npsi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>npsi<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>))</span>
<span id="cb24-24"></span>
<span id="cb24-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb24-26">  })</span>
<span id="cb24-27">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb25-3">    params,</span>
<span id="cb25-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb25-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb25-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb25-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb25-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ks"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 8.29801e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 6.27732e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 4.74105e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 3.57492e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 2.69115e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 2.02247e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 1.51736e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 1.13643e-16
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 8.49653e-17
 
DLSODA-  Warning..Internal T (=R1) and H (=R2) are
      such that in the machine, T + H = T on the next step  
     (H = step size). Solver will continue anyway.
In above message, R1 = 9.93182, R2 = 6.34116e-17
 
DLSODA-  Above warning has been issued I1 times.  
     It will not be issued again for this problem.
In above message, I1 = 10
 </code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb27-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/potential-driven-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Parameters</p>
<p><img src="https://latex.codecogs.com/png.latex?K_s">: Saturated soil hydraulic conductivity <img src="https://latex.codecogs.com/png.latex?(m%5E3%20m%5E%7B-2%7D%20yr%5E%7B-1%7D)"> <img src="https://latex.codecogs.com/png.latex?c">: Unitless empirical soil parameter</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb28-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb28-3"></span>
<span id="cb28-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb28-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb28-6"></span>
<span id="cb28-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb28-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb28-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb28-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb28-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.47</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">146</span>),</span>
<span id="cb28-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb28-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb28-14">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ks)</span>
<span id="cb28-15"></span>
<span id="cb28-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb28-17">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb28-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb28-19"></span>
<span id="cb28-20">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>c</span>
<span id="cb28-21"></span>
<span id="cb28-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb28-23">  })</span>
<span id="cb28-24">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb29-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb29-3">    params,</span>
<span id="cb29-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb29-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb29-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb29-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb29-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb29-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ks"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/infiltration-drainage-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Now with climatic variability of amplitude = the annual mean rainfall</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb31-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb31-3"></span>
<span id="cb31-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb31-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb31-6"></span>
<span id="cb31-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb31-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb31-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb31-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb31-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.47</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5550.37</span>),</span>
<span id="cb31-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>,</span>
<span id="cb31-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-14">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ks)</span>
<span id="cb31-15"></span>
<span id="cb31-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb31-17">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb31-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb31-19"></span>
<span id="cb31-20">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>c</span>
<span id="cb31-21"></span>
<span id="cb31-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb31-23">  })</span>
<span id="cb31-24">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb32-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb32-3">    params,</span>
<span id="cb32-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb32-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb32-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb32-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb32-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb32-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ks"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb33-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/variable-rain-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb34-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb34-3"></span>
<span id="cb34-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb34-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb34-6"></span>
<span id="cb34-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb34-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb34-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb34-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb34-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.47</span>),</span>
<span id="cb34-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>,</span>
<span id="cb34-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb34-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pe =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>),</span>
<span id="cb34-15">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.46</span>,</span>
<span id="cb34-16">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LAI =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb34-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pe)</span>
<span id="cb34-18"></span>
<span id="cb34-19"></span>
<span id="cb34-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb34-21">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb34-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb34-23"></span>
<span id="cb34-24">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb34-25">    pe<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LAI)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))))</span>
<span id="cb34-26"></span>
<span id="cb34-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb34-28">  })</span>
<span id="cb34-29">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb35-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb35-3">    params,</span>
<span id="cb35-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb35-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb35-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb35-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb35-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb35-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pe"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb36-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>pe)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/evap-lai-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb37-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb37-3"></span>
<span id="cb37-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb37-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb37-6"></span>
<span id="cb37-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters</span></span>
<span id="cb37-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb37-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb37-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R  =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb37-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.47</span>),</span>
<span id="cb37-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>,</span>
<span id="cb37-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb37-14">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pe =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>),</span>
<span id="cb37-15">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.46</span>,</span>
<span id="cb37-16">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LAI =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb37-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pe)</span>
<span id="cb37-18"></span>
<span id="cb37-19"></span>
<span id="cb37-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb37-21">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb37-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb37-23"></span>
<span id="cb37-24">    dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>c <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb37-25">    pe<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> LAI)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))))</span>
<span id="cb37-26"></span>
<span id="cb37-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb37-28">  })</span>
<span id="cb37-29">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb38-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb38-3">    params,</span>
<span id="cb38-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb38-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb38-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb38-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb38-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb38-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pe"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb39-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>pe)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/evap-lai-variable-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Evaporation should occur only in the top 10 cm of soil <a href="https://pure.mpg.de/rest/items/item_1781494/component/file_1786328/content" class="uri">https://pure.mpg.de/rest/items/item_1781494/component/file_1786328/content</a></p>
<p><img src="https://latex.codecogs.com/png.latex?J"> is defined as:</p>
<p><img src="https://latex.codecogs.com/png.latex?J=%20-D_z(%5Ctheta_z)%5Cfrac%7B%5Cdelta%5Ctheta%7D%7B%5Cdelta%7Bz%7D%7D,"></p>
<p>Where the diffusivity, <img src="https://latex.codecogs.com/png.latex?D"> <img src="https://latex.codecogs.com/png.latex?(m%5E2%20s%5E%7B-1%7D)"> is defined as:</p>
<p><img src="https://latex.codecogs.com/png.latex?D_z(%5Ctheta_z)%20=%20%5Cfrac%7BK_z%7D%7Bg*p_w%7D%5Cfrac%7B%5Cdelta%5Cpsi%7D%7B%5Cdelta%5Ctheta%7D,"></p>
<p>and the hydraulic conductivity <img src="https://latex.codecogs.com/png.latex?K_d"> <img src="https://latex.codecogs.com/png.latex?(m~yr%5E%7B-1%7D)"> is defined as:</p>
<p><img src="https://latex.codecogs.com/png.latex?K_z=K_%7Bsat%7D%5Cbigg(%5Cfrac%7B%5Ctheta_z%7D%7B%5Ctheta_%7Bsat%7D%7D%5Cbigg)%5E%7Bnk%7D."></p>
<p><img src="https://latex.codecogs.com/png.latex?K_%7Bsat%7D"> <img src="https://latex.codecogs.com/png.latex?(m~yr%5E%7B-1%7D)"> and <img src="https://latex.codecogs.com/png.latex?nk"> (unitless) are empirical parameter which vary with soil type, <img src="https://latex.codecogs.com/png.latex?g"> is acceleration due to gravity <img src="https://latex.codecogs.com/png.latex?(9.8~m~s%5E%7B-2%7D)"> and <img src="https://latex.codecogs.com/png.latex?p_w"> is the density of water <img src="https://latex.codecogs.com/png.latex?(1000~kg~m%5E%7B-3%7D)">. Finally, <img src="https://latex.codecogs.com/png.latex?%5Cpsi"> is the soil water potential.</p>
<p>Because the soil water potential at a given depth is related to soil moisture content according to the following equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cpsi_z=-a_%5Cpsi%20%5Ctheta_z%5E%7B-n_%5Cpsi%7D,"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?a_%5Cpsi"> and <img src="https://latex.codecogs.com/png.latex?n_%5Cpsi"> are soil-dependent empirical parameters. It follows that diffusivity can be calculated in terms of soil moisture content at a given depth according to the following equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?D_z(%5Ctheta_z)%20=%20%5Cfrac%7BK_z%7D%7Bg*p_w%7Da_%7B%5Cpsi%7Dn_%7B%5Cpsi%7D%5Ctheta_z%5E%7B-n_%7B%5Cpsi%7D-1%7D,"></p>
<p>Thus, <img src="https://latex.codecogs.com/png.latex?J"> equals:</p>
<p><img src="https://latex.codecogs.com/png.latex?J=%20-%5Cfrac%7BK_%7Bsat%7D%5Cbigg(%5Cfrac%7B%5Ctheta_z%7D%7B%5Ctheta_%7Bsat%7D%7D%5Cbigg)%5E%7Bnk%7D%7D%7Bg*p_w%7Da_%7B%5Cpsi%7Dn_%7B%5Cpsi%7D%5Ctheta_z%5E%7B-n_%7B%5Cpsi%7D-1%7D%5Cfrac%7B%5Cdelta%5Ctheta%7D%7B%5Cdelta%7Bz%7D%7D,"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Timestep</span></span>
<span id="cb40-2">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb40-3"></span>
<span id="cb40-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Variables</span></span>
<span id="cb40-5">theta_init <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>)</span>
<span id="cb40-6"></span>
<span id="cb40-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model parameters for silty loam (Landsberg)</span></span>
<span id="cb40-8">params <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb40-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span>(</span>
<span id="cb40-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#convert to day</span></span>
<span id="cb40-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ksat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>),</span>
<span id="cb40-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nk =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.9</span>,</span>
<span id="cb40-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_sat =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.482</span>,</span>
<span id="cb40-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.8</span>,</span>
<span id="cb40-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pw =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb40-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">apsi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.87</span>,</span>
<span id="cb40-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">npsi =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb40-18">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(., .<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ksat)</span>
<span id="cb40-19"></span>
<span id="cb40-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates of change</span></span>
<span id="cb40-21">theta_rates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t, theta, params) {</span>
<span id="cb40-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(theta, params)), {</span>
<span id="cb40-23">    soil_conductivity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ksat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>theta_sat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>nk</span>
<span id="cb40-24">    dpsi_dtheta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> apsi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> npsi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>npsi<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)</span>
<span id="cb40-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dtheta_dt = - (soil_conductivity/(g*pw))*dpsi_dtheta*((theta-0.2)/0.1)</span></span>
<span id="cb40-26">      dtheta_dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (soil_conductivity)</span>
<span id="cb40-27"></span>
<span id="cb40-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(dtheta_dt)))</span>
<span id="cb40-29">  })</span>
<span id="cb40-30">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1">logistic_solution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb41-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dfr</span>(</span>
<span id="cb41-3">    params,</span>
<span id="cb41-4">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ode</span>(</span>
<span id="cb41-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> theta_init,</span>
<span id="cb41-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> t,</span>
<span id="cb41-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> theta_rates,</span>
<span id="cb41-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parms =</span> .x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb41-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ks"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1">logistic_solution <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb42-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(time, theta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col=</span>ks)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/index_files/figure-html/diffusivity-flux-plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>



 ]]></description>
  <category>experimental</category>
  <category>soil-water</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2024-05-22-water-bucket-model/</guid>
  <pubDate>Wed, 22 May 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Key technologies used to build the plant package</title>
  <dc:creator>Rich FitzJohn &amp; Daniel Falster</dc:creator>
  <link>https://traitecoevo.github.io/overstorey/posts/2016-02-23-plant-key-technologies/</link>
  <description><![CDATA[ 




<div class="callout callout-style-simple callout-note">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>Originally published on the <a href="https://methodsblog.com/2016/02/23/plant/"><em>Methods in Ecology and Evolution</em> Methods blog</a> on 23 February 2016, by <strong>Rich FitzJohn and Daniel Falster</strong>, and reposted here in the Overstorey notebook. It accompanies the package’s methods paper, Falster <em>et al.</em>, <a href="https://doi.org/10.1111/2041-210X.12525">doi:10.1111/2041-210X.12525</a>.</p>
</div>
</div>
</div>
<p>Our <a href="https://doi.org/10.1111/2041-210X.12525">paper</a> in <em>Methods in Ecology and Evolution</em> describes a new software package, <strong><a href="https://github.com/traitecoevo/plant"><code>plant</code></a></strong>. <code>plant</code> is an individual-based simulation model that simulates the growth of individual trees, stands of competing plants, or entire metacommunities under a disturbance regime, using common physiological rules and trait-based functional trade-offs to capture differences among species.</p>
<section id="non-linear-processes-and-thousands-of-plants" class="level2">
<h2 class="anchored" data-anchor-id="non-linear-processes-and-thousands-of-plants">Non-linear processes and thousands of plants</h2>
<p>Since the development of gap models in the 1970s (e.g. <a href="https://doi.org/10.2307/2258570">Botkin 1972</a>), researchers have been using computer simulations to investigate how elements of plant biology interact with competition and disturbance regimes to influence vegetation demography, structure and diversity. Simulating the competitive interactions among many thousands of plants, however, is no easy task.</p>
<p>Despite widespread recognition of the importance of key non-linear processes — such as size-structured competition, disturbance, and trait-based trade-offs — for vegetation dynamics, relatively few researchers have been brave (or daft) enough to try and incorporate such processes into their models. The situation is most extreme in theoretical ecology, where much contemporary theory (e.g.&nbsp;coexistence theory, neutral theory) is still built around completely unstructured populations.</p>
</section>
<section id="features-of-plant" class="level2">
<h2 class="anchored" data-anchor-id="features-of-plant">Features of <code>plant</code></h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2016-02-23-plant-key-technologies/fig-1.png" class="img-fluid figure-img" style="width:40.0%"></p>
<figcaption>Key processes modelled within the <code>plant</code> package.</figcaption>
</figure>
</div>
<p>The <code>plant</code> package attempts to change that by providing an extensible, open source framework for studying trait-, size- and patch-structured dynamics. One thing that makes the <code>plant</code> model significant is the focus on traits. <code>plant</code> is one of several attempts seeking to integrate current understanding about trait based trade-offs into a model of individual plant function (see also <a href="https://doi.org/10.1890/0012-9615(2001)071%5B0557:AMFSVD%5D2.0.CO;2">Moorcroft et al.&nbsp;2001</a>, <a href="https://doi.org/10.1111/gcb.12870">Sakschewski et al.&nbsp;2015</a>).</p>
<p>A second feature that makes the <code>plant</code> software significant is that it is perhaps the first example where a computationally intensive model has been packaged up in a way that enables widespread usage, makes the model more usable and doesn’t sacrifice speed.</p>
<p>In this post we will describe the key technologies used to build the <code>plant</code> software.</p>
</section>
<section id="overcoming-the-speedgenerality-trade-off" class="level2">
<h2 class="anchored" data-anchor-id="overcoming-the-speedgenerality-trade-off">Overcoming the speed/generality trade-off</h2>
<p><a href="https://www.r-project.org/">R</a> has become a widely used language in ecology and evolution, but it’s not ideal for building individual based simulations, due to its relatively low speed. The solution to this has traditionally been to write simulations in a lower-level compiled language (such as C, C++ or Java), have the simulation write output to files, and analyse those with R or a higher-level language (<a href="https://github.com/traitecoevo/evolved_neutrality">indeed that is the direction originally taken with this model</a>). This traditional approach leads to a strong speed/flexibility trade-off. This trade-off arises because of:</p>
<ul>
<li>the need to write code to generate and parse the output files</li>
<li>the need to include options for parsing all sorts of arguments in your C++ code</li>
<li>the need to regularly recompile the C++ code whenever you want to add an option</li>
<li>the inability to exploit R’s built-in functions (e.g.&nbsp;for probability distributions or integration, for non-time sensitive elements of the code)</li>
</ul>
<p>Overall, having separate code for running the model and analysing model output greatly reduces flexibility in interacting with the model.</p>
<p>In <code>plant</code>, we wanted the best of both worlds – a simulation that would run at the speed of a compiled language but would allow us to interact with every part of the model. We wanted to be able to simulate our metacommunity of many species with 1000’s of plants and then drill down and inspect the physiological details of any individual plant. At the same time, we wanted the machinery to be flexible enough to allow us to swap out the low-level physiological model of the plants used while keeping the higher level behaviour the same.</p>
<p>We implemented the core of the model in C++, because code written in C++ can be <em>much</em> faster than code written in R. In our C++ code, we have plants that are grouped together into species, which exist in patches. They interact with a common light environment, and the whole system is controlled by a rather large set of parameters. Exposing these to R is a challenge because this modelling style relies on a fundamentally different approach to dealing with exposing data to R (in this style of C++ objects modify themselves, whereas in R objects are generally immutable with function calls creating new objects).</p>
<p>To bridge this divide we combined two new technologies in R; <a href="https://www.rcpp.org/">Rcpp</a> and <a href="https://github.com/wch/R6">R6 classes</a> with a new package of our own making, <a href="https://github.com/richfitz/RcppR6">RcppR6</a>. This allows us to export a “pointer” to our C++ objects to R, and interact with it using the same set of methods that are available on the C++ side. As these objects contain other objects (species contain plants, etc) we had to create interfaces for all of our objects, which we did using a code generation approach. We first describe the <a href="https://github.com/traitecoevo/plant/blob/master/inst/RcppR6_classes.yml">interface</a> for each object type we want to interact with. For example, <a href="https://github.com/traitecoevo/plant/blob/v1.0.0/inst/RcppR6_classes.yml#L298-336">here</a> is the interface for our basic “plant” type. The <a href="https://github.com/richfitz/RcppR6">RcppR6</a> package then generates a <a href="https://github.com/traitecoevo/plant/blob/master/src/RcppR6.cpp">lot</a> of <a href="https://github.com/traitecoevo/plant/blob/master/R/RcppR6.R">boilerplate</a> code required to export the objects to R and back again (<a href="https://en.wikipedia.org/wiki/Boilerplate_code">boilerplate are sections of code that have to be included in many places with little or no alteration</a>).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2016-02-23-plant-key-technologies/rcppr6.png" class="img-fluid figure-img" style="width:60.0%"></p>
<figcaption>The <code>RcppR6</code> package reduces the amount of interface code you need to write when linking C++ with R.</figcaption>
</figure>
</div>
<p>We used a “<em><a href="https://en.wikipedia.org/wiki/Generic_programming">generic programming</a></em>” approach to allow for different physiologies; plants provide particular methods (such as reporting their height, growth rate, etc) and they can be used with any of the higher level functions (such as the SCM algorithm, or tools to grow plants to a different size). For example, <a href="https://github.com/traitecoevo/plant/blob/master/inst/include/plant/plant_tools.h">this file</a> implements a function to compute the whole plant light compensation point for any given plant type <code>T</code>. The C++ compiler establishes that each plant type that the function is used with can be used and generates appropriate code for each.</p>
<p>On the R side, we use R’s simple <a href="http://adv-r.had.co.nz/OO-essentials.html#s3">“S3”</a> generic programming system to dispatch to the appropriate compiled function based on the type of the plant used. We think this mix of compile-time polymorphism and run-time polymorphism could enable efficient models and expressive bindings from dynamic languages.</p>
<p>Overall our approach allowed us to have both the expressiveness and interactivity of a dynamic language with the speed of C++. When running a simulation almost all the code runs at compiled speed, with R (which has a reputation for being slow) idle. But when the time comes to analyse our work with the outputs of the model, we can directly access any part of the simulation. We exploit this ability to interact with the model at any level in the <a href="https://traitecoevo.github.io/plant">vignettes for the package</a>.</p>
<p>You can see some code using the <code>plant</code> package, in <a href="https://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12525">Appendix S3 of our paper</a>.</p>
</section>
<section id="version-control-facilitates-seamless-collaboration" class="level2">
<h2 class="anchored" data-anchor-id="version-control-facilitates-seamless-collaboration">Version control facilitates seamless collaboration</h2>
<p>Increasingly, scientists are recognising <a href="https://doi.org/10.1186/1751-0473-8-7">the virtues of serious version control</a>. We use <a href="https://git-scm.com/">git</a> and, as we were early adopters, the entire history of development for <code>plant</code> has been tracked under version control. Alongside git, we used the code-sharing website <a href="https://github.com/traitecoevo/plant/">GitHub</a> to host our git repository. GitHub facilitates seamless collaboration by:</p>
<ul>
<li>providing a nice interface for seeing who changed what and when</li>
<li>keeping older versions of the code</li>
<li>allowing users (including ourselves) to report potential issues</li>
<li>allowing others to clone the package and make changes</li>
</ul>
<p>This track-record of what changed when was invaluable to us as we tracked down bugs and regressions and for working on code collaboratively.</p>
</section>
<section id="regular-testing-saves-time-in-the-long-run" class="level2">
<h2 class="anchored" data-anchor-id="regular-testing-saves-time-in-the-long-run">Regular testing saves time in the long run</h2>
<p>Models as large as this require a lot of testing to be sure that they are correct. Until recently, this testing process tended to be manual. As researchers develop models they will usually test them extensively, but these tests generally do not get stored for future use.</p>
<p>We made use of the <a href="https://github.com/r-lib/testthat">testthat</a> package to run a large set of automated tests (1307 at last count) covering all aspects of the model from validating inputs to the behaviour of the model in known pathological parts of parameter space. We combine this test suite with <a href="https://travis-ci.org/">Travis CI</a> — an automated testing service — to ensure that the tests are run every time someone makes a change to the code (and we all get an email if someone breaks it).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2016-02-23-plant-key-technologies/travis.png" class="img-fluid figure-img" style="width:50.0%"></p>
<figcaption>Pushing changes in our code to GitHub automatically triggers a bunch of tests to run via Travis CI. We get notified if we inadvertently broke any core functionality.</figcaption>
</figure>
</div>
<p>This test-driven-development approach has allowed us to swap out large parts of the package as we developed it and changed our minds about direction. We can run any changes through the test suite, so we can be reasonably sure we haven’t broken any previous behaviour. While testing is not a panacea it allowed us to be much more confident than we would otherwise have been in making changes to the model.</p>
<p>We strongly encourage other groups writing simulation models to consider testing from the outset as we believe it saves time in the long run.</p>
</section>
<section id="r-packages-easy-installation-and-distribution" class="level2">
<h2 class="anchored" data-anchor-id="r-packages-easy-installation-and-distribution">R packages: easy installation and distribution</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://traitecoevo.github.io/overstorey/posts/2016-02-23-plant-key-technologies/rlogo.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="The R logo" width="120"></p>
</figure>
</div>
<p>Using R brings many benefits beyond ease of analysis. Specifically, there are well established processes for installing packages, making it easy to deploy software across diverse platforms. While many packages are deployed via R’s core packaging system <a href="https://cran.r-project.org/">CRAN</a>, packages can also be directly installed from GitHub using the handy <a href="https://github.com/r-lib/devtools">devtools package</a>.</p>
<p>Installing <code>plant</code> should be as easy as running these lines of code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"devtools"</span>)</span>
<span id="cb1-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"traitecoevo/plant"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dependencies =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>(unfortunately we are waiting on updates to the R/Windows compiler toolchain for this to work on Windows.)</p>
<p>From then on, all you need to do is run <code>library(plant)</code> and you’re ready to use our model. Running <code>library(help = plant)</code> from within R will display all the help files for the project.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>As simulation models become more common place in ecology, we modellers should move beyond implementing one-off models that work locally but use bespoke interfaces and arbitrary command line arguments to run. We hope that <code>plant</code>, with its extensible interface and combination of compiled-language speed and dynamic language flexibility, will be a useful starting point for further research in this space.</p>
<div class="callout callout-style-simple callout-tip">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>To find out more about <code>plant</code>, read <a href="https://doi.org/10.1111/2041-210X.12525"><em>Plant: A package for modelling forest trait ecology &amp; evolution</em> by Falster <em>et al.</em> (2015)</a>. This article was part of the British Ecological Society’s Cross-Journal Special Feature, <em>Demography Beyond the Population</em>. You can also read the <a href="https://methodsblog.com/2016/02/23/plant/">original post on the MEE Methods blog</a>.</p>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>announcement</category>
  <category>engineering</category>
  <guid>https://traitecoevo.github.io/overstorey/posts/2016-02-23-plant-key-technologies/</guid>
  <pubDate>Tue, 23 Feb 2016 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
