• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

materialsproject / pymatgen / 4075885785

pending completion
4075885785

push

github

Shyue Ping Ong
Merge branch 'master' of github.com:materialsproject/pymatgen

96 of 96 new or added lines in 27 files covered. (100.0%)

81013 of 102710 relevant lines covered (78.88%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.67
/pymatgen/analysis/elasticity/tests/test_elastic.py
1
from __future__ import annotations
1✔
2

3
import json
1✔
4
import os
1✔
5
import random
1✔
6
import unittest
1✔
7
import warnings
1✔
8
from copy import deepcopy
1✔
9

10
import numpy as np
1✔
11
import pytest
1✔
12
from scipy.misc import central_diff_weights
1✔
13

14
from pymatgen.analysis.elasticity.elastic import (
1✔
15
    ComplianceTensor,
16
    ElasticTensor,
17
    ElasticTensorExpansion,
18
    NthOrderElasticTensor,
19
    diff_fit,
20
    find_eq_stress,
21
    generate_pseudo,
22
    get_diff_coeff,
23
    get_strain_state_dict,
24
)
25
from pymatgen.analysis.elasticity.strain import Deformation, Strain
1✔
26
from pymatgen.analysis.elasticity.stress import Stress
1✔
27
from pymatgen.core.lattice import Lattice
1✔
28
from pymatgen.core.structure import Structure
1✔
29
from pymatgen.core.tensors import Tensor
1✔
30
from pymatgen.core.units import FloatWithUnit
1✔
31
from pymatgen.util.testing import PymatgenTest
1✔
32

33

34
class ElasticTensorTest(PymatgenTest):
1✔
35
    def setUp(self):
1✔
36
        self.voigt_1 = [
1✔
37
            [59.33, 28.08, 28.08, 0, 0, 0],
38
            [28.08, 59.31, 28.07, 0, 0, 0],
39
            [28.08, 28.07, 59.32, 0, 0, 0],
40
            [0, 0, 0, 26.35, 0, 0],
41
            [0, 0, 0, 0, 26.35, 0],
42
            [0, 0, 0, 0, 0, 26.35],
43
        ]
44
        mat = np.random.randn(6, 6)
1✔
45
        mat = mat + np.transpose(mat)
1✔
46
        self.rand_elastic_tensor = ElasticTensor.from_voigt(mat)
1✔
47
        self.ft = np.array(
1✔
48
            [
49
                [
50
                    [[59.33, 0, 0], [0, 28.08, 0], [0, 0, 28.08]],
51
                    [[0, 26.35, 0], [26.35, 0, 0], [0, 0, 0]],
52
                    [[0, 0, 26.35], [0, 0, 0], [26.35, 0, 0]],
53
                ],
54
                [
55
                    [[0, 26.35, 0], [26.35, 0, 0], [0, 0, 0]],
56
                    [[28.08, 0, 0], [0, 59.31, 0], [0, 0, 28.07]],
57
                    [[0, 0, 0], [0, 0, 26.35], [0, 26.35, 0]],
58
                ],
59
                [
60
                    [[0, 0, 26.35], [0, 0, 0], [26.35, 0, 0]],
61
                    [[0, 0, 0], [0, 0, 26.35], [0, 26.35, 0]],
62
                    [[28.08, 0, 0], [0, 28.07, 0], [0, 0, 59.32]],
63
                ],
64
            ]
65
        )
66

67
        self.elastic_tensor_1 = ElasticTensor(self.ft)
1✔
68
        filepath = os.path.join(PymatgenTest.TEST_FILES_DIR, "Sn_def_stress.json")
1✔
69
        with open(filepath) as f:
1✔
70
            self.def_stress_dict = json.load(f)
1✔
71
        with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "test_toec_data.json")) as f:
1✔
72
            self.toec_dict = json.load(f)
1✔
73
        self.structure = self.get_structure("Sn")
1✔
74

75
        warnings.simplefilter("always")
1✔
76

77
    def test_properties(self):
1✔
78
        # compliance tensor
79
        ct = ComplianceTensor.from_voigt(np.linalg.inv(self.elastic_tensor_1.voigt))
1✔
80
        self.assertArrayAlmostEqual(ct, self.elastic_tensor_1.compliance_tensor)
