def analysis():

                    # for U0 in range(4, 20):
                    nt = len(layout_y)  # Number of turbines
                    summation = 0.0
                    r0 = 40.0  # Turbine rotor radius
                    D = 2.0 * r0
                    A = pi * r0 ** 2.0
                    H = 70.0  # Hub height
                    ia = 0.08  # Ambient turbulence intensity according to vanluvanee. 8% on average

                    def deff(U0):
                        return D * sqrt((1.0 + sqrt(1.0 - Ct(U0))) / (2.0 * sqrt(1.0 - Ct(U0))))

                    rnb = max(1.08 * D, 1.08 * D + 21.7 * D * (ia - 0.05))
                    r95 = 0.5 * (rnb + min(H, rnb))

                    def x0(U0):
                        return 9.5 * D / ((2.0 * r95 / deff(U0)) ** 3.0 - 1.0)

                    def c1(U0):
                        return (deff(U0) / 2.0) ** (5.0 / 2.0) * (105.0 / 2.0 / pi) ** (- 1.0 / 2.0) * (Ct(U0) * A * x0(U0)) ** (- 5.0 / 6.0)  # Prandtl mixing length

                    def distance_to_front(x, y, theta, r):
                        theta = deg2rad(theta)
                        return abs(x + tan(theta) * y - r / cos(theta)) / sqrt(1.0 + tan(theta) ** 2.0)

                    for wind in range(0, len(windrose_angle)):
                    # for wind in range(90, 91):
                        # if wind in [100, 133, 271, 280, 313]:
                        #     continue
                        # U1 = windrose_speed[wind]  # Free stream wind speed
                        # U0 = U1 * (70.0 / 10.0) ** 0.11 # Power or log law for wind shear profile
                            # U0 = U1 * log(70.0 / 0.005) / log(10.0 / 0.005)
                        U0 = 8.5
                        angle = windrose_angle[wind]
                        angle3 = angle + 180.0
                        deficit_matrix = [[0.0 for x in range(nt)] for x in range(nt)]
                        distance = [[0.0 for x in range(2)] for x in range(nt)]

                        U = [U0 for x in range(nt)]
                        total_deficit = [0.0 for x in range(nt)]

                        for tur in range(nt):
                            distance[tur] = [distance_to_front(layout_x[tur], layout_y[tur], angle, 100000000.0), tur]
                        distance.sort()

                        for turbine in range(nt):
                            for num in range(turbine):
                                total_deficit[distance[turbine][1]] += deficit_matrix[distance[turbine][1]][distance[num][1]] ** 2.0
                            total_deficit[distance[turbine][1]] = sqrt(total_deficit[distance[turbine][1]])
                            U[distance[turbine][1]] = U0 * (1.0 - total_deficit[distance[turbine][1]])
                            flag = [False for x in range(nt)]
                            proportion = [0.0 for x in range(nt)]
                            perpendicular_distance = [0.0 for x in range(nt)]
                            parallel_distance = [0.0 for x in range(nt)]
                            for i in range(turbine + 1, nt):
                                proportion[distance[i][1]], flag[distance[i][1]], perpendicular_distance[distance[i][1]], parallel_distance[distance[i][1]] = wake.determine_if_in_wake(layout_x[distance[turbine][1]], layout_y[distance[turbine][1]], layout_x[distance[i][1]], layout_y[distance[i][1]], A, c1(U[distance[turbine][1]]), Ct(U[distance[turbine][1]]), angle3, r0, x0(U[distance[turbine][1]]))
                                if parallel_distance[distance[i][1]] > 0.0: ## Add if proportion is 0, skip operation and deficit_matrix == 0.
                                    deficit_matrix[distance[i][1]][distance[turbine][1]] = proportion[distance[i][1]] * wake.wake_deficit(U[distance[turbine][1]], Ct(U[distance[turbine][1]]), A, parallel_distance[distance[i][1]] + x0(U[distance[turbine][1]]), perpendicular_distance[distance[i][1]], c1(U[distance[turbine][1]]))
                                else:
                                    deficit_matrix[distance[i][1]][distance[turbine][1]] = 0.0

                        # for turb in range(nt):
                        #     for i in range(nt):
                        #         output.write('{0:f}\t'.format(deficit_matrix[turb][i]))
                        #     output.write('\n')
                        #     output2.write('{0:d}\t{1:.1f}\t{2:.1f}\t{3:f}\t{4:f}\t{5:d}\t{6:f}\n'.format(turb, layout_x[turb], layout_y[turb], total_deficit[turb], U[turb], int(angle), power(U[turb])))
                        # output2.write('\n')

                        # for n in range(nt):
                        #     aver[n] += power(U[n]) / 360.0
                        # ---------------------------------------TODO UNCOMMENT -----------------------------------------
                        if withdata:
                            turb_data.write('{0:f} {1:f}\n'.format(angle, power(U[14])))

                        # Farm efficiency
                        profit = 0.0
                        efficiency_proportion = [0.0 for x in range(0, len(windrose_frequency))]
                        for l in range(nt):
                            profit += power(U[l])
                        efficiency = profit * 100.0 / (float(nt) * power(U[distance[0][1]]))
                        efficiency_proportion[wind] = efficiency * windrose_frequency[wind] / 100.0
                    # ---------------------------------------TODO UNCOMMENT ------------------------------------------
                        if withdata:
                            direction.write('{0:f} {1:f}\n'.format(angle, profit))
                        summation += efficiency_proportion[wind]
                    return summation
