Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing: Parsing is simply syntax analysis. It analyzes symbols strung together in the form of natural language, computer language or data structures, Conforming to the rules of Grammar.

Parser: It is a compiler or interpreter which takes the input data and breaks it down into meaningful forms using computer instructions. this broken up data is used in other components in program to achieve meaningful results.

Here, in the problem, we take NASA's thermodynamic data for different gasses, break it down appropriately and extract coefficients for wach gas and calculate Enthaly, Specific heat and Entropy and plot it against Temperature.

One popular use of parsing in mechanical engineering is to extract nodal data from the input file in FEM problems.

Code explanation:

1.) We open the THERMO.DAT using fopen and save the data under the variable f1.

3.) We use fgetl, which reads the lines from files, and removes the newline characters. We store the heading of the data under the variable first_line.

4.) We use fgetl again to read the second line, this secon line contains global temperature data. we store under a variable.Now if we notice the variable, we do not see numbers, instead we see it as a string. we use strsplit, to split the string based on the delimiter ' ', a delimiter that separates the numbers. Once we split them and store them, we conver these split strings into numbers.

5.)We skip the next 3 lines as we do not need them. We then proceed to read the next 53 lines using a For loop which contains the coefficient data of different gas.We obtain individual lines from the scanned 53 lines using fgets.We identify the character 'E' using strfind.We get an array of numbers, which is the position of E or character number of E in each line, we know the size of the number so, e individually extract each coefficient of the gas as a string, convert it into a number and use them in appropriate equations.

6.) Once the numbers are extracted, we need to input them into the respective equations to find out the Specific heat, Enthaply and Entropy of each gas.

7.) We define functions that take these coefficients, temperatures as input and use them in the main program. we also define a local range of temperature for which the individualk coefficients are given.

8.) We use cd(pwd) to open the current directory, using mkdir, we create specific folders, here it's the name of the 'species' as indiviual folders in the directory.

9.) We plot the Cp Vs T, S Vs T, H vs T plots and store them in their respective files with each loop.

Results:

Code:

%mol weight for O2,N2,CO2


function M=molecular_mass(species)

atoms={'H','N', 'O'};

weights=[1 14 12];

M=0;%we iteratively add the weight

for i=1:length(species)
    
    for j=1:length(weights)
        
        if (strcmp(species(i),atoms(j))==1)
            M=M+weights(j);
            g=j;
        end
        
        n=str2double(species(i));
        
        if n>1
            p=n-1;
            
            M=M+weights(p)*(p);
        end
    end
    
end
function S =entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,R)

mid_temp=1000;%it is in the data

if temp>=mid_temp
    
    S=(a1.*log(temp)+a2.*(temp)+a3.*(temp.^2)./2+a4.*(temp.^3)./3+a5.*(temp.^4)./4 +a7)*R;
    
else
    S=(a8.*log(temp)+a9.*(temp)+a10.*(temp.^2)./2+a11.*(temp.^3)./3+a12.*(temp.^4)./4 +a14)*R;
end
%enthaply_function

function H= enthalpy_fun(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,R)

mid_temp=1000;%it is in the data

if temp>=mid_temp
    
    H=(a1.*(temp)+a2.*(temp.^2)./2+a3.*(temp.^3)./3+a4.*(temp.^4)./4+a5.*(temp.^5)./5 +a6)*R;
    
else
    H=(a8.*(temp)+a9.*(temp.^2)./2+a10.*(temp.^3)./3+a11.*(temp.^4)./4+a12.*(temp.^5)./5 +a13)*R;
end
%specific heat function;

function Cp= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,R)

mid_temp=1000;%it is in the data

if temp>=mid_temp
    
    Cp=(a1+a2.*(temp)+a3.*(temp.^2)+a4.*(temp.^3)+a5.*(temp.^4))*R;
    
else
    Cp=(a8+a9.*(temp)+a10.*(temp.^2)+a11.*(temp.^3)+a12.*(temp.^4))*R;
