Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: We have to extract the data from the given file and calculate the required thermodynamic properties

Procedure:

1) We need to read the data from the given file Using fopen.

f1=fopen('thermo.dat','r') 'r':- This we read the file

2) Then we can use the fgetl command to get to the desired line in the data file. ( we need to skip the first 5 line of the data file as it doesn't contain any information that is required to solve the given equation). to skip we will use the for loop

3) lineb = fgetl(f1); line 'b' is the first line of the species data which contains name of species and local temperature of the respective species. All the value will be in the form of string we need to covert them into different value.

The logic used to extract the species name and local temperature is explained below is explianed below:

We will use the ' ' ('space') as a delimeter to indentify and get the individual value.

species_info = strsplit(lineb,' '); % spliting the string into each element of the string
species_name = species_info{1}; % selecting the species_name from the species_info
local_temp_1 = str2num(species_info{end-3}); % local minimum temperature 3rd last from end
local_temp_2 = str2num(species_info{end-1}); % local medium temperature last from end
local_temp_3 = str2num(species_info{end-2}); % local maximum temperature 2nd last from end

'str2num': this will covert the string cell into number

4) linec = fgetl(f1); line 'c' is the second line of the respective species data, will contain the 5 coeffecient of high temperature. It will be labelled as: (a1,a2,a3,a4, and a5), we will use 'E' as a delimeter to get the each value pf the coefficent.

The logic used to extract the each cofficient is explianed below:linee = fgetl(f1)

a=findstr(linec,'E'); 'findstr' :- this will find the position of 'E' from one end of the string.

a1=linec(1:a(1)+3); a1:- will the first coefficient required to solve the given equation.

a(1) :- will be the first position where we will find 'E' from the from one left end

a(1)+3 :- 3 places after a(1), the first coefficient (a1) will end.

a2=linec(a(1)+4:a(2)+3); a2:- will the second coefficient required to solve the given equation.

a(2) :- will be the second position where we will find 'E' from the from one left end

a(1)+4:- 4 places after a(1), the second coefficient (a2) will start.

a(2)+3:- 3 places after a(2), the first coefficient (a2) will end

5) lined = fgetl(f1); line 'd' is the third line of the respective species data,the starting two coeffecient are of high temperature.labelled as (a6 and a7) and last three coefficients are low temperature. It will be labelled as (b1,b2 and b3) we will use 'E' as a delimeter to get the each value pf the coefficent.

6) linee = fgetl(f1); line 'e' is the fourth line of the respective species data, will contain the 5 coeffecient of low temperature. It will be labelled as: (b4,b5,b6, and b7), we will use 'E' as a delimeter to get the each value pf the coefficent.

7) We will use the the 'for loop', so that we can repeat the same process for all other 52 species in the data file.

8) As we can observe, the value of coefficient is different, for different temperature.

1) a1, a2, a3, a4, a5, a6 and a7 are higher temperature coefficient. (If temperature is more than Mid temperature)

2) b1, b2, b3, b4, b5, b6 and b7 are lower temperature coefficient. (If temperature is less than Mid temperature)

so we will use 'IF ELSE' condition we will use be:

IF temperature is more than Mid temperature then it will substitute the higher temperature coefficient and ELSE it will substitute the lower temperature coefficient.

And this will be used in the function file.

9) When a group of atoms bind together in a specific proportion, they form a compound and the smallest fundamental unit of that compound is known as a molecule. Molecular formula of a compound is a chemical formula that gives the information about the number of atoms of each element present in the molecule of that compound.
Molecular weight : molecular weight of a molecule is actually the sum of the weight of all the atoms of that compound. When we measure the weight of one molecule we call is molecular weight and we generally represent it by the unit amu or u. Full form of amu is an atomic mass unit.

When we measure the weight of 1 gram mole molecules of a compound we call it gram molecular weight. It is represented in gram. Both the molecular weights are equal in numerical digits but different in units.
Molecular formula of water: H2O

H2O is the molecular formula of a water compound’s molecule that tells us that to form one molecule of water 2 hydrogen atoms and 1 oxygen atom combine together.
Atomic weight of hydrogen = 1 amu
Atomic weight of oxygen = 16 amu
Molecular weight of water= 2 (weight of hydrogen atom) + weight of oxygen atom

