%Example
clear all; close all; %clc;

global fid Module Design;
% Namapovani cest k dodatecnym souborum.
addpath('../3D modeler/');
addpath('../analysis/');
addpath('../general/');
addpath('../boundary/');
addpath('../other/');


%Parametry
sir = 38.1472368639;%37.7;
vys = 28.5302304401;%28.4;
tl_sub = 1.6;
tl_zar = 0.5;%1;
del_zar = 9;%6;
%port
x_port = 6;
y_port = 7.1;

% Adresa docasneho souboru temp.
tempScriptFileHfss = 'C:/temp/tmpDataHfss.m';

%Nazev ukladaneho souboru xxx.hfss
hfssSkriptTemp = 'C:/temp/tmpDataHfss.hfss';

% Adresa souboru s vyexporotvanymi vysledky
hfssExportFile = 'C:/temp/tempDataExportHFSS.m';

% Vytvoreni docasneho m-file.
fid = fopen(tempScriptFileHfss, 'wt');
%--------------------------------------------------------------------------

HfssNewProject(fid);
HfssInsertDesign(fid,'Example_antena');
HfssBox(fid,'Dielektrikum','FR4_epoxy',[-sir,-vys,0],[sir+(2*sir),vys+(2*vys),tl_sub],'mm');
HfssRectangle(fid,'Antena','Z',[0,0,tl_sub],sir,vys,'mm');
HfssSetColor(fid,'Antena',[202 184 123]);
HfssSetTransparency(fid,'Antena',0);
HfssRectangle(fid,'Zarez1','Z',[0,((vys/2)-(tl_zar/2)),tl_sub],del_zar,tl_zar,'mm');
HfssSubtract(fid,'Antena','Zarez1');
HfssRectangle(fid,'Zarez2','Z',[((sir/2)-(tl_zar/2)),0,tl_sub],tl_zar,del_zar,'mm');
HfssSubtract(fid,'Antena','Zarez2');
HfssRectangle(fid,'Zarez3','Z',[(sir-del_zar),((vys/2)-(tl_zar/2)),tl_sub],del_zar,tl_zar,'mm');
HfssSubtract(fid,'Antena','Zarez3');
HfssRectangle(fid,'Zarez4','Z',[((sir/2)-(tl_zar/2)),(vys-del_zar),tl_sub],tl_zar,del_zar,'mm');
HfssSubtract(fid,'Antena','Zarez4');
HfssRectangle(fid,'Ground','Z',[(-sir),(-vys),0],sir+(2*sir),vys+(2*vys),'mm');
HfssSetColor(fid,'Ground',[202 184 123]);
HfssSetTransparency(fid,'Ground',0);
HfssCircle(fid,'G_Dira','Z',[((sir/2)+x_port),((vys/2)+y_port),0],2.7,'mm');
HfssSubtract(fid,'Ground','G_Dira');
HfssCylinder(fid,'Koax','Z',[((sir/2)+x_port),((vys/2)+y_port),0],2.7,-8,'mm','vacuum');
HfssCylinder(fid,'Port1','Z',[((sir/2)+x_port),((vys/2)+y_port),0],0.5,-8,'mm','vacuum');
HfssAssignMaterial(fid,'Port1','pec');
HfssSetColor(fid,'Port1',[202 184 123]);
HfssCylinder(fid,'Pin','Z',[((sir/2)+x_port),((vys/2)+y_port),0],0.5,tl_sub,'mm','vacuum');
HfssAssignMaterial(fid,'Pin','pec');
HfssSetColor(fid,'Pin',[202 184 123]);
HfssBox(fid,'airvol','air',[(-sir),(-vys),0],[sir+(2*sir),vys+(2*vys),(20*tl_sub)],'mm');
HfssSetTransparency(fid,'airvol',0.9);
FaceIdAirVol = HfssGetFaceByPosition(fid,'airvol',[sir,vys,20*tl_sub],'mm');
HfssAssignRadiationFaces(fid,'AirvolRad1',FaceIdAirVol);
FaceIdAirVol = HfssGetFaceByPosition(fid,'airvol',[(-sir+sir),-vys,tl_sub],'mm');
HfssAssignRadiationFaces(fid,'AirvolRad2',FaceIdAirVol);
FaceIdAirVol = HfssGetFaceByPosition(fid,'airvol',[(-sir),(-vys+vys),tl_sub],'mm');
HfssAssignRadiationFaces(fid,'AirvolRad3',FaceIdAirVol);
FaceIdAirVol = HfssGetFaceByPosition(fid,'airvol',[(2*sir),0,tl_sub],'mm');
HfssAssignRadiationFaces(fid,'AirvolRad4',FaceIdAirVol);
FaceIdAirVol = HfssGetFaceByPosition(fid,'airvol',[0,(2*vys),tl_sub],'mm');
HfssAssignRadiationFaces(fid,'AirvolRad5',FaceIdAirVol);
HfssAssignPE(fid,'PeE1','Antena',false);
HfssAssignPE(fid,'PeE2','Ground',true);
HfssCircle(fid,'WPort','Z',[((sir/2)+x_port),((vys/2)+y_port),-8],2.7,'mm');
HfssSetTransparency(fid,'WPort',1);
HfssAssignWavePort(fid,'WavePort','WPort',[((sir/2)+x_port),((vys/2)+y_port),-8],...
   [((sir/2)+x_port+2.7),((vys/2)+y_port),-8],'mm');
HfssInsertSolution(fid,'Setup3GHz',3,0.02,25);
HfssFastSweep(fid,'sweep1to3GHz','Setup3GHz',1,3,1000);
HfssSolveSetup(fid, 'Setup3GHz');

% Ulozeni vytvoreneho projektu
%HfssSaveProject(fid,hfssSkriptTemp);
HfssExportNetworkData(fid,hfssExportFile,'Setup3GHz','sweep1to3GHz','m',50);

% Zobrazeni vysledku                   
run(hfssExportFile); 
SF = GetFrequencyFromDiagram(0.5,hfssExportFile);
plot(f/1e9, 20*log10(abs(S)));
hold on;
xlabel('Frequency (GHz) ->');
ylabel('S_{11} (dB) ->');
axis([1, 3, -20, 0]);   

% Odstrani nactene cesty.
% rmpath('../3D modeler/');
% rmpath('../general/');
% rmpath('../analysis/');
% rmpath('../boundary/');                    
                   
                   