SimbodyMatterSubsystem.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMBODY_MATTER_SUBSYSTEM_H_
00002 #define SimTK_SIMBODY_MATTER_SUBSYSTEM_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTK Simbody(tm)                         *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core biosimulation toolkit originating from      *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2006-8 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors: Paul Mitiguy                                                 *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00035 #include "SimTKcommon.h"
00036 #include "simbody/internal/common.h"
00037 #include "simbody/internal/MobilizedBody.h"
00038 
00039 #include <cassert>
00040 #include <vector>
00041 #include <iostream>
00042 
00043 class SimbodyMatterSubsystemRep;
00044 
00045 namespace SimTK {
00046 
00047 class MobilizedBody;
00048 class MultibodySystem;
00049 class Constraint;
00050 
00122 class SimTK_SIMBODY_EXPORT SimbodyMatterSubsystem : public Subsystem {
00123 public:
00125     SimbodyMatterSubsystem();
00126     explicit SimbodyMatterSubsystem(MultibodySystem&);
00127 
00128     class Subtree; // used for working with a connected subgraph of the MobilizedBody tree
00129     class SubtreeResults;
00130 
00131     // These are the same as the compiler defaults but are handy to
00132     // have around explicitly for debugging.
00133     ~SimbodyMatterSubsystem() {
00134     }
00135     SimbodyMatterSubsystem(const SimbodyMatterSubsystem& ss) : Subsystem(ss) {
00136     }
00137     SimbodyMatterSubsystem& operator=(const SimbodyMatterSubsystem& ss) {
00138         Subsystem::operator=(ss);
00139         return *this;
00140     }
00141     
00143     bool getShowDefaultGeometry() const;
00144 
00146     void setShowDefaultGeometry(bool show);
00147 
00148 
00150         // PAUL'S FRIENDLY INTERFACE //
00152 
00157     Real calcSystemMass(const State& s) const;
00158 
00159 
00165     Vec3 calcSystemMassCenterLocationInGround(const State& s) const;
00166 
00167 
00173     MassProperties calcSystemMassPropertiesInGround(const State& s) const;
00174 
00180     Inertia calcSystemCentralInertiaInGround(const State& s) const;
00181 
00187     Vec3 calcSystemMassCenterVelocityInGround(const State& s) const;
00188 
00194     Vec3 calcSystemMassCenterAccelerationInGround(const State& s) const;
00195 
00202     SpatialVec calcSystemMomentumAboutGroundOrigin(const State& s) const;
00203 
00205         // CONSTRUCTION //
00207 
00208     // Attach new matter using the indicated parent body as the reference
00209     // frame, with the mobilizer and mass properties provided by 'child'.
00210     // We take over ownership of child's representation from the given
00211     // handle, leaving that handle as a reference to our new matter object.
00212     // It is an error if the given handle wasn't the owner of the
00213     // matter representation.
00214     MobilizedBodyIndex   adoptMobilizedBody(MobilizedBodyIndex parent, MobilizedBody& child);
00215     const MobilizedBody& getMobilizedBody(MobilizedBodyIndex) const;
00216     MobilizedBody&       updMobilizedBody(MobilizedBodyIndex);
00217 
00218 
00219     // Note: topology is not marked invalid upon returning a writable reference
00220     // here; that will be done only if a non-const method of the returned MobilizedBody
00221     // is called. That means it is OK to use Ground() to satisfy a const argument;
00222     // it won't have an "invalidate topology" side effect.
00223     const MobilizedBody::Ground& getGround() const;
00224     MobilizedBody::Ground&       updGround();
00225     MobilizedBody::Ground&       Ground() {return updGround();}
00226 
00227     ConstraintIndex   adoptConstraint(Constraint&);
00228     const Constraint& getConstraint(ConstraintIndex) const;
00229     Constraint&       updConstraint(ConstraintIndex);
00230 
00232         // OPERATORS //
00234 
00235         // Operators make use of the State but do not write their results back
00236         // into the State, not even into the State cache.
00237 
00253     void calcAcceleration(const State&,
00254         const Vector&              mobilityForces,
00255         const Vector_<SpatialVec>& bodyForces,
00256         Vector&                    udot,
00257         Vector_<SpatialVec>&       A_GB) const;
00258 
00259 
00266     void calcAccelerationIgnoringConstraints(const State&,
00267         const Vector&              mobilityForces,
00268         const Vector_<SpatialVec>& bodyForces,
00269         Vector&                    udot,
00270         Vector_<SpatialVec>&       A_GB) const;
00271 
00281     void calcMInverseV(const State&,
00282         const Vector&        v,
00283         Vector&              MinvV,
00284         Vector_<SpatialVec>& A_GB) const; // <-- TODO: get rid of this A
00285 
00287     void calcSpatialKinematicsFromInternal(const State&,
00288         const Vector&        v,
00289         Vector_<SpatialVec>& Jv) const;
00290 
00292     void calcInternalGradientFromSpatial(const State&,
00293         const Vector_<SpatialVec>& dEdR,
00294         Vector&                    dEdQ) const; // really Qbar
00295 
00297     Real calcKineticEnergy(const State&) const;
00298 
00304     void calcTreeEquivalentMobilityForces(const State&, 
00305         const Vector_<SpatialVec>& bodyForces,
00306         Vector&                    mobilityForces) const;
00307 
00309     void calcQDot(const State& s,
00310         const Vector& u,
00311         Vector&       qdot) const;
00312 
00314     void calcQDotDot(const State& s,
00315         const Vector& udot,
00316         Vector&       qdotdot) const;
00317 
00324     void multiplyByQMatrix(const State& s, bool transposeMatrix, const Vector& in, Vector& out) const;
00325 
00332     void multiplyByQMatrixInverse(const State& s, bool transposeMatrix, const Vector& in, Vector& out) const;
00333     
00341     void calcMobilizerReactionForces(const State& s, Vector_<SpatialVec>& forces) const;
00342 
00343     // These are available after realizeTopology().
00344 
00353     int getNBodies() const;
00354 
00357     int getNConstraints() const;
00358 
00360     int getNParticles() const;
00361 
00364     int getNMobilities() const; 
00365 
00368     int getTotalQAlloc() const;
00369 
00372     int getTotalMultAlloc() const;
00373 
00379     void setUseEulerAngles(State&, bool) const;
00380     bool getUseEulerAngles  (const State&) const;
00381     int  getNQuaternionsInUse(const State&) const;
00382 
00383     void setMobilizerIsPrescribed(State&, MobilizedBodyIndex, bool) const;
00384     bool isMobilizerPrescribed  (const State&, MobilizedBodyIndex) const;
00385     bool isUsingQuaternion(const State&, MobilizedBodyIndex) const;
00386     QuaternionPoolIndex getQuaternionPoolIndex(const State&, MobilizedBodyIndex) const;
00387     AnglePoolIndex      getAnglePoolIndex(const State&, MobilizedBodyIndex) const;
00388     void setConstraintIsDisabled(State&, ConstraintIndex constraint, bool) const;
00389     bool isConstraintDisabled(const State&, ConstraintIndex constraint) const;
00390     
00393     void convertToEulerAngles(const State& inputState, State& outputState) const;
00394     
00397     void convertToQuaternions(const State& inputState, State& outputState) const; 
00398 
00399     // Dynamics stage responses.
00400 
00401     // Cross joint
00402     const SpatialVec& getCoriolisAcceleration(const State&, MobilizedBodyIndex) const;
00403 
00404     // Including parent
00405     const SpatialVec& getTotalCoriolisAcceleration(const State&, MobilizedBodyIndex) const;
00406 
00407     const SpatialVec& getGyroscopicForce(const State&, MobilizedBodyIndex) const;
00408     const SpatialVec& getCentrifugalForces(const State&, MobilizedBodyIndex) const;
00409     const SpatialMat& getArticulatedBodyInertia(const State& s, MobilizedBodyIndex) const;
00410 
00411         // PARTICLES
00412         // TODO: not currently implemented. Use a point mass with a Cartesian (translation)
00413         // mobilizer to Ground instead. The idea here would be to special-case particles
00414         // to make them faster; there would be no additional functionality.
00415 
00416     // The generalized coordinates for a particle are always the three measure numbers
00417     // (x,y,z) of the particle's Ground-relative Cartesian location vector. The generalized
00418     // speeds are always the three corresponding measure numbers of the particle's
00419     // Ground-relative Cartesian velocity. The generalized applied forces are
00420     // always the three measure numbers of a Ground-relative force vector.
00421     const Vector_<Vec3>& getAllParticleLocations    (const State&) const;
00422     const Vector_<Vec3>& getAllParticleVelocities   (const State&) const;
00423 
00424     const Vec3& getParticleLocation(const State& s, ParticleIndex p) const {
00425         return getAllParticleLocations(s)[p];
00426     }
00427     const Vec3& getParticleVelocity(const State& s, ParticleIndex p) const {
00428         return getAllParticleVelocities(s)[p];
00429     }
00430 
00431     Vector& updAllParticleMasses(State& s) const;
00432 
00433     void setAllParticleMasses(State& s, const Vector& masses) const {
00434         updAllParticleMasses(s) = masses;
00435     }
00436 
00437     // Note that particle generalized coordinates, speeds, and applied forces
00438     // are defined to be the particle Cartesian locations, velocities, and
00439     // applied force vectors, so can be set directly at Stage::Model or higher.
00440 
00441     // These are the only routines that must be provided by the concrete MatterSubsystem.
00442     Vector_<Vec3>& updAllParticleLocations(State&)     const;
00443     Vector_<Vec3>& updAllParticleVelocities(State&)    const;
00444 
00445     // The following inline routines are provided by the generic MatterSubsystem class
00446     // for convenience.
00447 
00448     Vec3& updParticleLocation(State& s, ParticleIndex p) const {
00449         return updAllParticleLocations(s)[p];
00450     }
00451     Vec3& updParticleVelocity(State& s, ParticleIndex p) const {
00452         return updAllParticleVelocities(s)[p];
00453     }
00454 
00455     void setParticleLocation(State& s, ParticleIndex p, const Vec3& r) const {
00456         updAllParticleLocations(s)[p] = r;
00457     }
00458     void setParticleVelocity(State& s, ParticleIndex p, const Vec3& v) const {
00459         updAllParticleVelocities(s)[p] = v;
00460     }
00461 
00462     void setAllParticleLocations(State& s, const Vector_<Vec3>& r) const {
00463         updAllParticleLocations(s) = r;
00464     }
00465     void setAllParticleVelocities(State& s, const Vector_<Vec3>& v) const {
00466         updAllParticleVelocities(s) = v;
00467     }
00468 
00470     const Vector& getAllParticleMasses(const State&) const;
00471 
00472     const Vector_<Vec3>& getAllParticleAccelerations(const State&) const;
00473 
00474     const Vec3& getParticleAcceleration(const State& s, ParticleIndex p) const {
00475         return getAllParticleAccelerations(s)[p];
00476     }
00477 
00478         // POSITION STAGE responses //
00479 
00480         // POSITION STAGE operators //
00481 
00485     void addInStationForce(const State&, MobilizedBodyIndex bodyB, const Vec3& stationOnB, 
00486                            const Vec3& forceInG, Vector_<SpatialVec>& bodyForcesInG) const;
00487 
00490     void addInBodyTorque(const State&, MobilizedBodyIndex, const Vec3& torqueInG, 
00491                          Vector_<SpatialVec>& bodyForcesInG) const;
00492 
00495     void addInMobilityForce(const State&, MobilizedBodyIndex, MobilizerUIndex which, Real f,
00496                             Vector& mobilityForces) const;
00497 
00498         // POSITION STAGE solvers //
00499 
00505     bool projectQConstraints(State& s, Real consAccuracy, const Vector& yWeights,
00506                              const Vector& ooTols, Vector& yErrest, System::ProjectOptions) const;
00507 
00508         // VELOCITY STAGE responses //
00509 
00510         // VELOCITY STAGE operators //
00511 
00512         // VELOCITY STAGE solvers //
00513 
00519     bool projectUConstraints(State& s, Real consAccuracy, const Vector& yWeights,
00520                              const Vector& ooTols, Vector& yErrest, System::ProjectOptions) const;
00521 
00522         // ACCELERATION STAGE reponses
00523 
00524 
00525         // Bookkeeping
00526     SimTK_PIMPL_DOWNCAST(SimbodyMatterSubsystem, Subsystem);
00527     const SimbodyMatterSubsystemRep& getRep() const;
00528     SimbodyMatterSubsystemRep&       updRep();
00529 };
00530 
00531 SimTK_SIMBODY_EXPORT std::ostream& 
00532 operator<<(std::ostream&, const SimbodyMatterSubsystem&);
00533 
00534 
00535 } // namespace SimTK
00536 
00537 #endif // SimTK_SIMBODY_MATTER_SUBSYSTEM_H_

Generated on Fri Sep 26 07:44:16 2008 for SimTKcore by  doxygen 1.5.6