|
| 1 | +"""Test individual units of the PyVMCON implementation.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pyvmcon.vmcon import _revise_B |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class BRevisionAsset: |
| 13 | + """Test asset for testing B matrix revision.""" |
| 14 | + |
| 15 | + B: np.ndarray |
| 16 | + ksi: np.ndarray |
| 17 | + eta: np.ndarray |
| 18 | + expected_new_B: np.ndarray # noqa: N815 |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "test_asset", |
| 23 | + [ |
| 24 | + BRevisionAsset( |
| 25 | + B=np.identity(2), |
| 26 | + ksi=np.array([-0.66666666666666663, -0.83333333333333348]), |
| 27 | + eta=np.array([-1.3425925925925923, -1.7129629629629632]), |
| 28 | + expected_new_B=np.array( |
| 29 | + [ |
| 30 | + [1.3858727457706470, 0.50241291449459347], |
| 31 | + [0.50241291449459347, 1.6536252239598812], |
| 32 | + ] |
| 33 | + ), |
| 34 | + ), |
| 35 | + BRevisionAsset( |
| 36 | + B=np.array( |
| 37 | + [ |
| 38 | + [2.1875467668036239, 1.4714414127452644], |
| 39 | + [1.4714414127452644, 2.7501870672148332], |
| 40 | + ] |
| 41 | + ), |
| 42 | + ksi=np.array([-1.2385140125071988e-6, -6.1925700625482853e-7]), |
| 43 | + eta=np.array([-3.6205427119684330e-6, -3.5255433852299234e-6]), |
| 44 | + expected_new_B=np.array( |
| 45 | + [ |
| 46 | + [2.1875592084316073, 1.4714730232083293], |
| 47 | + [1.4714730232083293, 2.7502368318126678], |
| 48 | + ] |
| 49 | + ), |
| 50 | + ), |
| 51 | + ], |
| 52 | +) |
| 53 | +def test_revise_B(test_asset): |
| 54 | + """Tests the hessian update implementation. |
| 55 | +
|
| 56 | + Uses data from Example 1 of the NEA (Crane) to ensure PyVMCON agrees with that |
| 57 | + implementation to at least 14 decimal places. |
| 58 | + """ |
| 59 | + new_B = _revise_B(test_asset.B, test_asset.ksi, test_asset.eta) |
| 60 | + |
| 61 | + # check symmetric |
| 62 | + np.testing.assert_array_almost_equal(new_B, new_B.T, decimal=14) |
| 63 | + |
| 64 | + # check our revision agrees with NEA version of VMCON |
| 65 | + np.testing.assert_array_almost_equal(new_B, test_asset.expected_new_B, decimal=14) |
0 commit comments