def get_strain_e(ParaIn, dstpath, StrainMode, BaseVec, strain = 0.0, Modei = 0): StrainOrMode = "Strain" if Modei == 0: StrainOrMode = "Mode" (flag_se, flag_continue, BaseName, RunStr, PreName) = getparam(ParaIn, dstpath, StrainOrMode) #BaseVec = eglue.get_base_vec(BaseName) Efilename = PreName + "_" + StrainOrMode + ".txt" if flag_continue: os.chdir(dstpath) #E0 is energy for strain-energy method, and stress for strain-stress method E0 = get_continue_strain_e(Efilename, flag_se, strain, Modei) if Modei == 0: os.chdir("../../") else: if Modei == 0: esutils.creat_folder(BaseName) shutil.copyfile("INPUT", BaseName + "/INPUT") #print os.getcwd() esutils.creat_folder(dstpath) eglue.copy_files(BaseName, dstpath) os.chdir(dstpath) if Modei > 0: StrainMatrix = strain*StrainMode[Modei-1] BaseVecNew = BaseVec .dot (crydef.strain2deformgrad(StrainMatrix)) crylat.print_lattice(BaseVecNew) eglue.write_base_vec(BaseName, BaseVecNew) os.system(RunStr + ">> FP_OUT") E0 = get_org_strain_e(BaseName, flag_se) print(E0) np.savetxt(Efilename, E0) if Modei == 0: os.chdir("../../") return E0
def get_strain_e(ParaIn, dstpath, StrainMode, BaseVec, strain=0.0, Modei=0): ''' In the strain folder, run first principles code and get the energy Parameters ---------- ParaIn: dict The dict of input parameters dstpath: str The destination path StrainMode: 3D np.ndarray, size: nx3x3 The strain mode. BaseVec: np.ndarray The crystal vector strain: float The value of the strain. e.g. 0.03 Modei: int The ith strain mode. When modei=0, it means the undeformed structure Return ------ E0: list The energy of current strain. in multi unit ''' StrainOrMode = "Strain" if Modei == 0: StrainOrMode = "Mode" (flag_se, flag_continue, BaseName, RunStr, PreName) = getparam(ParaIn, dstpath, StrainOrMode) EnergyCode = ParaIn['EnergyCode'].lower() eglue = importlib.import_module('elastic3rd.energy.{}'.format(EnergyCode)) #BaseVec = eglue.get_base_vec(BaseName) Efilename = PreName + "_" + StrainOrMode + ".txt" if flag_continue: os.chdir(dstpath) #E0 is energy for strain-energy method, and stress for strain-stress method E0 = get_continue_strain_e(Efilename, flag_se, strain, Modei) if Modei == 0: os.chdir("../../") else: if Modei == 0: esutils.creat_folder(BaseName) shutil.copyfile("INPUT", BaseName + "/INPUT") #print os.getcwd() esutils.creat_folder(dstpath) eglue.copy_files(BaseName, dstpath) os.chdir(dstpath) if Modei > 0: StrainMatrix = strain * StrainMode[Modei - 1] BaseVecNew = BaseVec.dot(crydef.strain2deformgrad(StrainMatrix)) crylat.print_lattice(BaseVecNew) eglue.write_base_vec(BaseName, BaseVecNew) os.system(RunStr + ">> FP_OUT") E0 = get_org_strain_e(BaseName, flag_se, EnergyCode=EnergyCode) np.savetxt(Efilename, E0) if Modei == 0: os.chdir("../../") return E0
def elastic3(INPUT = "INPUT"): #Print LOGO esutils.print_logo() #Read INPUT #ParaIn = esutils.read_input(INPUT) print("===================The input parameters===================") esutils.print_parain(ParaIn) flag_se = ParaIn['FlagSE'].lower() #Import glue as eglue #eglue = __import__("energy." + ParaIn['EnergyCode'], fromlist = ParaIn['EnergyCode']) #Deform mode this is for cubic #TODO : for any symmtry CrystalType = ParaIn['CrystalType'] Ord = ParaIn['Ord'] CalMode = ParaIn['CalMode'].lower() #StrainMode = [] if CalMode == 's': StrainIn = esutils.read_strainmode() if flag_se == "e": Cij_mode, coef_e, StrainMode = essym.CoefForSingleMode(CrystalType, Ord, StrainIn) elif flag_se == "s": pass elif CalMode == 'w': if flag_se == "e": coef_e, StrainMode = essym.gen_strain_mode(CrystalType, Ord) elif flag_se == "s": pass #Mode_index = range(1, 7) StrainList = esutils.gen_strain_list(ParaIn) n_Strain = len(StrainList) n_Mode = StrainMode.shape[0] #n_Mode = len(Mode_index) E = np.zeros((n_Strain, n_Mode)) if flag_se == "s": E = np.zeros((n_Strain, 6*n_Mode)) if flag_se == "e": Efilename_Mode = "Energy_Mode.txt" elif flag_se == "s": Efilename_Mode = "Stress_Mode.txt" #Get the energy run string BaseName = ParaIn['BaseName'] if ParaIn['EnergyRun']: RunStr = eglue.run(ParaIn['NP'], BaseName) else: RunStr = energy.glue.run() #os.system(RunStr) #Get the base vector BaseVec = eglue.get_base_vec(BaseName) print("====================Crystal Structure====================") V0 = crylat.print_lattice(BaseVec) #Calculate the energy of undeformed structure # creat the folder Mode0 dstpath = BaseName + "/Mode0" E0 = get_strain_e(ParaIn, dstpath, StrainMode, BaseVec) if CalMode == 's': shutil.copyfile("STRAINMODE", BaseName + "/STRAINMODE") #print("\n") print("\n==================Deformed Crystal========================") #print E for i in range(1, n_Mode + 1): print("----------------------------------------------------------") print("Start calculating Mode " + str(i)) flag_continue = 0 ModePath = BaseName + "/Mode" + str(i) if ParaIn['Continue']: flag_continue = esutils.iscontinue(ModePath, "Mode", flag_se) if flag_continue: E = get_continue_mode_e(ModePath, E, i, flag_se) else: print(os.getcwd()) esutils.creat_folder(ModePath) #os.chdir(ModePath) for j in range(0, n_Strain): print("Start calculating Strain " + str(StrainList[j]) + " in Mode " + str(i)) if j == int(n_Strain/2): if flag_se == "e": E[j, i-1] = E0[0] elif flag_se == "s": pass else: StrainPath = ModePath + "/Strain" + str(StrainList[j]) Strain = StrainList[j]/100.0 #print os.getcwd() Eij = get_strain_e(ParaIn, StrainPath, StrainMode, BaseVec, Strain, i) if flag_se == "e": E[j, i-1] = Eij[0] print("Energy:") esutils.print_e(Eij) elif flag_se == "s": pass os.chdir("../../../") print("End of Strain " + str(StrainList[j]) + " in Mode " + str(i) + "\n") os.chdir(ModePath) if flag_se == "e": np.savetxt(Efilename_Mode, E[:, i-1]) os.chdir("../../") print("End of Mode " + str(i) + "\n") np.savetxt(BaseName + "/EEnergy.txt", E) print("\n==================Post Processing========================") coef_fit = espost.get_coef(StrainList/100., E, V0, flag_se, 3) print(coef_fit) coef2 = coef_e.coef2 coef3 = coef_e.coef3 (C2, C3) = espost.get_cij(coef_fit, coef2, coef3, flag_se) essym.print_cijk(CrystalType, 2) print(C2) essym.print_cijk(CrystalType, 3) print(C3) print("========================!!!END!!!=========================")
def elastic3(): #Print LOGO esutils.print_logo() #Read INPUT if len(sys.argv) > 1: INPUT = sys.argv[1] else: INPUT = "INPUT" ParaIn = esutils.read_input(INPUT) print("===================The input parameters===================") esutils.print_parain(ParaIn) #Import glue as eglue eglue = __import__("energy." + ParaIn['EnergyCode'], fromlist=ParaIn['EnergyCode']) #Deform mode this is for cubic #TODO : for any symmtry Mode_index = range(1, 7) #Get the energy run string BaseName = ParaIn['BaseName'] if ParaIn['EnergyRun']: RunStr = eglue.run(ParaIn['NP'], BaseName) else: RunStr = energy.glue.run() #os.system(RunStr) #Get the base vector BaseVec = eglue.get_base_vec(BaseName) print("====================Crystal Structure====================") V0 = crylat.print_lattice(BaseVec) #Calculate the energy of undeformed structure # creat the folder Mode0 esutils.creat_folder(BaseName) dstpath = BaseName + "/Mode0" esutils.creat_folder(dstpath) eglue.copy_files(BaseName, dstpath) os.chdir(dstpath) os.system(RunStr) E0 = eglue.get_energy(BaseName) os.chdir("../../") print("Energy of undeformed structure:") esutils.print_e(E0) #print("\n") print("\n==================Deformed Crystal========================") StrainList = esutils.gen_strain_list(ParaIn) n_Strain = len(StrainList) n_Mode = len(Mode_index) E = np.zeros((n_Strain, n_Mode)) #print E for i in range(1, n_Mode + 1): print("----------------------------------------------------------") print("Start calculating Mode " + str(i)) ModePath = BaseName + "/Mode" + str(i) esutils.creat_folder(ModePath) for j in range(0, n_Strain): print("Start calculating Strain " + str(StrainList[j]) + " in Mode " + str(i)) if j == int(n_Strain / 2): E[j][i - 1] = E0[0] else: StrainPath = ModePath + "/Strain" + str(StrainList[j]) esutils.creat_folder(StrainPath) eglue.copy_files(BaseName, StrainPath) os.chdir(StrainPath) Strain = StrainList[j] / 100.0 StrainVerctor = crydef.deform_mode(Strain, i) StrainMatrix = crydef.vec2matrix(StrainVerctor) BaseVecNew = BaseVec.dot( crydef.strain2deformgrad(StrainMatrix)) eglue.write_base_vec(BaseName, BaseVecNew) os.system(RunStr) Eij = eglue.get_energy(BaseName) E[j][i - 1] = Eij[0] crylat.print_lattice(BaseVecNew) print("Energy:") esutils.print_e(Eij) os.chdir("../../../") print("End of Strain " + str(StrainList[j]) + " in Mode " + str(i) + "\n") print("End of Mode " + str(i) + "\n") np.savetxt(BaseName + "/EEnergy.txt", E) print("\n==================Post Processing========================") coef_fit = espost.get_coef(StrainList, E, V0) print coef_fit (C2, C3) = espost.get_cij(coef_fit, crystaltype) print C2 print C3 print("========================!!!END!!!=========================")
def elastic3(INPUT="INPUT"): #This is the main function for calculation #Print LOGO esutils.print_logo() #Read INPUT ParaIn = esutils.read_input(INPUT) print("===================The input parameters===================") esutils.print_parain(ParaIn) flag_se = ParaIn['FlagSE'].lower() EnergyCode = ParaIn['EnergyCode'].lower() #Import first principles code module according to the EnergyCode settings in INPUT file eglue = importlib.import_module('elastic3rd.energy.{}'.format(EnergyCode)) #Get some parameter's value from INPUT CrystalType = ParaIn['CrystalType'] Ord = ParaIn['Ord'] CalMode = ParaIn['CalMode'].lower() #Get the strain modes according to symmetry and order, the strain-stress method is under development if CalMode == 's': StrainIn = esutils.read_strainmode() if flag_se == "e": Cij_mode, coef_e, StrainMode = essym.CoefForSingleMode( CrystalType, Ord, StrainIn) elif flag_se == "s": pass elif CalMode == 'w': if flag_se == "e": coef_e, StrainMode = essym.gen_strain_mode(CrystalType, Ord) elif flag_se == "s": pass #Get the strain list StrainList = esutils.gen_strain_list(ParaIn) n_Strain = len(StrainList) n_Mode = StrainMode.shape[0] #Init the energy E = np.zeros((n_Strain, n_Mode)) if flag_se == "s": E = np.zeros((n_Strain, 6 * n_Mode)) if flag_se == "e": Efilename_Mode = "Energy_Mode.txt" elif flag_se == "s": Efilename_Mode = "Stress_Mode.txt" #Get the energy run string BaseName = ParaIn['BaseName'] if ParaIn['EnergyRun']: RunStr = eglue.run(ParaIn['NP'], BaseName) else: RunStr = energy.glue.run() #Get the base vector BaseVec = eglue.get_base_vec(BaseName) print("====================Crystal Structure====================") V0 = crylat.print_lattice(BaseVec) #Calculate the energy of undeformed structure # creat the folder Mode0 dstpath = BaseName + "/Mode0" E0 = get_strain_e(ParaIn, dstpath, StrainMode, BaseVec) if CalMode == 's': shutil.copyfile("STRAINMODE", BaseName + "/STRAINMODE") #Begin the calculation of deformed structure print("\n==================Deformed Crystal========================") #Do a loop over the strain modes for i in range(1, n_Mode + 1): print("----------------------------------------------------------") print("Start calculating Mode " + str(i)) flag_continue = 0 #Judge if this mode has been calculated or not ModePath = BaseName + "/Mode" + str(i) if ParaIn['Continue']: flag_continue = esutils.iscontinue(ModePath, "Mode", flag_se) if flag_continue: #Calculated previously E = get_continue_mode_e(ModePath, E, i, flag_se) else: #Not calculated yet, new calculations print(os.getcwd()) esutils.creat_folder(ModePath) #Do a loop over strain list for j in range(0, n_Strain): print("Start calculating Strain " + str(StrainList[j]) + " in Mode " + str(i)) if j == int(n_Strain / 2): #This is the un-deformed structure if flag_se == "e": E[j, i - 1] = E0[0] elif flag_se == "s": pass else: #Deformed structure StrainPath = ModePath + "/Strain" + str(StrainList[j]) Strain = StrainList[j] / 100.0 #The judge on the continue mode is done in the following function Eij = get_strain_e(ParaIn, StrainPath, StrainMode, BaseVec, Strain, i) if flag_se == "e": E[j, i - 1] = Eij[0] print("Energy:") elif flag_se == "s": pass os.chdir("../../../") print("End of Strain " + str(StrainList[j]) + " in Mode " + str(i) + "\n") os.chdir(ModePath) if flag_se == "e": np.savetxt(Efilename_Mode, E[:, i - 1]) os.chdir("../../") print("End of Mode " + str(i) + "\n") #Sve the energy np.savetxt(BaseName + "/EEnergy.txt", E) ##Post process print("\n==================Post Processing========================") coef_fit = espost.get_coef(StrainList / 100., E, V0, flag_se, 3) print(coef_fit) coef2 = coef_e.coef2 coef3 = coef_e.coef3 (C2, C3) = espost.get_cij(coef_fit, coef2, coef3, flag_se) essym.print_cijk(CrystalType, 2) print(C2) essym.print_cijk(CrystalType, 3) print(C3) print("========================!!!END!!!=========================")