Challenge

Project 1 - Parsing NASA thermodynamic data

Aim- To write a code that extracts the co-efficients and calculates the thermodynamic properties for all the species in the data file.

Objective –

  1. To parse the thermodynamic data file and to calculate the thermodynamic properties of a gas species present in that file
  2. Calculate the molecular weight of each species
  3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
  4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.

Theory-

  • File parsing means, how to read / write the information from any type of file and can be manipulated that data from the file for our convenience.
  • Using fgetl command ,we read first line of the file.
  • Global temperature values are extracted by using strsplit command .
  • To read species values in the file, we are opening for loop which performs 53 passes.
  • Inside which species name area extracted by using fgetl and local tempertaure are separated using findstr command.
  • Then we are creating 53 files (each file in each pass) using make directory (mkdir) command.
  • Then to calculate Enthalpy,Entropy and specific heat for given values we are calling a function 'thermal_properties'.
  • In that function,we are calculating thermodynamic properties using formula specified in function’thermal_properties’ and ‘molecular_weight’.
  • Then using those values we are ploting Cp vs temp ,H vs temp and S vs temp.
  • Also we already given the atomic weight of Hydrogen,Carbon,Argon,Nitrogen and Oxygen.
  • Initially we assume molecular mass as zero then by using strcmp, we are comparing species name and element
  • Using those result we are calculating molecular mass for the species name.
  • fclose command closes the thermo.dat file.

Screen shot of folders saved outputs:

Main Code:

clear all
close all
clc
%input gas constant
R=8.314;

%reading the file
f1=fopen('THERMO.dat','r');
fgetl(f1);                  %% to read the line 1 of file
v=fgetl(f1);

%skiping of unwanted lines
for i=1:3
fgetl(f1);
end

A=strsplit(v,' ');                 %% to extract tempature values from file
global_low_temp=str2num(A{2})      %% tempature converted string to numbers format
global_mid_temp=str2num(A{3})
global_high_temp=str2num(A{4})

%reading the coefficients
for b=1:53
species=fgetl(f1);
species_name=species(1:6);
local=strfind(species,'.');
local_low_temp=str2num(species(local(1)-4:local(1)+4));  
local_mid_temp=str2num(species(local(3)-4:local(3)+4));
local_high_temp=str2double(species(local(2)-4:local(2)+4));

T1=global_low_temp;
T2=global_mid_temp;
T3=global_high_temp;

%%%% Extracting of coefficients
line_1=fgetl(f1);
a=strfind(line_1,'E');
a1=str2double(line_1(1:a(1)+3));
a2=str2double(line_1(a(1)+4:a(2)+3));
a3=str2double(line_1(a(2)+4:a(3)+3));
a4=str2double(line_1(a(3)+4:a(4)+3));
V=size(a);
if V[1 5]>0;
a5=str2double(line_1(a(4)+4:a(5)+3));
else
a5=0;
end

line_2=fgetl(f1);
a_2=strfind(line_2,'E');
a6=str2double(line_2(1:a_2(1)+3));
a7=str2double(line_2(a_2(1)+4:a_2(2)+3));
a8=str2double(line_2(a_2(2)+4:a_2(3)+3));
a9=str2double(line_2(a_2(3)+4:a_2(4)+3));
I=size(a);
if I[1 5]>0;
a10=str2double(line_2(a_2(4)+4:a_2(5)+3));
else
a10=0
end

line_3=fgetl(f1);
a_3=strfind(line_3,'E');
a11=str2double(line_3(1:a_3(1)+3));
a12=str2double(line_3(a_3(1)+4:a_3(2)+3));
a13=str2double(line_3(a_3(2)+4:a_3(3)+3));
a14=str2double(line_3(a_3(3)+4:a_3(4)+3));
%creating the file to save plots
mkdir(['D:\Online courses\skill-lync metlab\NASA Project',species_name]);

T=linspace(T1,T3,100);
[Cp,H,S]=thermal_properties(R,T,T1,T2,T3,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14);

%plot of tempature vs specific heat
figure(1)
plot(T,Cp,'linewidth',3,'color','r')
title(species_name)
xlabel('Temperature')
ylabel('specific heat')

%plot of temperature vs enthalpy
figure(2)
plot(T,H,'linewidth',3,'color','g')
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')

%plot of temperature vs entropy
figure(3)
plot(T,S,'linewidth',3,'color','b')
title(species_name)
xlabel('Temperature')
ylabel('Entropy')

data=(['D:\Online courses\skill-lync metlab\NASA Project',species_name]);
cd(data)

%saving the plots in respective files
saveas(1,'specific_heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
%calculating molecular weight
molecular_mass=molecular_weight(species_name);
disp(['molecular weight of' species_name])
disp(molecular_mass)
end
%closing of file
fclose(f1)

thermal_propertires function code:

function[Cp,H,S]=thermal_properties(R,T,T1,T2,T3,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14)
for i=1:length(T)
if T(i)>T2
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
end

Molecular_weight function:

function [molecular_mass]=molecular_weight(species_name)

elements=['H','C','O','N','AR'];
atomic_weight=[1.0079 12.0107 15.99 14.0067 39.948 32.065];
molecular_mass=0;
for i=1:length(species_name)
for j=1:length(elements)
if strcmp(species_name(i),elements(j))
molecular_mass=molecular_mass+atomic_weight(j);
a=j;
end
end
end
n=str2double(species_name(i))
if n>1
molecular_mass=molecular_mass+elements(a)*(n-1);
end
end

O2 plots:

Enthalpy

Entropy O2:

Specific heat O2:

N2 plots:

N2 enthalpy:

N2 Entropy:

N2 Specific heat:

CO2 Plots

CO2 Enthalpy:

CO2 Entropy:

CO2 Specific heat:

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.

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.