Parsing NASA thermodynamic data in matlab
AIM:
To write the code in matlab to parse the nasa thermodynamic file and calculate the thermodynamic properties of different gases species.
OBJECTIVE:
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
To guide you we have broken down the problem into meaningful bits that you can work on individually.
1. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data(Tip: use fopen and fgetl). The formulae are shown below.
Here R is the universal gas constant, T is the temeprature
The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
Also use the LOCAL TEMPERATURE RANGES which is unique for each individual species, not the GLOBAL TEMPERATURE RANGE.
2. Calculate the molecular weight of each species and display it in the command window.
3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
5. The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
6. Please write a detailed report to describe your logic.
PARSING:
Data parsing is a process in which a string of data is converted from one format to another.
ENTROPY:
Amount of energy in the system no longer available to do mechanical work.
ENTHALPY:
Enthalpy is defined as quantity of energy.
1-Total enthalpy(H):
Total enthalpy is the product of mass and specific enthalpy. The unit of total enthalpy is KJ.
2-Specific enthalpy(h):
Specific enthalpy is the sum of internal energy and the product of pressure and velocity. The unit of specific enthalpy is KJKG.
SPECIFIC HEAT:
specific heat is the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.
CODE:
clear all
close all
clc
f1=fopen('THERMO.dat','r');
d=fgetl(f1);
t=fgetl(f1);
t=strsplit(t);
low_temp=str2num(t{2});
mid_temp=str2num(t{3});
high_temp=str2num(t{4});
for i=1:3;
d=fgetl(f1);
end
for m=1:53;
s=fgetl(f1);
s=strsplit(s);
species=(s{1});
temp1=str2num(s{end-1});
temp2=str2num(s{end-2});
temp3=str2num(s{end-3});
line1=fgetl(f1);
a=findstr(line1,'E');
a1=str2num(line1(1:a(1)+3));
a2=str2num(line1(a(1)+4:a(2)+3));
a3=str2num(line1(a(2)+4:a(3)+3));
a4=str2num(line1(a(3)+4:a(4)+3));
a5=str2num(line1(a(4)+4:a(5)+3));
line2=fgetl(f1);
a=findstr(line2,'E');
a6=str2num(line2(1:a(1)+3));
a7=str2num(line2(a(1)+4:a(2)+3));
a8=str2num(line2(a(2)+4:a(3)+3));
a9=str2num(line2(a(3)+4:a(4)+3));
a10=str2num(line2(a(4)+4:a(5)+3));
line3=fgetl(f1);
a=findstr(line3,'E');
a11=str2num(line3(1:a(1)+3));
a12=str2num(line3(a(1)+4:a(2)+3));
a13=str2num(line3(a(2)+4:a(3)+3));
a14=str2num(line3(a(3)+4:a(4)+3));
teml=linspace(temp1,temp2,100);
temh=linspace(temp3,temp1,100);
r=8.314;
cph=r.*(a1 + a2.*teml + a3.*teml.^2 + a4.*teml.^3 + a5.*teml.^4);
Hh=( a1 + (a2.*teml)/2+(a3.*teml.^2)/3+(a4.*teml.^3)/4+(a5.*teml.^4)/5+ a6.*teml ).*r.*teml;
Sh=r.*(a1.*log(teml) + a2.*teml + (a3.*teml.^2)/2 + (a4.*teml.^3)/3 + (a5.*teml.^4)/4 + a7);
cpl=r.*(a8 + a9.*temh + a10.*temh.^2 + a11.*temh.^3 + a12.*temh.^4);
Hl=(a8 + (a9.*temh)/2+(a10.*temh.^2)/3+(a11.*temh.^3)/4+(a12.*temh.^4)/5+a13.*temh).*r.*temh ;
Sl=r.*(a8.*log(temh) + a9.*temh + (a10.*temh.^2)/2 + (a11.*temh.^3)/3 + (a12.*temh.^4)/4 + a14);
figure(1)
plot([temh,teml],[cpl,cph])
xlabel('temperature')
ylabel('specific heat')
title(species)
figure(2)
plot([temh,teml],[Hl,Hh])
xlabel('temperature')
ylabel('enthalpy')
title(species)
figure(3)
plot([temh,teml],[Sl,Sh])
xlabel('temperature')
ylabel('entropy')
title(species)
mkdir(species)
cd(species)
saveas(1,'t_vs_cp.jpg')
saveas(2,'t_vs_H.jpg')
saveas(3,'t_vs_S.jpg')
cd ..
M_W=molecular_weight(species);
sprintf('molecular weight of %s=%d',species,M_W)
end
FUNCTION CODE FOR MOLECULAR WEIGHT:
function mw=molecular_weight(species)
element=['H','O','N','R','C'];
atomic_weight=[1,16,14,40,12];
mw=0;
for i=1:length(species)
for j=1:length(element)
if strcmp(species(i),element(j))
mw=mw+atomic_weight(j);
p=j;
end
end
a=str2num(species(i));
if a>1
mw=mw+atomic_weight(p)*(a-1);
end
end
end
EXPLANATION:
-As usual we are going to start the code with clear all, close all, clc to delete the previous command ,workspace and plots.
-We are going parse the nasa thermodynamic file using the command fopen and fgetl.
-fgetl is used get the first line from tha data and when we use this command again it goes to next line.
-{} is used to covert the cell into the array.
-str2num is used to convert the string into number.
-strsplit is used to split the string. findstr is used to find the character in string.
-Using the fopen,fgetl,str2num,strsplit and findstr , we can get the golbal temperatures, species temperature and 14 co_efficient values of spicies.
-Using loop for 53 times , to get values for all 53 species.
-With help of all values, we can find the specific heat , entropy and enthalpy.
-plot the graph t vs cp ,t vs h, t vs s.Before ploting the graph ,we need to set the working directory and save as the all plots in as that directory.
-Finally,we need to calculate the molecular weight. we need to compare the species and element but if it is not match , then convert the string into the number and make the formula little bit change.
OUTPUTS OF O2,N2 & C02:









OUTPUT OF MOLECULAR WEIGHT:









Screenshot of the window where all the folders are saved:



PLOTS OF DIFFERENT SPECIES IN GOOGLE DRIVE:
https://drive.google.com/drive/folders/1FSu7FBoPfRcR0JJJzjuixsYoQruqLQM1?usp=sharing
CONCLUTION:
Finally , we calculated the thermodynamic properties(cp,h,s) and molecular weight using the nasa thermodynamic data file.