Example #1
0
def copy_run(src,dst):

    if not os.path.isdir(dst):
        os.mkdir(dst)
                
    cp(src+'/adapted0.smb',dst+'/adapted0.smb')
    cp(src+'/batch_slurm',dst+'/batch_slurm')
    cp(src+'/batch_torque',dst+'/batch_torque')
    cp(src+'/C1input',dst+'/C1input')
    cp(src+'/coil.dat',dst+'/coil.dat')
    cp(src+'/current.dat',dst+'/current.dat')
    cp(src+'/diiid0.020.smb',dst+'/diiid0.020.smb')
    cp(src+'/diiid0.02.smd',dst+'/diiid0.02.smd')
    cp(src+'/diiid0.02.sms',dst+'/diiid0.02.sms')
    cp(src+'/diiid0.02.txt',dst+'/diiid0.02.txt')
    cp(src+'/geqdsk',dst+'/geqdsk')
    cp(src+'/profile_ne',dst+'/profile_ne')
    cp(src+'/profile_te',dst+'/profile_te')
    cp(src+'/profile_omega',dst+'/profile_omega')
    cp(src+'/rmp_coil.dat',dst+'/rmp_coil.dat')
    cp(src+'/rmp_current.dat',dst+'/rmp_current.dat')
    cp(src+'/sizefieldParam',dst+'/sizefieldParam')
    
    return
Example #2
0
def extract_profiles(machine="DIII-D", profile="all"):

    if machine in ["DIII-D", "NSTX-U"]:

        profile = "all"
        print "Extracting all profiles from single file"

        if len(glob(r"m3dc1_profiles_*.txt")) != 0:
            mysh.cp(r"m3dc1_profiles_*.txt", "m3dc1_profiles_0.txt")
            prof = "m3dc1_profiles_0.txt"
        elif len(glob(r"p*.*")) != 0:
            mysh.cp(r"p*.*", "p0.0")
            prof = "p0.0"
        else:
            print "Error: EFIT profiles file not found"
            return

        # extract profiles using Nate's utility
        call(["extract_profiles.sh", prof])

        if prof == "p0.0":
            # extact the Carbon toroidal rotation from the p-file
            with open("p0.0", "r") as fin:
                iprint = False
                for line in fin:
                    if not iprint:
                        if "psinorm omeg" in line:
                            iprint = True
                            fout = open("profile_omega.Ctor", "w")
                    else:
                        if "psinorm" not in line:
                            fout.write(line)
                        else:
                            iprint = False
                            fout.close()

        os.remove(prof)

    elif machine in ["AUG"]:

        print "Extracting profile " + profile

        if profile in ["all", "ne"]:
            if len(glob(r"neprof_*.asc")) != 0:
                mysh.cp(r"neprof_*.asc", "neprof_0.asc")
                prof = "neprof_0.asc"
                ne = np.loadtxt("neprof_0.asc")
                ne[:, 0] = ne[:, 0] ** 2
                ne[:, 1] = ne[:, 1] * 1e-20
                ne = ne[ne[:, 0] <= 1.0]
                np.savetxt("profile_ne", ne, fmt="%.6e", delimiter="   ")
                os.remove(prof)

        if profile in ["all", "te", "Te"]:
            if len(glob(r"Teprof_*.asc")) != 0:
                mysh.cp(r"Teprof_*.asc", "Teprof_0.asc")
                prof = "Teprof_0.asc"
                Te = np.loadtxt("Teprof_0.asc")
                Te[:, 0] = Te[:, 0] ** 2
                Te[:, 1] = Te[:, 1] * 1e-3
                Te = Te[Te[:, 0] <= 1.0]
                np.savetxt("profile_te", Te, fmt="%.6e", delimiter="   ")
                os.remove(prof)

        if profile in ["all", "vt", "vtor"]:
            if len(glob(r"vtprof_*.asc")) != 0:
                mysh.cp(r"vtprof_*.asc", "vtprof_0.asc")
                prof = "vtprof_0.asc"
                vt = np.loadtxt("vtprof_0.asc")
                vt[:, 0] = vt[:, 0] ** 2
                vt[:, 1] = vt[:, 1] * 1e-3
                vt = vt[vt[:, 0] <= 1.0]
                np.savetxt("profile_omega.Btor", vt, fmt="%.6e", delimiter="   ")
                os.remove(prof)
                mysh.cp(r"profile_omega.Btor", "profile_omega")

        if profile in ["all", "omgeb", "ExB"]:
            if len(glob(r"omgeb_*.asc")) != 0:
                mysh.cp(r"omgeb_*.asc", "omgeb_0.asc")
                prof = "omgeb_0.asc"
                omgeb = np.loadtxt("omgeb_0.asc")
                omgeb[:, 0] = omgeb[:, 0] ** 2
                omgeb[:, 1] = omgeb[:, 1] * 1e-3
                omgeb = omgeb[omgeb[:, 0] <= 1.0]

                # interpolate profile omega onto Te grid
                if len(glob(r"profile_te")) != 0:
                    Te = np.loadtxt("profile_te")
                    Pte = Te[:, 0]
                    O = interp1d(omgeb[:, 0], omgeb[:, 1], fill_value="extrapolate")
                    om2 = O(Pte)
                    omgeb = np.transpose(np.array([Pte, om2]))

                np.savetxt("profile_omega.ExB", omgeb, fmt="%.6e", delimiter="   ")
                os.remove(prof)
                mysh.cp(r"profile_omega.ExB", "profile_omega")
    return
