Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
samwaseda committed Aug 5, 2024
1 parent 92fed20 commit 8c75292
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion tests/unit/test_elasticity.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_greens_function(self):
medium = LinearElasticity(create_random_C(isotropic=True))
self.assertAlmostEqual(
medium.get_greens_function([1, 1, 1], isotropic=True)[0, 0],
medium.get_greens_function([1, 1, 1], isotropic=False)[0, 0]
medium.get_greens_function([1, 1, 1], isotropic=False)[0, 0],
)


Expand Down
57 changes: 35 additions & 22 deletions tests/unit/test_eshelby.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
def create_random_HL(b=None):
C = np.zeros((6, 6))
C[:3, :3] = np.random.random()
C[:3, :3] += np.random.random()*np.eye(3)
C[3:, 3:] = np.random.random()*np.eye(3)
C[:3, :3] += np.random.random() * np.eye(3)
C[3:, 3:] = np.random.random() * np.eye(3)
C = tools.C_from_voigt(C)
if b is None:
b = np.random.random(3)
Expand All @@ -20,54 +20,67 @@ def test_p(self):
hl = create_random_HL()
self.assertTrue(
np.allclose(np.absolute(np.linalg.det(hl._get_pmat(hl.p))), 0),
'p-matrix has a full dimension'
"p-matrix has a full dimension",
)

def test_Ak(self):
hl = create_random_HL()
self.assertTrue(
np.allclose(np.absolute(np.einsum('nk,nik->ni', hl.Ak, hl._get_pmat(hl.p))), 0),
'Ak is not the kernel of the p-matrix'
np.allclose(
np.absolute(np.einsum("nk,nik->ni", hl.Ak, hl._get_pmat(hl.p))), 0
),
"Ak is not the kernel of the p-matrix",
)

def test_DAk(self):
hl = create_random_HL()
self.assertTrue(
np.isclose(np.real(np.einsum('nk,n->k', hl.Ak, hl.D)), hl.burgers_vector).all(),
'Magnitude not corresponding to the Burgers vector'
np.isclose(
np.real(np.einsum("nk,n->k", hl.Ak, hl.D)), hl.burgers_vector
).all(),
"Magnitude not corresponding to the Burgers vector",
)

def test_Dq(self):
hl = create_random_HL()
F = np.einsum('n,ij->nij', hl.p, hl.elastic_tensor[:, 1, :, 1])
F = np.einsum("n,ij->nij", hl.p, hl.elastic_tensor[:, 1, :, 1])
F += hl.elastic_tensor[:, 1, :, 0]
F = np.einsum('nik,nk->ni', F, hl.Ak)
F = np.einsum("nik,nk->ni", F, hl.Ak)
self.assertTrue(
np.allclose(np.real(np.einsum('nk,n->k', F, hl.D)), 0),
'Equilibrium condition not satisfied'
np.allclose(np.real(np.einsum("nk,n->k", F, hl.D)), 0),
"Equilibrium condition not satisfied",
)

def test_displacement(self):
hl = create_random_HL(b=[0, 0, 1])
positions = (np.random.random((100, 2))-0.5)*10
d_analytical = np.arctan2(*positions.T[:2][::-1])/2/np.pi
positions = (np.random.random((100, 2)) - 0.5) * 10
d_analytical = np.arctan2(*positions.T[:2][::-1]) / 2 / np.pi
self.assertTrue(
np.all(np.absolute(hl.get_displacement(positions)[:, -1]-d_analytical) < 1.0e-4),
'Screw dislocation displacement field not reproduced'
np.all(
np.absolute(hl.get_displacement(positions)[:, -1] - d_analytical)
< 1.0e-4
),
"Screw dislocation displacement field not reproduced",
)

