NASA File parsing
NASA File Parsing
File Parsing in MATLAB:
In general, the term \"parsing\" refers to MATLAB reading an m-file (script or function) and translating it into an internal code that it stores in memory. It is this \"parsed\" file in memory that is actually run when you execute the code.
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H, and S. They have also documented the coefficients that are required to evaluate these polynomials for species, using that data file this programming is done using Matlab.
Function coding for Formula calculating the Cp, H, S:
In this coding using function command, the formula is developed.
Here temperatures vary from each species where the first 7 will be local high temperature and the rest 7 species will the local low temperature. So here we use if and else command to calculate both temperatures.
Formula:
#program
function [Cp,H,S] = inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
for i = 1:length(T)
if T(i)>Tm1
Cp= (c1 + c2.*T + c3.*T.^2 + c4.*T.^3 + c5.*T.^4).*R;
H = (c1+ (c2.*T)./2+ (c3.*T.^2)./3 + (c4.*T.^3)./4 + (c5.*T.^4)./5+ c6./T).*T.*R;
S = (c1.*log(T)+ c2.*T + (c3.*T.^2)./2 + (c4.*T.^3)./3+(c5.*T.^4)./4 + c7)*R;
else
Cp= (c8 + c9.*T + c10.*T.^2+ c11.*T.^3+ c12.*T.^4)*R;
H = (c8 + (c9.*T)./2 + (c10.*T.^2)./3 + (c11.*T.^3)./4 + (c12.*T.^4)./5 + c13./T)*R;
S = (c8.*log(T)+ (c9.*T) + (c10.*T.^2)./2 + (c11.*T.^3)./3+(c12.*T.^4)./4 + c14)*R;
end
end
end
Function coding for calculating the Molecular Weight
In this molecular weight is calculated using function command here atomic name and atomic weight are given as inputs to the species. A loop is created for calculating molecular weight of species individually,strcmp is used to compare species with the elements and calculate their molecular weight. Then string is converted in to number using str2double command.
#program
function molecular_weight_calculation = molecular_weight(sp_name)
species = (sp_name)
elements = [\'H\',\'C\',\'O\',\'N\',\'A\',\'S\'];
atomic_weight =[1.00794 12.0107 15.999 14.0067 39.948,32];
molecular_weight_calculation = 0;
for i = 1:length(species)
for j = 1:length(elements)
if strcmp(species(i),elements(j))
molecular_weight_calculation = molecular_weight_calculation+atomic_weight(j);
a = j;
end
end
n = str2double(species(i));
if n>1;
molecular_weight_calculation = molecular_weight_calculation+atomic_weight(a)*(n-1);
end
end
end
Main Program:
Firstly to read the data file fopen command is used, and to get the information fgetl is used.
Using strsplit we get the global temperatures and then using fgetl command we skip the lines which are not required.
Loop command is created to get the values of all the temperatures and coefficients present in the file
To get temperatures and coefficients values we used strfind to specify them accurately.
So the data is in string format in order to convert it into a numbers str2double command is used.
The function of the formula is called here for calculation.
To make a folder mkdir command is used by specifying the directory.
Then plotting is taken place using the command plot.
For changing directory, we use the command cd in order to save the plots of specific heat, entropy, enthalpy in each individual file of species.
Then the molecular weight is calculated by calling the molecular weight calculation function.
and the file is closed using the command fclose.
#program
clear all
close all
clc
f1 = fopen(\'THERMO.dat\',\'r\');
fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,\' \');
Global_low_temp = str2double(A{2});
Global_mid_temp = str2double(A{3});
Global_high_temp = str2double(A{4});
%Lines
for i = 3:5
g = fgetl(f1);
end
%temp
for j = 1:53
species = fgetl(f1);
t = strfind(species,\'.\');
%local temp
species(52-3:52+3);
Tl = species(52-3:t(1)+3);
Tl1 = str2double(Tl);
species(t(1)+6:62+3);
Th = species(t(1)+6:t(2)+3);
Th1 = str2double(Th);
species(t(2)+6:72+3);
Tm = species(t(2)+6:t(3)+3);
Tm1 = str2double(Tm);
sp_name = species(1:6);
% co-efficients1
coeff_line_1 = fgetl(f1);
A11 = strfind(coeff_line_1,\'E\');
coeff_line_1(1:12+3);
a1 = coeff_line_1(1:A11(1)+3);
c1 = str2double(a1);
a2 = coeff_line_1(A11(1)+4:A11(2)+3);
c2 = str2double(a2);
a3 = coeff_line_1(A11(2)+4:A11(3)+3);
c3 = str2double(a3);
a4 = coeff_line_1(A11(3)+4:A11(4)+3);
c4 = str2double(a4);
a5 = coeff_line_1(A11(4)+4:A11(5)+3);
c5 = str2double(a5);
% Co-efficients 2
coeff_line_2 =fgetl(f1);
A12 = strfind(coeff_line_2,\'E\');
coeff_line_2(1:12+3);
a6 = coeff_line_2(1:A12(1)+3);
c6 = str2double(a6);
a7 = coeff_line_1(A12(1)+4:A11(2)+3);
c7 = str2double(a7);
a8 = coeff_line_1(A12(2)+4:A11(3)+3);
c8 = str2double(a8);
a9 = coeff_line_1(A12(3)+4:A11(4)+3);
c9 = str2double(a9);
a10 = coeff_line_1(A12(4)+4:A11(5)+3);
c10 = str2double(a10);
% co-effecient 3
coeff_line_3 = fgetl(f1);
A13 = strfind(coeff_line_3,\'E\');
coeff_line_3(1:12+3);
a11 = coeff_line_2(1:A13(1)+3);
c11 = str2double(a11);
a12 = coeff_line_1(A13(1)+4:A11(2)+3);
c12 = str2double(a12);
a13 = coeff_line_1(A13(2)+4:A11(3)+3);
c13 = str2double(a13);
a14 = coeff_line_1(A13(2)+4:A11(4)+3);
c14 = str2double(a14);
T = linspace(Tl1,Th1,100);
R = 8.314;
[Cp,H,S] = inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
mkdir(\'C:\\Users\\hp\\Documents\\MATLAB\\ASSIGNMENT\\NASA File parsing\\Species\',sp_name)
%plotting
figure(1)
plot(T,Cp,\'LineWidth\',3,\'Color\',\'b\');
xlabel(\'Temp\');
ylabel(\'Specific Heat\');
title(sp_name);
figure(2)
plot(T,H,\'LineWidth\',3,\'Color\',\'b\');
xlabel(\'Temp\');
ylabel(\'Entropy\');
title(sp_name);
figure(3)
plot(T,S,\'LineWidth\',3,\'Color\',\'b\');
xlabel(\'Temp\');
ylabel(\'Enthalpy\')
title(sp_name);
program = ([\'C:\\Users\\hp\\Documents\\MATLAB\\ASSIGNMENT\\NASA File parsing\\Species\\\',sp_name]);
cd(program)
saveas(1,\'Specific Heat.jpg\')
saveas(2,\'Entropy.jpg\')
saveas(3,\'Enthalpy.jpg\')
cd(\'C:\\Users\\hp\\Documents\\MATLAB\\ASSIGNMENT\\NASA File parsing\')
molecular_weight_calculation = molecular_weight(sp_name)
end
fclose(f1)
Results
1. Screenshots of the saved files :
2.Plots for O2, N2 and Co2 species
a.Plots for O2
b.plots for N2
c.Plots for Co2
3.Molecular Weight calculation
Errors:
The above error is because mentioning the directory correctly later it was corrected it.
This error is because of using negative integers in the array later it was replaced by logical values.