= 2*1 + 1*16 = 18amu

logic used: (Molecular weight of water = molecular weight of hydrogen + molecular weight of oxgen )

Logic used for calulation will be:

1) First we will create array of all the basic elements and compare them with the species name given in the data file. this will provide the no of mole of each basic elements used to form the specific elements.

2) Then we will create array which will contain all the molecular weight of basic elements. Molecular weight of species = molecular weight of basic element 1 + molecular weight of basic element 2 + and so on.

Code of File Parsing project:

clear all
close all
clc

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

% skipping the first 5 lines  as that data is not required for solving
for x=1:5
    fgetl(f1)
end

for i = 1:53
% calculating the enthalphy, entropy, specific_heat, molecular_weight
    lineb = fgetl(f1); % select the line no:1 of 4, for the specific species from thermo data file
    species_info = strsplit(lineb,' '); % spliting the string 
    species_name = species_info{1}; % selecting the species_name from the species_info
    local_temp_1 = str2num(species_info{end-3}); % local minimum temperature
    local_temp_2 = str2num(species_info{end-1}); % local medium temperature
    local_temp_3 = str2num(species_info{end-2}); % local maximum temperature

% selecting the value from the thermo data file. 
% its used to gets the variable for calculating the functions

    linec = fgetl(f1); % line no: 2 of 4, for the specific species from thermo data file
    a=findstr(linec,'E'); % using 'E' as the refference to get the value of variable
    variable_info = strsplit(linec,'E');                                                
                        % spliting the string into each element of the string
    
    a1=linec(1:a(1)+3); % a(1) will be position of first 'E', three places after that 
                          the coefficient ends
    a1=str2num(a1);     % coverting the string to numerical value
    
    a2=linec(a(1)+4:a(2)+3); % a(1) will be position of first 'E', four places after 
                               that the new coefficient (a2) will start
    a2=str2num(a2); 
    
    a3=linec(a(2)+4:a(3)+3);
    a3=str2num(a3);
    
    a4=linec(a(3)+4:a(4)+3);
    a4=str2num(a4);
    
    a5=linec(a(4)+4:a(5)+3);
    a5=str2num(a5);
    
    lined = fgetl(f1); % line no: 3 of 4, for the specific species from thermo data file
    b=findstr(lined,'E');  % using 'E' as the refference to get the value of variable
    variable_info = strsplit(lined,'E');
    
    a6=lined(1:b(1)+3);
    a6=str2num(a6);
    
    a7=lined(b(1)+4:b(2)+3);
    a7=str2num(a7);
    
    b1=lined(b(2)+4:b(3)+3);
    b1=str2num(b1);
    
    b2=lined(b(3)+4:b(4)+3);
    b2=str2num(b2);
    
    b3=lined(b(4)+4:b(5)+3);
    b3=str2num(b3);
    
    linee = fgetl(f1); % line no: 4 of 4, for the specific species from thermo data file
    c=findstr(linee,'E');  % using 'E' as the refference to get the value of variable
    variable_info = strsplit(linee,'E');
    
    b4=linee(1:c(1)+3);
    b4=str2num(b4);
    
    b5=linee(c(1)+4:c(2)+3);
    b5=str2num(b5);
    
    b6=linee(c(2)+4:c(3)+3);
    b6=str2num(b6);
    
    b7=linee(c(3)+4:c(4)+3);
    b7=str2num(b7);
    
    R=8.314; % Universal gas constant in k-mole/kg-k
    temp = linspace(local_temp_1,local_temp_3,500); % array of the temperature
    
    % Calling the different function file to calculate h, s and cp
    
    enthalphy= enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,local_temp_2,temp,R);
    entrophy=entrophy_fun(a1, a2, a3, a4, a5, a7, temp, R,local_temp_2, b1, b2, b3, b4, b5, b7);
    specific_heat=specific_heat_fun(a1, a2, a3, a4, a5, temp, R, local_temp_2,  b1, b2, b3, b4, b5);
   
    mkdir(species_name); % this will create the new folder with its species name on it.
    cd(species_name);
    
    figure(1)
    plot(temp,enthalphy,'linewidth',2,'color','r')
    xlabel('Temperature')
    ylabel('Enthalphy')
    title('Temperature vs Enthalphy',species_name)
    grid on
    saveas(figure(1),'Enthalphy.jpg')
    
    figure(2)
    plot(temp,entrophy,'linewidth',2,'color','b')
    xlabel('Temperature')
    ylabel('Entrophy')
    title('Temperature vs Entrophy',species_name)
    grid on
    saveas(figure(2),'Entropy.jpg')
    
    figure(3)
    plot(temp,specific_heat,'linewidth',2,'color','b')
    xlabel('Temperature')
    ylabel('Specific heat')
    title('Temperature vs Specific heat',species_name)
    grid on
    saveas(figure(3),'Specific Heat.jpg')
    
    cd .. % its used so that we can get all the separate folder of all the species
    molecular_weight = molecular_weight_fun(species_name);
    k = sprintf('Molecular weight of %s = %f',species_name,molecular_weight);           
         % '%s' its used to show the string which contains the name if the element.                                                   
         % '%f' (floating value) its used to show the numeric of molecular weight
         % sprintf it will print the molecular weight in the command window
    disp(k)
