#NEWLINE# def amr_grid_to_yt_stream( levels , dust_id = 0 ) : #NEWLINE# #INDENT# if ( ( len ( levels ) == 0 ) or ( len ( levels [ 0 ] . grids ) == 0 ) ) : #NEWLINE# #INDENT# raise Exception ( 'Need at least one level with one grid to convert to a yt object' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( levels ) == 1 ) : #NEWLINE# #INDENT# refine = 2 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dx = [ ] #NEWLINE# dy = [ ] #NEWLINE# dz = [ ] #NEWLINE# for ( ilevel , level ) in enumerate ( levels ) : #NEWLINE# #INDENT# for ( igrid , grid ) in enumerate ( level . grids ) : #NEWLINE# #INDENT# gdx = ( ( grid . xmax - grid . xmin ) / float ( grid . nx ) ) #NEWLINE# gdy = ( ( grid . ymax - grid . ymin ) / float ( grid . ny ) ) #NEWLINE# gdz = ( ( grid . zmax - grid . zmin ) / float ( grid . nz ) ) #NEWLINE# if ( igrid == 0 ) : #NEWLINE# #INDENT# dx . append ( gdx ) #NEWLINE# dy . append ( gdy ) #NEWLINE# dz . append ( gdz ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not almost_equal ( dx [ ( - 1 ) ] , gdx ) ) : #NEWLINE# #INDENT# raise Exception ( ( 'dx scale differs between grids in level %i (expected %g and got %g)' % ( ilevel , dx [ ( - 1 ) ] , gdx ) ) ) #NEWLINE# #UNINDENT# if ( not almost_equal ( dy [ ( - 1 ) ] , gdy ) ) : #NEWLINE# #INDENT# raise Exception ( ( 'dy scale differs between grids in level %i (expected %g and got %g)' % ( ilevel , dy [ ( - 1 ) ] , gdy ) ) ) #NEWLINE# #UNINDENT# if ( not almost_equal ( dz [ ( - 1 ) ] , gdz ) ) : #NEWLINE# #INDENT# raise Exception ( ( 'dz scale differs between grids in level %i (expected %g and got %g)' % ( ilevel , dz [ ( - 1 ) ] , gdz ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# dx = np . array ( dx ) #NEWLINE# dy = np . array ( dy ) #NEWLINE# dz = np . array ( dz ) #NEWLINE# refine_x = ( dx [ : ( - 1 ) ] / dx [ 1 : ] ) #NEWLINE# refine_y = ( dy [ : ( - 1 ) ] / dy [ 1 : ] ) #NEWLINE# refine_z = ( dz [ : ( - 1 ) ] / dz [ 1 : ] ) #NEWLINE# for i in range ( ( len ( levels ) - 1 ) ) : #NEWLINE# #INDENT# if ( abs ( ( refine_x [ i ] - round ( refine_x [ i ] ) ) ) > 1e-05 ) : #NEWLINE# #INDENT# raise Exception ( ( 'refinement ratio is not an integer (%g)' % refine_x [ i ] ) ) #NEWLINE# #UNINDENT# if ( abs ( ( refine_y [ i ] - round ( refine_y [ i ] ) ) ) > 1e-05 ) : #NEWLINE# #INDENT# raise Exception ( ( 'refinement ratio is not an integer (%g)' % refine_y [ i ] ) ) #NEWLINE# #UNINDENT# if ( abs ( ( refine_z [ i ] - round ( refine_z [ i ] ) ) ) > 1e-05 ) : #NEWLINE# #INDENT# raise Exception ( ( 'refinement ratio is not an integer (%g)' % refine_z [ i ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# refine_x = np . round ( refine_x ) . astype ( int ) #NEWLINE# refine_y = np . round ( refine_y ) . astype ( int ) #NEWLINE# refine_z = np . round ( refine_z ) . astype ( int ) #NEWLINE# if ( not np . all ( ( np . hstack ( [ refine_x , refine_y , refine_z ] ) == refine_x [ 0 ] ) ) ) : #NEWLINE# #INDENT# raise Exception ( ( 'refinement ratio changes between levels and/or directions (x = %s, y = %s, z = %s)' % ( str ( refine_x ) , str ( refine_y ) , str ( refine_z ) ) ) ) #NEWLINE# #UNINDENT# refine = int ( refine_x [ 0 ] ) #NEWLINE# #UNINDENT# #UNINDENT# if ( refine != 2 ) : #NEWLINE# #INDENT# raise ValueError ( 'load_amr_grid only supports refine=2' ) #NEWLINE# #UNINDENT# xmin = ymin = zmin = ( + np . inf ) #NEWLINE# xmax = ymax = zmax = ( - np . inf ) #NEWLINE# grid_data = [ ] #NEWLINE# for ( ilevel , level ) in enumerate ( levels ) : #NEWLINE# #INDENT# for grid in level . grids : #NEWLINE# #INDENT# grid_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# grid_dict [ 'left_edge' ] = [ grid . xmin , grid . ymin , grid . zmin ] #NEWLINE# grid_dict [ 'right_edge' ] = [ grid . xmax , grid . ymax , grid . zmax ] #NEWLINE# grid_dict [ 'dimensions' ] = [ grid . nx , grid . ny , grid . nz ] #NEWLINE# grid_dict [ 'level' ] = ilevel #NEWLINE# for field in grid . quantities : #NEWLINE# #INDENT# grid_dict [ ( 'gas' , field ) ] = grid . quantities [ field ] [ dust_id ] . transpose ( ) #NEWLINE# #UNINDENT# grid_data . append ( grid_dict ) #NEWLINE# xmin = min ( xmin , grid . xmin ) #NEWLINE# xmax = max ( xmax , grid . xmax ) #NEWLINE# ymin = min ( ymin , grid . ymin ) #NEWLINE# ymax = max ( ymax , grid . ymax ) #NEWLINE# zmin = min ( zmin , grid . zmin ) #NEWLINE# zmax = max ( zmax , grid . zmax ) #NEWLINE# #UNINDENT# #UNINDENT# grid0 = levels [ 0 ] . grids [ 0 ] #NEWLINE# dx = ( ( grid0 . xmax - grid0 . xmin ) / float ( grid0 . nx ) ) #NEWLINE# nx = int ( round ( ( ( xmax - xmin ) / dx ) ) ) #NEWLINE# dy = ( ( grid0 . ymax - grid0 . ymin ) / float ( grid0 . ny ) ) #NEWLINE# ny = int ( round ( ( ( ymax - ymin ) / dy ) ) ) #NEWLINE# dz = ( ( grid0 . zmax - grid0 . zmin ) / float ( grid0 . nz ) ) #NEWLINE# nz = int ( round ( ( ( zmax - zmin ) / dz ) ) ) #NEWLINE# domain_dimensions = np . array ( [ nx , ny , nz ] ) #NEWLINE# bbox = np . array ( [ [ xmin , xmax ] , [ ymin , ymax ] , [ zmin , zmax ] ] ) #NEWLINE# from yt.mods import load_amr_grids #NEWLINE# spf = load_amr_grids ( grid_data , domain_dimensions , bbox = bbox , geometry = ( 'cartesian' , ( 'x' , 'y' , 'z' ) ) ) #NEWLINE# return spf