1✔
81
        # KG average properties
82
        assert self.elastic_tensor_1.k_voigt == pytest.approx(38.49111111111)
1✔
83
        assert self.elastic_tensor_1.g_voigt == pytest.approx(22.05866666666)
1✔
84
        assert self.elastic_tensor_1.k_reuss == pytest.approx(38.49110945133)
1✔
85
        assert self.elastic_tensor_1.g_reuss == pytest.approx(20.67146635306)
1✔
86
        assert self.elastic_tensor_1.k_vrh == pytest.approx(38.49111028122)
1✔
87
        assert self.elastic_tensor_1.g_vrh == pytest.approx(21.36506650986)
1✔
88

89
        # universal anisotropy
90
        assert self.elastic_tensor_1.universal_anisotropy == pytest.approx(0.33553509658699)
1✔
91
        # homogeneous Poisson
92
        assert self.elastic_tensor_1.homogeneous_poisson == pytest.approx(0.26579965576472)
1✔
93
        # voigt notation tensor
94
        self.assertArrayAlmostEqual(self.elastic_tensor_1.voigt, self.voigt_1)
1✔
95
        # young's modulus
96
        assert self.elastic_tensor_1.y_mod == pytest.approx(54087787667.160583)
1✔
97

98
        # prop dict
99
        prop_dict = self.elastic_tensor_1.property_dict
1✔
100
        assert prop_dict["homogeneous_poisson"] == pytest.approx(0.26579965576)
1✔
101
        for k, v in prop_dict.items():
1✔
102
            assert getattr(self.elastic_tensor_1, k) == pytest.approx(v)
1✔
103

104
    def test_directional_elastic_mod(self):
1✔
105
        assert self.elastic_tensor_1.directional_elastic_mod([1, 0, 0]) == pytest.approx(
1✔
106
            self.elastic_tensor_1.voigt[0, 0]
107
        )
108
        assert self.elastic_tensor_1.directional_elastic_mod([1, 1, 1]) == pytest.approx(73.624444444)
1✔
109

110
    def test_compliance_tensor(self):
1✔
111
        stress = self.elastic_tensor_1.calculate_stress([0.01] + [0] * 5)
1✔
112
        comp = self.elastic_tensor_1.compliance_tensor
1✔
113
        strain = Strain(comp.einsum_sequence([stress]))
1✔
114
        self.assertArrayAlmostEqual(strain.voigt, [0.01] + [0] * 5)
1✔
115

116
    def test_directional_poisson_ratio(self):
1✔
117
        v_12 = self.elastic_tensor_1.directional_poisson_ratio([1, 0, 0], [0, 1, 0])
1✔
118
        assert v_12 == pytest.approx(0.321388)
1✔
119

120
    def test_structure_based_methods(self):
1✔
121
        # trans_velocity
122
        assert self.elastic_tensor_1.trans_v(self.structure) == pytest.approx(1996.35019877)
1✔
123
        # long_velocity
124
        assert self.elastic_tensor_1.long_v(self.structure) == pytest.approx(3534.68123832)
1✔
125
        # Snyder properties
126
        assert self.elastic_tensor_1.snyder_ac(self.structure) == pytest.approx(18.06127074)
1✔
127
        assert self.elastic_tensor_1.snyder_opt(self.structure) == pytest.approx(0.18937465)
1✔
128
        assert self.elastic_tensor_1.snyder_total(self.structure) == pytest.approx(18.25064540)
1✔
129
        # Clarke
130
        assert self.elastic_tensor_1.clarke_thermalcond(self.structure) == pytest.approx(0.3450307)
1✔
131
        # Cahill
132
        assert self.elastic_tensor_1.cahill_thermalcond(self.structure) == pytest.approx(0.37896275)
1✔
133
        # Debye
134
        assert self.elastic_tensor_1.debye_temperature(self.structure) == pytest.approx(198.8037985019)
1✔
135

136
        # structure-property dict
137
        sprop_dict = self.elastic_tensor_1.get_structure_property_dict(self.structure)
1✔
138
        assert sprop_dict["long_v"] == pytest.approx(3534.68123832)
1✔
139
        for val in sprop_dict.values():
1✔
140
            assert not isinstance(val, FloatWithUnit)
