Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: Write a code to extract data from the given file for each species.

Objective:

  1. File contains global temperature ranges ,local temperature ranges and coefficients for each species.
  2. For every temperature value each species will get different thermodynamic properties like Specific heat (Cp), Entropy(S) and Enthalpy(H)
  3. There is a variation of thermodynamic properties with Temperature. Code is developed to obtain to get those values and variation for each species
  4. Code can be developed in two ways: i) Get thermodynamic values for the user specified species- here we get plots and values for given input only.ii) Thermodynamic values of all species can get in a single click and we can save those in a separate folder

Code explanation:

  1. Load the given file into workspace
  2. Extract header information- Global temperature values
  3. Read data from line by line and extract those values
  4. Write a function to calculate molecular weights of species
  5. Calculating thermodynamic properties for each species with the known formulas
  6. Generating plots from the above data
  7. Saving in the desktop in location
  8. Close the all files

1.Load the given file into workspace:

thermo=fopen('THERMO.dat','r')

2.Extract header information- Global temperature values

fgetl(thermo);

tempdat=fgetl(thermo);

% splitting datta at space

A=strsplit(tempdat,' ')

% extractnig the global temperature range

LowGt=str2num(A{2});

MidGt=str2num(A{3});

HighGt=str2num(A{4});

% it is a cell array so we should use {}

% data for the species is from 5th line so writing fgetl for 5 th time

fgetl(thermo);

fgetl(thermo);

fgetl(thermo);

3.Read data from line by line and extract those values

% creating a loop for 53 lines

for i=1:53

data=fgetl(thermo);

a=strsplit(data,' ');

b=a{1};

Line1=fgetl(thermo);

SL1= findstr(Line1,'E');

% extracing data for each coefficient

coef1=Line1(1:SL1(1)+3);

coef2=Line1(SL1(1)+4:SL1(2)+3);

coef3=Line1(SL1(2)+4:SL1(3)+3);

coef4=Line1(SL1(3)+4:SL1(4)+3);

coef5=Line1(SL1(4)+4:SL1(5)+3);

% converting into numbers

a1=str2num(coef1);

a2=str2num(coef2);

a3=str2num(coef3);

a4=str2num(coef4);

a5=str2num(coef5);

% extracting the local temperature range of the input molecule

mlt=a(end-1);

midlt=str2double(mlt);

hlt=a(end-2);

highlt=str2double(hlt);

llt=a(end-3);

lowlt=str2double(llt)

4.Write a function to calculate molecular weights of species

function Mol_weight=MW(get)

Weight=0;

% input as molecule name

% atom deals with H C N O Ar

% to find molecular weight, we have to compare the atoms from input

atoms=['H','C','N','O','A'];

% only A is taken instead of Ar

mass=[1,12,14,16,40];

% since we have to compare each characters for mass,using 2 for loop

for i=1:length(get)

for j=1:length(atoms)

if strcmpi(get(i),atoms(j))

Weight=Weight+mass(j);

y=j;

end

end

% for more number of same atoms in the molecule

number=str2num(get(i));

if (number>1)

% for c2 we have to add mass of c 2 times

% intially since for c, mass is addes to mol_weight

% for extra molecule we use (number-1) so adding one or more weight

weight=Weight+(mass(y))*(number-1);

end

Mol_weight=Weight

5.Calculating thermodynamic properties for each species with the known formulas

% creating local temperature range for each species

TRange=linspace(lowlt,highlt,500);

% obtaining the value of specific heat,entropy and enthalpy from a mathematical formula

for i=1:length(TRange);

if (TRange(i)>midlt)

s_val(i)= ((a1*log(TRange(i))+(a2*TRange(i))+(a3*(TRange(i).^2))/2)+(a4*(TRange(i).^3))/3+(a5*(TRange(i).^4))/4+a7)*R;

h_val(i)= (a1+((a2*TRange(i))./2)+(a3.*(TRange(i).^2))./3 +(a4*(TRange(i).^3))./4 +(a5*(TRange(i).^4))./5+(a6./TRange(i)))*(R.*TRange(i));

cp_val(i)=(a1+a2*TRange(i)+a3*TRange(i).^2+a4*TRange(i).^3+a5*TRange(i).^4).*R;

