Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: To write a code in MATLAB to parse the NASA Thermodynamic data file and calculating the thermodynamic properties of various gas species

Objective:

  1. Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  2. Calculate the molecular weight of each species and display result in command window
  3. Plot the specific heat (cp),Enthalpy and entropy for local temperature range (low temperature :high temperature )on indivuval basis
  4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

Introduction

File Parsing

  • Parsing, syntax analysis is the process of analysing the string of symbols, either in natural languages, computer languages or data structures, conforming to the rules of format grammar
  • Within computational linguistic the term is used to refer to the format analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic to each other, which may also connotation semantic and other information. Some parsing algorithm may generate a parse forest or list of parse trees for a syntactically ambiguous input.
  • 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 and performing a tramdformation on the data

Specific heat (cp)

  • Specific heat is used when we are referring to something particular. Heat capacity is the ratio of the quantity of heat required to alter the temperature by one degree Celsius. But when we consider a certain amount of mass we make use of the word Specific Heat or Specific Heat Capacity. The heat capacity of most systems is not constant and it depends on quantities like the pressure, volume and temperature.
  • Specific heat is the quantity of heat essential to raise the temperature of one gram of any substance by 1 degree Celsius.

Where,

  • Mathematical form
  • Polynomial form
  • R=Universal gas constant
    Δ Q is the heat gained or lost
    Δ T is the temperature difference
  • m is the mass

Enthalpy (H)

  • Enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system.

In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system: H = E + PV.

Entropy (S)

  • Entropy is a scientific concept as well as a measurable physical property that is most commonly associated with a state of disorder
  • 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 Program

clear all
close all
clc
f_1=fopen('THERMO.dat','r');
%reading the 1st line from data 
first_line=fgetl(f_1);  
%univesal gas constant 
R=8.314;
comments1=fgetl(f_1);
comments2=fgetl(f_1);
comments3=fgetl(f_1);
comments4=fgetl(f_1);

for i=1:53

    line1=fgetl(f_1);
    element_name=strtok(line1);
%obtaining local max,min.,medium temperature
%obtaining temperature value from splitting data
temp_range=strsplit(line1,' ');
local_min_temp = str2num(temp_range{end-3});
local_mid_temp = str2num(temp_range{end-1});
local_max_temp = str2num(temp_range{end-2});

%create a vector of 500 evenly spaced temperature points
T=linspace(local_min_temp,local_max_temp,500);

line2=fgetl(f_1);

A= findstr(line2,'E');

line3=fgetl(f_1);
B= findstr(line3,'E');

line4=fgetl(f_1);
C= findstr(line4,'E');

a1=str2num(line2(1:A(1)+3));
a2=str2num(line2(A(1)+4:A(2)+3));
a3=str2num(line2(A(2)+4:A(3)+3));
a4=str2num(line2(A(3)+4:A(4)+3));
a5=str2num(line2(A(4)+4:A(5)+3));

a6=str2num(line3(1:B(1)+3));
a7=str2num(line3(B(1)+4:B(2)+3));
a8=str2num(line3(B(2)+4:B(3)+3));
a9=str2num(line3(B(3)+4:B(4)+3));
a10=str2num(line3(B(4)+4:B(5)+3));


a11=str2num(line4(1:C(1)+3));
a12=str2num(line4(C(1)+4:C(2)+3));
a13=str2num(line4(C(2)+4:C(3)+3));
a14=str2num(line4(C(3)+4:C(4)+3));

%Specific heat 
cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temp);

%ENTHALPY
H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temp);

%ENTROPY 
S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,local_mid_temp);

%calculating molecular weight of the species
Molecular_wt(i)=molecular_weight(element_name);
warning('off')
%saving the plotting data 
mkdir(element_name)

cd(element_name)

figure(1)
plot(T,cp,'LineWidth',2,'Color','b')
title(sprintf('Specific heat vs temprature for %s',element_name))
xlabel('Temperature')
ylabel('specific heat')
grid on
saveas(figure(1),'specific heat.png');

figure(2)
plot(T,H,'LineWidth',2,'Color','r')
title(sprintf('enthalpy vs temprature for %s',element_name))
xlabel('Temperature')
ylabel('Enthalpy')
grid on
saveas(figure(2),'Enthalpy.png');


figure(3)
plot(T,S,'LineWidth',2,'Color','K')
title(sprintf('Entropy vs temprature for %s',element_name))
xlabel('Temperature')
ylabel('Entropy')
grid on
saveas(figure(3),'Entropy.png');

cd ..
end



Step wise explanation of code

  1. First use fopen command to open NASA thermodynamic data. File in matlab
  2. Use fgetl command to read the first line of the data ie,THERMO,
  3. Use the value of universal gas constant (R), and by using the fgetl command access the comment line used in NASA thermodynamic data
  4. 4.using control loop statements(for_loop)to calculate the cp,H,S for all the 53 species and use strtok command to get the selected part of the string (species name)
  1. To obtain the local max minimum,medium temperature from the value from splitting data for that purpose we use strsplit command convert the string into the number using str2num command and we create variable forlocal minimum,medium,max temperature
  2. Create a vector of 500 evenly spaced temperature points by using linspace command
  3. in next line we assign the line no 2 of the nasa thermodynamic data there is coefficient values in order to access this values we use fgetl command and assign variable line 2 findstr command for finding position of ‘E’ in line 2 we use the variable A to store this values.similarly for line3 line4 we use variable b,c to store the value of position of ‘E’.
  4. 8.to extract the a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14 coefficient we use str2num command
  1. To calculate the specific heat, Enthalpy, Entropy, molecular weight of each species we use the function statements which show in the following code
  2. To save the plotting data in new folder we use mkdir and cd command
  3. To create the plot of specific heat enthalpy,entropy,verses temperature we use plot command
  4. we use sprint command in title to create the different species title
  5. end use to the end for loop and start for 2nd elements
  6. we using the control statement(if_else)command to calculating the value of specific heat ,enthalpy entropy
  7. for molecular weight calculation we create the function which takes atomic weight of hydrogen carbon nitrogen oxygen argon as input because all the elements are consist of all these atoms.
  8. now we use the loop and strcmp command is used to compare elements with species if they both match then molecular weight will be calculated
  9. now take the string of species name and position to calculate their weight here n is the number of molecules of the elements present in the species

Function code for Specific heat (cp)

function cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temp)
% comparing with Local temprature

if T<=local_mid_temp
    cp=R.*(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4);
else
    cp=R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
end

Function code for Enthalpy (H)

function H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temp)
%caompring with local temprature
if T<=local_mid_temp
    H=R.*((a8.*T+(a9.T.^2)./2+(a10.*T.^3)./3+(a11.*T.^4)./4+(a12.*T.^5)./5)./5+a13);
else
    H=R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6);

end
end

Function code for Entropy (S)

function S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,local_mid_temp)
%caomparing with Entropy
if T<=local_mid_temp
    S=R*(a8*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14);

else
    S=R*(a1*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7);
end
end

Function code for Molecular weight (MW)

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 16 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 and Output

Folder in which all element plot are save

plot of O2,N2,CO2

FOR O2

1.

2.

3.

For N2

1.

2.

3.

FOR CO2

1.

2.

3.

Molecular weight of each Elements

ERRORS

Facing lots of error when doing project in this error i was forget to end function and if_else loop to over come this error checking all code correctly and carrect the mistake and facing some problem when doing project then taking the one to one session

References:

https://en.wikipedia.org/wiki/Parsing

https://in.mathworks.com/help/matlab/ref/fgetl.html

https://in.mathworks.com/help/matlab/ref/strsplit.html


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.