forwardDynamics.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "rigidBodyModel.H"
27 #include "rigidBodyModelState.H"
28 #include "rigidBodyRestraint.H"
29 
30 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
31 
33 (
34  scalarField& tau,
36 ) const
37 {
38  if (restraints_.empty())
39  {
40  return;
41  }
42 
43  forAll(restraints_, ri)
44  {
45  DebugInfo << "Restraint " << restraints_[ri].name();
46 
47  // Accumulate the restraint forces
48  restraints_[ri].restrain(tau, fx);
49  }
50 }
51 
52 
54 (
55  rigidBodyModelState& state,
56  const scalarField& tau,
57  const Field<spatialVector>& fx
58 ) const
59 {
60  scalarField& qDdot = state.qDdot();
61 
63  << "q = " << state.q() << nl
64  << "qDot = " << state.qDot() << nl
65  << "tau = " << tau << endl;
66 
67  // Joint state returned by jcalc
68  joint::XSvc J;
69 
70  v_[0] = Zero;
71 
72  for (label i=1; i<nBodies(); i++)
73  {
74  const joint& jnt = joints()[i];
75  jnt.jcalc(J, state);
76 
77  S_[i] = J.S;
78  S1_[i] = J.S1;
79 
80  Xlambda_[i] = J.X & XT_[i];
81 
82  const label lambdai = lambda_[i];
83 
84  if (lambdai != 0)
85  {
86  X0_[i] = Xlambda_[i] & X0_[lambdai];
87  }
88  else
89  {
90  X0_[i] = Xlambda_[i];
91  }
92 
93  v_[i] = (Xlambda_[i] & v_[lambdai]) + J.v;
94  c_[i] = J.c + (v_[i] ^ J.v);
95  IA_[i] = I(i);
96  pA_[i] = v_[i] ^* (I(i) & v_[i]);
97 
98  if (fx.size())
99  {
100  pA_[i] -= *X0_[i] & fx[i];
101  }
102  }
103 
104  for (label i=nBodies()-1; i>0; i--)
105  {
106  const joint& jnt = joints()[i];
107  const label qi = jnt.qIndex();
108 
109  if (jnt.nDoF() == 0)
110  {
111  U1_[i] = Zero;
112  u_[i] = Zero;
113 
114  const label lambdai = lambda_[i];
115 
116  if (lambdai != 0)
117  {
118  const spatialTensor Ia(IA_[i]);
119 
120  const spatialVector pa(pA_[i] + (Ia & c_[i]));
121 
122  IA_[lambdai] +=
123  spatialTensor(Xlambda_[i].T())
124  & Ia
125  & spatialTensor(Xlambda_[i]);
126 
127  pA_[lambdai] += Xlambda_[i].T() & pa;
128  }
129  }
130  else if (jnt.nDoF() == 1)
131  {
132  U1_[i] = IA_[i] & S1_[i];
133  Dinv_[i].xx() = 1/(S1_[i] && U1_[i]);
134  u_[i].x() = tau[qi] - (S1_[i] && pA_[i]);
135 
136  const label lambdai = lambda_[i];
137 
138  if (lambdai != 0)
139  {
140  const spatialTensor Ia
141  (
142  IA_[i] - (U1_[i]*(Dinv_[i].xx()*U1_[i]))
143  );
144 
145  const spatialVector pa
146  (
147  pA_[i] + (Ia & c_[i]) + U1_[i]*(Dinv_[i].xx()*u_[i].x())
148  );
149 
150  IA_[lambdai] +=
151  spatialTensor(Xlambda_[i].T())
152  & Ia
153  & spatialTensor(Xlambda_[i]);
154 
155  pA_[lambdai] += Xlambda_[i].T() & pa;
156  }
157  }
158  else
159  {
160  U_[i] = IA_[i] & S_[i];
161  Dinv_[i] = (S_[i].T() & U_[i]).inv();
162  u_[i] = tau.block<vector>(qi) - (S_[i].T() & pA_[i]);
163 
164  const label lambdai = lambda_[i];
165 
166  if (lambdai != 0)
167  {
168  const spatialTensor Ia
169  (
170  IA_[i] - (U_[i] & Dinv_[i] & U_[i].T())
171  );
172 
173  const spatialVector pa
174  (
175  pA_[i] + (Ia & c_[i]) + (U_[i] & Dinv_[i] & u_[i])
176  );
177 
178  IA_[lambdai] +=
179  spatialTensor(Xlambda_[i].T())
180  & Ia
181  & spatialTensor(Xlambda_[i]);
182 
183  pA_[lambdai] += Xlambda_[i].T() & pa;
184  }
185  }
186  }
187 
188  a_[0] = spatialVector(Zero, -g_);
189 
190  for (label i=1; i<nBodies(); i++)
191  {
192  const joint& jnt = joints()[i];
193  const label qi = jnt.qIndex();
194 
195  a_[i] = (Xlambda_[i] & a_[lambda_[i]]) + c_[i];
196 
197  if (jnt.nDoF() == 0)
198  {
199  // do nothing
200  }
201  else if (jnt.nDoF() == 1)
202  {
203  qDdot[qi] = Dinv_[i].xx()*(u_[i].x() - (U1_[i] && a_[i]));
204  a_[i] += S1_[i]*qDdot[qi];
205  }
206  else
207  {
208  vector qDdoti(Dinv_[i] & (u_[i] - (U_[i].T() & a_[i])));
209 
210  // Need to add mutable "block<vector>" to Field
211  qDdot[qi] = qDdoti.x();
212  qDdot[qi+1] = qDdoti.y();
213  qDdot[qi+2] = qDdoti.z();
214 
215  a_[i] += (S_[i] & qDdoti);
216  }
217  }
218 
219  DebugInfo
220  << "qDdot = " << qDdot << nl
221  << "a = " << a_ << endl;
222 }
223 
224 
226 (
227  const rigidBodyModelState& state
228 ) const
229 {
230  const scalarField& qDdot = state.qDdot();
231 
233 
234  // Joint state returned by jcalc
235  joint::XSvc J;
236 
237  v_[0] = Zero;
238  a_[0] = spatialVector(Zero, -g_);
239 
240  for (label i=1; i<nBodies(); i++)
241  {
242  const joint& jnt = joints()[i];
243  const label qi = jnt.qIndex();
244 
245  jnt.jcalc(J, state);
246 
247  S_[i] = J.S;
248  S1_[i] = J.S1;
249 
250  Xlambda_[i] = J.X & XT_[i];
251 
252  const label lambdai = lambda_[i];
253 
254  if (lambdai != 0)
255  {
256  X0_[i] = Xlambda_[i] & X0_[lambdai];
257  }
258  else
259  {
260  X0_[i] = Xlambda_[i];
261  }
262 
263  v_[i] = (Xlambda_[i] & v_[lambdai]) + J.v;
264  c_[i] = J.c + (v_[i] ^ J.v);
265  a_[i] = (Xlambda_[i] & a_[lambdai]) + c_[i];
266 
267  if (jnt.nDoF() == 0)
268  {
269  // do nothing
270  }
271  else if (jnt.nDoF() == 1)
272  {
273  a_[i] += S1_[i]*qDdot[qi];
274  }
275  else
276  {
277  a_[i] += S_[i] & qDdot.block<vector>(qi);
278  }
279  }
280 
281  DebugInfo<< "a = " << a_ << endl;
282 }
283 
284 
285 // ************************************************************************* //
Abstract base-class for all rigid-body joints.
Definition: joint.H:80
Templated 3D spatial tensor derived from MatrixSpace used to represent transformations of spatial vec...
Definition: SpatialTensor.H:64
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
spatialVector v
The constrained joint velocity.
Definition: joint.H:137
spatialVector S1
The joint motion sub-space (1-DoF)
Definition: joint.H:134
void forwardDynamicsCorrection(const rigidBodyModelState &state) const
Correct the velocity and acceleration of the bodies in the model.
compactSpatialTensor S
The joint motion sub-space (3-DoF)
Definition: joint.H:131
virtual void jcalc(XSvc &J, const rigidBodyModelState &state) const =0
Update the rigidBodyModel state for the joint given.
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
void applyRestraints(scalarField &tau, Field< spatialVector > &fx) const
Apply the restraints and accumulate the internal joint forces.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
const Cmpt & z() const
Definition: VectorI.H:87
spatialVector c
The constrained joint acceleration correction.
Definition: joint.H:141
const Cmpt & y() const
Definition: VectorI.H:81
SpatialTensor< scalar > spatialTensor
SpatialTensor of scalars.
Definition: spatialTensor.H:47
Holds the motion state of rigid-body model.
Joint state returned by jcalc.
Definition: joint.H:123
static const Identity< scalar > I
Definition: Identity.H:93
const scalarField & q() const
Return access to the joint position and orientation.
SpatialVector< scalar > spatialVector
SpatialVector of scalars.
Definition: spatialVector.H:47
#define DebugInFunction
Report an information message using Foam::Info.
const scalarField & qDot() const
Return access to the joint velocity.
static const zero Zero
Definition: zero.H:97
VSForm block(const label start) const
Definition: Field.C:508
const Cmpt & x() const
Definition: VectorI.H:75
#define DebugInfo
Report an information message using Foam::Info.
label nDoF() const
Return the number of degrees of freedom in this joint.
Definition: jointI.H:39
static const char nl
Definition: Ostream.H:265
const volScalarField & T
spatialTransform X
The joint transformation.
Definition: joint.H:128
const scalarField & qDdot() const
Return access to the joint acceleration.
void forwardDynamics(rigidBodyModelState &state, const scalarField &tau, const Field< spatialVector > &fx) const
Calculate the joint acceleration qDdot from the joint state q,.