Project 1 - Parsing NASA thermodynamic data
Aim: To write a matlab code to study and parse the NASA Thermodynamic data file and plot the results.
Objective:
- Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file
- Calculate the molecular weight of each species and display it in the 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.
INTRODUCTION:
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as specific heat, enthalpy and entropy. They have documented coefficients that are required to evaluate these polynomials for 1000+ species
The thermodynamic properties in terms of co-efficients are given below:
Here R is the universal gas constant, T is the temeprature
The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
Terminologies:
File parsing:file parsing here is extracting the useful information from a file and utilizing the information for our work.
Specific heat: The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.
Enthalpy: a thermodynamic quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.
Entropy: 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.
Main code:
clear all
close all
clc
%opening and reading the file
file=fopen('THERMO.dat','r')
%HEADING
line1= fgetl(file);
%global temperature values
line2=fgetl(file);
%comments
com_1=fgetl(file);
com_2=fgetl(file);
com_3=fgetl(file);
S=strsplit(line2,' ');
global_low_temperature= str2double(S{2});
global_mid_temperature= str2double(S{3});
global_low_temperature= str2double(S{4});
%species information
for i=1:53
line=fgetl(file); %reading the line
A=strsplit(line,' ');
species=A(1);
Q=strfind(line,'G');
local_low_temp=line(Q+3:55);
local_high_temp=line(Q+13:65);
local_mid_temp=line(Q+23:75);
lt=str2double(local_low_temp);
ht=str2double(local_high_temp);
mt=str2double(local_mid_temp);
%reading the coeff data lines
line_1=fgetl(file);
F1=strfind(line_1,'E');
C1=str2double(line_1(1:(F1(1)+3)));
C2=str2double(line_1((F1(1)+4):(F1(2)+3)));
C3=str2double(line_1((F1(2)+4):(F1(3)+3)));
C4=str2double(line_1((F1(3)+4):(F1(4)+3)));
C5=str2double(line_1((F1(4)+4):(F1(5)+3)));
line_2=fgetl(file);
F2=strfind(line_2,'E');
C6=str2double(line_2(1:(F2(1)+3)));
C7=str2double(line_2((F2(1)+4):(F2(2)+3)));
C8=str2double(line_2((F2(2)+4):(F2(3)+3)));
C9=str2double(line_2((F2(3)+4):(F2(4)+3)));
C10=str2double(line_2((F2(4)+4):(F2(5)+3)));
line_3=fgetl(file);
F3=strfind(line_3,'E');
C11=str2double(line_3(1:(F3(1)+3)));
C12=str2double(line_3((F3(1)+4):(F3(2)+3)));
C13=str2double(line_3((F3(2)+4):(F3(3)+3)));
C14=str2double(line_3((F3(3)+4):(F3(4)+3)));
%TEMPERATURE RANGES FOR CALCULATION
T=linspace(lt,ht,500);
%universal gas constant
R=8.314;
%claculation of properties based on lower and upper temperature
if T>=mt
cp=(C1+C2.*T+C3.*T.^2+C4.*T.^3+C5.*T.^4).*R;
H=(C1+(C2.*T)./2+(C3.*T.^2)./3+(C4.*T.^3)./4+(C5.*T.^4)./5+C6./T).*T.*R;
S=(C1*log(T)+C2.*T+(C3.*T.^2)./2+(C4.*T.^3)./3+(C5.*T.^4)./4+C7).*R;
else
cp=(C8+C9.*T+C10.*T.^2+C11.*T.^3+C12.*T.^4).*R;
H=(C8+(C9.*T)./2+(C10.*T.^2)./3+(C11.*T.^3)./4+(C12.*T.^4)./5+C13./T).*T.*R;
S=(C8*log(T)+C9.*T+(C10.*T.^2)./2+(C11.*T.^3)./3+(C12.*T.^4)./4+C14).*R;
end
%saving as a folder
spec=char(species);
fd=sprintf('%s',spec);
cur_dir=pwd;
mkdir(spec);
cd(spec);
%plotting
%temperature vs speific heat
figure(1);
plot(T,cp,'linewidth',4,'color','b')
title('speific heat vs temperature')
xlabel('TEMPERATURE in K')
ylabel('ENTROPY in KJ')
%temperature vs enthalpy
figure(2);
plot(T,H,'linewidth',4,'color','r')
title('enthalpy vs temperature')
xlabel('TEMPERATURE in K')
ylabel('ENTHALPY in KJ/(mol)')
%temperature vs entropy
figure(3);
plot(T,S,'linewidth',4,'color','g')
title('entropy vs temperature')
xlabel('TEMPERATURE in K')
ylabel('ENTROPY in KJ/(mol K)')
%saving of plots
saveas(figure(1),'specific heat.png');
saveas(figure(2),'enthalpy.png');
saveas(figure(3),'entropy.png');
cd(cur_dir);
MM=molecular_mass(species)
end
fclose(file);
function code:
function [MM]=molecular_mass(species_name)
species=upper(species_name)
MM=0;
atoms=['H' 'C' 'N' 'O' 'AR'];
atomic_weight=[1 12 14 16 40];
for i=1:length(species)
for j=1:length(atoms)
if strcmp(species(i),atoms(j))
MM=MM+atomic_weight(j);
u=j;
end
end
n=str2double(species(i));
if n>1
MM=MM+atomic_weight(u)*(n-1);
end
end
end
code explanation:
- Firstly, 'THERMO.dat' file is read using fopen() and fgetl() command is used to read each line of the file. 1-5 lines of the files are the heading and comments itself
- Next line consist of species name and the local temperature. Species name code is written using strsplit() command local temperatures using strfind() by detecting ‘Q’ because the temperatures are after the letter ‘Q’ at a spacing of 10 digits between each temperature.
- From next line, 14 coefficient values are collected by using strfind() and str2double() command. Using the letter ‘G’ the coefficients are differentiated as coeff last 3 digits after letter ‘G’ and the 14 coeff values are stored.
- For calculating specific heat, enthalpy and entropy, temperature range is set from low to high temperature with 500 values and is differntiated using the mean temperature.
- Graphs are plotted for specific heat, enthalpy and entropy for each species in a separate folder
- New folder is formed using mkdir and cd(change current folder or directory) and data is stored
- Molecular mass is calculated using a different function and is called in every iteration
- In function, atoms and species are compared and atomic weight is calculated and is return.
- Lastly, file is closed using fclose() command.
Results and plot:
- Plots for N2:
2.plots for O2:
3. plots for CO2:
screenshot of molecular mass:
screenshot of species folder:
errors:
error1: unrecognised varaible 't'
solution: typing error. it should be 'T'.
error2: incorrect open command
solution: fopen() should be used
references:
https://skill-lync.com/projects/Parsing-NASA-thermodynamic-data-77857