Challenge

Parsing NASA thermodynamic data

AIM:- To write a code in matlab to parase the NASA thermodyanamics data file and then calculate thermodyanamics properties of various gas

Objective:

Write a function to extract 14 coefficients and calculate Enthalpy, Entropy, Specific heat & molecular weight for all the species in the data file.

Plot Cp, Enthalpy, and Entropy for temperature range.

Save the plots as images with appropriate names and dump them in separate folders for each species with suitable names. All these should be generated automatically.

THEORY:

File Parsing

Parsing a file means reading the data stream of some sort and building an in-memory model of semantic content of the data. This aims at facilitating & performing a transformation on the data. In general, 'Parsing' refers to MATLAB reading an 'm-file' and then translating the data of 'm-file' into internal code in MATLAB, which it stores in memory. When you execute the code, the file which is parsed in memory will run.

Specific Heat (Cp) –

It is the ratio of the quantity of heat required to raise the temperature of a body by 1 degree to that required to raise the temperature of an equal mass of water by one degree.

Cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4

Where Cp = Specific heat,

R = Universal gas constant.

Entropy –

It is 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.

S/R = al lnt + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a 7

Where, S = Entropy

R = Universal gas constant.

Enthalpy - H = U+PV Simply, Enthalpy is the sum of internal energy and product of pressure and volume. When a process occurs at constant pressure.

H/RT = al + a2 T/2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T

Where H = Enthalpy

R = Universal gas constant.

Main Code Explanation

  1. I have used 'fopen' command to open Nasa's experimental data.
  2. If you open the experimental data and analyze what's happening, we can easily code it according to our requirements.
  3. jst line consists of text (THERMO). I gave 'fgetl' command to read the 1st o Second-line consists of 3 temperature values. Like 1st line use 'fgetl' to read the 2nd line and store it in variable global_temp.
  4. After reading the 2nd line, I have used 'strsplit' command using 'blank space' as a delimiter. 'strsplit' splits the data using the given delimiter. Therefore, temp_range consists of all 3 temperature values in a vector.
  5. Now, obtain the global minimum, medium, and maximum temperatures from the split data, use 'str2num' as this converts character array to numeric array.
  6. Then create a vector of 400 evenly spaced temperature points using 'linspace' command and provide universal gas constant value.
  7. 30,4th & 5th lines in the data are comments. Use 'fgetl' again for reading those 3 lines. o From line '6' to line '218', there are a set of 4 lines. 1st line consists of the name of species 2nd, 3rd, 4th lines consist of experimental data.

8.Now, I will explain the code for these 4 lines of data.

  1. Line 1-1 have read line '6' using 'fgetl' and stored it in line 1 variable.
  2. I have used 'strtok' command to get the selected parts of the string (ie, species name 'o')
  3. Line 2 - I have read line 7' using 'fgetl' and stored it in line 2 variable.
  4. Use 'strfind' command to find the places of 'E' in line 7, 'strfind' is used to find strings within other strings. Now store them in an array named 'A'
  5. Similarly, follow these steps for the next 2 lines. Then, you will get A, B, C arrays respectively.
  6. Now, extract all the coefficients (a, A2, A3, A4, A5, A6, A7, bi, b2, 63, 64, 65, 66, b7) using 'str2num' command.

Main program:-