end

clear all
close all
clc

%Universal Gas Constant

R=8.314;

%opening the file

f1=fopen('THERMO.txt.dat','r');

%as we can see in the text file, the first line is the heaing 'Thermo'
%we do not need it.

first_line= fgetl(f1); %this skips the first line and stores it as 'first_line'

%the second line is Global Temperature
given_temp=fgetl(f1);

%this line of Global temperature consists of low, mid and high temperatures
%we need to split them

%I removed the space which is also consiered as a character,we can start
%the inex from 2 if we don't want to adjust the data for spaces.

A=strsplit(given_temp,' ');

low_globetemp=str2double(A{1});
mid_globetemp=str2double(A{2});
high_globetemp=str2double(A{3});

%the next three lines are just disclaimer and information
b=1;
for i=1:3
    b=i+1;
    fgetl(f1);
end

%now we need to read the data and extract the coefficients

for i=1:53
    
    
    line=fgetl(f1);
    
    B=strsplit(line,' ');
    
    species= B(1);
    
    line1=fgets(f1);
   
 %we need to search for a string 'E'
 
    x=strfind(line1,'E');
 
 %now we know where E is present in the line, so we find all the
 %coefficients before E and three characters after E an store it as number
 
    a1=str2double(line1(1:x(1)+3));
    a2=str2double(line1(x(1)+4:x(2)+3));
    a3=str2double(line1(x(2)+4:x(3)+3));
    a4=str2double(line1(x(3)+4:x(4)+3));
    a5=str2double(line1(x(4)+4:x(5)+3));
 
 
 
 %now we read secondline coeffs and repeat the process
 
    line2=fgetl(f1);
    y=strfind(line2,'E');
 
    a6=str2double(line2(1:y(1)+3));
    a7=str2double(line2(y(1)+4:y(2)+3));
    a8=str2double(line2(y(2)+4:y(3)+3));
    a9=str2double(line2(y(3)+4:y(4)+3));
    a10=str2double(line2(y(4)+4:y(5)+3));
    
    
    
   
    
%now we find the coefficient data for third line

    line3=fgetl(f1);

    z=strfind(line3,'E');

    a11=str2double(line3(1:z(1)+3));
    a12=str2double(line3(z(1)+4:z(2)+3));
    a13=str2double(line3(z(2)+4:z(3)+3));
    a14=str2double(line3(z(3)+4:z(4)+3));
    
%now we define local temperatures

    temp=linspace(200,5000,500);

%specific heat

    Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,R);

%Entropy

    S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,R);

%Enthalpy
    H=enthalpy_fun(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,R);



%automating the save

    spec=char(species);

    fd= sprintf('%s',spec);

    cur_dir=pwd;

    mkdir(spec);

    cd(spec);


%plot Specific heat(Cp) vs Temperature(T)

    figure(1);

    plot(temp,Cp,'linewidth',4,'color','r');
    title('Specific heat(Cp) vs Temperature(T)')
    xlabel('Temperature in K');
    ylabel('Specific heat in KJ');

%Enthalpy v Temp

    figure(2)

    plot(temp,H,'linewidth',4,'color','g');
    title('Enthaply(H) vs Temperature(T)')
    xlabel('Temperature in K');
    ylabel('Enthalpy in KJ/(mol)');




%Entropy vs Temperature

    figure(3)

    plot(temp,S,'linewidth',4,'color','c');
    title('Entropy(S) vs Temperature(T)')
    xlabel('Temperature in K')
    ylabel('Entropy in KJ/mol-K')

%saving plots

    saveas(figure(1),'Cp.png');
    saveas(figure(2),'H.png');
    saveas(figure(3),'S.png');

    cd(cur_dir);

%Molecular mass

    M=molecular_mass(species);

    f2=fopen('molecular mass','w');
    fprintf('%s n %s n %f n ','species','Molecular mass',M)
    fclose(f2)




















end







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.

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.