Project 1 - Parsing NASA thermodynamic data
Aim:To parse the thermodynamic data file of NASA and calculate the thermoynamic properties of the gas species provided in tha file.
Theory: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. The main aim in this project is to capture all the coefficients provided in the file and to calulate the enthapy , entropy and the specific heat of the particular species.
Governing Equations:
`Cp=R*(a1+a2*T+a3*(T^2)+a4*(T^3)+a5*(T^4))`
`H=R*T*(a1+a2*(T/2)+a3*((T^2)/3)+a4*((T^3)/4)+a5*((T^4)/5)+a6/T)`
`S=R*(a1*log(T)+a2*T+a3*((T^2)/2)+a4*((T^3)/3)+a5*((T^4)/4)+a7)`
where a1 a2 a3 a4 a5 a6 a7 are the coefficients used while considering temparature between local medium to local maximum.
b1 b2 b3 b4 b5 b6 b7 are the coefficients used while considering temparature between local minimum to local medium.
Cp= Specific heat
H = Enthalpy
S= Entropy
Software Used: MATLAB R2018b
Commands Used:
- fopen(): This format is used to open the data file provided.
- fgetl(): This command reads the first data in the file.
- str2num(): Converts string data into numbers..
- feof(): This is used in while loop, which reads the file line by line till the end.
- strsplit(): This command is used to split the cell array according to the provided delimitter.
- strcmp(str1,str2): Compares the elemental position of each string (mainly used in if else statements).
- str2double(): Converts string elements to numbers,with faster processing.
- findstr(): Finds the position particular string element in a particular string.
- mkdir(): Creates a directory with name specified inside the parantheses.
- cd(): Changes from one direcctory to another directory specified in the parantheses.
- saveas(): Saves the file with name provided inside the parantheses.
MATLAB program:
There two types of programs attached in this report.
- A program which calculates therodynamic properties of all the species ,plots the respective graphs and saves the file automatically.
- This program is more user friendly , which asks the user to enter the name of the species, and calculates the therodynamic properties only of that specie.
TYPE 1 :
Main Code:
clear all
close all
clc
f1=fopen('THERMO.dat','r') %opening the file provided
fgetl(f1)
A=fgetl(f1); %reading the first line
a=str2num(A) %cnverting the string into numbers
global_minima=a(1); %finding the global maxima, minima and medium temparatures
global_medium=a(2);
global_maxima=a(3);
for i=1:3
disp(fgetl(f1)) %displaying the 3 comments using a for loop and fgetl command
end
while ~ feof(f1) %reads all the lines in a loop
line1=strsplit(fgetl(f1),' '); %the first line of the spcies is splitted with a space delimitter
if strcmp(line1,'END') %comparing to know the if the string reaches the last line of the file
break; % if it reaches the last line ,it comes out of the loop
end
species=line1{1}; %assigning the first value of the line as the name of the species
l=length(line1);
local_minimum=str2double(line1{l-3});%finding the local minima maxima and medium temparatures of particular species
local_medium=str2double(line1{l-1});
local_maximum=str2double(line1{l-2});
%moving to the next line(extracting the coeficients)
line2=fgetl(f1);%getting the first line of the coefficients
line3=fgetl(f1); %getting the second line of coefficients
line4=fgetl(f1); %getting the third line of coefficients
A=findstr(line2,'E'); %finding the symbol of exponent using findstr command
B=findstr(line3,'E');
C=findstr(line4,'E');
a1=line2(1:A(1)+3); %assigning the values of coefficients to differnt variables
a2=line2(A(1)+4:A(2)+3);
a3=line2(A(2)+4:A(3)+3);
a4=line2(A(3)+4:A(4)+3);
a5=line2(A(4)+4:A(5)+3);
a6=line3(1:B(1)+3);
a7=line3(B(1)+4:B(2)+3); %end of high temparature coefficients
b1=line3(B(2)+4:B(3)+3);
b2=line3(B(3)+4:B(4)+3);
b3=line3(B(4)+4:B(5)+3);
b4=line4(1:C(1)+3);
b5=line4(C(1)+4:C(2)+3);
b6=line4(C(2)+4:C(3)+3);
b7=line4(C(3)+4:C(4)+3);%end of low temparature coefficients
a1=str2double(a1); %converting all the strings to numbers
a2=str2double(a2);
a3=str2double(a3);
a4=str2double(a4);
a5=str2double(a5);
a6=str2double(a6);
a7=str2double(a7);
b1=str2double(b1);
b2=str2double(b2);
b3=str2double(b3);
b4=str2double(b4);
b5=str2double(b5);
b6=str2double(b6);
b7=str2double(b7);
T=linspace(local_minimum,local_maximum,100);%100 values of temparatures are created using linspace command
for i=1:length(T) %calling the function to calculte the thermal properties
[Cp(i),H(i),S(i)]= thermal_properties(local_medium,T(i),a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7);
end
%creating folder
mkdir (species); %creating separte foleders for the plots to get dumped in
cur_dir=pwd;
cd(species);
figure(1) %creating the first plot (Cp vs Temparature)
plot(T,Cp,'linewidth',2,'color','r')
graph_title=sprintf('Specific heat v/s local temparature range of %s',species);
title(graph_title);
xlabel('Local temparature range')
ylabel('specific heat')
figure(2) %creating the second plot (H vs Temparature)
plot(T,H,'linewidth',2,'color','b')
graph_title=sprintf('Enthalpy v/s local temparature range of %s',species);
title(graph_title);
xlabel('Local temparature range')
ylabel('Enthalpy')
figure(3) %creating the third plot (S vs Temparature)
plot(T,S,'linewidth',2,'color','g')
graph_title=sprintf('Entropy v/s local temparature range of %s',species);
title(graph_title);
xlabel('Local temparature range')
ylabel('Entropy')
saveas(1,'Cp vs T.png'); %saving the plots as named below
saveas(2,'Enthalpy vs T.png');
saveas(3,'Entropy vs T.png');
cd(cur_dir);
%molecular weight
Molecular_Weight= molecular_weight(species); %calling the function to calculate the moelcular weight
fprintf('The molecular weight of %s is %d',species,Molecular_Weight) %printing the values of the molecular weight in the command window
fprintf('\n')
end
Main Code Explanation:
- The code is started with clear all, close all ,and clc which cleares the screen and all the preexisting plots.
- Using the fopen command the THERMO.dat file is opened and this assigned to a file ID f1.
- The first line of the file is read using fgetl command
- The second line has the data of the global temperatures.
- So this line is extracted using fgetl command
- The line is saved as a cell array which contains the data of all formats.
- So str2num command is used which converts the string array into numbers and these numbers are assigned to three different variables namely global_maxima, global_medium,and global_minima
- The next 3 lines are considered as the comments since it starts from exclamatory mark.
- So these lines are also read fgetl command and the for loop is used to make the process more easier.
- Now inorder to read the species while loop is used.
- The syntax used is :while~feof(f1) .The feof command helps to read all the lines till the end of the file.
- So when the first line of the species is encountered,the first line is broken into parts using strsplit using the delimitter as space
- Later an if statement is used which includes string comparision, ie when the loop encounters the word END , the program comes out of the loop.
- Now the first line of the line1 is assigned to a variable named species.
- And the values of local maxima , local minima , and local medium temperatures are found
- Now the next three lines are the coefficients of the specie.
- Inorder to extract these, the findstr command is used which finds the position of a particular string element
- Acording to the position of that string the coefficients are extracted.
- Once the extraction of the coefficients is over, the function is called which calculates the thermodynamic properties.
- The thermodynamic properties are found for range of 100 temperatures between local minima and local maxima
- After the calulation is over , a new directory is made using mkdir command whose names will be the names of the species.
- The command cd is used which changes the path of the directory into new directory named after the species
- The plots of Cp , H , S vs T are drawn and are saved in png format using saveas command. The saveas command saves the file with the name specified.
- After the plots for one specie is done , cd command is again used which changes the directory into current directory.
- Lastly a function which calculates the molecular weight is called, and using fprintf command, the results are printed in the command window.
Function used to calculate the thermodynamic proprties:
function [Cp,H,S]= thermal_properties(local_medium,T,a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7)
R=8.314; %Universal gas constant in KJ/Kg-K
if(T>=local_medium)
Cp=R*(a1+a2*T+a3*(T^2)+a4*(T^3)+a5*(T^4)); %calculation of properties using high temparature coefficients
H=R*T*(a1+a2*(T/2)+a3*((T^2)/3)+a4*((T^3)/4)+a5*((T^4)/5)+a6/T);
S=R*(a1*log(T)+a2*T+a3*((T^2)/2)+a4*((T^3)/3)+a5*((T^4)/4)+a7);
else
Cp=R*(b1+b2*T+b3*(T^2)+b4*(T^3)+b5*(T^4)); %calculation of properties using low temparature coefficients
H=R*T*(b1+b2*(T/2)+b3*((T^2)/3)+b4*((T^3)/4)+b5*((T^4)/5)+b6/T);
S=R*(b1*log(T)+b2*T+b3*((T^2)/2)+b4*((T^3)/3)+b5*((T^4)/4)+b7);
end
end
Code Explanation:
- This function is saved as thermal_properties.
- It takes the inputs such as local_medium,T,a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7
- The value of the universal gas constant is specified.
- An if else statement is used to calculate the thermodynamic properties.
- The equations used are already specified above.
Function Used to caculate the molecular weights of the species.
function M= molecular_weight(species) % function to find the molecular weight
elements=['H' 'C' 'O' 'N' 'Ar']; %all the elements which were there in the file
Atomic_mass=[1 12 16 14 40]; %their respective atomic masses
M=0; %initially the molecular weight is 0
for i=1:length(species)
for j=1:length(elements)
if strcmp(species(i),elements(j)) %string comparision
M=M+Atomic_mass(j); %loic to find the molecular weight of the individual elements
x=j;
end
end
n=str2num(species(i)); %if the same element is repeated more than once the following logic is used
if n>1
M=M+(Atomic_mass(x)*(n-1));
end
end
end
Code Explanation:
- The function is saved under the name of molecular_weight.
- Two arrays are created namely, elements and Atomic_mass.
- The array elements, contains the string elements , which are nothing but the element names.
- The Atomic_mass contains the atomic masses of the elements specified above.
- Initial molecular weight is considered as zero.
- Two for loops are used to calculate the moleular weight.
- The for loops can only calculate the molecular weights of individual elements,meaning ,if there are not more than one element of same type.
- In order to calculate the molecular weights of all the elements in a compound , if statement is used, which basically calculates the number of atoms of a specific element.
- Later the equation used in the code is used to calculate the molecular weights of all the compounds.
- This value is returned back to the main function.
Results and Plots:
Plots of O2:
Specific heat vs local temperature range
Enthalpy vs local temperature range
Entropy vs local temperature range
Plots of CO2:
Specific heat vs local temperature range
Enthalpy vs local temperature range
Entropy vs local temperature range
Plots of N2:
Specific heat vs local temperature range
Enthalpy vs local temperature range
Entropy vs local temperature range
Screenshot showing the folders containing the plots of species
Screenshot showing the molecular weights of individual specie
TYPE 2: User friendly code
Main code:
clear all
close all
clc
f1=fopen('THERMO.dat','r')
fgetl(f1)
A=fgetl(f1);
a=str2num(A);
global_minima=a(1);
global_medium=a(2);
global_maxima=a(3);
for i=1:3
disp(fgetl(f1))
fprintf('\n')
end
fprintf('\n')
x=input('Please enter the species name: ','s');
x=upper(x)
while ~ feof(f1)
line1=strsplit(fgetl(f1),' ');
if strcmp(line1,'END')==1
break;
end
species=line1{1};
if strcmp(species,x)==1
l=length(line1);
local_minimum=str2double(line1{l-3});
local_medium=str2double(line1{l-1});
local_maximum=str2double(line1{l-2});
%moving to the next line(extracting the coeficients)
line2=fgetl(f1);%getting the first line of the species
line3=fgetl(f1);
line4=fgetl(f1);
A=findstr(line2,'E');
B=findstr(line3,'E');
C=findstr(line4,'E');
a1=line2(1:A(1)+3);
a2=line2(A(1)+4:A(2)+3);
a3=line2(A(2)+4:A(3)+3);
a4=line2(A(3)+4:A(4)+3);
a5=line2(A(4)+4:A(5)+3);
a6=line3(1:B(1)+3);
a7=line3(B(1)+4:B(2)+3);
b1=line3(B(2)+4:B(3)+3);
b2=line3(B(3)+4:B(4)+3);
b3=line3(B(4)+4:B(5)+3);
b4=line4(1:C(1)+3);
b5=line4(C(1)+4:C(2)+3);
b6=line4(C(2)+4:C(3)+3);
b7=line4(C(3)+4:C(4)+3);
a1=str2double(a1);
a2=str2double(a2);
a3=str2double(a3);
a4=str2double(a4);
a5=str2double(a5);
a6=str2double(a6);
a7=str2double(a7);
b1=str2double(b1);
b2=str2double(b2);
b3=str2double(b3);
b4=str2double(b4);
b5=str2double(b5);
b6=str2double(b6);
b7=str2double(b7);
T=linspace(local_minimum,local_maximum,100);
for i=1:length(T)
[Cp(i),H(i),S(i)]= thermal_properties(local_medium,T(i),a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7);
end
%creating folder
mkdir (species);
cur_dir=pwd;
cd(species);
figure(1)
plot(T,Cp,'linewidth',2,'color','r')
graph_title=sprintf('Specific heat v/s local temparature range',species);
title(graph_title);
xlabel('Local temparature range')
ylabel('specific heat')
figure(2)
plot(T,H,'linewidth',2,'color','b')
graph_title=sprintf('Enthalpy v/s local temparature range',species);
title(graph_title)
xlabel('Local temparature range')
ylabel('Enthalpy')
figure(3)
plot(T,S,'linewidth',2,'color','g')
graph_title=sprintf('Entropy v/s local temparature range',species);
title(graph_title);
xlabel('Local temparature range')
ylabel('Entropy')
saveas(1,'Cp vs T.png');
saveas(2,'Enthalpy vs T.png');
saveas(3,'Entropy vs T.png');
cd(cur_dir)
Molecular_Weight= molecular_weight(species); %calling the function to calculate the moelcular weight
fprintf('The molecular weight of %s is %d',species,Molecular_Weight) %printing the values of the molecular weight in the command window
fprintf('\n')
break;
end
end
species=line1{1};
if strcmp(species,x)==0
fprintf('invalid input')
end
- Small changes are made in the previous code inorder to obtain more user friendly experience.
- Once after the three comments are printed, the user is asked to enter the name of the specie of his choice and this is stored in a variable named x.
- The value entered can be case insensitive, as the entered string will be converted to upper case using upper command.
- In the while loop , a comparison is made using if statement, as follows
if strcmp(species,x)==1 , only if this statement is true, the further statements will get executed.
- If the above statement is false , then the lines below it will not get executed.
- If the user inputs a wrong key other than the names of the element, then it will dislay as 'invalid input' in the command window.
- The function code for both the molecular weight and the thermal properties remain the same.
Results:
It asks for the user to enter the specie.
It creates displays the moleclar weight and also creates the respective graphs and the folder
It also displays as 'invalid input' if the input value doesnot match with the spcie name.
Errors faced:
There were alot of typing errors made while writing the program.
And also syntax errors were common which were resolved later by learning from the matlab documentation.
Reference links:
https://www.quora.com/What-does-it-means-to-parse-a-file
https://in.mathworks.com/matlabcentral/answers/106320-make-a-loop-to-calculate-molecular-weight
Inference:
File parsing is a very important concept which helps us to read and write data into a file and make further use of it. MATLAB is a powerful software which can conduct these opertions very effectievely.