% Inputs case files generated from Multistorey_examples parametric study
% Inputs a sample input file for the 2x2 example frame 
% Make sure all of the above files are in the same folder
% Outputs ABAQUS input files to be run in DOS
clc, clear, close all

% Update this link with the output from §4.2 of Example_740_750_760_Multistorey_Frames.m
sims_data = dlmread('para_cases_output_new_20191220142149.txt','\t',2,0);

% Update this link with the base ABAQUS input file 
inp_base_ID = fopen('190430_base_file_editable.inp','r');
inp_base_data = fscanf(inp_base_ID,'%c');

% Create input files
case_num = zeros(1,size(sims_data,1));
P = zeros(2,3,size(sims_data,1));
Ic = zeros(2,3,size(sims_data,1));
Ib = zeros(2,2,size(sims_data,1));
R_conn = zeros(1,size(sims_data,1));
Rl = zeros(1,3,size(sims_data,1));
Pcr = zeros(1,size(sims_data,1));
for sim_num = 1:size(sims_data,1)
    
    % Populate data matrices
    case_num(sim_num) = sims_data(sim_num,1);
    P(1,1,sim_num) = -1*sims_data(sim_num,2); % -1 for downwards!!!
    P(1,2,sim_num) = -1*sims_data(sim_num,3); % -1 for downwards!!!
    P(1,3,sim_num) = -1*sims_data(sim_num,4); % -1 for downwards!!!
    P(2,1,sim_num) = -1*sims_data(sim_num,5); % -1 for downwards!!!
    P(2,2,sim_num) = -1*sims_data(sim_num,6); % -1 for downwards!!!
    P(2,3,sim_num) = -1*sims_data(sim_num,7); % -1 for downwards!!!
    Ic(1,1,sim_num) = sims_data(sim_num,8);
    Ic(1,2,sim_num) = sims_data(sim_num,9);
    Ic(1,3,sim_num) = sims_data(sim_num,10);
    Ic(2,1,sim_num) = sims_data(sim_num,11);
    Ic(2,2,sim_num) = sims_data(sim_num,12);
    Ic(2,3,sim_num) = sims_data(sim_num,13);
    Ib(1,1,sim_num) = sims_data(sim_num,14);
    Ib(1,2,sim_num) = sims_data(sim_num,15);
    Ib(2,1,sim_num) = sims_data(sim_num,16);
    Ib(2,2,sim_num) = sims_data(sim_num,17);
    Rl(1,1,sim_num) = sims_data(sim_num,18);
    Rl(1,2,sim_num) = sims_data(sim_num,19);
    Rl(1,3,sim_num) = sims_data(sim_num,20);
    R_conn(sim_num) = sims_data(sim_num,21);
    Pcr(sim_num) = sims_data(sim_num,22);
    
    % Output an input file for each case
    sim_filename = strcat('inp_para_case_',num2str(sim_num),'.inp');%,'_',...
        %string(datetime('now','TimeZone','local','Format','yMMddHHmmss')),'.inp');
    inp_sim_ID = fopen(sim_filename,'w');
    inp_sim_data = inp_base_data;
    inp_sim_data = strrep(inp_sim_data,'EDIT_JOB_NAME',strcat('sim_case_',num2str(sim_num)));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IB_B11',num2str(Ib(1,1,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IB_B12',num2str(Ib(1,2,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IB_B21',num2str(Ib(2,1,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IB_B22',num2str(Ib(2,2,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IC_C11',num2str(Ic(1,1,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IC_C12',num2str(Ic(1,2,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IC_C13',num2str(Ic(1,3,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IC_C21',num2str(Ic(2,1,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IC_C22',num2str(Ic(2,2,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_IC_C23',num2str(Ic(2,3,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_P11',num2str(P(1,1,sim_num),'%.4f'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_P12',num2str(P(1,2,sim_num),'%.4f'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_P13',num2str(P(1,3,sim_num),'%.4f'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_P21',num2str(P(2,1,sim_num),'%.4f'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_P22',num2str(P(2,2,sim_num),'%.4f'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_P23',num2str(P(2,3,sim_num),'%.4f'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_RL1',num2str(Rl(1,1,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_RL2',num2str(Rl(1,2,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_RL3',num2str(Rl(1,3,sim_num),'%.6e'));
    inp_sim_data = strrep(inp_sim_data,'EDIT_RCONN',num2str(R_conn(sim_num),'%.6e'));
    fprintf(inp_sim_ID,inp_sim_data);
    fclose(inp_sim_ID);
end

% Close files
fclose('all');