Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: To write a code in Matlab to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas
species.
Objective:

  • Calculate the molecular weight of each species and display it in the command window.
  • Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
  • Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All of these
    should be generated automatically.
  • The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H, and S plots.

SOLUTION:

Writing a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the 'THERMO.dat'
file. The formulae are shown below

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

File Parsing:-

  • Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term parsing comes from Latin pars (orationis), meaning part (of speech).Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information.Some parsing algorithms may generate a parse forest or list of parse trees for a syntactically ambiguous input.
  • 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 perfoming some kind of transformation on the data.
  • File parsing here means extracting the useful information from a file and utilizing that information
    for our program.

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:-The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat.

Cp = R*(a1 + (a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4))

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.

S = R*(a1.*log(T) + (a2*T) + ((a3*(T).^2)/2) + ((a4*(T).^3)/3) + ((a5*(T).^4)/4) + a7)

Enthalpy:- Enthalpy , a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume. In a system contained so as to prevent mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy. Its value is determined entirely by the temperature, pressure, and composition of the system and not by its history.

H = R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + ((a6./T)))

MATLAB CODE(MAIN):-

% Program to Parse NASA's thermodynamic data
clear all
close all
clc
% Universal Gas constant(R)
R = 8.314;
% loading data file
File = fopen('THERMO.dat','r');
% reads 1st line
fgetl(File);
% collect Global temprature
tline = fgetl(File);
Global_Temp = strsplit(tline,' ');
Gobal_low_temp = str2num(Global_Temp{2});
Gobal_mid_temp = str2num(Global_Temp{3});
Gobal_high_temp = str2num(Global_Temp{4});
% skip comments
fgetl(File);
fgetl(File);
fgetl(File);
for i = 1 : 53
 % to iterate through all 53 elements
 % reads species
 line1 = fgetl(File);
 species = get_species(line1);
 % calculate molecular weight and print it
 molecular_weight = get_molecular_weight(species);
 fprintf('nMolecular Weight of %s is : %d',species,molecular_weight)
 % Collect local temprature range
 local_temp_range = get_local_temp_range(line1);
 % collect Coefficient data
 a(1:5) = get_coeff(fgetl(File));
 a(6:10) = get_coeff(fgetl(File));
 a(11:14) = get_coeff(fgetl(File));
 % Temprature
 T = linspace(local_temp_range(1),local_temp_range(3),500);
 % Specific Heat
 Cp = Specific_heat(a, local_temp_range, T, R);
 % Enthalpy
 H = Enthalpy(a, local_temp_range, T, R);
 % Entropy
 S = Entropy(a, local_temp_range, T, R);
 % Plotting and saving in folder
 current_dir = pwd;
 mkdir(species);
 % creates new folder with species name
 cd(species);
 % open that folder
 % plotting temprature vs specific heat
 figure(1)
 plot(T, Cp ,'linewidth', 2 ,'color', 'b')
 xlabel('Temprature[K]');
 ylabel('Specific Heat[KJ/Kg-K]');
 title(sprintf('Temprature vs Specific Heat plot for %s',species))
 grid on
 % Save jpg of figure(1)
 saveas(figure(1), 'Specific Heat.jpg')
 % plotting temprature vs enthalpy
 figure(2)
 plot(T,H,'linewidth',2,'color','b')
 xlabel('Temprature[K]');
 ylabel('Enthalpy[KJ/Kg]');
 title(sprintf('Temprature vs Enthalpy plot for %s',species))
 grid on
 % Save jpg of figure(2)
 saveas(figure(2), 'Enthalpy.jpg')
 % plotting temprature vs entropy
 figure(3)
 plot(T,S,'linewidth',2,'color','b')
 xlabel('Temprature[K]');
 ylabel('Entropy[KJ/Kg-K]');
 title(sprintf('Temprature vs Entropy plot for %s',species))
 grid on
 % Save jpg of figure(3)
 saveas(figure(3), 'Entropy.jpg')
 cd(current_dir)
 % goes back to parent folder
end

SOLUTION ALGORITHM:
1. The program begins with the clear all, close all and clc commands to free up the command window and the workspace.
2. We will then read the data from the external NASA thermodynamic file using fopen(to open the file) and fgetl(to read the content in the file line by line) commands.
3. The useful header information will be extracted from the file such as global temperature limits.
4. Then we will proceed to extract the individual species data of all the species present in the file using fgetl command to read the file line by line and skipping the line not containing any useful data.
5. The data that is to be extracted for all the 53 species mentioned in the file are 14 coefficients(7 for higher temperature and & for lower temperature), species name and the local temperature limits.
6. This process will be carried out with the help of a for loop.
7. Calculation of specific heat, enthalpy, entropy and molecular weight will be done in separate function files and will be called in the main program.
8. Then we will create plots for specific heat v/s temperature, enthalpy v/s temperature and entropy v/s temperature for all the 53 species and these 3 plots will be saved in separate folders for each species.
9. After this we will display the molecular weight of all the species in the command window.
10. The code for the entire program will be written in MATLAB.

