Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

Write a code in Matlab to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.

OBJECTIVES:

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.

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.

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.

NASA Thermodynamic File:-

NASA thermodynamics file which is THERMO.dat file in which there are temperature range and 53 species are given in these data and each species contains 14 coefficients and temperature ranges and we have to parse these 14 coefficients to find Specific heat, Entropy and Enthalpy using formula as:-

where,

R = Universal Gas constant

T = Temperature

Cp = Specific heat (KJ)

S = Entropy(KJ/mol-K)

H = Enthalpy(KJ/mol)

a(i) = Coefficients where, i = 1 to 14

SPECIFIC HEAT:

Specific heat, 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. For example, the specific heat of water is 1 calorie (or 4.186 joules) per gram per Celsius degree.

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.

ENTHAPY:

Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the product of the pressure and volume of the system. It reflects the capacity to do non-mechanical work and the capacity to release heat. Enthalpy is denoted as H

MOLECULAR WEIGHT:

The molar mass/molecular weight is actually the sum of the total mass in grams of the atoms present to make up a molecule per mole. The unit of molar mass is grams/mole.

PROGRAM:

FUNCTION FOR SPECIFIC HEAT

% function for specific heat
function CP = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)
% calculation for specific heat higher and lower temperature respectively
if (T<=tempmid)
CP = R*(a8+(a9*T)+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4));
else
CP = R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4));
end
end

EXPLANATION:

  • Write function CP equal to 14 coefficient and temperature and gas constant,tempmid.
  • using if write the condition is temperature is less the are equal to temperature mid.
  • using as our cp formula to write higher temperature condition.
  • else after
  • using as our cp formula to write higher temperature condition.
  • close the if end else to using end command.

FUNCTION FOR ENTROPY:

% function for Entropy calculation
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)
% calculation for entropy higher and lower temperature respectively
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
end

EXPLANATION:

  • Write function S equal to 14 coefficient and temperature and gas constant,tempmid.
  • using if write the condition is temperature is less the are equal to temperature mid.
  • using as our entropy formula to write higher temperature condition.
  • else after
  • using as our entropy formula to write higher temperature condition.
  • close the if end else to using end command.

FUNCTION FOR ENTHALPY:

% function for calculating Enthalpy
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
% calculation for enthalpy higher and lower temperature respectively
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

EXPLANATION:

  • Write function H equal to 14 coefficient and temperature and gas constant,tempmid.
  • using if write the condition is temperature is less the are equal to temperature mid.
  • using as our enthalpy formula to write higher temperature condition.
  • else after
  • using as our enthalpy formula to write higher temperature condition.
  • close the if end else to using end command.

FUNCTION FOR MOLECULAR WEIGHT:

% function for molecular weight calculation
function MW = Molecular_Weight(species)
% Defining atomic name of the species
atomic_name = ['H','C','O','Ar'];
% Hydrogen,carbon,oxygen,argon
% Defining atomic weight of the species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
MW =0;
% adding atomic number to obtain particular value
for i = 1:length(species)
for j = 1:length(atomic_name)
if strcmp(species(i),atomic_name(j))
MW = MW+atomic_weight(j);
position = j;
end
end
n = str2num(species);
if (n>1)
MW =MW+atomic_weight(position)*(n-1);
end
end
fprintf('Molecular Weight of %s : ', species);
fprintf('%f',MW);
disp(' ')
end
EXPLANATION:
  • Firstly we create an Molecular_weightfunction MW and assigning its species input to that.
  • Then define the main element of all 53 species in an array atomic_name that are :- ‘H’,’C’,’O’,’N’,’Ar’.
  • Then by taking the above main elements we will create thereatomic_weight array from any source like internet that are :- 008 12.011 15.999 14.007 39.948.
  • Then we will assign molecular starting range from MW to zero and then create “for” loop condition which runs to the length of the species and an another nested “for” loop is created which runs to the length of the string atomic_name.
  • Then we will create a condition using “If” condition and “strcmp” command to compare the species and atomic_name strings to calculate molecular masses if by comparison both string found same then we will assign its molecular weight.
  • Now we will convert the string to numbers using “str2num” command and assign to then we run a condition of “if” loop to find whether the species contains more than one element and if there are more than 1 elements that is n>1 then we will increment the atomic weight by multiplication of position and n-1 to the atomic_weight array.
  • Then we will print the molecular weight using fprintfcommand and display it using disp.

PROGRAM FOR MAIN CODE:

