def C(T, num_microstates, microstate_sweep_separation, halt_percent_change, sweep_upper_limit, m_n, m_path, initial_seed, initial_world_grid):
	if info: print 'Beginning C for T = %.3f, n = %d' % (T, m_n)

	E_values = []

	# initialize and run to the first convergence	
	if info2: print '\nStarting microstate # 0'

	# loop_till_conv(T, halt_percent_change, sweep_upper_limit, m_n, initial_seed, initial_world_grid)
	equalized_world_grid, sweep_number = loop_till_conv(T, halt_percent_change, sweep_upper_limit, m_n, initial_seed, initial_world_grid)
	world_grid = np.copy(equalized_world_grid)

	# E_values.append( E(m_n, world_grid) )
	E_values.append( sweepMod.find_E(J, NN_type, equalized_world_grid) )

	if debugging_plots:
		# save debugging plots
		name = 'C_T%.2f_n%d_equalized_world_grid' % (T, m_n)
		plot_grid('Equalized', m_path, name, T, None, None, -9, equalized_world_grid)

	# Run more sweeps to generate more E values
	for i in range(num_microstates-1):
		if info2: print '\nStarting microstate # %d' % (i+1)

		for j in range(microstate_sweep_separation):
			# world_grid = sweep(T, m_n, NN_type, world_grid)	
			sweepMod.run_sweep(T, kB, J, NN_type, initial_seed+1+i, world_grid)

		# E_values.append( E(m_n, world_grid) )
		E_values.append( sweepMod.find_E(J, NN_type, world_grid) )

	if debugging_plots:
		# save debugging plots
		name = 'C_T%.2f_n%d_last_sweep_world_grid' % (T, m_n)
		plot_grid('Last Sweep', m_path, name, T, None, None, -9, world_grid)


	# Compute the variance, then C
	variance_E = np.mean( np.square(E_values) ) - np.mean(E_values)**2

	C = variance_E / (kB*T*T)

	if info: print 'C for T = %.3f, n = %d Completed! C = %.4f' % (T, m_n, C)

	return [C, equalized_world_grid]
def loop_till_conv(T, halt_percent_change, sweep_upper_limit, m_n, initial_seed, world_grid):

	num_history = 10
	history = np.linspace(99.0*m_n*m_n, 99.0*m_n*m_n, num_history)

	old_conv_var = 99.0*m_n*m_n
	new_conv_var = 99.0*m_n*m_n

	sweep_number = 0

	# sweep until the mean of the last num_history percent changes in the convergence variable is < halt_percent_change
	while np.mean(history) > halt_percent_change and sweep_number < sweep_upper_limit:

		# world_grid = sweep(T, m_n, NN_type, world_grid)
		sweepMod.run_sweep(T, kB, J, NN_type, initial_seed+sweep_number, world_grid)

		# new_conv_var = E(m_n, world_grid) # Use E as the convergence variable
		new_conv_var = sweepMod.find_E(J, NN_type, world_grid)

		if new_conv_var != 0.0:
			history[sweep_number%num_history] = abs((new_conv_var - old_conv_var)/new_conv_var) # store the percent change
		else:
			# if debugging2: print 'avoiding divide by zero error'
			history[sweep_number%num_history] = 10*halt_percent_change # give it a large non zero number just to kick it back up and keep it moving


#		if info: print 'Sweep #%d, E = %.2f, DeltaE = %.2f, M = %.2f, conv mean = %.5f' % (sweep_number, new_conv_var, new_conv_var - old_conv_var, M_over_N(m_n, world_grid), np.mean(history) ) # sweep info
		# if info and sweep_number > max(0, sweep_upper_limit-50): print 'Sweep #%d, E = %.2f, DeltaE = %.2f, M = %.2f, conv mean = %.5f' % (sweep_number, new_conv_var, new_conv_var - old_conv_var, M_over_N(m_n, world_grid), np.mean(history) ) # sweep info

		old_conv_var = new_conv_var

		sweep_number += 1

	if sweep_number >= sweep_upper_limit: print 'Warning sweep upper limit hit!\nOn T = %.2f' % T

	return [world_grid, sweep_number]
	info = False
	info2 = False


	# C module testing
	T = 1.0
	seed = 7
	n = 500

	initial_world_grid = initialize(n, seed)
	world_grid = np.copy(initial_world_grid)

	for i in range(15):
		print 'On Sweep #%d' % i
		seed += 1
		sweepMod.run_sweep(T, kB, J, NN_type, seed, world_grid)

	t1 = time.time()
	print 'Python E = %.5f' % ( E(n, world_grid) )
	print('Run Time: %s seconds' % (time.time() - t1)) 

	t2 = time.time()
	print 'Compiled C E = %.5f' % ( sweepMod.find_E(J, NN_type, world_grid) )  
	print('Run Time: %s seconds' % (time.time() - t2)) 

	# plot_grid('Initial', output_path, 'initial', T, None, None, -9, initial_world_grid)
	# plot_grid('Final', output_path, 'final', T, None, None, seed, world_grid)


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