def test_strain(self):
hl = create_random_HL(b=[0, 0, 1])
positions = (np.random.random((100, 2))-0.5)*10
strain_analytical = positions[:, 0]/np.sum(positions**2, axis=-1)/4/np.pi
positions = (np.random.random((100, 2)) - 0.5) * 10
strain_analytical = positions[:, 0] / np.sum(positions**2, axis=-1) / 4 / np.pi
self.assertTrue(
np.all(np.absolute(hl.get_strain(positions)[:, 1, 2]-strain_analytical) < 1.0e-4),
'Screw dislocation strain field (yz-component) not reproduced'
np.all(
np.absolute(hl.get_strain(positions)[:, 1, 2] - strain_analytical)
< 1.0e-4
),
"Screw dislocation strain field (yz-component) not reproduced",
)
strain_analytical = -positions[:,1]/np.sum(positions**2, axis=-1)/4/np.pi
strain_analytical = -positions[:, 1] / np.sum(positions**2, axis=-1) / 4 / np.pi
self.assertTrue(
np.all(np.absolute(hl.get_strain(positions)[:, 0, 2]-strain_analytical) < 1.0e-4),
'Screw dislocation strain field (xz-component) not reproduced'
np.all(
np.absolute(hl.get_strain(positions)[:, 0, 2] - strain_analytical)
< 1.0e-4
),
"Screw dislocation strain field (xz-component) not reproduced",
)


Expand Down
34 changes: 20 additions & 14 deletions tests/unit/test_green.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def create_random_C(isotropic=False):
coeff_C12 = np.array([0.65797601, -0.0199679])
coeff_C44 = np.array([0.72753844, -0.30418746])
C = np.zeros((6, 6))
C11 = C11_range[0]+np.random.random()*np.ptp(C11_range)
C12 = np.polyval(coeff_C12, C11)+0.2*(np.random.random()-0.5)
C44 = np.polyval(coeff_C44, C11)+0.2*(np.random.random()-0.5)
C11 = C11_range[0] + np.random.random() * np.ptp(C11_range)
C12 = np.polyval(coeff_C12, C11) + 0.2 * (np.random.random() - 0.5)
C44 = np.polyval(coeff_C44, C11) + 0.2 * (np.random.random() - 0.5)
C[:3, :3] = C12
C[:3, :3] += (C11-C12)*np.eye(3)
C[:3, :3] += (C11 - C12) * np.eye(3)
if isotropic:
C[3:, 3:] = np.eye(3)*(C[0, 0]-C[0, 1])/2
C[3:, 3:] = np.eye(3) * (C[0, 0] - C[0, 1]) / 2
else:
C[3:, 3:] = C44*np.eye(3)
C[3:, 3:] = C44 * np.eye(3)
return tools.C_from_voigt(C)


Expand All @@ -28,12 +28,16 @@ def test_derivative(self):
dz = 1.0e-6
index = np.random.randint(3)
positions[1, index] += dz
G_an = aniso.get_greens_function(positions.mean(axis=0), derivative=1)[:, index, :]
G_num = np.diff(aniso.get_greens_function(positions), axis=0)/dz
self.assertTrue(np.isclose(G_num-G_an, 0).all())
G_an = aniso.get_greens_function(positions.mean(axis=0), derivative=2)[:, :, :, index]
G_num = np.diff(aniso.get_greens_function(positions, derivative=1), axis=0)/dz
self.assertTrue(np.isclose(G_num-G_an, 0).all())
G_an = aniso.get_greens_function(positions.mean(axis=0), derivative=1)[
:, index, :
]
G_num = np.diff(aniso.get_greens_function(positions), axis=0) / dz
self.assertTrue(np.isclose(G_num - G_an, 0).all())
G_an = aniso.get_greens_function(positions.mean(axis=0), derivative=2)[
:, :, :, index
]
G_num = np.diff(aniso.get_greens_function(positions, derivative=1), axis=0) / dz
self.assertTrue(np.isclose(G_num - G_an, 0).all())

def test_comp_iso_aniso(self):
shear_modulus = 52.5
Expand All @@ -43,7 +47,9 @@ def test_comp_iso_aniso(self):
C_12 = lame_parameter
C_44 = shear_modulus
iso = Isotropic(poissons_ratio, shear_modulus)
aniso = Anisotropic(tools.C_from_voigt(tools.coeff_to_voigt([C_11, C_12, C_44])))
aniso = Anisotropic(
tools.C_from_voigt(tools.coeff_to_voigt([C_11, C_12, C_44]))
)
x = np.random.randn(100, 3) * 10
for i in range(3):
self.assertLess(
Expand All @@ -52,7 +58,7 @@ def test_comp_iso_aniso(self):
- aniso.get_greens_function(x, derivative=i)
),
1e-8,
msg=f"Aniso- and isotropic Green's F's give different results for derivative={i}"
msg=f"Aniso- and isotropic Green's F's give different results for derivative={i}",
)

def test_memory(self):
Expand Down

0 comments on commit 8c75292

Please sign in to comment.