|
5 | 5 | from pathlib import Path |
6 | 6 |
|
7 | 7 | from ase.build import bulk |
| 8 | +import numpy as np |
8 | 9 | from pytest import approx |
9 | 10 |
|
10 | 11 | from janus_core.calculations.elasticity import Elasticity |
11 | 12 | from janus_core.helpers.mlip_calculators import choose_calculator |
| 13 | +from tests.utils import assert_log_contains |
12 | 14 |
|
13 | 15 | DATA_PATH = Path(__file__).parent / "data" |
14 | 16 | MODEL_PATH = Path(__file__).parent / "models" / "mace_mp_small.model" |
15 | 17 |
|
16 | 18 |
|
17 | 19 | def test_calc_elasticity(tmp_path): |
18 | 20 | """Test calculating elasticity for Aluminium.""" |
| 21 | + elasticity_path = tmp_path / "elasticity-elastic_tensor.dat" |
| 22 | + log_file = tmp_path / "elasticity.log" |
| 23 | + |
19 | 24 | struct = bulk("Al", crystalstructure="fcc") |
20 | 25 | struct.calc = choose_calculator(arch="mace_mp", model=MODEL_PATH) |
21 | 26 |
|
22 | | - elasticity = Elasticity(struct, file_prefix=tmp_path / "elasticity") |
| 27 | + elasticity = Elasticity( |
| 28 | + struct, file_prefix=tmp_path / "elasticity", log_kwargs={"filename": log_file} |
| 29 | + ) |
23 | 30 | elastic_tensor = elasticity.run() |
24 | 31 |
|
25 | 32 | assert elastic_tensor.k_reuss == approx(80.34637735135381, rel=1e-3) |
| 33 | + |
| 34 | + # Check geometry optimization run by default |
| 35 | + assert_log_contains( |
| 36 | + log_file, |
| 37 | + includes=["Using filter", "Using optimizer", "Starting geometry optimization"], |
| 38 | + ) |
| 39 | + |
| 40 | + assert elasticity_path.exists() |
| 41 | + written_elasticity = np.loadtxt(elasticity_path) |
| 42 | + # Defaulting to 6x6 Voigt notation |
| 43 | + assert len(written_elasticity) == 45 |
| 44 | + |
| 45 | + for i, prop in enumerate( |
| 46 | + ( |
| 47 | + "k_reuss", |
| 48 | + "k_voigt", |
| 49 | + "k_vrh", |
| 50 | + "g_reuss", |
| 51 | + "g_voigt", |
| 52 | + "g_vrh", |
| 53 | + "y_mod", |
| 54 | + "universal_anisotropy", |
| 55 | + "homogeneous_poisson", |
| 56 | + ) |
| 57 | + ): |
| 58 | + assert elastic_tensor.property_dict[prop] == approx( |
| 59 | + written_elasticity[i], rel=1e-3 |
| 60 | + ) |
| 61 | + |
| 62 | + assert written_elasticity[9:] == approx(elastic_tensor.voigt.flatten(), rel=1e-3) |
| 63 | + |
| 64 | + |
| 65 | +def test_no_optimize_no_write_voigt(tmp_path): |
| 66 | + """Test calculating elasticity for Aluminium without optimization.""" |
| 67 | + elasticity_path = tmp_path / "elasticity-elastic_tensor.dat" |
| 68 | + log_file = tmp_path / "elasticity.log" |
| 69 | + |
| 70 | + struct = bulk("Al", crystalstructure="fcc") |
| 71 | + struct.calc = choose_calculator(arch="mace_mp", model=MODEL_PATH) |
| 72 | + |
| 73 | + elasticity = Elasticity( |
| 74 | + struct, |
| 75 | + file_prefix=tmp_path / "elasticity", |
| 76 | + log_kwargs={"filename": log_file}, |
| 77 | + minimize=False, |
| 78 | + write_voigt=False, |
| 79 | + ) |
| 80 | + elastic_tensor = elasticity.run() |
| 81 | + |
| 82 | + assert elastic_tensor.k_reuss == approx(80.34637735135381, rel=1e-3) |
| 83 | + |
| 84 | + # Check geometry optimization run by default |
| 85 | + assert_log_contains( |
| 86 | + log_file, |
| 87 | + excludes=["Using filter", "Using optimizer", "Starting geometry optimization"], |
| 88 | + ) |
| 89 | + |
| 90 | + assert elasticity_path.exists() |
| 91 | + written_elasticity = np.loadtxt(elasticity_path) |
| 92 | + # Selected to full tensor 3x3x3x3 |
| 93 | + assert len(written_elasticity) == 90 |
| 94 | + |
| 95 | + assert written_elasticity[0] == approx(elastic_tensor.k_reuss, rel=1e-3) |
| 96 | + |
| 97 | + for i, prop in enumerate( |
| 98 | + ( |
| 99 | + "k_reuss", |
| 100 | + "k_voigt", |
| 101 | + "k_vrh", |
| 102 | + "g_reuss", |
| 103 | + "g_voigt", |
| 104 | + "g_vrh", |
| 105 | + "y_mod", |
| 106 | + "universal_anisotropy", |
| 107 | + "homogeneous_poisson", |
| 108 | + ) |
| 109 | + ): |
| 110 | + assert elastic_tensor.property_dict[prop] == approx( |
| 111 | + written_elasticity[i], rel=1e-3 |
| 112 | + ) |
| 113 | + |
| 114 | + assert written_elasticity[9:] == approx(elastic_tensor.flatten(), rel=1e-3) |
0 commit comments