clc
clear all
%Ridge waveguide with port boundary conditions

%Read mesh data
%Nodes Elements Domains Faces FacesIndex
tic 
load MeshData.mat
Mesh.Nodes=Mesh.Nodes*1e-6;
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.ff0=3e10;
Physic.c_const=299792458;%The speed of light in vacuum
Physic.lam0=Physic.c_const/Physic.ff0;%wavelength 
Physic.k0=2*pi/Physic.lam0;%wavevector
%Material lib
load Material.mat
Material.epsilonr=epsilonr;
Material.mur=mur;
Material.sigma=zeros(length(Material.mur),1);
Material.epsilonrF=epsilonrF;
Material.murF=murF;
Material.sigmaF=zeros(length(Material.murF),1);
Physic.omega=2*pi*Physic.c_const/Physic.lam0;
Material.temp=1/Physic.k0*120*pi;
%Boundary conditions
Physic.Port1=[33 37 41 45 49 53 57 62 67 72 76 82 87 93 98 103 107 112 117 122 127 132 137 142];%port1 input
Physic.Port2=[34 38 42 46 50 54 58 63 68 73 77 83 88 94 99 104 108 113 118 123 128 133 138 143];%port2 output
Physic.PEC=[1 2 3 4 5 8 9 12 13 16 17 20 21 24 25 28 29 148 149 152 153 156 157 160 161 164 165 174];%PEC
disp('The time taken of physical model');
toc

%Port solving
tic
%solver settings
Port1Solver.targetNeff=3.6;
Port1Solver.modeNum=1;
Port1Solver.lam0=Physic.lam0;
Port1Solver.type=1;%input port
Port2Solver.targetNeff=3.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,Port2Solver.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]=FemMatrixAssembly(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,Port1Mesh.OldFaces,Mesh.AllFaces,Mesh.FacesOfElements,normE,4,0.5);
% plotE2(Mesh.Nodes,Port2Mesh.OldFaces,Mesh.AllFaces,Mesh.FacesOfElements,normE,5,0.5);
% disp('The time taken of plotting electric field intensity ');
% toc

%plot electric field intensity 
tic
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

