import dynamics

import matplotlib.pylab as plt
import matplotlib.gridspec as gridspec

from turbine import Turbine

# Turn off gravity
dynamics.OPT_GRAVITY = True
dynamics.OPT_GEOMETRIC_STIFFNESS = True

# Create model
bladed_file = r"C:\Users\Rick Lupton\Dropbox\phd\Bladed\Models\OC3-Hywind_SparBuoy_NREL5MW.prj"
tb = Turbine(bladed_file, rigid=False)
tb.set_base_motion(4, 0.80, 10 * pi / 180)
# t,y = tb.simulate(rotor_speed=2, t1=60, dt=0.05, init=True)


def p(parts=False):
    fig = plt.figure()
    fig.set_size_inches(15, 10, forward=True)
    gs = gridspec.GridSpec(6, 2)
    motion = ["surge", "sway", "heave", "roll", "pitch", "yaw"][tb.base_motion]
    fig.suptitle("Blade loads in response to base motion: {}".format(motion))

    assert motion == "pitch"
    az = [y[1][:, 0], y[6][:, 0], y[11][:, 0]]
    pitch = [y[0][:, tb.base_motion], y[5][:, tb.base_motion], y[10][:, tb.base_motion]]
    mh = tb.modes.mass * tb.tower.hubheight
    h = tb.tower.hubheight
import dynamics

import matplotlib.pylab as plt
import matplotlib.gridspec as gridspec

from turbine import Turbine

# Turn off gravity
dynamics.OPT_GRAVITY = True
dynamics.OPT_GEOMETRIC_STIFFNESS = True

# Create model
bladed_file = r'C:\Users\Rick Lupton\Dropbox\phd\Bladed\Models\OC3-Hywind_SparBuoy_NREL5MW.prj'
tb = Turbine(bladed_file, rigid=False)
tb.set_base_motion(4, 0.80, 10 * pi / 180)
#t,y = tb.simulate(rotor_speed=2, t1=60, dt=0.05, init=True)


def p(parts=False):
    fig = plt.figure()
    fig.set_size_inches(15, 10, forward=True)
    gs = gridspec.GridSpec(6, 2)
    motion = ['surge', 'sway', 'heave', 'roll', 'pitch', 'yaw'][tb.base_motion]
    fig.suptitle('Blade loads in response to base motion: {}'.format(motion))

    assert motion == 'pitch'
    az = [y[1][:, 0], y[6][:, 0], y[11][:, 0]]
    pitch = [
        y[0][:, tb.base_motion], y[5][:, tb.base_motion], y[10][:,
                                                                tb.base_motion]
import dynamics
from turbine import Turbine

# Turn off gravity
dynamics.OPT_GRAVITY = True
dynamics.OPT_GEOMETRIC_STIFFNESS = True

# Parameters
pitch_freq  = 0.3 # rad/s
pitch_amp   = 0.3 # rad
rotor_speed = 2   # rad/s

# Create model
bladed_file = r'C:\Users\Rick Lupton\Dropbox\phd\Bladed\Models\OC3-Hywind_SparBuoy_NREL5MW.prj'
tb = Turbine(bladed_file)
tb.set_base_motion(4, pitch_freq, pitch_amp)

# Simulate
#t,y = tb.simulate(rotor_speed=2, t1=90, dt=0.10, init=True)

def mbc(az, u):
    N = len(az)
    B = array([
        np.ones((3,N)),
        [2*np.cos(az), 2*np.cos(az+2*pi/3), 2*np.cos(az+4*pi/3)],
        [2*np.sin(az), 2*np.sin(az+2*pi/3), 2*np.sin(az+4*pi/3)],
    ]) / 3
    u = array(u)
    z = np.einsum('zbt,btj->jzt', B, u)
    return z