Skip to content

Commit

Permalink
cgyro_converge: fix error in the plot of averaged windows and add opt…
Browse files Browse the repository at this point in the history
…ion to swich between analysis of one case or all cases in the directory
  • Loading branch information
Galina Avdeeva committed Sep 20, 2024
1 parent d1f1961 commit a54e5fa
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions cgyro/bin/cgyro_converge
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ import matplotlib.pyplot as plt
from pygacode.cgyro import data
from pygacode.gacodefuncs import *

# --- to visualize results for one case plot=True------
plot=True

directory="/global/cfs/cdirs/cgyrodb/"

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 +40,8 @@ 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 @@ -44,49 +50,57 @@ for path, folders, files in os.walk(directory):
dt=tmax/nwin
t0 = tmax-nwin*dt

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

#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:
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()


0 comments on commit a54e5fa

Please sign in to comment.