Challenge

Project 1 - Parsing NASA thermodynamic data

Main script

clear all
close all
clc

f=fopen('THERMO.dat','r');

%Temperature raange definition
T=linspace(200,3500,200);
%Universal gas constant
R = 8.314;

for i =1:5
fgetl(f);
end

for k=1:53
c = fgetl(f);
d = strsplit(c,' ');
species_name = d{1};
z = length(d);
t1 = str2double(d{z-3}); %lowtemp
t2 = str2double(d{z-2}); %hightemp
t3 = str2double(d{z-1}); %midtemp

%To obtain high temperature coefficients
coeff_l1 = fgetl(f);
pos_E = strfind(coeff_l1,'E');
a1 = str2double(coeff_l1(1:pos_E(1)+3));
a2 = str2double(coeff_l1((pos_E(1)+4):(pos_E(2)+3)));
a3 = str2double(coeff_l1((pos_E(2)+4):(pos_E(3)+3)));
a4 = str2double(coeff_l1((pos_E(3)+4):(pos_E(4)+3)));
a5 = str2double(coeff_l1((pos_E(4)+4):(pos_E(5)+3)));
coeff_l2 = fgetl(f);
pos_E1 = strfind(coeff_l2,'E');
a6 = coeff_l2(1:pos_E1(1)+3);
a7 = coeff_l2((pos_E1(1)+4):(pos_E1(2)+3));
%To obtain low temperature coefficients
a8 = str2double(coeff_l2((pos_E1(2)+4):(pos_E1(3)+3)));
a9 = str2double(coeff_l2((pos_E1(3)+4):(pos_E1(4)+3)));
a10 = str2double(coeff_l2((pos_E1(4)+4):(pos_E1(5)+3)));
coeff_l3 = fgetl(f);
pos_E2 = strfind(coeff_l3,'E');
a11 = str2double(coeff_l3(1:pos_E2(1)+3));
a12 = str2double(coeff_l3((pos_E2(1)+4):(pos_E2(2)+3)));
a13 = str2double(coeff_l3((pos_E2(2)+4):(pos_E2(3)+3)));
a14 = str2double(coeff_l3((pos_E2(3)+4):(pos_E2(4)+3)));
%To calculate the values of specific heat,enthalpy and entropy.
[cp,H,S]= thermo_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,T,t1,t3,t2);
%Plotting the output graphs
plotting(cp,H,S,T,species_name)
%Calculating molecular weights
molecular_weight(k)= mol_weight(species_name);
disp(molecular_weight(k));
mole_weight = strcat(molecular_weight(k),species_name);

%Function to write molecular weight to file
weight = molecular_weight(k);
write_to_file(species_name,weight);

end
fclose(f);

Function scripts
%To calculate specific heat,enthalpy and entropy


function[cp,H,S]= thermo_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,T,t1,t3,t2)
if T<=t3
cp=R*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
H= R*((a1*T)+a2*((T.^2)/2)+a3.*(T.^3/3)+a4.*(T.^4/4)+a5.*(T.^5/5)+a6);
S= R*(a1*log(T)+a2*T+a3.*(T.^2/2)+a4.*(T.^3/3)+a5.*(T.^4/4)+a7);
else
cp=R*(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4);
H= R*((a8*T)+a9*((T.^2)/2)+a10.*(T.^3/3)+a11.*(T.^4/4)+a12.*(T.^5/5)+a13);
S= R*(a8*log(T)+a9*T+a10.*((T.^2)/2)+a11.*((T.^3)/3)+a12.*((T.^4)/4)+a14);
end

%Function to plot graphs for all the species

function plotting(cp,H,S,T,species_name)
cd(pwd);
mkdir('C:\Users\hp\Desktop\Matlas\Species',species_name);
%Plotting temperature versus specific heat

figure(1)
plot(T,cp,'LineWidth',3,'Color','b')
title(sprintf('Specific heat vs temperature for %s:',string(species_name)));
grid on
xlabel('Temperature')
ylabel('Specific heat')

%Plotting temperature versus enthalpy

figure(2)
plot(T,H,'LineWidth',3,'Color','g')
title(sprintf('Enthalpy vs temperature for %s:',string(species_name)));
grid on
xlabel('Temperature')
ylabel('Enthalpy')

%Plotting temperature versus entropy

figure(3)
plot(T,S,'LineWidth',3,'Color','r')
title(sprintf('Entropy vs temperature for %s:',string(species_name)));
grid on
xlabel('Temperature')
ylabel('Entropy')
cd('C:\Users\hp\Desktop\Matlas\Species');
saveas(1,'Specific_heat vs temperature.png')
saveas(2,'Enthalpy vs temperature.png')
saveas(3,'Entropy vs temperature.png')
cd .. ;
end

%Function to calculate molecular weight

function[m] = mol_weight(species_name)
e = {'H' 'C' 'N' 'O' 'AR'};

%atomic weight of each element
atomic_wt = [1 12 14 16 40];
m = 0;

for i=1:length(species_name)
for j = 1:length(e)
if strcmp(species_name(i),e(j))
m = m + atomic_wt(j);
position = j;
end
end

n = str2double(species_name(i));
if n>1
m = m +atomic_wt(position)*(n-1);
end
end
end

%Write molecular weights of species to a file

function write_to_file(species_name,weight)
dir_name=species_name;
molw_file = fopen('molecular_weight.dat','a+');
fprintf(molw_file,'Molecular weight of %s is %f \n',string(dir_name),weight);
fclose(molw_file);
end

Output Plots

O2

CO2

N2

Google drive link for output species folder

https://drive.google.com/drive/folders/1ijEvu_CdfijVCT9hO5dfLPKBr8Op_cAz?usp=sharing

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.