Project

Parsing NASA thermodynamic data using Matlab

AIM - To write a code in Matlab to parse the NASA thermodynamic data and then to calculate the thermodynamic properties of gaseous species.

OBJECTIVE -

1.To write a MATLAB coding for reading the NASA thermodynamic data file.

2.Coding to extract the coefficient from the file.

3.Coding to calculate thermodynamic properties with the help of extracted coeffiecnt.

4.To calculate the molecular weight of the species.

5.Saving the graph with appropriate name and dump them in seperate folder for each species with suitable name.This will be generated automatically.

INTRODUCTION -

File Parsing means reading a data stream of some sort and making a memory model of the content of that data.Here, we have been given a NASA THERMODYNAMIC DATA which has a data of high temperature and low tempertaure of 53 gaseous species.We have to parse the data file through Matlab and have to calculate the molecular weight for the respective gases.

DEFINATIONS :

Specific Heat : It is the amount of heat per unit mass required to raise the temperature by one degree celsius.

Enthalpy : It is a thermodynamic quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.

Entropy : It is a thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system.

Molecular mass :The molecular mass is the mass of a given molecule.

Atomic mass: It is the mass of a single atom of a chemical element. It includes the masses of the 3 subatomic particles that make up an atom: protons, neutrons and electrons.

Here, 'R' is the universal gas constant and 'T' is the temperature.

UNDERSTANDING THE DATA:

1. The file starts with a header and the file ends with a 'END' character. The file contains 218 lines.

2. The speices data has 14 coefficients saved in 4 lines.

3. The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low -temperature coefficients.

4.The data starts with species name and detail of its state, then the temperature limts are given and finally the value of coefficients are given.

PROGRAMMING ALONG WITH EXPLANATION:

Code for the first five line of the data

clear all 
close all 
clc 

f = fopen('THERMO.dat','r')

% getting heading 

heading = fgetl(f)

%Global temperature value

global_temp = fgetl(f)

X = strsplit(global_temp,' ')

global_low_temp = str2num(X{2})
global_mid_temp = str2num(X{3})
global_high_temp = str2num(X{4})

fgetl(f)
fgetl(f)
fgetl(f)

EXPLANATION:

1.The program starts with 'clear all','close all' and 'clc'.

2. fopen opens an existing file and assigning it to a variable 'f'.

3. The 'fgetl' reads the the first line from the file and it is assigned to a variable heading,this will print:

4. the next line is read using the same command of 'fgetl' and assigning it to a variable called 'global_temp'.Then we use 'strsplit' to split the data into a array cell and assigning it to a variable 'X'. In order to convert the cell element from string to number we have used 'str2num', and assaining to a respective variable.The output will be

5.The next three line from the data file have no significance to our analysis so we skip those three line.

Code for reading the 53 species and extracting the coefficient values of the same.

for i = 1:53
            
    
line1 = fgetl(f);
sp_name=line1(1:6);

local_temp1=line1(49:55);
ltemp=str2num(local_temp1);

local_temp3=line1(58:65);
htemp=str2num(local_temp3);

local_temp2=line1(68:75);
atemp=str2num(local_temp2);

line2 = fgetl(f);
line3 = fgetl(f);
line4 = fgetl(f);
a=findstr(line2,'E');
b=findstr(line3,'E');
c=findstr(line4,'E');



%extracting the coefficients of high temp
a1=str2num(line2(1:a(1)+3));
a2=str2num(line2(a(1)+3:a(2)+3));
a3=str2num(line2(a(2)+4:a(3)+3));
a4=str2num(line2(a(3)+4:a(4)+3));
a5=str2num(line2(a(4)+4:a(5)+3));
a6=str2num(line3(1:b(1)+3));
a7=str2num(line3(b(1)+4:b(2)+3));
%extracting the coefficients of low temp
a8=str2num(line3(b(2)+4:b(3)+3));
a9=str2num(line3(b(3)+4:b(4)+3));
a10=str2num(line3(b(4)+4:b(5)+3));
a11=str2num(line4((1:c(1)+3)));
a12=str2num(line4(c(1)+4:c(2)+3));
a13=str2num(line4(c(2)+4:c(3)+3));
a14=str2num(line4(c(3)+4:c(4)+3));

