clc
clear all
%Ridge waveguide with port boundary conditions

%Read mesh data
%Nodes Elements Domains Faces FacesIndex
tic 
load MeshData2BW.mat
Mesh.NbrNodes=length(Mesh.Nodes); %number of vertices
Mesh.NbrElements=length(Mesh.Elements);%number of elements
disp('the time taken of read mesh data: ');
toc

%Physical model
tic
%Physical variables
Physic.lam0=1.55e-6;%wavelength 
Physic.c_const=299792458;%The speed of light in vacuum
Physic.k0=2*pi/Physic.lam0;%wavevector
%Material lib
Material.epsilonr=[4 11.9 11.9 11.9]';%relative permittivity
Material.mur=[1 1 1 1]';%relative permeability
Material.sigma=[0 0 5000 0]';%electrical conductivity
Material.epsilon0=8.854187817e-12;
Physic.omega=2*pi*Physic.c_const/Physic.lam0;
Material.temp=1/Physic.k0*120*pi;%Physic.omega*Material.epsilon0;

Material.epsilonrF=[0    0 4    4    0 0 0 11.9 11.9 0 ...
                       0    0 11.9 11.9 0 0 0 0    0    11.9 ...
                       11.9 0 0    0]';
Material.murF=ones(24,1);
Material.sigmaF=[0 0 0 0 0 0 0 0 0 0 0 0 5000 5000 ...
    0 0 0 0 0 0 0 0 0 0]';
%Boundary conditions
Physic.Port1=[3 8 13 20];%port1 input
Physic.Port2=[4 9 14 21];%port2 output
Physic.PEC=[1 2 5 24];%PEC
disp('The time taken of physical model');
toc

%Port solving
tic
%solver settings
Port1Solver.targetNeff=2.6;
Port1Solver.modeNum=1;
Port1Solver.lam0=Physic.lam0;
Port1Solver.type=1;%input port
Port2Solver.targetNeff=2.6;
Port2Solver.modeNum=1;
Port2Solver.lam0=Physic.lam0;
Port2Solver.type=2;%output port
%Port1
Port1Mesh=GetBMesh(Mesh.Nodes,Mesh.Faces,Mesh.FacesIndex,Physic.Port1);
[Port1Solver,Port1Mesh]=BoundaryEigenMode(Port1Mesh,Physic,Material,Port1Solver);
Port1Solver=GetPowerCoef(Port1Solver,Port1Mesh,Material.murF,1);%Compute normalized electric field intensity coefficient
Plot2Dfield(Port1Mesh,Port1Solver.modeNum,Port1Solver.normE,Port1Solver.neff,1,0.5)%Plot electric field intensity
Port1Mesh.normal=[0;0;-1];
%Port2
Port2Mesh=GetBMesh(Mesh.Nodes,Mesh.Faces,Mesh.FacesIndex,Physic.Port2);
[Port2Solver,Port2Mesh]=BoundaryEigenMode(Port2Mesh,Physic,Material,Port2Solver);
Plot2Dfield(Port2Mesh,Port2Solver.modeNum,Port2Solver.normE,Port1Solver.neff,2,0.5)%Plotting electric field intensity
Port2Mesh.normal=[0;0;1];
disp('The time taken of port solving');
toc

%Matrix assembly and solution
[Solver,Mesh]=FemMatrixAssembly2(Physic,Material,Mesh,...
    Port1Mesh,Port1Solver,Port2Mesh,Port2Solver);

AllFaces=GetBMesh(Mesh.Nodes,Mesh.Faces,Mesh.FacesIndex,...
    [Physic.Port1,Physic.Port2,Physic.PEC]);
%plot electric field intensity 
% tic
% [Ex,Ey,Ez,normE]=GetExEyEz2(1,Mesh.Nodes,Mesh.Elements,Mesh.EdgesOfElements,Solver.Et);
% plotE2(Mesh.Nodes,AllFaces.OldFaces,Mesh.AllFaces,Mesh.FacesOfElements,normE,3,0.5);
% In=plotE2(Mesh.Nodes,Port1Mesh.OldFaces,Mesh.AllFaces,Mesh.FacesOfElements,normE,4,0.5);
% Out=plotE2(Mesh.Nodes,Port2Mesh.OldFaces,Mesh.AllFaces,Mesh.FacesOfElements,normE,5,0.5);
% Scale=Out/In;
% disp('The time taken of plotting electric field intensity ');
% toc

%plot electric field intensity 
tic
plotE(Mesh.Nodes,AllFaces.OldFaces,Solver.normE,3,0.5);
In=plotE(Mesh.Nodes,Port1Mesh.OldFaces,Solver.normE,4,0.5);
Out=plotE(Mesh.Nodes,Port2Mesh.OldFaces,Solver.normE,5,0.5);
scale=Out/In;
disp('The time taken of plotting electric field intensity ');
toc