Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc
% Inputs
R = 8.316; % R is a gas constant
% reading the header & skipping the data
f = fopen('THERMO.DAT','r');
% read the first five line using for loop
for i = 1:5
header = fgetl(f);
end
for j = 1:53
% to extract the species block
line_1 = fgetl(f);
a = strsplit(line_1,' ');
% to extract the species name
species_name = a{1};
% to extract the local temp
local_low_temp = str2num(a{end-3});
local_high_temp = str2num(a{end-2});
local_mid_temp = str2num(a{end-1});
% to extract the coeffs
coeff_1 = fgetl(f);
% to find the position of E in coeff_1
posE = strfind(coeff_1,'E');
% a1 - a7 is the high temperature
% a8 - a14 is the low temperature
a1 = str2num(coeff_1(1:posE(1)+3));
a2 = str2num(coeff_1(posE(1)+4:posE(2)+3));
a3 = str2num(coeff_1(posE(2)+4:posE(3)+3));
a4 = str2num(coeff_1(posE(3)+4:posE(4)+3));
a5 = str2num(coeff_1(posE(4)+4:posE(5)+3));
% to extract the coeffs
coeff_2 = fgetl(f);
% to find the position of E in coeff_2
posE = strfind(coeff_2,'E');
a6 = str2num(coeff_2(1:posE(1)+3));
a7 = str2num(coeff_2(posE(1)+4:posE(2)+3));
a8 = str2num(coeff_2(posE(2)+4:posE(3)+3));
a9 = str2num(coeff_2(posE(3)+4:posE(4)+3));
a10 = str2num(coeff_2(posE(4)+4:posE(5)+3));
% to extract the coeffs
coeff_3 = fgetl(f);
% to find the position of E in coeff_1
posE = strfind(coeff_3,'E');
a11 = str2num(coeff_3(1:posE(1)+3));
a12 = str2num(coeff_3(posE(1)+4:posE(2)+3));
a13 = str2num(coeff_3(posE(2)+4:posE(3)+3));
a14 = str2num(coeff_3(posE(3)+4:posE(4)+3));
% take input of temperature
T = linspace(local_low_temp,local_high_temp,300);
% calculate the CP,S,H
for k = 1:length(T)
if (T(k) >= local_mid_temp)
CP(k) = (a1 + (a2*T(k)) + (a3*T(k).^2) + (a4*T(k).^3) + (a5*T(k).^4)).*R;
H(k) = (a1 + (a2*T(k)/2) + (a3*T(k).^2/3) + (a4*T(k).^3/4) + (a5*T(k).^4/5) + (a6./T(k))).*R*T(k);
S(k) = ((a1.*log(T(k))) + (a2*T(k)) + (a3*T(k).^2/2) + (a4*T(k).^3/3) + (a5*T(k).^4/4) + a7).*R;
elseif (T(k) <= local_mid_temp)
CP(k) = (a8 + a9*T(k) + a10*T(k).^2 + a11*T(k).^3 + a12*T(k).^4).*R;
H(k) = (a8 + (a9*T(k)/2) + (a10*T(k).^2/3) + (a11*T(k).^3/4) + (a12*T(k).^4/5) + (a13./T(k))).*R*T(k);
S(k) = (a8.*log(T(k)) + (a9*T(k)) + (a10*T(k).^2/2) + (a11*T(k).^3/3) + (a12*T(k).^4/4) + a14).*R;
end
end
% plotting and saving the results
cd('C:\Users\DELL\OneDrive\Desktop\NASA parsing thermodynamic\Termodatas')
mkdir(species_name);
cd(species_name);
figure(1)
plot(T,CP,'linewidth',5,'color','b')
xlabel('T')
ylabel('CP')
title(sprintf('Specific heat vs Temperature range of %s',species_name));
saveas(figure(1),'specfic heat.jpg')
figure(2)
plot(T,H,'linewidth',5,'Color','g')
xlabel('T')
ylabel('H')
title(sprintf('Enthalpy vs Temperature range of %s',species_name));
saveas(figure(2),'enthalpy.jpg')
figure(3)
plot(T,S,'linewidth',5,'Color','r')
xlabel('T')
ylabel('S')
title(sprintf('Entropy vs Temperature range of %s',species_name));
saveas(figure(3),'entropy.jpg')
cd('C:\Users\DELL\OneDrive\Desktop\NASA parsing thermodynamic\Termodatas')
% to calculate the molucular weight
atomic_name = ['H', 'C', 'N', 'O', 'Ar'];
atomic_weight = [1, 12, 14, 16, 39];
% starting range of molecular weight
molecular_weight = 0;
for l = 1:length(species_name)
for m = 1:length(atomic_name)
if strcmp (species_name(l),atomic_name(m));
molecular_weight = molecular_weight+atomic_weight(m);
position = m;
end
end
n = str2num(species_name(l));
if n>1
molecular_weight = molecular_weight+atomic_weight(position)*(n-1);
end
end
fprintf('molecular weight of %s = %f', species_name,molecular_weight);
disp(' ')
end
STEPS :

. Specific Heat:

It is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase of one unit in its temperature.

. Entropy:

Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system. The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena.

. Enthalpy:

Enthalpy, a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume. In a system enclosed so as to prevent the mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.

Code Explanation:

  1. 'clear all' - clear all the variables in the workspace,

    'close all' - closes all the plots and figures and

    'clc' - clears the command window.

  2. Making a directory using 'mkdir' with location address.
  3. After that, reading the text file 'Thermo.dat' file and opening it by using ‘fopen’ and 'r' as reading command.
  4. For reading the line in a text file using ‘fgetl’.
  5. To obtain the temperature range from the Thermo.dat file, using ‘strsplit’ to split the temperature into a particular string cell.
  6. Used ‘str2num’ for converting string cell to numbers.
  7. Divided the temperature into a string and then converted to a number so that temperature values can be used separately to solve the NASA thermodynamics.
  8. Using ‘for & end’ loops to skip the next three comment lines in the text file.
  9. Reading all the 53 species to plot specific heat(cp), enthalpy(H), and entropy(S), with temperature values, and to find the molecular weights.
  10. Used ‘for’ loop to read 53 species.
  11. Used ‘strfind’ to find string.
  12. Using 'linspace' specified the temperature range from low to high.
  13. Adding universal gas constant value ‘R’.
  14. Extracted higher and lower temperature coefficient by reading line by line from the species_name.
  15. calculate the specific heat, enthalpy, entropy.
  16. Calculating Molecular weight.

Syntax Used during Coding

Fopen

Open a file or obtain information about open files

‘r’

Open the file for reading

fgetl

Read line from file

strsplit

Split string or character vector at specified delimiter

str2num

Convert character array or string to numeric array

strfind

Find one string within another

linspace

Generate linearly spaced vectors

fprintf

Write formatted data to file

pwd

Identify current directory

mkdir

Make new directory

cd

Change working directory

output of molecular weight screenshot :

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.