Пример #2
0
def analysis():

    layout_x = []
    layout_y = []
    for line in layout:
        columns = line.split()
        layout_x.append(float(columns[0]))
        layout_y.append(float(columns[1]))

    windrose_angle = []
    windrose_speed = []
    windrose_frequency = []
    for line in windrose:
        columns = line.split()
        windrose_angle.append(float(columns[0]))
        windrose_speed.append(float(columns[1]))
        windrose_frequency.append(float(columns[2]))

    layout.close()
    windrose.close()

    def Ct(U0):
        return 0.0001923077 * U0**4.0 + -0.0075407925 * U0**3.0 + 0.096462704 * U0**2.0 - 0.5012354312 * U0 + 1.7184749184

    # def power(U0):
    #     return -0.0071427272 * U0**5.0 + 0.5981106302 * U0**4.0 - 18.5218157059 * U0**3.0 + 251.0929636046 * U0**2.0 - 1257.8070377904 * U0 + 2043.2240149783

    def power(U0):
        if U0 < 4.0:
            return 0.0
        elif U0 >= 4.0:
            return 19.7907842158 * U0**2.0 - 74.9080669331 * U0 + 37.257967033  # From 4 to 11 m/s

    # def Cp(U0):
    #     return power(U0) / (0.5 * 1.225 * pi * r0**2.0 * U0**3.0)

    # for U0 in range(4, 20):
    nt = 80  # Number of turbines
    summation = 0.0
    p = [0.0 for x in range(nt)]
    ct = 0.0
    for wind in range(0, len(windrose_speed)):
        ct += 1
        # for wind in range(180-2, 180+3):
        # if wind in [100, 133, 271, 280, 313]:
        #     continue
        # U1 = windrose_speed[wind]  # Free stream wind speed
        # U0 = U1 * (70.0 / 10.0)**0.11 # Power or log law for wind shear profile
        # U0 = U1 * log(70.0 / 0.005) / log(10.0 / 0.005)
        U0 = 8.5
        k = 0.04  # Decay constant
        r0 = 40.0  # Turbine rotor radius
        angle = windrose_angle[wind]
        angle3 = angle + 180.0
        deficit_matrix = [[0.0 for x in range(nt)] for x in range(nt)]
        proportion = [[0.0 for x in range(nt)] for x in range(nt)]
        affected_matrix = [[] for x in range(nt)]

        for turbine in range(0, nt):
            flag = [False for x in range(nt)]
            for i in range(nt):
                if i != turbine:
                    proportion[i][turbine], flag[
                        i] = wake.determine_if_in_wake(layout_x[turbine],
                                                       layout_y[turbine],
                                                       layout_x[i],
                                                       layout_y[i], k, r0,
                                                       angle3)
                elif i == turbine:
                    proportion[i][turbine] = 0.0
                    flag[i] = False
                if flag[i] is True:
                    affected_matrix[i].append(turbine)

        deficit = [0.0 for x in range(nt)]
        length = [0 for x in range(nt)]
        U = [0.0 for x in range(nt)]

        for i in range(nt):
            length[i] = len(affected_matrix[i])
            row.write('{0:f}\t{1:f}\t{2:d}\n'.format(layout_x[i], layout_y[i],
                                                     len(affected_matrix[i])))
        row_vector = [[] for x in range(0, max(length) + 1)]

        for i in range(nt):
            for n in range(0, max(length) + 1):
                if length[i] == n:
                    row_vector[n].append(i)
        total_deficit = [0.0 for x in range(nt)]
        counter = 0
        for n in range(0, max(length) + 1):
            # for n in range(0, 5):
            if n == 0:
                for turb in row_vector[n]:
                    deficit[turb] = 0.0
                    U[turb] = U0
            else:
                for turb in row_vector[n]:
                    for i in affected_matrix[turb]:
                        # print U[i], turb, turb, i, i, counter
                        if U[i] == 0.0:
                            row_vector[n].append(turb)
                            counter += 1
                            continue
                        elif deficit_matrix[turb][i] == 0.0:
                            deficit_matrix[turb][
                                i] = proportion[turb][i] * wake.wake_deficit(
                                    Ct(U[i]), k,
                                    wake.distance(layout_x[turb],
                                                  layout_y[turb], layout_x[i],
                                                  layout_y[i]), r0)
                            total_deficit[turb] += deficit_matrix[turb][i]**2.0
                    if counter >= 20000:
                        break
                    total_deficit[turb] = sqrt(total_deficit[turb])
                    U[turb] = U0 * (1.0 - total_deficit[turb])

        for turb in range(nt):
            for i in range(nt):
                output.write('{0:f}\t'.format(deficit_matrix[turb][i]))
            output.write('\n')
            output2.write(
                '{0:d}\t{1:.1f}\t{2:.1f}\t{3:f}\t{4:f}\t{5:d}\t{6:f}\n'.format(
                    turb, layout_x[turb], layout_y[turb], total_deficit[turb],
                    U[turb], int(angle), power(U[turb])))
        output2.write('\n')

        # Individual turbine efficiency
        # if angle == 0:
        # for g in range(nt):
        #     p[g] += power(U[g])/power(U[row_vector[0][0]])
        turb_data.write('{0:f}\t{1:f}\n'.format(angle, power(U[14])))

        # Farm efficiency
        profit = 0.0
        efficiency_proportion = [
            0.0 for x in range(0, len(windrose_frequency))
        ]
        for l in range(nt):
            profit += power(U[l])
        # print row_vector[0][0]
        efficiency = profit * 100.0 / (float(nt) * power(U[row_vector[0][0]]))
        efficiency_proportion[
            wind] = efficiency * windrose_frequency[wind] / 100.0
        # print 'Farm efficiency with wind direction = {0:d} deg: {1:2.2f}%'.format(int(angle), efficiency)
        print angle, efficiency
        direction.write('{0:f}\t{1:f}\n'.format(angle, efficiency))
        summation += efficiency_proportion[wind]
    print 'total farm efficiency is {0:f} %'.format(summation)

    # for g in range(nt):
    #     turb_data.write('{0:d}\t{1:f}\t{2:f}\t{3:f}\n'.format(g, layout_x[g], layout_y[g], p[g] / ct))
    # turb_data.write('\n')

    turb_data.close()
    output.close()
    output2.close()
    draw.close()
    direction.close()
    row.close()
