Wednesday, April 1, 2015

Vulcan-CFD MATLAB files

I've written a number of MATLAB files to assist my work in Vulcan-CFD.  Below, I've listed the code for each of these files.


Convert Plot3D files to Plot2D

clc; close all; clear all;

% input
d='C:\Users\John\Documents\Pointwise\backwardStepEx';
filename='backwardStepEx_rev1.x';


%% read in 2D(x=m,y=n,z=1) data
cd(d)
inputFile=filename;
outputFile=[filename(1:end-2) '_Plot2D.x'];
A0 = importdata(inputFile);  %raw data
A=A0;  %raw data to be trucated

% number of blocks in data
numBlocks=A(1);

% grab matrix sizes from the header
for i = 1:numBlocks
    m(i)=A(2+(i-1)*3);
    n(i)=A(3+(i-1)*3);
end

% throw away header info from A
A=A(2+numBlocks*3:end);

% grab x and y data from A and ignore z
for i = 1:numBlocks
    N=m(i)*n(i);
    x{i}=A(1:N);
    A=A(N+1:end);
    y{i}=A(1:N);
    A=A(N+1:end);
    A=A(N+1:end); %delete z
end

% set origin to bottom left.  
xmin=x{1}(1);
ymin=y{1}(1);
for i = 1:numBlocks
    for j=1:length(x{i})
        if xmin > x{i}(j);
            xmin = x{i}(j);
        end
        if ymin > y{i}(j);
            ymin = y{i}(j);
        end
    end
end
for i = 1:numBlocks
    x{i}(:)=x{i}(:)-xmin;
    y{i}(:)=y{i}(:)-ymin;
end
%% plot data
% figure; hold on
% colors=['brkgc'];
% for i = 1:numBlocks
%     plot(x{i},y{i},[colors(i),'+'])
% end


%% write 2D(x=m,y=n) data (with the single z value stripped out)
fileID = fopen(outputFile,'w');

% write number of blocks
fprintf(fileID,[num2str(numBlocks) '\r\n']);

% write matrix sizes for each block
for i = 1:numBlocks
    fprintf(fileID,[num2str(m(i)), '\t', num2str(n(i)), '\r\n']);
end

% write x and y data for each block
for i = 1:numBlocks
    p=x{i};
    for k=1:2
        if k==1
            p=x{i};
        else
            p=y{i};
        end
        
        while(length(p)>=4)
    %         fprintf(fileID,[num2str(p(1)), '\t', num2str(p(2)), '\t', num2str(p(3)), '\t',num2str(p(4)), '\r\n']);
            fprintf(fileID,'%1.15e\t%1.15e\t%1.15e\t%1.15e\r\n',p(1),p(2),p(3),p(4));
            p=p(5:end);
        end
        if p~=0
            for j=1:length(p)
                fprintf(fileID,'%1.15e\t', p(j));
            end
            fprintf(fileID,'\r\n');
        end
    
    end
end

fclose('all');

beep


Add Zeros to Time History Files

% The time_merge script with Vulcan-CFD puts writes the files names with numbers that are not padded with zeros.  (E.g.  file_7.f instead of file_0007.f).  Tecplot, however, has trouble determining the correct file order when importing if the buffer zeros are not present.  This code adds the buffer zeros.
clc; close all; clear variables;

cd(uigetdir)
% cd('\\crunch6\John\circleVortexShedding\Time_files\Plot3d_files')
% read in all files with a *.f extension
firstFileList = dir('*.f');

% this next section of code further filters the list of files without a
% single '_' character.  
j=1;
for i=1:length(firstFileList)
    if length(findstr(firstFileList(i).name,'_'))==1
        secondFileList(j)=firstFileList(i);
        j=j+1;
    end
end

files=secondFileList;

n=length(files);
m=length(num2str(n));


for i=1:n
    oldname=files(i).name;
    % find number between the '_' and '.' characters
    num=oldname(findstr(oldname,'_')+1:findstr(oldname,'.')-1);
    
    % buffer with zeros
    while length(num)<m
        num=['0',num];
    end
    
    %rename file
    newname=[oldname(1:findstr(oldname,'_')),num,'.f'];
    if strcmp(oldname,newname)==0
%     dos(['rename "' oldname '" "' newname '"']); % (1)
        movefile(oldname,newname);
    end
end