// engine.mjs: the arithmetic behind /strata/keplers-wine-barrel/.
// Everything the page prints about volumes is computed here, in the reader's browser,
// and the same file is what the check imports. No data; only geometry.
//
// The cask lies on its side. L is its length between the heads, D the diameter.
// The bunghole is on top, halfway along. The gauger's rod goes in at the bunghole
// and down to the far bottom edge of one head, so it spans half the length across
// and one whole diameter down:   rod^2 = (L/2)^2 + D^2.

export const cylinder = {
  rod: (L, D) => Math.hypot(L / 2, D),
  volume: (L, D) => (Math.PI / 4) * D * D * L,
};

// Hold the rod at length d and set the shape by k = L/D. Returns L, D and volume.
export function cylinderAtRod(d, k) {
  const D = d / Math.sqrt(1 + (k * k) / 4);
  const L = k * D;
  return { L, D, V: cylinder.volume(L, D) };
}

// The best shape for a given rod: maximise V = (pi/2) a b^2 with a = L/2, b = D, a^2 + b^2 = d^2.
// dV/da = (pi/2)(d^2 - 3a^2) = 0  ->  a = d/sqrt3, b = d*sqrt(2/3), so D = sqrt2 * (L/2), L/D = sqrt2.
export const BEST_K = Math.SQRT2;
export const vmaxCoef = Math.PI / (3 * Math.sqrt(3)); // Vmax = vmaxCoef * d^3

// What fraction of the best volume a shape k holds, at the same rod.
export function fraction(k) {
  return cylinderAtRod(1, k).V / vmaxCoef;
}

// Near the top, fraction ~ exp(-(2/3) u^2) with u = ln(k / sqrt2): the loss is second order.
export const CURVATURE = 2 / 3;
export function fractionQuadratic(k) {
  const u = Math.log(k / BEST_K);
  return Math.exp(-CURVATURE * u * u);
}

// The widest band of shapes whose loss stays under `loss` (0.01 = 1%), by bisection each side.
export function band(loss) {
  const target = 1 - loss;
  const solve = (lo, hi) => { // fraction(lo) < target <= fraction(hi) or reversed
    for (let i = 0; i < 200; i++) {
      const m = (lo + hi) / 2;
      if ((fraction(m) >= target) === (fraction(hi) >= target)) hi = m; else lo = m;
    }
    return (lo + hi) / 2;
  };
  return [solve(0.05, BEST_K), solve(20, BEST_K)];
}

// A rod graduated on the best shape reads vmaxCoef * d^3. Any other cask with the same rod
// holds less, so on a cylinder the reading can only err one way: it overstates.
export function rodReading(d) { return vmaxCoef * d * d * d; }

// Misreading the rod itself is a different kind of error: first order, and tripled.
// A relative error e in d gives (1+e)^3 - 1 in the reading.
export function readingError(e) { return Math.pow(1 + e, 3) - 1; }

// ---- Kepler's own units (Messekunst Archimedis, 1616, ch. 76, pp. 64 to 65) ----------------
// He takes the rod as 20 parts and gives the "Leib" of half a cask as D^2 * h, where h is
// the half-length from bung to head and D the head diameter, so D^2 = 400 - h^2.
export const KROD = 20;
export function leib(h, rod = KROD) { return (rod * rod - h * h) * h; }
export const H_AUSTRIA = KROD / Math.sqrt(3);      // the best half-length: 11.547
export const H_RHINE = KROD / Math.SQRT2;          // staves twice the diameter, so h = D: 14.142

// Every figure he printed in that chapter, with the half-length he used for it.
export const KEPLER_ROWS = [
  { h: 11, printed: 3069, cask: 'Austrian, too short' },
  { h: H_AUSTRIA, printed: 3080, cask: 'Austrian, exactly right' },
  { h: 12, printed: 3072, cask: 'Austrian, too long' },
  { h: 14, printed: 2856, cask: 'Rhenish, too short' },
  { h: H_RHINE, printed: 2828, cask: 'Rhenish, exactly right' },
  { h: 15, printed: 2625, cask: 'Rhenish, too long' },
];

// His way of stating a loss: "the Nth Achtering" is missing, i.e. N = full / |difference|.
export function oneIn(full, v) { return full / Math.abs(full - v); }

// A cooper misses the half-length by `miss` parts. Against the rod made for that cask's own
// shape, how far off is the content? (Positive = the cask holds more than its rod says.)
export function coopersMiss(h0, miss) { return leib(h0 + miss) / leib(h0) - 1; }

// The Linz rod (ch. 93, p. 99): 100 puncten of rod = 1 Achtering, and an Achtering is
// 614110 cubic puncten (ch. 93). So the rod reads content = 0.614110 * d^3; the best plain
// cylinder through the heads holds pi / (3 sqrt 3) * d^3 = 0.6046 d^3.
export const LINZ_ROD_COEF = 614110 / 1e6;
