%Dipole
clear all; close all; clc;
global fid cst mws;
% add paths to the required m-files.
addpath('../component/');
addpath('../convert/');
addpath('../global/');
addpath('../check3d/');
addpath('../Import_Export/');
addpath('../material/');
addpath('../mesh/');
addpath('../monitors/');
addpath('../operation on solids/');
addpath('../ports/');
addpath('../results/');
addpath('../solids/');
addpath('../solver/');
 
% Vytvoreni souboru temp
tmpScriptFile = 'C:/cst_temp/tmpData.m';

% Pojmenovani docasneho temp adresare pro ulozeni .mod souboru.
tmpAdrSoub = 'C:/cst_temp/dipole.mod';

% Create a new temporary CST script file.
fid = fopen(tmpScriptFile, 'wt');

%definovani pormennych
lambda = 10;
d = 0.025*lambda;
I2 = 0.5*lambda;
r = 0.003369*lambda;

%Vytvoreni modelu pomoci funkci 
CstNewProject(fid);
CstUnits(fid,'easy','mm','gHz','ns');
CstNewComponent(fid,'PEC');
CstCylinder(fid,'Driven Element','PEC','PEC','Z',r,0,0,0,-I2/2,I2/2,0);
CstBrick(fid,'Solid1','PEC','Vacuum',-0.5,0.4,-0.4,0.3,-d/2,d/2);
CstSolid(fid,'Subtract','PEC','Driven Element','Solid1');
CstAutoMesh(fid);
CstSetFrekvencyRange(fid,25,35);
CstBackground(fid,'Normal',1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,'Normal',0.0,'True');
CstBoundary(fid,'expanded open','expanded open','expanded open','expanded open',...
    'expanded open','expanded open','magnetic','magnetic','electric','isothermal',...
    'isothermal','isothermal','isothermal','isothermal','isothermal',...
    'False','','','','','','','None','None','None','None','None','None');
CstPickCenterpointFromId(fid,'PEC:Driven Element',2);
CstPickCenterpointFromId(fid,'PEC:Driven Element',1);
CstDiscretePort(fid,1,'SParameter',65,1.0,1.0,[0,0,-d/2],[0,0,d/2],'True','False','True');
CstMonitor(fid,'farfield (f=27.18)','Frequency','Farfield',27.18);
CstMeshProperties(fid,'True',20.0,15,50,'True','PBA');
CstTimeDomainSolverEasy(fid,'TD-S','All','All',-60,'False','False',50,'False','False','False','False','False');
CstStartSolver(fid);
SPar11=CstGet1DResultSxx([1,1]);
plot(SPar11(:,1),SPar11(:,1),'r');
CstSaveAsProject(tmpAdrSoub);


