% ----------------------------------------------------------------------------
% function HfssInsertSolution(fid, HName, HfGHz, [HmaxDeltaS = 0.02], 
%                             [HmaxPass = 25])
% 
% Popis funkce :
% ------------- 
% Vlozeni noveho solution setupu v HFSS.
%
% Parametery :
% ------------
% fid       - soubor identifikujici HFSS script.
% HName      - jmeno solution setupu.
% HfGHz      - pouzita frekvence (v GHz).
% HmaxError  - maximum chyb ktere jsou tolerovany (mohou byt mezi 0 a 1,
%             prednastaveno je 0.02).
% HmaxPasses - max # of passes to be run before the simulation is declared
%             as 'did not converge' (default is 25).
% 
%
% Priklad pouziti funkce :
% ---------
% fid = fopen('antenna.m', 'wt');
% ... 
% HfssInsertSolution(fid, 'Setup150MHz', 0.15, 0.02, 25);
% ----------------------------------------------------------------------------

function HfssInsertSolution(fid, HName, HfGHz, HmaxDeltaS, HmaxPass)
global Module Design

% arguments processor.
if (nargin < 3)
	error('Insufficient number of arguments !');
elseif (nargin < 4)
	HmaxDeltaS = [];
	HmaxPass = [];
elseif (nargin < 5)
	HmaxPass = [];
end;

% defaults processing.
if isempty(HmaxDeltaS)
	HmaxDeltaS = 0.02;
end;
if isempty(HmaxPass)
	HmaxPass = 25;
end;

%zapis do souboru temp
fprintf(fid,'%% Nastaveni analyzy\n');
fprintf(fid,'Module = Design.GetModule(''AnalysisSetup'');\n');
fprintf(fid,'Module.InsertSetup(''HfssDriven'',...\n');
fprintf(fid,['{''NAME:',HName,''',...\n']);
fprintf(fid,['''Frequency:='',''',[num2str(HfGHz),'GHz'''],',...\n']);
fprintf(fid,'''PortsOnly:='',false,...\n');
fprintf(fid,['''maxDeltaS:='',',num2str(HmaxDeltaS),',...\n']);
fprintf(fid,'''UseMatrixConv:='',false,...\n');
fprintf(fid,['''MaximumPasses:='',',num2str(HmaxPass),',...\n']);
fprintf(fid,'''MinimumPasses:='',1,...\n');
fprintf(fid,'''MinimumConvergedPasses:='',1,...\n');
fprintf(fid,'''PercentRefinement:='',20,...\n');
fprintf(fid,'''ReducedSolutionBasis:='',false,...\n');
fprintf(fid,'''DoLambdaRefine:='',true,...\n');
fprintf(fid,'''DoMaterialLambda:='',true,...\n');
fprintf(fid,'''Target:='',0.3333,...\n');
fprintf(fid,'''PortAccuracy:='',2,...\n');
fprintf(fid,'''SetPortMinMaxTri:='',false})\n\n');

%Prime vykonani v progrqamu Hfss
Module = Design.GetModule('AnalysisSetup');
Module.InsertSetup('HfssDriven',...
    {['NAME:',HName],...
    'Frequency:=',[num2str(HfGHz),'GHz'],...
    'PortsOnly:=',false,...
    'maxDeltaS:=',HmaxDeltaS,...
    'UseMatrixConv:=',false,...
    'MaximumPasses:=',HmaxPass,...
    'MinimumPasses:=',1,...
    'MinimumConvergedPasses:=',1,...
    'PercentRefinement:=',20,...
    'ReducedSolutionBasis:=',false,...
    'DoLambdaRefine:=',true,...
    'DoMaterialLambda:=',true,...
    'Target:=',0.3333,...
    'PortAccuracy:=',2,...
    'SetPortMinMaxTri:=',false})
end
