Project

MATLAB code to calculate the Thremodynamic properties of different species from given NASA Thermodynamic data by using the concept of File Parsing.

Objective:- MATLAB code to calculate the Thremodynamic properties of different species from given NASA Thermodynamic data by using the concept of File Parsing.

Introduction

Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.

                    Data text file ---> Primitive data ---> Complex data

Data text file - NASA Thermodynamic file consist of compound like O ,O2 ,H ,H2O and many more. and there polynomial coefficient.

                  Downlaod Link for NASA file-https://drive.google.com/open?id=1uAEQUklkD9gSzn2UzePjkR6Az4tPc1h3

Primitive data - Convert this data into interger and float value, string value,

Complex data - Convert this primitive data into useful information like calculate specific heat, entropy, enthalpy.

Equation to calculate specific heat, entropy, enthalpy

where : 

           Cp - specific heat

           H - enthalpy

           S - entropy

           R - universal gas constant.

           T - temperature

there are 14  polynomial coefficients for each compound first seven coefficients are for higher temperature range and second seven coefficients are for lower temperature range.

Step 1:- By using this equation create a function.

-As per main code t1,t2and t3 are the temperature between t1 and t3 there are 500 temperature values so if temperature is less than t2 use last 7 coefficient or if it is more than t2 use first 7 coefficient.

Specific Heat Function

function cp=specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,T,t2)
for i=1:length(T)
    if(T(i)<=t2)
        cp(i)=c8+c9*T(i)+c10*T(i)^2+c11*T(i)^3+c12*T(i)^4;
    else
        cp(i)=c1+c2*T(i)+c3*T(i)^2+c4*T(i)^3+c5*T(i)^4;
    end
end

Enthalpy Function

function H=enthaply(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,T,t2)
for i=1:length(T)
    if(T(i)<=t2)
        H(i)=(c8+c9*T(i)/2+c10*T(i)^2/3+c11*T(i)^3/4+c12*T(i)^4/5+c13/T(i))*T(i);
    else
        H(i)=(c1+c2*T(i)/2+c3*T(i)^2/3+c4*T(i)^3/4+c5*T(i)^4/5+c6/T(i))*T(i);
    end
end

Entropy Function

function S=entrpy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,T,t2)
for i=1:length(T)
    if(T(i)<=t2)
        S(i)=c8*log(T(i))+c9*T(i)+c10*T(i)^2/2+c11*T(i)^3/3+c12*T(i)^4/4+c14;
    else
        S(i)=c1*log(T(i))+c2*T(i)+c3*T(i)^2/2+c4*T(i)^3/3+c5*T(i)^4/4+c7;
    end
end

Step 2:- Main code

- open file by using fopen cmd

- skip header means first row by using fget () cmd

- using fgetl cmd again we open second row in this row there are three temperature value low, medium,high temperature then split the line by using strsplit() cmd but split value is in string so convert string to number by using str2num() cmd.

- skip the comments by using fgetl() cmd

close all
clear all
clc

%Open file
file=fopen("THERMO.dat",'r');
%Skip Header
fgetl(file);
%Temperature Value
T_line=fgetl(file);
A=strsplit(T_line,' ');
t1=str2num(A{2});
t2=str2num(A{3});
t3=str2num(A{4});
%Skip Comment
fgetl(file);
fgetl(file);
fgetl(file);

- As you seen in data there are 53 compound or species so it is hard to write a code for each species so we write only 1 species code and using for loop automatically it give the resilt to  all 53 species.

- As you seen in data there are 4 rows for each species first row contain species name and second to fourth contain polynomial coefficients.

- Execute line1 by using fgetl() cmd, split the line1 using strsplit() cmd to get the species name so we can find the molecular weight.

- write a code to calculate molecular weight, As you seen the data every species either and or contain atoms-O,H,N,C,S,A and there atomic weight are-16,1,14,12,32,40. By using this atoms and atomic weight and compair with species so we can find the molecuare weight.

