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