Skip to content

Commit 8c0e7a3

Browse files
Improvements for Jacobian and JAVP
1 parent 09f0c92 commit 8c0e7a3

File tree

7 files changed

+27
-42
lines changed

7 files changed

+27
-42
lines changed

Diff for: src/+otp/+kuramotosivashinsky/+presets/Canonical.m

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
% but the more points are required to do a good discretization. The current
88
% canonical implementation with the size, L, and is used in
99
%
10-
% Kassam, Aly-Khan, and Lloyd N. Trefethen.
11-
% "Fourth-order time-stepping for stiff PDEs."
10+
% Kassam, Aly-Khan, and Lloyd N. Trefethen.
11+
% "Fourth-order time-stepping for stiff PDEs."
1212
% SIAM Journal on Scientific Computing 26, no. 4 (2005): 1214-1233.
1313
%
1414

@@ -20,26 +20,22 @@
2020
p = inputParser;
2121
addParameter(p, 'Size', 128);
2222
addParameter(p, 'L', 32*pi);
23-
2423
parse(p, varargin{:});
2524

2625
s = p.Results;
2726

2827
N = s.Size;
2928
L = s.L;
30-
3129
params.L = L;
3230

33-
h = L/N;
34-
3531
% exclude the left boundary point as it is identical to the
3632
% right boundary point
37-
x = linspace(h, L, N).';
33+
x = linspace(L / N, L, N).';
3834

39-
u0 = cospi(2*x/L).*(1+sinpi(2*x/L));
35+
u0 = cospi(2 * x / L) .* (1 + sinpi(2 * x / L));
4036

4137
u0hat = fft(u0);
42-
38+
4339
tspan = [0, 150];
4440

4541
obj = [email protected](tspan, u0hat, params);

Diff for: src/+otp/+kuramotosivashinsky/+presets/HairerWanner.m

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
p = inputParser;
77
addParameter(p, 'Size', 1024);
8-
addParameter(p, 'L', 80*pi);
9-
8+
addParameter(p, 'L', 80 * pi);
9+
1010
parse(p, varargin{:});
1111

1212
s = p.Results;
@@ -16,21 +16,19 @@
1616

1717
params.L = L;
1818

19-
h = L/N;
20-
2119
% exclude the left boundary point as it is identical to the
2220
% right boundary point
23-
x = linspace(h, L, N).';
21+
x = linspace(L/N, L, N).';
2422

2523
eta1 = min(x/L, 0.1 - x/L);
2624
eta2 = 20*(x/L - 0.2).*(0.3 - x/L);
2725
eta3 = min(x/L - 0.6, 0.7 - x/L);
2826
eta4 = min(x/L - 0.9, 1 - x/L);
2927

30-
u0 = 16*max(0, max(eta1, max(eta2, max(eta3, eta4))));
28+
u0 = 16*max(0, max([eta1, eta2, eta3, eta4], [], 2));
3129

3230
u0hat = fft(u0);
33-
31+
3432
tspan = [0, 100];
3533

3634
obj = [email protected](tspan, u0hat, params);

Diff for: src/+otp/+kuramotosivashinsky/KuramotoSivashinskyProblem.m

+8-16
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,27 @@
99
methods
1010
function soly = convert2grid(~, soly)
1111

12-
soly = real(ifft(soly));
12+
soly = abs(ifft(soly));
1313

1414
end
1515

1616
end
1717

1818
methods (Access = protected)
1919
function onSettingsChanged(obj)
20-
21-
L = obj.Parameters.L;
22-
2320
N = obj.NumVars;
2421

25-
div = L/(2*pi);
22+
k = 2 * pi * [0:(N/2 - 1), 0, (-N/2 + 1):-1].' / obj.Parameters.L;
23+
ik = 1i * k;
24+
k24 = k.^2 - k.^4;
2625

27-
% note that k already has "i" in it
28-
k = (1i*[0:(N/2 - 1), 0, (-N/2 + 1):-1].'/div);
29-
k2 = k.^2;
30-
k4 = k.^4;
31-
k24 = k2 + k4;
32-
33-
obj.Rhs = otp.Rhs(@(t, u) otp.kuramotosivashinsky.f(t, u, k, k24), ...
26+
obj.Rhs = otp.Rhs(@(t, u) otp.kuramotosivashinsky.f(t, u, ik, k24), ...
3427
otp.Rhs.FieldNames.Jacobian, ...
35-
@(t, u) otp.kuramotosivashinsky.jac(t,u, k, k24), ...
28+
@(t, u) otp.kuramotosivashinsky.jac(t,u, ik, k24), ...
3629
otp.Rhs.FieldNames.JacobianVectorProduct, ...
37-
@(t, u, v) otp.kuramotosivashinsky.jvp(t, u, v, k, k24), ...
30+
@(t, u, v) otp.kuramotosivashinsky.jvp(t, u, v, ik, k24), ...
3831
otp.Rhs.FieldNames.JacobianAdjointVectorProduct, ...
39-
@(t, u, v) otp.kuramotosivashinsky.javp(t, u, v, k, k24));
40-
32+
@(t, u, v) otp.kuramotosivashinsky.javp(t, u, v, ik, k24));
4133
end
4234

4335
function validateNewState(obj, newTimeSpan, newY0, newParameters)

Diff for: src/+otp/+kuramotosivashinsky/f.m

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
function ut = f(~, u, k, k24)
1+
function ut = f(~, u, ik, k24)
22

3-
u2 = fft(real(ifft(u)).^2);
4-
5-
ut = -k24.*u - (k/2).*u2;
3+
ut = k24 .* u - (ik/2) .* fft(ifft(u).^2);
64

75
end

Diff for: src/+otp/+kuramotosivashinsky/jac.m

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
function j = jac(~, u, k, k24)
1+
function j = jac(~, u, ik, k24)
22

3-
j = -diag(k24) - k.*ifft(fft(diag(real(ifft(u)))).').';
3+
circulantU = toeplitz(u, [u(1); flipud(u(2:end))]);
4+
j = diag(k24) - ik / length(u) .* circulantU;
45

56
end

Diff for: src/+otp/+kuramotosivashinsky/javp.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function jv = javp(~, u, v, k, k24)
1+
function jv = javp(~, u, v, ik, k24)
22

3-
jv = -k24.*v - fft(real(ifft(u)).*ifft(conj(k).*v));
3+
jv = k24 .* v + fft(conj(ifft(u)) .* ifft(ik .* v));
44

55
end

Diff for: src/+otp/+kuramotosivashinsky/jvp.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function jv = jvp(~, u, v, k, k24)
1+
function jv = jvp(~, u, v, ik, k24)
22

3-
jv = -k24.*v - k.*fft(real(ifft(u)).*ifft(v));
3+
jv = k24 .* v - ik .* fft(ifft(u) .* ifft(v));
44

55
end

0 commit comments

Comments
 (0)