Simbody

common.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMBODY_COMMON_H_
00002 #define SimTK_SIMBODY_COMMON_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                             SimTK Simbody(tm)                              *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK 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) 2005-11 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
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 
00039 #include "SimTKcommon.h"
00040 
00041 #include <cassert>
00042 #include <vector>
00043 #include <limits>
00044 
00045 
00046 // Shared libraries are messy in Visual Studio. We have to distinguish three
00047 // cases:
00048 //   (1) this header is being used to build the simbody shared library (dllexport)
00049 //   (2) this header is being used by a *client* of the simbody shared
00050 //       library (dllimport)
00051 //   (3) we are building the simbody static library, or the client is
00052 //       being compiled with the expectation of linking with the
00053 //       simbody static library (nothing special needed)
00054 // In the CMake script for building this library, we define one of the symbols
00055 //     SimTK_SIMBODY_BUILDING_{SHARED|STATIC}_LIBRARY
00056 // Client code normally has no special symbol defined, in which case we'll
00057 // assume it wants to use the shared library. However, if the client defines
00058 // the symbol SimTK_USE_STATIC_LIBRARIES we'll suppress the dllimport so
00059 // that the client code can be linked with static libraries. Note that
00060 // the client symbol is not library dependent, while the library symbols
00061 // affect only the simbody library, meaning that other libraries can
00062 // be clients of this one. However, we are assuming all-static or all-shared.
00063 
00064 #ifdef _WIN32
00065     #ifdef _MSC_VER
00066     #pragma warning(disable:4231) // need to use 'extern' template explicit instantiation
00067     #endif
00068     #if defined(SimTK_SIMBODY_BUILDING_SHARED_LIBRARY)
00069         #define SimTK_SIMBODY_EXPORT __declspec(dllexport)
00070         // Keep MS VC++ quiet when it tries to instantiate incomplete template classes in a DLL.
00071         #ifdef _MSC_VER
00072         #pragma warning(disable:4661)
00073         #endif
00074     #elif defined(SimTK_SIMBODY_BUILDING_STATIC_LIBRARY) || defined(SimTK_USE_STATIC_LIBRARIES)
00075         #define SimTK_SIMBODY_EXPORT
00076     #else
00077         #define SimTK_SIMBODY_EXPORT __declspec(dllimport)   // i.e., a client of a shared library
00078     #endif
00079 #else
00080     #define SimTK_SIMBODY_EXPORT // Linux, Mac
00081 #endif
00082 
00083 // Every SimTK library must provide these two routines, with the library
00084 // name appearing after the "version_" and "about_".
00085 extern "C" {
00086     SimTK_SIMBODY_EXPORT void SimTK_version_simbody(int* major, int* minor, int* build);
00087     SimTK_SIMBODY_EXPORT void SimTK_about_simbody(const char* key, int maxlen, char* value);
00088 }
00089 
00090 namespace SimTK {
00091     
00092     // MATTER SUBSYSTEM-GLOBAL INDEX TYPES
00093 
00094 
00103 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MobilizedBodyIndex);
00104 
00108 typedef MobilizedBodyIndex MobodIndex;
00109 
00113 static const MobilizedBodyIndex GroundIndex(0);
00114 
00119 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstraintIndex);
00120 
00121 // TODO: This is for arrays indexed by MatterSubsystem-global ParticleIndex, as yet to be defined.
00122 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ParticleIndex);
00123 
00124 // Constrained Bodies in constraints where the Ancestor body is not Ground (we call
00125 // these "Ancestor Constrained Bodies") require some additional cached data, such as
00126 // their orientations and velocities in the Ancestor frame, so are each
00127 // allocated a slot in pools of that data. Those pools are indexed by this type.
00128 SimTK_DEFINE_UNIQUE_INDEX_TYPE(AncestorConstrainedBodyPoolIndex);
00129 
00130 // This is for "u-squared" arrays, that is, arrays which allocate space for an nuXnu block
00131 // for each MobilizedBody.
00132 SimTK_DEFINE_UNIQUE_INDEX_TYPE(USquaredIndex);
00133 
00134 // This is for "quaternion information" arrays, which have total dimension equal to the
00135 // number of quaternions currently in use as generalized coordinates for modeling the Matter
00136 // Subsystem's MobilizedBodies. Primarily this is for storing the norm of quaternions so we need
00137 // calculate them only once.
00138 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QuaternionPoolIndex);
00139 
00140 // This is for "angle information" arrays, which have dimension equal to the total number
00141 // of generalized coordinates currently in use which are just angles in radians. For those
00142 // we want to precalculate some expensive things like sine and cosine so that we can just
00143 // do that once.
00144 SimTK_DEFINE_UNIQUE_INDEX_TYPE(AnglePoolIndex);
00145 
00146 // These are for indexing the pools of prescribed q's, u's, udots, and calculated forces
00147 // needed to produce the udots. The arrays are allocated in order of MobilizedBodyIndex, and
00148 // then in q and u order within the mobilizer. A mobilier with prescribed positions q gets
00149 // slots in the u and udot pools also to hold derivatives, and similarly if it is the
00150 // velocities u that are prescribed there will be slots in the udot pools. Note that
00151 // the Q index can be used to index qdot and qdotdot arrays if needed. Note that a 
00152 // prescribed force is produced whenever there is a udot that is not force driven; that
00153 // includes prescribed udots but also zero and discrete ones.
00154 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresQPoolIndex);
00155 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresUPoolIndex);
00156 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresUDotPoolIndex);
00157 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresForcePoolIndex);
00158 
00159     // PER-MOBILIZER INDEX TYPES
00160 
00167 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MobilizerQIndex);
00174 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MobilizerUIndex);
00175 
00176     // PER-CONSTRAINT INDEX TYPES
00177     
00178 // This is the Constraint-specific index of the MobilizedBodies which are *directly* affected
00179 // by a constraint, through body forces or body torques on these bodies.
00180 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedBodyIndex);
00181 
00182 // This is the Constraint-specific index of the MobilizedBodies whose mobilizers' mobilities
00183 // can appear explicitly in constraint equations, and which are acted upon by the Constraint
00184 // through generation of generalized (mobility) forces. Note that for a multi-dof mobilizer
00185 // we don't select individual mobilities; it is all or nothing so we can use the MobilizedBody
00186 // to stand for its mobilizer.
00187 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedMobilizerIndex);
00188 
00189 // This is the Constraint-specific index of a coordinate q which can be *directly* affected
00190 // by this constraint through generation of a mobility force on a corresponding mobility. These
00191 // are numbered in order of ConstrainedMobilizerIndex for the mobilizers for which these are the q's.
00192 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedQIndex);
00193 
00194 // This is the Constraint-specific index of a mobility u which can be *directly* affected
00195 // by this constraint through generation of a mobility force. These are numbered in order
00196 // of ConstrainedMobilizerIndex for the bodies for which these are the u's.
00197 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedUIndex);
00198 
00199 
00200 // This is the Constraint-specific index of a coordinate q which can be involved in any
00201 // constraint equation of this constraint, either directly through ConstrainedMobilizers
00202 // or indirectly as a result of its effects on ConstrainedBodies (that is, this list
00203 // includes all the ConstraintQIndex entries above, plus possibly many more). These are in sorted
00204 // order by subsystem-wide QIndex, and each QIndex appears at most once.
00205 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ParticipatingQIndex);
00206 
00207 // This is the Constraint-specific index of a coordinate u which can be involved in any
00208 // constraint equation of this constraint, either directly through ConstrainedMobilizers
00209 // or indirectly as a result of its effects on ConstrainedBodies (that is, this list
00210 // includes all the ConstraintUIndex entries above, plus possibly many more). These are in sorted
00211 // order by subsystem-wide UIndex, and each UIndex appears at most once.
00212 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ParticipatingUIndex);
00213 
00214     // SUBTREE INDEX TYPES
00215 
00216 // And similarly for other unique Index types.
00217 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubtreeBodyIndex);
00218 static const SubtreeBodyIndex SubtreeAncestorIndex(0);
00219 
00220 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubtreeQIndex);
00221 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubtreeUIndex);
00222 
00223     // INDEX TYPES FOR OTHER SUBSYSTEMS
00224 
00229 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ForceIndex);
00230 
00231 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactSetIndex);
00232 
00233 
00234 namespace Exception {
00235 
00236 
00237 class APIMethodFailed : public Base {
00238 public:
00239     APIMethodFailed(const char* fn, int ln, String method, String cause) : Base(fn,ln)
00240     {
00241         setMessage(method + " failed because:\n  " + cause);
00242     }
00243 };
00244 
00245 
00246 // This just reports rep-level bad things up to the API level with a helpful string.
00247 class RepLevelException : public Base {
00248 public:
00249     RepLevelException(const char* fn, int ln, String message) : Base(fn,ln)
00250     {
00251         setMessage(message);
00252     }
00253 };
00254 
00255 class MobilizerCantExactlyRepresentRequestedQuantity : public Base {
00256 public:
00257     MobilizerCantExactlyRepresentRequestedQuantity(const char* fn, int ln, 
00258        String method, MobilizedBodyIndex body, String quantity) : Base(fn,ln)
00259     {
00260         setMessage(method + "(): the mobilizer for body " + String((int)body)
00261             + " can't represent the given " + quantity + " to machine precision");
00262     }
00263 private:
00264 };
00265 
00266 class NewtonRaphsonFailure : public Base {
00267 public:
00268     NewtonRaphsonFailure(const char* fn, int ln, String msg) : Base(fn,ln)
00269     {
00270         setMessage("NewtonRaphson failure: " + msg);
00271     }
00272 private:
00273 };
00274 
00275 class LoopConstraintConstructionFailure : public Base {
00276 public:
00277     LoopConstraintConstructionFailure(const char* fn, int ln, String msg) : Base(fn,ln)
00278     {
00279         setMessage("Loop constraint construction failure: " + msg);
00280     }
00281 private:
00282 };
00283 
00284 } // namespace SimTK::Exception
00285 
00286 
00287 
00288 } // namespace SimTK
00289 
00290 #endif // SimTK_SIMBODY_COMMON_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines