m7-jax 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
m7_jax/__init__.py ADDED
@@ -0,0 +1,8 @@
1
+ # Copyright 2026 Reflective
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Fortran-validated ECHAM-HAM M7 aerosol microphysics in JAX.
4
+
5
+ Enable jax_enable_x64 to reproduce the float64 Fortran; ``step_all`` also runs
6
+ in float32. Native units are molecules/cm3, particles/cm3, ug/m3 for non-sulfate
7
+ mass, radii cm, temperature K and pressure Pa.
8
+ """
m7_jax/_ad.py ADDED
@@ -0,0 +1,90 @@
1
+ # Copyright 2026 Reflective
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Reverse-mode-safe elementary operations with bit-identical forward values.
4
+
5
+ M7's native expressions are evaluated in every cell and masked with
6
+ ``jnp.where``, and its gates (``cmin_aernl``, ``cminvol``, ...) admit values
7
+ far below one. Two derivative rules then overflow even where the function
8
+ itself is perfectly well behaved:
9
+
10
+ - ``d(x/y)/dy = -x/y**2``: ``y**2`` underflows (to zero in float32 for
11
+ ``|y| < 1e-19``), so a quotient with a small but valid denominator gets an
12
+ infinite partial derivative;
13
+ - ``d(x**(1/3))/dx`` is infinite at ``x = 0``.
14
+
15
+ A masked branch's zero cotangent times such an infinity is NaN, and an
16
+ unmasked one is infinite, either way poisoning every input gradient. The
17
+ helpers below return exactly the same forward value as the plain operation
18
+ (bit for bit) and differentiate it without the overflowing intermediate.
19
+ """
20
+ import jax
21
+ import jax.numpy as jnp
22
+
23
+
24
+ @jax.custom_jvp
25
+ def div(x, y):
26
+ """``x / y``, differentiated as ``(dx - (x/y)*dy)/y`` (no ``y**2``)."""
27
+ return x / y
28
+
29
+
30
+ @div.defjvp
31
+ def _div_jvp(primals, tangents):
32
+ x, y = primals
33
+ dx, dy = tangents
34
+ quotient = x / y
35
+ return quotient, jnp.broadcast_to((dx - quotient * dy) / y, quotient.shape)
36
+
37
+
38
+ def cbrt_volume(x):
39
+ """``x**(1/3)`` for a non-negative volume, with a zero derivative at 0.
40
+
41
+ The value is ``x**(1/3)`` bit for bit: 0 at 0, NaN for a negative or NaN
42
+ argument. Only the masked zero's derivative changes, from infinity to 0.
43
+ """
44
+ positive = x > 0
45
+ root = jnp.where(positive, x, 1.0) ** (1 / 3)
46
+ return jnp.where(positive, root, jnp.where(x == 0, jnp.zeros_like(root), jnp.nan))
47
+
48
+
49
+ def sqrt(x):
50
+ """``sqrt(x)`` with a finite reverse-mode derivative at ``x = 0``.
51
+
52
+ The forward value is ``jnp.sqrt(x)`` bit for bit: zero (of either sign)
53
+ returns itself, and every other value, NaN and negatives included, goes
54
+ through ``jnp.sqrt`` unchanged. The derivative ``1/(2 sqrt(x))`` is
55
+ infinite at 0. Masked branches meet exactly 0, for example the analytical
56
+ decay's nucleating radicals without a source, or the coagulation jump
57
+ distance of two empty modes, and their zero cotangent times that infinity
58
+ is NaN. The double ``where`` gives the zero a zero derivative instead.
59
+ """
60
+ zero = x == 0
61
+ return jnp.where(zero, x, jnp.sqrt(jnp.where(zero, 1.0, x)))
62
+
63
+
64
+ def with_gradient_of(value_fn, gradient_fn):
65
+ """``value_fn``'s value, differentiated as ``gradient_fn``.
66
+
67
+ For two forms of the same function in real arithmetic -- a native
68
+ expression whose partial derivatives are huge and cancelling (and
69
+ overflow in float32), and an algebraically identical form whose are not
70
+ -- this returns ``value_fn``'s value bit for bit and ``gradient_fn``'s
71
+ derivative, which is the derivative of the same function.
72
+ """
73
+ @jax.custom_jvp
74
+ def f(*args):
75
+ return value_fn(*args)
76
+
77
+ @f.defjvp
78
+ def _jvp(primals, tangents):
79
+ return value_fn(*primals), jax.jvp(gradient_fn, primals, tangents)[1]
80
+
81
+ return f
82
+
83
+
84
+ def frozen(x, dtype=None):
85
+ """A lookup-table constant: ``x`` as an array of ``dtype`` with no gradient.
86
+
87
+ Tables are data, never differentiated; detaching them up front keeps
88
+ reverse mode from building scatter-add cotangents for their gathers.
89
+ """
90
+ return jax.lax.stop_gradient(jnp.asarray(x, dtype=dtype))