neuralgcm/neuralgcm

Hybrid ML + physics model of the Earth's atmosphere

966 stars Python 9 components

13 hidden assumptions · 7-stage pipeline · 9 components

Like any codebase, this scientific computing makes assumptions it never checks — most are routine. The ones worth your attention are below.

Hybrid ML/physics model that simulates Earth's atmosphere by combining neural networks with traditional physics equations

Atmospheric data enters as observational records (temperature, wind, pressure on lat/lon grids), gets transformed to spectral harmonic coefficients for efficient physics calculations, passes through both traditional equation solvers and neural network parameterizations that compute tendency updates, then integrates forward in time before being converted back to observational variables for comparison with targets. The core loop alternates between physics-based tendencies (computed in spectral space) and ML-based corrections (learned from data residuals).

Under the hood, the system uses 3 feedback loops, 3 data pools, 4 control points to manage its runtime behavior.

A 9-component scientific computing. 178 files analyzed. Data flows through 7 distinct pipeline stages.

Hidden Assumptions

Most of what this code assumes is routine. These 3 are the ones most likely to cause trouble here. The rest are minor; they're under "Show everything".

Worth your attention first

If pressure levels are not monotonic (e.g., due to numerical errors or incorrect data), jnp.searchsorted produces wrong indices leading to interpolation to wrong pressure levels, causing physically impossible temperature/wind profiles

Worth your attention first

If training data uses different variable names (e.g., 'u_wind' vs 'u_component_of_wind'), the operator silently produces wrong observation mappings, causing the model to train on incorrect variable associations

Worth your attention first

If the model is configured with a different number of vertical levels or pressure units differ from the hardcoded reference, array indexing fails or physical calculations use wrong pressure values, corrupting thermodynamic computations

Show everything (10 more)
Ordering

The neural parameterization expects spherical harmonic coefficients to be ordered consistently with the ylm_map coordinate system, particularly that wavenumber indices match between input data and the mapping

If this fails: If input coefficients have different wavenumber ordering than expected by ylm_map, the neural network processes wrong spectral components, leading to physically inconsistent tendency predictions that violate conservation laws

neuralgcm/experimental/atmosphere/parameterizations.py:ModalNeuralDivCurlParameterization
Contract

FixerModule protocol assumes that diagnostic fields and target fields have identical coordinate systems and that the diagnostic calculation succeeded without NaN/inf values

If this fails: If diagnostics contain NaN/inf due to numerical instability or coordinate mismatches, the fixer propagates these bad values into tendency corrections, causing the entire simulation to diverge

neuralgcm/experimental/atmosphere/fixers.py:FixerModule
Temporal

Energy balance adjustments assume that energy imbalances represent steady-state errors rather than transient physical processes, and that redistributing energy preserves the model's temporal stability

If this fails: If energy imbalances arise from legitimate transient processes (e.g., during rapid weather transitions), forcing balance creates artificial energy redistributions that suppress realistic weather variability

neuralgcm/experimental/atmosphere/diagnostics.py:EnergyBalanceModule
Resource

Linear interpolation with extrapolation assumes that arrays fit in memory and that unlimited extrapolation won't produce values outside the valid physical range for atmospheric variables

If this fails: Extrapolation can produce unphysical values like negative absolute temperatures or supersonic wind speeds that cause downstream numerical instability in physics computations

neuralgcm/experimental/atmosphere/interpolators.py:_linear_interp_with_linear_extrap
Domain

The regridding function assumes input datasets have coordinate dimensions named exactly 'longitude' and 'latitude', and that coordinate values are in the same units as expected by the regridder

If this fails: If input data uses different coordinate names or units (degrees vs radians), xarray.apply_ufunc fails to find the core dimensions or regridding operates on wrong coordinate values, producing geographically misplaced data

neuralgcm/demo.py:_horizontal_regrid
Environment

Demo functions assume that importlib.resources and pickle can successfully load packaged model data, and that the dinosaur library's coordinate systems are compatible with the loaded model configuration

If this fails: If package installation is incomplete or dinosaur version mismatches, demo fails with unclear import errors, making it impossible for users to verify basic functionality

neuralgcm/demo.py
Scale

Pressure interpolation assumes that the pressure range in input data covers the target pressure levels, but linear extrapolation is used without bounds checking for extreme values

If this fails: Extrapolating to pressure levels far outside the data range (e.g., stratosphere when data only covers troposphere) produces unphysical atmospheric profiles that violate hydrostatic balance

neuralgcm/experimental/atmosphere/interpolators.py:LinearOnPressure
Contract

The primitive equations module assumes that input atmospheric state fields have consistent physical units and that the coordinate system metadata correctly describes the spatial grid

