To access and interpret the data from a given file using Octave.
AIM:-
To access and interpret the Thermodynamic data from a given file using Octave.
Description:-
- File parsing is an important technique used for accessing and handling data in different applications.
- In the present application, the thermodynamic data is accessed to calculate various thermodynamic properties, and their variation with temperature is also observed.
- The properties calculated are Entropy, Specific heat, Enthalpy
- These values are calculated at different temperatures using the relations :
- Here,
R is the universal Gas constant,
T is the temperature at an instant,
Coefficients(a1-a7) are obtained from the data.
- The Molecular weight is calculated by considering the atomic weights of corresponding elements.
Procedure :
- The given file is Loaded into octave using 'fopen' command with reading access and a file pointer is defined.
- Next, 'fgetl' command is used to load the line into a variable.
- Here for some lines 'fskipl' command is used to neglect the lines.
- After loading 'strsplit' command is used to divide the single string into a cell array.
- This cell array is then used to feed the data into different variables.
- When needed 'str2num' command is used to convert alphabetical data into numerical data.
- Next, this data is passed into a different function for sorting and separating.
- The data is passed into different functions that process the input data and give corresponding thermodynamic properties.
- In the function, the properties are calculated over a range of temperatures so that the version can be observed.
- This output data is fed into files and the plots of variation are also generated.
- The molecular weight of species is also calculated and printed onto the command window.
- All this data is saved in corresponding directories that are created to be species-specific.
- All the files are closed and the program is terminated.
Plots:
Code:
- Main code to access and initialize the data:
clear all; close all; clc; f1=fopen('Thermo.dat','r');%opening the file count=fskipl(f1,Inf);%counting the lines in file by skipping them fseek(f1,0,SEEK_SET());%bringing the file pointer to start of the file %variable for maintaining the count of lines fskipl(f1);%skipping the firsr line %Getting the global temprature data line_1= fgetl(f1); Global_temps=strsplit(line_1," "); Global_low_temp=str2num(Global_temps{2}); Global_medium_temp=str2num(Global_temps{3}); Global_high_temp=str2num(Global_temps{4}); %Skipping Header information for i=3:5 fskipl(f1); endfor %Handling the species block for j=1:53 %maintaining count of species %Handling the first Line of species block species_line_1=fgetl(f1); temp=strsplit(species_line_1,'G ');%seperating the data temp_1=strsplit(temp{1}); species_name(1,j)=strtrim(temp_1(1));%saving the species name temp_2=strsplit(temp{2}); %storing local temprature ranges Local_Low_temp(1,j)=str2num(temp_2{2}); Local_High_temp(1,j)=str2num(temp_2{3}); Local_Medium_temp(1,j)=str2num(temp_2{4}); %Handling the second Line of species block species_line_2=fgetl(f1); pos=strfind(species_line_2,"E");%seperating the data Low_a1(1,j)=(str2num(species_line_2(1:pos(1)+3)));%coverting alphabetical data into numerical data Low_a2(1,j)=(str2num(species_line_2(pos(1)+4:pos(2)+3))); Low_a3(1,j)=(str2num(species_line_2(pos(2)+4:pos(3)+3))); Low_a4(1,j)=(str2num(species_line_2(pos(3)+4:pos(4)+3))); Low_a5(1,j)=(str2num(species_line_2(pos(4)+4:pos(5)+3))); %Handling the third Line of species block species_line_3=fgetl(f1); pos=strfind(species_line_3,"E"); Low_a6(1,j)=(str2num(species_line_3(1:pos(1)+3)));%seperating the data Low_a7(1,j)=(str2num(species_line_3(pos(1)+4:pos(2)+3))); High_a1(1,j)=(str2num(species_line_3(pos(2)+4:pos(3)+3))); High_a2(1,j)=(str2num(species_line_3(pos(3)+4:pos(4)+3))); High_a3(1,j)=(str2num(species_line_3(pos(4)+4:pos(5)+3))); %Handling the fourth Line of species block species_line_4=fgetl(f1); pos=strfind(species_line_4,"E"); High_a4(1,j)=(str2num(species_line_4(1:pos(1)+3)));%seperating the data High_a5(1,j)=(str2num(species_line_4(pos(1)+4:pos(2)+3))); High_a6(1,j)=(str2num(species_line_4(pos(2)+4:pos(3)+3))); High_a7(1,j)=(str2num(species_line_4(pos(3)+4:pos(4)+3))); endfor fclose(f1); mkdir "Thermo";%creating the parent directory %passing the data for printing and sorting fileparse(species_name,Low_a1,Low_a2,Low_a3,Low_a4,Low_a5,Low_a6,Low_a7, Local_Low_temp,Local_Medium_temp,Local_High_temp,High_a1,High_a2,High_a3,High_a4,High_a5,High_a6,High_a7); - Code for displaying and saving data into subfolders :
function fileparse(species_name,Low_a1,Low_a2,Low_a3,Low_a4,Low_a5,Low_a6,Low_a7,Local_Low_temp,Local_Medium_temp,Local_High_temp,High_a1,High_a2,High_a3,High_a4,High_a5,High_a6,High_a7) for i=1:53 %For iterating in between the diffrent species T=Local_Low_temp(i):Local_High_temp(i);%creating temprature ranges for each species %passing the data for caluclation of diffrent parameters for each species cp= caluclate_cp(species_name(i),Low_a1(i),Low_a2(i),Low_a3(i),Low_a4(i),Low_a5(i),Low_a6(i),Low_a7(i),Local_Low_temp(i),Local_Medium_temp(i),Local_High_temp(i),High_a1(i),High_a2(i),High_a3(i),High_a4(i),High_a5(i),High_a6(i),High_a7(i)); H= caluclate_H(species_name(i),Low_a1(i),Low_a2(i),Low_a3(i),Low_a4(i),Low_a5(i),Low_a6(i),Low_a7(i),Local_Low_temp(i),Local_Medium_temp(i),Local_High_temp(i),High_a1(i),High_a2(i),High_a3(i),High_a4(i),High_a5(i),High_a6(i),High_a7(i)); s= caluclate_s(species_name(i),Low_a1(i),Low_a2(i),Low_a3(i),Low_a4(i),Low_a5(i),Low_a6(i),Low_a7(i),Local_Low_temp(i),Local_Medium_temp(i),Local_High_temp(i),High_a1(i),High_a2(i),High_a3(i),High_a4(i),High_a5(i),High_a6(i),High_a7(i)); mol_wt=mole(species_name(i)); %Creating files for sperating the data name=char(species_name(i)); mkdir("Thermo",name);%creating a file for particular species %opening the file cd Thermo; cd (name); %adding data into the file f1=fopen('Data.txt','w');%creating a text file for adding the data %Printing diffrent parameters into the text file fprintf(f1,['Species Name :%s\n'],name); fprintf(f1,"Molecular Weight : %f\n",mol_wt); fprintf("%5s - %f\n",name,mol_wt);%printing the molecular weights onto command window fprintf(f1,['Species Low Temp coefficients\n']); fprintf(f1,"a1=%5.6es\na2=%5.6e\na3=%5.6e\na4=%5.6e\na5=%5.6e\na6=%5.6e\na7=%5.6e\n",Low_a1(i),Low_a2(i),Low_a3(i),Low_a4(i),Low_a5(i),Low_a6(i),Low_a7(i)); fprintf(f1,['\n\nSpecies Low Temp coefficients\n']); fprintf(f1,"a1=%5.6e\na2=%5.6e\na3=%5.6e\na4=%5.6e\na5=%5.6e\na6=%5.6e\na7=%5.6e\n",High_a1(i),High_a2(i),High_a3(i),High_a4(i),High_a5(i),High_a6(i),High_a7(i)); fprintf(f1,"\nLocal_High_temp=%d,\nLocal_Medium_temp=%d,\nLocal_Low_temp=%d\n",Local_High_temp(i),Local_Medium_temp(i),Local_Low_temp(i)); %handling the caluclated parameters fprintf(f1,"The CP values are:\n"); %printing thr specific heat values in the file for j=1:length(cp) fprintf(f1,"%5.6e\n",cp(j)); endfor fprintf(f1,"The Enthalphy values are:\n"); %printing thr Enthalphy values in the file for j=1:length(H) fprintf(f1,"%5.6e\n",H(j)); endfor fprintf(f1,"The Entropy values are:\n"); %printing the Entropy values in the file for j=1:length(s) fprintf(f1,"%5.6e\n",s(j)); endfor fclose(f1);%closing the text file %Plotting the specific heat values plot(T,cp,'linewidth',3); xlabel(['\fontsize{20}Temprature']); ylabel(['\fontsize{20}Cp']); title(['\fontsize{25}Cp vs Temprature-' ' ' char(species_name(i))]); text=sprintf('%s.png',char(species_name(i))); saveas (gca,text);%saving the plot %plotting the Enthalphy data plot(T,H,'linewidth',3); xlabel(['\fontsize{20}Temprature']); ylabel(['\fontsize{20}Enthalphy']); title(['\fontsize{25}Enthalphy vs Temprature-' ' ' char(species_name(i))]); text=sprintf('Enthal_%s.png',char(species_name(i))); saveas (gca,text);%saving the plot %plotting the Entropy data plot(T,s,'linewidth',3); xlabel(['\fontsize{20}Temprature']); ylabel(['\fontsize{20}Entropy']); title(['\fontsize{25}Entropy vs Temprature-' ' ' char(species_name(i))]); text=sprintf('Entro_%s.png',char(species_name(i))); saveas (gca,text);%saving the plot %closing the directory cd ..; cd ..; endfor endfunction - Code for calculating specific heat:
function c_p= caluclate_cp(species_name,Low_a1,Low_a2,Low_a3,Low_a4,Low_a5,Low_a6,Low_a7,Local_Low_temp,Local_Medium_temp,Local_High_temp,High_a1,High_a2,High_a3,High_a4,High_a5,High_a6,High_a7) %parameters used in formula T=Local_Low_temp:Local_High_temp; R=8.314; %actual caluclations if(T<Local_Medium_temp) %low temprature region cp(1,:)=R.*(Low_a1+Low_a2.*T+Low_a3.*T.^2+Low_a4.*T.^3+Low_a5.*T.^4); else% High Temp region cp(1,:)=R.*(High_a1+High_a2.*T+High_a3.*T.^2+High_a4.*T.^3+High_a5.*T.^4); endif %passing the result parameters c_p=cp; endfunction - Code for calculating Enthalpy:
function c_p= caluclate_H(species_name,Low_a1,Low_a2,Low_a3,Low_a4,Low_a5,Low_a6,Low_a7,Local_Low_temp,Local_Medium_temp,Local_High_temp,High_a1,High_a2,High_a3,High_a4,High_a5,High_a6,High_a7) %parameters used in formula T=Local_Low_temp:Local_High_temp; R=8.314; %actual caluclations if(T<Local_Medium_temp)%low temprature region cp(1,:)=R.*T.*(Low_a1+(Low_a2.*T)./2+(Low_a3.*T.^2)./3+(Low_a4.*T.^3)./4+(Low_a5.*T.^4)./5+Low_a6./T); else%High temprature region cp(1,:)=R.*T.*(High_a1+(High_a2.*T)./2+(High_a3.*T.^2)./3+(High_a4.*T.^3)./4+(High_a5.*T.^4)./5+High_a6./T); endif %passing the result data c_p=cp; endfunction - Code for calculating Entropy:
function c_p= caluclate_s(species_name,Low_a1,Low_a2,Low_a3,Low_a4,Low_a5,Low_a6,Low_a7,Local_Low_temp,Local_Medium_temp,Local_High_temp,High_a1,High_a2,High_a3,High_a4,High_a5,High_a6,High_a7) %parameters used in formula T=Local_Low_temp:Local_High_temp; R=8.314; %actual caluclations if(T<Local_Medium_temp)%low temprature region cp(1,:)=R.*(Low_a1.*log(T)+(Low_a2.*T)+(Low_a3.*T.^2)./2+(Low_a4.*T.^3)./3+(Low_a5.*T.^4)./4+Low_a7); else%High temprature region cp(1,:)=R.*(High_a1.*log(T)+(High_a2.*T)+(High_a3.*T.^2)./2+(High_a4.*T.^3)./3+(High_a5.*T.^4)./4+High_a7); endif %passing the result data c_p=cp; endfunction - Code for calculating Molecular weight:
function mol_wt=mole(species_name) %defing the masses of diffrent elements d=struct('o',15.999,'h',1.00784,'n',14.0067,'c',12.0107,'a',39.948); elements={'o','h','n','c','a'}; %handling species name species_name=lower(species_name); species_name=char(species_name);%converting cell array into string mol_wt=0; %splitting string into corresponding characters for i=1:length(species_name)%Iterating between characters of species name if(isdigit(species_name(i)))%handling repeated elements mol_wt=mol_wt-d.(species_name(i-1)); mol_wt=mol_wt+(str2num(species_name(i))*d.(species_name(i-1))); else if(!(any(cell2mat(elements)==species_name(i)))) break;%ending the loop for characters other than element names endif mol_wt=mol_wt+d.(species_name(i)); endif endfor endfunction