Example #3
0
def autoC1(task='all',machine='DIII-D',C1inputs=None,
           interactive=True, OMFIT=False, calcs=[(0,0,0)]):
    
    if task == 'all':
        task = 'setup'

    if OMFIT:
        interactive = False

    template = os.environ.get('AUTOC1_HOME')+'/templates/'+ machine + '/'
    
    if machine is 'DIII-D':
        rot = 'eb1'
    elif machine is 'NSTX-U':
        rot = 'eb1'
    elif machine is 'AUG':
        rot = 'eb1'
    else:
        rot = 'eb1'

    C1arch = os.environ.get('M3DC1_ARCH')

    if C1arch == 'sunfire.r6':
        submit_batch = ['sbatch','batch_slurm']
    elif C1arch == 'saturn':
        submit_batch = ['qsub','batch_torque']
    else:
        print 'Error: autoC1 does not support M3DC1_ARCH = '+C1arch
        return 

    if task == 'setup':

        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print 'Setting up equilibrium files in efit/'
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print
        
        os.chdir('efit/')

        if not OMFIT:
            mysh.cp(r'g*.*','geqdsk')
            extract_profiles(machine=machine)

        if machine in ['DIII-D','NSTX-U']:

            fc = open('current.dat','w')
            mysh.cp(r'a*.*','a0.0')
            call(['a2cc', 'a0.0'], stdout=fc)
            fc.close()
            os.remove('a0.0')
            
        if interactive:
            next = '-'
                
            while next not in ['Y','N']:
                   
                next = raw_input('>>> Would you like to extend profile_ne and profile_te? (Y/N) ')
            
                if next == 'Y':
                    print  "Trying: extend_profile('profile_ne',minval=1e-2,psimax=1.1,psimin=0.95,center=0.98,width=0.01,smooth=None)"
                    loop_extprof('profile_ne',minval=1e-2,psimax=1.1,psimin=0.95,
                                 center=0.98,width=0.01,smooth=None)
                    os.rename('profile_ne.extpy','profile_ne')                
                    
                    print  "Trying: extend_profile('profile_te',minval=1e-4,psimax=1.1,psimin=0.95,center=0.98,width=0.01,smooth=None)"
                    loop_extprof('profile_te',minval=1e-4,psimax=1.1,psimin=0.95,
                                 center=0.98,width=0.01,smooth=None)                
                    os.rename('profile_te.extpy','profile_te')
                    
                    print
                    print  'Using extended profiles'
                    print
                elif next == 'N':
                    print  'Using default profiles'
                else:
                    print '*** Improper response ***'
        
        os.chdir('..')
    
        task = 'efit'
        
        print
        print
    
    else:
        print 'Skipping setup of equilibrium files'
        print
        
    
    if task == 'efit':
    
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print 'Calculating EFIT equilibrium in uni_efit/'
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print
    
        if not os.path.isdir('uni_efit/'):
            sh.copytree(template+'uni_efit/','uni_efit')
        
        load_equil('efit/','uni_efit/')
        os.chdir('uni_efit/')
        mod_C1input(C1inputs)
        call(submit_batch)
        print
        
        if machine in ['AUG']:
            while not os.path.exists('time_000.h5'):
                sleep(10)
            call("\idl -e '@get_aug_currents'",shell=True)
        
        os.chdir('..')
    
        task = 'uni_equil'
        
        print
        print
    else:
        print 'Skipping calculation of EFIT equilibrium'
        print
        
    
    if task == 'uni_equil':
    
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print 'Calculating equilibrium with M3D-C1 GS solver in uni_equil/'
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print
    
        if not os.path.isdir('uni_equil/'):
            sh.copytree(template+'uni_equil/','uni_equil')
        
        load_equil('efit/','uni_equil/')
        os.chdir('uni_equil/')
        mod_C1input(C1inputs)
        
        if interactive:
            while True:
                min_iter = raw_input('>>> Please enter minimum iteration number: ')
    
                try:
                    min_iter = int(min_iter)
                    if min_iter < 1:
                        print '*** min_iter must be greater than or equal to 1 ***'                    
                    else:
                        break
                except ValueError:
                    print '*** min_iter must be an integer ***'
        else:
            min_iter = 1
        
        iter = 0
        for iter in range(1,min_iter):
            
            call(submit_batch)
            print
            while not os.path.exists('time_000.h5'):
                sleep(10)
            print  '>>> iter '+str(iter)+' time_000.h5 created'
            print check_output('grep "Final error in GS solution" C1stdout',shell=True)
            
            os.mkdir('iter_'+str(iter))
            move_iter('iter_'+str(iter)+'/')
            
        next = 'Y'
        iter += 1
        
        while next != 'N':
        
            call(submit_batch)
            print
            while not os.path.exists('time_000.h5'):
                sleep(10)
            print
            print  '>>> iter '+str(iter)+' time_000.h5 created'
            print check_output('grep "Final error in GS solution" C1stdout',shell=True)
            os.mkdir('iter_'+str(iter))
            move_iter('iter_'+str(iter)+'/')
        
            if interactive:
                
                print '>>> Check the equilibrium match for iter_'+str(iter)
            
                next = '-'
                
                while next not in ['Y','N']:
                    
                    next = raw_input('>>> Would you like to do another iteration? (Y/N) ')
                    
                    if next == 'Y':
                        iter += 1
                    elif next == 'N':
                        break
                    else:
                        print '*** Improper response ***'
            else:
                next = 'N'
            
        else: 
            print '>>> Continuing to mesh adaptation'
            print '>>> Check the equilibrium match for iter_'+str(iter)
            
        
        print
        print '>>> Stopped equilibrium iteration after ',iter,' iterations'
        
        os.chdir('..')        
        
        if interactive:
            
            next = '-'
                
            while next not in ['Y','N']:
                
                next = raw_input('>>> Would you like to continue onto mesh adaptation? (Y/N) ')
    
                if next == 'Y':
                    mysh.cp('uni_equil/iter_'+str(iter)+'/current.dat','uni_equil/current.dat.good')
                    continue
                elif next == 'N':
                    return
                else:
                    print '*** Improper response ***'
        
        else:
            
            f = plt.figure(figsize=(16,5))
            gs= gridspec.GridSpec(1,4,width_ratios=[4,5,5,5])
            fs = 0.5
            ax = plt.subplot(gs[0])
            C1py.plot_shape(folder=['uni_efit/','uni_equil/iter_1/'],
                            rrange=[1.0,2.5],zrange=[-1.25,1.25],
                            fs=fs,ax=ax,title='Shape')
            leg = ax.legend(fontsize=24*fs,frameon=True,loc=[0.6,0.85])
            leg.get_frame().set_facecolor('white')
            plts = [('ne',r'$n_e$',plt.subplot(gs[1])),
                    ('te',r'$T_e$',plt.subplot(gs[2])),
                    ('ti',r'$T_i$',plt.subplot(gs[3]))]
            for field,title,ax in plts:
                C1py.plot_field(field,filename='uni_equil/iter_1/C1.h5',
                                slice=-1,rrange=[1.0,2.5],zrange=[-1.25,1.25],
                                lcfs=True,range=[-1,1],fs=fs,ax=ax,
                                title=title,palette='coolwarm')
            f.tight_layout()
            plt.show()
            f.savefig('equil_check.pdf')
            
            mysh.cp('uni_equil/iter_'+str(iter)+'/current.dat','uni_equil/current.dat.good')
                    
    
        task = 'adapt'
        
        print
        print
        
    else:
        print 'Skipping calculation of equilibrium with M3D-C1 GS solver'
        print
    
    if task == 'adapt':
        
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print 'Adapting mesh to equilibrium in rw1_adapt/'
        print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
        print
    
        if not os.path.isdir('rw1_adapt/'):
            sh.copytree(template+'rw1_adapt/','rw1_adapt')
        
        load_equil('efit/','rw1_adapt/')
        mysh.cp('uni_equil/current.dat.good', 'rw1_adapt/current.dat')
        os.chdir('rw1_adapt/')
        mod_C1input(C1inputs)
        
        call(submit_batch)
        print
        
        print  '>>> Wait for adapted0.smb to be created'
        while not os.path.exists('adapted0.smb'):
            sleep(10)
        
        print  '>>> Mesh adaptation complete'
    
        os.chdir('..')
    
        task = 'calculation'
        
        print
        print
    
    else:
        print 'Skipping mesh adaptation'
        print
    
    
    if interactive:
        calcs = None
    else:
        ncalc = 0
    
    opts = {'0':'exit',
            '1':'equilibrium',
            '2':'stability',
            '3':'response',
            '4':'examine'}

    while True:

        if task == 'calculation':
            
            if interactive:
                
                print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
                print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
                print
                print '>>> What kind of calculation would you like to perform?'
                print '>>> 1)  Equilibrium'
                print '>>> 2)  Linear stability'
                print '>>> 3)  Linear 3D response'
                print '>>> 4)  Examine results with IDL'
                print
                
                option = '-'
                
                while option not in opts:
                    option = raw_input('>>> Please enter the desired option (1-4, 0 to exit): ')
                    if option not in opts:
                        print '*** Improper response ***'
                        
                print
                
                task = opts[option]
                
            else:
                    
                if ncalc == len(calcs):
                    task = exit
                else:
                    
                    option, ntor, nflu = calcs[ncalc]
                    task = opts[option]
                    print task + ' calculation'
                    if ntor is not None:
                        print '   n='+ntor 
                    if nflu is not None:
                        print '   '+nflu+'-fluid'
                    print
        
        if task == 'exit':
            print 'Exiting'
            return
            
        elif task == 'equilibrium':
            
            print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
            print 'Calculate equilibrium with adapted mesh'
            print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
            print
            
            if not os.path.isdir('rw1_equil/'):
                sh.copytree(template+'rw1_equil/','rw1_equil')
        
            load_equil('rw1_adapt/','rw1_equil/')
            mysh.cp('rw1_adapt/adapted0.smb', 'rw1_equil/adapted0.smb')
            os.chdir('rw1_equil/')
            mod_C1input(C1inputs)
        
            call(submit_batch)
            print
            
            print  '>>> Job m3dc1_equil submitted'
            
            if interactive:
                print  '>>> You can do other calculations in the meantime'
                raw_input('>>> Press <ENTER> twice to start another calculation')
                raw_input('>>> Press <ENTER> again to proceed')
            
            os.chdir('..')
            print
            print
    
        elif task == 'stability':
            
            print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
            print 'Calculate linear stability'
            print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
            print
        
            if interactive:
                while True:
                    ntor = raw_input('>>> Please enter the desired ntor: ')
    
                    try:
                        int(ntor)
                        break
                    except ValueError:
                        print '*** ntor must be an integer ***'
    
    
                nflu = ''
                while nflu not in ['1','2']:
                    
                    nflu = raw_input('>>> How many fluids? (1 or 2) ')
                        
                    if nflu not in ['1','2']:
                        print '*** Improper response ***'                
                
                print
            
            print 'Calculating stability for '+nflu+'F and ntor = ' + ntor
    
            ndir = 'n='+ntor+'/'
            
            if not os.path.isdir(ndir):
                os.mkdir(ndir)
            os.chdir(ndir)
            
            stab = rot+'_'+nflu+'f_stab/'
            if not os.path.isdir(stab):
                sh.copytree(template+'n=/'+stab,stab)
        
            load_equil('../rw1_adapt/',stab)
            mysh.cp('../rw1_adapt/adapted0.smb', stab+'adapted0.smb')
            os.chdir(stab)
            mod_C1input(C1inputs)
            for line in fileinput.input('C1input',inplace=1):
                print re.sub(r'ntor = ','ntor = '+ntor,line.rstrip('\n'))
        
            call(submit_batch)
            print
            print  '>>> Job m3dc1_stab submitted'
            
            if interactive:
                print  '>>> You can do other calculations in the meantime'
                raw_input('>>> Press <ENTER> twice to start another calculation')
                raw_input('>>> Press <ENTER> again to proceed')
            
            os.chdir('../..')
            print
            print
            
        elif task == 'response':
            
            print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
            print 'Calculate 3D plasma response'
            print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
            print
                
            if interactive:  
                while True:
                    ntor = raw_input('>>> Please enter the desired ntor: ')
    
                    try:
                        int(ntor)
                        break
                    except ValueError:
                        print '*** ntor must be an integer ***'
                    
                nflu = ''
                while nflu not in ['1','2']:
                    
                    nflu = raw_input('>>> How many fluids? (1 or 2) ')
                        
                    if nflu not in ['1','2']:
                        print '*** Improper response ***'
                             
                
                print
                
            print 'Calculating '+nflu+'F response for ntor = ' + ntor
    
            ndir = 'n='+ntor+'/'
            
            if not os.path.isdir(ndir):
                os.mkdir(ndir)
            os.chdir(ndir)
            
            up = rot+'_'+nflu+'f_iu/'
            low = rot+'_'+nflu+'f_il/'
            
            if not os.path.isdir(up):
                sh.copytree(template+'n=/'+up,up)
            load_equil('../rw1_adapt/',up)
            mysh.cp('../rw1_adapt/adapted0.smb', up+'adapted0.smb')
            os.chdir(up)
            mod_C1input(C1inputs)
            for line in fileinput.input('C1input',inplace=1):
                print re.sub(r'ntor = ','ntor = '+ntor,line.rstrip('\n'))
            call(submit_batch)
            
            os.chdir('..')
            
            if not os.path.isdir(low):
                sh.copytree(template+'n=/'+low,low)
            load_equil('../rw1_adapt/',low)
            mysh.cp('../rw1_adapt/adapted0.smb', low+'adapted0.smb')
            os.chdir(low)
            mod_C1input(C1inputs)
            for line in fileinput.input('C1input',inplace=1):
                print re.sub(r'ntor = ','ntor = '+ntor,line.rstrip('\n'))
            call(submit_batch)
                
            print
            print  '>>> Jobs m3dc1_iu and m3dc1_il submitted'
            if interactive:            
                print  '>>> You can do other calculations in the meantime'
                raw_input('>>> Press <ENTER> twice to start another calculation')
                raw_input('>>> Press <ENTER> again to proceed')
                
            os.chdir('../..')
            print
            print
            
        elif task == 'examine':
            
            print '>>> Here are some good things to check:'
            print '>>> Is the adapted equilbrium a good shape match?'
            print ">>> Is 'jy' a good match to the EFIT?"
            print ">>> Does 'ne', 'te', 'ti', or 'p' go negative anywhere?"
            print
            
            if interactive:
                next = '-'
                
                while next not in ['Y','N']:
                    
                    next = raw_input('>>> Would you like to check the quality of the equilibrium? (Y/N) ')
                    
                    if next == 'Y':
                        print 'Launching IDL for checking quality of the equilibrium'
                        print 'Recommend the following commands, but modify as need be'
                        print 'e.g., rw1_equil/ can be replaced by a completed response or stability run in n=2/'
                        print "plot_shape,['uni_efit/','rw1_equil/']+'C1.h5',rrange=[1.0,2.5],zrange=[-1.25,1.25],thick=3,/iso"
                        print "plot_flux_average,'jy',-1,file=['uni_efit/','rw1_equil/']+'C1.h5',/mks,/norm,table=39,thick=3,bins=400,points=400"
                        print "plot_field,'ti',-1,R,Z,file='rw1_equil/C1.h5',/mks,points=400,cutz=0.,/ylog"
                        print
                        
                        call(['idl'])
                        
                        okay = '-'
                        
                        while okay not in ['Y','N']:
                            okay = '>>> Is the equilibrium good enough to continue? (Y/N) '
                            
                            if okay == 'Y':
                                continue
                            elif okay == 'N':
                                return
                            else:
                                print '*** Improper response ***'
                    
                    elif next == 'N':
                        print  'Continuing, but equilibrium may have problems'
                    else:
                        print '*** Improper response ***'
            
            else:
                print ">>> In non-interactive mode"
                print ">>> Please launch IDL separately"
            
        task = 'calculation'
        if not interactive:
            ncalc +=1 
    
    return    
Example #4
0
def load_equil(src,dst):
    cp(src+'/geqdsk',dst+'/geqdsk')
    cp(src+'/profile_ne',dst+'/profile_ne')
    cp(src+'/profile_te',dst+'/profile_te')
    cp(src+'/profile_omega',dst+'/profile_omega')
    cp(src+'/profile_vphi',dst+'/profile_vphi')
    cp(src+'/current.dat',dst+'/current.dat')
    return