1✔
141
        for k, v in sprop_dict.items():
1✔
142
            if k == "structure":
1✔
143
                assert v == self.structure
1✔
144
            else:
145
                f = getattr(self.elastic_tensor_1, k)
1✔
146
                if callable(f):
1✔
147
                    assert getattr(self.elastic_tensor_1, k)(self.structure) == pytest.approx(v)
1✔
148
                else:
149
                    assert getattr(self.elastic_tensor_1, k) == pytest.approx(v)
1✔
150

151
        # Test other sprop dict modes
152
        sprop_dict = self.elastic_tensor_1.get_structure_property_dict(self.structure, include_base_props=False)
1✔
153
        assert "k_vrh" not in sprop_dict
1✔
154

155
        # Test ValueError being raised for structure properties
156
        test_et = deepcopy(self.elastic_tensor_1)
1✔
157
        test_et[0][0][0][0] = -100000
1✔
158
        prop_dict = test_et.property_dict
1✔
159
        for attr_name in sprop_dict:
1✔
160
            if attr_name not in (list(prop_dict) + ["structure"]):
1✔
161
                with pytest.raises(ValueError):
1✔
162
                    getattr(test_et, attr_name)(self.structure)
1✔
163
        with pytest.raises(ValueError):
1✔
164
            test_et.get_structure_property_dict(self.structure)
1✔
165
        noval_sprop_dict = test_et.get_structure_property_dict(self.structure, ignore_errors=True)
1✔
166
        assert noval_sprop_dict["snyder_ac"] is None
1✔
167

168
    def test_new(self):
1✔
169
        self.assertArrayAlmostEqual(self.elastic_tensor_1, ElasticTensor(self.ft))
1✔
170
        nonsymm = self.ft
1✔
171
        nonsymm[0, 1, 2, 2] += 1.0
1✔
172
        with warnings.catch_warnings(record=True) as w:
1✔
173
            ElasticTensor(nonsymm)
1✔
174
            assert len(w) == 1
1✔
175
        badtensor1 = np.zeros((3, 3, 3))
1✔
176
        badtensor2 = np.zeros((3, 3, 3, 2))
1✔
177
        with pytest.raises(ValueError):
1✔
178
            ElasticTensor(badtensor1)
1✔
179
        with pytest.raises(ValueError):
1✔
180
            ElasticTensor(badtensor2)
1✔
181

182
    def test_from_pseudoinverse(self):
1✔
183
        strain_list = [Strain.from_deformation(def_matrix) for def_matrix in self.def_stress_dict["deformations"]]
1✔
184
        stress_list = [stress for stress in self.def_stress_dict["stresses"]]
1✔
185
        with warnings.catch_warnings(record=True):
1✔
186
            et_fl = -0.1 * ElasticTensor.from_pseudoinverse(strain_list, stress_list).voigt
1✔
187
            self.assertArrayAlmostEqual(
1✔
188
                et_fl.round(2),
189
                [
190
                    [59.29, 24.36, 22.46, 0, 0, 0],
191
                    [28.06, 56.91, 22.46, 0, 0, 0],
192
                    [28.06, 25.98, 54.67, 0, 0, 0],
193
                    [0, 0, 0, 26.35, 0, 0],
194
                    [0, 0, 0, 0, 26.35, 0],
195
                    [0, 0, 0, 0, 0, 26.35],
196
                ],
197
            )
198

199
    def test_from_independent_strains(self):
1✔
200
        strains = self.toec_dict["strains"]
1✔
201
        stresses = self.toec_dict["stresses"]
1✔
202
        with warnings.catch_warnings(record=True):
1✔
203
            et = ElasticTensor.from_independent_strains(strains, stresses)
1✔
204
        self.assertArrayAlmostEqual(et.voigt, self.toec_dict["C2_raw"], decimal=-1)
1✔
205

206
    def test_energy_density(self):
1✔
207
        film_elac = ElasticTensor.from_voigt(
1✔
208
            [
209
                [324.32, 187.3, 170.92, 0.0, 0.0, 0.0],
210
                [187.3, 324.32, 170.92, 0.0, 0.0, 0.0],
211
                [170.92, 170.92, 408.41, 0.0, 0.0, 0.0],
212
                [0.0, 0.0, 0.0, 150.73, 0.0, 0.0],
213
                [0.0, 0.0, 0.0, 0.0, 150.73, 0.0],
214
                [0.0, 0.0, 0.0, 0.0, 0.0, 238.74],
215
            ]
216
        )
