Challenge

Project 1 - Parsing NASA thermodynamic data

REPORT

AIM-Write a program to parse NASA thermodynamic data file and then calculate thermodynamic properties for various species

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.

> Attach screenshot of the window where all the folders are saved and Plots for o2, N2 and Co2 species

FILE PARSING

Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating perfoming some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated

NASA Thermodynamic File

dat file is a data file containing 14 coefficients for 53 different elements given by NASA. These coefficients are utilized to calculate the specific heat, enthalpy & entropy for a particular species. Each element has its own range of local temperature and coefficients

Where,

R-Universal gas constant

T-Temperature in K

Cp-Specific heat in kj

S-Entropy in kj/mol-k

H-Enthalpy in kj/mol

a(i)=coefficients , where i=1 to 14

SPECIFIC HEAT -Cp

Specific heat is the quantity of heat required to raise the temperature of one fram of a substance by one degre celsius.The relationship between heat and temperature change expressed in the form shown below

Cp=R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4))

ENTROPY-S

Entropy is the measure of system's thermal energy per unit temperature that is unavailable for usefull 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.Equation for entropy is shown below

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

ENTHALPY-H

Enthalpyis a property of thermodynamic system and it is the sum of the internal energy and the product of the pressure and volume of a thermodynamic system.Equation for enthalphy is shown below

H=R*T.*(a1+((a2*T)/2)+((a3*(T).^2)/3)+((a4*(T).^3)/4)+((a5*(T).^4)/5)+((a6./T))

Main program

close all
clear all
clc
% Reading the data file
% open file and reading THERMO.dat file
f1=fopen('THERMO.dat','r');
get=fgetl(f1);

% Now reading global temperature from this file
temp=fgetl(f1);

% Spliting

s=strsplit(temp);

% Converting string cells into numbers
templow=str2num(s{2});
tempmid=str2num(s{3});
temphigh=str2num(s{4});


% Universal gas constant R
R=8.134;

% Skipping command lines by using for loop
for i=1:3
    get=fgetl(f1);
end

% Reading local temprature and coefficients for each species
for j=1:53
    tline=fgetl(f1);

%Spliting into cell array
A=strsplit(tline,' ');
% Reading the name of species
species=A{1};

% Reading local temperature for each species 

low_temp=str2num(A{end-3}); %Converting string into number
med_temp=str2num(A{end-2});
high_temp=str2num(A{end-1});
T=linspace(low_temp, high_temp, 1000);


% Getting the next line
tline1=fgetl(f1);

% To find the string E
a=strfind(tline1,'E');

% Finding  7 higher coefficients and  7 lower coefficients
a1=tline1(1:a(1)+3);
a1=str2num(a1);
a2=tline1(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=tline1(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=tline1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=tline1(a(4)+4:a(5)+3);
a5=str2num(a5);

% getting the next line
tline2=fgetl(f1);
% to find string E
b=strfind(tline2,'E');
a6=tline2(1:b(1)+3);
a6=str2num(a6);
a7=tline2(b(1)+4:b(2)+3);
a7=str2num(a7);
a8=tline2(b(2)+4:b(3)+3);
a8=str2num(a8);
a9=tline2(b(3)+4:b(4)+3);
a9=str2num(a9);
a10=tline2(b(4)+4:b(5)+3);
a10=str2num(a10);

% getting the next line
tline3=fgetl(f1);
d=strfind(tline3,'E');
a11=tline3(1:d(1)+3);
a11=str2num(a11);
a12=tline3(d(1)+4:d(2)+3);
a12=str2num(a12);
a13=tline3(d(2)+4:d(3)+3);
a13=str2num(a13);
a14=tline3(d(3)+4:d(4)+3);
a14=str2num(a14);

% calculating specific heat
Cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);

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

% calculating enthalpy

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

% calculating molecular weight

W=molecular_weight(species);

mkdir(species)
cd(species)
%ploting
%plot 1 Specific heat vs Temperature
figure(1)
plot(T,Cp,'LineWidth',2,'Color','r');
xlabel('TEMPERATURE(K)');
ylabel('SPECIFIC HEAT(KJ');
title(sprintf('Specific heat vs Temperature range of %s',species))
saveas(figure(1),'cp.jpg')

% plot-2 Entropy vs Temperature
figure(2)
plot(T,S,'linewidth',2,'color','b')
xlabel('TEMPERATURE(K)')
ylabel('ENTROPY(KJ/MOL-K)')
title(sprintf('Entropy vs Temperature range of %s',species))
saveas(figure(2),'s.jpg')

%plot-3 Enthalpy vs Temperature
figure(3)
plot(T,H,'LineWidth',2,'Color','r');
xlabel('TEMPERATURE(K)');
ylabel('ENTHALPY(KJ/MOL)')
title(sprintf('Enthalpy vs Temperature range of %s',species))
saveas(figure(3),'h.jpg')
cd ..

end





STEPS

>First we open open THERMO.dat file and reading this file using fopen command

>Then we read the first line using fgetl command and we read the temperatures and we split temperature using strsplit to convert into cell array

>Then we will convert this string to numbers using str2num command to find low , mid and high temperatures

>Then we will creat an array for temperature between low and hihg temperatures using linspace command

> We skip the command lines by using fgetl command in loop

>Now using fgetl command in loop to read 53 species or elements in the file

>Then we split into cell array to find species line and then find species name and the find the number of temperatures in species lines

>Then we read the new line using fegtl

>We have to extract the values of coefficients ,for this we have to find position of E

>To find string E we use strfind command

>Taking position of E as reference we find the values of coefficients from the lines for each species

>First seven coefficients are for higher temperature and then remaining seven coefficients are for lower temperature

>We created functions for specific heat, entropy, enthalpy and molecular weight and using this functions we can find the values

>Now by using cd we are changing the folder and by using mkdir to creat new folder

>Then we are ploting the plots for all species and saving the plots by using saveas command

Function to find Specific heat
function Cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)

% calculating specific heat for higher anf lower temperature
if (T<=tempmid)
    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

end
Steps
>Here we are calling a function taking all 14 coefficients ,gas constant-R ,temperature-T,and tempmid as inputs
>We have the formula for specific heat and we put this in the loop
>Output of this function gives the value for specific heat for all species
>Now we creat if condition in loop for temperature less than or equal to mid temperature to specify lower temperature coefficients in formula
>Then we are creat else command to specify higher temperature coefficients in formula
Function to find Entropy
function S= entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)

