Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

To write a Matlab program to extract the 14 co-efficients from NASA thermodynamic data file and to calculates the enthalpy, entropy and specific heats for all the species in the data file. Also by plotting the graphs of every species and to find the molecular mass of all species.

Description:

File parsing is done to extract data from the file, it should be as generic as possible to fetch the data from different files. In this program we are getting the 14 co-effecients from the data and by using this we calculate enthalpy, entropy and specific heat for different species and the condition is that the FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

The formulae to calculate specific heat, enthalpy and entropy are given below.

Program Algorithm:

  • THERMO.dat file is imported to the program by using the \'fopen\' command and each and every line is read by using \'fgetl\' command.
  • Inorder to get the header information \'strsplit\' command is used to split the string and the global temperatures are fetched.
  • Next three statements are irrelevent, hence its is skipped by using the \'fgetl\' line inside the \'for loop\'.
  • Now the species data is read , since there are 53 species and the logic remains the same hence \'for loop\' for the 53 species is declared and the main program is given inside the \'for loop\'.
  • The species first line contains the species name and local temperatures and the next three lines contains the co-effecients.
  • Hence by using the \'strsplit\' command the species_name and the local temperatures are obtained.
  • Inorder to get the co-effecients \'strfind\' command is used find the variable\'E\' by using this as reference co-effecients are assigned.
  • To create a seperate folder to save the plot for every species \'mkdir\' command is used.
  • For plotting we need a set of temperatures hence the local species temperature are splitted into 1000 points by using \'linspace\' command.
  • To assign the high and low temperature co-effecients each and every local temperature is compared with the local mid temperature.
  • Thus by using the formulae specific heat, entropy and enthalpy are calculated and plotting is carried out.
  • For saving the plots \'cd\' command is used.
  • To find the molecular mass for each and every element atomic weight is assigned now by using the \'strcmp\' command each species elements are compared and molecular mass is stored using two \'for loops\', one to iterate the species element and one to compare the elements.
  • In some species like HNO3 the element\'s atomic weight to be added twice (or) thrice, hence flag operator is used to get the position and that element atomic weight is added to the molecular weight by using the \'if\' condition.

clear all
close all
clc

%universal gas constant
R=8.314;

%reading the file
f1=fopen('THERMO.txt','r')
%creating a file for molecular weight
f2=fopen('molecular weight.txt','w')

header=fgetl(f1)

%global temperature
l1=fgetl(f1)

for i=1:3
    l2=fgetl(f1)
end

T=linspace(300,1000,200);

for i=1:52
    l3=fgetl(f1);
    A=strsplit(l3,' ');

    species=strtok(l3,' ');

    %coefficient of species
    a=fgetl(f1);
    b=fgetl(f1);
    c=fgetl(f1);

    %exponential extraction
    d=strfind(a,'E');
    e=strfind(b,'E');
    f=strfind(c,'E');
    
    %high temperature coefficients
    a1=str2num(a(1:d(1)+3));
    a2=str2num(a(d(1)+4:d(2)+3));
    a3=str2num(a(d(2)+4:d(3)+3));
    a4=str2num(a(d(3)+4:d(4)+3));
    a5=str2num(a(d(4)+4:d(5)+3));
    a6=str2num(b(1:e(1)+3));
    a7=str2num(b(e(1)+4:e(2)+3));

    %low temperature coefficients
    b1=str2num(b(e(2)+4:e(3)+3));
    b2=str2num(b(e(3)+4:e(4)+3));
    b3=str2num(b(e(4)+4:e(5)+3));
    b4=str2num(c(1:f(1)+3));
    b5=str2num(c(f(1)+4:f(2)+3));
    b6=str2num(c(f(2)+4:f(3)+3));
    b7=str2num(c(f(3)+4:f(4)+3));

    %calculating Cp, H, S and mol_wt
    [Cp]=specificheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R);
    [H]=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R);
    [S]=entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,T,R);
    [Mw]=mol_wt(species);

    %creating folder for species
    mkdir(species);
    current_dir=pwd;
    cd(species)

    %plotting
    %temperature vs. specific heat
    figure(1)
    plot(T,Cp,'Color','r')
    xlabel('temperature[k]')
    ylabel('specific heat[kJ]')
    title('temperature vs specific heat');
    grid on
    image=getframe(gcf)
    imwrite(image.cdata,'T vs Cp.png')
    saveas(1,'specific heat','jpg')

    %temperature vs. enthalpy
    figure(2)
    plot(T,H,'Color','b')
    xlabel('temperature[k]')
    ylabel('enthalpy[kJ/mol]')
    title('temperature vs enthalpy');
    grid on
    image=getframe(gcf)
    imwrite(image.cdata,'T vs H.png')
    saveas(2,'enthalpy','jpg')

    %temperature vs. entropy
    figure(3)
    plot(T,S,'Color','g')
    xlabel('temperature[k]')
    ylabel('entropy')
    title('temperature vs entropy');
    grid on
    image=getframe(gcf)
    imwrite(image.cdata,'T vs S.png')
    saveas(3,'entropy','jpg')

    %changing the folder
    cd(current_dir);
    fprintf(f2,'molecular weight of %s is %f',species);
end
fclose(f2);
fclose(f1);
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.