def Energy_cl(v):
    
    funval = 0.0
    
    for bond in range(num_bond):
        id1 = sub_idx[bond, 0]
        id2 = sub_idx[bond, 1]
        
        alpha1 = v[4*id1]
        alpha2 = v[4*id1+1]
        theta  = v[4*id1+2]
        phi    = v[4*id1+3]
        
        alpha1_p = v[4*id2]
        alpha2_p = v[4*id2+1]
        theta_p  = v[4*id2+2]
        phi_p    = v[4*id2+3]
        
        S1p = fg.fun_sp(alpha1, alpha2, theta, phi)
        S1m = fg.fun_sm(alpha1, alpha2, theta, phi)
        S1z = fg.fun_sz(theta, phi)
        
        S2p = fg.fun_sp(alpha1_p, alpha2_p, theta_p, phi_p)
        S2m = fg.fun_sm(alpha1_p, alpha2_p, theta_p, phi_p)
        S2z = fg.fun_sz(theta_p, phi_p)
        
        funval += Jex[bond] * (0.5*(S1p*S2m+S1m*S2p) + Delta[bond]*S1z*S2z) \
                + Jpm[bond] * (gamma_ij[bond]*S1p*S2p + gamma_ijc[bond]*S1m*S2m) \
                - 0.5*1j*Jzpm[bond]*(gamma_ijc[bond]*S1p*S2z \
                                   - gamma_ij[bond]*S1m*S2z  \
                                   + gamma_ijc[bond]*S1z*S2p \
                                   - gamma_ij[bond]*S1z*S2m)
                                     
    for sub_lat in range(num_sub):
        theta = v[4*sub_lat+2]
        phi   = v[4*sub_lat+3]
        Sz = fg.fun_sz(theta, phi)
        Sz_sq = fg.fun_sz_sq(theta)        
        funval += - (D_ion*Sz_sq + h_ext*Sz)
        
    funval = np.real(funval)
    funval = float(funval)    
    return funval
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 30 21:19:47 2019

@author: hao
"""

import numpy as np
import fun_generators as fg
import GLSW

h = np.array([0.0, 1.0, 3.0, 4.0, 4.74])
lenh = len(h)
xs = range(lenh)
q = np.array([0.0, 0.0, 0.0])
ham = GLSW.sw_hamiltonian(q)

for flag in xs:
    field = h[flag]
    fname = 'gs_info/h=' + str(field) + 'T/opt_angles.txt'
    angles = np.load(fname)
    print('h= ', field, '\n')

    for flag1 in range(4):
        theta = angles[flag1 * 4 + 2]
        phi = angles[flag1 * 4 + 3]
        Sz = fg.fun_sz(theta, phi)
        Szsq = fg.fun_sz_sq(theta)
        print('sublattice: ', flag1, '\n')
        print('Sz = ', Sz, 'Sz**2= ', Szsq, '\n')
        print('find new local minima, updates!')
        x_global_opt = x_local_opt
        e_global_opt = e_local_opt

    print("finishing the %s th random experiment!" % flag1)
    print("time elpase in this experiment is %s seconds" %
          (time.time() - start_time))
    print("the return flag is %s" % return_flag)

mf_vals = np.zeros((4, 4))

for flag2 in range(4):
    alpha1 = x_global_opt[flag2 * 4]
    alpha2 = x_global_opt[flag2 * 4 + 1]
    theta = x_global_opt[flag2 * 4 + 2]
    phi = x_global_opt[flag2 * 4 + 3]

    tmp1 = fg.fun_sp(alpha1, alpha2, theta, phi)
    tmp2 = fg.fun_sm(alpha1, alpha2, theta, phi)
    mf_vals[flag2, 0] = np.real((tmp1 + tmp2) / 2.0)
    mf_vals[flag2, 1] = np.real((tmp1 - tmp2) / (2.0 * 1j))
    tmp3 = fg.fun_sz(theta, phi)
    mf_vals[flag2, 2] = tmp3
    tmp4 = fg.fun_sz_sq(theta)
    mf_vals[flag2, 3] = tmp4

fname = dirpath + 'opt_angles.txt'
np.savetxt(fname, x_global_opt)
fname1 = dirpath + 'mfvals.txt'
np.savetxt(fname1, mf_vals)