How ansys/pyoptislang works
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