%% CAS_F0028_VERIFY.m — de Broglie relation (p = h/λ)
%  Pillar : Particle Mechanics
%  CalRef : Math Appendix §4A, Particle Mechanics Calibration §3C
%  Chain  : constitutive postulate p=h/λ → photon dictionary check → wave number
%  Hardening: isAlways(..., 'Unknown', 'false') on every symbolic assertion
%            symvar-based presence checks; solver and has-function avoided

clear; clc;
fprintf('\n=== CAS F0028 : de Broglie relation ===\n\n');

%% ── Symbols ──────────────────────────────────────────────────
syms h_pl lambda_db p_mom real positive   % Planck constant, wavelength, momentum
syms nu c_light real positive             % frequency, speed of light
syms E_ph real positive                   % photon energy
syms k_wave real positive                 % wave number
syms hbar real positive                   % reduced Planck constant

%% ── A. Inputs ────────────────────────────────────────────────
%  de Broglie postulate: p = h/λ
p_deBroglie = h_pl / lambda_db;

%% ── Step 1: Constitutive relation structure ─────────────────
%  p = h/λ  →  p·λ = h
res1 = simplify(p_deBroglie * lambda_db - h_pl);
assert(isAlways(res1 == 0, 'Unknown', 'false'), ...
    'FAIL Step 1: p·λ ≠ h');
fprintf('Step 1  PASS — p·λ = h (constitutive relation)\n');

%% ── Step 2: Wavelength from momentum ────────────────────────
%  λ = h/p
lambda_from_p = h_pl / p_mom;
%  Verify: substituting into p = h/λ recovers p
p_check = h_pl / lambda_from_p;
res2 = simplify(p_check - p_mom);
assert(isAlways(res2 == 0, 'Unknown', 'false'), ...
    'FAIL Step 2: h/(h/p) ≠ p');
fprintf('Step 2  PASS — λ = h/p → p = h/λ (round-trip)\n');

%% ── Step 3: Photon dictionary consistency ───────────────────
%  Photon: E = hν, λ = c/ν, p = E/c
%  → p = hν/c = h/(c/ν) = h/λ  ✓
E_photon = h_pl * nu;
lambda_photon = c_light / nu;
p_photon_energy = E_photon / c_light;         % p = E/c = hν/c
p_photon_deBroglie = h_pl / lambda_photon;    % p = h/λ = h/(c/ν) = hν/c

res3 = simplify(p_photon_energy - p_photon_deBroglie);
assert(isAlways(res3 == 0, 'Unknown', 'false'), ...
    'FAIL Step 3: photon E/c ≠ h/λ');
fprintf('Step 3  PASS — Photon: p = E/c = hν/c = h/λ (dictionary consistent)\n');

%% ── Step 4: Wave number form p = ℏk ────────────────────────
%  k = 2π/λ, ℏ = h/(2π)
%  p = h/λ = h·k/(2π) = ℏk
p_wavenumber = hbar * k_wave;
%  Substitute ℏ = h/(2π), k = 2π/λ
p_wn_explicit = (h_pl / (2*sym(pi))) * (2*sym(pi) / lambda_db);
res4 = simplify(p_wn_explicit - p_deBroglie);
assert(isAlways(res4 == 0, 'Unknown', 'false'), ...
    'FAIL Step 4: ℏk ≠ h/λ');
fprintf('Step 4  PASS — p = ℏk = h/λ (wave number form)\n');

%% ── Step 5: Energy-momentum for photons ─────────────────────
%  E = pc (massless) → E = hc/λ = hν
%  Verify: hc/λ = hν when λ = c/ν
E_from_p = p_deBroglie * c_light;  % = hc/λ
E_from_nu = h_pl * nu;
%  Substitute λ = c/ν
E_sub = subs(E_from_p, lambda_db, c_light/nu);
res5 = simplify(E_sub - E_from_nu);
assert(isAlways(res5 == 0, 'Unknown', 'false'), ...
    'FAIL Step 5: hc/λ ≠ hν at λ=c/ν');
fprintf('Step 5  PASS — E = pc = hc/λ = hν (photon energy-momentum)\n');

