Project 1 - Parsing NASA thermodynamic data
Aim and Objective:-
- To write the function that extracts the co-efficients from Nasa File using fopen and fgetl, and also to calculate the Specific Heat, Enthalpy and Entropy.
- To plot the graph of Specific heat, enthalpy and entropy against the local temperature range for each species.
- To calculate and display the molecular weight of each Species.
- Create separate folder for every species with graph of specific heat, enthalpy and entropy of every species.
General Formula used :-
`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`
Methods used :-
- For loop and fgetl is combinely used to extract and store each and every line and co-efficients from the NASA file.
- It is also used to store graphs of every coefficient in a defined folder.
- Function is written to calculate the Specific heat, Enthaly and Entropy
- Function is written to calculate the Molecular weight of Species.
Content Body :-
- For loop is used to get the data of 53 species.
- Fgetl is used to extract each and every co-efficient of the specific species.
- Num(molecule) is a program which is used to extract the 53 lines of species with there local temperature. And their corresponding lines are named as num(molecule)+1 i.e L1,L2 and L3
- Fopen and fgetl are used to extract and store each and every line from the Nasa file.
- Strsplit is used to split the string of line and present as individual string in that line
- Str2double is used to convert the numbers from string to number.
- Strfind is used to find the positions of E and G, which is also used to extract the co-efficient for particular species.
- Function is called to calculate the specific heat, enthalpy and entropy and also molecular weight of that species.
- Graph is plotted for specific heat, enthalpy and entropy against temperature for every species.
- Mkdir is used to create the folder
- Sprint is used to name the file we save
- Fullfile is used to create the path to save the file
- Saveas is used to save the file in the corresponding folder
clear all
close all
clc
tic
for molecule = 1 : 53;
num = 6:4:218; %species starting point
L1 = num(molecule)+1; %coefficient line 1 of species
L2 = num(molecule)+2; %coefficient line 2 of species
L3 = num(molecule)+3; %coefficient line 3 of species
%Reading a file and extracting every single line
nasa_file = fopen('THERMO.dat','r');
for line = 1:218;
fline{line} = fgetl(nasa_file);
end
%Input
R = 8.314; % Universal gas constant
global_temp = strsplit(fline{2},' ');
min_global_temp = str2double(global_temp{2});
mid_global_temp = str2double(global_temp{3});
max_global_temp = str2double(global_temp{4});
%Species and local temp extraction
local_temp = fline{num(molecule)};
get_species = strsplit(local_temp,' ');
species = get_species{1};
%to find position G and local temp
position_G = strfind(fline{num(molecule)},'G');
min = local_temp(position_G(1)+2:position_G(1)+10);
max = local_temp(position_G(1)+12:position_G(1)+20);
mid = local_temp(position_G(1)+22:position_G(1)+30);
tmin = str2double(min);
tmax = str2double(mid);
tmid = str2double(max);
%finding the position E and also coefficient of species
position_E1 = strfind(fline{L1},'E'); %finding E
ceff_line1 = fline{L1};
ceff1 = ceff_line1(1:position_E1(1)+3);
ceff2 = ceff_line1(position_E1(1)+4:position_E1(2)+3);
ceff3 = ceff_line1(position_E1(2)+4:position_E1(3)+3);
ceff4 = ceff_line1(position_E1(3)+4:position_E1(4)+3);
ceff5 = ceff_line1(position_E1(4)+4:position_E1(5)+3);
position_E2 = strfind(fline{L2},'E'); %finding E
ceff_line2 = fline{L2};
ceff6 = ceff_line2(1:position_E2(1)+3);
ceff7 = ceff_line2(position_E2(1)+4:position_E2(2)+3);
ceff8 = ceff_line2(position_E2(2)+4:position_E2(3)+3);
ceff9 = ceff_line2(position_E2(3)+4:position_E2(4)+3);
ceff10 = ceff_line2(position_E2(4)+4:position_E2(5)+3);
position_E3 = strfind(fline{L3},'E'); %finding E
ceff_line3 = fline{L3};
ceff11 = ceff_line3(1:position_E3(1)+3);
ceff12 = ceff_line3(position_E3(1)+4:position_E3(2)+3);
ceff13 = ceff_line3(position_E3(2)+4:position_E3(3)+3);
ceff14 = ceff_line3(position_E3(3)+4:position_E3(4)+3);
%converting string to num
cef1 = str2double(ceff1);
cef2 = str2double(ceff2);
cef3 = str2double(ceff3);
cef4 = str2double(ceff4);
cef5 = str2double(ceff5);
cef6 = str2double(ceff6);
cef7 = str2double(ceff7);
cef8 = str2double(ceff8);
cef9 = str2double(ceff9);
cef10 = str2double(ceff10);
cef11 = str2double(ceff11);
cef12 = str2double(ceff12);
cef13 = str2double(ceff13);
cef14 = str2double(ceff14);
%creatinf an array of coefficient
Cefff = [cef1;cef2;cef3;cef4;cef5;cef6;cef7;cef8;cef9;cef10;cef11;cef12;cef13;cef14];
%callinf a function to calculate properties
%tpoint = 2*(number of temperature points between tmin and tmax)
tpoint = 100;
[properties] = thermo_file(Cefff,tmin,tmax,tmid,tpoint,R);
data = properties';
%calling a function to calculate molecular weight
molecular_weight = chemical_formula(species);
disp(['Molecular weight of' species]);
disp(molecular_weight)
%ploting
%temperature vs specific heat
figure(1)
plot(data(:,1),data(:,2),'linewidth',3,'color','r')
xlabel('Temperature')
ylabel('Specific Heat')
title('Species','color','k')
grid on
figure(2)
plot(data(:,1),data(:,3),'linewidth',3,'color','b')
xlabel('Temperature')
ylabel('Enthalpy')
title('Species','color','k')
grid on
figure(3)
plot(data(:,1),data(:,4),'linewidth',3,'color','g')
xlabel('Temperature')
ylabel('Entropy')
title('Species','color','k')
grid on
%create folder and save the graph
%for each iteration SPECIES is different therefore 'mkdir'
%create a directory for a SPECIES FOLDER in working folder
folder_name = [species];
mkdir(folder_name);
figure_name1 = sprintf([species '_SpHeat.jpg']) ;
figure_name2 = sprintf([species '_Enthalpy.jpg']);
figure_name3 = sprintf([species '_Entropy.jpg']);
folder_path1 = fullfile(folder_name,figure_name1);
folder_path2 = fullfile(folder_name,figure_name2);
folder_path3 = fullfile(folder_name,figure_name3);
saveas(figure(1),folder_path1);
saveas(figure(2),folder_path2);
saveas(figure(3),folder_path3);
end
Study_time = toc
Function to calculate Specific heat, Enthalpy and Entropy :-
- Properties is an array of temperature, specific heat, enthalpy and entropy.
- Cefff is an array of 14 coefficients, first 7 co-efficient is a high temperature co-efficient and last 7 co-efficient is low temperature co-efficient.
- R is a universal gas constant.
- For loops are used to calculate the high temperature and low temperature co-efficient.
%to find specific, enthalpy, entropy for each species
function [properties] = thermo_file(Cefff,tmin,tmax,tmid,tpoint,R);
a1 = Cefff(1);
a2 = Cefff(2);
a3 = Cefff(3);
a4 = Cefff(4);
a5 = Cefff(5);
a6 = Cefff(6);
a7 = Cefff(7);
a8 = Cefff(8);
a9 = Cefff(9);
a10 = Cefff(10);
a11 = Cefff(11);
a12 = Cefff(12);
a13 = Cefff(13);
a14 = Cefff(14);
%Calculating enthalpy, entropy and specific heat for low temperature
lt = linspace(tmin,tmid,tpoint); % low temperature point
ht = linspace(tmid,tmax,tpoint); % high temperature point
%low temperature are a8,a9,a10,a11,a12,a13,a14
for i = 1 : length(lt)
cp(i) = (a8 + (a9*lt(i)) + (a10*lt(i).^2) + (a11*lt(i).^3) + (a12*lt(i).^4))*R;
h(i) = (a8 + (a9*((lt(i).^2)./2)) + (a10*((lt(i).^3)./3)) + (a11*((lt(i).^4)./4)) + (a12*((lt(i).^5)./5)) + (a13/lt(i)))*R;
s(i) = (a8*log(lt(i)) + (a9*lt(i)) + (a10*((lt(i).^2)./2)) + (a11*((lt(i).^3)./3)) + (a12*((lt(i).^4)./4)) + a14)*R;
temperature(i) = lt(i);
end
%high temperature are a1,a2,a3,a4,a5,a6,a7
for j = 1:length(ht);
cp(j) = (a1 + (a2*ht(j)) + (a3*ht(j).^2) + (a4*ht(j).^3) + (a5*ht(j).^4))*R;
h(j) = (a1 + (a2*((ht(j).^2)./2)) + (a3*((ht(j).^3)./3)) + (a4*((ht(j).^4)./4)) + (a5*((ht(j).^5)./5)) + (a6/ht(j)))*R;
s(j) = (a1*log(ht(j)) + (a2*ht(j)) + (a3*((ht(j).^2)./2)) + (a4*((ht(j).^3)./3)) + (a5*((ht(j).^4)./4)) + a7)*R;
temperature(j) = ht(j);
end
%Results
properties = [temperature;cp;h;s];
end
Function to calculate molecular weight:-
- Species is an input to the function.
- For loop is used to extract and calculate the molecular weight of species.
- Strcmpi is used to compare the Element with species and gives the logical number in the form of 0 and 1.
- An array of mass and and numeric is created to store the mass number and number of molecules.
- If h=1 it assigns the mass to species and if h=0 it converts the string to num. If it is greater than 1 it will assign to numeric(p).
%calculating the molecular weight
function molecular_weight = chemical_formula(species);
elements = {'H' 'C' 'N' 'O' 'A'};
atomic_mass = [1 12 14 16 40];
%to store atomic mass of each element
mass = zeros(1,length(species));
%to store each molecule in the species
numeric = ones(1,length(species)+1);
%to get atomic weight of each element in a species
for q = 1:length(species);
for p = 1:length(elements);
h = strcmpi(species(q),elements(p));
if h == 1;
mass(q) = atomic_mass(p);
else
digit = str2double(species(q));
if digit > 1;
numeric(q) = digit;
end
end
end
end
%zeroes of mass arrays are replaced by atomic number of species
%ones of numeric arrays are replaced by no. of molecules
mass;
numeric;
initial = 0;
%to calculate molecular weight of species
for r = 1:length(species);
Mass = initial + (mass(r).*numeric(r+1));
initial = Mass;
end
molecular_weight = Mass;
end
Conclusion:-
- Specific Heat, enthalpy and entropy is calculated.
- Molecular weight is calculated and also screenshot is attached.
- Plots are attached for O2, N2 and Co2 species.