Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Simbody/include/simbody/internal/common.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMBODY_COMMON_H_
2 #define SimTK_SIMBODY_COMMON_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm) *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
31 #include "SimTKcommon.h"
32 
33 #include <cassert>
34 #include <vector>
35 #include <limits>
36 
37 
38 // Shared libraries are messy in Visual Studio. We have to distinguish three
39 // cases:
40 // (1) this header is being used to build the simbody shared library (dllexport)
41 // (2) this header is being used by a *client* of the simbody shared
42 // library (dllimport)
43 // (3) we are building the simbody static library, or the client is
44 // being compiled with the expectation of linking with the
45 // simbody static library (nothing special needed)
46 // In the CMake script for building this library, we define one of the symbols
47 // SimTK_SIMBODY_BUILDING_{SHARED|STATIC}_LIBRARY
48 // Client code normally has no special symbol defined, in which case we'll
49 // assume it wants to use the shared library. However, if the client defines
50 // the symbol SimTK_USE_STATIC_LIBRARIES we'll suppress the dllimport so
51 // that the client code can be linked with static libraries. Note that
52 // the client symbol is not library dependent, while the library symbols
53 // affect only the simbody library, meaning that other libraries can
54 // be clients of this one. However, we are assuming all-static or all-shared.
55 
56 #ifdef _WIN32
57  #ifdef _MSC_VER
58  #pragma warning(disable:4231) // need to use 'extern' template explicit instantiation
59  #endif
60  #if defined(SimTK_SIMBODY_BUILDING_SHARED_LIBRARY)
61  #define SimTK_SIMBODY_EXPORT __declspec(dllexport)
62  // Keep MS VC++ quiet when it tries to instantiate incomplete template classes in a DLL.
63  #ifdef _MSC_VER
64  #pragma warning(disable:4661)
65  #endif
66  #elif defined(SimTK_SIMBODY_BUILDING_STATIC_LIBRARY) || defined(SimTK_USE_STATIC_LIBRARIES)
67  #define SimTK_SIMBODY_EXPORT
68  #else
69  #define SimTK_SIMBODY_EXPORT __declspec(dllimport) // i.e., a client of a shared library
70  #endif
71 #else
72  #define SimTK_SIMBODY_EXPORT // Linux, Mac
73 #endif
74 
75 // Every SimTK library must provide these two routines, with the library
76 // name appearing after the "version_" and "about_".
77 extern "C" {
78  SimTK_SIMBODY_EXPORT void SimTK_version_simbody(int* major, int* minor, int* build);
79  SimTK_SIMBODY_EXPORT void SimTK_about_simbody(const char* key, int maxlen, char* value);
80 }
81 
82 namespace SimTK {
83 
84  // MATTER SUBSYSTEM-GLOBAL INDEX TYPES
85 
86 
95 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MobilizedBodyIndex);
96 
101 
105 static const MobilizedBodyIndex GroundIndex(0);
106 
113 
114 // TODO: This is for arrays indexed by MatterSubsystem-global ParticleIndex,
115 // as yet to be defined.
116 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ParticleIndex);
117 
118 // Constrained Bodies in constraints where the Ancestor body is not Ground (we
119 // call these "Ancestor Constrained Bodies") require some additional cached
120 // data, such as their orientations and velocities in the Ancestor frame, so
121 // are each allocated a slot in pools of that data. Those pools are indexed by
122 // this type.
123 SimTK_DEFINE_UNIQUE_INDEX_TYPE(AncestorConstrainedBodyPoolIndex);
124 
125 // This is for "u-squared" arrays, that is, arrays which allocate space for an
126 // nuXnu block for each MobilizedBody.
127 SimTK_DEFINE_UNIQUE_INDEX_TYPE(USquaredIndex);
128 
129 // This is for "quaternion information" arrays, which have total dimension
130 // equal to the number of quaternions currently in use as generalized
131 // coordinates for modeling the Matter Subsystem's MobilizedBodies. Primarily
132 // this is for storing the norm of quaternions so we need calculate them only
133 // once.
134 SimTK_DEFINE_UNIQUE_INDEX_TYPE(QuaternionPoolIndex);
135 
136 // This is for miscellaneous Real-valued position cache data that individual
137 // mobilizers ask us to hold for generalized coordinate q precalculations
138 // (e.g. sines and cosines).
139 SimTK_DEFINE_UNIQUE_INDEX_TYPE(MobodQPoolIndex);
140 
141 // These are for indexing the pools of prescribed q's, u's, udots, and calculated forces
142 // needed to produce the udots. The arrays are allocated in order of MobilizedBodyIndex, and
143 // then in q and u order within the mobilizer. A mobilier with prescribed positions q gets
144 // slots in the u and udot pools also to hold derivatives, and similarly if it is the
145 // velocities u that are prescribed there will be slots in the udot pools. Note that
146 // the Q index can be used to index qdot and qdotdot arrays if needed. Note that a
147 // prescribed force is produced whenever there is a udot that is not force driven; that
148 // includes prescribed udots but also zero and discrete ones.
149 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresQPoolIndex);
150 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresUPoolIndex);
151 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresUDotPoolIndex);
152 SimTK_DEFINE_UNIQUE_INDEX_TYPE(PresForcePoolIndex);
153 
154  // PER-MOBILIZER INDEX TYPES
155 
170 
171  // PER-CONSTRAINT INDEX TYPES
172 
173 // This is the Constraint-specific index of the MobilizedBodies which are *directly* affected
174 // by a constraint, through body forces or body torques on these bodies.
175 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedBodyIndex);
176 
177 // This is the Constraint-specific index of the MobilizedBodies whose mobilizers' mobilities
178 // can appear explicitly in constraint equations, and which are acted upon by the Constraint
179 // through generation of generalized (mobility) forces. Note that for a multi-dof mobilizer
180 // we don't select individual mobilities; it is all or nothing so we can use the MobilizedBody
181 // to stand for its mobilizer.
182 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedMobilizerIndex);
183 
184 // This is the Constraint-specific index of a coordinate q which can be *directly* affected
185 // by this constraint through generation of a mobility force on a corresponding mobility. These
186 // are numbered in order of ConstrainedMobilizerIndex for the mobilizers for which these are the q's.
187 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedQIndex);
188 
189 // This is the Constraint-specific index of a mobility u which can be *directly* affected
190 // by this constraint through generation of a mobility force. These are numbered in order
191 // of ConstrainedMobilizerIndex for the bodies for which these are the u's.
192 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ConstrainedUIndex);
193 
194 
195 // This is the Constraint-specific index of a coordinate q which can be involved in any
196 // constraint equation of this constraint, either directly through ConstrainedMobilizers
197 // or indirectly as a result of its effects on ConstrainedBodies (that is, this list
198 // includes all the ConstraintQIndex entries above, plus possibly many more). These are in sorted
199 // order by subsystem-wide QIndex, and each QIndex appears at most once.
200 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ParticipatingQIndex);
201 
202 // This is the Constraint-specific index of a coordinate u which can be involved in any
203 // constraint equation of this constraint, either directly through ConstrainedMobilizers
204 // or indirectly as a result of its effects on ConstrainedBodies (that is, this list
205 // includes all the ConstraintUIndex entries above, plus possibly many more). These are in sorted
206 // order by subsystem-wide UIndex, and each UIndex appears at most once.
207 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ParticipatingUIndex);
208 
209  // SUBTREE INDEX TYPES
210 
211 // And similarly for other unique Index types.
212 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubtreeBodyIndex);
213 static const SubtreeBodyIndex SubtreeAncestorIndex(0);
214 
215 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubtreeQIndex);
216 SimTK_DEFINE_UNIQUE_INDEX_TYPE(SubtreeUIndex);
217 
218  // INDEX TYPES FOR OTHER SUBSYSTEMS
219 
225 
226 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactSetIndex);
227 
228 
229 namespace Exception {
230 
231 
232 class APIMethodFailed : public Base {
233 public:
234  APIMethodFailed(const char* fn, int ln, String method, String cause) : Base(fn,ln)
235  {
236  setMessage(method + " failed because:\n " + cause);
237  }
238 };
239 
240 
241 // This just reports rep-level bad things up to the API level with a helpful string.
242 class RepLevelException : public Base {
243 public:
244  RepLevelException(const char* fn, int ln, String message) : Base(fn,ln)
245  {
246  setMessage(message);
247  }
248 };
249 
251 public:
253  String method, MobilizedBodyIndex body, String quantity) : Base(fn,ln)
254  {
255  setMessage(method + "(): the mobilizer for body " + String((int)body)
256  + " can't represent the given " + quantity + " to machine precision");
257  }
258 private:
259 };
260 
261 class NewtonRaphsonFailure : public Base {
262 public:
263  NewtonRaphsonFailure(const char* fn, int ln, String msg) : Base(fn,ln)
264  {
265  setMessage("NewtonRaphson failure: " + msg);
266  }
267 private:
268 };
269 
271 public:
272  LoopConstraintConstructionFailure(const char* fn, int ln, String msg) : Base(fn,ln)
273  {
274  setMessage("Loop constraint construction failure: " + msg);
275  }
276 private:
277 };
278 
279 } // namespace SimTK::Exception
280 
281 
282 
283 } // namespace SimTK
284 
285 #endif // SimTK_SIMBODY_COMMON_H_
LoopConstraintConstructionFailure(const char *fn, int ln, String msg)
Definition: Simbody/include/simbody/internal/common.h:272
This is for arrays indexed by mobilized body number within a subsystem (typically the SimbodyMatterSu...
SimTK_DEFINE_UNIQUE_INDEX_TYPE(AssemblyConditionIndex)
Definition: Simbody/include/simbody/internal/common.h:270
static const SubtreeBodyIndex SubtreeAncestorIndex(0)
Definition: Simbody/include/simbody/internal/common.h:232
void SimTK_version_simbody(int *major, int *minor, int *build)
void setMessage(const std::string &msgin)
Definition: Exception.h:56
static const MobilizedBodyIndex GroundIndex(0)
This is the MobilizedBodyIndex corresponding to the unique Ground body; its index is always zero...
APIMethodFailed(const char *fn, int ln, String method, String cause)
Definition: Simbody/include/simbody/internal/common.h:234
Definition: Simbody/include/simbody/internal/common.h:242
Definition: Simbody/include/simbody/internal/common.h:261
This is for arrays indexed by constraint number within a subsystem (typically the SimbodyMatterSubsys...
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
This type represents the index of a Force element within its subsystem.
NewtonRaphsonFailure(const char *fn, int ln, String msg)
Definition: Simbody/include/simbody/internal/common.h:263
Definition: Exception.h:45
MobilizedBodyIndex MobodIndex
This is the approved abbeviation for MobilizedBodyIndex. Feel free to use it if you get tired of typi...
Definition: Simbody/include/simbody/internal/common.h:100
void SimTK_about_simbody(const char *key, int maxlen, char *value)
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
MobilizerCantExactlyRepresentRequestedQuantity(const char *fn, int ln, String method, MobilizedBodyIndex body, String quantity)
Definition: Simbody/include/simbody/internal/common.h:252
#define SimTK_SIMBODY_EXPORT
Definition: Simbody/include/simbody/internal/common.h:72
The Mobilizer associated with each MobilizedBody, once modeled, has a specific number of generalized ...
RepLevelException(const char *fn, int ln, String message)
Definition: Simbody/include/simbody/internal/common.h:244
Definition: Simbody/include/simbody/internal/common.h:250
The Mobilizer associated with each MobilizedBody, once modeled, has a specific number of generalized ...