# ansys/pyoptislang architecture

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

- Interactive diagram: https://gitdiagram.com/ansys/pyoptislang
- Repository: https://github.com/ansys/pyoptislang

## Overview

##### Project type and purpose
- This is a Python library (wrapper) for controlling Ansys optiSLang.
- It provides both local process management (launching optiSLang) and remote TCP/IP connections.
- Key use-cases: create/open/save/run optiSLang projects, evaluate designs, connect nodes, run Python scripts on the backend.

##### File structure
- src/ansys/optislang/core: core API modules (Optislang class, application logic, project management, node definitions, examples helper).
- src/ansys/optislang/core/tcp: modules for TCP communication (server commands, queries, managers).
- tests/: unit tests for both core and TCP layers.
- doc/: full Sphinx documentation; examples/: sample user scripts.

##### README for additional insights
- Describes two modes: local launch (spawn process) vs. remote connect (host/port).
- Lists main capabilities (start/manage instances, project lifecycle, script execution, design evaluation).

##### Main components
a. User Code
– Your Python scripts (examples/* or client code).
b. Python API Library
– Optislang class (core/application.py)
– Managers (core/managers.py)
– Project layer (core/project.py & project_parametric.py)
– NodeTypes & Nodes definitions (core/node_types.py, core/nodes.py)
– Utility layers (core/io.py, core/json_utils.py, core/logging.py, core/utils.py)
c. Communication Layer
– Local OSL process manager (core/osl_process.py)
– TCP client stubs (core/tcp/*.py)
d. External Service
– Ansys optiSLang application (local process or remote TCP server)
e. External Resources
– .opf project files (filesystem)
– Licensed optiSLang installation
– Optional solver interfaces (e.g., Abaqus in examples)

##### Relationships and data flow
1. User code invokes methods on the Optislang class.
2. The Optislang class delegates to application and manager modules.
3. For local mode: core/osl_process launches an optiSLang process and connects via TCP.
For remote mode: core/tcp modules initiate a TCP socket to the existing server.
4. Commands/queries (JSON-encoded) go over TCP to the optiSLang server.
5. The server executes actions: project load/run, node creation, design evaluation, script execution.
6. Results (status, data, project copy) stream back over TCP, get decoded in core/json_utils and core/io.
7. Library exposes results to user code; .opf files are read/written to disk.

##### Architectural patterns & design principles
- Layered architecture:
– Presentation: user scripts
– API layer: Optislang + managers + domain objects
– Infrastructure: TCP communication, process spawning
– External system: Ansys optiSLang server
- Separation of concerns: core vs. tcp modules
- Pluggable node types: abstract factories in node_types.py
- JSON messaging for command/query serialization

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Layer"
        UserScript["User Script"]:::user
    end

    subgraph "API Layer"
        Optislang["Optislang API"]:::api
        Application["Application Logic"]:::api
        Managers["Managers"]:::api
        Project["Project Management"]:::api
        ProjectParam["Parametric Project"]:::api
        NodeTypes["Node Types"]:::api
        Nodes["Nodes"]:::api
        SlotTypes["Slot Types"]:::api
        IO["I/O Module"]:::util
        JSONUtils["JSON Utils"]:::util
        Logging["Logging"]:::util
        Utils["Utils"]:::util
        Encoding["Encoding & Errors"]:::util
    end

    subgraph "Infrastructure Layer"
        OSLProcess["OSL Process Manager"]:::infra
        OSLServerLocal["OSL Server Wrapper"]:::infra
        TCPApp["TCP Application Client"]:::infra
        TCPManagers["TCP Managers"]:::infra
        TCPNodes["TCP Node Proxies"]:::infra
        TCPProject["TCP Project Proxies"]:::infra
        ServerCommands["Server Commands"]:::infra
        ServerQueries["Server Queries"]:::infra
        TCPSlotTypes["TCP Slot Types"]:::infra
        TCPServerStub["TCP Server Stub"]:::infra
    end

    subgraph "External Services"
        OptiServer["Ansys optiSLang Server"]:::external
        Filesystem[".opf Project Files"]:::fs
        License["Licensed Installation"]:::fs
    end

    UserScript -->|uses| Optislang
    Optislang -->|delegates| Application
    Optislang -->|delegates| Managers
    Managers -->|manages| Project
    Managers -->|manages| ProjectParam
    Managers -->|handles| NodeTypes
    NodeTypes -->|creates| Nodes
    Nodes -->|defines| SlotTypes
    Application -->|launch/connect| OSLProcess
    Application -->|connects| TCPApp
    OSLProcess -->|wraps| OSLServerLocal
    OSLServerLocal -->|controls| OptiServer
    TCPApp -->|issues| ServerCommands
    TCPManagers -->|orchestrates| ServerCommands
    ServerCommands -->|sends| OptiServer
    OptiServer -->|responses| JSONUtils
    JSONUtils -->|writes| IO
    IO -->|reads/writes| Filesystem
    JSONUtils -->|uses| Utils
    Handlers -->|logs| Logging
    Encoding -->|defines| Errors
    OptiServer -->|loads/saves| Filesystem
    OptiServer -->|requires| License

    classDef user fill:#D6EAF8,stroke:#1F618D
    classDef api fill:#AED6F1,stroke:#2874A6
    classDef util fill:#D1F2EB,stroke:#117A65
    classDef infra fill:#ABEBC6,stroke:#239B56
    classDef external fill:#F9E79F,stroke:#B7950B
    classDef fs fill:#D5D8DC,stroke:#566573

    click UserScript "https://github.com/ansys/pyoptislang/tree/main/examples/"
    click Optislang "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/optislang.py"
    click Application "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/application.py"
    click Managers "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/managers.py"
    click Project "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/project.py"
    click ProjectParam "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/project_parametric.py"
    click NodeTypes "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/node_types.py"
    click Nodes "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/nodes.py"
    click SlotTypes "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/slot_types.py"
    click IO "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/io.py"
    click JSONUtils "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/json_utils.py"
    click Logging "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/logging.py"
    click Utils "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/utils.py"
    click Encoding "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/encoding.py"
    click OSLProcess "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/osl_process.py"
    click OSLServerLocal "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/osl_server.py"
    click TCPApp "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/application.py"
    click TCPManagers "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/managers.py"
    click TCPNodes "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/nodes.py"
    click TCPProject "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/project.py"
    click ServerCommands "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/server_commands.py"
    click ServerQueries "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/server_queries.py"
    click TCPSlotTypes "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/slot_types.py"
    click TCPServerStub "https://github.com/ansys/pyoptislang/blob/main/src/ansys/optislang/core/tcp/osl_server.py"
```
