clear all
%clearvars -except nstartac
OSpath='C:\Program Files (x86)\Tcl\OpenSees\OpenSees246x32.exe';
nGM=40;

% STANDARD INPUT:
cdr=pwd;
for j=1:length(cdr)
    if strcmp(cdr(j),'\')==1
        cdr(j)='/';
    end
end

    
accFolder=[];
m=[3628.34668891074]; % mass of SDOF system in tons
F=[7838.32159230838 7838.32159230838 0.00795645909698035]; % forces of the F-U envelope of SDOF system 
U=[0.0572512667666076 0.326768411034384 0.683291022610579]; % displacements of the F-U envelope of SDOF system
SactNC=0.56;

hysteretic=[0 0 0 0 0.8]; % parameters of hysteretic backbone [pinchx pinchy damage1 damage2 beta]
Period=1.02;  % period of SDOF model
alfaM=4*pi*0.05/Period;
betaK=0;
betaKcomm=0;
betaKinit=0;

for k=1:nGM
accName=(['gmr' num2str(k) '.acc']);
racDT=load(['DT.acc']);
dt=racDT(k);
tInteg=dt/2; 

   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input file for OpenSees SDOF model
file = fopen(['SDOF_acc' num2str(k) '.tcl'], 'w+');

fprintf(file,'# Model for SDOF system \n');
fprintf(file,'model BasicBuilder -ndm 3 -ndf 6 \n');
% definition of nodes and mass
fprintf(file,'#Nodes, restraints, material end element \n');
fprintf(file,'node 1  0.0 0.0 0.0 \n');
fprintf(file,'node 2  0.0 0.0 0.0 -mass %15.3e 0.0 0.0 0.0 0.0 0.0 \n',m);
fprintf(file,'fix  1  1 1 1 1 1 1\n'); %fixed node
fprintf(file,'fix  2  0 1 1 1 1 1\n'); 
%                                               Fp1	Up1	Fp2	Up2	Fp3 Up3 Fm1 Um1 Fm2	Um2 Fm3 Um3 pincX pinchY d1 d2 beta
fprintf(file,'uniaxialMaterial Hysteretic	1   %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e %15.3e \n',...
                                                F(1),U(1),F(2),U(2),F(3),U(3),-F(1),-U(1),-F(2),-U(2),-F(3),-U(3),hysteretic(1),hysteretic(2),hysteretic(3),hysteretic(4),hysteretic(5));


% definition of section aggregator and zero length element
%                                       tag mat1 code1 
fprintf(file,'section Aggregator        1     1   P  \n');
%                                      eleTag iNode jNode secTag 
fprintf(file,'element zeroLengthSection 1 	  1      2      1   \n');

% definition of recorders
fprintf(file,'# definition of recorders \n');
fprintf(file,['recorder Element -time -file Fsdof_acc' num2str(k) '.out -ele  1 section force \n']);
fprintf(file,['recorder Element -time -file Dsdof_acc' num2str(k) '.out -ele  1 section deformation \n']);

% parameters
fprintf(file,'test NormDispIncr 1.0e-6 12 0 \n');
fprintf(file,'algorithm Newton \n');
fprintf(file,'system SparseGeneral -piv \n');
fprintf(file,'numberer RCM \n');
fprintf(file,'constraints Transformation \n');
fprintf(file,'integrator Newmark   0.5  0.25  \n');
fprintf(file,'rayleigh %15.3e %15.3e %15.3e %15.3e \n',alfaM,betaK,betaKcomm,betaKinit);
fprintf(file,'analysis Transient \n');
fprintf(file,'set ok 0 \n');
fprintf(file,'set currentTime 0.0 \n');
fprintf(file,'set napaka 1e-4; \n');
fprintf(file,'set napaka2 1e-4; \n');
fprintf(file,'set dt %15.3e; \n',tInteg);
fprintf(file,'set dt2 %15.3e; \n',tInteg);
fprintf(file,'set dt3 %15.3e; \n',tInteg/2);
fprintf(file,'set dt4 %15.3e; \n',tInteg/2);
fclose(file);


% variables

acc=load(accName);
tFinal=dt*max(size(acc));
[s] = spec(acc, 0.05, dt, Period);
sf=SactNC/s*9.81;


file=fopen(['SDOFdynamic_acc' num2str(k) '.tcl'], 'w+');
   fprintf(file,'set tFinal %15.6e \n',tFinal);
   fprintf(file,'set accX "Path -filePath %s -dt %15.3e -factor %15.3e" \n', accName, dt, sf);
   fprintf(file,'# Define the ground motion excitation \n');
   %                                      tag dir   accel series 
   fprintf(file,'pattern UniformExcitation  1   1  -accel $accX \n');
   
fclose(file);
   
    
file = fopen(['RunSDOF_DA_acc' num2str(k) '.tcl'], 'w+');
    fprintf(file,['source SDOF_acc' num2str(k) '.tcl \n']);
    fprintf(file,['source SDOFdynamic_acc' num2str(k) '.tcl \n']);
    fprintf(file,'source DynamicAnalysis1.tcl \n');
    fprintf(file,'wipe \n');
fclose(file);
        
file = fopen(['RunSDOF_DA_acc' num2str(k) '.bat'], 'w+');
    fprintf(file,['"%s"  RunSDOF_DA_acc' num2str(k) '.tcl'],OSpath);
fclose(file);
    

%eval(['!RunSDOF_DA_acc' num2str(k) '.bat']);
pause(0.2)


end