/** A reduced two-cell finite-volume heat ledger. Not an atmospheric solver.
 * Compile: javac FluxLedger.java ; run: java FluxLedger
 * Source supplied for review; no JDK was available in the website build environment.
 */
public final class FluxLedger {
    public static double[] step(double[] temperature, double[] capacity,
                                double conductance, double dt) {
        if (temperature.length != 2 || capacity.length != 2 ||
            !(capacity[0] > 0) || !(capacity[1] > 0) ||
            !(conductance >= 0) || !(dt >= 0)) {
            throw new IllegalArgumentException("Two positive capacities and nonnegative G/dt required");
        }
        if (conductance > 0 && dt > Math.min(capacity[0], capacity[1]) / conductance) {
            throw new IllegalArgumentException("Explicit convexity bound exceeded");
        }
        double sharedTransfer = dt * conductance * (temperature[0] - temperature[1]);
        return new double[] {temperature[0] - sharedTransfer / capacity[0],
                             temperature[1] + sharedTransfer / capacity[1]};
    }
    public static void main(String[] args) {
        double[] capacity = {2.0, 1.0}, temperature = {300.0, 280.0};
        double initialEnergy = capacity[0] * temperature[0] + capacity[1] * temperature[1];
        temperature = step(temperature, capacity, 0.1, 1.0);
        double finalEnergy = capacity[0] * temperature[0] + capacity[1] * temperature[1];
        if (Math.abs(finalEnergy - initialEnergy) > 1e-10) {
            throw new AssertionError("Shared transfer failed the extensive budget");
        }
        System.out.printf("T1=%.6f K,T2=%.6f K,energy=%.6f J,remainder=%.3e J%n",
                          temperature[0], temperature[1], finalEnergy, finalEnergy-initialEnergy);
    }
}