Пример #3
0
def analysis(v):
    output = open('matrix_larsen.dat', 'w')
    output.write(
        '# This file has the wake deficit matrix per turbine per wind direction\n'
    )
    output2 = open('final_speed_larsen.dat', 'w')
    output2.write(
        '# This file has the deficit, wind speed and power at each turbine per wind direction.\n# Turbine number\tX-coordinate\tY-coordinate\tTotal speed deficit\tTotal wind speed\tWind direction angle\tPower produced\n'
    )
    layout = open('../area_overlap/horns_rev.dat',
                  'r')  # Or horns_rev.dat for full layout
    windrose = open('horns_rev_windrose2.dat', 'r')
    draw = open('draw_horns_rev_larsen.dat', 'w')
    draw.write(
        '# This file has the turbines affected by the wake of one turbine at one direction.\n'
    )
    # draw2 = open('drawline.dat', 'w')
    turb_data = open('turb17_larsenviejo.dat', 'w')
    direction = open('direction_efficiency_larsen.dat', 'w')
    direction.write(
        '# This file includes the efficiency of the whole farm by wind direction.\n# Wind direction angle\tFarm efficiency\n'
    )
    nt = 80  # Or 80 for full layout
    layout_x = []
    layout_y = []
    for line in layout:
        columns = line.split()
        layout_x.append(float(columns[0]))
        layout_y.append(float(columns[1]))

    windrose_angle = []
    windrose_speed = []
    windrose_frequency = []
    for line in windrose:
        columns = line.split()
        windrose_angle.append(float(columns[0]))
        windrose_speed.append(float(columns[1]))
        windrose_frequency.append(float(columns[2]))

    layout.close()
    windrose.close()
    summation = 0.0

    def power(U0):
        if U0 < 4.0:
            return 0.0
        elif U0 <= 25.0:
            return 0.0003234808 * U0**7.0 - 0.0331940121 * U0**6.0 + 1.3883148012 * U0**5.0 - 30.3162345004 * U0**4.0 + 367.6835557011 * U0**3.0 - 2441.6860655008 * U0**2.0 + 8345.6777042343 * U0 - 11352.9366182805
        else:
            return 0.0

    r0 = 40.0  # Turbine rotor radius
    D = 2.0 * r0
    A = pi * r0**2.0
    ct = 0.81
    deff = D * sqrt((1.0 + sqrt(1.0 - ct)) / (2.0 * sqrt(1.0 - ct)))
    H = 70.0  # Hub height
    ia = 0.08  # Ambient turbulence intensity according to vanlucanee. 8% on average
    rnb = max(1.08 * D, 1.08 * D + 21.7 * D * (ia - 0.05))
    r95 = 0.5 * (rnb + min(H, rnb))
    x0 = 9.5 * D / ((2.0 * r95 / deff)**3.0 - 1.0)
    c1 = (deff / 2.0)**(5.0 / 2.0) * (105.0 / 2.0 / pi)**(-1.0 / 2.0) * (
        ct * A * x0)**(-5.0 / 6.0)  # Prandtl mixing length

    for wind in range(0, len(windrose_speed)):
        # for wind in range(0, 1):
        #     U1 = windrose_speed[wind]  # Free stream wind speed
        # U0 = U1 * (70.0 / 10.0)**0.11 # Power or log law for wind shear profile
        # U0 = U1 * log(70.0 / 0.005) / log(10.0 / 0.005)
        U0 = 8.5
        angle = windrose_angle[wind]
        angle3 = angle + 180.0
        wake_deficit_matrix = [[0.0 for x in range(nt)] for x in range(nt)]
        for turbine in range(nt):
            flag = [False for x in range(nt)]
            proportion = [0.0 for x in range(nt)]
            perpendicular_distance = [0.0 for x in range(nt)]
            parallel_distance = [0.0 for x in range(nt)]
            for i in range(nt):
                proportion[i], flag[i], perpendicular_distance[
                    i], parallel_distance[i] = wake.determine_if_in_wake(
                        layout_x[turbine], layout_y[turbine], layout_x[i],
                        layout_y[i], A, c1, ct, angle3, r0)

            # Matrix with effect of each turbine <i = turbine> on every other turbine <j> of the farm
            for j in range(nt):
                if turbine != j and flag[j] is True:
                    # print (U0, ct, A, parallel_distance[j], perpendicular_distance[j], c1, x0, r95, rnb, deff)
                    wake_deficit_matrix[turbine][
                        j] = proportion[j] * wake.wake_deficit(
                            U0, ct, A, parallel_distance[j],
                            perpendicular_distance[j], c1)
                elif turbine == j:
                    wake_deficit_matrix[turbine][j] = 0.0
                output.write('{0:f}\t'.format(wake_deficit_matrix[turbine][j]))
            output.write('\n')

        total_deficit = [0.0 for x in range(nt)]
        total_speed = [0.0 for x in range(nt)]
        for j in range(nt):
            for i in range(nt):
                total_deficit[j] += wake_deficit_matrix[i][j]**2.0
            total_deficit[j] = sqrt(total_deficit[j])
            total_speed[j] = U0 * (1.0 - total_deficit[j])
            output2.write(
                '{0:d}\t{1:.1f}\t{2:.1f}\t{3:f}\t{4:f}\t{5:d}\t{6:f}\n'.format(
                    j, layout_x[j], layout_y[j], total_deficit[j],
                    total_speed[j], int(angle), power(total_speed[j])))
        output2.write('\n')
        turb_data.write('{0:f}\t{1:f}\n'.format(angle, power(total_speed[14])))

        # Individual turbine efficiency
        # for g in range(nt):
        #      turb_data.write('{0:f}\n'.format(total_speed[g]))

        # Farm efficiency
        profit = 0.0
        efficiency_proportion = [
            0.0 for x in range(0, len(windrose_frequency))
        ]
        efficiency = 0.0
        for l in range(nt):
            profit += power(total_speed[l])
        efficiency = profit * 100.0 / (nt * power(U0))
        efficiency_proportion[
            wind] = efficiency / 100.0 * windrose_frequency[wind]
        # print 'Farm efficiency with wind direction = {0:d} deg: {1:2.2f}%'.format(int(angle), efficiency)
        direction.write('{0:f}\t{1:f}\n'.format(angle, efficiency))
        summation += efficiency_proportion[wind]
    print('total farm efficiency is {0:f} %'.format(summation))

    turb_data.close()
    output.close()
    output2.close()
    draw.close()
    direction.close()
