cd('C:/Users/Administrator/Desktop/西电-合作/三维matlab代码/matlab 3D二阶基+散射边界条件+单周期边界');
[phy, cfg] = case_config();
load(cfg.meshMatFile);
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*2+mesh.NbrFace*2;
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);
solver=assembly_rbc(mesh,solver);
solver.A=solver.P'*solver.A*solver.P;
solver.b=solver.P'*solver.b;
r=symamd(solver.A);
solver.A=solver.A(r,r);
solver.b=solver.b(r);
solver.x=solver.A\solver.b;
q=zeros(length(r),1);
for i=1:length(r), q(r(i))=i; end
solver.x=solver.x(q);
solver.x=solver.P*solver.x;
triIndex=findTri([2,5],mesh);
[x,y,z,normE]=get_ele(triIndex,mesh,solver);
fid=fopen('matlab_norme_stats.txt','w');
fprintf(fid,'dof=%d\n',solver.dof);
fprintf(fid,'reduced=%d\n',size(solver.P,2));
fprintf(fid,'max_normE_face25=%.6g\n',max(normE(:)));
fprintf(fid,'mean_normE_face25=%.6g\n',mean(normE(:)));
fprintf(fid,'|x|_max=%.6g\n',max(abs(solver.x)));
fprintf(fid,'|b|_max=%.6g\n',max(abs(solver.b)));
% also vertex field if get_ele_vertices exists
try
  [xv,yv,zv,nEv]=get_ele_vertices(mesh,solver);
  fprintf(fid,'max_normE_vertices=%.6g\n',max(nEv(:)));
  fprintf(fid,'mean_normE_vertices=%.6g\n',mean(nEv(:)));
  dlmwrite('matlab_normE_vertices.txt', nEv(:), 'precision', 16);
catch ME
  fprintf(fid,'vertex_err=%s\n', ME.message);
end
fclose(fid);
dlmwrite('matlab_x_real.txt', real(solver.x), 'precision', 16);
dlmwrite('matlab_x_imag.txt', imag(solver.x), 'precision', 16);
disp('done');
