Project 1 - Parsing NASA thermodynamic data
Aim :- To write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various species.
Introduction :-
File Parsing -
file parsing is all about reading the information from the text file & how thet information can be used in a computer language. By using the file parsing we can decode parsing method. file parsing can be used to do the calculations required for the specific purpose of the application. parsing helps us make something understandable by analyzing its parts &bits. This is done by partially analyzing the data understanding its underlying structure ( By making some assumpstions based on whta yuo're expecting to see), and then explicitly representing thet structure in the code.
Input equation :-
Procedure :- The value of universal gas R is given by as 8.31 as an input. then the given THERNO.dat file has to be accessed to obtain the necessary data 'fopen' cammand is used to open the desired file and the keyword 'r' is used to read the file. this thermo.dat file is assign to a variable 'f1' th efile is read line by line 'fget' command. the first line represents the tittle so it is ignored. the second line has the global maximum, global mid and global minimum values which is split into three values by using strsplit command and converted into number AN array of temperature is generated using linspace command the next three lines are ignored as they contains only commands. A for loop is created to read the data of all the species. 'fgetl' command inside the loop makes our task easier then the next three lines of the file is red which contains the high and low temperature data of the particular species. the read lines are in the form of s string so they are converted into numbers using the strsnum command and stored in specific variables so they can be used for further calculations. the low temperatures values are stored in variables lc1, lc2, lc3, lc4, lc5, lc6, lc7 and high temperature values are stored in variables hc1, hc2, hc3, hc4, hc5, hc6, hc7 the next part of program contains a for loop formulas to calculate specific heat, enthalpy, entropy for lower temperatures given T<=t. an else comman is used to calculate the thermodynamic properties at higher temperature if the temperature fails to satisfy the criteriam given above plot of specific heat vs temperature is plotted using the save as command the three figures are saved in the new folder which is created by element name. using the "cd" command the folder is back to the original folder. the functions runs for 52 times through the for loop and creates seperate folder for each species and saves all the plots in the folders. molecular weght of different species is calculated.
MATLAD Code :-
clear all
close all
clc
R=8.314;
f1=fopen('THERMO.DAT','R');
%preprocessing
header=fget1(f1);
overglobaltemperatureranges=fget1(f1);
a=strsplit(overglobaltemperatureranges,' ');
low_global_temperature=str2num(a{2});
medium_global_temperature=str2num(a{3});
high_global_temperature=str2num({4});
for i=1:3;
erase=fget1(f1);
end
%parsing the species
for j=1:53;
overal_local_temperature_ranges=fget1(f1);
b=strsplit(overal_local_temperature_ranges,' ' );
low_local_temperature=str2num(b{end-3});
medium_local_temperature=str2num(b{end-1});
high_local_temperature=str2num(b{end-2});
species_name=b{1};
temperature_range=linspace(low_local_temperature,high_local_temperature,200);
coef_line_1=fget(f1);
coef_line_2=fget(f1);
coef_line_3=fget(f1);
%calculating the values of coefficients
x=findstr(coef_line_2,'E');
a6=str2num(coef_line_2(1:y(1)+3));
a7+str2num(coef_line_2(y(1)+4:y(2)+3));
a8+str2num(coef_line_2(y(2)+4:y(3)+3));
a9+str2num(coef_line_2(y(3)+4:y(4)+3));
a10+str2num(coef_line_2(y(4)+4:y(5)+3));
z=findstr(coef_line_3,'E');
a11=str2num(coef_line_3(1:z(1)+3));
a12=str2num(coef_line_3(z(1)+4:z(2)+3));
a13=str2num(coef_line_3(z(2)+4:z(3)+3));
a14=str2num(coef_line_3(z(3)+4:z(4)+3));
%calculating the values of cp, enthalpy and entropy
[cp h s]=thermodynamic_properties(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,temperature_range,medium_global_temperature);
%calculating the molecular weight of species
[molecular_weight(j)]=molecular_mass(species_name);
%plotting
set(0,'defaultfigurevisible','off')
m(1)=figure;
plot(temperature_range,cp,'linewidth',3,'color','g');
xlabel('temperature(K)');
ylabel('specific heat (J/kg-k)');
title(['specific heat vs temperature',species_name]);
m(2)=figure;
plot(temperature_range,ch,'linewidth',3,'color','r');
xlabel('temperature(K)');
ylabel('enthalpy (J/kg)');
title(['enthalpy vs temperature',species_name]);
m(3)=figure;
plot(temperature_range,s,'linewidth',3,'color','y');
xlabel('temperature(K)');
ylabel('entropy (J/kg-k)');
title(['entropy vs temperature',species_name]);
%printing molecular weight data in command window
fprintf('species name :%6s ',species_name)
fprintf('molecular weight : %15s n',molecular_weight(j))
cd('D:species');
mkdir(species_name);
cd(species_name);
%saving as .jpg for windows explorer
saveas(m(1),'specific_heat_vs_temp.jpg');
saveas(m(2),'enthalpy_vs_temp.jpg');
saveas(m(3),'entropy_vs_temp.jpg');
%saving as .fig for MATLAB
saveas(m(1),'specific_heat_vs_temp.jpg');
saveas(m(2),'enthalpy_vs_temp.jpg');
saveas(m(3),'entropy_vs_temp.jpg');
%saving data to file
f2=fopen('data_parsing.txt','w');
fprintf(f2, '%15s : %15s n' : 'species name',species_name);
fprintf(f2, '%15s : %15.3s n' : 'molecular weight',molecular_weight(j));
fprintf(f2, ' for the local temperature range : %3.1fk to %3.1fk nn',low_local_temperature,high_local_temperature);
fprintf(f2, '6s %6s %16s %16s n' , 'temperature(k)' , 'specific heat(j/kg)' , 'enthalpy(j/kg)' , 'entropy(j/k');
fprintf(f2, '%11.2f %17.2f %19.2f %20.2f n' ,temperature_range,cp,h,s);
cd('D:matlabbin');
end
fprintf('n the input file has been stored nn')
%getting input from the user to display plots
input=input('do you require data for specific species? Y/N' , 's')
if strcmpi(input,'y')
name=input ('provide name of species :','s')
cd('D:species')
cd(name)
openfig('specific_heat_vs_temp','visible');
openfig('enthalpy_vs_temp' , 'visible');
openfig('entropy_vs_temp' , 'visible');
fprintf('the plots corresponding to the species have been opened n')
fprintf('n the program has ended n')
elseif strcmpi(input,'N')
fprintf('invalid input n')
fprintf('n the program has ended n ')
end
Thermodynamics propeties of functions codes
function[cp,h,s]= thermodynamics_properties(a1, a2,a3,a4,a5,a6,a7,a8,a9,a10, a11,a12,a13,a14,R,temperature_ range,medium_global_ temperature)
for k=1:length(temperature_range);
if temperature_range(k)>medium_ global_temperature
cp(k)=R.*(a1+(a2.*temperature_ range(k))+(a3.*(temperature_ range(k)).^2)+(a4.*( temperature_range(k)).^3)+(a5. *(temperature_range(k).^4)));
h(k)=R.*temperature_range(k).* (a1+(a2.*temperature_range(k)) ./(2)+(a3.*(temperature_range( k)).^2)./(3)+(a4.*( temperature_range(k)).^3)./(4) +(a5.*(temperature_range(k).^ 4)./(5)+(a6)./(temperature_ range(k)));
s(k)=R.*((a1.*log(temperature_ range(k)))+(a2.8temperature_ range(k))+(a3.*(temperature_ range(k)).^2+(a4.*( temperature_range(k)).^4)./(4) +(a7));
else
cp(k)=R.*(a8+(a9.* 9temperature_range(k))+(a10.*( temperature_range(k)).^2)+( a11.*(temperature_range(k)).^ 3)+(a12.*(temperature_range(k) ).^4)));
h(k)=R.*temperature_range(k).* (a8+(a9.*temperature_range(k)) ./(2)+(a10.*(temperature_ range(k).^2)(3)+9a11.*( temperature_range(k)).^4)./(5) +(a13./(temperature_range(k))) ;
s(k)=R.*((a8.*log(temperature_ range(k)).^3)./(3)+(a12.*( temperature_range(k)).^4)./(4) +a14);
end
end
Molecular weight function code
this function for molecular weights calculations requires element as input argument and it is saved in the varibles 'element' and the output argument is molecular weight.
1. A string array is created which contains the neme of atoms.
2. A variableatomic_wt is created which contains the atomic weight of respectives atoms in the string array.
3. Then used 'strsmp' command to compare the name of element with thw string array.
4. If there is a repetition of an element . 'str2double' is used to find that and (n-1) is multiplied to atomic_wt of the lement to calculate molecular weight.
function [molecular_weight]=molecular_ mass(species_name)
atoms=['O','C','H','A','N'];
atomic_weight=[15.999 , 12.0107 , 1.00784 , 39.948 , 14.0067];
molecular_weight=0
x=1;
for i=1:length(species_name);
for j=1:length(atoms);
if strcmp(species_name(i),atoms( j))==1;
molecular_weight=atomic_ weight(j)+molecular_weight;
atomicweight=atomic_weight(j);
end
end
t=isstrprop(species_name(i),' digit');
c=str2double(regexp(species_ name,'d*','match'));
if t==1
molecular_weight=atomicweight* (c(x)-1)+molecular_weight;
x=x+1;
end
end
end
O2 PLOTS
CO2 PLOTS
N2 PLOTS
CONLCUSION
Here we can conduct he file parsing is the best approsch to parse any file with large data available in the file using any programming software for extracting the decoding data available in the file. this type of filr parsing is very usefulm in current era of industries where we transfer the large engineering data file from one place to another.