Project 1 - Parsing NASA thermodynamic data
Project 1- Parsing NASA Thermodynamic data
AIM: Write a code to extract all the co-efficients from the data file and Calculate the Specific Heat, Entropy and Enthalpy of all the different species.
OBJECTIVE:
- To Calculate Molecular Weight of the different species given and display in 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 suitable name. All these should be generated automatically.
THEORY:
File parsing is to split the file or other inputs or data and can be stored to calculate different calculations. 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.
Entropy: It is 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)))
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))
Algorithm:
- Firstly, THERMO.dat file is imported to the program by using the 'fopen' command and each and every line is read by using 'fgetl' command.
- 'fgetl' command is used to read the global temperature.
- 'strsplit' is used to split the each temperature and convert those string intp a cell array
- 'str2double' is used to covert those cell array containing string variable to decimal numbers
- Global temperatures are extracted and stored in the respective variables.
- Next three lines are skipped using a for loop and fgetl command.
- for the next 53 sets of species , for loop is used to extract all the 14 coefficients for each species
- to extract the coefficients 'strfind' is used to find the letter 'E' in the line and store each coefficents from c1 to c14
- after storing all the 14 coefficients a folder is created using the command 'mkdir' and change the file location using 'cd' command.
- create an array to store the local low temperature and local high temperature and having 1000 values.
- start a nested for loop and for each species calculate the sp. heat, enthalpy and entropy and store them in each variable.
- Plot the graphs for temperature vs sp. heat , temperature vs entropy and temperature vs enthalpy
- store thoss graphs plotted in their respective folder having species name.
- Calculate the Molecular weights of each species using a function name called molecular_weight.
- in that function store the species name and create 2 arrays to store the elements name and their respective atomic weights on the 2nd array.
- 'strcmp ' function is used to compare each letters with the array storing elements inside a for loop.
- after Calculating the molecular weights, they are displayed in the command window.
- close the file using 'fclose' command.
MAIN CODE:
% Parsing NASA Thermodynamic data
close all
clear all
clc
% Opening the 'THERMO' data file and reading the data file
file = fopen('THERMO.dat','r');
% Getting the Header
line_1= fgetl(file);
% Getting the Global Temperature
line_2= fgetl(file);
% Extracting the Global Temperature from the Line
A= strsplit(line_2,' ');
global_low_temp= str2double(A{2});
global_mid_temp= str2double(A{3});
global_high_temp= str2double(A{4});
% for Skipping the next three comment lines
for i=1:3
fgetl(file)
end
for i=1:53 % There are 53 sets of Species in the file
species= fgetl(file);
T1=strsplit(species,' ');
x= length(T1);
% Local Low temperature
temp1= str2double(T1{x-3});
% Local high Temperature
temp2= str2double(T1{x-2});
% Local mid Temperature
temp3=str2double(T1{x-1});
species_name= species(1:6);
% Extracting the 1st five Coefficients from the line
line1=fgetl(file);
L1 = strfind(line1,'E');
c1 = str2double(line1(1:(L1(1)+3)));
c2 = str2double(line1((L1(1)+4):(L1(2)+3)));
c3 = str2double(line1((L1(2)+4):(L1(3)+3)));
c4 = str2double(line1((L1(3)+4):(L1(4)+3)));
c5 = str2double(line1((L1(4)+4):(L1(5)+3)));
% Extracting the next five Coefficients from the line
line2 = fgetl(file);
L2 = strfind(line2,'E');
c6 = str2double(line2(1:(L2(1)+3)));
c7 = str2double(line2(L2(1)+4:L2(2)+3));
c8 = str2double(line2(L2(2)+4:L2(3)+3));
c9 = str2double(line2(L2(3)+4:L2(4)+3));
c10 = str2double(line2(L2(4)+4:L2(5)+3));
%Extracting the next four Coefficients from the line
line3 = fgetl(file);
L3= strfind(line3,'E');
c11 = str2double(line3(1:(L3(1)+3)));
c12 = str2double(line3(L3(1)+4:L3(2)+3));
c13 = str2double(line3(L3(2)+4:L3(3)+3));
c14 = str2double(line3(L3(3)+4:L3(4)+3));
%Creating directory for each species
cd('C:\Users\SUBHANKAR MOHANTA\Desktop\Matlab Image\File_Parsing\')
dir_name = species_name;
mkdir(dir_name);
Temp = linspace(temp1,temp2,1000);
R =8.314; %Universal gas constant in J/mol-K
%Calculation of the properties based on Upper and Lower temperature
for j = 1:length(Temp)
if Temp(j)>temp3
spec_heat = (c1+c2.*Temp+c3.*Temp.^2+c4.*Temp.^3+c5.*Temp.^4).*R;
Enthalpy = (c1+(c2.*Temp)./2+(c3.*Temp.^2)/3+(c4.*Temp.^3)/4+(c5.*Temp.^4/5)+c6./Temp).*Temp.*R;
Entropy = (c1.*log(Temp)+c2.*Temp+(c3.*Temp.^2)/2+(c4.*Temp.^3)/3+(c5.*Temp.^4)/4+c7).*R;
else
spec_heat = (c8+c9.*Temp+c10.*Temp.^2+c11.*Temp.^3+c12.*Temp.^4).*R;
Enthalpy = (c8+(c9.*Temp)/2+(c10.*Temp.^2)/3+(c11.*Temp.^3)/4+(c12.*Temp.^4/5)+c13./Temp).*Temp.*R;
Entropy = (c8.*log(Temp)+c9.*Temp+(c10.*Temp.^2)/2+(c11.*Temp.^3)/3+(c12.*Temp.^4)/4+c14).*R;
end
end
%Plotting
% Temperature vs Entropy
figure(1)
plot(Temp, Entropy,'linewidth',2,'color','r')
title('Entropy Plot of',species_name);
xlabel('Temperature [T]')
ylabel('Entropy [S] ')
grid on
%Temperature vs Enthalpy
figure(2)
plot(Temp,Enthalpy,'linewidth',2,'color','g')
title(['Enthalphy Plot of ', species_name]);
xlabel('Temperature [T]')
ylabel('Enthalpy(H)--->')
grid on
%Temperature vs Specific heat
figure(3)
plot(Temp,spec_heat,'linewidth',2,'color','b')
title(['Specific Heat Plot of ', species_name])
xlabel('Temperature [T]')
ylabel('Specific heat [cp]')
grid on
pause(0.0001)
cd(dir_name);
%Saving the plots in directory
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');
molecular_W= molecular_weight(species_name);
K = sprintf('Molecular weight of %s is %f',species_name,molecular_W);
disp(K) % The weight is made to display in the command window
end
fclose(file)
THE FUNCTION CODE:
function M = molecular_weight(species_name) species = (species_name); elements = ['H','C','N','O','A']; Atomic_weight = [1,12,14,16,40]; % Assigning atomic weight for each array element of a species M=0; % Initialising value for M for i=1:length(species) for j = 1:length(elements) if strcmp(species_name(i),elements(j)) %Compares the species name with species array M=M+Atomic_weight(j); x=j; end end n=str2num(species_name(i)); % extracts the numbers present in the species name if n>1 M=M+(Atomic_weight(x)*(n-1)); % To add the repetitive elements of the species with the weight end end end
ERRORS FOUND DURING CODING:
1
.
Wrong Expression written to covert the string to decimal.
2.

Wrong file namegiven to create a folder.
OUTPUTS:
1
Folders having all the different species.

2. Molecular weight of each species displayed in command window.


3 Plots of Oxygen



4 Plots of Nitrogen



5 plots of Cabon Dioxide



REFERENCES:
- https://in.mathworks.com/help/matlab/ref/fgetl.html
- https://in.mathworks.com/help/matlab/ref/str2double.html#:~:text=X%20%3D%20str2double(%20str%20)%20converts,X%20is%20a%20numeric%20scalar.
- https://skill-lync.com/student-projects/NASA-thermodynamic-file-parsing-57964
- https://in.mathworks.com/help/matlab/ref/strfind.html
- https://in.mathworks.com/help/stateflow/ref/strcmp.html#:~:text=tf%20%3D%20strcmp(%20s1%2Cs2%20)%20compares%20strings%20s1%20and,the%20two%20strings%20are%20identical.
FILE SHARING:
https://drive.google.com/drive/folders/19SG4r80l3Jt2C9aqU-Nq0hKvY6BQ9nb0?usp=sharing