-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constrained pendulum problem #36
base: master
Are you sure you want to change the base?
Changes from 6 commits
9c47f78
6d54390
0caf7cc
afdfa7e
cd25494
f072d66
dfee988
13cf20d
0bc36d2
42d822f
9b2ee98
a114be8
c3463f3
1b997dd
3bd98e8
5fa4f92
90bc7af
c51c478
46a4436
d23d13d
8453493
7ec54c6
c5cce23
ff96669
fc51b18
3c01164
e04aa6c
5a2cef0
73a155a
026c815
0e45849
2d282cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
classdef Canonical < otp.pendulumdae.PendulumDAEProblem | ||
%CANONICAL The constrained pendulum problem | ||
% | ||
% See | ||
% Hairer, E., Roche, M., Lubich, C. (1989). Description of differential-algebraic problems. | ||
% In: The Numerical Solution of Differential-Algebraic Systems by Runge-Kutta Methods. | ||
% Lecture Notes in Mathematics, vol 1409. Springer, Berlin, Heidelberg. | ||
% https://doi.org/10.1007/BFb0093948 | ||
|
||
methods | ||
function obj = Canonical | ||
tspan = [0; 10]; | ||
|
||
params = otp.pendulumdae.PendulumDAEParameters; | ||
params.Mass = 1; | ||
params.Length = 1; | ||
params.Gravity = otp.utils.PhysicalConstants.EarthGravity; | ||
|
||
y0 = [sqrt(2)/2; sqrt(2)/2; 0; 0; 0; 0; 0]; | ||
|
||
obj = [email protected](tspan, y0, params); | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
classdef PendulumDAEParameters | ||
%CONSTRAINEDPENDULUMPARAMETERS | ||
properties | ||
%GRAVITY is acceleration due to gravity | ||
Gravity %MATLAB ONLY: (1,1) {mustBeReal, mustBeFinite, mustBePositive} = 9.8 | ||
%MASSE of the node of the pendulum | ||
Mass %MATLAB ONLY: (:,1) {mustBeReal, mustBeFinite, mustBePositive} = 1 | ||
%LENGTH to the node of the pendulum | ||
Length %MATLAB ONLY: (:,1) {mustBeReal, mustBeFinite, mustBePositive} = 1 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||
classdef PendulumDAEProblem < otp.Problem | ||||
%CONSTRAINEDPENDULUM PROBLEM This is a Hessenberg Index-2 DAE problem posed in terms of three constraints | ||||
% | ||||
|
||||
properties (SetAccess=protected) | ||||
RHSDifferential | ||||
RHSAlgebraic | ||||
end | ||||
|
||||
properties (Dependent) | ||||
Y0Differential | ||||
Z0Algebraic | ||||
end | ||||
|
||||
methods | ||||
function obj = PendulumDAEProblem(timeSpan, y0, parameters) | ||||
[email protected]('Constrained Pendulum', 7, timeSpan, y0, parameters); | ||||
end | ||||
end | ||||
|
||||
methods (Access = protected) | ||||
function onSettingsChanged(obj) | ||||
m = obj.Parameters.Mass; | ||||
l = obj.Parameters.Length; | ||||
g = obj.Parameters.Gravity; | ||||
|
||||
% get initial energy | ||||
initialconstraints = otp.pendulumdae.constraints([], obj.Y0Differential, g, m, l, 0); | ||||
E0 = initialconstraints(3); | ||||
|
||||
|
||||
% The right hand size in terms of x, y, x', y', and three control parameters z | ||||
obj.RHS = otp.RHS(@(t, y) otp.pendulumdae.f(t, y, g, m, l, E0), ... | ||||
'Mass', @(t, y) otp.pendulumdae.mass(t, y, g, m, l, E0)); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the mass matrix is constant, it can be a matrix instead of a function handle. See
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just copying the pendulum problem, which had @(t,y). I will change |
||||
|
||||
obj.RHSDifferential = otp.RHS(@(t, y) otp.pendulumdae.fdifferential(t, y, g, m, l, E0)); | ||||
|
||||
obj.RHSAlgebraic = otp.RHS(@(t, y) otp.pendulumdae.constraints(t, y, g, m, l, E0), ... | ||||
'Jacobian', @(t, y) otp.pendulumdae.constraintsjacobian(t, y, g, m, l, E0)); | ||||
end | ||||
end | ||||
|
||||
methods | ||||
function y0differential = get.Y0Differential(obj) | ||||
y0differential = obj.Y0(1:4); | ||||
end | ||||
|
||||
function z0algebraic = get.Z0Algebraic(obj) | ||||
z0algebraic = obj.Y0(5:end); | ||||
end | ||||
end | ||||
|
||||
end | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function c = constraints(~, state, g, m, l, E0) | ||
|
||
x = state(1, :); | ||
y = state(2, :); | ||
u = state(3, :); | ||
v = state(4, :); | ||
|
||
c1 = x.^2 + y.^2 - l^2; | ||
c2 = 2*(x.*u + y.*v); | ||
c3 = m*(g*(y + l) + 0.5*(u.^2 + v.^2)) - E0; | ||
|
||
c = [c1; c2; c3]; | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function dc = constraintsjacobian(~, state, g, m, ~, ~) | ||
|
||
x = state(1); | ||
y = state(2); | ||
u = state(3); | ||
v = state(4); | ||
|
||
dc1dx = 2*x; | ||
dc1dy = 2*y; | ||
dc1du = 0; | ||
dc1dv = 0; | ||
|
||
dc2dx = 2*u; | ||
dc2dy = 2*v; | ||
dc2du = 2*x; | ||
dc2dv = 2*y; | ||
|
||
dc3dx = 0; | ||
dc3dy = m*g; | ||
dc3du = m*u; | ||
dc3dv = m*v; | ||
|
||
dc = [dc1dx, dc1dy, dc1du, dc1dv; ... | ||
dc2dx, dc2dy, dc2du, dc2dv; ... | ||
dc3dx, dc3dy, dc3du, dc3dv]; | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function dfull = f(t, statepluscontrol, g, m, l, E0) | ||
|
||
state = statepluscontrol(1:4, :); | ||
control = statepluscontrol(5:end, :); | ||
|
||
dstate = otp.pendulumdae.fdifferential(t, state, g, m, l, E0) ... | ||
- otp.pendulumdae.constraintsjacobian(t, state, g, m, l, E0).'*control; | ||
|
||
c = otp.pendulumdae.constraints(t, state, g, m, l, E0); | ||
|
||
dfull = [dstate; c]; | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function dstate = fdifferential(~, state, g, m, ~, ~) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The convention for functions is camel case now: https://github.com/ComputationalScienceLaboratory/ODE-Test-Problems/blob/master/docs/CONTRIBUTING.md#functions |
||
|
||
x = state(1, :); | ||
y = state(2, :); | ||
u = state(3, :); | ||
v = state(4, :); | ||
|
||
lxy2 = x.^2 + y.^2; | ||
|
||
lambda = (m/lxy2)*(u.^2 + v.^2) - (g/lxy2)*y; | ||
|
||
dx = u; | ||
dy = v; | ||
du = -(lambda/m).*x; | ||
dv = -(lambda/m)*y - g/m; | ||
|
||
dstate = [dx; dy; du; dv]; | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function M = mass(~, ~, ~, ~, ~, ~) | ||
|
||
M = [eye(4), zeros(4, 3); zeros(3, 7)]; | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to having these, but if we do include this, I think all DAE problems should have these for consistency. Also, I think
Y0Algebraic
would be better thanZ0Algebraic
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't really differentiate the two. Algebraic implies there is no direct differential equation thus it is a variable obtained from differentiation of the constraint. I think it should stay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this naming is pretty clear. It's mostly motivated by established naming conventions. For partitioned problems, we use
<property><partition name>
. E.g.RHSStiff and RHSNonstiff
jacobianDifferential and jacobianAlgebraic
Y0Position and Y0Velocity
This is nice for autocompletion as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad I misinterpreted what was being proposed. I thought you wanted
Y0Differential -> Y0Algebraic
notZ0Algebraic -> Y0Algebraic
. I agree on the convention you proposed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of changing it to that already, glad to see we all think alike