Пример #1
0
def run_epednn(inputs, outputs, betan):
    eped_inputs = {}
    eped_inputs['r'] = inputs['R']
    eped_inputs['ip'] = inputs['Ip']
    eped_inputs['a'] = outputs['a']
    eped_inputs['bt'] = inputs['Bt']
    eped_inputs['kappa'] = inputs['kappa']
    eped_inputs['delta'] = inputs['delta']
    eped_inputs['neped'] = 10.0 * outputs[
        'ne_ped']  #convert 10^20 m^-3 to 10^19 m^-3
    eped_inputs['zeffped'] = inputs['zeff']
    eped_inputs['betan'] = betan
    mixing = 'dt'
    if mixing == 'dt':
        eped_inputs['m'] = 2.5
    else:
        eped_inputs['m'] = 2.0

    model = 'eped1nn/models/EPED_mb_128_pow_norm_common_30x10.pb'
    nn_inputs = np.atleast_2d([[
        eped_inputs['a'], eped_inputs['betan'], eped_inputs['bt'],
        eped_inputs['delta'], eped_inputs['ip'], eped_inputs['kappa'],
        eped_inputs['m'], eped_inputs['neped'], eped_inputs['r'],
        eped_inputs['zeffped']
    ]])
    with btf_connect(path=model) as tf:
        eped_run = tf.run(nn_inputs)
        pped = eped_run[0, 4]
        wped = eped_run[0, -3]

    return wped, pped
Пример #2
0
def run_tglf(inputs, outputs, tglf_model, ne, Te, gridpoint, betap):

    Rmaj0 = inputs['R'] * 1.e2  # m to cm
    a = outputs['a'] * 1.e2  # m to cm
    Bt = inputs['Bt'] * 1.e4  # T to Gauss
    q0 = inputs['q0']
    q95 = outputs['q95']
    zeff = inputs['zeff']
    kappa1 = inputs['kappa']
    delta1 = inputs['delta']

    r = np.linspace(0, a, 101)
    dr = r[1] - r[0]

    # Shafranov shift estimated as linear interpolation between R0 - (R0+betap*r*r/R), ~10% underestimated comparing to geqdsk
    Rmaj = np.ones(101) * (Rmaj0 + (betap + 0.5) * r[-1] * r[-1] / Rmaj0) - (
        betap + 0.5) * r * r / Rmaj0
    dRmaj = np.gradient(Rmaj, dr)
    # New shifted elipse estimation of dRmaj

    kappa0 = 0.5 * (1. + kappa1)
    kappa = kappa0 * np.ones(101) + (kappa1 - kappa0) * r**2 / a**2
    delta = 0.5 * delta1 * (r / a + r**2 / a**2)

    dlogTdr = np.gradient(Te, dr) / Te
    dlogndr = np.gradient(ne, dr) / ne

    skappa = r * np.gradient(kappa, dr) / kappa
    sdelta = r * np.gradient(delta, dr)

    q = q0 + (q95 - q0) * r**2 / (0.95 * a)**2

    #rough estimate of psi for elongated plasma (found to be 1.11 times overestimated compared to geqdsk)
    #This error could cause ~10% difference in flux when converting to Gyro-Bohm units to real units

    #psi = kappa[1:]*Bt*scipy.integrate.cumtrapz(r/q,dx=dr)
    #psi = np.append(0.0,psi)
    #bunit = q/r*np.gradient(psi,dr)
    # New Miller psi estimate based on Stacey et al 2009
    bunit = np.zeros(101)
    for i in range(1, 101):
        theta = np.linspace(0, 2.0 * np.pi, 100)
        dtheta = theta[1] - theta[0]
        x = np.arcsin(delta[i])
        sdelta_stacey = sdelta[i] / np.sqrt(1. - delta[i]**2)
        bigtheta = np.sqrt(
            np.sin(theta + x * np.sin(theta))**2 *
            (1. + x * np.cos(theta))**2 + kappa[i]**2 * np.cos(theta)**2)
        bigtheta *= 1.0 / (np.cos(x * np.sin(theta)) +
                           2.0 * dRmaj[i] * np.cos(theta) +
                           (skappa[i] - sdelta_stacey * np.cos(theta) +
                            (1. + skappa[i]) * x * np.cos(theta)) *
                           np.sin(theta) * np.sin(theta + x * np.sin(theta)))
        line_integrand = 1.0 / (
            1 + r[i] / Rmaj[i] * np.cos(theta + x * np.sin(theta))) / bigtheta
        Ztmp = kappa[i] * r[i] * np.sin(theta)
        Rtmp = Rmaj[i] + r[i] * np.cos(theta + x * np.sin(theta))
        dRdt = np.gradient(Rtmp, dtheta)
        dZdt = np.gradient(Ztmp, dtheta)
        area = scipy.integrate.cumtrapz(
            np.sqrt(dRdt**2 + dZdt**2) * line_integrand, theta)
        bunit[i] = Bt * kappa[i] * (area[-1] / 2.0 / np.pi / r[i])
    bunit[0] = bunit[1]

    if tglf_model == 'nn':
        input_tglf = {}
    else:
        input_tglf = OMFIT['TGLF_GACODE']['FILES']['input.tglf']
    input_tglf['VEXB_SHEAR'] = 0.0

    mixing = 'dt'
    if mixing == 'dt':
        input_tglf['NS'] = 5
        input_tglf['MASS_1'] = 0.000272313
        input_tglf['MASS_2'] = 1.
        input_tglf['MASS_3'] = 1.5
        input_tglf['MASS_4'] = 2.
        input_tglf['MASS_5'] = 10.

        for i in range(1, 6):
            input_tglf['VPAR_' + str(i)] = 0.0
            input_tglf['VPAR_SHEAR_' + str(i)] = 0.0
            input_tglf['RLNS_' + str(i)] = -a * dlogndr[gridpoint]
            input_tglf['RLTS_' + str(i)] = -a * dlogTdr[gridpoint]

        input_tglf['TAUS_1'] = 1.
        input_tglf['TAUS_2'] = inputs['Tratio']
        input_tglf['TAUS_3'] = inputs['Tratio']
        input_tglf['TAUS_4'] = inputs['Tratio']
        input_tglf['TAUS_5'] = inputs['Tratio']

        input_tglf['ZS_2'] = 1
        input_tglf['ZS_3'] = 1
        input_tglf['ZS_4'] = zimp1 = 2
        input_tglf['ZS_5'] = zimp2 = 10
        impurityFraction2 = (zeff - 1.) / (
            5 * zimp1**2 + zimp2**2 - 5 * zimp1 - zimp2
        )  # setting He impurity density to 5 times Neon
        impurityFraction1 = 5.0 * impurityFraction2
        input_tglf['AS_1'] = AS_1 = 1.
        input_tglf['AS_2'] = AS_2 = 0.5 * (1. - zimp2 * impurityFraction2 -
                                           zimp2 * impurityFraction2)
        input_tglf['AS_3'] = AS_3 = 0.5 * (1. - zimp2 * impurityFraction2 -
                                           zimp2 * impurityFraction2)
        input_tglf['AS_4'] = AS_4 = impurityFraction1
        input_tglf['AS_5'] = AS_5 = impurityFraction2
    else:
        input_tglf['NS'] = 3
        input_tglf['MASS_1'] = 0.000272313
        input_tglf['MASS_2'] = 1.
        input_tglf['MASS_3'] = 6.0

        for i in range(1, 4):
            input_tglf['VPAR_' + str(i)] = 0.0
            input_tglf['VPAR_SHEAR_' + str(i)] = 0.0
            input_tglf['RLNS_' + str(i)] = -a * dlogndr[gridpoint]
            input_tglf['RLTS_' + str(i)] = -a * dlogTdr[gridpoint]

        input_tglf['TAUS_1'] = 1.
        input_tglf['TAUS_2'] = inputs['Tratio']
        input_tglf['TAUS_3'] = inputs['Tratio']

        input_tglf['TAUS_4'] = inputs['Tratio']
        input_tglf['TAUS_5'] = inputs['Tratio']

        zimp1 = 6.0
        impurityFraction1 = (zeff - 1.) / (zimp1 * (zimp1 - 1.))
        input_tglf['AS_1'] = AS_1 = 1.
        input_tglf['AS_2'] = AS_2 = 1. - zimp1 * impurityFraction1
        input_tglf['AS_3'] = AS_3 = impurityFraction1
        AS_4 = AS_5 = 0.0

    e = 4.8e-10
    k = 1.6e-12
    me = 2.0 * 1.6e-24 * input_tglf['MASS_1']
    mi = 2.0 * 1.6e-24 * input_tglf['MASS_2']
    c = 3.e10
    c_s = np.sqrt(k * Te / mi)
    input_tglf['BETAE'] = 8. * np.pi * ne[gridpoint] * k * Te[
        gridpoint] / bunit[gridpoint]**2
    loglam = 24.0 - np.log(np.sqrt(ne[gridpoint] / Te[gridpoint]))
    input_tglf['XNUE'] = r[gridpoint] / c_s[gridpoint] * e**4 * np.sqrt(
        2.) * np.pi * ne[gridpoint] * loglam / (np.sqrt(me) *
                                                (k * Te[gridpoint])**1.5)
    input_tglf['ZEFF'] = zeff
    rho_s = c_s / (e * bunit / (mi * c))
    input_tglf['DEBYE'] = 7.43e2 * np.sqrt(Te[gridpoint] /
                                           (ne[gridpoint])) / rho_s[gridpoint]

    input_tglf['RMIN_LOC'] = r[gridpoint] / a
    input_tglf['RMAJ_LOC'] = Rmaj[gridpoint] / a
    input_tglf['ZMAJ_LOC'] = 0.
    input_tglf['DRMINDX_LOC'] = 1.
    input_tglf['DRMAJDX_LOC'] = dRmaj[gridpoint] / a
    input_tglf['DZMAJDX_LOC'] = 0.

    input_tglf['Q_LOC'] = q[gridpoint]
    input_tglf['KAPPA_LOC'] = kappa[gridpoint]
    input_tglf['S_KAPPA_LOC'] = skappa[gridpoint]
    input_tglf['DELTA_LOC'] = delta[gridpoint]
    input_tglf['S_DELTA_LOC'] = sdelta[gridpoint]
    input_tglf['ZETA_LOC'] = 0.0
    input_tglf['S_ZETA_LOC'] = 0.0

    press = ne * Te * (AS_1 + AS_2 + AS_3 + AS_4 + AS_5)
    dpdr = np.gradient(press, dr) / press
    dqdr = np.gradient(q, dr)
    input_tglf['P_PRIME_LOC'] = q[gridpoint] / (
        r[gridpoint] /
        a)**2 * r[gridpoint] * input_tglf['BETAE'] / (8. * np.pi) * (
            (AS_1 + AS_2 * input_tglf['TAUS_2'] + AS_3 * input_tglf['TAUS_3'] +
             AS_4 * input_tglf['TAUS_4'] + AS_5 * input_tglf['TAUS_5']) *
            dpdr[gridpoint])
    s = r[gridpoint] / q[gridpoint] * dqdr[gridpoint]
    input_tglf['Q_PRIME_LOC'] = q[gridpoint]**2 * a**2 / r[gridpoint]**2 * s

    q_gb = ne * k * Te * c_s * (rho_s / a)**2 * 1.0e-7

    nn_inputs = np.atleast_2d([[
        input_tglf['AS_2'], input_tglf['AS_3'], input_tglf['BETAE'],
        input_tglf['DEBYE'], input_tglf['DELTA_LOC'],
        input_tglf['DRMAJDX_LOC'], input_tglf['KAPPA_LOC'],
        input_tglf['P_PRIME_LOC'], input_tglf['Q_LOC'],
        input_tglf['Q_PRIME_LOC'], input_tglf['RLNS_1'], input_tglf['RLNS_2'],
        input_tglf['RLNS_3'], input_tglf['RLTS_1'], input_tglf['RLTS_2'],
        input_tglf['RMAJ_LOC'], input_tglf['RMIN_LOC'],
        input_tglf['S_KAPPA_LOC'], input_tglf['TAUS_2'],
        input_tglf['VEXB_SHEAR'], input_tglf['XNUE'], input_tglf['ZEFF']
    ]] * 2)

    if (tglf_model == 'nn'):
        model = 'tglfnn/models/nn_SAT0_mb_1024_abs_reg_common_stair2x2x6.pb'
        with btf_connect(path=model) as tf:
            qturb = sum(tf.run(nn_inputs)[0, 0:2]) * q_gb[gridpoint]
    else:
        #OMFIT['TGLF_GACODE']['input.tglf']=OMFITgacode(input_tglf)
        OMFIT['TGLF_GACODE']['SCRIPTS']['runTGLF'].run()
        qturb = sum(OMFIT['TGLF_GACODE']['FILES']['gbflux']['data']
                    ['Q/Q_GB']) * q_gb[gridpoint]

    return qturb
Пример #3
0
import sys
sys.path.append("neural")
import numpy as np
from brainfusetf import btf_connect
import time

model = 'eped1nn/models/EPED_mb_128_pow_norm_common_30x10.pb'
#model = 'tglfnn/models/nn_SAT0_mb_1024_abs_reg_common_stair2x2x6.pb'
input = np.atleast_2d([[
    5.45338e-01, 4.17925e-02, 7.21986e-03, 1.24956e-01, -1.37899e-01,
    1.58491e+00, -4.20101e-03, 1.55640e+00, 8.36932e+00, 1.02569e+00,
    2.05921e+00, -4.45231e-01, 3.00670e+00, 2.06023e+00, 2.38220e+00,
    7.66336e-01, 3.20824e-01, 1.14110e+00, 3.21049e-01, 3.36619e-01,
    1.87324e+00
]] * 2)

print input
t0 = time.time()
with btf_connect(path=model) as tf:
    print tf.info()
#with btf_connect(path=model) as tf:
#    print tf.run(input=input)
#print tf.run(input=input)[0,0:2]
# time.sleep(1)
#print tf.run(input=input*3)
#print(time.time()-t0)