Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

C++ implementation of RTE+RRTMGP (Radiative Transfer for Energetics + Rapid Radiative Transfer Model for GCM applications Parallel), including a CUDA Monte Carlo ray tracer for 3D radiative transfer. Original Fortran version lives in the `rte-rrtmgp/` submodule.

## Build Commands

```bash
# CPU-only build (from repo root)
mkdir build && cd build
cmake -DSYST=macbook_brew ..
make

# CUDA build
cmake -DSYST=<config> -DUSECUDA=ON ..

# Single precision build
cmake -DSYST=<config> -DUSESP=ON ..

# Debug build
cmake -DSYST=<config> -DCMAKE_BUILD_TYPE=DEBUG ..
```

Config files are in `config/` (e.g., `macbook_brew`, `macbook_brew_gcc`, `ubuntu_22lts`).

## Running Tests

The build produces `test_rte_rrtmgp` (CPU) and optionally `test_rte_rrtmgp_gpu`, `test_rte_rrtmgp_rt_gpu`, `test_rte_rrtmgp_bw_gpu` (CUDA).

Test cases use Python scripts for setup, execution, and validation:

```bash
# RFMIP test (clear-sky reference case)
cd rfmip && ./make_links.sh && ln -sf ../build/test_rte_rrtmgp .
python3 rfmip_init.py && python3 rfmip_run.py
python3 compare-to-reference.py --ref_dir ../rrtmgp-data/examples/rfmip-clear-sky/reference --tst_dir . --var rld rlu rsd rsu --file 'r??_Efx_RTE-RRTMGP-181204_rad-irf_r1i1p1f1_gn.nc' --failure_threshold=5.8e-2

# All-sky test (cloudy conditions)
cd allsky && ./make_links.sh && ln -sf ../build/test_rte_rrtmgp .
python3 allsky_init.py && python3 allsky_run.py
python3 allsky_check.py --failure_threshold=5.8e-2
```

Required data files (linked via `make_links.sh`): `coefficients_lw.nc`, `coefficients_sw.nc`, `cloud_coefficients_lw.nc`, `cloud_coefficients_sw.nc`, and `rte_rrtmgp_input.nc`.

## Dependencies

- C++ and Fortran compilers (Fortran needed for `src_kernels/`)
- NetCDF (with HDF5/SZIP)
- Boost
- CUDA toolkit + cuRAND (optional, for GPU builds)
- Python 3 with numpy, netcdf4, xarray (for test scripts)
- Git submodules: `rte-rrtmgp/` (Fortran reference) and `rrtmgp-data/` (coefficient data)

## Architecture

### Dual CPU/GPU implementation pattern

Core physics classes exist in both CPU (`src/`, `include/`) and CUDA (`src_cuda/`, `include/`) variants. The CUDA ray tracer lives in `src_cuda_rt/` and `include_rt/`. Each CPU class has a GPU mirror with device-side memory management.

### Key classes

- **`Array<T,N>`** (`include/array.h`): Template N-dimensional array, used throughout for all data storage
- **`Gas_optics_rrtmgp`**: Computes gas optical properties from k-distributions (largest source file)
- **`Optical_props`** / `Optical_props_1scl` / `Optical_props_2str`: Optical properties with 1-scalar or 2-stream representations
- **`Rte_lw`** / **`Rte_sw`**: Longwave and shortwave RTE solvers
- **`Cloud_optics`** / **`Aerosol_optics`**: Cloud and aerosol optical property parameterizations
- **`Source_functions`**: Planck source function calculations
- **`Gas_concs`**: Gas concentration container (maps gas names to arrays)
- **`Radiation_solver`** (`src_test/`): Top-level solver that orchestrates the full radiation calculation

### Precision and type abstraction

`include/types.h` defines `Float` (double or float via `-DRTE_USE_SP`), `Bool` (int or signed char via `-DRTE_USE_CBOOL`), and `Int` (unsigned long long). All physics code uses these type aliases.

### Fortran kernel bridge

`src_kernels/` contains Fortran kernels from the original RTE-RRTMGP, compiled and linked into the C++ library. Headers in `include/` declare the C-compatible interfaces (e.g., `rrtmgp_kernels.h`).

### Ray tracer (CUDA only)

Monte Carlo ray tracer in `src_cuda_rt/` and `include_rt/` supports forward (`Raytracer`), longwave (`Raytracer_lw`), and backward/adjoint (`Raytracer_bw`) modes. Uses cuRAND for random number generation. Helper functions for scattering (Rayleigh, Henyey-Greenstein) are in `raytracer_functions.h`.

