Project

Parsing NASA Thermodynamic Data using MATLAB

Aim:To parse the NASA thermodynamic data file and to calculate the thermodynamic properties of various gas species.

Introduction: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.

Problem Statement:1. 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(NASA termodynamic Data).The formulae are shown below.

Here R is the universal gas constant, T is the temeprature

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

2. Calculate the molecular weight of each species and display it in the command window.

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. All these should be generated automatically.

MATLAB Program:

close all
clear all
clc

f1=fopen('THERMO.dat','r');
fgetl(f1);
Globaltemp=fgetl(f1);
temp=strsplit(Globaltemp,'  ');
t=str2double(temp);
Global_low_temp=t(2);
Global_mid_temp=t(3);
Global_high_temp=t(4);

%Reading comments line with for loop
for i=3:5
    fgetl(f1);
end

%Extracting all species data
for j=1:213
    A{j}=fgetl(f1);
end

A=A';
species=A(1:4:end);

%Extracting species name from cell
for k=1:53
    species_name=strtok(species);
    species_temp{k}=strsplit(species{k},'  ');
    temp_range=species_temp';
end

%Input from the user
User_input=input('Enter valid species name:','s');
species_input=upper(User_input);

%String Comparison
for k=1:53
    if strcmp(species_input,species_name{k})
        X=k;
    end
end

%Extracting co-efficient of gas species
line_1=strfind(A{4*X-3+1},'E');
a{1,1}=A{4*X-3+1}(1:line_1(1)+3);
a{2,1}=A{4*X-3+1}(line_1(1)+4:line_1(2)+3);
a{3,1}=A{4*X-3+1}(line_1(2)+4:line_1(3)+3);
a{4,1}=A{4*X-3+1}(line_1(3)+4:line_1(4)+3);
a{5,1}=A{4*X-3+1}(line_1(4)+4:line_1(5)+3);

line_2=strfind(A{4*X-3+2},'E');
a{6,1}=A{4*X-3+2}(1:line_2(1)+3);
a{7,1}=A{4*X-3+2}(line_2(1)+4:line_2(2)+3);
a{8,1}=A{4*X-3+2}(line_2(2)+4:line_2(3)+3);
a{9,1}=A{4*X-3+2}(line_2(3)+4:line_2(4)+3);
a{10,1}=A{4*X-3+2}(line_2(4)+4:line_2()+3);

line_3=strfind(A{4*X-3+3},'E');
a{11,1}=A{4*X-3+3}(1:line_3(1)+3);
a{12,1}=A{4*X-3+3}(line_3(1)+4:line_3(2)+3);
a{13,1}=A{4*X-3+3}(line_3(2)+4:line_3(3)+3);
a{14,1}=A{4*X-3+3}(line_3(3)+4:line_3(4)+3);

%Conversion of string values into numbers
for i=1:14
    coeff{i}=str2double(a{i});
end

%First 7 are High temperature co-efficients from(1 to7)
%second 7 are low temperature co-efficients from (8 to 14)
high_temp_coeff_range=coeff(1:7);
low_temp_coeff_range=coeff(8:14);

%Extracting local temp ranges for each species
Temp=size(temp_range{X,1});
if Temp(2)==8
    temp1=temp_range{X,1}(1,5);
    temp2=temp_range{X,1}(1,7);
    temp3=temp_range{X,1}(1,6);
else
    if Temp(2)==9
        temp1=temp_range{X,1}(1,6);
        temp2=temp_range{X,1}(1,8);
        temp3=temp_range{X,1}(1,7);
    else
        if Temp(2)==10
            temp1=temp_range{X,1}(1,7);
            temp2=temp_range{X,1}(1,9);
            temp3=temp_range{X,1}(1,8);
        else
            if Temp(2)==11
                temp1=temp_range{X,1}(1,8);
                temp2=temp_range{X,1}(1,10);
                temp3=temp_range{X,1}(1,9);
            else
                if Temp(2)==12
                    temp1=temp_range{X,1}(1,9);
                    temp2=temp_range{X,1}(1,11);
                    temp3=temp_range{X,1}(1,10);
            end
        end
    end
end
end
%Conversion of Local temp string value into numbers
low_temp=str2double(temp1);
mid_temp=str2double(temp2);
high_temp=str2double(temp3);

%Creating the range of  high values of local temperatures
high_temp_range=linspace(mid_temp,high_temp,20);

