<reponame>obaica/TB2J<gh_stars>1-10
import os
import pickle
import copy
import numpy as np
from scipy.spatial.transform import Rotation
from TB2J.io_exchange import SpinIO

# Rotation from x to z
Rxz = Rotation.from_euler('y', -90, degrees=True)
# Rotation from y to z
Ryz = Rotation.from_euler('x', 90, degrees=True)

def merge_DMI(Dx, Dy, Dz):
    Dx_z = Rxz.apply(Dx)
    Dy_z = Ryz.apply(Dy)
    D = (np.array([0.0, 0.5, 0.5]) * Dx_z + np.array([0.5, 0.0, 0.5]) * Dy_z +
         np.array([0.5, 0.5, 0.0]) * Dz)
    return D


class Merger():
    def __init__(self, path_x, path_y, path_z):
        self.dat_x=SpinIO.load_pickle(os.path.join(path_x, 'TB2J_results'), 'TB2J.pickle')
        self.dat_y=SpinIO.load_pickle(os.path.join(path_y, 'TB2J_results'), 'TB2J.pickle')
        self.dat_z=SpinIO.load_pickle(os.path.join(path_z, 'TB2J_results'), 'TB2J.pickle')
        self.dat=copy.copy(self.dat_z)

    def merge_DMI(self):
        dmi_ddict={}
        if self.dat_x.has_dmi and self.dat_y.has_dmi and self.dat_z.has_dmi:
            Dxdict=self.dat_x.dmi_ddict
            Dydict=self.dat_y.dmi_ddict
            Dzdict=self.dat_z.dmi_ddict
            for key, Dz in Dzdict.items():
                Dx= Dxdict[key]
                Dy= Dydict[key]
                dmi_ddict[key] = merge_DMI(Dx, Dy, Dz)
                # if key==((0,1,1), 1,0) and False:
                #     print("----------------------------")
                #     print("Dx:", Dx*1000)
                #     Dx_z = Rxz.apply(Dx)
                #     print("Dx_z:", Dx_z*1000)
                #     print("Dy:", Dy*1000)
                #     Dy_z = Ryz.apply(Dy)
                #     print("Dy_z:", Dy_z*1000)
                #     print("Dz:", Dz*1000)
                #    print("Resulst:", merge_DMI(Dx, Dy, Dz)*1000)
            self.dat.dmi_ddict=dmi_ddict

    def write(self, path='TB2J_results'):
        self.dat.write_all(path=path)



def merge(path_x, path_y, path_z, save=True, path='TB2J_results'):
    m=Merger(path_x, path_y, path_z)
    m.merge_DMI()
    if save:
        m.write(path=path)
    return m.dat


import os
import pickle
import copy
import numpy as np
from scipy.spatial.transform import Rotation
from TB2J.io_exchange import SpinIO

# Rotation from x to z
Rxz = Rotation.from_euler('y', -90, degrees=True)
# Rotation from y to z
Ryz = Rotation.from_euler('x', 90, degrees=True)

def merge_DMI(Dx, Dy, Dz):
    Dx_z = Rxz.apply(Dx)
    Dy_z = Ryz.apply(Dy)
    D = (np.array([0.0, 0.5, 0.5]) * Dx_z + np.array([0.5, 0.0, 0.5]) * Dy_z +
         np.array([0.5, 0.5, 0.0]) * Dz)
    return D


class Merger():
    def __init__(self, path_x, path_y, path_z):
        self.dat_x=SpinIO.load_pickle(os.path.join(path_x, 'TB2J_results'), 'TB2J.pickle')
        self.dat_y=SpinIO.load_pickle(os.path.join(path_y, 'TB2J_results'), 'TB2J.pickle')
        self.dat_z=SpinIO.load_pickle(os.path.join(path_z, 'TB2J_results'), 'TB2J.pickle')
        self.dat=copy.copy(self.dat_z)

    def merge_DMI(self):
        dmi_ddict={}
        if self.dat_x.has_dmi and self.dat_y.has_dmi and self.dat_z.has_dmi:
            Dxdict=self.dat_x.dmi_ddict
            Dydict=self.dat_y.dmi_ddict
            Dzdict=self.dat_z.dmi_ddict
            for key, Dz in Dzdict.items():
                Dx= Dxdict[key]
                Dy= Dydict[key]
                dmi_ddict[key] = merge_DMI(Dx, Dy, Dz)
                # if key==((0,1,1), 1,0) and False:
                #     print("----------------------------")
                #     print("Dx:", Dx*1000)
                #     Dx_z = Rxz.apply(Dx)
                #     print("Dx_z:", Dx_z*1000)
                #     print("Dy:", Dy*1000)
                #     Dy_z = Ryz.apply(Dy)
                #     print("Dy_z:", Dy_z*1000)
                #     print("Dz:", Dz*1000)
                #    print("Resulst:", merge_DMI(Dx, Dy, Dz)*1000)
            self.dat.dmi_ddict=dmi_ddict

    def write(self, path='TB2J_results'):
        self.dat.write_all(path=path)



def merge(path_x, path_y, path_z, save=True, path='TB2J_results'):
    m=Merger(path_x, path_y, path_z)
    m.merge_DMI()
    if save:
        m.write(path=path)
    return m.dat
# Rotation from x to z # Rotation from y to z # if key==((0,1,1), 1,0) and False: #     print("----------------------------") #     print("Dx:", Dx*1000) #     Dx_z = Rxz.apply(Dx) #     print("Dx_z:", Dx_z*1000) #     print("Dy:", Dy*1000) #     Dy_z = Ryz.apply(Dy) #     print("Dy_z:", Dy_z*1000) #     print("Dz:", Dz*1000) #    print("Resulst:", merge_DMI(Dx, Dy, Dz)*1000)