def test_convergence(python_method_impl, problem, method, expected_order): pytest.importorskip("scipy") code = method.generate() from leap.implicit import replace_AssignImplicit code = replace_AssignImplicit(code, {"solve": solver_hook}) from pytools.convergence import EOCRecorder eocrec = EOCRecorder() for n in range(2, 7): dt = 2**(-n) y_0 = problem.initial() t_start = problem.t_start t_end = problem.t_end from functools import partial interp = python_method_impl(code, function_map={ "<func>expl_y": problem.nonstiff, "<func>impl_y": problem.stiff, "<func>solver": partial(solver, problem.stiff), }) interp.set_up(t_start=t_start, dt_start=dt, context={"y": y_0}) times = [] values = [] for event in interp.run(t_end=t_end): if isinstance(event, interp.StateComputed): values.append(event.state_component) times.append(event.t) times = np.array(times) values = np.array(values) assert abs(times[-1] - t_end) < 1e-10 times = np.array(times) error = np.linalg.norm(values[-1] - problem.exact(t_end)) eocrec.add_data_point(dt, error) print("------------------------------------------------------") print("expected order %d" % expected_order) print("------------------------------------------------------") print(eocrec.pretty_print()) orderest = eocrec.estimate_order_of_convergence()[0, 1] assert orderest > 0.9 * expected_order
def generate(self, solver_hook): """Return code that implements the implicit Euler method for the single state component supported.""" with CodeBuilder(name="primary") as cb: self._make_primary(cb) code = DAGCode.from_phases_list( [cb.as_execution_phase(next_phase="primary")], initial_phase="primary") from leap.implicit import replace_AssignImplicit return replace_AssignImplicit(code, {self.SOLVER_EXPRESSION_ID: solver_hook})
def demo_rk_implicit(): from dagrt.language import CodeBuilder from pymbolic import var k1, k2, t, dt, y, f = ( var(name) for name in "k1 k2 <t> <dt> <state>y <func>f".split()) gamma = (2 - 2**0.5) / 2 with CodeBuilder("primary") as cb: cb.assign_implicit_1( k1, solve_component=k1, expression=k1 - f(t + gamma * dt, y + dt * gamma * k1), guess=f(t, y)) cb.assign_implicit_1( k2, solve_component=k2, expression=k2 - f(t + dt, y + dt * ((1 - gamma) * k1 + gamma * k2)), # noqa guess=k1) cb(y, y + dt * ((1 - gamma) * k1 + gamma * k2)) cb(t, t + dt) cb.yield_state(y, "y", t, None) from dagrt.language import DAGCode code = DAGCode(phases={ "primary": cb.as_execution_phase(next_phase="primary") }, initial_phase="primary") def solver_hook(solve_expr, unknown, solver_id, guess): from dagrt.expression import match, substitute pieces = match( "k - <func>rhs(time, y + dt * (c0 + c1 * k))", solve_expr, pre_match={"k": unknown}) return substitute("-10 * (dt * c0 + y) / (10 * dt * c1 + 1)", pieces) from leap.implicit import replace_AssignImplicit code = replace_AssignImplicit(code, solver_hook) print(code) from dagrt.codegen import PythonCodeGenerator IRKMethodBuilder = PythonCodeGenerator("IRKMethodBuilder").get_class(code) eocrec = get_convergence_data(IRKMethodBuilder, SimpleDecayProblem()) print(eocrec.pretty_print())
def run(): from leap.rk.imex import KennedyCarpenterIMEXARK4MethodBuilder from dagrt.codegen import PythonCodeGenerator # Construct the method generator. mgen = KennedyCarpenterIMEXARK4MethodBuilder("y", atol=_atol) # Generate the code for the method. code = mgen.generate() from leap.implicit import replace_AssignImplicit code = replace_AssignImplicit(code, {"solve": solver_hook}) IMEXIntegrator = PythonCodeGenerator("IMEXIntegrator").get_class(code) # Set up the problem and run the method. from functools import partial problem = KapsProblem(epsilon=0.001) integrator = IMEXIntegrator( function_map={ "<func>expl_y": problem.nonstiff, "<func>impl_y": problem.stiff, "<func>solver": partial(solver, problem.stiff, problem.jacobian), "<func>j": problem.jacobian }) integrator.set_up(t_start=problem.t_start, dt_start=1.0e-1, context={"y": problem.initial()}) t = None y = None for event in integrator.run(t_end=problem.t_end): if isinstance(event, integrator.StateComputed): t = event.t y = event.state_component print("Error: " + str(np.linalg.norm(y - problem.exact(t))))
def main(): # order = 4 # hist_length = 4 order = 3 hist_length = 3 static_dt = True stepper = MultiRateMultiStepMethodBuilder(order, ( ( 'dt', 'fast', '=', MRHistory(1, "<func>f", ("fast", "slow"), hist_length=hist_length, is_rhs_implicit=True), ), ( 'dt', 'slow', '=', MRHistory(1, "<func>s", ("fast", "slow"), rhs_policy=rhs_policy.late, hist_length=hist_length, is_rhs_implicit=False), ), ), static_dt=static_dt) from dagrt.function_registry import (base_function_registry, register_function, UserType) # This stays unchanged from the existing explicit RK4 RHS. freg = register_function(base_function_registry, "<func>s", ("t", "fast", "slow"), result_names=("result", ), result_kinds=(UserType("slow"), )) freg = freg.register_codegen( "<func>s", "cxx", cxx.CallCode(""" // Purely homogeneous chemistry simulation. for (int i = 0;i < 4;i++){ ${result}[i] = 0.0; } """)) # Here, we need to call a new chemistry RHS that loops through # all the points *under the hood.* freg = register_function(freg, "<func>f", ("t", "fast", "slow"), result_names=("result", ), result_kinds=(UserType("fast"), )) freg = freg.register_codegen( "<func>f", "cxx", cxx.CallCode(""" // PyJac inputs. double jac[(NS+1)*(NS+1)]; double jac_trans[(NS+1)*(NS+1)]; double phi[(NS+1)]; double phi_guess[(NS+1)]; double phi_old[(NS+1)]; double dphi[(NS+1)]; double dphi_old[(NS+1)]; double corr[(NS+1)]; double corr_weights[(NS+1)]; double corr_weighted[(NS+1)]; double reltol = 1e-6; double abstol = 1e-12; /* For Lapack */ int ipiv[NS+1], info; int nrhs = 1; int nsp_l = NS+1; // Dummy time for pyJac. double tout = 0; // Work array for PyJac. double* rwk_dphi = (double*)malloc(245 * sizeof(double)); memset(rwk_dphi, 0, 245 * sizeof(double)); double massFractions[NS]; double mw[NS]; // FIXME: Assumes the last (inert) species is nitrogen. mw[NS-1] = 2*14.00674; for (int i = 0; i < NS-1; ++i ){ mw[i] = mw[NS-1]*mw_factor[i]; } double rho = 0.2072648773462248; double vol = 1.0 / rho; double tol = 1e-10; double mass_sum = 0.0; for (int i = 2; i <= NS; ++i ){ massFractions[i-2] = ${fast}[i]*mw[i-2]; mass_sum += massFractions[i-2]; } //massFractions[8] = 1 - mass_sum; massFractions[8] = 0.0; // PyJac converted input state. //phi[0] = ${fast}[0]; //phi[1] = ${fast}[1]; // Update temperature and pressure using Cantera? Cantera::IdealGasMix * GasMixture; GasMixture = new Cantera::IdealGasMix("Mechanisms/sanDiego.xml"); double int_energy = 788261.179011143; GasMixture->setMassFractions(massFractions); GasMixture->setState_UV(int_energy, vol, tol); phi[0] = GasMixture->temperature(); phi[1] = GasMixture->pressure(); delete GasMixture; for (int j=2;j <= NS; j++) { phi[j] = massFractions[j-2]/mw[j-2]; } // PyJac call for source term species_rates (&tout, &vol, phi, dphi, rwk_dphi); for (int j=0;j <= NS; j++) { ${result}[j] = dphi[j]; } """)) # The tricky part - this is going to require a # nonlinear solve of some kind. freg = register_function(freg, "<func>solver", ("fast", "slow", "coeff", "t"), result_names=("result", ), result_kinds=(UserType("fast"), )) freg = freg.register_codegen( "<func>solver", "cxx", cxx.CallCode(""" // PyJac inputs. double jac[(NS+1)*(NS+1)]; double jac_trans[(NS+1)*(NS+1)]; double jac_sub[(NS-1)*(NS-1)]; double phi[(NS+1)]; double phi_guess[(NS+1)]; double phi_old[(NS+1)]; double dphi[(NS+1)]; double dphi_sub[(NS-1)]; double dphi_old[(NS+1)]; double corr[(NS+1)]; double corr_weights[(NS+1)]; double corr_weighted[(NS+1)]; double reltol = 1e-6; double abstol = 1e-12; /* For Lapack */ int ipiv[NS-1], info; int nrhs = 1; int nsp_l = NS-1; int nsp_l_full = NS+1; // Dummy time for pyJac. double tout = 0; // Work array for PyJac. double* rwk_dphi = (double*)malloc(245 * sizeof(double)); memset(rwk_dphi, 0, 245 * sizeof(double)); double* rwk_jac = (double*)malloc(245 * sizeof(double)); memset(rwk_jac, 0, 245 * sizeof(double)); /* 1D point-sized identity matrix */ double ident[(NS-1)*(NS-1)]; for (int i=0; i < (NS-1)*(NS-1); i++) { if (i % (NS) == 0) { ident[i] = 1; } else { ident[i] = 0; } } // THIS IS WHERE ALL OF THE // NEWTON/PYJAC STUFF GOES. // Get chemical state at this point. double massFractions[NS]; double mw[NS]; // FIXME: Assumes the last (inert) species is nitrogen. mw[NS-1] = 2*14.00674; for (int i = 0; i < NS-1; ++i ){ mw[i] = mw[NS-1]*mw_factor[i]; } double rho = 0.2072648773462248; double mass_sum = 0.0; for (int i = 2; i <= NS; ++i ){ massFractions[i-2] = ${fast}[i]*mw[i-2]; mass_sum += massFractions[i-2]; } //massFractions[8] = 1 - mass_sum; massFractions[8] = 0.0; double vol = 1.0 / 0.2072648773462248; double tol = 1e-10; // PyJac converted input state. //phi[0] = ${fast}[0]; //phi[1] = ${fast}[1]; Cantera::IdealGasMix * GasMixture; GasMixture = new Cantera::IdealGasMix("Mechanisms/sanDiego.xml"); double int_energy = 788261.179011143; GasMixture->setMassFractions(massFractions); GasMixture->setState_UV(int_energy, vol, tol); phi[0] = GasMixture->temperature(); phi[1] = GasMixture->pressure(); //delete GasMixture; for (int j=2;j <= NS; j++) { phi[j] = massFractions[j-2]/mw[j-2]; } for (int j=0;j <= NS; j++) { phi_old[j] = phi[j]; } for (int j=0;j <= NS; j++) { phi_guess[j] = phi[j]; } // Newton loop within this point (for now). double corr_norm = 1.0; // PyJac call for source term species_rates (&tout, &vol, phi, dphi_old, rwk_dphi); //while (abs(corr_norm) >= reltol) { while (abs(corr_norm) >= 1e-8) { species_rates (&tout, &vol, phi_guess, dphi, rwk_dphi); // Get Jacobian at this point. jacobian (&tout, &vol, phi_guess, jac, rwk_jac); for (int j=0;j <= NS; j++) { // IMEX AM dphi[j] = phi_guess[j] - phi_old[j] - ${coeff} * dphi[j]; } // Take subset of RHS vector. for (int j=2;j <= NS; j++) { dphi_sub[j-2] = dphi[j]; } // Transpose the Jacobian. // PYJAC outputs Fortran-ordering for (int i = 0; i < (NS+1); ++i ) { for (int j = 0; j < (NS+1); ++j ) { // Index in the original matrix. int index1 = i*(NS+1)+j; // Index in the transpose matrix. int index2 = j*(NS+1)+i; jac_trans[index2] = jac[index1]; } } for (int i=0; i<(NS+1)*(NS+1); i++) { jac[i] = jac_trans[i]; } // Take subset of Jacobian for algebraic changes. for (int i = 0; i < (NS-1); ++i ) { for (int j = 0; j < (NS-1); ++j ) { jac_sub[i*(NS-1)+j] = jac[(i+2)*(NS+1)+2+j]; } } // Make the algebraic changes, // using PyJac routines as needed. // Get internal energies. double int_energies[NS]; eval_u(phi_guess, int_energies); // Get CVs. double cvs[NS]; eval_cv(phi_guess, cvs); // Get total CV. double mass_sum = 0.0; for (int i = 2; i <= NS; ++i ){ massFractions[i-2] = phi_guess[i]*mw[i-2]; mass_sum += massFractions[i-2]; } //massFractions[8] = 1 - mass_sum; massFractions[8] = 0.0; double cv_total = 0.0; for (int i = 0; i <= NS-1; ++i ){ cv_total += cvs[i]*(massFractions[i]/mw[i]); } // Modify the Jacobian for the algebraic constraint. for (int i = 0; i < (NS-1); ++i ) { for (int j = 0; j < (NS-1); ++j ) { jac_sub[i*(NS-1)+j] -= jac[j+2]*int_energies[i]/cv_total; } } /* Subtract from identity to get jac for Newton */ for (int j=0;j < (NS-1)*(NS-1); j++) { jac_sub[j] = ident[j] - ${coeff} * jac_sub[j]; // IMEX AM jac_sub[j] = -jac_sub[j]; } // Do the inversion with the assistance of Lapack. dgesv_(&nsp_l, &nrhs, jac_sub, &nsp_l, ipiv, dphi_sub, &nsp_l, &info); // Add the correction to the buffer. mass_sum = 0.0; for (int i = 2; i <= NS; ++i ){ massFractions[i-2] = (phi_guess[i] + dphi_sub[i-2])*mw[i-2]; mass_sum += massFractions[i-2]; } //massFractions[8] = 1 - mass_sum; massFractions[8] = 0.0; GasMixture->setMassFractions(massFractions); GasMixture->setState_UV(int_energy, vol, tol); corr[0] = GasMixture->temperature() - phi_guess[0]; corr[1] = GasMixture->pressure() - phi_guess[1]; for (int j=2;j <= NS; j++) { corr[j] = dphi_sub[j-2]; } for (int j=0;j <= NS; j++) { corr_weights[j] = 1.0 / (reltol * abs(phi_guess[j]) + abstol); corr_weighted[j] = corr[j]*corr_weights[j]; } //corr_norm = dnrm2_(&nsp_l, corr, &nrhs); corr_norm = dnrm2_(&nsp_l_full, corr_weighted, &nrhs); //std::cout << "Correction norm: " << corr_norm << std::endl; for (int j=0;j <= NS; j++) { phi_guess[j] = phi_guess[j] + corr[j]; } } // Now outside the Newton loop (presumably having converged), // we update the RHS using the actual state. species_rates (&tout, &vol, phi_guess, dphi, rwk_dphi); for (int j=0;j <= NS; j++) { ${result}[j] = dphi[j]; } delete GasMixture; """)) code = stepper.generate() # Implicit solve thingy from leap.implicit import replace_AssignImplicit code = replace_AssignImplicit(code, {"solve": am_solver_hook}) # print(code) codegen = cxx.CodeGenerator( 'LeapIMEX', user_type_map={ "fast": cxx.ArrayType( (10, ), cxx.BuiltinType('double'), ), "slow": cxx.ArrayType( (4, ), cxx.BuiltinType('double'), ), }, function_registry=freg, emit_instrumentation=True, timing_function="clock", header_preamble="\n#include \"mechanism.hpp\"\n#include " + "\"species_rates.hpp\"\n#include " + "\"jacobian.hpp\"\n#include \"memcpy_2d.hpp" + "\"\n#include \"lapack_kernels.H\"\n#include " + "\"cantera/IdealGasMix.h\"\n#include " + "\"cantera/thermo.h\"\n#include " + "\"cantera/kinetics.h\"\n#include " + "\"cantera/transport.h\"") import sys # Write out Leap/Dagrt code: with open(sys.argv[1], "a") as outf: code_str = codegen(code) print(code_str, file=outf)
def test_adaptive(python_method_impl, problem, method): pytest.importorskip("scipy") t_start = problem.t_start t_end = problem.t_end dt = 1.0e-1 tols = [10.0**(-j) for j in range(1, 5)] from pytools.convergence import EOCRecorder eocrec = EOCRecorder() # Test that tightening the tolerance will decrease the overall error. for atol in tols: generator = method("y", atol=atol) code = generator.generate() #sgen = ScipySolverGenerator(*generator.implicit_expression()) from leap.implicit import replace_AssignImplicit code = replace_AssignImplicit(code, {"solve": solver_hook}) from functools import partial interp = python_method_impl(code, function_map={ "<func>expl_y": problem.nonstiff, "<func>impl_y": problem.stiff, "<func>solver": partial(solver, problem.stiff) }) interp.set_up(t_start=t_start, dt_start=dt, context={"y": problem.initial()}) times = [] values = [] new_times = [] new_values = [] for event in interp.run(t_end=t_end): clear_flag = False if isinstance(event, interp.StateComputed): assert event.component_id == "y" new_values.append(event.state_component) new_times.append(event.t) elif isinstance(event, interp.StepCompleted): values.extend(new_values) times.extend(new_times) clear_flag = True elif isinstance(event, interp.StepFailed): clear_flag = True if clear_flag: del new_times[:] del new_values[:] times = np.array(times) values = np.array(values) exact = problem.exact(times[-1]) error = np.linalg.norm(values[-1] - exact) eocrec.add_data_point(atol, error) print("Error vs. tolerance") print(eocrec.pretty_print()) order = eocrec.estimate_order_of_convergence()[0, 1] assert order > 0.9