# clustermontecarlo/cmc-cosmic architecture

> Architecture diagram of the GitHub repository clustermontecarlo/cmc-cosmic, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/clustermontecarlo/cmc-cosmic
- Repository: https://github.com/clustermontecarlo/cmc-cosmic

## Overview

1. **Project purpose**
   - CMC-COSMIC is a high-performance astrophysical simulation code for long-term evolution of dense star clusters.
   - The main execution model is a **Monte Carlo/Hénon-style cluster dynamics engine** coupled to **stellar/binary evolution** and **few-body encounter handling**.

2. **Primary runtime boundary: the CMC simulation engine**
   - The core application lives under `src/cmc` with headers in `include/cmc`.
   - This layer contains the cluster state evolution, relaxation, orbit averaging, I/O, MPI coordination, binary handling, stellar-evolution hooks, and encounter logic.
   - It is the central subsystem that drives time evolution and coordinates all other components.

3. **Parallel execution and HPC dependencies**
   - The code is designed for HPC clusters and uses **MPI** (`include/cmc/cmc_mpi.h`, `src/cmc/cmc_mpi.c`).
   - Build and runtime dependencies called out in the README are **MPI, HDF5, GSL, and CMake**.
   - The repo is organized for compiled native binaries rather than a service or library-only architecture.

4. **Stellar and binary evolution integration**
   - `src/bse_wrap` and `include/bse_wrap` provide the bridge into the embedded **COSMIC/BSE** stellar evolution code.
   - This is a major boundary: cluster dynamics code calls into the evolution layer to update stellar types, masses, binaries, and compact-object outcomes.
   - The actual COSMIC submodule is vendored in-tree under `src/bse_wrap/bse/COSMIC`.

5. **Few-body encounter subsystem**
   - `src/fewbody-0.24` and `include/fewbody-0.24` are a bundled third-party few-body scattering/interaction package.
   - CMC uses this for close encounters such as binary-single, binary-binary, triple, and hierarchical interactions.
   - This is another clear subsystem boundary: the Monte Carlo cluster model delegates rare but strong interactions to a specialized few-body solver.

6. **Core physics modules inside CMC**
   - The `src/cmc` files are split by physics concern:
     - relaxation and dynamics: `cmc_relaxation.c`, `cmc_dynamics.c`, `cmc_core.c`
     - binaries and interactions: `cmc_binbin.c`, `cmc_binsingle.c`, `cmc_sscollision.c`, `cmc_bhlosscone.c`
     - stellar evolution and removal: `cmc_stellar_evolution.c`, `cmc_remove_star.c`, `cmc_evolution_thr.c`
     - numerics/search/sorting/orbits: `cmc_search_grid.c`, `cmc_sort.c`, `cmc_orbit.c`, `cmc_nr.c`
     - I/O and utilities: `cmc_io.c`, `cmc_utils.c`, `cmc_fits.c`
   - Architecturally, this is a modular C codebase with one central state model and many specialized physics handlers.

7. **Data flow through the system**
   - Input starts from initial-condition generators or parameter files, then enters the CMC engine.
   - The engine evolves cluster state over time, invoking stellar evolution and encounter solvers as needed.
   - Results are written to multiple output streams and formats, including HDF5 snapshots and text logs/dat files.
   - Example outputs in `docs/sphinx/source/example_output` show the expected artifacts: dynamical logs, binary interaction logs, BH catalogs, lagrangian radii, and snapshot files.

8. **Initial-condition and preprocessing utilities**
   - `src/utils` contains standalone command-line generators and transformers:
     - `mkking`, `mkplummer`, `mkelson` for cluster models
     - `setimf`, `setstellar`, `setunits`, `addbinaries`
     - FITS/ASCII conversion utilities under `ascii2fits`
   - These are upstream tools in the workflow: they prepare cluster models before the main CMC run.

9. **Analysis and Python tooling**
   - `tools/cmc_parser.py` and `tools/cmctoolkit.py` provide post-processing and analysis support in Python.
   - These sit downstream of the simulation outputs and are likely used for parsing logs, organizing data, or producing derived measurements.
   - The repo therefore includes both the compiled simulation core and a lightweight analysis layer.

10. **GPU/CUDA code path**
   - The `cuda` directory is a separate CUDA implementation/support area with its own build files and common helper library.
   - It appears to be an auxiliary or experimental acceleration path rather than the main code path described in the README.
   - For architecture diagrams, it should be shown as an optional GPU subsystem adjacent to the core C implementation, not as the primary execution route.

11. **Build and packaging structure**
   - The project uses **CMake** as the main build system, with additional CUDA makefiles and a top-level `Dockerfile`.
   - CI is handled via GitHub Actions workflows for packaging, registry publishing, and GitHub Pages docs deployment.
   - This indicates a reproducible HPC-oriented build pipeline, with container support for deployment and testing.