Пример #4
0
def larsen(a, windrose_angle, windrose_speed, windrose_frequency):

    import wake_geometry as wake
    from math import sqrt, log, tan, cos, pi
    from numpy import deg2rad
    nt = len(a)  # Number of turbines

    summation = 0.0

    layout_x = [0.0 for x in range(nt)]
    layout_y = [0.0 for x in range(nt)]
    for x in range(nt):
        layout_x[x] = float(a[x][0])
        layout_y[x] = float(a[x][1])

    def Ct(U0):
        if U0 < 4.0:
            return 0.1
        elif U0 <= 25.0:
            return 0.00000073139922126945 * U0**6.0 - 0.0000668905596915255 * U0**5.0 + 0.0023937885 * U0**4.0 + -0.0420283143 * U0**3.0 + 0.3716111285 * U0**2.0 - 1.5686969749 * U0 + 3.2991094727
        else:
            return 0.0

    def power(U0):
        if U0 < 4.0:
            return 0.0
        elif U0 <= 25.0:
            return 0.0003234808 * U0**7.0 - 0.0331940121 * U0**6.0 + 1.3883148012 * U0**5.0 - 30.3162345004 * U0**4.0 + 367.6835557011 * U0**3.0 - 2441.6860655008 * U0**2.0 + 8345.6777042343 * U0 - 11352.9366182805
        else:
            return 0.0

    # for U0 in range(4, 20):
    r0 = 40.0  # Turbine rotor radius
    D = 2.0 * r0
    A = pi * r0**2.0
    H = 70.0  # Hub height
    ia = 0.08  # Ambient turbulence intensity according to vanlucanee. 8% on average

    def deff(U0):
        return D * sqrt(
            (1.0 + sqrt(1.0 - Ct(U0))) / (2.0 * sqrt(1.0 - Ct(U0))))

    rnb = max(1.08 * D, 1.08 * D + 21.7 * D * (ia - 0.05))
    r95 = 0.5 * (rnb + min(H, rnb))

    def x0(U0):
        return 9.5 * D / ((2.0 * r95 / deff(U0))**3.0 - 1.0)

    def c1(U0):
        return (deff(U0) / 2.0)**(5.0 / 2.0) * (105.0 / 2.0 / pi)**(
            -1.0 / 2.0) * (Ct(U0) * A * x0(U0))**(-5.0 / 6.0
                                                  )  # Prandtl mixing length

    def distance_to_front(x, y, theta, r):
        theta = deg2rad(theta)
        return abs(x + tan(theta) * y - r / cos(theta)) / sqrt(1.0 +
                                                               tan(theta)**2.0)

    for wind in range(0, len(windrose_angle)):
        U1 = windrose_speed[wind]  # Free stream wind speed
        U0 = U1 * (70.0 /
                   10.0)**0.11  # Power or log law for wind shear profile
        angle = windrose_angle[wind]
        angle3 = angle + 180.0
        deficit_matrix = [[0.0 for x in range(nt)] for x in range(nt)]
        distance = [[0.0 for x in range(2)] for x in range(nt)]

        U = [U0 for x in range(nt)]
        total_deficit = [0.0 for x in range(nt)]

        for tur in range(nt):
            distance[tur] = [
                distance_to_front(layout_x[tur], layout_y[tur], angle,
                                  100000000.0), tur
            ]
        distance.sort()

        for turbine in range(nt):
            for num in range(turbine):
                total_deficit[distance[turbine][1]] += deficit_matrix[
                    distance[turbine][1]][distance[num][1]]**2.0
            total_deficit[distance[turbine][1]] = sqrt(
                total_deficit[distance[turbine][1]])
            U[distance[turbine]
              [1]] = U0 * (1.0 - total_deficit[distance[turbine][1]])
            flag = [False for x in range(nt)]
            proportion = [0.0 for x in range(nt)]
            perpendicular_distance = [0.0 for x in range(nt)]
            parallel_distance = [0.0 for x in range(nt)]
            for i in range(turbine + 1, nt):
                proportion[distance[i][1]], flag[
                    distance[i][1]], perpendicular_distance[
                        distance[i][1]], parallel_distance[
                            distance[i][1]] = wake.determine_if_in_wake(
                                layout_x[distance[turbine][1]],
                                layout_y[distance[turbine][1]],
                                layout_x[distance[i][1]],
                                layout_y[distance[i][1]], A,
                                c1(U[distance[turbine][1]]),
                                Ct(U[distance[turbine][1]]), angle3, r0)
                if parallel_distance[distance[i][1]] > 0.0:
                    deficit_matrix[distance[i][1]][distance[turbine][
                        1]] = proportion[distance[i][1]] * wake.wake_deficit(
                            U[distance[turbine][1]], Ct(
                                U[distance[turbine][1]]), A,
                            parallel_distance[distance[i][1]],
                            perpendicular_distance[distance[i][1]],
                            c1(U[distance[turbine][1]]))
                else:
                    deficit_matrix[distance[i][1]][distance[turbine][1]] = 0.0

        # Farm efficiency
        profit = 0.0
        efficiency_proportion = [
            0.0 for x in range(0, len(windrose_frequency))
        ]
        for l in range(nt):
            profit += power(U[l])
        efficiency = profit * 100.0 / (float(nt) * power(U[distance[0][1]]))
        efficiency_proportion[
            wind] = efficiency * windrose_frequency[wind] / 100.0
        summation += efficiency_proportion[wind]
    return 100.0 - summation
