Skip to main content

Material Definition with Automatic Differentiation

Project description

matADi

Material Definition with Automatic Differentiation (AD)

PyPI version shields.io License: GPL v3 Made with love in Graz (Austria) codecov DOI Codestyle black GitHub Repo stars PyPI - Downloads

matADi is a simple Python module which acts as a wrapper on top of casADi for easy definitions of hyperelastic strain energy functions. Gradients (stresses) and hessians (elasticity tensors) are carried out by casADi's powerful and fast Automatic Differentiation (AD) capabilities. It is designed to handle inputs with trailing axes which is especially useful for the application in Python-based finite element modules like scikit-fem or FElupe. Mixed-field formulations are supported as well as single-field formulations.

Installation

Install matADi from PyPI via pip.

pip install matadi

Usage

First, we have to define a symbolic variable on which our strain energy function will be based on.

Note: A variable of matADi is an instance of a symbolic variable of casADi (casadi.SX.sym). All matadi.math functions are simple links to (symbolic) casADi-functions.

from matadi import Variable, Material
from matadi.math import det, transpose, trace

F = Variable("F", 3, 3)

Next, take your favorite paper on hyperelasticity or be creative and define your own strain energy density function as a function of some variables x (where x is always a list of variables).

def neohooke(x, mu=1.0, bulk=200.0):
    """Strain energy density function of a nearly-incompressible 
    Neo-Hookean isotropic hyperelastic material formulation."""

    F = x[0]
    
    J = det(F)
    C = transpose(F) @ F
    I1_iso = J ** (-2 / 3) * trace(C)

    return mu * (I1_iso - 3) + bulk * (J - 1) ** 2 / 2

With this simple Python function we create an instance of a Material, which allows extra args and kwargs to be passed to our strain energy function. This instance now enables the evaluation of both gradient (stress) and hessian (elasticity) via methods based on automatic differentiation - optionally also on input data containing trailing axes. If necessary, the strain energy density function itself will be evaluated on input data with optional trailing axes by the function method.

Mat = Material(
    x=[F],
    fun=neohooke,
    kwargs={"mu": 1.0, "bulk": 10.0},
)

# init some random deformation gradients
defgrad = np.random.rand(3, 3, 5, 100) - 0.5

for a in range(3):
    defgrad[a, a] += 1.0

W = Mat.function([defgrad])[0]
P = Mat.gradient([defgrad])[0]
A = Mat.hessian([defgrad])[0]

Hints

Please have a look at casADi's documentation. It is very powerful but unfortunately does not support all the Python stuff you would expect. For example Python's default if-statements can't be used in combination with a symbolic boolean operation. If you use eigvals to symbolically calculate eigenvalues and their corresponding gradients please call gradient and hessian methods with Mat.gradient([defgrad], modify=True, eps=1e-5) to avoid the gradient to be filled with NaN's. This is because the gradient of the implemented eigenvalue calculation is not defined for the case of repeated equal eigenvalues. The modify argument adds a small number eps=1e-5 to the diagonal entries of the input data.

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

matadi-0.0.3.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

matadi-0.0.3-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file matadi-0.0.3.tar.gz.

File metadata

  • Download URL: matadi-0.0.3.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for matadi-0.0.3.tar.gz
Algorithm Hash digest
SHA256 31a24d1d7a714e68f2b0fbe8b70d33f2dd5dbcad2c4bdabf4cf8f7f263ba4f80
MD5 f272593f570a558f5457fda42d2a9e12
BLAKE2b-256 452d9d7f6ba362ce0ec3c42cbd6a3d1ec274713895dcec1847d0658a46ed3bef

See more details on using hashes here.

File details

Details for the file matadi-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: matadi-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for matadi-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fbe8af766ac6435505a1e320f80a9d8e7850f30b1fdb78f1e1eafb756d836070
MD5 73d33bbb742d9ab1024f105d08d332ad
BLAKE2b-256 25c763bf692250ad46b637d8fc916f245d22e83afa43c15630a910277ba42817

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