Challenge

Project 1 - Parsing NASA thermodynamic data

File Parsing refers to how Information can be read or extreacted from a particular file and how that Information can be used in a computer program. With the help of File parsing we can read Data from External files and Databases and we use use that Information or data in our Calculations.

Here in this Project we will be using the concept of Data Parsing to extract the Coefficients from a Text file. With the help of these Coefficients the Thermodynamic Properties such as Specific heat(Cp), Enthalpy(H) and Entropy(S) will be evaluated for different Species given the file.

Certain formulae will be used to calculate these Tthermodynamic Properties, which are as follows :

Cp/R = a1 + a2*T +a3*T^2 + a4*T^3 +a5*T^4

H/R = a1 + a2*T/2 +a3*T^2/3 + a4*T^3/4 +a5*T^4/5 + a6/T

S/R = a1* lnT+ a2*T +a3*T^2/2 + a4*T^3/3 +a5*T^4/4 +a7

  • Following Code is used for the Extraction of the Co-effs and the Calculation of the Thermodynamic Properties:
clear all
close all
clc

%Reading the THERMO.dat file
f1 = fopen('THERMO.dat','r');
Heading = fgetl(f1);

%Extracting the Global temperature
Temp = fgetl(f1);
t = strsplit(Temp,' ');
gt_low = str2num(t{2});
gt_mid = str2num(t{3});
gt_high = str2num(t{4});

%skipping the next 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);

%for loop to extract Coeffs and calculation of Cp,H and S for all the 53 species

for i = 1:53
    species = fgetl(f1);
    A = strsplit(species,' ');
    species_name = A{1};
    
    %getting the local temp
    l_temp = species(49:75);
    local = strsplit(l_temp,' ');
    lt = str2num(local{1});
    ht = str2num(local{2});
    mt = str2num(local{3});
    
    %Extracting all the fourteen co-efficients
    line1_coeffs = fgetl(f1);
    C = findstr(line1_coeffs,'E');
    c1 = line1_coeffs(1:C(1)+3);
    c1 = str2num(c1);
    c2 = line1_coeffs(C(1)+4:C(2)+3);
    c2 = str2num(c2);
    c3 = line1_coeffs(C(2)+4:C(3)+3);
    c3 = str2num(c3);
    c4 = line1_coeffs(C(3)+4:C(4)+3);
    c4 = str2num(c4);
    c5 = line1_coeffs(C(4)+4:C(5)+3);
    c5 = str2num(c5);
    
    line2_coeffs = fgetl(f1);
    D = findstr(line2_coeffs,'E');
    c6 = line2_coeffs(1:D(1)+3);
    c6 = str2num(c6);
    c7 = line2_coeffs(D(1)+4:D(2)+3);
    c7 = str2num(c7);
    c8 = line2_coeffs(D(2)+4:D(3)+3);
    c8 = str2num(c8);
    c9 = line2_coeffs(D(3)+4:D(4)+3);
    c9 = str2num(c9);
    c10 = line2_coeffs(D(4)+4:D(5)+3);
    c10 = str2num(c10);
    
    line3_coeffs = fgetl(f1);
    F = findstr(line3_coeffs,'E');
    c11 = line3_coeffs(1:F(1)+3);
    c11 = str2num(c11);
    c12 = line3_coeffs(F(1)+4:F(2)+3);
    c12 = str2num(c12);
    c13 = line3_coeffs(F(2)+4:F(3)+3);
    c13 = str2num(c13);
    c14 = line3_coeffs(F(3)+4:F(4)+3);
    c14 = str2num(c14);
    
    T = linspace (lt,ht,1000);
    R = 8.314;
    
    for j = 1:length(T)
        if T>mt
            Cp = (c1 + c2.*(T) + c3.*(T.^2) + c4.*(T.^3) + c5.*(T.^4))*R;
            H = (c1 + (c2.*T)./2 +(c3.*T.^2)./3 +  (c4.*T.^3)./4 + (c5.*T.^4)./5 + (c6./T)).*T*R;
            S = (c1*log(T) + (c2.*T) + (c3.*T.^2)./2 +  (c4.*T.^3)./3 + (c5.*T.^4)./4 + c7)*R;
        else
            Cp = (c8 + c9.*(T) + c10.*(T.^2) + c11.*(T.^3) + c12.*(T.^4))*R;
            H = (c8 + (c9.*T)./2 +(c10.*T.^2)./3 +  (c11.*T.^3)./4 + (c12.*T.^4)./5 + (c13./T)).*T*R;
            S = (c8*log(T) + (c9.*T) + (c10.*T.^2)./2 +  (c11.*T.^3)./3 + (c12.*T.^4)./4 + c14)*R;
        end
    end
    
    
    mkdir(species_name);
    cd(species_name);
    
    %plotting for Specific heat, Enthalpy and Entropy
    
    %Temperature vs Specific heat
    figure(1);
    plot(T,Cp,'linewidth',3,'color','r');
    xlabel('Temperature(K)');
    ylabel('Specific Heat (kJ)');
    title('Temperature vs Specific heat');
    
    %Temperature vs Enthalpy
    figure(2);
    plot(T,H,'linewidth',3,'color','g');
    xlabel('Temperature(K)');
    ylabel('Enthalpy (kJ/mol)');
    title('Temperature vs Enthalpy');
    
    %Temperature vs Entropy
    figure(3);
    plot(T,S,'linewidth',3,'color','b');
    xlabel('Temperature(K)');
    ylabel('Specific Heat (kJ/mol K)');
    title('Temperature vs Entropy');
        
    
    saveas(figure(1),'specific_heat.jpg');
    saveas(figure(2),'Enthalpy.jpg');
    saveas(figure(3),'Entropy.jpg');
    
    
    cd('C:/Users/Divyanshu/Desktop/data parsing');
    
    M_WEIGHT = mw(species_name);
    sprintf('Tthe Molecular Weight of %s is %d',species_name,M_WEIGHT)
           
