adtzlr / matadi

Material Definition with Automatic Differentiation
GNU General Public License v3.0
21 stars 2 forks source link

Fix hyperelastic materials to work with FElupe>5.3.1 #121

Closed adtzlr closed 1 year ago

adtzlr commented 1 year ago

FElupe>5.3.1 calls materials with

# allow (unused) state variables to be passed as the c-
W = umat.function([deformation_gradient, statevars])
P, statevars_new = umat.gradient([deformation_gradient, statevars]) # also returns the updated state variables
A = umat.hessian([deformation_gradient, statevars])

This is just a small change for matADi with a wrapper for the function, gradient and hessian methods for hyperelastic materials.

class MaterialHyperelastic:
    def __init__(self, fun, **kwargs):
        F = Variable("F", 3, 3)
        self.x = [F]
        self.fun = fun
        self.kwargs = kwargs
        self.W = Material(self.x, self._fun_wrapper, kwargs=self.kwargs)
        self.gradient_vector_product = self.W.gradient_vector_product
        self.hessian_vector_product = self.W.hessian_vector_product

    def _fun_wrapper(self, x, **kwargs):
        return self.fun(x[0], **kwargs)

    def function(self, x, *args, **kwargs):
        return self.W.function(x[:1], *args, **kwargs)

    def gradient(self, x, *args, **kwargs):
        return [*self.W.gradient(x[:1], *args, **kwargs), None]

    def hessian(self, x, *args, **kwargs):
        return self.W.hessian(x[:1], *args, **kwargs)