Example #1
0
def minimize(pmt, q, ID):
    stop = 0
    ps = make_ps()
    ps[0] = [
        0.19067284, 0.13088517, 0.09616244, 0.16611313, 0.14309298, 0.24170699,
        0.51105228, 0.21396462, 0.7625556, 0.57751496, 1.19543109, 0.7949252,
        28.36920857, 0.1
    ]
    PS = np.zeros((1000, np.shape(ps)[1]))
    ls = []
    while not stop:
        stop = 1
        count = 0
        if pmt >= 0:
            ps[:, pmt] = q
        l = np.zeros(np.shape(ps)[0])
        for i in range(len(l)):
            l[i] = L(ps[i], pmt, q, PS, ls, ID)
        while len(ls) < 550:
            count += 1
            h = 0.5
            a = 1
            g = 1
            s = 0.5
            ind = np.argsort(l)
            m = np.mean(ps[ind[:-1]], axis=0)

            r = m + a * (m - ps[ind[-1]])
            lr = L(r, pmt, q, PS, ls, ID)
            if l[ind[0]] < lr and lr < l[ind[-2]]:
                ps[ind[-1]] = r
                l[ind[-1]] = lr
            elif lr < l[ind[0]]:
                e = m + g * (r - m)
                le = L(e, pmt, q, PS, ls, ID)
                if le < lr:
                    ps[ind[-1]] = e
                    l[ind[-1]] = le
                else:
                    ps[ind[-1]] = r
                    l[ind[-1]] = lr
            else:
                c = m + h * (ps[ind[-1]] - m)
                lc = L(c, pmt, q, PS, ls, ID)
                if lc < l[ind[-1]]:
                    ps[ind[-1]] = c
                    l[ind[-1]] = lc
                else:
                    for i in ind[1:]:
                        ps[i] = ps[ind[0]] + s * (ps[i] - ps[ind[0]])
                        l[i] = L(ps[i], pmt, q, PS, ls, ID)
Example #2
0
def minimize(pmt, q, ID):
    stop=0
    ps=make_ps()
    ps[:,0]=0
    PS=np.zeros((2000, np.shape(ps)[1]))
    ls=[]
    while not stop:
        stop=1
        count=0
        # if q>=0:
        #     ps[:,1]=q
        # if ID==1:
        #     ps[:,0]=1
        l=np.zeros(np.shape(ps)[0])
        for i in range(len(l)):
            l[i]=L(ps[i], pmt, q, PS, ls, ID)
        while len(ls)<1750:
            count+=1
            h=0.5
            a=1
            g=1
            s=0.5
            ind=np.argsort(l, axis=0)
            m=np.mean(ps[ind[:-1]], axis=0)

            r=m+a*(m-ps[ind[-1]])
            lr=L(r, pmt, q, PS, ls, ID)
            if l[ind[0]]<lr and lr<l[ind[-2]]:
                ps[ind[-1]]=r
                l[ind[-1]]=lr
            elif lr<l[ind[0]]:
                e=m+g*(r-m)
                le=L(e, pmt, q, PS, ls, ID)
                if le<lr:
                    ps[ind[-1]]=e
                    l[ind[-1]]=le
                else:
                    ps[ind[-1]]=r
                    l[ind[-1]]=lr
            else:
                c=m+h*(ps[ind[-1]]-m)
                lc=L(c, pmt, q, PS, ls, ID)
                if lc<l[ind[-1]]:
                    ps[ind[-1]]=c
                    l[ind[-1]]=lc
                else:
                    for i in ind[1:]:
                        ps[i]=ps[ind[0]]+s*(ps[i]-ps[ind[0]])
                        l[i]=L(ps[i], pmt, q, PS, ls, ID)
Example #3
0
def minimize(pmt, q, ID):
    stop = 0
    ps = make_ps()
    # ps[0]=[0.15*160/120, 0.2*105/175, 0.15*95/140, 0.2*130/170, 0.15*125/145, 0.2, 30, np.sqrt(1000*662/30)]
    PS = np.zeros((1000, np.shape(ps)[1]))
    ls = []
    while not stop:
        stop = 1
        count = 0
        if pmt >= 0:
            ps[:, pmt] = q
        l = np.zeros(np.shape(ps)[0])
        for i in range(len(l)):
            l[i] = L(ps[i], pmt, q, PS, ls, ID)
        while len(ls) < 550:
            count += 1
            h = 0.5
            a = 1
            g = 1
            s = 0.5
            ind = np.argsort(l)
            m = np.mean(ps[ind[:-1]], axis=0)

            r = m + a * (m - ps[ind[-1]])
            lr = L(r, pmt, q, PS, ls, ID)
            if l[ind[0]] < lr and lr < l[ind[-2]]:
                ps[ind[-1]] = r
                l[ind[-1]] = lr
            elif lr < l[ind[0]]:
                e = m + g * (r - m)
                le = L(e, pmt, q, PS, ls, ID)
                if le < lr:
                    ps[ind[-1]] = e
                    l[ind[-1]] = le
                else:
                    ps[ind[-1]] = r
                    l[ind[-1]] = lr
            else:
                c = m + h * (ps[ind[-1]] - m)
                lc = L(c, pmt, q, PS, ls, ID)
                if lc < l[ind[-1]]:
                    ps[ind[-1]] = c
                    l[ind[-1]] = lc
                else:
                    for i in ind[1:]:
                        ps[i] = ps[ind[0]] + s * (ps[i] - ps[ind[0]])
                        l[i] = L(ps[i], pmt, q, PS, ls, ID)
