# %% [markdown]
# # Equilibrated citric acid droplets reproduce BAT’s prediction above the measured water activity
#
# Marsh et al. inferred water activity and solute mass fraction for aqueous
# citric acid droplets at 293.15 K. We compare all 12 points in their
# [Table S9.2](https://doi.org/10.5194/acp-17-5583-2017-supplement) with the
# parameters used for the citric-acid BAT curve in [Gorkowski et al.,
# Figure 2b](https://doi.org/10.5194/acp-19-13383-2019). The parameters are
# fixed before evaluating the measurements. The final calculation asks whether
# finite-rate exchange approaches the same water content from either direction.

# %%
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style as mplstyle
from particula import Aerosol, plotting
from particula.dynamics.condensation.condensation_strategies import (
    CondensationIsothermal,
)
from particula.dynamics.condensation.species_mapping import (
    CondensationSpeciesMap,
)
from particula.execution.condensation_ode import CondensationODE
from particula.execution.cpu_restart import (
    checkpoint_condensation,
    resume_condensation,
)
from particula.execution.recording import Recorder, run_until
from particula.gas import EnvironmentData, GasData
from particula.gas.vapor_pressure_strategies import (
    ConstantVaporPressureStrategy,
)
from particula.particles import ActivityBAT, ParticleData
from particula.particles.surface_strategies import SurfaceStrategyVolume
from particula.util.constants import GAS_CONSTANT
from scipy.integrate import solve_ivp
from scipy.optimize import brentq

DATA = Path("docs/data/thermodynamics/marsh2017-citric-acid.csv")
M_WATER = 0.01801528
M_ORGANIC = M_WATER / 0.0938
DENSITY = 1469.1849580794367


# %% [markdown]
# ## Evaluate particle activity at measured composition
#
# Use the authors' mass ratio and O:C (1.16667). The common mass scale
# cancels when BAT forms a mole fraction. Evaluate both edges of the
# composition error bar and compare that prediction interval with the
# asymmetric experimental water-activity interval. The observed mass-fraction
# range lies on the monotonic homogeneous branch for this compound.


# %%
def water_activity(fraction, density=DENSITY):
    """Evaluate particle water activity at citric acid mass fraction."""
    mass = np.stack((1 - fraction, fraction), axis=-1)
    return ActivityBAT(M_ORGANIC, 1.16667, density).activity(mass)[..., 0]


def read_points():
    """Locate the CSV from a checkout or extracted example bundle."""
    starts = [Path.cwd()]
    if "__file__" in globals():
        starts.append(Path(__file__).resolve().parent)
    for start in starts:
        for parent in (start, *start.parents):
            candidate = parent / DATA
            if candidate.is_file():
                return np.loadtxt(candidate, delimiter=",", skiprows=1).T
    raise FileNotFoundError(f"Extract the example bundle containing {DATA}")


# %% [markdown]
# ## Relax a droplet in a fixed water-vapor reservoir
#
# Use isothermal continuum diffusion, with water mass rate
# $4\pi r D\rho_{v,sat}(a_{w,ambient}-a_{w,particle})$. The saturation
# vapor density is 0.0173 kg/m³ at 293.15 K and D is 2.5 × 10⁻⁵ m²/s.
# Dry radius is 5 µm. These choices set the time scale of this diagnostic.
# The flat-interface pressure, prescribed reservoir and nonvolatile solute
# isolate the long-time composition. The measurement supplies no relaxation
# time for this test. Integrating the log water-to-solute mass ratio preserves
# positive masses. Each reservoir receives the opposite of the droplet's
# water-mass change.


