File Parsing a 'NASA Thermodynamic Data'
AIM: File parsing a thermodynamic data from the given and analysing the data
Objective:
- 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.
- 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.
- 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.
Theory:
File parsing
Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.
Thermodynamic data
In this case, we take up the NASA's study on elements for different thermodynamic of its behaviour on different thermal conditions and its corresponding thermodynamic data value for each element
For data , Refer:{http://combustion.berkeley.edu/gri-mech/version30/files30/thermo30.dat}
Molecular Weight
Molecular weight is a measure of the sum of the atomic values of the atoms in a molecule Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations. Molecular weight is commonly abbreviated by M.W. or MW. Molecular weight is either unitless or expressed in terms of atomic mass units (amu) or Daltons (Da).
Atomic mass
The total weight of an atom is called the atomic weight. It is approximately equal to the number of protons and neutrons, with a little extra added by the electrons.
(In this thermodynamic data, the atomic weight of elements are mentioned below in the figure)
Fig1: Atmoic weight of Elements
Specific Heat
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat.
where `Cp`- specific heat
`R`- universal gas constant
`T` - Temperature
`a`- Co-efficients
Universal Gas constant
The gas constant is denoted by the symbol R or R. It is equivalent to the Boltzmann constant, but expressed in units of energy per temperature increment per mole, i.e. the pressure–volume product, rather than energy per temperature increment per particle.
Enthalpy
Enthalphy is a thermodynamic quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.
where `h`- enthalpy
`R`- universal gas constant
`T` - Temperature
`a`- Co-efficients
Entropy
Entropy is a thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system.
where `S`- entropy
`R`- universal gas constant
`T` - Temperature
`a`- Co-efficients
Program:
MAIN CODE
- To extract co-efficients from data with calculating and ploting specific heat , entropy & enthalpy
clear all
close all
clc
%Code
f1=fopen('thermo.dat','r'); %opening the data file. ensure it is in same directory
fgetl(f1); %takes the pointer to the next line
z=strsplit(fgetl(f1),' '); %spilts the values in the line using a delimiter
global_min_temp=str2double(z(2)); %specifying the split value to a variable
global_mid_temp=str2double(z(3));
global_max_temp=str2double(z(4));
fgetl(f1);fgetl(f1);fgetl(f1); %skipping the lines
x=input('Enter the name of the species:','s'); %Asks the user for put
for i=1:53 %does 53 iterations for all the elements present in data
q1=fgetl(f1); %assining the the line to variable for using
tline=strsplit(q1,' '); %splits the with delimiter specified
species=tline{1}; %takes the 1st value of the splitted line
if strcmpi(species,x) %syntax to compare the user defined element and the data
z_loc=strfind(q1,'000'); %to find a value on the line
loc_min=str2double(tline(length(tline)-3)); %specifies the value to a variable from the positin of the value found previously)
loc_mid=str2double(tline(length(tline)-1)); %repeating the process
loc_max=str2double(tline(length(tline)-2)); %repeating the process
%defining temperature
T=linspace(loc_min,loc_max,3300); %(defining the series of temperatures to for ploting)
%Defining species parameters
%Low temperature co-efficientss
line1=fgetl(f1); %(getiing to next line)
a=strfind(line1,'E'); (%Finding the position of E to find the position of start:end of value)
a1=str2double(line1(1:a(1)+3)); %(assining the co-efficient from the positions and converting the string as a number/valure)
a2=str2double(line1(a(1)+4:a(2)+3)); %(repeat finding which value positions)
a3=str2double(line1(a(2)+4:a(3)+3));
a4=str2double(line1(a(3)+4:a(4)+3));
a5=str2double(line1(a(4)+4:a(5)+3));
line2=fgetl(f1);
q2=findstr(line2,'E');
a6=str2double(line2(1:q2(1)+3));
a7=str2double(line2(q2(1)+4:q2(2)+3));
%high temperature co-efficients
a8=str2double(line2(q2(2)+4:q2(3)+3));
a9=str2double(line2(q2(3)+4:q2(4)+3));
a10=str2double(line2(q2(4)+4:q2(5)+3));
line3=fgetl(f1);
q3=strfind(line3,'E');
a11=str2double(line3(1:q3(1)+3));
a12=str2double(line3(q3(1)+4:q3(2)+3));
a13=str2double(line3(q3(2)+4:q3(3)+3));
a14=str2double(line3(q3(3)+4:q3(4)+3));
%For loop for ploting
for i=1:length(T)
[cp,H,S]=thermo_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T(i),loc_mid);
specheat(i)=cp; %(intialising the output from function to a variable for ploting)
enthalphy(i)=H;
entrophy(i)=S;
end
%Ploting: (ploting the graph with temperature against the parameters found)
figure(1);
plot(T,specheat);
xlabel('Temperature(K)');
ylabel('Specific heat');
title('Temperature VS Specific heat');
saveas(1,'SpecificHeat.jpg'); %specifing the file/[image\graph] to store in the folder)
figure(2);
plot(T,enthalphy)
xlabel('Temperature(K)');
ylabel('Enthalphy');
title('Temperature VS Enthalpy');
saveas(2,'Enthalpy.jpg');
figure(3);
plot(T,entrophy)
xlabel('Temperature(K)');
ylabel('Entrophy');
title('Temperature VS Entropy');
saveas(3,'Entropy.jpg');
%Molecular weight function calling:
molecularweight= mol_weight(species); %(function calling)
k=sprintf('%s Molecular Weight =%f',species,molecularweight);
%printing the molecular weight on the command window)
disp(k);
else
%(pointer gets to the next set of element if doesnt match the previous element)
fgetl(f1);fgetl(f1);fgetl(f1);
end
%Defining directory:
directory2=pwd
mkdir('results',species) %(creating the folder results)
%(specifing the input to be taken to store in the folder)
directory=sprintf('C:/Users/Skill_Lync/Documents/MATLAB/results/%s',species);
cd(directory);
cd(directory2);
end
fclose(f1); %(close data file)
- Program for function (thermodynamic calculation)
function [cp,H,S]= thermo_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,loc_mid)
R= 8.3145;
if T>=loc_mid
cp= (a1+(a2*T)+(a3*T^2)+(a4*T^3)+(a5*T^4))*R;
H=(a1+((a2*T)/2)+((a3*T^2)/3)+((a4*T^3)/4)+((a5*T^4)/5)+a6*T)*R*T;
S=(a1*log(T)+(a2*T)+((a3*T^2)/2)+((a4*T^3)/3)+((a5*T^4)/4)+a7)*R;
else
cp= (a8+(a9*T)+(a10*T^2)+(a11*T^3)+(a12*T^4))*R;
H=(a8+((a9*T)/2)+((a10*T^2)/3)+((a11*T^3)/4)+((a12*T^4)/5)+a13*T)*R*T;
S=(a8*log(T)+(a9*T)+((a10*T^2)/2)+((a11*T^3)/3)+((a12*T^4)/4)+a14)*R;
end
end
- Program for function (molecular weight calculation)
function[f]= mol_weight(species) %(specifing function)
atomic_mass=[15.999,1.008,12.011,14.007,39.948]; %(defining atmoic masses)
elements=['O','H','C','N','Ar']; %(defining the element)
mol_wt=0;
for i=1:length(species) %(to iterate for number the element individual)
for j=1:length(elements) %(to iterate for taking up value of mass for each indivifual atom)
if strcmpi(species(i),elements(j)) %(to compare if the species name and element matches)
mol_wt=mol_wt+atomic_mass(j); %(to get the coresponding value of mass for the element and calcualte)
z=j;
end
end
var=str2double(species(i)); %(convert as a integer/number)
if(var>1)
mol_wt=mol_wt+(atomic_mass(z)*(var-1));
end
f=mol_wt;
end
Output
- Example1: For oxygen (O)
Graph:
Molecular weight:
- Example 2: For Nitrogen (N2)
GRAPH
Molecular weight:
- Each element saved in a individual directory