Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:- Parsing NASA themodynamics data and calculation of various thermodynamic properties of the speciesvin the data file.

OBJECTIVES:-

1) calculating the molecular weight of all the species and display in commandn window.

2) plotting the specific heat,enthalpy entropy for local temp range specific for each species.

3) saving the plot as images with apropriate names and dumping them in seprate folder for each species with suitable name.

THEORY:-

Data parsing:-

Definition:

Data parsing is a method where one string of data gets converted into a different type of data. So let’s say you receive your data in raw HTML, a parser will take the said HTML and transform it into a more readable data format that can be easily read and understood.

What does a parser do?

A well-made parser will distinguish which information of the HTML string is needed, and in accordance to the parsers pre-written code and rules, it will pick out the necessary information and convert it into JSON, CSV or a table, for example.

It’s important to mention that a parser itself is not tied to a data format. It’s a tool that converts one data format into another, how it converts it and into what depends on how the parser was built.

Parsers are used for many technologies, including:

  • Java and other programming languages
  • HTML and XML
  • Interactive data language and object definition language
  • SQL and other database languages
  • Modeling languages
  • Scripting languages
  • HTTP and other internet protocols

Formula:-

To calculate specific heat,enthalpy and entropy where R is universal gas constant and T is temperature

Specific heat:-Specific heat, ratio of the quantity of heat required to raise the temperature of a body one degree to that required to raise the temperature of an equal mass of water one degree.

The SI unit of specific heat is joule per kelvin and kilogram, J/(K kg).
CODE :-
function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)

% calculating specific heat
%calculating specific heat for higher and lower temperature respectively.
%a1 a2 a3 a4 a5 are higher temp coeffs
%a8 a9 a10 a11 a12 are lower temp coeffs

if T <=tempmid
    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

- steps that we followed to execute the code

first we define custome function which is specific heat and named that files same as our function name.

then we give inputs to the function where we used 7 higher temp coeffs and 7 low temp coeff and universal gas constant and temp also the mid temp to seprate our temp range with comparision operetor

the we put the formulae to get the all coefficients of temperature and executed formula for both high and low temp by using if else case

than close the function loop with end command

Entropy:- a thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system.

The SI unit of entropy is joules per kelvin (J⋅K−1)

function [S] = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)

%calculating entropy
%calculating entropy for lower and higher temperature range

if(T<=tempmid)
    
    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 that we followed to execute the code

first we define custome function which is entropy and named that files same as our function name.

then we give inputs to the function where we used 7 higher temp coeffs and 7 low temp coeff and universal gas constant and temp also the mid temp to seprate our temp range with comparision operetor

the we put the formulae to get the all coefficients of temperature and executed formula for both high and low temp by using if else case

than close the function loop with end command.

Enthalpy:- a thermodynamic quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.

The unit of measurement for enthalpy in the International System of Units (SI) is the joule.

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)

%calculating enthalpy
%calculating enthalpy for higher and lower energy


if(T<=tempmid)
    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 that we followed to execute the code

first we define custome function which is entropy and named that files same as our function name.

then we give inputs to the function where we used 7 higher temp coeffs and 7 low temp coeff and universal gas constant and temp also the mid temp to seprate our temp range with comparision operetor

the we put the formulae to get the all coefficients of temperature and executed formula for both high and low temp by using if else case

than close the function loop with end command.

Molecular weight:-: the average mass of a molecule of a compound compared to ¹/₁₂ the mass of carbon 12 and calculated as the sum of the atomic weights of the constituent atoms

The molecular weight is the mass of one mole of a substance. Usually, the units used for this are grams per mole.

function W = molecular_weight(species)

%defining atomic name of species
atomic_name = ['H','C','O','N','Ar'];

%defining atomic masses of main species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];

W=0;        %molecular weight starting range
for i=1:length(species)
    for j=1:length(atomic_name)  %loops runs to the length of the atomic names
        if strcmp(species(i), atomic_name(j));
            W = W + atomic_weight(j);
            position = j;
        end
    end
    n = str2num(species);      %now we will find if thair are more element in the species
    
    if (n>1)
        W = W + atomic_weight(position)*(n-1);
    end

end
fprintf('Molecular Weight of %s is %d',species,W);
end

- steps that we followed to executw the code

first we create custome function of molecular weight and saved with similer name to the function name

then we define atomic name of species and also thair atomic masses

the consider our molecular weight starting range is zero

then created for loop to executed the same code for all the species to calculate thair molecular weight

then we looked for is there any other elements are present in the species and also increment thair molecular weight

with if end command

then we print the molecular weight values with fprintf command and also display the molecular weight values

and close the function with end command

code:-

- writing code for data parsing from file THERMO.DAT and calculating molecular weight of each species and plotting graph

clear all
close all
clc

%inputs
R = 8.314;   %gas constant

%reading data file
f1 = fopen('THERMO.dat','r');
get = fgetl(f1);

%now reading temp from this file
temp = fgetl(f1);

%splitting
s = strsplit(temp);

%converting stringds cells into numbers
templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh = str2num(s{4});

%temp range
T = linspace(templow,temphigh,1000);

%skipping comment lines

for i = 1:3
    get=fgetl(f1);
end
%reading the species
for j=1:53
    tline=fgetl(f1);
    
    A = strsplit(tline,' '); %splitting line cell array
    species = (A{1});
    %converting strings to number
    
    lowtemp = str2num(A{end-3});
    midtemp = str2num(A{end-2});
    hightemp = str2num(A{end-1});
    
    tline1 = fgetl(f1);   %getting the next line
    a = strfind(tline1,'E');  %finding the string E
    
    %finding first 7 higher and second 7 lower coefficients
  
    a1 = tline1(1:a(1)+3);
    a1 = str2num(a1);
    a2 = tline1(a(1)+4:a(2)+3);
    a2 = str2num(a2);
    a3 = tline1(a(2)+4:a(3)+3);
    a3 = str2num(a3);
    a4 = tline1(a(3)+4:a(4)+3);
    a4 = str2num(a4);
    a5 = tline1(a(4)+4:a(5)+3);
    a5 = str2num(a5);
    
    tline2 = fgetl(f1);
    b = strfind(tline2,'E');
    a6 = tline2(1:b(1)+3);
    a6 = str2num(a6);
    a7 = tline2(b(1)+4:b(2)+3);
    a7 = str2num(a7);
    
   %finding lower coeffs
   a8 = tline2(b(2)+4:b(3)+3);
   a8 = str2num(a8);
   a9 = tline2(b(3)+4:b(4)+3);
   a9 = str2num(a9);
   a10 = tline2(b(4)+4:b(5)+3);
   a10 = str2num(a10);
   
   tline3 = fgetl(f1);
   c = strfind(tline3,'E');
   a11 = tline3(1:c(1)+3);
   a11 = str2num(a11);
   a12 = tline3(c(1)+4:c(2)+3);
   a12 = str2num(a12);
   a13 = tline3(c(2)+4:c(3)+3);
   a13 = str2num(a13);
   a14 = tline3(c(3)+4:c(4)+3);
   a14 = str2num(a14);
   
   %calculating specific heat
   Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
   
   %calculating entropy
   S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
   
   %calculating enthalpy
   H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);
   
   W = molecular_weight(species);
  

%writing molecular weight file
MW_file= fopen('Molecular Weight.txt','W');
fprintf(MW_file,'Molecular Weight of %s is %d',species,W);
fclose(MW_file);    %closing the file

%plotting cp,H,S with 
%saving in folder
cd('C:/Users/HP/Desktop/temp matlab programs/species')  %changing directory
mkdir(species);  %creating directory
cd(species);
   

%plot 1 cp vs T
figure(1)
plot(T,Cp,'linewidth',2,'color','r');
xlabel('Temperature(K)')
ylabel('specific heat(KJ)');
title(sprintf('Specific heat vs Temperature range of %s',species))
grid on
saveas(figure(1),'Specific Heat.jpg');

%plot 2 S vs T
figure(2)
plot(T,S,'linewidth',2,'color','b');
xlabel('Temperature(K)')
ylabel('entropy(KJ/mol-k)');
title(sprintf('Entropy vs Temperature range of %s',species))
grid on
saveas(figure(2),'Entropy.jpg');

%plot 3 H vs T
figure(3)
plot(T,H,'linewidth',2,'color','g');
xlabel('Temperature(K)')
ylabel('Enthalpy(KJ/mol)');
title(sprintf('Enthalpy vs Temperature range of %s',species))
grid on
saveas(figure(3),'Enthalpy.jpg');

cd('C:/Users/HP/Desktop/temp matlab programs')% chnagingn directory to original 



%calculating molecular weight of species
    W = molecular_weight(species)
end


saveas(figure(3),'Enthalpy

steps that we followed to execute the code:-

using clear all,close all and clc command to erase previous files residues .

then giving inputs to the code which is universal gas constant'

the we read the file THERMO.DAT with fopen command and transport into matlab with fget command \

now reading temp the temp lines from the file with fgetl command

then splitting the temp lines with strsplit command

then we converted strings cells into numbers by str2num command for temp range

then we using linspace command to decide temp values for temp range

the we skipped comment lines with fgetl command by creating loop to skip 3 lines at a time with fgetl command

the we read all the species with for loop the we split the arrays with strsplit

then converted numbers into strings for temp range

then getting new line by fget and finding sting E with strfind command

the we started looking for first 7 high and low temp coefficients and traced thair exact loaction and converted each of them into numbers by str2num

then we calculated cp ,enthalpy,molecular weight and entropy by simply calling the functions.

then write a molecular weight file open it,print molecular weight for each species and closed it

then plotting graph of cp,enthalpy and entropy for each species and created directory for it.

the plotting graph for cp,enthalpy and entropy against temerature and save them in same directory

then change the directory into original directory and calculated molecular weight for each species.

GRAPHS:-

1) specific heat, enthalpy and entropy graphs for O2:-

Specific heat:-

Enthalpy:-

Entropy:-

2) specific heat, enthalpy and entropy graphs for N2:-

Specific heat:-

Enthalpy:-

Entropy:-

3) specific heat, enthalpy and entropy graphs for :-

Specific heat:-

Enthalpy:-

Entropy:-

Screenshot of directory where all the species wit their respected folders are saved:-

ERRORS:-errors face while writing code and their solution to resolved them:-

command window throwing an error og 'matrix diamensions must agree'to resolved this i rechecked the program and their is a typo i was missing out exact location of values so i correct the typo and program got executed .

Google drive link for molecular weight values:- https://drive.google.com/drive/folders/1oonTOaFi4Y2fm1LeZ_1QdA_G9K2TFIHm?usp=sharing

RESULTS:-

1) All the molecular weight values showing in command window screenshots are attached below.

2) All the species dumped into their respected folder and the screenshot is mention above which is showinhg the path of the folder.

3)plot for N2,02 and CO2 for specific heat,enthalpy and entropy is mention above.

(Note:-All the related screenshots are attached below)

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.