FUNCTION FOR CALCULATING MOLECULAR WEIGHT:

Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to
determine stoichiometry in chemical reactions and equations.

  • Writing function for the molecular weight we have to specify a specific atomic number.
  • The atomic number should match the atomic name.
  • For this calculation:
  • Hydrogen = 1.008
  • Carbon = 12.011
  • Oxygen = 15.999
  • Nitrogen = 14.007
  • Argon = 39.948
  • Now provide an initial starting range of molecular weight by 0.
  • By using for loop add atomic number with each species to get particular molecular weight.
function molecular_weight = get_molecular_weight(species)
molecular_weight = 0;
Atoms = ['O', 'H', 'N', 'C', 'S', 'A'];
Atomic_weights = [16, 1, 14, 12, 32, 40];
for i = 1 : length(species)
 for j = 1 : length(Atoms)
 if strcmpi(species(i), Atoms(j))
 molecular_weight = molecular_weight + Atomic_weights(j);
 mark = j;
 end
 end
 k = str2num(species(i));
 if k>1
 molecular_weight = molecular_weight + Atomic_weights(mark)*(k-1);
 end
end

FUNCTION FOR CALCULATING ENTHALPY:
A thermodynamic quantity is 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. Enthalpy is similar to energy, but not the same.

H=(a8+(a9.*t./2)+(a10.*(t.^2)./3)+(a11.*(t.^3)./4)+(a12.*(t.^4)./5)+ (a13./t)).*R.*t

function H = Enthalpy(a, local_temp_range, T, R)
for i = 1 : length(T)
 if T(i) <= local_temp_range(2)
 H(i) = R*T(i)*(a(8) + a(9)*T(i)/2 + a(10)*(T(i)^2)/3 + a(11)*(T(i)^3)/4 + a(12)*(T(i)^4)/5 + a(13)/T(i));
 else
 H(i) = R*T(i)*(a(1) + a(2)*T(i)/2 + a(3)*(T(i)^2)/3 + a(4)*(T(i)^3)/4 + a(5)*(T(i)^4)/5 + a(6)/T(i));
 end
end

FUNCTION FOR CALCULATING ENTROPY:

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. Entropy is also a measure of the number of possible arrangements the atoms in a system can have.

s=(a8*log(t)+a9.*t+(a10.*(t.^2)./2)+(a11.*(t.^3)./3)+(a12.*(t.^4)./4)+a14).*R

function S = Entropy(a, local_temp_range, T, R)
for i = 1 : length(T)
 if T(i) <= local_temp_range(2)
 S(i) = R*(a(8)*log(T(i)) + a(9)*T(i) + a(10)*(T(i)^2)/2 + a(11)*(T(i)^3)/3 + a(12)*(T(i)^4)/4 + a(14));
 else
 S(i) = R*(a(1)*log(T(i)) + a(2)*T(i) + a(3)*(T(i)^2)/2 + a(4)*(T(i)^3)/3 + a(5)*(T(i)^4)/4 + a(7));
 end
end

FUNCTION FOR CALCULATING SPECIFIC HEAT:

The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.

CP=(a1+a2.*t+(a3.*t.^2)+(a4.*t.^3)+(a5.*t.^4)).*R

function Cp = Specific_heat(a, local_temp_range, T, R)
for i = 1 : length(T)
 if T(i) <= local_temp_range(2)
 Cp(i) = R*(a(8) + a(9)*T(i) + a(10)*T(i)^2 + a(11)*T(i)^3 + a(12)*T(i)^4);
 else
 Cp(i) = R*(a(1) + a(2)*T(i) + a(3)*T(i)^2 + a(4)*T(i)^3 + a(5)*T(i)^4);
 end
end

FUNCTION FOR CALCULATING COEFFICIENTS:

Cfunction [a] = get_coeff(line)
E = findstr(line,'E');
a(1) = str2num(line(1:E(1)+3));
for i = 1 : length(E)-1
 temp = str2num(line(E(i)+4:E(i+1)+3));
 a(i+1) = temp;
end
end

FUNCTION FOR CALCULATING LOCAL TEMPERATURE RANGE:

function [temp] = get_local_temp_range(line)
split = strsplit(line, ' ');
l = length(split);
mid_temp = str2num(split{l-1});
high_temp = str2num(split{l-2});
low_temp = str2num(split{l-3});
temp = [low_temp mid_temp high_temp];
end

FUNCTION FOR CALCULATING SPECIES:

This code is basically to create a text file for molecular weight.

function species = get_species(line)
temp = strsplit(line, ' ');
species = (temp{1});
end

RESULTS:

COMMAND WINDOW OUTPUT:

Plots of specie O2 :-

Plots of specie N2 :-

Plots of specie CO2 :-

Screenshot of the Directory where Different Species are saved:-

Conclusion :

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

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.