12. **Documentation and examples as part of the repo workflow**
   - `docs/sphinx` and `docs/doxygen` contain the user-facing documentation sources.
   - `examples` and `ci` contain runnable configuration files, input data, and test automation.
   - These are not runtime subsystems, but they shape the operational flow: example input → simulation → outputs → documentation/analysis.

Main entry points: [`src/cmc/cmc_core.c`](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_core.c) (Core state), [`src/cmc/cmc.c`](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc.c) (CMC runtime), [`src/cmc/cmc_io.c`](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_io.c) (I/O layer), [`CMakeLists.txt`](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/CMakeLists.txt) (Build system)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_core["CMC engine"]
  node_n_cmc_main["CMC runtime<br/>simulation engine<br/>[cmc.c]"]
  node_n_cmc_core["Core state<br/>cluster state<br/>[cmc_core.c]"]
  node_n_cmc_dynamics["Dynamics<br/>relaxation/orbits<br/>[cmc_dynamics.c]"]
  node_n_cmc_binary["Binary physics<br/>encounter handlers<br/>[cmc_binbin.c]"]
  node_n_cmc_evolution["Stellar evolution<br/>evolution hooks"]
  node_n_cmc_io["I/O layer<br/>outputs/checkpoints<br/>[cmc_io.c]"]
  node_n_cmc_mpi["MPI layer<br/>parallel runtime<br/>[cmc_mpi.c]"]
end

subgraph group_grp_embedded["Embedded solvers"]
  node_n_bse_wrap["BSE bridge<br/>stellar-evolution wrapper<br/>[bse_wrap.c]"]
  node_n_cosmic["COSMIC<br/>embedded stellar evolution"]
  node_n_fewbody["Few-body solver<br/>interaction solver<br/>[fewbody.c]"]
end

subgraph group_grp_tools["Workflow tools"]
  node_n_utils_ic["IC generators<br/>preprocessing tools<br/>[mkking.c]"]
  node_n_utils_transform["Transform tools<br/>input converters<br/>[setimf.c]"]
  node_n_analysis["Python tools<br/>post-processing<br/>[cmc_parser.py]"]
  node_n_docs["Docs<br/>documentation<br/>[index.rst]"]
  node_n_build["Build system<br/>cmake/docker/ci<br/>[CMakeLists.txt]"]
end

subgraph group_grp_gpu["GPU path"]
  node_n_cuda["CUDA path<br/>gpu acceleration<br/>[cmc_cuda.cu]"]
end

node_n_utils_ic -->|"prepare inputs"| node_n_cmc_main
node_n_utils_transform -->|"prepare inputs"| node_n_cmc_main
node_n_cmc_main -->|"drives"| node_n_cmc_core
node_n_cmc_core -->|"evolves"| node_n_cmc_dynamics
node_n_cmc_core -->|"dispatches"| node_n_cmc_binary
node_n_cmc_core -->|"updates stars"| node_n_cmc_evolution
node_n_cmc_core -->|"calls"| node_n_bse_wrap
node_n_bse_wrap -->|"delegates"| node_n_cosmic
node_n_cmc_binary -->|"delegates"| node_n_fewbody
node_n_cmc_core -->|"parallelizes"| node_n_cmc_mpi
node_n_cmc_core -->|"writes"| node_n_cmc_io
node_n_cmc_io -.->|"matches examples"| node_n_docs
node_n_analysis -->|"parses"| node_n_cmc_io
node_n_docs -.->|"documents"| node_n_utils_ic
node_n_build -->|"builds"| node_n_cmc_main
node_n_build -.->|"builds"| node_n_cuda
node_n_cuda -.->|"adjacent path"| node_n_cmc_main

