forked from uc3m-aerospace/anakin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3157e59
commit 3a20abc
Showing
5 changed files
with
220 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,30 +2,32 @@ | |
particle: class to model a point particle. Inherits from point. | ||
P0 = anakin.particle(); % no arguments return default object | ||
P = anakin.particle(<P|A|a|c|x,y,z>,<mass>,<S1>); | ||
P = anakin.particle(<mass>,<P|A|a|c|x,y,z>,<S1>); | ||
where: | ||
- <> denotes optional arguments | ||
- | denotes alternative arguments | ||
- P0 is the default particle (mass 1, located at the origin) | ||
- P is a particle | ||
- mass is the object mass | ||
- A is a point | ||
- a is a vector | ||
- c is an array with the three vector components | ||
- x,y,z are the three vector components | ||
- mass is the object mass | ||
- S1 is a frame. If given, all previous input as relative to that frame | ||
METHODS: | ||
* p: linear momentum in a given reference frame | ||
* H: angular momentum about a point in a given reference frame | ||
* T: kinetic energy in a given reference frame | ||
* inertia: tensor of inertia about a point | ||
* subs: takes values of the symbolic unknowns and returns a particle | ||
object which is purely numeric | ||
AUTHOR: Mario Merino <[email protected]> | ||
%} | ||
classdef particle < anakin.point | ||
properties | ||
properties (SetAccess = protected) | ||
mass = 1; % mass of the object | ||
end | ||
methods % creation | ||
|
@@ -39,7 +41,7 @@ | |
case 0 % no arguments | ||
return; | ||
case 1 | ||
Pt = anakin.particle(varargin{1},anakin.frame).c; | ||
Pt = anakin.particle(varargin{1},anakin.frame); | ||
P.c = Pt.c; | ||
P.mass = Pt.mass; | ||
case 2 | ||
|
@@ -51,34 +53,32 @@ | |
P.mass = varargin{1}; | ||
end | ||
else | ||
Pt = anakin.particle(varargin{1},varargin{2},anakin.frame).c; | ||
Pt = anakin.particle(varargin{1},varargin{2},anakin.frame); | ||
P.c = Pt.c; | ||
P.mass = Pt.mass; | ||
end | ||
case 3 | ||
if isa(varargin{end},'anakin.frame') % last is frame | ||
if isa(varargin{1},'anakin.vector') || numel(varargin{1}) == 3 % (vector or components), mass, frame | ||
P.c = varargin{3}.matrix * anakin.vector(varargin{1}).c + varargin{3}.c; | ||
P.mass = varargin{2}; | ||
if isa(varargin{2},'anakin.vector') || numel(varargin{2}) == 3 % (vector or components), mass, frame | ||
P.c = varargin{3}.matrix * anakin.vector(varargin{2}).c + varargin{3}.c; | ||
P.mass = varargin{1}; | ||
end | ||
else | ||
Pt = anakin.particle(varargin{1},varargin{2},varargin{3},anakin.frame).c; | ||
Pt = anakin.particle(varargin{1},varargin{2},varargin{3},anakin.frame); | ||
P.c = Pt.c; | ||
P.mass = Pt.mass; | ||
end | ||
case 4 | ||
if isa(varargin{end},'anakin.frame') % last is frame | ||
if isa(varargin{1},'anakin.vector') || numel(varargin{1}) == 3 % xyz, frame | ||
P.c = varargin{4}.matrix * anakin.vector(varargin{1},varargin{2},varargin{3}).c + varargin{4}.c; | ||
end | ||
if isa(varargin{end},'anakin.frame') % last is frame | ||
P.c = varargin{4}.matrix * anakin.vector(varargin{1},varargin{2},varargin{3}).c + varargin{4}.c; | ||
else | ||
Pt = anakin.particle(varargin{1},varargin{2},varargin{3},varargin{4},anakin.frame).c; | ||
Pt = anakin.particle(varargin{1},varargin{2},varargin{3},varargin{4},anakin.frame); | ||
P.c = Pt.c; | ||
P.mass = Pt.mass; | ||
end | ||
case 5 % xyz, mass, frame | ||
P.c = varargin{5}.matrix * anakin.vector(varargin{1},varargin{2},varargin{3}).c + varargin{5}.c; | ||
P.mass = varargin{4}; | ||
P.c = varargin{5}.matrix * anakin.vector(varargin{2},varargin{3},varargin{4}).c + varargin{5}.c; | ||
P.mass = varargin{1}; | ||
otherwise % other possibilities are not allowed | ||
error('Wrong number of arguments in particle'); | ||
end | ||
|
@@ -111,6 +111,14 @@ | |
S1 = anakin.frame; % default frame | ||
end | ||
T = (P.mass/2) * dot(P.vel(S1),P.vel(S1)); | ||
if isa(T,'sym') % symbolic input | ||
T = formula(simplify(T)); % simplify and force sym rather than symfun to allow indexing | ||
end | ||
end | ||
function P_ = subs(P,variables,values) % particularize symbolic vector | ||
P_ = P; | ||
P_.c = double(subs(P.c,variables,values)); | ||
P_.mass = double(subs(P.mass,variables,values)); | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
%{ | ||
Compendium of all tests for the function/class in the name of this file. | ||
NOTES: | ||
* The functions to be tested must be in the Matlab path. You can run the | ||
tests by executing 'runtests'. For convenience, you can set the run | ||
botton of Matlab's interface to runtests(<this function>) | ||
* Your working directory must be the | ||
directory where this test file is contained. | ||
AUTHOR: Mario Merino <[email protected]> | ||
%} | ||
function tests = test_particle | ||
clc; | ||
tests = functiontests(localfunctions); | ||
end | ||
|
||
%---------------------------------------------------------------------- | ||
%---------------------------------------------------------------------- | ||
%---------------------------------------------------------------------- | ||
|
||
function test_creator(~) % Call creator without arguments | ||
import anakin.* | ||
S1 = frame([4,5,6],[0,1,0;-1,0,0;0,0,1]); | ||
x = 1; y = 2; z = 3; c = [x;y;z]; cp = [6;4;9]; | ||
a = vector(c); A = point(c); | ||
mass = 3; | ||
|
||
P = particle(); | ||
assert(all(P.pos.components == [0;0;0])); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(particle); | ||
assert(all(P.pos.components == [0;0;0])); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(A); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(a); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(c); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(c'); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(x,y,z); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(particle,S1); | ||
assert(all(P.pos.components == [4;5;6])); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(A,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(a,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(c,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(c',S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(x,y,z,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == 1)); | ||
|
||
P = particle(mass); | ||
assert(all(P.pos.components == [0;0;0])); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,A); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,a); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,c); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,c'); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,x,y,z); | ||
assert(all(P.pos.components == c)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,A,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,a,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,c,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,c',S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == mass)); | ||
|
||
P = particle(mass,x,y,z,S1); | ||
assert(all(P.pos.components == cp)); | ||
assert(all(P.mass == mass)); | ||
|
||
end | ||
|
||
function test_momentum(~) % call momentum, linear momentum, energy | ||
import anakin.* | ||
if license('test','symbolic_toolbox') | ||
syms t; | ||
syms theta(t) xi(t); | ||
assume([in(t, 'real'), in(theta(t), 'real'), in(xi(t), 'real'),... | ||
in(diff(theta(t),t), 'real'), in(diff(xi(t),t), 'real')]); | ||
c = formula([cos(theta);xi^2;xi]); | ||
cp = formula([-sin(theta)*diff(theta,t);2*xi*diff(xi,t);diff(xi,t)]); | ||
mass = 3; | ||
P = particle(mass,c); | ||
|
||
assert(all(isAlways(P.p.components == 3*cp))); | ||
assert(all(isAlways(P.H.components == 3*cross(c,cp)))); | ||
assert(all(isAlways(P.T == 3*dot(cp,cp)/2))); | ||
end | ||
end | ||
|
||
function test_subs(~) % Particularize a symbolic vector | ||
import anakin.* | ||
if license('test','symbolic_toolbox') | ||
syms t; | ||
syms theta(t) xi(t); | ||
assume([in(t, 'real'), in(theta(t), 'real'), in(xi(t), 'real'),... | ||
in(diff(theta(t),t), 'real'), in(diff(xi(t),t), 'real')]); | ||
c = formula([cos(theta);xi^2;xi]); | ||
mass = 3; | ||
P = particle(mass,c); | ||
|
||
assert(P.subs({t,theta,xi},{1,3,-2}).pos == vector([cos(3);4;-2])); % call with cell arrays | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters