Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:
To write parsing of NASA thermodynamic file and to find the molecular weight of O2,N2,CO2.
OBJECTIVE:
•	To write a function that extracts the 14 co-efficients and calculates the entropy ,enthalpy and specific heats for all the species in the data file.
•	To calculate the molecular weight of each species and display that in the command window.
•	To plot the Cp,Enthalpy,and Entropy for the local temperature range (low temperature : High temperature) specific for each species.
•	To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
THEORY:
FILE PARSING
File parsing is basically refers to how information can be read from particular file or text file or any type of file and how that information can be used in a computer program . Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data . This aims at facilitating performing some kind of transformation on the data.
SPECIFIC HEAT:
The specific heat of a substance , especially a gas , may be significantly higher when it is allowed to expand as it is heated (specific heat at constant pressure ) than when is heated in a closed vessel that prevents expansion (specific heat at constant volume ). These two values are usually denoted by Cp and Cv , respectively .  The quotients of the heat capacity ratio is
                                                Gamma = Cp/Cv
ENTHALPY:
Enthalpy , the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Enthalpy is an energy-like property or state function, it has the dimensions of energy, and its value is determined entirely by the temperature,pressure and composition of the system and not by its history . In symbols the enthalpy H equals the sum of the internal energy , E and the product of the pressure P and volume V of the system,
                                            H = E + PV
MOLECULAR WEIGHT:
The sum of the atomic masses of all atoms in a molecule , based on a scale in which the atomic masses of hydrogen , carbon, nitrogen and oxygen are 1,12,14 and 16 respectively . For example , the molecular weight of water , which has two atoms of hydrogen and one atom of oxygen is 18 (i.e., 2+16) , Also called molecular mass.
FORMULA FOR EQUATING THE FUNCTION :
The formulaes are shown in images as follows,
              
             
 Here R is the universal gas constant , T is the temperature 
 R – universal gas constant ( 8.314 J/(mol-K))
 Cp – specific heat 
H – Enthalpy
S -  Entropy
 Where a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14 are the coefficients of each species.
The functions are assigned individually for calling in the main program to calculate molecular weight.
SYNTAX USED IN THIS PROGRAM
Some of syntax that are used in the program are given below,
             f1 = fopen(‘THERMO.dat’.’r);
To open the data that are given in challenges and ‘r’ syntax is used for read the file . check whether the program name is initiate as same of file data ‘THERMO.dat’
              Line_o = fgetl(f1);
This syntax is used to get the first line of the data.
              Fgetl(f1)

STEP BY STEP EXPLANATION OF MAIN PROGRAM:
•	First step is to read the file that are given in challenges THERMO.data by using the syntax of f1 = fopen(‘THERMO.dat’,’r’).
•	Now file are assigned as input data contained various species with different coefficients.
•	Fgetl(f1) syntax is ued for calling the first line of thermo.data so that line are called . For loop executed for skip the first three lines.
•	We want to split the coefficient by the numerical value so that string split syntax is used  strsplit(line_1,’ ‘).
•	Global temperature (low,mid,high) are also splitted in that data.
•	To separate each  co-efficients in species Find string command is used
                                            Coeff = fgetl(f1);
                                            A = findstr(coeff,’E’)’
•	By applying this string the coefficient value . Then formula are assign for calculating the next coefficient in same species a1 = 1:12+3.
•	A2 = str2double(coeff(a1 + 4:a2+3)) now a2 are calculated by this equation here str2double is refer as string2num that syntax seperates the coefficient as numerical value.
•	Same process are followed for 14 coefficients in species .
•	Then open an loop for finding the values of all 53 elements to calculate molecular weight.
•	Functions are assigned in different tabs by using formulae were given in challenges.
•	Where R is the universal gas constant (8.314 J / (mol-K)), T is the temperature in function.
FUNCTION PROGRAM:
SPECIFIC HEAT(C_P)
% Function of Specific Heat(c_p)

function[c_p] = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,local_mid_temperature,T)
R = 8.314;
if(T>=local_mid_temperature)
    c_p = R.*(a1+(a2*T)+(a3*(T.^2))+(a4*(T.^3))+(a5*(T.^4)));
else
    c_p = R.*(a8+(a9*T)+(a10*(T.^2))+(a11*(T.^3))+(a12*(T.^4)));
end

ENTHALPY(H)
% Function of Enthalpy(H)

function[h] = enthalpy_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,local_mid_temperature,T)
R = 8.314;
if(T>=local_mid_temperature)

    h = R.*T.*(a1+a2*(T./2)+(a3*(T.^2)./3)+(a4*(T.^3)./4)+(a5*(T.^4)./5)+(a6./T));
else
    h = R.*T.*(a8+a9*(T./2)+(a10*(T.^2)./3)+(a11*(T.^3)./4)+(a12*(T.^4)./5)+(a13./T));
end

ENTROPY(S)
% Function of Entropy(S)

function[s] = entropy_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,local_mid_temperature,T)
R = 8.314;
if (T>=local_mid_temperature)

    s = R.*(a1*log(T)+(a2*(T))+(a3*(T.^2)/2)+(a4*(T.^3)/3)+(a5*(T.^4)/4)+(a7));
