Example #1
0
#f = RHS(b=0.4, A_F=1, w_F=2)
f = RHS(b=0.4, A_F=0, w_F=2)
f = RHS(b=0.4, A_F=1, w_F=np.pi)
f = RHS(b=0.4, A_F=20,
        w_F=2 * np.pi)  # qualitatively wrong FE, almost ok BE, smaller T
#f = RHS(b=0.4, A_F=20, w_F=0.5*np.pi)  # cool, FE almost there, BE good

# Define different sets of experiments
solvers_theta = [
    odespy.ForwardEuler(f),
    # Implicit methods must use Newton solver to converge
    odespy.BackwardEuler(f, nonlinear_solver='Newton'),
    odespy.CrankNicolson(f, nonlinear_solver='Newton'),
]

solvers_RK = [odespy.RK2(f), odespy.RK4(f)]
solvers_accurate = [
    odespy.RK4(f),
    odespy.CrankNicolson(f, nonlinear_solver='Newton'),
    odespy.DormandPrince(f, atol=0.001, rtol=0.02)
]
solvers_CN = [odespy.CrankNicolson(f, nonlinear_solver='Newton')]

if __name__ == '__main__':
    timesteps_per_period = 20
    solver_collection = 'theta'
    num_periods = 1
    try:
        # Example: python vib_odespy.py 30 accurate 50
        timesteps_per_period = int(sys.argv[1])
        solver_collection = sys.argv[2]
Example #2
0
    def __call__(self, u, t):
        theta, omega = u
        c = self.c
        return [omega, -c * theta]


problem = Problem(c=1, Theta=pi / 4)

import odespy
solvers = [
    odespy.ThetaRule(problem, theta=0),  # Forward Euler
    odespy.ThetaRule(problem, theta=0.5),  # Midpoint
    odespy.ThetaRule(problem, theta=1),  # Backward Euler
    odespy.RK4(problem),
    odespy.RK2(problem),
    odespy.MidpointIter(problem, max_iter=5, eps_iter=0.01),
    odespy.Leapfrog(problem),
    odespy.LeapfrogFiltered(problem),
]

theta_exact = lambda t: problem.Theta * numpy.cos(sqrt(problem.c) * t)

import sys
try:
    num_periods = int(sys.argv[1])
except IndexError:
    num_periods = 8  # default

T = num_periods * problem.period  # final time
results = {}
Example #3
0
def fblasius(y, x):
    """ODE-system for the Blasius-equation"""
    return [y[1],y[2], -y[0]*y[2]]

import odespy

solvers=[]
solvers.append(odespy.RK4(fblasius))
solvers.append(odespy.RK2(fblasius))
solvers.append(odespy.RK3(fblasius))

from numpy import linspace, exp
xmin = 0
xmax = 5.75

N = 150  # no x-values
xspan = linspace(xmin, xmax, N+1)

smin=0.1
smax=0.8

Ns=30
srange = linspace(smin,smax,Ns)

from matplotlib.pyplot import *
#change some default values to make plots more readable on the screen
LNWDT=5; FNT=25
matplotlib.rcParams['lines.linewidth'] = LNWDT; matplotlib.rcParams['font.size'] = FNT
figure()
legends=[]
linet=['r-',':','.','-.','--']
Example #4
0
import sys


def f(u, t):
    return -a * u


I = 1
a = 2
T = 6
dt = float(sys.argv[1]) if len(sys.argv) >= 2 else 0.75
Nt = int(round(T / dt))
t_mesh = np.linspace(0, Nt * dt, Nt + 1)

solvers = [
    odespy.RK2(f),
    odespy.RK3(f),
    odespy.RK4(f),
    # BackwardEuler must use Newton solver to converge
    # (Picard is default and leads to divergence)
    odespy.BackwardEuler(f, nonlinear_solver='Newton')
]

legends = []
for solver in solvers:
    solver.set_initial_condition(I)
    u, t = solver.solve(t_mesh)

    plt.plot(t, u)
    plt.hold('on')
    legends.append(solver.__class__.__name__)
Example #5
0
    def advance(self):
        u, f, n, t = self.u, self.f, self.n, self.t
        dt = t[n + 1] - t[n]
        dt3 = dt / 3.0
        K1 = dt * f(u[n], t[n])
        K2 = dt * f(u[n] + K1 / 3.0, t[n] + dt3)
        K3 = dt * f(u[n] - K1 / 3 + K2, t[n] + 2 * dt3)
        K4 = dt * f(u[n] + K1 - K2 + K3, t[n] + dt)
        u_new = u[n] + (K1 + 3 * K2 + 3 * K3 + K4) / 8.0
        return u_new


solvers = []
solvers.append(odespy.RK4(f))
solvers.append(odespy.RK2(f))
solvers.append(odespy.RK3(f))
solvers.append(Kutta4(f))

from numpy import linspace, exp
T = 30  # end of simulation
N = 150  # no of time steps
time = linspace(0, T, N + 1)

from matplotlib.pyplot import *
#change some default values to make plots more readable on the screen
LNWDT = 5
FNT = 25
matplotlib.rcParams['lines.linewidth'] = LNWDT
matplotlib.rcParams['font.size'] = FNT
figure()