217

218
        dfm = Deformation(
1✔
219
            [
220
                [-9.86004855e-01, 2.27539582e-01, -4.64426035e-17],
221
                [-2.47802121e-01, -9.91208483e-01, -7.58675185e-17],
222
                [-6.12323400e-17, -6.12323400e-17, 1.00000000e00],
223
            ]
224
        )
225

226
        assert film_elac.energy_density(dfm.green_lagrange_strain) == pytest.approx(0.00125664672793)
1✔
227

228
        film_elac.energy_density(
1✔
229
            Strain.from_deformation(
230
                [
231
                    [0.99774738, 0.11520994, -0.0],
232
                    [-0.11520994, 0.99774738, 0.0],
233
                    [
234
                        -0.0,
235
                        -0.0,
236
                        1.0,
237
                    ],
238
                ]
239
            )
240
        )
241

242

243
class ElasticTensorExpansionTest(PymatgenTest):
1✔
244
    def setUp(self):
1✔
245
        with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "test_toec_data.json")) as f:
1✔
246
            self.data_dict = json.load(f)
1✔
247
        self.strains = [Strain(sm) for sm in self.data_dict["strains"]]
1✔
248
        self.pk_stresses = [Stress(d) for d in self.data_dict["pk_stresses"]]
1✔
249
        self.c2 = self.data_dict["C2_raw"]
1✔
250
        self.c3 = self.data_dict["C3_raw"]
1✔
251
        self.exp = ElasticTensorExpansion.from_voigt([self.c2, self.c3])
1✔
252
        self.cu = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3.623), ["Cu"], [[0] * 3])
1✔
253
        indices = [(0, 0), (0, 1), (3, 3)]
1✔
254
        values = [167.8, 113.5, 74.5]
1✔
255
        cu_c2 = ElasticTensor.from_values_indices(values, indices, structure=self.cu, populate=True)
1✔
256
        indices = [(0, 0, 0), (0, 0, 1), (0, 1, 2), (0, 3, 3), (0, 5, 5), (3, 4, 5)]
1✔
257
        values = [-1507.0, -965.0, -71.0, -7.0, -901.0, 45.0]
1✔
258
        cu_c3 = Tensor.from_values_indices(values, indices, structure=self.cu, populate=True)
1✔
259
        self.exp_cu = ElasticTensorExpansion([cu_c2, cu_c3])
1✔
260
        cu_c4 = Tensor.from_voigt(self.data_dict["Cu_fourth_order"])
1✔
261
        self.exp_cu_4 = ElasticTensorExpansion([cu_c2, cu_c3, cu_c4])
1✔
262
        warnings.simplefilter("ignore")
1✔
263

264
    def tearDown(self):
1✔
265
        warnings.simplefilter("default")
1✔
266

267
    def test_init(self):
1✔
268
        cijkl = Tensor.from_voigt(self.c2)
1✔
269
        cijklmn = Tensor.from_voigt(self.c3)
1✔
270
        exp = ElasticTensorExpansion([cijkl, cijklmn])
1✔
271
        ElasticTensorExpansion.from_voigt([self.c2, self.c3])
1✔
272
        assert exp.order == 3
1✔
273

274
    def test_from_diff_fit(self):
1✔
275
        ElasticTensorExpansion.from_diff_fit(self.strains, self.pk_stresses)
1✔
276

277
    def test_calculate_stress(self):
1✔
278
        calc_stress = self.exp.calculate_stress(self.strains[0])
1✔
279
        self.assertArrayAlmostEqual(self.pk_stresses[0], calc_stress, decimal=2)
1✔
280

281
    def test_energy_density(self):
1✔
282
        e_density = self.exp.energy_density(self.strains[0])
1✔
283
        assert e_density == pytest.approx(1.36363099e-4)
1✔
284

285
    def test_gruneisen(self):
1✔
286
        # Get GGT
287
        ggt = self.exp_cu.get_ggt([1, 0, 0], [0, 1, 0])
1✔
288
        self.assertArrayAlmostEqual(np.eye(3) * np.array([4.92080537, 4.2852349, -0.7147651]), ggt)
1✔
289
        # Get TGT
290
        tgt = self.exp_cu.get_tgt()
1✔
291
        self.assertArrayAlmostEqual(tgt, np.eye(3) * 2.59631832)
1✔
292

293
        # Get heat capacity
294
        c0 = self.exp_cu.get_heat_capacity(0, self.cu, [1, 0, 0], [0, 1, 0])
1✔
295
        assert c0 == 0.0
1✔
296
        c = self.exp_cu.get_heat_capacity(300, self.cu, [1, 0, 0], [0, 1, 0])
1✔
297
        assert c == pytest.approx(8.285611958)
1✔
298

299
        # Get Gruneisen parameter
300
        gp = self.exp_cu.get_gruneisen_parameter()
1✔
301
        assert gp == pytest.approx(2.59631832)
1✔
302
        _ = self.exp_cu.get_gruneisen_parameter(temperature=200, structure=self.cu)
1✔
303

304
    def test_thermal_expansion_coeff(self):
1✔
305
        # TODO get rid of duplicates
306
        alpha_dp = self.exp_cu.thermal_expansion_coeff(self.cu, 300, mode="dulong-petit")
1✔
307
        alpha_dp_ground_truth = 6.3471959e-07 * np.ones((3, 3))
1✔
308
        alpha_dp_ground_truth[np.diag_indices(3)] = 2.2875769e-7
1✔
309
        self.assertArrayAlmostEqual(alpha_dp_ground_truth, alpha_dp, decimal=4)
1✔
310

311
        alpha_debye = self.exp_cu.thermal_expansion_coeff(self.cu, 300, mode="debye")
1✔
312
        alpha_comp = 5.9435148e-7 * np.ones((3, 3))
1✔
313
        alpha_comp[np.diag_indices(3)] = 21.4533472e-06
1✔
314
        self.assertArrayAlmostEqual(alpha_comp, alpha_debye)
1✔
315

316
    def test_get_compliance_expansion(self):
1✔
317
        ce_exp = self.exp_cu.get_compliance_expansion()
1✔
318
        et_comp = ElasticTensorExpansion(ce_exp)
1✔
319
        strain_orig = Strain.from_voigt([0.01, 0, 0, 0, 0, 0])
1✔
320
        stress = self.exp_cu.calculate_stress(strain_orig)
1✔
321
        strain_revert = et_comp.calculate_stress(stress)
1✔
322
        self.assertArrayAlmostEqual(strain_orig, strain_revert, decimal=4)
1✔
323

324
    def test_get_effective_ecs(self):
1✔
325
        # Ensure zero strain is same as SOEC
326
        test_zero = self.exp_cu.get_effective_ecs(np.zeros((3, 3)))
1✔
327
        self.assertArrayAlmostEqual(test_zero, self.exp_cu[0])
1✔
328
        s = np.zeros((3, 3))
1✔
329
        s[0, 0] = 0.02
1✔
330
        test_2percent = self.exp_cu.get_effective_ecs(s)
1✔
331
        diff = test_2percent - test_zero
1✔
332
        self.assertArrayAlmostEqual(self.exp_cu[1].einsum_sequence([s]), diff)
1✔
333

334
    def test_get_strain_from_stress(self):
1✔
335
        strain = Strain.from_voigt([0.05, 0, 0, 0, 0, 0])
1✔
336
        stress3 = self.exp_cu.calculate_stress(strain)
1✔
337
        strain_revert3 = self.exp_cu.get_strain_from_stress(stress3)
1✔
338
        self.assertArrayAlmostEqual(strain, strain_revert3, decimal=2)
1✔
339
        # fourth order
340
        stress4 = self.exp_cu_4.calculate_stress(strain)
1✔
341
        strain_revert4 = self.exp_cu_4.get_strain_from_stress(stress4)
1✔
342
        self.assertArrayAlmostEqual(strain, strain_revert4, decimal=2)
1✔
343

344
    def test_get_yield_stress(self):
1✔
345
        self.exp_cu_4.get_yield_stress([1, 0, 0])
1✔
346

347

