def test_single_rate_identical(order=3, hist_length=3): from leap.multistep import AdamsBashforthMethodBuilder from dagrt.exec_numpy import NumpyInterpreter from multirate_test_systems import Full ode = Full() t_start = 0 dt = 0.1 # {{{ single rate single_rate_method = AdamsBashforthMethodBuilder("y", order=order, hist_length=hist_length) single_rate_code = single_rate_method.generate() def single_rate_rhs(t, y): f, s = y return np.array([ ode.f2f_rhs(t, f, s) + ode.s2f_rhs(t, f, s), ode.f2s_rhs(t, f, s) + ode.s2s_rhs(t, f, s), ]) single_rate_interp = NumpyInterpreter( single_rate_code, function_map={"<func>y": single_rate_rhs}) single_rate_interp.set_up( t_start=t_start, dt_start=dt, context={"y": np.array([ ode.soln_0(t_start), ode.soln_1(t_start), ])}) single_rate_values = {} nsteps = 20 for event in single_rate_interp.run(): if isinstance(event, single_rate_interp.StateComputed): single_rate_values[event.t] = event.state_component if len(single_rate_values) == nsteps: break # }}} # {{{ two rate multi_rate_method = MultiRateMultiStepMethodBuilder( order, ( ( 'dt', 'fast', '=', MRHistory( 1, "<func>f", ( "fast", "slow", ), hist_length=hist_length), ), ( 'dt', 'slow', '=', MRHistory(1, "<func>s", ( "fast", "slow", ), rhs_policy=rhs_policy.late, hist_length=hist_length), ), ), hist_consistency_threshold=1e-8, early_hist_consistency_threshold=dt**order) multi_rate_code = multi_rate_method.generate() def rhs_fast(t, fast, slow): return ode.f2f_rhs(t, fast, slow) + ode.s2f_rhs(t, fast, slow) def rhs_slow(t, fast, slow): return ode.f2s_rhs(t, fast, slow) + ode.s2s_rhs(t, fast, slow) multi_rate_interp = NumpyInterpreter(multi_rate_code, function_map={ "<func>f": rhs_fast, "<func>s": rhs_slow }) multi_rate_interp.set_up(t_start=t_start, dt_start=dt, context={ "fast": ode.soln_0(t_start), "slow": ode.soln_1(t_start), }) multi_rate_values = {} for event in multi_rate_interp.run(): if isinstance(event, single_rate_interp.StateComputed): idx = {"fast": 0, "slow": 1}[event.component_id] if event.t not in multi_rate_values: multi_rate_values[event.t] = [None, None] multi_rate_values[event.t][idx] = event.state_component if len(multi_rate_values) > nsteps: break # }}} times = sorted(single_rate_values) single_rate_values = np.array([single_rate_values[t] for t in times]) multi_rate_values = np.array([multi_rate_values[t] for t in times]) print(single_rate_values) print(multi_rate_values) diff = la.norm((single_rate_values - multi_rate_values).reshape(-1)) assert diff < 1e-13
def python_method_impl_interpreter(code, **kwargs): from dagrt.exec_numpy import NumpyInterpreter return NumpyInterpreter(code, **kwargs)
def test_step_matrix(method, show_matrix=True, show_dag=False): component_id = 'y' code = method.generate() if show_dag: from dagrt.language import show_dependency_graph show_dependency_graph(code) from dagrt.exec_numpy import NumpyInterpreter from leap.step_matrix import StepMatrixFinder from pymbolic import var # {{{ build matrix def rhs_sym(t, y): return var("lambda") * y finder = StepMatrixFinder(code, function_map={"<func>" + component_id: rhs_sym}) mat = finder.get_phase_step_matrix("primary") if show_matrix: print('Variables: %s' % finder.variables) from pytools import indices_in_shape for i in indices_in_shape(mat.shape): print(i, mat[i]) # }}} dt = 0.1 lambda_ = -0.4 def rhs(t, y): return lambda_ * y interp = NumpyInterpreter(code, function_map={"<func>" + component_id: rhs}) interp.set_up(t_start=0, dt_start=dt, context={component_id: 15}) assert interp.next_phase == "initial" for event in interp.run_single_step(): pass assert interp.next_phase == "primary" start_values = np.array([interp.context[v] for v in finder.variables]) for event in interp.run_single_step(): pass assert interp.next_phase == "primary" stop_values = np.array([interp.context[v] for v in finder.variables]) from dagrt.expression import EvaluationMapper concrete_mat = EvaluationMapper({ "lambda": lambda_, "<dt>": dt, }, {})(mat) stop_values_from_mat = concrete_mat.dot(start_values) rel_err = ( la.norm(stop_values - stop_values_from_mat) / # noqa: W504 la.norm(stop_values)) assert rel_err < 1e-12
def main(show_dag=False, plot_solution=False): component_id = "y" method = ODE23Method(component_id, use_high_order=True) expected_order = 3 # Use "DEBUG" to trace execution logging.basicConfig(level=logging.INFO) code = method.generate() if show_dag: from dagrt.language import show_dependency_graph show_dependency_graph(code) from dagrt.exec_numpy import NumpyInterpreter def rhs(t, y): u, v = y return np.array([v, -u/t**2], dtype=np.float64) def soln(t): inner = np.sqrt(3)/2*np.log(t) return np.sqrt(t)*( 5*np.sqrt(3)/3*np.sin(inner) + np.cos(inner) ) from pytools.convergence import EOCRecorder eocrec = EOCRecorder() for n in range(4, 7): dt = 2**(-n) t = 1 y = np.array([1, 3], dtype=np.float64) final_t = 10 interp = NumpyInterpreter(code, function_map={"<func>" + component_id: rhs}) interp.set_up(t_start=t, dt_start=dt, context={component_id: y}) times = [] values = [] for event in interp.run(t_end=final_t): if isinstance(event, interp.StateComputed): assert event.component_id == component_id values.append(event.state_component[0]) times.append(event.t) assert abs(times[-1] - final_t) < 1e-10 times = np.array(times) if plot_solution: import matplotlib.pyplot as pt pt.plot(times, values, label="comp") pt.plot(times, soln(times), label="true") pt.show() error = abs(values[-1]-soln(final_t)) eocrec.add_data_point(dt, error) print("------------------------------------------------------") print("%s: expected order %d" % (method, expected_order)) print("------------------------------------------------------") print(eocrec.pretty_print()) orderest = eocrec.estimate_order_of_convergence()[0, 1] assert orderest > expected_order*0.95