else

s_val(i)= ((a8*log(TRange(i)))+(a9*TRange(i))+(a10*(TRange(i).^2))/2+(a11*(TRange(i).^3))/3+(a12*(TRange(i).^4))/4+a14)*R;

h_val(i) =(a8+(a9*TRange(i))./2+(a10*(TRange(i).^2))./3+(a11*(TRange(i).^3))./4+(a12*(TRange(i).^4))./5 +(a13./TRange(i)))*(R.*TRange(i));

cp_val(i)=(a8+a9*TRange(i)+a10*TRange(i).^2+a11*TRange(i).^3+a12*TRange(i).^4)*R;

end

end

6.Generating plots from the above data

% plotting cp versus T

figure(1)

plot(TRange,cp_val)

grid on

xlabel('Temperature')

ylabel('Specific Heat')

title(b)

% plotting Entropy versus T

figure(2)

plot(TRange,s_val)

grid on

xlabel('Temperature')

ylabel('Entropy(J/Kg)')

title(b)

% plotting Enthalpy versus T

figure(3)

plot(TRange,h_val)

grid on

xlabel('Temperature')

ylabel('Enthalpy(J/Kg)')

title(b)

end % here loop ended

7.Saving in the desktop in location:

% creating directory to store the plots

% defining current folder

