Challenge

Parsing NASA thermodynamic data

NASA Thermodynamics Data File Parsing

- 1. Aim: Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file - NASA thermodynamics data.

- 2. Objectives:

- Calculate the specific heat, enthalpy and entropy using the data file.

- Code a function to find the molecular weight of all the species.

- Plot the graph of specific heat, enthalpy and entropy vs temperature.

- Extracting the data line by line as assigning to required variables.

- 3. Method

- In order to extract the necessary data number of steps are need to be followed. The first step, is to open the file, reading it and saving this entire data into a variable which can be used again and again as per requirement.

- For this purpose "open" command is used. Depending upon the requirement either we can read the file or write in the file and it depends upon the code. As shown below ( 'r' ) is added which means this file needs to be read. The syntax for this command is shown below.

fileID = fopen(filename)

- As the file contains several string and numbers we need to assign these to different variables as per requirement.

- In order to read a line or data from the file " fgetl " command is used. This command takes the 1st line and we can save it in the respected variable. The syntax for the command is shown below.

tline = fgetl(fileID)

- In order to get the values we need to separate those and take each value out. However, while doing this these form a cell array, which is a special array which takes values of different sizes and types. In order to do this, two commands are used, one is " strsplit " and " str2double " which takes the values and convert them into integers, which then need to save in the respected variables. The syntax for these commands is as below.

- In this code, the delimiter is an empty space (' '), then the split command will split the values wherever there is a empty space.

C = strsplit(str,delimiter) splits str at the delimiters specified by delimiter.
X = str2double(str)

- X= str2double(str) converts the text in str to double precision values. str contains text that represents real or complex numeric values. str can be a character vector, a cell array of character vectors, or a string array. If str is a character vector or string scalar, then X is a numeric scalar. If str is a cell array of character vectors or a string array, then X is a numeric array that is the same size as str.

- In order to take the values from cell array a {} is used and not a ().

- While loop:

- In order to run a loop, iterate it a number of time and perform the same operations again and again while loop is used. The loop is iterated until the condition is true.

while expression
    statements
end

- Folder:

- Creating a folder and saving all the images is one requirement of this project and to do so matlab has a function called " mkdir ". This is used to create a new folder.

- Here a folder named 'species' is created and all the plots for all the species are stored in that folder.

mkdir folderName

- Change directory:

- In order to change the current folder and save the files change directory ( cd ) is used.

cd
cd newFolder
oldFolder = cd(newFolder)

- In order to save the images to specified file format " saves " command is used. The syntax for this is shown below.

saveas(fig,filename)
saveas(fig,filename,formattype)

- To calculate the molecular weight, a function is formed which takes the species string as an input and calculates the molecular weight. Here two for loops are used. The 1st for loop will hold the species name and second for loop will hold the atomic weight.

- The function takes only 1 input which is the species_name and returns the values in MM.

- A variable n is introduced which the integer values of species_name(i), in case, if this n value is greater than 1 then another condition is there which will calculate the MM.

- Formulas:

clear all
close all 
clc
% opening and reading the thermo.dat file 
f1 = fopen('THERMO.dat','r');
header = fgetl(f1);
% reading low, medium, high temperature, here temp is string, convert it
% into integer
% strsplit splits the temp variable and the split is depend upon the input
% in ' ', which is in this case is a space' '. This forms a cell array
% which contains the data of varying types and sizes. Global temperature
% values are taken from this array.
temp = fgetl(f1);
temp_int = strsplit(temp,' ');
global_low_temp = str2double(temp_int{2});
global_mid_temp = str2double(temp_int{3});
global_high_temp = str2double(temp_int{4});

% next 3 lines are comments which can be skipped
comment1 = fgetl(f1);
comment2 = fgetl(f1);
comment3 = fgetl(f1);

%In order to extract the local temperature valuesagain a cell array is
%formed from where the low,mid,high value of temperature are taken.  Here a
%while loop is used to extract all the values however a for loop can be
%used as well.
x = 1;
while x <= 53
% coefficients of mono automic oxygen
species = fgetl(f1);
local_temp = strsplit(species,' ');
species_name = (local_temp{1});
local_low = str2double(local_temp{length(local_temp)-3});
local_high = str2double(local_temp{length(local_temp)-2});
local_mid = str2double(local_temp{length(local_temp)-1});

% coefficients of line 1
coe_line_1 = fgetl(f1);
a = strfind(coe_line_1,'E');
% strfind will take the coe_line_1 as input & it will find all the 'E' and
% it will form a array which contains all the values of E i.e the position
% at which E is there.
% a(1) is the position where 1st E is there and likewise.  to extract
% coefficient value the following method is used.
c1 = str2double(coe_line_1(1:a(1)+3));
c2 = str2double(coe_line_1(a(1)+4:a(2)+3));
c3 = str2double(coe_line_1(a(2)+4:a(3)+3));
c4 = str2double(coe_line_1(a(3)+4:a(4)+3));
c5 = str2double(coe_line_1(a(4)+4:a(5)+3));

%coefficients of line 2
coe_line_2 = fgetl(f1);
a2 = strfind(coe_line_2,'E');
c6 = str2double(coe_line_2(1:a2(1)+3));
c7 = str2double(coe_line_2(a2(1)+4:a2(2)+3));
c8 = str2double(coe_line_2(a2(2)+4:a2(3)+3));
c9 = str2double(coe_line_2(a2(3)+4:a2(4)+3));
c10 = str2double(coe_line_2(a2(4)+4:a2(5)+3));

