Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:- Write a program to parse NASA's thermodynamic data file and calculates the enthalpy,entropy and specific heats for all the species in the data file.

Introduction:

Parsing:

Parsing is the process of splitting file into pieces of data that can be easily stored or manipulated . So we are splitting string into parts then recognizing the part to convert it into something simpler than string.

Specific Heat(c_p):

Specific heat is defined as amount of heat required to raise temperature by one degree celsius.

Enthalpy(H):

Enthalpy is the measure of total energy content of body. Enthaly H can be written as.

H = U+pV

Where,

H = Enthalpy of the system

U = Internal energy of the system

p = Pressure of the system

V = Volume of the system

Enthalpy is not measured directly. however, the change in enthalpy `(triangleH)`is measured. Which is the heat added or lost by the system. At constant temperature, for the process heat flow is equal to the change in enthalpy.

Entropy(s):

Entropy is defined as it is part of energy that can be converted into useful mechanical work. Entropy generation is measure of magnitude of irreversibility present during that process. Entropy generation can be used as a quantative measure os irreversibility associated with process. Change in entropy can be calculated by

ds=dQ/T

Where

ds = change in entropy

dQ = Heat transfer

T = Temperature of system

Formulae of specific Hear& Entropy used for calculation:

The first 7 co-efficient are High temperature co-efficient and next 7 co-efficient are low temperature co-efficients.

Function of cp h and s:

function  [cp, h ,s] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,global_med_temp)
R = 8.314;
%for getting the array of cp,h and s
for k = 1:length(T)
    %to compare the temperature to assign the coefficients
    %a1-a7 is high temperature coefficients
    %a8-a14 is the low temperature coefficients 
    if T(k)>global_med_temp
        cp = (a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4)*R;
        h = (a1+(a2.*T)/2+(a3.*T.^2)/3+(a4.*T.^3)/4+(a5.*T.^4)/5+(a6./T))*R.*T;
        s =(a1.*log(T)+a2.*T+(a3.*T.^2)/2+(a4.*T.^3)/3+(a5.*T.^4)/4+a7);
    else
        cp = (a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
        h = (a8+(a9.*T)/2+(a10.*T.^2)/3+(a11.*T.^3)/4+(a12.*T.^4)/5+(a13./T))*R.*T;
        s = (a8.*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14);
    end
end
end

Molecular weight calculate:

function [MW]=molecular_weight(species_name)

elements=['H','C','O','N','AR'];
atomic_weight=[1,12,16,14,40];


MW=0;

for i=1:length(species_name)
   for j=1:length(elements)
     tf= strcmp(species_name(i),elements(j));
     if tf==1
          MW=MW+atomic_weight(j);
          pos=j;
     end
    end
end

  n=str2double(species_name(i));
     if n>1

      MW=MW+atomic_weight(pos)*(n-1);
     end

Main program

clear all
close all
clc
%for getting the data file
f1 = fopen('THERMO.dat','r');
file_name=fgetl(f1);

%reading the first line of the file
header_global_temp = fgetl(f1);

A = strsplit(header_global_temp,' ');

%obtaining the global max,medium and minimum temp: 
global_min_temp=strsplit(A{1});
global_mid_temp=strsplit(A{2});
global_max_temp=strsplit(A{3});

%ignoring the comments in file
for i=1:3
    fgetl(f1);
end

for i=1:53
%reading all line data
    header=fgetl(f1);
    

%finding the string

global_temp=strsplit(header,' ');
species_name=(global_temp{1});
global_min_temp=str2double(global_temp{end-3});
global_max_temp=str2double(global_temp{end-2});
global_med_temp=str2double(global_temp{end-1});
T = linspace(global_min_temp,global_max_temp,500);

%finding the coefficients a1 to a14
tline1=fgetl(f1);
tline2=fgetl(f1);
tline3=fgetl(f1);

a=strfind(tline1,'E');
a1=tline1(1:a(1)+3);
a1=str2double(a1);

a2=tline1(a(1)+4:a(2)+3);
a2=str2double(a2);

a3=tline1(a(2)+4:a(3)+3);
a3=str2double(a3);

a4=tline1(a(3)+4:a(4)+3);
a4=str2double(a4);

a5=tline1(a(4)+4:a(5)+3);
a5=str2double(a5);

%finding strings and convert to numbers in line3
a=strfind(tline2,'E');

a6=tline2(1:a(1)+3);
a6=str2double(a6);

a7=tline2(a(1)+4:a(2)+3);
a7=str2double(a7);

a8=tline2(a(2)+4:a(3)+3);
a8=str2double(a8);

a9=tline2(a(3)+4:a(4)+3);
a9=str2double(a9);

a10=tline2(a(4)+4:a(5)+3);
a10=str2double(a10);

%finding strings and convert to numbers line4
a=strfind(tline3,'E');
a11=tline3(1:a(1)+3);
a11=str2double(a11);

a12=tline3(a(1)+4:a(2)+3);
a12=str2double(a12);

a13=tline3(a(2)+4:a(3)+3);
a13=str2double(a13);

a14=tline3(a(3)+4:a(4)+3);
a14=str2double(a14);


 %calling the thermodynamic equation function
 [cp, h ,s] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,global_med_temp);