348
class NthOrderElasticTensorTest(PymatgenTest):
1✔
349
    def setUp(self):
1✔
350
        with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "test_toec_data.json")) as f:
1✔
351
            self.data_dict = json.load(f)
1✔
352
        self.strains = [Strain(sm) for sm in self.data_dict["strains"]]
1✔
353
        self.pk_stresses = [Stress(d) for d in self.data_dict["pk_stresses"]]
1✔
354
        self.c2 = NthOrderElasticTensor.from_voigt(self.data_dict["C2_raw"])
1✔
355
        self.c3 = NthOrderElasticTensor.from_voigt(self.data_dict["C3_raw"])
1✔
356

357
    def test_init(self):
1✔
358
        c2 = NthOrderElasticTensor(self.c2.tolist())
1✔
359
        c3 = NthOrderElasticTensor(self.c3.tolist())
1✔
360
        c4 = NthOrderElasticTensor(np.zeros([3] * 8))
1✔
361
        for n, c in enumerate([c2, c3, c4]):
1✔
362
            assert c.order == n + 2
1✔
363
        with pytest.raises(ValueError):
1✔
364
            NthOrderElasticTensor(np.zeros([3] * 5))
1✔
365

366
    def test_from_diff_fit(self):
1✔
367
        c3 = NthOrderElasticTensor.from_diff_fit(
1✔
368
            self.strains,
369
            self.pk_stresses,
370
            eq_stress=self.data_dict["eq_stress"],
371
            order=3,
372
        )
373
        self.assertArrayAlmostEqual(c3.voigt, self.data_dict["C3_raw"], decimal=2)
1✔
374

375
    def test_calculate_stress(self):
1✔
376
        calc_stress = self.c2.calculate_stress(self.strains[0])
1✔
377
        self.assertArrayAlmostEqual(self.pk_stresses[0], calc_stress, decimal=0)
1✔
378
        # Test calculation from voigt strain
379
        self.c2.calculate_stress(self.strains[0].voigt)
1✔
380

381
    def test_energy_density(self):
1✔
382
        self.c3.energy_density(self.strains[0])
1✔
383

384

385
class DiffFitTest(PymatgenTest):
1✔
386
    """
387
    Tests various functions related to diff fitting
388
    """
389

390
    def setUp(self):
1✔
391
        with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "test_toec_data.json")) as f:
1✔
392
            self.data_dict = json.load(f)
1✔
393
        self.strains = [Strain(sm) for sm in self.data_dict["strains"]]
1✔
394
        self.pk_stresses = [Stress(d) for d in self.data_dict["pk_stresses"]]
1✔
395

396
    def test_get_strain_state_dict(self):
1✔
397
        strain_inds = [(0,), (1,), (2,), (1, 3), (1, 2, 3)]
1✔
398
        vecs = {}
1✔
399
        strain_states = []
1✔
400
        for strain_ind in strain_inds:
1✔
401
            ss = np.zeros(6)
1✔
402
            np.put(ss, strain_ind, 1)
1✔
403
            strain_states.append(tuple(ss))
1✔
404
            vec = np.zeros((4, 6))
1✔
405
            rand_values = np.random.uniform(0.1, 1, 4)
1✔
406
            for i in strain_ind:
1✔
407
                vec[:, i] = rand_values
1✔
408
            vecs[strain_ind] = vec
1✔
409
        all_strains = [Strain.from_voigt(v).zeroed() for vec in vecs.values() for v in vec]
1✔
410
        random.shuffle(all_strains)
1✔
411
        all_stresses = [Stress.from_voigt(np.random.random(6)).zeroed() for s in all_strains]
1✔
412
        strain_dict = {k.tobytes(): v for k, v in zip(all_strains, all_stresses)}
1✔
413
        ss_dict = get_strain_state_dict(all_strains, all_stresses, add_eq=False)
1✔
414
        # Check length of ss_dict
415
        assert len(strain_inds) == len(ss_dict)
1✔
416
        # Check sets of strain states are correct
417
        assert set(strain_states) == set(ss_dict)
1✔
418
        for data in ss_dict.values():
1✔
419
            # Check correspondence of strains/stresses
420
            for strain, stress in zip(data["strains"], data["stresses"]):
