def Convolution(img): os.system('clear') Kernel = None InpStr = "" InpInt = 0 print("1) [[1, 1, 1], [1, 1, 1], [1, 1, 1]] / 9") print( "2) [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]] / 25" ) print() print("3) [[1, 1, 1], [1, 2, 1], [1, 1, 1]] / 10") print("4) [[1, 2, 1], [2, 4, 2], [1, 2, 1]] / 16") print("5) [[1, 1, 1], [1, 0, 1], [1, 1, 1]] / 9") print() print( "6) [[1, 4, 7, 4, 1], [4, 16, 26, 16, 4], [7, 26, 41, 26, 7], [4, 16, 26, 16, 4],[1, 4, 7, 4, 1]] / 273" ) print() print("7) [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]") print("8) [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]") print() print("9) [[1 * 129] * 129]]") print() print("10) [[1], [-1]]") print("11) [[1, -1]]") print("12) [[0, 1], [-1, 0]]") print("13) [[1, 0], [0, -1]]") print() print("14) [[1, 1, 1,], [0, 0, 0], [-1, -1, -1]]") print("15) [[1, 0, -1], [1, 0, -1], [1, 0, -1]]") print() print("16) [[0, 1, 0], [1, -4, 1], [0, 1, 0]]") print("17) [[1, 1, 1], [1, -8, 1], [1, 1, 1]]") print( "18) [[0, 0, 1, 0, 0], [0, 1, 2, 1, 0], [1, 2, -16, 2, 1], [0, 1, 2, 1, 0], [0, 0, 1, 0 ,0]]" ) print( "19) [[2, 4, 5, 4, 2], [4, 9, 12, 9, 4], [5, 12, 15, 12, 5], [4, 9, 12, 9, 4], [2, 4, 5, 4, 2]]" ) print() print("20) High-boost [0, -1, 0], [-1, 8, -1], [0, -1, 0]") print("21) High-boost [-1, -1, -1], [-1, 16, -1], [-1, -1, -1]") print("0) EXIT") while 1: InpStr = input("Input the kernal number you need: ") try: InpInt = int(InpStr) except: print("Input Error") continue else: if InpInt < 0 or InpInt > 30: print("Input Error") continue else: break if InpInt == 0: return img if InpInt == 1: Kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) / 9 if InpInt == 2: Kernel = np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) / 25 if InpInt == 3: Kernal = np.array([[1, 1, 1], [1, 2, 1], [1, 1, 1]]) / 10 if InpInt == 4: Kernel = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) / 16 if InpInt == 5: Kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) / 9 if InpInt == 6: Kernel = np.array([[1, 4, 7, 4, 1], [4, 16, 26, 16, 4], [7, 26, 41, 26, 7], [4, 16, 26, 16, 4], [1, 4, 7, 4, 1]]) / 273 if InpInt == 7: Kernel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) if InpInt == 8: Kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) if InpInt == 9: Kernel = np.array([[1 for n in range(129)] for n in range(129)]) / 129 if InpInt == 10: Kernel = np.array([[1], [-1]]) if InpInt == 11: Kernel = np.array([[1, -1]]) if InpInt == 12: Kernel = np.array([[0, 1], [-1, 0]]) if InpInt == 13: Kernel = np.array([[1, 0], [0, -1]]) if InpInt == 14: Kernel = np.array([[ 1, 1, 1, ], [0, 0, 0], [-1, -1, -1]]) if InpInt == 15: Kernel = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]]) if InpInt == 16: Kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) if InpInt == 17: Kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) if InpInt == 18: Kernel = np.array([[0, 0, 1, 0, 0], [0, 1, 2, 1, 0], [1, 2, -16, 2, 1], [0, 1, 2, 1, 0], [0, 0, 1, 0, 0]]) if InpInt == 19: Kernel = np.array([[2, 4, 5, 4, 2], [4, 9, 12, 9, 4], [ 5, 12, 15, 12, 5 ], [4, 9, 12, 9, 4], [2, 4, 5, 4, 2]]) / 115 if InpInt == 20: Kernel = np.array([[0, -1, 0], [-1, 8, -1], [0, -1, 0]]) if InpInt == 21: Kernel = np.array([[-1, -1, -1], [-1, 16, -1], [-1, -1, -1]]) #Kernel = np.array([[]]) #Kernel = np.array([[]]) """ InpInt = 0 print("1) Normal Convolution method") print("2) FFT-IFFT Method") while 1: InpStr = input("Now input the way to convolution: ") try: InpInt = int(InpStr) except: print("Input Error") continue else: if InpInt < 0 or InpInt > 2: print("Input Error") continue else: break if InpInt == 1: img = NormalConvolution(img, Kernel) else: NewImg = D2FFT(img, Kernel) for i in range(0, len(img)): for j in range(0, len(img[0])): try: img[i][j] = NewImg[i+1][j+1] except: print("Error") return [0] """ NewImg = D2FFT(img, Kernel) for i in range(0, len(img)): for j in range(0, len(img[0])): try: img[i][j] = NewImg[i + 1][j + 1] except: print("Error") return [0] Init.ArrOutput(img) return img
def main(OutputFile=False, initial_val=[], old_information="", Calc_Jaco=True): map_model = False File = 0 if OutputFile: FileName = os.path.join( "Output", model.model_name + str(Init.GetTime()) + ".model") File = open(FileName, "a") if len(initial_val) == 0: initial_val = model.initial_val initial_t = model.initial_t final_t = model.final_t delta_t = model.delta_t information = model.information if type(delta_t) == int: map_model = True delta_t = float(delta_t) if len(old_information) != 0: if information != old_information: while 1: print( "WARNING, you may used wrong model because old model information is different from new one." ) print(old_information) print(information) inp_str = input("Continue? (y/n)") if inp_str == "y": break elif inp_str == "n": sys.exit() else: os.system("clear") print("Input error") continue String = information String += "\n" String += str(initial_t) + " " String += str(final_t) + " " String += str(delta_t) + " \n" print(String) print("Data generate") if map_model == False: Val_set = Runge_Kutta("model.f", initial_val, initial_t, final_t, delta_t) Val_set = Val_set.tolist() if OutputFile: String += Init.ArrOutput(Val_set, Mode=0, Save_File=False) String = write_str(File, String) else: Val_set = map_calculator("model.f", initial_val, initial_t, final_t, delta_t) Val_set = Val_set.tolist() if OutputFile: String += Init.ArrOutput(Val_set, Mode=0, Save_File=False) String = write_str(File, String) Jaco_set = [] if Calc_Jaco: print("Jacobian generate") pool = multiprocessing.Pool(processes=MULTI_CORE) Jaco_set = pool.map(model.Jf, Val_set) pool.close() pool.join() if OutputFile: String = write_str(File, String) if OutputFile: print("Model Output") tmp = [] for i in range(0, len(Jaco_set)): tmp.append( np.resize(Jaco_set[i], (len(initial_val) * len(initial_val))).tolist()) String += Init.ArrOutput(tmp, Mode=0, Save_File=False) String = write_str(File, String) File.close() if CAL_ERROR: Data_block = [] for kase in range(0, len(Jaco_set) - 1): Jaco = Jaco_set[kase].tolist() tmp_block = [] tmp_block.append(Jaco) tmp_block.append(Val_set[kase]) tmp_block.append(Val_set[kase + 1]) tmp_block.append(len(initial_val)) Data_block.append(tmp_block) pool = multiprocessing.Pool(processes=MULTI_CORE) error_meow = pool.map(Cal_error_meow, Data_block) pool.close() pool.join() print(error_meow) FileName = os.path.join( "Output", model.model_name + "-error" + str(Init.GetTime()) + ".model") File = open(FileName, "w") File.write(str(error_meow)) File.close() x = [n + 1 for n in range(len(Jaco_set) - 1)] fig = plt.gcf() plt.grid(True) plt.plot(x, error_meow) plt.show() return information, initial_val, initial_t, final_t, delta_t, Val_set, Jaco_set