Project

NASA thermodynamic data

AIM: To write a code in Matlab to parse the NASA thermodynamic data file and to calculate molecular weight of each species and to plot the Cp, Enthalpy, and Entropy for the local temperature range.

Introduction:

NASA polynomials have this form as shown above.

Here R is the universal gas constant, T is the temperature. aw,a2,a3,a4,a5,a6,a7 are the numerical co-efficients supplied in the thermodynamic file.

The first 7 co-efficents ate HIGH-temperature co-efficients(above 1000k) and the next 7 co-efficients are LOW-temperature co-efficients(below 1000k).

File parcing refers to to how informaton can be read from a particular text file or any type of file and how the informaiton can be used in computer program.

The THERMO file is a data file containing 14 co-efficients for 53 different elements given by NASA. These co-efficients are utilized to calculate the specific heat, enthalpy and entropy for a particular element. Each element has its own range of local temperature. THERMO file has 53 elements.

Coding:

i) specific heat :

it is the amount of energy that must be added, in the form of heat, to one mass of the substance in order to cause an increase of one unit in its temperature.

Cp= (a1 + a2T + a3T^2 + a4T^3 + a5T^4)*R

ii) Entropy:

Entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system. The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena.

% entropy calculation function

function S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)
%comparing with global temperature
%if less than global temperature - minimum co-efficients are taken

if T <= tempmid
S = R.*(a8.*log(T) + (a9.*T) + (a10*((T).^2)/2) +(a11*((T).^3)/3) + (a12*((T).^4)/4) + a14);
else
S = R.*(a1.*log(T) + (a2.*T) + (a3*((T).^2)/2) + (a4*((T).^3)/3) + (a5*((T).^4)/4) + a7);
end

iii)

Enthalpy

% enthalpy calculation function

function H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
%calculating enthalpy
%calculating enthalpy for higher and lower temperature

    if T <= tempmid
        H = R*T.*(a8+((a9*T)/2))+((a10*(T).^2)/3)+((a11*(T).^3)/4)+((a12*(T).^4)/5)+((a13./T));
    else
        H = R*T.*(a1 +((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + ((a6./T)));
    end
end

Main code:

clear all
close all
clc

%inpts
R=8.314; %gas constant
f1 = fopen('THERMO.dat','r');
%opening file in read mode

get=fgetl(f1); %reading data file

%reading temperature from the file

temp = fgetl(f1);

%spliting

s = strsplit(temp);

%converting strings cells into numbers

templow = str2double(s{2});

tempmid = str2double(s{3});

temphigh = str2double(s{4});

%Temperature Range

T = linspace(templow,temphigh,1000);

%skipping comment lines

for i=1:3

get=fgetl(f1);

end
%reading the species

for j=1:53

tline=fgetl(f1);

A=strsplit(tline, ' ');

species = (A{1});

%converting string to number

lowtemp = str2double(A(end-3));

midtemp = str2double(A(end-2));

hightemp = str2double(A(end-1));
tline1 = fgetl(f1);
%getting the next line

a = strfind(tline1,'E'); %to find the string E

%finding the first 7 higher and second 7 lower co=efficients

%finding higher co-efficients

a1 = tline1(1:a(1)+3);

a1 = str2double(a1);

a2 = tline1(a(1)+4:a(2)+3);

a2 = str2double(a2);

a3 = tline1(a(2)+4:a(3)+3);

a3 = str2double(a3);

a4 = tline1(a(3)+4:a(4)+3);

a4 = str2double(a4);

a5 = tline1(a(4)+4:a(5)+3);

a5 = str2double(a5);

tline2 = fgetl(f1);

b = strfind(tline2,'E');

a6 = tline2(1:b(1)+3);

a6 = str2double(a6);

a7 = tline2(b(1)+4:b(2)+3);

a7 = str2double(a7);

%finding lower co-efficients

a8 = tline2(b(2)+4:b(3)+3);

a8 = str2double(a8);

a9 = tline2(b(3)+4:b(4)+3);

a9 = str2double(a9);

a10 = tline2(b(4)+4:b(5)+3);

a10 = str2double(a10);

tline3 = fgetl(f1);

c = strfind(tline3,'E');

a11 = tline3(1:c(1)+3);

a11=str2double(a11);

a12 = tline3(c(1)+4:c(2)+3);

a12 = str2double(a12);

a13 = tline3(c(2)+4:c(3)+3);

a13 = str2double(a13);

a14 = tline3(c(3)+4:c(4)+3);

a14 = str2double(a14);

%calculate specific heat

Cp = specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);

%calculate Entropy

S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);

%calculate Enthalpy

H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);

%calculating Molecular weight of the species

W = molecular_weight(species);

%writing molecular weight file

MW_file = fopen('Molecular weight.txt','w');

fprintf(MW_file,'Molecular weight of %s is %d',species,W);

fclose(MW_file);

%closing file

%plotting Cp,H and S with Temperature

%Saving in a folder

%cd(species_nasa)

%changing directory

mkdir(species);

cd(species);
%plot 1 Cp vs T

figure(1)

plot(T,Cp,'linewidth',2,'color','b');

xlabel('temperature(K)');

ylabel('specific heat(KJ/mol-K)');

title(sprintf('specific heat vs temperature range of %s',species));

grid on

saveas(figure(1),'specific heat.jpg');

%plot 2 S vs T

figure(2)

plot(T,S,'linewidth',2,'color','r');

xlabel('temperature');

ylabel('Entropy(KJ/mol-K)');

title(sprintf('Entropy vs temperature range of %s',species));

grid on

saveas(figure(2),'Entropy.jpg');

%plot 3 Cp vs T 

figure(3)

plot(T,H,'linewidth',2,'color','g');

xlabel('temperature');

ylabel('Enthalpy(KJ/mol)');

title(sprintf('enthalpy vs temperature range of %s',species));

grid on

saveas(figure(3),'Enthalpy.jpg');

cd ..
%cd('C:UsersjnanaOneDriveDesktopskill lync') %changing the directory to matlab path

%molecular weight

W = molecular_weight(species);

end

steps:

  • first, we need to input the parameter R and we will open a file THERMO.txt file and reading this file using fopen command.
  • later we need to read THERMO file using fgetl command and then we read temperature file and we will split the temperatures using strsplit command to convert it into cell array and then we will convert this string to number using str2num command to find low,high and middle temperatures.
  • then we assign temperature rage using the linspace command. Then we run loop to skip command lines the data file using fgetl command.
  • next, we will run the loop to find 53 element in the data by assigning fgetl command and then splitting it into the cell array to find the species line and then find the species name and then find the number of the temperatures in the species line.
  • then we read new line. in order to read the co-efficient in the data file we will first find E string using strfind command to find the before and after numbers of the co-efficient then we will find the remaining 14 coefficients and convert them to numbers from strings.
  • out of these we will find first 7 coefficents which are for higher temperature and then second 7 coefficents which are for lower tmperature
  • we then specify Cp, S, H functions to these parameters using these 14 co-efficents for lower and higher temperature. then we will specify molecular weight W function to find the Molecular weight of the different species.
  • now we change the directory using Cd command and then close the text file using fclose command.
  • we have to save our files using mkdir command and then we will change the directory to the created folder.
  • saveas command to save the plots

FINAL RESULTS:

plots of species O2:

Plots of species CO2:

Plots of species N2:

the molecular weight of each species:

The folder where the files are saved:

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.