%plotting the curves
%cp v/s T
figure(1)
plot(cp,T,'linewidth',3,'color','r');
grid on
title('species_name')
xlabel('specific_heat [j/kl]')
ylabel('temperature [k]')

% h v/s T
figure(2)
plot(h,T,'linewidth',3,'color','b')
title(species_name)
xlabel('enthalpy [j]')
ylabel('temperature [k]')

%entropy v/s temperature
figure(3)
plot(s,T,'linewidth',3,'Color','g')
title(species_name)
xlabel('entropy  [kj-1/mol]')
ylabel('temperature [k]')

%changing the directory and saving the file
mkdir(species_name)
directory=pwd;
cd(species_name)
saveas(figure(1),'cp_vs_temp.jpeg');
saveas(figure(2),'enthalpy_vs_temp.jpeg');
saveas(figure(3),'entropy_vs_temp.jpeg');
cd ..


[MW]=molecular_weight(species_name);
disp(['molecular_weight of:-' species_name])
disp(MW)

end

%fclose(f1);

Output plots for species:

1. Plots for o2:

fig. Temperature v/s specific heat

fig. Temperature v/s enthalpy

fig. Temperature v/s entropy

2. Plots for co2

fig. Temperature v/s specific heat

fig. Temperature v/s enthalpy

fig. Temperature v/s entropy

Explanation of code:

1. fopen is use to open file 'THERMO.dat' 'r' is readable format and save it as f1.

2. fgetl is used to read the each and every line of file.

3. line 2 contains global temp and present in the form of string hence to extract the values of global temp split the string using 'strsplit' and save it as global low, mid or high temp.

4. for i=1:3

fgetl(f1)

end

It will read next 3 lines which are comment but since we use semicolon it will supress the output within code and won't show those 3 comments.

4. There are total 53 molecule present in THERMO.dat file hence we use i=1:53.

5. for every chemical element there are 4 lines, 1st line is name of species and to extract name of species we used.

A=strsplit(header_global_temp,' ')

global_min_temp=strsplit(A{1})

Curly brackets({}) are used since A is call array.

6. Also header_global_temp contains values of local temperature different for different species to extract it find the value of 'strfind' and 'str2double' converts string into double precise value.

7. Now to plot the smooth curve divide local temperature values starting from local low temperature to local high temperature using 'linspace' command.

Error:

1. To calculate moleculare weight.

Solution: All plots and molecular weight will be in same folder

2. In function program also error to calculate the molecular_weight

Solution: In file_reading1 to change the 'species_name'

Workspace of program:

Molecular weight:

fig. molecular weight of different species

Plots of species saved in folder

References:

1.https://skill-lync.com/student-projects/parsing-nasa-thermodynamic-data-190

2.https://medium.com/the-mighty-programmer/what-is-parsing-4012f997d265

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.