Example #4
0
def minimize(rec, param, ind, param_name):
    ls = []
    PS = np.zeros((100, 6))
    stop = 0
    count = 0
    ps = make_ps(rec_to_p(rec))
    ps[:, ind] = param
    l = np.zeros(np.shape(ps)[0])
    for i in range(len(l)):
        print('Init minimizing', i, 'out of', len(l))
        l[i] = L(ps[i], param, ind, param_name, PS, ls)
    while len(ls) < np.shape(PS)[0]:
        print(param_name, '=', param, count)
        count += 1
        h = 0.5
        a = 1
        g = 1
        s = 0.5
        ind = np.argsort(l)
        m = np.mean(ps[ind[:-1]], axis=0)

        r = m + a * (m - ps[ind[-1]])
        lr = L(r, param, ind, param_name, PS, ls)
        if l[ind[0]] < lr and lr < l[ind[-2]]:
            ps[ind[-1]] = r
            l[ind[-1]] = lr
        elif lr < l[ind[0]]:
            e = m + g * (r - m)
            le = L(e, param, ind, param_name, PS, ls)
            if le < lr:
                ps[ind[-1]] = e
                l[ind[-1]] = le
            else:
                ps[ind[-1]] = r
                l[ind[-1]] = lr
        else:
            c = m + h * (ps[ind[-1]] - m)
            lc = L(c, param, ind, param_name, PS, ls)
            if lc < l[ind[-1]]:
                ps[ind[-1]] = c
                l[ind[-1]] = lc
            else:
                for i in ind[1:]:
                    ps[i] = ps[ind[0]] + s * (ps[i] - ps[ind[0]])
                    l[i] = L(ps[i], param, ind, param_name, PS, ls)
Example #5
0
def minimize():
    stop = 0
    while not stop:
        count = 0
        # ps=make_ps(rec_to_p(rec))
        ps = make_ps()
        l = np.zeros(np.shape(ps)[0])
        for i in range(len(l)):
            l[i] = L(ps[i])
        while count < 1000:
            count += 1
            h = 0.5
            a = 1
            g = 1
            s = 0.5
            ind = np.argsort(l)
            m = np.mean(ps[ind[:-1]], axis=0)

            r = m + a * (m - ps[ind[-1]])
            lr = L(r)
            if l[ind[0]] < lr and lr < l[ind[-2]]:
                ps[ind[-1]] = r
                l[ind[-1]] = lr
            elif lr < l[ind[0]]:
                e = m + g * (r - m)
                le = L(e)
                if le < lr:
                    ps[ind[-1]] = e
                    l[ind[-1]] = le
                else:
                    ps[ind[-1]] = r
                    l[ind[-1]] = lr
            else:
                c = m + h * (ps[ind[-1]] - m)
                lc = L(c)
                if lc < l[ind[-1]]:
                    ps[ind[-1]] = c
                    l[ind[-1]] = lc
                else:
                    for i in ind[1:]:
                        ps[i] = ps[ind[0]] + s * (ps[i] - ps[ind[0]])
                        l[i] = L(ps[i])
Example #6
0
def thetal(T, p, wv, wl):
    """
    thetal(T,p,wv,wl)

    Calculates the liquid water potential temperature of a parcel.

    Parameters
    - - - - - -
    T : float
        Temperature (K).
    p : float
        Pressure (Pa)
    wv : float
        Vapour mixing ratio (kg/kg).
    wl : float
        Liquid water potential temperature (kg/kg).


    Returns
    - - - -
    thetalOut : float
        Liquid water potential temperature (K).


    References
    - - - - - -
    Emanuel 4.5.15 p. 121

    Examples
    - - - - -
    >>> thetal(300., 8.e4, 0.03, 0.01)
    298.21374486200278
    
    """
    c = constants()
    Lval = L(T)
    wt = wv + wl
    chi = (c.Rd + wt * c.Rv) / (c.cpd + wt * c.cpv)
    gamma = wt * c.Rv / (c.cpd + wt * c.cpv)
    thetaVal = T * (c.p0 / p)**chi
    term1 = (1 - wl / (1. + wt)) * (1 - wl / (c.eps + wt))**(chi - 1)
    term2 = (1 - wl / wt)**(-1. * gamma)
    cp = c.cpd + wt * c.cpv
    thetalOut = thetaVal * term1 * term2 \
                * np.exp(-1. * Lval * wl/(cp*T))

    return thetalOut
Example #7
0
              format(name=token_name,
                     text="'" + text +
                     "'" if token_name != "FLOATING" else text,
                     st_line=tok_start_line,
                     st_col=tok_start_column,
                     end_line=tok_end_line,
                     end_col=tok_end_column),
              end="\t")
    print()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('file', help='L-program file name')
    args = parser.parse_args()

    file_stream = FileStream(args.file)
    lexer = Lexer(file_stream)

    class MyErrorListener:
        def syntaxError(self, recognizer, offendingSymbol, line,
                        charPositionInLine, msg, exc):
            print('Syntax error at ({line}:{char}): {msg}'.format(
                line=line - 1, char=charPositionInLine, msg=msg))
            sys.exit(1)

    lexer.removeErrorListeners()
    lexer.addErrorListener(MyErrorListener())

    output(lexer)