end
fclose(f1);
  • Function code for the calculation of Molecular Weight:
function [mol_wt] = mw(species_name)

%calculating molecular weight
    M = upper(species_name);
    E = ['H','C','N','O','AR'];
    At_wt = [1,12,14,16,40];
    mol_wt = 0;
    
    for k = 1:length(M)
        for n = 1:length(E)
            if strcmp(M(k),E(n));
                mol_wt = mol_wt + At_wt(n);
                p = n;
            end
        end
        z = str2num(M(k));
        if z>1
            mol_wt = mol_wt + At_wt(p)*(z-1);
        end        
    end
end         

Logic Description: So to start with, the primary command we use to read the file line by line is 'fgetl' and that's what we have used here too. First time it will give the heading of the File and in this case it is 'THERMO'. Now if we use 'fgetl' again, this time the command will read the next line and extracts the Global Temperature values for us.

The line which starts with "!" is a comment and these can be ignored as they aren't going to help us really in any of the calculations. This can be acheived by using 'fgetl' command as shown in the code.

Now as i have mentioned before, the 2nd time we use 'fgetl' it extracts/reads the gloabl temperature line and it can be stored in a variable (here 'Temp'). These values which the command reads is in string form and if we want to extract the individual temperature values just by writing code like Temp(1) and expecting the result as 300, its not going to do that. This is where the stringsplit (strsplit) command comes into play. So if we see the line we notice that there are spaces between the Temperature values and if we can indentify the position of these spaces we have kind of indentifies the two numbers,one on left of the given space and one on the right side of it.

So what we have done here is, we have taken a variable 't' and t = strsplit('Temp', ' '); , so here we have provide the line we want to split i.e., Temp with a delimiter, which in this case is space denoted as ' '. Its creates an array which is a cell array which contains three numbers '300' '1000' '5000'. Now the forst entry of this array i.e. t(1) will gives '300.00' but the values is till a string value and i we converted this into a number using str2num command; str2num(t{1}) and it gave us the output 300 and stroed in another variable called gt_low denoting the Global Low temperature, similarly the Global mid and Global high temperatures are stored in gt_mid and gt_high respectivelty.

The cell array have a special data structure which Matlab provides where we can store data of different types so thats why we have used the {} here with str2num command. The next three lines are ignored using fgetl command as mentioned in the beginning as well.

Next we have to parse the species block. Here again we have used the 'fgetl' command to read the first line of the species block and stored in another variable called species. This will run under for loop as the logic behind reading the species block is true for every species in the file. There are total 53 species present in the file so the loop will run for 53 times. Again the line 'species' is splitted using strsplit command and ' ' being the delimiter, this being stored in varaible A. So A{1} gives us the first entry of the line which is the name of the species itself. In the same line we have local temperature values but now in order to extract them we provide the first and the last string position from where the Temperature string starts, species(49:75) and stored it in l_temp and kind of created a sub-line within species. Now to extract the temperature values we applied the same logic of splitting the line with strsplit and storing in local, local= strsplit('l_temp', ' ') , using ' ' as delimiter and getting local low temperature as lt=str2num(local{1}) and similary high and mid temperature as ht=str2num(local{2}); and mt = str2num(local{3}); . We use 'fgetl' again to read the the first line of coefficients and store it in line1_coeffs. Now to extract the coefficients we first locate the position of the Exponential character (E) in the line. If we can do this, we can read what's there on either side of E and combine them into single Coefficient. To achieve this we used the command findstr (findstring), findstr(line1_coeffs,'E'); and saved it into a variable C. The result of this will provide us the location of all Es present in the line. So by indexing we can read what's on the left and ride side of E. By observation we know that there are 3 numbers after E and its true for all the coefficient in the dat file. So C(1) will give us the location of first E and C(1)+3 will be the end position of 1st Coefficient. There from 1st position to the C(1)+3 position we have our 1st coefficient and the same logic have been applied in the code and storing the 1st coefficient in c1, c1 = line1_coeffs(1:C(1)+3); . Now c1 is still a string, so we use str2num to convert it into a number; c1 = str2num(c1); Now to extract the 2nd coefficient we just need to see where it starts from and that is quite obvious from the file that where the 1st coeff ends thats where the 2nd coeff starts. As we already know the end position of 1st coeff is C(1)+3, therefore the starting position of 2nd coeff would be C(1)+4 and ending position will be C(2)+3, C(2) being the position of 2nd E character. So the c2 = line1_coeffs(C(1)+4:C(2)+3); and its again converted into number using str2num. Similarly we extracted c3,c4 and c5 , they being 3rd, 4th and 5th coefficient in the line respectively. c3 = line1_coeffs(C(2)+4:C(3)+3); , c4= line1_coeffs(C(3)+4:C(4)+3); and c5= line1_coeffs(C(4)+4:C(5)+3);, where in C(3),C(4) and C(5) are the position of 3rd, 4th and 5th E character in the line. The same logic is applied to extract the coefficients from the remaining two lines of a particular species.

