matlab tecplot data,MATLAB讀取TECPLOT笛卡爾網格三維流場數據

基於笛卡爾網格的三維流動數值模擬,其流場信息可以通過tecplot格式進行輸出,方便tecplot對流場進行可視化處理,但對數據進行在加工時,還是導入matlab中比較方便,那么對於一個tecplot數據文件,matlab是不能直接讀取的,必須有一個函數將tecplot數據文件中數據轉換成matlab便於操作的數據格式。

tecplot數據文件前三行是文件頭,其中第一行是數據文件說明,第二行中是文件中所定義的變量名,第三行,對於笛卡爾網格的流場,其包含了每個空間方向上離散的數據數目。通過處理第二行文本,可以獲取所定義的變量及其數目。

tecplot文件中數據一行是一個記錄,一行中數據的順序和文件頭中第二行定義的變量順序相對應,通常前三個數據是x,y,z,對應網格點空間位置。

Title= "simulation data"

VARIABLES= "X","Y","Z","U","V","W","RHO"

ZONE T= "BOX",I= 100,J=321,K=100,F= POINT

0 0 0 0.000252868 0.00386761 -0.00194455 1000.01

1 0 0 -0.000252631 -0.00258331 0.00188909 1000.01

2 0 0 0.000252594 0.00441002 -0.00183506 1000.01

3 0 0 -0.000252256 -0.0019755 0.00178188 1000

4 0 0 0.000252931 0.00492305 -0.00173004 1000

...

1、matlab讀取tecplot文件,通過讀取文件頭獲取文件所定義變量以及變量數目,同時讀取文件中所包含數據信息,所讀取的數據保存在一個四維數組中,最后一個維度代表每個變量,變量名保存在一個元胞數組中。

1.1 tecplot數據為空間三維流場,tecplot2mat_3D

% read data from tecplot file, and save the variables to mat

% filename: the name of the tecplot file including the extensions

% var: the data of variables, is a four dimensions array, the last dimension is the the number of variable

% var_name: the name of the variables, is a cell

% var_num: the number of the variables

function [var,var_name,var_num] = tecplot2mat_3D(filename)

%% tecplot data file read

% open the file

fid = fopen(filename);

% read the second line of data file

[~] = fgetl(fid);

str = fgetl(fid);

% get the number of the variables

o1 = regexpi(str,'"','start');

var_num = length(o1)/2;

% get the name of the variables

var_name = cell(1,var_num);

for i = 1:var_num

var_name{1,i} = str(o1(2*i-1)+1:o1(2*i)-1);

end

% read the data

strformat = repmat('%f',1,var_num);

data = textscan(fid,strformat,'headerlines',1);

data = cell2mat(data);

% close the file

fclose(fid);

%% reshape data

% get discrete points

xi = sort(unique(data(:,1)));

yi = sort(unique(data(:,2)));

zi = sort(unique(data(:,3)));

% number of the discrete points

num_x = length(xi);

num_y = length(yi);

num_z = length(zi);

% initialize the three demonsions array

var = zeros(num_x,num_y,num_z,var_num);

% assignment the array according to the data

for n = 1:size(data,1)

% method 1: we don't know the relationship between the number and the index, we must find the index according to the value

% index_x = find( data(n,1) == xi );

% index_y = find( data(n,2) == yi );

% index_z = find( data(n,3) == zi );

% method 2: we know the relationship between the value and the index, we can directly access the index

index_x = data(n,1) + 1;

index_y = data(n,2) + 1;

index_z = data(n,3) + 1;

% access the data

for i = 1:var_num

var(index_x,index_y,index_z,i) = data(n,i);

end

end

fprintf('reshape the data\n');

%% data save to mat

index_str = find( '.' == filename );

if isempty(index_str)

else

filename = filename( 1:index_str-1 );

end

eval(['save ',filename,'.mat var var_name var_num;']);

fprintf('save the data\n');

end

1.2 tecplot數據為空間二維流場,tecplot2mat_2D

% read data from tecplot file, and save the variables to mat

% filename: the name of the tecplot file including the extensions

% var: the data of variables, is a three dimensions array, the last dimension is the the number of variable

% var_name: the name of the variables, is a cell

% var_num: the number of the variables, is a number

function [var,var_name,var_num] = tecplot2mat_2D(filename)

%% tecplot data file read

% open the file

fid = fopen(filename);

% read the second line of data file

[~] = fgetl(fid);

str = fgetl(fid);

% get the number of the variables

o1 = regexpi(str,'"','start');

var_num = length(o1)/2;

% get the name of the variables

var_name = cell(1,var_num);

for i = 1:var_num

var_name{1,i} = str(o1(2*i-1)+1:o1(2*i)-1);

end

% read the data

strformat = repmat('%f',1,var_num);

data = textscan(fid,strformat,'headerlines',1);

data = cell2mat(data);

% close the file

fclose(fid);

%% reshape data

% get discrete points

xi = sort(unique(data(:,1)));

yi = sort(unique(data(:,2)));

% number of the discrete points

num_x = length(xi);

num_y = length(yi);

% initialize the three demonsions array

var = zeros(num_x,num_y,var_num);

% assignment the array according to the data

for n = 1:size(data,1)

% method 1: we don't know the relationship between the number and the index, we must find the index according to the value

% index_x = find(data(n,1) == xi);

% index_y = find(data(n,2) == yi);

% method 2: we know the relationship between the value and the index, we can directly access the index

index_x = data(n,1) + 1;

index_y = data(n,2) + 1;

% access the data

for i = 1:var_num

var(index_x,index_y,i) = data(n,i);

end

end

fprintf('reshape the data\n');

%% data save to mat

index_str = find( '.' == filename );

if isempty(index_str)

else

filename = filename( 1:index_str-1 );

end

eval(['save ',filename,'.mat var var_name var_num;']);

fprintf('save the data\n');

end

2、測試腳本,讀取給定的TECPLOT文件名,輸出文件包含數據以及將文件中定義的變量加載到MATLAB工作區

clc;clear;

close all;

filename = 'U3D.dat';

[var,var_name,var_num] = tecplot2mat_3D(filename);

for i = 1:var_num

eval([var_name{1,i},'=var(:,:,:,i);']);

end

3、測試結果,tecplot文件定義的變量就全部加載到工作區了。

23918da50b6651169e05657f17459656.jpe

40817fe7be0b8069fad0748e26507227.png

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值