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

Use copy of x0 in scipy.optimize.newton to prevent side effect #134

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions src/pylife/materiallaws/notch_approximation_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np
from scipy import optimize
import pandas as pd
import copy

import pylife.materiallaws.rambgood

Expand Down Expand Up @@ -135,7 +136,7 @@ def stress(self, load, *, rtol=1e-4, tol=1e-4):
'''
stress = optimize.newton(
func=self._stress_implicit,
x0=load,
x0=copy.copy(load),
fprime=self._d_stress_implicit,
args=([load]),
rtol=rtol, tol=tol, maxiter=20
Expand Down Expand Up @@ -192,10 +193,9 @@ def load(self, stress, *, rtol=1e-4, tol=1e-4):
# => sigma/E + (sigma/K')^(1/n') = (L/sigma * K_p * e_star)
# => (sigma/E + (sigma/K')^(1/n')) / K_p * sigma = L * e_star(L)
# <=> self._ramberg_osgood_relation.strain(stress) / self._K_p * stress = L * e_star(L)

load = optimize.newton(
func=self._load_implicit,
x0=stress,
x0=copy.copy(stress),
fprime=self._d_load_implicit,
args=([stress]),
rtol=rtol, tol=tol, maxiter=20
Expand Down Expand Up @@ -225,7 +225,7 @@ def stress_secondary_branch(self, delta_load, *, rtol=1e-4, tol=1e-4):
'''
delta_stress = optimize.newton(
func=self._stress_secondary_implicit,
x0=delta_load,
x0=copy.copy(delta_load),
fprime=self._d_stress_secondary_implicit,
args=([delta_load]),
rtol=rtol, tol=tol, maxiter=20
Expand Down Expand Up @@ -281,10 +281,9 @@ def load_secondary_branch(self, delta_stress, *, rtol=1e-4, tol=1e-4):
# => sigma/E + (sigma/K')^(1/n') = (L/sigma * K_p * e_star)
# => (sigma/E + (sigma/K')^(1/n')) / K_p * sigma = L * e_star(L)
# <=> self._ramberg_osgood_relation.strain(stress) / self._K_p * stress = L * e_star(L)

delta_load = optimize.newton(
func=self._load_secondary_implicit,
x0=delta_stress,
x0=copy.copy(delta_stress),
fprime=self._d_load_secondary_implicit,
args=([delta_stress]),
rtol=rtol, tol=tol, maxiter=20
Expand Down
22 changes: 20 additions & 2 deletions tests/materiallaws/test_notch_approximation_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_extended_neuber_example_1():
K = 1184 # [MPa]
n = 0.187 # [-]
K_p = 3.5 # [-] (de: Traglastformzahl) K_p = F_plastic / F_yield (3.1.1)

L = pd.Series([100, -200, 100, -250, 200, 0, 200, -200])
c = 1.4
gamma_L = (250+6.6)/250
Expand All @@ -52,7 +52,7 @@ def test_extended_neuber_example_1():
assert np.isclose(maximum_absolute_load, 359.3, rtol=1e-3)

binned_notch_approximation_law = pylife.materiallaws.notch_approximation_law.Binned(
notch_approximation_law, maximum_absolute_load)
notch_approximation_law, maximum_absolute_load, 100)

# some rows of PFAD are given in the FKM nonlinear example on p.76
pd.testing.assert_series_equal(binned_notch_approximation_law._lut_primary_branch.iloc[0], \
Expand All @@ -72,6 +72,8 @@ def test_extended_neuber_example_1():
pd.testing.assert_frame_equal(
binned_notch_approximation_law._lut_secondary_branch, expected_matrix_AST_162, rtol=1e-3, atol=1e-5)

assert np.isclose(binned_notch_approximation_law._lut_primary_branch.load.max(), maximum_absolute_load)


def test_extended_neuber_example_2():
""" example under 2.7.2, p.78 of FKM nonlinear, "Welle mit V-Kerbe" """
Expand Down Expand Up @@ -118,6 +120,22 @@ def test_extended_neuber_example_2():
binned_notch_approximation_law._lut_secondary_branch, expected_matrix_AST_171, rtol=1e-3, atol=1e-5)


def test_extended_neuber_example_no_binning_vectorized():
E = 206e3 # [MPa] Young's modulus
K = 1184 # [MPa]
n = 0.187 # [-]
K_p = 3.5 # [-] (de: Traglastformzahl) K_p = F_plastic / F_yield (3.1.1)

notch_approximation_law = ExtendedNeuber(E, K, n, K_p)

load = np.array([150.0, 175.0, 200.0])
stress = notch_approximation_law.stress(load)
stress_secondary_branch = notch_approximation_law.stress_secondary_branch(load)

np.testing.assert_allclose(stress, np.array([148.463622, 171.674936, 193.702502]), rtol=1e-3)
np.testing.assert_allclose(stress_secondary_branch, np.array([149.92007905, 174.81829204, 199.63066067]), rtol=1e-3)


@pytest.mark.parametrize('stress, load', [
(22, 42),
(40, 40),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def test_seeger_beste_example_1():
pd.testing.assert_frame_equal(
binned_notch_approximation_law._lut_secondary_branch, expected_matrix_AST_162_seeger_beste, rtol=1e-3, atol=1e-5)

assert np.isclose(binned_notch_approximation_law._lut_primary_branch.load.max(), maximum_absolute_load)


def test_seeger_beste_example_2():
""" example under 2.7.2, p.78 of FKM nonlinear, "Welle mit V-Kerbe" """
Expand Down
Loading