### Test executables

`src_test/` contains the test driver code. `radiation_solver.cpp` (CPU) and `radiation_solver.cu` (GPU) implement the full solver pipeline used by all test cases. The different `.cu` test files correspond to different GPU executables (two-stream, ray tracer, backward).

## Licensing

Library code (`src/`, `include/`): BSD 3-clause. Test code (`src_test/`, `include_test/`): GPLv3.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ else()
endif()

# Start the project only after the system specific settings are loaded.
# If CUDA is enabled, don't compile the Fortran kernels as they are not needed.
if(USECUDA)
project(rte-rrtmgp-cpp C CXX Fortran CUDA)
else()
Expand Down Expand Up @@ -106,12 +107,13 @@ if(USECUDA)
endif()
endif()

add_subdirectory(src_kernels)
add_subdirectory(src)
if(USECUDA)
add_subdirectory(src_kernels_cuda)
add_subdirectory(src_cuda)
add_subdirectory(src_kernels_cuda_rt)
add_subdirectory(src_cuda_rt)
endif()

add_subdirectory(src)
add_subdirectory(src_kernels)
add_subdirectory(src_test)
6 changes: 3 additions & 3 deletions include/Aerosol_optics.h → include/aerosol_optics.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#ifndef AEROSOL_OPTICS_H
#define AEROSOL_OPTICS_H
#include "Array.h"
#include "Optical_props.h"
#include "Gas_concs.h"
#include "array.h"
#include "optical_props.h"
#include "gas_concs.h"
#include "types.h"


Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions include/Cloud_optics.h → include/cloud_optics.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#ifndef CLOUD_OPTICS_H
#define CLOUD_OPTICS_H

#include "Array.h"
#include "Optical_props.h"
#include "array.h"
#include "optical_props.h"
#include "types.h"


Expand Down
2 changes: 1 addition & 1 deletion include/Fluxes.h → include/fluxes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <memory>
#include <stdexcept>

#include "Array.h"
#include "array.h"
#include "types.h"

// Forward declarations.
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions include/Gas_optics.h → include/gas_optics.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include <string>

#include "Array.h"
#include "Optical_props.h"
#include "array.h"
#include "optical_props.h"

// Forward declarations.
class Gas_concs_gpu;
Expand Down
4 changes: 2 additions & 2 deletions include/Gas_optics_rrtmgp.h → include/gas_optics_rrtmgp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include <string>

#include "Array.h"
#include "Gas_optics.h"
#include "array.h"
#include "gas_optics.h"
#include "types.h"


Expand Down
2 changes: 1 addition & 1 deletion include/Optical_props.h → include/optical_props.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define OPTICAL_PROPS_H

#include <memory>
#include "Array.h"
#include "array.h"
#include "types.h"


Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion include/Source_functions.h → include/source_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef SOURCE_FUNCTIONS_H
#define SOURCE_FUNCTIONS_H

#include "Optical_props.h"
#include "optical_props.h"


template<typename, int> class Array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#ifndef AEROSOL_OPTICS_RT_H
#define AEROSOL_OPTICS_RT_H

#include "Array.h"
#include "Optical_props_rt.h"
#include "Gas_concs.h"
#include "array.h"
#include "optical_props_rt.h"
#include "gas_concs.h"
#include "types.h"

using Aerosol_concs_gpu = Gas_concs_gpu;
Expand Down
4 changes: 2 additions & 2 deletions include_rt/Cloud_optics_rt.h → include_rt/cloud_optics_rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#ifndef CLOUD_OPTICS_RT_H
#define CLOUD_OPTICS_RT_H

#include "Array.h"
#include "Optical_props_rt.h"
#include "array.h"
#include "optical_props_rt.h"
#include "types.h"

// Forward declarations.
Expand Down
2 changes: 1 addition & 1 deletion include_rt/Fluxes_rt.h → include_rt/fluxes_rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <stdexcept>

#include "types.h"
#include "Array.h"
#include "array.h"

// Forward declarations.
class Optical_props_arry_rt;
Expand Down
2 changes: 1 addition & 1 deletion include_rt/Gas_concs_rt.h → include_rt/gas_concs_rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <map>
#include <string>

#include "Gas_concs.h"
#include "gas_concs.h"
#include "types.h"

template<typename, int> class Array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include <string>

#include "Array.h"
#include "Gas_optics_rt.h"
#include "array.h"
#include "gas_optics_rt.h"
#include "types.h"

