File size: 2,817 Bytes
3116550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""Regenerate corrected cubic B-spline bases and plot them vertically."""

from __future__ import annotations

from pathlib import Path

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
import numpy as np  # noqa: E402

from synthetic_iso_generator import (
    CRUST_BS_PATH,
    MANTLE_BS_PATH,
    _build_bspline_basis,
)


ROOT = Path(__file__).resolve().parent
OUTPUT_PNG = ROOT / "bspline_bases_vertical.png"
GRID_STYLE = dict(color="#898888C5", linestyle=(0, (8, 8)), linewidth=0.4)


def _validate(name: str, basis: np.ndarray, expected_shape: tuple[int, int]) -> None:
    if basis.shape != expected_shape:
        raise ValueError(f"{name}: expected {expected_shape}, got {basis.shape}")
    if np.min(basis) < -1.0e-12:
        raise ValueError(f"{name}: basis contains negative values")
    if not np.allclose(basis.sum(axis=1), 1.0, atol=1.0e-12):
        raise ValueError(f"{name}: basis does not form a partition of unity")


def main() -> None:
    crust = _build_bspline_basis(4, 21, 0.0, 1.0)
    mantle = _build_bspline_basis(5, 80, 0.0, 1.0)
    _validate("crust", crust, (21, 4))
    _validate("mantle", mantle, (80, 5))

    np.savetxt(CRUST_BS_PATH, crust, fmt="%.18e")
    np.savetxt(MANTLE_BS_PATH, mantle, fmt="%.18e")

    colors = plt.get_cmap("tab10").colors
    fig, axes = plt.subplots(1, 2, figsize=(10, 9), facecolor="white")
    for ax, basis, layer in (
        (axes[0], crust, "Crust"),
        (axes[1], mantle, "Mantle"),
    ):
        normalized_depth = np.linspace(0.0, 1.0, basis.shape[0])
        for index in range(basis.shape[1]):
            ax.plot(basis[:, index], normalized_depth, lw=2.5,
                    color=colors[index], label=f"{layer} B{index + 1}")
        ax.set_title(f"{layer} cubic B-spline basis", fontsize=17,
                     fontweight="bold")
        ax.set_xlabel("Basis amplitude", fontsize=14)
        ax.set_ylabel("Normalized layer depth", fontsize=14)
        ax.set_xlim(-0.03, 1.03)
        ax.set_ylim(1.0, 0.0)
        ax.grid(**GRID_STYLE)
        ax.legend(frameon=False, fontsize=11)
        ax.tick_params(labelsize=12)

    fig.suptitle("Regenerated open-uniform cubic B-spline bases",
                 fontsize=18, fontweight="bold")
    fig.tight_layout()
    fig.savefig(OUTPUT_PNG, dpi=300, bbox_inches="tight", facecolor="white")
    plt.close(fig)

    print(f"Saved crust basis  {crust.shape} -> {CRUST_BS_PATH}")
    print(f"Saved mantle basis {mantle.shape} -> {MANTLE_BS_PATH}")
    print(f"Saved plot -> {OUTPUT_PNG}")
    print(f"Crust partition-sum range: {crust.sum(1).min():.16f} .. {crust.sum(1).max():.16f}")
    print(f"Mantle partition-sum range: {mantle.sum(1).min():.16f} .. {mantle.sum(1).max():.16f}")


if __name__ == "__main__":
    main()