Challenge

Project 1 - Parsing NASA thermodynamic data

AIM;

TO Write a code inmatlab to parsing the nasa thermodynamic data file and calculate the thermodynamic properties of various gas species

OBJECTIVES;

  • TO write a function that extract the co-eff and calculate the specific heat(cp),enthalpy(h) and entropy(s) for all the species in the data file
  • To calculatye the molecular weight of each species and display in the command window
  • And plot cp,enthalpy and entropy for local temperature renge(low temp : high temp)
  • To save the plots a image with appropriate namesand dump them in separate folders for each species with suitable name and all these should be done automatically
  • The mane program just take the 'THERMO.dat' file as input and generate cp,H,S plots

FILE PARSING DEFINITION;

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.

specific heat;

specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree. For example, the specific heat of water is 1 calorie (or 4.186 joules) per gram per Celsius degree.

Cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4

function of (Cp)

function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,TEMPmid)
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
Enthalpy;

Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the product of the pressure and volume of the system. It reflects the capacity to do non-mechanical work and the capacity to release heat.Enthalpy is denoted as H; specific enthalpy denoted as h. Common units used to express enthalpy are the joule, calorie, or BTU (British Thermal Unit.) Enthalpy in a throttling process is constant.

H/RT = a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T

function of (H)

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,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
ENTROPY (S)
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. The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena.
S/R =a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7
function of (S)
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
function of molecular weight(W)
function W = Molecular_weight(species)
atomic_name = ['H','C','O','N','Ar'];
atomic_weight = [1.008 12.011 15.999 14.007 39.948]
W = 0
for i = 1:length(species)
for j = 1:length(atomic_name)
if strcmp(species(i),atomic_name(j))
W = W + atomic_weight(j);
position = j;
end
end
n = str2num(species);
if n>1
W = W + atomic_weight(position)*(n-1);
end
end
fprintf('Molecular weight of %s :- ',species);
fprintf('%f',W);
disp(' ')
end
GOVERNING EQUATION;
THERMO.dat file
MAIN CODE;
clear all
close all
clc

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

%noe reading temperature from this file
 temp = fgetl(f1);
%spliting

s = strsplit(temp);

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

%temperature range
T = linspace(templow,temphigh,1000);

%skipping comment lines
 for i = 1:3
     get=fgetl(f1);
 end

 %reading the species
for j = 1:53;
 tline=fgetl(f1);

 A=strsplit(tline,' ');
 species = (A{1});

 %converting string to number
 lowtemp = str2double(A{end-3});
 midtemp = str2double(A{end-2});
 hightemp = str2double(A{end-1});


 tline1 = fgetl(f1);
 a = strfind(tline1,'E');

 %finding higher coefficients
 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);

 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);


 %inputs
 R = 8.314;%gas constant 

%calaulate specfic_heat (cp)
 Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);

%calculate entropy (S)

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

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

%calculate molecular weight 

W = Molecular_weight(species);

Mw_file = fopen('Molecular_weight.txt','w');
fprintf(Mw_file,'Molecular weight of %s is %d',species,W);
fclose(Mw_file);


%plotting cp,H,S with temperature
 
%saving in folder
cd('C:\Users\lakshmipathy\Documents\MATLAB\O\O2')
mkdir(species);
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(k)');
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(k)');
ylabel('enthalpy(kj/mol');
title(sprintf('enthalpy vs temperature range of %s',species));
grid on
saveas(figure(3),'enthalpy.jpg');

cd('C:\Users\lakshmipathy\Documents\MATLAB\O\O2')

W = Molecular_weight(species)

end
command used in code
- Read line from file, removing newline characters
This MATLAB function returns the next line of the specified file, removing the newline characters.

strsplit(str,delimiter) splits str at the delimiters specified by delimiter.

If str has consecutive delimiters, with no other characters between them, then strsplit treats them as one delimiter. For example, both strsplit('Hello,world',',') and strsplit('Hello,,,world',',') return the same output.

str2double (str) converts the text in str to double precision values. str contains text that represents real or complex numeric values. str can be a character vector, a cell array of character vectors, or a string array.

cd - Change or view current folder on SFTP or FTP server

mkdir - Make new folder on SFTP or FTP server This MATLAB function makes the specified folder on the SFTP or FTP server associated with s.

RESULTS;

screenshot of the directory

plot for o2;

Molecular weight of O2 :- 15.999000

plot for n2;

Molecular weight of N2 :- 14.007000

plot for co2;

Molecular weight of CO2 :- 28.010000

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.