t = linspace(ltemp,htemp,500);

for j= 1:500
 
    if t>=atemp
    cp=(a1+a2.*t+(a3.*t.^2)+(a4.*t.^3)+(a5.*t.^4)).*R;
    h=(a1+(a2.*t./2)+(a3.*(t.^2)./3)+(a4.*(t.^3)./4)+(a5.*(t.^4)./5)+(a6./t)).*R.*t;
    s=(a1*log(t)+a2.*t+(a3.*(t.^2)./2)+(a4.*(t.^3)./3)+(a5.*(t.^4)./4)+a7).*R;
    else
     cp=(a8+a9.*t+(a10.*t.^2)+(a11.*t.^3)+(a12.*t.^4)).*R;
    h=(a8+(a9.*t./2)+(a10.*(t.^2)./3)+(a11.*(t.^3)./4)+(a12.*(t.^4)./5)+(a13./t)).*R.*t;
    s=(a8*log(t)+a9.*t+(a10.*(t.^2)./2)+(a11.*(t.^3)./3)+(a12.*(t.^4)./4)+a14).*R;
    
    end
end

EXPLANATION:

1. We use "for loop" to get all the 53 species.Then in order to read the next line we use, 'fgetl'.To read the values of that line we assign it to variable 'line1'."sp_name" variable is defined to get the species name

2.In order to find the low temperature, average temperature and high temperature, we extract the data from the line by locating the values from the same line and storing them in the variables "ltemp", "atemp" and "htemp" respectively.

3.Now, we are getting the next three lines and saving them in the variables called "line1","line2" and "line3". The common thing in these lines are "E", in order to find the letter "E" from the line we use "strfind", and saivng them in variable called "a","b" and "c" respectively

4.Since, we now know the position of "E" in each line we can find the coefficients of high temperature and low temperture. The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low -temperature coefficients.

5.Then we use "linspace" command to get 500 equal values between low tempertaure and high temperature.Now we have all the 14 coeffiecents at our hand, now we can use the formula for Specific heat at constant pressure(Cp),Enthalpy(H) and Entropy(S) in another for loop. Now,we close both the for loops.

FULL MATLAB CODING :

clear all 
close all 
clc 

f = fopen('THERMO.dat','r');
R = 8.314;
% getting heading 

heading = fgetl(f);

%Global temperature value

global_temp = fgetl(f);

X = strsplit(global_temp,' ');

global_low_temp = str2num(X{2});
global_mid_temp = str2num(X{3});
global_high_temp = str2num(X{4});

fgetl(f);
fgetl(f);
fgetl(f);
mkdir('C:\Users\Skill_Lync\Desktop\temp_prog\Results');
%there are total 53 species 
for i = 1:53
            
    
line1 = fgetl(f);
sp_name=line1(1:6);

local_temp1=line1(49:55);
ltemp=str2num(local_temp1);

local_temp3=line1(58:65);
htemp=str2num(local_temp3);

local_temp2=line1(68:75);
atemp=str2num(local_temp2);

line2 = fgetl(f);
line3 = fgetl(f);
line4 = fgetl(f);
a=findstr(line2,'E');
b=findstr(line3,'E');
c=findstr(line4,'E');



%extracting the coefficients of high temp
a1=str2num(line2(1:a(1)+3));
a2=str2num(line2(a(1)+3:a(2)+3));
a3=str2num(line2(a(2)+4:a(3)+3));
a4=str2num(line2(a(3)+4:a(4)+3));
a5=str2num(line2(a(4)+4:a(5)+3));
a6=str2num(line3(1:b(1)+3));
a7=str2num(line3(b(1)+4:b(2)+3));
%extracting the coefficients of low temp
a8=str2num(line3(b(2)+4:b(3)+3));
a9=str2num(line3(b(3)+4:b(4)+3));
a10=str2num(line3(b(4)+4:b(5)+3));
a11=str2num(line4((1:c(1)+3)));
a12=str2num(line4(c(1)+4:c(2)+3));
a13=str2num(line4(c(2)+4:c(3)+3));
a14=str2num(line4(c(3)+4:c(4)+3));