% coefficient of line 3
coe_line_3 = fgetl(f1);
a3 = strfind(coe_line_3,'E');
c11 = str2double(coe_line_3(1:a3(1)+3));
c12 = str2double(coe_line_3(a3(1)+4:a3(2)+3));
c13 = str2double(coe_line_3(a3(2)+4:a3(3)+3));
c14 = str2double(coe_line_3(a3(3)+4:a3(4)+3));

% creating a folder in order to store all the values. Here a folder named
% 'species' is created where all the values of coefficients are stored.
mkdir(['D:skill lyncmatlab course 1 sem1temp_programfile parsingspecies',species_name])

%T = linspace(200,3500,100); but it varries for some species to avoid error
%use variable names instead of values
T = linspace(local_low,local_high,1000);
R = 8.314; % value of universal gas constant 
% to get array of specific heat, enthalpy and entropy we need to iterate
% the loop for all the values. The values form c1 to c7 are of high
% temperature coefficients and c8 to c14 are low temperature coefficients.
    for i = 1:length(T)
        if T(i)>local_mid
            % high temperature coefficients
            Cp(i) = (c1 + c2*T(i) + c3*T(i)^2 + c4*T(i)^3 + c5*T(i)^4)*R;
            H(i) = (c1 + (c2*T(i)/2)+(c3*T(i)^2/3) + (c4*T(i)^3/4) + (c5*T(i)^4/5) + c6/T(i))*T(i)*R;
            S(i) = R*(c1*log(T(i)) + c2*T(i) + (c3*T(i)^2/2) + (c4*T(i)^3/3) + (c5*T(i)^4)/4 + c7);
        else
            % low temperature coefficients 
            Cp(i) =  (c8 + c9*T(i) + c10*T(i)^2 + c11*T(i)^3 + c12*T(i)^4)*R; 
            H(i) = (c8 + (c9*T(i)/2)+(c10*T(i)^2/3) + (c11*T(i)^3/4) + (c12*T(i)^4/5) + c13/T(i))*T(i)*R;
            S(i) = R*(c8*log(T(i)) + c9*T(i) + (c10*T(i)^2/2) + (c11*T(i)^3/3) + (c12*T(i)^4)/4 + c14);
        end
    end

figure(1)
% plot for Cp and T
plot(T,Cp,'linewidth',2,'color','r');
xlabel('Temperature');
ylabel('Specific heat kj');
title(' Temperature vs Specific heat plot');

figure(2)
% plot foe enthalpy and Temperature
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Enthalpy');
title(' Temperature and Enthalpy');

figure(3)
%plot for entropy and temperature
plot(T,S,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Entropy');
title(' Temperature and Entropy');

% cd new_folder changes the current folder to the new_folder
cd(['D:skill lyncmatlab course 1 sem1temp_programfile parsingspecies',species_name])

% saveas saves the figure to specific file format, here it is jpg.
% "saveas(fig,filename,formattype)" is the syntax
saveas(1,'Specific heat','jpg');
saveas(2,'Enthalpy','jpg');
saveas(3,'Entropy','jpg');

% this will change the current folder  from 'D:skill lyncmatlab course 1
% sem1temp_programfile parsingspecies to D:skill lyncmatlab course 1
% sem1temp_programfile parsing 
cd...

%calculating molecular weight for each species
mol_weight = mol_mass(species_name);

end

fclose(f1);

% in order to get the molecular weight of each species following loop is
% formed. 2 variables atoms and atm_weight are used as a array and these
% two arrays will be used. 
% MM is initialized to 0 and this also defined the new variable MM which is
% our output. In the 2nd for loop we get the MM as j is incremented, here j
% values is saved in position variable in order to the values of atm_weight
% of that position and calculate the MM if n>1. 
function MM = mol_mass(species_name)
    atoms = ['O','H','C','N','A'];
    atm_weight = [16 1 12 14 40];
    MM = 0; 
    for i = 1:length(species_name)
        for j = 1:length(atm_weight)
            if strcmp(species_name(i),atoms(j))
                MM = MM + atm_weight(j);
            position = j;
            end
        end
            n = str2double(species_name(i));
   if n>1
       MM = MM + atm_weight(position)*(n-1);
   end
   end
    sprintf('molecular weight of %s:%f u',species_name,MM)
    % Format data into string or character vector
    
end

- Plots obtained from the data for O2, N2, CO2:

All folder has been created after running the code and plots for all the species are saved in these folders.

Output plots for CO2

Output plots for N2

output plots for O2

- Problems faced:

- In order to get the plots, the values of local low, mid, and high temperature are necessary. However, the number of columns for each species are not the same and this has to be taken carefully. If these values are not properly taken then the plots would not come properly or not even visible. The same mistake has been done and the output is shown below as there is no plot got plotted and again corrections have been done. The images showed the corrections done.

An error occurred due to the code shown above as values of local temperature are not in the same column for all the species. For some species, there are 8 columns and for some, there is less number of columns.

The code shown above solves the errors and plots are getting plotted properly.

- 4. Learning outcomes:

- To understand how to read and write a file.

- How to take the data from the file and how to convert the string into an integer variable and how to take values from a cell array.

- Create the functions as per requirements and create the folder and how to change the directory.

- Saving the files and plotting the graphs from the data.

- 5. Conclusion:

- In order to get the data from the file number of steps are need to be followed.

- Before taking the data from a cell array it is necessary to understand what the number of column for each species and accordingly loop has to form.

- Plots has been formed as per the temperature and coefficient values.

Reference:

https://in.mathworks.com/matlabcentral/fileexchange/26139-molecular-weight-calculator-function

https://in.mathworks.com/matlabcentral/answers/65098-strcmp-in-a-loop?s_tid=srchtitle

https://in.mathworks.com/help/matlab/ref/saveas.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.