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.
OBJECTIVE:-
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.Calculate the molecular weight of each species and display it in the command window.
2.Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each
species.
3.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.
4.The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
Screenshot of the window where all the folders are saved.
5.Plots for o2, N2 and Co2 species.
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.
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.
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.
H = R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + ((a6./T)))
MAIN CODE
clear all
close all
clc
%reading the data
%reading header information
NASA_DATA=fopen('THERMO.dat','r');
fgetl(NASA_DATA); %leaveing this line as a comment
GT=fgetl(NASA_DATA); %second line in data containg global_temperature_ranges
A=strsplit(GT,' '); %converting second line from string to number
GLOBAL_MIN_TEMP=str2num(A{1});
GLOBAL_MID_TEMP=str2num(A{2});
GLOBAL_MAX_TEMP=str2num(A{3});
fgetl(NASA_DATA); %leaveing this line as a comment
fgetl(NASA_DATA); %leaveing this line as a comment
fgetl(NASA_DATA); %leaveing this line as a comment
for i=1:53
%Reading the species block
gt=fgetl(NASA_DATA);
a=strsplit(gt,' ');
SPECIES_NAME=[a{1}]
T1=str2num(a{end-3});
T2=str2num(a{end-2});
T3=str2num(a{end-1});
T=linspace(T1,T2,1000);
%High Temperature co_effients
line1=fgetl(NASA_DATA);
b=findstr(line1,'E');
a1=line1(1:b(1)+3);
a1=str2num(a1);
a2=line1(b(1)+4:b(2)+3);
a2=str2num(a2);
a3=line1(b(2)+4:b(3)+3);
a3=str2num(a3);
a4=line1(b(3)+4:b(4)+3);
a4=str2num(a4);
a5=line1(b(4)+4:b(5)+3);
a5=str2num(a5);
line2=fgetl(NASA_DATA);
c=strfind(line2,'E');
a6=line2(1:c(1)+3);
a6=str2num(a6);
%low Temperature co_effients
a7=line2(c(1)+4:c(2)+3);
a7=str2num(a7);
a8=line2(c(2)+4:c(3)+3);
a8=str2num(a8);
a9=line2(c(3)+4:c(4)+3);
a9=str2num(a9);
a10=line2(c(4)+4:c(5)+3);
a10=str2num(a10);
line3=fgetl(NASA_DATA);
d=findstr(line3,'E');
a11=line3(1:d(1)+3);
a11=str2num(a11);
a12=line3(d(1)+4:d(2)+3);
a12=str2num(a12);
a13=line3(d(2)+4:d(3)+3);
a13=str2num(a13);
a14=line3(d(3)+4:d(4)+3);
a14=str2num(a14);
%SPECIFIC_HEAT
R=8.314; %UNIVESRSAL GAS CONSTANT
cp=Specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,T,T1,T2,T3,R);
%Enthalphy
h=enthalphy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,T,R,T1,T2,T3);
%Entrophy
s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,T1,T2,T3,R);
mkdir(SPECIES_NAME);
cd(SPECIES_NAME);
%plotting
figure(1);
plot(T,cp,'linewidth',3);
xlabel('Temperature');
ylabel('Specific_Heat');
title(sprintf('TEMPERATURE V/s SPECIFICHEAT %s',SPECIES_NAME));
saveas(figure(1),'Specific_heat.jpg')
figure(2);
plot(T,h,'linewidth',3,'color','r');
xlabel('Temperature');
ylabel('enthalphy');
title(sprintf('TEMPERATURE V/s ENTHALPHY %s',SPECIES_NAME));
saveas(figure(2),'enthalphy.jpg')
figure(3);
plot(T,s,'linewidth',3,'color','g');
xlabel('Temperature');
ylabel('entrophy');
title(sprintf('TEMPERATURE V/s ENTROPHY %s',SPECIES_NAME));
saveas(figure(3),'entrophy.jpg');
cd('G:matlabprogramme matlab');
%Molecular weight
% Molecular weight
W = Molecular_weight(SPECIES_NAME);
end
- opening and reading the file using fopen and assingn the file_id (I assingned as NASA_DATA).
- skipping/leaving the first five lines using fgetl command(fgetl: is a command used to open line by line of file).
- now from the line of six we get the species .The SIXTH line contains species name local temperatures from the sixth line next three lines contains the co_efficient values.
- now i read the line using fgetl command ,i saw that is the format of string ,now i splitted the string using strsplit command.
- now it splitted in the way a character,a word or anumber.
- now i get local temperatures using str2num and the position of that number in the string.
- from here reading the line using fgetl command and convert all the co_effients in that string to numbers.
- we get all the 14 co_eeficients.
- GIVEN_CONDTION(first 7 co_effients are for HIGHER_TEMP second 7 for LOWER_TEMP.
- now by creating the functions for specific_heat,entropy,enthaphy i calulated them.
- i creadted a directory with the name of species_name(mkdir(species_name)) and then changed the main directory to species_name(cd(species_name).
- To the three plots as figures using save as command in that species_name folder
- plotting (T,(Cp,h,s))
- i again chaged my directory to main path (cd('path')
- Then at last i calculated the molecular_weight by wrinting the function to molecular_weight.
- if we go through the file throughly thereb is 53 type species are in the same way
- so, i itrated the same command for 53 times
- we get 53 species_name folder and the the plots as figures in that.
specific_heat
- Now we will find the Specific heat by creating Specific heat function and assigning its relted formula coefficent from 14
coefficients, Gas constant, Temperature and mid temperature. - Now we will creatre an condition using if loop and make the temperature greater than to mid temperature to specify higher temperature coefficent in the formula.
- Then we will use else condition to specify coefficient of lower temperature coefficent in the formula.
- Then end the loop and function to calculate Specific Heat.
function h=enthalphy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,T,R,T1,T2,T3);
for i=1:length(T);
if T(i)>=T3
h(i)=R*T(i)*(a1+((a2*T(i))/2)+((a3*(T(i))^2)/3)+((a4*(T(i))^3)/4)+((a5*(T(i))^4)/5)+(a6/T(i)));
else
h(i)=R*T(i)*(a8+((a9*T(i))/2)+((a10*(T(i))^2)/3)+((a11*(T(i))^3)/4)+((a12*(T(i))^4)/5)+(a13/T(i)));
end
end
end
Entrophy
- Now we will find the Entropy by creating Entropy function and assigning its relted formula coefficent from 14 coefficients,Gas constant, Temperature and mid temperature.
- Now we will creatre an condition using if loop and make the temperature greater than mid temperature to specify higher temperature coefficent in the formula.
Then we will use else condition to specify coefficient of lower temperature coefficent in the formula. - Then end the loop and function to calculate Entropy.
function s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,T1,T2,T3,R);
for i=1:length(T);
if T(i)>T3
s(i)=R*((a1*(log(T(i))))+((a2)*(T(i)))+(((a3)*(T(i)).^2)/2)+(((a4)*(T(i)).^3)/3)+(((a5)*(T(i)).^4)/4)+a7);
else
s(i)=R*((a8*(log(T(i))))+((a9)*(T(i)))+(((a10)*(T(i)).^2)/2)+(((a11)*(T(i)).^3)/3)+(((a12)*(T(i)).^4)/4)+a14);
end
end
end
Enthalphy
- Now we will find the Enthalpy by creating Enthalpy function and assigning its relted formula coefficent from 14 coefficients,Gas constant, Temperature and mid temperature.
- Now we will creatre an condition using if loop and make the temperature greater than and equal to mid temperature to specify high temperature coefficent in the formula.
Then we will use else condition to specify coefficient of lower temperature coefficent in the formula. - Then end the loop and function to calculate Enthalpy.
function h=enthalphy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,T,R,T1,T2,T3);
for i=1:length(T);
if T(i)>=T3
h(i)=R*T(i)*(a1+((a2*T(i))/2)+((a3*(T(i))^2)/3)+((a4*(T(i))^3)/4)+((a5*(T(i))^4)/5)+(a6/T(i)));
else
h(i)=R*T(i)*(a8+((a9*T(i))/2)+((a10*(T(i))^2)/3)+((a11*(T(i))^3)/4)+((a12*(T(i))^4)/5)+(a13/T(i)));
end
end
end
Molecular_WEIGHT
- Firstly we create an Molecular_weight functionW and assigning species input to that.
- Then define the main element of all 53 species in an array atomic_name that are :- 'H','C','O','N','A'.
- Then by taking above main elements we will create there atomic_wieght array from any source like internet that are :-
1.008 12.011 15.999 14.007 39.948. - Then we will assign molecular starting range W To zero and then create an 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 an condition using if condition and use 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 string to the number using str2num command assign to n. Then we run an condition of if loop to find weather
the species contains more than one element if there are more then 1 elements that is n>1 then we will increment the atomic - weight by the multiplication of position and n-1 to the atomic_weight array.
- Then we will print the Molecular weight using fprintf command and display it using disp command
function W = Molecular_weight(SPECIES_NAME)
% Defining Atomic name of the species
atomic_name = ['H','C','O','N','A'];
% Defining Atomic masses of main species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
W=0; % Molecular weight starting range
for i=1:length(SPECIES_NAME) % Loop runs to the length of the species
for j=1:length(atomic_name) % Loop runs to the length of the atomic names
if strcmp(SPECIES_NAME(i), atomic_name(j)) %function to compare 2 strings
W = W + atomic_weight(j);
position = j;
end
end
n = str2double(SPECIES_NAME(i)); % Now we will find if there is more element in the species then incrementing
%atomic weight of the species
if n>1
W = W + atomic_weight(position)*(n-1);
end
end
fprintf('Molecular Weight of %s :- ',SPECIES_NAME);
fprintf('%f',W);
disp(' ')
MOLECULAR_WEIGHT SCREEN SHOTS



screenshot of all flolders of plots

O2 PLOTS



N2 plots



Co2 plots



ERRORS
- Faced a lot of syntax errors like parenthesis,colons i corrected those type of errors by reading the error warning .
- i index the same for two fgetl commands while extacting the co_effients ,so it reads the same line twice beacause of that after the sixth co_efficient the remaing all is as the first 6 Co_efficients
- so, i get the wrong plots for all the species i over came it by undergoing a checking and compareing the co efficient with thermo.dat file
- while writing the fuction codes i got a lot of syntax and indexing errors which causes wrong plotting and sccessive error messages

