def runtests(p, problem, parallel=False, tol=None): from nodepy import rk, ivp, conv import numpy as np from nodepy import loadmethod if tol is None: tol = [10.**(-m) for m in range(2, 11)] # Load methods print 'constructing Ex-Euler' ex = rk.extrap_pair(p) print 'constructing Ex-midpoint' exm = rk.extrap_pair(p / 2, 'midpoint') print 'constructing DC-Euler' dc = rk.DC_pair(p - 1) if p == 6: ork = rk.loadRKM('CMR6') elif p == 8: ork = rk.loadRKM('DP8') elif p == 10: ork = loadmethod.load_rkpair_from_file('rk108curtis.txt') elif p == 12: ork = loadmethod.load_rkpair_from_file('rk129hiroshi.txt') if problem == 'suite': myivp = ivp.detest_suite_minus() else: myivp = ivp.detest(problem) methods = [ork, ex, exm, dc] [work, err] = conv.ptest(methods, myivp, tol, verbosity=1, parallel=parallel) plot_efficiency(methods, err, work) fname = 'eff_' + problem + '_' + str(p) if parallel: fname = fname + '_par' plt.ylabel('Sequential derivative evaluations') plt.savefig(fname + '.pdf')
def run_tolerances(tols, p=8): from nodepy import rk from nodepy import loadmethod err = np.zeros((4, len(tols))) work = np.zeros((4, len(tols))) rkm = rk.loadRKM("BS5").__num__() q_ref, dx, iwork = stegoton(rkm, tol=1.0e-13) rkm = rk.extrap_pair(p / 2, "midpoint").__num__() rke = rk.extrap_pair(p).__num__() rkd = rk.DC_pair(p - 1, grid="cheb").__num__() if p == 6: ork = rk.loadRKM("CMR6") elif p == 8: ork = rk.loadRKM("DP8") elif p == 10: ork = loadmethod.load_rkpair_from_file("rk108curtis.txt") elif p == 12: ork = loadmethod.load_rkpair_from_file("rk129hiroshi.txt") for i, tol in enumerate(tols): q, dx, iwork = stegoton(ork, tol) err[0, i] = np.linalg.norm(q - q_ref, 1) * dx work[0, i] = iwork * len(rkm) q, dx, iwork = stegoton(rke, tol) err[2, i] = np.linalg.norm(q - q_ref, 1) * dx work[2, i] = iwork * len(rke) q, dx, iwork = stegoton(rkm, tol) err[1, i] = np.linalg.norm(q - q_ref, 1) * dx work[1, i] = iwork * len(rkm) q, dx, iwork = stegoton(rkd, tol) err[3, i] = np.linalg.norm(q - q_ref, 1) * dx work[3, i] = iwork * len(rkd) return err, work, ork
def run_tolerances(tols, p=8): from nodepy import rk from nodepy import loadmethod err = np.zeros((4, len(tols))) work = np.zeros((4, len(tols))) rkm = rk.loadRKM('BS5').__num__() q_ref, dx, iwork = stegoton(rkm, tol=1.e-13) rkm = rk.extrap_pair(p / 2, 'midpoint').__num__() rke = rk.extrap_pair(p).__num__() rkd = rk.DC_pair(p - 1, grid='cheb').__num__() if p == 6: ork = rk.loadRKM('CMR6') elif p == 8: ork = rk.loadRKM('DP8') elif p == 10: ork = loadmethod.load_rkpair_from_file('rk108curtis.txt') elif p == 12: ork = loadmethod.load_rkpair_from_file('rk129hiroshi.txt') for i, tol in enumerate(tols): q, dx, iwork = stegoton(ork, tol) err[0, i] = np.linalg.norm(q - q_ref, 1) * dx work[0, i] = iwork * len(rkm) q, dx, iwork = stegoton(rke, tol) err[2, i] = np.linalg.norm(q - q_ref, 1) * dx work[2, i] = iwork * len(rke) q, dx, iwork = stegoton(rkm, tol) err[1, i] = np.linalg.norm(q - q_ref, 1) * dx work[1, i] = iwork * len(rkm) q, dx, iwork = stegoton(rkd, tol) err[3, i] = np.linalg.norm(q - q_ref, 1) * dx work[3, i] = iwork * len(rkd) return err, work, ork
def runtests(p,problem,parallel=False,tol=None): from nodepy import rk, ivp, conv import numpy as np from nodepy import loadmethod if tol is None: tol = [10.**(-m) for m in range(2,11)] # Load methods print 'constructing Ex-Euler' ex = rk.extrap_pair(p); print 'constructing Ex-midpoint' exm = rk.extrap_pair(p/2,'midpoint'); print 'constructing DC-Euler' dc = rk.DC_pair(p-1); if p == 6: ork = rk.loadRKM('CMR6') elif p == 8: ork = rk.loadRKM('DP8') elif p == 10: ork = loadmethod.load_rkpair_from_file('rk108curtis.txt') elif p == 12: ork = loadmethod.load_rkpair_from_file('rk129hiroshi.txt') if problem == 'suite': myivp = ivp.detest_suite_minus() else: myivp = ivp.detest(problem); methods = [ork,ex,exm,dc] [work,err] = conv.ptest(methods,myivp,tol,verbosity=1,parallel=parallel); plot_efficiency(methods, err, work) fname = 'eff_'+problem+'_'+str(p) if parallel: fname = fname + '_par' plt.ylabel('Sequential derivative evaluations') plt.savefig(fname+'.pdf')
def erk(u0, f, t_eval, method="RK44", args=()): rkbt = rk.loadRKM(method).__num__() u = np.zeros((t_eval.size, *u0.shape), dtype=u0.dtype) u[0, ...] = u0 t_step = len(t_eval) - 1 rate = 20 for k, t1 in enumerate(t_eval[1:], start=1): t_percent = np.int(np.around(k / t_step * rate)) print("\r[{}{}]({}/{})".format("#" * t_percent, "+" * (rate - t_percent), k, t_step), end="") t0 = t_eval[k - 1] dt = t1 - t0 u[k, ...] = erk_step(t0, u[k - 1, ...], dt, f, rkbt, args) print() return u
def write_numipedia(): """ Main function to write the whole numipedia site. Loops over all methods in rk.loadRKM('All') and writes a page for each. TODO: - Add parameterized method families - Add more methods """ from nodepy import rk methods = rk.loadRKM('All') for key,method in methods.iteritems(): fname = method.shortname+'.html' print fname #write_page(method, fname=fname) write_method_page(method, fname=fname) write_index_page(methods)
import numpy as np ivpoptim = IVPOPTIM() if testkey == '2OD': #system of differential equations ivpoptim.u0 = u0 #initial parameters ivpoptim.T = 3.0 #Final time of integration ivpoptim.rhs = lambda t, u: (s0[0] * u - s0[1] * u ) #right side of ODE system ivpoptim.dt0 = 0.01 #time step else: raise Exception('Unknown Detest problem') ivpoptim.name = testkey ivpoptim.description = 'Problem ' + testkey + ' of the non-stiff DETEST suite.' return ivpoptim rk44 = rk.loadRKM('RK44') #load runge-kutta ivp = detest('2OD') myivp = ivp #load initial parameters from problem t, u = rk44(myivp) #approximate soluton of problem u4 = np.array([u[1667], u[3334], u[5001]]) t4 = np.array([t[1667], t[3334], t[5001]]) pogr1 = ([(u4[0] - u1), (u4[1] - u2), (u4[2] - u3)] ) #difference between model and fact values when time is 1,2,3 norma1 = round( ln.norm(pogr1[0]), 4 ) #the distance between the points of the model and fact values when time is 1 norma2 = round( ln.norm(pogr1[1]), 4 ) #the distance between the points of the model and fact values when time is 2 norma3 = round( ln.norm(pogr1[2]), 4
if testkey == 'Differential equation for exact solution': #differential equation #with parameter which is (-1), i.e. we think, that a0=-1, # it is necessary to find "experimental" solution which is identically to exact solution ivpoptim.u0 = u0 #initial value ivpoptim.T = 3.0 #Final time of integration ivpoptim.rhs = lambda t, z: -2 * t * (z**2) #right side of ODE ivpoptim.dt0 = 0.01 #time step else: raise Exception('Unknown Detest problem') ivpoptim.name = testkey ivpoptim.description = 'Problem ' + testkey + ' of the non-stiff DETEST suite.' return ivpoptim rk44 = rk.loadRKM('RK44') #load runge-kutta ivp = detest('Differential equation for exact solution') myivp = ivp #load initial parameters from problem t, z = rk44(myivp) #approximate soluton of problem z1 = np.array([z[1667], z[3334], z[5001]]) #"experimental" solutions in moment of time 1,2,3 def detest(testkey): #Description of problem import numpy as np ivpoptim = IVPOPTIM() if testkey == 'Differential equation': #differential equation with parameter a0 which is not (-1) #print('\nDifferential equation -----') ivpoptim.u0 = u0 #initial value ivpoptim.T = 3.0 #Final time of integration ivpoptim.rhs = lambda t, u: a0 * t * (u**2) #right side of ODE
s_start=4 s_end=12 order = range(s_start,s_end+1) I_R = np.zeros((4,len(order))) I_I = np.zeros((4,len(order))) I_R_scaled = np.zeros((4,len(order))) I_I_scaled = np.zeros((4,len(order))) for i,p in enumerate(order): print p if p == 4: ork = rk.loadRKM('Merson43') if p == 6: ork = rk.loadRKM('CMR6') elif p == 8: ork = rk.loadRKM('DP8') elif p == 10: ork = loadmethod.load_rkpair_from_file('rk108curtis.txt') elif p == 12: ork = loadmethod.load_rkpair_from_file('rk129hiroshi.txt') pp,q = ork.stability_function() I_R[0,i] = stability_function.real_stability_interval(pp,q) I_I[0,i] = stability_function.imaginary_stability_interval(pp,q) I_R_scaled[0,i] = I_R[0,i]/len(ork) I_I_scaled[0,i] = I_I[0,i]/len(ork)