#----------------------------------------------------------------------------------#

set ModelName C12S3_SF
cd ..

#cd.. Changes the Current Working Directory (One Folder Backward) To Catch The EQ Files  

# Define Time History Loop (Also Can Be Used For 1 Record Analysis)

foreach {record_name dt_record steps scalee} {\
12-Near-R6-C2-0.01-5346-2.91.AT2 0.01 5346 2.91\
12-Near-R1101-C2-0.01-5400-1.29.AT2 0.01 5400 1.29\
12-Near-R1244-C2-0.005-18000-0.88.AT2 0.005 18000 0.88\
12-Near-R1244-C1-0.005-18000-0.98.AT2 0.005 18000 0.98\
12-Near-R1491-C1-0.005-18000-2.09.AT2 0.005 18000 2.09\
12-Near-R1491-C2-0.005-18000-2.06.AT2 0.005 18000 2.06\
12-Near-R1495-C2-0.005-11200-1.65.AT2 0.005 11200 1.65\
12-Near-R1536-C1-0.005-18000-0.61.AT2 0.005 18000 0.61\
12-Near-R1536-C2-0.005-18000-1.05.AT2 0.005 18000 1.05\
12-Near-R1547-C1-0.005-18000-1.41.AT2 0.005 18000 1.41\
12-Near-R1547-C2-0.005-18000-1.47.AT2 0.005 18000 1.47\
12-Near-R5619-C1-0.01-25800-2.83.AT2 0.01 25800 2.83\
12-Near-R5825-C2-0.005-20000-1.45.AT2 0.005 20000 1.45\
12-Near-R5825-C1-0.005-20000-1.37.AT2 0.005 20000 1.37\
12-Near-R6897-C1-0.005-27540-1.69.AT2 0.005 27540 1.69\
12-Near-R6911-C1-0.005-12036-0.73.AT2 0.005 12036 0.73\
12-Near-R6927-C2-0.005-14870-0.96.AT2 0.005 14870 0.96\
12-Near-R6960-C2-0.005-27370-1.24.AT2 0.005 27370 1.24\

} {

wipe
wipeAnalysis

#--------------------#
   source $ModelName/$ModelName.tcl
#--------------------#

#################
# DISPLAY MODEL #
#################

#set scale 30
#recorder display DispShape 450 0 500 400 -wipe
#vup 0 1 0 # dirn defining up direction of view plane
#vpn 0 0 1 # direction of outward normal to view plane
#prp 2 1.50 10 # eye location in local coord sys defined by viewing system
#display 1 -5 1
#viewWindow -$scale $scale -$scale  $scale ;#scale view for Structure


##################
# GRAVITY SOLVER #
##################


constraints Plain ;
numberer RCM ;
system BandGeneral ;
test NormDispIncr 1.e-8 8 2;
algorithm Newton ; 
integrator LoadControl 0.1;
analysis Static ;
analyze 10;
loadConst -time 0.0;

puts "Check: Gravity Load Applied!"


####################
# Define Recorders #
####################

#file mkdir $ModelName/Drifts;
#
#  recorder Drift  -file $ModelName/Drifts/$record_name.out -time -iNode 201021 202025 203025 204025 205025 206025 207025 208025 209025 210025 211025 212025 -jNode 202025 203025 204025 205025 206025 207025 208025 209025 210025 211025 212025 213025 -dof 1 -perpDirn 2

file mkdir $ModelName/Other_Outputs/$record_name;
file mkdir $ModelName/Other_Outputs/$record_name/Accelerations;
file mkdir $ModelName/Other_Outputs/$record_name/Elements;
file mkdir $ModelName/Other_Outputs/Displacements;

recorder Node -file $ModelName/Other_Outputs/Displacements/1stStory_Disp_$record_name.out -time -node 202021 -dof 1 disp
recorder Node -file $ModelName/Other_Outputs/Displacements/Roof_Disp_$record_name.out -time -node 213021 -dof 1 disp

set AccLIST { 202021 203021 204021 205021 206021 207021 208021 209021 210021 211021 212021 213021 }
foreach nodeNum $AccLIST {
		recorder Node -file $ModelName/Other_Outputs/$record_name/Accelerations/$nodeNum.out -time -node $nodeNum -dof 1 accel
            }

set ColumnLIST { 30101 30102 }
foreach colNum $ColumnLIST {
            recorder Element  -file $ModelName/Other_Outputs/$record_name/Elements/ColumnForces_$colNum.out -ele $colNum globalForces
            }


#######################################################

## Time History Analysis (Nonlinear Dynamic Analysis) #
#######################################################

## Ground Motion Acceleration ##


set AccelSeries "Series -dt $dt_record -filePath EQ/$record_name -factor [expr 0.75 * 9.81 * $scalee]";
pattern UniformExcitation 10  1 -accel  $AccelSeries;


## THA Solver ##


constraints Transformation ;
numberer RCM ;
system BandGeneral ;
set tol 1.e-8;
test NormDispIncr $tol 200 2;
set defaultAlgorithmType KrylovNewton;
algorithm $defaultAlgorithmType;
integrator Newmark 0.5 0.25;
analysis Transient ;

set dt $dt_record;
set Nsteps $steps;
set step1 10;
set dt1 [expr $dt/$step1];

# --------------------------------------------------------------------------------------------
for {set step 1} {$step <= $Nsteps} {incr step} {
	set ok [analyze 1 $dt];
	if {$ok != 0} {
		puts "\n******************* reduced time step dt/n ******************\n"
		test NormDispIncr $tol 2000 2;
		for {set i 1} {$i <= $step1} {incr i} {
			puts "\nTrying KrylovNewton (dt/n)...\n"
			algorithm $defaultAlgorithmType;
			set ok [analyze 1 $dt1];
			if {$ok != 0} {
				puts "\nTrying Newton with Initial Tangent (dt/n)...\n"
				algorithm Newton -initial;
				set ok [analyze 1 $dt1];
			}
			if {$ok != 0} {
				puts "\nTrying Broyden (dt/n)...\n"
				algorithm Broyden 8;
				set ok [analyze 1 $dt1];
			}
			if {$ok != 0} {
				puts "\nTrying NewtonWithLineSearch (dt/n)...\n"
				algorithm NewtonLineSearch 0.8;
				set ok [analyze 1 $dt1];
			}
			if {$ok != 0} {
				puts "\nTrying ModifiedNewton (dt/n)...\n"
				algorithm ModifiedNewton;
				set ok [analyze 1 $dt1];
			}
		}
	puts "\n******************* normal time step dt ******************\n"
	}
	test NormDispIncr $tol 200 2;
	algorithm $defaultAlgorithmType;
};

puts "\n\nTIME HISTORY ANALYSIS DONE.";

}
