# --------------------------------------------------------------------------------------------   
# Dynamic analysis  
# --------------------------------------------------------------------------------------------   
  
set factor {{EQfactor}}
set Eq_factor [expr $factor*9.80665]
set transversal $Eq_factor 
set folder_factor [expr $factor];
   
set dampingfactor 0.05    
   
# VALUES FOR CURRENT ANALYSIS 
set alpha_MATLAB  {{alpha}}
set beta_MATLAB  {{beta}}
set Current_Analysis {{ii}}
 
file mkdir "outputFiles/$Current_Analysis" 
file mkdir "outputFiles/$Current_Analysis/$folder_factor" 


# set recorders  
recorder Node -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_allDispl.out"  -time -nodeRange 1 360 -dof 1 2 3 4 5 6 disp
recorder Node -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_allForce.out"  -time -nodeRange 1 360 -dof 1 2 3 4 5 6 reaction 

recorder Element -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_allElementForce.out"  -time -eleRange 1 100 localForce 
recorder Element -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_allElementDrift.out"  -time -eleRange 1 100 drift 
recorder Element -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_allElementShearDamage.out"  -time -eleRange 1 100 shear state 

# Define dynamic excitation   
# ----------------------------------------------   
set dt_GM  0.005;  
set currentTime 0.0;  
loadConst -time $currentTime   
 
set recordPath  Records/$Current_Analysis/ 
set Ecomp _E.txt 
set Ncomp _N.txt 
set groundMotionPath1 [concat $recordPath$Current_Analysis$Ecomp];  
set groundMotionPath2 [concat $recordPath$Current_Analysis$Ncomp];    
    
set direction1    1    
set direction2	  2   
   
timeSeries Path 3  -dt $dt_GM  -filePath $groundMotionPath1  -factor $transversal   
timeSeries Path 4  -dt $dt_GM  -filePath $groundMotionPath2  -factor $Eq_factor  
#                           $patternTag $dir -accel $tsTag <-vel0 $vel0> <-fact $cFactor>   
pattern UniformExcitation   3   	$direction1   -accel 3    
pattern UniformExcitation   4 	$direction2   -accel 4   
   
recorder Node 	 -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_AccelX.out"    -time -timeSeries 3 -nodeRange 1 360 -dof 1 accel   
recorder Node 	 -file "outputFiles/$Current_Analysis/$folder_factor/analysis3_Dynamic_AccelY.out"  -time -timeSeries 4 -nodeRange 1 360 -dof 2 accel   
   
# Define damping model   
# ----------------------------------------------   
set betaKcurrent    0.0;   
set betaKinitial    1.0;   
set betaKcommitted  0.0;   
   
# User defined. Alpha (mass proportional) 0.955790, beta (initial stiffness proprotional) 0.000870   
rayleigh     $alpha_MATLAB [expr $betaKcurrent* 0.000870]     [expr $betaKinitial* $beta_MATLAB]    [expr $betaKcommitted* 0.000870];   
   
   
# Create the analysis   
# ----------------------------------------------   
#    
constraints Transformation   
numberer RCM    
system SparseGEN;    
integrator Newmark 0.5 0.25    
analysis Transient   
  
  
   
set maxTime {{max_time}}
set subd 1.000000;   
set incr 0.005;   
set substepIfNotConverged  1.   
set counter 0;  
  
  
   
while {$currentTime<$maxTime} {    
    set nSteps [expr int(abs($maxTime-$currentTime)/$incr)]    
    if ($nSteps<1) {    
       break;    
    }    
    test NormUnbalance 15.00e+03 500     
    algorithm 	KrylovNewton     
    
    set ok  [analyze $nSteps  $incr]    
	    
	    
    
    if ($ok!=0) {    
		if ($counter>150) {  
		throw {ARITH DIVZERO {divide by zero}} {divide by zero}  
		}  
		incr counter 1;  
        test NormUnbalance 15.00e+03 500 5    
        algorithm Newton -initial    
        set ok  [analyze [expr int($substepIfNotConverged)]  [expr $incr/$substepIfNotConverged]]      
    }    
  
    set currentTime [getTime]    
    puts [format "Continues from time %.3f s" $currentTime];     
	puts [format "Time is $currentTime and Uncovergence counter is %d " $counter]    
}    
    
    
after 5000     
   
#remove loadPattern 3   
#remove loadPattern 4   
   
  
   
puts "Dynamic analysis completed."   
puts "Time is $[getTime]."   
   
   
    
   
set numModes 10;    
system BandGeneral    
numberer RCM    
constraints Transformation    
# eigenvalues analysis    
set lambda [eigen  $numModes];    
   
set omega {}    
set f {}    
set T {}    
   
foreach lam $lambda {    
  lappend omega [expr sqrt($lam)]    
  lappend f [expr sqrt($lam)/(2.*$pi)]    
  lappend T [expr (2.*$pi)/sqrt($lam)]    
}    
# write output    
set period "outputFiles/$Current_Analysis/$folder_factor/Periodsafterdynamic.out"    
set Periods [open $period "w"]    
set ind 0;    
foreach tt $T {     
   set toPlot    [lindex $f  $ind]		    
   puts $Periods " $tt $toPlot"    
   set ind [expr $ind+1];	    
	 puts [expr $tt]     
}     
close $Periods     
record   
   
   
puts "Eigenvalues analysis completed."    
puts "Above are the periods after the dynamic analysis."   
puts "Time is $[getTime]."   
remove recorders;    
   
   
   
puts "Analysis completed"   
   
after 5000   
   
remove loadPattern 3   
   
   