%% ── Step 6: Nonrelativistic kinetic energy ──────────────────
%  T = p²/(2m) = h²/(2mλ²)
syms m_part real positive
T_kin = p_deBroglie^2 / (2*m_part);
T_expected = h_pl^2 / (2*m_part*lambda_db^2);
res6 = simplify(T_kin - T_expected);
assert(isAlways(res6 == 0, 'Unknown', 'false'), ...
    'FAIL Step 6: T ≠ h²/(2mλ²)');
fprintf('Step 6  PASS — T = p²/(2m) = h²/(2mλ²)\n');

%% ── Step 7: Numerical — electron at 100 eV ─────────────────
%  T = 100 eV → p = √(2mT)
%  λ = h/p
h_val = 6.62607015e-34;
m_e = 9.1093837015e-31;
eV = 1.602176634e-19;
T_100eV = 100 * eV;
p_val = sqrt(2 * m_e * T_100eV);
lambda_val = h_val / p_val;
lambda_expected_nm = 0.1226;  % ≈ 0.123 nm for 100 eV electron
lambda_calc_nm = lambda_val * 1e9;
assert(abs(lambda_calc_nm - lambda_expected_nm) < 0.005, ...
    'FAIL Step 7: electron wavelength mismatch');
fprintf('Step 7  PASS — 100 eV electron: λ = %.4f nm (expected ≈ 0.123 nm)\n', lambda_calc_nm);

%% ── Step 8: Numerical — photon at visible (550 nm) ─────────
%  λ = 550 nm, p = h/λ
lambda_vis = 550e-9;
p_photon_val = h_val / lambda_vis;
p_expected = 1.205e-27;  % ≈ 1.2×10⁻²⁷ kg·m/s
assert(abs(p_photon_val - p_expected)/p_expected < 0.01, ...
    'FAIL Step 8: photon momentum mismatch');
fprintf('Step 8  PASS — 550 nm photon: p = %.3e kg·m/s\n', p_photon_val);

%% ── Step 9: Cross-block — F0013 wave quantization link ─────
%  F0013: k_n = 2πn/L (mode quantization)
%  de Broglie: p = ℏk → p_n = ℏ·2πn/L = hn/L
%  This is the Bohr–Sommerfeld quantization: p·L = nh
syms n_q real positive
syms L_box real positive
k_n = 2*sym(pi)*n_q / L_box;
p_n = (h_pl/(2*sym(pi))) * k_n;
res9 = simplify(p_n - h_pl*n_q/L_box);
assert(isAlways(res9 == 0, 'Unknown', 'false'), ...
    'FAIL Step 9: p_n ≠ hn/L');
%  Verify p·L = nh
pL = p_n * L_box;
res9b = simplify(pL - n_q*h_pl);
assert(isAlways(res9b == 0, 'Unknown', 'false'), ...
    'FAIL Step 9b: p·L ≠ nh');
fprintf('Step 9  PASS — p_n = hn/L, p·L = nh (consistent with F0013)\n');

%% ── Step 10: Self-test — wrong relation p = h·λ ────────────
%  Wrong: p = h·λ (multiply instead of divide)
p_wrong = h_pl * lambda_db;
res_wrong = simplify(p_wrong - p_deBroglie);
assert(~isAlways(res_wrong == 0, 'Unknown', 'false'), ...
    'FAIL Step 10a: wrong relation not detected');
fprintf('Step 10a PASS — Wrong p=h·λ detected as incorrect\n');

%  Quantify residual: p_wrong − p_correct = hλ − h/λ = h(λ − 1/λ) = h(λ²−1)/λ
res_quant = simplify(res_wrong - h_pl*(lambda_db - 1/lambda_db));
assert(isAlways(res_quant == 0, 'Unknown', 'false'), ...
    'FAIL Step 10b: wrong residual not quantified');
fprintf('Step 10b PASS — Wrong residual = h(λ−1/λ) (quantified)\n');

%% ── Summary ─────────────────────────────────────────────────
fprintf('\n✓ ALL 11 ASSERTIONS PASSED — F0028 audit complete.\n');
fprintf('  Chain: p=h/λ (postulate) → photon dictionary → ℏk form → quantization\n');
fprintf('  Cross-ref: F0013 (mode quantization), F0019 (photon E=hν)\n');
