Challenge

Project 1 - Parsing NASA thermodynamic data

Program for extracting 14 co-efficients and get desired plots
close all
clear all
clc
%opening and reading the NASA thermal data file
file=fopen('THERMO.dat','r');
%Read heading
line1=fgetl(file);
%read global temprature values
line2=fgetl(file);
%splitting the line to get global temp values
A = strsplit(line2,' ');
%reading the global temp values
Global_low_temprature = str2num(A{2});
Global_mid_temprature = str2num(A{3});
Global_high_temprature = str2num(A{4});
%Next three lines are comments from program point of view
comment_1 = fgetl(file);
comment_2 = fgetl(file);
comment_3 = fgetl(file);

%extracing the species information 
for i = 1:53
    
    species=fgetl(file);
    
    %reading the species namce as it is present in first six spaces
    
 species_name = species(1:6);
 
 %reading the first coefficents line
 Line_1 = fgetl(file);
 
 %identifying the E character so as to differentiate the coefficients
 L1 = strfind(Line_1,'E');
 
 %extracting the coefficients based on postiion of E
 c1 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
 
 c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
 
 c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
 
 c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
 
 c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));
 
 %reading the second coefficients line
 Line_2 = fgetl(file);
 
 L2 = strfind(Line_2,'E');
 
 c6 = str2num(Line_2(1:(L2(1)+3)));
 
 c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
 
 c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
 
 c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
 
 c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));
 
 
 %Reading the first coefficients line
 Line_3 = fgetl(file);
 
 L3 = strfind(Line_3,'E');
 
 c11 = str2num(Line_3(1:(L3(1)+3)));
 
 c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
 
 c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
 
 c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));
 
 %creating folder for each species
 mkdir(['E:\SKILL LYNC\NASA file parsing\species\',species_name]);
 
 
 %Temprature ranges assumed for calculation purpose
 T =linspace(300,5000,100);
 
 %Universal gas constant
 R = 8.314;
 
 �lculation of properties based on the  lower and upper temprature
 for j =length(T)
     
     %first seven coefficients are used if Temp is greater than 3000
     if T(j)>3000
        
 Cp =(c1+c2.*T+c3.*T.^2+c4.*T.^3+c5.*T.^4).*R;
 H = (c1+(c2.*T)./2+(c3.*T.^2)/3+(c4.*T.^3)/4+(c5.*T.^4/5)+c6./T).*T.*R;
 S = (c1*log(T)+c2.*T+(c3.*T.^2)/2+(c4.*T.^3)/3+(c5.*T.^4)/4+c7).*R;
 
 %otherwise the next seven coefficients are used
     else
 Cp = (c8+c9.*T+c10.*T.^2+c11.*T.^3+c12.*T.^4).*R;
 H = (c8+(c9.*T)/2+(c10.*T.^2)/3+(c11.*T.^3)/4+(c12.*T.^4/5)+c13./T).*T.*R;
 S = (c8.*log(T)+c9.*T+(c10.*T.^2)/2+(c11.*T.^3)/3+(c12.*T.^4)/4+C14).*R;
 
     end
 end
 %plotting the properties with respect to local temp values
 
 %teamprature vs Entropy
 figure(1)
 plot(T,S,'linewidth',4,'color','red')
 title(species_name)
 xlabel('Temprature')
 ylabel('Entropy')
 
 %teamprature vs Enthalpy
 figure(2)
 plot(T,H,'linewidth',4,'color','blue')
 title(species_name)
 xlabel('Temprature')
 ylabel('Enthalpy')
 
 
 %teamprature vs specific Heat 
 figure(3)
 plot(T,Cp,'linewidth',4,'color','green')
 title(species_name)
 xlabel('Temprature')
 ylabel('Specific Heat')
 
 
 %CHANGING THE FILEPATH TO SAVE THE PLOTS CD
 saving = (['E:\SKILL LYNC\NASA file parsing\species\',species_name]);
 cd(saving)
 
 %saving the plots
 saveas(1,'Entropy.jpg');
 saveas(2,'Enthalpy.jpg');
 saveas(3,'Specific Heat.jpg');
 
 �lculating the molecular mass of each species
 
 M_M_ = Molecular_mass(species_name);
 Species = species_name;
 
end
fclose(file);
 

EXPLAINATION:

The program begins with clear all, close all and clc which clears the workspace, closes all the plots and clears the command window 

next, NASA thermodat file is opened using the command fopen. 

Using the fgetl command, we extracted each line of the file into the command window. The first line being the header info followed by global temperature values.

The global temperature values obtained in the previous step is in the form of strings. then to convert them in numbers, str2num command is used.

From the 6th line onwards, we have the 53 species name and their 14 coefficients. Using the for loop, name of species and coefficients are extracted.

The strfind command helps us to locate the letter E(exponential) which is used as reference for gettiing the coefficient values. 

 The mkdir command creates a folder for saving the plots that will be generated.

 A range of temperature is assumed for calculating enthalpy,entropy and specific heat values.

 In the next step, the for loop determines thend, the calculated parameters are plotted against the corresponding value of temperature. parameters for each temperature values.

 With the help of plot command, the calculated parameters are plotted

 The file directory is then changed to the one that was created in the 9th step to save these plots.

 Saveas command helps us in saving these plots 

 The directory is then switched back for molecular mass calculation.

 Next line is a function call to calculate the molecular mass.

The file is closed.

Program to calculate the Molecular mass of each species:
�fining the function
function MM = Molecular_mass(species_name)
%talking input as the specied name
species = upper(species_name);
�fining the elements of species
Constituents = ['H', 'C', 'N','O','A'];
�fining the atomic wt of elements
Atomic_weight = [1 12 14 16 40];
MM = 0;
�lculating the moelcular mass based on elements in species
for i = 1:length(species)
    for j = 1:length(Constituents)
        
        %comparing the species and elements
        if strcmp(species(i),Constituents(j))
            MM = MM + Atomic_weight(j);
            u = j;
        end
    end
    
    n = str2double(species(i));
    if n>1
        MM = MM + Atomic_weight(u)*(n-1);
    end
    
end
end

EXPLANATION:

This function takes species_name as input and gives out its Molecular mass as output.

 Each species name is stored in string

 From the file given,  the constituents that make up each species are H, C, N, O and Ar. 

The atomic weights of each constituent is stored in an array called atomic_weight.

 The molecular weight is set to zero in the beginning,

 Using the nested for loop, molecular weight is calculated


FILES CREATED:



PLOTS FOR N2, O2, CO2:


N2:

Enthalpy - 


ENTROPY - 


SPECIFIC HEAT - 




CO2 - 

ENTHALPY -


SPECIFIC HEAT -


ENTROPY - 


SPECIFIC HEAT - 




ERRORS - 




Changes made to overcome the above error - 

- changing the directory

- using cd command for saving

- using proper paranthesis were required

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.