If this fails: Unit inconsistencies or wrong coordinate metadata cause physics calculations to use incorrect scaling factors, leading to wrong magnitude tendencies that can destabilize the simulation

neuralgcm/experimental/atmosphere/equations.py:PrimitiveEquationsModule
Ordering

The observation operator assumes that vertical levels in the atmospheric state are ordered consistently with the target observation data (either surface-to-top or top-to-surface)

If this fails: If vertical ordering differs between model and observations, computed observation variables have flipped vertical profiles, causing training to learn inverted atmospheric relationships

neuralgcm/experimental/atmosphere/observation_operators.py:StandardVariablesObservationOperator
Domain

Atmospheric diagnostics assume that all required state variables are present and physically meaningful, with no validation that computed diagnostics fall within realistic ranges for Earth's atmosphere

If this fails: Diagnostic calculations proceed even with unphysical input states, producing misleading diagnostic values that hide model problems and make it difficult to detect when the simulation has gone off track

neuralgcm/experimental/atmosphere/diagnostics.py

Open the standalone hidden-assumptions report for neuralgcm →

How Data Flows Through the System

Atmospheric data enters as observational records (temperature, wind, pressure on lat/lon grids), gets transformed to spectral harmonic coefficients for efficient physics calculations, passes through both traditional equation solvers and neural network parameterizations that compute tendency updates, then integrates forward in time before being converted back to observational variables for comparison with targets. The core loop alternates between physics-based tendencies (computed in spectral space) and ML-based corrections (learned from data residuals).

  1. Initialize atmospheric state — Functions in idealized_states.py convert initial conditions or observations into the model's internal atmospheric state representation using coordinate system transformations and field mapping [ObservedVariables → AtmosphericState]
  2. Transform to spectral space — FixedYlmMapping.nodal_to_modal converts physical grid fields to spherical harmonic coefficients, enabling efficient computation of derivatives and global operations required by physics equations [AtmosphericState → SphericalHarmonicCoefficients]
  3. Compute physics tendencies — PrimitiveEquationsModule calculates time derivatives using traditional atmospheric physics equations - momentum advection, pressure gradients, Coriolis forces, and thermodynamic processes [SphericalHarmonicCoefficients → Tendencies]
  4. Apply neural parameterizations — ModalNeuralDivCurlParameterization and similar neural modules predict sub-grid scale processes by learning from data residuals, adding ML-computed tendencies to physics-based ones [SphericalHarmonicCoefficients → Tendencies]
  5. Enforce conservation constraints — EnergyFixerModule adjusts total tendencies to maintain physical conservation laws like energy and mass balance, redistributing any imbalances according to physical constraints [Tendencies → Tendencies]
  6. Integrate forward in time — ImplicitExplicitTimeIntegrator advances the atmospheric state using the combined tendencies, handling fast gravity waves implicitly and advection explicitly for numerical stability [AtmosphericState → AtmosphericState]
  7. Convert to observations — StandardVariablesObservationOperator transforms the updated model state back to standard meteorological variables (u,v,t,z) for comparison with targets during training or for output [AtmosphericState → ObservedVariables] (config: u_name, v_name, vector_name)

Data Models

The data structures that flow between stages — the contracts that hold the system together.

AtmosphericState neuralgcm/experimental/atmosphere/equations.py
dict[str, cx.Field] with keys like 'temperature', 'u_component_of_wind', 'v_component_of_wind', 'surface_pressure' where cx.Field contains arrays with coordinate system metadata
Created from initial conditions or observations, transformed between coordinate systems, updated by physics and ML components, and converted to observational variables
SphericalHarmonicCoefficients neuralgcm/experimental/core/spherical_harmonics.py
complex arrays with dimensions (wavenumber_m, wavenumber_l, vertical_levels) representing atmospheric fields in spectral space
Generated by transforming gridded data to spectral space, operated on by physics equations and neural networks, then transformed back to physical grid
CoordinateGrid neuralgcm/experimental/core/coordinates.py
cx.Coordinate objects defining spatial grids like LonLatGrid.T42() with longitude/latitude arrays or SigmaLevels with vertical level definitions
Defined at model initialization, used throughout simulation to define where data lives spatially, enables transformations between different grid resolutions
Tendencies neuralgcm/experimental/atmosphere/equations.py
dict[str, cx.Field] with same structure as AtmosphericState but containing time derivatives (rates of change) for each variable
Computed by physics equations and neural network parameterizations, accumulated from multiple sources, fed into time integrator to update atmospheric state
TrainingBatch neuralgcm/experimental/xreader/iterators.py
PyTree with 'block' containing atmospheric data arrays and 'sub_slices' list defining temporal/spatial windows for training sequences
Loaded from zarr datasets, chunked into training sequences, fed through model for prediction and loss computation
DiagnosticMetrics neuralgcm/experimental/training/trainer.py
dataclass with train: dict[str, float], eval: dict[str, float], eval_ema: dict[str, float] containing loss and accuracy metrics
Computed during training steps, accumulated over batches, logged for monitoring training progress and model performance

