Challenge

Project 1 - Parsing NASA thermodynamic data

                                                                                                                            Report

AIM:

1] Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.

2] calculate the molecular weight of each species and display it in the command window.

3] Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

4] Plots for o2, N2 and Co2 species and Screenshot of the window where all the folders are saved.

5] Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

FILE PARSING:

            'NASA thermodynamic data file' contains co efficients for polynomial that are used to calculate thermodynamic properties for various chemical species.we are use file parsing to read the data given in NASA thermodynamic data file to extract these co efficient and perform calculation in matlab.

            To parse data means to break it down into parts so that its syntax can be analysed and understood easily.the actual definition of parsing is to spit a file or other input into pieces of data that can be easily stored and manipulated. so in file parsing we spit a string into parts then recognize the parts to convert it into something simpler than string.file parsing is used to read and write data in a file.

the formula to calculate specific heat enthalpy entropy:

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.Also use the LOCAL TEMPERATURE RANGES which is unique for each individual species, not the GLOBAL TEMPERATURE RANGE.

Main matlab code to parse NASA thermodynamic data:

clear all
close all
clc

% reading the thermodynamic data file using 'fopen' command
f1 = fopen('THERMO.dat','r');
% 'fgetl' is used to read the data file line by line.
% first 5 line are header in this global temperature we are not used for
% calculation 

for m = 1:5
    header_line = fgetl(f1)
end
%defining universal gas constant[R in J/mol-k]
R = 8.3145;
% reading all species individually
for k= 1:53
    tline = fgetl(f1);
    A = strsplit(tline,' ');
    species_name = (A{1});
    
    %reading local temperature for each species
    local_low_temperature = str2num(A{end-3});
    local_high_temperature = str2num(A{end-2});
    local_mid_temperature = str2num(A{end-1});

    %reading 1st line of temperature values
    tline = fgetl(f1);
    a = findstr(tline,'E');

    % converting the string of value to number
    a1 = tline(1:a(1)+3);
    a1 = str2num(a1);
    a2 = tline(a(1)+4:a(2)+3);
    a2 = str2num(a2);
    a3 = tline(a(2)+4:a(3)+3);
    a3 = str2num(a3);
    a4 = tline(a(3)+4:a(4)+3);
    a4 = str2num(a4);
    a5 = tline(a(4)+4:a(5)+3);
    a5 = str2num(a5);

    %reading 1st line of temperature values
    tline = fgetl(f1);
    a = findstr(tline,'E');

    % converting the string of value to number
    a6 = tline(1:a(1)+3);
    a6 = str2num(a6);
    a7 = tline(a(1)+4:a(2)+3);
    a7 = str2num(a7);
    a8 = tline(a(2)+4:a(3)+3);
    a8 = str2num(a8);
    a9 = tline(a(3)+4:a(4)+3);
    a9 = str2num(a9);
    a10 = tline(a(4)+4:a(5)+3);
    a10 = str2num(a10);

    %reading 1st line of temperature values
    tline = fgetl(f1);
    a = findstr(tline,'E');

    % converting the string of value to number
    a11 = tline(1:a(1)+3);
    a11 = str2num(a11);
    a12 = tline(a(1)+4:a(2)+3);
    a12 = str2num(a12);
    a13 = tline(a(2)+4:a(3)+3);
    a13 = str2num(a13);
    a14 = tline(a(3)+4:a(4)+3);
    a14 = str2num(a14);

    %defining the temperature range for calculation
    T = linspace(local_low_temperature,local_high_temperature,1000);

    %calculating specific heat,enthalpy,entropy,molecular weight
    Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,T,local_mid_temperature);

    H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,T,local_mid_temperature);

    S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temperature);

    M = molecular_weight(species_name);

    %creating seperate directory to store graph and value of each
    %coefficient of species
    cd('E:\Aditya\MATLAB\FILE_PARSING');
    mkdir(species_name);
    cd(species_name);

    %writting molecular weight to a text file
    M_file = fopen('molecular_weight.txt','w');
    fprintf(M_file,'molecular weight of %s is %d',species_name,M);
    fclose(M_file)
    
    %plotting temperature vs specific heat graph and saving
    figure(1)
    plot(T,Cp,'LineWidth',2,'Color','r');
    xlabel('temperature[k]');
    ylabel('specific heat[KJ]');
    title(sprintf('temperature vs specific heat of %s',species_name))
    grid on
    saveas(figure(1),'specific heat.jpg');


    %plotting temperature vs enthalpy graph and saving
    figure(2)
    plot(T,H,'LineWidth',2,'Color','b');
    xlabel('temperature[k]');
    ylabel('enthalpy[KJ/mol]');
    title(sprintf('temperature vs enthalpy of %s',species_name))
    grid on
    saveas(figure(2),'enthalpy.jpg');


    %plotting temperature vs entropy graph and saving
    figure(3)
    plot(T,S,'LineWidth',2,'Color','g');
    xlabel('temperature[k]');
    ylabel('entropy[KJ/mol-k]');
    title(sprintf('temperature vs entropy of %s',species_name))
    grid on
    saveas(figure(3),'entropy.jpg');

    % changing directory to original directory
    cd('E:\Aditya\MATLAB\FILE_PARSING');
