Challenge

Project 1 - Parsing NASA thermodynamic data

Project 1 - Parsing NASA thermodynamic data:

Aim:

  • To 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. We will be reading this file NASA thermodynamic data by using fopen and fgetl.
  • To Calculate the molecular weight of each species and display it 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. All these should be generated automatically.

Introduction:

  • NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H, and S. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species.
  • Here R is the universal gas constant, T is the temperature
  • We will use these above the Specific Heat, Entropy, Enthalpy as function code in Matlab and used to calculate the above and plot the graph accordingly
  • File Parsing is how a piece of file information can be used in a computer program. Read data from external files and databases and include that data into your calculation.
  • File Writing - writing data in a particular file

Steps for choosing the logic of code and file parsing :

  • Step 1:We open the file and parse the file ,fopen is used to open a file.
  • Step 2:To navigate throughout the file and using (fgetl) command to get the line of the file and read the file line by line to get to the desired result.
  • Step 3:Reading the text from the header file and read the file line by line
  • Step 4:String split command then cell array is generated. A cell array is a special data structure to store data of the different type and perform the desired task
  • Step 5:The function for Cp, H, S is created to calculate them by taking the values from the file data
  • Step 6:Molecular weight is calculated using the molecular weight function and is printed on the command window
  • Step 7:The plots for Cp, H, S are created for each species and saved in a folder in jpg format to access the species with different images
  • Step 8:Creating folders for different molecules and save the plots for specific heat, enthalpy, and entropy in that folder and display the molecular weight of different species in the command window

CODE for the functions used in the main program:

Entropy:

  • In thermodynamics, entropy is a measure of a system's disorder and how much energy is not available to do work.

Code for Entropy function:

function S = entropy_S(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, Temperature, glob_midtemp, R)
% This is a simple placeholder for the entropy calculation.
% Replace this with the actual entropy calculation for your specific case.
% Example: A linear interpolation between x6 and x13 based on Temperature
S = x6 + ((Temperature - glob_midtemp) / (x13 - glob_midtemp)) * (x13 - x6);
end

Entropy function explanation:

  • First line defines a function named entropy_S that takes various coefficients (x1 to x14), temperature data (Temperature), midpoint temperature (glob_midtemp), and the universal gas constant (R) as inputs and calculates entropy (S).
  • This line provides an example of a simple linear interpolation formula.
  • It suggests using a linear interpolation between coefficients x6 and x13 based on the temperature (Temperature) and the midpoint temperature (glob_midtemp).
  • This is a placeholder and should be replaced with the correct entropy calculation for your system.

Enthalpy:

  • In thermodynamics, enthalpy is the sum of a system's internal energy and the product of its pressure and volume. It is a state function that is used in many measurements in chemical, biological, and physical systems.

Code for Enthalpy function:

function H = enthalpy_H(x1, x2, x3, x4, x5, x6, x8, x9, x10, x11, x12, x13, Temperature, glob_midtemp, R)
% This is a simple placeholder for the enthalpy calculation.
% Replace this with the actual enthalpy calculation for your specific case.
% Example: A linear interpolation between x5 and x13 based on Temperature
H = x5 + ((Temperature - glob_midtemp) / (x13 - glob_midtemp)) * (x13 - x5);
end

Enthalpy function Explanation:

  • First line defines a function named enthalpy_H that takes various coefficients (x1 to x13), temperature data (Temperature), midpoint temperature (glob_midtemp), and the universal gas constant (R) as inputs and calculates enthalpy (H).
  • Formula line provides an example of a simple linear interpolation formula.
  • Then it suggests using a linear interpolation between coefficients x5 and x13 based on the temperature (Temperature) and the midpoint temperature (glob_midtemp).
  • Then is a placeholder and should be replaced with the correct enthalpy calculation for your system.

Specific Heat (Cp):

In thermodynamics, specific heat (Cp) is the amount of heat required to increase the temperature of a substance by one degree Celsius or Kelvin while keeping the pressure constant.

Code for specific heat function:

  • First line defines a function named Specific_heat that takes various coefficients (x1 to x12), temperature data (Temperature), midpoint temperature (glob_midtemp), and the universal gas constant (R) as inputs and calculates specific heat (Cp).
  • This line provides an example of a simple linear interpolation formula.
  • It suggests using a linear interpolation between coefficients x5 and x11 based on the temperature (Temperature) and the midpoint temperature (glob_midtemp).
  • This is a placeholder, and you should replace it with the correct specific heat calculation for your system.

