clc
clear all

%参考四面体
%顶点1-[1,0,0] 2-[0,1,0] 3-[0,0,1] 4-[0,0,1]

% 算例配置（改波长/材料/边界请编辑 case_config_doublePBC.m）
[phy, cfg] = case_config_doublePBC();

%读取网格
load(cfg.meshMatFile);
mesh.incIndex=findTri(phy.inc,mesh);
mesh.outIndex=findTri(phy.out,mesh);
mesh.PBCIndex=findPBCIndex(phy.src,phy.dst,cfg.pbcWaveVec,mesh);
mesh.PBCphi=cfg.pbcPhi;
mesh.PBCIndex2=findPBCIndex(phy.src2,phy.dst2,cfg.pbcWaveVec2,mesh);
mesh.PBCphi2=cfg.pbcPhi2;

%矩阵组装
solver.Ai=[];solver.Aj=[];solver.Av=[];
solver.b=zeros(mesh.NbrEdge,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);
%Bloch boundary
solver=assembly_pbc_double(mesh,solver);

%求解
solver.A=solver.P'*solver.A*solver.P;
solver.b=solver.P'*solver.b;
solver.x=solver.A\solver.b;
solver.x=solver.P*solver.x;

%计算电场
triIndex=findTri(3,mesh);
[x,y,normE]=get_ele2(triIndex,mesh,solver);
patch(x,y,normE,'EdgeAlpha',0)
colormap jet;
colorbar
min(min(normE))
max(max(normE))