1✔
421
                self.assertArrayAlmostEqual(
1✔
422
                    Stress.from_voigt(stress),
423
                    strain_dict[Strain.from_voigt(strain).tobytes()],
424
                )
425
        # Add test to ensure zero strain state doesn't cause issue
426
        strains, stresses = [Strain.from_voigt([-0.01] + [0] * 5)], [Stress(np.eye(3))]
1✔
427
        ss_dict = get_strain_state_dict(strains, stresses)
1✔
428
        self.assertArrayAlmostEqual(list(ss_dict), [[1, 0, 0, 0, 0, 0]])
1✔
429

430
    def test_find_eq_stress(self):
1✔
431
        test_strains = deepcopy(self.strains)
1✔
432
        test_stresses = deepcopy(self.pk_stresses)
1✔
433
        with warnings.catch_warnings(record=True):
1✔
434
            no_eq = find_eq_stress(test_strains, test_stresses)
1✔
435
            self.assertArrayAlmostEqual(no_eq, np.zeros((3, 3)))
1✔
436
        test_strains[3] = Strain.from_voigt(np.zeros(6))
1✔
437
        eq_stress = find_eq_stress(test_strains, test_stresses)
1✔
438
        self.assertArrayAlmostEqual(test_stresses[3], eq_stress)
1✔
439

440
    def test_get_diff_coeff(self):
1✔
441
        forward_11 = get_diff_coeff([0, 1], 1)
1✔
442
        forward_13 = get_diff_coeff([0, 1, 2, 3], 1)
1✔
443
        backward_26 = get_diff_coeff(np.arange(-6, 1), 2)
1✔
444
        central_29 = get_diff_coeff(np.arange(-4, 5), 2)
1✔
445
        self.assertArrayAlmostEqual(forward_11, [-1, 1])
1✔
446
        self.assertArrayAlmostEqual(forward_13, [-11.0 / 6, 3, -3.0 / 2, 1.0 / 3])
1✔
447
        self.assertArrayAlmostEqual(
1✔
448
            backward_26,
449
            [
450
                137.0 / 180,
451
                -27.0 / 5,
452
                33.0 / 2,
453
                -254.0 / 9,
454
                117.0 / 4,
455
                -87.0 / 5,
456
                203.0 / 45,
457
            ],
458
        )
459
        self.assertArrayAlmostEqual(central_29, central_diff_weights(9, 2))
1✔
460

461
    def test_generate_pseudo(self):
1✔
462
        strain_states = np.eye(6).tolist()
1✔
463
        m2, abs = generate_pseudo(strain_states, order=2)
1✔
464
        m3, abs = generate_pseudo(strain_states, order=3)
1✔
465
        m4, abs = generate_pseudo(strain_states, order=4)
1✔
466

467
    def test_fit(self):
1✔
468
        diff_fit(self.strains, self.pk_stresses, self.data_dict["eq_stress"])
1✔
469
        reduced = [(e, pk) for e, pk in zip(self.strains, self.pk_stresses) if not (abs(abs(e) - 0.05) < 1e-10).any()]
1✔
470
        # Get reduced dataset
471
        r_strains, r_pk_stresses = zip(*reduced)
1✔
472
        with warnings.catch_warnings(record=True):
1✔
473
            c2 = diff_fit(r_strains, r_pk_stresses, self.data_dict["eq_stress"], order=2)
1✔
474
            c2, c3, c4 = diff_fit(r_strains, r_pk_stresses, self.data_dict["eq_stress"], order=4)
1✔
475
            c2, c3 = diff_fit(self.strains, self.pk_stresses, self.data_dict["eq_stress"], order=3)
1✔
476
            c2_red, c3_red = diff_fit(r_strains, r_pk_stresses, self.data_dict["eq_stress"], order=3)
1✔
477
            self.assertArrayAlmostEqual(c2.voigt, self.data_dict["C2_raw"])
1✔
478
            self.assertArrayAlmostEqual(c3.voigt, self.data_dict["C3_raw"], decimal=5)
1✔
479
            self.assertArrayAlmostEqual(c2, c2_red, decimal=0)
1✔
480
            self.assertArrayAlmostEqual(c3, c3_red, decimal=-1)
1✔
481

482

483
if __name__ == "__main__":
1✔
484
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc