Skip to content

Commit

Permalink
Optionally close figures in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adtzlr committed Feb 7, 2025
1 parent 58c78c7 commit 48d2b03
Showing 1 changed file with 76 additions and 20 deletions.
96 changes: 76 additions & 20 deletions tests/test_constitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def elasticity(x, mu, lmbda):
dsde = linear_elastic.hessian([F, None])


def test_umat_hyperelastic_statevars():
def test_umat_hyperelastic_statevars(close_figs=True):
r, x = pre(sym=False, add_identity=True)
F = x[0]

Expand All @@ -329,8 +329,11 @@ def test_umat_hyperelastic_statevars():
ux = fem.math.linsteps([1, 2, 1], num=10)
ax = umat.plot(ux=ux, bx=None, ps=None, incompressible=True)

if close_figs:
plt.close(ax.get_figure())


def test_umat_hyperelastic():
def test_umat_hyperelastic(close_figs=True):
r, x = pre(sym=False, add_identity=True)
F = x[0]

Expand Down Expand Up @@ -433,18 +436,34 @@ def neo_hooke(C, mu=1):

for incompressible in [False, True]:
ax = umat.plot(incompressible=incompressible)

if close_figs:
plt.close(ax.get_figure())

ax = umat.screenshot(incompressible=incompressible)

if close_figs:
plt.close(ax.get_figure())

umat = fem.Hyperelastic(fem.neo_hooke, mu=np.nan)

with pytest.raises(ValueError):
umat.plot(bx=None, ps=None)
ax = umat.plot(bx=None, ps=None)

if close_figs:
plt.close(ax.get_figure())

with pytest.raises(ValueError):
umat.plot(ux=None, bx=None)
ax = umat.plot(ux=None, bx=None)

if close_figs:
plt.close(ax.get_figure())

with pytest.raises(ValueError):
umat.plot(ux=None, ps=None)
ax = umat.plot(ux=None, ps=None)

if close_figs:
plt.close(ax.get_figure())


def test_umat_hyperelastic2():
Expand Down Expand Up @@ -476,7 +495,7 @@ def neo_hooke(F, mu=1):
assert np.allclose(dsde, dsde2)


def test_umat_viscoelastic():
def test_umat_viscoelastic(close_figs=True):
r, x = pre(sym=False, add_identity=True, add_random=True)
F = x[0]

Expand All @@ -503,13 +522,16 @@ def viscoelastic(C, Cin, mu, eta, dtime):
umat = fem.Hyperelastic(
fem.constitution.finite_strain_viscoelastic, nstatevars=6, **kwargs
)
umat.plot(
ax = umat.plot(
ux=fem.math.linsteps([1, 1.5, 1, 2, 1, 2.5, 1], num=15),
ps=None,
bx=None,
incompressible=True,
)

if close_figs:
plt.close(ax.get_figure())

s2, statevars_new = umat.gradient([F, statevars])
dsde2 = umat.hessian([F, statevars])

Expand Down Expand Up @@ -598,7 +620,7 @@ def test_elpliso():
dsde = umat.hessian([F, statevars])


def test_composite():
def test_composite(close_figs=True):
r, x = pre(sym=False, add_identity=True)
F = x[0]

Expand All @@ -608,11 +630,14 @@ def test_composite():

ax = umat.plot()

if close_figs:
plt.close(ax.get_figure())

dWdF, statevars_new = umat.gradient([F, None])
(d2WdFdF,) = umat.hessian([F, None])


def test_optimize():
def test_optimize(close_figs=True):
stretches, stresses = (
np.array(
[
Expand Down Expand Up @@ -646,11 +671,24 @@ def test_optimize():
* np.array([1.0, 0.0980665])
).T

for model in [
fem.extended_tube,
]:
umat = fem.Hyperelastic(model)
with pytest.warns():
umat_new, res = umat.optimize(ux=[stretches, stresses], incompressible=True)

ux = np.linspace(stretches.min(), stretches.max(), num=200)
ax = umat_new.plot(incompressible=True, ux=ux, bx=None, ps=None)
ax.plot(stretches, stresses, "C0x")

if close_figs:
plt.close(ax.get_figure())

for model in [
fem.neo_hooke,
fem.ogden,
fem.third_order_deformation,
fem.extended_tube,
fem.alexander,
]:
umat = fem.Hyperelastic(model)
Expand All @@ -660,6 +698,9 @@ def test_optimize():
ax = umat_new.plot(incompressible=True, ux=ux, bx=None, ps=None)
ax.plot(stretches, stresses, "C0x")

if close_figs:
plt.close(ax.get_figure())

for model in [
fem.neo_hooke,
fem.miehe_goektepe_lulei,
Expand All @@ -673,6 +714,9 @@ def test_optimize():
ax = umat_new.plot(incompressible=True, ux=ux, bx=None, ps=None)
ax.plot(stretches, stresses, "C0x")

if close_figs:
plt.close(ax.get_figure())


def test_lagrange():
r, x = pre(sym=False, add_identity=True)
Expand Down Expand Up @@ -766,40 +810,52 @@ def test_laplace():
assert A[0].shape == (3, 3, 3, 3, 1, 1)


def test_plot_negative_stretches():
def test_plot_negative_stretches(close_figs=True):
stretches = np.linspace(-0.5, 1, 16)
umat = fem.NeoHooke(mu=1.0, bulk=2.0)

for incompressible in [False, True]:
with pytest.raises(ValueError):
umat.plot(ux=stretches, incompressible=incompressible)
ax = umat.plot(ux=stretches, incompressible=incompressible)

if close_figs:
plt.close(ax.get_figure())

with pytest.raises(ValueError):
umat.plot(bx=stretches, incompressible=incompressible)
ax = umat.plot(bx=stretches, incompressible=incompressible)

if close_figs:
plt.close(ax.get_figure())

with pytest.raises(ValueError):
umat.plot(ps=stretches, incompressible=incompressible)
ax = umat.plot(ps=stretches, incompressible=incompressible)

if close_figs:
plt.close(ax.get_figure())


if __name__ == "__main__":

close_figs = True

test_nh()
test_linear()
test_linear_orthotropic()
test_linear_planestress()
test_linear_planestrain()
test_kinematics()
test_umat()
test_umat_hyperelastic()
test_umat_hyperelastic(close_figs=close_figs)
test_umat_hyperelastic2()
test_umat_hyperelastic_statevars()
test_umat_viscoelastic()
test_umat_hyperelastic_statevars(close_figs=close_figs)
test_umat_viscoelastic(close_figs=close_figs)
test_umat_viscoelastic2()
test_umat_strain()
test_umat_strain_plasticity()
test_elpliso()
test_composite()
test_optimize()
test_composite(close_figs=close_figs)
test_optimize(close_figs=close_figs)
test_lagrange()
test_lagrange_statevars()
test_laplace()
test_plot_negative_stretches()
test_plot_negative_stretches(close_figs=True)

0 comments on commit 48d2b03

Please sign in to comment.