Challenge

Project 1 - Parsing NASA thermodynamic data

FILE PARSING:

File parsing is a process of analyzing a string of symbols, either in natural language, computer language or data structure conforming the rules of a formal grammar. The term parsing comes from the Latin pars (orations), meaning part of speech. With in 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 to a parse tree showing their syntactic relation to each other, which may also contain 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 in a data stream of some sort and building as in memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on that data.

NASA THERMODYNAMIC DATA FILE:

In NASA thermodynamics file there are 53 species. Each species contains 14 coefficient and temperature ranges. And we have to parse this data file to get the (Cp – KJ) Specific heat, (S- J/mol*k)Entropy, (H- KJ/mol)Enthalpy.

R = Universal Gas Constant (8.314 J/mol*k)

SPECIFIC HEAT (KJ):

Specific heat is the amount of heat required for a unit mass to raise the temperature by one degree Celsius. The relationship between the heat and temperature change is usually expressed in the form given.

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

ENTROPY (J/mol*K):

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.

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

ENTHALPY:

Enthalpy , a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume. In a system contained so as to prevent mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy. It is a state function.

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

MAIN CODE:

clear all

close all

clc

R = 8.314 % Universal gas constant in J/(mol*k)

f1 = fopen('THERMO_1.txt','r')

fgetl(f1)

temp = fgetl(f1)

s = strsplit(temp)

templow = str2num(s{2})

tempmid = str2num(s{3})

temphi = str2num(s{4})

T = linspace(templow,temphi,1000);

for i = 1:3

get = fgetl(f1)

end

for j = 1:53

tline = fgetl(f1)

H = strsplit(tline,' ')

species = (H{1})

lowtemp = str2double(H{end-3})

midtemp = str2double(H{end-2})

hightemp = str2double(H{end-1})

tline1 = 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)

tline2 = fgetl(f1)

b = strfind(tline2,'E')

a6 = tline2(1:b(1)+3)

a6 = str2double(a6)

a7 = tline2(b(1)+4:b(2)+3)

a7 = str2double(a7)

% Finding Lower coefficients

a8 = tline2(b(2)+4:b(3)+3)

a8 = str2double(a8)

a9 = tline2(b(3)+4:b(4)+3)

a9 = str2double(a9)

a10 = tline2(b(4)+4:b(5)+3)

a10 = str2double(a10)

tline3 = fgetl(f1)

c = strfind(tline3,'E')

a11 = tline3(1:c(1)+3)

a11 = str2double(a11)

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

a12 = str2double(a12);

a13 = tline3(c(2)+4:c(3)+3);

a13 = str2double(a13);

a14 = tline3(c(3)+4:c(4)+3);

a14 = str2double(a14);

% calculate specific heat

Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);

% calculate Entropy

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

% claculate Enthalpy

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

% Calculate Molecular weight of the Species

W = Molecular_weight(species);

% writing molecular weight file

MW_file = fopen('Molecular Weight.txt','w');

fprintf(MW_file,'Molecular weight of %s is %d ',species,W);

fclose(MW_file); % Closing the file

%Plotting Cp, H and S with Temperature

cd('D:/matlab programs') % changing directory

mkdir(species); %creating directory

cd(species);

% Plot 1 Cp vs T

figure(1)

plot(T,Cp,'linewidth',2,'color','b');

xlabel('Temperature(K)');

ylabel('Specific Heat(KJ)');

title(sprintf('Specific heat vs Temperature range of %s',species));

grid on

saveas(figure(1),'Specific Heat.jpg');

% Plot 2 S vs T

figure(2)

plot(T,S,'linewidth',2,'color','r');

xlabel('Temperature');

ylabel('Entropy(KJ/mol-K)');

title(sprintf('Entropy vs Temperature range of %s',species));

grid on

saveas(figure(2),'Entropy.jpg');

% Plot 3 Cp vs T

figure(3)

plot(T,H,'linewidth',2,'color','g');

xlabel('Temperature');

ylabel('Enthalpy(KJ/mol)');

title(sprintf('Enthalpy vs Temperature range of %s',species));

grid on

saveas(figure(3),'Enthalpy.jpg');

cd('D:/Matlab Programs') %Changing directory to Original matlab path

% Molecular weight

W = Molecular_weight(species)

