clear all; clc; close all
rng(100, 'twister')
uqlab
%% definition of the inputs marginals
Iopts.Marginals(1).Name = ['Joints',num2str(1)]; 
Iopts.Marginals(1).Type = 'Uniform';
Iopts.Marginals(1).Parameters = [1.35e6 1.5e6]; % in Pa
for i1 = 2:6
Iopts.Marginals(i1).Name = ['Joints',num2str(i1)]; 
Iopts.Marginals(i1).Type = 'Uniform';
Iopts.Marginals(i1).Parameters = [7.125e6 7.875e6]; % in Pa
end
Iopts.Marginals(7).Name = ['Root',num2str(7)]; 
Iopts.Marginals(7).Type = 'Uniform';
Iopts.Marginals(7).Parameters = [4.75e7 5.25e7]; % in Pa
Iopts.Marginals(8).Name = ['Root',num2str(8)]; 
Iopts.Marginals(8).Type = 'Uniform';
Iopts.Marginals(8).Parameters = [4.75e7 5.25e7]; % in Pa
Iopts.Marginals(9).Name = ['Root',num2str(9)]; 
Iopts.Marginals(9).Type = 'Uniform';
Iopts.Marginals(9).Parameters = [4.75e7 5.25e7]; % in Pa
Iopts.Marginals(10).Name = ['Root',num2str(10)]; 
Iopts.Marginals(10).Type = 'Uniform';
Iopts.Marginals(10).Parameters = [9.5e6 1.05e7]; % in Pa
Iopts.Marginals(11).Name = ['Root',num2str(11)]; 
Iopts.Marginals(11).Type = 'Uniform';
Iopts.Marginals(11).Parameters = [2.85e6 3.15e6]; % in Pa
Iopts.Marginals(12).Name = ['Root',num2str(12)]; 
Iopts.Marginals(12).Type = 'Uniform';
Iopts.Marginals(12).Parameters = [9.5e6 1.05e7]; % in Pa
% create the input obj
myInput = uq_createInput(Iopts);
% number of run of nastran
X = uq_getSample(700,'Sobol'); % i will run three different conf of the 6 root stiffness identify in the tamplate
                           % i will replace each X(i,:) in a different .bdf
                           % file
%% read the tamplate and open a new file
% define the marker in the tamplate
marker = {'<', 'X', '>'} ;
bound = strcat(marker{1},marker{3});
%number of run 
Nrun = size(X,1);
pathTamplate = ' ';
executionPath = ' ';
InputExt = 'bdf';

%define the input file for each run of the code.

for i1 = 1:Nrun
    inputName = strcat('InputRun',num2str(i1));
    inputFile = fullfile(executionPath,strcat(inputName, '.', InputExt));
    finp = fopen(inputFile,'w'); %open file for writing only
    if finp < 0; error('error in input file %d',i1); end
    % open or reading the tamplate file 
    ftemp = fopen(pathTamplate,'r');
    if ftemp<0
        error('Error in tamplate opening');
    end
    while ~feof(ftemp) % read template untill the end
        str = fgetl(ftemp); % the current line in a string
        flag1 = regexp(str,'PELAS');
        flag2 = regexp(str,'PBUSH');
        if (~isempty(flag1)) || (~isempty(flag2))
        for i2 = 1:size(X,2)
            % variable name i need to find in the file x(boh,i2);
            expression = strcat(marker{1}, marker{2}, num2str(i2,'%05u'), marker{3});
            % convert into a string with correct formatting the number
            strX = num2nastranBDF(X(i1,i2));
            
            % search for expression in the current list (str), if found
            % replaced it by strX
            str = regexprep(str, expression,strX);
        end
        end
        % Copy the current line (possibly modified) of the template file
        % into the new input file
        fprintf(finp,'%s\r\n', str);
    end 
    fclose(ftemp);
    status_file = fclose(finp) ; % 0 if ok, -1 if nok
    if status_file ~=0
        error('Something went wrong when closing the new generated input file') ;
    end

end
%% creation file to execute (.bat)
pathNastran = ' '; %path to nastran.exe
pathEXE = ' '; %path to the .bdf files
% creation of the file 
inputName = 'ToExeNastra';
inputFile = fullfile(pathEXE,strcat(inputName, '.txt'));
finE = fopen(inputFile,'w'); %open file for writing only
if finE < 0; error('error in openin .bat file'); end
for i1=1:Nrun
    inputFile = strcat('InputRun',num2str(i1));
    inputFile = fullfile(executionPath,strcat(inputFile, '.', InputExt));
    fprintf(finE,'%s ', pathNastran);
    fprintf(finE,'"%s" \n', inputFile);
    fprintf(finE,'TIMEOUT 2\n');
end 
status_file = fclose(finE) ; % 0 if ok, -1 if nok
if status_file ~=0
    error('Something went wrong when closing the new generated .bat file');
end
%% after running nastran from prompt some cleaning 
% delete *.DBALL
% delete *.MASTER
% delete *.IFPDAT
% delete *.xdb
%% open and save the result in a .mat 
% for running this section i need to add to the path nastran_output folder
% allocating memory for the output 
% Y = zeros(10,Nrun);
for i1 = 1:Nrun
    outFilename = strcat('InputRun',num2str(i1));
    outFilename = strcat(outFilename, '.f06');
    Y(:,i1) = uq_readNastranOutput(outFilename);
end 