Now we have also defined the Temperature range for which we will plotting the Thermodynamic properties against temperature. T = linspace(lt,ht,1000);

With the help of for loop and If condition statement we substituted the extracted coefficients into the given formulae which corresponds to high temperature and low temperature respectively. At this point we made a seperate directory folder using mkdir command with a specific species name and saved their respective plots into them using saveas command.

For the calculation of Molecular weight of the species we wrote a seperate function code and later called it into the main function.

CODE Explanation

  • First Command Window and Workspace are being cleared and closed.
  • To start with we open the Thermodynamic data file i.e THERMO.dat and the data in the file is being read with the help of 'fopen' command
  • following this we read the Heading of the file with help of 'fgetl' command, reading the first line.
  • Whenever we use 'fgetl' command after this point, the command will read the next line, so using 'fgetl' again we read the Global Temperatures and they are converted to number form from String with the help of 'str2num' command.
  • Using 'strsplit' command we extract and read Global low, mid and high Temperatures given in the file.
  • At this point we need to skip next 3 lines and this is done by using 'fgetl' command again for 3 times.
  • Following this next we have to extract the coefficients which are specific for each species. This is done by using 'for loop'. We run the loop 53 times as we have 53 different species in the file.
  • Within the loop we read and extracted the local temperatures for each species. this is important as we can to calculate the thermodynamic props. using the local low,mid and high temperatures.
  • Using 'findstr' command we specifically found out the number of exponential in the first line of coefficients and accordingly extracted the number of Coefficients.
  • This is done of the all the coefficients carrying line seperately.
  • The coefficients which are being extracted are in String form and are converted to number with 'str2num' command.
  • Once we extracted all the coefficients, next we have to calculate the Thermodynamic properties i.e. Specific heat (Cp), Enthalpy(H) and Entropy(S). We calculated these properties with the Formulae given above and substituted the extracted Coefficients in the formulae.
  • The first 7 coefficients are high temp. coefficients and the next 7 are low temp. coefficients. So to calculate these properties specifically for high and low temperture again 'for loop'is used and distinguished using 'if' condition statement.
  • The value of Gas Constant is taken equal to 8.314 J/(mol.K) and the temperature range is taken from 'lt' to 'ht' with 1000 values between them.
  • Following the calculation of the Thermodynamic Properties, the current directory is changed to the new directory which will be made for each species with that species name.
  • Now we will plot the graphs ' Specific heat vs Temperature', 'Enthalpy vs Temperature' and 'Entropy vs Temperature' for each Species using 'Plot' command. These plots are then be saved and dumped in the seperate directory folder with their name as mentioned in the last step.
  • 'cd' command is used to get back to the original directory.
  • Following the calculation of the Thermodynamic properties, Molecular weight of each species is caluclated for which a seperate code is written under the function name 'mw'.
  • This function code is called into the main data parsing code and the molecular weight is displayed in the the command window, following which the loop is closed.
  • And finally we close the THERMO.dat file using 'fclose' command.
  • PLOTS FOR O2

1. Specific heat vs Temperture plot

2. Enthalpy vs Temperature plot

3. Entropy vs Temperature Plot

  • PLOTS FOR N2

1. Specific heat vs Temperture plot

2. Enthalpy vs Temperature plot

3. Entropy vs Temperature Plot

  • PLOTS FOR CO2

1. Specific heat vs Temperture plot

2. Enthalpy vs Temperature plot

3. Entropy vs Temperature Plot

  • Screenshot of the WINDOW where all the folders are saved

  • Molecular Weights of the Species

  • Errors and Correction

1.

We got this error because we have used () instead of {} in line 26 ( species name = A(1) ) as shown in the figure below :

and this error was reolved by using {} in species name= A{1}, as it is a cell array and to extract information from a cell array we have to use {}.

  • References

https://in.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html

https://in.mathworks.com/matlabcentral/answers/276569-how-to-read-a-custom-file-with-data-and-search-and-parse

https://in.mathworks.com/help/matlab/ref/fgetl.html

https://in.mathworks.com/help/matlab/ref/strcmp.html

https://blogs.mathworks.com/loren/2016/09/26/indexing-with-curly-braces/

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.