if (T<=tempmid)
    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
Steps
>Here we are calling a function taking all 14 coefficients ,gas constant-R ,temperature-T,and tempmid as inputs
>We have the formula for entropy and we put this in the loop
>Output of this function gives the value for entropy for all species
>Now we creat if condition in loop for temperature less than or equal to mid temperature to specify lower temperature coefficients in formula
>Then we are creat else command to specify higher temperature coefficients in formula
Function to find Enthalpy
function H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,a14,T,R,tempmid)

if T<=(tempmid)
    H=R*T.*(a8+((a9*T)/2)+((a10*(T).^2)/3)+((a11*(T).^3)/4)+((a12*(T).^4)/5)+((a13./T)));
else
     H=R*T.*(a1+((a2*T)/2)+((a3*(T).^2)/3)+((a4*(T).^3)/4)+((a5*(T).^4)/5)+((a6./T)));
end
end​
Steps
>Here we are calling a function taking all 14 coefficients ,gas constant-R ,temperature-T,and tempmid as inputs
>We have the formula for enthalpy and we put this in the loop
>Output of this function gives the value for enthalphy for all species
>Now we creat if condition in loop for temperature less than or equal to mid temperature to specify lower temperature coefficients in formula
>Then we are creat else command to specify higher temperature coefficients in formula
Function to find molecular weight
function W=molecular_weight(species)
% Defining atomic name of the species
atomic_name=['H','C','N','O','Ar'];

% Defining atomic weight
atomic_weight=[1.008 12.01 15.99 14.007 39.48];

% Molecular weight starting fr0om zero
W=0;
for i=1:length(species) % loop runs up to the length of species
    for j=1:length(atomic_name) % loop runs up to the length of atomic name
if strcmp(species(i),atomic_name(j))% comparing the two strings
    W=W+atomic_weight(j);
    position=j;
end
    end
    n=str2double(species(i)); % here we were searching for more atoms if any ,we are adding the weight of that atom 
    if n>1
        W=W+atomic_weight(position)*(n-1);
    end
    fprintf('Molecular weight of %s = %f \n',species,W)
end
Steps
>Here we are creating function for finding molecular weight by taking input as species
>Here we are defining the atomic name of the species and atomic weight of the species using arrays
>Here we use two loops ,one runs through atomic name array and other runs through atomic weight array
>Then we are creating an if condition and use strcmp command to compare the species and atomic name strings to calculate molecular masses
>Now by using str2double we can find value of n
>n is the number of atoms of the same kind
>Then we are creating a condtion for n>1 ,for n>1f function takes on modified fromula for calculating the weight of extra atoms and gives the value for molecular weight
Attachments
>Screenshot of the window where all the folders are saved
>Output -molecular weight displayed in command window(five screenshots)
>Plots for o2, N2 and Co2 species
>Screenshot of error
References
For getting details of gas constant,specific heat,entropy,enthalpy and molecular weight - /httpp://www.britannica.com
Error faced
Screenshot of error is attached
Iam try run the function code for entropy alone,this leads to an error
I overcome this error by running the function along with the main code
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.