def perturb(self, base_size=None, pert=1e-6, wave=6, directory=".", write=True): self.cubes = {} map_to_eigen = { 'd': 0, 'vx': 2, 'vy': 3, 'vz': 4, 'Bx': -1, 'By': 5, 'Bz': 6, 'E': 1 } map_to_label = { 'd': 'density', 'vx': 'x-velocity', 'vy': 'y-velocity', 'vz': 'z-velocity', 'Bx': 'Bx', 'By': 'By', 'Bz': 'Bz', 'E': 'TotalEnergy' } face_offset = { 'Bx': nar([1, 0, 0]), 'By': nar([0, 1, 0]), 'Bz': nar([0, 0, 1]) } for field in ['d', 'E', 'vx', 'vy', 'vz', 'Bx', 'By', 'Bz']: size = base_size + face_offset.get(field, 0) this_set = np.ones(size) * quan[field] if field in ['vx', 'vy', 'vz', 'E']: this_set *= self.cubes['density'] self.cubes[map_to_label[field]] = this_set for field in ['d', 'E', 'vx', 'vy', 'vz', 'Bx', 'By', 'Bz']: size = base_size + face_offset.get(field, 0) amplitude = np.zeros(size) amplitude[:size[0] / 2, :, :] = 1 amplitude[size[0] / 2:, :, :] = -1 if field is not 'Bx': the_wave = pert * amplitude * self.right[ map_to_eigen[field]][wave] self.cubes[map_to_label[field]] += the_wave for field in ['d', 'E', 'vx', 'vy', 'vz', 'Bx', 'By', 'Bz']: this_filename = "%s/%s_16.h5" % (directory, map_to_label[field]) if field in ['vx', 'vy', 'vz', 'E']: self.cubes[map_to_label[field]] /= self.cubes[ map_to_label['d']] if write: enzo_write.dump_h5(self.cubes[map_to_label[field]], this_filename)
def rot_write(self, k_rot=None, base_size=None, pert=1e-6, directory=".", write=True, wave=None, pert_shape='fft', start=True, real_fft=True, blah=True): #if self.cubes is None: # print("no cubes") #else: # print("keys",self.cubes.keys()) field_list = ['d', 'px', 'py', 'pz', 'hx', 'hy', 'hz', 'e'] if self.form == 'rb96': field_list = ['d', 'px', 'py', 'pz', 'hx', 'hy', 'hz', 'p'] enzo_field_list = [ 'd', 'vx', 'vy', 'vz', 'hx', 'hy', 'hz', 'e_specific' ] if self.HydroMethod == 6: face_offset = { 'hx': nar([1, 0, 0]), 'hy': nar([0, 1, 0]), 'hz': nar([0, 0, 1]) } else: face_offset = {} if start: self.all_hats = {} #self.cubes_test={} self.all_p = {} self.temp_right_back = {} for f in field_list: self.all_p[f] = 0. cube_slice = [ slice(0, base_size[0]), slice(0, base_size[1]), slice(0, base_size[2]) ] print("Eigenvector formulation %s" % self.form) self.directory = directory map_to_label = { 'd': 'density', 'vx': 'x-velocity', 'vy': 'y-velocity', 'vz': 'z-velocity', 'hx': 'Bx', 'hy': 'By', 'hz': 'Bz', 'e_specific': 'TotalEnergy', 'p': 'GasPressure' } if pert_shape == 'fft': self.wave_to_fields(k_rot, wave) #breadcrum 1 kint = k_rot.astype(int) for f in field_list: if real_fft: this_size = np.array( [base_size[0], base_size[1], base_size[2] // 2 + 1]) else: this_size = base_size #np.array([base_size[0],base_size[1],base_size[2]//2+1]) self.all_hats[f] = np.zeros(this_size) * 1j if blah: self.all_hats[f][kint[0, ...], kint[1, ...], kint[2, ...]] = self.rot[f] * pert else: self.all_hats[f] = self.rot[f] * pert print("ug %s " % f + str(nz(self.all_hats[f]))) #pdb.set_trace() #print( "put %s %0.2e"%(f,(self.rot[f]*pert)[0])) if not real_fft: #self.all_hats[f][kint[0,...],kint[1,...],-kint[2,...]] = (self.all_hats[f][kint[0,...],kint[1,...],kint[2,...]] ).conj() if kint is not None: self.all_hats[f][-kint[0, ...], -kint[1, ...], -kint[2, ...]] = (self.all_hats[f][ kint[0, ...], kint[1, ...], kint[2, ...]]).conj() else: print( "Shoot, i haven't sorted the conjugation yet. I hope you did." ) #print("wtf",kint[0,...],kint[1,...],-kint[2,...]) #print("make hats ROT: %3s %s"%(f,str(self.rot[f]*pert))) #print("make hats fld: %3s %s"%(f,str(nz(self.all_hats[f])))) #print("make hats prt: %3s %s"%(f,str(pert))) for f in field_list: print("=== pre %3s " % f + str(len(nonzero(self.all_hats[f])))) # print("=== pre %3s "%f+str(nz(self.all_hats[f]))) for f in field_list: # print("=== two %3s "%f+str(len(nonzero(self.all_hats[f])))) if 'tmp' in dir(): del tmp if real_fft: tmp = np.fft.irfftn(self.all_hats[f]) #np.fft.irfftn(self.all_hats[f]) else: tmp = np.fft.ifftn(self.all_hats[f]) print("=== pos %3s " % f + str(len(nonzero(self.all_hats[f])))) real_mean = np.mean(np.abs(tmp.real)) imag_mean = np.mean(np.abs(tmp.imag)) if (real_mean + imag_mean) > 1e-16: if imag_mean / (real_mean + imag_mean) > 1e-9: print("Warning: large imaginary component") self.all_p[f] += tmp.real * tmp.size * 0.5 elif pert_shape == 'square_x': size = base_size #+face_offset.get(f,0) amplitude = np.zeros(size) amplitude[:size[0] // 2, :, :] = 1 amplitude[size[0] // 2:, :, :] = -1 for f in field_list: self.all_p[f] = amplitude * pert[0] * self.right[self.wave][f] #print("WTF CLOWN ", pert[0]*self.right[self.wave][f], self.wave,f) # if self.cubes is None: self.cubes = fieldthing() for field in field_list: size = base_size + face_offset.get(field, 0) if field in self.cubes: this_set = self.cubes[field] #print("recalw: %s %s %s"%(field,map_to_label[field],str(this_set.shape))) else: this_set = np.ones(size) * self.quan[field] #print("Setup: %s %s %s"%(field,map_to_label[field],str(this_set.shape))) #if field in ['vx','py','pz','e']: # if np.abs(np.mean(self.cubes['density']) - 1.0) > 1e-7: # print("YOU MAY NOT WANT TO BE MULTIPLYING ENERGY BY DENSITY INITALLLY.") # #I think actually we don't, but it's what Enzo currently does. # this_set *= self.cubes['density'] self.cubes[field] = this_set for field in field_list: size = base_size + face_offset.get(field, 0) self.cubes[field][cube_slice] += self.all_p[field] #self.temp_right_back[field]=np.fft.fftn(self.cubes[map_to_label[field]][cube_slice] ) #self.temp_right_back[field]=np.fft.rfftn(self.cubes[map_to_label[field]][cube_slice] ) #self.cubes_test[field] = self.cubes[map_to_label[field]][cube_slice] - self.quan[field] for field in enzo_field_list: #self.cubes[map_to_label[field]] = wrap_faces(self.cubes[map_to_label[field]], field) if self.HydroMethod == 6: wrap_faces(self.cubes[field], field) this_filename = "%s/%s_16.h5" % (directory, map_to_label[field]) #if field in ['px','py','pz','e']: # vel_field = vp[field] # self.cubes[map_to_label[vel_field]] = self.cubes[map_to_label['d']]self.cubes[map_to_label['d']] if write: enzo_write.dump_h5(self.cubes[field], this_filename) print("wrote " + this_filename + " with shape " + str(self.cubes[field].shape))
def rot_write(self, k_rot=None, base_size=None, pert=1e-6, directory=".", write=True, wave=None, pert_shape='fft'): #if self.cubes is None: # print("no cubes") #else: # print("keys",self.cubes.keys()) all_hats = {} all_p = {} face_offset = { 'hx': nar([1, 0, 0]), 'hy': nar([0, 1, 0]), 'hz': nar([0, 0, 1]) } cube_slice = [ slice(0, base_size[0]), slice(0, base_size[1]), slice(0, base_size[2]) ] print("Eigenvector formulation %s" % self.form) field_list = ['d', 'vx', 'vy', 'vz', 'hx', 'hy', 'hz', 'e'] if self.form == 'rb96': field_list = ['d', 'vx', 'vy', 'vz', 'hx', 'hy', 'hz', 'p'] self.directory = directory if pert_shape == 'fft': self.wave_to_fields(k_rot, wave) kint = k_rot.astype(int) for f in field_list: all_hats[f] = np.zeros(base_size) * 1j all_hats[f][kint[0, ...], kint[1, ...], kint[2, ...]] = self.rot[f] * pert all_hats[f][-kint[0, ...], -kint[1, ...], -kint[2, ...]] = (all_hats[f][kint[0, ...], kint[1, ...], kint[2, ...]]).conj() for f in field_list: tmp = np.fft.ifftn(all_hats[f]) real_mean = np.mean(np.abs(tmp.real)) imag_mean = np.mean(np.abs(tmp.imag)) if (real_mean + imag_mean) > 1e-16: if imag_mean / (real_mean + imag_mean) > 1e-9: print("Warning: large imaginary component") all_p[f] = tmp.real * tmp.size * 0.5 elif pert_shape == 'square_x': size = base_size #+face_offset.get(f,0) amplitude = np.zeros(size) amplitude[:size[0] / 2, :, :] = 1 amplitude[size[0] / 2:, :, :] = -1 for f in field_list: all_p[f] = amplitude * pert[0] * self.right[self.wave][f] #print("WTF CLOWN ", pert[0]*self.right[self.wave][f], self.wave,f) # if self.cubes is None: self.cubes = {} map_to_label = { 'd': 'density', 'vx': 'x-velocity', 'vy': 'y-velocity', 'vz': 'z-velocity', 'hx': 'Bx', 'hy': 'By', 'hz': 'Bz', 'e': 'TotalEnergy', 'p': 'GasPressure' } face_offset = { 'hx': nar([1, 0, 0]), 'hy': nar([0, 1, 0]), 'hz': nar([0, 0, 1]) } for field in field_list: size = base_size + face_offset.get(field, 0) if map_to_label[field] in self.cubes: this_set = self.cubes[map_to_label[field]] else: this_set = np.ones(size) * self.quan[field] if field in ['vx', 'vy', 'vz', 'e']: if np.abs(np.mean(self.cubes['density']) - 1.0) > 1e-7: print( "YOU MAY NOT WANT TO BE MULTIPLYING ENERGY BY DENSITY INITALLLY." ) #I think actually we don't, but it's what Enzo currently does. this_set *= self.cubes['density'] self.cubes[map_to_label[field]] = this_set for field in field_list: size = base_size + face_offset.get(field, 0) self.cubes[map_to_label[field]][cube_slice] += all_p[field] for field in field_list: #self.cubes[map_to_label[field]] = wrap_faces(self.cubes[map_to_label[field]], field) wrap_faces(self.cubes[map_to_label[field]], field) this_filename = "%s/%s_16.h5" % (directory, map_to_label[field]) if field in ['vx', 'vy', 'vz', 'e']: self.cubes[map_to_label[field]] /= self.cubes[ map_to_label['d']] if write: enzo_write.dump_h5(self.cubes[map_to_label[field]], this_filename) print("wrote " + this_filename)
def r16_cube_right_fast(directory, write=True): #base_size = nar([64]*3) base_size = nar([16] * 3) pert = 1e-6 wave = 6 quan = {} d = 1. vx = vy = vz = 0 Bx, By, Bz = 1.0, 1.41421, 0.5 P = 0.6 Gamma = 1.6666666667 Egas = P / ((Gamma - 1) * d) E = 0.5 * (vx * vx + vy * vy + vz * vz) + 0.5 * (Bx * Bx + By * By + Bz * Bz) / d + Egas quan.update({ 'd': d, 'vx': vx, 'vy': vy, 'vz': vz, 'Bx': Bx, 'By': By, 'Bz': Bz, 'P': P, 'Gamma': Gamma, 'Egas': Egas, 'E': E }) right = p49_eigen.eigen(d, vx, vy, vz, Bx, By, Bz, E, Gamma=Gamma) speeds = p49_eigen.speeds(d, vx, vy, vz, Bx, By, Bz, E, Gamma=Gamma) cubes = {} thing = { 'd': 0, 'vx': 2, 'vy': 3, 'vz': 4, 'Bx': -1, 'By': 5, 'Bz': 6, 'E': 1 } thing2 = { 'd': 'density', 'vx': 'x-velocity', 'vy': 'y-velocity', 'vz': 'z-velocity', 'Bx': 'Bx', 'By': 'By', 'Bz': 'Bz', 'E': 'TotalEnergy' } off = {'Bx': nar([1, 0, 0]), 'By': nar([0, 1, 0]), 'Bz': nar([0, 0, 1])} for field in ['d', 'E', 'vx', 'vy', 'vz', 'Bx', 'By', 'Bz']: size = base_size + off.get(field, 0) print(field, size) this_set = np.ones(size) * quan[field] if field in ['vx', 'vy', 'vz', 'E']: this_set *= cubes['density'] cubes[thing2[field]] = this_set for field in ['d', 'E', 'vx', 'vy', 'vz', 'Bx', 'By', 'Bz']: size = base_size + off.get(field, 0) print(field, size) amplitude = np.zeros(size) amplitude[:size[0] / 2, :, :] = 1 amplitude[size[0] / 2:, :, :] = -1 if field is not 'Bx': #this_set += pert*amplitude*right[ thing[field] ][wave] the_wave = pert * amplitude * right[thing[field]][wave] #this_set = cubes[thing2[field]]+ the_wave cubes[thing2[field]] += the_wave for field in ['d', 'E', 'vx', 'vy', 'vz', 'Bx', 'By', 'Bz']: this_filename = "%s/%s_16.h5" % (directory, thing2[field]) if field in ['vx', 'vy', 'vz', 'E']: cubes[thing2[field]] /= cubes[thing2['d']] if write: enzo_write.dump_h5(cubes[thing2[field]], this_filename) for a, b in [['d', 'Density'], ['vx', 'x-velocity'], ['vy', 'y-velocity'], ['E', 'TotalEnergy']]: quan[b] = quan[a] return cubes, right, quan
EigenVector[5, 0] = 1.687435 EigenVector[6, 0] = 0.596600 if 1: EigenVector[0, 4] = 0.898492 EigenVector[1, 4] = 0.792409 EigenVector[2, 4] = 0.441938 EigenVector[3, 4] = 0.824454 EigenVector[4, 4] = 0.291489 EigenVector[5, 4] = -0.405521 EigenVector[6, 4] = -0.143374 for i, j in zip(right[:, 4], EigenVector[:, 4]): print(i, j, (i - j) / (i + j)) """ shape = np.array([16,16,16]) dx = 1./shape x,y,z=np.mgrid[0:1:dx[0],0:1:dx[1],0:1:dx[2]] density=np.ones(shape) vx = np.zeros(shape) vy = np.zeros(shape) vz = np.zeros(shape) directory = '/Users/dcollins/scratch/Paper49b_play/r13d_left_fast_new' if 0: r_circ = 0.2 center=[0.5,0.5] r_vec = ((x-center[0])**2+(y-center[1])**2)**0.5 mask = r_vec < r_circ vx[mask] = -0.1*(y[mask]-center[1])