示例#1
0
def load_Bwall(folder='./',slice=0,phasing=0.,machine='diiid',cur_up=1.,
               cur_low=1.,ntor=None):
                   
    file = folder+'/Bwall-'+str(slice)+'.txt'
    
    if machine is 'diiid':
        conv = 1.0
    elif machine is 'aug':
        conv = -1.0
        
    fac = geofac(machine=machine,ntor=ntor)
    
#    print fac
        
    cur_up  = fac*cur_up
    cur_low = fac*cur_low
        
    A  = np.loadtxt(file)
    Z  = A[:,0]
    Bu = A[:,1] + 1j*A[:,2]
    Bl = A[:,3] + 1j*A[:,4]
    
    cur = xr.DataArray(np.cos(pi*phasing/180.)+conv*1j*np.sin(pi*phasing/180.),
                             {'phasing':phasing})
                             
    Bwu = cur_up*xr.DataArray(Bu,[('Z',Z)])
    Bwl = cur_low*xr.DataArray(Bl,[('Z',Z)])
    Bwall = cur*Bwu + Bwl
    
    return Bwall
示例#2
0
def calc_puc(hfs='hfs.txt', lfs='lfs.txt', phasing=0., cur_up=1., cur_low=1.,
              machine=None, field=None,  ntor=None):
                  
    if machine is 'diiid':
        conv = 1.0
    elif machine is 'aug':
        conv = -1.0
    elif machine is 'kstar':
        conv = 1.0
        
    phasing = np.asarray(phasing)
    
    fac = geofac(machine=machine,ntor=ntor)            
    cur_up  = fac*cur_up
    cur_low = fac*cur_low
    
        
    dBz    = np.loadtxt(hfs)
    dBh    = dBz[:,0] + 1j*dBz[:,1]
    dBh_tu = dBh[0]
    dBh_tl = dBh[1]
    dBh_vu = dBh[2]
    dBh_vl = dBh[3]
    
    dBz    = np.loadtxt(lfs)
    dBl    = dBz[:,0] + 1j*dBz[:,1]
    dBl_tu = dBl[0]
    dBl_tl = dBl[1]
    dBl_vu = dBl[2]
    dBl_vl = dBl[3]
    
    cur = xr.DataArray(np.cos(pi*phasing/180.)+conv*1j*np.sin(pi*phasing/180.),
                             {'phasing':phasing})
                             
    if   field == 0:
        dBl = cur*cur_up*dBl_vu + cur_low*dBl_vl
        dBh = cur*cur_up*dBh_vu + cur_low*dBh_vl
    elif field == 1:
        dBl = cur*cur_up*(dBl_tu - dBl_vu) + cur_low*(dBl_tl - dBl_vl)
        dBh = cur*cur_up*(dBh_tu - dBh_vu) + cur_low*(dBh_tl - dBh_vl)
    elif field == 2:
        dBl = cur*cur_up*dBl_tu + cur_low*dBl_tl
        dBh = cur*cur_up*dBh_tu + cur_low*dBh_tl
    else:
        print("Error: field = " + str(field) + " unacceptable")
        return None
        
    
    return (dBh, dBl)
示例#3
0
def load_Bres(folder='./',slice=0,phasing=0.,machine='diiid',cur_up=1.,
               cur_low=1.,ntor=None,phase=False):
                   
    fup  = folder+'/Bres_upper-'+str(slice)+'.txt'
    flow = folder+'/Bres_lower-'+str(slice)+'.txt'    
    
    if machine is 'diiid':
        conv = 1.0
    elif machine is 'aug':
        conv = -1.0
        
    fac = geofac(machine=machine,ntor=ntor)
    
#    print fac
        
    cur_up  = fac*cur_up
    cur_low = fac*cur_low
        
    A_up   = np.loadtxt(fup)
    m      = A_up[:,0]
    mag_up = A_up[:,1]
    ph_up  = A_up[:,2]
    Psi    = A_up[:,3]
    q      = A_up[:,4]
    Bru    = mag_up*(np.cos(ph_up) + 1j*np.sin(ph_up))

    A_low   = np.loadtxt(flow)
    mag_low = A_low[:,1]
    ph_low  = A_low[:,2]
    Brl     = mag_low*(np.cos(ph_low) + 1j*np.sin(ph_low))
    
    
    cur = xr.DataArray(np.cos(pi*phasing/180.)+conv*1j*np.sin(pi*phasing/180.),
                             {'phasing':phasing})
                             
    Bres_up  = cur_up*xr.DataArray(Bru,[('Psi',Psi)])
    Bres_low = cur_low*xr.DataArray(Brl,[('Psi',Psi)])
    Bres = cur*Bres_up + Bres_low
    if phase:
        Bres.data = np.angle(Bres.data,deg=True) % 360.
    else:
        Bres.data = np.abs(Bres.data)
    q = xr.DataArray(q,[('Psi',Psi)])
    
    return (Bres, q)
示例#4
0
def load_Bmns(
    folder="./",
    phasing=0.0,
    slice=0,
    cur_up=1.0,
    cur_low=1.0,
    cur_mid=None,
    machine=None,
    iplasma=None,
    code="m3dc1",
    ntor=None,
    phase=False,
    Jmn=False,
):

    if machine is "diiid":
        conv = 1.0
    elif machine is "aug":
        conv = -1.0
    elif machine is "kstar":
        conv = 1.0

    if phase is not True:
        phase = False

    phasing = np.asarray(phasing)

    if code is "all":

        Bc1 = load_Bmns(
            folder=folder,
            phasing=phasing,
            slice=slice,
            cur_up=cur_up,
            cur_low=cur_low,
            machine=machine,
            iplasma=iplasma,
            code="m3dc1",
            ntor=ntor,
        )
        Bmars = load_Bmns(
            folder=folder,
            phasing=phasing,
            slice=slice,
            cur_up=cur_up,
            cur_low=cur_low,
            machine=machine,
            iplasma=iplasma,
            code="mars",
            ntor=ntor,
        )
        Bipec = load_Bmns(
            folder=folder,
            phasing=phasing,
            slice=slice,
            cur_up=cur_up,
            cur_low=cur_low,
            machine=machine,
            iplasma=iplasma,
            code="ipec",
            ntor=ntor,
        )

        return (Bc1, Bmars, Bipec)

    elif code is "m3dc1":

        if Jmn:
            file_up = folder + "/jmn3_upper-" + str(slice) + ".cdf"
            file_low = folder + "/jmn3_lower-" + str(slice) + ".cdf"
        else:
            file_up = folder + "/bmn_upper-" + str(slice) + ".cdf"
            file_low = folder + "/bmn_lower-" + str(slice) + ".cdf"

        ds_up = xr.open_dataset(file_up)
        ds_low = xr.open_dataset(file_low)

        ntor = ds_up.attrs["ntor"]

        fac = geofac(machine=machine, ntor=ntor)
        cur_up = fac * cur_up
        cur_low = fac * cur_low

        m = ds_up.m

        try:
            print("Using Bmn netCDF version " + str(ds_up.version))
            Psi = ds_up.psi_norm
        except AttributeError:
            print("Using Bmn netCDF version 0")
            Psi = ds_up.psi

        Bup = ds_up.bmn_real.data + 1j * ds_up.bmn_imag.data
        Blow = ds_low.bmn_real.data + 1j * ds_low.bmn_imag.data

        if cur_mid is not None:
            if Jmn:
                file_mid = folder + "/jmn_middle-" + str(slice) + ".cdf"
            else:
                file_mid = folder + "/bmn_middle-" + str(slice) + ".cdf"
            ds_mid = xr.open_dataset(file_mid)
            cur_mid = fac * cur_mid
            Bmid = ds_mid.bmn_real.data + 1j * ds_mid.bmn_imag.data

        if (iplasma is not None) and (slice > 0):

            if Jmn:
                file_up = folder + "/jmn3_upper-0.cdf"
                file_low = folder + "/jmn3_lower-0.cdf"
            else:
                file_up = folder + "/bmn_upper-0.cdf"
                file_low = folder + "/bmn_lower-0.cdf"

            ds_vu = xr.open_dataset(file_up)
            ds_vl = xr.open_dataset(file_low)

            Bup -= ds_vu.bmn_real.data
            Bup -= 1j * ds_vu.bmn_imag.data
            Blow -= ds_vl.bmn_real.data
            Blow -= 1j * ds_vl.bmn_imag.data

            if cur_mid is not None:
                if Jmn:
                    file_mid = folder + "/jmn3_middle-0.cdf"
                else:
                    file_mid = folder + "/bmn_middle-0.cdf"
                ds_vm = xr.open_dataset(file_mid)
                Bmid -= ds_vm.bmn_real.data
                Bmid -= 1j * ds_vm.bmn_imag.data

        q = ds_up.q.data

    elif code is "mars":

        file = folder + "/mars_bmn.nc"
        dset = xr.open_dataset(file)

        m = dset.m
        Psi = dset.s ** 2.0

        if slice is 0:
            Bup = dset.vacuum_upper_real.data + 1j * dset.vacuum_upper_imag.data
            Blow = dset.vacuum_lower_real.data + 1j * dset.vacuum_lower_imag.data
        elif slice is 1:
            Bup = dset.total_upper_real.data + 1j * dset.total_upper_imag.data
            Blow = dset.total_lower_real.data + 1j * dset.total_lower_imag.data

            if iplasma is not None:
                Bup -= dset.vacuum_upper_real.data
                Bup -= 1j * dset.vacuum_upper_imag.data
                Blow -= dset.vacuum_lower_real.data
                Blow -= 1j * dset.vacuum_lower_imag.data

        else:
            return NotImplemented

        Psi1 = dset.q_s ** 2.0
        q1 = dset.q
        f = interp1d(Psi1, q1, bounds_error=False)
        q = f(Psi)

        # flip signs because theta is flipped
        conv = -conv
        m = -m
        q = -q

    elif code is "ipec":

        if (slice == 0) or (iplasma is not None):

            # get the vacuum field

            base = folder + "ipec_vbnormal"
            sr = 7
            # skip this many rows
            cr = 3
            # column of real Bmn (zero-based)
            ci = 4
            # column of imaginary Bmn (zero-based)

            (Pv, mv, Bvu, Bvl, qv) = B_ipec(base, sr, cr, ci)

        if slice == 1:

            # get the total field

            base = folder + "ipec_xbnormal"
            sr = 8
            # skip this many rows
            cr = 7
            # column of real Bmn
            ci = 8
            # column of imaginary Bmn

            (Pt, mt, Btu, Btl, qt) = B_ipec(base, sr, cr, ci)

        if slice == 0:
            # just the vacuum field
            (Psi, m, Bup, Blow, q) = (Pv, mv, Bvu, Bvl, qv)
        elif (slice == 1) and (iplasma is None):
            # just the total field
            (Psi, m, Bup, Blow, q) = (Pt, mt, Btu, Btl, qt)

        else:
            # we need to subtract the vacuum from the total field
            Rv = RBS(Pv, mv, Bvu.real)
            Iv = RBS(Pv, mv, Bvu.imag)
            Bvu = Rv(Pt, mt) + 1j * Iv(Pt, mt)

            Rv = RBS(Pv, mv, Bvl.real)
            Iv = RBS(Pv, mv, Bvl.imag)
            Bvl = Rv(Pt, mt) + 1j * Iv(Pt, mt)

            (Psi, m, Bup, Blow, q) = (Pt, mt, Btu - Bvu, Btl - Bvl, qt)

        conv = -conv
        m = -m
        q = -q

    else:
        return NotImplemented

    # Quantities used for all code results

    cur = xr.DataArray(np.cos(pi * phasing / 180.0) + conv * 1j * np.sin(pi * phasing / 180.0), {"phasing": phasing})

    Bmn_up = cur_up * xr.DataArray(Bup, [("Psi", Psi), ("m", m)])
    Bmn_low = cur_low * xr.DataArray(Blow, [("Psi", Psi), ("m", m)])
    Bmn = cur * Bmn_up + Bmn_low

    if cur_mid is not None:
        cur2 = xr.DataArray(
            np.cos(pi * phasing / 180.0) + conv * 1j * np.sin(pi * phasing / 180.0), {"phasing2": phasing}
        )
        Bmn_mid = cur_mid * xr.DataArray(Bmid, [("Psi", Psi), ("m", m)])
        Bmn = Bmn + cur2 * Bmn_mid

    if phase:
        Bmn = xru.angle(Bmn, deg=True) % 360.0
    else:
        Bmn = np.abs(Bmn)

    q = xr.DataArray(q, [("Psi", Psi)])

    if Jmn:
        Bmns = xr.Dataset({"Jmn": Bmn, "q": q})
    else:
        Bmns = xr.Dataset({"Bmn": Bmn, "q": q})

    Bmns.attrs["ntor"] = ntor
    Bmns.attrs["phase"] = phase

    return Bmns