def main():
    g = get_geometry(pi/2)

    file_name = 'thermo_4probe_spectral.h5'

    # Load data from a file, if it exists
    solver = u.CurrentSolver.resume(file_name, g)
    solver.set_solvers(kin_solver=u.KIN_SOLVER_BLOCK,
                       sp_solver=u.SP_SOLVER_TWPBVP)

    # Solve and save spectral quantities to a file.
    # This takes some time, so we'll want to avoid redoing it unnecessarily.
    #
    # calculage_G=True instructs the solver also to calculate a spectral
    # conductance matrix for the circuit, which can be used to conveniently
    # evaluate currents given distribution functions in the terminals.
    #
    solver.solve_spectral_and_save_if_needed(file_name, calculate_G=True)

    # Solve thermovoltage vs. temperature
    dT = 0.001

    # ... and write results to a text file
    output = open('thermo_4probe.dat', 'w')
    print >> output, "%% %14s %14s" % ("T (E_T)", "dV/dT (ueV/K)")

    for T in logspace(log10(dT + 1e-4), log10(20), 100):
        # Compute the thermovoltage:
        g.t_mu = 0
        g.t_t = T
        g.t_t[2] += dT/2
        g.t_t[3] -= dT/2

        # Make the terminals 2, 3 to float
        # Currents entering them flow in wires 2, 3
        
        def zero_currents():
            Ic, Ie = solver.get_currents_from_G(w_jT=[2,3], w_jL=[])
            return [Ic[2], Ic[3]]

        def set_potentials(z):
            g.t_mu[2], g.t_mu[3] = z

        u.optimize_parameters_for([0,0], zero_currents, set_potentials)

        # Print the thermovoltage at terminal 2 in ueV/K
        print >> output, "  %14g %14g" % (T, g.t_mu[2] / dT * 86.17343)
def main():
    g = get_geometry(pi/2)

    file_name = 'nonlocal_thermovoltage_spectral.h5'

    # It is possible to do a self-consistent iteration, but this is quite
    # slow.
    #
    # Change False to True below, if you want to try that:
    do_selfconsistent_iteration = False

    # Load data from a file, if it exists
    solver = u.CurrentSolver.resume(file_name, g, ne=200, chunksize=50)
    solver.set_solvers(kin_solver=u.KIN_SOLVER_BLOCK,
                       sp_solver=u.SP_SOLVER_TWPBVP)

    # Solve and save spectral quantities to a file.
    # This takes some time, so we'll want to avoid redoing it unnecessarily.
    #
    # calculage_G=True instructs the solver also to calculate a spectral
    # conductance matrix for the circuit, which can be used to conveniently
    # evaluate currents given distribution functions in the terminals.
    #
    if not do_selfconsistent_iteration:
        solver.solve_spectral_and_save_if_needed(file_name, calculate_G=True)
 

    # Solve thermovoltage vs. temperature
    dT = 0.05

    # ... and write results to a text file
    output = open('nonlocal_thermovoltage.dat', 'w')
    print >> output, "%% %14s %14s" % ("T (E_T)", "dV/dT (ueV/K)")

    if do_selfconsistent_iteration:
        Ts = logspace(log10(dT + 1e-4), log10(10), 15)[1:]
    else:
        Ts = logspace(log10(dT + 1e-4), log10(10), 100)

    for T in Ts:
        
        # (Optional) self-consistent iteration
        if do_selfconsistent_iteration:
            g.t_mu = 0
            g.t_t = T
            it = u.self_consistent_matsubara_iteration(g, max_ne=50)
            #it = u.self_consistent_realtime_iteration(solver)
            for k, d, I_error in it:
                print >> sys.stderr, "%% Self-consistent iteration %d (residual %g)" % (k, d.residual_norm())
                if (d.residual_norm() < 1e-3 * 100 and I_error < 1e-5):
                   break
            else:
                raise RuntimeError("Self-cons. iteration didn't converge!")

            solver.solve_spectral()
            solver.calculate_G()
            solver.save("nonlocal_thermovoltage_T_%.2f.h5" % T)

        # Compute the thermovoltage:
        g.t_mu = 0
        g.t_t = T
        g.t_t[11] += dT

        # Make the terminals 0, 3, 4, 5, 11 to float
        # Currents entering them flow in wires 0, 3, 4, 5, 7
        
        def zero_currents():
            Ic, Ie = solver.get_currents_from_G(w_jT=[0,3,4,5,7], w_jL=[])
            #Ic, Ie = solver.get_currents(w_jT=[0,3,4,5,7], w_jL=[], ix=0)
            return [Ic[0], Ic[3], Ic[4], Ic[5], Ic[7]]

        def set_potentials(z):
            g.t_mu[0], g.t_mu[3], g.t_mu[4], g.t_mu[5], g.t_mu[11] = z

        u.optimize_parameters_for([0,0,0,0,0], zero_currents, set_potentials)

        # Print the thermovoltage at terminal 4 in ueV/K
        print >> output, "  %14g %14g" % (T, g.t_mu[4] / dT * 86.17343)

    # Solve the kinetic equations for some temperature and a larger
    # temperature difference, and dump the result for inspection.
    g.t_t = 1e-4
    g.t_t[11] = 8

    solver.solve_kinetic()
    solver.save('dump.h5')

    output.close()