def analysis():

    layout_x = []
    layout_y = []
    for line in layout:
        columns = line.split()
        layout_x.append(float(columns[0]))
        layout_y.append(float(columns[1]))

    windrose_angle = []
    windrose_speed = []
    windrose_frequency = []
    for line in windrose:
        columns = line.split()
        windrose_angle.append(float(columns[0]))
        windrose_speed.append(float(columns[1]))
        windrose_frequency.append(float(columns[2]))

    layout.close()
    windrose.close()

    def Ct(U0):
        return 0.0001923077 * U0**4.0 + -0.0075407925 * U0**3.0 + 0.096462704 * U0**2.0 - 0.5012354312 * U0 + 1.7184749184

    # def power(U0):
    #     return -0.0071427272 * U0**5.0 + 0.5981106302 * U0**4.0 - 18.5218157059 * U0**3.0 + 251.0929636046 * U0**2.0 - 1257.8070377904 * U0 + 2043.2240149783

    def power(U0):
        if U0 < 4.0:
            return 0.0
        elif U0 >= 4.0:
            return 19.7907842158 * U0 ** 2.0 - 74.9080669331 * U0 + 37.257967033  # From 4 to 11 m/s
    # def Cp(U0):
    #     return power(U0) / (0.5 * 1.225 * pi * r0**2.0 * U0**3.0)

    # for U0 in range(4, 20):
    nt = 80  # Number of turbines
    summation = 0.0
    p = [0.0 for x in range(nt)]
    ct = 0.0
    for wind in range(0, len(windrose_speed)):
        ct += 1
    # for wind in range(180-2, 180+3):
        # if wind in [100, 133, 271, 280, 313]:
        #     continue
        # U1 = windrose_speed[wind]  # Free stream wind speed
        # U0 = U1 * (70.0 / 10.0)**0.11 # Power or log law for wind shear profile
        # U0 = U1 * log(70.0 / 0.005) / log(10.0 / 0.005)
        U0 = 8.5
        k = 0.04  # Decay constant
        r0 = 40.0  # Turbine rotor radius
        angle = windrose_angle[wind]
        angle3 = angle + 180.0
        deficit_matrix = [[0.0 for x in range(nt)] for x in range(nt)]
        proportion = [[0.0 for x in range(nt)] for x in range(nt)]
        affected_matrix = [[] for x in range(nt)]

        for turbine in range(0, nt):
            flag = [False for x in range(nt)]
            for i in range(nt):
                if i != turbine:
                    proportion[i][turbine], flag[i] = wake.determine_if_in_wake(layout_x[turbine], layout_y[turbine], layout_x[i], layout_y[i], k, r0, angle3)
                elif i == turbine:
                    proportion[i][turbine] = 0.0
                    flag[i] = False
                if flag[i] is True:
                    affected_matrix[i].append(turbine)

        deficit = [0.0 for x in range(nt)]
        length = [0 for x in range(nt)]
        U = [0.0 for x in range(nt)]

        for i in range(nt):
            length[i] = len(affected_matrix[i])
            row.write('{0:f}\t{1:f}\t{2:d}\n'.format(layout_x[i], layout_y[i], len(affected_matrix[i])))
        row_vector = [[] for x in range(0, max(length) + 1)]

        for i in range(nt):
            for n in range(0, max(length) + 1):
                if length[i] == n:
                    row_vector[n].append(i)
        total_deficit = [0.0 for x in range(nt)]
        counter = 0
        for n in range(0, max(length) + 1):
        # for n in range(0, 5):
            if n == 0:
                for turb in row_vector[n]:
                    deficit[turb] = 0.0
                    U[turb] = U0
            else:
                for turb in row_vector[n]:
                    for i in affected_matrix[turb]:
                        # print U[i], turb, turb, i, i, counter
                        if U[i] == 0.0:
                            row_vector[n].append(turb)
                            counter += 1
                            continue
                        elif deficit_matrix[turb][i] == 0.0:
                            deficit_matrix[turb][i] = proportion[turb][i] * wake.wake_deficit(Ct(U[i]), k, wake.distance(layout_x[turb], layout_y[turb], layout_x[i], layout_y[i]), r0)
                            total_deficit[turb] += deficit_matrix[turb][i] ** 2.0
                    if counter >= 20000:
                        break
                    total_deficit[turb] = sqrt(total_deficit[turb])
                    U[turb] = U0 * (1.0 - total_deficit[turb])

        for turb in range(nt):
            for i in range(nt):
                output.write('{0:f}\t'.format(deficit_matrix[turb][i]))
            output.write('\n')
            output2.write('{0:d}\t{1:.1f}\t{2:.1f}\t{3:f}\t{4:f}\t{5:d}\t{6:f}\n'.format(turb, layout_x[turb], layout_y[turb], total_deficit[turb], U[turb], int(angle), power(U[turb])))
        output2.write('\n')

        # Individual turbine efficiency
        # if angle == 0:
        # for g in range(nt):
        #     p[g] += power(U[g])/power(U[row_vector[0][0]])
        turb_data.write('{0:f}\t{1:f}\n'.format(angle, power(U[14])))

        # Farm efficiency
        profit = 0.0
        efficiency_proportion = [0.0 for x in range(0, len(windrose_frequency))]
        for l in range(nt):
            profit += power(U[l])
        # print row_vector[0][0]
        efficiency = profit * 100.0 / (float(nt) * power(U[row_vector[0][0]]))
        efficiency_proportion[wind] = efficiency * windrose_frequency[wind] / 100.0
        # print 'Farm efficiency with wind direction = {0:d} deg: {1:2.2f}%'.format(int(angle), efficiency)
        print angle, efficiency
        direction.write('{0:f}\t{1:f}\n'.format(angle, efficiency))
        summation += efficiency_proportion[wind]
    print 'total farm efficiency is {0:f} %'.format(summation)

    # for g in range(nt):
    #     turb_data.write('{0:d}\t{1:f}\t{2:f}\t{3:f}\n'.format(g, layout_x[g], layout_y[g], p[g] / ct))
    # turb_data.write('\n')

    turb_data.close()
    output.close()
    output2.close()
    draw.close()
    direction.close()
    row.close()