% code for extract co efficient
clear all
close all
clc
% input
R = 8.314 % gas constant (J/(mol-K))
% Reading data file
f1 = fopen('THERMO.dat','r')
get = fgetl(f1);
% Reading the temperature from this file
temp = fgetl(f1);
% splitting
s = strsplit(temp);
% converting string cells into numbers
templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh = str2num(s{4});
% Temperature rang
T = linspace(templow,temphigh,1000);
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 cells into number
lowtemp = str2num(A{end-3});
midtemp = str2num(A{end-2});
hightemp = str2num(A{end-1});
tline1 = fgetl(f1);
a = strfind(tline1, 'E');
% finding the first seven and second seven co efficients
a1 = tline1(1:a(1)+3);
a1 = str2num(a1);
a2 = tline1(a(1)+4:a(2)+3);
a2 = str2num(a2);
a3 = tline1(a(2)+4:a(3)+3);
a3 = str2num(a3);
a4 = tline1(a(3)+4:a(4)+3);
a4 = str2num(a4);
a5 = tline1(a(4)+4:a(5)+3);
a5 = str2num(a5);
tline2 =fgetl(f1);
b = strfind(tline2, 'E');
a6 = tline2(1:b(1)+3);
a6 =str2num(a6);
a7 = tline2(b(1)+4:b(2)+3);
a7 = str2num(a7);
% finding lower temperature
a8 = tline2(b(2)+4:b(3)+3);
a8 = str2num(a8);
a9 = tline2(b(3)+4:b(4)+3);
a9 = str2num(a9);
a10 = tline2(b(4)+4:b(5)+3);
a10 = str2num(a10);
tline3 = fgetl(f1);
c = strfind(tline3, 'E');
a11 = tline3(1:c(1)+3);
a11 = str2num(a11);
a12 = tline3(c(1)+4:c(2)+3);
a12 = str2num(a12);
a13 = tline3(c(2)+4:c(3)+3);
a13 = str2num(a13);
a14 = tline3(c(3)+4:c(4)+3);
a14 = str2num(a14);
% calcualtion for specific heat
CP = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
% calcualtion for entropy
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
% calculation for enthalpy
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
% calculation for molecular weight
MW = Molecular_Weight(species);
% Writting molecular weight file
MW_file = fopen('Molecular_Weight.txt','w');
fprintf(MW_file,'Molecular Weight of %s id %d',species,MW);
fclose(MW_file);
% ploting for CP,S,H and Temperature
% saving in a folder
cd('C:\Users\basky\Desktop\file parsing\species\')
mkdir(species);
cd(species);
% plot1 CP vs TEMPERATURE
figure(1)
plot(T,CP,'LineWidth',3,'Color','r');
xlabel('Temperature in (K) ');
ylabel('specific_heat in (KJ');
title(sprintf('specificheat vs Temperature range of %s',species));
grid on
saveas(figure(1),'specificheat.jpg');
% plot2 S vs TEMPERATURE
figure(2)
plot(T,S,'LineWidth',3,'Color','b');
xlabel('Temperature in (K) ');
ylabel('Entropy');
title(sprintf('Entropy vs Temperature range of %s',species));
grid on
saveas(figure(2),'Entropy.jpg');
% plot3 H vs TEMPERATURE
figure(3)
plot(T,H,'LineWidth',3,'Color','g');
xlabel('Temperature in (K) ');
ylabel('Enthalpy ');
title(sprintf('Enthalpy vs Temperature range of %s',species));
grid on
saveas(figure(3),'Enthalpy.jpg');
cd('C:\Users\basky\Desktop\file parsing\mw\')
% molecular weight
MW = Molecular_Weight(species)
end
EXPLNATION:
  • using claer all,close all,clc to clear the past data from command window,workspace,and figure window.
  • To give gas constant R value.
  • assign f1 variable to using fopen command to write THERMO.dat file.
  • read temp value usiing fgetl command.
  • splitting the temperature file while using strsplit command.
  • converting string cells into number using str2num command to get templow,tempmid,temphigh values.
  • using linspace command to take temp low to temp high ,indivual values.
  • using for loop assign i to skip the 3 line
  • for loop using toread the j=1:53 species .
  • assign variable A to using strsplit command splitting the data.
  • gives the species is A of first line.
  • using str2num command to string cell into convert number in low,mid,high temperature values.
  • using tline command to get f1 from line 1 data.
  • assign variable name a ,strfind command using to read E from the species.
  • finding the first 14 coefficient using tline and str2num command.
  • apply to CP,S,H and molecular weight funtion.
  • writting molecular weight file ussing fopen and assign MW_file.
  • plotting file to save seperate file ,while using cd and mkdir command.
  • using plot command and xlabel and ylabel ,title command assign get that CP VS T, S VS T , H VS T.
  • using cd (current directory) to save molecular weight value to text format.
  • finally close the for loop end conditon using end command.

PLOT FOR O2:

PLOT FOR N2:

PLOT FOR CO2:

MOLECULAR WEIGHT:

SPECIES:

CONCLUTION:

Using Octave code, the necessary coefficients required to evaluate various thermodynamic parameters for different species are extracted from NASA thermodynamic data file. The spec ific heat, entropy & enthalpy for all the species are calculated and plotted against the local temperature range. The molecular weight is calculated for all the species.

WHILE PROGRAMMING TAKE VEDIO FILE LINK:

https://drive.google.com/file/d/1wta4qwfqXgIbxlnrYyPpCeSCq7BE5nZA/view?usp=sharing

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.