# ranphe/circuitsim architecture

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

- Interactive diagram: https://gitdiagram.com/ranphe/circuitsim
- Repository: https://github.com/ranphe/circuitsim

## Overview

##### Project Type and Purpose
- CircuitSim is a standalone desktop Java application with a Swing-based GUI for interactively visualizing and analyzing electrical circuits via the Modified Nodal Analysis (MNA) method.
- It reads a plain-text circuit description, builds an in-memory representation of components and nodes, computes node voltages, lays out the circuit graphically, and lets users export the result as a PNG.

##### High-Level Components
a. User Interface Layer
- Main (entry point that initializes the GUI)
- PanelCircuito (Swing JPanel that renders and handles user interactions)
- Menu/Options (Load dialog, Export action)
b. Input Parsing
- CircuitoParser (reads *.txt file, tokenizes lines, constructs Componente and Nodo objects)
c. Domain Model
- Componente (abstract base for R, C, F sources)
- Nodo (circuit node with identifiers and computed voltages)
- ValorElectrico (encapsulates numeric values, units)
d. Analysis Engine
- MNA (assembles conductance/current matrices, solves linear system for node voltages)
e. Layout Engine
- LayoutCircuito (BFS-based algorithm that assigns (x,y) positions to nodos for clear visualization)
f. Utilities
- Exporter (writes current JPanel as PNG) – implicit in PanelCircuito
- Prueba (test harness / debug entry)
g. Data Flow / Dependencies
- Main → UI
- UI “Load” → CircuitoParser → Domain Model
- Domain Model → MNA → voltages populated in Nodo
- Domain Model + voltages → LayoutCircuito → positional data
- Layout + Domain Model → PanelCircuito.render() → screen & optional PNG

##### Architectural Patterns and Design Principles
- Layered architecture: UI ←→ Parser ←→ Domain ←→ MNA ←→ Layout
- Separation of concerns: parsing, data modeling, numerical analysis, layout, and rendering are in separate classes/packages.
- In-memory model: all data lives in Java objects; no external database.
- Event-driven GUI on Swing’s EDT (Event Dispatch Thread).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External entities
    ExternalLoad["Load File"]:::external
    ExternalExport["Export PNG"]:::external
    Screen["Display (Swing EDT)"]:::external

    %% UI Layer
    subgraph "UI Layer"
        Main["Main (entry point)"]:::ui
        Panel["PanelCircuito (renders GUI)"]:::ui
    end

    %% Parser
    subgraph "Parser"
        Parser["CircuitoParser"]:::core
    end

    %% Domain Model
    subgraph "Domain Model"
        Componente["Componente"]:::domain
        Nodo["Nodo"]:::domain
        Valor["ValorElectrico"]:::domain
    end

    %% Analysis Engine
    subgraph "Analysis Engine"
        MNA["MNA"]:::core
    end

    %% Layout Engine
    subgraph "Layout Engine"
        Layout["LayoutCircuito"]:::core
    end

    %% Utilities
    subgraph "Utilities"
        Prueba["Prueba (test harness)"]:::util
    end

    %% In-memory data
    Data[(In-memory Data Structures)]:::data

    %% Data Flows
    ExternalLoad -.->|"file contents"| Parser
    Parser -->|"constructed objects"| Data
    Data -->|"model"| Componente
    Data -->|"model"| Nodo
    Data -->|"model"| Valor

    Data -->|"domain model"| MNA
    MNA -->|"voltages"| Data

    Data -->|"voltage-populated model"| Layout
    Layout -->|"positional data"| Data

    Main -->|"initializes GUI"| Panel
    Data -->|"model + positions"| Panel
    Panel -->|"render"| Screen
    Panel -.->|"writes PNG"| ExternalExport

    %% Test harness
    Prueba -->|"debug entry"| Parser

    %% Click Events
    click Main "https://github.com/ranphe/circuitsim/blob/main/src/Main.java"
    click Panel "https://github.com/ranphe/circuitsim/blob/main/src/PanelCircuito.java"
    click Parser "https://github.com/ranphe/circuitsim/blob/main/src/CircuitoParser.java"
    click Componente "https://github.com/ranphe/circuitsim/blob/main/src/Componente.java"
    click Nodo "https://github.com/ranphe/circuitsim/blob/main/src/Nodo.java"
    click Valor "https://github.com/ranphe/circuitsim/blob/main/src/ValorElectrico.java"
    click MNA "https://github.com/ranphe/circuitsim/blob/main/src/MNA.java"
    click Layout "https://github.com/ranphe/circuitsim/blob/main/src/LayoutCircuito.java"
    click Prueba "https://github.com/ranphe/circuitsim/blob/main/src/Prueba.java"

    %% Styles
    classDef ui fill:#FFA500,stroke:#333,stroke-width:1px
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef domain fill:#90EE90,stroke:#333,stroke-width:1px
    classDef data fill:#DDDDDD,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef util fill:#E0E0E0,stroke:#333,stroke-width:1px
    classDef external fill:#F0F0F0,stroke:#333,stroke-width:1px,stroke-dasharray: 2 2
```