end

Code of Function File for calculation of thermodynamic properties asked in project:

1) Enthalpy function code file:

% Project 1 - NASA File Parsing - Enthalpy Function Code
function H_Value = enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,local_temp_2,temp,R)
if (temp>local_temp_2)
    H_Value = (a1)+((a2.*temp)./2)+((a3.*(temp.^2))./3) +((a4.*(temp.^3))./4)+((a5.*(temp.^4))./5)+(a6./temp).*(R.*temp);
else
    H_Value = (b1)+((b2.*temp)./2)+((b3.*(temp.^2))./3) +((b4.*(temp.^3))./4)+((b5.*(temp.^4))./5)+(b6./temp).*(R.*temp);
end
end

2) Entropy function code file:

% Project 1 - NASA File Parsing - Entrophy Function Code
function [s]=entrophy_fun(a1, a2, a3, a4, a5, a7, temp, R,local_temp_2, b1, b2, b3, b4, b5, b7)
if temp>= local_temp_2
   s= R*( a1*log(temp) + (a2*temp) + a3*((temp.^2)/3) + a4*((temp.^3)/4) + a5*((temp.^4)/5) + a7);
else
   s= R*( b1*log(temp) + (b2*temp) + b3*((temp.^2)/3) + b4*((temp.^3)/4) + b5*((temp.^4)/5) + b7);
end 
end

3) Specific Heat function code file:

% Project 1 - NASA File Parsing - Specific Heat Function Code
function[cp]=specific_heat_fun(a1, a2, a3, a4, a5,temp , R, local_temp_2,  b1, b2, b3, b4, b5)
if temp>=local_temp_2
    cp = R*(a1 + a2*(temp) + a3*(temp.^2) + a4*(temp.^3) + a5*(temp.^4));
else 
    cp = R*(b1 + b2*(temp) + b3*(temp.^2) + b4*(temp.^3) + b5*(temp.^4));
    
end
end

4) Molecular Weight function code file:

% Project 1 - NASA File Parsing - Molecular weight Function Code
function[mw]=molecular_weight_fun(species_name)
element= ['H','C','N','O','A'];      % all the basic elements used to create the species.
atomic_weight= [1,12,14,16,40];      % molecular weights of all the basic elements used 
                                      to create the species_name
mw=0;                                 % intial molecular weight

for i=1:length(species_name)         % verifying the each element from the species_name

   for  j=1:length(element)           % verifying the each element

    if strcmp(species_name(i),element(j))         % comparing the string (species_name 
                                                    and element)
        mw = mw + atomic_weight(j); % calculating the molecular weight of given species
        a=j; 
    end
   end
   
   n = str2num(species_name(i)); % converting the string to number in species name

   if(n>1)
       mw=mw + atomic_weight(a)*(n-1); % calculating the molecular weight
   end
end
end

Attachment:

1) Screenshot of the window where all the folders are saved.

2) The molecular weight of each species and display it in the command window:

3) Work Space screenshot:

4) Plots for o2 species:

5) Plots for N2 species:

6) Plots for Co2 species:

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.