NASA thermodynamic data parsing using MATLAB
Challenge = data file parsing
Aim = To write script for data parsing thermodynamic properties like specific heat, enthalpy, entropy and Molecular weight.
Introduction: NASA released file which contains thermodynamic data of 53 species which can be used to calculate thermodynamic properties like entropy, enthalpy specific heat.
Written script:
clear all
close all
clc
%%parsing Nasa thermodynamic data
% processing data file
file = fopen('thermo.dat','r');% command to process data file
fgetl(file) %line skipping command
temperature_line =fgetl(file);
A= strsplit(temperature_line,' '); % command to split value i.e, imaginary line
global_low_temperature = str2double(A{2});% assigning low temperature range
global_mid_temperature = str2double(A{3});% assigning mid temperature range
global_high_temperature = str2double(A{4});% assigning high temperature range
R =8.314; %% universal gas constant
for i = 1:3 %skipping unwanted lines
fgetl(file)
end
%processing species
for t = 1:53 % processing temperature
main_line= fgetl(file);%processing line first of data species
AB = strsplit(main_line,' '); %spliting string
AC=(AB{1});
% step 3 define the values for low temperature and high temperature.
local_low_temp=str2double(AB{end-3});
local_mid_temp =str2double(AB{end-1});
local_high_temp=str2double(AB{end-2});
% processing 14 coefficents value
%%data_line 1
% high temperature coffecient from a1 to a7
data_line1 = fgetl(file);
a = strfind(data_line1,'E');
a1 = str2num(data_line1(1:a(1)+3));
a2 = str2num(data_line1(a(1)+4:a(2)+3));
a3 = str2num(data_line1(a(2)+4:a(3)+3));
a4 = str2num(data_line1(a(3)+4:a(4)+3));
a5 = str2num(data_line1(a(4)+4:a(5)+3));
%data_line 2
data_line2 = fgetl(file);
b = strfind(data_line2,'E');
a6 = str2num(data_line2(1:b(1)+3));
a7 = str2num(data_line2(b(1)+4:b(2)+3));
a8 = str2num(data_line2(b(2)+4:b(3)+3));% low temperature coefficient from a8 to a14
a9 = str2num(data_line2(b(3)+4:b(4)+3));
a10 = str2num(data_line2(b(4)+4:b(5)+3));
% data_line3
data_line3 = fgetl(file);
c = strfind(data_line3,'E');
a11 = str2num(data_line3(1:c(1)+3));
a12 = str2num(data_line3(c(1)+4:c(2)+3));
a13 = str2num(data_line3(c(2)+4:c(3)+3));
a14 = str2num(data_line3(c(3)+4:c(4)+3));
% defining space for low and high temperature range
LTR = linspace(local_low_temp,local_mid_temp,100);
HTR = linspace(local_mid_temp,local_high_temp,100);
for n = 1:100
%%calculation of specific_heat
% polynomial for high temperature
cp_high(n) = R*(a1+(a2*HTR(n))+(a3*(HTR(n)).^2)+(a4*(HTR(n)).^3)+(a5*(HTR(n)).^4));
%polynomial for low temperature
cp_low(n) = R*(a8+(a9*LTR(n))+(a10*(LTR(n)).^2)+(a11*(LTR(n)).^3)+(a12*(LTR(n)).^4));
%%calculation of Enthalpy
% polynomial for high temperature
H_high(n) = R*HTR(n)*(a1+(a2*(HTR(n)/2))+(a3*((HTR(n).^2)/3))+(a4*((HTR(n).^3)/4))+(a5*((HTR(n).^4)/5))+(a6/HTR(n)));
%polynomial for low temperature
H_low(n) = R*LTR(n)*(a8+(a9*(LTR(n)/2))+(a10*((LTR(n).^2)/3))+(a11*((LTR(n).^3)/4))+(a12*((LTR(n).^4)/5))+(a13/LTR(n)));
%%calculation of Entropy
% polynomial for high temperature
S_high(n) = R*(a1*log(HTR(n))+(a2*HTR(n))+(a3*(((HTR(n)).^2)/2))+(a4*(((HTR(n)).^3)/3))+(a5*(((HTR(n)).^4)/4))+a7);
%polynomial for low temperature
S_low(n) = R*(a8*log(LTR(n))+(a9*LTR(n))+(a10*(((LTR(n)).^2)/2))+(a11*(((LTR(n)).^3)/3))+(a12*(((LTR(n)).^4)/4))+a14);
end
% calculation of molecular weight of all the species
%hyperlinked with the function code
MW(t)=molecular_wt(AC)
% storing file for the species
data_parsing= AC ;
mkdir(data_parsing);
current_dir=pwd;
cd(data_parsing)
%Creating plot
%Plotting specific heat and temperature
figure(1)
plot(HTR,cp_high,'linewidth',5,'color','g');
hold on
plot(LTR,cp_low,'linewidth',5,'color','r')
hold off
xlabel('Temp');
ylabel('Specific heat');
title('Temperature vs Specific heat');
%Plotting Entropy and temperature
figure(2)
plot(HTR,S_high,'linewidth',5,'color','g');
hold on
plot(LTR,S_low,'linewidth',5,'color','b');
hold off
xlabel('Temp');
ylabel('Entropy');
title('Temperature vs Entropy');
%Plotting enthalpy and Temperature
figure(3)
plot(HTR,H_high,'linewidth',5,'color','r');
hold on
plot(LTR,H_low,'linewidth',5,'color','y');
hold off
xlabel('Temp');
ylabel('Enthalpy');
title('Temp vs Enthalpy');
hold off
savefig(figure(1),'specific heat plots.fig');
savefig(figure(2),'enthalpy plots.fig');
savefig(figure(3),'entropy plots.fig');
cd(current_dir);
end
Code explanation:
In starting, we have executed thermodynamic data dat file using fopen command which is an inbuilt function in MATLAB. first-line of dat file consist of unwanted line which we skipped using fgetl command.Again using fgetl command to read second-line we assigned global temperature range and split data using strsplit command which is used to split a string. once again file consist of unwanted data which is eliminated using the for loop and fgetl command.Now, we are on 6th no. line of thermodynamic dat file which consist of main data whic is required to generate and complete our project objective. using for loop command data is processed and assigned to variable (t) and 14 coefficient value are extracted using str2num command afterwards coefficient data is fitted into polynomial value and processed for calculation of specific heat, enthalpy and entropy. Separate function is written to calculate molecular weight of all the species and generated results of all the species are saved into directory which consist of plots between temp and specific heat, entropy and enthalpy.
Mol_wt function code :
function mol_weight = molecular_wt(AC)
species = (AC);
elements = ['H','C','O','N','S','A']
atomic_weight = [1,12,15,14,32,39]
mol_weight = 0;
for i = 1:length(species);
for j = 1: length(elements);
if strcmp(species(i),elements(j));
mol_weight = mol_weight+atomic_weight(j);
position = j;
end
end
n = str2num(species(i));
if n>1
mol_weight = mol_weight+atomic_weight(position)*(n-1);
end
end
end
molecular weight screenshot :
Screenshot of species folder :
Plot between Specific heat and temperature :CO2
Plot between entropy and temperature :CO2
Plot between enthalpy and temperature :CO2
Plot between Specific heat and temperature :O2
Plot between entropy and temperature :O2
Plot between enthalpy and temperature :O2
Plot between Specific heat and temperature :N2
Plot between entropy and temperature :N2
Plot between enthalpy and temperature :N2
THANK YOU