ToPy Topology Optimization Framework

repository·master·Indexed 17 days ago

https://github.com/williamhunter/topy

A lightweight Python framework for solving compliance (stiffness), mechanism synthesis, and heat conduction problems in 2D and 3D. It supports problem definition via ToPy Problem Definition (.tpd) files or configuration dictionaries and provides a Python API and CLI tool (optimise.py) for execution. Stable release v0.4.1 targets Python 2 with Pysparse, while the master branch supports Python 3. Results can be exported as PNG images for 2D problems or .vtk files for 3D problems.

Tokens
1.7K
Snippets
10
Records
11
Agent score
69%

What's inside ToPy

  1. Define a problem using a TPD file

    master

    A ToPy Problem Definition (TPD) file is a simple text file containing keywords that define the optimization constraints, grid, and parameters. The file must have the .tpd extension.

    To use a TPD file in Python, load it into a Topology instance using load_tpd_file().

    Minimal required parameters include:

    • PROB_TYPE: Problem type (e.g., comp for compliance/stiffness).
    • PROB_NAME: Name of the problem.
    • ETA: Parameter value.
    • DOF_PN: Degrees of freedom.
    • VOL_FRAC: Volume fraction.
    • FILT_RAD: Filter radius.
    • P_FAC: Power factor.
    • ELEM_K: Element type (e.g., Q4).
    • NUM_ELEM_X, NUM_ELEM_Y, NUM_ELEM_Z: Grid dimensions.
    • NUM_ITER: Number of iterations.
    • FXTR_NODE_X, FXTR_NODE_Y: Fixation node coordinates.
    • LOAD_NODE_Y: Load node coordinate.
    • LOAD_VALU_Y: Load value.
    from topy import Topology
    
    topology = Topology()
    topology.load_tpd_file('file.tpd')
  2. Define a problem using a Config dictionary

    master

    You can define a problem directly in Python using a configuration dictionary. This is useful for rapid experimentation without modifying external text files. The dictionary keys and requirements are identical to those used in a .tpd file.

    Pass the dictionary to the Topology constructor via the config argument.

    config = {
         'DOF_PN': 2,
         'ELEM_K': 'Q4',
         'ETA': '0.5',
         'FILT_RAD': 1.5,
         'FXTR_NODE_X': range(1, 22),
         'FXTR_NODE_Y': 1281,
         'LOAD_NODE_Y': 1,
         'LOAD_VALU_Y': -1,
         'NUM_ELEM_X': 60,
         'NUM_ELEM_Y': 20,
         'NUM_ELEM_Z': 0,
         'NUM_ITER': 94,
         'PROB_NAME': 'beam_2d_reci',
         'PROB_TYPE': 'comp',
         'P_FAC': 3.0,
         'VOL_FRAC': 0.5
    }
    
    from topy import Topology
    topology = Topology(config=config)
  3. Generate element stiffness matrices

    master

    The first time you run ToPy, it may automatically generate element stiffness matrices using SymPy. This process can take several minutes and is usually only required once.

    If you want to manually trigger the creation of these matrices without solving a full problem, run the optimise.py script located in the scripts folder.

    # Run from the scripts directory to generate matrices
    python scripts/optimise.py
  4. Run the 'optimise.py' script to solve a problem

    master

    To solve a problem defined in a TPD (ToPy Problem Definition) file, use the optimise.py script. You must run this command from within the specific example directory containing the .tpd file.

    Note for Windows users: Unlike Linux or macOS, you cannot use symbolic links to reference a central optimise.py script. You must manually copy the optimise.py file into each example directory where you wish to run it.

    python optimise.py <filename>.tpd
  5. Configure Python environment and dependencies

    master

    ToPy requires Python 2.7 due to its dependency on Pysparse. On Windows, ensure your Python 'bitness' (32 or 64 bit) matches the packages you download.

    Windows Setup Steps

    1. Verify Python and Pip: Ensure python and pip are in your 'Path' Environment Variables. You can check this by typing them into a command prompt. If pip is missing, add Python27\Scripts to your Environment Variables.
    2. Install NumPy+MKL: Download the Python 2.7 version from Christoph Gohlke's website and install via pip.
    3. Install Pysparse: Download from Christoph Gohlke's website and install via pip. Note: NumPy must be installed before Pysparse.
    4. Install remaining dependencies: Use pip to install matplotlib, SymPy, and PyVTK.

    Note: When using pip on Windows, run the command prompt as Administrator.

    Linux and Mac Setup

    Install the following packages via pip or system package managers (e.g., apt-get, yum, rpm):

    • NumPy+MKL
    • Pysparse
    • matplotlib
    • SymPy
    • PyVTK
  6. Verify Python architecture

    master

    If you are unsure whether your Python installation is 32-bit or 64-bit (which is critical for selecting the correct Windows binaries), run the following command in a Python shell:

    import platform
    platform.architecture()
  7. Install ToPy

    master

    ToPy can be installed from source after downloading its dependencies.

    Note on Python Versions:

    • The stable release (v0.4.1) is designed for Python 2 and works with Pysparse (recommended if using the Anaconda distribution).
    • The latest master branch (unstable) supports Python 3.

    To install the stable release:

    1. Download the latest stable release from the GitHub releases page.
    2. Navigate to the topy/topy directory.
    3. Run the installation command.
    $ cd topy/topy
    $ python setup.py install
  8. Visualize optimization results

    master

    The topy.visualization module allows you to export results for viewing:

    • 2D problems: Save as .png images.
    • 3D problems: Save as .vtk files (viewable with Mayavi or ParaView).

    To create an animation from a sequence of PNG images, you can use the ImageMagick convert tool.

    convert -delay 35 *.png anim.gif
  9. Solve a topology optimization problem

    master

    You can solve a defined problem using either the command line or a Python script.

    Command Line

    Run the optimise.py script and pass the path to your .tpd file:

    python topy/scripts/optimise.py <filename>.tpd

    Python API

    1. Initialize a Topology object with your config.
    2. Call .set_top_params() to prepare the instance.
    3. Call topy.optimise(t) to run the solver.
    import topy
    
    config = {...}
    t = topy.Topology(config)
    t.set_top_params()
    topy.optimise(t)