clear all
close all
clc
f1=fopen('THERMO.dat','r');
first_line=fgetl(f1); %Reading the Ist line from data
%Obataining global max,min and medium temperatures
global_temp=fgetl(f1) %Reading 2nd line from data
temp_range=strsplit(global_temp,' ') %spliting 2nd line using blank space as delimeteres
%Obtaining temperatures values from splitted data
global_min_temp=str2double(temp_range{2})
global_med_temp=str2double(temp_range{3})
global_max_temp=str2double(temp_range{4})
%skip next 3 lines which contains comments
for i=1:3
fgetl(f1);
end
%create a vector of 400 evenly spaced temperatures points
T=linspace(global_min_temp,global_max_temp,400);
R=8.314; %Universal gass constant
n=53; %Total number of species
for i=1:n
line1=fgetl(f1);
element_name=strtok(line1);
line2=fgetl(f1);
e1=strfind(line2,'E');
a1=str2double(line2(1:e1(1)+3));
a2=str2double(line2(e1(1)+4:e1(2)+3));
a3=str2double(line2(e1(2)+4:e1(3)+3));
a4=str2double(line2(e1(3)+4:e1(4)+3));
a5=str2double(line2(e1(4)+4:e1(5)+3));
line3=fgetl(f1);
e2=strfind(line3,'E');
a6=str2double(line3(1:e2(1)+3));
a7=str2double(line3(e2(1)+4:e2(2)+3));
a8=str2double(line3(e2(2)+4:e2(3)+3));
a9=str2double(line3(e2(3)+4:e2(4)+3));
a10=str2double(line3(e2(4)+4:e2(5)+3));
line4=fgetl(f1);
e3=strfind(line4,'E');
a11=str2double(line4(1:e3(1)+3));
a12=str2double(line4(e3(1)+4:e3(2)+3));
a13=str2double(line4(e3(2)+4:e3(3)+3));
a14=str2double(line4(e3(3)+4:e3(4)+3));
%specific heat
cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_med_temp);
%Enthalpy
h=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_med_temp);
%Entropy
s=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_med_temp);
%Calculating molecular weight of the species
molecular_wt(i)=molecular_weight(element_name);
%Saving the data
mkdir(['D:NASA projectspecies_name',element_name]);
%plotting the data
figure(1)
plot(T,cp,'linewidth',2,'color','b')
title(sprintf('specific heat vs Temperature for %s',element_name))
xlabel('bf TEMPERATURE')
ylabel('bf SPECIFIC HEAT')
grid on
figure(2)
plot(T,h,'linewidth',2,'color','r')
title(sprintf('Enthalpy vs Temperature for %s',element_name))
xlabel('bf TEMPERATURE')
ylabel('bf ENTHALPY')
grid on
figure(3)
plot(T,s,'linewidth',2,'color','g')
title(sprintf('Entropy vs Temperature for %s',element_name))
xlabel('bf TEMPERATURE')
ylabel('bf ENTROPY')
grid on
cd(['D:NASA projectspecies_name',element_name]);
saveas(1,'Specific heat.png');
saveas(2,'Enthalpy.png');
saveas(3,'Entropy.png');
cd ..
end
fclose(f1);

%Program to specific heat

% Specific heat function program
function cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_med_temp)
%Comparing with global temp
if T>global_med_temp
cp=R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
else
cp=R.*(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4);
end

%Program to Calculate enthalpy

%Enthalpy function program
function h=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_med_temp)
if T>global_med_temp
h=R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6);
else
h=R.*(a8.*T+(a9.*T.^2)./2+(a10.*T.^3)./3+(a11.*T.^4)./4+(a12.*T.^5)./5+a13);
end

%Program to Calculate entropy

%Entropy function program

function s=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_med_temp)
if T<=global_med_temp
s=R.*(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7);
else
s=R.*(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14);
end

%Program to Calculate Molecular weight

% Molecular weight function program
function MW=molecular_weight(element_name)
atoms=['H','C','N','O','A'];
fprintf('molecular weight of ');
fprintf(element_name);
fprintf(' : ');
atomic_weight=[1 12 14 15 39];
MW=0;
for i=1:length(element_name)
for j=1:length(atoms)
if strcmp(element_name(i),atoms(j))
MW=MW+atomic_weight(j);
position=j;
end
end
n=str2num(element_name(i));
if n>1;
MW=MW+atomic_weight(position)*(n-1);
end
end
fprintf('%d',MW);
disp(' ');
end
RESULTS:-
Plots of O2,N2 and CO2
O2-
N2-
CO2-
OUTPUTS-
screenshot of saved data-
Screenshots of molecular weight-
Screenshots of command window and workspace-
Errors-errors faced using cd command then after resolve it by changing the folder path.
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.