# %%
def relax(target, duration, tolerance):
    """Integrate 12 uncoupled droplets from each of two water loadings."""
    dry_mass = 4 * np.pi / 3 * (5e-6) ** 3 * DENSITY
    ambient = np.tile(target, 2)
    initial = np.repeat([0.05, 80.0], len(target))

    def rate(_, log_ratio):
        ratio = np.exp(log_ratio)
        radius = np.cbrt(
            3 * dry_mass * (1 / DENSITY + ratio / 998.2) / (4 * np.pi)
        )
        delta = ambient - water_activity(1 / (1 + ratio))
        return 4 * np.pi * radius * 2.5e-5 * 0.0173 * delta / (dry_mass * ratio)

    sol = solve_ivp(
        rate,
        (0, duration),
        np.log(initial),
        method="BDF",
        rtol=tolerance,
        atol=tolerance * 0.01,
    )
    if not sol.success:
        raise RuntimeError(sol.message)
    final = np.exp(sol.y[:, -1])
    return (1 / (1 + final)).reshape(2, -1), (
        dry_mass * (initial - final)
    ).reshape(2, -1)


# %% [markdown]
# ## Check the native condensation and restart workflow
#
# At the measured activity nearest 0.75, integrate the two initial droplets
# with Particula's isothermal transport and ROS3 solver. The water reservoir
# is held fixed, and the Kelvin correction uses a constant 0.072 N/m surface
# tension and 998.2 kg/m³ surface density. Compare against an independent root
# with that same curvature correction. Record accepted states and continue
# from a checkpoint at 10 s to test reproduction of the 2000 s endpoint.
# The experimental comparison above uses bulk activity.


# %%
def native_check(target):
    """Check a finite-radius native trajectory and its resumed endpoint."""
    temperature = 293.15
    dry_mass = 4 * np.pi / 3 * (5e-6) ** 3 * DENSITY
    initial = Aerosol(
        particles=ParticleData(
            masses=dry_mass * np.array([[[0.05, 1.0], [80.0, 1.0]]]),
            concentration=np.ones((1, 2)),
            charge=np.zeros((1, 2)),
            density=np.array([998.2, DENSITY]),
            volume=np.ones(1),
            distribution_type="particle_resolved",
        ),
        gas=GasData(
            ["water"],
            np.array([M_WATER]),
            np.array([[target * 0.0173]]),
            np.array([True]),
        ),
        environment=EnvironmentData([temperature], [101325.0], [[target]]),
    )
    strategy = CondensationIsothermal(
        molar_mass=np.array([M_WATER]),
        diffusion_coefficient=2.5e-5,
        accommodation_coefficient=1.0,
        update_gases=False,
        activity_strategy=ActivityBAT(M_ORGANIC, 1.16667, DENSITY),
        surface_strategy=SurfaceStrategyVolume(
            density=998.2, surface_tension=0.072
        ),
        vapor_pressure_strategy=ConstantVaporPressureStrategy(
            0.0173 * GAS_CONSTANT * temperature / M_WATER
        ),
        species_map=CondensationSpeciesMap(("water",), ((0, 0),)),
    )
    owner = CondensationODE(
        initial,
        strategy,
        method="ros3",
        rtol=1e-5,
        particle_atol=1e-20,
        first_step=0.01,
    )
    history = Recorder()
    run_until(owner, 10.0, output_times=[0.0, 1.0, 10.0], recorder=history)
    resumed = resume_condensation(checkpoint_condensation(owner), strategy)
    run_until(owner, 2000.0)
    run_until(resumed, 2000.0, recorder=history)
    state = resumed.snapshot()
    np.testing.assert_array_equal(
        owner.snapshot().particles.masses, state.particles.masses
    )
    np.testing.assert_array_equal(
        state.particles.masses[..., 1], initial.particles.masses[..., 1]
    )
    np.testing.assert_array_equal(
        state.gas.concentration, initial.gas.concentration
    )
    final = dry_mass / state.particles.masses[0].sum(axis=1)

    def surface_ratio(mfs):
        volume = dry_mass * (1 / DENSITY + (1 / mfs - 1) / 998.2)
        radius = np.cbrt(3 * volume / (4 * np.pi))
        return water_activity(mfs) * np.exp(
            2 * 0.072 * M_WATER / (998.2 * GAS_CONSTANT * temperature * radius)
        )

    root = brentq(lambda f: surface_ratio(f) - target, 1e-6, 1.0)
    np.testing.assert_allclose(final, root, rtol=0, atol=1e-6)
    return {
        "target_activity": float(target),
        "root_mfs": float(root),
        "kinetic_mfs": final.tolist(),
        "observation_times_s": history.arrays()["time"].tolist(),
    }


