CG  Version 25
RigidBodyMotion.h
Go to the documentation of this file.
1 #ifndef RIGID_BODY_MOTION_H
2 #define RIGID_BODY_MOTION_H
3 
4 // Class for keeping track of the position and orientation of a rigid body moving
5 // under the influence of forces and torques.
6 
7 #include "Overture.h"
8 #define KK_DEBUG
9 #include "DBase.hh"
10 using namespace DBase;
11 
12 
13 class GenericGraphicsInterface;
14 
16 {
17 
18 public:
19 
21 {
22  positionHasNoConstraint=0,
25  positionIsFixed
26 };
27 
29  {
30  rotationHasNoConstraint=0,
33  rotationIsFixed
34  };
35 
37  {
38  leapFrogTrapezoidal=0,
40  implicitRungeKutta
41  } timeSteppingMethod;
42 
44  {
45  polynomialTwilightZone=0,
46  trigonometricTwilightZone
47  };
48 
49 
51  {
52  timePolynomialBodyForce=0,
53  timeFunctionBodyForce
54  };
55 
56  enum
57  {
58  defaultOrderOfAccuracy=-1234
59  };
60 
61 
62  RigidBodyMotion(int numberOfDimensions = 3);
63  ~RigidBodyMotion();
64 
65  bool centerOfMassHasBeenInitialized() const;
66 
67  // correct solution at time t using new values of the forces at time t.
68  int correct( real t,
69  const RealArray & force,
70  const RealArray & torque,
71  RealArray & xCM,
72  RealArray & rotation );
73 
74  // Version of correct that takes added mass matrices.
75  int correct( real t,
76  const RealArray & force,
77  const RealArray & torque,
78  const RealArray & A11, const RealArray & A12, const RealArray & A21, const RealArray & A22,
79  RealArray & xCM,
80  RealArray & rotation );
81 
82  // get from a data base file
83  int get( const GenericDataBase & dir, const aString & name);
84 
85  int getAcceleration( real t, RealArray & vCM ) const;
86 
87  // evaluate the added mass matrices at time t
88  int getAddedMassMatrices( const real t, RealArray & A11 , RealArray & A12 , RealArray & A21, RealArray & A22 ) const;
89 
90  int getAngularAcceleration( real t, RealArray & omegaDot ) const;
91 
92  int getAngularVelocities( real t, RealArray & omega ) const;
93 
94  int getAxesOfInertia( real t, RealArray & axesOfInertia ) const;
95 
96  int getBodyForces( const real t, RealArray & bodyForce, RealArray & bodyTorque ) const;
97 
98  bool getCorrectionHasConverged();
99 
100  // general purpose routine:
101  int getCoordinates( real t,
102  RealArray & xCM = Overture::nullRealArray(),
103  RealArray & vCM = Overture::nullRealArray(),
104  RealArray & rotation = Overture::nullRealArray(),
105  RealArray & omega = Overture::nullRealArray(),
106  RealArray & omegaDot = Overture::nullRealArray(),
107  RealArray & aCM = Overture::nullRealArray(),
108  RealArray & axesOfInertia = Overture::nullRealArray() ) const;
109 
110  real getDensity() const; // return the density, if known. If not known return a negative value.
111 
112  // get exact solution (e.g. for twilightzone forcing)
113  int getExactSolution( const int deriv, const real t, RealArray & xe, RealArray & ve , RealArray & we ) const;
114 
115  // return the force and torque at a given time t
116  int getForce(const real t, RealArray & fv, RealArray & gv ) const;
117 
118  int getInitialConditions(RealArray & x0 = Overture::nullRealArray() ,
119  RealArray & v0 = Overture::nullRealArray() ,
120  RealArray & w0 = Overture::nullRealArray() ,
121  RealArray & axesOfInertia = Overture::nullRealArray() ) const ;
122 
123  real getMass() const;
124 
125  real getMaximumRelativeCorrection();
126 
127  RealArray getMomentsOfInertia() const;
128 
129  int getPosition( real t, RealArray & xCM ) const;
130 
131  // determine the velocity or acceleration of a point p(t) belonging to the body.
132  int getPointAcceleration( real t, const RealArray & p, RealArray & ap) const;
133  int getPointVelocity( real t, const RealArray & p, RealArray & vp) const;
134 
135 
136  // use this next routine to compute the point velocity or acceleration for many points.
137  int getPointTransformationMatrix( real t,
138  RealArray & rDotRt = Overture::nullRealArray(),
139  RealArray & rDotDotRt = Overture::nullRealArray() ) const ;
140 
141  real getTimeStepEstimate() const;
142 
143  // Return the name of the time stepping method as a string.
144  aString getTimeSteppingMethodName() const;
145 
146  int getVelocity( real t, RealArray & vCM ) const;
147 
148  int getRotationMatrix(real t,
149  RealArray & r,
150  RealArray & rDot = Overture::nullRealArray(),
151  RealArray & rDotDot = Overture::nullRealArray() ) const;
152 
153  // Indicate whether added mass matrices will be used (and provided by the user)
154  int includeAddedMass( bool trueOrFalse = true );
155 
156  // integrate to a new time
157  int integrate( real tf,
158  const RealArray & force,
159  const RealArray & torque,
160  real t,
161  RealArray & xCM,
162  RealArray & rotation );
163 
164  // Version of integrate that takes added mass matrices.
165  int integrate( real tf,
166  const RealArray & force,
167  const RealArray & torque,
168  real t,
169  const RealArray & A11, const RealArray & A12, const RealArray & A21, const RealArray & A22,
170  RealArray & xCM,
171  RealArray & rotation );
172 
173  bool massHasBeenInitialized() const;
174 
175  int momentumTransfer( real t, RealArray & v );
176 
177 
178  // put to a data base file
179  int put( GenericDataBase & dir, const aString & name) const;
180 
181  // Reset the rigid body to it's initial state. (e.g. remove saved solutions etc.)
182  int reset();
183 
184  int setInitialCentreOfMass( const RealArray & x0 );
185 
186  int setInitialConditions( real t0=0.,
187  const RealArray & x0 = Overture::nullRealArray() ,
188  const RealArray & v0 = Overture::nullRealArray() ,
189  const RealArray & w0 = Overture::nullRealArray() ,
190  const RealArray & axesOfInertia = Overture::nullRealArray() );
191 
192  void setMass( const real totalMass );
193 
194  // Set the tolerance for the Newton iteration for implicit schemes.
195  int setNewtonTolerance( real tol );
196 
197  // Supply forcing at "negative" times for startup (the fourth-order scheme requires values at t=-dt).
198  int setPastTimeForcing( real t, const RealArray & force, const RealArray & torque );
199  int setPastTimeForcing( real t, const RealArray & force, const RealArray & torque,
200  const RealArray & A11, const RealArray & A12, const RealArray & A21, const RealArray & A22 );
201 
202  int setPositionConstraint( PositionConstraintEnum constraint, const RealArray & values );
203 
204  int setProperties(real totalMass,
205  const RealArray & momentsOfInertia,
206  const int numberOfDimensions = 3 );
207 
208  int setRotationConstraint( RotationConstraintEnum constraint, const RealArray & values );
209 
210  int setTwilightZone( bool trueOrFalse, TwilightZoneTypeEnum type = trigonometricTwilightZone );
211 
212  // choose the time stepping method
213  int setTimeSteppingMethod( const TimeSteppingMethodEnum method, int orderOfAccuracy=defaultOrderOfAccuracy );
214 
215  int update( GenericGraphicsInterface & gi );
216 
217  // Return true if the added mass matrices are being used.
218  bool useAddedMass() const;
219 
220  static int debug; // debug flag
221 
222 
223 
224  protected:
225 
226 
228  {
229  applyConstraintsToPosition=0,
231  applyConstraintsToForces
232  };
233 
234 
235  int applyConstraints( const int step, const ConstraintApplicationEnum option );
236 
237  int buildBodyForceOptionsDialog(DialogData & dialog );
238 
239  // Solve the DIRK equations
240  int dirkImplicitSolve( const real dt, const real aii, const real tc, const RealArray & yv, const RealArray &yv0,
241  RealArray & kv );
242 
243  int getBodyForceOption(const aString & answer, DialogData & dialog );
244 
245  // get forcing (protected routine)
246  int getForceInternal(const real t, RealArray & fv, RealArray & gv, RealArray *pA=NULL ) const;
247 
248  // Take a step with implicit Runge-Kutta.
249  int takeStepImplicitRungeKutta( const real t0, const real dt );
250 
251  // Take a leap frog step: (predictor)
252  int takeStepLeapFrog( const real t0, const real dt );
253 
254  // Take a step with the trapezoidal rule (corrector)
255  int takeStepTrapezoid( const real t0, const real dt );
256 
257  // Take a step of the trapezoidal rule (corrector)
258  // int takeStepTrapezoid( const real t, const real dt );
259 
260  // Take a step with extrapolation (predictor)
261  int takeStepExtrapolation( const real t0, const real dt );
262 
263  real mass; // total mass
264  int numberOfDimensions; // 2 or 3 dimensional problem
265  int current; // position of current solution in arrays.
266  int numberOfSteps; // number of time steps taken
267  int numberSaved; // number of different times we have saved force and moment data.
268  int maximumNumberToSave; // save at most this many previous time values of various quantities
269  int initialConditionsGiven; // keeps track of which initial conditions have been given
270  real density; // In cases where the volume can be determined we can use the density to get the mass.
271 
272  real damping; // for damping oscillations.
273 
274  RealArray mI; // 3 moment of inertia
275 
276  RealArray e, e0; // e(0:2,0:2) : 3 axes of inertia, e0=e at t=0
277 
278  RealArray x, v; // centre of mass: position, velocity
279  RealArray x0, v0, w0; // initial centre of mass, velocity and angular velocity
280 
281  RealArray f, g; // force and torque
282 
283  RealArray w; // angular velocities about axes of inertia
284 
285  RealArray time; // arrays of times for which we know data.
286 
287  RealArray bodyForceCoeff, bodyTorqueCoeff; // coefficients in the body force
288 
289  const Range R; // R=Range(0,2)
290 
293  RealArray constraintValues;
294 
295  // keep track of the convergence of the correction steps (used for light bodies)
299 
303 
307 
310 
311  FILE *logFile;
312 
314 
315  // The database is the new way to hold parameters.
316  mutable DataBase dbase;
317 };
318 
319 
320 #endif