Skip to content

Commit

Permalink
modify script cgyro_converge
Browse files Browse the repository at this point in the history
  • Loading branch information
Galina Avdeeva committed Sep 20, 2024
1 parent 07f0832 commit d1f1961
Showing 1 changed file with 78 additions and 22 deletions.
100 changes: 78 additions & 22 deletions cgyro/bin/cgyro_converge
Original file line number Diff line number Diff line change
@@ -1,36 +1,92 @@
#!/usr/bin/env python

import numpy as np
import os
import matplotlib as mpl
import matplotlib.pyplot as plt

from pygacode.cgyro import data
from pygacode.gacodefuncs import *

# read minimal data for setup (look in current directory)
sim = data.cgyrodata('./',fast=True,silent=True)

# copy time vector and number of species
t = sim.t
ns = sim.n_species
directory="/global/cfs/cdirs/cgyrodb/"

# read bin.cgyro.ky_flux
sim.getflux()
fout = "./cases_results.txt"
fo = open(fout, "w")

# select field=0 (phi), moment=1 (Q), species (0), and sum over kx
field=0 ; moment=1 ; species=0
y = np.sum(sim.ky_flux[species,moment,field,:,:],axis=0)

# Simulation length (max time)
tmax = t[-1]
for path, folders, files in os.walk(directory):
# Open file#
for filename in files:
if 'out.cgyro.info' in filename:

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

# size and number of time windows
dt = 50.0
nwin = int(tmax/dt)
t0 = tmax-nwin*dt
# copy time vector and number of species
t = sim.t
ns = sim.n_species
linestyles=['-','--',':','-.']
# read bin.cgyro.ky_flux
sim.getflux()

# 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)
ave = time_average(y[:],t,imin,imax)
print(i,ave)

# 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
#nwin = int(tmax/dt)
nwin=8
dt=tmax/nwin
t0 = tmax-nwin*dt


#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))
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])
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)


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])






# 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 convergance:
fo.write("TRUE \n")
else:
fo.write("FALSE \n")
#plt.legend()
#plt.show()


0 comments on commit d1f1961

Please sign in to comment.