# dssat/dssat-csm-os architecture

> Architecture diagram of the GitHub repository dssat/dssat-csm-os, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dssat/dssat-csm-os
- Repository: https://github.com/dssat/dssat-csm-os

## Overview

##### Main Components
- Build & Configuration: Includes files like CMakeLists.txt, the cmake/Modules folder, and other build scripts (distclean.cmake). This represents how the project is built and configured (using CMake to set up Fortran compiling, etc.).
- Data Management: The “Data” directory holds genotype, pest, standard model data, and many CDE files. This is the backbone of simulation inputs.
- Input and Preprocessing: The InputModule directory contains blocks and Fortran source files (such as INSOIL.for, INSTGE.for, etc.) that handle data input and initial variable setups for simulation.
- Simulation Modules:
– Soil Module: Found under Soil/ (with submodules for water, nutrients, organic matter, etc.).
– Crop/Plant Modules: The Plant directory includes crop-specific simulation codes (e.g., CERES, CSYCA, CANEGRO, etc.) which simulate the growth of over 45 crop types.
– Management Module: Contains Fortran files under Management/ to simulate agricultural management processes.
– Weather Module: Files in Weather/ deal with weather data, forecasting, and climate inputs.
- Utilities: In the Utilities directory, codes that assist in error processing, output generation, and miscellaneous support functions are located.
- External/Additional Packages: Other folders (like SPAM, Soil subdirectories, etc.) extend the simulation capabilities (e.g., water stress, evapotranspiration, etc.).

##### Relationships and Interactions
- The Build System (managed by CMake) compiles the various simulation modules.
- At runtime, data from the Data Management module feeds into both the InputModule and Simulation Modules.
- The Simulation Engine acts as the integration point by orchestrating interactions among the Soil, Crop, Weather, and Management modules.
- Utilities support simulation runs (output handling, error checking, etc.).

##### Architectural Patterns
- Modular/MVC-like Separation: Each scientific discipline (soil, crop, weather, management) is isolated to allow for ease of replacement or extension.
- Data-Driven Design: The project relies on extensive data repositories (genotypes, weather, pest, etc.) which are decoupled from the simulation logic.
- Build-time Flexibility: The use of CMake and multiple IFort/Other Fortran flag configurations provides flexibility in compiling and testing.

## Diagram (Mermaid)

```mermaid
graph TD
  %% Build & Configuration Subgraph
  subgraph "Build & Configuration"
    BuildConfig["Build Config"]:::build
    CMakeModules["CMake Modules"]:::build
    DistClean["Clean Script"]:::build
  end

  %% Data Management and External Packages
  DataManagement["Data Management"]:::data
  ExternalPackages["External Packages"]:::external

  %% Input and Preprocessing
  InputPreprocessing["Input & Preprocessing"]:::input

  %% Simulation Engine and Modules
  SimEngine["Simulation Engine"]:::simulation
  SoilModule["Soil Module"]:::simulation
  CropPlantModule["Crop/Plant Modules"]:::simulation
  WeatherModule["Weather Module"]:::simulation
  ManagementModule["Management Module"]:::simulation

  %% Utilities
  Utilities["Utilities"]:::utility

  %% Connections from Build & Configuration to Input Module
  BuildConfig --> InputPreprocessing
  CMakeModules --> InputPreprocessing
  DistClean --> InputPreprocessing

  %% Data Flow from Data Management to Input Module
  DataManagement --> InputPreprocessing

  %% Input Module feeds the Simulation Engine
  InputPreprocessing --> SimEngine

  %% External Packages also contribute to Simulation Engine
  ExternalPackages --> SimEngine

  %% Simulation Engine distributes to Simulation Modules
  SimEngine --> SoilModule
  SimEngine --> CropPlantModule
  SimEngine --> WeatherModule
  SimEngine --> ManagementModule

  %% Build system also directly supports Simulation Modules (build-time integration)
  BuildConfig --- SoilModule
  BuildConfig --- CropPlantModule
  BuildConfig --- WeatherModule
  BuildConfig --- ManagementModule

  %% Simulation Modules send outputs to Utilities
  SoilModule --> Utilities
  CropPlantModule --> Utilities
  WeatherModule --> Utilities
  ManagementModule --> Utilities

  %% Class Definitions for Colors
  classDef build fill:#f9c74f,stroke:#333,stroke-width:2px;
  classDef data fill:#90be6d,stroke:#333,stroke-width:2px;
  classDef input fill:#f9844a,stroke:#333,stroke-width:2px;
  classDef simulation fill:#f3722c,stroke:#333,stroke-width:2px;
  classDef utility fill:#577590,stroke:#333,stroke-width:2px;
  classDef external fill:#277da1,stroke:#333,stroke-width:2px;

  %% Click Events
  click BuildConfig "https://github.com/dssat/dssat-csm-os/blob/develop/CMakeLists.txt"
  click CMakeModules "https://github.com/dssat/dssat-csm-os/tree/develop/cmake/Modules"
  click DistClean "https://github.com/dssat/dssat-csm-os/blob/develop/distclean.cmake"
  click DataManagement "https://github.com/dssat/dssat-csm-os/tree/develop/Data"
  click InputPreprocessing "https://github.com/dssat/dssat-csm-os/tree/develop/InputModule"
  click SoilModule "https://github.com/dssat/dssat-csm-os/tree/develop/Soil"
  click CropPlantModule "https://github.com/dssat/dssat-csm-os/tree/develop/Plant"
  click ManagementModule "https://github.com/dssat/dssat-csm-os/tree/develop/Management"
  click WeatherModule "https://github.com/dssat/dssat-csm-os/tree/develop/Weather"
  click Utilities "https://github.com/dssat/dssat-csm-os/tree/develop/Utilities"
  click ExternalPackages "https://github.com/dssat/dssat-csm-os/tree/develop/SPAM"
```
