Project 1 - Parsing NASA thermodynamic data
File Parsing:
It is a technique where we can read the contents from a file as well as store the contents in a variable and use the data contained in the file in our program to perform various computations.
As a part of this challenge, we are supposed to calculate the specific heat, enthalpy, entropy, and molecular mass of each species using the formula given and extracting the required data from the THERMO file provided.
first, open the thermo file in Matlab using fopen command in the read mode.
Make sure the file is in same folder as that of matlab.
we can read the individual lines in the file using fgetl command and store the output of the same in the variable.
The second line contains the global temp values and we store the same in Variable A using fgetl command and separate the temp valuse using strsplit and convert to numeric value using str2num command.
Read the next 3 lines using fgetl command and leave them as they are comment lines
as there are 53 species use a for loop from 1 to 53
Line 6 contains species which is the first element and temperature which is there at line end so the logic used is First line no.6 is read using fgetl command and stored in a variable named line_6 then all the elements of line_6 is separated and stored in 'sp' variable using str split command and the first element is species name and the same is stored under the variable named species then calculate the length of sp and store the last three elements as the local temperature sets.
then read the next line and store all the 14 coefficients and calculate cp,h,s for different temperatures by using the formula given.
then create a folder where we want to store the plot, using mkdir command specify the location of the folder.
then plot all the three parameters calculated separately and store the plots.
cd command helps the matlab come out of its folder and store the plots in the folder specified.
To calculate molecular weight:
first of all we make a list of all atoms that are present in the molecules and also there respective atomic weights and each character of species is compared with atoms list and if they match the respective atomic weight is added else if it does not match it might be a numeric value and the same is converted to numeric using str2num command and it is stored in a variable called factor and if factor is greater than 1 it is multiplied with the species of (i).
Errors in programming :
not major errors was encountered minor logical errors and usage of normal brackets instead of flower brackets resulted in errors.
close all
clear all
clc
% open the thermo file in read mode
f1=fopen('THERMO.dat','r');
fgetl(f1);
% store the contents of second line (Global temperature limits)
A=fgetl(f1);
A=strsplit(A,' '); % extract the temperature values
% change from string to number use flower bracket as it is cell array
global_low_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_high_temp=str2num(A{4});
% read next 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);
% since there are 53 species
for i=1:53
% Line 6 contains species name and local temp values
line_6=fgetl(f1);
sp=strsplit(line_6,' ');
species=sp{1}; % first element is species name
temp=length(sp); % last three ele are temp values
temp1=sp{temp-3};
temp2=sp{temp-2};
temp3=sp{temp-1};
local_low_temp=str2double(temp1); % use str2double nstead of str2num
local_high_temp=str2double(temp2);
local_mid_temp=str2double(temp3);
T=linspace(local_low_temp,local_high_temp,10); %generate temp array
% extract the coeffs
% a1 to a7 high temp coeffs and a8 to a14 low temp coeffs
line_7=fgetl(f1);
a=findstr(line_7,'E');
a1=line_7(1:a(1)+3);
a1=str2num(a1);
a2=line_7(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=line_7(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=line_7(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=line_7(a(4)+4:a(5)+3);
a5=str2num(a5);
line_8=fgetl(f1);
a=findstr(line_8,'E');
a6=line_8(1:a(1)+3);
a6=str2num(a6);
a7=line_8(a(1)+4:a(2)+3);
a7=str2num(a7);
a8=line_8(a(2)+4:a(3)+3);
a8=str2num(a8);
a9=line_8(a(3)+4:a(4)+3);
a9=str2num(a9);
a10=line_8(a(4)+4:a(5)+3);
a10=str2num(a10);
line_9=fgetl(f1);
a=findstr(line_9,'E');
a11=line_9(1:a(1)+3);
a11=str2num(a11);
a12=line_9(a(1)+4:a(2)+3);
a12=str2num(a12);
a13=line_9(a(2)+4:a(3)+3);
a13=str2num(a13);
a14=line_9(a(3)+4:a(4)+3);
a14=str2num(a14);
R = 8.314;
% calculate cp, h, s for various temp
if T <= local_mid_temp
Cp = (a8 + (a9 * T) + (a10*T.^2)+(a11*T.^3) + (a12*T.^4)).*R;
H= ( a8+((a9.*T)/2)+((a10.*T.^2)/3)+((a11.*T.^3)/4) + ((a12.*T.^4)/5)+ (a13./T)).*T*R ;
S = (a8*log(T) +(a9*T)+(a10*T.^2/2)+(a11* T.^3/3) + (a12*T.^4/4) +a13).*R;
else
Cp = R*(a1+(a2*T)+(a3*T.^2) +(a4*T.^3)+(a5*T.^4));
H= ( a1+(a2*T)/2)+((a3*T.^2)/3) + (a4*T.^3/4) + ((a5*T.^4)/5) +(a6./T).*T*R;
S = (a1*log(T)+(a2*T+(a3*T.^2/2)+(a4*T.^3/3) +(a5*T.^4/4) + a7)).*R;
end
% create directory to store the plots
mkdir(['C:\Users\Welcome\Documents\MATLAB\File parsing\',species])
% generate plot
figure(1)
plot(T,Cp)
title (species);
xlabel ('temperature')
ylabel (' specific heat' )
figure(2)
plot(T,H)
title (species);
xlabel ('temperature')
ylabel (' enthalpy' )
figure(3)
plot(T,S)
title (species);
xlabel ('temperature')
ylabel (' entropy' )
% use cd command to make the matlab come out of its folder and save the
% plots in other folder.
cd(['C:\Users\Welcome\Documents\MATLAB\File parsing\',species])
% save the plots as jpeg format
saveas(1,'Specific Heat','jpeg')
saveas(2,'Enthalpy','jpeg')
saveas(3,'Entropy','jpeg')
% calculation of molecular mass
mm=0;
atoms=['H','C','N','O','A','S'];
aw=[1,12,14,16,40,32];
for i=1:length(species)
for j=1:length(atoms)
if ( strcmp(atoms(j),species(i))==1)
mm=mm+aw(j);
k=j;
end
end
factor=str2double(species(i));
if (factor>1)
mm=mm+((factor*aw(k))-aw(k)) ;
end
end
fprintf('molecular weight of ')
fprintf(species)
fprintf(':')
fprintf('%d',mm)
disp(' ');
end