def cartesian(wwinp): """This function reads in a Cartesian WWINP file and returns a structured mesh tagged with weight window lower bounds. Parameters ---------- wwinp : file name The weight window input file to create a mesh from. """ print "Parsing Cartesian WWINP" particle_identifier = linecache.getline(wwinp, 1).split()[2] if particle_identifier == '1': particle = 'n' elif particle_identifier =='2': particle = 'p' # collect easy to parse energy group and mesh sized info num_e_bins = int(linecache.getline(wwinp, 2).split()[-1]) # total number of fine mesh points nfx, nfy, nfz = \ [int(float(x)) for x in linecache.getline(wwinp, 3).split()[0:3]] x0, y0, z0 = [float(x) for x in linecache.getline(wwinp, 3).split()[3:6]] # number of course points ncx, ncy, ncz = \ [int(float(x)) for x in linecache.getline(wwinp, 4).split()[0:3]] # get the x, y, and z bounds x_bounds, x_last_line = block_2_bounds(wwinp, 5, ncx) y_bounds, y_last_line = block_2_bounds(wwinp, x_last_line + 1, ncy) z_bounds, z_last_line = block_2_bounds(wwinp, y_last_line + 1, ncz) # create structured mesh these bounds sm = ScdMesh(x_bounds, y_bounds, z_bounds) # Find the first line of WW values by counting through the e_bin values # until the expected number of e_bins is reached # Then create a root level tag with energy the energy bounds. e_upper_bounds = [] line_num = z_last_line + 1 while len(e_upper_bounds) < num_e_bins: e_upper_bounds += \ [float(x) for x in linecache.getline(wwinp, line_num).split()] line_num += 1 ww_first_line = line_num # tag structured mesh with WW values tag_mesh(sm, wwinp, ww_first_line, num_e_bins, nfx*nfy*nfz, particle) # tag root level of sm with energy bounds tag_e_bin = sm.imesh.createTag("E_upper_bounds", len(e_upper_bounds), float) tag_e_bin[sm.imesh.rootSet] = e_upper_bounds return sm
def setUp(self): self.mesh = iMesh.Mesh() self.sm = ScdMesh( range(10,15), # i = 0,1,2,3 range(21,25), # j = 0,1,2 range(31,34), # k = 0,1 self.mesh ) self.I = range(0,4) self.J = range(0,3) self.K = range(0,2)
def test_get_divs(self): x = [1, 2.5, 4, 6.9] y = [-12, -10, -.5] z = [100, 200] sm = ScdMesh( x, y, z ) self.assertEqual( sm.getDivisions('x'), x ) self.assertEqual( sm.getDivisions('y'), y ) self.assertEqual( sm.getDivisions('z'), z )
def test_large_iterator(self): print "building large mesh" big = ScdMesh(range(1,100), range(101,200), range(201,300)) print "iterating (1)" for i in big.iterateHex(): pass print "iterating (2)" for i in big.iterateHex( 'yzx' ): pass
def test_hex_volume(self): sm = ScdMesh( [0,1,3], [-3,-2,0], [12,13,15] ) self.assertEqual( sm.getHexVolume(0,0,0), 1 ) self.assertEqual( sm.getHexVolume(1,0,0), 2 ) self.assertEqual( sm.getHexVolume(0,1,0), 2 ) self.assertEqual( sm.getHexVolume(1,1,0), 4 ) self.assertEqual( sm.getHexVolume(1,1,1), 8 ) ijk_all = itertools.product(*([[0,1]]*3)) for V, ijk in itertools.izip_longest(sm.iterateHexVolumes(), ijk_all): self.assertEqual( V, sm.getHexVolume(*ijk) )
def test_unequal_grid_size(self): """Test creating an mmgrid on a mesh with uneven grid spacing""" grid_side = [-3, 0, .1, .2, 3] sm = ScdMesh(*([grid_side] * 3)) grid = mmgrid.mmGrid(sm) grid.generate(5) #, True) for ijk, x in numpy.ndenumerate(grid.grid): self.assertAlmostEqual(sum(x['mats']), 1.0, msg='Normality at ijk={0}:\n' ' sum({1}) = {2} != 1.0'.format( ijk, x['mats'], sum(x['mats'])))
def test_get_vtx(self): # mesh with valid i values 0-4, j values 0-3, k values 0-2 x_range = numpy.array(range(10,15),dtype=numpy.float64) y_range = numpy.array(range(21,24),dtype=numpy.float64) z_range = numpy.array(range(31,33),dtype=numpy.float64) sm = ScdMesh( x_range, y_range, z_range, self.mesh ) for i,x in enumerate(x_range): for j,y in enumerate(y_range): for k,z in enumerate(z_range): vtx = sm.getVtx(i,j,k) vcoord = self.mesh.getVtxCoords( vtx ) self.assertTrue( all( vcoord == [x,y,z]) )
def test_get_hex(self): # mesh with valid i values 0-4, j values 0-3, k values 0-2 sm = ScdMesh( range(11,16), range(21,25), range(31,34) ) def check( e ): self.assertTrue( isinstance(e, iBase.Entity) ) check(sm.getHex(0, 0, 0)) check(sm.getHex(1, 1, 1)) check(sm.getHex(3, 0, 0)) check(sm.getHex(3, 2, 1)) self.assertRaises(ScdMeshError, sm.getHex, -1,-1,-1) self.assertRaises(ScdMeshError, sm.getHex, 4, 0, 0) self.assertRaises(ScdMeshError, sm.getHex, 0, 3, 0) self.assertRaises(ScdMeshError, sm.getHex, 0, 0, 2)
def test_mmgrid_generate(self): grid_side = [-5, -3.75, -2.5, -1.25, 0, 1.25, 2.5, 3.75, 5] sm = ScdMesh(*([grid_side] * 3)) grid = mmgrid.mmGrid(sm) grid.generate(2, True) for ijk, x in numpy.ndenumerate(grid.grid): self.assertAlmostEqual(sum(x['mats']), 1.0, msg='Normality at ijk={0}:\n' ' sum({1}) = {2} != 1.0'.format( ijk, x['mats'], sum(x['mats']))) grid.create_tags()
def test_rayframes(self): # a mesh with divisions at -1, 0, and 1 for all three dimensions sm = ScdMesh(*([(-1, 0, 1)] * 3)) mg = mmgrid.mmGrid(sm) squares = [(-1, 0, -1, 0), (-1, 0, 0, 1), (0, 1, -1, 0), (0, 1, 0, 1)] ijks = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1]] for (ijk, ab), ab_test, ijk_test in itertools.izip_longest( mg._rayframe('x'), squares, ijks): self.assertEqual(ab, ab_test) self.assertEqual(ijk, ijk_test)
def create_ww_mesh(flux_mesh, e_group_names): """ This function reads a flux mesh and returns a weight window mesh tagged with all zeros in every energy group. This is used for the intial MAGIC weight window generation, for which no preexisting weight window mesh is given. Parameters ---------- flux_mesh : ScdMesh A ScdMesh tagged with fluxes in the form X_group_YYY and or X_group_total. Addition required tags are "particle" (1 for n, 2 for p) and E_group_bounds (vector of energy upper bounds). e_group_names : vector of energy names In the form X_group_YYY or X_group_total. """ ww_mesh = ScdMesh(flux_mesh.getDivisions('x'),\ flux_mesh.getDivisions('y'),\ flux_mesh.getDivisions('z')) # create ww tags for e_group_name in e_group_names: ww_tag = ww_mesh.imesh.createTag('ww_{0}'.format(e_group_name), 1, float) for voxel in ww_mesh.iterateHex('xyz'): ww_tag[voxel] = 0 # create e_upper_bound tags e_upper_bounds = \ flux_mesh.imesh.getTagHandle("E_upper_bounds")[flux_mesh.imesh.rootSet] if isinstance(e_upper_bounds, float): e_upper_bounds = [e_upper_bounds] e_tag = ww_mesh.imesh.createTag("E_upper_bounds", len(e_upper_bounds), float) e_tag[ww_mesh.imesh.rootSet] = \ flux_mesh.imesh.getTagHandle("E_upper_bounds")[flux_mesh.imesh.rootSet] # create particle tag particle = flux_mesh.imesh.getTagHandle("particle")[ flux_mesh.imesh.rootSet] particle_tag = ww_mesh.imesh.createTag("particle", 1, int) particle_tag[ww_mesh.imesh.rootSet] = particle return ww_mesh
def test_create(self): sm = ScdMesh( range(1,5), range(1,4), range(1,3), self.mesh ) self.assertEqual( sm.dims, (0,0,0,3,2,1) )
def test_mmgrid_create(self): sm = ScdMesh(*([(-1, 0, 1)] * 3)) grid = mmgrid.mmGrid(sm) self.assertEqual(grid.grid.shape, (2, 2, 2)) self.assertEqual(grid.grid[0, 0, 0]['mats'].shape, (3, )) self.assertEqual(grid.grid[0, 0, 0]['errs'].shape, (3, ))
def read_meshtal(filename, tally_line, norm=1.0, **kw): """Read an MCNP meshtal file and return a tagged structured mesh for it The optional normalization factor will be multiplied into each flux value. This can be used to rescale a tally if a tally multiplier was not used in the original MCNP problem. Parameters ---------- filename : string File path to meshtal file. tally_line : int Line number in file where tally begins norm : float, optional Normalization factor to multiply into each flux value. Keyword arguments: smesh: An existing scdmesh on which to tag the fluxes. A ScdMeshError is raised if this mesh has incompatible ijk dims Returns ------- sm : ScdMesh object Opened structured mesh from filename, with meshtally data tagged """ # Getting relevant information from meshtal header meshtal_type = find_meshtal_type(filename, tally_line) m = find_first_line(filename, tally_line) x_bounds, y_bounds, z_bounds, e_groups = \ find_mesh_bounds( filename, tally_line ) # Calculating pertinent information from meshtal header and input spatial_points = (len(x_bounds) - 1) * (len(y_bounds) - 1) * (len(z_bounds) - 1) if len(e_groups) > 2: e_bins = len(e_groups) #don't substract 1; cancels with totals bin elif len(e_groups ) == 2: #for 1 energy bin, meshtal doesn't have TOTALS group e_bins = 1 sm = ScdMesh(x_bounds, y_bounds, z_bounds) if 'smesh' in kw: dims = kw['smesh'].dims if dims != sm.dims: raise ScdMeshError( \ "Incorrect dimension in preexisting structured mesh") sm = kw['smesh'] # Tagging structured mesh with particle type at root level tag_particle = sm.imesh.createTag("particle", 1, int) if meshtal_type == 'n': tag_particle[sm.imesh.rootSet] = 1 elif meshtal_type == 'p': tag_particle[sm.imesh.rootSet] = 2 # Tagging structured mesh with energy lower bounds (at root level) # only the upper bounds are tagged, so the first value in e_groups, # which is always 0.000 is ommitted. tag_e_bin = sm.imesh.createTag("E_upper_bounds", len(e_groups) - 1, float) tag_e_bin[sm.imesh.rootSet] = e_groups[1:] # Tagging structured mesh tag_fluxes(filename, meshtal_type, m, spatial_points, e_bins, sm, norm) return sm