# tumftm/global_racetrajectory_optimization architecture

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

- Interactive diagram: https://gitdiagram.com/tumftm/global_racetrajectory_optimization
- Repository: https://github.com/tumftm/global_racetrajectory_optimization

## Overview

##### Project Type and Purpose
- The project is primarily a trajectory planning and optimization tool for race cars. It generates optimal racelines based on different objectives (shortest path, minimum curvature, minimum time, and minimum time with powertrain effects). It also handles friction map generation and includes a powertrain model for more detailed simulation.
- The README clearly describes various components (friction mapping, trajectory helpers, and powertrain behavior) that are all essential to computing an optimal racing trajectory.

##### Analyzing the File Structure for Architectural Clues
- Top-Level Components:
- Core modules: The primary source directories are “frictionmap”, “helper_funcs_glob”, and “opt_mintime_traj”. Each has its own submodules and “src” directories, suggesting a modular design.
- Inputs and Parameters: The “inputs” folder gathers various racing track files, friction maps, and vehicle dynamic information, while the “params” folder contains configuration files (e.g., racecar.ini) needed for the optimization routines.
- Execution Scripts: The main scripts (e.g., main_gen_frictionmap.py and main_globaltraj.py) tie together the disparate packages to run friction map generation or the global trajectory optimization.
- Design Patterns:
- Functional separation is apparent: one module for friction maps, one for helper functions common to the trajectory planning, and one for the optimization (which even contains a subcomponent for modeling the powertrain).
- The presence of configuration, input, and output files (e.g., CSV export formats) suggests clear separation between data ingestion, processing, and export layers.

a. Data/Configuration Layer:
- Inputs: "inputs/frictionmaps", "inputs/tracks", and "inputs/veh_dyn_info"
- Parameters: "params/racecar.ini" and any configuration values in "requirements.txt".
- These are consumed by the main execution scripts and various modules.

b. Friction Map Module:
- Package: "frictionmap"
- Functions: Creation and plotting of friction maps (e.g., plot_frictionmap_data.py, plot_frictionmap_grid.py).
- Role: To process raw friction map files and generate visualization or intermediate data for trajectory planning.

c. Global Helper Functions:
- Package: "helper_funcs_glob"
- Functions: A variety of helper routines including data import, track interpolation, exported trajectory formatting (LTPL and race trajectory), and plotting functions.
- Role: To provide reusable code that supports both curvature and time-optimal trajectory planning.

d. Global Trajectory Optimization Module:
- Main scripts: "main_globaltraj.py" and functions within "opt_mintime_traj/src"
- Core components:
- Base trajectory planning algorithms (minimum curvature and time-optimal planning).
- Output formatting modules that generate two distinct trajectory output formats (Race Trajectory and LTPL Trajectory).
- Interaction: Consumes data from friction maps, reference track inputs, and vehicle dynamics; processes them using optimization techniques; and passes results to export routines.

e. Powertrain Optimization Component (Optional/Integrated):
- Package inside "opt_mintime_traj/powertrain_src"
- Functions: Modeling of battery, electric motor, inverter, and radiators to provide powertrain behavior simulation.
- Interaction: Directly integrated within the minimum time optimal trajectory operations when detailed powertrain dynamics are needed.

f. Execution and Integration Layer:
- Main entry points: "main_gen_frictionmap.py" for friction map generation and "main_globaltraj.py" for global trajectory optimization.
- Role: Coordinate process execution: parameter setup, data ingestion, function calls across modules, and final output generation.