System Behavior

How the system operates at runtime — where data accumulates, what loops, what waits, and what controls what.

Data Pools

Atmospheric State Memory (state-store)
Maintains current atmospheric state (temperature, winds, pressure) across integration steps, preserving coordinate system metadata and field relationships
Spectral Coefficient Cache (buffer)
Temporarily stores spherical harmonic coefficients during physics computations, enabling efficient derivative calculations and spectral filtering
Training Data Archive (file-store)
Zarr-based storage of atmospheric training data chunked for efficient streaming during model training and evaluation

Feedback Loops

Delays

Control Points

Technology Stack

JAX (compute)
Provides automatic differentiation and JIT compilation for both physics calculations and neural network training in the atmospheric model
Flax (framework)
Builds neural network components that parameterize sub-grid atmospheric processes, integrating with JAX for gradient-based training
coordax (library)
Manages coordinate system abstractions and field transformations, enabling seamless operations between different spatial representations
dinosaur (library)
Provides core atmospheric physics equations and primitive equation solvers that form the physics backbone of the hybrid model
xarray (library)
Handles labeled multi-dimensional atmospheric datasets with metadata preservation for input/output operations
zarr (serialization)
Stores compressed atmospheric training data in chunked format for efficient streaming during model training
optax (library)
Implements gradient-based optimizers for training the neural network components of the atmospheric model
gin-config (library)
Manages configuration and hyperparameter injection for experiments and model architecture choices

Key Components

Explore the interactive analysis

See the full architecture map, data flow, and code patterns visualization.

Analyze on CodeSea

Related Scientific Computing Repositories

Frequently Asked Questions

What is neuralgcm used for?

Hybrid ML/physics model that simulates Earth's atmosphere by combining neural networks with traditional physics equations neuralgcm/neuralgcm is a 9-component scientific computing written in Python. Data flows through 7 distinct pipeline stages. The codebase contains 178 files.

How is neuralgcm architected?

neuralgcm is organized into 5 architecture layers: Atmospheric Physics, Core Components, Training Infrastructure, Data Pipeline, and 1 more. Data flows through 7 distinct pipeline stages. This layered structure keeps concerns separated and modules independent.

How does data flow through neuralgcm?

Data moves through 7 stages: Initialize atmospheric state → Transform to spectral space → Compute physics tendencies → Apply neural parameterizations → Enforce conservation constraints → .... Atmospheric data enters as observational records (temperature, wind, pressure on lat/lon grids), gets transformed to spectral harmonic coefficients for efficient physics calculations, passes through both traditional equation solvers and neural network parameterizations that compute tendency updates, then integrates forward in time before being converted back to observational variables for comparison with targets. The core loop alternates between physics-based tendencies (computed in spectral space) and ML-based corrections (learned from data residuals). This pipeline design reflects a complex multi-stage processing system.

What technologies does neuralgcm use?

The core stack includes JAX (Provides automatic differentiation and JIT compilation for both physics calculations and neural network training in the atmospheric model), Flax (Builds neural network components that parameterize sub-grid atmospheric processes, integrating with JAX for gradient-based training), coordax (Manages coordinate system abstractions and field transformations, enabling seamless operations between different spatial representations), dinosaur (Provides core atmospheric physics equations and primitive equation solvers that form the physics backbone of the hybrid model), xarray (Handles labeled multi-dimensional atmospheric datasets with metadata preservation for input/output operations), zarr (Stores compressed atmospheric training data in chunked format for efficient streaming during model training), and 2 more. A focused set of dependencies that keeps the build manageable.

What system dynamics does neuralgcm have?

neuralgcm exhibits 3 data pools (Atmospheric State Memory, Spectral Coefficient Cache), 3 feedback loops, 4 control points, 3 delays. The feedback loops handle simulation and training-loop. These runtime behaviors shape how the system responds to load, failures, and configuration changes.

What design patterns does neuralgcm use?

5 design patterns detected: Coordinate System Abstraction, Physics-ML Hybrid Architecture, Spectral Method Implementation, Conservation Law Enforcement, Field-Coordinate Binding.

Analyzed on April 20, 2026 by CodeSea. Written by .