Challenge

Project 1 - Parsing NASA thermodynamic data

INTRODUCTION-

Parsing is the process of analyzing a string of symbols, either in natural language,computer languages or data structures, conforming to the rules of formal grammar. Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information.

AIM -

1. Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data.

2. Calculate the molecular weight of each species and display it in the command window.

3. Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.

PROCEDURE-

1)The program uses the following commands 'fopen' command opens the thermodynamic data file [therm.dat] and stores it to a file Id called f1 . 'fgetl' of f1 command takes the value of the first line of the therm.dat file.

2) The 2nd line in the file represents global temperatures, thus the values are stored for temperature calculations.

3)Since the temperatures are inside the string we use ' strsplit ' command that splits the string in the specified delimiter, here ' ' (space) is the delimiter, after splitting the values are assigned to variables ( global low, mid and high temperatures), using ' str2double ' command string value is converted to the numeric value.

4) A temperature range with 500 values between global high and low temperature is made by using 'linspace' command, the number of values is specified by ' linspace ' command.

5)Preallocations are made to store the properties or data of all the elements present in the file.

6)Then skipping the 3 comment lines present in the therm.dat file, which has command 'fgetl of f1'.

7)Command 'cd(pwd)' opens a present working directory. 'mkdir' command makes a new folder with the name specified inside brackets.

8)A folder named 'Elements' is created in the working directory which is used to store all the related output files of the program.

9)Command 'cd()' makes the new folder as the directory. At last, a 'for' loop must run 'j' times, where j is a number of elements.

10) After the loop gets executed, cd .. is used to get back to the original directory.

CODE-

clear all 
close all
clc


% universal gas constant
R = 8.314 ; 

%Reading header
f1 = fopen('THERMO.dat','r');
top= fgetl(f1);

%Reading Global Temperature Line

Global_temp = fgetl(f1);
C = strsplit(Global_temp,' ');
global_low_temp =str2double(C{2}); %Global Low Temperature 
global_mid_temp =str2double(C{3}); %Global Mid Temperature 
global_high_temp =str2double(C{4}); %Global High Temperature 

%%skipping 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);

%using for loop to consider 53 elements

for i = 1:53
line= fgetl(f1); %Reading the line
A=strsplit(line,' '); %Seperating Variable
species=A(1); %Naming First Variable as 'Species'
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); %Local Low Temperature
ht = str2double(local_high_temp); %Local high Temperature 
mt = str2double(local_mid_temp); %Local mid Temperature


%line 1-Reading the coefficient data

cfline1= fgetl(f1);
X = strfind(cfline1,'E');
a1 = cfline1(1:X(1)+3); 
a1 = str2double(a1); %storing 1st coefficient 
a2 = cfline1(X(1)+4:X(2)+3);
a2 = str2double(a2); %storing 2nd coefficient 
a3 = cfline1(X(2)+4:X(3)+3);
a3 = str2double(a3); %storing 3rd coefficient 
a4 = cfline1(X(3)+4:X(4)+3);
a4 = str2double(a4); %storing 4th coefficient 
a5 = cfline1(X(4)+4:X(5)+3);
a5 = str2double(a5); %storing 5th coefficient
 
%line 2-Reading coefficient data of 2nd line

cfline2 = fgetl(f1);
G = strfind(cfline2,'E');
a6 = cfline2(1:G(1)+3);
a6 = str2double(a6); %storing 6th coefficient 
a7 = cfline2(G(1)+4:G(2)+3);
a7 = str2double(a7); %storing 7th coefficient 
a8 = cfline2(G(2)+4:G(3)+3);
a8 = str2double(a8); %storing 8th coefficient 
a9 = cfline2(G(3)+4:G(4)+3);
a9 = str2double(a9); %storing 9th coefficient 
a10 = cfline2(G(4)+4:G(5)+3);
a10 = str2double(a10); %storing 10th coefficient 

%line 3-Reading coefficient data of 3rd line

cfline3 = fgetl(f1);
I = strfind(cfline3,'E');
a11 = cfline3(1:I(1)+3);
a11 = str2double(a11); %storing 11th coefficient 
a12 = cfline3(I(1)+4:I(2)+3);
a12 = str2double(a12); %storing 12th coefficient 
a13 = cfline3(I(2)+4:I(3)+3);
a13 = str2double(a13); %storing 13th coefficient 
a14 = cfline3(I(3)+4:I(4)+3);
a14=str2double(a14); %storing 14th coefficient 



%Ranging the temperature low temperature to high temperature

T=linspace(lt,ht,500);

%Specific Heat

Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Entropy

S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Enthaphy

H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Saving as a folder
spec=char(species); 
fd = sprintf('%s',spec);
cur_dir=pwd;
mkdir(spec);
cd(spec);
  
  
%Plot 1, Specific heat (Cp) vs Temperature (T)

figure(1);
plot(T,Cp,'linewidth',2,'color','r');
xlabel('Temperature in K');
ylabel('Specific heat in kJ');
title('Specific heat vs Temperature');
  
%Plot 2, Enthalpy (H) vs Temperature (T)
  
figure(2);
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature in K');
ylabel('Enthalpy in kJ/(mol)');
title('Enthalpy vs Temperature');
  
%Plot 3, Entropy (S) vs Temperature (T)

figure(3);
plot(T,S,'linewidth',2,'color','g');
xlabel('Temperature in K');
ylabel('Entropy in kJ/(mol K)');
title('Entropy vs Temperature');

% saving the plots that got generated

saveas(figure(1), 'cp.png');
saveas(figure(2), 'h.png');
saveas(figure(3), 's.png');
cd(cur_dir);
%Molecular madd
M = molecular_mass(species);

f2 = fopen('molecular mass','w');
fprintf('%s n %s n %f n','species','Molecular mass',M)
fclose(f2);
end
 

fclose(f1);

FUNCTIONS-

1) ENTROPY-

function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    S=(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7)*R;
else
    S=(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14)*R;
end

2) ENTHALPY-

function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
H=(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
H=(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end

3)SP HEAT-

function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    Cp=(a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4))*R;
else
    Cp=(a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
end

4) MOLECULAR MASS-

function M= molecular_mass(species)
        atoms={'H','C','N','O','AR'};
        AtomicWeight=[1 12 14 16 40];
               M=0;
        for i=1:length(species)
            for j=1:length(AtomicWeight)
                if (strcmp(species(i),atoms(j))==1)
                    M=M+AtomicWeight(j);
                    g=j;
                end
            end
         % Adding the weights of the repeating atoms
                n=str2double(species(i));
                if n>1
                    p=(n-1);
                    M=M+AtomicWeight(p)*(n-1);
                end
                         
      end

PLOTS-

O2

N2 -

Co2

FOLDER-

ERRORS FACED-

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.