- Now execute line2 by using fgetl() cmd, find the 1 value or letter which is same in every coefficient by using findstr(), and seperate all coefficient by using hte letter.

- Do same for line3 and line4.

- Now you get all coefficient

ct=1;
for i=1:53
line1=fgetl(file);
get_species=strsplit(line1,' ');
species=get_species{1};
�lculating molecular weight
atoms=['O','H','N','C','S','A'];
atomic_weights=[16,1,14,12,32,40];
w=0;
for i = 1:length(species);
    for j = 1:length(atoms);
        if strcmpi(species(i),atoms(j));
            w=w+atomic_weights(j);
            mark = j;
        end
    end
    num=str2num(species(i));
    if num>1;
        w=w+atomic_weights(mark)*(num-1);
    end
    
end
fprintf("\n Molecular weight of %s is = %d",species,w)

line2=fgetl(file);
a=findstr(line2,'E');
c1=str2num(line2(1:a(1)+3));          
c2=str2num(line2(a(1)+4:a(2)+3));
c3=str2num(line2(a(2)+4:a(3)+3));
c4=str2num(line2(a(3)+4:a(4)+3));
c5=str2num(line2(a(4)+4:a(5)+3));

line3=fgetl(file);
b=findstr(line3,'E');
c6=str2num(line3(1:b(1)+3));
c7=str2num(line3(b(1)+4:b(2)+3));
c8=str2num(line3(b(2)+4:b(3)+3));
c9=str2num(line3(b(3)+4:b(4)+3));
c10=str2num(line3(b(4)+4:b(5)+3));

line4=fgetl(file);
c=findstr(line4,'E');
c11=str2num(line4(1:c(1)+3));
c12=str2num(line4(c(1)+4:c(2)+3));
c13=str2num(line4(c(2)+4:c(3)+3));
c14=str2num(line4(c(3)+4:c(4)+3));

- Now get temperature range by using linspace cmd from low to high temperature.

- Use this temperature range find specific heat,enthalpy and entropy.

%R-Gas Constant
R=8.314;
%Temperature range
N=500;                   %N-number of temperature value between initial and last temperature
T=linspace(t1,t3,N);  %T-Temperature
%Specific Heat
Cp=specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,T,t2)*R;
%Enthalpy
H=enthaply(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,T,t2)*R;
%Entropy
S=entrpy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,T,t2)*R;

- After that as you want to store the data or plot in a specific folder so first create the path and after that create a folder, name as species and then open that folder so it can store plots.

- Create a plot between Temperature and specific heat

- Create a plot between Temperature and Enthalpy

- Create a plot between Temperature and entropy

- after that end the for loop

%Identify Path
current_dir=pwd;
%Creat Folder
mkdir(species);
%Open that Folder
cd(species);

%plotting Temperatur Vs Specific heat
figure(1);
plot(T,Cp,'color','r');
xlabel("Temperature(K)");
ylabel("Specific Heat(KJ/Kg-K)");
title(sprintf('Temperatur Vs Splecific heat plot %s',species));
saveas(1,"Specific_heat.png");

%plotting Temperatur Vs Enthaply
figure(2);
plot(T,H,'color','b');
xlabel("Temperature(K)");
ylabel("Enthalpy(KJ/kg)");
title(sprintf('Temperatur Vs Enthaply plot %s',species));
saveas(2,"Enthalpy.png");

%plotting Temperatur Vs Entropy
figure(3);
plot(T,S,'color','b');
xlabel("Temperature(K)");
ylabel("Entrophy(KJ/kg-K)");
title(sprintf('Temperatur Vs Entropy plot %s',species));
saveas(3,"Entropy.png");

cd(current_dir);
ct=ct+1;
end

OUTPUT

- Molecular weigth

 

- Saving graphs of each species in seperate folder

- O2

- N2

- CO2

 Google drive project code link

-  https://drive.google.com/open?id=1FhJi3Yc6SZVDu2GendDN1C-b4Sb5eZas

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.