# Apply a spin rotation on top of the Polarized collinear solution and
# update non-selfconsistently.
device_configuration = nlread('polarized-0.hdf5', DeviceConfiguration)[-1]
calculator = device_configuration.calculator()
exchange_correlation = NCLDA.PZ
calculator = calculator(
    exchange_correlation=exchange_correlation,
    iteration_control_parameters=NonSelfconsistent
    )

# -------------------------------------------------------------
# Initial State
# -------------------------------------------------------------
# Define the spin rotation in polar coordinates
theta = 90
left_spins  = [(i, 1, theta * Degrees, 0 * Degrees) for i in range(8)]
center_spins  = [(i, 0, 0 * Degrees, 0 * Degrees) for i in range(8, 22)]
right_spins = [(i, 1, 0 * Degrees, 0 * Degrees) for i in range(22, 30)]
spin_list   = left_spins + center_spins + right_spins
initial_spin = InitialSpin(scaled_spins=spin_list)
device_configuration.setCalculator(
    calculator,
    initial_spin=initial_spin,
    initial_state=device_configuration
)

# Update and save.
device_configuration.update()
filename = 'theta-{}.hdf5'.format(theta)
nlsave(filename, device_configuration)

# Calculate and save the atom-resolved torkance.
torkance, torkance_per_kpoint, kpoints, weights = calculateLinearResponseSpinTransferTorkance(
    device_configuration)
nlsave('stt-90.hdf5', torkance, object_id='torkance')

# Calculate and save the finite bias torque in a bias range of interest.
biases = numpy.linspace(-1.0, 1.0, 21) * Volt
torque, torque_per_kpoint, kpoints, weights = calculateNonSelfConsistentFiniteBiasSpinTransferTorque(
    configuration=device_configuration,
    biases=biases,
    potential_drop_region=(7, 22))
nlsave('stt-90.hdf5', torque, object_id='torque')
nlsave('stt-90.hdf5', biases, object_id='biases')

