Project 1 - Parsing NASA thermodynamic data
OBJECTIVE:
- To write a function that extracts the co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the given data.
- To calculate the molecular weight of each species.
- Plotting the curve for Cp, Enthalpy and Entropy for the local temperature range and save them in a directory in separate folder which has to be done automatically.
We can use matlab or octave to do the above said process.
DEFINITION OF THE TERMS:
SPECIFIC HEAT:
It is the amount of heat that must be added to one unit of mass of the substance in order to cause an increase of one unit in temperature.
The heat required to raise the temperature of 1 kg of water by 1 K is 4184 joules, so the specific heat capacity of water is 4184 J⋅kg−1⋅K−1.
ENTHALPY:
Enthalpy is a property of a thermodynamic system, and is defined as the sum of the system's internal energy and the product of its pressure and volume.
It is a state function used in many measurements in chemical, biological, and physical systems at a constant pressure, that is conveniently provided by the large ambient atmosphere.
Its unit is joule since it is a energy.
ENTROPY:
Entropy is defined as a measure of randomness or disorder of a system.The tendency in nature for systems to become disordered and for less energy to be available for use as work.
The entropy of a system can in fact be shown to be a measure of its disorder and of the unavailability of energy to do work.
FORMULAE USED: The below formula is used to calculate specific heat,enthalpy and entropy with the temperature input.
CODE SCRIPTED:
clear all
close all
clc
% opening the given file
e = fopen('THERMO.dat','r');
%reading the first line
first_line = fgetl(e);
%reading the second line which has global temperature
global_temp = fgetl(e);
%splitting the string in global temp line using delimiter ''
temp_range = strsplit(global_temp,' ');
%assigning the low,mid,high global temperatures
globaltemp_low = temp_range(2);
globaltemp_mid = temp_range(3);
globaltemp_high = temp_range(4);
%value of gas constant
R = 8.314;
%using for loop to read the three comment lines
for i= 1:3
fgetl(e);
end
%for loop for reading,calculating the co-oeffecint and gas molecules
for i = 1:4:212
%reading the first line of each gas molecules
name = fgetl(e);
%splitting the line
lt = strsplit(name,' ');
%taking the first column of the splitted string which is the name of molecule
molecule = lt{1};
%calculating the low,mid,high of local temperature using str2num command
LLT = str2num(lt{end-3});
LMT = str2num(lt{end-1});
LHT = str2num(lt{end-2});
%creating an array of temperature where LLT,LHT are start and end point
T = linspace(LLT,LHT,500);
%reading the next line where the first five co-effecient
B = fgetl(e);
%to seperate each co-effecient we use E as string to find
b = strfind(B,'E');
%calculating the co-effecients
La1 = B(1:(b(1))+3);
La1 = str2num(La1);
La2 = B(b(1)+4:b(2)+3);
La2 = str2num(La2);
La3 = B(b(2)+4:b(3)+3);
La3 = str2num(La3);
La4 = B(b(3)+4:b(4)+3);
La4 = str2num(La4);
La5 = B(b(4)+4:b(5)+3);
La5 = str2num(La5);
%reading the next line where the next five co-effecient
C = fgetl(e);
%to seperate each co-effecient we use E as string to find
c = strfind(C,'E');
%calculating the co-effecients
La6 = C((1:c(1)+3));
La6 = str2num(La6);
La7 = C(c(1)+4:c(2)+3);
La7 = str2num(La7);
ha1 = C(c(2)+4:c(3)+3);
ha1 = str2num(ha1);
ha2 = C(c(3)+4:c(4)+3);
ha2 = str2num(ha2);
ha3 = C(c(4)+4:c(5)+3);
ha3 = str2num(ha3);
%reading the next line where the next four co-effecient
D = fgetl(e);
%to seperate each co-effecient we use E as string to find
d = strfind(D,'E');
%calculating the co-effecients
ha4 = D((1:d(1)+3));
ha4 = str2num(ha4);
ha5 = D(d(1)+4:d(2)+3);
ha5= str2num(ha5);
ha6 = D(d(2)+4:d(3)+3);
ha6 = str2num(ha6);
ha7 = D(d(3)+4:d(4)+3);
ha7 = str2num(ha7);
%to create two set(low temp,high temp) of values we use if condition
if T < LMT
%the formula is provided in the question
C_p = R*(La1+La2.*T+La3.*T.^2+La4.*T.^3+La5.*T.^4);
H = R*(La1+La2.*(T./2)+La3.*(T.^2./3)+La4.*(T.^3./4)+La5.*(T.^4./5)+La6./T);
S = R*(La1.*log(T)+La2+La3.*(T.^2./2)+La4.*(T.^3./3)+La5.*(T.^4./4)+La7);
else
C_p = R*(ha1+ha2.*T+ha3.*T.^2+ha4.*T.^3+ha5.*T.^4);
H = R*(ha1+ha2.*(T./2)+ha3.*(T.^2./3)+ha4.*(T.^3./4)+ha5.*(T.^4./5)+ha6./T);
S = R*(ha1.*log(T)+ha2+ha3.*(T.^2./2)+ha4.*(T.^3./3)+ha5.*(T.^4./4)+ha7);
endif
%defining the element in an array for molecular weight calculation
elements = ['H','O','C','N','A','S'];
%atomic weight of the respective elements
atomic_wt = [1,16,12,14,40,32];
%initialise the molecular weight as 0
mol_wt = 0;
%convert the numerical elements in molecule to respective characters
molecules = char(molecule);
%for loop is used to calculate the molecular weight for all gas molecules
for h = 1:length(molecules)
% nested loop is used to repeat the process until the length of elements
for g = 1:length(elements)
if strcmpi(molecules(h),elements(g)) %the molecule and element string is compared for same values
%molecular weight is calculated
mol_wt = mol_wt+atomic_wt(g);
%it is the number of times the element is there
position = g;
endif
end
%the molecule is converted double number from string
n= str2double(molecules(h));
if n>1 %the following command is used if n is greater than 1
mol_wt = mol_wt+atomic_wt(position)*(n-1);
endif
end
%making directory in the name of respective molecules name and make them current
mkdir(molecule)
cd(molecule)
%plotting between temperature vs specific heat,enthalpy,entropy
figure(1)
plot(T,C_p,'linewidth',3,'color','r')
xlabel('temp[K]')
ylabel('specific heat [KJ/Kg]')
title(molecule)
figure(2)
plot(T,H,'linewidth',3,'color','r')
xlabel('temp[K]')
ylabel('enthalpy [KJ]')
title(molecule)
figure(3)
plot(T,S,'linewidth',3,'color','r')
xlabel('temp[K]')
ylabel('entropy [KJ]')
title(molecule)
%saving the plot in .jpg format while making a folder.
saveas(figure(1),'specific heat.jpg')
saveas(figure(2),'enthalpy.jpg')
saveas(figure(3),'entropy.jpg')
cd...
%printing the molecular weight of molecules in the command window
sprintf('molecular weight %s : %f\n',molecules,mol_wt)
end
#Here I use octave to script the code#
EXPLANATION OF THE CODE:
COMMANDS USED: Learning new commands is very essential for executing this program,it will be a bit difficult without knowing this here i am going to list some commands with explaination that are used in this code.Though we know the concept of how to do the program ,without knowing the command it will be difficult.
fgetl() - this command is used to get the line from the data provided after using fopen.
strsplit - This command is used to split the string with a delimiter here we use a empty space.
str2num - It is used to convert the string(alphabets) to number
strfind/findstr - This command is used to get the desired string in the given line which is mentioned as its position nummber.
if else - This condition is used to get this or that answer where it is provided with necessary condition to permit the following step.
It should be terminated with end/endif command similar to for.
strcmp/strcmpi - This command is used to compare two strings of array or cell where it returns 1 if the strings are same 0 if it is not.
str2double - It is used to convert a string to a real or complex number.str2double can replace str2num
For unconvertible scalar or character string input str2double returns a NaN.
mkdir - This command is used to make a directory.Create a directory named DIRNAME in the directory PARENT, creating
any intermediate directories if necessary.
cd - This command is used to change the directory.EX:OLD_DIR = cd (DIR).Here Change the current working directory to DIR.
saveas - This command saves the file/image with the desired filename with desired format.
sprintf/fprintf - It is used to print the template that we want to print in the command window.
char - It is short for character, which is a data type that holds one character (letter, number, etc.) of data. It is one of the datatype.
- The first line shows that to clearing the previous commands and files,followed by opening the given file THERMO.dat selecting read option
- Then reading the following lines using fgetl command and get the global temperature which is in second line of the file we str2num to convert the string to numbers or array values
- Here we skip the comment lines as it is not needed for the program we use for loop to skip those lines.
- After that we start to write the program for finding the coeffecient of gas molecules,molecular weight of gas molecules.we have to repeat the code for 53 times in order to get the details,so we use for loop to get the recursive operation.
- Initially we want to understad the data file,there are four lines for each species first line consists name of gas and its local temperature range and the consecutive three lines contains the 14 co-effecient which is used for calculation,then we get the fgetl command to get the first line of the gas species.
- After getting the line we split the string using strsplit to seperate the name and temperature range.Then we get the name of all species by get the first column.By using str2num we get local temperature range we create array of temperature using linspace.
- For finding the co-effecient we get those lines and get each value by finding the common string in each and every value here it is 'E' by strfind command,where every value has E followed by three digits so that we take the limit from 1 to the position of E plus three digits ,then second value limit is from the first position of E plus four to second E position plus three and so on.
- We find the co-effecient of all the values by following the above process.the first seven values are low temperature range co-effecient and next seven are high temperature co-effecient.
- After finding the co-effecient ,we start to find the values of specific heat,enthalpy,entropy using the formula which is provided in the question.since we have to find two set of values for low and high temperature range we use if condition that states less than mid temperature.
- Then we want to find the molecular weight of the species , we assign the elements in a array and its atomic weight respectively then we run a for loop for the times of molecule length and element lenth inside the main loop.
- We calculate the molecular weight by simply comparing the string of molecule and elements which returns value if it is a single element inorder to get molecular weight of molecules like h20 we use a if condition where we define the formula that the atomic weight will be multiplied with number of molecules and it is summed with molecular weight calculated.After calculating the molecular weight we print the values using sprints for the number of times the molecule present there.
- Plot the curve between temperature versus specific heat,entropy and enthalpy for all the gas molecules and saving the plot in separate directory auromatically.To do that we use mkdir command and to make the directory current cd command is used.
- Finally we save the figure in the name of molecule using saveas command which takes figurename and format as input.
PLOTTING: Each gas molecules has three plots for specific heat,enthalpy,entropy with temperature.
O2:



N2:



CO2:



molecular weight of gas species displayed in command window is shown below:


This image shows saving the plot in seperate folder which contains the image of plot.

CONCLUSION:
- The code is scripted for the program and executed successfully.
- The molecular weight of the gas species is calculated.
- The plots for the temperature verse specific heat,enthalpy and entropy is drawn and stored in seperate directory which is done automatically.