Fix unit mismatch in glycogen energy flux term in R() helper#10
Merged
RodrigoZepeda merged 3 commits intoMay 29, 2026
Merged
Conversation
The glycogen contribution to the energy balance equation (Hall et al. 2011 Lancet, Eq 3/9) requires ρ_G·dG/dt in kcal/day. The code was passing dG/dt in kg/day without multiplying by roG (4206.5 kcal/kg), making the term ~4000x smaller than intended. In practice the impact is negligible (~0.01 vs ~42 kcal/day against ~2000+ total), and all existing tests still pass within their tolerance bands.
Contributor
|
Thank you so much for the report. I'll take a look at the equations (its been a while) and come back later this week. |
Contributor
|
Thank you for the report! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug fix: glycogen energy flux term — units mismatch in
Adult::R()File:
src/adult_weight.cpp:374One-line fix:
+ dG(t, G)→+ roG * dG(t, G)TL;DR
The lean-mass right-hand-side helper
Adult::R()sums several terms that must all be in kcal/day. One of those terms — the glycogen energy flux — was being passed in kg/day, off by a factor ofρ_G ≈ 4206.5 kcal/kg. Multiplying byroGrestores the units required by Eq 3 / Eq 9 of the Hall et al. 2011 Lancet web appendix.At-a-glance diff
//R helper for Lean derivative NumericVector Adult::R(double t, NumericVector L, NumericVector G, NumericVector AT, NumericVector ECF){ NumericVector F = fatMass(L); NumericVector weight = L + F + ECF + 3.7*(G); - NumericVector R3 = K + delta*weight + TEF(t) + AT - TotalIntake(t) + dG(t, G); + NumericVector R3 = K + delta*weight + TEF(t) + AT - TotalIntake(t) + roG*dG(t, G); return (R3 + gammaL*L + gammaF*F)/(alfa1 + alfa2*F); }Unit audit of
R3R3is a sum that must be dimensionally homogeneous in kcal/day:KK(rmr·PAL − γ_L·L − γ_F·F − δ·BW)delta*weightδ · BWTEF(t)TEF = β_TEF · ΔEIATATTotalIntake(t)EIdG(t, G)dG/dtroG*dG(t, G)ρ_G · dG/dtdG(t, G)is defined atsrc/adult_weight.cpp:315-317:This already matches the paper's Eq 1 solved for
dG/dt: dividing both sides byρ_GyieldsdG/dtin kg/day. So every consumer ofdG(...)that wants an energy flux must multiply byroGagain.Paper reference — the equation the code is implementing
From the Hall et al. 2011 Lancet supplementary web appendix (NIDDK PDF, pages 1–3):
Eq 1 — glycogen dynamics:
So
dG/dt = (CI − k_G · G²) / ρ_G, in kg/day — exactly whatAdult::dG()returns.Eq 3 — energy partitioning (lean):
The glycogen term enters the partitioning equation as
ρ_G · dG/dt(kcal/day), notdG/dtalone.Eq 9 — closed-form EE (which
R()implements after substituting Eq 3 into Eq 5):Again, the glycogen flux appears as
ρ_G · dG/dt. Without theroGmultiplier, the C++ code was computingEEwith a glycogen term roughly 4000× too small.Numerical impact
For a sedentary adult at energy balance with
CI ≈ 1100 kcal/daycarbohydrate intake andG ≈ 500 g, the steady-state glycogen fluxdG/dtis essentially zero, so the term contributes near nothing. During a transient (first days of a diet change),dG/dtcan reach ~0.01 kg/day:dG/dt≈ 0.01 kcal/day ← negligible (wrong by 4 orders of magnitude)ρ_G · dG/dt≈ 4206.5 × 0.01 ≈ 42 kcal/day ← non-trivial vs ~2000+ kcal/day totalThe fix changes lean-mass dynamics by ~0.5–2% during the first ~2 weeks of a step diet change, and is negligible thereafter. Existing test tolerances still pass.
References
All four primary papers from the same author group describe the partitioning equation with the same
ρ_G · dG/dtconvention — the units are unambiguous across the literature:– Lancet article page · PubMed · PMC full text · Web Appendix PDF (NIDDK) ← Eq 1, 3, 9 cited above
– PLOS open-access · PMC · PubMed
– Journal
– Journal
Constants used in the code, traceable to the appendix:
src/adult_weight.cpp)roG = 4206.501roF = 9440.727roLweight += 3.7*G