clc
clear all
%Ridge waveguide with port boundary conditions

%Read mesh data
%Nodes Elements Domains Faces FacesIndex
tic 
%MeshDatax 9w   MeshDataxx 14w  
load MeshDatax.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
Physic.omega=2*pi*Physic.c_const/Physic.lam0;
Material.temp=1/Physic.k0*120*pi;
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.powerCoef,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);
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;
S11=Solver.e1/abs(Port1Solver.powerCoef);
S21=Solver.e2/abs(Port1Solver.powerCoef);
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