click node_n_cmc_main "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc.c"
click node_n_cmc_core "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_core.c"
click node_n_cmc_dynamics "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_dynamics.c"
click node_n_cmc_binary "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_binbin.c"
click node_n_cmc_evolution "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_stellar_evolution.c"
click node_n_cmc_io "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_io.c"
click node_n_cmc_mpi "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_mpi.c"
click node_n_bse_wrap "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/bse_wrap/bse_wrap.c"
click node_n_cosmic "https://github.com/clustermontecarlo/cmc-cosmic/tree/master/src/bse_wrap/bse/COSMIC"
click node_n_fewbody "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/fewbody-0.24/fewbody.c"
click node_n_utils_ic "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/utils/mkking/mkking.c"
click node_n_utils_transform "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/utils/setimf/setimf.c"
click node_n_analysis "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/tools/cmc_parser.py"
click node_n_docs "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/docs/sphinx/source/index.rst"
click node_n_build "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/CMakeLists.txt"
click node_n_cuda "https://github.com/clustermontecarlo/cmc-cosmic/blob/master/cuda/cmc_cuda.cu"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_cmc_main,node_n_cmc_core,node_n_cmc_dynamics,node_n_cmc_binary,node_n_cmc_evolution,node_n_cmc_io,node_n_cmc_mpi toneBlue
class node_n_bse_wrap,node_n_cosmic,node_n_fewbody toneAmber
class node_n_utils_ic,node_n_utils_transform,node_n_analysis,node_n_docs,node_n_build toneMint
class node_n_cuda toneRose
```

## Components

### CMC engine

Main Monte Carlo cluster-dynamics runtime and physics handlers.

- [CMC runtime](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc.c): simulation engine · Top-level cluster evolution executable and state coordinator. · `src/cmc/cmc.c`
- [Core state](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_core.c): cluster state · Cluster state model, time stepping, and central orchestration. · `src/cmc/cmc_core.c`
- [Dynamics](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_dynamics.c): relaxation/orbits · Monte Carlo relaxation, orbit averaging, and dynamical updates. · `src/cmc/cmc_dynamics.c`
- [Binary physics](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_binbin.c): encounter handlers · Binary-single, binary-binary, collisions, and loss-cone handling. · `src/cmc/cmc_binbin.c`
- [Stellar evolution](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_stellar_evolution.c): evolution hooks · Stellar evolution coupling, removal, and evolutionary thresholds. · `src/cmc/cmc_stellar_evolution.c`
- [I/O layer](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_io.c): outputs/checkpoints · Snapshot writing, logs, fits helpers, and runtime data output. · `src/cmc/cmc_io.c`
- [MPI layer](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/cmc/cmc_mpi.c): parallel runtime · Parallel coordination and message-passing support. · `src/cmc/cmc_mpi.c`

### Embedded solvers

Bundled external physics solvers used by the core engine.

- [BSE bridge](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/bse_wrap/bse_wrap.c): stellar-evolution wrapper · Interface between CMC and the embedded COSMIC/BSE evolution code. · `src/bse_wrap/bse_wrap.c`
- [COSMIC](https://github.com/clustermontecarlo/cmc-cosmic/tree/master/src/bse_wrap/bse/COSMIC): embedded stellar evolution · Vendored COSMIC stellar and binary evolution implementation. · `src/bse_wrap/bse/COSMIC`
- [Few-body solver](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/fewbody-0.24/fewbody.c): interaction solver · Bundled few-body scattering and hierarchical interaction package. · `src/fewbody-0.24/fewbody.c`

### Workflow tools

Input generation, post-processing, documentation, and build infrastructure.

- [IC generators](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/utils/mkking/mkking.c): preprocessing tools · Standalone initial-condition and cluster model generators. · `src/utils/mkking/mkking.c`
- [Transform tools](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/src/utils/setimf/setimf.c): input converters · Utilities for IMF, units, stellar settings, binaries, and FITS conversion. · `src/utils/setimf/setimf.c`
- [Python tools](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/tools/cmc_parser.py): post-processing · Python parsing and analysis helpers for simulation outputs. · `tools/cmc_parser.py`
- [Docs](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/docs/sphinx/source/index.rst): documentation · Sphinx and Doxygen documentation sources plus example outputs. · `docs/sphinx/source/index.rst`
- [Build system](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/CMakeLists.txt): cmake/docker/ci · CMake configuration, Dockerfile, and CI workflows for reproducible builds. · `CMakeLists.txt`

### GPU path

Auxiliary CUDA code path and support library.

- [CUDA path](https://github.com/clustermontecarlo/cmc-cosmic/blob/master/cuda/cmc_cuda.cu): gpu acceleration · Auxiliary CUDA implementation and helper library. · `cuda/cmc_cuda.cu`

## Connections

- IC generators → CMC runtime: prepare inputs · Initial-condition generators produce cluster models consumed by the simulation.
- Transform tools → CMC runtime: prepare inputs · Preprocessing utilities transform tables, units, and binary populations before runtime.
- CMC runtime → Core state: drives · The executable enters the central cluster state and time-evolution loop.
- Core state → Dynamics: evolves · Core state updates invoke relaxation, orbit handling, and dynamical stepping.
- Core state → Binary physics: dispatches · Rare close encounters are routed to specialized binary and collision handlers.
- Core state → Stellar evolution: updates stars · The cluster model consults stellar evolution and removal logic as stars age or transform.
- Core state → BSE bridge: calls · CMC uses the wrapper to access COSMIC/BSE evolution routines.
- BSE bridge → COSMIC: delegates · The wrapper forwards evolution requests into the vendored COSMIC code.
- Binary physics → Few-body solver: delegates · Strong few-body interactions are solved by the bundled scattering package.
- Core state → MPI layer: parallelizes · Runtime coordination uses MPI for distributed execution.
- Core state → I/O layer: writes · Simulation state and diagnostics are persisted through the I/O layer.
- I/O layer → Docs: matches examples · Documented example outputs describe the expected files and logs.
- Python tools → I/O layer: parses · Python tools consume logs, catalogs, and snapshot outputs.
- Docs → IC generators: documents · Documentation includes input generation and example configuration workflows.
- Build system → CMC runtime: builds · CMake and related tooling assemble the main simulation binaries.
- Build system → CUDA path: builds · Separate build files support the optional CUDA path.
- CUDA path → CMC runtime: adjacent path · GPU code exists as an auxiliary implementation alongside the main C code path.
