Challenge

Project 1 - Parsing NASA thermodynamic data

Title :- Parsing NASA Thermodynamic data.

Aim :- Write a code to open and read file called "Thermo.dat", extract all the required data i.e. temperatures, coefficients for different gases. Calculate specific heat, Entropy and Enthalpy for each gas with corresponding values of temperature and coefficients. Plot Specific heat Vs Time, Entropy Vs Time and Enthalpy Vs Time graphs.

Objectives :-

1. To open and read file called "Thermo.dat''.

2. Extract temperatures and coefficients for each gas.

3. Calculate specific heat, entropy and enthalpy for each gas with respect to temperature and coefficients.

4. Calculate molecular weight of each species.

5. Plot Specific heat Vs temperature, Entropy Vs temperature and Enthalpy Vs temperature graph.

Theory :-

Parsing, syntax analysis or sintactic analysis is a process of analysing a string of symbols, either in natural language, computer language or data stucture. The term has different meanings in differernt branches of linguistic and computer science. Traditional sentence parsing is often performed as a method of understanding exact meaning of sentence or word. And within computational linguistic the term is used to refer formal analysis by a computer or other words of its constitutes, resulting in parse tree showing there syntactic relation to each other. Data parsing is nothing but extrcting required information form file and use that extracted data for further calculation, plotting graphs etc with help of computer or computer language.

Governing Equations :-

1. Specific Heat :- It is quantity of heat required to raised one gram of a substance by one degree celcius. The unit of specific heat is calories or joules.

Formula to calculate specific heat is given by,

`(cp)/r = a1+a2*T+a3*T^2+a4*T^3+a5*T^4`

2. Enthalpy :- Enthalpy is an energy like property or state function. Enthalpy is sum of internal energy and product of pressure and volume of thermodynamic system.

Formula to calculate enthalpy is given by,

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

3. Entropy :- Entropy is measure of systems thermal energy per unit temperature that is available energy to do work.'

Formula to calculate Entropy is given by,

`S/R = a1*ln(t) + a2*T+ (a3*T^2)/2+(a4*T^3)/3+(a5*T^4)/4+a7`

First seven coefficients are for High temperature and second seven coefficient are for low temperatures.

Also need to consider Local temperature range for calculation for each species, not global temperature.

Code explanation :-

clear all

close all

clc

%Open the THERMO.dat file

f1 = fopen ('THERMO.dat','r');

% Reading header information.

Header_info = fgetl(f1);

%Reading the temperature values distingushed as local low, average and high temperature.

line = fgetl(f1);

a = strsplit(line,' ');

global_low_temp = str2num(a{2}); %Extracting global low temp.

global_average_temp = str2num(a{3}); %Extracting global average temp.

global_high_temp = str2num(a{4}); %Extracting global high temp.

%Neglecting further three lines

fgetl(f1);

fgetl(f1);

fgetl(f1);

%Extracting data for each gas.

for i = 1:53

line = fgetl(f1)

a = strsplit(line,' ');

Gas = a(1);

e = findstr(line,'.');

local_low_temp = str2num(line(e(1)-4:e(1)+4)); %Extracting local low temp.

local_average_temp = str2num(line(e(2)-4:e(2)+4)); %Extracting local average temp.

local_high_temp = str2num(line(e(3)-4:e(3)+4)); %Extracting local high temp.

%Extracting all 14 coefficients required for calculation.

ct = 1

for j = 1:3

line = fgetl(f1);

b = findstr(line,'E');

for i = 1:length (b)

if ct<=7 %Applying condition on Coefficients for further calculations.

Ha(ct) = str2num(line(b(i)-11:b(i)+3));

ct = ct+1;

else

La(ct-7) = str2num(line(b(i)-11:b(i)+3));

ct = ct+1;

end

end

end

%Initializing specific heat, entropy, enthalpy values to zero.

cp = 0;

s= 0;

H = 0;

%Calling the function where cp,s and H are calculated.

[cp,H,S] = coefficient(local_low_temp,local_avearge_temp,local_high_temp,La,Ha);

%Molecular weight Calculating function

M = weight(gas);

%Plotting

GAS = char(Gas);

T = local_low_temp:local_high_temp;

current = pwd;

mkdir(GAS);

cd(GAS)

%Plotting the fig(1)

figure(1);

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

legend(Gas);

txt1 = ['Molecular weight:' num2str(M)];

text(500,max(cp)*0.98,txt1,'color','r');

%Giving Grid to plot

grid on

%Giving label and title for the plot

xlabel('temperature(k)');

ylabel('Specific heat (Kj/mol)');

title('Specific temperature Vs temperature')

%Plotting the fig(2)

figure(2);

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

legend(Gas);

txt1 = ['Molecular weight:' num2str(M)];

text(500,max(cp)*0.98,txt1,'color','b');

%Giving Grid to plot

grid on

