Commit 1f51c8a8 authored by Henry Weller's avatar Henry Weller
Browse files

rigidBodyDynamics: Added support for restraints and a linear spring with damper

applications/test/rigidBodyDynamics/spring: Test of the linear spring with damper restraint
Damped simple harmonic motion of a weight on a spring is simulated and
the results compared with analytical solution

    Test-spring
    gnuplot spring.gnuplot
    evince spring.eps

This development is sponsored by Carnegie Wave Energy Ltd.
parent 762aa208
spring.C
EXE = $(FOAM_USER_APPBIN)/Test-spring
EXE_INC = \
-I$(LIB_SRC)/rigidBodyDynamics/lnInclude
EXE_LIBS = \
-lrigidBodyDynamics
bodies
{
weight
{
type rigidBody;
mass 9.6;
centreOfMass (0 0 0);
inertia (0.1052 0 0 0.1052 0 0.1778);
parent root;
transform (1 0 0 0 1 0 0 0 1) (0 0 0);
joint
{
type Pz;
}
}
}
restraints
{
spring
{
type linearSpring;
body weight;
anchor (0 0 0.5);
refAttachmentPt (0 0 0);
stiffness 5000;
damping 50;
restLength 0.4;
}
}
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
spring
Description
Simple weight and damped-spirng simulation with 1-DoF.
\*---------------------------------------------------------------------------*/
#include "rigidBodyModel.H"
#include "masslessBody.H"
#include "sphere.H"
#include "joints.H"
#include "IFstream.H"
#include "OFstream.H"
using namespace Foam;
using namespace RBD;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
// Create the spring model from dictionary
rigidBodyModel spring(dictionary(IFstream("spring")()));
Info<< spring << endl;
// Create the joint-space state fields
scalarField q(spring.nDoF(), Zero);
scalarField w(spring.nw(), Zero);
scalarField qDot(spring.nDoF(), Zero);
scalarField qDdot(spring.nDoF(), Zero);
scalarField tau(spring.nDoF(), Zero);
Field<spatialVector> fx(spring.nBodies(), Zero);
OFstream qFile("qVsTime");
OFstream qDotFile("qDotVsTime");
// Integrate the motion of the spring for 4s using a symplectic method
scalar deltaT = 0.002;
for (scalar t=0; t<4; t+=deltaT)
{
qDot += 0.5*deltaT*qDdot;
q += deltaT*qDot;
// Update the body-state prior to the evaluation of the restraints
spring.forwardDynamicsCorrection
(
q,
w,
qDot,
qDdot
);
// Accumulate the restraint forces
fx = Zero;
spring.applyRestraints(fx);
// Calculate the body acceleration for the given state
// and restraint forces
spring.forwardDynamics
(
q,
w,
qDot,
tau,
fx,
qDdot
);
// Update the velocity
qDot += 0.5*deltaT*qDdot;
// Write the results for graph generation
// using 'gnuplot spring.gnuplot'
qFile << t << " " << q[0] << endl;
qDotFile << t << " " << qDot[0] << endl;
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
# \\/ M anipulation |
#-------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
#
# Script
# spring.gnuplot
#
# Description
# Creates an PostScript graph file of Test-spring results vs
# the analytical solution.
#
#------------------------------------------------------------------------------
reset
set samples 2000
k = 5000.0
m = 9.6
c = 50.0
a = -0.1
omega = sqrt(k/m)
zeta = c/(2.0*m*omega)
phi = atan((sqrt(1.0 - zeta**2))/zeta)
A = a/sin(phi)
pos(A, t, omega, phi, zeta) = A*exp(-zeta*omega*t)*sin(sqrt(1-zeta**2)*omega*t + phi)
vel(A, t, omega, phi, zeta) = \
A*exp(-zeta*omega*t)*\
( \
sqrt(1-zeta**2)*omega*cos(sqrt(1-zeta**2)*omega*t + phi) \
- zeta*omega*sin(sqrt(1-zeta**2)*omega*t + phi) \
)
set xlabel "Time/[s]"
set ylabel "Position"
set ytics nomirror
set y2tics
set yrange [-0.1:0.1]
set y2range [-2:2]
set xzeroaxis
set terminal postscript eps color enhanced solid
set output "spring.eps"
plot \
"qVsTime" u 1:($2 - 0.1) w l t "Simulation, centre of mass relative to start", \
pos(A, x, omega, phi, zeta) w l t "Analytical solution, centre of mass", \
"qDotVsTime" u 1:2 w l axes x1y2 t "Simulation, vertical velocity", \
vel(A, x, omega, phi, zeta) w l axes x1y2 t "Analytical solution, vertical velocity"
#------------------------------------------------------------------------------
......@@ -25,6 +25,10 @@ joints/Pz/Pz.C
joints/Pa/Pa.C
joints/Pxyz/Pxyz.C
restraints/restraint/rigidBodyRestraint.C
restraints/restraint/rigidBodyRestraintNew.C
restraints/linearSpring/linearSpring.C
rigidBodyModel/rigidBodyModel.C
rigidBodyModel/forwardDynamics.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "linearSpring.H"
#include "rigidBodyModel.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace RBD
{
namespace restraints
{
defineTypeNameAndDebug(linearSpring, 0);
addToRunTimeSelectionTable
(
restraint,
linearSpring,
dictionary
);
}
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::RBD::restraints::linearSpring::linearSpring
(
const word& name,
const dictionary& dict,
const rigidBodyModel& model
)
:
restraint(name, dict, model),
anchor_(),
refAttachmentPt_(),
stiffness_(),
damping_(),
restLength_()
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::RBD::restraints::linearSpring::~linearSpring()
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
Foam::spatialVector Foam::RBD::restraints::linearSpring::restrain() const
{
spatialVector attachmentPt
(
model_.X0(bodyIndex_).inv() && spatialVector(Zero, refAttachmentPt_)
);
// Current axis of the spring
vector r = attachmentPt.l() - anchor_;
scalar magR = mag(r);
r /= (magR + VSMALL);
// Velocity of the end of the spring
vector v = model_.v(bodyIndex_, refAttachmentPt_).l();
// Force and moment including optional damping
vector force = (-stiffness_*(magR - restLength_) - damping_*(r & v))*r;
vector moment = (attachmentPt.l() - model_.X0(bodyIndex_).r()) ^ force;
if (model_.debug)
{
Info<< " attachmentPt - anchor " << r*magR
<< " spring length " << magR
<< " force " << force
<< " moment " << moment
<< endl;
}
return spatialVector(moment, force);
}
bool Foam::RBD::restraints::linearSpring::read
(
const dictionary& dict
)
{
restraint::read(dict);
coeffs_.lookup("anchor") >> anchor_;
coeffs_.lookup("refAttachmentPt") >> refAttachmentPt_;
coeffs_.lookup("stiffness") >> stiffness_;
coeffs_.lookup("damping") >> damping_;
coeffs_.lookup("restLength") >> restLength_;
return true;
}
void Foam::RBD::restraints::linearSpring::write
(
Ostream& os
) const
{
restraint::write(os);
os.writeKeyword("anchor")
<< anchor_ << token::END_STATEMENT << nl;
os.writeKeyword("refAttachmentPt")
<< refAttachmentPt_ << token::END_STATEMENT << nl;
os.writeKeyword("stiffness")
<< stiffness_ << token::END_STATEMENT << nl;
os.writeKeyword("damping")
<< damping_ << token::END_STATEMENT << nl;
os.writeKeyword("restLength")
<< restLength_ << token::END_STATEMENT << nl;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::RBD::restraints::linearSpring
Description
Linear spring restraint.
SourceFiles
linearSpring.C
\*---------------------------------------------------------------------------*/
#ifndef linearSpring_H
#define linearSpring_H
#include "rigidBodyRestraint.H"
#include "point.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace RBD
{
namespace restraints
{
/*---------------------------------------------------------------------------*\
Class linearSpring Declaration
\*---------------------------------------------------------------------------*/
class linearSpring
:
public restraint
{
// Private data
//- Anchor point, where the spring is attached to an immovable
// object
point anchor_;
//- Reference point of attachment to the solid body
point refAttachmentPt_;
//- Spring stiffness coefficient (N/m)
scalar stiffness_;
//- Damping coefficient (Ns/m)
scalar damping_;
//- Rest length - length of spring when no forces are applied to it
scalar restLength_;
public:
//- Runtime type information
TypeName("linearSpring");
// Constructors
//- Construct from components
linearSpring
(
const word& name,
const dictionary& dict,
const rigidBodyModel& model
);
//- Construct and return a clone
virtual autoPtr<restraint> clone() const
{
return autoPtr<restraint>
(
new linearSpring(*this)
);
}
//- Destructor
virtual ~linearSpring();
// Member Functions
//- Return the external force applied to the body by this restraint
virtual spatialVector restrain() const;
//- Update properties from given dictionary
virtual bool read(const dictionary& dict);
//- Write
virtual void write(Ostream&) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace restraints
} // End namespace RBD
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------* \
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "rigidBodyRestraint.H"
#include "rigidBodyModel.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace RBD
{
defineTypeNameAndDebug(restraint, 0);
defineRunTimeSelectionTable(restraint, dictionary);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::RBD::restraint::restraint
(
const word& name,
const dictionary& dict,
const rigidBodyModel& model
)
:
name_(name),
bodyIndex_(model.bodyID(dict.lookup("body"))),
coeffs_(dict),
model_(model)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::RBD::restraint::~restraint()
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
const Foam::dictionary& Foam::RBD::restraint::coeffDict() const
{
return coeffs_;
}
bool Foam::RBD::restraint::read(const dictionary& dict)
{
coeffs_ = dict;
return true;
}
void Foam::RBD::restraint::write(Ostream& os) const
{
os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox