MassCenterMotionRemover.h

Go to the documentation of this file.
00001 #ifndef SimTK_MASS_CENTER_MOTION_REMOVER_H_
00002 #define SimTK_MASS_CENTER_MOTION_REMOVER_H_
00003 /* -------------------------------------------------------------------------- *
00004  *                      SimTK Core: SimTK Simbody(tm)                         *
00005  * -------------------------------------------------------------------------- *
00006  * This is part of the SimTK Core biosimulation toolkit originating from      *
00007  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00008  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00009  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00010  *                                                                            *
00011  * Portions copyright (c) 2009 Stanford University and the Authors.           *
00012  * Authors: Michael Sherman                                                   *
00013  * Contributors:                                                              *
00014  *                                                                            *
00015  * Permission is hereby granted, free of charge, to any person obtaining a    *
00016  * copy of this software and associated documentation files (the "Software"), *
00017  * to deal in the Software without restriction, including without limitation  *
00018  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00019  * and/or sell copies of the Software, and to permit persons to whom the      *
00020  * Software is furnished to do so, subject to the following conditions:       *
00021  *                                                                            *
00022  * The above copyright notice and this permission notice shall be included in *
00023  * all copies or substantial portions of the Software.                        *
00024  *                                                                            *
00025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00026  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00027  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00028  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00029  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00030  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00031  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00032  * -------------------------------------------------------------------------- */
00033 
00034 #include "SimTKsimbody.h"
00035 #include "molmodel/internal/common.h"
00036 #include "molmodel/internal/MolecularMechanicsSystem.h"
00037 
00038 namespace SimTK {
00039 
00050 class MassCenterMotionRemover 
00051 :   public PeriodicEventHandler {
00052 public:
00069     explicit MassCenterMotionRemover(const MolecularMechanicsSystem& system, 
00070                             Real adjustmentInterval = 1)   // ps
00071     :   PeriodicEventHandler(adjustmentInterval),
00072         system(system), linearMomOnly(false), 
00073         enableCOMCorrection(false), comTol(1), comPos(0)
00074     {
00075     }
00076 
00088     MassCenterMotionRemover& disableAngularMomentumRemoval(bool disable) 
00089     {   linearMomOnly = disable; return *this; }
00095     bool isAngularMomentumRemovalDisabled() const {return linearMomOnly;}
00096 
00112     MassCenterMotionRemover& enableMassCenterCorrection(bool enabled) 
00113     {   enableCOMCorrection = enabled; return *this; }
00119     bool isMassCenterCorrectionEnabled() const {return enableCOMCorrection;}
00120 
00136     MassCenterMotionRemover& 
00137     setDesiredMassCenterLocation(const Vec3& groundLocationInNm) 
00138     {   comPos = groundLocationInNm; return *this; }
00144     const Vec3& getDesiredMassCenterLocation() const {return comPos;}
00145 
00164     MassCenterMotionRemover& 
00165     setMassCenterLocationTolerance(Real toleranceInNm) {
00166         SimTK_APIARGCHECK1_ALWAYS(toleranceInNm > 0, 
00167             "MassCenterMotionRemover","setMassCenterLocationTolerance", 
00168             "Illegal mass center tolerance %g.", toleranceInNm);
00169         comTol = toleranceInNm; 
00170         return *this; 
00171     }
00177     Real getMassCenterLocationTolerance() const {return comTol;}
00178 
00187     void removeSystemMomentum(State& state) const
00188     {   system.removeSystemRigidBodyMomentum(state, linearMomOnly); }
00189 
00199     void correctMassCenterLocation(State& state) const 
00200     {   system.moveSystemMassCenter(state, comPos); }
00201 
00214     Real calcMassCenterError(const State& state) const
00215     {   return (system.calcSystemMassCenterLocation(state) - comPos).norm(); }
00216 
00217     // This is the concrete implementation of the handleEvent() virtual
00218     // method required by every EventHandler; don't call this directly,
00219     // use removeSystemMomentum() or correctMassCenterLocation() instead.
00221     void handleEvent(State& state, Real accuracy, 
00222                      const Vector& yWeights, const Vector& ooConstraintTols, 
00223                      Stage& lowestModified, bool& shouldTerminate) const
00224     {
00225         system.realize(state, Stage::Velocity);
00226         removeSystemMomentum(state); 
00227         lowestModified = Stage::Velocity; // tell integrator what we changed
00228         if (enableCOMCorrection && calcMassCenterError(state) > comTol) {
00229             correctMassCenterLocation(state);
00230             lowestModified = Stage::Position;
00231         }
00232     }
00234 
00235 private:
00236     const MolecularMechanicsSystem& system;
00237     bool    linearMomOnly; // should we correct angular momentum?
00238     bool    enableCOMCorrection; // should we relocate the COM?
00239     Real    comTol;    // how far to let the COM drift (nm).
00240     Vec3    comPos;    // where the COM is expected to stay.
00241 };
00242 
00243 } // namespace SimTK
00244 
00245 #endif // SimTK_MASS_CENTER_MOTION_REMOVER_H_

Generated on Thu Aug 12 16:37:07 2010 for SimTKcore by  doxygen 1.6.1