%Giving label and title for the plot

xlabel('temperature(k)');

ylabel('enthalpy (Kj-mol)');

title('enthalpy Vs temperature');

%Plotting the fig(3)

figure(3);

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

legend(Gas);

txt1 = ['Molecular weight:' num2str(M)];

text(500,max(cp)*0.98,txt1,'color','b');

%Giving Grid to plot

grid on

%Giving label and title for the plot

xlabel('temperature(k)');

ylabel('entropy(Kj)');

title('entropy Vs temperature')

saveas(figure(1),'cp.jpg');

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

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

cd (current);

end

%Function to calculate molecular weight

function Mo = weight(Gas)

weight = [1 12 14 16 32 0 40];

Gas1 = ['H' 'C' 'N' 'O' 'S' 'A' 'R'];

number[1 2 3 4]

M=0;

GAS = char(Gas);

for i=1:length(GAS)

for j=1:length(Gas1)

if GAS(i) ==Gas1(j)

M(i) = weight (j);

end

for j = 1:length(number)

if str2double (GAS(i)) ==number(j)

m(i) = M(i-1)*(number(j)-1);

end

end

end

end

Mo = sum (M);

end

%Function to calculate cp, H, S.

function [cp,H,S] = coefficient (local_low_temp,local_average_temp,local_high_temperature,La,Ha)

for = 1:local_high_temperature-local_low_temperature+1

R= 8.314;

T = i + local_low_temp+1;

if T<= local_avearge_temp

cp(i) = R*(La(1) + La(2)*T+La(3)*T^2+La(4)*T^3+La(5)*T^4;

H(i) = R*T*(La(1)*+ (La(2)*T)/2 + (La(3)*T^2)/3 + (La(4)*T^3)/4 + (La(5)*T^4)/5 + La(6)/T);

S(i) =R*(La(1)*log(T) + (La(2)*T) + (La(3)*T^2)/2 + (La(4)*T^3)/3 + (La(5)*T^4)/4 + (La(7)));

else

cp(i) = R*(Ha(1) + Ha(2)*T+Ha(3)*T^2+Ha(4)*T^3+Ha(5)*T^4;

H(i) = R*T*(Ha(1)*+ (Ha(2)*T)/2 + (Ha(3)*T^2)/3 + (Ha(4)*T^3)/4 + (Ha(5)*T^4)/5 + Ha(6)/T);

S(i) =R*(Ha(1)*log(T) + (Ha(2)*T) + (Ha(3)*T^2)/2 + (Ha(4)*T^3)/3 + (Ha(5)*T^4)/4 + (Ha(7)));

end

end

end

Code Explanation :-

Step-1 :- opening the file called "Thermo.dat" and apply read mode to read the complete file. Using fgetl command we can get each line from file as per our requirement.

Step-2 :- First line is saved in Thermo_info variable. Next line contents global temperature ranges, using fgetl command temperature are extracted and using strsplit command we can seperate the temperature as global_low_temp, global_average_temp, global_high_temp.

Step-3 :- Neglecting further three lines as it is not required, using fgetl command.

Step-4 :- Extract the local temperatures range and save it in variable called local_low_temp, local_avearge_temp, local_high_temp for different gases as given in data.

Step-5 :- Extract all the fourteen coefficient required for calculation of specific heat, enthalpy, entropy for all 53 gases and apply condition that first seven coefficient are for low tempearture range and further seven coefficient are for high temperaure range. based on that different properties of gases are calculated.

Step-6 :- Once the properties are calculated plot the graphs and put it in folder with initial values of specific heat, entropy and enthalpy as zero. Using numerous command like pwd, mkdir, cd, saveas written in code save corresponding file to respective directory.

step-7 :- Calculate Molecular weight for each gas. Create a function called weight apply proper input arguments and conditions to calculate molecular weight of each gas and save it in its corresponding folder as craeted earlier.

Results :-

After succesful running of code variables are created in workspace as shown below,

As per objective different folders are created for respective gas and all extarcted data for that gas is saved into that.

Some graphs are attached which are plotted,

for example for 02

Specific heat Vs temperature plot.

Enthalpy Vs Temp plot.

Entropy Vs Temp plot.

C02

Specific heat Vs Temp plot

Enthalpy Vs Temp plot

Entropy Vs Temp plot,

N2

Specific Heat Vs Temp plot

Enthalpy Vs Temp Plot

Entropy Vs Temp Plot

Molecular Weight of each Gas,

Conclusion :-

File parsing for file called "Thermo.dat" is done. Extracting data from file to calculate properties like specific heat, entropy and enthalpy is done. Molecular weight of each gas is calculated by craeting function called weight. Plotting for different properties Vs temperaure is done. Folders are created for each gas and respective data is dump into that.

References :-

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

https://www.mathworks.com/matlabcentral/answers/497882-parsing-text-files-for-beginners

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.