|
| 1 | +import argparse |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import yt |
| 5 | + |
| 6 | +import yt_idv |
| 7 | + |
| 8 | +# yt reminder: phi is the azimuthal angle (0 to 2pi) |
| 9 | +# theta is the co-latitude, the angle from north (0 to pi) |
| 10 | +# coord ordering here will be r, phi, theta |
| 11 | +bbox_options = { |
| 12 | + "partial": np.array([[0.5, 1.0], [0.0, np.pi / 3], [np.pi / 4, np.pi / 2]]), |
| 13 | + "whole": np.array([[0.0, 1.0], [0.0, 2 * np.pi], [0, np.pi]]), |
| 14 | + "shell": np.array([[0.7, 1.0], [0.0, 2 * np.pi], [0, np.pi]]), |
| 15 | + "north_hemi": np.array([[0.1, 1.0], [0.0, 2 * np.pi], [0, 0.5 * np.pi]]), |
| 16 | + "north_shell": np.array([[0.8, 1.0], [0.0, 2 * np.pi], [0, 0.5 * np.pi]]), |
| 17 | + "south_hemi": np.array([[0.1, 1.0], [0.0, 2 * np.pi], [0.5 * np.pi, np.pi]]), |
| 18 | + "ew_hemi": np.array([[0.1, 1.0], [0.0, np.pi], [0.0, np.pi]]), |
| 19 | + "quadrant_shell": np.array([[0.6, 1.0], [0.0, np.pi / 2], [0.0, np.pi / 2]]), |
| 20 | +} |
| 21 | + |
| 22 | +if __name__ == "__main__": |
| 23 | + |
| 24 | + parser = argparse.ArgumentParser( |
| 25 | + prog="spherical_amr_rendering", |
| 26 | + description="Loads an example spherical dataset in yt_idv", |
| 27 | + ) |
| 28 | + |
| 29 | + msg = f"The geometry subset to generate: one of {list(bbox_options.keys())}" |
| 30 | + parser.add_argument("-d", "--domain", default="partial", help=msg) |
| 31 | + msg = ( |
| 32 | + "The field to plot. Provide a comma-separated string with field_type,field " |
| 33 | + "e.g., to plot the field tuple ('index', 'phi'): \n " |
| 34 | + " $ python amr_spherical_volume_rendering.py -f index,x " |
| 35 | + "\nIf a single string is provided, a field type of gas is assumed." |
| 36 | + ) |
| 37 | + parser.add_argument("-f", "--field", default="index,phi", help=msg) |
| 38 | + parser.add_argument( |
| 39 | + "-np", "--nprocs", default=64, help="number of grids to decompose domain to" |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "-sz", "--size", default=256, help="dimensions, will be (size, size size)" |
| 43 | + ) |
| 44 | + |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + sz = (int(args.size),) * 3 |
| 48 | + fake_data = {"density": np.random.random(sz)} |
| 49 | + |
| 50 | + field = str(args.field).split(",") |
| 51 | + if len(field) == 1: |
| 52 | + field = ("gas", str(field).strip()) |
| 53 | + elif len(field) == 2: |
| 54 | + field = (field[0].strip(), field[1].strip()) |
| 55 | + else: |
| 56 | + raise RuntimeError( |
| 57 | + "Unexpected field formatting. Provide a single string" |
| 58 | + " to provide just a field (will assume field type " |
| 59 | + " of 'gas', or a comma separated string to provide a " |
| 60 | + "field type and a field" |
| 61 | + ) |
| 62 | + |
| 63 | + if args.domain not in bbox_options: |
| 64 | + raise RuntimeError( |
| 65 | + f"domain must be one of {list(bbox_options.keys())}, found {args.domain}" |
| 66 | + ) |
| 67 | + bbox = bbox_options[args.domain] |
| 68 | + |
| 69 | + nprocs = int(args.nprocs) |
| 70 | + |
| 71 | + ds = yt.load_uniform_grid( |
| 72 | + fake_data, |
| 73 | + sz, |
| 74 | + bbox=bbox, |
| 75 | + nprocs=nprocs, |
| 76 | + geometry="spherical", |
| 77 | + axis_order=("r", "phi", "theta"), |
| 78 | + length_unit=1, |
| 79 | + ) |
| 80 | + |
| 81 | + phi_c = ds.quan(ds.domain_center[ds.coordinates.axis_id["phi"]].d, "") |
| 82 | + theta_c = ds.quan(ds.domain_center[ds.coordinates.axis_id["theta"]].d, "") |
| 83 | + rmax = ds.domain_right_edge[ds.coordinates.axis_id["r"]] |
| 84 | + phi_f = ds.quan(15.0 * np.pi / 180.0, "") |
| 85 | + theta_f = ds.quan(15.0 * np.pi / 180.0, "") |
| 86 | + min_val = ds.quan(0.1, "") |
| 87 | + |
| 88 | + def _tube(field, data): |
| 89 | + phi = data["index", "phi"] |
| 90 | + theta = data["index", "theta"] |
| 91 | + tube = (1 - min_val) * np.exp(-(((theta - theta_c) / theta_f) ** 2)) |
| 92 | + tube = tube * np.exp(-(((phi - phi_c) / phi_f) ** 2)) |
| 93 | + return tube + min_val |
| 94 | + |
| 95 | + ds.add_field( |
| 96 | + name=("stream", "tube"), |
| 97 | + function=_tube, |
| 98 | + sampling_type="local", |
| 99 | + ) |
| 100 | + |
| 101 | + def _r_rev(field, data): |
| 102 | + r = data["index", "r"] |
| 103 | + return rmax - r |
| 104 | + |
| 105 | + ds.add_field( |
| 106 | + name=("stream", "r_rev"), |
| 107 | + function=_r_rev, |
| 108 | + sampling_type="local", |
| 109 | + ) |
| 110 | + |
| 111 | + if field not in ds.field_list + ds.derived_field_list: |
| 112 | + spaces = " " * 8 |
| 113 | + fld_list_str = f"\n{spaces}".join(str(fld) for fld in ds.field_list) |
| 114 | + drv_fld_list_str = f"\n{spaces}".join(str(fld) for fld in ds.derived_field_list) |
| 115 | + raise RuntimeError( |
| 116 | + f"field {field} not in field_list or derived_field_list:\n" |
| 117 | + f"\n ds.field_list:\n{spaces}{fld_list_str}" |
| 118 | + f"\n ds.derived_field_list:\n{spaces}{drv_fld_list_str}" |
| 119 | + ) |
| 120 | + |
| 121 | + rc = yt_idv.render_context(height=800, width=800, gui=True) |
| 122 | + sg = rc.add_scene(ds, field, no_ghost=True) |
| 123 | + rc.scene.components[0].sample_factor = 5.0 |
| 124 | + rc.scene.components[0].cmap_log = False |
| 125 | + rc.scene.components[0]._reset_cmap_bounds() |
| 126 | + |
| 127 | + rc.run() |
0 commit comments