% MotionGenesis file: MGTemplateBasic.txt
% Copyright (c) 2009-2025 Motion Genesis LLC.
%-------------------------------------------------------------------------------
%   Physical objects.
NewtonianFrame  N        % Example: Earth
RigidBody       A        % Example: Upper arm.
RigidFrame      B        % Comment.
Particle        Q        % Comment.
Point           P( B )   % Point P is a point of B.
%-------------------------------------------------------------------------------
%   Mathematical declarations.
Variable   qA''           % Angle qA and its 1st and 2nd derivatives.
Variable   Fx, Fy         % Reaction forces.
Constant   g = 9.8 m/s^2  % Earth's gravitational acceleration.
Specified  TA             % Torque on A from N.
%-------------------------------------------------------------------------------
%   Mass and inertia properties.
A.SetMass( mA = 2 kg )
Q.SetMass( mQ = 3 kg )
A.SetInertia( Acm,  IAxx = 0.2 kg*m^2,  IAyy = 0.3 kg*m^2,  IAzz = 0.4 kg*m^2 )
B.SetInertia( Bcm,  IBxx, IByy, IBzz,  IBxy, IByz, IBzx )
%-------------------------------------------------------------------------------
%   Rotational kinematics.
A.RotateX( N, qA )
B.RotateNegativeY( A, qB )
C.SetAngularVelocity( N, someVector> )
%-------------------------------------------------------------------------------
%   Translational kinematics.
Q.SetPosition( No, positionVector> )
Q.SetPositionVelocity( No, positionVector> ) % Sets position, velocity.
Q.Translate( No, positionVector> )           % Sets position, velocity, acceleration.
Q.SetVelocityAcceleration( N, velVector> )   % Differentiates to find acceleration.
%-------------------------------------------------------------------------------
%   Configuration constraints (if any).
LoopConstraint[1] = Dot( Loop>,  Nx> )
LoopConstraint[2] = Dot( Loop>,  Ny> )
%-------------------------------------------------------------------------------
%   Motion constraints (if any).
MotionConstraint[1] = Dot( Q.GetVelocity(N), Ax> )   % E.g., rolling at point Q.
MotionConstraint[2] = Dot( Q.GetVelocity(N), Ay> )
SolveDt( MotionConstraint = 0,  varName1, varName2 )
%-------------------------------------------------------------------------------
%   Add relevant contact and distance forces.
System.AddForceGravity( -g*Ny> )            % Add gravity force to each massive object.
Q.AddForce( someVector> )                   % External force.
Q.AddForce( P, actionReactionForce> )       % Law of action/reaction.
%-------------------------------------------------------------------------------
%   Add relevant torques.
A.AddTorque( SomeVector> )                  % External torque.
B.AddTorque( A, actionReactionTorque> )     % Law of action/reaction.
%-------------------------------------------------------------------------------
%   Translational dynamic equations (F = m*a).
%   For static analysis, use  B.GetStatics()  or  System.GetStatics().
Dynamics[1] = Dot(  B.GetDynamics(),  Nx>  )
Dynamics[2] = Dot(  System(A,B).GetDynamics(),  Ny>  )
%-------------------------------------------------------------------------------
%   Rotational dynamic equations (M = DH/Dt + ...).
%   For static analysis, use  B.GetStatics( aboutPoint ).
Dynamics[3] = Dot(  B.GetDynamics(Bcm),  Bz>  )
%-------------------------------------------------------------------------------
%   Alternative: Form dynamic equations with Kane's method.
%   For static analysis, use  System.GetStaticsKane().
SetGeneralizedSpeed( x', theta', vx, vy, wz )
DynamicsKane = System.GetDynamicsKane()
%-------------------------------------------------------------------------------
%   Optional: Solve algebraic equations for list of unknowns.
%   Efficient alternative: Delay this until the ODE command below.
Solve( Dynamics = 0,   x'', y'', Fx, Fy )
%-------------------------------------------------------------------------------
%   Power calculations (if needed).
%   System.GetPower() gets the power of _all_ forces and torques on the system.
powerF1 = P1.GetPowerResultantForce()      % Power due to P1.GetResultantForce().
powerF2 = P1.GetPowerResultantForce( P2 )  % Power due to P1.GetResultantForce(P2).
powerB1 = B1.GetPowerResultantTorque()     % Power due to B1.GetTorque()
powerB2 = B2.GetPowerForce( B2 )  % Power due to B1.GetTorque( B2 )
%-------------------------------------------------------------------------------
%   Work done by forces and torques (if needed).
Variable workDone' = powerP1 + powerP2 + ...   % Work done by forces & torques.
Input  workDone = 0 Joules                     % Initial value of workDone.
%-------------------------------------------------------------------------------
%   Potential, kinetic, and mechanical energy calculations (if needed).
KE = System.GetKineticEnergy()
PEgravity = System.GetForceGravityPotentialEnergy( -g*Ny>, No )
MechanicalEnergy = KE + PEgravity - workDone
%-------------------------------------------------------------------------------
%   Momentum calculations (if needed).
H> = System.GetAngularMomentum( aboutPoint )
L> = EvaluateToNumber( System.GetLinearMomentum() )
%-------------------------------------------------------------------------------
%   Other calculations (e.g., for output).
xQ = Dot( Q.GetPosition(No),  Nx> )
%-------------------------------------------------------------------------------
%   Expressions for specified quantities (if needed).
TA = 2*cos(pi*t) + 4*cos(pi*t)
%-------------------------------------------------------------------------------
%   Set initial values for variables.
%   If needed, solve constraints to determine initial values, e.g.,
%   SolveSetInput( LoopConstraint = 0,  qA = qAguess deg, qB = qBguess deg )
Input  qA = 30 deg,  qA' = 0 rad/sec
Input  qB = 90 deg,  qB' = 0 rad/sec
%-------------------------------------------------------------------------------
%   List output quantities.
Output  t sec,  x meters,  Fx Newton,  Fy Newton
%-------------------------------------------------------------------------------
%   Set numerical integration parameters.
Input  tFinal = 8 sec,  tStep = 0.02 sec,  absError = 1.0E-07
%-------------------------------------------------------------------------------
%   Solve ODEs and write Output to the data file someFilename.1
%   Alternative: Auto-generate efficient MATLAB, C, or Fortran code with:
%   ODE( Dynamics = 0,  listOfVariablesToSolve )  Filename.m
ODE()  Filename
%-------------------------------------------------------------------------------
%   Plot columns 2 and 3 vs. column 1 using data in the file Filename.1
Plot Filename.1[1, 2, 3]
Pause
%-------------------------------------------------------------------------------
%   Set new initial values, solve ODEs, and plot again.
Input  qA := 45 deg,  qA' := 10 rad/sec
ODE()  fileB
Plot Filename.1[1, 2, 3]  fileB.1[1, 2, 3]
%-------------------------------------------------------------------------------
%   Record input together with responses.
Save  someFilename.html
Quit

