function varargout = LinkF06(varargin)
% LinkF06 - Read the normal data from NASTRAN output file *.f06.
%
% [GM,FreqW,ModeS] = LinkF06(FileF06,FileAset,NMode)
%
% FileF06 : filename of NASTRAN output file (*.f06)
% FileAset : filename include Aset grid ID / Aset direction
% NMode : no of used mode (default : all)
%
% GM : generalized mass matrix (M, NMode*1)
% FreqW : frequency (Radian, NMode*1)
% ModeS : normal mode shape (NAset*NMode)
%
% *** input/output is optinal select
% Ckeck the input & output property
PreS = [blanks(10),'*** Error : ']; EndS = ' ! <== LinkF06 ***';
PreW = [blanks(5),'*** Warning : '];
if nargin > 3, error([PreS,'No more 3 input arguments',EndS]); end
if nargin < 2, error([PreS,'At least 2 input arguments',EndS]); end
if nargout > 3, error([PreS,'No more 3 output arguments',EndS]); end
FileF06 = varargin{1};
if ~ischar(FileF06), error([PreS,'Wrong input arguments (not char)',EndS]); end
fid = fopen(FileF06,'rt');
if fid == -1, error([PreS,'Can not open the file - ',FileF06,EndS]); end
fclose(fid);
FileAset = varargin{2};
if ~ischar(FileAset), error([PreS,'Wrong input arguments (not char)',EndS]); end
fid = fopen(FileAset,'rt');
if fid == -1, error([PreS,'Can not open the file - ',FileAset,EndS]); end
fclose(fid);
NMode=[];
if nargin >= 3, NMode = varargin{3}; if ~isempty(NMode)
if ~isInteger(NMode), error([PreS,'Wrong used mode no (not integer)',EndS]); end
if NMode <= 0 , error([PreS,'Wrong used mode no (not positive)',EndS]); end
end; end
% *** Begin the reading/sorting process ***
% read the ASET data want to sort the normal data
fid = fopen(FileAset,'rt'); frewind(fid); fgetl(fid); % read title
Aset = fscanf(fid,'%i',[2,inf]); fclose(fid);
Aset = Aset'; Aset = sortrows(Aset,[1,2]); NAset = size(Aset,1);
for k=2:NAset, if Aset(k,1)==Aset(k-1,1), if Aset(k,2)==Aset(k-1,2)
error([PreS,'Wrong input file - ',FileAset,' (repeat ASET)',EndS]); end; end; end
if ~isempty(find( Aset(:,2)<1 | Aset(:,2)>6, 1 ))
error([PreS,'Wrong direction - ',FileAset,EndS]); end
% read the normal data from NASTRAN output file
if ~isempty(NMode), NModeF = NMode; else NModeF = 300; end % max default = 300
EigenV = zeros(NModeF,1); FreqW = zeros(NModeF,1); GM = zeros(NModeF,1);
fid = fopen(FileF06,'rt'); frewind(fid);
% read the eigen matrix ( EigenV / FreqW / GM )
while ~feof(fid), ChkStr = fscanf(fid,'%s',1);
if strcmp(ChkStr,'MODE'), ChkStr = fscanf(fid,'%s',2); ChkStr = fscanf(fid,'%s',1);
if strcmp(ChkStr,'RADIANS'), ChkStr = fscanf(fid,'%s',1);
if strcmp(ChkStr,'CYCLES'), fgetl(fid); k=1;
while k<=NModeF, ChkStr = fscanf(fid,'%s',1); ModeNo = str2num(ChkStr);
if isInteger(ModeNo), if ModeNo==k, Mx = fscanf(fid,'%f',[1,6]);
if Mx(5)~=0, EigenV(k) = Mx(2); FreqW(k) = Mx(3); GM(k) = Mx(5); k= k+1;
else if ~isempty(NMode), if k-1 < NMode,
disp([PreW,'Mode no in F06 is not enough',EndS]); end; end
break; end
end; end; fgetl(fid);
end; NMode = k-1; break
end; end; end; fgetl(fid);
end
EigenV = EigenV(1:NMode); FreqW = FreqW(1:NMode); GM = GM(1:NMode);
% read the Normal Mode matrix ( NAset*NMode )
ModeS = zeros(NAset,NMode);
for iMode=1:NMode, while ~feof(fid), ChkStr = fscanf(fid,'%s',1);
if strcmp(ChkStr,'EIGENVALUE'), ChkStr = fscanf(fid,'%s',1);
ChkStr = fscanf(fid,'%s',1); EigenR = str2num(ChkStr);
if EigenR==EigenV(iMode), fgetl(fid); ChkStr = fscanf(fid,'%s',1);
if strcmp(ChkStr, 'CYCLES'), ChkStr = fscanf(fid,'%65c',1); % bypass 65 blanks
ChkStr = fscanf(fid,'%s',1); ModeNo = str2num(ChkStr);
if ModeNo==iMode, fgetl(fid); k=1;
while k<=NAset, ChkStr = fscanf(fid,'%s',1); AsetID = str2num(ChkStr);
if isInteger(AsetID)
if AsetID==Aset(k,1), ChkStr = fscanf(fid,'%s',1);
Mx = fscanf(fid,'%f',[1,6]); ModeS(k,iMode) = Mx(Aset(k,2)); k=k+1;
if k>NAset, break; end
while AsetID==Aset(k,1), ModeS(k,iMode) = Mx(Aset(k,2)); k=k+1;
if k>NAset, break; end; end
elseif AsetID > Aset(k,1),
error([PreS,'Can not find ASET ID - ',num2str(Aset(k,1)),EndS]); end
elseif strcmp(ChkStr,'EIGENVALUE')
ChkStr = fscanf(fid,'%s',1); EigenR = fscanf(fid,'%f',1);
if EigenR~=EigenV(iMode)
error([PreS,'Can not find ASET ID - ',num2str(Aset(k,1)),EndS]); end
end; fgetl(fid);
end; break
end; end; end; end; fgetl(fid);
end; end
% output arguments definition
if nargout >= 1,varargout{1} = GM;
if nargout >= 2, varargout{2} = FreqW;
if nargout >= 3, varargout{3} = ModeS; end;
end;
end
beep; disp([blanks(5),'*** Function is complete',EndS]);
return