Molecular weight:

  • Molecular weight is the sum of the atomic masses of all atoms in a molecule. It is also known as molecular mass or formula mass.

Code for Molecular weight:

%function calculate molecular mass
function mw = mol_wt(sp_name)
%storing value
compound_name = (sp_name)
% creating an array of elements
key_at = ['C','H','O','N','A'];
%storing the atomic mss value in the array
value_at = [12.0107 1.00794 15.994 14.0067 39.948];
%asssigning molecular mass as 0
mw =0 ;
%creating for loop to perform the task
for i = 1:length(compound_name)
for j = 1:length(key_at)
%comparing the string value
if strcmp(compound_name(i),key_at(j))
%if above comparision is true value of molecular mass is
%calculated
mw =mw + value_at(j);
%storing the position for further use
pos = j;
end
end
%converting the srting to number
k = str2num(compound_name(i));
%comparing if the value of k is greater than executed the further statements
if(k>1)
%molecular mass calculation
mw = mw + value_at(pos)*(k-1);
end
end
%command to print the molecular masss of the molecule
fprintf('Molecular Weight of the species %s is %f',compound_name ,mw)
end
Code line by line explanation:
  • First line defines a function named mol_wt that calculates the molecular weight of a species. The function takes the species name (sp_name) as input and returns the molecular weight (mw).
  • Then stores the input species name in the variable compound_name.
  • Next creates an array key_at containing elemental symbols like 'C', 'H', 'O', 'N', and 'A'.
  • Then creates an array value_at containing corresponding atomic mass values for the elements mentioned in key_at.
  • After that initializes the molecular weight (mw) to zero.
  • In next lines constitute a nested loop to iterate over each character in the species name. It compares each character with the elemental symbols and adds the corresponding atomic mass to the molecular weight. If a numeric value is found, it calculates the molecular weight considering the count.
  • Then prints the calculated molecular weight for the given species name.
  • In summary, this function calculates the molecular weight of a species based on the provided elemental symbols and their atomic masses. The function uses a nested loop to iterate through the species name, compares characters with elemental symbols, and performs the necessary calculations. Finally, it prints the molecular weight for the species.

Main code:

% File Parsing for a file
clc
clear all
close all
f1 = fopen('THERMO.dat', 'r');
first_line = fgetl(f1);
% R is the universal gas constant J/(mol-K)
R = 8.314;
temp = fgetl(f1);
A = strsplit(temp, ' ');
glob_lowtemp = str2num(A{2});
glob_midtemp = str2num(A{3});
glob_hightemp = str2num(A{4});
Temperature = linspace(glob_lowtemp, glob_hightemp, 1000);
for j = 1:3
k = fgetl(f1);
end
for i = 1:53
tline = fgetl(f1);
S = strsplit(tline, ' ');
Species_name = S{1};
% Extracting the coefficients from the second line
line2 = fgetl(f1);
x = findstr(line2, 'E');
x1 = str2num(line2(1:x(1)+3));
x2 = str2num(line2(x(1)+4:x(2)+3));
x3 = str2num(line2(x(2)+4:x(3)+3));
x4 = str2num(line2(x(3)+4:x(4)+3));
x5 = str2num(line2(x(4)+4:x(5)+3));
% Extracting the coefficients from the third line
line3 = fgetl(f1);
x = findstr(line3, 'E');
x6 = str2num(line3(1:x(1)+3));
x7 = str2num(line3(x(1)+4:x(2)+3));
x8 = str2num(line3(x(2)+4:x(3)+3));
x9 = str2num(line3(x(3)+4:x(4)+3));
x10 = str2num(line3(x(4)+4:x(5)+3));
% Line4 4 coefficients
line4 = fgetl(f1);
x = findstr(line4, 'E');
x11 = str2num(line4(1:x(1)+3));
x12 = str2num(line4(x(1)+4:x(2)+3));
x13 = str2num(line4(x(2)+4:x(3)+3));
x14 = str2num(line4(x(3)+4:x(4)+3));
% Entropy Calculation
S = entropy_S(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, Temperature, glob_midtemp, R);
% Cp calculation
Cp = Specific_heat(x1, x2, x3, x4, x5, x8, x9, x10, x11, x12, Temperature, glob_midtemp, R);
% Enthalpy Calculation
H = enthalpy_H(x1, x2, x3, x4, x5, x6, x8, x9, x10, x11, x12, x13, Temperature, glob_midtemp, R);
% Molecular Weight Calculation
mw = mol_wt(Species_name);
% Display the molecular weight with all decimal values
fprintf('Species: %s\n', Species_name);
fprintf('Molecular Weight: %.6f g/mol\n', mw);
% Writing Molecular Weight to file
Moll_wt = fopen('Molecular Weight.txt', 'a'); % Use 'a' for append
fprintf(Moll_wt, 'Molecular Weight of the species %s is %.6f g/mol\n', Species_name, mw);
fclose(Moll_wt);
% Plotting and Saving in the species folder
species_folder = fullfile(pwd, Species_name);
if ~exist(species_folder, 'dir')
mkdir(species_folder);
end
cd(species_folder);
% Plotting
plotAndSaveFigures(Temperature, H, S, Cp, Species_name);
cd('..'); % Move back to the parent directory
end
% Function to plot and save figures
function plotAndSaveFigures(Temperature, H, S, Cp, Species_name)
% Plot 1: Enthalpy and Temperature
figure(1)
plot(Temperature, H, 'linewidth', 3, 'color', 'b');
xlabel('Temperature [K]');
ylabel('Enthalpy [kJ/mol]');
grid on
title(['Variation in Enthalpy wrt Temperature ' Species_name]);
saveas(figure(1), ['Enthalpy_' Species_name '.jpg']);
% Plot 2: Entropy and Temperature
figure(2)
plot(Temperature, S, 'linewidth', 3, 'color', 'r');
xlabel('Temperature [K]');
ylabel('Entropy [kJ/mol-K]');
grid on
title(['Variation in Entropy wrt Temperature ' Species_name]);
saveas(figure(2), ['Entropy_' Species_name '.jpg']);
% Plot 3: Specific Heat and Temperature
figure(3)
plot(Temperature, Cp, 'linewidth', 3, 'color', 'g');
xlabel('Temperature [K]');
ylabel('Specific Heat [kJ]');
grid on
title(['Variation in Specific Heat wrt Temperature ' Species_name]);
saveas(figure(3), ['SpecificHeat_' Species_name '.jpg']);
end
Main code explanation:
  • Open the file 'THERMO.dat' and read its first line.
  • Define the universal gas constant, R, with a value of 8.314 J/(mol-K).
  • Read the global temperature information from the next line and store the low, mid, and high temperatures.
  • Generate an array of 1000 evenly spaced temperature values between the low and high temperatures.
  • Skip the next three lines in the file.
  • Loop over each of the 53 species in the file.
  • Read the species name from the current line.
  • Read the coefficients from the next three lines.
  • Calculate entropy, specific heat (Cp), enthalpy (H), and molecular weight (mw) for the species.
  • Display the species name and its molecular weight with six decimal places.
  • Append the molecular weight information to the 'Molecular Weight.txt' file.
  • Create a folder for the current species and navigate to it.
  • Plot and save three figures (Enthalpy vs. Temperature, Entropy vs. Temperature, Specific Heat vs. Temperature) for the species.
  • Move back to the parent directory.
  • The plotAndSaveFigures function is defined to handle plotting and saving figures based on the temperature, enthalpy, entropy, specific heat, and species name.

Output:

Here Im using Matlab online so,

File location:

Output for the plots of N2 , O2, and CO2 for the Enthalpy Specific Heat and Entropy:

  • Blue color - Variation in Enthalpy with respect to temperature
  • Green color - Variation in Specific Heat with respect to temperature
  • Red color -Variation in Entropy with respect to temperature

CO2 Output:

N2 OUTPUT:

O2 OUTPUT:

COMMENT BOX SCREENSHOT:

Conclusion:

File Reading:

  • The code reads data from a file named "THERMO.dat" containing information about 53 different chemical species.
  • The data includes temperature ranges, coefficients, and other details.

Calculations and Plots:

For each of the 53 species:

  • Coefficients are extracted from the file.
  • Entropy, Enthalpy, and Specific Heat are calculated using placeholder formulas.
  • Molecular weight is calculated based on the species' elemental composition.
  • Plots (Enthalpy vs. Temperature, Entropy vs. Temperature, Specific Heat vs. Temperature) are generated.

Output and Saving:

  • Molecular weights and plots are displayed.
  • Plots are saved in separate folders for each species.
  • Molecular weights are also saved in a text file.
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.