Wednesday, November 5, 2014

Pointwise - Converting 2D Plot3d grid files to 2D Plot2d

Introduction
I was trying to use Pointwise to generate Plot2d grids, and I discovered that Pointwise appears to be only capable of generating Plot3d grids.  I couldn't find a solution inside of Pointwise so I wrote the Matlab script below to address it.

A quick explanation on the difference between 2D-Plot3d and 2D-Plot2D grids.  When generating a 2D grid in Pointwise, the program outputs a z-coordinate for each point (which is always 0).  Here's an example of the difference between the 2D-Plot3d and 2D-Plot2d grid file:

2D-Plot3d
2
30 30 1
59 30 1
...xdata(block 1)...
...ydata(block 1)...
...zdata(block 1) = 0...
...xdata(block 2)...
...ydata(block 2)...
...zdata(block 2) = 0...

2D-Plot2d
2
30 30
59 30
...xdata(block 1)...
...ydata(block 1)...
...xdata(block 2)...
...ydata(block 2)...

Why does this matter?  

First, Vulcan doesn't care.  When importing the grid file into vulcanig, the user can specify if it is a Plot3D or Plot2D file, and Vulcan will take care of it.

However, TecPlot DOES care.  Because the output of vulcan is in a true 2D format, TecPlot cannot plot the solution against the 2D-Plot3D grid.  It needs the 2D-Plot2D grid.  


The code

The following code is written in MATLAB

clc; close all; clear all;

%% input
inputFile=['C:\Users\John\Documents\PointwiseExample','\','expansionChamber.x'];
outputFile='grid.x';

%% read in 2D(x=m,y=n,z=1) data
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


%% plot data
figure; hold on
colors=['brkg'];
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






No comments:

Post a Comment