def check_RungeKutta4_exact_multi(platform, func_src, func_pyf): from runge_kutta import RungeKutta #---------------------------------------------------- # Allocate #---------------------------------------------------- ps = PreSetup() nx, dt, tmax, yinit = ps.nx, ps.dt, ps.tmax, ps.yinit y = ArrayAs(platform, yinit, 'y') rk = RungeKutta(platform, nx, dt) #---------------------------------------------------- # Core function #---------------------------------------------------- lib = platform.source_compile(func_src, func_pyf) func_core = platform.get_function(lib, 'func') func_core.prepare('iDOO', nx) # (t, y, ret) func = lambda t, y, ret: func_core.prepared_call(t,y,ret) comm = lambda k: None #---------------------------------------------------- # RK4 #---------------------------------------------------- t = 0 for tstep in xrange(tmax): rk.update_rk4(t, y, func, comm) t += dt aa_equal(ps.exact_func(t), y.get(), 13)
def main(): print MENU INPUT = int(raw_input("Option: ")) if INPUT == 1: handle_simpsons_task() elif INPUT == 2: handle_gaussian_task() elif INPUT == 3: u0 = float(raw_input("Enter u0: ")) handle_runge_kutta(u0) elif INPUT == 4: u0 = float(raw_input("Enter u0: ")) ranges = float(raw_input("Enter ranges: ")) runge = RungeKutta(FUNCTION2, u0=u0, start=0.0, end=1.0, ranges=ranges) result, h = runge.solve(output=True) print "RESULT: {0} with h={1}".format(result, h)
def __init__(self, config: Config): '''The function arguments represent the following polynomial terms:\n Ly(x) = g(x)y"(x) + p(x)y'(x) + q(x)y(x) = f(x)\n with the following boundary conditions:\n a0*y(a) + a1*y'(a) = A,\n b0*y(b) + b1*y'(b) = B''' a = config['a'] b = config['b'] a0 = config['a0'] a1 = config['a1'] b0 = config['b0'] b1 = config['b1'] A = config['A'] B = config['B'] p = config['p(x)'] q = config['q(x)'] g = config['g(x)'] f = config['f(x)'] if abs(a0) + abs(a1) == 0 or abs(b0) + abs(b1) == 0: raise RuntimeError('The boundary condition does not exist.') else: self.a = a self.b = b self.C = lambda v, dv, u, du: (B - b0 * v - b1 * dv) / (b0 * u + b1 * du) self.Lu = f"0 - ({p})*y' / ({g}) - ({q})*y / ({g})" self.Lv = f"({f}) / ({g}) - ({p})*y' / ({g}) - ({q})*y / ({g})" self.u_func_val = -(a1 / a0) if a0 != 0 else 1 self.u_diff_val = 1 if a0 != 0 else -(a0 / a1) self.v_func_val = A / a0 if a0 != 0 else 0 self.v_diff_val = 0 if a0 != 0 else A / a1 self.u_rk = RungeKutta(self.Lu, a, self.u_func_val, self.u_diff_val) # First Cauchy problem self.v_rk = RungeKutta(self.Lv, a, self.v_func_val, self.v_diff_val) # Second Cauchy problem
def handle_runge_kutta(u0): print "{0: >16}\t{1: <16} {2: <14}".format( "H", "Result", "Runge") ranges = 10 prev_result = 0.0 prev_runge = 0.0 runge = 0.0 while ranges <= TASK3.N_LIMIT: runge_kutta = RungeKutta(FUNCTION2, u0=u0, start=0.0, end=1.0, ranges=ranges) result, h = runge_kutta.solve() if prev_result != 0: runge = runge_error(prev_result, result, TASK3.PRECISION) output = "{0: >16}\t{1: <16} {2: <14} {3: <14}".format( h, result, runge, prev_runge*1.0/runge) else: output = "{0: >16}\t{1: <16}".format( h, result) print output prev_result = result prev_runge = runge ranges *= 2
import os import matplotlib.pyplot as plt from problem import Problem1, Problem2 from runge_kutta import RungeKutta if __name__ == '__main__': out_dir = '../report_tex' # Problem 1 for i, epsilon in enumerate([0.05, 0.1, 0.3, 1.0]): h = 0.05 n_iter = 4000 p1 = Problem1(h, n_iter, epsilon, 0.1, 0.1) rk = RungeKutta(p1.h, p1.n_iter, p1.dxdt, p1.dydt, p1.x0, p1.y0) rk.solve() x, y = rk.get_output() plt.figure(figsize=(18, 6)) plt.subplot(131) plt.plot(x, y) plt.title(u'$x$-$\dot{x}$') plt.xlabel(u'$x$') plt.ylabel(u'$\dot{x}$') plt.subplot(132) plt.plot(x) plt.title(u'$t$-$x$') plt.xlabel(u'$t$') plt.ylabel(u'$\dot{x}$')
velocity[0,gi,gj,0,ie] = 2*pi*(cos(lat)*cos(alpha) + sin(lat)*cos(lon)*sin(alpha)) velocity[1,gi,gj,0,ie] = - 2*pi*sin(lon)*sin(alpha) ''' """ #---------------------------------------------- # spectral element #---------------------------------------------- interact = InteractBetweenElems() inside = InsideElems(N, ngll, nelem, state, interact) # minimum dx, dt max_v = 2*pi dt = cfl*min_dx/max_v tloop = RungeKutta(dt, inside) tloop.allocate(state.psi.shape, state.psi.dtype) #---------------------------------------------- # print the setup information #---------------------------------------------- print '-'*47 print 'N\t\t', N print 'ngll\t\t', ngll print 'nelem\t\t', nelem print 'cfl\t\t', cfl print 'min_dx\t\t', min_dx print 'dt\t\t', dt #tmax = int( numpy.ceil(1/dt) )
class ReductionMethod: '''Solution of the boundary value problem by dividing it into 2 Cauchy problems.''' #def __init__(self, a: float, b: float, a0: float, a1: float, A: float, b0: float, b1: float, B: float, p: str, q: str, f: str, g: str = '1'): def __init__(self, config: Config): '''The function arguments represent the following polynomial terms:\n Ly(x) = g(x)y"(x) + p(x)y'(x) + q(x)y(x) = f(x)\n with the following boundary conditions:\n a0*y(a) + a1*y'(a) = A,\n b0*y(b) + b1*y'(b) = B''' a = config['a'] b = config['b'] a0 = config['a0'] a1 = config['a1'] b0 = config['b0'] b1 = config['b1'] A = config['A'] B = config['B'] p = config['p(x)'] q = config['q(x)'] g = config['g(x)'] f = config['f(x)'] if abs(a0) + abs(a1) == 0 or abs(b0) + abs(b1) == 0: raise RuntimeError('The boundary condition does not exist.') else: self.a = a self.b = b self.C = lambda v, dv, u, du: (B - b0 * v - b1 * dv) / (b0 * u + b1 * du) self.Lu = f"0 - ({p})*y' / ({g}) - ({q})*y / ({g})" self.Lv = f"({f}) / ({g}) - ({p})*y' / ({g}) - ({q})*y / ({g})" self.u_func_val = -(a1 / a0) if a0 != 0 else 1 self.u_diff_val = 1 if a0 != 0 else -(a0 / a1) self.v_func_val = A / a0 if a0 != 0 else 0 self.v_diff_val = 0 if a0 != 0 else A / a1 self.u_rk = RungeKutta(self.Lu, a, self.u_func_val, self.u_diff_val) # First Cauchy problem self.v_rk = RungeKutta(self.Lv, a, self.v_func_val, self.v_diff_val) # Second Cauchy problem def get_values(self, step: float) -> float: '''Calculates the approximate values of the needed funciton and yields them.''' un, vn, dun, dvn = None, None, None, None crutch_u = copy(self.u_rk) crutch_v = copy(self.v_rk) for u_zip, v_zip in zip( crutch_u.get_values( self.a, self.b, step ), # Just a crutch for getting the values of v(b) and u(b). crutch_v.get_values(self.a, self.b, step) ): # Year, I've made the separate loop for getting these values. un, dun = u_zip[1], u_zip[ 2] # Year, I've gone through this loop twice. vn, dvn = v_zip[1], v_zip[ 2] # I just don't know how to get it without this loop, so this will be depricated in future. C = self.C(vn, dvn, un, dun) for u_zip, v_zip in zip(self.u_rk.get_values(self.a, self.b, step), self.v_rk.get_values(self.a, self.b, step)): x, u, _ = u_zip # We don't need the derivate here, so it's ommited. x, v, _ = v_zip yield x, C * u + v
lat1, lon1 = latlons[0,:] lat2, lon2 = latlons[1,:] min_dx = arccos( sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos( abs(lon1-lon2) ) ) # minimum dt max_v = 2*pi dt = cfl*min_dx/max_v #---------------------------------------------- # spectral element #---------------------------------------------- print 'a' se = SpectralElement2D(platform, cubegrid, velocity) print 'b' rk = RungeKutta(platform, ep_size, dt) print 'c' #---------------------------------------------- # print the setup information #---------------------------------------------- print '-'*47 print 'ne\t\t', ne print 'ngq\t\t', ngq print 'cfl\t\t', cfl print 'min_dx\t\t', min_dx print 'dt\t\t', dt #tmax = int( numpy.ceil(1/dt) ) tmax = 1000