mkdir(['C:\Users\ragha\OneDrive\Documents\MATLAB\file parsing\images\species name\',b])

cd(['C:\Users\ragha\OneDrive\Documents\MATLAB\file parsing\images\species name\',b])

% saving the images to current folder in specified formats

saveas(figure(1),'cp vs T.jpg')

saveas(figure(2),'s_val vs T.jpg')

saveas(figure(3),'h_val vs T.jpg')

% definning current folder back as parent folder

% cd('E:')

8.Closing all opened files:

% closing the opened files

fclose('all');

function Mol_weight=MW(get)
Weight=0;
% input as molecule name
% atom deals with H C N O Ar
% to find molecular weight, we have to compare the atoms from input

atoms=['H','C','N','O','A'];
% only A is taken instead of Ar
mass=[1,12,14,16,40];
% since we have to compare each characters for mass,using 2 for loop

for i=1:length(get)
    for j=1:length(atoms)
        if strcmpi(get(i),atoms(j))
            Weight=Weight+mass(j);
            y=j;
        end
    end
  % for more number of same atoms in the molecule
  number=str2num(get(i));
  if (number>1)
  % for c2 we have to add mass of c 2 times
  % intially since for c, mass is addes to mol_weight
  % for extra molecule we use (number-1) so adding one or more weight
  weight=Weight+(mass(y))*(number-1);
  end
  Mol_weight=Weight
end

clear all
close all
clc
% reading data from the file
thermo=fopen('THERMO.dat','r')
fgetl(thermo);
tempdat=fgetl(thermo);
A=strsplit(tempdat,' ') % splitting datta at space
% extractnig the global temperature range
LowGt=str2num(A{2});    % it is a cell array so we use{}
MidGt=str2num(A{3});
HighGt=str2num(A{4});
fgetl(thermo);  % data for the species is from 5th line so writing fgetl for 5 th time
fgetl(thermo);
fgetl(thermo);
% defining variable to store the input from user end 
%get=input('Enter species name ','s')
% while ~ feof(thermo)
% creating a loop for 53 lines
    for i=1:53
    data=fgetl(thermo);
    a=strsplit(data,' ');
    b=a{1};
% comparing input and data of molecule
%     match=strcmpi(b,get)
%     if match==1
%         disp('species data available')
% extracting coefficients of desired molecule
    Line1=fgetl(thermo);
    SL1= findstr(Line1,'E');
    coef1=Line1(1:SL1(1)+3);    % extracing data for each coefficient
    coef2=Line1(SL1(1)+4:SL1(2)+3);
    coef3=Line1(SL1(2)+4:SL1(3)+3);
    coef4=Line1(SL1(3)+4:SL1(4)+3);
    coef5=Line1(SL1(4)+4:SL1(5)+3);
    a1=str2num(coef1);  % converting into numbers
    a2=str2num(coef2);
    a3=str2num(coef3);
    a4=str2num(coef4);
    a5=str2num(coef5);
   
 Line2=fgetl(thermo)
 SL2=findstr(Line2,'E')
    coef6=Line2(1:SL2(1)+3);
    coef7=Line2(SL2(1)+4:SL2(2)+3);
    coef8=Line2(SL2(2)+4:SL2(3)+3);
    coef9=Line2(SL2(3)+4:SL2(4)+3);
    coef10=Line2(SL2(4)+4:SL2(5)+3);
    a6=str2num(coef6);
    a7=str2num(coef7);
    a8=str2num(coef8);
    a9=str2num(coef9);
    a10=str2num(coef10);

Line3=fgetl(thermo)
SL3=findstr(Line3,'E')
    coef11=Line3(1:SL3(1)+3);
    coef12=Line3(SL3(1)+4:SL3(2)+3);
    coef13=Line3(SL3(2)+4:SL3(3)+3);
    coef14=Line3(SL3(3)+4:SL3(4)+3);
    a11=str2num(coef11);
    a12=str2num(coef12);
    a13=str2num(coef13);
    a14=str2num(coef14);


% extracting the local temperature range of the input molecule
mlt=a(end-1);
midlt=str2double(mlt);
hlt=a(end-2);
highlt=str2double(hlt);
llt=a(end-3);
lowlt=str2double(llt)

% break
% 
%     elseif match==0
%         disp('species data not available')
%     else
%         continue
%     end
% end
% f=upper(get);
% Displaying Molecular weight of the input molecule
mole_Weight=MW(b)
disp('Molecules entered')
disp(b)
disp('moleWeight')
TRange=linspace(lowlt,highlt,500);  % creating local temperature range for each species
R=8.314
% obtaining the value of specific heat,entropy and enthalpy from a
% mathematical formula
for i=1:length(TRange);
if (TRange(i)>midlt)
s_val(i)=  ((a1*log(TRange(i))+(a2*TRange(i))+(a3*(TRange(i).^2))/2)+(a4*(TRange(i).^3))/3+(a5*(TRange(i).^4))/4+a7)*R;
h_val(i)= (a1+((a2*TRange(i))./2)+(a3.*(TRange(i).^2))./3 +(a4*(TRange(i).^3))./4 +(a5*(TRange(i).^4))./5+(a6./TRange(i)))*(R.*TRange(i));
cp_val(i)=(a1+a2*TRange(i)+a3*TRange(i).^2+a4*TRange(i).^3+a5*TRange(i).^4).*R;
else
s_val(i)=  ((a8*log(TRange(i)))+(a9*TRange(i))+(a10*(TRange(i).^2))/2+(a11*(TRange(i).^3))/3+(a12*(TRange(i).^4))/4+a14)*R;
h_val(i) =(a8+(a9*TRange(i))./2+(a10*(TRange(i).^2))./3+(a11*(TRange(i).^3))./4+(a12*(TRange(i).^4))./5 +(a13./TRange(i)))*(R.*TRange(i));
cp_val(i)=(a8+a9*TRange(i)+a10*TRange(i).^2+a11*TRange(i).^3+a12*TRange(i).^4)*R;
end
end
% plotting cp versus T
figure(1)
plot(TRange,cp_val)
grid on
xlabel('Temperature')
ylabel('Specific Heat')
title(b)
% plotting Entropy versus T
figure(2)
plot(TRange,s_val)
grid on
xlabel('Temperature')
ylabel('Entropy(J/Kg)')
title(b)
% plotting Enthalpy versus T
figure(3)
plot(TRange,h_val)
grid on
xlabel('Temperature')
ylabel('Enthalpy(J/Kg)')
title(b)
% creating directory to store the plots
% defining current folder
mkdir(['C:\Users\ragha\OneDrive\Documents\MATLAB\file parsing\images\species name\',b])
cd(['C:\Users\ragha\OneDrive\Documents\MATLAB\file parsing\images\species name\',b])
% saving the images to current folder in specified formats
saveas(figure(1),'cp vs T.jpg')
saveas(figure(2),'s_val vs T.jpg')
saveas(figure(3),'h_val vs T.jpg')
% definning current folder back as parent folder
% cd('E:')
% closing the opened files
    end     % here loop ended 
fclose('all');

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.