|
| 1 | +from math import ceil |
| 2 | +import os |
| 3 | +import os.path |
| 4 | +import yt |
| 5 | +import libconf # for help reading the parameter file (dependency of yt) |
| 6 | + |
| 7 | +from answer_testing import EnzoETest |
| 8 | + |
| 9 | +_base_file = os.path.basename(__file__) |
| 10 | + |
| 11 | +def _sched_var_type(name): |
| 12 | + d = {"cycle" : int, "time" : float, |
| 13 | + "seconds" : float, "minutes" : float, "hours" : float} |
| 14 | + try: |
| 15 | + return d[name] |
| 16 | + except KeyError: |
| 17 | + raise ValueError( |
| 18 | + f"{name!r} is not a known scheduling variable. Those include " |
| 19 | + f"{list(name_type_pairs.keys())!r}." |
| 20 | + ) |
| 21 | + |
| 22 | +def _all_schedule_var_vals(sched_params, var_name, start_val, last_val): |
| 23 | + """ |
| 24 | + Returns a sequence holding all values of a schedule variable in the |
| 25 | + inclusive interval [start_val, last_val] |
| 26 | + """ |
| 27 | + |
| 28 | + if sched_params.get('var', None) != var_name: |
| 29 | + raise ValueError("This function is expecting for there to be a 'var' " |
| 30 | + f"parameter in sched_params with value {var_name!r}") |
| 31 | + |
| 32 | + var_type = _sched_var_type(var_name) |
| 33 | + def coerce_arg_(arg_name, arg): |
| 34 | + out = var_type(arg) |
| 35 | + if out != arg: |
| 36 | + raise ValueError( |
| 37 | + f"the argument {arg_name!r} has a value {arg} that can't be " |
| 38 | + f"losslessly coerced to the {var_type.__name__} dtype " |
| 39 | + f"associated with the {var_name!r} scheduling variable." |
| 40 | + ) |
| 41 | + return out |
| 42 | + |
| 43 | + start_val = coerce_arg_("start_val", start_val) |
| 44 | + last_val = coerce_arg_("last_val", last_val) |
| 45 | + assert last_val > start_val |
| 46 | + |
| 47 | + if ('list' in sched_params) and (len(sched_params) == 2): |
| 48 | + return sorted([e for e in sched_params['list'] |
| 49 | + if ((e >= start_val) and (e <= last_val))]) |
| 50 | + elif all(k in ('var', 'start', 'stop', 'step') for k in sched_params): |
| 51 | + # parse parameters |
| 52 | + sched_start = sched_params.get('start', 0) |
| 53 | + sched_step = sched_params.get('step', 1) |
| 54 | + if (int(sched_start)!=sched_start) or (int(sched_step)!=sched_step): |
| 55 | + # we can run into problems with exact floating point values if we |
| 56 | + # don't do this... |
| 57 | + raise RuntimeError("This function only handles schedule:start and " |
| 58 | + "schedule:step values that are integers") |
| 59 | + |
| 60 | + # the schedule:stop parameter is inclusive, so fall back to last_val |
| 61 | + # (also inclusive) when schedule:stop isn't specified |
| 62 | + sched_stop = sched_params.get('stop', last_val) |
| 63 | + |
| 64 | + # compute vals |
| 65 | + if sched_stop < start_val: |
| 66 | + return [] |
| 67 | + cur_val = sched_start |
| 68 | + # if applicable, advance cur_val until, cur_val >= start_val |
| 69 | + while cur_val <= start_val: |
| 70 | + cur_val += sched_step |
| 71 | + out = [] |
| 72 | + while cur_val <= min(last_val, sched_stop): |
| 73 | + out.append(cur_val) |
| 74 | + cur_val += sched_step |
| 75 | + return out |
| 76 | + else: |
| 77 | + raise ValueError(f"{sched_params!r} doesn't describe a valid schedule") |
| 78 | + |
| 79 | +def scheduled_output_dirs(parameters, var_name, start_val, last_val): |
| 80 | + """ |
| 81 | + Constructs a generator that iterates over the expected output directories |
| 82 | + produced for a given configuration of a given output-method that uses a |
| 83 | + schedule parameterized by the scheduling-variable, var_name. |
| 84 | +
|
| 85 | + Specifically, this iterates over directories when the scheduling-variable |
| 86 | + has values in the inclusive interval [start_val, last_val]. This yields a |
| 87 | + 2-tuple where the first element gives the directory name and the second |
| 88 | + gives the associated value of the scheduling-variable. |
| 89 | +
|
| 90 | + Notes |
| 91 | + ----- |
| 92 | + When the scheduling-variable is associated with a floating point variable |
| 93 | + (e.g. ``var_name == "time"``), one could imagine that this generator might |
| 94 | + slightly mis-predict the exact values of the scheduling-variable when the |
| 95 | + output-directories are produced. For that reason, this generator requires |
| 96 | + that the schedule:start and schedule:stop parameters are integers, when |
| 97 | + they are specified. |
| 98 | +
|
| 99 | + This is a fairly generic operation that could be helpful in a lot of |
| 100 | + scenarios. |
| 101 | + """ |
| 102 | + |
| 103 | + # construct a function to format the path_names parameter for a given value |
| 104 | + # of the scheduling parameter |
| 105 | + if ('path_name' not in parameters): |
| 106 | + raise ValueError("output-method is missing the path_name parameter") |
| 107 | + elif not isinstance(parameters['path_name'], (list,tuple,str)): |
| 108 | + raise ValueError("path_name parameter has unexpected type") |
| 109 | + elif isinstance(parameters['path_name'], str): |
| 110 | + def format_path(val): return parameters['path_name'] |
| 111 | + elif len(parameters['path_name']) == 1: |
| 112 | + def format_path(val): return parameters['path_name'][0] |
| 113 | + elif any(fmt_v != var_name for fmt_v in parameters['path_name'][1:]): |
| 114 | + # we could relax this requirement if we knew the exact mapping between |
| 115 | + # the various scheduling variables (i.e. from parsing the log) |
| 116 | + raise ValueError("{var_name!r} is expected to be the only variable " |
| 117 | + "used for formatting the path_name parameter") |
| 118 | + else: |
| 119 | + def format_path(val): |
| 120 | + n_vars = len(parameters['path_name']) - 1 |
| 121 | + fmt_arg = tuple(val for i in range(n_vars)) |
| 122 | + return parameters['path_name'][0] % fmt_arg |
| 123 | + |
| 124 | + if (('schedule' not in parameters) or |
| 125 | + (parameters['schedule']['var'] != var_name)): |
| 126 | + raise ValueError("output-method must have an associated schedule that " |
| 127 | + f"is parameterized by the {var_name!r} variable.") |
| 128 | + vals = _all_schedule_var_vals(parameters['schedule'], var_name, |
| 129 | + start_val, last_val) |
| 130 | + |
| 131 | + for val in vals: |
| 132 | + yield format_path(val), val |
| 133 | + |
| 134 | + |
| 135 | +class TestMethodScheduleTime(EnzoETest): |
| 136 | + """ |
| 137 | + The purpose of this test is to ensure that the Enzo-E executes methods at |
| 138 | + the exact simulation times at when they are scheduled. |
| 139 | +
|
| 140 | + In short, we setup a minimal simulation and use the "null" Method to |
| 141 | + enforce an upper-bound on the timestep during each cycle. We also setup 2 |
| 142 | + occurences of the output method with schedules based on simulation-times, |
| 143 | + that include execution times that shouldn't coincide occur when just |
| 144 | + considering the maximum timestep. This python code checks when the outputs |
| 145 | + are found. |
| 146 | +
|
| 147 | + Unlike many other tests - this test does not require comparison against |
| 148 | + previous versions of the code. There is an unambiguous "right answer" that |
| 149 | + shouldn't change... |
| 150 | + """ |
| 151 | + |
| 152 | + parameter_file = "Schedule/Method_Scheduling_time.in" |
| 153 | + max_runtime = 5 |
| 154 | + ncpus = 1 |
| 155 | + |
| 156 | + def test_method_schedule_time(self): |
| 157 | + # parse the parameters (after they were translated to libconfig format) |
| 158 | + with open('parameters.libconfig', 'r') as f: |
| 159 | + config = libconf.load(f) |
| 160 | + |
| 161 | + start_t = 0.0 |
| 162 | + stop_t = config["Stopping"]["time"] |
| 163 | + |
| 164 | + itr = scheduled_output_dirs(config["Method"]["output_step"], |
| 165 | + "time", start_t, stop_t) |
| 166 | + for dirname, t in itr: |
| 167 | + if t != stop_t: |
| 168 | + assert os.path.isdir(dirname) |
| 169 | + |
| 170 | + itr = scheduled_output_dirs(config["Method"]["output_list"], |
| 171 | + "time", start_t, stop_t) |
| 172 | + for dirname, t in itr: |
| 173 | + if t != stop_t: |
| 174 | + assert os.path.isdir(dirname) |
0 commit comments