tests.test_config

PyDRex: tests for configuration file format.

  1"""> PyDRex: tests for configuration file format."""
  2
  3import pathlib as pl
  4
  5import numpy as np
  6from pydrex import core as _core
  7from pydrex import io as _io
  8from pydrex import velocity as _velocity
  9
 10
 11def test_steady_specfile():
 12    """Test TOML spec file for case of input mesh with steady velocity field."""
 13    config = _io.parse_config(_io.data("specs") / "steady_mesh.toml")
 14    assert list(config.keys()) == ["name", "input", "output", "parameters"]
 15    assert config["name"] == "pydrex-spec-steady-mesh"
 16    _input = config["input"]
 17    _mesh = _input["mesh"]
 18    assert _mesh.points.shape == (1705, 3)
 19    assert list(_mesh.point_data.keys()) == [
 20        "Pressure",
 21        "Time",
 22        "Velocity",
 23        "VelocityGradient",
 24    ]
 25    assert _mesh.cells[0].type == "triangle"
 26    assert _input["locations_final"].X == (500000.0, 500000.0, 500000.0, 500000.0)
 27    assert _input["locations_final"].Z == (-20000.0, -40000.0, -60000.0, -80000.0)
 28    assert _input["strain_final"] == 2.5
 29    _output = config["output"]
 30    assert _output["directory"].name == "out_steady"
 31    assert _output["raw_output"] == [
 32        _core.MineralPhase.olivine,
 33        _core.MineralPhase.enstatite,
 34    ]
 35    assert _output["diagnostics"] == [
 36        _core.MineralPhase.olivine,
 37        _core.MineralPhase.enstatite,
 38    ]
 39    assert _output["anisotropy"] == ["Voigt", "hexaxis", "moduli", "%decomp"]
 40    assert _output["log_level"] == "DEBUG"
 41    assert config["parameters"] == {
 42        "phase_assemblage": (_core.MineralPhase.olivine, _core.MineralPhase.enstatite),
 43        "phase_fractions": [0.7, 0.3],
 44        "initial_olivine_fabric": _core.MineralFabric.olivine_A,
 45        "stress_exponent": 1.5,
 46        "deformation_exponent": 3.5,
 47        "gbm_mobility": 125,
 48        "gbs_threshold": 0.3,
 49        "nucleation_efficiency": 5.0,
 50        "number_of_grains": 5000,
 51        "disl_Peierls_stress": 2,
 52        "disl_prefactors": (1e-16, 1e-17),
 53        "diff_prefactors": (1e-10, 1e-10),
 54        "disl_lowtemp_switch": 0.7,
 55        "disl_activation_energy": 460.0,
 56        "disl_activation_volume": 12.0,
 57        "diff_activation_energies": (430.0, 330.0),
 58        "diff_activation_volumes": (4.0, 4.0),
 59        "disl_coefficients": (
 60            4.4e8,
 61            -5.26e4,
 62            2.11e-2,
 63            1.74e-4,
 64            -41.8,
 65            4.21e-2,
 66            -1.14e-5,
 67        ),
 68    }
 69
 70
 71def test_specfile():
 72    """Test TOML spec file parsing."""
 73    config = _io.parse_config(_io.data("specs") / "spec.toml")
 74    assert list(config.keys()) == ["name", "input", "output", "parameters"]
 75    _input = config["input"]
 76    # TODO: Add some example mesh/path files and use them in another test.
 77    assert _input["mesh"] is None
 78    assert _input["locations_final"] is None
 79    assert (
 80        _input["velocity_gradient"][1].func
 81        == _velocity.simple_shear_2d("Y", "X", 5e-6)[1].func
 82    )
 83    assert (
 84        _input["velocity_gradient"][1].keywords
 85        == _velocity.simple_shear_2d("Y", "X", 5e-6)[1].keywords
 86    )
 87    assert _input["locations_initial"] == _io.read_scsv(
 88        _io.data("specs") / "start.scsv"
 89    )
 90    assert _input["timestep"] == 1e9
 91    assert _input["paths"] is None
 92    outdir = pl.Path(_io.data("specs") / "out").resolve()
 93    _output = config["output"]
 94    assert _output["directory"] == outdir
 95    np.testing.assert_array_equal(_output["raw_output"], [_core.MineralPhase.olivine])
 96    np.testing.assert_array_equal(_output["diagnostics"], [_core.MineralPhase.olivine])
 97    assert _output["anisotropy"] is True
 98    assert _output["paths"] is None
 99    assert _output["log_level"] == "DEBUG"