t = linspace(ltemp,htemp,500);

for j= 1:500
 
    if t>=atemp
    cp=(a1+a2.*t+(a3.*t.^2)+(a4.*t.^3)+(a5.*t.^4)).*R;
    h=(a1+(a2.*t./2)+(a3.*(t.^2)./3)+(a4.*(t.^3)./4)+(a5.*(t.^4)./5)+(a6./t)).*R.*t;
    s=(a1*log(t)+a2.*t+(a3.*(t.^2)./2)+(a4.*(t.^3)./3)+(a5.*(t.^4)./4)+a7).*R;
    else
     cp=(a8+a9.*t+(a10.*t.^2)+(a11.*t.^3)+(a12.*t.^4)).*R;
    h=(a8+(a9.*t./2)+(a10.*(t.^2)./3)+(a11.*(t.^3)./4)+(a12.*(t.^4)./5)+(a13./t)).*R.*t;
    s=(a8*log(t)+a9.*t+(a10.*(t.^2)./2)+(a11.*(t.^3)./3)+(a12.*(t.^4)./4)+a14).*R;
    
    end
end

oldfolder = cd('C:\Users\Skill_Lync\Desktop\temp_prog');

figure(1);
plot(t,s,'color','b','linewidth',3);
xlabel('Temperature');
ylabel('Entropy');
title(sp_name);
figure(2);
plot(t,cp,'color','r','linewidth',3);
xlabel('Temperature');
ylabel('Specific Heat');
title(sp_name);
figure(3);
plot(t,h,'color','k','linewidth',3);
xlabel('Temperature');
ylabel('Enthalpy');
title(sp_name);
mkdir('C:\Users\Skill_Lync\Desktop\temp_prog\Results\',sp_name);
cd(['C:\Users\Skill_Lync\Desktop\temp_prog\Results\',sp_name]);
saveas(figure(1),'1.jpeg');
saveas(figure(2),'2.jpeg');
saveas(figure(3),'3.jpeg');

cd(oldfolder)
molecular_W = molecular_weight(sp_name);
K = sprintf('Molecular weight of %s is %f',sp_name,molecular_W);
disp(K)


end

MOLECULAR WEIGHT FUNCTION:

function M = molecular_weight(sp_name)
species = (sp_name)
elements = ['H','C','N','O','A'];
Atomic_weight = [1,12,14,16,40];
M=0;
for i=1:length(species)
    for j = 1:length(elements)
        if strcmp(sp_name(i),elements(j))
            M=M+Atomic_weight(j);
            x=j;
        end
    end
    n=str2num(sp_name(i));
    if n>1
        M=M+(Atomic_weight(x)*(n-1));
    end
end
end

EXPLANATION:

1. Under this function our aim is to calculate the molecular weight of the atoms.We will match the elements with our function which is "['H','C','N','O','A']" we have given atomic molecular weight of the corresponding elements in the variable called "Atomic_weight" which is [1,12,14,16,40].., with the help of "strcmp" (which refers to string comparison) we will compare element names.Then we take Atomic_weight and add with the corresponding atom.Then we compare the species name.

2. We have used a "if condition" because the molecular weight will add up and we will get incorrect element which is not even present, i.e { M=M+Atomic_weight(j);} to overome this issue we have used "if condition" and provided the condition as { M=M+(Atomic_weight(x)*(n-1)); }. For example, CO3 here n=3, after the condition executes this will become CO2 with the help of (n-1).

RESULTS :

1.Screenshot of the window where all the folders are saved.

2.Molecular weight will bve displayed in command window

3. Plots for `O_2`

a. Specific Heat vs Temperature

b. Entropy vs Temperature

c. Enthalpy vs Temperature

4. Plots for `N_2`

a. Specific Heat vs Temperature

b. Entropy vs Temperature

c. Enthalpy vs Temperature

5.Plots for `CO_2`

a. Specific Heat vs Temperature

b. Entropy vs Temperature

c. Enthalpy vs Temperature

REFERENCE :

1.Reference for making a directory

2.Reference for changing the directory

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.