All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species. Objective: * To write a function that extract 14 coefficients and calculate Enthalpy, Entropy, Specific heat & molecular weight for all the species in the data file. * To plot…
Mukesh Kanna S
updated on 11 Jul 2022
AIM:
To write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
Objective:
* To write a function that extract 14 coefficients and calculate Enthalpy, Entropy, Specific heat & molecular weight for all the species in the data file.
* To plot Cp, Enthalpy, and Entropy for temperature range.
* To calculate the molecular weight of each species and display it in command window.
* To save the plots as images with appropriate names and dump them in separate folders for each species with suitable names. All these should be generated automatically.
File Parsing
Parsing: Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating performing some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.
NASA Thermodynamic File:-
NASA thermodynamics file which is THERMO.dat file in which there are temperature range and 53 species are given in these data and each species contains 14 coefficients and temperature ranges and we have to parse these 14 coefficients to find Specific heat, Entropy and Enthalpy using formula as:-
where,
R = Universal Gas constant
T = Temperature
Cp = Specific heat (KJ/kg-k)
S = Entropy(KJ/mol-K)
H = Enthalpy(KJ/mol)
a(i) = Coefficients where, i = 1 to 14
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.
Cp = R*(a1 + (a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4))
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.
S = R*(a1.*log(T) + (a2*T) + ((a3*(T).^2)/2) + ((a4*(T).^3)/3) + ((a5*(T).^4)/4) + a7)
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 contained so as to prevent mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.
H = R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + ((a6./T)))
Main code:-
clc
close all
clear all
%Reading data file
f1=fopen('THERMO.dat','r')
fgetl(f1); %reading the first line
A=fgetl(f1); %reading the second line containing temperatures
%spliting the string
A=strsplit(A,' ');
%Converting string to number
global_low_temp=str2double(A{1});
global_mid_temp=str2double(A{2});
global_high_temp=str2double(A{3});
%Temperature range
T=linspace(global_low_temp,global_high_temp,1000);
%input
R=8.314; %Universal gas constant
%Ignoring comment lines
for i=1:3
fgetl(f1);
end
%Reading the species
for j=1:53
C=fgetl(f1);
line1=strsplit(C,' ');
species=(line1{1}); %Read the name of species
temp_low=str2double(line1{end-3});
temp_mid=str2double(line1{end-1});
temp_high=str2double(line1{end-2});
d=fgetl(f1); %Moving to next line
line2=strfind(d,'E'); %finding the Character E
a1=str2double(d(1:line2(1)+3)); %Finding seven high and seven low temp coeffs
a2=str2double(d(line2(1)+4:line2(2)+3));%Converting string to number
a3=str2double(d(line2(2)+4:line2(3)+3));
a4=str2double(d(line2(3)+4:line2(4)+3));
a5=str2double(d(line2(4)+4:line2(5)+3));
e=fgetl(f1);
line3=strfind(e,'E');
a6=str2double(e(1:line3(1)+3));
a7=str2double(e(line3(1)+4:line3(2)+3));
a8=str2double(e(line3(2)+4:line3(3)+3));%finding lower temp coefficients
a9=str2double(e(line3(3)+4:line3(4)+3));
a10=str2double(e(line3(4)+4:line3(5)+3));
f=fgetl(f1);
line4=strfind(f,'E');
a11=str2double(f(1:line4(1)+3));
a12=str2double(f(line4(1)+4:line2(2)+3));
a13=str2double(f(line4(2)+4:line4(3)+3));
a14=str2double(f(line4(3)+4:line4(4)+3));
%calculation of specific_heat
Cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,temp_mid);
%Calculation of Enthalpy
H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,temp_mid);
%Calculation of Entrophy
S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,temp_mid);
%Saving
cd('/Users/mukesh/Documents/MATLAB/NASA Project') %creating new directory
%dir_name=species;
mkdir(species); %Making new folder of species name
cd(species) %Saving the folder in new directory
%Plotting
figure(1)
plot(T,Cp,'LineWidth',5,'Color','r')
title(sprintf('Specific heat Vs Temperature for %s',species))
xlabel('Temperature [K]') %labelling
ylabel('Specific)heat [KJ/Kg-K]')
grid on
figure(2)
plot(T,H,'LineWidth',5,'Color','g')
title(sprintf('Enthalpy Vs Temperature for %s',species)) %assigning title
xlabel('Temperature [K]')
ylabel('Enthalpy [KJ/mol]')
grid on
figure(3)
plot(T,S,'LineWidth',5,'Color','b')
title(sprintf('Entropy Vs Temperature for %s',species))
xlabel('Temperature [K]')
ylabel('Entropy [KJ/mol-K]')
grid on
pause(0.0001) %to see every figures, pausing it by milliseconds
saveas(figure(1),'Specific heat.png');
saveas(figure(2),'Enthalpy.png');
saveas(figure(3),'Entropy.png');
cd .. %go back to previous directory
%calculation of molecular weight
w=molecular_weight(species);
end
fclose(f1)
Explanation-
* First we will open a file THERMO.dat file and reading this file using fopen command.
* Then, we will read first THERMO using fgetl command and then we read temperature file and we will split the temperatures using strsplit command to convert it into cell array and then we will convert this string to number using str2double command to find low, high and mid temperatures.
* Then we will assign temperature range using linspace command. Then we run loop to skip command lines the data file using fgetl command inside the loop and we will Input the parameter R.
* Now, we will run the loop to find 53 element in the data file by assigning fgetl command and then spliting into the cell array to find the species line and then find the species name and then find the number of the temperatures in the species line.
* Then we read the new line. In order to read the coefficient in the data file we will first find the 'E' string using strfind command to find before and after numbers of the coefficients then we will find remaining 14 coefficients and convert them to numbers from strings.
* Out of which we will find first 7 Coefficients which are for Higher temperature and then Second 7 Coefficients which are for Lower temperature.
* Now we will specify Specific heat Cp, Entropy S, Enthalpy H functions to find these parameters using these 14 coefficients for Lower and higher Temperature.
* Now we change the directory using cd command and then creating directory folder where we have to save our files using mkdir command and then we will change the directory to this created folder.
* Now, we will plot Specific heat, Entropy and Enthalpy with the Temperature for different species. We we will use sprintf command to in the title command to create different species title.
* Then we will use saveas command to save the plots of different species in the directory folder. Now we will change the directory to which our main code file runs in the MATLAB.
* Now we will specify molecular weight W function to find the Molecular weight of the different species and then close the txt file using fclose command.
Function to find Specific Heat :-
function Cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,temp_mid)
if(T<=temp_mid)
Cp=R*(a8+(a9*T)+(a10*(T.^2))+(a11*(T.^3))+(a12*(T.^4)));
else
Cp=R*(a1+(a2*T)+(a3*(T.^2))+(a4*(T.^3))+(a5*(T.^4)));
end
end
Steps-
* Now we will find the Specific heat by creating Specific heat function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
* Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.
* Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
* Then end the loop and function to calculate Specific Heat.
Function to find Enthalpy:-
function H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,temp_mid)
if (T<=temp_mid)
H=R*T.*(a8+((a9*T)/2)+((a10*(T.^2))/3)+((a11*(T.^3))/4)+((a12*(T.^4))/5)+(a13./T));
else
H=R*T.*(a1+((a2*T)/2)+((a3*(T.^2))/3)+((a4*(T.^3))/4)+((a5*(T.^4))/5)+(a6./T));
end
end
STEPS :-
* Now we will find the Enthalpy by creating Enthalpy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
* Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.
* Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
* Then end the loop and function to calculate Enthalpy.
Function to find Entropy:-
function S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,temp_mid)
if (T<=temp_mid)
S=R*((a8*log(T))+(a9*T)+((a10*(T.^2))/2)+((a11*(T.^3))/3)+((a12*(T.^4))/4)+a14);
else
S=R*((a1*log(T))+(a2*T)+((a3*(T.^2))/2)+((a4*(T.^3))/3)+((a5*(T.^4))/4)+a7);
end
end
STEPS :-
* Now we will find the Entropy by creating Entropy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature and do the rest as same as enthalpy and specific heat.
Function to find Molecular weight of the different Species :-
function W = molecular_weight(species)
% Defining Atomic name of the species
atomic_name = ['H','C','O','N','A'];
% Defining Atomic masses of main species
atomic_weight = [1 12 16 14 39];
W=0;% Molecular weight starting range
for i=1:length(species) % Loop runs to the length of the species
for j=1:length(atomic_name) % Loop runs to the length of the atomic names
if strcmp(species(i), atomic_name(j)) %function to compare 2 strings
W = W + atomic_weight(j);
position = j;
end
end
n = str2double(species(i)); % Now we will find if there is more element in the species then incrementing atomic weight of the species
if n>1
W = W + atomic_weight(position)*(n-1);
end
end
fprintf('Molecular Weight of %s :- ',species);
fprintf('%f',W);
disp(' ')
end
STEPS :-
* Firstly we create an Molecular_weight functionW and assigning species input to that.
* Then define the main element of all 53 species in an array atomic_name that are :- 'H','C','O','N','A'.
* Then by taking above main elements we will create there atomic_wieght array from any source like internet that are :- 1 12 16 14 39.
* Then we will assign molecular starting range W To zero and then create an for loop condition which runs to the length of the species and an another nested for loop is created which runs to the length of the string atomic_name.
* Then we will create an condition using if condition and use strcmp command to compare the species and atomic_name strings to calculate molecular masses if by comparison both string found same then we will assign its molecular weight.
* Now, we will string to the number using str2double command assign to n. Then we run an condition of if loop to find weather the species contains more than one element if there are more then 1 elements that is n>1 then we will increment the atomic weight by the multiplication of position and n-1 to the atomic_weight array.
* Then we will print the Molecular weight using fprintf command and display it using disp command.
i met with an error showing that "unable to change the current folder". The path i provided is wrong and then i provide a valid path and then change the directory.
i have attached the screenshot of molecular weight of all species displaying in command window and screenshot of folder cotaining all species.
Plots for O2,N2,CO2
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Underbody Coating
Underbody Coating is a sprayable coating, which protects the under surface of the vehicle from corrosion. Underbody coating provides long term protection from corrosion to car's underbody. It also protects parts like internal body panels, frame rails and other inner cavities that are not physically accessible…
13 Jan 2023 02:52 PM IST
Benchmarking
Aim: To expain about Benchmarking and finding a good car for Mr.D.S.Pughazhyendhii by comparing the cars. Benchmarking :- Benchmarking is a process of measuring the performance of a company’s products, services, or processes…
13 Jan 2023 07:25 AM IST
Project - Analysis of a practical automotive wiring circuit
1) a) Genarator b) Battery c) Starter d) Ignition coil e) Ammeter f) Foot selector switch g) Breaker i) Fuse j) Current and voltage regulator k) Distributor 2) Genarator: A generator is a device that transforms mechanical energy into electrical energy, typically by electromagnetic induction via Faraday's Law.An electric…
28 Nov 2022 01:22 PM IST
Week 10- Assembly Workbench
AIM :- To create assemblies of Quick return mechanism and CV joint and to also create 2D drawing of the given assemblies in CATIA. ASSEMBLY :- Assembly modeling is a technology and method used by computer-aided design and product visualization computer software systems to handle multiple files that represent components…
30 Sep 2022 05:10 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.