def larsen(a, windrose_angle, windrose_speed, windrose_frequency):

    import wake_geometry as wake
    from math import sqrt, log, tan, cos, pi
    from numpy import deg2rad
    nt = len(a)  # Number of turbines

    summation = 0.0

    layout_x = [0.0 for x in range(nt)]
    layout_y = [0.0 for x in range(nt)]
    for x in range(nt):
        layout_x[x] = float(a[x][0])
        layout_y[x] = float(a[x][1])

    def Ct(U0):
        if U0 < 4.0:
            return 0.1
        elif U0 <= 25.0:
            return 0.00000073139922126945 * U0 ** 6.0 - 0.0000668905596915255 * U0 ** 5.0 + 0.0023937885 * U0 ** 4.0 + - 0.0420283143 * U0 ** 3.0 + 0.3716111285 * U0 ** 2.0 - 1.5686969749 * U0 + 3.2991094727
        else:
            return 0.0

    def power(U0):
        if U0 < 4.0:
            return 0.0
        elif U0 <= 25.0:
            return 0.0003234808 * U0 ** 7.0 - 0.0331940121 * U0 ** 6.0 + 1.3883148012 * U0 **5.0 - 30.3162345004 * U0 ** 4.0 + 367.6835557011 * U0 ** 3.0 - 2441.6860655008 * U0 ** 2.0 + 8345.6777042343 * U0 - 11352.9366182805
        else:
            return 0.0

    # for U0 in range(4, 20):
    r0 = 40.0  # Turbine rotor radius
    D = 2.0 * r0
    A = pi * r0 ** 2.0
    H = 70.0  # Hub height
    ia = 0.08  # Ambient turbulence intensity according to vanlucanee. 8% on average

    def deff(U0):
        return D * sqrt((1.0 + sqrt(1.0 - Ct(U0))) / (2.0 * sqrt(1.0 - Ct(U0))))

    rnb = max(1.08 * D, 1.08 * D + 21.7 * D * (ia - 0.05))
    r95 = 0.5 * (rnb + min(H, rnb))

    def x0(U0):
        return 9.5 * D / ((2.0 * r95 / deff(U0)) ** 3.0 - 1.0)

    def c1(U0):
        return (deff(U0) / 2.0) ** (5.0 / 2.0) * (105.0 / 2.0 / pi) ** (- 1.0 / 2.0) * (Ct(U0) * A * x0(U0)) ** (- 5.0 / 6.0)  # Prandtl mixing length

    def distance_to_front(x, y, theta, r):
        theta = deg2rad(theta)
        return abs(x + tan(theta) * y - r / cos(theta)) / sqrt(1.0 + tan(theta) ** 2.0)

    for wind in range(0, len(windrose_angle)):
        U1 = windrose_speed[wind]  # Free stream wind speed
        U0 = U1 * (70.0 / 10.0) ** 0.11 # Power or log law for wind shear profile
        angle = windrose_angle[wind]
        angle3 = angle + 180.0
        deficit_matrix = [[0.0 for x in range(nt)] for x in range(nt)]
        distance = [[0.0 for x in range(2)] for x in range(nt)]

        U = [U0 for x in range(nt)]
        total_deficit = [0.0 for x in range(nt)]

        for tur in range(nt):
            distance[tur] = [distance_to_front(layout_x[tur], layout_y[tur], angle, 100000000.0), tur]
        distance.sort()

        for turbine in range(nt):
            for num in range(turbine):
                total_deficit[distance[turbine][1]] += deficit_matrix[distance[turbine][1]][distance[num][1]] ** 2.0
            total_deficit[distance[turbine][1]] = sqrt(total_deficit[distance[turbine][1]])
            U[distance[turbine][1]] = U0 * (1.0 - total_deficit[distance[turbine][1]])
            flag = [False for x in range(nt)]
            proportion = [0.0 for x in range(nt)]
            perpendicular_distance = [0.0 for x in range(nt)]
            parallel_distance = [0.0 for x in range(nt)]
            for i in range(turbine + 1, nt):
                proportion[distance[i][1]], flag[distance[i][1]], perpendicular_distance[distance[i][1]], parallel_distance[distance[i][1]] = wake.determine_if_in_wake(layout_x[distance[turbine][1]], layout_y[distance[turbine][1]], layout_x[distance[i][1]], layout_y[distance[i][1]], A, c1(U[distance[turbine][1]]), Ct(U[distance[turbine][1]]), angle3, r0)
                if parallel_distance[distance[i][1]] > 0.0:
                    deficit_matrix[distance[i][1]][distance[turbine][1]] = proportion[distance[i][1]] * wake.wake_deficit(U[distance[turbine][1]], Ct(U[distance[turbine][1]]), A, parallel_distance[distance[i][1]], perpendicular_distance[distance[i][1]], c1(U[distance[turbine][1]]))
                else:
                    deficit_matrix[distance[i][1]][distance[turbine][1]] = 0.0

        # Farm efficiency
        profit = 0.0
        efficiency_proportion = [0.0 for x in range(0, len(windrose_frequency))]
        for l in range(nt):
            profit += power(U[l])
        efficiency = profit * 100.0 / (float(nt) * power(U[distance[0][1]]))
        efficiency_proportion[wind] = efficiency * windrose_frequency[wind] / 100.0
        summation += efficiency_proportion[wind]
    return 100.0 - summation