% POWERED by jordi.marce.nogue@uni-hamburg.de

clear all; clear memory; clc; close all; format shortg;

% INPUT
% -----------------------------------------------------------------

% All the data will be in a .csv file with headers
% The data should be the name of the model

DATASET = readtable('models-sam-nowhales.csv');

% sort data

DATASET = sortrows(DATASET);
[NMODELS,NFILES] = size(DATASET); 

% INTERVALS' METHOD
% ------------------------------------------------------------------------

NSubdiv = 50;
SBVMmax = 5.0;


% LOOP 
% ------------------------------------------------------------------------

for j=1:NMODELS
    
    str1=string(DATASET.MODEL(j));
    
    % Read the text files and save it in Matlab format
    
    SBVMISES = tdfread(strcat(str1,'.txt'));
   
    SBMATRIU(:,1) = SBVMISES.Element_Number(:);
    SBMATRIU(:,2) = SBVMISES.Element_Volume(:);   
    SBMATRIU(:,3) = SBVMISES.von_Mises_stress(:);
        
    save(strcat(str1,'.mat'),'SBMATRIU');
   
    
   % INTERVALS 
   % -----------------------------------------------------------------
   
   INT(:) = Function_StressDistribution(SBMATRIU, NSubdiv, SBVMmax );
 
   clear SBMATRIU;
   clear DIM MAXDIM;
        
   % COPY TO CSV FILE      
   % -----------------------------------------------------------------     
   
    
   RESULTS_INTERVALS_aux = array2table(INT);
      
   if j==1
       RESULTS_INTERVALS = RESULTS_INTERVALS_aux;
   else      
       RESULTS_INTERVALS = [RESULTS_INTERVALS; RESULTS_INTERVALS_aux];
   end
    
   clear SBVMISES;
    
    clear SBVMISES MAXDIM POSITION RESULTS_POSITION;
    clear RESULTS_QIM_aux NAME_LIST RESULTS_EXCEL;
    clear INT RESULTS_INTERVALS_aux;
    clear DIM m n;
    
end


DATAFILE = [DATASET RESULTS_INTERVALS]
writetable(DATAFILE,'intervals-models-sam-nowhales.csv');


clear SBVMmax;
clear i j sets str1 NSubdiv NModels NFILES DATASET DATAFILE;