100    assert config["parameters"] == {
101        "phase_assemblage": (_core.MineralPhase.olivine,),
102        "phase_fractions": (1.0,),
103        "initial_olivine_fabric": _core.MineralFabric.olivine_A,
104        "stress_exponent": 1.5,
105        "deformation_exponent": 3.5,
106        "gbm_mobility": 125,
107        "gbs_threshold": 0.3,
108        "nucleation_efficiency": 5.0,
109        "number_of_grains": 2000,
110        "disl_Peierls_stress": 2,
111        "disl_prefactors": (1e-16, 1e-17),
112        "diff_prefactors": (1e-10, 1e-10),
113        "disl_lowtemp_switch": 0.7,
114        "disl_activation_energy": 460.0,
115        "disl_activation_volume": 12.0,
116        "diff_activation_energies": (430.0, 330.0),
117        "diff_activation_volumes": (4.0, 4.0),
118        "disl_coefficients": (
119            4.4e8,
120            -5.26e4,
121            2.11e-2,
122            1.74e-4,
123            -41.8,
124            4.21e-2,
125            -1.14e-5,
126        ),
127    }
def test_steady_specfile():
12def test_steady_specfile():
13    """Test TOML spec file for case of input mesh with steady velocity field."""
14    config = _io.parse_config(_io.data("specs") / "steady_mesh.toml")
15    assert list(config.keys()) == ["name", "input", "output", "parameters"]
16    assert config["name"] == "pydrex-spec-steady-mesh"
17    _input = config["input"]
18    _mesh = _input["mesh"]
19    assert _mesh.points.shape == (1705, 3)
20    assert list(_mesh.point_data.keys()) == [
21        "Pressure",
22        "Time",
23        "Velocity",
24        "VelocityGradient",
25    ]
26    assert _mesh.cells[0].type == "triangle"
27    assert _input["locations_final"].X == (500000.0, 500000.0, 500000.0, 500000.0)
28    assert _input["locations_final"].Z == (-20000.0, -40000.0, -60000.0, -80000.0)
29    assert _input["strain_final"] == 2.5
30    _output = config["output"]
31    assert _output["directory"].name == "out_steady"
32    assert _output["raw_output"] == [
33        _core.MineralPhase.olivine,
34        _core.MineralPhase.enstatite,
35    ]
36    assert _output["diagnostics"] == [
37        _core.MineralPhase.olivine,
38        _core.MineralPhase.enstatite,
39    ]
40    assert _output["anisotropy"] == ["Voigt", "hexaxis", "moduli", "%decomp"]
41    assert _output["log_level"] == "DEBUG"
42    assert config["parameters"] == {
43        "phase_assemblage": (_core.MineralPhase.olivine, _core.MineralPhase.enstatite),
44        "phase_fractions": [0.7, 0.3],
45        "initial_olivine_fabric": _core.MineralFabric.olivine_A,
46        "stress_exponent": 1.5,
47        "deformation_exponent": 3.5,
48        "gbm_mobility": 125,
49        "gbs_threshold": 0.3,
50        "nucleation_efficiency": 5.0,
51        "number_of_grains": 5000,
52        "disl_Peierls_stress": 2,
53        "disl_prefactors": (1e-16, 1e-17),
54        "diff_prefactors": (1e-10, 1e-10),
55        "disl_lowtemp_switch": 0.7,
56        "disl_activation_energy": 460.0,
57        "disl_activation_volume": 12.0,
58        "diff_activation_energies": (430.0, 330.0),
59        "diff_activation_volumes": (4.0, 4.0),
60        "disl_coefficients": (
61            4.4e8,
62            -5.26e4,
63            2.11e-2,
64            1.74e-4,
65            -41.8,
66            4.21e-2,
67            -1.14e-5,
68        ),
69    }

Test TOML spec file for case of input mesh with steady velocity field.

def test_specfile():
 72def test_specfile():
 73    """Test TOML spec file parsing."""
 74    config = _io.parse_config(_io.data("specs") / "spec.toml")
 75    assert list(config.keys()) == ["name", "input", "output", "parameters"]
 76    _input = config["input"]
 77    # TODO: Add some example mesh/path files and use them in another test.
 78    assert _input["mesh"] is None
 79    assert _input["locations_final"] is None
 80    assert (
 81        _input["velocity_gradient"][1].func
 82        == _velocity.simple_shear_2d("Y", "X", 5e-6)[1].func
 83    )
 84    assert (
 85        _input["velocity_gradient"][1].keywords
 86        == _velocity.simple_shear_2d("Y", "X", 5e-6)[1].keywords
 87    )
 88    assert _input["locations_initial"] == _io.read_scsv(
 89        _io.data("specs") / "start.scsv"
 90    )
 91    assert _input["timestep"] == 1e9
 92    assert _input["paths"] is None
 93    outdir = pl.Path(_io.data("specs") / "out").resolve()
 94    _output = config["output"]
 95    assert _output["directory"] == outdir
 96    np.testing.assert_array_equal(_output["raw_output"], [_core.MineralPhase.olivine])
 97    np.testing.assert_array_equal(_output["diagnostics"], [_core.MineralPhase.olivine])
 98    assert _output["anisotropy"] is True
 99    assert _output["paths"] is None
100    assert _output["log_level"] == "DEBUG"
101    assert config["parameters"] == {
102        "phase_assemblage": (_core.MineralPhase.olivine,),
103        "phase_fractions": (1.0,),
104        "initial_olivine_fabric": _core.MineralFabric.olivine_A,
105        "stress_exponent": 1.5,
106        "deformation_exponent": 3.5,
107        "gbm_mobility": 125,
108        "gbs_threshold": 0.3,
109        "nucleation_efficiency": 5.0,
110        "number_of_grains": 2000,
111        "disl_Peierls_stress": 2,
112        "disl_prefactors": (1e-16, 1e-17),
113        "diff_prefactors": (1e-10, 1e-10),
114        "disl_lowtemp_switch": 0.7,
115        "disl_activation_energy": 460.0,
116        "disl_activation_volume": 12.0,
117        "diff_activation_energies": (430.0, 330.0),
118        "diff_activation_volumes": (4.0, 4.0),
119        "disl_coefficients": (
120            4.4e8,
121            -5.26e4,
122            2.11e-2,
123            1.74e-4,
124            -41.8,
125            4.21e-2,
126            -1.14e-5,
127        ),
128    }

Test TOML spec file parsing.