Project 1 - Parsing NASA thermodynamic data
Main program:
clear
data = fopen('THERMO.dat','r');
r=fgetl(data);
a = fgetl(data);
b = str2num(a);
global_low_temp = b(1);
global_mid_temp = b(2);
global_high_temp = b(3);
for i = 3:5
q = fgetl(data);
end
counter1 = 1;
counter2 = 1;
for j = 1:212
base = fgetl(data);
m = strsplit(base);
n = m{end};
if n == '1'
%Species identifier
species = m{1};
all_species(counter1,:) = {species};
all_species(counter1,2) = m(end-1);%local_temp1
all_species(counter1,3) = m(end-2);%local_temp2
all_species(counter1,4) = m(end-3);%local_temp3
counter1 = counter1 + 1;
elseif n == '2'
l1 = strfind(base,'E');
c1 = str2double(base(1:(l1(1)+3)));
c2 = str2double(base((l1(1)+4):(l1(2)+3)));
c3 = str2double(base((l1(2)+4):(l1(3)+3)));
c4 = str2double(base((l1(3)+4):(l1(4)+3)));
c5 = str2double(base((l1(4)+4):(l1(5)+3)));
elseif n == '3'
l2 = strfind(base,'E');
c6 = str2double(base(1:(l2(1)+3)));
c7 = str2double(base((l2(1)+4):(l2(2)+3)));
c8 = str2double(base((l2(2)+4):(l2(3)+3)));
c9 = str2double(base((l2(3)+4):(l2(4)+3)));
c10 = str2double(base((l2(4)+4):(l2(5)+3)));
elseif n == '4'
l3 = strfind(base,'E');
c11 = str2double(base(1:(l3(1)+3)));
c12 = str2double(base((l3(1)+4):(l3(2)+3)));
c13 = str2double(base((l3(2)+4):(l3(3)+3)));
c14 = str2double(base((l3(3)+4):(l3(4)+3)));
const = [c1;c2;c3;c4;c5;c6;c7;c8;c9;c10;c11;c12;c13;c14];
all_constants((counter2),:) = {const};
counter2 = counter2 + 1;
end
end
all_data = [all_species all_constants]; %Combining all data into single cell for simplicity
counter3 = size(all_data);
%%
for k = 1:counter3(:,1)
this_species = all_data{k,1};
this_const = all_data{k,5};
local_temp1 = str2double(all_data{k,2});
local_temp2 = str2double(all_data{k,3});
local_temp3 = str2double(all_data{k,4});
all_local = [local_temp1;local_temp2;local_temp3];
max_local_temp = max([local_temp1,local_temp2,local_temp3]);
min_local_temp = min([local_temp1,local_temp2,local_temp3]);
local_temps = linspace(min_local_temp,max_local_temp,200);
%Finding mid temperature
for y = 1:3
if all_local(y,:) ~= max_local_temp && all_local(y,:) ~= min_local_temp
mid_local = all_local(y,:);
end
end
[Cp,H,S] = formula(this_const,local_temps,mid_local);
if ~exist('D:/Courses/_Matlab_practice/File_parsing/Species_results','dir')
mkdir('D:/Courses/_Matlab_practice/File_parsing/Species_results/',this_species);
else
mkdir('D:/Courses/_Matlab_practice/File_parsing/Species_results/',this_species);
end
%Plotting
figure(1);
plot(local_temps,Cp,'linewidth',2,'color','r');
grid on;
title(this_species);
xlabel('Temperature');
ylabel('Specific Heat');
figure(2);
plot(local_temps,H,'linewidth',2,'color','g');
grid on;
title(this_species);
xlabel('Temperature');
ylabel('Enthalpy');
figure(3);
plot(local_temps,S,'linewidth',2,'color','b');
grid on;
title(this_species);
xlabel('Temperature');
ylabel('Etropy');
cd(['D:\Courses\_Matlab_practice\File_parsing\Species_results\',this_species]);
saveas(1,'Specific Heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
cd('D:\Courses\_Matlab_practice\File_parsing')
[final_molecular_weight] = molecular_weight(this_species);
final_molecular_data(k,1) = {(this_species)};
final_molecular_data{k,2} = final_molecular_weight;
fprintf('The molecular mass of %s is f.\n',final_molecular_data{k,1},final_molecular_data{k,2});
end
Function for the formulae:
function [Cp,H,S] = formula(this_const,local_temps,mid_local)
R = 8.314;
for i = 1:length(local_temps)
if local_temps(i)>mid_local
Cp = (this_const(1) + this_const(2).*local_temps + this_const(3).*local_temps.^2 + this_const(4).*local_temps.^3 +this_const(5).*local_temps.^4).*R;
H = (this_const(1) + (this_const(2).*local_temps)./2 + (this_const(3).*local_temps.^2)./3 + (this_const(4).*local_temps.^3)./4 +(this_const(5).*local_temps.^4)./5 + this_const(6)./local_temps).*local_temps.*R;
S = (this_const(1).*log(local_temps) + this_const(2).*local_temps + (this_const(3).*local_temps.^2)./2 + (this_const(4).*local_temps.^3)./3 +(this_const(5).*local_temps.^4)./4 + this_const(7)).*R;
else
Cp = (this_const(8) + this_const(9).*local_temps + this_const(10).*local_temps.^2 + this_const(11).*local_temps.^3 +this_const(12).*local_temps.^4).*R;
H = (this_const(8) + (this_const(9).*local_temps)./2 + (this_const(10).*local_temps.^2)./3 + (this_const(11).*local_temps.^3)./4 +(this_const(12).*local_temps.^4)./5 + this_const(13)./local_temps).*local_temps*R;
S = (this_const(8).*log(local_temps) + this_const(9).*local_temps + (this_const(10).*local_temps.^2)./2 + (this_const(11).*local_temps.^3)./3 +(this_const(12).*local_temps.^4)./4 + this_const(14)).*R;
end
end
end
Function for the calculation of molecular mass:
function [final_molecular_weight] = molecular_weight(this_species)
elements = ['H','C','O','N','A','S'];
atomic_weight = [1.00794 12.0107 15.999 14.0067 39.948 32.065];
final_molecular_weight = 0;
for i = 1:length(this_species)
for j = 1:length(elements)
if strcmp(this_species(i),elements(j))
final_molecular_weight = final_molecular_weight + atomic_weight(j);
a = j;
end
end
n = str2num(this_species(i));
if n > 1
final_molecular_weight = final_molecular_weight + atomic_weight(a)*(n-1);
end
end
end
Code explanation:
- 1. The code opens the thermo.dat file
- The global temperatures are obtained
- a couple of lines are skipped
- The code basically reads the last character of the line and does the necessary extraction of constants based on the last line of code
- If the line ends with 1, then the local temperatures are extracted ( low, mid and high)
- The name of the species are also extracted at the same time.
- If the line ends with 2, then 5 constants are extracted i.e. c1 to c5.
- If the line ends with 3, then c6 to c10 are extracted.
- If the line ends with 4, then c11 to c14 are extracted.
- all of the constants are combined into one single array
- The species name, the local temperatures as well as the constants are stored in a single cell for easier access.
- Each row of the cell is extracted, first with the species name and then the constants.
- The local temps for each species are also extracted for that species.
- The max and the min are also identified along with the mid-temperature
- Cp, H and S are calculated using the function.
- the plots for Cp, H and S are shown and saved in the respective species named folder.
- The molecular weight is calculated
- The molecular weight along with the species name is displayed
- END
Results:

O2:



N2:



CO2:


