function [DynamicResult]=SDOFDynamicAnalysis_SactNC(OSpath,SDOFdata,ACCdata)


% perform Dynamic analysis for SDOF system. 
% 
% INPUT DATA
% 
%   - OSPath=is a path and exe file of OpenSees
%            for example: 'D:\opensees162\opensees.exe'
%   - SDOFdata is a structure containing following field
%     m    = mass of SDOF system
%     F    = forces of the F-U envelope of SDOF system
%     U    = displalement of the F-U envelope of SDOF system
%     hysteretic = parameters of hysteretic behaviour
%            Hysteretic material : [pinchX pinchY d1 d2 beta]
%            Takeda material     : [beta]
%     dynamic=[tInteg alfaM betaK betaKcomm betaKinit]
%             !!! Newmark integration scheme is used (gama=0.5, beta 0.25)
% 
%  - ACCdata is a structure containing following field
%    name= {'acc file name'...}
%    path= {'acc path'...}, e.g. 'D:/SDOF/acc/' (!!! tcl notation)
%    dt  = [dt1...] time step in acc file

%
% OUTPUT DATA
%
%  - DynamicResult is a structure containing following field:
%    maxD    - max displacement of IDA analysis
%    PGA     - pga (g) of dynamic analysis
%    Sa      - spectral acceleration at T1 (g) of dynamic analysis 
%    F       - time-history of forces of SDOF model
%    D       - time-history of displacement of SDOF model
%    tA      - time until the analysis is performed
%
%
%
% Referenced manuscripts:
%
% Dolsek M. (2010). Development of computing environment for the seismic
% performance assessment of reinforced concrete frames by using simplified
% nonlinear models. Bulletin of Earthquake Engineering (Published online
% 16 May 2010), DOI: 10.1007/s10518-010-9184-8. 
%
% Contact: mdolsek@ikpir.fgg.uni-lj.si
%
% Copyright (C) 2010 Matjaz Dolsek
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation, version 2.
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% accelerograms

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input data for SDOF model and dynamic analysis
m=SDOFdata.m;
F=SDOFdata.F;
U=SDOFdata.D;
%Cdrift=max(max(abs(SDOFdata.envelope.UNC)))*1.01;
hysteretic=SDOFdata.hysteretic;
tInteg=SDOFdata.dynamic(1);
alfaM=SDOFdata.dynamic(2);
betaK=SDOFdata.dynamic(3);
betaKcomm=SDOFdata.dynamic(4);
betaKinit=SDOFdata.dynamic(5);


    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input file for OpenSees SDOF model
file = fopen('SDOF.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 Node -file TDsdof.out -time -node 2 -dof 1 2 disp \n');
fprintf(file,'recorder EnvelopeElement -time -file EFsdof.out -ele  1 section force \n');
fprintf(file,'recorder EnvelopeElement -time -file EDsdof.out -ele  1 section deformation \n');
fprintf(file,'recorder Element -time -file Fsdof.out -ele  1 section force \n');
fprintf(file,'recorder Element -time -file Dsdof.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

accName=ACCdata.name;
acc=load(accName);
PGA=max(abs(acc(:,1)));
g=9.81;
dt=ACCdata.dt;
tFinal=dt*max(size(acc));

[s] = spec(acc, 0.05, dt, [SDOFdata.T]);



% starts analysis
IM=PGA*SDOFdata.SactNC/s; % g
sf=SDOFdata.SactNC/s*g;
ACCdata.sf=sf;
ACCdata.tFinal=tFinal;

SDOFDynamicAnalysis(OSpath,ACCdata);
maxDt=load('Dsdof.out');
tA=maxDt(end,1);
maxD=max(abs(maxDt(:,2)));


DynamicResult.F=load('Fsdof.out');    
DynamicResult.D=maxDt;   
DynamicResult.maxD=maxD;      
DynamicResult.PGA=IM; 
DynamicResult.Sa=spec(acc*sf/g, 0.05, dt, [SDOFdata.T]); 
DynamicResult.tA=tA;
DynamicResult.tFinal=tFinal;






    