##### Relationships and Data Flow
- This feeds into the Friction Map Module where friction data is processed and visualized (if needed).
- Helper functions offer support services (track interpolation, preprocessing) to the Global Trajectory Optimization Module.
- In the Global Trajectory Module, if the powertrain option is enabled in the parameters, data flows into the Powertrain submodule; otherwise, optimization proceeds with simplified assumptions.
- Finally, the computed trajectories are exported in either Race Trajectory or LTPL Trajectory formats, ready for subsequent use by another system (like a graph-based local trajectory planner).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data/Configuration Layer
    subgraph "Data/Configuration Layer"
        subgraph "Inputs"
            I1["Frictionmaps"]:::input
            I2["Tracks"]:::input
            I3["Veh Dyn Info"]:::input
        end
        subgraph "Parameters"
            P1["Racecar Configuration"]:::input
            P2["Requirements"]:::input
        end
        DC["Data & Config"]:::input
    end

    %% Execution & Integration Layer
    subgraph "Execution & Integration Layer"
        D1["Friction Map Generation Script"]:::execution
        D2["Global Trajectory Execution Script"]:::execution
    end

    %% Processing Modules
    subgraph "Processing Modules"
        subgraph "Friction Map Module"
            FM_Dir["Core Module"]:::module
            FM1["plot_frictionmap_data.py"]:::module
            FM2["plot_frictionmap_grid.py"]:::module
            FM3["reftrack_functions.py"]:::module
            FM_Dir --- FM1
            FM_Dir --- FM2
            FM_Dir --- FM3
        end

        subgraph "Global Helper Functions"
            GH_Dir["Core Functions"]:::module
            GH1["calc_min_bound_dists.py"]:::module
            GH2["check_traj.py"]:::module
            GH3["export_traj_ltpl.py"]:::module
            GH4["export_traj_race.py"]:::module
            GH5["import_track.py"]:::module
            GH6["interp_track.py"]:::module
            GH7["prep_track.py"]:::module
            GH8["result_plots.py"]:::module
            GH_Dir --- GH1
            GH_Dir --- GH2
            GH_Dir --- GH3
            GH_Dir --- GH4
            GH_Dir --- GH5
            GH_Dir --- GH6
            GH_Dir --- GH7
            GH_Dir --- GH8
        end

        subgraph "Global Trajectory Optimization Module"
            GT_Dir["Core Optimization"]:::module
            GT1["approx_friction_map.py"]:::module
            GT2["export_mintime_solution.py"]:::module
            GT3["extract_friction_coeffs.py"]:::module
            GT4["friction_map_interface.py"]:::module
            GT5["friction_map_plot.py"]:::module
            GT6["opt_mintime.py"]:::module
            GT7["result_plots_mintime.py"]:::module
            GT_Dir --- GT1
            GT_Dir --- GT2
            GT_Dir --- GT3
            GT_Dir --- GT4
            GT_Dir --- GT5
            GT_Dir --- GT6
            GT_Dir --- GT7
        end

        subgraph "Powertrain Module"
            PT_Dir["Powertrain Core"]:::module
            PT1["Battery.py"]:::module
            PT2["EMachine.py"]:::module
            PT3["Inverter.py"]:::module
            PT4["Radiators.py"]:::module
            PT_Dir --- PT1
            PT_Dir --- PT2
            PT_Dir --- PT3
            PT_Dir --- PT4
        end
    end

    %% Output Generation Layer
    subgraph "Output Generation"
        C1["Trajectory Output: Race Trajectory"]:::output
        C2["Trajectory Output: LTPL Trajectory"]:::output
    end

    %% Data/Configuration Aggregation
    I1 --> DC
    I2 --> DC
    I3 --> DC
    P1 --> DC
    P2 --> DC

    %% Execution Layer Connections
    DC --> D1
    DC --> D2

    %% Processing Connections
    D1 --> FM_Dir
    D2 --> GH_Dir
    D2 --> GT_Dir
    FM_Dir --> GT_Dir
    GH_Dir --> GT_Dir
    %% Optional Powertrain Integration
    GT_Dir --> PT_Dir
    PT_Dir --> GT_Dir

    %% Output Connections
    GT_Dir --> C1
    GT_Dir --> C2

    %% Click Events for Data/Configuration Layer
    click I1 "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/inputs/frictionmaps"
    click I2 "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/inputs/tracks"
    click I3 "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/inputs/veh_dyn_info"
    click P1 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/params/racecar.ini"
    click P2 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/requirements.txt"

    %% Click Events for Friction Map Module
    click FM_Dir "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/frictionmap/src"
    click FM1 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/frictionmap/src/plot_frictionmap_data.py"
    click FM2 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/frictionmap/src/plot_frictionmap_grid.py"
    click FM3 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/frictionmap/src/reftrack_functions.py"

    %% Click Events for Global Helper Functions
    click GH_Dir "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/helper_funcs_glob/src"
    click GH1 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/calc_min_bound_dists.py"
    click GH2 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/check_traj.py"
    click GH3 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/export_traj_ltpl.py"
    click GH4 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/export_traj_race.py"
    click GH5 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/import_track.py"
    click GH6 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/interp_track.py"
    click GH7 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/prep_track.py"
    click GH8 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/helper_funcs_glob/src/result_plots.py"

    %% Click Events for Global Trajectory Optimization Module
    click GT_Dir "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/opt_mintime_traj/src"
    click GT1 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/approx_friction_map.py"
    click GT2 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/export_mintime_solution.py"
    click GT3 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/extract_friction_coeffs.py"
    click GT4 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/friction_map_interface.py"
    click GT5 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/friction_map_plot.py"
    click GT6 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/opt_mintime.py"
    click GT7 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/src/result_plots_mintime.py"

    %% Click Events for Powertrain Module
    click PT_Dir "https://github.com/tumftm/global_racetrajectory_optimization/tree/master/opt_mintime_traj/powertrain_src/src"
    click PT1 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/powertrain_src/src/Battery.py"
    click PT2 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/powertrain_src/src/EMachine.py"
    click PT3 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/powertrain_src/src/Inverter.py"
    click PT4 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/opt_mintime_traj/powertrain_src/src/Radiators.py"

    %% Click Events for Execution Layer
    click D1 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/main_gen_frictionmap.py"
    click D2 "https://github.com/tumftm/global_racetrajectory_optimization/blob/master/main_globaltraj.py"

    %% Classes and Styling
    classDef input fill:#fce,stroke:#333,stroke-width:2px;
    classDef module fill:#bbf,stroke:#333,stroke-width:2px;
    classDef execution fill:#cfc,stroke:#333,stroke-width:2px;
    classDef output fill:#fc9,stroke:#333,stroke-width:2px;
```
