Challenge

Project 1 - Parsing NASA thermodynamic data

PARSING NASA THERMODYNAMIC DATA

AIM:

To write a MATLAB program to extract 14 coefficients from the matlab extras THERMO.dat file to calculate enthalpy, entropy and specific heat, for all the species in the data file. Also by plotting graphs of every species and find the molecular mass of the species.

OBJECTIVE:

  1. To write a function that extracts 14 co-efficient for a given species from the given information.
  2. To write functions that calculate the enthalpy, entropy, and specific heat using NASA polynomials for the given species.
  3. To write a function that plots the Cp, Enthalpy, and Entropy of input species.
  4. To write a function that calculates the molecular weight of each species.

INTRODUCTION:

File parsing is done to extract data from the file, it should be generic as possible to fetch the data and by using this we calculate enthalpy, entropy & specific heat for different species and condition is that the FIRST 7 COEFFICIENTS are HIGH TEMPERATURE COEFFICIENTS & SECOND 7 COEFFICIENTS are LOW TEMPERATURE COEFFICIENTS.

Parsing can be defined as of breaking the data block into smaller blocks by following set of orders. so that it can be more easily interpreted, managed and sent to a computer NASA came up with the polynomials that can be used to evaluate thermodynamic properties such as Cp, Enthalpy, and Entropy using polynomials. They have also documented the co-efficient that are required to evaluate these polynomials for 1000+ species.

The formulas to calculate Cp, Enthalpy, and Entropy are given below:

Where,

  • al, a2, a3, a4, a5,.... Are numerical coefficients,
  • T is the temperature in kelvin (K),
  • R is the universal gas constant (J/k-mol.k),
  • Cp is the specific heat(J/k-mol.k),
  • H is the enthalpy(J),
  • S is the entropy(J/K).

SOME IMPORTANT DEFINATIONS:

  • Specific heat = The specific heat of a substance is the amount of energy it takes to heat a specific mass of that substance through a specific temperature gradient. In SI systems, this is joules per kelvin per kg. So for instance it takes roughly 4184 joules to heat 1 kg of water (at room temperature) by 1 kelvin. (1 kelvin is the same temperature delta as 1 degree C). The specific heat of water at room temperature is therefore 4184 j/kg.K. Note that specific heat is not constant - it changes with temperature, so it may take more energy to heat a hot substance through 1 degree from 200 to 201 degrees as it does to heat the same substance through 1 degree from 20 to 21 degrees. The specific heat of air is about 718 j/kg.K. So if you heat 1 kg of air from 20 to 100 degrees you have used a lot less energy than you meed to heat 1 kg of water through the same temperature difference. This is why we use hot water bottles, not hot air bottles. There isn't enough energy in a hot air bottle to last the night.
  • Enthalpy = Enthalpy is property of thermodynamics which is defined as the sum of internal energy of the system and product of pressure and volume of the system. It is denoted by 'H’ the above statement can be interpreted as, H=U+PV

SOME IMPORTANT POINTS REGARDING ENTHALPY UNIT:

  1. Enthalpy is measured in term of ‘Joule’ according to International system of unit (SI).
  2. In FPS, it is measured in BTU.
  3. In CGS, it is measured in calorie.
  • Entropy = Entropy comes in a bewildering number of flavors, interpretations, and misconceptions — from energy flow to disorder to information content — but | will assume that you refer to Thermodynamic Entropy, and mention some of the other “entropy definitions” only in passing.

Entropy S is a consequence of the Second Law of Thermodynamics, one form of which states that “In a closed or fully isolated system, unconstrained energy spontaneously tend to disperse, to spread out, from more concentrated forms to less concentrated forms.” That is, energy spontaneously flows from hot to cold, and never the other way around.

Entropy is not a material substance: S is a number, a measure of energy dispersal, as energy over temperature, in SI units as joule per kelvin (J/K) or — in base units — kg m22 s—2—2 K-1-1 (when measured as the “entropy content” of some substance, as S m— 1H, where m is either kg or mol). Simply put, entropy is a measure of the amount of “unusable energy” within a closed or isolated system (for example, our Universe).

To me, that gives us the “easiest” definition: Entropy is a measure — in units of joule per kelvin — of how energy in a closed or isolated system spontaneously disperses over time, and that total entropy in the Universe will always increase. Local and temporary decreasing entropy — as for example in Photosynthesis and Cellular Respiration— is always more than adequately compensated for by an increase of entropy elsewhere.

CODE:

clear all
close all
clc
% nasa data file parsing 
% extracting the coefficient of thermodynamics

dt=fopen('thermo.dat','r');
% readng the first 5 lines of file (not for use)
first_line = fgetl(dt); %sreading the 1st line from data
global_temp = fgetl(dt); %reading 2nd line from data
temp_range = strsplit(global_temp,' '); %splitting 2nd line using blank space as delimiter
comments1 = fgetl(dt); %reading 3rd line from data
comments2 = fgetl(dt); %reading 4th line from data
comments3 = fgetl(dt); %reading 5th line from data

for i = 1:53
    % Extracting name of gas species
    A = fgetl(dt); % Reading the sixth line
    species = strtok(A); % Reading the species name
    % Extracting co-efficient of gas species
    B = fgetl(dt);
    b = findstr(B,'E');
    a1 = B(1:b(1)+3); % Assigning 'a' variable for the temperature co-efficients
    a1 = str2num(a1);
    a2 = B(b(1)+4:b(2)+3);
    a2 = str2num(a2);
    a3 = B(b(2)+4:b(3)+3);
    a3 = str2num(a3);
    a4 = B(b(3)+4:b(4)+3);
    a4 = str2num(a4);
    a5 = B(b(4)+4:b(5)+3);
    a5 = str2num(a5);
    
    % second line of species coefficeint
    C = fgetl(dt);
    c = findstr(C,'E');
    a6 = C(1:c(1)+3);
    a6 = str2num(a6);
    a7= C(c(1)+4:c(2)+3);
    a7 = str2num(a7);
    a8 = C(c(2)+4:c(3)+3);
    a8 = str2num(a8);
    a9 = C(c(3)+4:c(4)+3);
    a9 = str2num(a9);
    al0 = C(c(4)+4:c(5)+3);
    a10 = str2num(al0);
    
    % third line of species coefficent
    D = fgetl(dt);
    d = findstr(D,'E');
    a11 = D(1:d(1)+3);
    a11 = str2num(a11);
    a12 = D(d(1)+4:d(2)+3);
    a12 = str2num(a12);
    a13 = D(d(2)+4:d(3)+3);
    a13 = str2num(a13);
    a14 = D(d(3)+4:d(4)+3);
    a14 = str2num(a14);
    
    %Creating a vector of 500 evenly spaced temperature points
    t1=200;
    t2=3500;
    t3=1000;
    T = linspace(t1,t2,500);
    
    % gas constant
    R=8.134;
    for i= 1:length(T)
        if T(i)>t3
            Cp(i) = (a1 + a2.*T(i) + a3.*T(i).^2 + a4.*T(i).^3 + a5.*T(i).^4)*R;
            H(i) = (a1+(a2.*T(i))/2 + (a3.*T(i).^2)/3 + (a4.*T(i).^3)/4 + (a5.*T(i).^4)/5 + (a6./T(i)))*R.*T(i);
            S(i) = a1.*log(T(i)) + a2.*T(i) + (a3.*T(i)^2)/2 + (a4.*T(i)^3)/3 + (a5.*T(i)^4)/4 + a7;
        else
            Cp(i) = (a8 + a9.*T(i) + a10.*T(i).^2 + a11.*T(i).^3 + a12*T(i).^4)*R;
            H(i) = (a8 + (a9.*T(i))/2 + (a10.*T(i).^2)/3 + (a11.*T(i).^3)/4 + (a12.*T(i).^4)/5+(a13./T(i)))*R.*T(i);
            S(i) = a8.*log(T(i)) + a9.*T(i) + (a10.*T(i)^2)/2 + (a11.*T(i)^3)/3 + (a12.*T(i).^4)/4 + a14;
        end
    end
    
    mkdir(['C:Desktop',species]);
    
    % Plotting specific heat vs temperature
    figure(1)
    plot (T,Cp,'color','b','linewidth',2)
    xlabel('Temperature (K)')
    ylabel('Specific Heat (J/K Kg)')
    title('Specific Heat vs Temperature')
    grid on

    % Plotting enthalpy vs temperature
    figure(2)
    plot (T,H, 'color', 'r','linewidth',2)
    xlabel('Temperature (K)')
    ylabel('Enthalpy (J/K)')
    title('Enthalpy vs Temperature' )
    grid on

    % Plotting entropy vs temperature
    figure(3)
    plot (T,S, 'color', 'g','linewidth',2)
    xlabel('Temperature (K)')
    ylabel('Entropy (J/K)')
    title('Entropy vs Temperature')
    grid on
    
    cd(['C:Desktop',species] );
    saveas(figure(1),'Cp vs Temp.jpg')
    saveas(figure(2),'Enthalpy vs Temp.jpg')
    saveas(figure(3),'Entropy vs Temp.jpg')
    
    % Molecular weight calculation
    elements = ['H','C','O','N','A']; % species elements vector
    atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948]; % the atomic weight value of respective species
    mol_wt = 0; % Declaring molecular weight to be '0'
    for l = 1:length(species)
        for m = 1:length(atomic_weight)
            if strcmp(species(l), elements(m))
                mol_wt = mol_wt + atomic_weight(m);
                position = m;
            end
            n = str2double(species(l));
            if n>1
                mol_wt = mol_wt + atomic_weight(position)*(n-1);
            end 

            % Printing the molecular weight of the gas species
            sprintf('Molecular Weight of %s is %f',species,mol_wt)
        end
    end
