Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check converg3 #408

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 48 additions & 24 deletions cgyro/bin/cgyro_converge
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ from pygacode.cgyro import data
from pygacode.gacodefuncs import *


directory="/global/cfs/cdirs/cgyrodb/"
# --- to visualize results for one case plot=True------
plot=True

fout = "./cases_results.txt"
fo = open(fout, "w")
if plot:
directory='./'
write_output=False
else:
directory= "/global/cfs/cdirs/cgyrodb/"
write_output=True
fout = "./cases_results_20%.txt"
fo = open(fout, "w")


for path, folders, files in os.walk(directory):
# Open file#
for filename in files:
if 'out.cgyro.info' in filename:

fo.write(f"#-----{path}-----#")
if write_output:
fo.write(f"#-----{path}-----#")
# read minimal data for setup (look in current directory)
sim = data.cgyrodata(f'{path}/',fast=True,silent=True)

Expand All @@ -34,8 +41,9 @@ for path, folders, files in os.walk(directory):

# Simulation length (max time)
tmax = t[-1]
#if len(t)!=tmax:
# fo.write("!! Problem with a time array !!")




# size and number of time windows
#dt = 50.0
Expand All @@ -45,48 +53,64 @@ for path, folders, files in os.walk(directory):
t0 = tmax-nwin*dt


#add plot for visualization
#fig = plt.figure(figsize=(12,6))
if plot:
#add plot for visualization
fig = plt.figure(figsize=(12,6))

plt.xlabel('time [$c_s$/a]')
plt.ylabel('Flux')
colors = mpl.cm.rainbow(np.linspace(0, 1, nwin))


#plt.xlabel('time [$c_s$/a]')
#plt.ylabel('Flux')
colors = mpl.cm.rainbow(np.linspace(0, 1, nwin))
convergance = True

# select field=0 (phi), moment=1 (Q), species (0), and sum over kx
for species_index in range(ns):
field=0 ; moment=1 ;
species=species_index
y = np.sum(sim.ky_flux[species,moment,field,:,:],axis=0)
#plt.plot(t,y, label=f"Q_{species_index}", linestyle=linestyles[species_index], color=colors[species_index])

if plot:
plt.plot(t,y, label=f"Q_{species_index}", linestyle=linestyles[species_index], color=colors[species_index])

average_array=[]

# averaging over all windows (note that time_average function is fast/optimized)
for i in range(nwin):
w=str(tmax-(i+1)*dt)+','+str(tmax-i*dt)
imin,imax=time_index(t,w)


imin,imax=time_index(t,w)

if imax>tmax:
fo.write("!! Problem with a time array !!")
ave = time_average(y[:],t,imin,imax)
average_array.append(ave)
#plt.hlines(y=ave, xmin=imin, xmax=imax, color=colors[i], lw=3, linestyle=linestyles[species_index])
if plot:
plt.hlines(y=ave, xmin=t[imin], xmax=t[imax], color=colors[i], lw=3, linestyle=linestyles[species_index])







# define the convergence by std between last free averaged regions as a pesentage to the total
if np.std(average_array[:3]/np.mean(average_array[:3])) > 0.1:
convergance = False

if np.std(average_array[:3]/np.mean(average_array[:3])) > 0.2:
convergance = False

if convergance:
fo.write("TRUE \n")
if write_output:
fo.write("TRUE \n")
else:
plt.title('Converged')
else:
fo.write("FALSE \n")
#plt.legend()
#plt.show()
if write_output:
fo.write("FALSE \n")
else:
plt.title("Didn't converge")

if plot:
plt.legend()
plt.show()