%Function Input for calculations
HT=high_temp_range;
R=8.314;
b=high_temp_coeff_range;

%Specific Heat Calculation
Cp_high=func_specific_heat_high(b,HT,R);

%Enthalpy Calculation
H_high=func_enthalpy_high(b,HT,R);

%Entropy Calculation
S_high=func_entropy_high(b,HT,R);

%Molecular weight calculation
species_elements={'O','H','C','N','S','A'};
atomic_mass=[16,1,12,14,32,40];
mol_wt=0;

for i=1:length(species_input)
    for u=1:length(species_elements)
        if strcmp(species_input(i),species_elements(u))
        mol_wt=mol_wt + atomic_mass(u);
        y=u;
        end
    end
    n=str2double(species_input(i));
    if n>1
        mol_wt=mol_wt + atomic_mass(y)*(n-1);
    end
end

%Plotting specific heat vs temp
figure(1)
plot(high_temp_range,Cp_high,'r','linewidth',3)
xlabel('Temperature(k)');
ylabel('Specific Heat (J/K kg)');
legend('High_temp');
title({'Specific Heat Vs Temperature',['Species name= ',species_input,' & Molecular weight=',num2str(mol_wt)]})

%Plotting Enthalpy vs temp
figure(2)
plot(high_temp_range,H_high,'c','linewidth',3)
xlabel('Temperature(k)');
ylabel('Enthalpy(J/K)');
legend('High Temp');
title({'Enthalpy Vs Temperature',['Species name= ',species_input, ' & Molecular weight= ',num2str(mol_wt)]});

%Plot entropy vs temp
figure(3)
plot(high_temp_range,S_high,'m','linewidth',3)
xlabel('Temperature(K) ');
ylabel('entropy(J/K) ');
legend('High temp');
title({'Entropy Vs Temperature',['Species name= ',species_input,' & Molecular weight= ',num2str(mol_wt)]});

%Saving the plotted file in the system directory with the folder name
mkdir(['C:\Users\dell\Desktop\skill lync\matlab\species\',species_input]);
directory=(['C:\Users\dell\Desktop\skill lync\matlab\species\',species_input]);
cd(directory)
saveas(1,'Cp Vs Temp.jpg');
saveas(2,'Enthalpy Vs Temp.jpg');
saveas(3,'Entropy Vs Temp.jpg');
cd('C:\Users\dell\Desktop\skill lync\matlab\Fileparsing\')
%functions is created for calculation of cp enthalpy and entropy
%for cp
function Cp_high=func_specific_heat_high(b,HT,R)
specific_heat_high=(b{1} +b{2}.*HT +b{3}.*HT.^2 +b{4}.*HT.^3 +b{5}.*HT.^4)*R;
Cp_high=specific_heat_high;
end

%for enthalpy
function S_high=func_entropy_high(b,HT,R)
entropy_high=(b{1}.*log(HT) + (b{2}.*HT) +(b{3}.*HT.^2)/2 +(b{4}.*HT.^3)/3 +(b{5}.*HT.^4)/4 + b{7})*R;
S_high=entropy_high;
end

%for entropy
function H_high=func_enthalpy_high(b,HT,R)
enthalpy_high=(b{1} + (b{2}.*HT)/2 + (b{3}.*HT.^2)/3+(b{4}.*HT.^3)/4 +(b{5}.*HT.^4)/5 + b{6}./HT).*R.*HT;
H_high=enthalpy_high;
end

Procedure:

1.Opening and reading the file data .First of all we have to open and read the data which is possible with the help of functions like fgetl,fopen etc.The data is then splitted using 'strsplit' command.This will split the whole line into no. of cells.

2.Extracting species data using for loop and stored it in variable 'A'.Actually in this we are getting data into row form so we convert this into column form using transpose function.

3.Now i have to take the input from the user by prompting the message 'Enter the valid species name'.

4.In the next step we have to extract the coefficicent of all the sppecies.

5.In the next step all the local temperature value is extracted.

6.Now the fucntion is created for calculation of cp ,enthalpy and entropy.

7.Then molecular weight is calculated and plotting is done.

Result:Plotting is done for all three i.e. cp,enthalpy and entropy with respect to temperature and species name and its molecular weight is mentioned in all three.

CH3OH species plot:

HCN species plot:

Now the folder in which the plots are saved:

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.