end

  1. Firstly, we have defined the value of universal gas constant.
  2. Then we have opened the file by the inbuilt command ‘fopen’ and given the permission to only read.
  3. Then we have used ‘fgetl’ command to read the data file. ‘fgetl’ command reads the file line by line.
  4. Temperature value defined by using ‘fgetl’ command.
  5. We have defined ‘s’ as the cell array, by using ‘strsplit’. ‘s’ is a 1*4 cell array.
  6. ‘str2num’ converts a character array or a string array in to a numeric matrix.
  7. Now, then we have defined ‘T’ using linspace between the templow to temphi. And number of iterations are 1000.
  8. For skipping text reading in the ‘thermo_1’ data file. We have used the for loop for skipping the next three lines.
  9. For reading the data file and 14 co-efficient for 53 times. We have used ‘for’ loop for defining 53 elements differently.
  10. Firstly ‘tline’ is defined using ‘fgetl’ command.
  11. After that, ‘strsplit’ command used to get ‘tline’ as a cell array.
  12. Str2double is used to convert the array in to double precision value.
  13. In order to read th co-efficient in the data file, Strfind is used to find the place of ‘E’
  14. Out of which we will find the first 7 co-efficient which are for the higher temperature and then 7 co-efficient are for lower temperature range.
  15. After that, we have defined Specific heat, Entropy, Enthalpy, functions to find these parameters using these 14 co-efficient for lower and higher temperature range. After that we have defined molecular weight function to get the molecular weight of different species.
  16. Now, to store the molecular weight of 53 elements, we have write a text file using ‘fprintf’ command. Firstly open the file and then close the file.
  17. Now, ‘cd’ command is used to change the saved file location.
  18. Mkdir command is used to create the folder.
  19. Then we will use ‘saveas’ command to save the plots of different species in the directory folder.

FUNCTION FOR SPECIFIC HEAT

function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)

% calculating specific heat for higher and lower temperature repectively.

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

  1. Now, we will find the specific heat by function.
  2. We have defined the specific heat by using lower and higher temperature difference.
  3. After that, we have created a condition using if loop and make temperature less than and equal to mid temperature to specify lower temperature co-efficient in the formula.
  4. After that, we have used else command to specify for higher temperature co-efficient in the formula.
  5. After ending the function command, we are able to calculate the specific heat.

FUNCTION FOR ENTROPY

function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)

% calculating entropy for higher and lower temperature repectively.

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

  1. Now we will find the Entropy by creating Entropy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
  2. Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.
  3. Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
  4. Then end the loop and function to calculate Entropy.

FUNCTION TO FIND ENTHALPY

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

% calculating enthalpy for higher and lower temperature repectively.

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

  1. Now we will find the Enthalpy by creating Enthalpy function and assigning its related formula coefficient from 14 coefficients, Gas constant, Temperature and mid temperature.
  2. Now we will create a condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient in the formula.
  3. Then we will use else condition to specify coefficient of Higher temperature coefficient in the formula.
  4. Then end the loop and function to calculate Enthalpy.

FUNCTION TO FIND MOLECULAR WEIGHT

function W = Molecular_weight(species)

% Defining Atomic name of the species

atomic_name = ['H','C','O','N','Ar'];

% Defining Atomic masses of main species

atomic_weight = [1.008 12.011 15.999 14.007 39.948];

W=0; % Molecular weight starting range

for i=1:length(species) % Loop runs to the length of the species

for j=1:length(atomic_name) % Loop runs to the length of the atomic names

if strcmp(species(i), atomic_name(j)) %function to compare 2 strings

W = W + atomic_weight(j);

position = j;

end

end

n = str2num(species); % Now we will find if there is more element in the species then incrementing atomic weight of the species

if n>1

W = W + atomic_weight(position)*(n-1);

end

end

fprintf('Molecular Weight of %s :- ',species);

fprintf('%f',W);

disp(' ')

end

  1. Firstly we have created a function to calculate the molecular weight.
  2. Then we have defined the main element of all 53 species in an array atomic_name that are ‘H’,’C’,’O’,’N’,’Ar’.
  3. Then, by taking above main element we will create there atomic array from data available on internet.
  4. Then we have assigned the molecular weight W = 0, and then created the for loop condition which run to the length of species.
  5. After that we have created another for loop in the previous loop (nested loop) which runs to the length of the string atomic_names.
  6. Then we have created a if conditional loop to compare the species and atomic_name by using the inbuild command ‘strcmp’.
  7. Then we have used str2num to convert the string to a cell array which was assigned to the ‘n’. Then we have run the if loop to find weather the species contains more than on element that is n>1 then we will increment the atomic weight by the multiplication of position and n-1 to the atomic_weight array.
  8. Then we have used ‘fprintf’ command to print the molecular weight and used ‘disp’ command to display the molecular weight.

O2 Plot

N2 Plot

CO2 Plot

Molecular weight of 53 different species.

ERROR

Error in typing the directory folder name.

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.