qmat.playgrounds.tibo.lorenz

Make use of the generic CoeffSolver of qmat to solve the Lorenz equations using a RK method or Spectral Deferred Correction.

 1"""
 2import numpy as np
 3import matplotlib.pyplot as plt
 4
 5from qmat import genQCoeffs, QDELTA_GENERATORS
 6from qmat.qcoeff.collocation import Collocation
 7from qmat.utils import Timer
 8
 9from qmat.solvers.generic import CoeffSolver
10from qmat.solvers.generic.diffops import Lorenz
11
12tEnd = 10
13nSteps = 1000
14diffOp = Lorenz()
15solver = CoeffSolver(diffOp, tEnd=tEnd, nSteps=nSteps)
16
17nodes, weights, Q = genQCoeffs("RK4")
18with Timer("RK solve", scale=nSteps, descr="tWall/step"):
19    uRK = solver.solve(Q, weights)
20
21coll = Collocation(nNodes=2, nodeType="LEGENDRE", quadType="RADAU-RIGHT")
22gen = QDELTA_GENERATORS["FE"](qGen=coll)
23QDelta = gen.getQDelta()
24with Timer("SDC solve", scale=nSteps, descr="tWall/step"):
25    uSDC = solver.solveSDC(4, coll.Q, coll.weights, QDelta)
26
27plt.figure("Solution")
28times = np.linspace(0, tEnd, nSteps+1)
29for i, v in enumerate(["x", "y", "z"]):
30    p = plt.plot(times, uRK[:, i], label=f"{v} RK")
31    plt.plot(times, uSDC[:, i], "--", c=p[0].get_color(), label=f"{v} SDC")
32plt.legend()
33plt.xlabel("time")
34plt.ylabel("trajectory")
35plt.gcf().set_size_inches(11, 6)
36plt.tight_layout()

Attributes

tEnd

nSteps

diffOp

solver

uRK

coll

gen

QDelta

uSDC

times

p

Module Contents

tEnd = 10
nSteps = 1000
diffOp
solver
uRK = None
coll
gen
QDelta
uSDC = None
times
p