Challenge

Project 1 - Parsing NASA thermodynamic data

Explanation,

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.

NASA THERMODYNAMIC DATA- 'THERMO.dat'file is a data file containe 14 coefficient for 53 species

SPECIFI HEAT - specific heat is the amount of heat to be supplied to the unit mass of a system in order to increase its temperature by one degree in a thermodymanic process.

ENTHALPY- it is a thermodynamic quality equivalent to the total heat content of a system. it is equal to the internalenergy of the system plus the product of pressure and volume.

ENTROPY- entropy is the measure of a system thermal energy per unit temperature that is unavilable for doing useful work.

we write the code for data parsing take 14 coefficient first 7coefficient is high temperature and then other 7 coefficient is low temperature.

after that write the seperate coad for specfic heat , entropy,enthalpy and molecular weight after creat a seperate folder for data parsing and save the each 53 species plots.

ERROR facing.

i use thermo.data in small letters but the orginal file in 'THERMO.data' after correct that it will be short out.

error occure in strfind command for unvalied name of the file after change the name of the file.

error in specific heat code reading some data mismatch after correct the mismatch code running.

if i creat a one new script and write the all specific heat,entropy,enthalpy and molecular weight calculating code in same script it shows error after that creat a new script for each specific heat,entropy,enthalpy and molecular weight ,resolve the error.

clear all
close all
clc

% inputs
R = 8.314;

% opening the file for reading the comment line
f1 = fopen('THERMO.dat','r');
get = fgetl(f1);

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

%spliting
%s = strsplit(temp);

% converting strings cell into numbers


% temperature range

%skipping comment lines
for i=1:3
    get=fgetl(f1);
end
%reading the species
for j=1:53
    tline=fgetl(f1);
    %split into cell arry
    A=strsplit(tline,' ');
    species = (A{1});

% converting strings to numbers
templow = str2num(A{end-3});
tempmid = str2num(A{end-2});
temphigh = str2num(A{end-1});
T= linspace(templow,temphigh,1000);
%getting next line
tline1 = fgetl(f1);
a= strfind(tline1,'E');

% finding first 7 high and second 7 low coefficients
% finding high 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 low coefficients
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);
% calculate specific heat
cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
% calculate entropy
 S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
 % calculate enthalpy
  H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);
  % calculate molecular weight file
   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);

   % plotting cp,H and S with temperature
   % sving in a folder
   mkdir(species)
   cd(species)
   figure(1)
   plot(T,cp,'LineWidth',3,'Color','b')
   xlabel('temperature')
   ylabel('specific heat')
   title('specific heat vs temperature of ',sprintf('%s',species))
   saveas(figure(1),'cp vs T.jpg')
figure(2)
   plot(T,H,'LineWidth',3,'Color','r')
   xlabel('temperature')
   ylabel('enthalpy')
   title('enthalpy vs temperature of ',sprintf('%s',species))
   saveas(figure(2),'H vs T.jpg')
figure(3)
   plot(T,S,'LineWidth',3,'Color','g')
   xlabel('temperature')
   ylabel('entropy')
   title('entropy vs temperature of ',sprintf('%s',species))
   saveas(figure(3),'S vs T.jpg')

cd ..

end

% function for specific heat
function cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)
% calculating specific heat for higher and lower temperature

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
end

% function for entropy
function S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)
% calculating entropy for higher and lower temperature

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

% function for enthalpy
function H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
% calculating enthalpy for higher and lower temperature

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

% function for molecular weight of diff species
function W = molecular_weight(species)
% defining atomic name of the species
atomic_name = ['H','C','O','N','A'];
% defining atomic masses of the main species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
W=0;
for i=1:length(species)
    for j=1:length(atomic_name)
if strcmp(species(i), atomic_name(j))
    W=W+atomic_weight(j);
    position = j;
end
    end
    n= str2num(species);
    if n>1
        W=W+atomic_weight(position)*(n-1);
    end
end
fprintf('molecular weight of %s:-',species);
fprintf('%f',W);
disp(' ')

end



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.