function main_export()
%MAIN_EXPORT  一阶单周期散射：导出 Ab 矩阵与 normE（格式同 3D 一阶散射 OutFile）。
%
% 输出 ./OutFile/：
%   Ai.txt, Aj.txt, Av.txt, Bv_real.txt, Bv_imag.txt  — PBC 投影后 P'*A*P, P'*b
%   Ex, Ey, Ez, normE                                 — 求解后全场
%
% 输出 ./OutFile_asm/：PBC 投影前的装配矩阵（与一阶散射 main.m 导出时机一致）。
%
% 网格：优先 c6.mat，否则读同目录 PBCmesh.dat。

clc;
[phy, cfg] = case_config();
root = fileparts(mfilename('fullpath'));
outDir = fullfile(root, 'OutFile');
outAsm = fullfile(root, 'OutFile_asm');

mesh = load_case_mesh(cfg, root);
mesh.incIndex = findTri(phy.inc, mesh);
mesh.outIndex = findTri(phy.out, mesh);
[mesh.PBCIndex, mesh.origIndex] = findPBCIndex(phy.src, phy.dst, cfg.pbcAngle, mesh);
mesh.PBCphi = cfg.pbcPhi;

solver.Ai = [];
solver.Aj = [];
solver.Av = [];
solver.dof = mesh.NbrEdge;
solver.b = zeros(solver.dof, 1);
solver = assembly_equ(phy, mesh, solver);
solver = assembly_out(phy, mesh, solver);
solver = assembly_inc(phy, mesh, solver);
solver.A = sparse(solver.Ai, solver.Aj, solver.Av);

% 装配矩阵（无 PBC），与一阶散射 main.m 一致
export_ab_matrix(solver, outAsm);

% Bloch 单周期边界
solver = assembly_pbc_single(mesh, solver);
solver.A = solver.P' * solver.A * solver.P;
solver.b = solver.P' * solver.b;

% 投影后线性系统（与 C++ pbc3d_sbc.json 对应）
export_ab_matrix(solver, outDir);

% 求解（与 main_singlePBC.m 一致）
solver.x = solver.A \ solver.b;
solver.x = solver.P * solver.x;

[Ex, Ey, Ez, normE] = get_ele_vertices(mesh, solver);
export_fields(Ex, Ey, Ez, normE, outDir);

fprintf('\n完成。\n');
fprintf('  投影后 Ab + 场 → %s\n', outDir);
fprintf('  装配 Ab（无 PBC）→ %s\n', outAsm);
end
