def get_solver(self, func): """ Returns the solver method from odespy package. Args: func: function function with ODE system. Returns: an instance of odeSolver """ if self.solverMethod is solverMethod.LSODA: solver = odespy.Lsoda(func) elif self.solverMethod is solverMethod.LSODAR: solver = odespy.Lsodar(func) elif self.solverMethod is solverMethod.LSODE: solver = odespy.Lsode(func) elif self.solverMethod is solverMethod.HEUN: solver = odespy.Heun(func) elif self.solverMethod is solverMethod.EULER: solver = odespy.Euler(func) elif self.solverMethod is solverMethod.RK4: solver = odespy.RK4(func) elif self.solverMethod is solverMethod.DORMAN_PRINCE: solver = odespy.DormandPrince(func) elif self.solverMethod is solverMethod.RKFehlberg: solver = odespy.RKFehlberg(func) elif self.solverMethod is solverMethod.Dopri5: solver = odespy.Dopri5(func) elif self.solverMethod is solverMethod.Dop853: solver = odespy.Dop853(func) elif self.solverMethod is solverMethod.Vode: solver = odespy.Vode(func) elif self.solverMethod is solverMethod.AdamsBashforth2: solver = odespy.AdamsBashforth2(func, method='bdf') elif self.solverMethod is solverMethod.Radau5: solver = odespy.Radau5(func) elif self.solverMethod is solverMethod.AdamsBashMoulton2: solver = odespy.AdamsBashMoulton2(func) # update default parameters solver.nsteps = SolverConfigurations.N_STEPS solver.atol = SolverConfigurations.ABSOLUTE_TOL solver.rtol = SolverConfigurations.RELATIVE_TOL return solver
path = [pos] dt = 5e-13 total_time = 0 #while (x[0]<=pos[0]<=x[-1] and y[0]<=pos[1]<=y[-1] and z[0]<=pos[2]<=z[-1] and total_time<1e12): start_time = time() #while (x[0]<=pos[0]<=x[-1] and y[0]<=pos[1]<=y[-1] and z[0]<=pos[2]<=z[-1] and total_time<dt*1e5): # pos,v = update_kinematics(pos,v,dt) # path.append(pos) # total_time+=dt #print total_time t_steps = np.linspace(0, 4e-8, 1e5) init_state = [ init_pos[0], init_pos[1], init_pos[2], init_v[0], init_v[1], init_v[2] ] #X = odeint(lorentz_force,init_state,t) solver = odespy.Dop853(lorentz_force) solver.set_initial_condition(init_state) X, t = solver.solve(t_steps) end_time = time() v_final = np.asarray([X[-1, 3], X[-1, 4], X[-1, 5]]) print(("Elapsed time was %g seconds" % (end_time - start_time))) #ax.plot(path_z,path_x,zs=path_y,linewidth=2) #path = np.asarray(path) #ax.plot(path[:,2],path[:,0],zs=path[:,1],linewidth=2) ax.plot(X[:, 2], X[:, 0], zs=X[:, 1], linewidth=2) ax.set_title('Path of electron through magnetic field') # these are matplotlib.patch.Patch properties textstr = 'init pos={0}\ninit mom={1} (MeV)\nB={2}'.format( init_pos, init_mom, 'ideal DS field map')
atol = 1E-7 rtol = 1E-5 adams_or_bdf = 'bdf' import odespy solvers = [ odespy.AdamsBashMoulton2(problem), odespy.AdamsBashMoulton3(problem), odespy.AdamsBashforth2(problem), odespy.AdamsBashforth3(problem), odespy.AdamsBashforth4(problem), odespy.AdaptiveResidual(problem, solver='Euler'), odespy.Backward2Step(problem), odespy.BackwardEuler(problem), odespy.Dop853(problem, rtol=rtol, atol=atol), odespy.Dopri5(problem, rtol=rtol, atol=atol), odespy.Euler(problem), odespy.Heun(problem), odespy.Leapfrog(problem), odespy.LeapfrogFiltered(problem), odespy.MidpointImplicit(problem), odespy.MidpointIter(problem, max_iter=10, eps_iter=1E-7), odespy.RK2(problem), odespy.RK3(problem), odespy.RK4(problem), odespy.RKFehlberg(problem, rtol=rtol, atol=atol), odespy.SymPy_odefun(problem), odespy.ThetaRule(problem), odespy.Trapezoidal(problem), odespy.Vode(problem, rtol=rtol, atol=atol, adams_or_bdf=adams_or_bdf),