Parsing NASA Thermodynamic Data using MATLAB Programming
AIM:
- To write a MATLAB function that extracts 14 coefficients from each of the 53 chemical species and calculate each of their entropy, enthalpy and specific heats using their respective coefficients and local temperature values.
- To also write a function that calculates the molecular weight of each of the 53 species and displays them in the command window.
- To also write a function to plot the Specific Heat, Enthalpy and Entropy against the local temperature range specific to each species and also saving the plots as images with appropriate names and dump them into separate folders for each species with a suitable name.
GOVERNING EQUATIONS & RELEVANT THEORY:
- I have been given a ‘.dat’ file which contains 53 species and data relevant to them. Link to the .dat file: https://drive.google.com/file/d/1bxAC_qANJhPESl5zJZScXvoWHK-QMFvp/view?usp=sharing
- The file contains local temperature values of the species and also the high temperature coefficients and low temperature coefficients specific to each species.
- I need to extract the all these relevant data from the file via a MATLAB program and calculate the necessary thermodynamic properties as mentioned in the challenge problem.
- The formulae for calculating the thermodynamic properties is as mentioned in the question:
where Cp -> Specific Heat of the species.
R -> Universal Gas Constant= 8.314 J/mol-K
T -> Local Temperature of the species.
[a1 a7] -> High Temperature coefficients.
[a8 a14] -> Low Temperature coefficients.
S -> Entropy of the species.
H -> Enthalpy of the species.
- After a quick overview of the species, I can conclude that the species comprises of these elements:
|
Element name |
Atomic mass |
|
Carbon (C) |
12.0107 a.m.u. |
|
Nitrogen (N) |
14.0067 a.m.u. |
|
Oxygen (O) |
15.9994 a.m.u. |
|
Hydrogen (H) |
1.00794 a.m.u. |
|
Argon (Ar) |
39.948 a.m.u. |
If the species are compounds, then using the atomic masses of the elements, we can calculate the molecular masses of the compounds.
BODY OF THE CONTENT:
- Programming Language used: MATLAB.
- Attached below is my code:
clear
close all
clc
f1=fopen('THERMO.dat','r');
count = 0;
c=1;
a=5;
�lculating number of lines in the 'THERMO.dat' file:
while strcmp(a,'END')==0
a=fgetl(f1);
count=count+1;
end
frewind(f1)
%Extracting the lines from the 'THERMO.dat' file and storing them in 'A' in the form of cell vector:
for NoOfLine=1:count
A{NoOfLine}=fgetl(f1);
end
A=A'; %Transposing the cell vector containg the contents of lines in the 'THERMO.dat' file:
m=0;
n=1;
R=8.314;
%Implementing nested for loops to extract the useful content required for getting the necessary plots:
for x=1:(count-6)/4
for y=1:4
tline{x,y}=A{c+5};
t{x,y}=strfind(tline{x,y},'E');
%Extracting local temperature values for each species and
�fining them over a range of 100 values with intial and final
%values equal to the local temperature values extracted from
%file:
if(y==1)
Indiv{x,y}=strsplit(tline{x,y},' ');
LocTempHigh(x,y)=str2double(Indiv{x,y}{y,length(Indiv{x,y})-2});
LocTempMid(x,y)=str2double(Indiv{x,y}{y,length(Indiv{x,y})-1});
LocTempLow(x,y)=str2double(Indiv{x,y}{y,length(Indiv{x,y})-3});
LocTempRange(x,:)=linspace(LocTempLow(x,y),LocTempHigh(x,y),1000);
end
%Extracting the 5 coeffcients from the 2nd line for each species:
if (y==2)
CellCoeff1{x,y-1}=tline{x,y}(1:t{x,y}(1)+3);
Coeff1(x,y-1)=str2double(CellCoeff1{x,y-1});
CellCoeff2{x,y-1}=tline{x,y}(t{x,y}(1)+4:t{x,y}(2)+3);
Coeff2(x,y-1)=str2double(CellCoeff2{x,y-1});
CellCoeff3{x,y-1}=tline{x,y}(t{x,y}(2)+4:t{x,y}(3)+3);
Coeff3(x,y-1)=str2double(CellCoeff3{x,y-1});
CellCoeff4{x,y-1}=tline{x,y}(t{x,y}(3)+4:t{x,y}(4)+3);
Coeff4(x,y-1)=str2double(CellCoeff4{x,y-1});
CellCoeff5{x,y-1}=tline{x,y}(t{x,y}(4)+4:t{x,y}(5)+3);
Coeff5(x,y-1)=str2double(CellCoeff5{x,y-1});
end
%Extracting the 5 coeffcients from the 3nd line for each species:
if (y==3)
CellCoeff6{x,y-2}=tline{x,y}(1:t{x,y}(1)+3);
Coeff6(x,y-2)=str2double(CellCoeff6{x,y-2});
CellCoeff7{x,y-2}=tline{x,y}(t{x,y}(1)+4:t{x,y}(2)+3);
Coeff7(x,y-2)=str2double(CellCoeff7{x,y-2});
CellCoeff8{x,y-2}=tline{x,y}(t{x,y}(2)+4:t{x,y}(3)+3);
Coeff8(x,y-2)=str2double(CellCoeff8{x,y-2});
CellCoeff9{x,y-2}=tline{x,y}(t{x,y}(3)+4:t{x,y}(4)+3);
Coeff9(x,y-2)=str2double(CellCoeff9{x,y-2});
CellCoeff10{x,y-2}=tline{x,y}(t{x,y}(4)+4:t{x,y}(5)+3);
Coeff10(x,y-2)=str2double(CellCoeff10{x,y-2});
end
%Extracting the 4 coeffcients from the 4nd line for each species:
if (y==4)
CellCoeff11{x,y-3}=tline{x,y}(1:t{x,y}(1)+3);
Coeff11(x,y-3)=str2double(CellCoeff11{x,y-3});
CellCoeff12{x,y-3}=tline{x,y}(t{x,y}(1)+4:t{x,y}(2)+3);
Coeff12(x,y-3)=str2double(CellCoeff12{x,y-3});
CellCoeff13{x,y-3}=tline{x,y}(t{x,y}(2)+4:t{x,y}(3)+3);
Coeff13(x,y-3)=str2double(CellCoeff13{x,y-3});
CellCoeff14{x,y-3}=tline{x,y}(t{x,y}(3)+4:t{x,y}(4)+3);
Coeff14(x,y-3)=str2double(CellCoeff14{x,y-3});
end
c=c+1;
end
end
e=2.71828;
�lculating the Specific Heat,Enthalpy and Entropy for Low temperature and High temperature values for each species:
for q=1:((count-6)/4)
for p=1:length(LocTempRange)
if(LocTempRange(q,p)<=LocTempMid(q))
Cp(p)=R*[Coeff8(q,1) + Coeff9(q,1)*LocTempRange(q,p) + Coeff10(q,1)*(LocTempRange(q,p))^2 + Coeff11(q,1)*(LocTempRange(q,p))^3 + Coeff12(q,1)*(LocTempRange(q,p))^4];
H(p)=R*[Coeff8(q,1)*(LocTempRange(q,p)) + Coeff9(q,1)*((LocTempRange(q,p))^2/2) + Coeff10(q,1)*((LocTempRange(q,p))^3/3) + Coeff11(q,1)*((LocTempRange(q,p))^4/4) + Coeff12(q,1)*((LocTempRange(q,p))^5/5) + Coeff13(q,1)];
S(p)=R*[Coeff8(q,1)*(log(LocTempRange(q,p))/log(e)) + Coeff9(q,1)*(LocTempRange(q,p)) + Coeff10(q,1)*((LocTempRange(q,p))^2/2) + Coeff11(q,1)*((LocTempRange(q,p))^3/3) + Coeff12(q,1)*((LocTempRange(q,p))^4/4) + Coeff14(q,1)];
end
if(LocTempRange(q,p)>LocTempMid(q,1))
Cp(p)=R*[Coeff1(q,1) + Coeff2(q,1)*LocTempRange(q,p) + Coeff3(q,1)*(LocTempRange(q,p))^2 + Coeff4(q,1)*(LocTempRange(q,p))^3 + Coeff5(q,1)*(LocTempRange(q,p))^4];
H(p)=R*[Coeff1(q,1)*(LocTempRange(q,p)) + Coeff2(q,1)*((LocTempRange(q,p))^2/2) + Coeff3(q,1)*((LocTempRange(q,p))^3/3) + Coeff4(q,1)*((LocTempRange(q,p))^4/4) + Coeff5(q,1)*((LocTempRange(q,p))^5/5) + Coeff6(q,1)];
S(p)=R*[Coeff1(q,1)*(log(LocTempRange(q,p))/log(e)) + Coeff2(q,1)*(LocTempRange(q,p)) + Coeff3(q,1)*((LocTempRange(q,p))^2/2) + Coeff4(q,1)*((LocTempRange(q,p))^3/3) + Coeff5(q,1)*((LocTempRange(q,p))^4/4) + Coeff7(q,1)];
end
end
%Plotting the Specific Heat-Temperature,Entropy-Temperature,Enthalpy-Temperature curves for each of the species:
figure(q)
%Plotting the Specific Heat-Temperature curve for each of the species:
subplot(3,1,1)
plot(LocTempRange(q,:),Cp,'Linewidth',2);
title(['Specific Heat-Temperature curve for:',char(Indiv{q,1}(1))])
xlabel('Temperature')
ylabel('Specific Heat')
%Plotting the Entropy-Temperature curve for each of the species:
subplot(3,1,2)
plot(LocTempRange(q,:),S,'Linewidth',2);
title(['Entropy-Temperature curve for:',char(Indiv{q,1}(1))])
xlabel('Temperature')
ylabel('Entropy')
%Plotting the Enthalpy-Temperature curve for each of the species:
subplot(3,1,3)
plot(LocTempRange(q,:),H,'Linewidth',2);
title(['Enthalpy-Temperature curve for:',char(Indiv{q,1}(1))])
xlabel('Temperature')
ylabel('Enthalpy')
%Saving all the obtained figures(containing the plots) in seperate folders corresponding to their respective species:
mkdir(['C:UsersReeteshDesktopSkill- LyncChallenge 8PlotsPlots',char(Indiv{q,1}(1))])
cd(['C:UsersReeteshDesktopSkill- LyncChallenge 8PlotsPlots',char(Indiv{q,1}(1))])
figu=sprintf('Plots for %s.jpg',char(Indiv{q,1}(1)));
saveas(gcf,figu);
end
�lculating the molecular masses of each of the different species and displaying them in the command window:
for u=1:(count-6)/4
Elem(u)=Indiv{u,1}(1);
Element=char(Elem(u));
MMass(u)=0;
for v=length(Element):-1:1
if strcmp(Element(v),'C')
MolMassC=12.0107;
MMass(u)=MMass(u)+MolMassC;
elseif strcmp(Element(v),'R') && strcmp(Element(v-1),'A')
MolMassAr=39.948;
MMass(u)=MMass(u)+MolMassAr;
elseif strcmp(Element(v),'N')
MolMassN=14.0067;
MMass(u)=MMass(u)+MolMassN;
elseif strcmp(Element(v),'H')
MolMassH=1.00794;
MMass(u)=MMass(u)+MolMassH;
elseif strcmp(Element(v),'O')
MolMassO=15.9994;
MMass(u)=MMass(u)+MolMassO;
elseif (isstrprop(Element(v),'alpha'))==0
if strcmp(Element(v),'(') || strcmp(Element(v),')')
MMass(u)=MMass(u)+0;
else
if strcmp(Element(v-1),'C')
Mass=12.0107;
elseif strcmp(Element(v-1),'N')
Mass=14.0067;
elseif strcmp(Element(v-1),'O')
Mass=15.9994;
elseif strcmp(Element(v-1),'H')
Mass=1.00794;
end
MMass(u)=MMass(u)+(str2num(Element(v)))*Mass;
MMass(u)=MMass(u)-Mass;
end
else
OtherMass=0;
MMass(u)=MMass(u)+OtherMass;
end
end
disp(['The Molecular Mass of ',char(Indiv{u,1}(1)),' is ',num2str(MMass(u))]); %Command to display the obtained molecular mass on the command window
end
fclose(f1); %Command to close the 'THERMO.dat' file.
- Firstly, I have opened the ‘THERMO.dat’ file using the MATLAB command named ‘fopen’.
- I have opened the file in read-only mode as our requirements only involve extracting the contents from the file.
- On carefully analysing the ‘THERMO.dat’ file, we’ll see that the file contains 53 different species where each species has its own High Temperature, Low Temperature and Mid-Temperature value.
- Each of the species also has 14 coefficients where all of these coefficients along with the local temperature values will be used to calculate the enthalpy, entropy and specific heat of the each of the species.
- For every single species, there are 4 lines in the file where the 1st line contains the local temperature values(High, Low and Mid), the 2nd line has contains the first five coefficients corresponding to their respective species, the 3rd line contains the 6th,7th,8th,9th,10th coefficients corresponding to their respective species and the 4th line contains 11th,12th,13th and 14th coefficients corresponding to their respective species.
- The above sequence is maintained for all 53 species.
- Therefore the no. of useful lines in the file is 53*4=212.
- On counting the total number of lines in the file (where each line has some content) in the file, we find out that it is 218. This process has been done using a code in MATLAB.
- Therefore the useless lines in the line (from the point of view of the given challenge) is 218-212=6.
- Again on careful analysis of the file, we’ll see the 1st 5 lines and the last line is useless (from the point of view of given challenge).
- Therefore at first, I have written a code to extract all the lines from the file (Line no. 1 to Line no. 218) using the command ‘fgetl’ and assigning it to variable A which is a cell vector.
- Then I Have implemented a nested for loop where the main for loop corresponds to the number of species as mentioned in the file and the sub- for loop corresponds to the 4 useful lines for each of the species.
- Therefore the main loop will run for 53 times and each time the main for loop runs, the sub-for loop will run 4 times. Therefore in total the sub for loop will run for 53*4=212 times.
- Within the sub-for loop I have written a code that will extract all the useful lines from the file (Line no. 6 to Line no. 217) and assign it to a variable named ‘tline’ which is a cell matrix of order (53 X 4) where each of the row corresponds to the species and each of the 4 columns corresponds to the useful line corresponding to their respective species arranged as per sequence in the file.
- After arranging all the useful lines in the form of a cell array, I have used the MATLAB command ‘strfind’ to locate the positions of the character of ‘E’ in the file. This is done so that the program can recognise each of the coefficients in the file(the coefficients having negative values have been arranged in a continuous fashion making it difficult to tell 1 coefficient from the other) and extract them.
- Using these positions of ‘E’ we can extract all the coefficients of the species and then convert them into ‘double’ format from ‘cell’ format as mathematical evaluation cannot be done on any content in ‘cell’ format.
- Also, the local temperature values present in the 1st line for each of the species has been extracted using the MATLAB command ‘strsplit’ which separates strings from each other where the delimiter for separation is a space(‘ ‘).
- After getting the individual strings from the 1 whole string, next step is to convert the required individual strings(the local temperature values) into double format from cell format in order to facilitate mathematical operations on the values.
- After getting the local temperature values a row vector of 1000 temperature values ranging from the Lowest Temperature value to the highest temperature values is defined for each of the species using the command ‘linspace’. This is done for all 53 species.
- After extracting the 14 coefficients and 1000 temperature values for each of the 53 species, now we need to calculate the specific heat, enthalpy and entropy values for each of the species.
- The calculation is done as per the formula mentioned in the challenge question.
- The code is written where a range of specific heat, entropy and enthalpy values are calculated for each of the species using the extracted temperature and coefficient values for each species. The number of specific heat, entropy and enthalpy values calculated for each species is equal to the no. of temperature values and the values of specific heat, entropy and enthalpy corresponds to its respective temperature values as defined previously for each species.
- According to the challenge question, the formula for calculating specific heat, enthalpy and entropy differs for high temperature and low temperature values.
- For high temperature values, the formula for calculating specific heat, enthalpy and entropy are as follows:
Where a1=1st coefficient
a2=2nd coefficient
a3=3rd coefficient
a4=4th coefficient
a5=5th coefficient
a6=6th coefficient
a7=7th coefficient
- For high temperature values, the formula for calculating specific heat, enthalpy and entropy are as follows:
where a8=8th coefficient
a9=9th coefficient
a10=10th coefficient
a11=11th coefficient
a12=12th coefficient
a13=13th coefficient
a14=14th coefficient
- Since the conditions for calculating the specific heat, entropy and enthalpy are different, the ‘if’ statements has been used to evaluate the mentioned thermodynamic parameters for high temperature values and low temperature values.
- The local mid-temperature value specific for each species is the deciding temperature value for differentiating between high temperature values and low temperature values for each species.
- Using this local mid-temperature value the clause is put in ‘if’ statements to calculate the thermodynamic parameters for high temperature values and low temperature values.
- In this way the specific heat, entropy and enthalpy are calculated for each species.
- Using the obtained values of specific heat, enthalpy and entropy for different temperature values, I have written a code to plot Specific Heat-Temperature, Entropy-Temperature and Enthalpy-Temperature curves for each species using the obtained Thermodynamic property values and the local temperature values. MATLAB commands used for this purpose include ‘figure’, ‘plot’,’subplot’,’xlabel’,’ylabel’,’title’,etc.
- After obtaining the required plots, a code is written where the plots for each species are saved in folders corresponding to each species and all these folders are placed in a common location. MATLAB commands used for this purpose include: ‘mkdir’, ’cd’, ‘sprintf’ and ‘saveas’.
- In this way the required plots for all 53 species are obtained.
- The second part of the challenge question has asked to calculate the molecular mass of each of the 53 species.
- On careful overview of all the species in the main file, we can say that the elements used in at least one of the species are:
- Argon (Ar)
- Carbon (C)
- Nitrogen (N)
- Oxygen (O)
- Hydrogen (H)
- The Molecular mass of these elements have been discussed in the ‘GOVERNING EQUATIONS & RELEVANT THEORY’ section.
- For this, I again implement nested for loops where the no. of times the main for loop runs corresponds the number of species and the sub-for loop runs corresponds to the number of characters(length) in each of the species.
- According to my code, using if/else statements, whenever the sub-for loop encounters a char ‘C’ it will evaluate the atomic mass to be 12.0107.Similarly if it encounters characters: ‘N’,’O’,’H’,’AR’, the program will evaluate the atomic mass to be 14.0067, 15.9994, 1.00794 and 39.948 respectively.
- If all the molecular masses evaluated belong to a particular run of the main for loop then my code adds up all the atomic masses encountered for that particular run of the main for loop and returns the molecular mass of the species. The command ‘disp’ is used to display the calculated molecular mass of the species on the command window.
- The MATLAB commands implemented for this particular operation are:’strcmp’,’if’,’elseif’,’else’,’str2num’,’isstrprop’,’num2str’.
- At the end the file is closed from MATLAB using the command ‘fclose’.
RESULTS:
- All the figures corresponding to the plots for each of the species are generated on running the code. As per the challenge question, I have been asked to display plots only for O2, CO2 and N2 Attached below are my plots for the mentioned species:
Plots for O2
Plots for CO2
Plots for N2
- On running the code, the molecular masses of all the species in the ‘THERMO.dat’ file are also displayed in the command window.
- As per the Challenge question, I have also been asked to attach the screenshot of the folder where all the folders containing the plots have been saved.
- Also attached below is the video link which shows the outputs generated on running the main code.
Click on the link: https://drive.google.com/file/d/1TcM-TpO9rh0mHAgjEMUfC5na3FDA6n0J/view?usp=sharing
ERRORS MADE WHILE PROGRAMMING:
Lots and lots of errors have been made by me while programming. Some of those errors were identified by MATLAB software during compilation of the code and error messages were generated in the command window. Those errors were rectified. Other errors made were correct as per the MATLAB syntax but the output generated by the code were different from expected results. These errors were identified in due time and the outputs generated after rectification were similar to the expected results.
CONCLUSION:
- MATLAB as a programming language and software can be used for diverse range of applications and operations catering to most engineering problems.
- Using this project as a reference, we can say that MATLAB can be used for extracting data from a text file and using that data we can perform diverse range operations and get a high-quality desired output.
- For this project, MATLAB has been highly successful in extracting the required data from the text file and deliver the required plots using the extracted data and also perform some mathematical operations on the extracted data.
REFERENCES:
- https://in.mathworks.com/matlabcentral/answers/545624-function-and-file-name-as-an-input-argument
- https://in.mathworks.com/matlabcentral/answers/317177-extracting-number-from-a-string
- https://in.mathworks.com/matlabcentral/answers/315550-variables-editor-won-t-open-in-spreadsheet-in-2016b
- https://in.mathworks.com/matlabcentral/answers/455509-find-the-index-of-given-value-in-an-array
- https://in.mathworks.com/matlabcentral/answers/74254-how-to-resolve-conversion-to-double-from-cell-is-not-possible
- https://in.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html
- https://in.mathworks.com/help/matlab/ref/cell2mat.html
- https://in.mathworks.com/matlabcentral/answers/16535-setting-variable-names-from-a-character-array