def simulate():
    """Compute residuals, sensitivity and kinetic convergence."""
    aw, plus, minus, mfs, dm = read_points()
    calc = water_activity(mfs)
    error = calc - aw
    lower, upper = water_activity(mfs + dm), water_activity(mfs - dm)
    covered = (lower <= aw + plus) & (upper >= aw - minus)
    inverse = np.array(
        [
            brentq(lambda f, target=a: water_activity(f) - target, 0, 1)
            for a in aw
        ]
    )
    coarse, _ = relax(aw, 2000.0, 1e-7)
    fine, reservoir = relax(aw, 4000.0, 1e-9)
    residual = np.max(np.abs(water_activity(fine) - aw))
    if (
        max(
            residual,
            np.max(np.abs(water_activity(coarse) - water_activity(fine))),
        )
        > 1e-4
    ):
        raise ArithmeticError("Kinetic endpoints exceed the activity tolerance")
    return dict(
        native_restart=native_check(aw[np.argmin(np.abs(aw - 0.75))]),
        activity=aw.tolist(),
        plus=plus.tolist(),
        minus=minus.tolist(),
        mfs=mfs.tolist(),
        dm=dm.tolist(),
        predicted=calc.tolist(),
        bias=float(error.mean()),
        rms=float(np.sqrt(np.mean(error**2))),
        maximum=float(np.max(np.abs(error))),
        covered=int(covered.sum()),
        density_rms=float(
            np.sqrt(np.mean((water_activity(mfs, 1550.0) - aw) ** 2))
        ),
        root_mfs=inverse.tolist(),
        kinetic_mfs=fine.tolist(),
        reservoir_exchange_kg=reservoir.tolist(),
        max_mfs_root_error=float(np.max(np.abs(fine - inverse))),
        max_activity_error=float(residual),
    )


# %% [markdown]
# <a id="interpret-the-residual"></a>
#
# ## Converged water exchange leaves the experimental discrepancy intact
#
# The left panel compares the observed and calculated activities. The right
# panel subtracts the observed value at each composition. Vertical and
# horizontal bars show the experimental activity and composition errors.


# %%
@mplstyle.context("particula.default")
def plot_results(result):
    """Plot published observations and the signed BAT discrepancies."""
    f = np.array(result["mfs"])
    aw = np.array(result["activity"])
    pred = np.array(result["predicted"])
    fig, axes = plotting.subplots(1, 2)
    axes[0].errorbar(
        f,
        aw,
        xerr=result["dm"],
        yerr=[result["minus"], result["plus"]],
        fmt="o",
        capsize=3,
        label="Marsh et al.",
    )
    axes[0].plot(f, pred, "s--", label="BAT")
    axes[0].set_ylabel("Particle water activity")
    plotting.legend(axes[0])
    axes[1].axhline(0, color="0.45", lw=1)
    axes[1].errorbar(
        f,
        pred - aw,
        xerr=result["dm"],
        yerr=[result["plus"], result["minus"]],
        fmt="o",
        capsize=3,
    )
    axes[1].set_ylabel("Predicted − measured activity")
    for ax in axes:
        ax.set_xlabel("Citric acid mass fraction")
    plotting.fit(fig)
    return fig


def run_example():
    """Run the complete comparison and display the publication figure."""
    result = simulate()
    print(
        f"Bias {result['bias']:+.5f}, RMS {result['rms']:.5f}, "
        f"overlapping uncertainty intervals {result['covered']}/12"
    )
    print(
        f"Maximum kinetic endpoint activity residual: {result['max_activity_error']:.2e}"
    )
    plot_results(result)
    plt.show()
    return result


if __name__ == "__main__":
    run_example()

# %% [markdown]
# BAT overpredicts measured water activity most strongly at high solute mass
# fraction. Using the melt-density estimate from the experimental study
# increases RMS error. Comparing additional compounds at matched temperature
# would help distinguish a citric-acid-specific discrepancy from a systematic
# bias in the reduced description of organic composition.