// Forward declarations.
Expand Down
4 changes: 2 additions & 2 deletions include_rt/Gas_optics_rt.h → include_rt/gas_optics_rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include <string>

#include "Array.h"
#include "Optical_props_rt.h"
#include "array.h"
#include "optical_props_rt.h"

// Forward declarations.
class Gas_concs_gpu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


#include <memory>
#include "Array.h"
#include "array.h"
#include "types.h"

// GPU version of optical props class
Expand Down
2 changes: 1 addition & 1 deletion include_rt/Raytracer.h → include_rt/raytracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

#include "types.h"
#include "Optical_props_rt.h"
#include "optical_props_rt.h"
#include "raytracer_definitions.h"

// Forward declarations.
Expand Down
2 changes: 1 addition & 1 deletion include_rt/Raytracer_bw.h → include_rt/raytracer_bw.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "types.h"
#include <curand_kernel.h>
#include "raytracer_kernels_bw.h"
#include "Optical_props_rt.h"
#include "optical_props_rt.h"

// Forward declarations.
template<typename, int> class Array_gpu;
Expand Down
2 changes: 1 addition & 1 deletion include_rt/Raytracer_lw.h → include_rt/raytracer_lw.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

#include "types.h"
#include "Optical_props_rt.h"
#include "optical_props_rt.h"
#include "raytracer_definitions.h"

// Forward declarations.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#ifndef SOURCE_FUNCTIONS_RT_H
#define SOURCE_FUNCTIONS_RT_H
#include "Optical_props_rt.h"
#include "optical_props_rt.h"

template<typename, int> class Array_gpu;

Expand Down
2 changes: 1 addition & 1 deletion include_rt_kernels/gpt_combine_kernels_cuda_rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define GPT_COMBINE_KERNELS_CUDA_RT_H

#include "types.h"
// #include "Gas_concs.h"
// #include "gas_concs.h"


namespace Gpt_combine_kernels_cuda_rt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <numeric>
#include <netcdf.h>

#include "Status.h"
#include "status.h"

enum class Netcdf_mode { Create, Read, Write };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#ifndef RADIATION_SOLVER_H
#define RADIATION_SOLVER_H

#include "Array.h"
#include "Gas_concs.h"
#include "Gas_optics_rrtmgp.h"
#include "Cloud_optics.h"
#include "Aerosol_optics.h"
#include "Rte_lw.h"
#include "Rte_sw.h"
#include "Source_functions.h"
#include "array.h"
#include "gas_concs.h"
#include "gas_optics_rrtmgp.h"
#include "cloud_optics.h"
#include "aerosol_optics.h"
#include "rte_lw.h"
#include "rte_sw.h"
#include "source_functions.h"


class Radiation_solver_longwave
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
#ifndef RADIATION_SOLVER_BW_H
#define RADIATION_SOLVER_BW_H

#include "Array.h"
#include "Gas_concs.h"
#include "Gas_optics_rrtmgp_rt.h"
#include "Cloud_optics_rt.h"
#include "Aerosol_optics_rt.h"
#include "Rte_lw_rt.h"
#include "Rte_sw_rt.h"
#include "Raytracer_bw.h"
#include "array.h"
#include "gas_concs.h"
#include "gas_optics_rrtmgp_rt.h"
#include "cloud_optics_rt.h"
#include "aerosol_optics_rt.h"
#include "rte_lw_rt.h"
#include "rte_sw_rt.h"
#include "raytracer_bw.h"
#include "raytracer_kernels_bw.h"
#include "Source_functions_rt.h"
#include "source_functions_rt.h"
#include <curand_kernel.h>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
#ifndef RADIATION_SOLVER_RT_H
#define RADIATION_SOLVER_RT_H

#include "Array.h"
#include "Gas_concs.h"
#include "Gas_optics_rrtmgp_rt.h"
#include "Cloud_optics_rt.h"
#include "Aerosol_optics_rt.h"
#include "Rte_lw_rt.h"
#include "Rte_sw_rt.h"
#include "Raytracer.h"
#include "Raytracer_lw.h"
#include "array.h"
#include "gas_concs.h"
#include "gas_optics_rrtmgp_rt.h"
#include "cloud_optics_rt.h"
#include "aerosol_optics_rt.h"
#include "rte_lw_rt.h"
#include "rte_sw_rt.h"
#include "raytracer.h"
#include "raytracer_lw.h"
#include "raytracer_kernels.h"
#include "Source_functions_rt.h"
#include "source_functions_rt.h"
#include <curand_kernel.h>


Expand Down
File renamed without changes.
Loading