Skip to content

Fix unit mismatch in glycogen energy flux term in R() helper#10

Merged
RodrigoZepeda merged 3 commits into
INSP-RH:masterfrom
ChocoTonic:fix/glycogen-energy-flux-units
May 29, 2026
Merged

Fix unit mismatch in glycogen energy flux term in R() helper#10
RodrigoZepeda merged 3 commits into
INSP-RH:masterfrom
ChocoTonic:fix/glycogen-energy-flux-units

Conversation

@ChocoTonic
Copy link
Copy Markdown
Contributor

Bug fix: glycogen energy flux term — units mismatch in Adult::R()

File: src/adult_weight.cpp:374
One-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 by roG restores 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 R3

R3 is a sum that must be dimensionally homogeneous in kcal/day:

Term in code Symbol Units
K K (rmr·PAL − γ_L·L − γ_F·F − δ·BW) kcal/day
delta*weight δ · BW (kcal/kg/day)·kg = kcal/day
TEF(t) TEF = β_TEF · ΔEI kcal/day
AT AT kcal/day
TotalIntake(t) EI kcal/day
Before fix: dG(t, G) dG/dt kg/day ← inconsistent
After fix: roG*dG(t, G) ρ_G · dG/dt kcal/kg · kg/day = kcal/day

dG(t, G) is defined at src/adult_weight.cpp:315-317:

NumericVector Adult::dG(double t, NumericVector G){
    return (CI(t) - kG*pow(G, 2.0))/roG;   // returns kg/day (Eq 1 solved for dG/dt)
}

This already matches the paper's Eq 1 solved for dG/dt: dividing both sides by ρ_G yields dG/dt in kg/day. So every consumer of dG(...) that wants an energy flux must multiply by roG again.

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:

ρ_G · dG/dt = CI − k_G · G² (units: kcal/day on both sides)

So dG/dt = (CI − k_G · G²) / ρ_G, in kg/day — exactly what Adult::dG() returns.

Eq 3 — energy partitioning (lean):

ρ_L · dL/dt = p · (EI − EE − ρ_G · dG/dt)

The glycogen term enters the partitioning equation as ρ_G · dG/dt (kcal/day), not dG/dt alone.

Eq 9 — closed-form EE (which R() implements after substituting Eq 3 into Eq 5):

EE = [K + γ_F·F + γ_L·L + δ·BW + TEF + AT + (EI − ρ_G · dG/dt) · (p·η_L/ρ_L + (1−p)·η_F/ρ_F)] / [1 + p·η_L/ρ_L + (1−p)·η_F/ρ_F]

Again, the glycogen flux appears as ρ_G · dG/dt. Without the roG multiplier, the C++ code was computing EE with a glycogen term roughly 4000× too small.

Numerical impact

For a sedentary adult at energy balance with CI ≈ 1100 kcal/day carbohydrate intake and G ≈ 500 g, the steady-state glycogen flux dG/dt is essentially zero, so the term contributes near nothing. During a transient (first days of a diet change), dG/dt can reach ~0.01 kg/day:

  • Buggy term: dG/dt ≈ 0.01 kcal/day ← negligible (wrong by 4 orders of magnitude)
  • Correct term: ρ_G · dG/dt ≈ 4206.5 × 0.01 ≈ 42 kcal/day ← non-trivial vs ~2000+ kcal/day total

The 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/dt convention — the units are unambiguous across the literature:

  1. Hall KD, Sacks G, Chandramohan D, Chow CC, Wang YC, Gortmaker SL, Swinburn BA. Quantification of the effect of energy imbalance on bodyweight. The Lancet 2011; 378(9793):826–837.
    Lancet article page · PubMed · PMC full text · Web Appendix PDF (NIDDK) ← Eq 1, 3, 9 cited above
  2. Chow CC, Hall KD. The dynamics of human body weight change. PLoS Comput Biol 2008; 4(3):e1000045.
    PLOS open-access · PMC · PubMed
  3. Hall KD. Predicting metabolic adaptation, body weight change, and energy intake in humans. Am J Physiol Endocrinol Metab 2010; 298(3):E449–E466.
    Journal
  4. Hall KD, Jordan PN. Modeling weight-loss maintenance to help prevent body weight regain. Am J Clin Nutr 2008; 88(6):1495–1503.
    Journal

Constants used in the code, traceable to the appendix:

Symbol Paper value Code (src/adult_weight.cpp) Notes
ρ_G 17.6 MJ/kg roG = 4206.501 line 235, kJ→kcal × 0.23900573614
ρ_F 39.5 MJ/kg roF = 9440.727 line 239
ρ_L 7.6 MJ/kg roL initialized similarly
2.7 g water / g glycogen appendix p. 1 weight += 3.7*G line 373, 1 g glycogen + 2.7 g water

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.
@RodrigoZepeda RodrigoZepeda marked this pull request as ready for review May 20, 2026 19:48
@RodrigoZepeda RodrigoZepeda self-requested a review May 20, 2026 19:48
@RodrigoZepeda
Copy link
Copy Markdown
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.

@RodrigoZepeda RodrigoZepeda merged commit 698ff71 into INSP-RH:master May 29, 2026
@RodrigoZepeda
Copy link
Copy Markdown
Contributor

Thank you for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants