# medwatt/gmid architecture

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

- Interactive diagram: https://gitdiagram.com/medwatt/gmid
- Repository: https://github.com/medwatt/gmid

## Overview

##### Main Components
- Lookup Table Generator: This module (located under src/mosplot/lookup_table_generator) is responsible for running SPICE simulations, reading model files, and building lookup tables. Note that it wraps simulator-specific modules (for both ngspice and hspice) and corresponding parsers.
- Simulator Abstraction & Parsers: Inside the lookup table generator, the simulators folder abstracts the simulation back-end. It has base simulator classes (base_simulator.py) and concrete implementations (hspice_simulator.py and ngspice_simulator.py) with dedicated sub-modules for parsing simulation outputs.
- Plotting Module: Under src/mosplot/plot, this module processes data stored in the lookup tables, evaluates expressions (via the Expression classes), and generates various plots (including handling interpolation, twin-axis plots, and quick plot methods). It also pulls in utility routines (in the plot/util subfolder) for data extraction and manipulation.
- Optimization Module: Located in src/mosplot/optimizer, this module focuses on optimizing circuit parameters given target specifications and uses the lookup table data. It includes data type definitions (datatypes.py), the optimizer engine (optimizer.py), and design report generation (design_report.py).
- Tests: The tests folder shows examples such as sample designs and lookup table testing, which are essential for validating inter-module interactions.

##### Out the Interactions
- Data Flow: The Lookup Table Generator uses SPICE simulators (via their respective modules) to produce simulation results that are parsed and stored as lookup tables (Python dictionaries). These tables then serve as the common data source for both the Plotting and Optimization modules.
- Util Functions: Shared helper code (under plot/util and similar utility parts) helps in extracting and formatting data consistently for plotting/analysis.

##### Architectural Patterns and Principles
- Modular Design: Each main functionality (lookup, plotting, optimization) is encapsulated in its own sub-package, reflecting a clear separation of concerns.
- Abstraction Layers: The simulator interface abstracts differences between ngspice and hspice, and the use of parsers for each keeps simulation-specific details separate from the high-level lookup table generation code.
- Reusability & Extensibility: The architecture supports easy extension (for instance, adding new simulators or plotting expressions) because each component communicates through defined interfaces and shared data structures (lookup tables as dictionaries).

##### Relevant Technologies
- Python 3.9+ as the runtime
- Libraries such as numpy, scipy, and matplotlib support numerical computations and plotting.
- External SPICE simulators (ngspice/hspice) are critical to generating simulation data.
- The system also includes standard Python packaging via setup.py and integration test cases.

## Diagram (Mermaid)

```mermaid
flowchart TD
    SPICEExt["External SPICE Simulators"]:::external

    subgraph "Lookup Table Generation"
        LTGenerator["Lookup Table Generator"]:::core
        NETGenerator["MOSFET Netlist Generator"]:::core
        MOSim["MOSFET Simulation"]:::core
        subgraph "Simulator Abstraction & Parsers"
            BaseSim["Base Simulator"]:::simulator
            HspiceSim["HSPICE Simulator"]:::simulator
            NgspiceSim["NGSPICE Simulator"]:::simulator
            HspiceParser["HSPICE Parser"]:::simulator
            NgspiceParser["NGSPICE Parser"]:::simulator
        end
    end

    LookupTable["Lookup Table Data"]:::data

    subgraph "Plotting Module"
        PlotMod["Plotting Module"]:::plot
        ExprEval["Expression Evaluator"]:::plot
        Interp["Interpolation Functionality"]:::plot
        PlotUtil["Plot Utility Functions"]:::plot
    end

    subgraph "Optimization Module"
        Optimizer["Optimizer Engine"]:::optimization
        OptDatatype["Data Types for Optimization"]:::optimization
        DesignReport["Design Report Generator"]:::optimization
    end

    Tests["Test Suite"]:::test

    SPICEExt -->|"simulation_input"| LTGenerator
    NETGenerator --> LTGenerator
    MOSim --> LTGenerator
    BaseSim --> LTGenerator
    HspiceSim --> LTGenerator
    NgspiceSim --> LTGenerator
    HspiceParser --> LTGenerator
    NgspiceParser --> LTGenerator
    LTGenerator --> LookupTable
    LookupTable -->|"lookup_data"| PlotMod
    LookupTable -->|"lookup_data"| Optimizer
    PlotMod --> ExprEval
    PlotMod --> Interp
    PlotMod --> PlotUtil
    Optimizer --> OptDatatype
    Optimizer --> DesignReport
    Tests --> LTGenerator
    Tests --> PlotMod
    Tests --> Optimizer

    click LTGenerator "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/lookup_table_generator.py"
    click NETGenerator "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/mosfet_netlist_generator.py"
    click MOSim "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/mosfet_simulation.py"
    click BaseSim "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/simulators/base_simulator.py"
    click HspiceSim "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/simulators/hspice_simulator.py"
    click NgspiceSim "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/simulators/ngspice_simulator.py"
    click HspiceParser "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/simulators/parsers/hspice.py"
    click NgspiceParser "https://github.com/medwatt/gmid/blob/main/src/mosplot/lookup_table_generator/simulators/parsers/ngspice.py"
    click PlotMod "https://github.com/medwatt/gmid/blob/main/src/mosplot/plot/plot.py"
    click ExprEval "https://github.com/medwatt/gmid/blob/main/src/mosplot/plot/expressions.py"
    click Interp "https://github.com/medwatt/gmid/blob/main/src/mosplot/plot/interpolation.py"
    click PlotUtil "https://github.com/medwatt/gmid/tree/main/src/mosplot/plot/util/"
    click Optimizer "https://github.com/medwatt/gmid/blob/main/src/mosplot/optimizer/optimizer.py"
    click OptDatatype "https://github.com/medwatt/gmid/blob/main/src/mosplot/optimizer/datatypes.py"
    click DesignReport "https://github.com/medwatt/gmid/blob/main/src/mosplot/optimizer/design_report.py"
    click Tests "https://github.com/medwatt/gmid/tree/main/tests/"

    classDef external fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef core fill:#ccf,stroke:#333,stroke-width:2px;
    classDef simulator fill:#ffc0cb,stroke:#333,stroke-width:2px;
    classDef data fill:#e0e0e0,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef plot fill:#b3ffb3,stroke:#333,stroke-width:2px;
    classDef optimization fill:#b3e5ff,stroke:#333,stroke-width:2px;
    classDef test fill:#ffb3b3,stroke:#333,stroke-width:2px;
```
