Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding hencky and log strain. #52

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pancax/constitutive_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
# models
from .mechanics.hyperelasticity.blatz_ko import BlatzKo
from .mechanics.hyperelasticity.gent import Gent
from .mechanics.hyperelasticity.hencky import Hencky
from .mechanics.hyperelasticity.neohookean import NeoHookean
from .mechanics.hyperelasticity.swanson import Swanson
6 changes: 6 additions & 0 deletions pancax/constitutive_models/mechanics/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import abstractmethod
from ..base import ConstitutiveModel, Scalar, Tensor
from ...math import tensor_math
from typing import Tuple
import jax
import jax.numpy as jnp
Expand Down Expand Up @@ -101,6 +102,11 @@ def jacobian(self, grad_u: Tensor) -> Scalar:
)
return J

def log_strain(self, grad_u: Tensor) -> Tensor:
F = self.deformation_gradient(grad_u)
C = F.T @ F
return tensor_math.mtk_log_sqrt(C)

def pk1_stress(self, grad_u: Tensor, *args) -> Tensor:
return jax.grad(self.energy, argnums=0)(grad_u, *args)

Expand Down
21 changes: 21 additions & 0 deletions pancax/constitutive_models/mechanics/hyperelasticity/hencky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ..base import HyperelasticModel, Scalar, Tensor
# TODO maybe move math/tensor_math into constiutive models?
from ....math import tensor_math
from ...properties import Property
import jax.numpy as jnp


class Hencky(HyperelasticModel):
bulk_modulus: Property
shear_modulus: Property

def energy(self, grad_u: Tensor) -> Scalar:
K, G = self.bulk_modulus, self.shear_modulus

# kinematics
E = self.log_strain(grad_u)
trE = jnp.trace(E)
psi = 0.5 * K * trE**2 + \
G * jnp.tensordot(E, E)
# G * tensor_math.norm_of_deviator_squared(E)
return psi
83 changes: 83 additions & 0 deletions test/constitutive_models/test_hencky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from pancax import BoundedProperty, Hencky
from pancax.math import tensor_math
from .utils import *
import jax
import jax.numpy as jnp
import pytest


K = 0.833
G = 0.3846
key = jax.random.key(0)

@pytest.fixture
def hencky_1():
return Hencky(
bulk_modulus=K,
shear_modulus=G
)

@pytest.fixture
def hencky_2():
return Hencky(
bulk_modulus=BoundedProperty(K, K, key),
shear_modulus=BoundedProperty(G, G, key)
)


# TODO fix this test

def simple_shear_test(model):
gammas = jnp.linspace(0.0, 1., 100)
Fs = jax.vmap(simple_shear)(gammas)
grad_us = jax.vmap(lambda F: F - jnp.eye(3))(Fs)
# Js = jax.vmap(model.jacobian)(grad_us)
# I1_bars = jax.vmap(model.I1_bar)(grad_us)
Js = jax.vmap(model.jacobian)(grad_us)
Es = jax.vmap(model.log_strain)(grad_us)
trEs = jax.vmap(jnp.trace)(Es)
print(trEs)
# Edevs = jax.vmap(lambda E: E - (1. / 3.) * jnp.trace(E) * jnp.eye(3))(Es)
Edevs = jax.vmap(tensor_math.dev)(Es)
psis = jax.vmap(model.energy)(grad_us)
sigmas = jax.vmap(model.cauchy_stress)(grad_us)
sigmas_an = jax.vmap(
lambda trE, devE, J: (K * trE * jnp.eye(3) + 2. * G * devE) / J, in_axes=(0, 0, 0)
)(trEs, Edevs, Js)

# print(psis)
# print(Es[-1, :, :])
# print(sigmas[-1, :, :])
# print(sigmas_an[-1, :, :])
# # print(sigmas - sigmas_an)
# assert jnp.allclose(sigmas, sigmas_an, atol=1e-8)
# assert False
# for (psi, sigma, gamma, I1_bar, J) in zip(psis, sigmas, gammas, I1_bars, Js):
# psi_an = 0.5 * K * (0.5 * (J**2 - 1) - jnp.log(J)) + \
# -0.5 * G * Jm * jnp.log(1. - (I1_bar - 3.) / Jm)
# sigma_11_an = 2. / 3. * G * Jm * gamma**2 / (Jm - gamma**2)
# sigma_22_an = -1. / 3. * G * Jm * gamma**2 / (Jm - gamma**2)
# sigma_12_an = G * Jm * gamma / (Jm - gamma**2)
# assert jnp.allclose(psi, psi_an)
# assert jnp.allclose(sigma[0, 0], sigma_11_an)
# assert jnp.allclose(sigma[1, 1], sigma_22_an)
# assert jnp.allclose(sigma[2, 2], sigma_22_an)
# # #
# assert jnp.allclose(sigma[0, 1], sigma_12_an)
# assert jnp.allclose(sigma[1, 2], 0.0)
# assert jnp.allclose(sigma[2, 0], 0.0)
# # #
# assert jnp.allclose(sigma[1, 0], sigma_12_an)
# assert jnp.allclose(sigma[2, 1], 0.0)
# assert jnp.allclose(sigma[0, 2], 0.0)


def test_simple_shear(hencky_1, hencky_2):
simple_shear_test(hencky_1)
simple_shear_test(hencky_2)


# def test_uniaxial_strain(gent_1, gent_2):
# uniaxial_strain_test(gent_1)
# uniaxial_strain_test(gent_2)

8 changes: 4 additions & 4 deletions test/constitutive_models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
def uniaxial_strain(lambda_: float):
return jnp.array([
[lambda_, 0., 0.],
[0., 1., 0.],
[0., 0., 1.]
[0., 1., 0.],
[0., 0., 1.]
])


def simple_shear(gamma: float):
return jnp.array([
[1., gamma, 0.],
[0., 1., 0.],
[0., 0., 1.]
[0., 1., 0.],
[0., 0., 1.]
])
Loading