Skip to main content

A python wrapper for Ansys Mechanical

Project description

PyAnsys Python PyPI GH-CI Codecov MIT Black

Overview

PyMechanical brings Ansys Mechanical to Python. It enables your Python programs to use Mechanical within Python’s ecosystem. It includes the ability to:

  • Connect to a remote Mechanical session

  • Embed an instance of Mechanical directly as a Python object

Install the package

PyMechanical has three installation modes: user, developer, and offline.

Install in user mode

Before installing PyMechanical in user mode, ensure that you have the latest version of pip installed by running this command:

python -m pip install -U pip

Then, install PyMechanical by running this command:

python -m pip install ansys-mechanical-core

Install in developer mode

Installing PyMechanical in developer mode allows you to modify the source and enhance it.

To install PyMechanical in developer mode, perform these steps:

  1. Clone the pymechanical repository:

    git clone https://github.com/pyansys/pymechanical
  2. Access the pymechanical directory where the repository has been cloned:

    cd pymechanical
  3. Create a clean Python virtual environment and activate it:

    # Create a virtual environment
    python -m venv .venv
    
    # Activate it in a POSIX system
    source .venv/bin/activate
    
    # Activate it in Windows CMD environment
    .venv\Scripts\activate.bat
    
    # Activate it in Windows Powershell
    .venv\Scripts\Activate.ps1
  4. Ensure that you have the latest required build system tools:

    python -m pip install -U pip tox flit twine
  5. Install the project in editable mode:

    # Install the minimum requirements
    python -m pip install -e .
    
    # Install the minimum + tests requirements
    python -m pip install -e .[tests]
    
    # Install the minimum + doc requirements
    python -m pip install -e .[doc]
    
    # Install all requirements
    python -m pip install -e .[tests,doc]
  6. Verify your development installation:

    tox

Install in offline mode

If you lack an internet connection on your installation machine (or you do not have access to the private Ansys PyPI packages repository), you should install PyMechanical by downloading the wheelhouse archive from the Releases Page for your corresponding machine architecture.

Each wheelhouse archive contains all the Python wheels necessary to install PyMechanical from scratch on Windows, Linux, and MacOS from Python 3.7 to 3.11. You can install a wheelhouse archive on an isolated system with a fresh Python installation or on a virtual environment.

On Linux

This code shows how to unzip the wheelhouse archive and install PyMechanical on Linux with Python 3.7:

unzip ansys-mechanical-core-v0.7.dev3-wheelhouse-Linux-3.7.zip wheelhouse
pip install ansys-mechanical-core -f wheelhouse --no-index --upgrade --ignore-installed

On Windows

If you’re on Windows with Python 3.9, unzip the wheelhouse archive to a wheelhouse directory and then install PyMechanical using the preceding command.

Consider installing into a virtual environment.

Dependencies

You must have a licensed copy of Ansys Mechanical installed. When using an embedded instance, that installation must be runnable from the same computer as your Python program. When using a remote session, a connection to that session must be reachable from your Python program.

Getting started

PyMechanical uses the built-in scripting capabilities of Mechanical. For information on the scripting APIs available, see the Scripting in Mechanical Guide in the Ansys Help.

Configuring the Mechanical installation

On a Windows system, the environment variable AWP_ROOT<ver> is configured when Mechanical is installed, where <ver> is the Mechanical release number, such as 231 for release 2023 R1. PyMechanical automatically uses this environment variable (or variables if there are multiple installations of different versions) to locate the latest Mechanical installation. On a Linux system, you must configure the AWP_ROOT<ver> environment variable to point to the absolute path of a Mechanical installation.

Starting a remote session

To start a remote session of Mechanical on your computer from Python, use the launch_mechanical() method. This methods returns an object representing the connection to the session:

import ansys.mechanical.core as pymechanical

mechanical = pymechanical.launch_mechanical()

Running commands on the remote session

Given a connection to a remote session, you can send an IronPython script. This uses the built-in scripting capabilities of Mechanical. Here is an example:

result = mechanical.run_python_script("2+3")
result = mechanical.run_python_script("ExtAPI.DataModel.Project.ProjectDirectory")

Using an embedded instance of Mechanical as a Python object

PyMechanical also supports directly embedding an instance of Mechanical as a Python object. In this mode, there is no externally running instance of Mechanical. This feature is supported on Windows for version 2023 R1 and later, and it will be supported on Linux for version 2023 R2 and later. Here is an example:

import ansys.mechanical.core as pymechanical

app = pymechanical.App()
result = app.ExtAPI.DataModel.Project.ProjectDirectory

Testing

This project takes advantage of tox. This tool automates common development tasks (similar to Makefile), but it is oriented towards Python development.

Using tox

While Makefile has rules, tox has environments. In fact, tox creates its own virtual environment so that anything being tested is isolated from the project to guarantee the project’s integrity.

The following environment commands are provided:

  • tox -e style: Checks for coding style quality.

  • tox -e py: Checks for unit tests.

  • tox -e py-coverage: Checks for unit testing and code coverage.

  • tox -e doc: Checks for documentation-building process.

Raw testing

If required, from the command line, you can call style commands like black, isort, and flake8. You can also call unit testing commands like pytest. However, running these commands do not guarantee that your project is being tested in an isolated environment, which is the reason why tools like tox exist.

Using pre-commit

The style checks implemented for PyMechanical take advantage of pre-commit. Developers are not forced but are encouraged to install this tool by running this command:

python -m pip install pre-commit && pre-commit install

Documentation

For building documentation, you can run the usual rules provided in the Sphinx make file. Here is an example:

#  build and view the doc from the POSIX system
make -C doc/ html && your_browser_name doc/html/index.html

# build and view the doc from CMD / PowerShell environment
.\doc\make.bat clean
.\doc\make.bat html
start .\doc\_build\html\index.html

However, the recommended way of checking documentation integrity is to use tox:

tox -e doc && your_browser_name .tox/doc_out/index.html

Distributing

If you would like to create either source or wheel files, start by installing the building requirements and then executing the build module:

python -m pip install -U pip
python -m flit build
python -m twine check dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansys-mechanical-core-0.7.0.tar.gz (50.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansys_mechanical_core-0.7.0-py3-none-any.whl (53.1 kB view details)

Uploaded Python 3

File details

Details for the file ansys-mechanical-core-0.7.0.tar.gz.

File metadata

  • Download URL: ansys-mechanical-core-0.7.0.tar.gz
  • Upload date:
  • Size: 50.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for ansys-mechanical-core-0.7.0.tar.gz
Algorithm Hash digest
SHA256 9c225f8be7b351b0229610d7fbc3542aa08fa729a077b3528fd4a9c7266db1e3
MD5 09d5a59e1363b7b6c8a26e9059c271a9
BLAKE2b-256 deb4db1c01470be0298271b344140a6c8fd7da15f09b63b063e4883af1ee6e26

See more details on using hashes here.

File details

Details for the file ansys_mechanical_core-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ansys_mechanical_core-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1b9b90829ad1dafa7f27600aab884a1aa4ad5e9afc55dd18016fa231024dd42
MD5 d873284c1f65dc495ddd85a6e0d7f420
BLAKE2b-256 e638e401c70df312fef0f37f1601c744cfbf36943d8404085ad574da76891345

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page