Skip to content

Commit 3e6ba2a

Browse files
Make superclass for params (#118)
* Make superclass for params * Fix param capitalization * Add docs * Update Ascher Linear DAE * Update CUSP * Update L'96 * Update Oregonator * Update PR * Update Robertson * Typo fixes * Fix Greek letters
1 parent af1f458 commit 3e6ba2a

File tree

28 files changed

+225
-179
lines changed

28 files changed

+225
-179
lines changed

docs/structure/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ TODO
88
:maxdepth: 1
99
:glob:
1010

11-
*
11+
problem
12+
rhs
13+
parameters

docs/structure/parameters.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Parameters
2+
================================================================================
3+
4+
.. automodule:: +otp
5+
.. autoclass:: Parameters
6+
:members:

toolbox/+otp/+ascherlineardae/+presets/Canonical.m

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,8 @@
1313
%
1414
% - ``Beta`` – Value of $β$.
1515

16-
p = inputParser;
17-
p.addParameter('beta', 1);
18-
p.parse(varargin{:});
19-
opts = p.Results;
20-
21-
params = otp.ascherlineardae.AscherLinearDAEParameters;
22-
params.Beta = opts.beta;
23-
24-
y0 = [1; params.Beta];
16+
params = otp.ascherlineardae.AscherLinearDAEParameters('Beta', 1, varargin{:});
17+
y0 = [1; params.Beta];
2518
tspan = [0.0; 1.0];
2619

2720
obj = obj@otp.ascherlineardae.AscherLinearDAEProblem(tspan, y0, params);

toolbox/+otp/+ascherlineardae/+presets/Petzold.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
function obj = Petzold
77
% Create the Petzold example of the Ascher linear DAE problem object.
88

9-
params = otp.ascherlineardae.AscherLinearDAEParameters;
10-
params.Beta = 0;
11-
12-
y0 = [1; params.Beta];
9+
params = otp.ascherlineardae.AscherLinearDAEParameters('Beta', 0);
10+
y0 = [1; params.Beta];
1311
tspan = [0.0; 1.0];
1412

1513
obj = obj@otp.ascherlineardae.AscherLinearDAEProblem(tspan, y0, params);

toolbox/+otp/+ascherlineardae/+presets/Stiff.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
methods
66
function obj = Stiff
77
% Create the stiff example of the Ascher linear DAE problem object.
8-
params = otp.ascherlineardae.AscherLinearDAEParameters;
9-
params.Beta = 100;
10-
11-
y0 = [1; params.Beta];
8+
9+
params = otp.ascherlineardae.AscherLinearDAEParameters('Beta', 100);
10+
y0 = [1; params.Beta];
1211
tspan = [0.0; 1.0];
1312

1413
obj = obj@otp.ascherlineardae.AscherLinearDAEProblem(tspan, y0, params);
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
classdef AscherLinearDAEParameters
1+
classdef AscherLinearDAEParameters < otp.Parameters
22
% Parameters for Ascher Linear DAE problem
33
properties
44
% A scalar parameter $β$ in the linear model. It affects the stifness of the problem.
55
Beta %MATLAB ONLY: (1,1) {mustBeNumeric} = 1
66
end
7+
8+
methods
9+
function obj = AscherLinearDAEParameters(varargin)
10+
% Create a Ascher Linear DAE parameters object.
11+
%
12+
% Parameters
13+
% ----------
14+
% varargin
15+
% A variable number of name-value pairs. A name can be any property of this class, and the subsequent
16+
% value initializes that property.
17+
18+
obj = obj@otp.Parameters(varargin{:});
19+
end
20+
end
721
end

toolbox/+otp/+cusp/+presets/Canonical.m

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
% b_i(0) &= 2 \sin\left( \frac{2 i \pi}{N} \right), \\
99
% $$
1010
%
11-
% for $i = 1, \dots, N$. The parameters are $\varepsilon = 10^{-4}$ and $\sigma = \frac{1}{144}$.
11+
% for $i = 1, \dots, N$. The parameters are $ε = 10^{-4}$ and $σ = \frac{1}{144}$.
1212

1313
methods
1414
function obj = Canonical(varargin)
@@ -20,8 +20,8 @@
2020
% A variable number of name-value pairs. The accepted names are
2121
%
2222
% - ``N`` – The number of cells in the spatial discretization.
23-
% - ``epsilon`` – Value of $\varepsilon$.
24-
% - ``sigma`` – Value of $\sigma$.
23+
% - ``epsilon`` – Value of $ε$.
24+
% - ``sigma`` – Value of $σ$.
2525
%
2626
% Returns
2727
% -------
@@ -30,22 +30,19 @@
3030

3131
p = inputParser;
3232
p.addParameter('N', 32);
33-
p.addParameter('epsilon', 1e-4);
34-
p.addParameter('sigma', 1/144);
3533
p.parse(varargin{:});
36-
opts = p.Results;
34+
n = p.Results.N;
3735

38-
params = otp.cusp.CUSPParameters;
39-
params.Epsilon = opts.epsilon;
40-
params.Sigma = opts.sigma;
41-
42-
ang = 2 * pi / opts.N * (1:opts.N).';
43-
y0 = zeros(opts.N, 1);
44-
a0 = -2*cos(ang);
45-
b0 = 2*sin(ang);
36+
ang = 2 * pi * (1:n).' / n;
37+
y0 = zeros(n, 1);
38+
a0 = -2 * cos(ang);
39+
b0 = 2 * sin(ang);
4640

4741
u0 = [y0; a0; b0];
4842
tspan = [0; 1.1];
43+
44+
unmatched = namedargs2cell(p.Unmatched);
45+
params = otp.cusp.CUSPParameters('Epsilon', 1e-4, 'Sigma', 1/144, unmatched{:});
4946

5047
obj = obj@otp.cusp.CUSPProblem(tspan, u0, params);
5148
end
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
classdef CUSPParameters
1+
classdef CUSPParameters < otp.Parameters
22
% Parameters for the CUSP problem.
33
properties
4-
% The stiffness parameter $\varepsilon$ for the "cusp catastrophe" model.
4+
% The stiffness parameter $ε$ for the "cusp catastrophe" model.
55
Epsilon %MATLAB ONLY: (1,1) {mustBeNumeric}
66

7-
% The diffusion coefficient $\sigma$ for all three variables.
7+
% The diffusion coefficient $σ$ for all three variables.
88
Sigma %MATLAB ONLY: (1,1) {mustBeNumeric}
99
end
10+
11+
methods
12+
function obj = CUSPParameters(varargin)
13+
% Create a CUSP parameters object.
14+
%
15+
% Parameters
16+
% ----------
17+
% varargin
18+
% A variable number of name-value pairs. A name can be any property of this class, and the subsequent
19+
% value initializes that property.
20+
21+
obj = obj@otp.Parameters(varargin{:});
22+
end
23+
end
1024
end
1125

toolbox/+otp/+cusp/CUSPProblem.m

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,32 @@
44
% The CUSP problem :cite:p:`HW96` (pp. 147-148) is the PDE
55
%
66
% $$
7-
% \frac{\partial y}{\partial t} &= -\frac{1}{\varepsilon} (y^3 + a y + b)
8-
% + \sigma \frac{\partial^2 y}{\partial x^2}, \\
9-
% \frac{\partial a}{\partial t} &= b + 0.07 v + \sigma \frac{\partial^2 a}{\partial x^2}, \\
10-
% \frac{\partial b}{\partial t} &= (1 - a^2) b - a - 0.4 y + 0.035 v + \sigma \frac{\partial^2 b}{\partial x^2},
7+
% \frac{\partial y}{\partial t} &= -\frac{1}{ε} (y^3 + a y + b) + σ \frac{\partial^2 y}{\partial x^2}, \\
8+
% \frac{\partial a}{\partial t} &= b + 0.07 v + σ \frac{\partial^2 a}{\partial x^2}, \\
9+
% \frac{\partial b}{\partial t} &= (1 - a^2) b - a - 0.4 y + 0.035 v + σ \frac{\partial^2 b}{\partial x^2},
1110
% $$
1211
%
1312
%
1413
% where $v = u / (u + 0.1)$ and $u = (y - 0.7)(y - 1.3)$. The spatial domain $x \in [0, 1]$ has period boundary
1514
% conditions. Discretization with second order finite difference on a grid with $N$ cells gives
1615
%
1716
% $$
18-
% y'_i &= -\frac{1}{\varepsilon} (y_i^3 + a_i y_i + b_i) + \sigma N^2 (y_{i-1} - 2 y_i + y_{i+1}) \\
19-
% a'_i &= b_i + 0.07 v_i + \sigma N^2 (a_{i-1} - 2 a_i + a_{i+1}) \\
20-
% b'_i &= (1 - a_i^2) b_i - a_i - 0.4 y_i + 0.035 v_i + \sigma N^2 (b_{i-1} - 2 b_i + b_{i+1}),
17+
% y'_i &= -\frac{1}{ε} (y_i^3 + a_i y_i + b_i) + σ N^2 (y_{i-1} - 2 y_i + y_{i+1}) \\
18+
% a'_i &= b_i + 0.07 v_i + σ N^2 (a_{i-1} - 2 a_i + a_{i+1}) \\
19+
% b'_i &= (1 - a_i^2) b_i - a_i - 0.4 y_i + 0.035 v_i + σ N^2 (b_{i-1} - 2 b_i + b_{i+1}),
2120
% $$
2221
%
2322
% where $i = 1, \dots, N$. Values at cell indices $i=0, N+1$ are specificied by the periodic boundary conditions.
2423
%
2524
% Notes
2625
% -----
27-
% +---------------------+----------------------------------------------------------------------------+
28-
% | Type | PDE |
29-
% +---------------------+----------------------------------------------------------------------------+
30-
% | Number of Variables | arbitrary multiple of 3 |
31-
% +---------------------+----------------------------------------------------------------------------+
32-
% | Stiff | typically, depending on $\varepsilon$, $\sigma$, and number of grid points |
33-
% +---------------------+----------------------------------------------------------------------------+
26+
% +---------------------+-------------------------------------------------------------+
27+
% | Type | PDE |
28+
% +---------------------+-------------------------------------------------------------+
29+
% | Number of Variables | arbitrary multiple of 3 |
30+
% +---------------------+-------------------------------------------------------------+
31+
% | Stiff | typically, depending on $ε$, $σ$, and number of grid points |
32+
% +---------------------+-------------------------------------------------------------+
3433
%
3534
% Example
3635
% -------
@@ -44,7 +43,7 @@
4443
end
4544

4645
properties (SetAccess = private)
47-
% Right-hand side containing the diffusion terms and the reaction terms multiplied by $\varepsilon^{-1}$.
46+
% Right-hand side containing the diffusion terms and the reaction terms multiplied by $ε^{-1}$.
4847
%
4948
% This partition of the RHS is used in :cite:p:`JM17`.
5049
%
@@ -53,7 +52,7 @@
5352
% RHSNonstiff
5453
RHSStiff
5554

56-
% Right-hand side containing the reaction terms not scaled by $\varepsilon^{-1}$.
55+
% Right-hand side containing the reaction terms not scaled by $ε^{-1}$.
5756
%
5857
% This partition of the RHS is used in :cite:p:`JM17`.
5958
%

toolbox/+otp/+lorenz63/+presets/Canonical.m

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@
1515
% - ``sigma`` – Value of $σ$.
1616
% - ``rho`` – Value of $ρ$.
1717
% - ``beta`` – Value of $β$.
18-
%
19-
20-
p = inputParser;
21-
p.addParameter('sigma', 10);
22-
p.addParameter('rho', 28);
23-
p.addParameter('beta', 8/3);
24-
p.parse(varargin{:});
25-
opts = p.Results;
26-
27-
params = otp.lorenz63.Lorenz63Parameters;
28-
29-
params.Sigma = opts.sigma;
30-
params.Rho = opts.rho;
31-
params.Beta = opts.beta;
3218

3319
y0 = [0; 1; 0];
3420
tspan = [0 60];
21+
params = otp.lorenz63.Lorenz63Parameters('Sigma', 10, 'Rho', 28, 'Beta', 8/3, varargin{:});
3522

3623
obj = obj@otp.lorenz63.Lorenz63Problem(tspan, y0, params);
3724
end

0 commit comments

Comments
 (0)