# swap-model/swap architecture

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

- Interactive diagram: https://gitdiagram.com/swap-model/swap
- Repository: https://github.com/swap-model/swap

## Overview

SWAP is a field-scale soil–water–atmosphere–plant simulator. Its principal workflow is to load and validate simulation configuration and meteorological inputs, run the configured time-stepped model, and write CSV results. The graph preserves the documented water, heat, solute, crop, and drainage domains. Source excerpts were unavailable, so links among runtime modules are limited to the documented configuration pipeline and high-level model workflow; detailed call ownership is not verified.

Main entry points: [`src/core/swap_main.f90`](https://github.com/swap-model/swap/blob/main/src/core/swap_main.f90) (Simulation entry), [`src/io/toml/load_swap_config.f90`](https://github.com/swap-model/swap/blob/main/src/io/toml/load_swap_config.f90) (Load configuration), [`src/validation/validation.f90`](https://github.com/swap-model/swap/blob/main/src/validation/validation.f90) (Validate settings), [`src/io/readmeteo.f90`](https://github.com/swap-model/swap/blob/main/src/io/readmeteo.f90) (Meteorology input)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Configuration and inputs"]
  node_loadconfig["Load configuration"]
  node_validate["Validate settings<br/>[validation.f90]"]
  node_configvars["Configure variables"]
  node_meteo["Meteorology input<br/>[readmeteo.f90]"]
end

subgraph group_engine["Simulation engine"]
  node_entry["Simulation entry<br/>[swap_main.f90]"]
  node_time["Time control<br/>[timecontrol.f90]"]
  node_state["Model state<br/>[variables.f90]"]
end

subgraph group_processes["Environmental processes"]
  node_atmosphere["Atmospheric processes"]
  node_soil["Soil water flow"]
  node_heat["Heat transport"]
  node_solute["Solute transport"]
  node_crop["Crop growth"]
  node_drainage["Drainage and runoff"]
  node_boundary["Soil boundaries"]
end

subgraph group_results["Results and diagnostics"]
  node_csvout["CSV results"]
  node_errors["Error collection<br/>[error.f90]"]
end

node_user(("Model user"))
node_configfiles["TOML configuration"]

node_user -->|"provides"| node_configfiles
node_configfiles -->|"loads"| node_loadconfig
node_loadconfig -->|"validates"| node_validate
node_validate -->|"finalizes"| node_configvars
node_user -->|"starts simulation"| node_entry
node_entry -->|"advances steps"| node_time
node_meteo -->|"supplies weather"| node_entry
node_entry -->|"updates state"| node_state
node_entry -->|"simulates"| node_atmosphere
node_entry -->|"simulates"| node_soil
node_entry -->|"simulates"| node_heat
node_entry -->|"simulates"| node_solute
node_entry -->|"simulates"| node_crop
node_entry -->|"simulates"| node_drainage
node_entry -->|"applies boundaries"| node_boundary
node_entry -->|"writes results"| node_csvout
node_entry -->|"collects errors"| node_errors

click node_loadconfig "https://github.com/swap-model/swap/blob/main/src/io/toml/load_swap_config.f90"
click node_validate "https://github.com/swap-model/swap/blob/main/src/validation/validation.f90"
click node_configvars "https://github.com/swap-model/swap/blob/main/src/io/toml/config_to_variables.f90"
click node_meteo "https://github.com/swap-model/swap/blob/main/src/io/readmeteo.f90"
click node_entry "https://github.com/swap-model/swap/blob/main/src/core/swap_main.f90"
click node_time "https://github.com/swap-model/swap/blob/main/src/core/timecontrol.f90"
click node_state "https://github.com/swap-model/swap/blob/main/src/core/variables.f90"
click node_atmosphere "https://github.com/swap-model/swap/tree/main/src/atmosphere"
click node_soil "https://github.com/swap-model/swap/tree/main/src/soil"
click node_heat "https://github.com/swap-model/swap/tree/main/src/heat"
click node_solute "https://github.com/swap-model/swap/tree/main/src/solute"
click node_crop "https://github.com/swap-model/swap/tree/main/src/crop"
click node_drainage "https://github.com/swap-model/swap/tree/main/src/drainage"
click node_boundary "https://github.com/swap-model/swap/tree/main/src/boundary"
click node_csvout "https://github.com/swap-model/swap/blob/main/src/io/swap_csv_output.f90"
click node_errors "https://github.com/swap-model/swap/blob/main/src/error/error.f90"

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_loadconfig,node_validate,node_configvars,node_meteo,node_user toneBlue
class node_entry,node_time,node_state toneAmber
class node_atmosphere,node_soil,node_heat,node_solute,node_crop,node_drainage,node_boundary toneMint
class node_csvout,node_errors toneRose
class node_configfiles toneIndigo
```

## Components

### Configuration and inputs

- [Load configuration](https://github.com/swap-model/swap/blob/main/src/io/toml/load_swap_config.f90): `src/io/toml/load_swap_config.f90`
- [Validate settings](https://github.com/swap-model/swap/blob/main/src/validation/validation.f90): `src/validation/validation.f90`
- [Configure variables](https://github.com/swap-model/swap/blob/main/src/io/toml/config_to_variables.f90): `src/io/toml/config_to_variables.f90`
- [Meteorology input](https://github.com/swap-model/swap/blob/main/src/io/readmeteo.f90): `src/io/readmeteo.f90`

### Simulation engine

- [Simulation entry](https://github.com/swap-model/swap/blob/main/src/core/swap_main.f90): `src/core/swap_main.f90`
- [Time control](https://github.com/swap-model/swap/blob/main/src/core/timecontrol.f90): `src/core/timecontrol.f90`
- [Model state](https://github.com/swap-model/swap/blob/main/src/core/variables.f90): `src/core/variables.f90`

### Environmental processes

- [Atmospheric processes](https://github.com/swap-model/swap/tree/main/src/atmosphere): `src/atmosphere`
- [Soil water flow](https://github.com/swap-model/swap/tree/main/src/soil): `src/soil`
- [Heat transport](https://github.com/swap-model/swap/tree/main/src/heat): `src/heat`
- [Solute transport](https://github.com/swap-model/swap/tree/main/src/solute): `src/solute`
- [Crop growth](https://github.com/swap-model/swap/tree/main/src/crop): `src/crop`
- [Drainage and runoff](https://github.com/swap-model/swap/tree/main/src/drainage): `src/drainage`
- [Soil boundaries](https://github.com/swap-model/swap/tree/main/src/boundary): `src/boundary`

### Results and diagnostics

- [CSV results](https://github.com/swap-model/swap/blob/main/src/io/swap_csv_output.f90): `src/io/swap_csv_output.f90`
- [Error collection](https://github.com/swap-model/swap/blob/main/src/error/error.f90): `src/error/error.f90`

### Other components

- **Model user**
- **TOML configuration**

## Connections

- Model user → TOML configuration: provides
- TOML configuration → Load configuration: loads
- Load configuration → Validate settings: validates
- Validate settings → Configure variables: finalizes
- Model user → Simulation entry: starts simulation
- Simulation entry → Time control: advances steps
- Meteorology input → Simulation entry: supplies weather
- Simulation entry → Model state: updates state
- Simulation entry → Atmospheric processes: simulates
- Simulation entry → Soil water flow: simulates
- Simulation entry → Heat transport: simulates
- Simulation entry → Solute transport: simulates
- Simulation entry → Crop growth: simulates
- Simulation entry → Drainage and runoff: simulates
- Simulation entry → Soil boundaries: applies boundaries
- Simulation entry → CSV results: writes results
- Simulation entry → Error collection: collects errors
