def _read_amr(self): """Open the oct file, read in octs level-by-level. For each oct, only the position, index, level and domain are needed - its position in the octree is found automatically. The most important is finding all the information to feed oct_handler.add """ self.oct_handler = RAMSESOctreeContainer( self.ds.domain_dimensions / 2, self.ds.domain_left_edge, self.ds.domain_right_edge, ) root_nodes = self.amr_header["numbl"][self.ds.min_level, :].sum() self.oct_handler.allocate_domains(self.total_oct_count, root_nodes) mylog.debug( "Reading domain AMR % 4i (%0.3e, %0.3e)", self.domain_id, self.total_oct_count.sum(), self.ngridbound.sum(), ) f = self.amr_file f.seek(self.amr_offset) min_level = self.ds.min_level max_level = read_amr( f, self.amr_header, self.ngridbound, min_level, self.oct_handler ) self.max_level = max_level self.oct_handler.finalize() # Close AMR file f.close()
def _read_amr(self): """Open the oct file, read in octs level-by-level. For each oct, only the position, index, level and domain are needed - its position in the octree is found automatically. The most important is finding all the information to feed oct_handler.add """ self.oct_handler = RAMSESOctreeContainer(self.ds.domain_dimensions / 2, self.ds.domain_left_edge, self.ds.domain_right_edge) root_nodes = self.amr_header['numbl'][self.ds.min_level, :].sum() self.oct_handler.allocate_domains(self.total_oct_count, root_nodes) mylog.debug("Reading domain AMR % 4i (%0.3e, %0.3e)", self.domain_id, self.total_oct_count.sum(), self.ngridbound.sum()) f = self.amr_file f.seek(self.amr_offset) def _ng(c, l): if c < self.amr_header['ncpu']: ng = self.amr_header['numbl'][l, c] else: ng = self.ngridbound[c - self.amr_header['ncpu'] + self.amr_header['nboundary'] * l] return ng min_level = self.ds.min_level # yt max level is not the same as the RAMSES one. # yt max level is the maximum number of additional refinement levels # so for a uni grid run with no refinement, it would be 0. # So we initially assume that. max_level = 0 nx, ny, nz = (((i - 1.0) / 2.0) for i in self.amr_header['nx']) for level in range(self.amr_header['nlevelmax']): # Easier if do this 1-indexed for cpu in range(self.amr_header['nboundary'] + self.amr_header['ncpu']): #ng is the number of octs on this level on this domain ng = _ng(cpu, level) if ng == 0: continue ind = fpu.read_vector(f, "I").astype("int64") # NOQA fpu.skip(f, 2) pos = np.empty((ng, 3), dtype='float64') pos[:, 0] = fpu.read_vector(f, "d") - nx pos[:, 1] = fpu.read_vector(f, "d") - ny pos[:, 2] = fpu.read_vector(f, "d") - nz #pos *= self.ds.domain_width #pos += self.dataset.domain_left_edge fpu.skip(f, 31) #parents = fpu.read_vector(f, "I") #fpu.skip(f, 6) #children = np.empty((ng, 8), dtype='int64') #for i in range(8): # children[:,i] = fpu.read_vector(f, "I") #cpu_map = np.empty((ng, 8), dtype="int64") #for i in range(8): # cpu_map[:,i] = fpu.read_vector(f, "I") #rmap = np.empty((ng, 8), dtype="int64") #for i in range(8): # rmap[:,i] = fpu.read_vector(f, "I") # We don't want duplicate grids. # Note that we're adding *grids*, not individual cells. if level >= min_level: assert (pos.shape[0] == ng) n = self.oct_handler.add(cpu + 1, level - min_level, pos, count_boundary=1) self._error_check(cpu, level, pos, n, ng, (nx, ny, nz)) if n > 0: max_level = max(level - min_level, max_level) self.max_level = max_level self.oct_handler.finalize() # Close AMR file f.close()
from yt.geometry.fake_octree import create_fake_octree from yt.geometry.oct_container import RAMSESOctreeContainer, ParticleOctreeContainer import numpy as np nocts = 3 max_level = 12 dn = 2 dd = np.ones(3, dtype='i4') * dn dle = np.ones(3, dtype='f8') * 0.0 dre = np.ones(3, dtype='f8') fsub = 0.25 domain = 1 oct_handler = RAMSESOctreeContainer(dd, dle, dre) leaves = create_fake_octree(oct_handler, nocts, max_level, dd, dle, dre, fsub) mask = np.ones((nocts, 8), dtype='bool') cell_count = nocts * 8 oct_counts = oct_handler.count_levels(max_level, 1, mask) level_counts = np.concatenate(([ 0, ], np.cumsum(oct_counts))) fc = oct_handler.fcoords(domain, mask, cell_count, level_counts.copy()) leavesb = oct_handler.count_leaves(mask) assert leaves == leavesb #Now take the fcoords, call them particles and recreate the same octree print "particle-based recreate" oct_handler2 = ParticleOctreeContainer(dd, dle, dre) oct_handler2.allocate_domains([nocts]) oct_handler2.n_ref = 1 #specifically make a maximum of 1 particle per oct oct_handler2.add(fc, 1)