How boschresearch/torchphysics works
Overall Project Purpose
• TorchPhysics is designed as a modular, maintainable, and expandable library for applying deep learning (e.g., PINNs, DeepONets, QRes, Deep Ritz) to solve differential equations and related inverse problems.
• The library builds on PyTorch (and PyTorch Lightning for training) and offers robust domain generation, various differential operators, and many advanced neural network models.
Main Components
– Library Core (“src/torchphysics”)
• Models: Contains various deep learning architectures (FNO, DeepONet, DeepRitz, FCN, etc.). Located in src/torchphysics/models.
• Problem Definition:
• Conditions: Defines how differential conditions are imposed (e.g., PINN, variational, etc.) – found in src/torchphysics/problem/conditions.
• Domains: Provides mesh-free domain structures with classes for Point, Interval, Circle, Triangle, Sphere, etc. Also includes domain operations (Union, Cut, Intersection) under src/torchphysics/problem/domains and its operations folder.
• Function Sets: Implements function sets (such as harmonic function sets) under src/torchphysics/problem/domains/functionsets.
• Samplers: Houses various sampling strategies for data generation (grid, random, etc.) located in src/torchphysics/problem/samplers.
• Spaces: Contains abstract representations of function spaces and their discretizations.
• Solver: The module (src/torchphysics/solver.py) that drives the training and evaluation pipeline using models and leveraging the PyTorch framework.
• Utilities: A collection of helper functions including callbacks, plotting, evaluation routines – see src/torchphysics/utils.
– Testing and Documentation:
• Tests: Extensive unit tests (in the tests folder) ensure each module works correctly.
• Docs and Examples: Detailed notebooks and written tutorials (found in docs/ and examples/) assist users in applying the library to vary applications.
Relationships and Interactions
– The “Problem” component (conditions, domains, samplers, and spaces) is central, as it describes the mathematical/deep learning problem.
– The “Models” module implements various network architectures that are used to approximate solutions on these problem formulations.
– The “Solver” acts as the orchestrator bringing together the model, problem setup, and training process. It also integrates utilities such as callbacks and evaluation functions.
– External Dependencies: The system relies on PyTorch, PyTorch Lightning, and other Python scientific libraries. These should be marked clearly as external systems.
– Examples and batch scripts (outside the core src tree) show how end users interact with the library (through Jupyter notebooks or command-line utilities).
Architectural Patterns and Design Principles
– Modularity: Each logical concept (model, problem-defining elements, solver, utilities) is separated into its own folder and submodules.
– Expandability: New architectures or problem conditions can be added by extending the modular components.
– Testability: A comprehensive tests suite ensures reliability.
– Use clear labels for each module/component.
• From Problem (conditions/domains) to Solver (indicating that problem definitions feed into the solver for training)
• From Models to Solver (indicating that the network architectures are instantiated and trained by the solver)
• From Utilities to both Models and Solver (for evaluation, callbacks, and plotting).
• Core modules (e.g., rectangles for Models, ellipses for Problem elements)