%vlnovod v CST
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('../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/vlnovod.mod';

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

%definovani pormennych
a = 10.10;
b = 20;
l = 50;

%Vytvoreni modelu pomoci funkci 
CstNewProject(fid);
CstUnits(fid,'easy','mm','gHz','ns');
CstMaterialExist('Copper');
CstNewComponent(fid,'vln');
CstBrick(fid,'vlnovod','vln','Copper',-a/2,a/2,-b/2,b/2,-l/2,l/2);
CstBrick(fid,'air','vln','Vacuum',-(a/2)+1,(a/2)-1,-(b/2)+1,(b/2)-1,-l/2,l/2);
CstSolid(fid,'Subtract','vln','vlnovod','air');
CstBackground(fid,'Normal',1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,'Normal',0.0,'True');
CstSetFrekvencyRange(fid,1,10);
CstWaveguidePort(fid,1,1,'False',0.0,0,50,'Free','zmin','False','False',...
    -a/2,a/2,-b/2,b/2,-l/2,-l/2,0.0,0.0,0.0,0.0,0.0,0.0);
CstWaveguidePort(fid,2,1,'False',0.0,0,50,'Free','zmax','False','False',...
    -a/2,a/2,-b/2,b/2,l/2,l/2,0.0,0.0,0.0,0.0,0.0,0.0);
CstBoundary(fid,'electric','electric','electric','electric','open','open','none',...
    'none','none','isothermal','isothermal','isothermal','isothermal','isothermal','isothermal',...
    'False','','','','','','',...
    'None','None','None','None','None','None');
CstTimeDomainSolverEasy(fid,'TD-S','All','All',-30,'False','False',50,'False','False','False','False','False');
CstStartSolver(fid);
SPar12=CstGet1DResultSxx([1,2]);
plot(SPar12(:,1),SPar12(:,2),'r');
CstSaveAsProject(tmpAdrSoub);
CstCloseProject;