else
    s = R.*(a8*log(T)+(a9*(T))+(a10*(T.^2)/2)+(a11*(T.^3)/3)+(a12*(T.^4)/4)+(a14));
end

MOLECULAR WEIGHT
% Function of molecular Weight 
function [Mol_wt] = molecular_weight(species_name)


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

atomic_mass = [1.00794 12.0107 14.0067 15.9994 39.948];

Mol_wt = 0;
z = 0;
for i = 1:length(species_name)
    for j = 1:length(elements)
        if strcmpi(species_name(i),elements(j))
        Mol_wt = Mol_wt+atomic_mass(j);
         z = j;
        end
    end

n = str2double(species_name(i));
   if (n>1)
    Mol_wt = Mol_wt+(atomic_mass(z)*(n-1));
   end
end
M = Mol_wt;
end



MAIN PROGRAM OF THERMODYNAMIC PARSING DATA :
clear all
close all
clc

% Input data

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

% line_0 = fgetl(f1);
% line_1 = fgetl(f1);
% split = strsplit(line_1,' ');
q = fgetl(f1);
split = strsplit(fgetl(f1), ' ');
low_temperature = str2double(split{2});
mid_temperature = str2double(split{3});
high_temperature = str2double(split{4});
fgetl(f1)
fgetl(f1)
fgetl(f1)

%for calculating the species 
%for loop for total coefficients

for i = 1:53
    line_2 = fgetl(f1);
    S = strsplit(line_2,' ');
    species_name = S{1};

    % Split the local Temperatures

    T = strfind(line_2,'G');
    temp_values = line_2(T+4:length(line_2));
    split_temp = strsplit(temp_values,' ');
    local_low_temperature = str2double(split_temp{1});
    local_mid_temperature = str2double(split_temp{3});
    local_high_temperature = str2double(split_temp{2});

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

    % Calculate the coe_eff value
    % a(1) = (1:12+3)
    
   a1 = str2double(coeff(1:a(1)+3));
   a2 = str2double(coeff(a(1)+4:a(2)+3));
   a3 = str2double(coeff(a(2)+4:a(3)+3));
   a4 = str2double(coeff(a(3)+4:a(4)+3));
   a5 = str2double(coeff(a(4)+4:a(5)+3));

   % coeff of 5 to 10

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

   a6 = str2double(coeff_1(1:a(1)+3));
   a7 = str2double(coeff_1(a(1)+4:a(2)+3));
   a8 = str2double(coeff_1(a(2)+4:a(3)+3));
   a9 = str2double(coeff_1(a(3)+4:a(4)+3));
   a10 = str2double(coeff_1(a(4)+4:a(5)+3));

   %coeff of 11 to 14
   coeff_2 = fgetl(f1);

   a11 = str2double(coeff_2(1:a(1)+3));
   a12 = str2double(coeff_2(a(1)+4:a(2)+3));
   a13 = str2double(coeff_2(a(2)+4:a(3)+3));
   a14 = str2double(coeff_2(a(3)+4:a(4)+3));

   R = 8.314;
   T = linspace(local_low_temperature,local_high_temperature,200);

   for j = 1:length(T)
       [c_p] = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,local_mid_temperature,T);
       [h] = enthalpy_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,local_mid_temperature,T);
       [s] = entropy_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,local_mid_temperature,T);
   end


   req_species = ["O2","N2","CO2"];
   if(species_name==req_species(1)||species_name==req_species(2)||species_name==req_species(3))
       mkdir(species_name)
       cd(species_name)

       % plot specific_heat vs Temperature
       figure(1)
       plot(T,c_p)
       title('specific heat vs Temperature')
       xlabel('Temperature[k]')
       ylabel('specific heat [J/mol]')
       image = getframe(gcf);
       imwrite(image.cdata,'cpvsT.png')

       %plot Enthalpy vs Temperature
       figure(2)
       plot(T,h)
       title('Enthalpy vs Temperature')
       xlabel('Temperature [k]')
       ylabel('Enthalpy')
       image = getframe(gcf);
       imwrite(image.cdata,'hvsT.png')

       % Plot Entropy vs temperature
       figure(3)
       plot(T,s)
       title('Entropy vs Temperature')
       xlabel('Temperature [k]')
       ylabel('Entropy')
       image = getframe(gcf);
       imwrite(image.cdata,'SvsT.png')

     cd('C:\Users\shaum\Desktop\file_parsing')
     
   end
[Mol_wt] = molecular_weight(species_name);
K = sprintf('%s mol_wt = %f',species_name,Mol_wt);
req_species = ["O2","N2","CO2"];
if(species_name==req_species(1)||species_name==req_species(2)||species_name==req_species(3))
  disp(K)
end
end



  
SOLVING THE STEPS OF ERRORS WHILE PROGRAMMING:
•	Change the path of the folder in molecular weight in desktop then the error is solved
•	The matrix values are exceeds in (4) , In that I give a matrix value of a4 and the error is solved

CONCLUSION:
The results are obtained by running the program . File parsing is successfully done and the plots are calculated and molecular weights  of 53 are calculated. 
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.