Skip to content

Commit

Permalink
Update _load.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ZAARAOUI999 authored Dec 19, 2023
1 parent 314f804 commit c745924
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions hypermat/_calibration/_load.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

####################### - بــسم الله الرحمــان الرحيــم - #####################

"""
HyperMAT
Created August 2023
Expand All @@ -21,38 +24,36 @@

import lmfit as lm
import numpy as np
from scipy.optimize import minimize, fsolve, zeros

import matplotlib.pyplot as plt
import matplotlib

from ._utils import to_dict, init_plot
from .._math import *
from scipy.optimize import fsolve
from ._utils import init_plot

matplotlib.rcParams['font.family'] = ['Times New Roman']
plt.minorticks_on()
plt.gca().grid(which='major', color='#808080')
plt.gca().grid(which='minor', color='#C0C0C0')

SN = {'E':r'Engineering Strain $\epsilon_e$', 'T':'True Strain $\epsilon_t$'}
SS = {'E':'Engineering Stress $\sigma_e$', 'T':'True Stress $\sigma_t$'}
SN = {'E':r'Engineering Strain $\epsilon_e$', r'T':'True Strain $\epsilon_t$'}
SS = {'E':r'Engineering Stress $\sigma_e$', r'T':'True Stress $\sigma_t$'}

class Test():
"""Test class"""
def __init__(self, umat, data, ss_type: str = 'E'):
"""ss_type: E for Engineering and T for True"""
self._umat = umat
self.data = data
self._grad = None
self._ss_type = ss_type
self._label = 'Experimental data'

def _objective(self):
"""Optimization function"""
raise NotImplementedError()

def stress(self):
"""Computes stress tensor"""
if self._ss_type=='E':
_stress = self._grad @ self._umat.jacobian(self._grad)
_stress = self._umat.jacobian(self._grad)
elif self._ss_type=='T':
_stress = np.einsum('...,...ij->...ij', det(self._grad),
self._grad @ self._umat.jacobian(self._grad) @ self._grad)
_stress = np.einsum('...,...ij->...ij', np.det(self._grad),
self._umat.jacobian(self._grad) @ self._grad)
return _stress

def fit_data(self, min_values: list[int|float] = [],
Expand All @@ -70,7 +71,7 @@ def fit_data(self, min_values: list[int|float] = [],

def plot_model(self, **kwargs):
"""Plots model strain-stress curve"""
_stress = self.stress()[...,0,0].ravel()
_stress = self.stress()[0,0,...].ravel()
_strain = self.data['strain']
init_plot()
plt.gca().plot(_strain, _stress, label=self._label, **kwargs)
Expand All @@ -91,12 +92,12 @@ class Uniaxial(Test):
def __init__(self, umat, data, **kwds):
super().__init__(umat, data, **kwds)
self._grad = self.update_grad()
self._label = 'Uniaxial'
self._label = 'Uniaxial (%s)'%(self._umat._label)

def _function(self, x):
"""Computes stress for a given stretch"""
F = self._grad
F[0,:,1,1] = F[0,:,2,2] = x
F[1,1,:,0] = F[2,2,:,0] = x
stress = self.stress()
return stress

Expand All @@ -110,10 +111,10 @@ def _objective(self, params):
else:
self._umat._bulk = params[param].value
def calcS22Abs(x):
return abs(self._function(x)[...,1,1].ravel())
return abs(self._function(x)[1,1,...].ravel())
# search for transverse stretch that gives S22=0
lam2 = fsolve(calcS22Abs, x0=1.0/np.sqrt(lamda))
stress = self._function(lam2)[...,0,0].ravel()
stress = self._function(lam2)[0,0,...].ravel()
return abs(self.data['stress'] - stress)

def update_grad(self):
Expand All @@ -124,19 +125,19 @@ def update_grad(self):
F = np.zeros((1,n,3,3))
F[0,:,0,0] = lamda
F[0,:,1,1] = F[0,:,2,2] = 1.0/np.sqrt(lamda)
return F
return F.T

class EquiBiaxial(Test):
"""Compressible biaxial loading"""
def __init__(self, umat, data, **kwds):
super().__init__(umat, data, **kwds)
self._grad = self.update_grad()
self._label = 'Equibiaxial'
self._label = 'Equibiaxial (%s)'%(self._umat._label)

def _function(self, x):
"""Computes stress for a given stretch"""
F = self._grad
F[0,:,2,2] = x
F[2,2,:,0] = x
stress = self.stress()
return stress

Expand All @@ -150,10 +151,10 @@ def _objective(self, params):
else:
self._umat._bulk = params[param].value
def calcS22Abs(x):
return abs(self._function(x)[...,2,2].ravel())
return abs(self._function(x)[2,2,...].ravel())
# search for transverse stretch that gives S33=0
lam2 = fsolve(calcS22Abs, x0=1.0/np.square(lamda))
stress = self._function(lam2)[...,0,0].ravel()
stress = self._function(lam2)[0,0,...].ravel()
return abs(self.data['stress'] - stress)

def update_grad(self):
Expand All @@ -164,19 +165,19 @@ def update_grad(self):
F = np.zeros((1,n,3,3))
F[0,:,0,0] = F[0,:,1,1] = lamda
F[0,:,2,2] = 1.0/np.square(lamda)
return F
return F.T

class PureShear(Test):
"""Compressible planar loading."""
def __init__(self, umat, data, **kwds):
super().__init__(umat, data, **kwds)
self._grad = self.update_grad()
self._label = 'Pure shear'
self._label = 'Pure shear (%s)'%(self._umat._label)

def _function(self, x):
"""Computes stress for a given stretch"""
F = self._grad
F[0,:,2,2] = x
F[2,2,:,0] = x
stress = self.stress()
return stress

Expand All @@ -190,10 +191,10 @@ def _objective(self, params):
else:
self._umat._bulk = params[param].value
def calcS22Abs(x):
return abs(self._function(x)[...,2,2].ravel())
return abs(self._function(x)[2,2,...].ravel())
# search for transverse stretch that gives S33=0
lam2 = fsolve(calcS22Abs, x0=1.0/lamda)
stress = self._function(lam2)[...,0,0].ravel()
stress = self._function(lam2)[0,0,...].ravel()
return abs(self.data['stress'] - stress)

def update_grad(self):
Expand All @@ -205,4 +206,4 @@ def update_grad(self):
F[0,:,0,0] = lamda
F[0,:,1,1] = 1.0
F[0,:,2,2] = 1.0/lamda
return F
return F.T

0 comments on commit c745924

Please sign in to comment.