end

 explanation of code:

  • the code start with clear all,close all ,clc.
  • then to open the THERMO.dat data file fopen command is used and reading permission is provided.
  • then to read the data line by line fgetl command is used.
  • then for loop used for skipping the header line.
  • after that give universal gas constant R value as input.and for loop is used to reading all species individually. 
  • in for loop the strsplit command used to split the string at a specific location.
  • all the species are saved in species_name variable.
  • the str2num command is used to convert string value of temperature into actual/intiger number and local temp.value is extracted.
  • to read the value of coefficients from the procceding three line fgetl command is used along with findstr command to find a comman term in the string here is E is given input as comman term
  • the comman term is found in order to seperate the value in the string and str2num is used to extract those value.
  • in this way all the 14 coefficients are calculate and stored in a variable a1 to a14
  • the local temperature range for calculation is defined for 1000 value between lower temp.to higher temp.by using linspace command.
  • after that for calculation of specific heat,enthalpy,entropy,and molecular weight we use the seperate function.
  • after that for creating a seperate directory to store the all species graph and all molecular weight value.
  • then to save all output to a seperate folder first the folder is changed using cd command which takes the folder address as input to change folder.the mkdir command makes a new folder for each species and cd will again change the folder to that perticular species name to save the output.
  • after that we new create M_file to sto store all molecular weight value in .txt format.
  • after that we plot the temperature vs specific heat using plot command.also we give xlabel and ylabel to xaxis and yaxis.also give title for plot with perticual species name by using sprintf command and finally save the plot in .jpg format by using saveas command.
  • after that we plot the temperature vs enthalpy using plot command.also we give xlabel and ylabel to xaxis and yaxis.also give title for plot with perticual species name by using sprintf command and finally save the plot in .jpg format by using saveas command.
  • after that we plot the temperature vs entropy using plot command.also we give xlabel and ylabel to xaxis and yaxis.also give title for plot with perticual species name by using sprintf command and finally save the plot in .jpg format by using saveas command.
  • at the end directory is change to original directory by using cd command.

Defining function for calculating specific heat.

%defining function for specific heat.
function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,T,local_mid_temperature)

if T >= local_mid_temperature
    Cp = R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4));
else
    Cp = R*(a8+(a9*T)+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4));
end
end

explanation of code defining fuction:

  • formula used to define specific heat:

                       Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,T,local_mid_temperature)           

  • in the function "if else loop" is used as there are 14 co-efficients in which the first 7 co-efficients are high temperature co-efficients and second 7 co-efficients are low temperature co-efficients.
  • so to calculating the specific heat of the species based on the formula the "if else loop" is used in such a way that if the temperature is above the local medium temperature of the respective species the function will take high temperature co-efficients and if the value is below the local medium temperature the function will take the low temperature co-efficients to perform the calculation.

Defining function for calculating enthalpy.

%defining function for enthalpy.
function H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,T,local_mid_temperature)

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
end

explanation of code defining fuction:

  • formula used to define ethalpy:

                         H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,T,local_mid_temperature)

  • in the function "if else loop" is used as there are 14 co-efficients in which the first 7 co-efficients are high temperature co-efficients and second 7 co-efficients are low temperature co-efficients.
  • so to calculating the enthalpy of the species based on the formula the "if else loop" is used in such a way that if the temperature is above the local medium temperature of the respective species the function will take high temperature co-efficients and if the value is below the local medium temperature the function will take the low temperature co-efficients to perform the calculation.

Defining function for calculating entropy.

%defining function for entropy.
function S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temperature)

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
end

explanation of code defining fuction:

  • formula used to define entropy:

                       S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temperature)

  • in the function "if else loop" is used as there are 14 co-efficients in which the first 7 co-efficients are high temperature co-efficients and second 7 co-efficients are low temperature co-efficients.
  • so to calculating the entropy of the species based on the formula the "if else loop" is used in such a way that if the temperature is above the local medium temperature of the respective species the function will take high temperature co-efficients and if the value is below the local medium temperature the function will take the low temperature co-efficients to perform the calculation.

Defining function for calculating molecular weight.

%defining function for molecular weight

function M = molecular_weight(species_name)
%molecular_weight(amu) molar_mass(gram/mol)
%defining atoms and their atomic weight
Atoms = ['O','H','C','N','Ar'];
Atomic_weight = [16,1,12,14,40];

M=0;
%comparing Atoms to the read data of species name and then calculating
%molecular weight
for i= 1:length(species_name)
    for j = 1:length(Atoms)
        if strcmp(species_name(i),Atoms(j))
            M = M + Atomic_weight(j);
            place = j;
        end
    end
    n = str2num(species_name(i));
    if (n>1)
        M = M + Atomic_weight(place)*(n-1);
    end
end
%displaying molecular weight in command window
fprintf('molecular weight of %s : ',species_name);
fprintf('%f',M);
disp(' ')
end

explanation of molecular weight function: 

  • the molecular weight function will take "species _name" as its input then the "atoms" along with their respective "atomic weight" is defined and initially value of M is stored as 0 then two for loop are used one for the data stored in species _name and another for the atoms that we defined.
  • the first for loop runs for entire length of species name that has been determined by the for loop in the main programthe second for loop runs for entire length of atoms array.
  • an if loop is used where at first the species _name and atoms is compared using strcmp. if the comparison is true then atomic of that particualar atom is added to the M variable at every place where atoms is present in the species.
  • another if loop is used to multiply the value (which is adjecent to the particular atom and whichindicades the number of that particular atom such as H2) to atomic weight.

Result

Output plot:

1] plot for "O2" species:

  • Temperature vs Specific heat:

                                     

  • Temperature vs enthalpy:

                                   

  • Temperature vs entropy:

                                     

2] plot for "N2" species:

  • Temperature vs Specific heat:

                                   

  • Temperature vs enthalpy:

                                  

  • Temperature vs entropy:

                                     

3] plot for "CO2" species:

  • Temperature vs Specific heat:

                                 

  • Temperature vs enthalpy:

                                 

  • Temperature vs entropy:

                                

screenshot of the window where all species folder are saved:

                                                 

Errors:

1] error in calculating enthalpy function.

                                               

solution:

here error show that matrix dimension must agree bcoz here i forgot the dot product. 

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.