end
fclose(dt)

CODE EXPLANATION:

  • The program begins with clear all , close all, cle which clears all the graphs, stored data previously saved.
  • The code is started with opening the file using command ‘fopen(thermo.dat,r)’ ,the ‘’ is used to read the file (we can aslo use ‘W' which write the file).
  • Next, 5 lines are includes the ‘fgetl’ command which read the each 5 lines text or integers (all the lines are stored or read n the string format). The first five seems to irrelevant as we need only the coefficient value and species name.
  • Now, we will create a loop which will extract/read the species name and its coefficients value. Starting with the species name, we type fgetl(dt) nd denoting it with the variable name A. It will read the sixth line which contains the species along with the temperature range.
  • Next line we use strtok which parses str from left to right, using whitespace characters as delimiters, and returns part or all of the text in species. That means we will get the all the species name under the species vector.
  • Now, we are going to extract the co-efficient for the gas species in which the co-efficient locations are like in the 2 row,next 5 in the 3 row and the rest in the 4 row.
  • We are going to extract these values similarly like we did for finding out the local temperatures by using the findstr and str2num commands and store it in the variables al to al4. For extracting we are going to locate the string ‘E’ which is present in the each co- efficient value and so it will be followed for all the co-efficients values in the same line and then we can proceed for the next lines.
  • The values from al to a7 represent the high temperature co-efficients and the values from a8 to al4 represent the low temperature co-efficients.
  • We will define the temperature as t1,t2,t3 equals to 200,3500 and,1000 respectively from which the t1 is low temperature , t2 is high temperature and t3 is mid temperature. Temperature range values from tl to t2 is assigned using the linspace command.
  • And we will define the gas constant R=8.134 .
  • Again we use the for loop command is used to calculate the values of specific heat, enthalpy and entropy and the if-else command is also used to find out the required values if the temperature below the mid temperature or above the mid temperature.
  • Then to create a new folder with the species we are going to use the mkdir command so that a new folder is created in the main folder.
  • Further, we plot the graph from the values obtained from the above step .The plots for specific heat vs temperature, enthalpy vs temperature and entropy vs temperature is plotted and the labelling of axis and the naming of the title.
  • Cd command is used to change the directory to current folder mentioned and all the plots is saved in jpg format in respective species folder using saveas command.

Molecular weight calculations:

  • For the molecular weight calculation we basically add the atomic weight all the species atom. In order to calculate the molecular weight of the species, we declare the species elements in the form of cell array. Their atomic mass is declared in the form of array.
  • Then initially molecular weight is declared as zero.
  • Now, for loop is used to detect if there are more than one species in the variable species. Stremp command is used to compare and match the elements and species for further process.
  • After the species name is matched and its atomic value added , it is stored under the variable name position =m .
  • Sincle there can be more than two molecules of same elements, that remaining elements is calculated by first converting the species to numer using strtodouble under n variable .
  • Now, if loop(n>1) is used to multiple the (n-1) to previous stored position value. Since the one molecule of element is already used the rest molecules are simply multiplied to previous by n-1.
  • Towards the end we use sprintf to print the species name and its weight and all the loop is closed and followed by fclose to close the file.

OUTPUT:

1. O2 PLOTS:

2. N2 PLOTS:

3. CO2 PLOTS:

  • All Folders were saved in Desktop

MOLECULAR WEIGHT:(values are round off)

  1. Molecular Weight of O: 15
  2. Molecular Weight of O2: 30
  3. Molecular Weight of H: 1
  4. Molecular Weight of H2: 2
  5. Molecular Weight of OH: 16
  6. Molecular Weight of H2O: 17
  7. Molecular Weight of HO2: 31
  8. Molecular Weight of H2O02: 32
  9. Molecular Weight of C: 12
  10. Molecular Weight of CH: 13
  11. Molecular Weight of CH2: 14
  12. Molecular Weight of CH2(S): 14
  13. Molecular Weight of CH3: 15
  14. Molecular Weight of CH4: 16
  15. Molecular Weight of CO: 27
  16. Molecular Weight of CO2: 42
  17. Molecular Weight of HCO: 28
  18. Molecular Weight of CH2O: 29
  19. Molecular Weight of CH2OH: 30
  20. Molecular Weight of CH30: 30
  21. Molecular Weight of CH3OH: 31
  22. Molecular Weight of C2H: 25
  23. Molecular Weight of C2H2: 26
  24. Molecular Weight of C2H3: 27
  25. Molecular Weight of C2H4: 28
  26. Molecular Weight of C2H5: 29
  27. Molecular Weight of C2H6: 30
  28. Molecular Weight of CH2CO: 41
  29. Molecular Weight of HCCO: 40
  30. Molecular Weight of HCCOH: 41
  31. Molecular Weight of H2CN: 28
  32. Molecular Weight of HCN: 27
  33. Molecular Weight of HNO: 30
  34. Molecular Weight of N: 14
  35. Molecular Weight of NNH: 29
  36. Molecular Weight of N20: 43
  37. Molecular Weight of NH: 15
  38. Molecular Weight of NH2: 16
  39. Molecular Weight of NH3: 17
  40. Molecular Weight of NO: 29
  41. Molecular Weight of NO2: 44
  42. Molecular Weight of HCNO: 42
  43. Molecular Weight of HOCN: 42
  44. Molecular Weight of HNCO: 42
  45. Molecular Weight of NCO: 41
  46. Molecular Weight of CN: 26
  47. Molecular Weight of HCNN: 41
  48. Molecular Weight of N2: 28
  49. Molecular Weight of AR: 39
  50. Molecular Weight of C3H8: 44
  51. Molecular Weight of C3H7: 43
  52. Molecular Weight of CH3CHO: 43
  53. Molecular Weight of CH2CHO : 42

CONCLUSION:

We have successfully parsed/extracted the coefficient value of each species from the NASA thermodynamic file and plotted the respective species graph for the specific heat, entropy and enthalpy followed by which we calculated the molecular mass also.

REFERENCES:

https://in.mathworks.com/help/matlab/ref/strtok.html

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.