Challenge

Project 1 - Parsing NASA thermodynamic data

>>Main code

close all
clear all
clc

%open the data file
f1 = fopen('THERMO.dat','r');

%Universal Gas constant(R)
R=8.314;

%for skipping the first five lines in data
for i=1:5
    a=fgetl(f1);
end

%Extract the all 53 species from data 
for i=1:53

%split the line into individual words using strsplit()
header=fgetl(f1);
b=strsplit(header,' ');

%Defining the species
%{} to convert cell in number
species = b{1};

%Defining local temperature value
%converting character value into real number
local_low_temp=str2double(b{end-3});
local_high_temp=str2double(b{end-2});
local_medium_temp=str2double(b{end-1});

%find 1000 values in local temp range and store as T
T= linspace(local_low_temp,local_high_temp,1000);

%Co-efficient values
line1=fgetl(f1);

%Defining the total 14 Co-efficient as a1-a14 and give real value with str2double() 

%search the position of E in values using strfind() command
position_e1=strfind(line1,'E');
a1= str2double(line1(1:(position_e1(1))+3));
a2= str2double(line1(position_e1(1)+4:position_e1(2)+3));
a3= str2double(line1(position_e1(2)+4:position_e1(3)+3));
a4= str2double(line1(position_e1(3)+4:position_e1(4)+3));
a5= str2double(line1(position_e1(4)+4:position_e1(5)+3));

line2=fgetl(f1);
position_e2=strfind(line1,'E');
a6= str2double(line2(1:(position_e2(1))+3));
a7= str2double(line2(position_e2(1)+4:position_e2(2)+3));
a8= str2double(line2(position_e2(2)+4:position_e2(3)+3));
a9= str2double(line2(position_e2(3)+4:position_e2(4)+3));
a10= str2double(line2(position_e2(4)+4:position_e2(5)+3));

line3=fgetl(f1);
position_e3=strfind(line1,'E');
a11= str2double(line3(1:(position_e3(1))+3));
a12= str2double(line3(position_e3(1)+4:position_e3(2)+3));
a13= str2double(line3(position_e3(2)+4:position_e3(3)+3));
a14= str2double(line3(position_e3(3)+4:position_e3(4)+3));

%Evaluate the Cp,H,S by defining individual function with formula
Cp=Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,local_medium_temp,R);
H=Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_medium_temp,R,T);
S=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_medium_temp,R,T);

% change directory(cd) or to give path to store further data


%making the folder for each species to store plot 
mkdir ('C:\Users\Ravina\Desktop\Matlad training',species)

cd (species)

%plot1 Specific Heat vs Temperature
figure(1)
plot(T,Cp)
xlabel('Temperature')
ylabel('Specific Heat')
title('Cp vs T')
%save the plot with image file
saveas(1,'Cp vs T','jpg')

%plot2 Entropy vs Temperature
figure(2)
plot(T,S)
xlabel('Temperature')
ylabel('Entropy')
title('S vs T')
saveas(2,'S vs T','jpg')

%plot3 Enthalpy vs Temperature
figure(3)
plot(T,H)
xlabel('Temperature')
ylabel('Enthalpy')
title('H vs T')
saveas(3,'H vs T','jpg')

%closing the directory
cd ..

%Display the molecular weight for given species
%Function Molecular_weight
Molecular_Weight=Molecular_weight(species);
end
fclose(f1)

File 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 perfoming 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.

In simple words, technique to use file as data and finding the desired outputs needed.

2) Molecular weight function code

%Program to calculate the molecular weight of required species
function [total_Mo_wt]= Molecular_weight(species)

total_Mo_wt=0;

%Provide the character for species
At_name=['O','H','C','N','AR'];

%Provide the Atomic_weight for given atomic name() in sequence only
%Round off value for ease
At_wt=[16,1,12,14,40];

%store the size according th species total letters
size=length(species);

%assign zero values in array upto size value in single row
Mo_wt=zeros(1,size);

%Performing for loop operation to enter values in Mo_wt array
for i=1:size

    %Distinguse if species has character or number
    [num,st]= str2num(species(i));

    %If it is character then if condition
    if st==0
        %For loop till species has At_name character
        for j=1:length(At_name)

            %Comparing Species() letter with At_name()
            if species(i)==At_name(j)

                %store values in array of Mo_wt till condition is true
                Mo_wt(i)= Mo_wt(i)+At_wt(j);
            end
        end
    %if it is number itself then else condition
    else
        Mo_wt(i-1)=Mo_wt(i-1)*num;

    end
end

%adding individual array values for Mo_wt and store as total_Mo_wt
total_Mo_wt=sum(Mo_wt);
fprintf('Molecular Weight of %s:- ',species);
fprintf('%f \n',total_Mo_wt);
end

3)Enthalpy function code

function [H] =Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_medium_temp,R,T)
    if T<=local_medium_temp
        H= (a8+a9*(T/2)+a10*((T.^2)/3)+a11*((T.^3)/4)+a12*((T.^4)/5)+a13./T)*R.*T;
    else 
       H= (a1+a2*(T/2)+a3*((T.^2)/3)+a4*((T.^3)/4)+a5*((T.^4)/5)+a6./T)*R.*T;
    end
   
end

4)Entropy function code

function [S]= Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_medium_temp,R,T)
    if T<=local_medium_temp
        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

5)Specific Heat function code

function [Cp]=Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,local_medium_temp,R)
    if T<=local_medium_temp
        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

>>Folder showing each species individual data :-

>>PLOT FOR O2

>>PLOT FOR N2

>> PLOT FOR CO2

>> Command Window:-

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.